From c03992270c83b50fa316d751caf98e0662698e98 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Orsini Date: Thu, 1 Dec 2011 20:24:26 +0000 Subject: [PATCH] use glibtop instead of sysconf --- NEWS | 4 ++-- config.h.in | 6 ------ configure | 30 +----------------------------- configure.ac | 10 +--------- src/server/Makefile.am | 8 -------- src/server/Makefile.in | 4 ++-- src/server/server.c | 6 ------ src/server/sysinfo.c | 42 ++++++++++++++++++++++-------------------- src/server/sysinfo.h | 14 +++++++++----- 9 files changed, 37 insertions(+), 87 deletions(-) diff --git a/NEWS b/NEWS index 61ee159..fa3e2a5 100644 --- a/NEWS +++ b/NEWS @@ -1,7 +1,7 @@ * v0.6.2.15 -** psensor-server: function 'sysinfo' is now optionnal to allow - compilation on kfreebsd. It fixes Debian bug #650533. +** psensor-server: user glibtop instead of 'sysinfo' which is not + available on kfreebsd. It fixes Debian bug #650533. * v0.6.2.14 diff --git a/config.h.in b/config.h.in index 37e9a4e..05ed111 100644 --- a/config.h.in +++ b/config.h.in @@ -123,18 +123,12 @@ /* Define to 1 if you have the `strtol' function. */ #undef HAVE_STRTOL -/* Use sysinfo */ -#undef HAVE_SYSINFO_FCT - /* Define to 1 if you have the header file. */ #undef HAVE_SYS_SOCKET_H /* Define to 1 if you have the header file. */ #undef HAVE_SYS_STAT_H -/* Define to 1 if you have the header file. */ -#undef HAVE_SYS_SYSINFO_H - /* Define to 1 if you have the header file. */ #undef HAVE_SYS_TIME_H diff --git a/configure b/configure index a73c5fd..fa91b4e 100755 --- a/configure +++ b/configure @@ -682,8 +682,6 @@ GMSGFMT MSGFMT GETTEXT_MACRO_VERSION USE_NLS -SYSINFO_FCT_FALSE -SYSINFO_FCT_TRUE LIBOBJS EGREP GREP @@ -4647,7 +4645,7 @@ fi done -for ac_header in arpa/inet.h netinet/in.h stdint.h stdlib.h string.h sys/socket.h sys/time.h unistd.h getopt.h sys/sysinfo.h +for ac_header in arpa/inet.h netinet/in.h stdint.h stdlib.h string.h sys/socket.h sys/time.h unistd.h getopt.h do : as_ac_Header=`$as_echo "ac_cv_header_$ac_header" | $as_tr_sh` ac_fn_c_check_header_mongrel "$LINENO" "$ac_header" "$as_ac_Header" "$ac_includes_default" @@ -4833,28 +4831,6 @@ fi done -# Detects whether the function sysinfo is available -# At least kfreebsd 6.0.3 does not provide it. -SYSINFO_FCT= -ac_fn_c_check_func "$LINENO" "sysinfo" "ac_cv_func_sysinfo" -if test "x$ac_cv_func_sysinfo" = xyes; then : - SYSINFO_FCT='1' -$as_echo "#define HAVE_SYSINFO_FCT 1" >>confdefs.h - -else - { $as_echo "$as_me:${as_lineno-$LINENO}: WARNING: \"sysinfo function not present\"" >&5 -$as_echo "$as_me: WARNING: \"sysinfo function not present\"" >&2;} -fi - - if test -n "$SYSINFO_FCT"; then - SYSINFO_FCT_TRUE= - SYSINFO_FCT_FALSE='#' -else - SYSINFO_FCT_TRUE='#' - SYSINFO_FCT_FALSE= -fi - - { $as_echo "$as_me:${as_lineno-$LINENO}: checking whether NLS is requested" >&5 @@ -8747,10 +8723,6 @@ if test -z "${am__fastdepCC_TRUE}" && test -z "${am__fastdepCC_FALSE}"; then as_fn_error $? "conditional \"am__fastdepCC\" was never defined. Usually this means the macro was only invoked conditionally." "$LINENO" 5 fi -if test -z "${SYSINFO_FCT_TRUE}" && test -z "${SYSINFO_FCT_FALSE}"; then - as_fn_error $? "conditional \"SYSINFO_FCT\" was never defined. -Usually this means the macro was only invoked conditionally." "$LINENO" 5 -fi if test -z "${GTK_TRUE}" && test -z "${GTK_FALSE}"; then as_fn_error $? "conditional \"GTK\" was never defined. Usually this means the macro was only invoked conditionally." "$LINENO" 5 diff --git a/configure.ac b/configure.ac index 51f2670..6b84003 100644 --- a/configure.ac +++ b/configure.ac @@ -18,7 +18,7 @@ AC_PROG_RANLIB # Checks for header files. AC_PATH_X -AC_CHECK_HEADERS([arpa/inet.h netinet/in.h stdint.h stdlib.h string.h sys/socket.h sys/time.h unistd.h getopt.h sys/sysinfo.h]) +AC_CHECK_HEADERS([arpa/inet.h netinet/in.h stdint.h stdlib.h string.h sys/socket.h sys/time.h unistd.h getopt.h]) # Checks for typedefs, structures, and compiler characteristics. AC_TYPE_SIZE_T @@ -29,14 +29,6 @@ AC_FUNC_MALLOC AC_FUNC_REALLOC AC_CHECK_FUNCS([gettimeofday memmove socket strdup strtol]) -# Detects whether the function sysinfo is available -# At least kfreebsd 6.0.3 does not provide it. -SYSINFO_FCT= -AC_CHECK_FUNC(sysinfo, - [SYSINFO_FCT='1' AC_DEFINE([HAVE_SYSINFO_FCT],[1],[Use sysinfo])], - [AC_MSG_WARN("sysinfo function not present")]) -AM_CONDITIONAL(SYSINFO_FCT, test -n "$SYSINFO_FCT") - AM_GNU_GETTEXT_VERSION([0.16]) AM_GNU_GETTEXT([external]) diff --git a/src/server/Makefile.am b/src/server/Makefile.am index 2ea35c0..dc4ebc1 100644 --- a/src/server/Makefile.am +++ b/src/server/Makefile.am @@ -20,17 +20,9 @@ if GTOP AM_CPPFLAGS += $(GTOP_CFLAGS) LIBS += $(GTOP_LIBS) AM_LDFLAGS = -Wl,--as-needed - -if SYSINFO_FCT psensor_server_SOURCES += sysinfo.h sysinfo.c endif -endif - - - - - dist_man_MANS = psensor-server.1 EXTRA_DIST = description.txt psensor-server.1: server.c $(top_srcdir)/configure.ac diff --git a/src/server/Makefile.in b/src/server/Makefile.in index 8a0de61..ca6c408 100644 --- a/src/server/Makefile.in +++ b/src/server/Makefile.in @@ -37,7 +37,7 @@ host_triplet = @host@ bin_PROGRAMS = psensor-server$(EXEEXT) @GTOP_TRUE@am__append_1 = $(GTOP_CFLAGS) @GTOP_TRUE@am__append_2 = $(GTOP_LIBS) -@GTOP_TRUE@@SYSINFO_FCT_TRUE@am__append_3 = sysinfo.h sysinfo.c +@GTOP_TRUE@am__append_3 = sysinfo.h sysinfo.c subdir = src/server DIST_COMMON = $(dist_man_MANS) $(srcdir)/Makefile.am \ $(srcdir)/Makefile.in @@ -53,7 +53,7 @@ am__installdirs = "$(DESTDIR)$(bindir)" "$(DESTDIR)$(man1dir)" PROGRAMS = $(bin_PROGRAMS) am__psensor_server_SOURCES_DIST = server.c server.h sysinfo.h \ sysinfo.c -@GTOP_TRUE@@SYSINFO_FCT_TRUE@am__objects_1 = sysinfo.$(OBJEXT) +@GTOP_TRUE@am__objects_1 = sysinfo.$(OBJEXT) am_psensor_server_OBJECTS = server.$(OBJEXT) $(am__objects_1) psensor_server_OBJECTS = $(am_psensor_server_OBJECTS) psensor_server_LDADD = $(LDADD) diff --git a/src/server/server.c b/src/server/server.c index d55ecf2..ccb4d67 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -153,10 +153,8 @@ create_response_api(const char *nurl, if (!strcmp(nurl, URL_BASE_API_1_0_SENSORS)) { page = sensors_to_json_string(server_data.sensors); #ifdef HAVE_GTOP -#ifdef HAVE_SYSINFO_FCT } else if (!strcmp(nurl, URL_API_1_0_SYSINFO)) { page = sysinfo_to_json_string(&server_data.psysinfo); -#endif } else if (!strcmp(nurl, URL_API_1_0_CPU_USAGE)) { page = sensor_to_json_string(server_data.cpu_usage); #endif @@ -384,9 +382,7 @@ int main(int argc, char *argv[]) pthread_mutex_lock(&mutex); #ifdef HAVE_GTOP -#ifdef HAVE_SYSINFO_FCT sysinfo_update(&server_data.psysinfo); -#endif cpu_usage_sensor_update(server_data.cpu_usage); #endif psensor_list_update_measures(server_data.sensors); @@ -406,9 +402,7 @@ int main(int argc, char *argv[]) sensors_cleanup(); #ifdef HAVE_GTOP -#ifdef HAVE_SYSINFO_FCT sysinfo_cleanup(); -#endif cpu_cleanup(); #endif diff --git a/src/server/sysinfo.c b/src/server/sysinfo.c index 139d66b..b58a607 100644 --- a/src/server/sysinfo.c +++ b/src/server/sysinfo.c @@ -20,7 +20,6 @@ #include #include #include -#include #include @@ -52,8 +51,10 @@ void sysinfo_update(struct psysinfo *info) last_used = used; last_total = cpu->total; - /* memory */ - sysinfo(&info->sysinfo); + glibtop_get_loadavg(&info->loadavg); + glibtop_get_mem(&info->mem); + glibtop_get_swap(&info->swap); + glibtop_get_uptime(&info->uptime); /* network */ if (!info->interfaces) @@ -66,34 +67,34 @@ void sysinfo_cleanup() free(cpu); } -static json_object *ram_to_json_object(const struct sysinfo *s) +static json_object *ram_to_json_object(const struct psysinfo *s) { json_object *obj = json_object_new_object(); json_object_object_add(obj, "total", - json_object_new_double(s->totalram)); + json_object_new_double(s->mem.total)); json_object_object_add(obj, "free", - json_object_new_double(s->freeram)); + json_object_new_double(s->mem.free)); json_object_object_add(obj, "shared", - json_object_new_double(s->sharedram)); + json_object_new_double(s->mem.shared)); json_object_object_add(obj, "buffer", - json_object_new_double(s->bufferram)); + json_object_new_double(s->mem.buffer)); return obj; } -static json_object *swap_to_json_object(const struct sysinfo *s) +static json_object *swap_to_json_object(const struct psysinfo *s) { json_object *obj = json_object_new_object(); json_object_object_add(obj, "total", - json_object_new_double(s->totalswap)); + json_object_new_double(s->swap.total)); json_object_object_add(obj, "free", - json_object_new_double(s->freeswap)); + json_object_new_double(s->swap.free)); return obj; } @@ -132,32 +133,33 @@ static json_object *net_to_json_object(const struct psysinfo *s) static json_object *sysinfo_to_json_object(const struct psysinfo *s) { - static float load_scale = 1 << SI_LOAD_SHIFT; - json_object *obj = json_object_new_object(); + json_object *obj; + + obj = json_object_new_object(); json_object_object_add(obj, "load", json_object_new_double(s->cpu_rate)); json_object_object_add (obj, "load_1", - json_object_new_double(s->sysinfo.loads[0] / load_scale)); + json_object_new_double(s->loadavg.loadavg[0])); json_object_object_add (obj, "load_5", - json_object_new_double(s->sysinfo.loads[1] / load_scale)); + json_object_new_double(s->loadavg.loadavg[1])); json_object_object_add (obj, "load_15", - json_object_new_double(s->sysinfo.loads[2] / load_scale)); + json_object_new_double(s->loadavg.loadavg[2])); json_object_object_add - (obj, "uptime", json_object_new_double(s->sysinfo.uptime)); + (obj, "uptime", json_object_new_double(s->uptime.uptime)); json_object_object_add - (obj, "mem_unit", json_object_new_double(s->sysinfo.mem_unit)); + (obj, "mem_unit", json_object_new_double(1)); - json_object_object_add(obj, "ram", ram_to_json_object(&s->sysinfo)); - json_object_object_add(obj, "swap", swap_to_json_object(&s->sysinfo)); + json_object_object_add(obj, "ram", ram_to_json_object(s)); + json_object_object_add(obj, "swap", swap_to_json_object(s)); json_object_object_add(obj, "net", net_to_json_object(s)); return obj; diff --git a/src/server/sysinfo.h b/src/server/sysinfo.h index 7740e19..612b293 100644 --- a/src/server/sysinfo.h +++ b/src/server/sysinfo.h @@ -20,14 +20,18 @@ #define _PSENSOR_SYSINFO_H_ #include -#include +#include +#include +#include +#include struct psysinfo { - float cpu_rate; + glibtop_loadavg loadavg; + glibtop_mem mem; + glibtop_swap swap; + glibtop_uptime uptime; -#ifdef HAVE_SYSINFO_FCT - struct sysinfo sysinfo; -#endif + float cpu_rate; char **interfaces; }; -- 2.7.4