-
Notifications
You must be signed in to change notification settings - Fork 1
/
nvml_monitor.h
75 lines (65 loc) · 1.96 KB
/
nvml_monitor.h
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
#ifndef NVMLMONTHREAD_H_
#define NVMLMONTHREAD_H_
#include <chrono>
#include <nvml.h>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <iostream>
#include <string>
#include <thread>
#include <vector>
#include <cuda_runtime.h>
//Adopted from https://github.com/mnicely/nvml_examples/blob/master/nvmlClass.h
class NVMLMonThread {
public:
nvmlDevice_t devhandle;
std::ofstream fhandle;
typedef struct _datum {
std::time_t timestamp;
uint temperature;
uint power;
nvmlUtilization_t utilization;
uint state;
} datum;
std::vector <datum> log_enum;
bool loop;
uint caller_state;
NVMLMonThread(int devID, std::string const &fname) {
nvmlInit();
nvmlDeviceGetHandleByIndex(devID, &devhandle);
log_enum.reserve(200000);
fhandle.open(fname, std::ios::out);
caller_state = 0;
printf("%li, %li\n", std::chrono::high_resolution_clock::duration::period::num, std::chrono::high_resolution_clock::duration::period::den);
}
~NVMLMonThread() {
nvmlShutdown();
store_log();
}
void log() {
datum point {};
loop = true;
while(loop){
point.timestamp = std::chrono::high_resolution_clock::now( ).time_since_epoch( ).count( );
nvmlDeviceGetTemperature( devhandle, NVML_TEMPERATURE_GPU, &point.temperature);
nvmlDeviceGetPowerUsage(devhandle, &point.power);
nvmlDeviceGetUtilizationRates(devhandle, &point.utilization);
point.state = caller_state;
log_enum.push_back(point);
std::this_thread::sleep_for(std::chrono::milliseconds(2));
}
}
void killThread() {
std::this_thread::sleep_for(std::chrono::seconds(2));
loop = false;
}
void store_log() {
for(int i = 0; i < static_cast<int>(log_enum.size()); i++){
fhandle << log_enum[i].timestamp << ", " << log_enum[i].temperature << ", " << log_enum[i].power << ", " << log_enum[i].utilization.gpu << ", " << log_enum[i].utilization.memory << ", " << log_enum[i].state << "\n";
}
fhandle.close();
}
};
#endif