/*
- Copyright (C) 2010-2011 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
-*/
-
+ * 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 *display;
-#include "psensor.h"
+static char *get_product_name(int id)
+{
+ char *name;
+ Bool res;
-Display *nvidia_sensors_dpy;
+ res = XNVCTRLQueryTargetStringAttribute(display,
+ NV_CTRL_TARGET_TYPE_GPU,
+ id,
+ 0,
+ NV_CTRL_STRING_PRODUCT_NAME,
+ &name);
+ if (res == True) {
+ if (strcmp(name, "Unknown")) {
+ return name;
+ } else {
+ log_err(_("Unknown NVIDIA product name for GPU %d"),
+ id);
+ free(name);
+ }
+ } else {
+ log_err(_("Failed to retrieve NVIDIA product name for GPU %d"),
+ id);
+ }
+
+ return strdup("NVIDIA");
+}
-int nvidia_get_sensor_temp(struct psensor *sensor)
+static double get_temp(int id)
{
+ Bool res;
int temp;
+
+ res = XNVCTRLQueryTargetAttribute(display,
+ NV_CTRL_TARGET_TYPE_GPU,
+ id,
+ 0,
+ NV_CTRL_GPU_CORE_TEMPERATURE,
+ &temp);
+
+ if (res == True)
+ return temp;
+ else
+ return UNKNOWN_DBL_VALUE;
+}
+
+static double get_ambient_temp(int id)
+{
Bool res;
+ int temp;
- res = XNVCTRLQueryTargetAttribute(nvidia_sensors_dpy,
+ res = XNVCTRLQueryTargetAttribute(display,
NV_CTRL_TARGET_TYPE_GPU,
- sensor->nvidia_id,
+ id,
0,
- NV_CTRL_GPU_CORE_TEMPERATURE, &temp);
+ NV_CTRL_AMBIENT_TEMPERATURE,
+ &temp);
- if (res == True) {
+ if (res == True)
return temp;
- } else {
- fprintf(stderr,
- _("ERROR: failed to retrieve nvidia temperature\n"));
- return 0;
+ else
+ return UNKNOWN_DBL_VALUE;
+}
+
+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;
}
-struct psensor *nvidia_create_sensor(int id, int values_max_length)
+static double get_usage(int id, int type)
+{
+ char *stype, *atts;
+ double v;
+ Bool res;
+
+ if (type & SENSOR_TYPE_GRAPHICS)
+ stype = "graphics";
+ else if (type & SENSOR_TYPE_VIDEO)
+ stype = "video";
+ else if (type & SENSOR_TYPE_MEMORY)
+ stype = "memory";
+ else if (type & SENSOR_TYPE_PCIE)
+ stype = "PCIe";
+ else
+ return UNKNOWN_DBL_VALUE;
+
+ res = XNVCTRLQueryTargetStringAttribute(display,
+ NV_CTRL_TARGET_TYPE_GPU,
+ id,
+ 0,
+ NV_CTRL_STRING_GPU_UTILIZATION,
+ &atts);
+
+ if (res != True)
+ return UNKNOWN_DBL_VALUE;
+
+ v = get_usage_att(atts, stype);
+
+ free(atts);
+
+ return v;
+}
+
+static void update(struct psensor *sensor)
+{
+ double v;
+
+ if (sensor->type & SENSOR_TYPE_TEMP) {
+ if (sensor->type & SENSOR_TYPE_AMBIENT)
+ v = get_ambient_temp(sensor->nvidia_id);
+ else
+ v = get_temp(sensor->nvidia_id);
+ } else { /* SENSOR_TYPE_USAGE */
+ v = get_usage(sensor->nvidia_id, sensor->type);
+ }
+
+ if (v == UNKNOWN_DBL_VALUE)
+ log_err(_("Failed to retrieve measure of type %x "
+ "for NVIDIA GPU %d"),
+ sensor->type,
+ sensor->nvidia_id);
+ psensor_set_current_value(sensor, v);
+}
+
+static struct psensor *create_temp_sensor(int id, int subtype, int values_len)
+{
+ char name[200];
+ char *sid, *pname;
+ struct psensor *s;
+ int t;
+
+ pname = get_product_name(id);
+
+ if (subtype & SENSOR_TYPE_AMBIENT)
+ sprintf(name, "%s %d ambient", pname, id);
+ else
+ sprintf(name, "%s %d", pname, id);
+ free(pname);
+
+ sid = malloc(strlen("NVIDIA") + 1 + strlen(name) + 1);
+ sprintf(sid, "NVIDIA %s", name);
+
+ t = SENSOR_TYPE_NVCTRL | SENSOR_TYPE_GPU | SENSOR_TYPE_TEMP | subtype;
+
+ s = psensor_create(sid,
+ strdup(name),
+ strdup(_("NVIDIA GPU")),
+ t,
+ values_len);
+
+ s->nvidia_id = id;
+
+ return s;
+}
+
+static struct psensor *create_usage_sensor(int id,
+ int subtype,
+ int values_len)
{
char name[200];
char *sid;
struct psensor *s;
+ int t;
+
+ if (subtype & SENSOR_TYPE_GRAPHICS)
+ sprintf(name, "GPU%d graphics", id);
+ else if (subtype & SENSOR_TYPE_MEMORY)
+ sprintf(name, "GPU%d memory", id);
+ else if (subtype & SENSOR_TYPE_VIDEO)
+ sprintf(name, "GPU%d video", id);
+ else /* if (subtype & SENSOR_TYPE_PCIE) */
+ sprintf(name, "GPU%d PCIe", id);
+
- 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);
+ t = SENSOR_TYPE_NVCTRL | SENSOR_TYPE_GPU | SENSOR_TYPE_USAGE | subtype;
- s = psensor_create(sid, strdup(name),
- SENSOR_TYPE_NVIDIA, values_max_length);
+ s = psensor_create(sid,
+ strdup(name),
+ strdup(_("NVIDIA GPU")),
+ t,
+ values_len);
s->nvidia_id = id;
return s;
}
-int nvidia_init()
+/*
+ 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 event_base, error_base;
- int num_gpus;
+ int evt, err, n;
- nvidia_sensors_dpy = XOpenDisplay(NULL);
+ display = XOpenDisplay(NULL);
- if (!nvidia_sensors_dpy) {
- fprintf(stderr, _("ERROR: nvidia initialization failure\n"));
+ if (!display) {
+ log_err(_("Cannot open connection to X11 server."));
return 0;
}
- if (XNVCTRLQueryExtension(nvidia_sensors_dpy, &event_base,
- &error_base)) {
- if (XNVCTRLQueryTargetCount(nvidia_sensors_dpy,
- NV_CTRL_TARGET_TYPE_GPU,
- &num_gpus)) {
- return num_gpus;
- }
-
- }
+ if (XNVCTRLQueryExtension(display, &evt, &err) &&
+ XNVCTRLQueryTargetCount(display, NV_CTRL_TARGET_TYPE_GPU, &n))
+ return n;
- fprintf(stderr, _("ERROR: nvidia initialization failure: %d\n"),
- error_base);
+ log_err(_("Failed to retrieve NVIDIA information."));
return 0;
}
void nvidia_psensor_list_update(struct psensor **sensors)
{
- struct psensor **s_ptr = sensors;
-
- while (*s_ptr) {
- struct psensor *sensor = *s_ptr;
+ struct psensor **ss, *s;
- if (sensor->type == SENSOR_TYPE_NVIDIA) {
- int val = nvidia_get_sensor_temp(sensor);
+ ss = sensors;
+ while (*ss) {
+ s = *ss;
- psensor_set_current_value(sensor, (double)val);
- }
+ if (s->type & SENSOR_TYPE_NVCTRL)
+ update(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;
-
-
- if (!nvidia_gpus_count) {
- fprintf(stderr,
- _("ERROR: "
- "no nvidia chips or initialization failure\n"));
+ int i, n;
+ struct psensor **tmp, **ss, *s;
+
+ n = init();
+
+ ss = sensors;
+ for (i = 0; i < n; i++) {
+ s = create_temp_sensor(i, 0, values_len);
+ tmp = psensor_list_add(ss, s);
+ if (ss != tmp)
+ free(ss);
+
+ ss = tmp;
+ s = create_temp_sensor(i, SENSOR_TYPE_AMBIENT, values_len);
+ tmp = psensor_list_add(ss, s);
+ if (ss != tmp)
+ free(ss);
+
+ ss = tmp;
+ s = create_usage_sensor(i, SENSOR_TYPE_GRAPHICS, values_len);
+ tmp = psensor_list_add(ss, s);
+ if (ss != tmp)
+ free(ss);
+
+ ss = tmp;
+ s = create_usage_sensor(i, SENSOR_TYPE_VIDEO, values_len);
+ tmp = psensor_list_add(ss, s);
+ if (ss != tmp)
+ free(ss);
+
+ ss = tmp;
+ s = create_usage_sensor(i, SENSOR_TYPE_MEMORY, values_len);
+ tmp = psensor_list_add(ss, s);
+ if (ss != tmp)
+ free(ss);
+
+ ss = tmp;
+ s = create_usage_sensor(i, SENSOR_TYPE_PCIE, values_len);
+ tmp = psensor_list_add(ss, s);
+ if (ss != tmp)
+ free(ss);
+
+ ss = tmp;
}
- for (i = 0; i < nvidia_gpus_count; i++) {
- struct psensor *sensor
- = nvidia_create_sensor(i, values_max_length);
-
- struct psensor **tmp_psensors = psensor_list_add(res,
- sensor);
-
- if (res != sensors)
- free(res);
+ return ss;
+}
- res = tmp_psensors;
+void nvidia_cleanup()
+{
+ if (display) {
+ XCloseDisplay(display);
+ display = NULL;
}
-
- return res;
}