From 4a0e974b7fe55dfac9ab1b4efe78d457850d10b6 Mon Sep 17 00:00:00 2001 From: Jim Warner Date: Tue, 14 Jun 2016 00:00:00 -0500 Subject: [PATCH] library: more tweaks for code and/or comments, 3rd gen Following is a summary of significant changes (if any) to each of these now upgraded 3rd gen library modules. ............................................ . eliminated duplicate decl of 'struct procps_meminfo' . standardized/normalized results struct union members . added 'std' & 'var' dividers in .c file, like . how did i miss relocating all these friggin' #undefs . cleanup 'get' return logic (remove a redundant 'if') ............................................... . repositioned the procps_pidsinfo structure in header . removed the extra trailing comma from enum pids_item . standardized/normalized results struct union members ........................................... . corrected comment typo (jeeze, in an 'aligned' para) . standardized/normalized results struct union members . added 'std' & 'var' dividers in .c file, like . removed an obsolete #undef from procps_slabinfo_sort . cleanup 'get' return logic (remove a redundant 'if') ............................................... . how did i miss relocating all these friggin' #undefs . corrected an initialization fencepost used with numa <=== see Craig, here's a bug fix . removed the extra trailing comma from enum stat_item . standardized/normalized results struct union members . added 'std' & 'var' dividers in .c file, like . strengthen those parm checks in procps_stat_get func . cleanup 'get' return logic (remove a redundant 'if') ............................................. . standardized/normalized results struct union members . added 'std' & 'var' dividers in .c file, like . cleanup 'get' return logic (remove a redundant 'if') [ virtually all of these tweaks reflect the author's ] [ continuing pursuit of an unreasonable goal -- that ] [ of a 'perfect' (plus 'pretty') C language program! ] Signed-off-by: Jim Warner --- proc/meminfo.c | 23 ++++++++++++++--------- proc/meminfo.h | 3 +-- proc/pids.h | 22 +++++++++++----------- proc/slabinfo.c | 7 +++++-- proc/slabinfo.h | 8 ++++---- proc/stat.c | 38 ++++++++++++++++++++++++-------------- proc/stat.h | 8 ++++---- proc/vmstat.c | 8 +++++--- proc/vmstat.h | 4 ++-- 9 files changed, 70 insertions(+), 51 deletions(-) diff --git a/proc/meminfo.c b/proc/meminfo.c index 6c9fa0fe..fd313e37 100644 --- a/proc/meminfo.c +++ b/proc/meminfo.c @@ -224,6 +224,10 @@ MEM_set(SWAP_FREE, ul_int, SwapFree) MEM_set(SWAP_TOTAL, ul_int, SwapTotal) MEM_set(SWAP_USED, ul_int, derived_swap_used) +#undef setDECL +#undef MEM_set +#undef HST_set + // ___ Results 'Get' Support |||||||||||||||||||||||||||||||||||||||||||||||||| @@ -332,6 +336,10 @@ MEM_get(SWAP_FREE, SwapFree) MEM_get(SWAP_TOTAL, SwapTotal) MEM_get(SWAP_USED, derived_swap_used) +#undef getDECL +#undef MEM_get +#undef HST_get + // ___ Controlling Table |||||||||||||||||||||||||||||||||||||||||||||||||||||| @@ -456,16 +464,11 @@ static struct { enum meminfo_item PROCPS_MEMINFO_logical_end = PROCPS_MEMINFO_SWAP_USED + 1; #undef setNAME -#undef setDECL -#undef MEM_set -#undef HST_set #undef getNAME -#undef getDECL -#undef MEM_get -#undef HST_get #undef RS #undef RG + // ___ Private Functions |||||||||||||||||||||||||||||||||||||||||||||||||||||| static inline void assign_results ( @@ -816,6 +819,8 @@ static struct stacks_extent *stacks_alloc ( // ___ Public Functions ||||||||||||||||||||||||||||||||||||||||||||||||||||||| +// --- standard required functions -------------------------------------------- + /* * procps_meminfo_new: * @@ -882,6 +887,8 @@ PROCPS_EXPORT int procps_meminfo_unref ( } // end: procps_meminfo_unref +// --- variable interface functions ------------------------------------------- + PROCPS_EXPORT signed long procps_meminfo_get ( struct procps_meminfo *info, enum meminfo_item item) @@ -904,9 +911,7 @@ PROCPS_EXPORT signed long procps_meminfo_get ( sav_secs = cur_secs; } - if (item < PROCPS_MEMINFO_logical_end) - return Item_table[item].getsfunc(info); - return -EINVAL; + return Item_table[item].getsfunc(info); } // end: procps_meminfo_get diff --git a/proc/meminfo.h b/proc/meminfo.h index 9343058c..d5380d82 100644 --- a/proc/meminfo.h +++ b/proc/meminfo.h @@ -121,12 +121,11 @@ enum meminfo_item { PROCPS_MEMINFO_SWAP_USED // ul_int }; -struct procps_meminfo; struct meminfo_result { enum meminfo_item item; union { - signed int s_int; + signed int s_int; unsigned long ul_int; } result; }; diff --git a/proc/pids.h b/proc/pids.h index c9838ccd..b978a5df 100644 --- a/proc/pids.h +++ b/proc/pids.h @@ -144,7 +144,7 @@ enum pids_item { PROCPS_PIDS_VM_USED, // sl_int PROCPS_PIDS_VSIZE_PGS, // ul_int PROCPS_PIDS_WCHAN_ADDR, // ul_int - PROCPS_PIDS_WCHAN_NAME, // str + PROCPS_PIDS_WCHAN_NAME // str }; enum pids_fetch_type { @@ -163,19 +163,17 @@ enum pids_sort_order { }; -struct procps_pidsinfo; - struct pids_result { enum pids_item item; union { - char s_ch; - int s_int; - unsigned int u_int; - long sl_int; - unsigned long ul_int; - unsigned long long ull_int; - char * str; - char ** strv; + signed char s_ch; + signed int s_int; + unsigned int u_int; + signed long sl_int; + unsigned long ul_int; + unsigned long long ull_int; + char *str; + char **strv; } result; }; @@ -198,6 +196,8 @@ struct pids_fetch { stack -> head [ rel_enum ] . result . type +struct procps_pidsinfo; + int procps_pids_new (struct procps_pidsinfo **info, enum pids_item *items, int numitems); int procps_pids_ref (struct procps_pidsinfo *info); int procps_pids_unref (struct procps_pidsinfo **info); diff --git a/proc/slabinfo.c b/proc/slabinfo.c index 4516c232..8249cc03 100644 --- a/proc/slabinfo.c +++ b/proc/slabinfo.c @@ -829,6 +829,8 @@ static int stacks_reconfig_maybe ( // ___ Public Functions ||||||||||||||||||||||||||||||||||||||||||||||||||||||| +// --- standard required functions -------------------------------------------- + /* * procps_slabinfo_new(): * @@ -859,7 +861,7 @@ PROCPS_EXPORT int procps_slabinfo_new ( /* do a priming read here for the following potential benefits: | 1) see if that caller's permissions were sufficient (root) | - 2) make delta results potentially useful, even is 1st time | */ + 2) make delta results potentially useful, even if 1st time | */ if ((rc = read_slabinfo_failed(p))) { procps_slabinfo_unref(&p); return rc; @@ -919,6 +921,8 @@ PROCPS_EXPORT int procps_slabinfo_unref ( } // end: procps_slabinfo_unref +// --- variable interface functions ------------------------------------------- + PROCPS_EXPORT signed long procps_slabinfo_get ( struct procps_slabinfo *info, enum slabinfo_item item) @@ -1056,5 +1060,4 @@ PROCPS_EXPORT struct slabinfo_stack **procps_slabinfo_sort ( qsort_r(stacks, numstacked, sizeof(void *), (QSR_t)Item_table[p->item].sortfunc, &parms); return stacks; - #undef QSORT_r } // end: procps_slabinfo_sort diff --git a/proc/slabinfo.h b/proc/slabinfo.h index 0c3df56f..6faa866d 100644 --- a/proc/slabinfo.h +++ b/proc/slabinfo.h @@ -78,10 +78,10 @@ enum slabinfo_sort_order { struct slabinfo_result { enum slabinfo_item item; union { - signed int s_int; - unsigned int u_int; - unsigned long ul_int; - char * str; + signed int s_int; + unsigned int u_int; + unsigned long ul_int; + char *str; } result; }; diff --git a/proc/stat.c b/proc/stat.c index 4f8ea3e2..8b7ef63f 100644 --- a/proc/stat.c +++ b/proc/stat.c @@ -185,6 +185,12 @@ setDECL(SYS_DELTA_PROC_BLOCKED) { (void)T; R->result.s_int = S->new.procs_blocke SYSsetH(SYS_DELTA_PROC_CREATED, s_int, procs_created) setDECL(SYS_DELTA_PROC_RUNNING) { (void)T; R->result.s_int = S->new.procs_running - S->old.procs_running; } +#undef setDECL +#undef TIC_set +#undef SYS_set +#undef TICsetH +#undef SYSsetH + // ___ Results 'Get' Support |||||||||||||||||||||||||||||||||||||||||||||||||| @@ -244,6 +250,12 @@ getDECL(SYS_DELTA_PROC_BLOCKED) { return I->sys_hist.new.procs_blocked - I->sys_ SYSgetH(SYS_DELTA_PROC_CREATED, procs_created) getDECL(SYS_DELTA_PROC_RUNNING) { return I->sys_hist.new.procs_running - I->sys_hist.old.procs_running; } +#undef getDECL +#undef TIC_get +#undef SYS_get +#undef TICgetH +#undef SYSgetH + // ___ Controlling Table |||||||||||||||||||||||||||||||||||||||||||||||||||||| @@ -314,17 +326,7 @@ enum stat_item PROCPS_STAT_TIC_highest = PROCPS_STAT_TIC_DELTA_GUEST_NICE; enum stat_item PROCPS_STAT_logical_end = PROCPS_STAT_SYS_DELTA_PROC_RUNNING + 1; #undef setNAME -#undef setDECL -#undef TIC_set -#undef SYS_set -#undef TICsetH -#undef SYSsetH #undef getNAME -#undef getDECL -#undef TIC_get -#undef SYS_get -#undef TICgetH -#undef SYSgetH #undef RS #undef RG @@ -458,6 +460,7 @@ static int make_numa_hist ( ( and be careful, this libnuma call returns the highest node id in use, ) ( NOT an actual number of nodes - some of those 'slots' might be unused ) */ info->nodes.total = info->our_max_node() + 1; + if (!info->nodes.hist.n_alloc || !(info->nodes.total < info->nodes.hist.n_alloc)) { info->nodes.hist.n_alloc = info->nodes.total + NEWOLD_INCR; @@ -469,7 +472,7 @@ static int make_numa_hist ( // forget all of the prior node statistics & anticipate unassigned slots memset(info->nodes.hist.tics, 0, info->nodes.hist.n_alloc * sizeof(struct hist_tic)); nod_ptr = info->nodes.hist.tics; - for (i = 0; i < info->cpus.hist.n_alloc; i++) { + for (i = 0; i < info->nodes.total; i++) { nod_ptr->id = nod_ptr->numa_node = PROCPS_STAT_NODE_INVALID; ++nod_ptr; } @@ -808,6 +811,8 @@ static struct stat_stack *update_single_stack ( // ___ Public Functions ||||||||||||||||||||||||||||||||||||||||||||||||||||||| +// --- standard required functions -------------------------------------------- + /* * procps_stat_new: * @@ -922,6 +927,8 @@ PROCPS_EXPORT int procps_stat_unref ( } // end: procps_stat_unref +// --- variable interface functions ------------------------------------------- + PROCPS_EXPORT signed long long procps_stat_get ( struct procps_statinfo *info, enum stat_item item) @@ -930,6 +937,11 @@ PROCPS_EXPORT signed long long procps_stat_get ( time_t cur_secs; int rc; + if (info == NULL) + return -EINVAL; + if (item < 0 || item >= PROCPS_STAT_logical_end) + return -EINVAL; + /* no sense reading the stat with every call from a program like vmstat who chooses not to use the much more efficient 'select' function ... */ cur_secs = time(NULL); @@ -939,9 +951,7 @@ PROCPS_EXPORT signed long long procps_stat_get ( sav_secs = cur_secs; } - if (item < PROCPS_STAT_logical_end) - return Item_table[item].getsfunc(info); - return -EINVAL; + return Item_table[item].getsfunc(info); } // end: procps_stat_get diff --git a/proc/stat.h b/proc/stat.h index 7fa3f2c7..1ebf7949 100644 --- a/proc/stat.h +++ b/proc/stat.h @@ -60,7 +60,7 @@ enum stat_item { PROCPS_STAT_SYS_DELTA_INTERRUPTS, // s_int PROCPS_STAT_SYS_DELTA_PROC_BLOCKED, // s_int PROCPS_STAT_SYS_DELTA_PROC_CREATED, // s_int - PROCPS_STAT_SYS_DELTA_PROC_RUNNING, // s_int + PROCPS_STAT_SYS_DELTA_PROC_RUNNING // s_int }; enum stat_reap_type { @@ -71,9 +71,9 @@ enum stat_reap_type { struct stat_result { enum stat_item item; union { - signed int s_int; - signed long sl_int; - unsigned long ul_int; + signed int s_int; + signed long sl_int; + unsigned long ul_int; unsigned long long ull_int; } result; }; diff --git a/proc/vmstat.c b/proc/vmstat.c index d0536668..0881cce8 100644 --- a/proc/vmstat.c +++ b/proc/vmstat.c @@ -1358,6 +1358,8 @@ static struct stacks_extent *stacks_alloc ( // ___ Public Functions ||||||||||||||||||||||||||||||||||||||||||||||||||||||| +// --- standard required functions -------------------------------------------- + /* * procps_vmstat_new: * @@ -1424,6 +1426,8 @@ PROCPS_EXPORT int procps_vmstat_unref ( } // end: procps_vmstat_unref +// --- variable interface functions ------------------------------------------- + PROCPS_EXPORT signed long procps_vmstat_get ( struct procps_vmstat *info, enum vmstat_item item) @@ -1446,9 +1450,7 @@ PROCPS_EXPORT signed long procps_vmstat_get ( sav_secs = cur_secs; } - if (item < PROCPS_VMSTAT_logical_end) - return Item_table[item].getsfunc(info); - return -EINVAL; + return Item_table[item].getsfunc(info); } // end: procps_vmstat_get diff --git a/proc/vmstat.h b/proc/vmstat.h index 9b8ea5e9..618f77ac 100644 --- a/proc/vmstat.h +++ b/proc/vmstat.h @@ -275,8 +275,8 @@ enum vmstat_item { struct vmstat_result { enum vmstat_item item; union { - signed long sl_int; - unsigned long ul_int; + signed long sl_int; + unsigned long ul_int; } result; };