/*
- 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-2016 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 <limits.h>
+#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <NVCtrl/NVCtrl.h>
#include <NVCtrl/NVCtrlLib.h>
+#include <nvidia.h>
+#include <psensor.h>
-#include "psensor.h"
+static Display *display;
-Display *nvidia_sensors_dpy;
+static const char *PROVIDER_NAME = "nvctrl";
-int nvidia_get_sensor_temp(struct psensor *sensor)
+static void set_nvidia_id(struct psensor *s, int id)
{
- int temp;
+ *(int *)s->provider_data = id;
+}
+
+static int get_nvidia_id(struct psensor *s)
+{
+ return *(int *)s->provider_data;
+}
+
+static char *get_product_name(int id, int type)
+{
+ char *name;
Bool res;
- res = XNVCTRLQueryTargetAttribute(nvidia_sensors_dpy,
- NV_CTRL_TARGET_TYPE_GPU,
- sensor->nvidia_id,
- 0,
- NV_CTRL_GPU_CORE_TEMPERATURE, &temp);
+ if (type & SENSOR_TYPE_FAN)
+ return strdup("NVIDIA");
+ res = XNVCTRLQueryTargetStringAttribute(display,
+ NV_CTRL_TARGET_TYPE_GPU,
+ id,
+ 0,
+ NV_CTRL_STRING_PRODUCT_NAME,
+ &name);
if (res == True) {
- return temp;
+ if (strcmp(name, "Unknown"))
+ return name;
+
+ log_err(_("%s: Unknown NVIDIA product name for GPU %d"),
+ PROVIDER_NAME,
+ id);
+ free(name);
} else {
- fprintf(stderr,
- _("ERROR: failed to retrieve nvidia temperature\n"));
- return 0;
+ log_err(_("%s: "
+ "Failed to retrieve NVIDIA product name for GPU %d"),
+ PROVIDER_NAME,
+ id);
}
+
+ return strdup("NVIDIA");
}
-struct psensor *nvidia_create_sensor(int id, int values_max_length)
+static double get_att(int target, int id, int att)
{
- char name[200];
- char *sid;
- struct psensor *s;
+ Bool res;
+ int temp;
- sprintf(name, "GPU%d", id);
+ res = XNVCTRLQueryTargetAttribute(display, target, id, 0, att, &temp);
- sid = malloc(strlen("nvidia") + 1 + strlen(name) + 1);
- sprintf(sid, "nvidia %s", name);
+ if (res == True)
+ return temp;
- s = psensor_create(sid, strdup(name),
- SENSOR_TYPE_NVIDIA, values_max_length);
+ return UNKNOWN_DBL_VALUE;
+}
- s->nvidia_id = id;
+static double get_usage_att(char *atts, const 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++;
+ }
- return s;
+ 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;
}
-int nvidia_init()
+static const char *get_nvidia_type_str(int type)
{
- int event_base, error_base;
- int num_gpus;
+ if (type & SENSOR_TYPE_GRAPHICS)
+ return "graphics";
- nvidia_sensors_dpy = XOpenDisplay(NULL);
+ if (type & SENSOR_TYPE_VIDEO)
+ return "video";
- if (!nvidia_sensors_dpy) {
- fprintf(stderr, _("ERROR: nvidia initialization failure\n"));
- return 0;
+ if (type & SENSOR_TYPE_MEMORY)
+ return "memory";
+
+ if (type & SENSOR_TYPE_PCIE)
+ return "PCIe";
+
+ if (type & SENSOR_TYPE_AMBIENT)
+ return "ambient";
+
+ if (type & SENSOR_TYPE_TEMP)
+ return "temp";
+
+ if (type & SENSOR_TYPE_FAN) {
+ if (type & SENSOR_TYPE_RPM)
+ return "fan rpm";
+
+ return "fan level";
}
- if (XNVCTRLQueryExtension(nvidia_sensors_dpy, &event_base,
- &error_base)) {
- if (XNVCTRLQueryTargetCount(nvidia_sensors_dpy,
- NV_CTRL_TARGET_TYPE_GPU,
- &num_gpus)) {
- return num_gpus;
- }
+ return "unknown";
+}
+
+static double get_usage(int id, int type)
+{
+ const char *stype;
+ char *atts;
+ double v;
+ Bool res;
+
+ stype = get_nvidia_type_str(type);
+
+ if (!stype)
+ 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 double get_value(int id, int type)
+{
+ int att;
+
+ if (type & SENSOR_TYPE_TEMP) {
+ if (type & SENSOR_TYPE_AMBIENT)
+ att = NV_CTRL_AMBIENT_TEMPERATURE;
+ else
+ att = NV_CTRL_GPU_CORE_TEMPERATURE;
+
+ return get_att(NV_CTRL_TARGET_TYPE_GPU, id, att);
+ } else if (type & SENSOR_TYPE_FAN) {
+ if (type & SENSOR_TYPE_RPM)
+ return get_att(NV_CTRL_TARGET_TYPE_COOLER,
+ id,
+ NV_CTRL_THERMAL_COOLER_SPEED);
+ else /* SENSOR_TYPE_PERCENT */
+ return get_att(NV_CTRL_TARGET_TYPE_COOLER,
+ id,
+ NV_CTRL_THERMAL_COOLER_LEVEL);
+ } else { /* SENSOR_TYPE_PERCENT */
+ return get_usage(id, type);
}
+}
- fprintf(stderr, _("ERROR: nvidia initialization failure: %d\n"),
- error_base);
+static void update(struct psensor *sensor)
+{
+ double v;
+ int id;
- return 0;
+ id = get_nvidia_id(sensor);
+
+ v = get_value(id, sensor->type);
+
+ if (v == UNKNOWN_DBL_VALUE)
+ log_err(_("%s: Failed to retrieve measure of type %x "
+ "for NVIDIA GPU %d"),
+ PROVIDER_NAME,
+ sensor->type,
+ id);
+ psensor_set_current_value(sensor, v);
}
-void nvidia_psensor_list_update(struct psensor **sensors)
+static int check_sensor(int id, int type)
{
- struct psensor **s_ptr = sensors;
+ return get_value(id, type) != UNKNOWN_DBL_VALUE;
+}
- while (*s_ptr) {
- struct psensor *sensor = *s_ptr;
+static char *i2str(int i)
+{
+ char *str;
+ size_t n;
- if (sensor->type == SENSOR_TYPE_NVIDIA) {
- int val = nvidia_get_sensor_temp(sensor);
+ /* second +1 to avoid issue about the conversion of a double
+ * to a lower int
+ */
+ n = 1 + (ceil(log10(INT_MAX)) + 1) + 1;
- psensor_set_current_value(sensor, (double)val);
- }
+ str = malloc(n);
+ snprintf(str, n, "%d", i);
+
+ return str;
+}
+
+static struct psensor *create_nvidia_sensor(int id, int subtype, int value_len)
+{
+ char *pname, *name, *strnid, *sid;
+ const char *stype;
+ int type;
+ size_t n;
+ struct psensor *s;
+ double v;
+
+ type = SENSOR_TYPE_NVCTRL | subtype;
+
+ if (!check_sensor(id, type))
+ return NULL;
+
+ pname = get_product_name(id, type);
+ strnid = i2str(id);
+ stype = get_nvidia_type_str(type);
+
+ n = strlen(pname) + 1 + strlen(strnid) + 1 + strlen(stype) + 1;
+ name = malloc(n);
+ sprintf(name, "%s %s %s", pname, strnid, stype);
+
+ sid = malloc(strlen(PROVIDER_NAME) + 1 + strlen(name) + 1);
+ sprintf(sid, "%s %s", PROVIDER_NAME, name);
- s_ptr++;
+ s = psensor_create(sid, name, pname, type, value_len);
+ s->provider_data = malloc(sizeof(int));
+ set_nvidia_id(s, id);
+
+ if ((type & SENSOR_TYPE_GPU) && (type & SENSOR_TYPE_TEMP)) {
+ v = get_att(NV_CTRL_TARGET_TYPE_GPU,
+ id,
+ NV_CTRL_GPU_CORE_THRESHOLD);
+ s->max = v;
}
+
+ free(strnid);
+
+ return s;
}
-struct psensor **nvidia_psensor_list_add(struct psensor **sensors,
- int values_max_length)
+static int init(void)
{
- int i;
- int nvidia_gpus_count = nvidia_init();
- struct psensor **res = sensors;
+ int evt, err;
+ display = XOpenDisplay(NULL);
- if (!nvidia_gpus_count) {
- fprintf(stderr,
- _("ERROR: "
- "no nvidia chips or initialization failure\n"));
+ if (!display) {
+ log_err(_("%s: Cannot open connection to X11 server."),
+ PROVIDER_NAME);
+ return 0;
}
- for (i = 0; i < nvidia_gpus_count; i++) {
- struct psensor *sensor
- = nvidia_create_sensor(i, values_max_length);
+ if (XNVCTRLQueryExtension(display, &evt, &err))
+ return 1;
- struct psensor **tmp_psensors = psensor_list_add(res,
- sensor);
+ log_err(_("%s: Failed to retrieve NVIDIA information."),
+ PROVIDER_NAME);
- if (res != sensors)
- free(res);
+ return 0;
+}
+
+void nvidia_psensor_list_update(struct psensor **sensors)
+{
+ struct psensor *s;
+
+ while (*sensors) {
+ s = *sensors;
+
+ if (!(s->type & SENSOR_TYPE_REMOTE)
+ && s->type & SENSOR_TYPE_NVCTRL)
+ update(s);
- res = tmp_psensors;
+ sensors++;
}
+}
+
+static void add(struct psensor ***sensors, int id, int type, int values_len)
+{
+ struct psensor *s;
+
+ s = create_nvidia_sensor(id, type, values_len);
- return res;
+ if (s)
+ psensor_list_append(sensors, s);
+}
+
+void nvidia_psensor_list_append(struct psensor ***ss, int values_len)
+{
+ int i, n, utype;
+ Bool ret;
+
+ if (!init())
+ return;
+
+ ret = XNVCTRLQueryTargetCount(display, NV_CTRL_TARGET_TYPE_GPU, &n);
+ if (ret == True) {
+ for (i = 0; i < n; i++) {
+ add(ss,
+ i,
+ SENSOR_TYPE_GPU | SENSOR_TYPE_TEMP,
+ values_len);
+
+ utype = SENSOR_TYPE_GPU | SENSOR_TYPE_PERCENT;
+ add(ss, i, utype | SENSOR_TYPE_AMBIENT, values_len);
+ add(ss, i, utype | SENSOR_TYPE_GRAPHICS, values_len);
+ add(ss, i, utype | SENSOR_TYPE_VIDEO, values_len);
+ add(ss, i, utype | SENSOR_TYPE_MEMORY, values_len);
+ add(ss, i, utype | SENSOR_TYPE_PCIE, values_len);
+ }
+ }
+
+ ret = XNVCTRLQueryTargetCount(display, NV_CTRL_TARGET_TYPE_COOLER, &n);
+ if (ret == True) {
+ log_fct("%s: Number of fans: %d", PROVIDER_NAME, n);
+ for (i = 0; i < n; i++) {
+ utype = SENSOR_TYPE_FAN | SENSOR_TYPE_RPM;
+ if (check_sensor(i, utype))
+ add(ss, i, utype, values_len);
+
+ utype = SENSOR_TYPE_FAN | SENSOR_TYPE_PERCENT;
+ if (check_sensor(i, utype))
+ add(ss, i, utype, values_len);
+ }
+ } else {
+ log_err(_("%s: Failed to retrieve number of fans."),
+ PROVIDER_NAME);
+ }
+}
+
+void nvidia_cleanup(void)
+{
+ if (display) {
+ XCloseDisplay(display);
+ display = NULL;
+ }
}