added support of free memory.
authorJean-Philippe Orsini <jeanfi@gmail.com>
Mon, 8 Sep 2014 20:15:28 +0000 (22:15 +0200)
committerJean-Philippe Orsini <jeanfi@gmail.com>
Mon, 8 Sep 2014 20:15:28 +0000 (22:15 +0200)
NEWS
NEWS.html
src/lib/Makefile.am
src/lib/Makefile.in
src/lib/nvidia.c
src/lib/pmem.c [new file with mode: 0644]
src/lib/pmem.h [new file with mode: 0644]
src/lib/psensor.c
src/lib/psensor.h

diff --git a/NEWS b/NEWS
index b829ddd..2bf623e 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -16,6 +16,7 @@ v1.1.2
 * Added NVidia fan support.
 * Added 'Hide' menu item in the contextual menu of the list of
   sensors.
+* Added free memory support.
 
 v1.1.1
 ------
index b5b40e5..6c70496 100644 (file)
--- a/NEWS.html
+++ b/NEWS.html
@@ -432,6 +432,11 @@ Added <em>Hide</em> menu item in the contextual menu of the list of
   sensors.\r
 </p>\r
 </li>\r
+<li>\r
+<p>\r
+Added free memory support.\r
+</p>\r
+</li>\r
 </ul></div>\r
 </div>\r
 </div>\r
@@ -2784,7 +2789,7 @@ Fixed BR1: crash when no temperature sensor is available
 <div id="footnotes"><hr /></div>\r
 <div id="footer">\r
 <div id="footer-text">\r
-Last updated 2014-09-08 08:39:43 CEST\r
+Last updated 2014-09-08 21:59:03 CEST\r
 </div>\r
 </div>\r
 </body>\r
index c7052b4..4fc188c 100644 (file)
@@ -37,7 +37,7 @@ libpsensor_a_SOURCES += amd.c
 endif
 
 if GTOP
-libpsensor_a_SOURCES += cpu.c
+libpsensor_a_SOURCES += cpu.c pmem.c pmem.h
 LIBS += $(GTOP_LIBS)
 AM_CPPFLAGS += $(GTOP_CFLAGS)
 endif
@@ -51,4 +51,5 @@ endif
 EXTRA_DIST=$(libpsensor_a_SOURCES) \
        amd.c \
        cpu.c \
-       nvidia.c
+       nvidia.c \
+       pmem.c pmem.h
index 044bfb7..1090062 100644 (file)
@@ -58,7 +58,7 @@ host_triplet = @host@
 @LIBATIADL_TRUE@am__append_5 = $(LIBATIADL_LIBS)
 @LIBATIADL_TRUE@am__append_6 = $(LIBATIADL_CFLAGS)
 @LIBATIADL_TRUE@am__append_7 = amd.c
-@GTOP_TRUE@am__append_8 = cpu.c
+@GTOP_TRUE@am__append_8 = cpu.c pmem.c pmem.h
 @GTOP_TRUE@am__append_9 = $(GTOP_LIBS)
 @GTOP_TRUE@am__append_10 = $(GTOP_CFLAGS)
 @JSON_TRUE@am__append_11 = psensor_json.h psensor_json.c
@@ -83,11 +83,11 @@ am__libpsensor_a_SOURCES_DIST = amd.h bool.h color.h color.c cpu.h \
        nvidia.h plog.h plog.c pmutex.h pmutex.c psensor.h psensor.c \
        ptime.h ptime.c pio.h pio.c slog.c slog.h temperature.c \
        temperature.h url.c url.h hdd_atasmart.c nvidia.c amd.c cpu.c \
-       psensor_json.h psensor_json.c
+       pmem.c pmem.h psensor_json.h psensor_json.c
 @ATASMART_TRUE@am__objects_1 = hdd_atasmart.$(OBJEXT)
 @NVIDIA_TRUE@am__objects_2 = nvidia.$(OBJEXT)
 @LIBATIADL_TRUE@am__objects_3 = amd.$(OBJEXT)
-@GTOP_TRUE@am__objects_4 = cpu.$(OBJEXT)
+@GTOP_TRUE@am__objects_4 = cpu.$(OBJEXT) pmem.$(OBJEXT)
 @JSON_TRUE@am__objects_5 = psensor_json.$(OBJEXT)
 am_libpsensor_a_OBJECTS = color.$(OBJEXT) hdd_hddtemp.$(OBJEXT) \
        lmsensor.$(OBJEXT) measure.$(OBJEXT) plog.$(OBJEXT) \
