From: Jean-Philippe Orsini Date: Thu, 18 Sep 2014 06:51:03 +0000 (+0200) Subject: merged cpu and mem sensors code X-Git-Tag: v1.1.2~57 X-Git-Url: https://git.wpitchoune.net/gitweb/?p=psensor.git;a=commitdiff_plain;h=b352229bd4526b1193080397ebb8136420d33c7e merged cpu and mem sensors code --- diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index 9674353..6bf03c3 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -4,7 +4,6 @@ libpsensor_a_SOURCES = \ amd.h\ bool.h\ color.h color.c\ - cpu.h\ hdd.h hdd_hddtemp.c\ lmsensor.h lmsensor.c\ measure.h measure.c \ @@ -37,7 +36,7 @@ libpsensor_a_SOURCES += amd.c endif if GTOP -libpsensor_a_SOURCES += cpu.c pmem.c pmem.h +libpsensor_a_SOURCES += pgtop2.c pgtop2.h LIBS += $(GTOP_LIBS) AM_CPPFLAGS += $(GTOP_CFLAGS) endif @@ -56,7 +55,6 @@ endif EXTRA_DIST=$(libpsensor_a_SOURCES) \ amd.c \ - cpu.c \ + pgtop2.c pgtop2.h \ nvidia.c \ - pmem.c pmem.h \ pudisks2.c pudisks2.h diff --git a/src/lib/Makefile.in b/src/lib/Makefile.in index a1caa25..3c9431c 100644 --- a/src/lib/Makefile.in +++ b/src/lib/Makefile.in @@ -85,7 +85,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 pmem.c pmem.h +@GTOP_TRUE@am__append_8 = pgtop2.c pgtop2.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 @@ -117,17 +117,17 @@ am__v_AR_0 = @echo " AR " $@; am__v_AR_1 = libpsensor_a_AR = $(AR) $(ARFLAGS) libpsensor_a_LIBADD = -am__libpsensor_a_SOURCES_DIST = amd.h bool.h color.h color.c cpu.h \ - hdd.h hdd_hddtemp.c lmsensor.h lmsensor.c measure.h measure.c \ +am__libpsensor_a_SOURCES_DIST = amd.h bool.h color.h color.c hdd.h \ + hdd_hddtemp.c lmsensor.h lmsensor.c measure.h measure.c \ 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 \ - pmem.c pmem.h psensor_json.h psensor_json.c pudisks2.c \ + temperature.h url.c url.h hdd_atasmart.c nvidia.c amd.c \ + pgtop2.c pgtop2.h psensor_json.h psensor_json.c pudisks2.c \ pudisks2.h @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) pmem.$(OBJEXT) +@GTOP_TRUE@am__objects_4 = pgtop2.$(OBJEXT) @JSON_TRUE@am__objects_5 = psensor_json.$(OBJEXT) @LIBUDISKS2_TRUE@am__objects_6 = pudisks2.$(OBJEXT) am_libpsensor_a_OBJECTS = color.$(OBJEXT) hdd_hddtemp.$(OBJEXT) \ @@ -348,7 +348,7 @@ top_build_prefix = @top_build_prefix@ top_builddir = @top_builddir@ top_srcdir = @top_srcdir@ noinst_LIBRARIES = libpsensor.a -libpsensor_a_SOURCES = amd.h bool.h color.h color.c cpu.h hdd.h \ +libpsensor_a_SOURCES = amd.h bool.h color.h color.c hdd.h \ hdd_hddtemp.c lmsensor.h lmsensor.c measure.h measure.c \ 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 \ @@ -359,9 +359,8 @@ AM_CPPFLAGS = -Wall -Werror $(am__append_3) $(am__append_6) \ $(am__append_10) $(am__append_13) $(am__append_16) EXTRA_DIST = $(libpsensor_a_SOURCES) \ amd.c \ - cpu.c \ + pgtop2.c pgtop2.h \ nvidia.c \ - pmem.c pmem.h \ pudisks2.c pudisks2.h all: all-am @@ -415,15 +414,14 @@ distclean-compile: @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/amd.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/color.Po@am__quote@ -@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/cpu.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/hdd_atasmart.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/hdd_hddtemp.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/lmsensor.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/measure.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/nvidia.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/pgtop2.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@ diff --git a/src/lib/cpu.c b/src/lib/cpu.c deleted file mode 100644 index 975757c..0000000 --- a/src/lib/cpu.c +++ /dev/null @@ -1,108 +0,0 @@ -/* - * 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 -#include -#define _(str) gettext(str) - -#include - -#include - -#include "cpu.h" - -static float last_used; -static float last_total; - -static const char *PROVIDER_NAME = "gtop2"; - -struct psensor *create_cpu_usage_sensor(int measures_len) -{ - char *label, *id; - int type; - struct psensor *psensor; - - id = g_strdup_printf("%s cpu usage", PROVIDER_NAME); - label = strdup(_("CPU usage")); - type = SENSOR_TYPE_GTOP | SENSOR_TYPE_CPU_USAGE; - - psensor = psensor_create(id, - label, - strdup(_("CPU")), - type, - measures_len); - - return psensor; -} - -void cpu_psensor_list_append(struct psensor ***sensors, int measures_len) -{ - struct psensor *s; - - s = create_cpu_usage_sensor(measures_len); - - psensor_list_append(sensors, s); -} - -static double get_usage() -{ - glibtop_cpu cpu; - unsigned long int used, dt; - double cpu_rate; - - glibtop_get_cpu(&cpu); - - used = cpu.user + cpu.nice + cpu.sys; - - dt = cpu.total - last_total; - - if (dt) - cpu_rate = 100.0 * (used - last_used) / dt; - else - cpu_rate = UNKNOWN_DBL_VALUE; - - last_used = used; - last_total = cpu.total; - - return cpu_rate; -} - -void cpu_usage_sensor_update(struct psensor *s) -{ - double v; - - v = get_usage(); - - if (v != UNKNOWN_DBL_VALUE) - psensor_set_current_value(s, v); -} - -void cpu_psensor_list_update(struct psensor **sensors) -{ - struct psensor *s; - - while (*sensors) { - s = *sensors; - - if (s->type & SENSOR_TYPE_GTOP - && s->type & SENSOR_TYPE_CPU_USAGE) - cpu_usage_sensor_update(s); - - sensors++; - } -} diff --git a/src/lib/cpu.h b/src/lib/cpu.h deleted file mode 100644 index 1df8804..0000000 --- a/src/lib/cpu.h +++ /dev/null @@ -1,31 +0,0 @@ -/* - * 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_CPU_H_ -#define _PSENSOR_CPU_H_ - -#include "psensor.h" - -struct psensor *create_cpu_usage_sensor(int measures_len); - -void cpu_usage_sensor_update(struct psensor *); -void cpu_psensor_list_update(struct psensor **); - -void cpu_psensor_list_append(struct psensor ***sensors, int values_max_len); - -#endif diff --git a/src/lib/pgtop2.c b/src/lib/pgtop2.c new file mode 100644 index 0000000..5e57d5f --- /dev/null +++ b/src/lib/pgtop2.c @@ -0,0 +1,145 @@ +/* + * 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 +#include +#define _(str) gettext(str) + +#include + +#include +#include + +#include + +static float last_used; +static float last_total; + +static const char *PROVIDER_NAME = "gtop2"; + +struct psensor *create_cpu_usage_sensor(int measures_len) +{ + char *label, *id; + int type; + struct psensor *psensor; + + id = g_strdup_printf("%s cpu usage", PROVIDER_NAME); + label = strdup(_("CPU usage")); + type = SENSOR_TYPE_GTOP | SENSOR_TYPE_CPU_USAGE; + + psensor = psensor_create(id, + label, + strdup(_("CPU")), + type, + measures_len); + + return psensor; +} + +static struct psensor *create_mem_free_sensor(int measures_len) +{ + char *id; + int type; + + id = g_strdup_printf("%s mem free", PROVIDER_NAME); + type = SENSOR_TYPE_GTOP | SENSOR_TYPE_MEMORY | SENSOR_TYPE_PERCENT; + + return psensor_create(id, + strdup(_("free memory")), + strdup(_("memory")), + type, + measures_len); +} + +static double get_usage() +{ + glibtop_cpu cpu; + unsigned long int used, dt; + double cpu_rate; + + glibtop_get_cpu(&cpu); + + used = cpu.user + cpu.nice + cpu.sys; + + dt = cpu.total - last_total; + + if (dt) + cpu_rate = 100.0 * (used - last_used) / dt; + else + cpu_rate = UNKNOWN_DBL_VALUE; + + last_used = used; + last_total = cpu.total; + + return cpu_rate; +} + +static double get_mem_free() +{ + glibtop_mem mem; + double v; + + glibtop_get_mem(&mem); + v = ((double)mem.free) * 100.0 / mem.total; + + return v; +} + +void gtop2_psensor_list_append(struct psensor ***sensors, int measures_len) +{ + psensor_list_append(sensors, create_cpu_usage_sensor(measures_len)); + psensor_list_append(sensors, create_mem_free_sensor(measures_len)); +} + +void cpu_usage_sensor_update(struct psensor *s) +{ + double v; + + v = get_usage(); + + if (v != UNKNOWN_DBL_VALUE) + psensor_set_current_value(s, v); +} + +void mem_free_sensor_update(struct psensor *s) +{ + double v; + + v = get_mem_free(); + + if (v != UNKNOWN_DBL_VALUE) + psensor_set_current_value(s, v); +} + +void gtop2_psensor_list_update(struct psensor **sensors) +{ + struct psensor *s; + + while (*sensors) { + s = *sensors; + + if (s->type & SENSOR_TYPE_GTOP) { + if (s->type & SENSOR_TYPE_CPU) + cpu_usage_sensor_update(s); + else if (s->type & SENSOR_TYPE_MEMORY) + mem_free_sensor_update(s); + } + + sensors++; + } +} diff --git a/src/lib/pgtop2.h b/src/lib/pgtop2.h new file mode 100644 index 0000000..c7c3968 --- /dev/null +++ b/src/lib/pgtop2.h @@ -0,0 +1,31 @@ +/* + * 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_PGTOP2_H_ +#define _PSENSOR_PGTOP2_H_ + +#include + +struct psensor *create_cpu_usage_sensor(int measures_len); +void cpu_usage_sensor_update(struct psensor *); + +void gtop2_psensor_list_update(struct psensor **); + +void gtop2_psensor_list_append(struct psensor ***sensors, int values_max_len); + +#endif diff --git a/src/lib/pmem.c b/src/lib/pmem.c deleted file mode 100644 index 59e552c..0000000 --- a/src/lib/pmem.c +++ /dev/null @@ -1,60 +0,0 @@ -/* - * 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 -#include -#define _(str) gettext(str) - -#include - -#include - -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), - strdup(_("free memory")), - strdup(_("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 deleted file mode 100644 index 2b5724a..0000000 --- a/src/lib/pmem.h +++ /dev/null @@ -1,27 +0,0 @@ -/* - * 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 - -void mem_psensor_list_add(struct psensor ***, int values_max_len); -void mem_psensor_list_update(struct psensor **); - -#endif diff --git a/src/lib/psensor.c b/src/lib/psensor.c index fb30352..1efff09 100644 --- a/src/lib/psensor.c +++ b/src/lib/psensor.c @@ -28,11 +28,6 @@ #include #include -#ifdef HAVE_GTOP -#include -#include -#endif - struct psensor *psensor_create(char *id, char *name, char *chip, @@ -441,10 +436,6 @@ 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; @@ -522,11 +513,6 @@ void psensor_list_update_measures(struct psensor **sensors) { lmsensor_psensor_list_update(sensors); -#ifdef HAVE_GTOP - cpu_psensor_list_update(sensors); - mem_psensor_list_update(sensors); -#endif - if (psensor_list_contains_type(sensors, SENSOR_TYPE_HDDTEMP)) hddtemp_psensor_list_update(sensors); diff --git a/src/main.c b/src/main.c index 7c68b64..267aa43 100644 --- a/src/main.c +++ b/src/main.c @@ -68,7 +68,7 @@ #endif #ifdef HAVE_GTOP -#include "cpu.h" +#include #endif static const char *program_name; @@ -169,6 +169,9 @@ static void *update_measures(void *data) #ifdef HAVE_LIBUDISKS2 udisks2_psensor_list_update(sensors); #endif +#ifdef HAVE_GTOP + gtop2_psensor_list_update(sensors); +#endif psensor_log_measures(sensors); @@ -468,7 +471,7 @@ static struct psensor **create_sensors_list(const char *url, #endif #ifdef HAVE_GTOP if (config_is_gtop2_enabled()) - cpu_psensor_list_append(&sensors, 600); + gtop2_psensor_list_append(&sensors, 600); #endif #ifdef HAVE_LIBUDISKS2 if (config_is_udisks2_enabled()) diff --git a/src/server/server.c b/src/server/server.c index 8a6e578..4e89100 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -39,7 +39,7 @@ #ifdef HAVE_GTOP #include "sysinfo.h" -#include "cpu.h" +#include #endif #include