wmSMPmon: Code formating cleanup

This commit is contained in:
Milan Čermák 2013-01-19 10:17:30 +01:00 committed by Carlos R. Mafra
parent cb8524e80a
commit d914561f0c
5 changed files with 601 additions and 615 deletions

View file

@ -13,66 +13,67 @@
# #
######################################################################*/
# include "standards.h"
# include "general.h"
#include "standards.h"
#include "general.h"
/*
* The usual program end --
* called only by functions in this section. */
void bye_bye (int eno, const char *str)
void bye_bye(int eno, const char *str)
{
fflush(stdout);
if (str) {
if (eno) perror(str);
else {
fputs(str, stderr);
eno = 1;
}
}
exit(eno);
fflush(stdout);
if (str) {
if (eno)
perror(str);
else {
fputs(str, stderr);
eno = 1;
}
}
exit(eno);
}
/*
* This routine simply formats whatever the caller wants and
* returns a pointer to the resulting 'const char' string... */
const char *fmtmk (const char *fmts, ...)
/*
* This routine simply formats whatever the caller wants and
* returns a pointer to the resulting 'const char' string... */
const char *fmtmk(const char *fmts, ...)
{
static char buf[BIGBUFSIZ]; /* with help stuff, our buffer */
va_list va; /* requirements exceed 1k */
va_start(va, fmts);
vsnprintf(buf, sizeof(buf), fmts, va);
va_end(va);
return (const char *)buf;
static char buf[BIGBUFSIZ]; /* with help stuff, our buffer */
va_list va; /* requirements exceed 1k */
va_start(va, fmts);
vsnprintf(buf, sizeof(buf), fmts, va);
va_end(va);
return ((const char *)buf);
}
/*
* Standard error handler to normalize the look of all err o/p */
void std_err (const char *str)
/*
* Standard error handler to normalize the look of all err o/p */
void std_err(const char *str)
{
static char buf[SMLBUFSIZ];
fflush(stdout);
/* we'll use our own buffer so callers can still use fmtmk()
* and, yes the leading tab is not the standard convention,
* but the standard is wrong -- OUR msg won't get lost in
* screen clutter, like so many others! */
snprintf(buf, sizeof(buf), "\t%s: %s\n", Myname, str);
/* not to worry, he'll change our exit code to 1 due to 'buf' */
bye_bye(0, buf);
static char buf[SMLBUFSIZ];
fflush(stdout);
/* we'll use our own buffer so callers can still use fmtmk()
* and, yes the leading tab is not the standard convention,
* but the standard is wrong -- OUR msg won't get lost in
* screen clutter, like so many others! */
snprintf(buf, sizeof(buf), "\t%s: %s\n", Myname, str);
/* not to worry, he'll change our exit code to 1 due to 'buf' */
bye_bye(0, buf);
}
/*
* Handle our own memory stuff without the risk of leaving the
* user's terminal in an ugly state should things go sour. */
void *alloc_c (unsigned numb)
{
void * p;
if (!numb) ++numb;
if (!(p = calloc(1, numb)))
std_err("failed memory allocate");
return p;
/*
* Handle our own memory stuff without the risk of leaving the
* user's terminal in an ugly state should things go sour. */
void *alloc_c(unsigned numb)
{
void * p;
if (!numb)
++numb;
if (!(p = calloc(1, numb)))
std_err("failed memory allocate");
return (p);
}

View file

@ -31,8 +31,8 @@ static int meminfo_fd = -1;
static char buf[1024];
/* assume no IO-wait stats (default kernel 2.4.x),
overridden if linux 2.5.x or 2.6.x */
/* assume no IO-wait stats (default kernel 2.4.x),
overridden if linux 2.5.x or 2.6.x */
static const char *States_fmts = STATES_line2x4;
@ -40,21 +40,21 @@ static const char *States_fmts = STATES_line2x4;
* that successive calls to the functions are more efficient.
* It also reads the current contents of the file into the global buf.
*/
#define FILE_TO_BUF(filename, fd) do{ \
static int local_n; \
if (fd == -1 && (fd = open(filename, O_RDONLY)) == -1) { \
fprintf(stderr, BAD_OPEN_MESSAGE); \
fflush(NULL); \
_exit(102); \
} \
lseek(fd, 0L, SEEK_SET); \
if ((local_n = read(fd, buf, sizeof buf - 1)) < 0) { \
perror(filename); \
fflush(NULL); \
_exit(103); \
} \
buf[local_n] = '\0'; \
}while(0)
#define FILE_TO_BUF(filename, fd) do { \
static int local_n; \
if (fd == -1 && (fd = open(filename, O_RDONLY)) == -1) {\
fprintf(stderr, BAD_OPEN_MESSAGE); \
fflush(NULL); \
_exit(102); \
} \
lseek(fd, 0L, SEEK_SET); \
if ((local_n = read(fd, buf, sizeof buf - 1)) < 0) {\
perror(filename); \
fflush(NULL); \
_exit(103); \
} \
buf[local_n] = '\0'; \
} while(0)
#define LINUX_VERSION(x,y,z) (0x10000*(x) + 0x100*(y) + z)
@ -66,41 +66,40 @@ static const char *States_fmts = STATES_line2x4;
/***********************************************/
unsigned int NumCpus_DoInit(void)
{
long smp_num_cpus;
int linux_version_code;
static struct utsname uts;
int x = 0, y = 0, z = 0; /* cleared in case sscanf() < 3 */
if (uname(&uts) == -1) /* failure implies impending death */
exit(1);
if (sscanf(uts.release, "%d.%d.%d", &x, &y, &z) < 3)
fprintf(stderr, /* *very* unlikely to happen by accident */
"Non-standard uts for running kernel:\n"
"release %s=%d.%d.%d gives version code %d\n",
uts.release, x, y, z, LINUX_VERSION(x,y,z));
linux_version_code = LINUX_VERSION(x, y, z);
if (linux_version_code > LINUX_VERSION(2, 5, 41))
States_fmts = STATES_line2x5;
if (linux_version_code >= LINUX_VERSION(2, 6, 0)) // grrr... only
// some 2.6.0-testX :-(
States_fmts = STATES_line2x6;
smp_num_cpus = sysconf(_SC_NPROCESSORS_CONF); // or _SC_NPROCESSORS_ONLN
if (smp_num_cpus < 1)
{
smp_num_cpus = 1; /* SPARC glibc is buggy */
}
if (smp_num_cpus > 255)
{
/* we don't support more than 255 CPUs (well, in fact no more
than two ate the moment... */
smp_num_cpus = 255;
}
return (int)smp_num_cpus;
long smp_num_cpus;
int linux_version_code;
static struct utsname uts;
int x = 0, y = 0, z = 0; /* cleared in case sscanf() < 3 */
if (uname(&uts) == -1) /* failure implies impending death */
exit(1);
if (sscanf(uts.release, "%d.%d.%d", &x, &y, &z) < 3)
fprintf(stderr, /* *very* unlikely to happen by accident */
"Non-standard uts for running kernel:\n"
"release %s=%d.%d.%d gives version code %d\n",
uts.release, x, y, z, LINUX_VERSION(x,y,z));
linux_version_code = LINUX_VERSION(x, y, z);
if (linux_version_code > LINUX_VERSION(2, 5, 41))
States_fmts = STATES_line2x5;
if (linux_version_code >= LINUX_VERSION(2, 6, 0)) {
// grrr... only some 2.6.0-testX :-(
States_fmts = STATES_line2x6;
}
smp_num_cpus = sysconf(_SC_NPROCESSORS_CONF); // or _SC_NPROCESSORS_ONLN
if (smp_num_cpus < 1) {
smp_num_cpus = 1; /* SPARC glibc is buggy */
}
if (smp_num_cpus > 255) {
/* we don't support more than 255 CPUs (well, in fact no more
than two ate the moment... */
smp_num_cpus = 255;
}
return (int)smp_num_cpus;
}
/***********************************************************************/
@ -115,12 +114,14 @@ unsigned int NumCpus_DoInit(void)
* GNU Library General Public License for more details.
*/
typedef struct mem_table_struct {
const char *name; /* memory type name */
unsigned long *slot; /* slot in return struct */
const char *name; /* memory type name */
unsigned long *slot; /* slot in return struct */
} mem_table_struct;
static int compare_mem_table_structs(const void *a, const void *b){
return strcmp(((const mem_table_struct*)a)->name,((const mem_table_struct*)b)->name);
static int compare_mem_table_structs(const void *a, const void *b)
{
return strcmp(((const mem_table_struct *) a)->name,
((const mem_table_struct *) b)->name);
}
/* example data, following junk, with comments added:
@ -189,80 +190,83 @@ unsigned long kb_inactive;
unsigned long kb_mapped;
unsigned long kb_pagetables;
static void meminfo(void){
char namebuf[16]; /* big enough to hold any row name */
mem_table_struct findme = { namebuf, NULL};
mem_table_struct *found;
char *head;
char *tail;
static const mem_table_struct mem_table[] = {
{"Active", &kb_active}, // important
{"Buffers", &kb_main_buffers}, // important
{"Cached", &kb_main_cached}, // important
{"Committed_AS", &kb_committed_as},
{"Dirty", &kb_dirty}, // kB version of vmstat nr_dirty
{"HighFree", &kb_high_free},
{"HighTotal", &kb_high_total},
{"Inact_clean", &kb_inact_clean},
{"Inact_dirty", &kb_inact_dirty},
{"Inact_laundry",&kb_inact_laundry},
{"Inact_target", &kb_inact_target},
{"Inactive", &kb_inactive}, // important
{"LowFree", &kb_low_free},
{"LowTotal", &kb_low_total},
{"Mapped", &kb_mapped}, // kB version of vmstat nr_mapped
{"MemFree", &kb_main_free}, // important
{"MemShared", &kb_main_shared}, // important
{"MemTotal", &kb_main_total}, // important
{"PageTables", &kb_pagetables}, // kB version of vmstat
// nr_page_table_pages
{"ReverseMaps", &nr_reversemaps}, // same as vmstat
// nr_page_table_pages
{"Slab", &kb_slab}, // kB version of vmstat nr_slab
{"SwapCached", &kb_swap_cached},
{"SwapFree", &kb_swap_free}, // important
{"SwapTotal", &kb_swap_total}, // important
{"Writeback", &kb_writeback}, // kB version of vmstat
// nr_writeback
};
const int mem_table_count = sizeof(mem_table)/sizeof(mem_table_struct);
FILE_TO_BUF(MEMINFO_FILE,meminfo_fd);
kb_inactive = ~0UL;
head = buf;
for(;;){
tail = strchr(head, ':');
if(!tail) break;
*tail = '\0';
if(strlen(head) >= sizeof(namebuf)){
head = tail+1;
goto nextline;
}
strcpy(namebuf,head);
found = bsearch(&findme, mem_table, mem_table_count,
sizeof(mem_table_struct), compare_mem_table_structs
);
head = tail+1;
if(!found) goto nextline;
*(found->slot) = strtoul(head,&tail,10);
nextline:
tail = strchr(head, '\n');
if(!tail) break;
head = tail+1;
}
if(!kb_low_total){ /* low==main except with large-memory support */
kb_low_total = kb_main_total;
kb_low_free = kb_main_free;
}
if(kb_inactive==~0UL){
kb_inactive = kb_inact_dirty + kb_inact_clean + kb_inact_laundry;
}
kb_swap_used = kb_swap_total - kb_swap_free;
kb_main_used = kb_main_total - kb_main_free;
}
static void meminfo(void)
{
char namebuf[16]; /* big enough to hold any row name */
mem_table_struct findme = { namebuf, NULL};
mem_table_struct *found;
char *head;
char *tail;
static const mem_table_struct mem_table[] = {
{"Active", &kb_active}, // important
{"Buffers", &kb_main_buffers}, // important
{"Cached", &kb_main_cached}, // important
{"Committed_AS", &kb_committed_as},
{"Dirty", &kb_dirty}, // kB version of vmstat nr_dirty
{"HighFree", &kb_high_free},
{"HighTotal", &kb_high_total},
{"Inact_clean", &kb_inact_clean},
{"Inact_dirty", &kb_inact_dirty},
{"Inact_laundry",&kb_inact_laundry},
{"Inact_target", &kb_inact_target},
{"Inactive", &kb_inactive}, // important
{"LowFree", &kb_low_free},
{"LowTotal", &kb_low_total},
{"Mapped", &kb_mapped}, // kB version of vmstat nr_mapped
{"MemFree", &kb_main_free}, // important
{"MemShared", &kb_main_shared}, // important
{"MemTotal", &kb_main_total}, // important
{"PageTables", &kb_pagetables}, // kB version of vmstat
// nr_page_table_pages
{"ReverseMaps", &nr_reversemaps}, // same as vmstat
// nr_page_table_pages
{"Slab", &kb_slab}, // kB version of vmstat nr_slab
{"SwapCached", &kb_swap_cached},
{"SwapFree", &kb_swap_free}, // important
{"SwapTotal", &kb_swap_total}, // important
{"Writeback", &kb_writeback}, // kB version of vmstat
// nr_writeback
};
const int mem_table_count =
sizeof(mem_table) / sizeof(mem_table_struct);
FILE_TO_BUF(MEMINFO_FILE,meminfo_fd);
kb_inactive = ~0UL;
head = buf;
for(;;) {
tail = strchr(head, ':');
if (!tail)
break;
*tail = '\0';
if (strlen(head) >= sizeof(namebuf)) {
head = tail + 1;
goto nextline;
}
strcpy(namebuf, head);
found = bsearch(&findme, mem_table, mem_table_count,
sizeof(mem_table_struct), compare_mem_table_structs);
head = tail + 1;
if (!found)
goto nextline;
*(found->slot) = strtoul(head, &tail, 10);
nextline:
tail = strchr(head, '\n');
if (!tail)
break;
head = tail + 1;
}
if (!kb_low_total) { /* low==main except with large-memory support */
kb_low_total = kb_main_total;
kb_low_free = kb_main_free;
}
if (kb_inactive == ~0UL) {
kb_inactive = kb_inact_dirty + kb_inact_clean + kb_inact_laundry;
}
kb_swap_used = kb_swap_total - kb_swap_free;
kb_main_used = kb_main_total - kb_main_free;
}
/*************************************************************************/
/*
@ -273,93 +277,98 @@ static void meminfo(void){
* cpus[Cpu_tot] == tics from the 1st /proc/stat line */
static CPU_t *cpus_refresh (CPU_t *cpus, unsigned int Cpu_tot)
{
static FILE *fp = NULL;
int i;
// enough for a /proc/stat CPU line (not the intr line)
char buf[SMLBUFSIZ];
/* by opening this file once, we'll avoid the hit on minor page faults
(sorry Linux, but you'll have to close it for us) */
if (!fp) {
if (!(fp = fopen("/proc/stat", "r")))
std_err(fmtmk("Failed /proc/stat open: %s", strerror(errno)));
/* note: we allocate one more CPU_t than Cpu_tot so that the
last slot can hold tics representing the /proc/stat cpu
summary (the first line read) -- that slot supports our
View_CPUSUM toggle */
cpus = alloc_c((1 + Cpu_tot) * sizeof(CPU_t));
}
rewind(fp);
fflush(fp);
// first value the last slot with the cpu summary line
if (!fgets(buf, sizeof(buf), fp)) std_err("failed /proc/stat read");
cpus[Cpu_tot].x = 0; // FIXME: can't tell by kernel version number
cpus[Cpu_tot].y = 0; // FIXME: can't tell by kernel version number
if (4 > sscanf(buf, CPU_FMTS_JUST1, &cpus[Cpu_tot].u, &cpus[Cpu_tot].n, &cpus[Cpu_tot].s, &cpus[Cpu_tot].i, &cpus[Cpu_tot].w, &cpus[Cpu_tot].x, &cpus[Cpu_tot].y))
std_err("failed /proc/stat read");
// and just in case we're 2.2.xx compiled without SMP support...
if (1 == Cpu_tot)
{
/* do it "manually", otherwise we overwrite charge and total */
cpus[0].u = cpus[1].u;
cpus[0].n = cpus[1].n;
cpus[0].s = cpus[1].s;
cpus[0].i = cpus[1].i;
cpus[0].w = cpus[1].w;
cpus[0].x = cpus[1].x;
cpus[0].y = cpus[1].y;
}
// now value each separate cpu's tics
for (i = 0; 1 < Cpu_tot && i < Cpu_tot; i++) {
if (!fgets(buf, sizeof(buf), fp)) std_err("failed /proc/stat read");
cpus[i].x = 0; // FIXME: can't tell by kernel version number
cpus[i].y = 0; // FIXME: can't tell by kernel version number
if (4 > sscanf(buf, CPU_FMTS_MULTI, &cpus[i].u, &cpus[i].n, &cpus[i].s, &cpus[i].i, &cpus[i].w, &cpus[i].x, &cpus[i].y))
std_err("failed /proc/stat read");
}
return cpus;
static FILE *fp = NULL;
int i;
// enough for a /proc/stat CPU line (not the intr line)
char buf[SMLBUFSIZ];
/* by opening this file once, we'll avoid the hit on minor page faults
(sorry Linux, but you'll have to close it for us) */
if (!fp) {
if (!(fp = fopen("/proc/stat", "r")))
std_err(fmtmk("Failed /proc/stat open: %s",
strerror(errno)));
/* note: we allocate one more CPU_t than Cpu_tot so that the
last slot can hold tics representing the /proc/stat cpu
summary (the first line read) -- that slot supports our
View_CPUSUM toggle */
cpus = alloc_c((1 + Cpu_tot) * sizeof(CPU_t));
}
rewind(fp);
fflush(fp);
// first value the last slot with the cpu summary line
if (!fgets(buf, sizeof(buf), fp))
std_err("failed /proc/stat read");
cpus[Cpu_tot].x = 0; // FIXME: can't tell by kernel version number
cpus[Cpu_tot].y = 0; // FIXME: can't tell by kernel version number
if (4 > sscanf(buf, CPU_FMTS_JUST1, &cpus[Cpu_tot].u, &cpus[Cpu_tot].n,
&cpus[Cpu_tot].s, &cpus[Cpu_tot].i, &cpus[Cpu_tot].w,
&cpus[Cpu_tot].x, &cpus[Cpu_tot].y))
std_err("failed /proc/stat read");
// and just in case we're 2.2.xx compiled without SMP support...
if (1 == Cpu_tot) {
/* do it "manually", otherwise we overwrite charge and total */
cpus[0].u = cpus[1].u;
cpus[0].n = cpus[1].n;
cpus[0].s = cpus[1].s;
cpus[0].i = cpus[1].i;
cpus[0].w = cpus[1].w;
cpus[0].x = cpus[1].x;
cpus[0].y = cpus[1].y;
}
// now value each separate cpu's tics
for (i = 0; 1 < Cpu_tot && i < Cpu_tot; i++) {
if (!fgets(buf, sizeof(buf), fp))
std_err("failed /proc/stat read");
cpus[i].x = 0; // FIXME: can't tell by kernel version number
cpus[i].y = 0; // FIXME: can't tell by kernel version number
if (4 > sscanf(buf, CPU_FMTS_MULTI, &cpus[i].u,
&cpus[i].n, &cpus[i].s, &cpus[i].i, &cpus[i].w,
&cpus[i].x, &cpus[i].y))
std_err("failed /proc/stat read");
}
return cpus;
}
unsigned int *Get_CPU_Load(unsigned int *load, unsigned int Cpu_tot)
{
static CPU_t *smpcpu = NULL;
unsigned int j;
register unsigned long charge, total = 0 ;
smpcpu = cpus_refresh(smpcpu, Cpu_tot);
for(j = 0 ; j < Cpu_tot ; j ++)
{
charge = smpcpu[j].u+smpcpu[j].s+smpcpu[j].n;
total = charge + smpcpu[j].i;
/* scale cpu to a maximum of HAUTEUR */
load[j] = ((HAUTEUR * (charge - smpcpu[j].charge)) / (total - smpcpu[j].total + 0.001)) + 1 ;
smpcpu[j].total = total ;
smpcpu[j].charge = charge ;
}
return load;
static CPU_t *smpcpu = NULL;
unsigned int j;
register unsigned long charge, total = 0;
smpcpu = cpus_refresh(smpcpu, Cpu_tot);
for (j = 0; j < Cpu_tot; j ++) {
charge = smpcpu[j].u + smpcpu[j].s + smpcpu[j].n;
total = charge + smpcpu[j].i;
/* scale cpu to a maximum of HAUTEUR */
load[j] = ((HAUTEUR * (charge - smpcpu[j].charge)) /
(total - smpcpu[j].total + 0.001)) + 1 ;
smpcpu[j].total = total ;
smpcpu[j].charge = charge ;
}
return load;
}
unsigned int Get_Memory(void)
{
meminfo();
return (kb_main_used - kb_main_cached) / (kb_main_total / 100) ;
/* should be between 0 and 100 now */
meminfo();
return ((kb_main_used - kb_main_cached) / (kb_main_total / 100));
/* should be between 0 and 100 now */
}
unsigned int Get_Swap(void)
{
/* returns swap usage as value between 0 and 100 OR 999 if no swap
* present */
meminfo();
/* returns swap usage as value between 0 and 100
* OR 999 if no swap present */
meminfo();
return ( kb_swap_total == 0 ? 999 : kb_swap_used / (kb_swap_total / 100) );
return (kb_swap_total == 0 ? 999 : kb_swap_used / (kb_swap_total / 100));
}

