-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
240 lines (203 loc) · 5.43 KB
/
main.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
#include <stdio.h>
#include <cstring>
#include <vector>
#include <chrono>
#include <ctime>
#include <thread>
#include <nvml.h>
#define checkError(err) \
if ((err) != NVML_SUCCESS) { \
printf("ERROR: An error has occurred on line %d. Error code: %d. Terminating!\n", __LINE__, err); \
nvmlShutdown(); \
return (err); \
}
struct GPUDevice {
GPUDevice();
int init();
int update();
void print();
int index;
std::string name;
unsigned fanSpeed;
unsigned power;
unsigned temperature;
nvmlUtilization_t utilization;
nvmlDriverModel_t dmCurrent;
nvmlDriverModel_t dmPending;
nvmlMemory_t memory;
nvmlDevice_t handle;
};
GPUDevice::GPUDevice() {
//blank
memset(this, 0, sizeof(GPUDevice));
}
int GPUDevice::init() {
nvmlReturn_t res;
res = nvmlDeviceGetHandleByIndex(index, &handle);
checkError(res);
char temp[256];
res = nvmlDeviceGetName(handle, temp, 256);
checkError(res);
name = temp;
const int GPU_NAME_TEXT_FIELD_SIZE = 22;
if (name.size() != GPU_NAME_TEXT_FIELD_SIZE) {
if (name.size() < GPU_NAME_TEXT_FIELD_SIZE) {
while (name.size() != GPU_NAME_TEXT_FIELD_SIZE) {
name.append(" ");
}
}
}
res = nvmlDeviceGetDriverModel(handle, &dmCurrent, &dmPending);
if(res != NVML_SUCCESS) {
printf("%s[%d] ERROR: Could not obtain Driver Model\n", __FUNCTION__, __LINE__);
}
return update();
}
int GPUDevice::update() {
nvmlReturn_t res;
res = nvmlDeviceGetFanSpeed(handle, &fanSpeed);
if (res != NVML_SUCCESS) {
fanSpeed = unsigned(-1);
res = NVML_SUCCESS;
}
checkError(res);
res = nvmlDeviceGetMemoryInfo(handle, &memory);
checkError(res);
res = nvmlDeviceGetPowerUsage(handle, &power);
if (res != NVML_SUCCESS) {
power = unsigned(-1);
res = NVML_SUCCESS;
} else {
//Result is in milliwatts so convert it to watts
power /= 1000;
}
checkError(res);
res = nvmlDeviceGetTemperature(handle, NVML_TEMPERATURE_GPU, &temperature);
checkError(res);
res = nvmlDeviceGetUtilizationRates(handle, &utilization);
checkError(res);
}
struct DeviceManager {
public:
DeviceManager();
~DeviceManager();
void print();
int update();
bool isValid() const { return valid; }
private:
int init();
std::string driverVersion;
unsigned int numDevices;
std::vector<GPUDevice> devices;
bool valid;
};
int DeviceManager::init() {
nvmlReturn_t err;
err = nvmlInit();
if (err == NVML_ERROR_DRIVER_NOT_LOADED) {
printf("ERROR: NVidia driver is not running. Initialization failed.\n");
} else if(err == NVML_ERROR_NO_PERMISSION) {
printf("ERROR: NVML does not have permission to talk to the driver. Initialization failed.\n");
} else if(err == NVML_ERROR_UNKNOWN) {
printf("ERROR: NVML encounted an unexpected error during initialization. Initialization failed.\n");
} else if(err == NVML_SUCCESS) {
char temp[256];
err = nvmlSystemGetDriverVersion(temp, 256);
checkError(err);
driverVersion = temp;
err = nvmlDeviceGetCount(&numDevices);
checkError(err);
devices.resize(numDevices);
for (int i = 0; i < numDevices; i++) {
GPUDevice &d = devices[i];
d.index = i;
d.init();
}
}
return 0;
}
DeviceManager::DeviceManager() :
numDevices(0),
driverVersion("Unknown"),
valid(0)
{
if (0 == init()) {
valid = 1;
}
}
DeviceManager::~DeviceManager() {
nvmlReturn_t err;
err = nvmlShutdown();
}
int DeviceManager::update() {
for (auto &d : devices) {
if (d.update()) {
valid = 0;
break;
}
}
return !valid;
}
//NVML_DEVICE_NAME_BUFFER_SIZE
void DeviceManager::print() {
std::time_t time = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
char *hrTime = hrTime = ctime(&time);
printf ("%s", hrTime);
printf("+-----------------------------------------------------------------------------+\n");
printf("| NVidia driver version: %s Device count : %2d |\n", driverVersion.c_str(), numDevices);
printf("|---------------------------------+---------------+---------------------------+\n");
printf("| Idx Name TCC/WDDM | Memory-usage | Temp Fan Power Util |\n");
printf("+-----------------------------------------------------------------------------+\n");
for (auto &d : devices) {
d.print();
}
printf("+-----------------------------------------------------------------------------+\n");
}
void GPUDevice::print() {
std::string driverMode = (dmCurrent == NVML_DRIVER_WDDM) ? "WDDM" : "TCC ";
char fan[10];
if (fanSpeed != unsigned(-1)) {
snprintf(fan, 10, " %3d%%" , fanSpeed);
} else {
snprintf(fan, 10, " N/A ");
}
char pow[10];
if (power != unsigned(-1)) {
snprintf(pow, 10, " %3dW" , power);
} else {
snprintf(pow, 10, " N/A ");
}
printf("| %2d %s %s | %5d / %5d | %3dC %s %s %3d%% |\n",
index, name.c_str(), driverMode.c_str(),
memory.used / (1024*1024), memory.total / (1024*1024),
temperature, fan, pow, utilization.gpu
);
}
#ifdef _WIN32
#include <Windows.h>
void moveCursorTo( int x, int y ) {
const HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
if (handle == INVALID_HANDLE_VALUE) {
return;
}
//Hide the cursor
const CONSOLE_CURSOR_INFO lpConsoleCursorInfo = {1, FALSE};
SetConsoleCursorInfo(handle, &lpConsoleCursorInfo);
const COORD p = { x, y };
SetConsoleCursorPosition(handle, p );
}
#else //!_WIN32
void moveCursorTo( int x, int y ) {
printf("\033[%d;%dH", y+1, x+1);
}
#endif //!_WIN32
int main() {
DeviceManager devMan;
while(devMan.isValid()) {
moveCursorTo(0, 0);
devMan.print();
std::this_thread::sleep_for(std::chrono::milliseconds(500));
devMan.update();
}
return 0;
}