Added NVidia GPU usage.
[psensor.git] / src / lib / nvidia.c
index 4a74bbf..99df996 100644 (file)
@@ -1,22 +1,21 @@
 /*
-    Copyright (C) 2010-2011 wpitchoune@gmail.com
-
-    This program is free software; you can redistribute it and/or modify
-    it under the terms of the GNU General Public License as published by
-    the Free Software Foundation; either version 2 of the License, or
-    (at your option) any later version.
-
-    This program is distributed in the hope that it will be useful,
-    but WITHOUT ANY WARRANTY; without even the implied warranty of
-    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-    GNU General Public License for more details.
-
-    You should have received a copy of the GNU General Public License
-    along with this program; if not, write to the Free Software
-    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
-    02110-1301 USA
-*/
-
+ * Copyright (C) 2010-2014 jeanfi@gmail.com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
 #include <locale.h>
 #include <libintl.h>
 #define _(str) gettext(str)
 #include <NVCtrl/NVCtrl.h>
 #include <NVCtrl/NVCtrlLib.h>
 
-
 #include "psensor.h"
 
-Display *nvidia_sensors_dpy;
+Display *display;
 
-int nvidia_get_sensor_temp(struct psensor *sensor)
+/* Returns the temperature (Celsius) of a NVIDIA GPU. */
+static int get_temp(struct psensor *sensor)
 {
        int temp;
        Bool res;
 
-       res = XNVCTRLQueryTargetAttribute(nvidia_sensors_dpy,
+       res = XNVCTRLQueryTargetAttribute(display,
                                          NV_CTRL_TARGET_TYPE_GPU,
                                          sensor->nvidia_id,
                                          0,
-                                         NV_CTRL_GPU_CORE_TEMPERATURE, &temp);
+                                         NV_CTRL_GPU_CORE_TEMPERATURE,
+                                         &temp);
 
-       if (res == True) {
+       if (res == True)
                return temp;
-       } else {
-               fprintf(stderr,
-                       _("ERROR: failed to retrieve nvidia temperature\n"));
-               return 0;
+
+       log_debug(_("NVIDIA proprietary driver not used or cannot "
+                   "retrieve NVIDIA GPU temperature."));
+       return 0;
+}
+
+static double get_usage_att(char *atts, char *att)
+{
+       char *c, *key, *strv, *s;
+       size_t n;
+       double v;
+
+       c = atts;
+
+       v = UNKNOWN_DBL_VALUE;
+       while (*c) {
+               s = c;
+               n = 0;
+               while (*c) {
+                       if (*c == '=')
+                               break;
+                       c++;
+                       n++;
+               }
+
+               key = strndup(s, n);
+
+               if (*c)
+                       c++;
+
+               n = 0;
+               s = c;
+               while (*c) {
+                       if (*c == ',')
+                               break;
+                       c++;
+                       n++;
+               }
+
+               strv = strndup(s, n);
+               if (!strcmp(key, att))
+                       v = atoi(strv);
+
+               free(key);
+               free(strv);
+
+               if (v != UNKNOWN_DBL_VALUE)
+                       break;
+
+               while (*c && (*c == ' ' || *c == ','))
+                       c++;
        }
+
+       return v;
+}
+
+static int get_usage(struct psensor *sensor)
+{
+       char *temp;
+       Bool res;
+
+       res = XNVCTRLQueryTargetStringAttribute(display,
+                                         NV_CTRL_TARGET_TYPE_GPU,
+                                         sensor->nvidia_id,
+                                         0,
+                                         NV_CTRL_STRING_GPU_UTILIZATION,
+                                         &temp);
+
+       if (res == True)
+               return get_usage_att(temp, "graphics");
+
+       log_debug(_("NVIDIA proprietary driver not used or cannot "
+                   "retrieve NVIDIA GPU usage."));
+       return 0;
 }
 
-struct psensor *nvidia_create_sensor(int id, int values_max_length)
+static struct psensor *create_temp_sensor(int id, int values_len)
 {
        char name[200];
        char *sid;
        struct psensor *s;
+       int t;
 
        sprintf(name, "GPU%d", id);
 
-       sid = malloc(strlen("nvidia") + 1 + strlen(name) + 1);
-       sprintf(sid, "nvidia %s", name);
+       sid = malloc(strlen("NVIDIA") + 1 + strlen(name) + 1);
+       sprintf(sid, "NVIDIA %s", name);
 
-       s = psensor_create(sid, strdup(name),
-                          SENSOR_TYPE_NVIDIA, values_max_length);
+       t = SENSOR_TYPE_NVCTRL | SENSOR_TYPE_GPU | SENSOR_TYPE_TEMP;
+
+       s = psensor_create(sid,
+                          strdup(name),
+                          strdup(_("NVIDIA GPU")),
+                          t,
+                          values_len);
 
        s->nvidia_id = id;
 
        return s;
 }
 
-int nvidia_init()
+static struct psensor *create_usage_sensor(int id, int values_len)
 {
-       int event_base, error_base;
-       int num_gpus;
+       char name[200];
+       char *sid;
+       struct psensor *s;
+       int t;
 
-       nvidia_sensors_dpy = XOpenDisplay(NULL);
+       sprintf(name, "GPU%d graphics", id);
 
-       if (!nvidia_sensors_dpy) {
-               fprintf(stderr, _("ERROR: nvidia initialization failure\n"));
-               return 0;
-       }
+       sid = malloc(strlen("NVIDIA") + 1 + strlen(name) + 1);
+       sprintf(sid, "NVIDIA %s", name);
 
-       if (XNVCTRLQueryExtension(nvidia_sensors_dpy, &event_base,
-                                 &error_base)) {
-               if (XNVCTRLQueryTargetCount(nvidia_sensors_dpy,
-                                           NV_CTRL_TARGET_TYPE_GPU,
-                                           &num_gpus)) {
-                       return num_gpus;
-               }
+       t = SENSOR_TYPE_NVCTRL | SENSOR_TYPE_GPU | SENSOR_TYPE_USAGE;
+
+       s = psensor_create(sid,
+                          strdup(name),
+                          strdup(_("NVIDIA GPU")),
+                          t,
+                          values_len);
+
+       s->nvidia_id = id;
+
+       return s;
+}
+
+/*
+  Opens connection to X server and returns the number
+  of NVIDIA GPUs.
 
+  Return 0 if no NVIDIA gpus or cannot get information.
+*/
+static int init()
+{
+       int evt, err, n;
+
+       display = XOpenDisplay(NULL);
+
+       if (!display) {
+               log_err(_("Cannot open connection to X11 server."));
+               return 0;
        }
 
-       fprintf(stderr, _("ERROR: nvidia initialization failure: %d\n"),
-               error_base);
+       if (XNVCTRLQueryExtension(display, &evt, &err) &&
+           XNVCTRLQueryTargetCount(display, NV_CTRL_TARGET_TYPE_GPU, &n))
+               return n;
+
+       log_err(_("Failed to retrieve NVIDIA information."));
 
        return 0;
 }
 
 void nvidia_psensor_list_update(struct psensor **sensors)
 {
-       struct psensor **s_ptr = sensors;
+       struct psensor **ss, *s;
 
-       while (*s_ptr) {
-               struct psensor *sensor = *s_ptr;
+       ss = sensors;
+       while (*ss) {
+               s = *ss;
 
-               if (sensor->type == SENSOR_TYPE_NVIDIA) {
-                       int val = nvidia_get_sensor_temp(sensor);
-
-                       psensor_set_current_value(sensor, (double)val);
+               if (s->type & SENSOR_TYPE_NVCTRL) {
+                       if (s->type & SENSOR_TYPE_TEMP)
+                               psensor_set_current_value(s, get_temp(s));
+                       else if (s->type & SENSOR_TYPE_USAGE)
+                               psensor_set_current_value(s, get_usage(s));
                }
 
-               s_ptr++;
+               ss++;
        }
 }
 
 struct psensor **nvidia_psensor_list_add(struct psensor **sensors,
-                                        int values_max_length)
+                                        int values_len)
 {
-       int i;
-       int nvidia_gpus_count = nvidia_init();
-       struct psensor **res = sensors;
+       int i, n;
+       struct psensor **tmp, **ss, *s;
 
+       n = init();
 
-       if (!nvidia_gpus_count) {
-               fprintf(stderr,
-                       _("ERROR: "
-                         "no nvidia chips or initialization failure\n"));
-       }
+       ss = sensors;
+       for (i = 0; i < n; i++) {
+               s = create_temp_sensor(i, values_len);
 
-       for (i = 0; i < nvidia_gpus_count; i++) {
-               struct psensor *sensor
-                       = nvidia_create_sensor(i, values_max_length);
+               tmp = psensor_list_add(ss, s);
 
-               struct psensor **tmp_psensors = psensor_list_add(res,
-                                                                sensor);
+               if (ss != tmp)
+                       free(ss);
 
-               if (res != sensors)
-                       free(res);
+               ss = tmp;
 
-               res = tmp_psensors;
+               s = create_usage_sensor(i, values_len);
+
+               tmp = psensor_list_add(ss, s);
+
+               if (ss != tmp)
+                       free(ss);
+
+               ss = tmp;
        }
 
-       return res;
+       return ss;
+}
+
+void nvidia_cleanup()
+{
+       if (display) {
+               XCloseDisplay(display);
+               display = NULL;
+       }
 }