View file

@ -5,29 +5,30 @@
#include <sys/utsname.h>
/* These are the possible fscanf formats used in /proc/stat
reads during history processing.
( 5th number only for Linux 2.5.41 and above ) */
/* These are the possible fscanf formats used in /proc/stat
reads during history processing.
( 5th number only for Linux 2.5.41 and above ) */
#define CPU_FMTS_JUST1 "cpu %Lu %Lu %Lu %Lu %Lu %Lu %Lu"
#define CPU_FMTS_MULTI "cpu%*d %Lu %Lu %Lu %Lu %Lu %Lu %Lu"
/* Summary Lines specially formatted string(s) -- see 'show_special' for syntax details + other cautions. */
/* Summary Lines specially formatted string(s) --
see 'show_special' for syntax details + other cautions. */
#define STATES_line2x4 "%s\03" \
" %#5.1f%% \02user,\03 %#5.1f%% \02system,\03 %#5.1f%% \02nice,\03 %#5.1f%% \02idle\03\n"
#define STATES_line2x5 "%s\03" \
" %#5.1f%% \02user,\03 %#5.1f%% \02system,\03 %#5.1f%% \02nice,\03 %#5.1f%% \02idle,\03 %#5.1f%% \02IO-wait\03\n"
#define STATES_line2x6 "%s\03" \
" %#4.1f%% \02us,\03 %#4.1f%% \02sy,\03 %#4.1f%% \02ni,\03 %#4.1f%% \02id,\03 %#4.1f%% \02wa,\03 %#4.1f%% \02hi,\03 %#4.1f%% \02si\03\n"
" %#4.1f%% \02us,\03 %#4.1f%% \02sy,\03 %#4.1f%% \02ni,\03 %#4.1f%% \02id,\03 %#4.1f%% \02wa,\03 %#4.1f%% \02hi,\03 %#4.1f%% \02si\03\n"
/* These typedefs attempt to ensure consistent 'ticks' handling */
/* These typedefs attempt to ensure consistent 'ticks' handling */
typedef unsigned long long TIC_t;
/* This structure stores a frame's cpu tics used in history
calculations. It exists primarily for SMP support but serves
all environments. */
/* This structure stores a frame's cpu tics used in history
calculations. It exists primarily for SMP support but serves
all environments. */
typedef struct CPU_t {
TIC_t u, n, s, i, w, x, y; // as represented in /proc/stat
TIC_t charge, total;
TIC_t u, n, s, i, w, x, y; // as represented in /proc/stat
TIC_t charge, total;
} CPU_t;
#endif /* SYSINFO_H */
#endif /* LINUX_SYSINFO_H */

