-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResourceMonitor.cpp
490 lines (401 loc) · 16.6 KB
/
ResourceMonitor.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
#include "ProcessFactory/ResourceMonitor.h"
#include "ProcessFactory.h"
#include "ProcessFactory/FactoryLibrary.h"
namespace ProcessFactorySpace
{
// -------------------------------------------------------
// local functions
// -------------------------------------------------------
string AvailableBytes(string executableName)
{
string executable(executableName);
executable.resize(executable.size() - 4);
size_t pos = executable.find_last_of("\\");
executable = executable.substr(pos+1);
return string("\\Process(" + executable + ")\\Working Set");
}
string ProcessTime(string executableName)
{
string executable(executableName);
executable.resize(executable.size() - 4);
size_t pos = executable.find_last_of("\\");
executable = executable.substr(pos+1);
return string("\\Process(" + executable + ")\\% Processor Time");
}
string FileDescriptorCount(string executableName)
{
string executable(executableName);
executable.resize(executable.size() - 4);
size_t pos = executable.find_last_of("\\");
executable = executable.substr(pos+1);
return string("\\Process(" + executable + ")\\Handle Count");
}
string ThreadCount(string executableName)
{
string executable(executableName);
executable.resize(executable.size() - 4);
size_t pos = executable.find_last_of("\\");
executable = executable.substr(pos+1);
return string("\\Process(" + executable + ")\\Thread Count");
}
// -------------------------------------------------------
// constructor/destructor ResourceMonitor
// -------------------------------------------------------
ResourceMonitor::ResourceMonitor(string hostName, int interval, bool autoStart)
: Thread(),
nodeHostName(hostName),
monitorInterval(interval),
processId(Thread::processId()),
monitorResources(true),
notifySystemManagerThread(NULL)
{
this->addNodeProcess();
if(autoStart == true) start();
}
ResourceMonitor::~ResourceMonitor()
{
guard.lock();
if(monitorResources == false)
{
guard.unlock();
}
else
{
monitorResources = false;
guard.unlock();
//msleep(monitorInterval);
}
}
// -------------------------------------------------------
// run()
// -------------------------------------------------------
void ResourceMonitor::run()
{
#ifdef USE_GCC
runOnLinux();
#else
runOnWindows();
#endif
}
void ResourceMonitor::runOnWindows()
{
#ifndef USE_GCC
while(monitorResources)
{
msleep(monitorInterval);
MutexLocker lock(&guard);
if(monitorResources == false) break;
// retrieve resources used by all processes, and the node
for(MapComponentResources::iterator it = mapComponentResources.begin(), it_end = mapComponentResources.end(); it != it_end; ++it)
{
HostResources &res = it->second;
if(mapPidPdhQuery.count(res.GetProcessId()) <= 0)
{
cout << "ResourceMonitor::runOnWindows(): WARNING! No PdhQuery associated with process id. Res: " << res << endl;
continue;
}
PdhQuery *query = mapPidPdhQuery[res.GetProcessId()];
ASSERT(query);
query->updateCounters();
if(res.GetResourceView() == HostResources::NODE) // -> node
{
setNodeResources(query, res);
}
else if(res.GetResourceView() == HostResources::PROCESS) // -> component
{
setProcessResources(query, res);
}
else
cout << "ResourceMonitor::runOnWindows(): WARNING! Not identified : " << res << endl;
//printf("%d cpu %3d%% \tavail (%d kb) \n", res.GetProcessId(), res.GetCpuUsage(), res.GetMemoryUsage());
}
if(notifySystemManagerThread)
notifySystemManagerThread->addAndWakeup(mapComponentResources);
}
#endif
}
void ResourceMonitor::runOnLinux()
{
int64 prevCPUTotal = 0, prevCPUIdle = 0;
ProcessFactoryLibrary::Linux::MetricStateWrapper64 sentState, recvState;
map<string, pair<int64, int64> > prevCPUStats;
HostResources::MapStringDouble currCPUStats;
vector<string> vectorPartitions;
ProcessFactoryLibrary::Linux::MetricStateWrapper32 diskTransfer, diskRead, diskWrite;
map<int, int64> mapPidTotalCPUUsage;
int64 prevTotalCPU = 0;
while(true)
{
// -- new impl --
if(prevCPUStats.empty() == false)
prevTotalCPU = prevCPUStats.begin()->second.second;
// -- end new impl --
msleep(monitorInterval);
MutexLocker lock(&guard);
int64 currentTotalCPU = 0;
vector<int> removeDeadProcesses;
// retrieve resources used by all processes, and the node
for(MapComponentResources::iterator it = mapComponentResources.begin(), it_end = mapComponentResources.end(); it != it_end; ++it)
{
HostResources &res = it->second;
if(res.GetResourceView() == HostResources::NODE)
{
double cpuUsage = ProcessFactoryLibrary::Linux::getCPUUsagePercent(prevCPUTotal, prevCPUIdle);
ProcessFactoryLibrary::Linux::getNetworkTraffic(sentState, recvState);
ProcessFactoryLibrary::Linux::getCPUUsagePercent(prevCPUStats, currCPUStats);
ProcessFactoryLibrary::Linux::getDiskTraffic(monitorInterval/1000, vectorPartitions, diskTransfer, diskRead, diskWrite);
ProcessFactoryLibrary::Linux::getNodeResources(res);
res.SetMapCPUTemperature(ProcessFactoryLibrary::Linux::GetCPUTemperatures());
res.SetCpuUsage(cpuUsage);
res.SetMapCPUUse(currCPUStats);
res.SetMapDiskTransfers(diskTransfer.metricState);
res.SetMapDiskRead(diskRead.metricState);
res.SetMapDiskWrite(diskWrite.metricState);
res.SetMapNIAmountSent(sentState.metricState);
res.SetMapNIAmountReceived(recvState.metricState);
res.SetNumFileDescriptors(ProcessFactoryLibrary::Linux::GetNumberOfFileDescriptors());
res.SetNumSockets(ProcessFactoryLibrary::Linux::GetNumberOfSockets());
}
else if(res.GetResourceView() == HostResources::PROCESS)
{
bool ret = ProcessFactoryLibrary::Linux::getProcessResources(res.GetProcessId(), res);
if(ret == false)
{
//cout << "ResourceMonitor::runOnLinux(): Removed dead process with pid " << res.GetProcessId() << endl;
removeDeadProcesses.push_back(res.GetProcessId());
break;
}
else
{
if(mapPidTotalCPUUsage.count(res.GetProcessId()) <= 0)
mapPidTotalCPUUsage.insert(pair<int, int64>(res.GetProcessId(), 0));
if(currentTotalCPU == 0)
{
map<string, pair<int64, int64> > currStats = prevCPUStats;
map<string, double> currentCPUStats;
ProcessFactoryLibrary::Linux::getCPUUsagePercent(currStats, currentCPUStats);
currentTotalCPU = currStats.begin()->second.second;
}
double cpuUsage = ProcessFactoryLibrary::Linux::getCPUUsagePercent(res.GetProcessId(), mapPidTotalCPUUsage[res.GetProcessId()], prevTotalCPU, currentTotalCPU);
res.SetCpuUsage(cpuUsage);
int numThreads = ProcessFactoryLibrary::Linux::GetNumberOfThreads(res.GetProcessId());
res.SetNumThreads(numThreads);
int fd = ProcessFactoryLibrary::Linux::GetNumberOfOpenFileDescriptors(res.GetProcessId());
res.SetNumFileDescriptors(fd);
}
}
else
cout << "ResourceMonitor::runOnLinux(): WARNING! Not identified : " << res << endl;
//printf("%d cpu %3d%% \tavail (%d kb) \n", res.GetProcessId(), res.GetCpuUsage(), res.GetMemoryUsage());
}
// -- remove dead processes --
for(vector<int>::iterator it = removeDeadProcesses.begin(), it_end = removeDeadProcesses.end(); it != it_end; ++it)
mapComponentResources.erase(*it);
// -- update SystemManager --
if(notifySystemManagerThread)
notifySystemManagerThread->addAndWakeup(mapComponentResources);
}
}
void ResourceMonitor::addNodeProcess()
{
MutexLocker lock(&guard);
HostResources nodeResources;
nodeResources.SetResourceView(HostResources::NODE);
nodeResources.SetHostName(nodeHostName);
nodeResources.SetComponentName(nodeHostName);
nodeResources.SetProcessId(1); // == 1 which is the init process, otherwise should be this->processId
nodeResources.SetHostDescription(HostInformation::COMPONENT_FACTORY);
if(mapComponentResources.count(nodeResources.GetProcessId()) > 0)
cout << "ResourceMonitor::addNodeProcess(): WARNING! node resource already exists! " << endl;
mapComponentResources.insert(pair<int, HostResources>(nodeResources.GetProcessId(), nodeResources));
#ifdef USE_GCC
// TODO
#else
PdhQuery *newQuery = new PdhQuery();
newQuery->init();
newQuery->addCounter("\\Processor(_Total)\\% Processor Time");
newQuery->addCounter("\\Memory\\Available Bytes");
newQuery->addCounter("\\Server(_Total)\\Files Open");
newQuery->addCounter("\\TCP\\Connections Established");
//newQuery->addCounter("\\UDP\\Connections Established");
#if 0
newQuery->addWildCardCounter("\\Network Interface(*//*#*)\\Bytes Received/sec");
newQuery->addWildCardCounter("\\Network Interface(*//*#*)\\Bytes Sent/sec");
newQuery->addWildCardCounter("\\Processor(*//*#*)\\% Processor Time");
newQuery->addWildCardCounter("\\PhysicalDisk(*//*#*)\\Disk Transfers/sec");
//newQuery->addWildCardCounter("\\PhysicalDisk(*/*#*)\\% Disk Read Time");
//newQuery->addWildCardCounter("\\PhysicalDisk(*/*#*)\\% Disk Write Time");
newQuery->addWildCardCounter("\\PhysicalDisk(*//*#*)\\Avg. Disk Bytes/Read");
newQuery->addWildCardCounter("\\PhysicalDisk(*//*#*)\\Avg. Disk Bytes/Write");
//newQuery->addWildCardCounter("\\PhysicalDisk(*/*#*)\\Disk Read Bytes/sec");
//newQuery->addWildCardCounter("\\PhysicalDisk(*/*#*)\\Disk Write Bytes/sec");
#elif 1
newQuery->addWildCardCounter("\\Network Interface(*)\\Bytes Received/sec");
newQuery->addWildCardCounter("\\Network Interface(*)\\Bytes Sent/sec");
newQuery->addWildCardCounter("\\Processor(*)\\% Processor Time");
newQuery->addWildCardCounter("\\PhysicalDisk(*)\\Disk Transfers/sec");
//newQuery->addWildCardCounter("\\PhysicalDisk(*/*#*)\\% Disk Read Time");
//newQuery->addWildCardCounter("\\PhysicalDisk(*/*#*)\\% Disk Write Time");
newQuery->addWildCardCounter("\\PhysicalDisk(*)\\Avg. Disk Bytes/Read");
newQuery->addWildCardCounter("\\PhysicalDisk(*)\\Avg. Disk Bytes/Write");
//newQuery->addWildCardCounter("\\PhysicalDisk(*/*#*)\\Disk Read Bytes/sec");
//newQuery->addWildCardCounter("\\PhysicalDisk(*/*#*)\\Disk Write Bytes/sec");
mapPidPdhQuery.insert(pair<int, PdhQuery*>(nodeResources.GetProcessId(), newQuery));
#endif
#endif
}
// -------------------------------------------------------
// Process/Node resources
// -------------------------------------------------------
#ifndef USE_GCC
void ResourceMonitor::setNodeResources(PdhQuery *query, HostResources &res)
{
res.SetCpuUsage(query->getCounter("\\Processor(_Total)\\% Processor Time"));
res.SetMemoryUsage(query->getCounter64("\\Memory\\Available Bytes"));
res.SetNumFileDescriptors(query->getCounter("\\Server(_Total)\\Files Open"));
res.SetNumSockets(query->getCounter("\\TCP\\Connections Established"));
#if 0
HostResources::MapStringInt64 mapCounterToValue;
query->getWildCardCounters("\\Network Interface(*/*#*)\\Bytes Received/sec", mapCounterToValue);
res.SetMapNIAmountReceived(mapCounterToValue);
mapCounterToValue.clear();
query->getWildCardCounters("\\Network Interface(*/*#*)\\Bytes Sent/sec", mapCounterToValue);
res.SetMapNIAmountSent(mapCounterToValue);
HostResources::MapStringDouble mapCPUToValue;
query->getWildCardCounters("\\Processor(*/*#*)\\% Processor Time", mapCPUToValue);
res.SetMapCPUUse(mapCPUToValue);
HostResources::MapStringInt32 mapToValue;
query->getWildCardCounters("\\PhysicalDisk(*/*#*)\\Disk Transfers/sec", mapToValue);
res.SetMapDiskTransfers(mapToValue);
mapToValue.clear();
query->getWildCardCounters("\\PhysicalDisk(*/*#*)\\Avg. Disk Bytes/Read", mapToValue);
res.SetMapDiskRead(mapToValue);
mapToValue.clear();
query->getWildCardCounters("\\PhysicalDisk(*/*#*)\\Avg. Disk Bytes/Write", mapToValue);
res.SetMapDiskWrite(mapToValue);
#elif 1
HostResources::MapStringInt64 mapCounterToValue;
query->getWildCardCounters("\\Network Interface(*)\\Bytes Received/sec", mapCounterToValue);
res.SetMapNIAmountReceived(mapCounterToValue);
mapCounterToValue.clear();
query->getWildCardCounters("\\Network Interface(*)\\Bytes Sent/sec", mapCounterToValue);
res.SetMapNIAmountSent(mapCounterToValue);
HostResources::MapStringDouble mapCPUToValue;
query->getWildCardCounters("\\Processor(*)\\% Processor Time", mapCPUToValue);
res.SetMapCPUUse(mapCPUToValue);
HostResources::MapStringInt32 mapToValue;
query->getWildCardCounters("\\PhysicalDisk(*)\\Disk Transfers/sec", mapToValue);
res.SetMapDiskTransfers(mapToValue);
mapToValue.clear();
query->getWildCardCounters("\\PhysicalDisk(*)\\Avg. Disk Bytes/Read", mapToValue);
res.SetMapDiskRead(mapToValue);
mapToValue.clear();
query->getWildCardCounters("\\PhysicalDisk(*)\\Avg. Disk Bytes/Write", mapToValue);
res.SetMapDiskWrite(mapToValue);
#endif
}
void ResourceMonitor::setProcessResources(PdhQuery *query, HostResources &res)
{
res.SetCpuUsage(query->getCounter((char*)ProcessTime(res.GetExecutableName()).c_str()));
res.SetMemoryUsage(query->getCounter64((char*)AvailableBytes(res.GetExecutableName()).c_str()));
res.SetNumThreads(query->getCounter((char*)ThreadCount(res.GetExecutableName()).c_str()));
res.SetNumFileDescriptors(query->getCounter((char*)FileDescriptorCount(res.GetExecutableName()).c_str()));
}
#endif
// -------------------------------------------------------
// public add/remove process
// -------------------------------------------------------
void ResourceMonitor::AddProcess(const HostInformation &info)
{
MutexLocker lock(&guard);
// -- start checks --
if(info.GetProcessId() < 0 || info.GetComponentName().empty())
{
stringstream stream;
stream << "ResourceMonitor::AddProcess(info): WARNING! Invalid HostInformation: " << info << endl;
cout << stream.str();
ExceptionMulticast::PostException(stream.str(), MiddlewareException::DATA_ERROR);
return;
}
if(mapComponentResources.count(info.GetProcessId()))
cout << "ResourceMonitor::AddProcess(info): WARNING! " << info.GetComponentName() << " is already being monitored! " << endl;
// -- end checks --
mapComponentResources.insert(pair<int, HostResources>(info.GetProcessId(), HostResources(info)));
#ifdef USE_GCC
// TODO
#else
// -- start check --
if(info.GetExecutableName().empty())
{
stringstream stream;
stream << "ResourceMonitor::AddProcess(info): WARNING! Missing executable name: " << info << endl;
cout << stream.str();
ExceptionMulticast::PostException(stream.str(), MiddlewareException::DATA_ERROR);
return;
}
// -- end check --
PdhQuery *newQuery = new PdhQuery();
newQuery->init();
newQuery->addCounter((char*)ProcessTime(info.GetExecutableName()).c_str());
newQuery->addCounter((char*)AvailableBytes(info.GetExecutableName()).c_str());
newQuery->addCounter((char*)ThreadCount(info.GetExecutableName()).c_str());
newQuery->addCounter((char*)FileDescriptorCount(info.GetExecutableName()).c_str());
mapPidPdhQuery.insert(pair<int, PdhQuery*>(info.GetProcessId(), newQuery));
#endif
}
void ResourceMonitor::RemoveProcess(const HostInformation &info)
{
MutexLocker lock(&guard);
if(!mapComponentResources.count(info.GetProcessId()))
{
//cerr << "Warning! " << info.GetComponentName() << " not found among monitored processes! " << endl;
return;
}
#ifndef USE_GCC
if(mapPidPdhQuery.count(info.GetProcessId()) > 0)
{
PdhQuery *query = mapPidPdhQuery[info.GetProcessId()];
ASSERT(query);
delete query;
mapPidPdhQuery.erase(info.GetProcessId());
}
#endif
mapComponentResources.erase(info.GetProcessId());
}
// -------------------------------------------------------
// public Process/Node resources
// - if id == -1 -> node resources else process resources
// -------------------------------------------------------
HostResources ResourceMonitor::GetResources(int id)
{
MutexLocker lock(&guard);
if(id <= -1)
{
for(MapComponentResources::iterator it = mapComponentResources.begin(), it_end = mapComponentResources.end(); it != it_end; ++it)
if(it->second.GetResourceView() == HostResources::NODE)
return it->second;
}
else if(!mapComponentResources.count(id))
{
cout << "ResourceMonitor::getResources(id): WARNING! " << id << " has no statistics yet!" << endl;
return HostResources();
}
return mapComponentResources[id];
}
string ResourceMonitor::GetStringAllResources()
{
MutexLocker lock(&guard);
stringstream stream;
for(MapComponentResources::iterator it = mapComponentResources.begin(), it_end = mapComponentResources.end(); it != it_end; ++it)
stream << it->second << endl;
return stream.str();
}
map<int, HostResources> ResourceMonitor::GetMapAllResources()
{
MutexLocker lock(&guard);
MapComponentResources mapResources;
for(MapComponentResources::const_iterator it = mapComponentResources.begin(), it_end = mapComponentResources.end(); it != it_end; ++it)
mapResources.insert(pair<int, HostResources>(it->first, it->second));
return mapResources;
}
} // namespace ProcessFactorySpace