-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontroller.cpp
579 lines (495 loc) · 20.3 KB
/
controller.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
#include "controller.h"
#include "bash_process_manager.h"
#include "table_userid.h"
#include "qevent.h"
#include <iostream>
#include <fstream>
#include <QProcess>
#include <QFile>
#include <qdatetime.h>
#include <QMetaObject>
#include <unistd.h>
#include <csignal>
#include <sys/stat.h>
Controller::Controller(QObject *parent) : QObject(parent)
{
slurm_process = new Bash_Process_Manager;
QObject::connect(slurm_process, &Bash_Process_Manager::slurmIdReady, this, &Controller::getSlurmID);
QObject::connect(slurm_process, &Bash_Process_Manager::slurm_status_changed, this, &Controller::slurm_status_changed);
QObject::connect(slurm_process, &Bash_Process_Manager::slotEndTime, this, &Controller::slotEndTime);
m_connectionName = "mainConnection";
}
void Controller::connect(QString hostname, QString databasename, int port, QString username, QString password){
char tempFileTemplate[] = "/tmp/XXXXXX.env";
std::cout << "Temp file template: " << tempFileTemplate << std::endl;
int fd = mkstemps(tempFileTemplate, 4);
m_envFilePath = tempFileTemplate;
if (fd == -1) {
std::cerr << "Failed to create temporary file: " << strerror(errno) << std::endl;
return;
}
close(fd);
std::cout << "Temporary file created: " << m_envFilePath << std::endl;
std::ofstream tempFile(m_envFilePath, std::ofstream::trunc);
if (!tempFile.is_open()) {
std::cerr << "Failed to open temporary file for writing: " << strerror(errno) << std::endl;
return;
}
tempFile << "DB_HOST=" << hostname.toStdString() << "\n";
tempFile << "DB_NAME=" << databasename.toStdString() << "\n";
tempFile << "DB_PORT=" << port << "\n";
tempFile << "DB_USER=" << username.toStdString() << "\n";
tempFile << "DB_PW=" << password.toStdString() << "\n";
bool connect = connectToDB(hostname, databasename, port, username, password);
//Hier stand vorher nur True, kann sein, dass das gebraucht wird!
m_connection_ready = connect;
m_job_table = new Table_UserID();
if(!m_cluster_ident.isEmpty()){
m_job_table->loadJobs(m_cluster_ident);
}
//m_job_table->setDatabaseConnection(m_dbConnection);
emit connectionSignal(connect);
}
Controller::~Controller()
{
}
bool Controller::copyEnvFile(){
char tempFileTemplate[] = "/tmp/XXXXXX.env";
int fd = mkstemps(tempFileTemplate, 4);
std::string temp_string = tempFileTemplate;
if (fd == -1) {
std::cerr << "Failed to create temporary file: " << strerror(errno) << std::endl;
return false;
}
close(fd);
QFile sourceFile(QString::fromStdString(m_envFilePath));
QFile destFile(QString::fromStdString(temp_string));
if (!destFile.open(QIODevice::WriteOnly)) {
qWarning() << "Failed to open destination file:" << temp_string;
return false;
}
if (!sourceFile.open(QIODevice::ReadOnly)) {
qWarning() << "Failed to open source file:" << m_envFilePath;
return false;
}
QByteArray fileData = sourceFile.readAll();
destFile.write(fileData);
if (!m_envFilePath.empty()) {
if (unlink(m_envFilePath.c_str()) != 0) {
std::cerr << "Failed to delete temporary file" << std::endl;
} else {
std::cout << "Temporary .env file deleted" << std::endl;
}
}
m_envFilePath = temp_string;
return true;
}
void Controller::copyOutputFile(){
QtConcurrent::run([this]() {
QProcess proc;
const char *homeDir = getenv("HOME");
QString filePath = QString(homeDir);
QString sshCommand = QString("scp %1@%2:%3/slurm-%4.out %5").arg(m_cluster_ident, m_cluster_address, m_remote_dir_bash, QString::number(m_slurm_id), filePath);
std::cout << sshCommand.toStdString() << std::endl;
proc.start("bash", QStringList() << "-c" << sshCommand);
// Warten, bis der Prozess gestartet ist
if (!proc.waitForStarted()) {
proc.kill();
}
// Warten, bis der Prozess beendet ist
if (!proc.waitForFinished()) {
proc.kill();
}
QString outputPath = filePath + "/slurm-" + QString::number(m_slurm_id) + ".out";
emit copiedOutputFile(outputPath);
});
}
QString Controller::connectCluster(const QString &address, const QString &ident, const QString &path){
//QString Controller::connectCluster(QString &address, QString &ident, QString &path){
m_cluster_address = address;
m_cluster_ident = ident;
m_cluster_eduMPI_path = path;
QString filePath = "/home/" + ident + "/eduMPI_files/tmp/";
QProcess process;
QString sshCommand = QString("ssh %1@%2 '[ -d \"%3\" ] && echo \"exists\" || echo \"not exist\"; [ -d \"%4\" ] || mkdir -p \"%4\"'").arg(ident, address, path, filePath);
// Starten des SSH-Befehls
process.start("bash", QStringList() << "-c" << sshCommand);
// Warten, bis der Prozess gestartet ist
if (!process.waitForStarted()) {
process.kill();
return "Error! The SSH process could not be started. Please check all details and your network connection. A VPN connection may be necessary. Restart the application.";
}
// Warten, bis der Prozess beendet ist
if (!process.waitForFinished()) {
process.kill();
//return "Error! The SSH process could not be started. Please check all details and your network connection. A VPN connection may be necessary. Restart the application.! ";
}
// Ausgabe des Prozesses lesen
QString output = process.readAllStandardOutput().trimmed();
QString errorOutput = process.readAllStandardError().trimmed();
// Überprüfen, ob der Pfad existiert
if (output == "exists") {
process.kill();
m_cluster_connection_ready = true;
return "Success! An SSH connection could be established and an EduMPI version exists under the specified path.";
} else if (output == "not exist") {
process.kill();
m_cluster_connection_ready = false;
return "Error! No EduMPI version could be found under the specified path.";
} else {
if (!errorOutput.isEmpty()) {
qDebug() << "Fehlerausgabe:" << errorOutput;
process.kill();
if(errorOutput.startsWith("ssh: connect to host")){
m_cluster_connection_ready = false;
return "Error! The specified host cannot be reached. Check your network connection. It may be necessary to establish a VPN connection.";
} else if(errorOutput.startsWith("ssh: Could not resolve hostname")){
m_cluster_connection_ready = false;
return "Error! There seems to be something wrong with your host information. Please check the name or IP address.";
}
m_cluster_connection_ready = false;
return "Error! There seems to be a problem with your ID. Before you can start an MPI application, you must generate an SSH key once for passwordless communication. Follow the instructions under: \nHelp > SSH-Key-Gen Guide.";
}
process.kill();
m_cluster_connection_ready = false;
return "Error! An error has occurred. Please check all details and your network connection. A VPN connection may be necessary.";
}
// Überprüfen auf Fehler
}
void Controller::connectClusterAsync(const QString &address, const QString &ident, const QString &path, QJSValue callback){
QtConcurrent::run([=]() {
QString result = connectCluster(address, ident, path);
// Ensure the callback is invoked in the main thread
QMetaObject::invokeMethod(this, [=]() {
callback.call(QJSValueList() << result);
}, Qt::QueuedConnection);
});
if(m_connection_ready){
m_job_table->loadJobs(m_cluster_ident);
}
}
void Controller::writeLocalBashFile(QString local_path, bool file, int proc_num){
//m_visualization = visualization;
m_start_timestamp = QTime();
if(m_componentsBuilt){
//removeClusterComponents();
m_componentsBuilt = false;
std::cout << "Nach Start der Bash: False!" << std::endl;
emit signalToClearDB();
}
emit setProcNum(proc_num);
if(local_path.startsWith("file:///")){
local_path.remove(0,7);
}
QProcess bash_proc;
QString resourcePath;
if(m_option != 2){
resourcePath = ":/bash_files/local_bash_skript.sh";
} else {
resourcePath = ":/bash_files/local_bash_skript_scorep.sh";
}
// Temporäre Datei erstellen
QFile f(resourcePath);
if (!f.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug() << "Fehler beim Öffnen der Ressource.";
return;
}
QString tempFilePath = QDir::temp().absoluteFilePath("temp_bash_script_"+ m_cluster_ident +".sh");
std::cout << "TempFile: " << tempFilePath.toStdString() << std::endl;
QFile tempFile(tempFilePath);
if (!tempFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
qDebug() << "Fehler beim Versuch, die temporäre Datei zu schreiben." << tempFile.errorString();
if(!tempFile.open(QIODevice::WriteOnly | QIODevice::Text)){
sleep(1);
return;
}
return;
}
int lineNumber = 0;
QTextStream in(&f);
QTextStream out(&tempFile);
while (!in.atEnd()) {
QString line = in.readLine();
lineNumber++;
out << line << "\n";
if(lineNumber == 9) {
if(file){
if(m_option != 3){
out << "scp \"" + QString::fromStdString(m_envFilePath) + "\" \"$REMOTE_USER@$REMOTE_HOST:$REMOTE_DIR/tmp/\"\n";
//m_remote_dir_bash = "/home/" + m_cluster_ident + "/eduMPI_files";
}
out << "scp \"" + local_path + "\" \"$REMOTE_USER@$REMOTE_HOST:$REMOTE_DIR/\"\n";
out << "scp \"" + m_remote_bash_path + "\" \"$REMOTE_USER@$REMOTE_HOST:$REMOTE_DIR/\"\n";
m_remote_dir_bash = "/home/" + m_cluster_ident + "/eduMPI_files";
} else{
out << "mkdir " + local_path + "/tmp\n";
out << "scp -r \"" + local_path + "\" \"$REMOTE_USER@$REMOTE_HOST:$REMOTE_DIR/\"\n";
int lastSlashIndex = local_path.lastIndexOf("/");
QString dir_name = local_path.mid(lastSlashIndex + 1);
out << "scp \"" + QString::fromStdString(m_envFilePath) + "\" \"$REMOTE_USER@$REMOTE_HOST:$REMOTE_DIR/tmp/\"\n";
out << "scp \"" + m_remote_bash_path + "\" \"$REMOTE_USER@$REMOTE_HOST:$REMOTE_DIR/" + "\"\n";
m_remote_dir_bash = "/home/" + m_cluster_ident + "/eduMPI_files/" + dir_name;
}
}
}
f.close();
tempFile.close();
// Ausführungsrechte für die temporäre Datei setzen
tempFile.setPermissions(tempFile.permissions() | QFile::ExeUser);
slurm_process->startProcess(QStringList() << "--" << tempFilePath << m_cluster_ident << m_cluster_address << m_remote_dir_bash << "remote_bash_eduMPI.sh" );
}
bool Controller::checkFile(QString source, QString program, bool file){
if(source.startsWith("file:///")){
source.remove(0,7);
}
if(file){
if(source.endsWith(".c")){
QFile fileA(source);
if(!fileA.exists()){
return false;
}
int slashIndex = source.lastIndexOf('/');
QString trimmedSource = source.left(slashIndex);
if(!program.endsWith(".c")){
program += ".c";
}
trimmedSource = trimmedSource + "/" + program;
QFile fileB(trimmedSource);
if(!fileB.exists()){
return false;
}
} else {
return false;
}
} else {
if(!source.endsWith("/")){
source += "/";
}
if(!program.endsWith(".c")){
program += ".c";
}
source += program;
QFile fileA(source);
if(!fileA.exists()){
return false;
}
}
return true;
}
QString Controller::readBash(){
const char *homeDir = getenv("HOME");
QString filePath = QString(homeDir) + "/skript.sh";
QFile file(filePath);
if(!file.open(QIODevice::ReadOnly | QIODevice::Text)){
qDebug() << "Fehler beim Öffnen der Datei";
//return "0";
}
QTextStream in(&file);
QString fileContent = in.readAll();
file.close();
return fileContent;
}
void Controller::writeBash(QString content){
const char *homeDir = getenv("HOME");
QString filePath = QString(homeDir) + "/skript.sh";
QFile file(filePath);
if(!file.open(QIODevice::WriteOnly | QIODevice::Text)){
qDebug() << "Fehler beim Öffnen der Datei";
//return "0";
}
else {
QTextStream stream(&file);
stream<<content;
file.close();
}
}
void Controller::startBash(int proc_num){
if(m_componentsBuilt){
//removeClusterComponents();
m_componentsBuilt = false;
std::cout << "Nach Start der Bash: False!" << std::endl;
emit signalToClearDB();
}
//emit signalToBuildComponents(proc_num);
std::cout << "StartBash" << std::endl;
QString homedir = getenv("HOME");
QString dir = homedir + "/skript.sh";
chmod(dir.toStdString().c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
QProcess process;
process.setProgram("gnome-terminal");
process.setArguments(QStringList() << "--" << dir << QString::number(proc_num));
process.startDetached();
}
void Controller::writeRemoteBashFile(QString program_name, int proc_num, int option){
m_option = option;
const char *homeDir = getenv("HOME");
QString filePath = QString::fromUtf8(homeDir) + "/remote_bash_eduMPI.sh";
m_remote_bash_path = filePath;
if(program_name.endsWith(".c")){
program_name.chop(2);
}
std::ofstream scriptFile(filePath.toStdString());
if(scriptFile.is_open()){
scriptFile << "#!/usr/bin/env bash\n";
scriptFile << "#SBATCH --partition=all\n";
if(option == 3){
scriptFile << "#SBATCH --ntasks=1\n";
scriptFile << "#SBATCH --job-name=eduMPI_OpenMP\n";
scriptFile << "#SBATCH --nodes=1\n";
scriptFile << "#SBATCH --exclusive\n";
} else {
scriptFile << "#SBATCH --ntasks=" << proc_num << "\n";
scriptFile << "#SBATCH --job-name=eduMPI\n";
}
if(option == 0){
scriptFile << "#SBATCH --cpus-per-task=2\n\n";
} else if (option == 3){
scriptFile << "#SBATCH --cpus-per-task=" << proc_num <<"\n\n";
} else {
scriptFile << "#SBATCH --cpus-per-task=1\n\n";
}
if(option == 0){
scriptFile << "for i in {1..5}; do\n";
scriptFile << " if source ." << m_envFilePath << " 2>/dev/null; then\n";
scriptFile << " export $(cut -d= -f1 ." << m_envFilePath << ")\n";
scriptFile << " rm ." << m_envFilePath << "\n";
scriptFile << " break\n";
scriptFile << " else\n";
scriptFile << " sleep 1\n";
scriptFile << " fi\n";
scriptFile << "done\n";
scriptFile << "export OMPI_MCA_coll_han_priority=0\n";
} else if(option == 2){
scriptFile << "export PATH=\"$PATH:/opt/scalasca/bin\"\n";
scriptFile << "export PATH=\"$PATH:/opt/scorep/bin\"\n";
} else if(option == 3){
scriptFile << "export OMP_NUM_THREADS=$SLURM_CPUS_PER_TASK\n";
}
if(option == 0){
scriptFile << "export EDUMPI_PROGRAM=" << program_name.toStdString() << ".c\n";
scriptFile << m_cluster_eduMPI_path.toStdString() << "/bin/mpicc " << program_name.toStdString() << ".c -o " << program_name.toStdString() << " -lm" << "\n";
scriptFile << m_cluster_eduMPI_path.toStdString() << "/bin/mpirun -n " << proc_num << " --map-by :PE=2 --mca pml ob1 ./"+program_name.toStdString();
} else if(option == 1) {
scriptFile << "mpicc " << program_name.toStdString() << ".c -o " << program_name.toStdString() << " -lm" << "\n";
scriptFile << "time mpirun -n " << proc_num << " ./"+program_name.toStdString();
} else if(option == 2) {
scriptFile << "scorep mpicc " << program_name.toStdString() << ".c -o " << program_name.toStdString() << " -lm" << "\n";
scriptFile << "scalasca -analyze -e scorep_" << program_name.toStdString() << "_" << proc_num << "_$SLURM_JOB_ID mpiexec -n " << proc_num << " ./"+program_name.toStdString();
} else if(option == 3){
scriptFile << "gcc " << program_name.toStdString() << ".c -o " << program_name.toStdString() << " -lm -fopenmp" << "\n";
scriptFile << "./" << program_name.toStdString();
}
scriptFile.close();
}
}
void Controller::slurm_status_changed(QString status){
qDebug() << "Call Slurm_Status_Changed " << status;
if(status.contains("running")||status.contains("pending")){
m_status_running = true;
}
//std::cout << "slurm_status_changed Methode, m_option " << m_option << std::endl;
if(m_option == 0){
if((status == "completed" || status == "cancelled")){
copyOutputFile();
/*} else {
std::cout << "m_start_timestamp same" << std::endl;
m_status_running = false;
return;
}*/
} else {
//std::cout << "slurm_status_changed, status: " << status.toStdString() << " m_componentsBuild" << m_componentsBuilt << std::endl;
}
} else {
if((status == "completed" || status == "cancelled")&& m_status_running){
qDebug() << "Call CopyOutputFile\n";
copyOutputFile();
//slurm_process->killProcess();
}
}
emit signalSlurmStatusChanged(status);
}
void Controller::slotEndTime(QDateTime time){
QTime qtimeTimestamp;
int seconds;
qtimeTimestamp = time.time();
QTime midnight;
midnight.setHMS(0,0,0);
seconds = midnight.secsTo(qtimeTimestamp);
emit signalEndTime(seconds);
}
void Controller::getSlurmID(const int id){
m_slurm_id = id;
emit liveSlurmID(m_slurm_id);
}
void Controller::cancelRunningJob(){
std::cout << "Signal: cancelRunningJob" << std::endl;
int signal = SIGTERM;
slurm_process->sendSignal(signal);
slurm_process->killProcess();
}
void Controller::closeApp(){
qDebug() << "CLOSE APP";
//removeClusterComponents();
if (!m_envFilePath.empty()) {
if (unlink(m_envFilePath.c_str()) != 0) {
std::cerr << "Failed to delete temporary file" << std::endl;
} else {
std::cout << "Temporary .env file deleted" << std::endl;
}
}
if(slurm_process){
int signal = SIGTERM;
slurm_process->sendSignal(signal);
slurm_process->killProcess();
} else {
qDebug() << "slurm_process is already removed";
}
/*std::cout << db.isOpen() << std::endl;
if(m_connection_ready){
QSqlQuery query;
query.exec("DELETE FROM cluster_information;");
sleep(1);
query.exec("CALL refresh_continuous_aggregate('mpi_ds_secondly', NULL, NULL);");
query.exec("DELETE FROM mpi_information;");
}
db.close();*/
}
//Slots and Signals for Table_UserID
void Controller::slotFetchEduMPIJobs(){
}
bool Controller::connectToDB(const QString &hostname, const QString &databasename, const int &port, const QString &username, const QString &password){
if (QSqlDatabase::contains(m_connectionName)) {
auto db = QSqlDatabase::database(m_connectionName);
if (db.isOpen()) {
qDebug() << "Database Connection " << m_connectionName << " is already open.";
return true;
}
}
auto db = QSqlDatabase::addDatabase("QPSQL", m_connectionName);
db.setHostName(hostname);
db.setPort(port);
db.setDatabaseName(databasename);
db.setUserName(username);
db.setPassword(password);
bool ok = db.open();
std::cout << "New Connection" << std::endl;
return ok;
}
QString Controller::getDatabaseConnection(){
return m_connectionName;
}
QString Controller::getClusterIdent(){
return m_cluster_ident;
}
void Controller::setComponentsBuild(bool b){
m_componentsBuilt = b;
}
void Controller::setTimestamp(QTime timestamp){
m_start_timestamp = timestamp;
}
Table_UserID* Controller::getJobTable(){
return m_job_table;
}
/*int Controller::count() const{
return 42;
}*/