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 const char *PROVIDER_NAME = "nvctrl";
40 static char *get_product_name(int id, int type)
45 if (type & SENSOR_TYPE_FAN)
46 return strdup("NVIDIA");
48 res = XNVCTRLQueryTargetStringAttribute(display,
49 NV_CTRL_TARGET_TYPE_GPU,
52 NV_CTRL_STRING_PRODUCT_NAME,
55 if (strcmp(name, "Unknown"))
58 log_err(_("%s: Unknown NVIDIA product name for GPU %d"),
64 "Failed to retrieve NVIDIA product name for GPU %d"),
69 return strdup("NVIDIA");
72 static double get_att(int target, int id, int att)
77 res = XNVCTRLQueryTargetAttribute(display, target, id, 0, att, &temp);
82 return UNKNOWN_DBL_VALUE;
85 static double get_usage_att(char *atts, const char *att)
87 char *c, *key, *strv, *s;
93 v = UNKNOWN_DBL_VALUE;
118 strv = strndup(s, n);
119 if (!strcmp(key, att))
125 if (v != UNKNOWN_DBL_VALUE)
128 while (*c && (*c == ' ' || *c == ','))
135 static const char *get_nvidia_type_str(int type)
137 if (type & SENSOR_TYPE_GRAPHICS)
140 if (type & SENSOR_TYPE_VIDEO)
143 if (type & SENSOR_TYPE_MEMORY)
146 if (type & SENSOR_TYPE_PCIE)
149 if (type & SENSOR_TYPE_AMBIENT)
152 if (type & SENSOR_TYPE_TEMP)
155 if (type & SENSOR_TYPE_FAN) {
156 if (type & SENSOR_TYPE_RPM)
165 static double get_usage(int id, int type)
172 stype = get_nvidia_type_str(type);
175 return UNKNOWN_DBL_VALUE;
177 res = XNVCTRLQueryTargetStringAttribute(display,
178 NV_CTRL_TARGET_TYPE_GPU,
181 NV_CTRL_STRING_GPU_UTILIZATION,
185 return UNKNOWN_DBL_VALUE;
187 v = get_usage_att(atts, stype);
194 static double get_value(int id, int type)
198 if (type & SENSOR_TYPE_TEMP) {
199 if (type & SENSOR_TYPE_AMBIENT)
200 att = NV_CTRL_AMBIENT_TEMPERATURE;
202 att = NV_CTRL_GPU_CORE_TEMPERATURE;
204 return get_att(NV_CTRL_TARGET_TYPE_GPU, id, att);
205 } else if (type & SENSOR_TYPE_FAN) {
206 if (type & SENSOR_TYPE_RPM)
207 return get_att(NV_CTRL_TARGET_TYPE_COOLER,
209 NV_CTRL_THERMAL_COOLER_SPEED);
210 else /* SENSOR_TYPE_PERCENT */
211 return get_att(NV_CTRL_TARGET_TYPE_COOLER,
213 NV_CTRL_THERMAL_COOLER_LEVEL);
214 } else { /* SENSOR_TYPE_PERCENT */
215 return get_usage(id, type);
219 static void update(struct psensor *sensor)
223 v = get_value(sensor->nvidia_id, sensor->type);
225 if (v == UNKNOWN_DBL_VALUE)
226 log_err(_("%s: Failed to retrieve measure of type %x "
227 "for NVIDIA GPU %d"),
231 psensor_set_current_value(sensor, v);
234 static int check_sensor(int id, int type)
236 return get_value(id, type) != UNKNOWN_DBL_VALUE;
239 static char *i2str(int i)
244 /* second +1 to avoid issue about the conversion of a double
246 n = 1 + (ceil(log10(INT_MAX)) + 1) + 1;
249 snprintf(str, n, "%d", i);
254 static struct psensor *create_nvidia_sensor(int id, int subtype, int value_len)
256 char *pname, *name, *strnid, *sid;
262 type = SENSOR_TYPE_NVCTRL | subtype;
264 if (!check_sensor(id, type))
267 pname = get_product_name(id, type);
269 stype = get_nvidia_type_str(type);
271 n = strlen(pname) + 1 + strlen(strnid) + 1 + strlen(stype) + 1;
273 sprintf(name, "%s %s %s", pname, strnid, stype);
275 sid = malloc(strlen(PROVIDER_NAME) + 1 + strlen(name) + 1);
276 sprintf(sid, "%s %s", PROVIDER_NAME, name);
278 s = psensor_create(sid, name, pname, type, value_len);
290 display = XOpenDisplay(NULL);
293 log_err(_("%s: Cannot open connection to X11 server."),
298 if (XNVCTRLQueryExtension(display, &evt, &err))
301 log_err(_("%s: Failed to retrieve NVIDIA information."),
307 void nvidia_psensor_list_update(struct psensor **sensors)
314 if (!(s->type & SENSOR_TYPE_REMOTE)
315 && s->type & SENSOR_TYPE_NVCTRL)
322 static void add(struct psensor ***sensors, int id, int type, int values_len)
326 s = create_nvidia_sensor(id, type, values_len);
329 psensor_list_append(sensors, s);
332 void nvidia_psensor_list_append(struct psensor ***ss, int values_len)
340 ret = XNVCTRLQueryTargetCount(display, NV_CTRL_TARGET_TYPE_GPU, &n);
342 for (i = 0; i < n; i++) {
345 SENSOR_TYPE_GPU | SENSOR_TYPE_TEMP,
348 utype = SENSOR_TYPE_GPU | SENSOR_TYPE_PERCENT;
349 add(ss, i, utype | SENSOR_TYPE_AMBIENT, values_len);
350 add(ss, i, utype | SENSOR_TYPE_GRAPHICS, values_len);
351 add(ss, i, utype | SENSOR_TYPE_VIDEO, values_len);
352 add(ss, i, utype | SENSOR_TYPE_MEMORY, values_len);
353 add(ss, i, utype | SENSOR_TYPE_PCIE, values_len);
357 ret = XNVCTRLQueryTargetCount(display, NV_CTRL_TARGET_TYPE_COOLER, &n);
359 log_fct("%s: Number of fans: %d", PROVIDER_NAME, n);
360 for (i = 0; i < n; i++) {
361 utype = SENSOR_TYPE_FAN | SENSOR_TYPE_RPM;
362 if (check_sensor(i, utype))
363 add(ss, i, utype, values_len);
365 utype = SENSOR_TYPE_FAN | SENSOR_TYPE_PERCENT;
366 if (check_sensor(i, utype))
367 add(ss, i, utype, values_len);
370 log_err(_("%s: Failed to retrieve number of fans."),
375 void nvidia_cleanup()
378 XCloseDisplay(display);