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>
38 static char *get_product_name(int id, int type)
43 if (type & SENSOR_TYPE_FAN)
44 return strdup("NVIDIA");
46 res = XNVCTRLQueryTargetStringAttribute(display,
47 NV_CTRL_TARGET_TYPE_GPU,
50 NV_CTRL_STRING_PRODUCT_NAME,
53 if (strcmp(name, "Unknown"))
56 log_err(_("Unknown NVIDIA product name for GPU %d"), id);
59 log_err(_("Failed to retrieve NVIDIA product name for GPU %d"),
63 return strdup("NVIDIA");
66 static double get_att(int target, int id, int att)
71 res = XNVCTRLQueryTargetAttribute(display, target, id, 0, att, &temp);
76 return UNKNOWN_DBL_VALUE;
79 static double get_usage_att(char *atts, const char *att)
81 char *c, *key, *strv, *s;
87 v = UNKNOWN_DBL_VALUE;
112 strv = strndup(s, n);
113 if (!strcmp(key, att))
119 if (v != UNKNOWN_DBL_VALUE)
122 while (*c && (*c == ' ' || *c == ','))
129 static const char *get_nvidia_type_str(int type)
131 if (type & SENSOR_TYPE_GRAPHICS)
134 if (type & SENSOR_TYPE_VIDEO)
137 if (type & SENSOR_TYPE_MEMORY)
140 if (type & SENSOR_TYPE_PCIE)
143 if (type & SENSOR_TYPE_AMBIENT)
146 if (type & SENSOR_TYPE_TEMP)
149 if (type & SENSOR_TYPE_FAN) {
150 if (type & SENSOR_TYPE_RPM)
159 static double get_usage(int id, int type)
166 stype = get_nvidia_type_str(type);
169 return UNKNOWN_DBL_VALUE;
171 res = XNVCTRLQueryTargetStringAttribute(display,
172 NV_CTRL_TARGET_TYPE_GPU,
175 NV_CTRL_STRING_GPU_UTILIZATION,
179 return UNKNOWN_DBL_VALUE;
181 v = get_usage_att(atts, stype);
188 static double get_value(int id, int type)
192 if (type & SENSOR_TYPE_TEMP) {
193 if (type & SENSOR_TYPE_AMBIENT)
194 att = NV_CTRL_AMBIENT_TEMPERATURE;
196 att = NV_CTRL_GPU_CORE_TEMPERATURE;
198 return get_att(NV_CTRL_TARGET_TYPE_GPU, id, att);
199 } else if (type & SENSOR_TYPE_FAN) {
200 if (type & SENSOR_TYPE_RPM)
201 return get_att(NV_CTRL_TARGET_TYPE_COOLER,
203 NV_CTRL_THERMAL_COOLER_SPEED);
204 else /* SENSOR_TYPE_PERCENT */
205 return get_att(NV_CTRL_TARGET_TYPE_COOLER,
207 NV_CTRL_THERMAL_COOLER_LEVEL);
208 } else { /* SENSOR_TYPE_PERCENT */
209 return get_usage(id, type);
213 static void update(struct psensor *sensor)
217 v = get_value(sensor->nvidia_id, sensor->type);
219 if (v == UNKNOWN_DBL_VALUE)
220 log_err(_("Failed to retrieve measure of type %x "
221 "for NVIDIA GPU %d"),
224 psensor_set_current_value(sensor, v);
227 static int check_sensor(int id, int type)
229 return get_value(id, type) != UNKNOWN_DBL_VALUE;
232 static char *i2str(int i)
237 /* second +1 to avoid issue about the conversion of a double
239 n = 1 + (ceil(log10(INT_MAX)) + 1) + 1;
242 snprintf(str, n, "%d", i);
247 static struct psensor *create_nvidia_sensor(int id, int subtype, int value_len)
249 char *pname, *name, *strnid, *sid;
255 type = SENSOR_TYPE_NVCTRL | subtype;
257 if (!check_sensor(id, type))
260 pname = get_product_name(id, type);
262 stype = get_nvidia_type_str(type);
264 n = strlen(pname) + 1 + strlen(strnid) + 1 + strlen(stype) + 1;
266 sprintf(name, "%s %s %s", pname, strnid, stype);
268 sid = malloc(strlen("nvidia") + 1 + strlen(name) + 1);
269 sprintf(sid, "nvidia %s", name);
271 s = psensor_create(sid, name, pname, type, value_len);
283 display = XOpenDisplay(NULL);
286 log_err(_("Cannot open connection to X11 server."));
290 if (XNVCTRLQueryExtension(display, &evt, &err))
293 log_err(_("Failed to retrieve NVIDIA information."));
298 void nvidia_psensor_list_update(struct psensor **sensors)
300 struct psensor **ss, *s;
306 if (s->type & SENSOR_TYPE_NVCTRL)
313 static void add(struct psensor ***sensors, int id, int type, int values_len)
317 s = create_nvidia_sensor(id, type, values_len);
320 psensor_list_append(sensors, s);
324 nvidia_psensor_list_add(struct psensor **ss, int values_len)
332 ret = XNVCTRLQueryTargetCount(display, NV_CTRL_TARGET_TYPE_GPU, &n);
334 for (i = 0; i < n; i++) {
337 SENSOR_TYPE_GPU | SENSOR_TYPE_TEMP,
340 utype = SENSOR_TYPE_GPU | SENSOR_TYPE_PERCENT;
341 add(&ss, i, utype | SENSOR_TYPE_AMBIENT, values_len);
342 add(&ss, i, utype | SENSOR_TYPE_GRAPHICS, values_len);
343 add(&ss, i, utype | SENSOR_TYPE_VIDEO, values_len);
344 add(&ss, i, utype | SENSOR_TYPE_MEMORY, values_len);
345 add(&ss, i, utype | SENSOR_TYPE_PCIE, values_len);
349 ret = XNVCTRLQueryTargetCount(display, NV_CTRL_TARGET_TYPE_COOLER, &n);
351 log_debug("NVIDIA: number of fans: %d", n);
352 for (i = 0; i < n; i++) {
353 utype = SENSOR_TYPE_FAN | SENSOR_TYPE_RPM;
354 if (check_sensor(i, utype))
355 add(&ss, i, utype, values_len);
357 utype = SENSOR_TYPE_FAN | SENSOR_TYPE_PERCENT;
358 if (check_sensor(i, utype))
359 add(&ss, i, utype, values_len);
362 log_err(_("NVIDIA: failed to retrieve number of fans."));
368 void nvidia_cleanup()
371 XCloseDisplay(display);