View file

@ -1,7 +1,7 @@
/***************************************************************************
wmSMPmon III - Window Maker system monitor
VERSION : 3.1
DATE : 2005-11-06
VERSION : 3.2
DATE : 2005-11-06
ORIGINAL AUTHORS : redseb <redseb@goupilfr.org> and
PhiR <phir@gcu-squad.org>
CONTRIBUTORS : Alain Schröder <alain@parkautomat.net>
@ -12,16 +12,15 @@ CURRENT MAINTAINER: Thomas Ribbrock <emgaron@gmx.net>
See file COPYING for information on distribution conditions.
***************************************************************************/
#include <string.h>
#include <X11/Xlib.h>
#include <X11/xpm.h>
#include <X11/extensions/shape.h>
#include "../wmgeneral/wmgeneral.h"
#include "wmSMPmon_master.xpm"
#include "wmSMPmon_mask.xbm"
#include "general.h"
#include <string.h>
#include <X11/Xlib.h>
#include <X11/xpm.h>
#include <X11/extensions/shape.h>
#include "../wmgeneral/wmgeneral.h"
#include "wmSMPmon_master.xpm"
#include "wmSMPmon_mask.xbm"
#include "general.h"
#include "standards.h"
#include "sysinfo-linux.h"
#define VERSION "3.2"
@ -39,315 +38,290 @@ void usage(int cpus, const char *str);
/*###### MAIN PROGRAM ###################################################*/
int main(int argc, char **argv)
{
XEvent Event ;
XEvent Event;
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 = DIV1,
c2 = DIV2,
etat = 1,
lecture = 1,
delay = 250000,
delta = 0,
load = 0,
load0o = 0,
load1o = 0,
no_swap = 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 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 = DIV1,
c2 = DIV2,
etat = 1,
lecture = 1,
delay = 250000,
delta = 0,
load = 0,
load0o = 0,
load1o = 0,
no_swap = 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 long load0t = 0, load1t = 0;
unsigned int *CPU_Load; /* CPU load per CPU array */
unsigned int t_idx = 0; /* Index to load history tables */
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));
/********** Initialisation **********/
NumCPUs = NumCpus_DoInit();
CPU_Load = alloc_c((NumCPUs) * sizeof(int));
if(NumCPUs == 1)
{
load_width = 8;
}
else
{
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)
{
if(!strncmp(argv[i], "-r", 2))
{
i ++ ;
if(i == argc)
{
/* parameter missing! */
usage(NumCPUs, "no refresh rate given when using -r!");
}
else
{
delay = atol(argv[i]) ;
}
i ++ ;
continue;
}
if(!strncmp(argv[i], "-h", 2))
{
usage(NumCPUs, NULL) ;
}
if(!strncmp(argv[i], "-g", 2) && NumCPUs > 1)
{
/* we only support this on SMP systems */
i ++ ;
if(i == argc)
{
/* parameter missing! */
usage(NumCPUs, "no graph style given when using -g!");
}
else
{
etat = atoi(argv[i]) ;
}
if(1 > etat || etat > 3)
usage(NumCPUs, "Unknown graph style") ;
i ++ ;
continue;
}
if(!strncmp(argv[i], "-no-swap", 8))
{
puts(MSG_NO_SWAP) ;
no_swap = VRAI ;
i ++ ;
continue;
}
/* if we get here, we found an illegal option */
usage(NumCPUs, "Illegal option!");
}
/* open initial window */
if(NumCPUs == 1)
{
/* we only have a single CPU - change the mask accordingly
NOTE: The for loop was derived from the differences between
wmSMPmon_mask.xbm and wmSMPmon_mask-single.xbm.
wmSMPmon_mask-single.xbm as such is NOT used in this
program! */
for (i = 33; i <= 289; i = i+8)
{
wmSMPmon_mask_bits[i] = 0xDF;
}
}
openXwindow(argc,argv,wmSMPmon_master_xpm,wmSMPmon_mask_bits,wmSMPmon_mask_width,wmSMPmon_mask_height) ;
if(NumCPUs >= 2)
{
/* 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 ++) {
t0[i] = 0 ;
t1[i] = 0 ;
tm[i] = 0 ;
}
/* -no-swap option was given */
if(no_swap)
copyXPMArea(60, 63, 60, 10, 6, 50) ;
/* MAIN LOOP */
while(VRAI)
{
if(lecture)
{
CPU_Load = Get_CPU_Load(CPU_Load, NumCPUs);
load = CPU_Load[0];
for (i = 1; i < NumCPUs >> 1; i++) {
load += CPU_Load[i];
if(NumCPUs == 1) {
load_width = 8;
} else {
load_width = 3;
}
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) ;
load0o = load;
}
if(NumCPUs >= 2)
{
/* we have two CPUs -> do CPU 1 */
load = 0;
for (; i < NumCPUs; i++) {
load += CPU_Load[i];
}
load = load / (NumCPUs >> 1);
Myname = strrchr(argv[0], '/');
if (Myname)
++Myname;
else
Myname = argv[0];
if(load != load1o)
{
/* redraw only if cpu load changed */
delta = HAUTEUR - load ;
copyXPMArea(108, 0, 3, HAUTEUR, 9, 5) ;
copyXPMArea(108, delta + 32, 3, load, 9, 5 + delta) ;
load1o = load;
}
}
/* process command line args */
i = 1; /* skip program name (i=0) */
while (argc > i) {
if (!strncmp(argv[i], "-r", 2)) {
i++;
if (i == argc) {
/* parameter missing! */
usage(NumCPUs,
"no refresh rate given when using -r!");
} else {
delay = atol(argv[i]) ;
}
i++;
continue;
}
if (!strncmp(argv[i], "-h", 2)) {
usage(NumCPUs, NULL);
}
if (!strncmp(argv[i], "-g", 2) && NumCPUs > 1) {
/* we only support this on SMP systems */
i++;
if (i == argc) {
/* parameter missing! */
usage(NumCPUs,
"no graph style given when using -g!");
} else {
etat = atoi(argv[i]);
}
/* we have to set load1t in any case to get the correct
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 (1 > etat || etat > 3)
usage(NumCPUs, "Unknown graph style");
i++;
continue;
}
if (!strncmp(argv[i], "-no-swap", 8)) {
puts(MSG_NO_SWAP);
no_swap = VRAI;
i++;
continue;
}
if(c1 > DIV1)
{
mem = Get_Memory();
/* if we get here, we found an illegal option */
usage(NumCPUs, "Illegal option!");
}
if(mem != prec_mem)
{
/* redraw only if mem changed */
copyXPMArea(30, 63, 30, 8, 29, 39) ;
copyXPMArea(0, 63, (mem * 30 / 100), 8, 29, 39) ;
prec_mem = mem ;
}
/* open initial window */
if (NumCPUs == 1) {
/* we only have a single CPU - change the mask accordingly
* NOTE: The for loop was derived from the differences between
* wmSMPmon_mask.xbm and wmSMPmon_mask-single.xbm.
* wmSMPmon_mask-single.xbm as such is NOT used in this
* program!
*/
for (i = 33; i <= 289; i = i+8) {
wmSMPmon_mask_bits[i] = 0xDF;
}
}
if(!no_swap)
{
mem = Get_Swap();
openXwindow(argc, argv, wmSMPmon_master_xpm, wmSMPmon_mask_bits,
wmSMPmon_mask_width, wmSMPmon_mask_height);
if(mem != prec_swap)
{
/* redraw if there was a change */
if(mem == 999)
{
/* swap is disabled => show "none" */
copyXPMArea(60, 63, 60, 10, 6, 50);
}
else
{
/* draw swap usage */
copyXPMArea(30, 63, 30, 8, 29, 50) ;
copyXPMArea(0, 63, (mem * 30 / 100), 8, 29, 50) ;
}
prec_swap = mem ;
}
}
c1 = 0;
}
if(NumCPUs >= 2) {
/* we have two CPUs -> draw separator between CPU load bars */
copyXPMArea(12, 4, 2, HAUTEUR + 2, 7, 4);
}
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)
{
/* draw graph */
switch(etat)
{
case 1 :
copyXPMArea(64, 32, TAILLE_T, HAUTEUR, 15, 5) ;
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, 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, 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 ;
}
draw_graph = FAUX ;
}
c1 ++ ;
c2 ++ ;
}
lecture = 1 - lecture ;
RedrawWindow() ;
if(NumCPUs >= 2 && XCheckMaskEvent(display, ButtonPressMask, &Event))
{
/* changing graph style not supported on single CPU systems */
if(Event.type == ButtonPress)
{
if((etat ++) >= 3)
etat = 1 ;
draw_graph = VRAI ;
}
}
usleep(delay);
}
delay = delay / 2 ;
for (i = 0; i < TAILLE_T; i ++) {
t0[i] = 0;
t1[i] = 0;
tm[i] = 0;
}
/* -no-swap option was given */
if (no_swap)
copyXPMArea(60, 63, 60, 10, 6, 50);
/* MAIN LOOP */
while (VRAI) {
if (lecture) {
CPU_Load = Get_CPU_Load(CPU_Load, NumCPUs);
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);
load0o = load;
}
if (NumCPUs >= 2) {
/* we have two CPUs -> do CPU 1 */
load = 0;
for (; i < NumCPUs; i++) {
load += CPU_Load[i];
}
load = load / (NumCPUs >> 1);
if (load != load1o) {
/* redraw only if cpu load changed */
delta = HAUTEUR - load;
copyXPMArea(108, 0, 3, HAUTEUR, 9, 5);
copyXPMArea(108, delta + 32, 3, load,
9, 5 + delta);
load1o = load;
}
}
/* we have to set load1t in any case to get the correct
* 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) {
mem = Get_Memory();
if (mem != prec_mem) {
/* redraw only if mem changed */
copyXPMArea(30, 63, 30, 8, 29, 39);
copyXPMArea(0, 63, (mem * 30 / 100), 8,
29, 39);
prec_mem = mem;
}
if (!no_swap) {
mem = Get_Swap();
if (mem != prec_swap) {
/* redraw if there was a change */
if (mem == 999) {
/* swap is disabled => show "none" */
copyXPMArea(60, 63, 60, 10, 6, 50);
} else {
/* draw swap usage */
copyXPMArea(30, 63, 30, 8, 29, 50);
copyXPMArea(0, 63, (mem * 30 / 100), 8, 29, 50);
}
prec_swap = mem;
}
}
c1 = 0;
}
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) {
/* draw graph */
switch (etat) {
case 1 :
copyXPMArea(64, 32, TAILLE_T, HAUTEUR, 15, 5);
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, 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, 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;
}
draw_graph = FAUX;
}
c1++;
c2++;
}
lecture = 1 - lecture ;
RedrawWindow();
if (NumCPUs >= 2 &&
XCheckMaskEvent(display, ButtonPressMask, &Event)) {
/* changing graph style not supported on single CPU systems */
if (Event.type == ButtonPress) {
if ((etat++) >= 3)
etat = 1;
draw_graph = VRAI;
}
}
usleep(delay);
}
}
/*###### Usage Message ##################################################*/
void usage(int cpus, const char *str)
{
fflush(stdout);
fflush(stdout);
if (str)
{
fprintf(stderr, "\nERROR: %s\n", str);
}
fputs("\nwmSMPmon "VERSION" - display system load (", stderr);
if(cpus == 1)
{
fputs("uniprocessor system)\n\n", stderr);
}
else
{
fputs("(multiprocessor system)\n\n", stderr);
}
fputs("Options : -h this help screen.\n"
" -r RATE refresh rate (in microseconds, default 250000).\n", stderr);
if (str) {
fprintf(stderr, "\nERROR: %s\n", str);
}
if(cpus > 1)
{
fputs(" -g STYLE graph style (try 2 or 3, default is 1).\n", stderr);
}
fputs(" -no-swap don't monitore swap size.\n\n"
"<redseb@goupilfr.org> http://goupilfr.org\n"
"<phir@gcu-squad.org> http://gcu-squad.org\n"
"<emgaron@gmx.net> http://www.ribbrock.org\n",
stderr) ;
exit(OK) ;
}
fputs("\nwmSMPmon "VERSION" - display system load (", stderr);
if(cpus == 1) {
fputs("uniprocessor system)\n\n", stderr);
} else {
fputs("multiprocessor system)\n\n", stderr);
}
fputs("Options : -h this help screen.\n"
" -r RATE refresh rate (in microseconds, default 250000).\n",
stderr);
if(cpus > 1) {
fputs(" -g STYLE graph style (try 2 or 3, default is 1).\n",
stderr);
}
fputs(" -no-swap don't monitore swap size.\n\n"
"<redseb@goupilfr.org> http://goupilfr.org\n"
"<phir@gcu-squad.org> http://gcu-squad.org\n"
"<emgaron@gmx.net> http://www.ribbrock.org\n",
stderr);
exit(OK);
}

