+ 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 const char *get_nvidia_type_str(int type)
+{
+ if (type & SENSOR_TYPE_GRAPHICS)
+ return "graphics";
+
+ if (type & SENSOR_TYPE_VIDEO)
+ return "video";
+
+ 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";
+ }
+
+ 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);
+ }
+}
+
+static void update(struct psensor *sensor)
+{
+ double v;
+ int id;
+
+ 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);
+}
+
+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;