avoid to update SMART more than each 30s (costly operation for the udisks2 daemon)
[psensor.git] / src / lib / psensor.c
index bf7765b..fb30352 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010-2013 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 "hdd.h"
-#include "psensor.h"
-#include "lmsensor.h"
+#include <hdd.h>
+#include <psensor.h>
+#include <lmsensor.h>
+#include <temperature.h>
 
 #ifdef HAVE_GTOP
-#include "cpu.h"
+#include <cpu.h>
+#include <pmem.h>
 #endif
 
 struct psensor *psensor_create(char *id,
@@ -37,13 +39,13 @@ struct psensor *psensor_create(char *id,
                               unsigned int type,
                               int values_max_length)
 {
-       struct psensor *psensor
-           = (struct psensor *)malloc(sizeof(struct psensor));
+       struct psensor *psensor;
+
+       psensor = (struct psensor *)malloc(sizeof(struct psensor));
 
        psensor->id = id;
        psensor->name = name;
        psensor->chip = chip;
-       psensor->enabled = 1;
        psensor->min = UNKNOWN_DBL_VALUE;
        psensor->max = UNKNOWN_DBL_VALUE;
 
@@ -64,8 +66,12 @@ struct psensor *psensor_create(char *id,
 
        psensor->color = NULL;
 
+       psensor->graph_enabled = 1;
        psensor->appindicator_enabled = 0;
 
+       psensor->provider_data = NULL;
+       psensor->provider_data_free_fct = &free;
+
        return psensor;
 }
 
@@ -80,6 +86,7 @@ void psensor_values_resize(struct psensor *s, int new_size)
 
        if (cur_ms) {
                int i;
+
                for (i = 0; i < new_size - 1 && i < cur_size - 1; i++)
                        measure_copy(&cur_ms[cur_size - i - 1],
                                     &new_ms[new_size - i - 1]);
@@ -91,26 +98,30 @@ void psensor_values_resize(struct psensor *s, int new_size)
        s->measures = new_ms;
 }
 
-void psensor_free(struct psensor *sensor)
+void psensor_free(struct psensor *s)
 {
-       if (sensor) {
-               log_debug("Cleanup %s", sensor->id);
+       if (!s)
+               return;
 
-               free(sensor->name);
-               free(sensor->id);
+       log_debug("Cleanup %s", s->id);
 
-               if (sensor->chip)
-                       free(sensor->chip);
+       free(s->name);
+       free(s->id);
 
-               if (sensor->color)
-                       free(sensor->color);
+       if (s->chip)
+               free(s->chip);
 
-               measures_free(sensor->measures);
+       if (s->color)
+               free(s->color);
 
-               free(sensor->url);
+       measures_free(s->measures);
 
-               free(sensor);
-       }
+       free(s->url);
+
+       if (s->provider_data && s->provider_data_free_fct)
+               s->provider_data_free_fct(s->provider_data);
+
+       free(s);
 }
 
 void psensor_list_free(struct psensor **sensors)
@@ -170,7 +181,9 @@ int psensor_list_contains_type(struct psensor **sensors, unsigned int type)
 struct psensor **psensor_list_add(struct psensor **sensors,
                                  struct psensor *sensor)
 {
-       int size = psensor_list_size(sensors);
+       int size;
+
+       size = psensor_list_size(sensors);
 
        struct psensor **result
            = malloc((size + 1 + 1) * sizeof(struct psensor *));
@@ -184,6 +197,22 @@ struct psensor **psensor_list_add(struct psensor **sensors,
        return result;
 }
 
+void psensor_list_append(struct psensor ***sensors, struct psensor *sensor)
+{
+       struct psensor **tmp;
+
+       if (!sensor)
+               return;
+
+       tmp = psensor_list_add(*sensors, sensor);
+
+       if (tmp != *sensors) {
+               free(*sensors);
+               *sensors = tmp;
+       }
+}
+
+
 struct psensor *psensor_list_get_by_id(struct psensor **sensors, const char *id)
 {
        struct psensor **sensors_cur = sensors;
@@ -203,23 +232,8 @@ int is_temp_type(unsigned int type)
        return type & SENSOR_TYPE_TEMP;
 }
 
-int is_fan_type(unsigned int type)
-{
-       return type & SENSOR_TYPE_FAN;
-}
-
-double celcius_to_fahrenheit(double c)
-{
-       return c * (9.0/5.0) + 32;
-}
-
-double fahrenheit_to_celcius(double f)
-{
-       return (f - 32) * (5.0/9.0);
-}
-
 char *
-psensor_value_to_str(unsigned int type, double value, int use_celcius)
+psensor_value_to_str(unsigned int type, double value, int use_celsius)
 {
        char *str;
        const char *unit;
@@ -230,10 +244,10 @@ psensor_value_to_str(unsigned int type, double value, int use_celcius)
         */
        str = malloc(20);
 
-       unit = psensor_type_to_unit_str(type, use_celcius);
+       unit = psensor_type_to_unit_str(type, use_celsius);
 
-       if (is_temp_type(type) && !use_celcius)
-               value = celcius_to_fahrenheit(value);
+       if (is_temp_type(type) && !use_celsius)
+               value = celsius_to_fahrenheit(value);
 
        sprintf(str, "%.0f%s", value, unit);
 
@@ -243,9 +257,9 @@ psensor_value_to_str(unsigned int type, double value, int use_celcius)
 char *
 psensor_measure_to_str(const struct measure *m,
                       unsigned int type,
-                      unsigned int use_celcius)
+                      unsigned int use_celsius)
 {
-       return psensor_value_to_str(type, m->value, use_celcius);
+       return psensor_value_to_str(type, m->value, use_celsius);
 }
 
 void psensor_set_current_value(struct psensor *sensor, double value)
@@ -258,9 +272,7 @@ void psensor_set_current_value(struct psensor *sensor, double value)
        psensor_set_current_measure(sensor, value, tv);
 }
 
-void
-psensor_set_current_measure(struct psensor *s,
-                           double v, struct timeval tv)
+void psensor_set_current_measure(struct psensor *s, double v, struct timeval tv)
 {
        memmove(s->measures,
                &s->measures[1],
@@ -301,7 +313,7 @@ struct measure *psensor_get_current_measure(struct psensor *sensor)
   Returns the minimal value of a given 'type' (SENSOR_TYPE_TEMP or
   SENSOR_TYPE_FAN)
  */
-double get_min_value(struct psensor **sensors, int type)
+static double get_min_value(struct psensor **sensors, int type)
 {
        double m = UNKNOWN_DBL_VALUE;
        struct psensor **s = sensors;
@@ -309,7 +321,7 @@ double get_min_value(struct psensor **sensors, int type)
        while (*s) {
                struct psensor *sensor = *s;
 
-               if (sensor->enabled && (sensor->type & type)) {
+               if (sensor->type & type) {
                        int i;
                        double t;
 
@@ -341,9 +353,10 @@ double get_max_value(struct psensor **sensors, int type)
        while (*s) {
                struct psensor *sensor = *s;
 
-               if (sensor->enabled && (sensor->type & type)) {
+               if (sensor->type & type) {
                        int i;
                        double t;
+
                        for (i = 0; i < sensor->values_max_length; i++) {
                                t = sensor->measures[i].value;
 
@@ -369,7 +382,7 @@ psensor_get_max_current_value(struct psensor **sensors, unsigned int type)
        while (*s_cur) {
                struct psensor *s = *s_cur;
 
-               if (s->enabled && (s->type & type)) {
+               if (s->graph_enabled && (s->type & type)) {
                        double v = psensor_get_current_value(s);
 
                        if (m == UNKNOWN_DBL_VALUE || v > m)
@@ -428,6 +441,10 @@ struct psensor **get_all_sensors(int use_libatasmart, int values_max_length)
                }
 #endif
 
+#ifdef HAVE_GTOP
+       mem_psensor_list_add(&psensors, values_max_length);
+#endif
+
        if (!psensors) {        /* there is no detected sensors */
                psensors = malloc(sizeof(struct psensor *));
                *psensors = NULL;
@@ -438,16 +455,28 @@ struct psensor **get_all_sensors(int use_libatasmart, int values_max_length)
 
 const char *psensor_type_to_str(unsigned int type)
 {
-       if (type & SENSOR_TYPE_NVCTRL)
-               return "NVidia GPU";
+       if (type & SENSOR_TYPE_NVCTRL) {
+               if (type & SENSOR_TYPE_TEMP)
+                       return "Temperature";
+               else if (type & SENSOR_TYPE_GRAPHICS)
+                       return "Graphics usage";
+               else if (type & SENSOR_TYPE_VIDEO)
+                       return "Video usage";
+               else if (type & SENSOR_TYPE_MEMORY)
+                       return "Memory usage";
+               else if (type & SENSOR_TYPE_PCIE)
+                       return "PCIe usage";
+
+               return "NVIDIA GPU";
+       }
 
        if (type & SENSOR_TYPE_ATIADL) {
                if (type & SENSOR_TYPE_TEMP)
                        return "AMD GPU Temperature";
                else if (type & SENSOR_TYPE_RPM)
                        return "AMD GPU Fan Speed";
-               else /* type & SENSOR_TYPE_USAGE */
-                       return "AMD GPU Usage";
+               /*else type & SENSOR_TYPE_USAGE */
+               return "AMD GPU Usage";
        }
 
        if ((type & SENSOR_TYPE_HDD_TEMP) == SENSOR_TYPE_HDD_TEMP)
@@ -459,7 +488,7 @@ const char *psensor_type_to_str(unsigned int type)
        if (type & SENSOR_TYPE_TEMP)
                return "Temperature";
 
-       if (type & SENSOR_TYPE_FAN)
+       if (type & SENSOR_TYPE_RPM)
                return "Fan";
 
        if (type & SENSOR_TYPE_CPU)
@@ -468,24 +497,25 @@ const char *psensor_type_to_str(unsigned int type)
        if (type & SENSOR_TYPE_REMOTE)
                return "Remote";
 
+       if (type & SENSOR_TYPE_MEMORY)
+               return "Memory";
+
        return "N/A";
 }
 
 
-const char *psensor_type_to_unit_str(unsigned int type, int use_celcius)
+const char *psensor_type_to_unit_str(unsigned int type, int use_celsius)
 {
        if (is_temp_type(type)) {
-               if (use_celcius)
+               if (use_celsius)
                        return "\302\260C";
-               else
-                       return "\302\260F";
-       } else if (is_fan_type(type)) {
+               return "\302\260F";
+       } else if (type & SENSOR_TYPE_RPM) {
                return _("RPM");
-       } else if (type & SENSOR_TYPE_CPU_USAGE) {
+       } else if (type & SENSOR_TYPE_PERCENT) {
                return _("%");
-       } else {
-               return _("N/A");
        }
+       return _("N/A");
 }
 
 void psensor_list_update_measures(struct psensor **sensors)
@@ -494,6 +524,7 @@ void psensor_list_update_measures(struct psensor **sensors)
 
 #ifdef HAVE_GTOP
        cpu_psensor_list_update(sensors);
+       mem_psensor_list_update(sensors);
 #endif
 
        if (psensor_list_contains_type(sensors, SENSOR_TYPE_HDDTEMP))
@@ -542,9 +573,32 @@ struct psensor **psensor_list_copy(struct psensor **sensors)
 }
 
 char *
-psensor_current_value_to_str(const struct psensor *s, unsigned int celcius)
+psensor_current_value_to_str(const struct psensor *s, unsigned int use_celsius)
 {
        return psensor_value_to_str(s->type,
                                    psensor_get_current_value(s),
-                                   celcius);
+                                   use_celsius);
+}
+
+struct psensor **psensor_list_filter_graph_enabled(struct psensor **sensors)
+{
+       int n, i;
+       struct psensor **result, **cur, *s;
+
+       if (!sensors)
+               return NULL;
+
+       n = psensor_list_size(sensors);
+       result = malloc((n+1) * sizeof(struct psensor *));
+
+       for (cur = sensors, i = 0; *cur; cur++) {
+               s = *cur;
+
+               if (s->graph_enabled)
+                       result[i++] = s;
+       }
+
+       result[i] = NULL;
+
+       return result;
 }