-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnvidia_plugin.c
More file actions
399 lines (346 loc) · 13.5 KB
/
Copy pathnvidia_plugin.c
File metadata and controls
399 lines (346 loc) · 13.5 KB
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
/*
* Collectd NVIDIA plugin
*
* Copyright: Roi Sucasas Font, Atos Research and Innovation, 2017.
*
* This code is being developed for the TANGO Project: http://tango-project.eu
* and is licensed under a GNU General Public License, version 3. Please, refer to the LICENSE.TXT file for more information
*
* This plugin has been created taken as a basis the plugin template for collectd written by Sebastian Harl <sh@tokkee.org>
* https://github.com/collectd/collectd/tree/master/contrib/examples/myplugin.c
*
*/
/*
* Notes:
* - plugins are executed in parallel, thus, thread-safe functions need to be used
* - each of the functions below (except module_register) is optional
*/
#if ! HAVE_CONFIG_H
#include <stdlib.h>
#include <string.h>
#ifndef __USE_ISOC99 /* required for NAN */
# define DISABLE_ISOC99 1
# define __USE_ISOC99 1
#endif /* !defined(__USE_ISOC99) */
#include <math.h>
#if DISABLE_ISOC99
# undef DISABLE_ISOC99
# undef __USE_ISOC99
#endif /* DISABLE_ISOC99 */
#include <time.h>
#endif /* ! HAVE_CONFIG */
#include "collectd.h"
#include "common.h"
#include "plugin.h"
#include <stdio.h>
// NVIDIA //
#include "nvml.h"
/*
* This function is called once upon startup to initialize the plugin.
*/
static int my_init(void) {
nvmlReturn_t result;
/* open sockets, initialize data structures, ... */
result = nvmlInit();
if (NVML_SUCCESS != result) {
printf("Failed to initialize NVML: %s\n", nvmlErrorString(result));
plugin_log(LOG_WARNING, "Failed to initialize NVML");
return 1;
}
/* A return value != 0 indicates an error and causes the plugin to be disabled. */
return 0;
}
/*
* submitValue:
* This is a utility function used by the read callback to populate a value_list_t
* and pass it to plugin_dispatch_values.
*/
static int submitValue(gauge_t value, const char *type, unsigned int deviceIndex) {
char strDeviceIndex[8];
/*
* value_list_t: https://collectd.org/wiki/index.php/Value_list
*
* The structure is defined in src/plugin.h as follows:
* struct value_list_s
* {
* value_t *values;
* int values_len;
* cdtime_t time;
* cdtime_t interval;
* char host[DATA_MAX_NAME_LEN];
* char plugin[DATA_MAX_NAME_LEN];
* char plugin_instance[DATA_MAX_NAME_LEN];
* char type[DATA_MAX_NAME_LEN];
* char type_instance[DATA_MAX_NAME_LEN];
* meta_data_t *meta;
* };
* typedef struct value_list_s value_list_t;
*
*
* collectd-unixsock: Hostname/Plugin/Type
* Examples:
* myhost/cpu-0/cpu-user
* myhost/disk-sda/disk_octets
*/
value_list_t vl = VALUE_LIST_INIT;
vl.values = &(value_t) { .gauge = value }; /* Convert the gauge_t to a value_t and add it to the value_list_t. */
vl.values_len = 1;
sprintf(strDeviceIndex, "%d", deviceIndex);
sstrncpy(vl.host, hostname_g, sizeof (vl.host));
sstrncpy(vl.plugin, "nvidia", sizeof (vl.plugin));
sstrncpy(vl.plugin_instance, strDeviceIndex, sizeof (vl.plugin_instance));
sstrncpy(vl.type, type, sizeof (vl.type));
/* dispatch the values to collectd which passes them on to all registered write functions */
return plugin_dispatch_values(&vl);
}
/*
* This function is called in regular intervalls to collect the data.
*/
static int my_read (void) {
nvmlReturn_t result;
unsigned int device_count, i;
result = nvmlDeviceGetCount(&device_count);
if (NVML_SUCCESS != result) {
printf("Failed to query device count: %s\n", nvmlErrorString(result));
plugin_log(LOG_WARNING, "Failed to query device count");
return 0;
}
for (i = 0; i < device_count; i++) {
nvmlDevice_t device;
char name[NVML_DEVICE_NAME_BUFFER_SIZE];
nvmlPciInfo_t pci;
float totalWatts;
unsigned int *power;
unsigned ipower = 1;
power = &ipower;
nvmlUtilization_t nvmlUtilization = { 0 };
unsigned int num_procs = 32;
nvmlProcessInfo_t procs[32];
unsigned int ret = 0;
unsigned int clockMHz;
// Query for device handle to perform operations on a device
// You can also query device handle by other features like: nvmlDeviceGetHandleBySerial, nvmlDeviceGetHandleByPciBusId
result = nvmlDeviceGetHandleByIndex(i, &device);
if (NVML_SUCCESS != result) {
printf("Failed to get handle for device %i: %s\n", i, nvmlErrorString(result));
plugin_log(LOG_WARNING, "Failed to get handle for device");
return 0;
}
result = nvmlDeviceGetName(device, name, NVML_DEVICE_NAME_BUFFER_SIZE);
if (NVML_SUCCESS != result) {
printf("Failed to get name of device %i: %s\n", i, nvmlErrorString(result));
plugin_log(LOG_WARNING, "Failed to get name of device");
return 0;
}
// pci.busId is very useful to know which device physically you're talking to
// Using PCI identifier you can also match nvmlDevice handle to CUDA device.
result = nvmlDeviceGetPciInfo(device, &pci);
if (NVML_SUCCESS != result) {
printf("Failed to get pci info for device %i: %s\n", i, nvmlErrorString(result));
plugin_log(LOG_WARNING, "Failed to get pci info for device");
return 0;
}
/***************************************************************************************
* POWER:
* The reading is accurate to within a range of +/- 5 watts. It is only available if
* power management mode is supported
***************************************************************************************/
result = (nvmlReturn_t)nvmlDeviceGetPowerUsage(device, power);
if (NVML_SUCCESS != result) {
printf(">> ERROR [nvmlDeviceGetPowerUsage]: %s\n", nvmlErrorString(result));
plugin_log(LOG_WARNING, "Failed to get power usage");
}
else {
totalWatts = *power / 1000.0;
printf(">> power: %f Watts\n", totalWatts);
if (submitValue(totalWatts, "power", i) != 0) {
WARNING("nvidia_plugin plugin: Dispatching a value failed.");
}
}
/***************************************************************************************
* UTILIZATION & MEMORY:
***************************************************************************************/
result = (nvmlReturn_t) nvmlDeviceGetUtilizationRates(device, &nvmlUtilization);
if (NVML_SUCCESS != result) {
printf(">> ERROR [nvmlDeviceGetUtilizationRates]: %s\n", nvmlErrorString(result));
}
else {
printf(">> utilization: gpu=%u, gmem=%u \n", nvmlUtilization.gpu, nvmlUtilization.memory);
/*
* Use values from 'types.db' (/opt/collectd/share/collectd/types.db) or add a new one:
* - new entry
* util value:GAUGE:0:U
* - existing entry:
* percent value:GAUGE:0:100.1
*/
if (submitValue(nvmlUtilization.gpu, "percent", i) != 0) {
WARNING("nvidia_plugin plugin: Dispatching a value [percent] failed.");
}
if (submitValue(nvmlUtilization.memory, "memory", i) != 0) {
WARNING("nvidia_plugin plugin: Dispatching a value [memory] failed.");
}
}
/***************************************************************************************
* TEMPERATURE
***************************************************************************************/
/*
nvmlReturn_t nvmlDeviceGetTemperature (nvmlDevice_t device, nvmlTemperatureSensors_t sensorType, unsigned int *temp)
Retrieves the current temperature readings for the device, in degrees C
*/
result = (nvmlReturn_t) nvmlDeviceGetTemperature(device, NVML_TEMPERATURE_GPU, &ret);
if (NVML_SUCCESS != result) {
printf(">> ERROR [nvmlDeviceGetTemperature]: %s\n", nvmlErrorString(result));
}
else {
printf(">> temperature: %d \n", ret);
// temperature
if (submitValue(ret, "temperature", i) != 0) {
WARNING("nvidia_plugin plugin: Dispatching a value failed.");
}
}
/***************************************************************************************
* RUNNING PROCESSES
***************************************************************************************/
/*
nvmlReturn_t nvmlDeviceGetComputeRunningProcesses (nvmlDevice_t device, unsigned int *infoCount, nvmlProcessInfo_t *infos)
This function returns information only about compute running processes (e.g. CUDA application which have active context).
Any graphics applications (e.g. using OpenGL, DirectX) won't be listed by this function.
*/
result = (nvmlReturn_t) nvmlDeviceGetComputeRunningProcesses(device, &num_procs, procs);
if (NVML_SUCCESS != result) {
printf(">> ERROR [nvmlDeviceGetComputeRunningProcesses]: %s\n", nvmlErrorString(result));
}
else {
printf(">> running processes: %i \n", num_procs);
/*
* Use values from 'types.db' (/opt/collectd/share/collectd/types.db) or add a new one:
* - new entry
* procs value:GAUGE:0:U
* - existing entry:
* objects value:GAUGE:0:U
*/
if (submitValue(num_procs, "objects", i) != 0) {
WARNING("nvidia_plugin plugin: Dispatching a value failed.");
}
}
/*
nvmlReturn_t nvmlDeviceGetGraphicsRunningProcesses (nvmlDevice_t device, unsigned int *infoCount, nvmlProcessInfo_t *infos)
This function returns information only about graphics based processes -eg. applications using OpenGL, DirectX-
*/
result = (nvmlReturn_t) nvmlDeviceGetGraphicsRunningProcesses(device, &num_procs, procs);
if (NVML_SUCCESS != result) {
printf(">> ERROR [nvmlDeviceGetGraphicsRunningProcesses]: %s\n", nvmlErrorString(result));
}
else {
printf(">> running grph processes: %i \n", num_procs);
/*
* Use values from 'types.db' (/opt/collectd/share/collectd/types.db) or add a new one:
* - new entry
* gprocs value:GAUGE:0:U
* - existing entry:
* threads value:GAUGE:0:U
*/
if (submitValue(num_procs, "threads", i) != 0) {
WARNING("nvidia_plugin plugin: Dispatching a value failed.");
}
}
/***************************************************************************************
* FRECUENCY
***************************************************************************************/
/*
* Clock types
* nvmlClockType_t : NVML_CLOCK_SM (SM clock domain), NVML_CLOCK_GRAPHICS (Graphics clock domain), NVML_CLOCK_MEM
enum nvmlClockType_t
Clock types.
All speeds are in Mhz.
Values
NVML_CLOCK_GRAPHICS = 0
Graphics clock domain.
NVML_CLOCK_SM = 1
SM clock domain.
NVML_CLOCK_MEM = 2
Memory clock domain.
NVML_CLOCK_VIDEO = 3
Video encoder/decoder clock domain.
NVML_CLOCK_COUNT
*/
result = (nvmlReturn_t)nvmlDeviceGetClockInfo(device, NVML_CLOCK_SM, &clockMHz);
if (NVML_SUCCESS != result) {
printf(">> ERROR [nvmlDeviceGetClockInfo]: %s\n", nvmlErrorString(result));
}
else {
printf(">> NVML_CLOCK_SM: %d \n", clockMHz);
/*
* Use values from 'types.db' (/opt/collectd/share/collectd/types.db) or add a new one:
* - new entry
* frequency_sm value:GAUGE:0:U
* - existing entry:
* frequency value:GAUGE:0:U
*/
if (submitValue(clockMHz, "frequency_sm", i) != 0) {
WARNING("nvidia_plugin plugin: Dispatching a value failed.");
}
}
result = (nvmlReturn_t)nvmlDeviceGetClockInfo(device, NVML_CLOCK_MEM, &clockMHz);
if (NVML_SUCCESS != result) {
printf(">> ERROR [nvmlDeviceGetClockInfo]: %s\n", nvmlErrorString(result));
}
else {
printf(">> NVML_CLOCK_MEM: %d \n", clockMHz);
/*
* Use values from 'types.db' (/opt/collectd/share/collectd/types.db) or add a new one:
* - new entry
* frequency_mem value:GAUGE:0:U
* - existing entry:
* frequency value:GAUGE:0:U
*/
if (submitValue(clockMHz, "frequency_mem", i) != 0) {
WARNING("nvidia_plugin plugin: Dispatching a value failed.");
}
}
result = (nvmlReturn_t)nvmlDeviceGetClockInfo(device, NVML_CLOCK_GRAPHICS, &clockMHz);
if (NVML_SUCCESS != result) {
printf(">> ERROR [nvmlDeviceGetClockInfo]: %s\n", nvmlErrorString(result));
}
else {
printf(">> NVML_CLOCK_GRAPHICS: %d \n", clockMHz);
/*
* Use values from 'types.db' (/opt/collectd/share/collectd/types.db) or add a new one:
* - new entries
* frequency_gr value:GAUGE:0:U
* frequency_mem value:GAUGE:0:U
* frequency_sm value:GAUGE:0:U
* - existing entry:
* frequency value:GAUGE:0:U
*/
if (submitValue(clockMHz, "frequency_gr", i) != 0) {
WARNING("nvidia_plugin plugin: Dispatching a value failed.");
}
}
}
/* A return value != 0 indicates an error and the plugin will be skipped for an increasing amount of time. */
return 0;
}
/*
* This function is called before shutting down collectd.
*/
static int my_shutdown(void) {
nvmlReturn_t result;
/* close sockets, free data structures, ... */
result = nvmlShutdown();
if (NVML_SUCCESS != result) {
printf("Failed to shutdown NVML: %s\n", nvmlErrorString(result));
plugin_log(LOG_WARNING, "Failed to shutdown NVML");
return -1;
}
return 0;
}
/*
* This function is called after loading the plugin to register it with collectd.
*/
void module_register(void) {
plugin_register_read("nvidia_plugin", my_read);
plugin_register_init("nvidia_plugin", my_init);
plugin_register_shutdown("nvidia_plugin", my_shutdown);
return;
}