From 9ef4282e9e32e857f4d0cac9df6d58f2cba159f4 Mon Sep 17 00:00:00 2001 From: reinelt Date: Wed, 3 Mar 2004 03:47:04 +0000 Subject: [lcd4linux @ 2004-03-03 03:47:04 by reinelt] big patch from Martin Hejl: - use qprintf() where appropriate - save CPU cycles on gettimeofday() - add quit() functions to free allocated memory - fixed lots of memory leaks git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@384 3ae390bd-cb1e-0410-b409-cd5a39f66f1f --- plugin_loadavg.c | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) (limited to 'plugin_loadavg.c') diff --git a/plugin_loadavg.c b/plugin_loadavg.c index 3160122..2c17492 100644 --- a/plugin_loadavg.c +++ b/plugin_loadavg.c @@ -1,4 +1,4 @@ -/* $Id: plugin_loadavg.c,v 1.3 2004/01/30 07:12:35 reinelt Exp $ +/* $Id: plugin_loadavg.c,v 1.4 2004/03/03 03:47:04 reinelt Exp $ * * plugin for load average * @@ -23,6 +23,13 @@ * * * $Log: plugin_loadavg.c,v $ + * Revision 1.4 2004/03/03 03:47:04 reinelt + * big patch from Martin Hejl: + * - use qprintf() where appropriate + * - save CPU cycles on gettimeofday() + * - add quit() functions to free allocated memory + * - fixed lots of memory leaks + * * Revision 1.3 2004/01/30 07:12:35 reinelt * HD44780 busy-flag support from Martin Hejl * loadavg() uClibc replacement from Martin Heyl @@ -59,6 +66,7 @@ #include #include #include +#include #include #include "debug.h" @@ -104,14 +112,25 @@ int getloadavg (double loadavg[], int nelem) static void my_loadavg (RESULT *result, RESULT *arg1) { - int nelem, index; - double loadavg[3]; + static int nelem; + int index,age; + static double loadavg[3]; + static struct timeval last_value; + struct timeval now; - nelem=getloadavg(loadavg, 3); - if (nelem<0) { - error ("getloadavg() failed!"); - SetResult(&result, R_STRING, ""); - return; + gettimeofday(&now,NULL); + + age = (now.tv_sec - last_value.tv_sec)*1000 + (now.tv_usec - last_value.tv_usec)/1000; + // reread every 10 msec only + if (age==0 || age>10) { + + nelem=getloadavg(loadavg, 3); + if (nelem<0) { + error ("getloadavg() failed!"); + SetResult(&result, R_STRING, ""); + return; + } + last_value=now; } index=R2N(arg1); @@ -120,6 +139,7 @@ static void my_loadavg (RESULT *result, RESULT *arg1) SetResult(&result, R_STRING, ""); return; } + SetResult(&result, R_NUMBER, &(loadavg[index-1])); return; @@ -133,3 +153,6 @@ int plugin_init_loadavg (void) return 0; } +void plugin_exit_loadavg(void) +{ +} -- cgit v1.2.3