added nvidia fan support
[psensor.git] / src / lib / nvidia.c
index b46a8b8..7352cef 100644 (file)
 
 Display *display;
 
-static char *get_product_name(int id)
+static char *get_product_name(int id, int type)
 {
        char *name;
        Bool res;
 
+       if (type & SENSOR_TYPE_FAN)
+               return strdup("NVIDIA");
+
        res = XNVCTRLQueryTargetStringAttribute(display,
                                                NV_CTRL_TARGET_TYPE_GPU,
                                                id,
@@ -129,18 +132,30 @@ static const char *get_nvidia_type_str(int type)
 {
        if (type & SENSOR_TYPE_GRAPHICS)
                return "graphics";
-       else if (type & SENSOR_TYPE_VIDEO)
+
+       if (type & SENSOR_TYPE_VIDEO)
                return "video";
-       else if (type & SENSOR_TYPE_MEMORY)
+
+       if (type & SENSOR_TYPE_MEMORY)
                return "memory";
-       else if (type & SENSOR_TYPE_PCIE)
+
+       if (type & SENSOR_TYPE_PCIE)
                return "PCIe";
-       else if (type & SENSOR_TYPE_AMBIENT)
+
+       if (type & SENSOR_TYPE_AMBIENT)
                return "ambient";
-       else if (type & SENSOR_TYPE_TEMP)
+
+       if (type & SENSOR_TYPE_TEMP)
                return "temp";
-       else
-               return "unknown";
+
+       if (type & SENSOR_TYPE_FAN) {
+               if (type & SENSOR_TYPE_RPM)
+                       return "fan rpm";
+               else
+                       return "fan level";
+       }
+
+       return "unknown";
 }
 
 static double get_usage(int id, int type)
@@ -183,7 +198,16 @@ static double get_value(int id, int type)
                        att = NV_CTRL_GPU_CORE_TEMPERATURE;
 
                return get_att(NV_CTRL_TARGET_TYPE_GPU, id, att);
-       } else { /* SENSOR_TYPE_USAGE */
+       } 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);
        }
 }
@@ -235,7 +259,7 @@ static struct psensor *create_nvidia_sensor(int id, int subtype, int value_len)
        if (!check_sensor(id, type))
                return NULL;
 
-       pname = get_product_name(id);
+       pname = get_product_name(id, type);
        strnid = i2str(id);
        stype = get_nvidia_type_str(type);
 
@@ -305,7 +329,7 @@ static void add(struct psensor ***sensors, int id, int type, int values_len)
 struct psensor **
 nvidia_psensor_list_add(struct psensor **ss, int values_len)
 {
-       int i, n, utype, rpm;
+       int i, n, utype;
        Bool ret;
 
        if (!init())
@@ -319,16 +343,11 @@ nvidia_psensor_list_add(struct psensor **ss, int values_len)
                            SENSOR_TYPE_GPU | SENSOR_TYPE_TEMP,
                            values_len);
 
-                       utype = SENSOR_TYPE_GPU | SENSOR_TYPE_USAGE;
-
+                       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);
                }
        }
@@ -337,20 +356,16 @@ nvidia_psensor_list_add(struct psensor **ss, int values_len)
        if (ret == True) {
                log_debug("NVIDIA: number of fans: %d", n);
                for (i = 0; i < n; i++) {
-                       ret = XNVCTRLQueryTargetAttribute
-                               (display,
-                                NV_CTRL_TARGET_TYPE_COOLER,
-                                i,
-                                0,
-                                NV_CTRL_THERMAL_COOLER_SPEED, &rpm);
-                       if (ret == True)
-                               log_debug("NVIDIA: fan speed %d %d", i, rpm);
-                       else
-                               log_err("NVIDIA: fail to retrieve fan speed %d",
-                                       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(_("Failed to retrieve number of NVIDIA fans."));
+               log_err(_("NVIDIA: failed to retrieve number of fans."));
        }
 
        return ss;