From 5c148f2a8e46983d905053d3e09d67ef6948ccf4 Mon Sep 17 00:00:00 2001 From: reinelt Date: Fri, 16 Jan 2004 05:04:53 +0000 Subject: [lcd4linux @ 2004-01-16 05:04:53 by reinelt] started plugin proc_stat which should parse /proc/stat which again is a paint in the a** thinking over implementation methods of delta functions (CPU load, ...) git-svn-id: https://ssl.bulix.org/svn/lcd4linux/trunk@314 3ae390bd-cb1e-0410-b409-cd5a39f66f1f --- Makefile.am | 1 + Makefile.in | 26 ++++----- cfg.c | 12 +++-- filter.c | 12 +++-- hash.c | 100 ++++++++++++++++++++++++++++++++-- hash.h | 39 +++++++++++--- lcd4linux.conf.sample | 2 +- plugin.c | 10 +++- plugin_proc_stat.c | 146 ++++++++++++++++++++++++++++++++++++++++++++++++++ system.c | 10 +++- 10 files changed, 326 insertions(+), 32 deletions(-) create mode 100644 plugin_proc_stat.c diff --git a/Makefile.am b/Makefile.am index da198e3..44755d7 100644 --- a/Makefile.am +++ b/Makefile.am @@ -32,6 +32,7 @@ plugin_string.c \ plugin_cfg.c \ plugin_uname.c \ plugin_loadavg.c \ +plugin_proc_stat.c \ plugin_cpuinfo.c \ plugin_meminfo.c \ plugin_i2c_sensors.c \ diff --git a/Makefile.in b/Makefile.in index 4389cfc..40bda22 100644 --- a/Makefile.in +++ b/Makefile.in @@ -106,7 +106,7 @@ lcd4linux_LDADD = @DRIVERS@ @DRVLIBS@ #remove next line for liblcd4linux lcd4linux_DEPENDENCIES = @DRIVERS@ -lcd4linux_SOURCES = lcd4linux.c pid.c pid.h hash.c hash.h parser.c parser.h processor.c processor.h layout.c layout.h timer.c timer.h evaluator.c evaluator.h widget.c widget.h widget_text.c plugin.c plugin.h plugin_math.c plugin_string.c plugin_cfg.c plugin_uname.c plugin_loadavg.c plugin_cpuinfo.c plugin_meminfo.c plugin_i2c_sensors.c plugin_xmms.c system.c system.h isdn.c isdn.h wifi.c wifi.h mail.c mail.h seti.c seti.h battery.c battery.h dvb.c dvb.h filter.c filter.h exec.c exec.h expr.c expr.h mail2.c socket.c socket.h imon.c imon.h display.c display.h drv.c drv.h debug.c debug.h cfg.c cfg.h lock.c lock.h pixmap.c pixmap.h bar.c bar.h icon.c icon.h fontmap.c fontmap.h udelay.c udelay.h +lcd4linux_SOURCES = lcd4linux.c pid.c pid.h hash.c hash.h parser.c parser.h processor.c processor.h layout.c layout.h timer.c timer.h evaluator.c evaluator.h widget.c widget.h widget_text.c plugin.c plugin.h plugin_math.c plugin_string.c plugin_cfg.c plugin_uname.c plugin_loadavg.c plugin_proc_stat.c plugin_cpuinfo.c plugin_meminfo.c plugin_i2c_sensors.c plugin_xmms.c system.c system.h isdn.c isdn.h wifi.c wifi.h mail.c mail.h seti.c seti.h battery.c battery.h dvb.c dvb.h filter.c filter.h exec.c exec.h expr.c expr.h mail2.c socket.c socket.h imon.c imon.h display.c display.h drv.c drv.h debug.c debug.h cfg.c cfg.h lock.c lock.h pixmap.c pixmap.h bar.c bar.h icon.c icon.h fontmap.c fontmap.h udelay.c udelay.h #liblcd4linux_la_DEPENDENCIES = @DRIVERS@ @@ -151,14 +151,14 @@ parser.$(OBJEXT) processor.$(OBJEXT) layout.$(OBJEXT) timer.$(OBJEXT) \ evaluator.$(OBJEXT) widget.$(OBJEXT) widget_text.$(OBJEXT) \ plugin.$(OBJEXT) plugin_math.$(OBJEXT) plugin_string.$(OBJEXT) \ plugin_cfg.$(OBJEXT) plugin_uname.$(OBJEXT) plugin_loadavg.$(OBJEXT) \ -plugin_cpuinfo.$(OBJEXT) plugin_meminfo.$(OBJEXT) \ -plugin_i2c_sensors.$(OBJEXT) plugin_xmms.$(OBJEXT) system.$(OBJEXT) \ -isdn.$(OBJEXT) wifi.$(OBJEXT) mail.$(OBJEXT) seti.$(OBJEXT) \ -battery.$(OBJEXT) dvb.$(OBJEXT) filter.$(OBJEXT) exec.$(OBJEXT) \ -expr.$(OBJEXT) mail2.$(OBJEXT) socket.$(OBJEXT) imon.$(OBJEXT) \ -display.$(OBJEXT) drv.$(OBJEXT) debug.$(OBJEXT) cfg.$(OBJEXT) \ -lock.$(OBJEXT) pixmap.$(OBJEXT) bar.$(OBJEXT) icon.$(OBJEXT) \ -fontmap.$(OBJEXT) udelay.$(OBJEXT) +plugin_proc_stat.$(OBJEXT) plugin_cpuinfo.$(OBJEXT) \ +plugin_meminfo.$(OBJEXT) plugin_i2c_sensors.$(OBJEXT) \ +plugin_xmms.$(OBJEXT) system.$(OBJEXT) isdn.$(OBJEXT) wifi.$(OBJEXT) \ +mail.$(OBJEXT) seti.$(OBJEXT) battery.$(OBJEXT) dvb.$(OBJEXT) \ +filter.$(OBJEXT) exec.$(OBJEXT) expr.$(OBJEXT) mail2.$(OBJEXT) \ +socket.$(OBJEXT) imon.$(OBJEXT) display.$(OBJEXT) drv.$(OBJEXT) \ +debug.$(OBJEXT) cfg.$(OBJEXT) lock.$(OBJEXT) pixmap.$(OBJEXT) \ +bar.$(OBJEXT) icon.$(OBJEXT) fontmap.$(OBJEXT) udelay.$(OBJEXT) CFLAGS = @CFLAGS@ COMPILE = $(CC) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) LTCOMPILE = $(LIBTOOL) --mode=compile $(CC) $(DEFS) $(INCLUDES) $(AM_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) @@ -185,10 +185,10 @@ DEP_FILES = .deps/BeckmannEgle.P .deps/Crystalfontz.P .deps/Cwlinux.P \ .deps/mail.P .deps/mail2.P .deps/parport.P .deps/parser.P .deps/pid.P \ .deps/pixmap.P .deps/plugin.P .deps/plugin_cfg.P .deps/plugin_cpuinfo.P \ .deps/plugin_i2c_sensors.P .deps/plugin_loadavg.P .deps/plugin_math.P \ -.deps/plugin_meminfo.P .deps/plugin_string.P .deps/plugin_uname.P \ -.deps/plugin_xmms.P .deps/processor.P .deps/seti.P .deps/socket.P \ -.deps/system.P .deps/timer.P .deps/udelay.P .deps/widget.P \ -.deps/widget_text.P .deps/wifi.P +.deps/plugin_meminfo.P .deps/plugin_proc_stat.P .deps/plugin_string.P \ +.deps/plugin_uname.P .deps/plugin_xmms.P .deps/processor.P .deps/seti.P \ +.deps/socket.P .deps/system.P .deps/timer.P .deps/udelay.P \ +.deps/widget.P .deps/widget_text.P .deps/wifi.P SOURCES = $(lcd4linux_SOURCES) $(EXTRA_lcd4linux_SOURCES) OBJECTS = $(lcd4linux_OBJECTS) diff --git a/cfg.c b/cfg.c index df7fa79..e604d0f 100644 --- a/cfg.c +++ b/cfg.c @@ -1,4 +1,4 @@ -/* $Id: cfg.c,v 1.27 2004/01/14 11:33:00 reinelt Exp $^ +/* $Id: cfg.c,v 1.28 2004/01/16 05:04:53 reinelt Exp $^ * * config file stuff * @@ -23,6 +23,12 @@ * * * $Log: cfg.c,v $ + * Revision 1.28 2004/01/16 05:04:53 reinelt + * started plugin proc_stat which should parse /proc/stat + * which again is a paint in the a** + * thinking over implementation methods of delta functions + * (CPU load, ...) + * * Revision 1.27 2004/01/14 11:33:00 reinelt * new plugin 'uname' which does what it's called * text widget nearly finished @@ -306,9 +312,9 @@ static void cfg_add (char *section, char *key, char *val, int lock) entry=bsearch(buffer, Config, nConfig, sizeof(ENTRY), c_lookup); if (entry!=NULL) { - free (buffer); if (entry->lock>lock) return; - debug ("Warning: key '%s': value '%s' overwritten with '%s'", buffer, entry->val, val); + debug ("Warning: key <%s>: value <%s> overwritten with <%s>", buffer, entry->val, val); + free (buffer); if (entry->val) free (entry->val); entry->val=dequote(strdup(val)); return; diff --git a/filter.c b/filter.c index 53d0472..df5dcf6 100644 --- a/filter.c +++ b/filter.c @@ -1,4 +1,4 @@ -/* $Id: filter.c,v 1.9 2004/01/09 04:16:06 reinelt Exp $ +/* $Id: filter.c,v 1.10 2004/01/16 05:04:53 reinelt Exp $ * * smooth and damp functions * @@ -22,6 +22,12 @@ * * * $Log: filter.c,v $ + * Revision 1.10 2004/01/16 05:04:53 reinelt + * started plugin proc_stat which should parse /proc/stat + * which again is a paint in the a** + * thinking over implementation methods of delta functions + * (CPU load, ...) + * * Revision 1.9 2004/01/09 04:16:06 reinelt * added 'section' argument to cfg_get(), but NULLed it on all calls by now. * @@ -110,7 +116,7 @@ double smooth(char *name, int period, double value) slots=2; else if (slots>SLOTS) slots=SLOTS; - + nFilter++; Filter=realloc(Filter, nFilter*sizeof(FILTER)); Filter[i].name=strdup(name); @@ -132,7 +138,7 @@ double smooth(char *name, int period, double value) t = SECONDS(Filter[i].time[0]) - SECONDS(Filter[i].time[Filter[i].slots-1]); v = Filter[i].value[0]-Filter[i].value[Filter[i].slots-1]; - + if (t==0.0 || v<0.0) return 0; else diff --git a/hash.c b/hash.c index 02f0f65..fb9e8c1 100644 --- a/hash.c +++ b/hash.c @@ -1,4 +1,4 @@ -/* $Id: hash.c,v 1.1 2004/01/13 10:03:01 reinelt Exp $ +/* $Id: hash.c,v 1.2 2004/01/16 05:04:53 reinelt Exp $ * * hashes (associative arrays) * @@ -23,6 +23,12 @@ * * * $Log: hash.c,v $ + * Revision 1.2 2004/01/16 05:04:53 reinelt + * started plugin proc_stat which should parse /proc/stat + * which again is a paint in the a** + * thinking over implementation methods of delta functions + * (CPU load, ...) + * * Revision 1.1 2004/01/13 10:03:01 reinelt * new util 'hash' for associative arrays * new plugin 'cpuinfo' @@ -56,6 +62,16 @@ static int hash_lookup_f (const void *a, const void *b) } +// bsearch compare function for filter entries +static int filter_lookup_f (const void *a, const void *b) +{ + char *key=(char*)a; + FILTER_ITEM *item=(FILTER_ITEM*)b; + + return strcmp(key, item->key); +} + + // qsort compare function for hash tables static int hash_sort_f (const void *a, const void *b) { @@ -66,6 +82,17 @@ static int hash_sort_f (const void *a, const void *b) } +// qsort compare function for filter tables +static int filter_sort_f (const void *a, const void *b) +{ + FILTER_ITEM *ha=(FILTER_ITEM*)a; + FILTER_ITEM *hb=(FILTER_ITEM*)b; + + return strcasecmp(ha->key, hb->key); +} + + + // insert a key/val pair into the hash table // the tbale will be searched linearly if the entry // does already exist, for the table may not be sorted. @@ -92,7 +119,7 @@ void hash_set (HASH *Hash, char *key, char *val) } -void *hash_get (HASH *Hash, char *key) +char *hash_get (HASH *Hash, char *key) { HASH_ITEM *Item; @@ -122,8 +149,75 @@ void hash_destroy (HASH *Hash) // free hash table free (Hash->Items); } - + Hash->nItems=0; Hash->sorted=0; Hash->Items=NULL; } + + +// insert a key/val pair into the filter table +// the tbale will be searched linearly if the entry +// does already exist, for the table may not be sorted. +// the table will be flagged 'unsorted' afterwards +void filter_set (FILTER *Filter, char *key, char *val) +{ + int i; + + // entry already exists? + for (i=0;inItems; i++) { + if (strcmp(key, Filter->Items[i].key)==0) { + // if (Filter->Items[i].val) free (Filter->Items[i].val); + // Filter->Items[i].val=strdup(val); + return; + } + } + + // add entry + Filter->sorted=0; + Filter->nItems++; + Filter->Items=realloc(Filter->Items,Filter->nItems*sizeof(FILTER_ITEM)); + Filter->Items[i].key=strdup(key); + + + Filter->Items[i].Slots=strdup(val); +} + + +double filter_get (FILTER *Filter, char *key) +{ + FILTER_ITEM *Item; + + if (!Filter->sorted) { + qsort(Filter->Items, Filter->nItems, sizeof(FILTER_ITEM), filter_sort_f); + Filter->sorted=1; + } + + Item=bsearch(key, Filter->Items, Filter->nItems, sizeof(FILTER_ITEM), filter_lookup_f); + if (Item==NULL) return 0.0; + if (Item->Slots==NULL) return 0.0; + return Item->Slots[0].val; + +} + + +void filter_destroy (FILTER *Filter) +{ + int i; + + if (Filter->Items) { + + // free all entries + for (i=0;inItems; i++) { + if (Filter->Items[i].key) free (Filter->Items[i].key); + if (Filter->Items[i].Slots) free (Filter->Items[i].Slots); + } + + // free filter table + free (Filter->Items); + } + + Filter->nItems=0; + Filter->sorted=0; + Filter->Items=NULL; +} diff --git a/hash.h b/hash.h index ff85719..90cf760 100644 --- a/hash.h +++ b/hash.h @@ -1,4 +1,4 @@ -/* $Id: hash.h,v 1.1 2004/01/13 10:03:01 reinelt Exp $ +/* $Id: hash.h,v 1.2 2004/01/16 05:04:53 reinelt Exp $ * * hashes (associative arrays) * @@ -23,6 +23,12 @@ * * * $Log: hash.h,v $ + * Revision 1.2 2004/01/16 05:04:53 reinelt + * started plugin proc_stat which should parse /proc/stat + * which again is a paint in the a** + * thinking over implementation methods of delta functions + * (CPU load, ...) + * * Revision 1.1 2004/01/13 10:03:01 reinelt * new util 'hash' for associative arrays * new plugin 'cpuinfo' @@ -32,21 +38,42 @@ #ifndef _HASH_H_ #define _HASH_H_ - -typedef struct HASH_ITEM { +typedef struct { char *key; char *val; } HASH_ITEM; -typedef struct HASH { - int nItems; + +typedef struct { int sorted; + int nItems; HASH_ITEM *Items; } HASH; +typedef struct { + struct timeval time; + double val; +} FILTER_SLOT; + +typedef struct { + char *key; + int nSlots; + FILTER_SLOT *Slots; +} FILTER_ITEM; + +typedef struct { + int sorted; + int nItems; + FILTER_ITEM *Items; +} FILTER; + void hash_set (HASH *Hash, char *key, char *val); -void *hash_get (HASH *Hash, char *key); +char *hash_get (HASH *Hash, char *key); void hash_destroy (HASH *Hash); +void filter_set (FILTER *Filter, char *key, double val); +double filter_get (FILTER *Filter, char *key); +void filter_destroy (FILTER *Filter); + #endif diff --git a/lcd4linux.conf.sample b/lcd4linux.conf.sample index fdd34d3..7d48af3 100644 --- a/lcd4linux.conf.sample +++ b/lcd4linux.conf.sample @@ -2,7 +2,7 @@ Display LK204 { Driver 'MatrixOrbital' Model 'LK204-24-USB' Port '/dev/usb/tts/0' -# Port '/dev/tts/0' + Port '/dev/tts/0' Speed 19200 Contrast 256/2 } diff --git a/plugin.c b/plugin.c index 4fc6de3..2d381ba 100644 --- a/plugin.c +++ b/plugin.c @@ -1,4 +1,4 @@ -/* $Id: plugin.c,v 1.12 2004/01/15 04:29:45 reinelt Exp $ +/* $Id: plugin.c,v 1.13 2004/01/16 05:04:53 reinelt Exp $ * * plugin handler for the Evaluator * @@ -22,6 +22,12 @@ * * * $Log: plugin.c,v $ + * Revision 1.13 2004/01/16 05:04:53 reinelt + * started plugin proc_stat which should parse /proc/stat + * which again is a paint in the a** + * thinking over implementation methods of delta functions + * (CPU load, ...) + * * Revision 1.12 2004/01/15 04:29:45 reinelt * moved lcd4linux.conf.sample to *.old * lcd4linux.conf.sample with new layout @@ -114,6 +120,7 @@ int plugin_init_string (void); int plugin_init_cfg (void); int plugin_init_uname (void); int plugin_init_loadavg (void); +int plugin_init_proc_stat (void); int plugin_init_cpuinfo (void); int plugin_init_meminfo (void); int plugin_init_i2c_sensors (void); @@ -127,6 +134,7 @@ int plugin_init (void) plugin_init_cfg(); plugin_init_uname(); plugin_init_loadavg(); + plugin_init_proc_stat(); plugin_init_cpuinfo(); plugin_init_meminfo(); // MR: segfaults here diff --git a/plugin_proc_stat.c b/plugin_proc_stat.c new file mode 100644 index 0000000..1e7ec0b --- /dev/null +++ b/plugin_proc_stat.c @@ -0,0 +1,146 @@ +/* $Id: plugin_proc_stat.c,v 1.1 2004/01/16 05:04:53 reinelt Exp $ + * + * plugin for /proc/stat parsing + * + * Copyright 2003 Michael Reinelt + * Copyright 2004 The LCD4Linux Team + * + * This file is part of LCD4Linux. + * + * LCD4Linux 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, or (at your option) + * any later version. + * + * LCD4Linux 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., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * + * $Log: plugin_proc_stat.c,v $ + * Revision 1.1 2004/01/16 05:04:53 reinelt + * started plugin proc_stat which should parse /proc/stat + * which again is a paint in the a** + * thinking over implementation methods of delta functions + * (CPU load, ...) + * + */ + +/* + * exported functions: + * + * int plugin_init_proc_stat (void) + * adds functions to access /proc/stat + * + */ + + +#include +#include +#include +#include +#include +#include +#include + +#include "debug.h" +#include "plugin.h" +#include "hash.h" + + +static HASH Stat = { 0, 0, NULL }; + +static int renew(int msec) +{ + static struct timeval end = {0, 0}; + struct timeval now; + + // update every 10 msec + gettimeofday(&now, NULL); + if (now.tv_sec==end.tv_sec?now.tv_usec 1000000) { + end.tv_usec -= 1000000; + end.tv_sec++; + } + return 1; +} + + +static int parse_proc_stat (void) +{ + FILE *stream; + + // update every 10 msec + if (!renew(10)) return 0; + + // destroy previous hash table + hash_destroy (&Stat); + + stream=fopen("/proc/stat", "r"); + if (stream==NULL) { + error ("fopen(/proc/stat) failed: %s", strerror(errno)); + return -1; + } + + while (!feof(stream)) { + char buffer[256]; + fgets (buffer, sizeof(buffer), stream); + + if (strncmp(buffer, "cpu", 3)==0) { + unsigned long v1, v2, v3, v4; + debug ("Michi: buffer=<%s>", buffer); + if (sscanf(buffer+4, " %lu %lu %lu %lu", &v1, &v2, &v3, &v4)==4) { + debug ("Michi: v1=<%ld> v2=<%ld> v3=<%ld> v4=<%ld>", v1, v2, v3, v4); + } + } + else if (strncmp(buffer, "page ", 5)==0) { + + } + else if (strncmp(buffer, "swap ", 5)==0) { + } + else if (strncmp(buffer, "intr ", 5)==0) { + } + else if (strncmp(buffer, "disk_io:", 8)==0) { + } + else if (strncmp(buffer, "ctxt ", 5)==0) { + } + else if (strncmp(buffer, "btime ", 5)==0) { + } + else if (strncmp(buffer, "processes ", 5)==0) { + } + else { + error ("unknown line <%s> from /proc/stat"); + } + } + fclose (stream); + return 0; +} + +static void my_proc_stat (RESULT *result, RESULT *arg1) +{ + char *key, *val; + + parse_proc_stat(); + + key=R2S(arg1); + val=""; + + SetResult(&result, R_STRING, ""); +} + + +int plugin_init_proc_stat (void) +{ + AddFunction ("stat", 1, my_proc_stat); + return 0; +} + diff --git a/system.c b/system.c index 7fc0919..fe49ca7 100644 --- a/system.c +++ b/system.c @@ -1,4 +1,4 @@ -/* $Id: system.c,v 1.32 2004/01/09 04:16:06 reinelt Exp $ +/* $Id: system.c,v 1.33 2004/01/16 05:04:53 reinelt Exp $ * * system status retreivement * @@ -22,6 +22,12 @@ * * * $Log: system.c,v $ + * Revision 1.33 2004/01/16 05:04:53 reinelt + * started plugin proc_stat which should parse /proc/stat + * which again is a paint in the a** + * thinking over implementation methods of delta functions + * (CPU load, ...) + * * Revision 1.32 2004/01/09 04:16:06 reinelt * added 'section' argument to cfg_get(), but NULLed it on all calls by now. * @@ -556,7 +562,7 @@ int Busy (double *user, double *nice, double *system, double *idle) d3=smooth("cpu_sys", 500, v3); d4=smooth("cpu_idle", 500, v4); d5=d1+d2+d3+d4; - + if (d5!=0.0) { *user=(d1+d2)/d5; *nice=d2/d5; -- cgit v1.2.3