Hi,
first of all, thanks for accepting my patch for wmtime.
Now, I'd like to submit another patch - for wmSMPmon dockapp this time.
I wanted to use it on Solaris which has different way to obtain system
statistics. I build a support file and changed Makefile to take the
operating system into account.
Then I moved to fixing the widget. The comments in the code said that it
supports only 2 CPUs. I've added an aggregation so that it still shows
two graphs but represents all CPUs.
Lastly, I some programmatic flaws - like multiple loops for one task -
cleaning up the code a bit. It should also speed up the widget a little.
I'd be happy to do more clean-ups (and get rid of that french naming).
Any comments are welcome.
Cheers,
Milan
diff --git a/wmSMPmon/Changelog b/wmSMPmon/Changelog
index bf1b1e5..99801da 100644
--- a/wmSMPmon/Changelog
+++ b/wmSMPmon/Changelog
@@ -1,3 +1,9 @@
+3.2
+
+- Added Solaris support.
+- Added CPU load aggreggation so that they fit into two columns.
+- A bit of code clean-up to speed up things.
+
3.1
- Fixed bug where wmSMPmon would crash if no swap is enabled/present
diff --git a/wmSMPmon/wmSMPmon/Makefile b/wmSMPmon/wmSMPmon/Makefile
index 338c665..1253b7f 100644
--- a/wmSMPmon/wmSMPmon/Makefile
+++ b/wmSMPmon/wmSMPmon/Makefile
@@ -5,7 +5,9 @@
BINDIR=/usr/local/bin/
MANDIR=/usr/local/share/man/
-SRC = general.c sysinfo-linux.c ../wmgeneral/wmgeneral.c wmSMPmon.c
+OS := $(shell uname -s)
+
+SRC = general.c ../wmgeneral/wmgeneral.c wmSMPmon.c
EXE = wmSMPmon
MAN = wmSMPmon.1
OBJ = $(SRC:.c=.o)
@@ -16,10 +18,18 @@ CC = gcc
CFLAGS = -Wall -O2 -g
LIB = -L/usr/X11R6/lib -lXpm -lXext -lX11
+ifeq ($(OS),Linux)
+SRC += sysinfo-linux.c
+endif
+ifeq ($(OS),SunOS)
+SRC += sysinfo-solaris.c
+LIB += -lkstat
+endif
+
all: $(OBJ)
$(CC) -o $(EXE) $(OBJ) $(LIB)
-$(OBJ): %.o : %.c
+$(OBJ): %.o : %.c
$(CC) $(CFLAGS) -c -o $@ $<
clean:
diff --git a/wmSMPmon/wmSMPmon/wmSMPmon.c b/wmSMPmon/wmSMPmon/wmSMPmon.c
index 47cdc55..87ca832 100644
--- a/wmSMPmon/wmSMPmon/wmSMPmon.c
+++ b/wmSMPmon/wmSMPmon/wmSMPmon.c
@@ -23,7 +23,7 @@ CURRENT MAINTAINER: Thomas Ribbrock <emga...@gmx.net>
#include "standards.h"
#include "sysinfo-linux.h"
-#define VERSION "3.1"
+#define VERSION "3.2"
/*###### Dividers for redraw-loops ######################################*/
#define DIV1 6
@@ -44,8 +44,8 @@ int main(int argc, char **argv)
unsigned int t0[TAILLE_T], /* history for CPU 0 -> Graph */
t1[TAILLE_T], /* history for CPU 1 -> Graph */
tm[TAILLE_T], /* history for CPU 0+1 -> Graph */
- c1 = 6,
- c2 = 9,
+ c1 = DIV1,
+ c2 = DIV2,
etat = 1,
lecture = 1,
delay = 250000,
@@ -54,19 +54,19 @@ int main(int argc, char **argv)
load0o = 0,
load1o = 0,
no_swap = FAUX,
- draw_graph = FAUX,
+ draw_graph = VRAI,
NumCPUs, /* number of CPUs */
i = 0, /* counter */
mem = 0, /* current memory/swap scaled to 0-100 */
prec_mem = 0, /* memory from previous round */
prec_swap = 0, /* swap from previous round */
load_width = 3; /* width of load bar: 3 for SMP, 8 for UP */
-
+
unsigned long load0t=0, load1t=0 ;
-
+
unsigned int *CPU_Load; /* CPU load per CPU array */
+ unsigned int t_idx = 0; /* Index to load history tables */
-
/********** Initialisation **********/
NumCPUs = NumCpus_DoInit();
CPU_Load = alloc_c((NumCPUs) * sizeof(int));
@@ -79,11 +79,10 @@ int main(int argc, char **argv)
{
load_width = 3;
}
-
+
Myname = strrchr(argv[0], '/');
if (Myname) ++Myname; else Myname = argv[0];
-
/* process command line args */
i = 1; /* skip program name (i=0) */
while(argc > i)
@@ -120,7 +119,7 @@ int main(int argc, char **argv)
{
etat = atoi(argv[i]) ;
}
-
+
if(1 > etat || etat > 3)
usage(NumCPUs, "Unknown graph style") ;
i ++ ;
@@ -151,7 +150,7 @@ int main(int argc, char **argv)
wmSMPmon_mask_bits[i] = 0xDF;
}
}
-
+
openXwindow(argc,argv,wmSMPmon_master_xpm,wmSMPmon_mask_bits,wmSMPmon_mask_width,wmSMPmon_mask_height) ;
if(NumCPUs >= 2)
@@ -159,15 +158,14 @@ int main(int argc, char **argv)
/* we have two CPUs -> draw separator between CPU load bars */
copyXPMArea(12, 4, 2, HAUTEUR + 2, 7, 4) ;
}
-
+
delay = delay / 2 ;
- for(i = 0 ; i < TAILLE_T ; i ++)
+ for(i = 0 ; i < TAILLE_T ; i ++) {
t0[i] = 0 ;
- for(i = 0 ; i < TAILLE_T ; i ++)
t1[i] = 0 ;
- for(i = 0 ; i < TAILLE_T ; i ++)
tm[i] = 0 ;
+ }
/* -no-swap option was given */
if(no_swap)
@@ -180,22 +178,29 @@ int main(int argc, char **argv)
{
CPU_Load = Get_CPU_Load(CPU_Load, NumCPUs);
- load = CPU_Load[0];
- load0t = load0t + load ;
+ load = CPU_Load[0];
+ for (i = 1; i < NumCPUs >> 1; i++) {
+ load += CPU_Load[i];
+ }
+ load = load / i;
+ load0t = load0t + load;
if(load != load0o)
{
/* redraw only if cpu load changed */
delta = HAUTEUR - load ;
- copyXPMArea(108, 0, load_width, HAUTEUR, 4, 5) ;
- copyXPMArea(108, delta + 32, load_width, load, 4, 5 + delta) ;
+ copyXPMArea(108, 0, load_width, HAUTEUR, 4, 5) ;
+ copyXPMArea(108, delta + 32, load_width, load, 4, 5 + delta) ;
load0o = load;
}
if(NumCPUs >= 2)
{
/* we have two CPUs -> do CPU 1 */
- /* FIXME: What about more CPUs? */
- load = CPU_Load[1];
+ load = 0;
+ for (; i < NumCPUs; i++) {
+ load += CPU_Load[i];
+ }
+ load = load / (NumCPUs >> 1);
if(load != load1o)
{
@@ -211,7 +216,6 @@ int main(int argc, char **argv)
graph below. With only one CPU, 'load' will still be
CPU_Load[0], on a SMP system, it will be CPU_Load[1]. */
load1t = load1t + load ;
-
if(c1 > DIV1)
{
@@ -223,7 +227,7 @@ int main(int argc, char **argv)
copyXPMArea(30, 63, 30, 8, 29, 39) ;
copyXPMArea(0, 63, (mem * 30 / 100), 8, 29, 39) ;
prec_mem = mem ;
- }
+ }
if(!no_swap)
{
@@ -248,50 +252,47 @@ int main(int argc, char **argv)
}
c1 = 0;
}
-
- if(c2 > DIV2)
+
+ if(c2 > DIV2) {
+ if((t0[t_idx] = load0t / c2) > HAUTEUR)
+ t0[t_idx] = HAUTEUR ;
+ t0[t_idx] /= 2;
+ if((t1[t_idx] = load1t / c2) > HAUTEUR)
+ t1[t_idx] = HAUTEUR ;
+ t1[t_idx] /= 2;
+ if((tm[t_idx] = (load0t + load1t) / (2 * c2)) > HAUTEUR)
+ tm[t_idx] = HAUTEUR ;
+ load0t = 0 ;
+ load1t = 0 ;
+ t_idx = (t_idx + 1) % TAILLE_T;
draw_graph = VRAI ;
+ c2 = 0;
+ }
if(draw_graph)
{
- for(i = 1 ; i < TAILLE_T ; i ++)
- {
- t0[i - 1] = t0[i] ;
- t1[i - 1] = t1[i] ;
- tm[i - 1] = tm[i] ;
- }
- if((t0[TAILLE_T - 1] = load0t / c2) > HAUTEUR)
- t0[TAILLE_T - 1] = HAUTEUR ;
- if((t1[TAILLE_T - 1] = load1t / c2) > HAUTEUR)
- t1[TAILLE_T - 1] = HAUTEUR ;
- if((tm[TAILLE_T - 1] = (load0t + load1t) / (2 * c2)) > HAUTEUR)
- tm[TAILLE_T - 1] = HAUTEUR ;
- load0t = 0 ;
- load1t = 0 ;
-
/* draw graph */
switch(etat)
{
case 1 :
copyXPMArea(64, 32, TAILLE_T, HAUTEUR, 15, 5) ;
- for(i = 0 ; i < TAILLE_T ; i ++)
- copyXPMArea(116, 0, 1, tm[i], 15 + i, HAUTEUR + 5 - tm[i]) ;
+ for(i = 0, load = t_idx ; i < TAILLE_T ; i ++, load++)
+ copyXPMArea(116, 0, 1, tm[load % TAILLE_T], 15 + i, HAUTEUR + 5 - tm[load % TAILLE_T]) ;
break ;
case 2 :
copyXPMArea(64, 0, TAILLE_T, HAUTEUR, 15, 5) ;
- for(i = 0 ; i < TAILLE_T ; i ++)
- copyXPMArea(116, 0, 1, t0[i]/2, 15 + i, HAUTEUR/2 + 5 - t0[i]/2) ;
- for(i = 0 ; i < TAILLE_T ; i ++)
- copyXPMArea(116, 0, 1, t1[i]/2, 15 + i, HAUTEUR/2 + 21 - t1[i]/2) ;
+ for(i = 0, load = t_idx ; i < TAILLE_T ; i ++, load++) {
+ copyXPMArea(116, 0, 1, t0[load % TAILLE_T], 15 + i, HAUTEUR/2 + 5 - t0[load % TAILLE_T]) ;
+ copyXPMArea(116, 0, 1, t1[load % TAILLE_T], 15 + i, HAUTEUR/2 + 21 - t1[load % TAILLE_T]) ;
+ }
break ;
case 3 :
copyXPMArea(64, 0, TAILLE_T, HAUTEUR, 15, 5) ;
- for(i = 0 ; i < TAILLE_T ; i ++)
- copyXPMArea(116, 0, 1, t0[i]/2, 15 + i, HAUTEUR/2 + 5 - t0[i]/2) ;
- for(i = 0 ; i < TAILLE_T ; i ++)
- copyXPMArea(117, (HAUTEUR - t1[i])/2, 1, t1[i]/2, 15 + i, HAUTEUR/2 + 6) ;
+ for(i = 0, load = t_idx ; i < TAILLE_T ; i ++, load++) {
+ copyXPMArea(116, 0, 1, t0[load % TAILLE_T], 15 + i, HAUTEUR/2 + 5 - t0[load % TAILLE_T]) ;
+ copyXPMArea(117, HAUTEUR/2 - t1[load % TAILLE_T], 1, t1[load % TAILLE_T], 15 + i, HAUTEUR/2 + 6) ;
+ }
break ;
}
- c2 = 0 ;
draw_graph = FAUX ;
}
c1 ++ ;
@@ -307,10 +308,9 @@ int main(int argc, char **argv)
if((etat ++) >= 3)
etat = 1 ;
draw_graph = VRAI ;
- lecture = VRAI ;
}
}
- usleep(delay);
+ usleep(delay);
}
}