@@ -280,7 +280,8 @@ AM_CPPFLAGS = -Wall -Werror $(am__append_3) $(am__append_6) \
 EXTRA_DIST = $(libpsensor_a_SOURCES) \
        amd.c \
        cpu.c \
-       nvidia.c
+       nvidia.c \
+       pmem.c pmem.h
 
 all: all-am
 
@@ -340,6 +341,7 @@ distclean-compile:
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/nvidia.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pio.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/plog.Po@am__quote@
+@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pmem.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pmutex.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/psensor.Po@am__quote@
 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/psensor_json.Po@am__quote@
index 7352cef..fa5a898 100644 (file)
@@ -315,15 +315,12 @@ void nvidia_psensor_list_update(struct psensor **sensors)
 
 static void add(struct psensor ***sensors, int id, int type, int values_len)
 {
-       struct psensor **tmp, *s;
+       struct psensor *s;
 
        s = create_nvidia_sensor(id, type, values_len);
 
-       if (s) {
-               tmp = psensor_list_add(*sensors, s);
-               free(*sensors);
-               *sensors = tmp;
-       }
+       if (s)
+               psensor_list_append(sensors, s);
 }
 
 struct psensor **
diff --git a/src/lib/pmem.c b/src/lib/pmem.c
new file mode 100644 (file)
index 0000000..3bb301d
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * 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
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
+#include <locale.h>
+#include <libintl.h>
+#define _(str) gettext(str)
+
+#include <glibtop/mem.h>
+
+#include <pmem.h>
+
+static const char *ID_MEM_FREE = "sys memory free";
+
+void mem_psensor_list_update(struct psensor **sensors)
+{
+       struct psensor *s;
+       glibtop_mem mem;
+       double v;
+
+       while (*sensors) {
+               s = *sensors;
+
+               if (!strcmp(s->id, ID_MEM_FREE)) {
+                       glibtop_get_mem(&mem);
+                       v = mem.free * 100 / mem.total;
+
+                       psensor_set_current_value(s, v);
+               }
+
+               sensors++;
+       }
+}
+
+void mem_psensor_list_add(struct psensor ***sensors, int values_max_len)
+{
+       struct psensor *s;
+
+       s = psensor_create(strdup(ID_MEM_FREE),
+                          _("free memory"),
+                          _("memory"),
+                          SENSOR_TYPE_MEMORY | SENSOR_TYPE_PERCENT,
+                          values_max_len);
+
+       psensor_list_append(sensors, s);
+}
diff --git a/src/lib/pmem.h b/src/lib/pmem.h
new file mode 100644 (file)
index 0000000..2b5724a
--- /dev/null
@@ -0,0 +1,27 @@
+/*
+ * 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
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301 USA
+ */
+#ifndef _PSENSOR_MEM_H_
+#define _PSENSOR_MEM_H_
+
+#include <psensor.h>
+
+void mem_psensor_list_add(struct psensor ***, int values_max_len);
+void mem_psensor_list_update(struct psensor **);
+
+#endif
index ec145b2..badd6d6 100644 (file)
@@ -29,7 +29,8 @@
 #include <temperature.h>
 
 #ifdef HAVE_GTOP
-#include "cpu.h"
+#include <cpu.h>
+#include <pmem.h>
 #endif
 
 struct psensor *psensor_create(char *id,
@@ -172,7 +173,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 *));
@@ -186,6 +189,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;
@@ -418,6 +437,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;
@@ -461,7 +484,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)
@@ -470,6 +493,9 @@ 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";
 }
 
@@ -496,6 +522,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))
index bcdacc5..3d1013b 100644 (file)
@@ -186,6 +186,8 @@ struct psensor **get_all_sensors(int use_libatasmart, int values_max_length);
 struct psensor **psensor_list_add(struct psensor **sensors,
                                  struct psensor *sensor);
 
+void psensor_list_append(struct psensor ***sensors, struct psensor *sensor);
+
 struct psensor **psensor_list_copy(struct psensor **);
 
 void psensor_set_current_value(struct psensor *sensor, double value);