2 #include <Poco/Logger.h>
3 #include <Poco/Timestamp.h>
18 : Poco::Runnable(), thread(), mutex(), terminated(false), timePeriod(DEFAULT_TIME_PERIOD),
19 asyncTasksQueue(asyncTasksQueue_), message(message_), statVariables(), counterStatVariables(), avgCounts()
34 Poco::Mutex::ScopedLock lock(mutex);
40 Poco::Mutex::ScopedLock lock(mutex);
46 Poco::Mutex::ScopedLock lock(mutex);
47 timePeriod=timePeriod_;
52 Poco::Mutex::ScopedLock lock(mutex);
56 void DRCEResourceMonitor::setStatVariables(
StatVariables&& rhs)
58 Poco::Mutex::ScopedLock lock(mutex);
59 statVariables = std::forward<StatVariables>(rhs);
64 Poco::Mutex::ScopedLock lock(mutex);
70 Poco::Mutex::ScopedLock lock(mutex);
71 statVariables = this->statVariables;
72 avgCounts = this->avgCounts;
77 Poco::Mutex::ScopedLock lock(mutex);
78 this->statVariables = statVariables;
79 this->counterStatVariables = statVariables;
80 this->avgCounts = avgCounts;
93 if (
fillFieldsArray(resultDataItem, asyncTasksQueue_, message_, pResourceMonitor))
102 std::stringstream outMsg;
103 outMsg <<
"DRCEResourceMonitor::fillFieldsArray task [ " << resultDataItem.
getRequestId() <<
" ] already not exist...";
104 Poco::Logger::root().log(Poco::Message(
drce_const::moduleName, outMsg.str(), Poco::Message::Priority::PRIO_TRACE));
112 std::stringstream outMsg;
113 outMsg <<
"DRCEResourceMonitor::fillFieldsArray task [ " << resultDataItem.
getRequestId() <<
" ] pid is empty ...";
114 Poco::Logger::root().log(Poco::Message(
drce_const::moduleName, outMsg.str(), Poco::Message::Priority::PRIO_TRACE));
120 std::stringstream outMsg;
121 outMsg <<
"DRCEResourceMonitor::fillFieldsArray task [ " << resultDataItem.
getRequestId() <<
" ] has pid [ " << asyncTask.
pid <<
" ] but process already not exist ...";
122 Poco::Logger::root().log(Poco::Message(
drce_const::moduleName, outMsg.str(), Poco::Message::Priority::PRIO_TRACE));
129 std::stringstream outMsg;
130 outMsg <<
"DRCEResourceMonitor::fillFieldsArray task [ " << resultDataItem.
getRequestId() <<
" ] has state: " <<
static_cast<int>(asyncTask.
state);
131 Poco::Logger::root().log(Poco::Message(
drce_const::moduleName, outMsg.str(), Poco::Message::Priority::PRIO_TRACE));
136 std::map<std::string, std::string>
fields;
137 fields.insert(std::map<std::string, std::string>::value_type(
drce_const::taskCpu, trim(fieldsArray.
cpu)));
138 fields.insert(std::map<std::string, std::string>::value_type(
drce_const::taskVram, std::to_string(fieldsArray.
vram)));
139 fields.insert(std::map<std::string, std::string>::value_type(
drce_const::taskRram, std::to_string(fieldsArray.
rram)));
143 fields.insert(std::map<std::string, std::string>::value_type(
drce_const::hostCpu, trim(resourceUsage.
cpu)));
155 if (pResourceMonitor)
158 if (fieldsArray.
rram)
162 catch(std::exception& e)
172 const unsigned int periodSleep = 1000;
173 Poco::Timestamp tsMinuteCounter;
174 size_t countSyncTasks(0), countAsyncTasks(0);
179 if (periodMs > periodSleep)
181 unsigned int repeat = periodMs/periodSleep;
182 for (
unsigned int i=0;i<repeat;++i)
184 Poco::Thread::sleep(periodSleep);
187 countTasksForMinute(tsMinuteCounter, countSyncTasks, countAsyncTasks);
192 Poco::Thread::sleep(periodMs);
193 countTasksForMinute(tsMinuteCounter, countSyncTasks, countAsyncTasks);
197 Poco::Logger::root().log(Poco::Message(
drce_const::moduleName,
"!!! DRCEResourceMonitor::run !!!", Poco::Message::Priority::PRIO_TRACE));
207 asyncTasksQueue.
executeNotification(resultData, DRCETaskRequest::RequestType::rtResourceMonitorNotification);
210 setStatVariables(std::forward<StatVariables>(recountStatVariables()));
212 catch(Poco::Exception& e)
216 catch(std::exception& e)
293 template <
class T>
void DRCEResourceMonitor::addStatValue(T value,
StatVariableData<T>& statVariableData,
size_t& count)
297 if (statVariableData.
min)
299 if (value < statVariableData.
min)
300 statVariableData.
min = value;
303 statVariableData.
min = value;
305 if (statVariableData.
max)
307 if(value > statVariableData.
max)
308 statVariableData.
max = value;
311 statVariableData.
max = value;
313 statVariableData.
avg += value;
318 void DRCEResourceMonitor::countTasksForMinute(Poco::Timestamp& ts,
size_t& countSyncTasks,
size_t& countAsyncTasks)
320 const size_t oneMinute = 60000000;
321 if (ts.isElapsed(oneMinute) || !countSyncTasks || !countAsyncTasks)
331 StatVariables DRCEResourceMonitor::recountStatVariables(
void)
333 StatVariables localStatVariables(counterStatVariables);
357 return localStatVariables;