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(_("Unknown NVIDIA product name for GPU %d"), id);
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)
136 if (type & SENSOR_TYPE_VIDEO)
139 if (type & SENSOR_TYPE_MEMORY)
142 if (type & SENSOR_TYPE_PCIE)
145 if (type & SENSOR_TYPE_AMBIENT)
148 if (type & SENSOR_TYPE_TEMP)
151 if (type & SENSOR_TYPE_FAN) {
152 if (type & SENSOR_TYPE_RPM)
161 static double get_usage(int id, int type)
168 stype = get_nvidia_type_str(type);
171 return UNKNOWN_DBL_VALUE;
173 res = XNVCTRLQueryTargetStringAttribute(display,
174 NV_CTRL_TARGET_TYPE_GPU,
177 NV_CTRL_STRING_GPU_UTILIZATION,
181 return UNKNOWN_DBL_VALUE;
183 v = get_usage_att(atts, stype);
190 static double get_value(int id, int type)
194 if (type & SENSOR_TYPE_TEMP) {
195 if (type & SENSOR_TYPE_AMBIENT)
196 att = NV_CTRL_AMBIENT_TEMPERATURE;
198 att = NV_CTRL_GPU_CORE_TEMPERATURE;
200 return get_att(NV_CTRL_TARGET_TYPE_GPU, id, att);
201 } else if (type & SENSOR_TYPE_FAN) {
202 if (type & SENSOR_TYPE_RPM)
203 return get_att(NV_CTRL_TARGET_TYPE_COOLER,
205 NV_CTRL_THERMAL_COOLER_SPEED);
206 else /* SENSOR_TYPE_PERCENT */
207 return get_att(NV_CTRL_TARGET_TYPE_COOLER,
209 NV_CTRL_THERMAL_COOLER_LEVEL);
210 } else { /* SENSOR_TYPE_PERCENT */
211 return get_usage(id, type);
215 static void update(struct psensor *sensor)
219 v = get_value(sensor->nvidia_id, sensor->type);
221 if (v == UNKNOWN_DBL_VALUE)
222 log_err(_("Failed to retrieve measure of type %x "
223 "for NVIDIA GPU %d"),
226 psensor_set_current_value(sensor, v);
229 static int check_sensor(int id, int type)
231 return get_value(id, type) != UNKNOWN_DBL_VALUE;
234 static char *i2str(int i)
239 /* second +1 to avoid issue about the conversion of a double
241 n = 1 + (ceil(log10(INT_MAX)) + 1) + 1;
244 snprintf(str, n, "%d", i);
249 static struct psensor *create_nvidia_sensor(int id, int subtype, int value_len)
251 char *pname, *name, *strnid, *sid;
257 type = SENSOR_TYPE_NVCTRL | subtype;
259 if (!check_sensor(id, type))
262 pname = get_product_name(id, type);
264 stype = get_nvidia_type_str(type);
266 n = strlen(pname) + 1 + strlen(strnid) + 1 + strlen(stype) + 1;
268 sprintf(name, "%s %s %s", pname, strnid, stype);
270 sid = malloc(strlen(PROVIDER_NAME) + 1 + strlen(name) + 1);
271 sprintf(sid, "%s %s", PROVIDER_NAME, name);
273 s = psensor_create(sid, name, pname, type, value_len);
285 display = XOpenDisplay(NULL);
288 log_err(_("Cannot open connection to X11 server."));
292 if (XNVCTRLQueryExtension(display, &evt, &err))
295 log_err(_("Failed to retrieve NVIDIA information."));
300 void nvidia_psensor_list_update(struct psensor **sensors)
307 if (!(s->type & SENSOR_TYPE_REMOTE)
308 && s->type & SENSOR_TYPE_NVCTRL)
315 static void add(struct psensor ***sensors, int id, int type, int values_len)
319 s = create_nvidia_sensor(id, type, values_len);
322 psensor_list_append(sensors, s);
325 void nvidia_psensor_list_append(struct psensor ***ss, int values_len)
333 ret = XNVCTRLQueryTargetCount(display, NV_CTRL_TARGET_TYPE_GPU, &n);
335 for (i = 0; i < n; i++) {
338 SENSOR_TYPE_GPU | SENSOR_TYPE_TEMP,
341 utype = SENSOR_TYPE_GPU | SENSOR_TYPE_PERCENT;
342 add(ss, i, utype | SENSOR_TYPE_AMBIENT, values_len);
343 add(ss, i, utype | SENSOR_TYPE_GRAPHICS, values_len);
344 add(ss, i, utype | SENSOR_TYPE_VIDEO, values_len);
345 add(ss, i, utype | SENSOR_TYPE_MEMORY, values_len);
346 add(ss, i, utype | SENSOR_TYPE_PCIE, values_len);
350 ret = XNVCTRLQueryTargetCount(display, NV_CTRL_TARGET_TYPE_COOLER, &n);
352 log_debug("NVIDIA: number of fans: %d", n);
353 for (i = 0; i < n; i++) {
354 utype = SENSOR_TYPE_FAN | SENSOR_TYPE_RPM;
355 if (check_sensor(i, utype))
356 add(ss, i, utype, values_len);
358 utype = SENSOR_TYPE_FAN | SENSOR_TYPE_PERCENT;
359 if (check_sensor(i, utype))
360 add(ss, i, utype, values_len);
363 log_err(_("NVIDIA: failed to retrieve number of fans."));
367 void nvidia_cleanup()
370 XCloseDisplay(display);