2 * Copyright (C) 2010-2016 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
261 n = 1 + (ceil(log10(INT_MAX)) + 1) + 1;
264 snprintf(str, n, "%d", i);
269 static struct psensor *create_nvidia_sensor(int id, int subtype, int value_len)
271 char *pname, *name, *strnid, *sid;
278 type = SENSOR_TYPE_NVCTRL | subtype;
280 if (!check_sensor(id, type))
283 pname = get_product_name(id, type);
285 stype = get_nvidia_type_str(type);
287 n = strlen(pname) + 1 + strlen(strnid) + 1 + strlen(stype) + 1;
289 sprintf(name, "%s %s %s", pname, strnid, stype);
291 sid = malloc(strlen(PROVIDER_NAME) + 1 + strlen(name) + 1);
292 sprintf(sid, "%s %s", PROVIDER_NAME, name);
294 s = psensor_create(sid, name, pname, type, value_len);
295 s->provider_data = malloc(sizeof(int));
296 set_nvidia_id(s, id);
298 if ((type & SENSOR_TYPE_GPU) && (type & SENSOR_TYPE_TEMP)) {
299 v = get_att(NV_CTRL_TARGET_TYPE_GPU,
301 NV_CTRL_GPU_CORE_THRESHOLD);
310 static int init(void)
314 display = XOpenDisplay(NULL);
317 log_err(_("%s: Cannot open connection to X11 server."),
322 if (XNVCTRLQueryExtension(display, &evt, &err))
325 log_err(_("%s: Failed to retrieve NVIDIA information."),
331 void nvidia_psensor_list_update(struct psensor **sensors)
338 if (!(s->type & SENSOR_TYPE_REMOTE)
339 && s->type & SENSOR_TYPE_NVCTRL)
346 static void add(struct psensor ***sensors, int id, int type, int values_len)
350 s = create_nvidia_sensor(id, type, values_len);
353 psensor_list_append(sensors, s);
356 void nvidia_psensor_list_append(struct psensor ***ss, int values_len)
364 ret = XNVCTRLQueryTargetCount(display, NV_CTRL_TARGET_TYPE_GPU, &n);
366 for (i = 0; i < n; i++) {
369 SENSOR_TYPE_GPU | SENSOR_TYPE_TEMP,
372 utype = SENSOR_TYPE_GPU | SENSOR_TYPE_PERCENT;
373 add(ss, i, utype | SENSOR_TYPE_AMBIENT, values_len);
374 add(ss, i, utype | SENSOR_TYPE_GRAPHICS, values_len);
375 add(ss, i, utype | SENSOR_TYPE_VIDEO, values_len);
376 add(ss, i, utype | SENSOR_TYPE_MEMORY, values_len);
377 add(ss, i, utype | SENSOR_TYPE_PCIE, values_len);
381 ret = XNVCTRLQueryTargetCount(display, NV_CTRL_TARGET_TYPE_COOLER, &n);
383 log_fct("%s: Number of fans: %d", PROVIDER_NAME, n);
384 for (i = 0; i < n; i++) {
385 utype = SENSOR_TYPE_FAN | SENSOR_TYPE_RPM;
386 if (check_sensor(i, utype))
387 add(ss, i, utype, values_len);
389 utype = SENSOR_TYPE_FAN | SENSOR_TYPE_PERCENT;
390 if (check_sensor(i, utype))
391 add(ss, i, utype, values_len);
394 log_err(_("%s: Failed to retrieve number of fans."),
399 void nvidia_cleanup(void)
402 XCloseDisplay(display);