/*
- * Copyright (C) 2010-2012 jeanfi@gmail.com
+ * 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
#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 "psensor.h"
+#include <psensor.h>
Display *display;
-/*
- Returns the temperature (Celcius) of a NVidia GPU.
-*/
-static int get_temp(struct psensor *sensor)
+static char *get_product_name(int id)
+{
+ char *name;
+ Bool res;
+
+ 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");
+}
+
+static double get_temp(int id)
{
- int temp;
Bool res;
+ int temp;
res = XNVCTRLQueryTargetAttribute(display,
NV_CTRL_TARGET_TYPE_GPU,
- sensor->nvidia_id,
+ id,
0,
NV_CTRL_GPU_CORE_TEMPERATURE,
&temp);
if (res == True)
return temp;
+ else
+ return UNKNOWN_DBL_VALUE;
+}
- fprintf(stderr, _("ERROR: failed to retrieve nvidia temperature\n"));
- return 0;
+static double get_ambient_temp(int id)
+{
+ Bool res;
+ int temp;
+
+ res = XNVCTRLQueryTargetAttribute(display,
+ NV_CTRL_TARGET_TYPE_GPU,
+ id,
+ 0,
+ NV_CTRL_AMBIENT_TEMPERATURE,
+ &temp);
+
+ if (res == True)
+ return temp;
+ else
+ return UNKNOWN_DBL_VALUE;
+}
+
+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++;
+ }
+
+ 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 struct psensor *create_sensor(int id, int values_len)
+static const char *get_nvidia_type_str(int type)
{
- char name[200];
- char *sid;
+ if (type & SENSOR_TYPE_GRAPHICS)
+ return "graphics";
+ else if (type & SENSOR_TYPE_VIDEO)
+ return "video";
+ else if (type & SENSOR_TYPE_MEMORY)
+ return "memory";
+ else if (type & SENSOR_TYPE_PCIE)
+ return "PCIe";
+ else if (type & SENSOR_TYPE_AMBIENT)
+ return "ambient";
+ else if (type & SENSOR_TYPE_TEMP)
+ return "temp";
+ else
+ 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)
+{
+ if (type & SENSOR_TYPE_TEMP) {
+ if (type & SENSOR_TYPE_AMBIENT)
+ return get_ambient_temp(id);
+ else
+ return get_temp(id);
+ } else { /* SENSOR_TYPE_USAGE */
+ return get_usage(id, type);
+ }
+}
+
+static void update(struct psensor *sensor)
+{
+ double v;
+
+ v = get_value(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 int check_sensor(int id, int type)
+{
+ return get_value(id, type) != UNKNOWN_DBL_VALUE;
+}
+
+static char *i2str(int i)
+{
+ char *str;
+ size_t n;
+
+ /* second +1 to avoid issue about the conversion of a double
+ * to a lower int */
+ n = 1 + (ceil(log10(INT_MAX)) + 1) + 1;
+
+ 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;
- sprintf(name, "GPU%d", id);
+ type = SENSOR_TYPE_NVCTRL | SENSOR_TYPE_GPU | subtype;
+
+ if (!check_sensor(id, type))
+ return NULL;
+
+ pname = get_product_name(id);
+ 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("nvidia") + 1 + strlen(name) + 1);
sprintf(sid, "nvidia %s", name);
- s = psensor_create(sid,
- strdup(name),
- SENSOR_TYPE_NVIDIA_TEMP,
- values_len);
-
+ s = psensor_create(sid, name, pname, type, value_len);
s->nvidia_id = id;
+ free(strnid);
+
return s;
}
/*
Opens connection to X server and returns the number
- of NVidia GPUs.
+ of NVIDIA GPUs.
- Return 0 if no NVidia gpus or cannot get information.
+ Return 0 if no NVIDIA gpus or cannot get information.
*/
static int init()
{
display = XOpenDisplay(NULL);
if (!display) {
- fprintf(stderr,
- _("ERROR: Cannot open connection to X Server\n"));
+ log_err(_("Cannot open connection to X11 server."));
return 0;
}
XNVCTRLQueryTargetCount(display, NV_CTRL_TARGET_TYPE_GPU, &n))
return n;
- fprintf(stderr, _("ERROR: Cannot retrieve NVidia information\n"));
+ log_err(_("Failed to retrieve NVIDIA information."));
return 0;
}
while (*ss) {
s = *ss;
- if (s->type == SENSOR_TYPE_NVIDIA_TEMP)
- psensor_set_current_value(s, get_temp(s));
+ if (s->type & SENSOR_TYPE_NVCTRL)
+ update(s);
ss++;
}
}
+/* static struct psensor ** */
+/* sensor_add(struct psensor **sensors, int subtype, int values_len) */
+/* { */
+/* } */
+
struct psensor **nvidia_psensor_list_add(struct psensor **sensors,
int values_len)
{
ss = sensors;
for (i = 0; i < n; i++) {
- s = create_sensor(i, values_len);
+ s = create_nvidia_sensor(i, SENSOR_TYPE_TEMP, values_len);
+ tmp = psensor_list_add(ss, s);
+ if (ss != tmp)
+ free(ss);
+
+ ss = tmp;
+ s = create_nvidia_sensor
+ (i,
+ SENSOR_TYPE_USAGE | SENSOR_TYPE_AMBIENT,
+ values_len);
+ tmp = psensor_list_add(ss, s);
+ if (ss != tmp)
+ free(ss);
+
+ ss = tmp;
+ s = create_nvidia_sensor
+ (i,
+ SENSOR_TYPE_USAGE | SENSOR_TYPE_GRAPHICS,
+ values_len);
+ tmp = psensor_list_add(ss, s);
+ if (ss != tmp)
+ free(ss);
+ ss = tmp;
+ s = create_nvidia_sensor
+ (i,
+ SENSOR_TYPE_USAGE | SENSOR_TYPE_VIDEO,
+ values_len);
tmp = psensor_list_add(ss, s);
+ if (ss != tmp)
+ free(ss);
+ ss = tmp;
+ s = create_nvidia_sensor
+ (i,
+ SENSOR_TYPE_USAGE | SENSOR_TYPE_MEMORY,
+ values_len);
+ tmp = psensor_list_add(ss, s);
+ if (ss != tmp)
+ free(ss);
+
+ ss = tmp;
+ s = create_nvidia_sensor
+ (i,
+ SENSOR_TYPE_USAGE | SENSOR_TYPE_PCIE,
+ values_len);
+ tmp = psensor_list_add(ss, s);
if (ss != tmp)
free(ss);