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)
29 #include <NVCtrl/NVCtrl.h>
30 #include <NVCtrl/NVCtrlLib.h>
36 /* Returns the temperature (Celsius) of a NVIDIA GPU. */
37 static int get_temp(struct psensor *sensor)
42 res = XNVCTRLQueryTargetAttribute(display,
43 NV_CTRL_TARGET_TYPE_GPU,
46 NV_CTRL_GPU_CORE_TEMPERATURE,
52 log_debug(_("NVIDIA proprietary driver not used or cannot "
53 "retrieve NVIDIA GPU temperature."));
57 static double get_usage_att(char *atts, char *att)
59 char *c, *key, *strv, *s;
65 v = UNKNOWN_DBL_VALUE;
91 if (!strcmp(key, att))
97 if (v != UNKNOWN_DBL_VALUE)
100 while (*c && (*c == ' ' || *c == ','))
107 static int get_usage(struct psensor *sensor)
112 res = XNVCTRLQueryTargetStringAttribute(display,
113 NV_CTRL_TARGET_TYPE_GPU,
116 NV_CTRL_STRING_GPU_UTILIZATION,
120 return get_usage_att(temp, "graphics");
122 log_debug(_("NVIDIA proprietary driver not used or cannot "
123 "retrieve NVIDIA GPU usage."));
127 static struct psensor *create_temp_sensor(int id, int values_len)
134 sprintf(name, "GPU%d", id);
136 sid = malloc(strlen("NVIDIA") + 1 + strlen(name) + 1);
137 sprintf(sid, "NVIDIA %s", name);
139 t = SENSOR_TYPE_NVCTRL | SENSOR_TYPE_GPU | SENSOR_TYPE_TEMP;
141 s = psensor_create(sid,
143 strdup(_("NVIDIA GPU")),
152 static struct psensor *create_usage_sensor(int id, int values_len)
159 sprintf(name, "GPU%d graphics", id);
161 sid = malloc(strlen("NVIDIA") + 1 + strlen(name) + 1);
162 sprintf(sid, "NVIDIA %s", name);
164 t = SENSOR_TYPE_NVCTRL | SENSOR_TYPE_GPU | SENSOR_TYPE_USAGE;
166 s = psensor_create(sid,
168 strdup(_("NVIDIA GPU")),
178 Opens connection to X server and returns the number
181 Return 0 if no NVIDIA gpus or cannot get information.
187 display = XOpenDisplay(NULL);
190 log_err(_("Cannot open connection to X11 server."));
194 if (XNVCTRLQueryExtension(display, &evt, &err) &&
195 XNVCTRLQueryTargetCount(display, NV_CTRL_TARGET_TYPE_GPU, &n))
198 log_err(_("Failed to retrieve NVIDIA information."));
203 void nvidia_psensor_list_update(struct psensor **sensors)
205 struct psensor **ss, *s;
211 if (s->type & SENSOR_TYPE_NVCTRL) {
212 if (s->type & SENSOR_TYPE_TEMP)
213 psensor_set_current_value(s, get_temp(s));
214 else if (s->type & SENSOR_TYPE_USAGE)
215 psensor_set_current_value(s, get_usage(s));
222 struct psensor **nvidia_psensor_list_add(struct psensor **sensors,
226 struct psensor **tmp, **ss, *s;
231 for (i = 0; i < n; i++) {
232 s = create_temp_sensor(i, values_len);
234 tmp = psensor_list_add(ss, s);
241 s = create_usage_sensor(i, values_len);
243 tmp = psensor_list_add(ss, s);
254 void nvidia_cleanup()
257 XCloseDisplay(display);