| 1 |
/* |
| 2 |
* Copyright (C) 2013-2015, 2019 by the Konclude Developer Team. |
| 3 |
* |
| 4 |
* This file is part of the reasoning system Konclude. |
| 5 |
* For details and support, see <http://konclude.com/>. |
| 6 |
* |
| 7 |
* Konclude is free software: you can redistribute it and/or modify |
| 8 |
* it under the terms of version 3 of the GNU Lesser General Public |
| 9 |
* License (LGPLv3) as published by the Free Software Foundation. |
| 10 |
* |
| 11 |
* Konclude is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU (Lesser) General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU (Lesser) General Public |
| 17 |
* License along with Konclude. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
* |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "CReasonerExecutableEvaluationProvider.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Test { |
| 27 |
|
| 28 |
namespace Evaluation { |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
CReasonerExecutableEvaluationProvider::CReasonerExecutableEvaluationProvider() : CIntervalThread("ReasonerExecutionThread"), CLogIdentifier("::Konclude::Test::Evaluation::ReasonerExecutionThread", this) { |
| 33 |
mProcess = nullptr; |
| 34 |
mKillTimeout = 1000; |
| 35 |
mReasonerPort = 8080; |
| 36 |
mMemoryCheckingThread = nullptr; |
| 37 |
|
| 38 |
mReasonerForcedTermination = false; |
| 39 |
startThread(); |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
CReasonerExecutableEvaluationProvider::~CReasonerExecutableEvaluationProvider() { |
| 44 |
stopThread(); |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
bool CReasonerExecutableEvaluationProvider::createReasoner(CConfiguration *config) { |
| 49 |
mConfig = config; |
| 50 |
|
| 51 |
mReasonerName = CConfigDataReader::readConfigString(config, "Konclude.Evaluation.Reasoner.Name"); |
| 52 |
|
| 53 |
mReasonerBinaryFile = CConfigDataReader::readConfigString(config,"Konclude.Evaluation.Reasoner.Execution.Binary.File"); |
| 54 |
mReasonerBinaryArguments = CConfigDataReader::readConfigString(config,"Konclude.Evaluation.Reasoner.Execution.Binary.Arguments"); |
| 55 |
mReasonerPort = CConfigDataReader::readConfigInteger(config,"Konclude.Evaluation.Reasoner.Address.Port"); |
| 56 |
|
| 57 |
mKillScriptString = CConfigDataReader::readConfigString(config, "Konclude.Evaluation.TerminateAssistProgram"); |
| 58 |
mKillScriptArgumentsString = CConfigDataReader::readConfigString(config, "Konclude.Evaluation.TerminateAssistAdditionalArgument"); |
| 59 |
|
| 60 |
mCheckingMemoryUsage = CConfigDataReader::readConfigBoolean(config, "Konclude.Evaluation.CheckingMemoryUsage"); |
| 61 |
|
| 62 |
mRestartSleepTime = CConfigDataReader::readConfigInteger(config, "Konclude.Evaluation.ReasonerRestartPausingTime"); |
| 63 |
mTerminationSleepTime = CConfigDataReader::readConfigInteger(config, "Konclude.Evaluation.ReasonerTerminationPausingTime"); |
| 64 |
|
| 65 |
mProcessFinished = false; |
| 66 |
mProcessError = false; |
| 67 |
|
| 68 |
startReasoner(); |
| 69 |
return true; |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
QString CReasonerExecutableEvaluationProvider::getReasonerAddressString() { |
| 74 |
return QString("127.0.0.1:%1").arg(mReasonerPort); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
void CReasonerExecutableEvaluationProvider::startReasoner() { |
| 80 |
CBlockingCallbackData blockCallBack; |
| 81 |
postEvent(new CReasonerEvaluationReasonerStartEvent(&blockCallBack)); |
| 82 |
blockCallBack.waitForCallback(); |
| 83 |
} |
| 84 |
|
| 85 |
void CReasonerExecutableEvaluationProvider::startReasonerThreaded() { |
| 86 |
mMemoryCheckingThread = nullptr; |
| 87 |
|
| 88 |
QString argString = QString("%1").arg(mReasonerBinaryArguments); |
| 89 |
QString appString = QString("%1 %2").arg(mReasonerBinaryFile).arg(argString); |
| 90 |
|
| 91 |
mProcess = new QProcess(); |
| 92 |
|
| 93 |
connect(mProcess, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError)), Qt::DirectConnection); |
| 94 |
connect(mProcess, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(processFinished(int, QProcess::ExitStatus)), Qt::DirectConnection); |
| 95 |
|
| 96 |
|
| 97 |
mProcess->start(appString); |
| 98 |
|
| 99 |
if (mCheckingMemoryUsage) { |
| 100 |
mMemoryCheckingThread = new CReasonerMemoryUsageCheckingThread(mConfig, QString::number((cint64)mProcess->pid())); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
bool CReasonerExecutableEvaluationProvider::restartReasoner() { |
| 106 |
CBlockingCallbackData blockCallBack; |
| 107 |
postEvent(new CReasonerEvaluationReasonerRestartEvent(&blockCallBack)); |
| 108 |
blockCallBack.waitForCallback(); |
| 109 |
return true; |
| 110 |
} |
| 111 |
|
| 112 |
bool CReasonerExecutableEvaluationProvider::restartReasonerThreaded() { |
| 113 |
|
| 114 |
terminateReasonerThreaded(); |
| 115 |
if (mRestartSleepTime > 0) { |
| 116 |
QThread::msleep(mRestartSleepTime); |
| 117 |
} |
| 118 |
startReasonerThreaded(); |
| 119 |
return true; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
void CReasonerExecutableEvaluationProvider::terminateReasoner() { |
| 125 |
CBlockingCallbackData blockCallBack; |
| 126 |
postEvent(new CReasonerEvaluationReasonerStopEvent(&blockCallBack)); |
| 127 |
blockCallBack.waitForCallback(); |
| 128 |
} |
| 129 |
|
| 130 |
void CReasonerExecutableEvaluationProvider::terminateReasonerThreaded() { |
| 131 |
|
| 132 |
if (mMemoryCheckingThread) { |
| 133 |
mMaxMemoryUsage = qMax(mMaxMemoryUsage, mMemoryCheckingThread->getMemoryUsage()); |
| 134 |
delete mMemoryCheckingThread; |
| 135 |
mMemoryCheckingThread = nullptr; |
| 136 |
} |
| 137 |
|
| 138 |
try { |
| 139 |
QByteArray standOutput = mProcess->readAllStandardOutput(); |
| 140 |
QByteArray standError = mProcess->readAllStandardError(); |
| 141 |
mReasonerStdOutText += QString(standOutput); |
| 142 |
mReasonerStdErrText += QString(standError); |
| 143 |
mReasonerErrorString += mProcess->errorString(); |
| 144 |
} catch (std::bad_alloc& ba) { |
| 145 |
mReasonerStdErrText = "Reasoners outputs exceed size limitations\r\n"; |
| 146 |
LOG(INFO, "::Konclude::Test::ReasonerEvaluationProvider", logTr("Failed to read reasoner output due to size limitations."), this); |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
disconnect(mProcess, SIGNAL(error(QProcess::ProcessError)), this, SLOT(processError(QProcess::ProcessError))); |
| 151 |
disconnect(mProcess, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(processFinished(int, QProcess::ExitStatus))); |
| 152 |
disconnect(mProcess); |
| 153 |
disconnect(this); |
| 154 |
if (!mProcessFinished) { |
| 155 |
assistTermination(mProcess->pid()); |
| 156 |
mProcessFinished = true; |
| 157 |
mReasonerFinishedInTime = false; |
| 158 |
mReasonerForcedTermination = true; |
| 159 |
mProcess->terminate(); |
| 160 |
mProcess->waitForFinished(mKillTimeout); |
| 161 |
mProcess->kill(); |
| 162 |
mProcess->waitForFinished(mKillTimeout); |
| 163 |
mProcess->kill(); |
| 164 |
mProcess->waitForFinished(mKillTimeout); |
| 165 |
} |
| 166 |
mProcess->close(); |
| 167 |
delete mProcess; |
| 168 |
mProcess = nullptr; |
| 169 |
|
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
CReasonerEvaluationTerminationResult* CReasonerExecutableEvaluationProvider::destroyReasoner() { |
| 174 |
terminateReasoner(); |
| 175 |
|
| 176 |
CReasonerEvaluationTerminationResult* result = new CReasonerEvaluationTerminationResult(); |
| 177 |
result->initResult(mProcessError, mReasonerFinishedInTime, mReasonerForcedTermination, mReasonerErrorString, mReasonerStdOutText, mReasonerStdErrText, mMaxMemoryUsage); |
| 178 |
|
| 179 |
mReasonerForcedTermination = false; |
| 180 |
mMaxMemoryUsage = 0; |
| 181 |
mReasonerFinishedInTime = true; |
| 182 |
mReasonerStdOutText.clear(); |
| 183 |
mReasonerStdErrText.clear(); |
| 184 |
mReasonerErrorString.clear(); |
| 185 |
mProcessFinished = false; |
| 186 |
mProcessError = false; |
| 187 |
|
| 188 |
if (mTerminationSleepTime > 0) { |
| 189 |
QThread::msleep(mTerminationSleepTime); |
| 190 |
} |
| 191 |
|
| 192 |
return result; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
bool CReasonerExecutableEvaluationProvider::assistTermination(Q_PID processID) { |
| 198 |
if (!mKillScriptString.isEmpty()) { |
| 199 |
QProcess killProcess; |
| 200 |
QStringList argumentList; |
| 201 |
argumentList += QString::number((cint64)processID); |
| 202 |
if (!mKillScriptArgumentsString.isEmpty()) { |
| 203 |
argumentList += mKillScriptArgumentsString; |
| 204 |
} |
| 205 |
LOG(INFO,"::Konclude::Test::ReasonerEvaluationProvider",logTr("Executing '%1' to assist termination of process with id '%2'.").arg(mKillScriptString).arg((cint64)processID),this); |
| 206 |
killProcess.start(mKillScriptString,argumentList); |
| 207 |
if (!killProcess.waitForStarted()) { |
| 208 |
return false; |
| 209 |
} |
| 210 |
if (!killProcess.waitForFinished()) { |
| 211 |
return false; |
| 212 |
} |
| 213 |
if (killProcess.exitCode() != 0) { |
| 214 |
return true; |
| 215 |
} |
| 216 |
} |
| 217 |
return false; |
| 218 |
} |
| 219 |
|
| 220 |
void CReasonerExecutableEvaluationProvider::processError(QProcess::ProcessError error) { |
| 221 |
if (!mProcessFinished) { |
| 222 |
mProcessError = true; |
| 223 |
} |
| 224 |
|
| 225 |
} |
| 226 |
|
| 227 |
void CReasonerExecutableEvaluationProvider::processFinished(int exitCode, QProcess::ExitStatus exitStatus) { |
| 228 |
if (!mProcessFinished) { |
| 229 |
mProcessFinished = true; |
| 230 |
if (exitStatus != QProcess::NormalExit) { |
| 231 |
mProcessError = true; |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
bool CReasonerExecutableEvaluationProvider::processTimer(qint64 timerID) { |
| 237 |
return false; |
| 238 |
} |
| 239 |
|
| 240 |
bool CReasonerExecutableEvaluationProvider::processCustomsEvents(QEvent::Type type, CCustomEvent* event) { |
| 241 |
if (CThread::processCustomsEvents(type, event)) { |
| 242 |
return true; |
| 243 |
} else { |
| 244 |
if (type == CReasonerEvaluationReasonerStartEvent::EVENTTYPE) { |
| 245 |
CReasonerEvaluationReasonerStartEvent* rerse = (CReasonerEvaluationReasonerStartEvent*)event; |
| 246 |
startReasonerThreaded(); |
| 247 |
CCallbackData* callback = rerse->getCallback(); |
| 248 |
if (callback) { |
| 249 |
callback->doCallback(); |
| 250 |
} |
| 251 |
return true; |
| 252 |
} else if (type == CReasonerEvaluationReasonerRestartEvent::EVENTTYPE) { |
| 253 |
CReasonerEvaluationReasonerRestartEvent* rerse = (CReasonerEvaluationReasonerRestartEvent*)event; |
| 254 |
restartReasonerThreaded(); |
| 255 |
CCallbackData* callback = rerse->getCallback(); |
| 256 |
if (callback) { |
| 257 |
callback->doCallback(); |
| 258 |
} |
| 259 |
return true; |
| 260 |
} else if (type == CReasonerEvaluationReasonerStopEvent::EVENTTYPE) { |
| 261 |
CReasonerEvaluationReasonerStopEvent* rerse = (CReasonerEvaluationReasonerStopEvent*)event; |
| 262 |
terminateReasonerThreaded(); |
| 263 |
CCallbackData* callback = rerse->getCallback(); |
| 264 |
if (callback) { |
| 265 |
callback->doCallback(); |
| 266 |
} |
| 267 |
return true; |
| 268 |
} |
| 269 |
} |
| 270 |
return false; |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
}; // end namespace Evaluation |
| 275 |
|
| 276 |
}; // end namespace Test |
| 277 |
|
| 278 |
}; // end namespace Konclude |