library: tweak formatting for readstat header and code
This patch mostly just eliminates darn tab characters. Plus the library function declarations and definitions have been standardized. Most visibly, the input params now have all been indented on their own separate line. The following names were changed to more closely match meminfo.c or provide a certain symmetry. Unfortunately that also impacted some other pgms which were updated. . 'procps_stat_get' evolved into 'procps_stat_get_sys' . 'procps_stat_info' is now known as 'procps_statinfo' [and just a little trailing whitespace was eliminated] Signed-off-by: Jim Warner <james.warner@comcast.net>
This commit is contained in:
parent
c3fd7473c5
commit
6bb1a033b4
@ -73,7 +73,7 @@ global:
|
||||
procps_stat_ref;
|
||||
procps_stat_unref;
|
||||
procps_stat_get_cpu;
|
||||
procps_stat_get;
|
||||
procps_stat_get_sys;
|
||||
procps_vmstat_new;
|
||||
procps_vmstat_read;
|
||||
procps_vmstat_ref;
|
||||
|
192
proc/readstat.c
192
proc/readstat.c
@ -31,12 +31,13 @@ struct stat_data {
|
||||
unsigned int procs_running;
|
||||
};
|
||||
|
||||
struct procps_stat_info {
|
||||
struct procps_statinfo {
|
||||
int refcount;
|
||||
int stat_fd;
|
||||
struct stat_data data;
|
||||
};
|
||||
|
||||
|
||||
/*
|
||||
* procps_stat_new:
|
||||
*
|
||||
@ -47,12 +48,13 @@ struct procps_stat_info {
|
||||
*
|
||||
* Returns: a new stat info container
|
||||
*/
|
||||
PROCPS_EXPORT int procps_stat_new(struct procps_stat_info **info)
|
||||
PROCPS_EXPORT int procps_stat_new (
|
||||
struct procps_statinfo **info)
|
||||
{
|
||||
struct procps_stat_info *v;
|
||||
v = calloc(1, sizeof(struct procps_stat_info));
|
||||
struct procps_statinfo *v;
|
||||
v = calloc(1, sizeof(struct procps_statinfo));
|
||||
if (!v)
|
||||
return -ENOMEM;
|
||||
return -ENOMEM;
|
||||
|
||||
v->refcount = 1;
|
||||
v->stat_fd = -1;
|
||||
@ -68,138 +70,146 @@ PROCPS_EXPORT int procps_stat_new(struct procps_stat_info **info)
|
||||
*
|
||||
* If CPU stats only needed, set cpu_only to non-zero
|
||||
*/
|
||||
PROCPS_EXPORT int procps_stat_read(struct procps_stat_info *info, const int cpu_only)
|
||||
PROCPS_EXPORT int procps_stat_read (
|
||||
struct procps_statinfo *info,
|
||||
const int cpu_only)
|
||||
{
|
||||
char buf[8192];
|
||||
char *head, *tail;
|
||||
int size;
|
||||
|
||||
if (info == NULL)
|
||||
return -1;
|
||||
return -1;
|
||||
|
||||
memset(&(info->data), 0, sizeof(struct stat_data));
|
||||
/* read in the data */
|
||||
|
||||
if (-1 == info->stat_fd && (info->stat_fd = open(STAT_FILE, O_RDONLY)) == -1) {
|
||||
return -errno;
|
||||
return -errno;
|
||||
}
|
||||
if (lseek(info->stat_fd, 0L, SEEK_SET) == -1) {
|
||||
return -errno;
|
||||
return -errno;
|
||||
}
|
||||
if ((size = read(info->stat_fd, buf, sizeof(buf)-1)) < 0) {
|
||||
return -1;
|
||||
return -1;
|
||||
}
|
||||
buf[size] = '\0';
|
||||
|
||||
/* Scan the file */
|
||||
head = buf;
|
||||
do {
|
||||
tail = strchr(head, ' ');
|
||||
if (!tail)
|
||||
break;
|
||||
*tail = '\0';
|
||||
if (0 == strcmp(head, "cpu")) {
|
||||
if (sscanf(tail+1, "%Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu",
|
||||
&(info->data.cpu_user),
|
||||
&(info->data.cpu_nice),
|
||||
&(info->data.cpu_sys),
|
||||
&(info->data.cpu_idle),
|
||||
&(info->data.cpu_iowait),
|
||||
&(info->data.cpu_irq),
|
||||
&(info->data.cpu_sirq),
|
||||
&(info->data.cpu_stol),
|
||||
&(info->data.cpu_guest),
|
||||
&(info->data.cpu_nice)
|
||||
) != 10)
|
||||
return -1;
|
||||
tail = strchr(head, ' ');
|
||||
if (!tail)
|
||||
break;
|
||||
*tail = '\0';
|
||||
if (0 == strcmp(head, "cpu")) {
|
||||
if (sscanf(tail+1, "%Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu",
|
||||
&(info->data.cpu_user),
|
||||
&(info->data.cpu_nice),
|
||||
&(info->data.cpu_sys),
|
||||
&(info->data.cpu_idle),
|
||||
&(info->data.cpu_iowait),
|
||||
&(info->data.cpu_irq),
|
||||
&(info->data.cpu_sirq),
|
||||
&(info->data.cpu_stol),
|
||||
&(info->data.cpu_guest),
|
||||
&(info->data.cpu_nice)
|
||||
) != 10)
|
||||
return -1;
|
||||
|
||||
if (cpu_only)
|
||||
return 0; // we got what we need
|
||||
} else if (0 == strcmp(head, "intr")) {
|
||||
info->data.intr = strtoul(tail+1, &tail, 10);
|
||||
} else if (0 == strcmp(head, "ctxt")) {
|
||||
info->data.ctxt = strtoul(tail+1, &tail, 10);
|
||||
} else if (0 == strcmp(head, "btime")) {
|
||||
info->data.btime = strtoul(tail+1, &tail, 10);
|
||||
} else if (0 == strcmp(head, "processes")) {
|
||||
info->data.procs = strtoul(tail+1, &tail, 10);
|
||||
} else if (0 == strcmp(head, "procs_blocked")) {
|
||||
info->data.procs_blocked = strtoul(tail+1, &tail, 10);
|
||||
} else if (0 == strcmp(head, "procs_running")) {
|
||||
info->data.procs_running = strtoul(tail+1, &tail, 10);
|
||||
}
|
||||
if (tail[0] != '\n')
|
||||
tail = strchr(tail+1, '\n');
|
||||
if (!tail)
|
||||
break;
|
||||
head = tail + 1;
|
||||
} while(tail);
|
||||
if (cpu_only)
|
||||
return 0; // we got what we need
|
||||
} else if (0 == strcmp(head, "intr")) {
|
||||
info->data.intr = strtoul(tail+1, &tail, 10);
|
||||
} else if (0 == strcmp(head, "ctxt")) {
|
||||
info->data.ctxt = strtoul(tail+1, &tail, 10);
|
||||
} else if (0 == strcmp(head, "btime")) {
|
||||
info->data.btime = strtoul(tail+1, &tail, 10);
|
||||
} else if (0 == strcmp(head, "processes")) {
|
||||
info->data.procs = strtoul(tail+1, &tail, 10);
|
||||
} else if (0 == strcmp(head, "procs_blocked")) {
|
||||
info->data.procs_blocked = strtoul(tail+1, &tail, 10);
|
||||
} else if (0 == strcmp(head, "procs_running")) {
|
||||
info->data.procs_running = strtoul(tail+1, &tail, 10);
|
||||
}
|
||||
if (tail[0] != '\n')
|
||||
tail = strchr(tail+1, '\n');
|
||||
if (!tail)
|
||||
break;
|
||||
head = tail + 1;
|
||||
} while (tail);
|
||||
if (info->data.procs)
|
||||
info->data.procs--; // exclude this process
|
||||
info->data.procs--; // exclude this process
|
||||
return 0;
|
||||
}
|
||||
|
||||
PROCPS_EXPORT struct procps_stat_info *procps_stat_ref(struct procps_stat_info *info)
|
||||
PROCPS_EXPORT struct procps_statinfo *procps_stat_ref (
|
||||
struct procps_statinfo *info)
|
||||
{
|
||||
if (info == NULL)
|
||||
return NULL;
|
||||
return NULL;
|
||||
info->refcount++;
|
||||
return info;
|
||||
}
|
||||
|
||||
PROCPS_EXPORT struct procps_stat_info *procps_stat_unref(struct procps_stat_info *info)
|
||||
PROCPS_EXPORT struct procps_statinfo *procps_stat_unref (
|
||||
struct procps_statinfo *info)
|
||||
{
|
||||
if (info == NULL)
|
||||
return NULL;
|
||||
return NULL;
|
||||
info->refcount--;
|
||||
if (info->refcount > 0)
|
||||
return NULL;
|
||||
return NULL;
|
||||
free(info);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
PROCPS_EXPORT jiff procps_stat_get_cpu(struct procps_stat_info *info, enum procps_cpu_item item)
|
||||
PROCPS_EXPORT jiff procps_stat_get_cpu (
|
||||
struct procps_statinfo *info,
|
||||
enum procps_cpu_item item)
|
||||
{
|
||||
switch(item) {
|
||||
case PROCPS_CPU_USER:
|
||||
return info->data.cpu_user;
|
||||
case PROCPS_CPU_NICE:
|
||||
return info->data.cpu_nice;
|
||||
case PROCPS_CPU_SYSTEM:
|
||||
return info->data.cpu_sys;
|
||||
case PROCPS_CPU_IDLE:
|
||||
return info->data.cpu_idle;
|
||||
case PROCPS_CPU_IOWAIT:
|
||||
return info->data.cpu_iowait;
|
||||
case PROCPS_CPU_IRQ:
|
||||
return info->data.cpu_irq;
|
||||
case PROCPS_CPU_SIRQ:
|
||||
return info->data.cpu_sirq;
|
||||
case PROCPS_CPU_STOLEN:
|
||||
return info->data.cpu_stol;
|
||||
case PROCPS_CPU_GUEST:
|
||||
return info->data.cpu_guest;
|
||||
case PROCPS_CPU_GNICE:
|
||||
return info->data.cpu_gnice;
|
||||
switch (item) {
|
||||
case PROCPS_CPU_USER:
|
||||
return info->data.cpu_user;
|
||||
case PROCPS_CPU_NICE:
|
||||
return info->data.cpu_nice;
|
||||
case PROCPS_CPU_SYSTEM:
|
||||
return info->data.cpu_sys;
|
||||
case PROCPS_CPU_IDLE:
|
||||
return info->data.cpu_idle;
|
||||
case PROCPS_CPU_IOWAIT:
|
||||
return info->data.cpu_iowait;
|
||||
case PROCPS_CPU_IRQ:
|
||||
return info->data.cpu_irq;
|
||||
case PROCPS_CPU_SIRQ:
|
||||
return info->data.cpu_sirq;
|
||||
case PROCPS_CPU_STOLEN:
|
||||
return info->data.cpu_stol;
|
||||
case PROCPS_CPU_GUEST:
|
||||
return info->data.cpu_guest;
|
||||
case PROCPS_CPU_GNICE:
|
||||
return info->data.cpu_gnice;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
PROCPS_EXPORT unsigned int procps_stat_get(struct procps_stat_info *info, enum procps_stat_item item)
|
||||
PROCPS_EXPORT unsigned int procps_stat_get_sys (
|
||||
struct procps_statinfo *info,
|
||||
enum procps_stat_item item)
|
||||
{
|
||||
switch(item) {
|
||||
case PROCPS_STAT_INTR:
|
||||
return info->data.intr;
|
||||
case PROCPS_STAT_CTXT:
|
||||
return info->data.ctxt;
|
||||
case PROCPS_STAT_BTIME:
|
||||
return info->data.btime;
|
||||
case PROCPS_STAT_PROCS:
|
||||
return info->data.procs;
|
||||
case PROCPS_STAT_PROCS_BLK:
|
||||
return info->data.procs_blocked;
|
||||
case PROCPS_STAT_PROCS_RUN:
|
||||
return info->data.procs_running;
|
||||
switch (item) {
|
||||
case PROCPS_STAT_INTR:
|
||||
return info->data.intr;
|
||||
case PROCPS_STAT_CTXT:
|
||||
return info->data.ctxt;
|
||||
case PROCPS_STAT_BTIME:
|
||||
return info->data.btime;
|
||||
case PROCPS_STAT_PROCS:
|
||||
return info->data.procs;
|
||||
case PROCPS_STAT_PROCS_BLK:
|
||||
return info->data.procs_blocked;
|
||||
case PROCPS_STAT_PROCS_RUN:
|
||||
return info->data.procs_running;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
@ -45,12 +45,17 @@ enum procps_stat_item {
|
||||
};
|
||||
|
||||
typedef unsigned long long jiff;
|
||||
struct procps_stat_info;
|
||||
int procps_stat_new(struct procps_stat_info **info);
|
||||
int procps_stat_read(struct procps_stat_info *info, const int cpu_only);
|
||||
struct procps_stat_info *procps_stat_ref(struct procps_stat_info *info);
|
||||
struct procps_stat_info *procps_stat_unref(struct procps_stat_info *info);
|
||||
jiff procps_stat_get_cpu(struct procps_stat_info *info, enum procps_cpu_item item);
|
||||
unsigned int procps_stat_get(struct procps_stat_info *info, enum procps_stat_item item);
|
||||
|
||||
struct procps_statinfo;
|
||||
|
||||
int procps_stat_new (struct procps_statinfo **info);
|
||||
int procps_stat_read (struct procps_statinfo *info, const int cpu_only);
|
||||
|
||||
struct procps_statinfo *procps_stat_ref (struct procps_statinfo *info);
|
||||
struct procps_statinfo *procps_stat_unref (struct procps_statinfo *info);
|
||||
|
||||
jiff procps_stat_get_cpu (struct procps_statinfo *info, enum procps_cpu_item item);
|
||||
unsigned int procps_stat_get_sys (struct procps_statinfo *info, enum procps_stat_item item);
|
||||
|
||||
__END_DECLS
|
||||
#endif
|
||||
|
@ -61,7 +61,7 @@
|
||||
#include <sys/mman.h>
|
||||
#include <sys/resource.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
|
||||
#include "c.h"
|
||||
#include "../proc/readproc.h"
|
||||
#include "../proc/sysinfo.h"
|
||||
@ -98,14 +98,14 @@ extern long Hertz;
|
||||
|
||||
static void get_boot_time(void)
|
||||
{
|
||||
struct procps_stat_info *sys_info;
|
||||
if (procps_stat_new(&sys_info) < 0)
|
||||
struct procps_statinfo *sys_info;
|
||||
if (procps_stat_new(&sys_info) < 0)
|
||||
xerrx(EXIT_FAILURE,
|
||||
_("Unable to create system stat structure"));
|
||||
if (procps_stat_read(sys_info,0) < 0)
|
||||
xerrx(EXIT_FAILURE,
|
||||
_("Unable to read system stat information"));
|
||||
boot_time = procps_stat_get(sys_info, PROCPS_STAT_BTIME);
|
||||
boot_time = procps_stat_get_sys(sys_info, PROCPS_STAT_BTIME);
|
||||
procps_stat_unref(sys_info);
|
||||
}
|
||||
|
||||
|
38
vmstat.c
38
vmstat.c
@ -298,7 +298,7 @@ static void new_format(void)
|
||||
time_t the_time;
|
||||
char timebuf[32];
|
||||
struct procps_vmstat *vm_info;
|
||||
struct procps_stat_info *sys_info;
|
||||
struct procps_statinfo *sys_info;
|
||||
struct procps_meminfo *mem_info;
|
||||
|
||||
sleep_half = (sleep_time / 2);
|
||||
@ -329,8 +329,8 @@ static void new_format(void)
|
||||
tm_ptr = localtime( &the_time );
|
||||
strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S", tm_ptr);
|
||||
}
|
||||
/* Do the intial fill */
|
||||
cpu_use[tog] = procps_stat_get_cpu(sys_info, PROCPS_CPU_USER) +
|
||||
/* Do the initial fill */
|
||||
cpu_use[tog] = procps_stat_get_cpu(sys_info, PROCPS_CPU_USER) +
|
||||
procps_stat_get_cpu(sys_info, PROCPS_CPU_NICE);
|
||||
cpu_sys[tog] = procps_stat_get_cpu(sys_info, PROCPS_CPU_SYSTEM);
|
||||
procps_stat_get_cpu(sys_info, PROCPS_CPU_IRQ) +
|
||||
@ -338,8 +338,8 @@ static void new_format(void)
|
||||
cpu_idl[tog] = procps_stat_get_cpu(sys_info, PROCPS_CPU_IDLE);
|
||||
cpu_iow[tog] = procps_stat_get_cpu(sys_info, PROCPS_CPU_IOWAIT);
|
||||
cpu_sto[tog] = procps_stat_get_cpu(sys_info, PROCPS_CPU_STOLEN);
|
||||
intr[tog] = procps_stat_get(sys_info, PROCPS_STAT_INTR);
|
||||
ctxt[tog] = procps_stat_get(sys_info, PROCPS_STAT_CTXT);
|
||||
intr[tog] = procps_stat_get_sys(sys_info, PROCPS_STAT_INTR);
|
||||
ctxt[tog] = procps_stat_get_sys(sys_info, PROCPS_STAT_CTXT);
|
||||
pgpgin[tog] = procps_vmstat_get(vm_info, PROCPS_VMSTAT_PGPGIN);
|
||||
pgpgout[tog] = procps_vmstat_get(vm_info, PROCPS_VMSTAT_PGPGOUT);
|
||||
pswpin[tog] = procps_vmstat_get(vm_info, PROCPS_VMSTAT_PSWPIN);
|
||||
@ -353,8 +353,8 @@ static void new_format(void)
|
||||
divo2 = Div / 2UL;
|
||||
|
||||
printf(w_option ? wide_format : format,
|
||||
procps_stat_get(sys_info, PROCPS_STAT_PROCS_RUN),
|
||||
procps_stat_get(sys_info, PROCPS_STAT_PROCS_BLK),
|
||||
procps_stat_get_sys(sys_info, PROCPS_STAT_PROCS_RUN),
|
||||
procps_stat_get_sys(sys_info, PROCPS_STAT_PROCS_BLK),
|
||||
unitConvert(procps_meminfo_get(mem_info, PROCPS_SWAP_USED)),
|
||||
unitConvert(procps_meminfo_get(mem_info, PROCPS_MEM_FREE)),
|
||||
unitConvert(procps_meminfo_get(mem_info, (a_option?PROCPS_MEM_INACTIVE:PROCPS_MEM_BUFFERS))),
|
||||
@ -400,8 +400,8 @@ static void new_format(void)
|
||||
cpu_idl[tog] = procps_stat_get_cpu(sys_info, PROCPS_CPU_IDLE);
|
||||
cpu_iow[tog] = procps_stat_get_cpu(sys_info, PROCPS_CPU_IOWAIT);
|
||||
cpu_sto[tog] = procps_stat_get_cpu(sys_info, PROCPS_CPU_STOLEN);
|
||||
intr[tog] = procps_stat_get(sys_info, PROCPS_STAT_INTR);
|
||||
ctxt[tog] = procps_stat_get(sys_info, PROCPS_STAT_CTXT);
|
||||
intr[tog] = procps_stat_get_sys(sys_info, PROCPS_STAT_INTR);
|
||||
ctxt[tog] = procps_stat_get_sys(sys_info, PROCPS_STAT_CTXT);
|
||||
pgpgin[tog] = procps_vmstat_get(vm_info, PROCPS_VMSTAT_PGPGIN);
|
||||
pgpgout[tog] = procps_vmstat_get(vm_info, PROCPS_VMSTAT_PGPGOUT);
|
||||
pswpin[tog] = procps_vmstat_get(vm_info, PROCPS_VMSTAT_PSWPIN);
|
||||
@ -437,8 +437,8 @@ static void new_format(void)
|
||||
if (!Div) Div = 1, didl = 1;
|
||||
divo2 = Div / 2UL;
|
||||
printf(w_option ? wide_format : format,
|
||||
procps_stat_get(sys_info, PROCPS_STAT_PROCS_RUN),
|
||||
procps_stat_get(sys_info, PROCPS_STAT_PROCS_BLK),
|
||||
procps_stat_get_sys(sys_info, PROCPS_STAT_PROCS_RUN),
|
||||
procps_stat_get_sys(sys_info, PROCPS_STAT_PROCS_BLK),
|
||||
unitConvert(procps_meminfo_get(mem_info, PROCPS_SWAP_USED)),
|
||||
unitConvert(procps_meminfo_get(mem_info, PROCPS_MEM_FREE)),
|
||||
unitConvert(procps_meminfo_get(mem_info,
|
||||
@ -837,7 +837,7 @@ static void disksum_format(void)
|
||||
|
||||
static void sum_format(void)
|
||||
{
|
||||
struct procps_stat_info *sys_info;
|
||||
struct procps_statinfo *sys_info;
|
||||
struct procps_vmstat *vm_info;
|
||||
struct procps_meminfo *mem_info;
|
||||
|
||||
@ -888,15 +888,15 @@ static void sum_format(void)
|
||||
printf(_("%13lu pages paged out\n"), procps_vmstat_get(vm_info, PROCPS_VMSTAT_PGPGOUT));
|
||||
printf(_("%13lu pages swapped in\n"), procps_vmstat_get(vm_info, PROCPS_VMSTAT_PSWPIN));
|
||||
printf(_("%13lu pages swapped out\n"), procps_vmstat_get(vm_info, PROCPS_VMSTAT_PSWPOUT));
|
||||
printf(_("%13u interrupts\n"), procps_stat_get(sys_info, PROCPS_STAT_INTR));
|
||||
printf(_("%13u CPU context switches\n"), procps_stat_get(sys_info, PROCPS_STAT_CTXT));
|
||||
printf(_("%13u boot time\n"), procps_stat_get(sys_info, PROCPS_STAT_BTIME));
|
||||
printf(_("%13u forks\n"), procps_stat_get(sys_info, PROCPS_STAT_PROCS));
|
||||
printf(_("%13u interrupts\n"), procps_stat_get_sys(sys_info, PROCPS_STAT_INTR));
|
||||
printf(_("%13u CPU context switches\n"), procps_stat_get_sys(sys_info, PROCPS_STAT_CTXT));
|
||||
printf(_("%13u boot time\n"), procps_stat_get_sys(sys_info, PROCPS_STAT_BTIME));
|
||||
printf(_("%13u forks\n"), procps_stat_get_sys(sys_info, PROCPS_STAT_PROCS));
|
||||
}
|
||||
|
||||
static void fork_format(void)
|
||||
{
|
||||
struct procps_stat_info *sys_info;
|
||||
struct procps_statinfo *sys_info;
|
||||
|
||||
if (procps_stat_new(&sys_info) < 0)
|
||||
xerrx(EXIT_FAILURE,
|
||||
@ -904,8 +904,8 @@ static void fork_format(void)
|
||||
if (procps_stat_read(sys_info,0) < 0)
|
||||
xerrx(EXIT_FAILURE,
|
||||
_("Unable to read system stat information"));
|
||||
|
||||
printf(_("%13u forks\n"), procps_stat_get(sys_info, PROCPS_STAT_PROCS));
|
||||
|
||||
printf(_("%13u forks\n"), procps_stat_get_sys(sys_info, PROCPS_STAT_PROCS));
|
||||
}
|
||||
|
||||
static int winhi(void)
|
||||
|
Loading…
Reference in New Issue
Block a user