This repository has been archived by the owner on Sep 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
metrics.cpp
477 lines (405 loc) · 16.8 KB
/
metrics.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
//
// metrics.cpp - file with definitions of functions related to defining and gathering the metrics
//
// 2022-2023 Damian Strojek @damianStrojek
// Piotr Garbowski @dideek
// Jakub Wasniewski @wisnia01
//
// External libraries
#include <iostream> // cin, cout
#include <string> // string, substr
#include <sstream> // stringstream
#include <array> // array
#include <memory> // pipe, decltype
// Internal headers
#include "metrics.h"
#define KILOBYTE 1024
SystemMetrics::SystemMetrics(){
this->processesRunning = -1;
this->processesAll = -1;
this->processesBlocked = -1;
this->contextSwitchRate = -1;
this->interruptRate = -1;
};
void SystemMetrics::printSystemMetrics(){
std::cout << "\n\t[SYSTEM METRICS]\n\n"
<< "Interrupt Rate = " << this->interruptRate << " interrupts/sec\n"
<< "Context Switch Rate = " << this->contextSwitchRate << " switches/sec\n"
<< "All Processes = " << this->processesAll << "\n"
<< "Running Processes = " << this->processesRunning << "\n"
<< "Blocked Processes = " << this->processesBlocked << "\n";
};
void getSystemMetrics(SystemMetrics &systemMetrics){
const char* command = "vmstat";
std::string output = exec(command), temp;
temp = output.substr(219, 4);
systemMetrics.interruptRate = std::stoi(temp); // interrupts/sec
temp = output.substr(224, 4);
systemMetrics.contextSwitchRate = std::stoi(temp); // context switches/sec
command = "cat /proc/loadavg | cut -d ' ' -f 4";
output = exec(command);
size_t slashPosition = output.find('/');
temp = output.substr(0, slashPosition);
systemMetrics.processesRunning = std::stoi(temp); // number of processes
temp = output.substr(slashPosition+1);
systemMetrics.processesAll = std::stoi(temp); // number of processes
command = "ps -eo state | grep -c '^D'";
output = exec(command);
systemMetrics.processesBlocked = std::stoi(output); // number of processes
//systemMetrics.printSystemMetrics();
};
ProcessorMetrics::ProcessorMetrics(){
this->timeUser = -1;
this->timeNice = -1;
this->timeSystem = -1;
this->timeIdle = -1;
this->timeIoWait = -1;
this->timeIRQ = -1;
this->timeSoftIRQ = -1;
this->timeSteal = -1;
this->timeGuest = -1;
this->instructionsRetired = -1;
this->cycles = -1;
this->frequencyRelative = -1;
this->unhaltedFrequency = -1;
this->cacheL2Misses = -1;
this->cacheL2Requests = -1;
this->cacheLLCLoadMisses = -1;
this->cacheLLCLoadMissRate = -1;
this->cacheLLCLoads = -1;
this->cacheLLCStoreMisses = -1;
this->cacheLLCStoreMissRate = -1;
this->cacheLLCStores = -1;
};
void ProcessorMetrics::printProcessorMetrics(){
std::cout << "\n\t[PROCESSOR METRICS]\n\n"
<< "Time User = " << this->timeUser << "\n"
<< "Time Nice = " << this->timeNice << "\n"
<< "Time System = " << this->timeSystem << "\n"
<< "Time Idle = " << this->timeIdle << "\n"
<< "Time I/O Wait = " << this->timeIoWait << "\n"
<< "Time IRQ = " << this->timeIRQ << "\n"
<< "Time Soft IRQ = " << this->timeSoftIRQ << "\n"
<< "Time Steal = " << this->timeSteal << "\n"
<< "Time Guest = " << this->timeGuest << "\n"
<< "Retired Instructions = " << this->instructionsRetired << "\n"
<< "Cycles = " << this->cycles << "\n"
<< "Relative Frequency = " << this->frequencyRelative << "\n"
<< "Unhalted Frequency = " << this->unhaltedFrequency << "\n"
<< "Cache L2 Requests = " << this->cacheL2Requests << "\n"
<< "Cache L2 Misses = " << this->cacheL2Misses << "\n"
<< "Cache LLC Loads = " << this->cacheLLCLoads << "\n"
<< "Cache LLC Load Misses = " << this->cacheLLCLoadMisses << "\n"
<< "Cache LLC Load Miss Rate = " << this->cacheLLCLoadMissRate << "\n"
<< "Cache LLC Stores = " << this->cacheLLCStores << "\n"
<< "Cache LLC Store Misses = " << this->cacheLLCStoreMisses << "\n"
<< "Cache LLC Store Miss Rate = " << this->cacheLLCStoreMissRate << "\n";
};
void getProcessorMetrics(ProcessorMetrics &processorMetrics){
const char* command = "cat /proc/cpuinfo | grep 'processor' -c";
std::string output = exec(command), temp;
int numberOfProcessors = std::stoi(output);
command = "cat /proc/stat";
output = exec(command);
std::stringstream streamOne(output);
streamOne >> temp; // Get rid of 'cpu' at the beggining
streamOne >> temp;
processorMetrics.timeUser = std::stoi(temp); // USER_HZ
streamOne >> temp;
processorMetrics.timeNice = std::stoi(temp); // USER_HZ
streamOne >> temp;
processorMetrics.timeSystem = std::stoi(temp); // USER_HZ
streamOne >> temp;
processorMetrics.timeIdle = std::stoi(temp); // USER_HZ
streamOne >> temp;
processorMetrics.timeIoWait = std::stoi(temp); // USER_HZ
streamOne >> temp;
processorMetrics.timeIRQ = std::stoi(temp); // USER_HZ
streamOne >> temp;
processorMetrics.timeSoftIRQ = std::stoi(temp); // USER_HZ
streamOne >> temp;
processorMetrics.timeSteal = std::stoi(temp); // USER_HZ
streamOne >> temp;
processorMetrics.timeGuest = std::stoi(temp); // USER_HZ
// sed 's/[\xE2\x80\xAF]//g' is getting rid of special white space characters
command = "perf stat -e 'l2_rqsts.references,l2_rqsts.miss,LLC-loads,LLC-stores,LLC-load-misses,LLC-store-misses' --all-cpus sleep 1 2>&1 | awk '/^[ ]*[0-9]/{print $1}' | sed 's/[\xE2\x80\xAF]//g'";
output = exec(command);
std::stringstream streamTwo(output);
streamTwo >> temp;
processorMetrics.cacheL2Requests = std::stoi(temp);
streamTwo >> temp;
processorMetrics.cacheL2Misses = std::stoi(temp);
streamTwo >> temp;
processorMetrics.cacheLLCLoads = std::stoi(temp);
streamTwo >> temp;
processorMetrics.cacheLLCStores = std::stoi(temp);
streamTwo >> temp;
processorMetrics.cacheLLCLoadMisses = std::stoi(temp);
streamTwo >> temp;
processorMetrics.cacheLLCStoreMisses = std::stoi(temp);
// Check division by zero and calculate miss rate
if(processorMetrics.cacheLLCLoads)
processorMetrics.cacheLLCLoadMissRate = float(processorMetrics.cacheLLCLoadMisses) / float(processorMetrics.cacheLLCLoads) * 100;
if(processorMetrics.cacheLLCStores)
processorMetrics.cacheLLCStoreMissRate = float(processorMetrics.cacheLLCStoreMisses) / float(processorMetrics.cacheLLCStores) * 100;
command = "perf stat -e instructions,cycles,cpu-clock,cpu-clock:u sleep 1 2>&1 | awk '/^[ ]*[0-9]/{print $1}' | sed 's/[\xE2\x80\xAF]//g' | tr ',' '.'";
output = exec(command);
std::stringstream streamThree(output);
streamThree >> temp;
processorMetrics.instructionsRetired = std::stoi(temp); // number of instructions
streamThree >> temp;
processorMetrics.cycles = std::stoi(temp); // number of cycles
streamThree >> temp;
processorMetrics.frequencyRelative = std::stof(temp); // MHz
streamThree >> temp;
processorMetrics.unhaltedFrequency = std::stof(temp); // MHz
//processorMetrics.printProcessorMetrics();
};
InputOutputMetrics::InputOutputMetrics(){
this->processID = GPROCESSID;
this->dataRead = -1;
this->readTime = -1;
this->readOperationsRate = -1;
this->dataWritten = -1;
this->writeTime = -1;
this->writeOperationsRate = -1;
this->flushTime = -1;
this->flushOperationsRate = -1;
};
void InputOutputMetrics::printInputOutputMetrics(){
std::cout << "\n\t[INPUT/OUTPUT METRICS]\n\n"
<< "Process ID = " << this->processID << "\n"
<< "Data Read = " << this->dataRead << " MB\n"
<< "Read Time = " << this->readTime << " ms\n"
<< "Read Operations Rate = " << this->readOperationsRate << "\n"
<< "Data Written = " << this->dataWritten << " MB\n"
<< "Write Time = " << this->writeTime << " ms\n"
<< "Write Operations Rate = " << this->writeOperationsRate << "\n"
<< "Flush Time = " << this->flushTime << " ms\n"
<< "Flush Operations Rate = " << this->flushOperationsRate << "\n";
};
void getInputOutputMetrics(InputOutputMetrics &inputOutputMetrics){
const char* command = "awk '{ print $2 }' /proc/$$/io";
std::string output = exec(command), temp;
std::stringstream streamOne(output);
streamOne >> temp;
inputOutputMetrics.dataRead = std::stof(temp) / KILOBYTE; // MB
streamOne >> temp;
inputOutputMetrics.dataWritten = std::stof(temp) / KILOBYTE; // MB
streamOne >> temp;
inputOutputMetrics.readOperationsRate = std::stoi(temp); // Number of operations
streamOne >> temp;
inputOutputMetrics.writeOperationsRate = std::stoi(temp); // Number of operations
streamOne >> temp;
command = "iostat -d -k | awk '/^[^ ]/ {device=$1} $1 ~ /sda/ {print 1000*$10/($4*$3), 1000*$11/($4*$3), $6/$4, $7/$6}'";
output = exec(command);
std::stringstream streamTwo(output);
streamTwo >> temp;
inputOutputMetrics.readTime = std::stof(temp); // ms
streamTwo >> temp;
inputOutputMetrics.writeTime = std::stof(temp); // ms
streamTwo >> temp;
inputOutputMetrics.flushOperationsRate = std::stof(temp); // operations/sec
streamTwo >> temp;
inputOutputMetrics.flushTime = std::stof(temp); // ms
//inputOutputMetrics.printInputOutputMetrics();
};
MemoryMetrics::MemoryMetrics(){
this->memoryUsed = -1;
this->memoryCached = -1;
this->swapUsed = -1;
this->swapCached = -1;
this->memoryActive = -1;
this->memoryInactive = -1;
this->pageInRate = -1;
this->pageOutRate = -1;
this->pageFaultRate = -1;
this->pageFaultsMajorRate = -1;
this->pageFreeRate = -1;
this->pageActivateRate = -1;
this->pageDeactivateRate = -1;
this->memoryReadRate = -1;
this->memoryWriteRate = -1;
this->memoryIoRate = -1;
};
void MemoryMetrics::printMemoryMetrics(){
std::cout << "\n\t[MEMORY METRICS]\n\n"
<< "Memory Used = " << this->memoryUsed << " MB\n"
<< "Memory Cached = " << this->memoryCached << " MB\n"
<< "Swap Used = " << this->swapUsed << " MB\n"
<< "Swap Cached = " << this->swapCached << " MB\n"
<< "Memory Active = " << this->memoryActive << " MB\n"
<< "Memory Inactive = " << this->memoryInactive << " MB\n"
<< "Page Read Rate = " << this->pageInRate << "\n"
<< "Page Save Rate = " << this->pageOutRate << "\n"
<< "Page Fault Rate = " << this->pageFaultRate << "\n"
<< "Page Fault Major Rate = " << this->pageFaultsMajorRate << "\n"
<< "Page Release Rate = " << this->pageFreeRate << "\n"
<< "Page Activate Rate = " << this->pageActivateRate << "\n"
<< "Page Deactivate Rate = " << this->pageDeactivateRate << "\n"
<< "Memory Read Rate = " << this->memoryReadRate << " MB/s\n"
<< "Memory Write Rate = " << this->memoryWriteRate << " MB/s\n"
<< "Memory I/O Rate = " << this->memoryIoRate << " MB/s\n";
};
void getMemoryMetrics(MemoryMetrics &memoryMetrics){
const char* command = "grep -v -e 'anon' -e 'file' /proc/meminfo | grep -E '^(MemTotal|Cached|SwapCached|SwapTotal|SwapFree|Active|Inactive)' | awk '{print $2}'";
std::string output = exec(command), temp, swapFree;
std::stringstream streamOne(output);
streamOne >> temp;
memoryMetrics.memoryUsed = std::stof(temp);
memoryMetrics.memoryUsed = memoryMetrics.memoryUsed / KILOBYTE; // MB
streamOne >> temp;
memoryMetrics.memoryCached = std::stof(temp);
memoryMetrics.memoryCached = memoryMetrics.memoryCached / KILOBYTE; // MB
streamOne >> temp;
memoryMetrics.swapCached = std::stof(temp);
memoryMetrics.swapCached = memoryMetrics.swapCached / KILOBYTE; // MB
streamOne >> temp;
memoryMetrics.memoryActive = std::stof(temp);
memoryMetrics.memoryActive = memoryMetrics.memoryActive / KILOBYTE; // MB
streamOne >> temp;
memoryMetrics.memoryInactive = std::stof(temp);
memoryMetrics.memoryInactive = memoryMetrics.memoryInactive / KILOBYTE; // MB
streamOne >> temp;
streamOne >> swapFree;
memoryMetrics.swapUsed = std::stof(temp) - std::stof(swapFree);
memoryMetrics.swapUsed = memoryMetrics.swapUsed / KILOBYTE; // MB
command = "sar -r -B 1 1 | awk 'NR==4{print $2,$3,$4,$5,$6,$7,$8}'";
output = exec(command);
std::stringstream streamTwo(output);
streamTwo >> temp;
memoryMetrics.pageInRate = std::stof(temp); // pages/sec
streamTwo >> temp;
memoryMetrics.pageOutRate = std::stof(temp); // pages/sec
streamTwo >> temp;
memoryMetrics.pageFaultRate = std::stof(temp); // pages/sec
streamTwo >> temp;
memoryMetrics.pageFaultsMajorRate = std::stof(temp); // pages/sec
streamTwo >> temp;
memoryMetrics.pageFreeRate = std::stof(temp); // pages/sec
streamTwo >> temp;
memoryMetrics.pageActivateRate = std::stof(temp); // kpages/sec
streamTwo >> temp;
memoryMetrics.pageDeactivateRate = std::stof(temp); // kpages/sec
command = "sar -b 1 1 | awk 'NR==4{print $6/1024,$7/1024,($6+$7)/1024}'";
output = exec(command);
std::stringstream streamThree(output);
streamThree >> temp;
memoryMetrics.memoryReadRate = std::stof(temp); // MB/s
streamThree >> temp;
memoryMetrics.memoryWriteRate = std::stof(temp); // MB/s
streamThree >> temp;
memoryMetrics.memoryIoRate = std::stof(temp); // MB/s
//memoryMetrics.printMemoryMetrics();
};
NetworkMetrics::NetworkMetrics(){
this->receivedData = -1;
this->receivePacketRate = -1;
this->sentData = -1;
this->sendPacketsRate = -1;
};
void NetworkMetrics::printNetworkMetrics(){
std::cout << "\n\t[NETWORK METRICS]\n\n"
<< "Receive Packet Rate = " << this->receivePacketRate << " KB/s\n"
<< "Send Packet Rate = " << this->sendPacketsRate << " KB/s\n"
<< "Packets Received = " << this->receivedData << "\n"
<< "Packets Sent = " << this->sentData << "\n";
};
void getNetworkMetrics(NetworkMetrics &networkMetrics){
const char* command = "ifstat 1 1 | tail -1 | awk '{ print $1, $2 }'";
std::string output = exec(command), temp;
std::stringstream streamOne(output);
streamOne >> temp;
networkMetrics.receivePacketRate = std::stof(temp); // KB/sec
streamOne >> temp;
networkMetrics.sendPacketsRate = std::stof(temp); // KB/sec
// Default interface: eth0
// des01 interface: ep0s31f6
command = "cat /proc/net/dev | awk '/^ *enp0s31f6:/ {rx=$3; tx=$11; print rx,tx; exit}'";
output = exec(command);
std::stringstream streamTwo(output);
streamTwo >> temp;
networkMetrics.receivedData = std::stoi(temp); // number of packets
streamTwo >> temp;
networkMetrics.sentData = std::stoi(temp); // number of packets
//networkMetrics.printNetworkMetrics();
};
PowerMetrics::PowerMetrics(){
this->processorPower = -1;
this->memoryPower = -1;
this->systemPower = -1;
this->gpuPower = 1;
this->gpuTemperature = -1;
this->gpuFanSpeed = -1;
this->gpuMemoryTotal = -1;
this->gpuMemoryUsed = -1;
this->gpuMemoryFree = -1;
this->gpuClocksCurrentSM = -1;
this->gpuClocksCurrentMemory = -1;
};
void PowerMetrics::printPowerMetrics(){
std::cout << "\n\t[POWER METRICS]\n"
<< "Processor = " << this->processorPower << "W\n"
<< "Memory = " << this->memoryPower << "W\n"
<< "System = " << this->systemPower << "W\n"
<< "GPU = " << this->gpuPower << "W\n"
<< "GPU Temperature = " << this->gpuTemperature << "C\n"
<< "GPU Fan Speed = " << this->gpuFanSpeed << "%\n"
<< "GPU Memory Total = " << this->gpuMemoryTotal << "MB\n"
<< "GPU Memory Used = " << this->gpuMemoryUsed << "MB\n"
<< "GPU Memory Free = " << this->gpuMemoryFree << "MB\n"
<< "GPU Clocks Current SM = " << this->gpuClocksCurrentSM << "MHz\n"
<< "GPU Clocks Current Memory = " << this->gpuClocksCurrentMemory << "MHz\n";
};
void getPowerMetrics(PowerMetrics &powerMetrics){
const char* command = "perf stat -e power/energy-cores/,power/energy-ram/,power/energy-pkg/ sleep 1 2>&1 | awk '/Joules/ {print $1}' | tr ',' '.'";
std::string output = exec(command), temp;
std::stringstream streamOne(output);
streamOne >> temp;
powerMetrics.processorPower = std::stof(temp);
streamOne >> temp;
powerMetrics.memoryPower = std::stof(temp);
streamOne >> temp;
powerMetrics.systemPower = std::stof(temp);
command = "nvidia-smi --query-gpu=power.draw,temperature.gpu,fan.speed,memory.total,memory.used,memory.free,clocks.current.sm,clocks.current.memory --format=csv,nounits,noheader | tr ',' ' '";
output = exec(command);
std::stringstream streamTwo(output);
streamTwo >> temp;
powerMetrics.gpuPower = std::stof(temp);
streamTwo >> temp >> temp;
powerMetrics.gpuTemperature = std::stof(temp);
streamTwo >> temp >> temp;
powerMetrics.gpuFanSpeed = std::stof(temp);
streamTwo >> temp >> temp;
powerMetrics.gpuMemoryTotal = std::stof(temp);
streamTwo >> temp >> temp;
powerMetrics.gpuMemoryUsed = std::stof(temp);
streamTwo >> temp >> temp;
powerMetrics.gpuMemoryFree = std::stof(temp);
streamTwo >> temp >> temp;
powerMetrics.gpuClocksCurrentSM = std::stof(temp);
streamTwo >> temp >> temp;
powerMetrics.gpuClocksCurrentMemory = std::stof(temp);
//powerMetrics.printPowerMetrics();
};
AllMetrics::AllMetrics(){
this->systemMetrics = SystemMetrics();
this->processorMetrics = ProcessorMetrics();
this->inputOutputMetrics = InputOutputMetrics();
this->memoryMetrics = MemoryMetrics();
this->networkMetrics = NetworkMetrics();
this->powerMetrics = PowerMetrics();
};
// Execute a Linux command and return the output using std::string
std::string exec(const char* cmd){
std::array<char, 128> buffer;
std::string result;
std::unique_ptr<FILE, decltype(&pclose)> pipe(popen(cmd, "r"), pclose);
if (!pipe)
throw std::runtime_error("popen() failed!");
while (fgets(buffer.data(), buffer.size(), pipe.get()) != nullptr)
result += buffer.data();
if(!result.length())
std::cout << "\n\n\t[ERROR] String returned by exec() has length 0\n";
return result;
};