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"),
61 log_err(_("Failed to retrieve NVIDIA product name for GPU %d"),
65 return strdup("NVIDIA");
68 static double get_att(int target, int id, int att)
73 res = XNVCTRLQueryTargetAttribute(display, target, id, 0, att, &temp);
78 return UNKNOWN_DBL_VALUE;
81 static double get_usage_att(char *atts, const char *att)
83 char *c, *key, *strv, *s;
89 v = UNKNOWN_DBL_VALUE;
114 strv = strndup(s, n);
115 if (!strcmp(key, att))
121 if (v != UNKNOWN_DBL_VALUE)
124 while (*c && (*c == ' ' || *c == ','))
131 static const char *get_nvidia_type_str(int type)
133 if (type & SENSOR_TYPE_GRAPHICS)
135 else if (type & SENSOR_TYPE_VIDEO)
137 else if (type & SENSOR_TYPE_MEMORY)
139 else if (type & SENSOR_TYPE_PCIE)
141 else if (type & SENSOR_TYPE_AMBIENT)
143 else if (type & SENSOR_TYPE_TEMP)
145 else if (type & SENSOR_TYPE_FAN)
151 static double get_usage(int id, int type)
158 stype = get_nvidia_type_str(type);
161 return UNKNOWN_DBL_VALUE;
163 res = XNVCTRLQueryTargetStringAttribute(display,
164 NV_CTRL_TARGET_TYPE_GPU,
167 NV_CTRL_STRING_GPU_UTILIZATION,
171 return UNKNOWN_DBL_VALUE;
173 v = get_usage_att(atts, stype);
180 static double get_value(int id, int type)
184 if (type & SENSOR_TYPE_TEMP) {
185 if (type & SENSOR_TYPE_AMBIENT)
186 att = NV_CTRL_AMBIENT_TEMPERATURE;
188 att = NV_CTRL_GPU_CORE_TEMPERATURE;
190 return get_att(NV_CTRL_TARGET_TYPE_GPU, id, att);
191 } else if (type & SENSOR_TYPE_FAN) {
192 return get_att(NV_CTRL_TARGET_TYPE_COOLER,
194 NV_CTRL_THERMAL_COOLER_SPEED);
195 } else { /* SENSOR_TYPE_USAGE */
196 return get_usage(id, type);
200 static void update(struct psensor *sensor)
204 v = get_value(sensor->nvidia_id, sensor->type);
206 if (v == UNKNOWN_DBL_VALUE)
207 log_err(_("Failed to retrieve measure of type %x "
208 "for NVIDIA GPU %d"),
211 psensor_set_current_value(sensor, v);
214 static int check_sensor(int id, int type)
216 return get_value(id, type) != UNKNOWN_DBL_VALUE;
219 static char *i2str(int i)
224 /* second +1 to avoid issue about the conversion of a double
226 n = 1 + (ceil(log10(INT_MAX)) + 1) + 1;
229 snprintf(str, n, "%d", i);
234 static struct psensor *create_nvidia_sensor(int id, int subtype, int value_len)
236 char *pname, *name, *strnid, *sid;
242 type = SENSOR_TYPE_NVCTRL | subtype;
244 if (!check_sensor(id, type))
247 pname = get_product_name(id, type);
249 stype = get_nvidia_type_str(type);
251 n = strlen(pname) + 1 + strlen(strnid) + 1 + strlen(stype) + 1;
254 sprintf(name, "%s %s %s", pname, strnid, stype);
256 sid = malloc(strlen("nvidia") + 1 + strlen(name) + 1);
257 sprintf(sid, "nvidia %s", name);
259 s = psensor_create(sid, name, pname, type, value_len);
271 display = XOpenDisplay(NULL);
274 log_err(_("Cannot open connection to X11 server."));
278 if (XNVCTRLQueryExtension(display, &evt, &err))
281 log_err(_("Failed to retrieve NVIDIA information."));
286 void nvidia_psensor_list_update(struct psensor **sensors)
288 struct psensor **ss, *s;
294 if (s->type & SENSOR_TYPE_NVCTRL)
301 static void add(struct psensor ***sensors, int id, int type, int values_len)
303 struct psensor **tmp, *s;
305 s = create_nvidia_sensor(id, type, values_len);
308 tmp = psensor_list_add(*sensors, s);
315 nvidia_psensor_list_add(struct psensor **ss, int values_len)
317 int i, n, utype, rpm;
324 ret = XNVCTRLQueryTargetCount(display, NV_CTRL_TARGET_TYPE_GPU, &n);
326 for (i = 0; i < n; i++) {
329 SENSOR_TYPE_GPU | SENSOR_TYPE_TEMP,
332 utype = SENSOR_TYPE_GPU | SENSOR_TYPE_USAGE;
333 add(&ss, i, utype | SENSOR_TYPE_AMBIENT, values_len);
334 add(&ss, i, utype | SENSOR_TYPE_GRAPHICS, values_len);
335 add(&ss, i, utype | SENSOR_TYPE_VIDEO, values_len);
336 add(&ss, i, utype | SENSOR_TYPE_MEMORY, values_len);
337 add(&ss, i, utype | SENSOR_TYPE_PCIE, values_len);
341 ret = XNVCTRLQueryTargetCount(display, NV_CTRL_TARGET_TYPE_COOLER, &n);
343 log_debug("NVIDIA: number of fans: %d", n);
344 for (i = 0; i < n; i++) {
345 ret = XNVCTRLQueryTargetAttribute
347 NV_CTRL_TARGET_TYPE_COOLER,
350 NV_CTRL_THERMAL_COOLER_SPEED, &rpm);
352 log_debug("NVIDIA: fan speed %d %d", i, rpm);
355 "failed to retrieve fan speed %d"),
358 ret = XNVCTRLQueryTargetAttribute
360 NV_CTRL_TARGET_TYPE_COOLER,
363 NV_CTRL_THERMAL_COOLER_LEVEL, &rpm);
365 log_debug("NVIDIA: fan level %d %d", i, rpm);
368 "failed to retrieve fan level %d"),
372 add(&ss, i, SENSOR_TYPE_FAN, values_len);
375 log_err(_("NVIDIA: failed to retrieve number of fans."));
378 ret = XNVCTRLQueryTargetCount(display, NV_CTRL_TARGET_TYPE_VCSC, &n);
380 log_debug("NVIDIA: number of VCSC: %d", n);
381 for (i = 0; i < n; i++) {
382 ret = XNVCTRLQueryTargetStringAttribute
384 NV_CTRL_TARGET_TYPE_VCSC,
387 NV_CTRL_STRING_VCSC_FAN_STATUS, &str);
389 log_debug("NVIDIA: vcsc fan %d %s", i, str);
392 "failed to retrieve vcsc fan info %d"),
395 add(&ss, i, SENSOR_TYPE_FAN, values_len);
402 void nvidia_cleanup()
405 XCloseDisplay(display);