]> granicus.if.org Git - procps-ng/commitdiff
library; add more of my gratuitous changes, <STAT> api
authorJim Warner <james.warner@comcast.net>
Tue, 10 May 2016 05:00:00 +0000 (00:00 -0500)
committerCraig Small <csmall@enc.com.au>
Wed, 11 May 2016 21:51:10 +0000 (07:51 +1000)
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 <james.warner@comcast.net
proc/stat.c
proc/stat.h

index 4aa1a33038d0181021db67301aa751fbc964225c..2fc56fda38e6ba980bdd149c9095659eda61eb3d 100644 (file)
@@ -71,10 +71,9 @@ struct hist_tic {
 };
 
 struct stacks_extent {
-    struct stat_stack **stacks;
-    int ext_numitems;                  // includes 'logical_end' delimiter
     int ext_numstacks;
     struct stacks_extent *next;
+    struct stat_stack **stacks;
 };
 
 struct fetch_support {
@@ -176,12 +175,11 @@ SYS_hst(SYS_DELTA_PROC_CREATED,  s_int,    procs_created)
 SYS_hst(SYS_DELTA_PROC_RUNNING,  s_int,    procs_running)
 
 
+// ___ Controlling Table ||||||||||||||||||||||||||||||||||||||||||||||||||||||
+
 typedef void (*SET_t)(struct stat_result *, struct hist_sys *, struct hist_tic *);
 #define RS(e) (SET_t)setNAME(e)
 
-
-// ___ Controlling Table ||||||||||||||||||||||||||||||||||||||||||||||||||||||
-
         /*
          * Need it be said?
          * This table must be kept in the exact same order as
@@ -190,7 +188,6 @@ static struct {
     SET_t setsfunc;              // the actual result setting routine
 } Item_table[] = {
 /*  setsfunc
-
     ---------------------------  */
   { RS(noop),                    },
   { RS(extra),                   },
@@ -269,17 +266,14 @@ static inline void assign_results (
 
 
 static inline void cleanup_stack (
-        struct stat_result *p,
-        int depth)
+        struct stat_result *this)
 {
-    int i;
-
-    for (i = 0; i < depth; i++) {
-        if (p->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;
index f86c1ce3e6a13afb4d8e7d97f88da85549689d71..37a32bd6616e0b0351b6e6d46f2fc23969703a48 100644 (file)
  * 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 <proc/procps.h>
-
 __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 (