$NetBSD: patch-ab,v 1.1.1.1 2005/01/12 11:52:10 agc Exp $ --- wmSMPmon.c.orig 2003-03-10 13:50:11.000000000 +0100 +++ wmSMPmon.c 2004-08-25 20:52:07.000000000 +0200 @@ -9,5 +9,4 @@ *******************************************************************************/ -#include #include #include @@ -20,5 +19,8 @@ #include "wmSMPmon_master.xpm" #include "wmSMPmon_mask.xbm" -#include +//#include +#include +#include +#include #define VERSION "2.2" @@ -35,12 +37,73 @@ void usage(void) ; +int getCpuUsage() { + int mib[] = { CTL_KERN, KERN_CP_TIME }; + u_int64_t cpu_time[CPUSTATES]; + size_t size = sizeof(cpu_time); + int total, used, result; + static int pre_total, pre_used; + + if (sysctl(mib, 2, &cpu_time, &size, NULL, 0) < 0) + return 0; + used = cpu_time[CP_USER] + cpu_time[CP_SYS] + cpu_time[CP_NICE] + cpu_time[CP_INTR]; + total = used + cpu_time[CP_IDLE]; + if ((pre_total == 0) || !(total - pre_total > 0)) { + result = 0; + } else { + result = 100 * (double)(used - pre_used) / (double)(total - pre_total); + } + pre_used = used; + pre_total = total; + + return result; +}; + +int getMemUsage() { + static int mib[] = { CTL_VM, VM_METER }; + struct vmtotal vm; + size_t size = sizeof(vm); + int per_mem; + + per_mem = 0; + if (sysctl(mib, 2, &vm, &size, NULL, 0) < 0) + bzero(&vm, sizeof(vm)); + if (vm.t_rm > 0) + per_mem = 100 * (double) vm.t_rm / (double) (vm.t_rm + vm.t_free); + if (per_mem > 97) per_mem = 100; + + return per_mem; +}; + +int getSwapUsage() { + int num_swap; + struct swapent *swap_dev; + int i, sused, stotal; + + stotal = sused = 0; + + if ((num_swap = swapctl(SWAP_NSWAP, 0, 0)) == 0) + return 0; + if ((swap_dev = malloc(num_swap * sizeof(*swap_dev))) == NULL) + return 0; + if (swapctl(SWAP_STATS, swap_dev, num_swap) == -1) + return 0; + + for (i = 0; i < num_swap; i++) { + if (swap_dev[i].se_flags & SWF_ENABLE) { + sused += swap_dev[i].se_inuse; + stotal += swap_dev[i].se_nblks; + } + } + + free(swap_dev); + if (sused == 0) + return 0; + return (100 * (double) sused / (double) stotal); +}; + int main(int argc, char **argv) { XEvent Event ; - unsigned char *t = NULL, - *u = NULL, - tmp[192] ; - unsigned int t0[TAILLE_T], t1[TAILLE_T], @@ -57,7 +120,5 @@ draw_graph = FAUX ; - unsigned long mem, prec_mem = 0, prec_swap = 0, - total0o = 0, charge0o = 0, - total1o = 0, charge1o = 0, cpu0o=0, cpu1o=0 ; + unsigned long mem, prec_mem = 0, prec_swap = 0; register unsigned long charge, total, cpu, i = 0 ; @@ -107,97 +168,25 @@ if(lecture) { - i = open("/proc/stat", O_RDONLY); - read(i, tmp, 192); - close(i); - - strtok(tmp, "\n"); - t = strtok(NULL, "\n"); - u = strtok(NULL, "\n"); /* For CPU 1 */ - - strtok(t, " "); /* CPU 0 */ - charge = strtol(strtok(NULL, " "), NULL, 10) + - strtol(strtok(NULL, " "), NULL, 10) + - strtol(strtok(NULL, " "), NULL, 10); - total = charge+ strtol(strtok(NULL, " "), NULL, 10); - if(!charge || !total) - { - puts(ERREUR_SMP) ; - exit(ERREUR) ; - } - cpu = ((HAUTEUR * (charge - charge0o)) / (total - total0o + 0.001)) + 1 ; - total0o = total ; - charge0o = charge ; - cpu0t = cpu0t + cpu ; - if(cpu != cpu0o) - { - delta = HAUTEUR - cpu ; - copyXPMArea(111, 0, 3, HAUTEUR, 4, 5) ; - copyXPMArea(108, delta, 3, cpu, 4, 5 + delta) ; - cpu0o = cpu; - } - if(strncmp ("cpu1", u, 4) == 0) - { - strtok(u, " "); /* CPU 1*/ - charge = strtol(strtok(NULL, " "), NULL, 10) + - strtol(strtok(NULL, " "), NULL, 10) + - strtol(strtok(NULL, " "), NULL, 10); - total = charge+ strtol(strtok(NULL, " "), NULL, 10); - cpu = ((HAUTEUR * (charge - charge1o)) / (total - total1o + 0.001)) + 1 ; - total1o = total; - charge1o = charge; - } - cpu1t = cpu1t + cpu ; - if(cpu != cpu1o) - { - delta = HAUTEUR - cpu ; - copyXPMArea(111, 0, 3, HAUTEUR, 9, 5) ; - copyXPMArea(108, delta, 3, cpu, 9, 5 + delta) ; - cpu1o = cpu; - } - if(c1 > DIV1) - { - i = open("/proc/meminfo", O_RDONLY); - read(i, tmp, 192); - close(i); - strtok(tmp, "\n"); - t = strtok(NULL, "\n"); - u = strtok(NULL, "\n"); - strtok(t, " "); - total = atoi(strtok(NULL, " ")); - charge = atoi(strtok(NULL, " ")); - strtok(NULL, " "); - strtok(NULL, " "); - strtok(NULL, " "); - cpu = atoi(strtok(NULL, " ")); - - mem = ((charge - cpu) / (total / 100)) ; - if(mem != prec_mem) - { - copyXPMArea(30, 63, 30, 8, 29, 39) ; - copyXPMArea(0, 63, (mem * 30 / 100), 8, 29, 39) ; - prec_mem = mem ; - } - strtok(u, " "); - if(!no_swap) - { - total = charge = 0 ; - total = atoi(strtok(NULL, " ")); - strtok(NULL, " "); - charge = atoi(strtok(NULL, " ")); - if(!charge || !total) - { - puts(ERREUR_SWP) ; - exit(ERREUR) ; - } - mem = 100 - (charge / (total / 100)) ; - if(mem != prec_swap) - { - copyXPMArea(30, 63, 30, 8, 29, 50) ; - copyXPMArea(0, 63, (mem * 30 / 100), 8, 29, 50) ; - prec_swap = mem ; - } - } - c1 = 0; - } + cpu = getCpuUsage(); + delta = (100 - cpu) * HAUTEUR / 100; + copyXPMArea(108, 0, 3, HAUTEUR, 4, 5); + copyXPMArea(108, 0, 3, HAUTEUR, 9, 5); + copyXPMArea(111, 0, 3, delta, 4, 5); + copyXPMArea(111, 0, 3, delta, 9, 5); + + mem = getMemUsage(); + if(mem != prec_mem) { + copyXPMArea(30, 63, 30, 8, 29, 39); + copyXPMArea(0, 63, (mem * 30 / 100), 8, 29, 39); + prec_mem = mem; + } + if(!no_swap) { + mem = getSwapUsage(); + if(mem != prec_swap) { + copyXPMArea(30, 63, 30, 8, 29, 50); + copyXPMArea(0, 63, (mem * 30 / 100), 8, 29, 50); + prec_swap = mem; + } + } if(c2 > DIV2) draw_graph = VRAI ; @@ -210,10 +199,13 @@ tm[i - 1] = tm[i] ; } - if((t0[TAILLE_T - 1] = cpu0t / c2) > HAUTEUR) +/* if((t0[TAILLE_T - 1] = cpu0t / c2) > HAUTEUR) t0[TAILLE_T - 1] = HAUTEUR ; if((t1[TAILLE_T - 1] = cpu1t / c2) > HAUTEUR) t1[TAILLE_T - 1] = HAUTEUR ; if((tm[TAILLE_T - 1] = (cpu0t + cpu1t) / (2 * c2)) > HAUTEUR) - tm[TAILLE_T - 1] = HAUTEUR ; + tm[TAILLE_T - 1] = HAUTEUR ;*/ + t0[TAILLE_T - 1] = cpu * HAUTEUR / 100; + t1[TAILLE_T - 1] = cpu * HAUTEUR / 100; + tm[TAILLE_T - 1] = cpu * HAUTEUR / 100; cpu0t = 0 ; cpu1t = 0 ;