cleanup
[psensor.git] / src / lib / nvidia.c
1 /*
2  * Copyright (C) 2010-2014 jeanfi@gmail.com
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301 USA
18  */
19 #include <locale.h>
20 #include <libintl.h>
21 #define _(str) gettext(str)
22
23 #include <limits.h>
24 #include <math.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include <X11/Xlib.h>
30
31 #include <NVCtrl/NVCtrl.h>
32 #include <NVCtrl/NVCtrlLib.h>
33
34 #include <psensor.h>
35
36 Display *display;
37
38 static char *get_product_name(int id)
39 {
40         char *name;
41         Bool res;
42
43         res = XNVCTRLQueryTargetStringAttribute(display,
44                                                 NV_CTRL_TARGET_TYPE_GPU,
45                                                 id,
46                                                 0,
47                                                 NV_CTRL_STRING_PRODUCT_NAME,
48                                                 &name);
49         if (res == True) {
50                 if (strcmp(name, "Unknown")) {
51                         return name;
52                 } else {
53                         log_err(_("Unknown NVIDIA product name for GPU %d"),
54                                 id);
55                         free(name);
56                 }
57         } else {
58                 log_err(_("Failed to retrieve NVIDIA product name for GPU %d"),
59                         id);
60         }
61
62         return strdup("NVIDIA");
63 }
64
65 static double get_att(int target, int id, int att)
66 {
67         Bool res;
68         int temp;
69
70         res = XNVCTRLQueryTargetAttribute(display, target, id, 0, att, &temp);
71
72         if (res == True)
73                 return temp;
74         else
75                 return UNKNOWN_DBL_VALUE;
76 }
77
78 static double get_usage_att(char *atts, const char *att)
79 {
80         char *c, *key, *strv, *s;
81         size_t n;
82         double v;
83
84         c = atts;
85
86         v = UNKNOWN_DBL_VALUE;
87         while (*c) {
88                 s = c;
89                 n = 0;
90                 while (*c) {
91                         if (*c == '=')
92                                 break;
93                         c++;
94                         n++;
95                 }
96
97                 key = strndup(s, n);
98
99                 if (*c)
100                         c++;
101
102                 n = 0;
103                 s = c;
104                 while (*c) {
105                         if (*c == ',')
106                                 break;
107                         c++;
108                         n++;
109                 }
110
111                 strv = strndup(s, n);
112                 if (!strcmp(key, att))
113                         v = atoi(strv);
114
115                 free(key);
116                 free(strv);
117
118                 if (v != UNKNOWN_DBL_VALUE)
119                         break;
120
121                 while (*c && (*c == ' ' || *c == ','))
122                         c++;
123         }
124
125         return v;
126 }
127
128 static const char *get_nvidia_type_str(int type)
129 {
130         if (type & SENSOR_TYPE_GRAPHICS)
131                 return "graphics";
132         else if (type & SENSOR_TYPE_VIDEO)
133                 return "video";
134         else if (type & SENSOR_TYPE_MEMORY)
135                 return "memory";
136         else if (type & SENSOR_TYPE_PCIE)
137                 return "PCIe";
138         else if (type & SENSOR_TYPE_AMBIENT)
139                 return "ambient";
140         else if (type & SENSOR_TYPE_TEMP)
141                 return "temp";
142         else
143                 return "unknown";
144 }
145
146 static double get_usage(int id, int type)
147 {
148         const char *stype;
149         char *atts;
150         double v;
151         Bool res;
152
153         stype = get_nvidia_type_str(type);
154
155         if (!stype)
156                 return UNKNOWN_DBL_VALUE;
157
158         res = XNVCTRLQueryTargetStringAttribute(display,
159                                                 NV_CTRL_TARGET_TYPE_GPU,
160                                                 id,
161                                                 0,
162                                                 NV_CTRL_STRING_GPU_UTILIZATION,
163                                                 &atts);
164
165         if (res != True)
166                 return UNKNOWN_DBL_VALUE;
167
168         v = get_usage_att(atts, stype);
169
170         free(atts);
171
172         return v;
173 }
174
175 static double get_value(int id, int type)
176 {
177         int att;
178
179         if (type & SENSOR_TYPE_TEMP) {
180                 if (type & SENSOR_TYPE_AMBIENT)
181                         att = NV_CTRL_AMBIENT_TEMPERATURE;
182                 else
183                         att = NV_CTRL_GPU_CORE_TEMPERATURE;
184
185                 return get_att(NV_CTRL_TARGET_TYPE_GPU, id, att);
186         } else { /* SENSOR_TYPE_USAGE */
187                 return get_usage(id, type);
188         }
189 }
190
191 static void update(struct psensor *sensor)
192 {
193         double v;
194
195         v = get_value(sensor->nvidia_id, sensor->type);
196
197         if (v == UNKNOWN_DBL_VALUE)
198                 log_err(_("Failed to retrieve measure of type %x "
199                           "for NVIDIA GPU %d"),
200                         sensor->type,
201                         sensor->nvidia_id);
202         psensor_set_current_value(sensor, v);
203 }
204
205 static int check_sensor(int id, int type)
206 {
207         return get_value(id, type) != UNKNOWN_DBL_VALUE;
208 }
209
210 static char *i2str(int i)
211 {
212         char *str;
213         size_t n;
214
215         /* second +1 to avoid issue about the conversion of a double
216          * to a lower int */
217         n = 1 + (ceil(log10(INT_MAX)) + 1) + 1;
218
219         str = malloc(n);
220         snprintf(str, n, "%d", i);
221
222         return str;
223 }
224
225 static struct psensor *create_nvidia_sensor(int id, int subtype, int value_len)
226 {
227         char *pname, *name, *strnid, *sid;
228         const char *stype;
229         int type;
230         size_t n;
231         struct psensor *s;
232
233         type = SENSOR_TYPE_NVCTRL | subtype;
234
235         if (!check_sensor(id, type))
236                 return NULL;
237
238         pname = get_product_name(id);
239         strnid = i2str(id);
240         stype = get_nvidia_type_str(type);
241
242         n = strlen(pname) + 1 + strlen(strnid) + 1 + strlen(stype) + 1;
243
244         name = malloc(n);
245         sprintf(name, "%s %s %s", pname, strnid, stype);
246
247         sid = malloc(strlen("nvidia") + 1 + strlen(name) + 1);
248         sprintf(sid, "nvidia %s", name);
249
250         s = psensor_create(sid, name, pname, type, value_len);
251         s->nvidia_id = id;
252
253         free(strnid);
254
255         return s;
256 }
257
258 /*
259   Opens connection to X server and returns the number
260   of NVIDIA GPUs.
261
262   Return 0 if no NVIDIA gpus or cannot get information.
263 */
264 static int init()
265 {
266         int evt, err, n;
267
268         display = XOpenDisplay(NULL);
269
270         if (!display) {
271                 log_err(_("Cannot open connection to X11 server."));
272                 return 0;
273         }
274
275         if (XNVCTRLQueryExtension(display, &evt, &err) &&
276             XNVCTRLQueryTargetCount(display, NV_CTRL_TARGET_TYPE_GPU, &n)) {
277                 return n;
278         }
279
280         log_err(_("Failed to retrieve NVIDIA information."));
281
282         return 0;
283 }
284
285 void nvidia_psensor_list_update(struct psensor **sensors)
286 {
287         struct psensor **ss, *s;
288
289         ss = sensors;
290         while (*ss) {
291                 s = *ss;
292
293                 if (s->type & SENSOR_TYPE_NVCTRL)
294                         update(s);
295
296                 ss++;
297         }
298 }
299
300 static void add(struct psensor ***sensors, int id, int type, int values_len)
301 {
302         struct psensor **tmp, *s;
303
304         s = create_nvidia_sensor(id, type, values_len);
305
306         if (s) {
307                 tmp = psensor_list_add(*sensors, s);
308                 free(*sensors);
309                 *sensors = tmp;
310         }
311 }
312
313 struct psensor **
314 nvidia_psensor_list_add(struct psensor **sensors, int values_len)
315 {
316         int i, n;
317         struct psensor **ss;
318
319         n = init();
320
321         ss = sensors;
322         for (i = 0; i < n; i++) {
323                 add(&ss,
324                     i,
325                     SENSOR_TYPE_GPU | SENSOR_TYPE_GPU | SENSOR_TYPE_TEMP,
326                     values_len);
327
328                 add(&ss,
329                     i,
330                     SENSOR_TYPE_GPU | SENSOR_TYPE_USAGE | SENSOR_TYPE_AMBIENT,
331                     values_len);
332
333                 add(&ss,
334                     i,
335                     SENSOR_TYPE_GPU | SENSOR_TYPE_USAGE | SENSOR_TYPE_GRAPHICS,
336                     values_len);
337
338                 add(&ss,
339                     i,
340                     SENSOR_TYPE_GPU | SENSOR_TYPE_USAGE | SENSOR_TYPE_VIDEO,
341                     values_len);
342
343                 add(&ss,
344                     i,
345                     SENSOR_TYPE_GPU | SENSOR_TYPE_USAGE | SENSOR_TYPE_MEMORY,
346                     values_len);
347
348                 add(&ss,
349                     i,
350                     SENSOR_TYPE_GPU | SENSOR_TYPE_USAGE | SENSOR_TYPE_PCIE,
351                     values_len);
352         }
353
354         return ss;
355 }
356
357 void nvidia_cleanup()
358 {
359         if (display) {
360                 XCloseDisplay(display);
361                 display = NULL;
362         }
363 }