View file

@ -292,34 +292,31 @@ void createXBMfromXPM(char *xbm, char **xpm, int sx, int sy) {
int i,j,k;
int width, height, numcol, depth;
int zero=0;
int zero = 0;
unsigned char bwrite;
int bcount;
int curpixel;
int bcount;
int curpixel;
sscanf(*xpm, "%d %d %d %d", &width, &height, &numcol, &depth);
for (k = 0; k != depth; k++) {
zero <<=8;
zero |= xpm[1][k];
}
for (k=0; k!=depth; k++)
{
zero <<=8;
zero |= xpm[1][k];
}
for (i=numcol+1; i < numcol+sy+1; i++) {
for (i = numcol + 1; i < numcol + sy + 1; i++) {
bcount = 0;
bwrite = 0;
for (j=0; j<sx*depth; j+=depth) {
bwrite >>= 1;
for (j = 0; j < sx * depth; j += depth) {
bwrite >>= 1;
curpixel=0;
for (k=0; k!=depth; k++)
{
curpixel <<=8;
curpixel |= xpm[i][j+k];
}
if ( curpixel != zero ) {
curpixel=0;
for (k = 0; k != depth; k++) {
curpixel <<=8;
curpixel |= xpm[i][j+k];
}
if (curpixel != zero) {
bwrite += 128;
}
bcount++;
@ -343,10 +340,10 @@ void createXBMfromXPM(char *xbm, char **xpm, int sx, int sy) {
|* dx,dy: first corner of target area *|
\***************************************************************************/
void copyXPMArea(int x, int y, int sx, int sy, int dx, int dy) {
XCopyArea(display, wmgen.pixmap, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
void copyXPMArea(int x, int y, int sx, int sy, int dx, int dy)
{
XCopyArea(display, wmgen.pixmap, wmgen.pixmap, NormalGC,
x, y, sx, sy, dx, dy);
}
/***************************************************************************\
@ -359,9 +356,10 @@ void copyXPMArea(int x, int y, int sx, int sy, int dx, int dy) {
|* dx,dy: first corner of target area *|
\***************************************************************************/
void copyXBMArea(int x, int y, int sx, int sy, int dx, int dy) {
XCopyArea(display, wmgen.mask, wmgen.pixmap, NormalGC, x, y, sx, sy, dx, dy);
void copyXBMArea(int x, int y, int sx, int sy, int dx, int dy)
{
XCopyArea(display, wmgen.mask, wmgen.pixmap, NormalGC,
x, y, sx, sy, dx, dy);
}
@ -369,10 +367,10 @@ void copyXBMArea(int x, int y, int sx, int sy, int dx, int dy) {
|* setMaskXY *|
\***************************************************************************/
void setMaskXY(int x, int y) {
XShapeCombineMask(display, win, ShapeBounding, x, y, pixmask, ShapeSet);
XShapeCombineMask(display, iconwin, ShapeBounding, x, y, pixmask, ShapeSet);
void setMaskXY(int x, int y)
{
XShapeCombineMask(display, win, ShapeBounding, x, y, pixmask, ShapeSet);
XShapeCombineMask(display, iconwin, ShapeBounding, x, y, pixmask, ShapeSet);
}
/***************************************************************************\
@ -381,20 +379,20 @@ void setMaskXY(int x, int y) {
void openXwindow(int argc, char *argv[], char *pixmap_bytes[], char *pixmask_bits, int pixmask_width, int pixmask_height) {
unsigned int borderwidth = 1;
XClassHint classHint;
char *display_name = NULL;
char *wname = argv[0];
XClassHint classHint;
char *display_name = NULL;
char *wname = argv[0];
XTextProperty name;
XGCValues gcv;
XGCValues gcv;
unsigned long gcm;
char *geometry = NULL;
char *geometry = NULL;
int dummy=0;
int i, wx, wy;
int dummy=0;
int i, wx, wy;
for (i=1; argv[i]; i++) {
for (i = 1; argv[i]; i++) {
if (!strcmp(argv[i], "-display")) {
display_name = argv[i+1];
i++;
@ -406,8 +404,8 @@ void openXwindow(int argc, char *argv[], char *pixmap_bytes[], char *pixmask_bit
}
if (!(display = XOpenDisplay(display_name))) {
fprintf(stderr, "%s: can't open display %s\n",
wname, XDisplayName(display_name));
fprintf(stderr, "%s: can't open display %s\n",
wname, XDisplayName(display_name));
exit(1);
}
screen = DefaultScreen(display);
@ -427,16 +425,19 @@ void openXwindow(int argc, char *argv[], char *pixmap_bytes[], char *pixmask_bit
fore_pix = GetColor("black");
XWMGeometry(display, screen, Geometry, NULL, borderwidth, &mysizehints,
&mysizehints.x, &mysizehints.y,&mysizehints.width,&mysizehints.height, &dummy);
&mysizehints.x, &mysizehints.y, &mysizehints.width,
&mysizehints.height, &dummy);
mysizehints.width = 64;
mysizehints.height = 64;
win = XCreateSimpleWindow(display, Root, mysizehints.x, mysizehints.y,
mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
iconwin = XCreateSimpleWindow(display, win, mysizehints.x, mysizehints.y,
mysizehints.width, mysizehints.height, borderwidth, fore_pix, back_pix);
mysizehints.width, mysizehints.height, borderwidth,
fore_pix, back_pix);
iconwin = XCreateSimpleWindow(display, win,
mysizehints.x, mysizehints.y, mysizehints.width, mysizehints.height,
borderwidth, fore_pix, back_pix);
/* Activate hints */
XSetWMNormalHints(display, win, &mysizehints);
@ -455,7 +456,7 @@ void openXwindow(int argc, char *argv[], char *pixmap_bytes[], char *pixmask_bit
XSetWMName(display, win, &name);
/* Create GC for drawing */
gcm = GCForeground | GCBackground | GCGraphicsExposures;
gcv.foreground = fore_pix;
gcv.background = back_pix;