1 #include "daemon/collectd.h"
2 #include "daemon/common.h"
3 #include "daemon/plugin.h"
9 #define MAX_DEVNAME_LEN 256
10 #define PLUGIN_NAME "gpu_nvml"
12 static nvmlReturn_t nv_status = NVML_SUCCESS;
13 static char *nv_errline = "";
15 #define TRY_CATCH(f, catch) \
16 if ((nv_status = f) != NVML_SUCCESS) { \
20 #define TRY(f) TRY_CATCH(f, catch)
21 #define WRAPGAUGE(x) ((value_t){.gauge = (gauge_t)(x)})
23 static const char *config_keys[] = {
27 static const unsigned int n_config_keys = STATIC_ARRAY_SIZE(config_keys);
29 static uint64_t conf_match_mask = 0;
30 static bool conf_mask_is_exclude = 0;
32 static int nvml_config(const char *key, const char *value) {
34 unsigned long device_ix;
37 if (strcasecmp(key, config_keys[0]) == 0) {
38 device_ix = strtoul(value, &eptr, 10);
45 conf_match_mask |= (1 << device_ix);
46 } else if (strcasecmp(key, config_keys[1])) {
48 IS_TRUE(value) { conf_mask_is_exclude = 1; }
56 static int nvml_init(void) {
60 catch : ERROR("NVML init failed with %d", nv_status);
64 static int nvml_shutdown(void) {
68 catch : ERROR("NVML shutdown failed with %d", nv_status);
72 static void nvml_submit(const char *plugin_instance, const char *type,
73 const char *type_instance, value_t nvml) {
75 value_list_t vl = VALUE_LIST_INIT;
80 sstrncpy(vl.plugin, PLUGIN_NAME, sizeof(vl.plugin));
81 sstrncpy(vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
83 sstrncpy(vl.type, type, sizeof(vl.type));
85 if (type_instance != NULL) {
86 sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance));
89 plugin_dispatch_values(&vl);
92 static int nvml_read(void) {
94 unsigned int device_count;
95 TRY_CATCH(nvmlDeviceGetCount(&device_count), catch_nocount);
97 if (device_count > 64) {
102 char dev_name[MAX_DEVNAME_LEN + 1];
103 unsigned int fan_speed;
104 nvmlUtilization_t utilization;
105 nvmlMemory_t meminfo;
106 unsigned int core_temp;
108 for (int ix = 0; ix < device_count; ix++) {
110 int is_match = ((1 << ix) & conf_match_mask) || (conf_match_mask == 0);
111 if (conf_mask_is_exclude == !!is_match) {
115 TRY(nvmlDeviceGetHandleByIndex(ix, &dev));
118 TRY(nvmlDeviceGetName(dev, dev_name, MAX_DEVNAME_LEN));
120 TRY(nvmlDeviceGetMemoryInfo(dev, &meminfo))
121 TRY(nvmlDeviceGetUtilizationRates(dev, &utilization))
122 TRY(nvmlDeviceGetFanSpeed(dev, &fan_speed))
123 TRY(nvmlDeviceGetTemperature(dev, NVML_TEMPERATURE_GPU, &core_temp))
125 double pct_mem_used = 100. * (double)meminfo.used / meminfo.total;
127 nvml_submit(dev_name, "percent", "GPU", WRAPGAUGE(pct_mem_used));
128 nvml_submit(dev_name, "percent", "GPU", WRAPGAUGE(utilization.gpu));
129 nvml_submit(dev_name, "fanspeed", "GPU", WRAPGAUGE(fan_speed));
130 nvml_submit(dev_name, "temperature", "GPU", WRAPGAUGE(core_temp));
133 catch : WARNING("NVML call \"%s\" failed with code %d!", nv_errline,
141 ERROR("Failed to enumerate NVIDIA GPUs (\"%s\" returned %d)", nv_errline,
146 void module_register(void) {
147 plugin_register_init(PLUGIN_NAME, nvml_init);
148 plugin_register_config(PLUGIN_NAME, nvml_config, config_keys, n_config_keys);
149 plugin_register_read(PLUGIN_NAME, nvml_read);
150 plugin_register_shutdown(PLUGIN_NAME, nvml_shutdown);