Added NVidia GPU graphics, memory, PCIe and video usage.
[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 <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #include <X11/Xlib.h>
28
29 #include <NVCtrl/NVCtrl.h>
30 #include <NVCtrl/NVCtrlLib.h>
31
32 #include "psensor.h"
33
34 Display *display;
35
36 /* Returns the temperature (Celsius) of a NVIDIA GPU. */
37 static int get_temp(struct psensor *sensor)
38 {
39         int temp;
40         Bool res;
41
42         res = XNVCTRLQueryTargetAttribute(display,
43                                           NV_CTRL_TARGET_TYPE_GPU,
44                                           sensor->nvidia_id,
45                                           0,
46                                           NV_CTRL_GPU_CORE_TEMPERATURE,
47                                           &temp);
48
49         if (res == True)
50                 return temp;
51
52         log_debug(_("NVIDIA proprietary driver not used or cannot "
53                     "retrieve NVIDIA GPU temperature."));
54         return 0;
55 }
56
57 static double get_usage_att(char *atts, char *att)
58 {
59         char *c, *key, *strv, *s;
60         size_t n;
61         double v;
62
63         c = atts;
64
65         v = UNKNOWN_DBL_VALUE;
66         while (*c) {
67                 s = c;
68                 n = 0;
69                 while (*c) {
70                         if (*c == '=')
71                                 break;
72                         c++;
73                         n++;
74                 }
75
76                 key = strndup(s, n);
77
78                 if (*c)
79                         c++;
80
81                 n = 0;
82                 s = c;
83                 while (*c) {
84                         if (*c == ',')
85                                 break;
86                         c++;
87                         n++;
88                 }
89
90                 strv = strndup(s, n);
91                 if (!strcmp(key, att))
92                         v = atoi(strv);
93
94                 free(key);
95                 free(strv);
96
97                 if (v != UNKNOWN_DBL_VALUE)
98                         break;
99
100                 while (*c && (*c == ' ' || *c == ','))
101                         c++;
102         }
103
104         return v;
105 }
106
107 static int get_usage(struct psensor *sensor)
108 {
109         char *temp;
110         Bool res;
111
112         res = XNVCTRLQueryTargetStringAttribute(display,
113                                           NV_CTRL_TARGET_TYPE_GPU,
114                                           sensor->nvidia_id,
115                                           0,
116                                           NV_CTRL_STRING_GPU_UTILIZATION,
117                                           &temp);
118
119         if (res == True) {
120                 if (sensor->type & SENSOR_TYPE_GRAPHICS)
121                         return get_usage_att(temp, "graphics");
122                 else if (sensor->type & SENSOR_TYPE_VIDEO)
123                         return get_usage_att(temp, "video");
124                 else if (sensor->type & SENSOR_TYPE_MEMORY)
125                         return get_usage_att(temp, "memory");
126                 else if (sensor->type & SENSOR_TYPE_PCIE)
127                         return get_usage_att(temp, "PCIe");
128         }
129
130         log_debug(_("NVIDIA proprietary driver not used or cannot "
131                     "retrieve NVIDIA GPU usage."));
132         return 0;
133 }
134
135 static struct psensor *create_temp_sensor(int id, int values_len)
136 {
137         char name[200];
138         char *sid;
139         struct psensor *s;
140         int t;
141
142         sprintf(name, "GPU%d", id);
143
144         sid = malloc(strlen("NVIDIA") + 1 + strlen(name) + 1);
145         sprintf(sid, "NVIDIA %s", name);
146
147         t = SENSOR_TYPE_NVCTRL | SENSOR_TYPE_GPU | SENSOR_TYPE_TEMP;
148
149         s = psensor_create(sid,
150                            strdup(name),
151                            strdup(_("NVIDIA GPU")),
152                            t,
153                            values_len);
154
155         s->nvidia_id = id;
156
157         return s;
158 }
159
160 static struct psensor *create_usage_sensor(int id,
161                                            int subtype,
162                                            int values_len)
163 {
164         char name[200];
165         char *sid;
166         struct psensor *s;
167         int t;
168
169         if (subtype & SENSOR_TYPE_GRAPHICS)
170                 sprintf(name, "GPU%d graphics", id);
171         else if (subtype & SENSOR_TYPE_MEMORY)
172                 sprintf(name, "GPU%d memory", id);
173         else if (subtype & SENSOR_TYPE_VIDEO)
174                 sprintf(name, "GPU%d video", id);
175         else /* if (subtype & SENSOR_TYPE_PCIE) */
176                 sprintf(name, "GPU%d PCIe", id);
177
178
179         sid = malloc(strlen("NVIDIA") + 1 + strlen(name) + 1);
180         sprintf(sid, "NVIDIA %s", name);
181
182         t = SENSOR_TYPE_NVCTRL | SENSOR_TYPE_GPU | SENSOR_TYPE_USAGE | subtype;
183
184         s = psensor_create(sid,
185                            strdup(name),
186                            strdup(_("NVIDIA GPU")),
187                            t,
188                            values_len);
189
190         s->nvidia_id = id;
191
192         return s;
193 }
194
195 /*
196   Opens connection to X server and returns the number
197   of NVIDIA GPUs.
198
199   Return 0 if no NVIDIA gpus or cannot get information.
200 */
201 static int init()
202 {
203         int evt, err, n;
204
205         display = XOpenDisplay(NULL);
206
207         if (!display) {
208                 log_err(_("Cannot open connection to X11 server."));
209                 return 0;
210         }
211
212         if (XNVCTRLQueryExtension(display, &evt, &err) &&
213             XNVCTRLQueryTargetCount(display, NV_CTRL_TARGET_TYPE_GPU, &n))
214                 return n;
215
216         log_err(_("Failed to retrieve NVIDIA information."));
217
218         return 0;
219 }
220
221 void nvidia_psensor_list_update(struct psensor **sensors)
222 {
223         struct psensor **ss, *s;
224
225         ss = sensors;
226         while (*ss) {
227                 s = *ss;
228
229                 if (s->type & SENSOR_TYPE_NVCTRL) {
230                         if (s->type & SENSOR_TYPE_TEMP)
231                                 psensor_set_current_value(s, get_temp(s));
232                         else if (s->type & SENSOR_TYPE_USAGE)
233                                 psensor_set_current_value(s, get_usage(s));
234                 }
235
236                 ss++;
237         }
238 }
239
240 struct psensor **nvidia_psensor_list_add(struct psensor **sensors,
241                                          int values_len)
242 {
243         int i, n;
244         struct psensor **tmp, **ss, *s;
245
246         n = init();
247
248         ss = sensors;
249         for (i = 0; i < n; i++) {
250                 s = create_temp_sensor(i, values_len);
251                 tmp = psensor_list_add(ss, s);
252                 if (ss != tmp)
253                         free(ss);
254
255                 ss = tmp;
256                 s = create_usage_sensor(i, SENSOR_TYPE_GRAPHICS, values_len);
257                 tmp = psensor_list_add(ss, s);
258                 if (ss != tmp)
259                         free(ss);
260
261                 ss = tmp;
262                 s = create_usage_sensor(i, SENSOR_TYPE_VIDEO, values_len);
263                 tmp = psensor_list_add(ss, s);
264                 if (ss != tmp)
265                         free(ss);
266
267                 ss = tmp;
268                 s = create_usage_sensor(i, SENSOR_TYPE_MEMORY, values_len);
269                 tmp = psensor_list_add(ss, s);
270                 if (ss != tmp)
271                         free(ss);
272
273                 ss = tmp;
274                 s = create_usage_sensor(i, SENSOR_TYPE_PCIE, values_len);
275                 tmp = psensor_list_add(ss, s);
276                 if (ss != tmp)
277                         free(ss);
278
279                 ss = tmp;
280         }
281
282         return ss;
283 }
284
285 void nvidia_cleanup()
286 {
287         if (display) {
288                 XCloseDisplay(display);
289                 display = NULL;
290         }
291 }