Debian Codegraph @

Source/Test/Evaluation/CReasonerMemoryUsageCheckingThread.cpp

raw
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 "CReasonerMemoryUsageCheckingThread.h"
22
23
24 namespace Konclude {
25
26 namespace Test {
27
28 namespace Evaluation {
29
30
31
32 CReasonerMemoryUsageCheckingThread::CReasonerMemoryUsageCheckingThread(CConfiguration *config, QString processId) : CIntervalThread("ReasonerMemoryCheckingThread"), CLogIdentifier("::Konclude::Test::Evaluation::ReasonerEvaluationManagerThread",this) {
33 mProcessId = processId;
34 mConfig = config;
35 mChecking = true;
36 mContinue = true;
37
38 mMaxMemoryUsage = 0;
39
40 mMemoryCheckingIntervall = CConfigDataReader::readConfigInteger(config, "Konclude.Evaluation.MemoryUsageCheckingInterval");
41
42 mMemoryCheckingProgramm = CConfigDataReader::readConfigString(config, "Konclude.Evaluation.MemoryUsageCheckingProgram");
43
44 startThread();
45 }
46
47
48 CReasonerMemoryUsageCheckingThread::~CReasonerMemoryUsageCheckingThread() {
49 stopThread();
50 }
51
52
53 cint64 CReasonerMemoryUsageCheckingThread::getMemoryUsage(bool stopChecking) {
54 if (stopChecking) {
55 mContinue = false;
56 }
57 CBlockingCallbackData blockCallbackData;
58 postEvent(new CReasonerMemoryUsageFinishedEvent(&blockCallbackData));
59 blockCallbackData.waitForCallback();
60 return mMaxMemoryUsage;
61 }
62
63
64 void CReasonerMemoryUsageCheckingThread::threadStarted() {
65 CIntervalThread::threadStarted();
66 startTimerWithInterval(MEMORY_CHECKING_TIMER, mMemoryCheckingIntervall);
67 }
68
69 void CReasonerMemoryUsageCheckingThread::threadStopped() {
70 CIntervalThread::threadStopped();
71 }
72
73
74 bool CReasonerMemoryUsageCheckingThread::processTimer(qint64 timerID) {
75 if (timerID == MEMORY_CHECKING_TIMER) {
76 checkMemory();
77 return true;
78 }
79 return false;
80 }
81
82 void CReasonerMemoryUsageCheckingThread::checkMemory() {
83 if (!mMemoryCheckingProgramm.isEmpty()) {
84 QProcess* process = new QProcess();
85 QString argString = QString("%1").arg(mProcessId);
86 QString appString = QString("%1 %2").arg(mMemoryCheckingProgramm).arg(argString);
87 process->start(appString);
88 process->waitForFinished();
89 QString output = process->readAllStandardOutput().trimmed();
90 bool parsed = false;
91 cint64 memoryUsage = output.toLongLong(&parsed);
92 if (!parsed) {
93 memoryUsage = output.toDouble(&parsed);
94 }
95 mMaxMemoryUsage = qMax(mMaxMemoryUsage, memoryUsage);
96 delete process;
97 }
98 }
99
100 bool CReasonerMemoryUsageCheckingThread::processCustomsEvents(QEvent::Type type, CCustomEvent* event) {
101 if (CThread::processCustomsEvents(type,event)) {
102 return true;
103 } else {
104 if (type == CReasonerMemoryUsageFinishedEvent::EVENTTYPE) {
105 CReasonerMemoryUsageFinishedEvent* rmusfe = (CReasonerMemoryUsageFinishedEvent*)event;
106 if (mChecking && !mContinue) {
107 stopTimer(MEMORY_CHECKING_TIMER);
108 mChecking = false;
109 }
110 if (mChecking) {
111 checkMemory();
112 }
113 CCallbackData* callback = rmusfe->getCallback();
114 if (callback) {
115 callback->doCallback();
116 }
117 return true;
118 }
119 }
120 return false;
121 }
122
123
124
125
126 }; // end namespace Evaluation
127
128 }; // end namespace Test
129
130 }; // end namespace Konclude