+static double get_temp(int id, int type)
+{
+ if (type & SENSOR_TYPE_AMBIENT)
+ return get_ambient_temp(id);
+ else
+ return nv_get_temp(id);
+}
+
+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 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) {
+ v = get_temp(sensor->nvidia_id, sensor->type);
+ } 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)