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 char *get_product_name(int id, int type)
46 if (type & SENSOR_TYPE_FAN)
47 return strdup("NVIDIA");
49 res = XNVCTRLQueryTargetStringAttribute(display,
50 NV_CTRL_TARGET_TYPE_GPU,
53 NV_CTRL_STRING_PRODUCT_NAME,
56 if (strcmp(name, "Unknown"))
59 log_err(_("%s: Unknown NVIDIA product name for GPU %d"),
65 "Failed to retrieve NVIDIA product name for GPU %d"),
70 return strdup("NVIDIA");
73 static double get_att(int target, int id, int att)
78 res = XNVCTRLQueryTargetAttribute(display, target, id, 0, att, &temp);
83 return UNKNOWN_DBL_VALUE;
86 static double get_usage_att(char *atts, const char *att)
88 char *c, *key, *strv, *s;
94 v = UNKNOWN_DBL_VALUE;
119 strv = strndup(s, n);
120 if (!strcmp(key, att))
126 if (v != UNKNOWN_DBL_VALUE)
129 while (*c && (*c == ' ' || *c == ','))
136 static const char *get_nvidia_type_str(int type)
138 if (type & SENSOR_TYPE_GRAPHICS)
141 if (type & SENSOR_TYPE_VIDEO)
144 if (type & SENSOR_TYPE_MEMORY)
147 if (type & SENSOR_TYPE_PCIE)
150 if (type & SENSOR_TYPE_AMBIENT)
153 if (type & SENSOR_TYPE_TEMP)
156 if (type & SENSOR_TYPE_FAN) {
157 if (type & SENSOR_TYPE_RPM)
166 static double get_usage(int id, int type)
173 stype = get_nvidia_type_str(type);
176 return UNKNOWN_DBL_VALUE;
178 res = XNVCTRLQueryTargetStringAttribute(display,
179 NV_CTRL_TARGET_TYPE_GPU,
182 NV_CTRL_STRING_GPU_UTILIZATION,
186 return UNKNOWN_DBL_VALUE;
188 v = get_usage_att(atts, stype);
195 static double get_value(int id, int type)
199 if (type & SENSOR_TYPE_TEMP) {
200 if (type & SENSOR_TYPE_AMBIENT)
201 att = NV_CTRL_AMBIENT_TEMPERATURE;
203 att = NV_CTRL_GPU_CORE_TEMPERATURE;
205 return get_att(NV_CTRL_TARGET_TYPE_GPU, id, att);
206 } else if (type & SENSOR_TYPE_FAN) {
207 if (type & SENSOR_TYPE_RPM)
208 return get_att(NV_CTRL_TARGET_TYPE_COOLER,
210 NV_CTRL_THERMAL_COOLER_SPEED);
211 else /* SENSOR_TYPE_PERCENT */
212 return get_att(NV_CTRL_TARGET_TYPE_COOLER,
214 NV_CTRL_THERMAL_COOLER_LEVEL);
215 } else { /* SENSOR_TYPE_PERCENT */
216 return get_usage(id, type);
220 static void update(struct psensor *sensor)
224 v = get_value(sensor->nvidia_id, sensor->type);
226 if (v == UNKNOWN_DBL_VALUE)
227 log_err(_("%s: Failed to retrieve measure of type %x "
228 "for NVIDIA GPU %d"),
232 psensor_set_current_value(sensor, v);
235 static int check_sensor(int id, int type)
237 return get_value(id, type) != UNKNOWN_DBL_VALUE;
240 static char *i2str(int i)
245 /* second +1 to avoid issue about the conversion of a double
247 n = 1 + (ceil(log10(INT_MAX)) + 1) + 1;
250 snprintf(str, n, "%d", i);
255 static struct psensor *create_nvidia_sensor(int id, int subtype, int value_len)
257 char *pname, *name, *strnid, *sid;
263 type = SENSOR_TYPE_NVCTRL | subtype;
265 if (!check_sensor(id, type))
268 pname = get_product_name(id, type);
270 stype = get_nvidia_type_str(type);
272 n = strlen(pname) + 1 + strlen(strnid) + 1 + strlen(stype) + 1;
274 sprintf(name, "%s %s %s", pname, strnid, stype);
276 sid = malloc(strlen(PROVIDER_NAME) + 1 + strlen(name) + 1);
277 sprintf(sid, "%s %s", PROVIDER_NAME, name);
279 s = psensor_create(sid, name, pname, type, value_len);
287 static int init(void)
291 display = XOpenDisplay(NULL);
294 log_err(_("%s: Cannot open connection to X11 server."),
299 if (XNVCTRLQueryExtension(display, &evt, &err))
302 log_err(_("%s: Failed to retrieve NVIDIA information."),
308 void nvidia_psensor_list_update(struct psensor **sensors)
315 if (!(s->type & SENSOR_TYPE_REMOTE)
316 && s->type & SENSOR_TYPE_NVCTRL)
323 static void add(struct psensor ***sensors, int id, int type, int values_len)
327 s = create_nvidia_sensor(id, type, values_len);
330 psensor_list_append(sensors, s);
333 void nvidia_psensor_list_append(struct psensor ***ss, int values_len)
341 ret = XNVCTRLQueryTargetCount(display, NV_CTRL_TARGET_TYPE_GPU, &n);
343 for (i = 0; i < n; i++) {
346 SENSOR_TYPE_GPU | SENSOR_TYPE_TEMP,
349 utype = SENSOR_TYPE_GPU | SENSOR_TYPE_PERCENT;
350 add(ss, i, utype | SENSOR_TYPE_AMBIENT, values_len);
351 add(ss, i, utype | SENSOR_TYPE_GRAPHICS, values_len);
352 add(ss, i, utype | SENSOR_TYPE_VIDEO, values_len);
353 add(ss, i, utype | SENSOR_TYPE_MEMORY, values_len);
354 add(ss, i, utype | SENSOR_TYPE_PCIE, values_len);
358 ret = XNVCTRLQueryTargetCount(display, NV_CTRL_TARGET_TYPE_COOLER, &n);
360 log_fct("%s: Number of fans: %d", PROVIDER_NAME, n);
361 for (i = 0; i < n; i++) {
362 utype = SENSOR_TYPE_FAN | SENSOR_TYPE_RPM;
363 if (check_sensor(i, utype))
364 add(ss, i, utype, values_len);
366 utype = SENSOR_TYPE_FAN | SENSOR_TYPE_PERCENT;
367 if (check_sensor(i, utype))
368 add(ss, i, utype, values_len);
371 log_err(_("%s: Failed to retrieve number of fans."),
376 void nvidia_cleanup(void)
379 XCloseDisplay(display);