From 1417ba56b57f3697fc6dcd08a12e29541439e5d1 Mon Sep 17 00:00:00 2001 From: Jim Warner Date: Tue, 10 May 2016 00:00:00 -0500 Subject: [PATCH] library; add more of my gratuitous changes, api This commit is just my usual tweaking after an initial submission, once the dust has settled & some more desk checking was performed. Here are the changes included: . get function need not check the 'stat_was_read' flag ( first time in save_sec == 0 so branch always taken ) . some table logic appeared outside the proper section . cleanup_stack's loop made to work like assign_result . eliminated an include of procps.h from stat.h header . removed 'ext_numitems' from the stacks_extent struct . changed the alignment of three prototypes for stat.h . reorganized the pointers for 'stacks_extents' struct . improved types indentation in header for readability . lastly, some really gratuitous changes made to align - comments within that private 'stacks_alloc' function Signed-off-by: Jim Warner item >= PROCPS_STAT_logical_end) + for (;;) { + if (this->item >= PROCPS_STAT_logical_end) break; - if (p->item > PROCPS_STAT_noop) - p->result.ull_int = 0; - ++p; + if (this->item > PROCPS_STAT_noop) + this->result.ull_int = 0; + ++this; } } // end: cleanup_stack @@ -292,7 +286,7 @@ static inline void cleanup_stacks_all ( while (ext) { for (i = 0; ext->stacks[i]; i++) - cleanup_stack(ext->stacks[i]->head, this->numitems); + cleanup_stack(ext->stacks[i]->head); ext = ext->next; }; this->dirty_stacks = 0; @@ -587,27 +581,27 @@ static struct stacks_extent *stacks_alloc ( if (maxstacks < 1) return NULL; - vect_size = sizeof(void *) * maxstacks; // address vectors themselves - vect_size += sizeof(void *); // plus NULL delimiter - head_size = sizeof(struct stat_stack); // a head struct - list_size = sizeof(struct stat_result) * this->numitems; // a results stack - blob_size = sizeof(struct stacks_extent); // the extent anchor itself - blob_size += vect_size; // all vectors + delim - blob_size += head_size * maxstacks; // all head structs - blob_size += list_size * maxstacks; // all results stacks - - /* note: all memory is allocated in a single blob, facilitating a later free(). - as a minimum, it's important that the result structures themselves always be - contiguous for each stack since they're accessed through relative position). */ + vect_size = sizeof(void *) * maxstacks; // size of the addr vectors | + vect_size += sizeof(void *); // plus NULL addr delimiter | + head_size = sizeof(struct stat_stack); // size of that head struct | + list_size = sizeof(struct stat_result) * this->numitems; // any single results stack | + blob_size = sizeof(struct stacks_extent); // the extent anchor itself | + blob_size += vect_size; // plus room for addr vects | + blob_size += head_size * maxstacks; // plus room for head thing | + blob_size += list_size * maxstacks; // plus room for our stacks | + + /* note: all of our memory is allocated in a single blob, facilitating a later free(). | + as a minimum, it is important that the result structures themselves always be | + contiguous for every stack since they are accessed through relative position. | */ if (NULL == (p_blob = calloc(1, blob_size))) return NULL; - p_blob->next = this->extents; - this->extents = p_blob; - p_blob->stacks = (void *)p_blob + sizeof(struct stacks_extent); - p_vect = p_blob->stacks; - v_head = (void *)p_vect + vect_size; - v_list = v_head + (head_size * maxstacks); + p_blob->next = this->extents; // push this extent onto... | + this->extents = p_blob; // ...some existing extents | + p_vect = (void *)p_blob + sizeof(struct stacks_extent); // prime our vector pointer | + p_blob->stacks = p_vect; // set actual vectors start | + v_head = (void *)p_vect + vect_size; // prime head pointer start | + v_list = v_head + (head_size * maxstacks); // prime our stacks pointer | for (i = 0; i < maxstacks; i++) { p_head = (struct stat_stack *)v_head; @@ -616,7 +610,6 @@ static struct stacks_extent *stacks_alloc ( v_list += list_size; v_head += head_size; } - p_blob->ext_numitems = this->numitems; p_blob->ext_numstacks = maxstacks; return p_blob; } // end: stacks_alloc @@ -849,7 +842,7 @@ PROCPS_EXPORT signed long long procps_stat_get ( /* 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); - if (!info->stat_was_read || 1 <= cur_secs - sav_secs) { + if (1 <= cur_secs - sav_secs) { if ((rc = read_stat_failed(info))) return rc; sav_secs = cur_secs; diff --git a/proc/stat.h b/proc/stat.h index f86c1ce3..37a32bd6 100644 --- a/proc/stat.h +++ b/proc/stat.h @@ -15,19 +15,18 @@ * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ + #ifndef PROC_STAT_H #define PROC_STAT_H -#include - __BEGIN_DECLS enum stat_item { PROCPS_STAT_noop, // ( never altered ) PROCPS_STAT_extra, // ( reset to zero ) - PROCPS_STAT_TIC_ID, // s_int - PROCPS_STAT_TIC_NUMA_NODE, // s_int + PROCPS_STAT_TIC_ID, // s_int + PROCPS_STAT_TIC_NUMA_NODE, // s_int PROCPS_STAT_TIC_USER, // ull_int PROCPS_STAT_TIC_NICE, // ull_int PROCPS_STAT_TIC_SYSTEM, // ull_int @@ -38,28 +37,28 @@ enum stat_item { PROCPS_STAT_TIC_STOLEN, // ull_int PROCPS_STAT_TIC_GUEST, // ull_int PROCPS_STAT_TIC_GUEST_NICE, // ull_int - PROCPS_STAT_TIC_DELTA_USER, // sl_int - PROCPS_STAT_TIC_DELTA_NICE, // sl_int - PROCPS_STAT_TIC_DELTA_SYSTEM, // sl_int - PROCPS_STAT_TIC_DELTA_IDLE, // sl_int - PROCPS_STAT_TIC_DELTA_IOWAIT, // sl_int - PROCPS_STAT_TIC_DELTA_IRQ, // sl_int - PROCPS_STAT_TIC_DELTA_SOFTIRQ, // sl_int - PROCPS_STAT_TIC_DELTA_STOLEN, // sl_int - PROCPS_STAT_TIC_DELTA_GUEST, // sl_int - PROCPS_STAT_TIC_DELTA_GUEST_NICE, // sl_int - - PROCPS_STAT_SYS_CTX_SWITCHES, // ul_int - PROCPS_STAT_SYS_INTERRUPTS, // ul_int - PROCPS_STAT_SYS_PROC_BLOCKED, // ul_int - PROCPS_STAT_SYS_PROC_CREATED, // ul_int - PROCPS_STAT_SYS_PROC_RUNNING, // ul_int - PROCPS_STAT_SYS_TIME_OF_BOOT, // ul_int - PROCPS_STAT_SYS_DELTA_CTX_SWITCHES, // s_int - 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_TIC_DELTA_USER, // sl_int + PROCPS_STAT_TIC_DELTA_NICE, // sl_int + PROCPS_STAT_TIC_DELTA_SYSTEM, // sl_int + PROCPS_STAT_TIC_DELTA_IDLE, // sl_int + PROCPS_STAT_TIC_DELTA_IOWAIT, // sl_int + PROCPS_STAT_TIC_DELTA_IRQ, // sl_int + PROCPS_STAT_TIC_DELTA_SOFTIRQ, // sl_int + PROCPS_STAT_TIC_DELTA_STOLEN, // sl_int + PROCPS_STAT_TIC_DELTA_GUEST, // sl_int + PROCPS_STAT_TIC_DELTA_GUEST_NICE, // sl_int + + PROCPS_STAT_SYS_CTX_SWITCHES, // ul_int + PROCPS_STAT_SYS_INTERRUPTS, // ul_int + PROCPS_STAT_SYS_PROC_BLOCKED, // ul_int + PROCPS_STAT_SYS_PROC_CREATED, // ul_int + PROCPS_STAT_SYS_PROC_RUNNING, // ul_int + PROCPS_STAT_SYS_TIME_OF_BOOT, // ul_int + PROCPS_STAT_SYS_DELTA_CTX_SWITCHES, // s_int + 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 }; enum stat_reap_type { @@ -70,10 +69,10 @@ enum stat_reap_type { struct stat_result { enum stat_item item; union { - int s_int; - long sl_int; - unsigned long ul_int; - unsigned long long ull_int; + signed int s_int; + signed long sl_int; + unsigned long ul_int; + unsigned long long ull_int; } result; }; @@ -102,8 +101,8 @@ struct stat_reaped { struct procps_statinfo; -int procps_stat_new (struct procps_statinfo **info); -int procps_stat_ref (struct procps_statinfo *info); +int procps_stat_new (struct procps_statinfo **info); +int procps_stat_ref (struct procps_statinfo *info); int procps_stat_unref (struct procps_statinfo **info); signed long long procps_stat_get ( -- 2.40.0