2 * Copyright (C) 2010-2014 jeanfi@gmail.com
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #define _(str) gettext(str)
29 #include <NVCtrl/NVCtrl.h>
30 #include <NVCtrl/NVCtrlLib.h>
36 /* Returns the temperature (Celsius) of a NVIDIA GPU. */
37 static int get_temp(struct psensor *sensor)
42 res = XNVCTRLQueryTargetAttribute(display,
43 NV_CTRL_TARGET_TYPE_GPU,
46 NV_CTRL_GPU_CORE_TEMPERATURE,
52 log_debug(_("NVIDIA proprietary driver not used or cannot "
53 "retrieve NVIDIA GPU temperature."));
57 static double get_usage_att(char *atts, char *att)
59 char *c, *key, *strv, *s;
65 v = UNKNOWN_DBL_VALUE;
91 if (!strcmp(key, att))
97 if (v != UNKNOWN_DBL_VALUE)
100 while (*c && (*c == ' ' || *c == ','))
107 static int get_usage(struct psensor *sensor)
112 res = XNVCTRLQueryTargetStringAttribute(display,
113 NV_CTRL_TARGET_TYPE_GPU,
116 NV_CTRL_STRING_GPU_UTILIZATION,
120 if (sensor->type & SENSOR_TYPE_GRAPHICS)
121 return get_usage_att(temp, "graphics");
122 else if (sensor->type & SENSOR_TYPE_VIDEO)
123 return get_usage_att(temp, "video");
124 else if (sensor->type & SENSOR_TYPE_MEMORY)
125 return get_usage_att(temp, "memory");
126 else if (sensor->type & SENSOR_TYPE_PCIE)
127 return get_usage_att(temp, "PCIe");
130 log_debug(_("NVIDIA proprietary driver not used or cannot "
131 "retrieve NVIDIA GPU usage."));
135 static struct psensor *create_temp_sensor(int id, int values_len)
142 sprintf(name, "GPU%d", id);
144 sid = malloc(strlen("NVIDIA") + 1 + strlen(name) + 1);
145 sprintf(sid, "NVIDIA %s", name);
147 t = SENSOR_TYPE_NVCTRL | SENSOR_TYPE_GPU | SENSOR_TYPE_TEMP;
149 s = psensor_create(sid,
151 strdup(_("NVIDIA GPU")),
160 static struct psensor *create_usage_sensor(int id,
169 if (subtype & SENSOR_TYPE_GRAPHICS)
170 sprintf(name, "GPU%d graphics", id);
171 else if (subtype & SENSOR_TYPE_MEMORY)
172 sprintf(name, "GPU%d memory", id);
173 else if (subtype & SENSOR_TYPE_VIDEO)
174 sprintf(name, "GPU%d video", id);
175 else /* if (subtype & SENSOR_TYPE_PCIE) */
176 sprintf(name, "GPU%d PCIe", id);
179 sid = malloc(strlen("NVIDIA") + 1 + strlen(name) + 1);
180 sprintf(sid, "NVIDIA %s", name);
182 t = SENSOR_TYPE_NVCTRL | SENSOR_TYPE_GPU | SENSOR_TYPE_USAGE | subtype;
184 s = psensor_create(sid,
186 strdup(_("NVIDIA GPU")),
196 Opens connection to X server and returns the number
199 Return 0 if no NVIDIA gpus or cannot get information.
205 display = XOpenDisplay(NULL);
208 log_err(_("Cannot open connection to X11 server."));
212 if (XNVCTRLQueryExtension(display, &evt, &err) &&
213 XNVCTRLQueryTargetCount(display, NV_CTRL_TARGET_TYPE_GPU, &n))
216 log_err(_("Failed to retrieve NVIDIA information."));
221 void nvidia_psensor_list_update(struct psensor **sensors)
223 struct psensor **ss, *s;
229 if (s->type & SENSOR_TYPE_NVCTRL) {
230 if (s->type & SENSOR_TYPE_TEMP)
231 psensor_set_current_value(s, get_temp(s));
232 else if (s->type & SENSOR_TYPE_USAGE)
233 psensor_set_current_value(s, get_usage(s));
240 struct psensor **nvidia_psensor_list_add(struct psensor **sensors,
244 struct psensor **tmp, **ss, *s;
249 for (i = 0; i < n; i++) {
250 s = create_temp_sensor(i, values_len);
251 tmp = psensor_list_add(ss, s);
256 s = create_usage_sensor(i, SENSOR_TYPE_GRAPHICS, values_len);
257 tmp = psensor_list_add(ss, s);
262 s = create_usage_sensor(i, SENSOR_TYPE_VIDEO, values_len);
263 tmp = psensor_list_add(ss, s);
268 s = create_usage_sensor(i, SENSOR_TYPE_MEMORY, values_len);
269 tmp = psensor_list_add(ss, s);
274 s = create_usage_sensor(i, SENSOR_TYPE_PCIE, values_len);
275 tmp = psensor_list_add(ss, s);
285 void nvidia_cleanup()
288 XCloseDisplay(display);