summaryrefslogtreecommitdiff
path: root/cad/plugins/NanoVision-1/src/JobManagement/GROMACS_JobMonitor.cpp
blob: 661dd87e39c4fd9bd779fc163058763db4f89c80 (plain)
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
// Copyright 2008 Nanorex, Inc.  See LICENSE file for details.

#include "GROMACS_JobMonitor.h"


/* CONSTRUCTOR */
GROMACS_JobMonitor::GROMACS_JobMonitor(const QString& initString)
		: JobMonitor(initString) {
}


/* DESTRUCTOR */
GROMACS_JobMonitor::~GROMACS_JobMonitor() {
}


/* FUNCTION: run
 *
 * Note: initString is the GROMACS process id (pid.)
 */
void GROMACS_JobMonitor::run() {
	
	QString title = tr("GROMACS process %1").arg(initString);
	QString debugMessage =
		tr("Emitting startedMonitoring(GMX, %1, %2)").arg(initString).arg(title);
	NXLOG_DEBUG("GROMACS_JobMonitor", qPrintable(debugMessage));
	emit startedMonitoring("GMX", initString, title);
	
	bool _aborted = aborted = false;
	bool emittedJobAborted = false;
	bool monitorError = false;
	bool stillRunning = true;
	QString pid = initString;
	while (stillRunning && /*!_aborted &&*/ !monitorError) {
		sleep(1);
		
		CheckJobActive(pid, stillRunning, monitorError);
		
		jobControlMutex.lock();
		_aborted = aborted;
		jobControlMutex.unlock();
	}
	
	if (_aborted && !emittedJobAborted) {
		debugMessage = tr("Emitting jobAborted(%1)").arg(initString);
		NXLOG_DEBUG("GROMACS_JobMonitor", qPrintable(debugMessage));
		emit jobAborted(initString);
		emittedJobAborted = true;
		
	} else if (monitorError) {
		;// TODO: handle this
		
	} else {
		debugMessage = tr("Emitting jobFinished(%1)").arg(initString);
		NXLOG_DEBUG("GROMACS_JobMonitor", qPrintable(debugMessage));
		emit jobFinished(initString);
	}
}


/* FUNCTION: CheckJobActive */
void GROMACS_JobMonitor::CheckJobActive(const QString& pid, bool& stillRunning,
										bool& monitorError) {
	monitorError = false;
#if defined(WIN32)
	QString command =
		QString("%1/ps %2")
			.arg(QCoreApplication::applicationDirPath()).arg(pid);
#else
	QString command = QString("ps %1").arg(pid);
#endif
	FILE* commandPipe;

	commandPipe = popen(qPrintable(command), "r");
	if (commandPipe == 0) {
		QString logMessage = tr("Command failed: %1").arg(command);
		NXLOG_SEVERE("GROMACS_JobMonitor::CheckJobActive",
					 qPrintable(logMessage));
		monitorError = true;
		return; // Abort function
	}

	stillRunning = false;
	static int bufferSize = 64;
	char buffer[bufferSize];
printf("%s output:\n", qPrintable(command));
	while (fgets(buffer, bufferSize, commandPipe) != 0) {
printf("\t%s\n", buffer);
		string bufferString = buffer;
		
		// Remove spaces from beginning of line
		while ((bufferString.length() > 0) && (bufferString[0] == ' '))
			bufferString = bufferString.substr(1);
		
		if (bufferString.compare(0, pid.length(), qPrintable(pid)) == 0)
			stillRunning = true;
	}

	int status = pclose(commandPipe);
	if (status == -1)
		NXLOG_DEBUG("GROMACS_JobMonitor::run", "close pipe failed");
}


/* FUNCTION: abortJob */
#if defined(WIN32)
#include "windows.h"
#endif

void GROMACS_JobMonitor::abortJob() {
	QMutexLocker locker(&jobControlMutex);
	
#if defined(WIN32)
	WCHAR mutexName[32];
	swprintf(mutexName, L"GMX_SIGTERM_Signal%d", initString.toInt());
wprintf(L">>> mutexName=%s\n", mutexName);

	HANDLE mutex = NULL;
	mutex =
    	CreateMutex( 
        	NULL,				// default security attributes
			FALSE,				// initially not owned
			mutexName);			// mutex name

	if (mutex != NULL) {
printf(">>> mutex created, not owned\n");
		// Once we can't lock the mutex it means that the GMX process has locked
		// it and has thereby acknowledged its existence and the SIGTERM signal.
		bool mutexLocked = false;
		while (!mutexLocked) {
			DWORD result =
				WaitForSingleObject( 
					mutex,		// handle to mutex
					0);			// don't wait, just test
					
			if (result == WAIT_OBJECT_0) {
printf(">>> Got mutex\n"); fflush(0);
				ReleaseMutex(mutex);
				
			} else {
printf(">>> mutex locked - GMX acks\n");fflush(0);
				aborted = true;
				mutexLocked = true;
			}
			sleep(1);
		}
		CloseHandle(mutex);
		
	} else {
printf(">>> couldn't create mutex\n");
	}
#else
	QString command = QString("kill -15 %1").arg(initString);
	int status = system(qPrintable(command));
	if (status == 0)
		aborted = true;
#endif
}