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)
31 #include <NVCtrl/NVCtrl.h>
32 #include <NVCtrl/NVCtrlLib.h>
37 static Display *display;
39 static const char *PROVIDER_NAME = "nvctrl";
41 static void set_nvidia_id(struct psensor *s, int id)
43 *(int *)s->provider_data = id;
46 static int get_nvidia_id(struct psensor *s)
48 return *(int *)s->provider_data;
51 static char *get_product_name(int id, int type)
56 if (type & SENSOR_TYPE_FAN)
57 return strdup("NVIDIA");
59 res = XNVCTRLQueryTargetStringAttribute(display,
60 NV_CTRL_TARGET_TYPE_GPU,
63 NV_CTRL_STRING_PRODUCT_NAME,
66 if (strcmp(name, "Unknown"))
69 log_err(_("%s: Unknown NVIDIA product name for GPU %d"),
75 "Failed to retrieve NVIDIA product name for GPU %d"),
80 return strdup("NVIDIA");
83 static double get_att(int target, int id, int att)
88 res = XNVCTRLQueryTargetAttribute(display, target, id, 0, att, &temp);
93 return UNKNOWN_DBL_VALUE;
96 static double get_usage_att(char *atts, const char *att)
98 char *c, *key, *strv, *s;
104 v = UNKNOWN_DBL_VALUE;
129 strv = strndup(s, n);
130 if (!strcmp(key, att))
136 if (v != UNKNOWN_DBL_VALUE)
139 while (*c && (*c == ' ' || *c == ','))
146 static const char *get_nvidia_type_str(int type)
148 if (type & SENSOR_TYPE_GRAPHICS)
151 if (type & SENSOR_TYPE_VIDEO)
154 if (type & SENSOR_TYPE_MEMORY)
157 if (type & SENSOR_TYPE_PCIE)
160 if (type & SENSOR_TYPE_AMBIENT)
163 if (type & SENSOR_TYPE_TEMP)
166 if (type & SENSOR_TYPE_FAN) {
167 if (type & SENSOR_TYPE_RPM)
176 static double get_usage(int id, int type)
183 stype = get_nvidia_type_str(type);
186 return UNKNOWN_DBL_VALUE;
188 res = XNVCTRLQueryTargetStringAttribute(display,
189 NV_CTRL_TARGET_TYPE_GPU,
192 NV_CTRL_STRING_GPU_UTILIZATION,
196 return UNKNOWN_DBL_VALUE;
198 v = get_usage_att(atts, stype);
205 static double get_value(int id, int type)
209 if (type & SENSOR_TYPE_TEMP) {
210 if (type & SENSOR_TYPE_AMBIENT)
211 att = NV_CTRL_AMBIENT_TEMPERATURE;
213 att = NV_CTRL_GPU_CORE_TEMPERATURE;
215 return get_att(NV_CTRL_TARGET_TYPE_GPU, id, att);
216 } else if (type & SENSOR_TYPE_FAN) {
217 if (type & SENSOR_TYPE_RPM)
218 return get_att(NV_CTRL_TARGET_TYPE_COOLER,
220 NV_CTRL_THERMAL_COOLER_SPEED);
221 else /* SENSOR_TYPE_PERCENT */
222 return get_att(NV_CTRL_TARGET_TYPE_COOLER,
224 NV_CTRL_THERMAL_COOLER_LEVEL);
225 } else { /* SENSOR_TYPE_PERCENT */
226 return get_usage(id, type);
230 static void update(struct psensor *sensor)
235 id = get_nvidia_id(sensor);
237 v = get_value(id, sensor->type);
239 if (v == UNKNOWN_DBL_VALUE)
240 log_err(_("%s: Failed to retrieve measure of type %x "
241 "for NVIDIA GPU %d"),
245 psensor_set_current_value(sensor, v);
248 static int check_sensor(int id, int type)
250 return get_value(id, type) != UNKNOWN_DBL_VALUE;
253 static char *i2str(int i)
258 /* second +1 to avoid issue about the conversion of a double
260 n = 1 + (ceil(log10(INT_MAX)) + 1) + 1;
263 snprintf(str, n, "%d", i);
268 static struct psensor *create_nvidia_sensor(int id, int subtype, int value_len)
270 char *pname, *name, *strnid, *sid;
277 type = SENSOR_TYPE_NVCTRL | subtype;
279 if (!check_sensor(id, type))
282 pname = get_product_name(id, type);
284 stype = get_nvidia_type_str(type);
286 n = strlen(pname) + 1 + strlen(strnid) + 1 + strlen(stype) + 1;
288 sprintf(name, "%s %s %s", pname, strnid, stype);
290 sid = malloc(strlen(PROVIDER_NAME) + 1 + strlen(name) + 1);
291 sprintf(sid, "%s %s", PROVIDER_NAME, name);
293 s = psensor_create(sid, name, pname, type, value_len);
294 s->provider_data = malloc(sizeof(int));
295 set_nvidia_id(s, id);
297 if ((type & SENSOR_TYPE_GPU) && (type & SENSOR_TYPE_TEMP)) {
298 v = get_att(NV_CTRL_TARGET_TYPE_GPU,
300 NV_CTRL_GPU_CORE_THRESHOLD);
309 static int init(void)
313 display = XOpenDisplay(NULL);
316 log_err(_("%s: Cannot open connection to X11 server."),
321 if (XNVCTRLQueryExtension(display, &evt, &err))
324 log_err(_("%s: Failed to retrieve NVIDIA information."),
330 void nvidia_psensor_list_update(struct psensor **sensors)
337 if (!(s->type & SENSOR_TYPE_REMOTE)
338 && s->type & SENSOR_TYPE_NVCTRL)
345 static void add(struct psensor ***sensors, int id, int type, int values_len)
349 s = create_nvidia_sensor(id, type, values_len);
352 psensor_list_append(sensors, s);
355 void nvidia_psensor_list_append(struct psensor ***ss, int values_len)
363 ret = XNVCTRLQueryTargetCount(display, NV_CTRL_TARGET_TYPE_GPU, &n);
365 for (i = 0; i < n; i++) {
368 SENSOR_TYPE_GPU | SENSOR_TYPE_TEMP,
371 utype = SENSOR_TYPE_GPU | SENSOR_TYPE_PERCENT;
372 add(ss, i, utype | SENSOR_TYPE_AMBIENT, values_len);
373 add(ss, i, utype | SENSOR_TYPE_GRAPHICS, values_len);
374 add(ss, i, utype | SENSOR_TYPE_VIDEO, values_len);
375 add(ss, i, utype | SENSOR_TYPE_MEMORY, values_len);
376 add(ss, i, utype | SENSOR_TYPE_PCIE, values_len);
380 ret = XNVCTRLQueryTargetCount(display, NV_CTRL_TARGET_TYPE_COOLER, &n);
382 log_fct("%s: Number of fans: %d", PROVIDER_NAME, n);
383 for (i = 0; i < n; i++) {
384 utype = SENSOR_TYPE_FAN | SENSOR_TYPE_RPM;
385 if (check_sensor(i, utype))
386 add(ss, i, utype, values_len);
388 utype = SENSOR_TYPE_FAN | SENSOR_TYPE_PERCENT;
389 if (check_sensor(i, utype))
390 add(ss, i, utype, values_len);
393 log_err(_("%s: Failed to retrieve number of fans."),
398 void nvidia_cleanup(void)
401 XCloseDisplay(display);