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)
43 res = XNVCTRLQueryTargetStringAttribute(display,
44 NV_CTRL_TARGET_TYPE_GPU,
47 NV_CTRL_STRING_PRODUCT_NAME,
50 if (strcmp(name, "Unknown")) {
53 log_err(_("Unknown NVIDIA product name for GPU %d"),
58 log_err(_("Failed to retrieve NVIDIA product name for GPU %d"),
62 return strdup("NVIDIA");
65 static double get_att(int target, int id, int att)
70 res = XNVCTRLQueryTargetAttribute(display, target, id, 0, att, &temp);
75 return UNKNOWN_DBL_VALUE;
78 static double get_usage_att(char *atts, const char *att)
80 char *c, *key, *strv, *s;
86 v = UNKNOWN_DBL_VALUE;
111 strv = strndup(s, n);
112 if (!strcmp(key, att))
118 if (v != UNKNOWN_DBL_VALUE)
121 while (*c && (*c == ' ' || *c == ','))
128 static const char *get_nvidia_type_str(int type)
130 if (type & SENSOR_TYPE_GRAPHICS)
132 else if (type & SENSOR_TYPE_VIDEO)
134 else if (type & SENSOR_TYPE_MEMORY)
136 else if (type & SENSOR_TYPE_PCIE)
138 else if (type & SENSOR_TYPE_AMBIENT)
140 else if (type & SENSOR_TYPE_TEMP)
146 static double get_usage(int id, int type)
153 stype = get_nvidia_type_str(type);
156 return UNKNOWN_DBL_VALUE;
158 res = XNVCTRLQueryTargetStringAttribute(display,
159 NV_CTRL_TARGET_TYPE_GPU,
162 NV_CTRL_STRING_GPU_UTILIZATION,
166 return UNKNOWN_DBL_VALUE;
168 v = get_usage_att(atts, stype);
175 static double get_value(int id, int type)
179 if (type & SENSOR_TYPE_TEMP) {
180 if (type & SENSOR_TYPE_AMBIENT)
181 att = NV_CTRL_AMBIENT_TEMPERATURE;
183 att = NV_CTRL_GPU_CORE_TEMPERATURE;
185 return get_att(NV_CTRL_TARGET_TYPE_GPU, id, att);
186 } else { /* SENSOR_TYPE_USAGE */
187 return get_usage(id, type);
191 static void update(struct psensor *sensor)
195 v = get_value(sensor->nvidia_id, sensor->type);
197 if (v == UNKNOWN_DBL_VALUE)
198 log_err(_("Failed to retrieve measure of type %x "
199 "for NVIDIA GPU %d"),
202 psensor_set_current_value(sensor, v);
205 static int check_sensor(int id, int type)
207 return get_value(id, type) != UNKNOWN_DBL_VALUE;
210 static char *i2str(int i)
215 /* second +1 to avoid issue about the conversion of a double
217 n = 1 + (ceil(log10(INT_MAX)) + 1) + 1;
220 snprintf(str, n, "%d", i);
225 static struct psensor *create_nvidia_sensor(int id, int subtype, int value_len)
227 char *pname, *name, *strnid, *sid;
233 type = SENSOR_TYPE_NVCTRL | subtype;
235 if (!check_sensor(id, type))
238 pname = get_product_name(id);
240 stype = get_nvidia_type_str(type);
242 n = strlen(pname) + 1 + strlen(strnid) + 1 + strlen(stype) + 1;
245 sprintf(name, "%s %s %s", pname, strnid, stype);
247 sid = malloc(strlen("nvidia") + 1 + strlen(name) + 1);
248 sprintf(sid, "nvidia %s", name);
250 s = psensor_create(sid, name, pname, type, value_len);
262 display = XOpenDisplay(NULL);
265 log_err(_("Cannot open connection to X11 server."));
269 if (XNVCTRLQueryExtension(display, &evt, &err))
272 log_err(_("Failed to retrieve NVIDIA information."));
277 void nvidia_psensor_list_update(struct psensor **sensors)
279 struct psensor **ss, *s;
285 if (s->type & SENSOR_TYPE_NVCTRL)
292 static void add(struct psensor ***sensors, int id, int type, int values_len)
294 struct psensor **tmp, *s;
296 s = create_nvidia_sensor(id, type, values_len);
299 tmp = psensor_list_add(*sensors, s);
306 nvidia_psensor_list_add(struct psensor **ss, int values_len)
308 int i, n, utype, rpm;
314 ret = XNVCTRLQueryTargetCount(display, NV_CTRL_TARGET_TYPE_GPU, &n);
316 for (i = 0; i < n; i++) {
319 SENSOR_TYPE_GPU | SENSOR_TYPE_TEMP,
322 utype = SENSOR_TYPE_GPU | SENSOR_TYPE_USAGE;
324 add(&ss, i, utype | SENSOR_TYPE_AMBIENT, values_len);
326 add(&ss, i, utype | SENSOR_TYPE_GRAPHICS, values_len);
328 add(&ss, i, utype | SENSOR_TYPE_VIDEO, values_len);
330 add(&ss, i, utype | SENSOR_TYPE_MEMORY, values_len);
332 add(&ss, i, utype | SENSOR_TYPE_PCIE, values_len);
336 ret = XNVCTRLQueryTargetCount(display, NV_CTRL_TARGET_TYPE_COOLER, &n);
338 log_debug("NVIDIA: number of fans: %d", n);
339 for (i = 0; i < n; i++) {
340 ret = XNVCTRLQueryTargetAttribute
342 NV_CTRL_TARGET_TYPE_COOLER,
345 NV_CTRL_THERMAL_COOLER_SPEED, &rpm);
347 log_debug("NVIDIA: fan speed %d %d", i, rpm);
349 log_err("NVIDIA: fail to retrieve fan speed %d",
353 log_err(_("Failed to retrieve number of NVIDIA fans."));
359 void nvidia_cleanup()
362 XCloseDisplay(display);