Hi all,
I's like to submit two patches for wmSMPmon.
I just found out that I forgot to include the Solaris support
implementation in the last patch. (I use Mercurial every day but I'm
getting used to GIT.) The file sysinfo-solaris.c is the sole content of
the first patch.
The second patch is a code formatting clean-up.
[PATCH 1/2] wmSMPmon: Solaris support
[PATCH 2/2] wmSMPmon: Code formating cleanup
Regards,
Milan Čermák
From cfd79a598845fa637f072b28d1c37a68d0ccb2d1 Mon Sep 17 00:00:00 2001
From: Milan Äermák <[email protected]>
Date: Sat, 19 Jan 2013 10:15:02 +0100
Subject: [PATCH 1/2] wmSMPmon: Solaris support
The support implementation itself was mising in the previous commit.
---
wmSMPmon/wmSMPmon/sysinfo-solaris.c | 265 +++++++++++++++++++++++++++++++++++
1 files changed, 265 insertions(+), 0 deletions(-)
create mode 100644 wmSMPmon/wmSMPmon/sysinfo-solaris.c
diff --git a/wmSMPmon/wmSMPmon/sysinfo-solaris.c b/wmSMPmon/wmSMPmon/sysinfo-solaris.c
new file mode 100644
index 0000000..a5abefe
--- /dev/null
+++ b/wmSMPmon/wmSMPmon/sysinfo-solaris.c
@@ -0,0 +1,265 @@
+/*
+ * sysinfo-solaris.c
+ *
+ * System information gathering for Solaris
+ */
+
+#include "general.h"
+#include "standards.h"
+#include <stdio.h>
+#include <stdlib.h>
+#include <strings.h>
+#include <stdint.h>
+#include <kstat.h>
+#include <sys/sysinfo.h>
+#include <sys/swap.h>
+
+/*
+ * The index field is for a fast lookup on the string.
+ * A -1 tells stat_data_lookup() that we need to locate
+ * the string. Once stat_data_lookup() has located the
+ * string, it will set the index of were we found it.
+ */
+typedef struct cpu_states_info {
+ char *field_name;
+ int index;
+} cpu_states_info_t;
+
+#define CPU_STATES 4
+static struct cpu_states_info cpu_states[CPU_STATES] = {
+ {"cpu_ticks_idle", -1},
+ {"cpu_ticks_user", -1},
+ {"cpu_ticks_kernel", -1},
+ {"cpu_ticks_wait", -1}
+};
+
+static kstat_ctl_t *kcp = NULL;
+static kstat_t *ksp_old;
+
+static uint64_t physmem = 0;
+
+/* NumCPUs_DoInit returns the number of CPUs present in the system and
+ performs any initialization necessary for the sysinfo-XXX module */
+unsigned int NumCpus_DoInit(void)
+{
+ int smp_num_cpus;
+ int i;
+
+ kcp = kstat_open();
+ if (kcp == NULL)
+ exit(1);
+
+ physmem = sysconf(_SC_PHYS_PAGES);
+ smp_num_cpus = sysconf(_SC_NPROCESSORS_CONF);
+ 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;
+ }
+
+ ksp_old = malloc(smp_num_cpus * sizeof (kstat_t));
+ if (ksp_old == NULL) {
+ kstat_close(kcp);
+ fprintf(stderr, "ERROR: Can't allocate cpu load history.\n");
+ exit(1);
+ }
+
+ for (i = 0; i < smp_num_cpus; i++) {
+ ksp_old[i].ks_data = NULL;
+ ksp_old[i].ks_data_size = 0;
+ }
+
+ return smp_num_cpus;
+}
+
+/*
+ * If index_ptr integer value is > -1 then the index points to the
+ * string entry in the ks_data that we are interested in. Otherwise
+ * we will need to walk the array.
+ */
+void *stat_data_lookup(kstat_t *ksp, char *name, int *index_ptr)
+{
+ int i;
+ int size;
+ int index;
+ char *namep, *datap;
+
+ switch (ksp->ks_type) {
+ case KSTAT_TYPE_NAMED:
+ size = sizeof (kstat_named_t);
+ namep = KSTAT_NAMED_PTR(ksp)->name;
+ break;
+ case KSTAT_TYPE_TIMER:
+ size = sizeof (kstat_timer_t);
+ namep = KSTAT_TIMER_PTR(ksp)->name;
+ break;
+ default:
+ errno = EINVAL;
+ return (NULL);
+ }
+
+ index = *index_ptr;
+ if (index >= 0) {
+ /* Short cut to the information. */
+ datap = ksp->ks_data;
+ datap = &datap[size*index];
+ return (datap);
+ }
+
+ /* Need to go find the string. */
+ datap = ksp->ks_data;
+ for (i = 0; i < ksp->ks_ndata; i++) {
+ if (strcmp(name, namep) == 0) {
+ *index_ptr = i;
+ return (datap);
+ }
+ namep += size;
+ datap += size;
+ }
+ errno = ENOENT;
+ return (NULL);
+}
+
+uint64_t kstat_delta(kstat_t *old, kstat_t *new, char *name, int *index)
+{
+ kstat_named_t *knew = stat_data_lookup(new, name, index);
+
+ if (old && old->ks_data) {
+ kstat_named_t *kold = stat_data_lookup(old, name, index);
+ return (knew->value.ui64 - kold->value.ui64);
+ }
+ return (knew->value.ui64);
+}
+
+uint64_t cpu_ticks_delta(kstat_t *old, kstat_t *new)
+{
+ uint64_t ticks = 0;
+ size_t i;
+
+ for (i = 0; i < CPU_STATES; i++) {
+ ticks += kstat_delta(old, new, cpu_states[i].field_name,
+ &cpu_states[i].index);
+ }
+ return ((ticks == 0) ? 1 : ticks);
+}
+
+int kstat_copy(const kstat_t *src, kstat_t *dst)
+{
+ void *dst_data = NULL;
+
+ if (dst->ks_data && dst->ks_data_size < src->ks_data_size)
+ free((void *)dst->ks_data);
+ else
+ dst_data = dst->ks_data;
+
+ *dst = *src;
+
+ if (src->ks_data != NULL) {
+ if (dst_data)
+ dst->ks_data = dst_data;
+ else if ((dst->ks_data = malloc(src->ks_data_size)) == NULL)
+ return (-1);
+ bcopy(src->ks_data, dst->ks_data, src->ks_data_size);
+ } else {
+ if (dst_data)
+ free((void *)dst_data);
+ dst->ks_data = NULL;
+ dst->ks_data_size = 0;
+ }
+ return (0);
+}
+
+/* Get_CPU_Load returns an array of CPU loads, one for each CPU, scaled
+ to HAUTEUR. The array is defined and allocated by the main program
+ and passed to the function as '*load'. The number of CPUs present
+ is given in 'Cpu_tot' */
+unsigned int *Get_CPU_Load(unsigned int *load, unsigned int Cpu_tot)
+{
+ kstat_t *ksp_new;
+ double factor;
+ uint64_t cur_load;
+ int i;
+
+ if (kcp == NULL || ksp_old == NULL)
+ return (load);
+
+ for (i = 0; i < Cpu_tot; i++) {
+ if ((ksp_new = kstat_lookup(kcp, "cpu", i, "sys")) == NULL) {
+ load[i] = 0;
+ continue;
+ }
+
+ if (kstat_read(kcp, ksp_new, NULL) == -1) {
+ load[i] = 0;
+ continue;
+ }
+
+ cur_load = cpu_ticks_delta(&ksp_old[i], ksp_new);
+ factor = HAUTEUR / (double)cur_load;
+
+ cur_load = kstat_delta(&ksp_old[i], ksp_new,
+ cpu_states[1].field_name, &cpu_states[1].index) +
+ kstat_delta(&ksp_old[i], ksp_new, cpu_states[2].field_name,
+ &cpu_states[2].index);
+ if (ksp_old[i].ks_data) {
+ load[i] = factor * cur_load;
+ }
+ kstat_copy(ksp_new, &ksp_old[i]);
+ }
+
+ return (load);
+}
+
+/* return current memory/swap usage on a scale from 0-100 */
+unsigned int Get_Memory(void)
+{
+ kstat_t *ksp_new;
+ static vminfo_t *vm_new = NULL;
+ static vminfo_t *vm_old = NULL;
+ vminfo_t *vm_swap;
+ static uint64_t freemem = 0;
+
+ if ((ksp_new = kstat_lookup(kcp, "unix", 0, "vminfo")) == NULL) {
+ return (0);
+ }
+
+ if (vm_new == NULL && (vm_new = malloc(sizeof (vminfo_t))) == NULL) {
+ return (0);
+ }
+
+ if (kstat_read(kcp, ksp_new, vm_new) == -1) {
+ return (0);
+ }
+
+ if (vm_old != NULL) {
+ uint64_t step = vm_new->updates - vm_old->updates;
+
+ if (step > 0) {
+ freemem = (vm_new->freemem - vm_old->freemem) / step;
+ }
+ }
+
+ vm_swap = vm_new;
+ vm_new = vm_old;
+ vm_old = vm_swap;
+
+ if (vm_new == NULL)
+ return (0);
+
+ return (100 * (physmem - freemem) / physmem);
+}
+
+unsigned int Get_Swap(void)
+{
+ struct anoninfo ai;
+
+ if (swapctl(SC_AINFO, &ai) == -1) {
+ return (0);
+ }
+
+ return (100 * (ai.ani_max - ai.ani_free) / ai.ani_max);
+}
--
1.7.2.5
From 42f3dd062d3ffeec91ba82777789222fc07b25d0 Mon Sep 17 00:00:00 2001
From: Milan Äermák <[email protected]>
Date: Sat, 19 Jan 2013 10:17:30 +0100
Subject: [PATCH 2/2] wmSMPmon: Code formating cleanup
---
wmSMPmon/wmSMPmon/general.c | 99 +++---
wmSMPmon/wmSMPmon/sysinfo-linux.c | 419 +++++++++++++-------------
wmSMPmon/wmSMPmon/sysinfo-linux.h | 25 +-
wmSMPmon/wmSMPmon/wmSMPmon.c | 604 ++++++++++++++++++-------------------
wmSMPmon/wmgeneral/wmgeneral.c | 101 +++---
5 files changed, 617 insertions(+), 631 deletions(-)
diff --git a/wmSMPmon/wmSMPmon/general.c b/wmSMPmon/wmSMPmon/general.c
index d4194bb..7ac8686 100644
--- a/wmSMPmon/wmSMPmon/general.c
+++ b/wmSMPmon/wmSMPmon/general.c
@@ -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);
}
diff --git a/wmSMPmon/wmSMPmon/sysinfo-linux.c b/wmSMPmon/wmSMPmon/sysinfo-linux.c
index 854008c..042fc4b 100644
--- a/wmSMPmon/wmSMPmon/sysinfo-linux.c
+++ b/wmSMPmon/wmSMPmon/sysinfo-linux.c
@@ -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));
}
diff --git a/wmSMPmon/wmSMPmon/sysinfo-linux.h b/wmSMPmon/wmSMPmon/sysinfo-linux.h
index 73850d7..da56ee0 100644
--- a/wmSMPmon/wmSMPmon/sysinfo-linux.h
+++ b/wmSMPmon/wmSMPmon/sysinfo-linux.h
@@ -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 */
diff --git a/wmSMPmon/wmSMPmon/wmSMPmon.c b/wmSMPmon/wmSMPmon/wmSMPmon.c
index 87ca832..3db6370 100644
--- a/wmSMPmon/wmSMPmon/wmSMPmon.c
+++ b/wmSMPmon/wmSMPmon/wmSMPmon.c
@@ -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 <[email protected]> and
PhiR <[email protected]>
CONTRIBUTORS : Alain Schröder <[email protected]>
@@ -12,16 +12,15 @@ CURRENT MAINTAINER: Thomas Ribbrock <[email protected]>
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 ;
-
- 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 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));
-
- 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];
+ 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 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));
+
+ 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;
}
- 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);
- }
-}
+ /* -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);
-
- 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(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"
- "<[email protected]> http://goupilfr.org\n"
- "<[email protected]> http://gcu-squad.org\n"
- "<[email protected]> http://www.ribbrock.org\n",
- stderr) ;
- exit(OK) ;
-}
+ 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(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"
+ "<[email protected]> http://goupilfr.org\n"
+ "<[email protected]> http://gcu-squad.org\n"
+ "<[email protected]> http://www.ribbrock.org\n",
+ stderr);
+
+ exit(OK);
+}
diff --git a/wmSMPmon/wmgeneral/wmgeneral.c b/wmSMPmon/wmgeneral/wmgeneral.c
index a49d686..13caa7b 100644
--- a/wmSMPmon/wmgeneral/wmgeneral.c
+++ b/wmSMPmon/wmgeneral/wmgeneral.c
@@ -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;
-
- curpixel=0;
- for (k=0; k!=depth; k++)
- {
- curpixel <<=8;
- curpixel |= xpm[i][j+k];
- }
-
- if ( curpixel != zero ) {
+ 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) {
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;
--
1.7.2.5