library: minimize the use of 'cleanup_stacks' routines

Some parts of our newlib implementation are the result
of functions which have been propagated from module to
module. In particular, those 'cleanup_stacks' routines
are all similar & likely originated in the <pids> api.

In that interface there was a need to free dynamically
acquired memory before the result structure was reused
to satisfy subsequent 'get', 'select' or 'reap' calls.
This, in turn, led to a concept of 'dirty' stacks with
the need to call one of two 'cleanup_stack' functions.

None of the remaining interfaces deal with such memory
yet they each had their own 'cleanup_stack' functions.
Those functions were responsible for resetting each of
the result unions to zero, excluding any 'noop' items.

The bottom line is that for all interfaces, repetitive
calls would require iterating through the stack(s) two
separate times: once to 'cleanup' another to 'assign'.

With this commit we will reduce iterations to just the
'assign' routine. A reset to zero will be accomplished
in the 'extra' item set routine (which is the only one
actually requiring any reset). All other items will be
reinitialized automatically by a new current set value
or upon reallocation when an items compliment changes.

In the <pids> interface, any freeing of dynamic memory
could have been accomplished by adding that 'freefunc'
check to the 'assign' function. However, that requires
an Item_table test with every item. Instead, we'll now
satisfy such needs as the very first step in those set
functions responsible for dynamically acquired memory.

[ the <pids> api retains 2 'cleanup_stack' functions ]
[ to accommodate stack(s) 'reset' & to serve 'unref' ]

Lastly, all the 'itemize_stack' functions were tweaked
by eliminating an unnecessary initialization of result
unions. That objective was already accomplished by the
calloc() in a 'stacks_alloc' function or the remaining
'cleanup_stack' routine found in the <pids> interface.

Signed-off-by: Jim Warner <james.warner@comcast.net>
This commit is contained in:
Jim Warner 2019-05-22 00:00:00 -05:00 committed by Craig Small
parent 22a3bcbd6d
commit 40883a0b85
6 changed files with 33 additions and 221 deletions

View File

@ -85,7 +85,6 @@ struct ext_support {
int numitems; // includes 'logical_end' delimiter
enum diskstats_item *items; // includes 'logical_end' delimiter
struct stacks_extent *extents; // anchor for these extents
int dirty_stacks;
};
struct fetch_support {
@ -122,7 +121,7 @@ struct diskstats_info {
#define HST_set(e,t,x) setDECL(e) { R->result. t = ( N->new . x - N->old. x ); }
setDECL(noop) { (void)R; (void)N; }
setDECL(extra) { (void)R; (void)N; }
setDECL(extra) { (void)N; R->result.ul_int = 0; }
DEV_set(DISKSTATS_NAME, str, name)
DEV_set(DISKSTATS_TYPE, s_int, type)
@ -429,34 +428,6 @@ static inline void diskstats_assign_results (
} // end: diskstats_assign_results
static inline void diskstats_cleanup_stack (
struct diskstats_result *this)
{
for (;;) {
if (this->item >= DISKSTATS_logical_end)
break;
if (this->item > DISKSTATS_noop)
this->result.ul_int = 0;
++this;
}
} // end: diskstats_cleanup_stack
static inline void diskstats_cleanup_stacks_all (
struct ext_support *this)
{
struct stacks_extent *ext = this->extents;
int i;
while (ext) {
for (i = 0; ext->stacks[i]; i++)
diskstats_cleanup_stack(ext->stacks[i]->head);
ext = ext->next;
};
this->dirty_stacks = 0;
} // end: diskstats_cleanup_stacks_all
static void diskstats_extents_free_all (
struct ext_support *this)
{
@ -478,7 +449,6 @@ static inline struct diskstats_result *diskstats_itemize_stack (
for (i = 0; i < depth; i++) {
p->item = items[i];
p->result.ul_int = 0;
++p;
}
return p_sav;
@ -653,7 +623,6 @@ static int diskstats_stacks_fetch (
return -1; // here, errno was set to ENOMEM
memcpy(info->fetch.anchor, ext->stacks, sizeof(void *) * n_alloc);
}
diskstats_cleanup_stacks_all(&info->fetch_ext);
// iterate stuff --------------------------------------
n_inuse = 0;
@ -872,14 +841,10 @@ PROCPS_EXPORT struct diskstats_reap *procps_diskstats_reap (
return NULL; // here, errno may be overridden with ENOMEM
errno = 0;
if (info->fetch_ext.dirty_stacks)
diskstats_cleanup_stacks_all(&info->fetch_ext);
if (diskstats_read_failed(info))
return NULL;
if (0 > diskstats_stacks_fetch(info))
return NULL;
info->fetch_ext.dirty_stacks = 1;
return &info->fetch.results;
} // end: procps_diskstats_reap
@ -911,9 +876,6 @@ PROCPS_EXPORT struct diskstats_stack *procps_diskstats_select (
&& (!diskstats_stacks_alloc(&info->select_ext, 1)))
return NULL;
if (info->select_ext.dirty_stacks)
diskstats_cleanup_stacks_all(&info->select_ext);
if (diskstats_read_failed(info))
return NULL;
if (!(node = node_get(info, name))) {
@ -922,7 +884,6 @@ PROCPS_EXPORT struct diskstats_stack *procps_diskstats_select (
}
diskstats_assign_results(info->select_ext.extents->stacks[0], node);
info->select_ext.dirty_stacks = 1;
return info->select_ext.extents->stacks[0];
} // end: procps_diskstats_select

View File

@ -116,7 +116,6 @@ struct stacks_extent {
struct meminfo_info {
int refcount;
int meminfo_fd;
int dirty_stacks;
struct mem_hist hist;
int numitems;
enum meminfo_item *items;
@ -138,7 +137,7 @@ struct meminfo_info {
#define HST_set(e,t,x) setDECL(e) { R->result. t = ( H->new . x - H->old. x ); }
setDECL(noop) { (void)R; (void)H; }
setDECL(extra) { (void)R; (void)H; }
setDECL(extra) { (void)H; R->result.ul_int = 0; }
MEM_set(MEM_ACTIVE, ul_int, Active)
MEM_set(MEM_ACTIVE_ANON, ul_int, Active_anon)
@ -457,34 +456,6 @@ static inline void meminfo_assign_results (
} // end: meminfo_assign_results
static inline void meminfo_cleanup_stack (
struct meminfo_result *this)
{
for (;;) {
if (this->item >= MEMINFO_logical_end)
break;
if (this->item > MEMINFO_noop)
this->result.ul_int = 0;
++this;
}
} // end: meminfo_cleanup_stack
static inline void meminfo_cleanup_stacks_all (
struct meminfo_info *info)
{
struct stacks_extent *ext = info->extents;
int i;
while (ext) {
for (i = 0; ext->stacks[i]; i++)
meminfo_cleanup_stack(ext->stacks[i]->head);
ext = ext->next;
};
info->dirty_stacks = 0;
} // end: meminfo_cleanup_stacks_all
static void meminfo_extents_free_all (
struct meminfo_info *info)
{
@ -506,7 +477,6 @@ static inline struct meminfo_result *meminfo_itemize_stack (
for (i = 0; i < depth; i++) {
p->item = items[i];
p->result.ul_int = 0;
++p;
}
return p_sav;
@ -939,13 +909,9 @@ PROCPS_EXPORT struct meminfo_stack *procps_meminfo_select (
&& (!meminfo_stacks_alloc(info, 1)))
return NULL;
if (info->dirty_stacks)
meminfo_cleanup_stacks_all(info);
if (meminfo_read_failed(info))
return NULL;
meminfo_assign_results(info->extents->stacks[0], &info->hist);
info->dirty_stacks = 1;
return info->extents->stacks[0];
} // end: procps_meminfo_select

View File

@ -78,7 +78,6 @@ struct pids_info {
struct fetch_support fetch; // support for procps_pids_reap & select
int history_yes; // need historical data
struct history_info *hist; // pointer to historical support data
int dirty_stacks; // stacks results need attention
proc_t*(*read_something)(PROCTAB*, proc_t*); // readproc/readeither via which
unsigned pgs2k_shift; // to convert some proc vaules
unsigned oldflags; // the old library PROC_FILL flagss
@ -92,6 +91,19 @@ struct pids_info {
};
// ___ Free Storage Support |||||||||||||||||||||||||||||||||||||||||||||||||||
#define freNAME(t) free_pids_ ## t
static void freNAME(str) (struct pids_result *R) {
if (R->result.str) free(R->result.str);
}
static void freNAME(strv) (struct pids_result *R) {
if (R->result.strv && *R->result.strv) free(*R->result.strv);
}
// ___ Results 'Set' Support ||||||||||||||||||||||||||||||||||||||||||||||||||
#define setNAME(e) set_pids_ ## e
@ -103,6 +115,7 @@ struct pids_info {
R->result. t = (long)(P-> x) << I -> pgs2k_shift; }
/* strdup of a static char array */
#define DUP_set(e,x) setDECL(e) { \
freNAME(str)(R); \
if (!(R->result.str = strdup(P-> x))) I->seterr = 1; }
/* regular assignment copy */
#define REG_set(e,t,x) setDECL(e) { \
@ -110,19 +123,22 @@ struct pids_info {
/* take ownership of a normal single string if possible, else return
some sort of hint that they duplicated this char * item ... */
#define STR_set(e,x) setDECL(e) { \
freNAME(str)(R); \
if (NULL != P-> x) { R->result.str = P-> x; P-> x = NULL; } \
else { R->result.str = strdup("[ duplicate " STRINGIFY(e) " ]"); \
if (!R->result.str) I->seterr = 1; } }
/* take ownership of true vectorized strings if possible, else return
some sort of hint that they duplicated this char ** item ... */
#define VEC_set(e,x) setDECL(e) { \
freNAME(strv)(R); \
if (NULL != P-> x) { R->result.strv = P-> x; P-> x = NULL; } \
else { R->result.strv = vectorize_this_str("[ duplicate " STRINGIFY(e) " ]"); \
if (!R->result.str) I->seterr = 1; } }
setDECL(noop) { (void)I; (void)R; (void)P; return; }
setDECL(extra) { (void)I; (void)R; (void)P; return; }
setDECL(noop) { (void)I; (void)R; (void)P; }
setDECL(extra) { (void)I; (void)P; R->result.ull_int = 0; }
REG_set(ADDR_END_CODE, ul_int, end_code)
REG_set(ADDR_KSTK_EIP, ul_int, kstk_eip)
REG_set(ADDR_KSTK_ESP, ul_int, kstk_esp)
@ -226,8 +242,8 @@ setDECL(TIME_ALL) { R->result.ull_int = (P->utime + P->stime) / I->hertz;
setDECL(TIME_ELAPSED) { unsigned long long t = P->start_time / I->hertz; R->result.ull_int = I->boot_seconds >= t ? (I->boot_seconds - t) : 0; }
REG_set(TIME_START, ull_int, start_time)
REG_set(TTY, s_int, tty)
setDECL(TTY_NAME) { char buf[64]; dev_to_tty(buf, sizeof(buf), P->tty, P->tid, ABBREV_DEV); if (!(R->result.str = strdup(buf))) I->seterr = 1; }
setDECL(TTY_NUMBER) { char buf[64]; dev_to_tty(buf, sizeof(buf), P->tty, P->tid, ABBREV_DEV|ABBREV_TTY|ABBREV_PTS); if (!(R->result.str = strdup(buf))) I->seterr = 1; }
setDECL(TTY_NAME) { char buf[64]; freNAME(str)(R); dev_to_tty(buf, sizeof(buf), P->tty, P->tid, ABBREV_DEV); if (!(R->result.str = strdup(buf))) I->seterr = 1; }
setDECL(TTY_NUMBER) { char buf[64]; freNAME(str)(R); dev_to_tty(buf, sizeof(buf), P->tty, P->tid, ABBREV_DEV|ABBREV_TTY|ABBREV_PTS); if (!(R->result.str = strdup(buf))) I->seterr = 1; }
REG_set(VM_DATA, ul_int, vm_data)
REG_set(VM_EXE, ul_int, vm_exe)
REG_set(VM_LIB, ul_int, vm_lib)
@ -241,7 +257,7 @@ REG_set(VM_STACK, ul_int, vm_stack)
REG_set(VM_SWAP, ul_int, vm_swap)
setDECL(VM_USED) { (void)I; R->result.ul_int = P->vm_swap + P->vm_rss; }
REG_set(VSIZE_PGS, ul_int, vsize)
setDECL(WCHAN_NAME) { if (!(R->result.str = strdup(lookup_wchan(P->tid)))) I->seterr = 1;; }
setDECL(WCHAN_NAME) { freNAME(str)(R); if (!(R->result.str = strdup(lookup_wchan(P->tid)))) I->seterr = 1;; }
#undef setDECL
#undef CVT_set
@ -251,19 +267,6 @@ setDECL(WCHAN_NAME) { if (!(R->result.str = strdup(lookup_wchan(P->tid)))) I
#undef VEC_set
// ___ Free Storage Support |||||||||||||||||||||||||||||||||||||||||||||||||||
#define freNAME(t) free_pids_ ## t
static void freNAME(str) (struct pids_result *R) {
if (R->result.str) free(R->result.str);
}
static void freNAME(strv) (struct pids_result *R) {
if (R->result.strv && *R->result.strv) free(*R->result.strv);
}
// ___ Sorting Support ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
struct sort_parms {
@ -777,7 +780,6 @@ static inline void pids_cleanup_stack (
break;
if (Item_table[item].freefunc)
Item_table[item].freefunc(this);
if (item > PIDS_noop)
this->result.ull_int = 0;
++this;
}
@ -795,7 +797,6 @@ static inline void pids_cleanup_stacks_all (
pids_cleanup_stack(ext->stacks[i]->head);
ext = ext->next;
};
info->dirty_stacks = 0;
} // end: pids_cleanup_stacks_all
@ -836,7 +837,6 @@ static inline struct pids_result *pids_itemize_stack (
for (i = 0; i < depth; i++) {
p->item = items[i];
p->result.ull_int = 0;
++p;
}
return p_sav;
@ -854,7 +854,6 @@ static void pids_itemize_stacks_all (
pids_itemize_stack(ext->stacks[i]->head, info->curitems, info->items);
ext = ext->next;
};
info->dirty_stacks = 0;
} // end: pids_itemize_stacks_all
@ -1052,7 +1051,6 @@ static int pids_stacks_fetch (
memcpy(info->fetch.anchor, ext->stacks, sizeof(void *) * STACKS_INCR);
n_alloc = STACKS_INCR;
}
pids_cleanup_stacks_all(info);
pids_toggle_history(info);
memset(&info->fetch.counts, 0, sizeof(struct pids_counts));
@ -1089,7 +1087,6 @@ static int pids_stacks_fetch (
}
memcpy(info->fetch.results.stacks, info->fetch.anchor, sizeof(void *) * n_inuse);
info->fetch.results.stacks[n_inuse] = NULL;
info->dirty_stacks = 1;
return n_inuse; // callers beware, this might be zero !
#undef n_alloc
@ -1304,8 +1301,6 @@ fresh_start:
}
errno = 0;
pids_cleanup_stack(info->get_ext->stacks[0]->head);
if (NULL == info->read_something(info->get_PT, &task))
return NULL;
if (!pids_assign_results(info, info->get_ext->stacks[0], &task))
@ -1360,7 +1355,6 @@ PROCPS_EXPORT int procps_pids_reset (
if (pids_items_check_failed(newitems, newnumitems))
return -EINVAL;
if (info->dirty_stacks)
pids_cleanup_stacks_all(info);
/* shame on this caller, they didn't change anything. and unless they have
@ -1394,6 +1388,7 @@ PROCPS_EXPORT int procps_pids_reset (
// account for above PIDS_logical_end
info->curitems = newnumitems + 1;
// if extents freed above, pids_stacks_alloc() will itemize ...
pids_itemize_stacks_all(info);
pids_libflags_set(info);

View File

@ -107,7 +107,6 @@ struct ext_support {
enum slabinfo_item lowest; // range of allowable enums
enum slabinfo_item highest;
#endif
int dirty_stacks;
};
struct fetch_support {
@ -146,7 +145,7 @@ struct slabinfo_info {
#define HST_set(e,t,x) setDECL(e) { (void)N; R->result. t = (signed long)S->new . x - S->old. x; }
setDECL(noop) { (void)R; (void)S; (void)N; }
setDECL(extra) { (void)R; (void)S; (void)N; }
setDECL(extra) { (void)S; (void)N; R->result.ul_int = 0; }
NOD_set(SLAB_NAME, str, name)
NOD_set(SLAB_NUM_OBJS, u_int, nr_objs)
@ -512,34 +511,6 @@ static inline void slabinfo_assign_results (
} // end: slabinfo_assign_results
static inline void slabinfo_cleanup_stack (
struct slabinfo_result *this)
{
for (;;) {
if (this->item >= SLABINFO_logical_end)
break;
if (this->item > SLABINFO_noop)
this->result.ul_int = 0;
++this;
}
} // end: slabinfo_cleanup_stack
static inline void slabinfo_cleanup_stacks_all (
struct ext_support *this)
{
struct stacks_extent *ext = this->extents;
int i;
while (ext) {
for (i = 0; ext->stacks[i]; i++)
slabinfo_cleanup_stack(ext->stacks[i]->head);
ext = ext->next;
};
this->dirty_stacks = 0;
} // end: slabinfo_cleanup_stacks_all
static void slabinfo_extents_free_all (
struct ext_support *this)
{
@ -561,7 +532,6 @@ static inline struct slabinfo_result *slabinfo_itemize_stack (
for (i = 0; i < depth; i++) {
p->item = items[i];
p->result.ul_int = 0;
++p;
}
return p_sav;
@ -684,7 +654,6 @@ static int slabinfo_stacks_fetch (
return -1; // here, errno was set to ENOMEM
memcpy(info->fetch.anchor, ext->stacks, sizeof(void *) * n_alloc);
}
slabinfo_cleanup_stacks_all(&info->fetch_ext);
// iterate stuff --------------------------------------
n_inuse = 0;
@ -897,14 +866,10 @@ PROCPS_EXPORT struct slabinfo_reap *procps_slabinfo_reap (
return NULL; // here, errno may be overridden with ENOMEM
errno = 0;
if (info->fetch_ext.dirty_stacks)
slabinfo_cleanup_stacks_all(&info->fetch_ext);
if (slabinfo_read_failed(info))
return NULL;
if (0 > slabinfo_stacks_fetch(info))
return NULL;
info->fetch_ext.dirty_stacks = 1;
return &info->fetch.results;
} // end: procps_slabinfo_reap
@ -933,13 +898,9 @@ PROCPS_EXPORT struct slabinfo_stack *procps_slabinfo_select (
&& (!slabinfo_stacks_alloc(&info->select_ext, 1)))
return NULL;
if (info->select_ext.dirty_stacks)
slabinfo_cleanup_stacks_all(&info->select_ext);
if (slabinfo_read_failed(info))
return NULL;
slabinfo_assign_results(info->select_ext.extents->stacks[0], &info->slabs, &info->nul_node);
info->select_ext.dirty_stacks = 1;
return info->select_ext.extents->stacks[0];
} // end: procps_slabinfo_select

View File

@ -88,7 +88,6 @@ struct item_support {
struct ext_support {
struct item_support *items; // how these stacks are configured
struct stacks_extent *extents; // anchor for these extents
int dirty_stacks;
};
struct tic_support {
@ -145,7 +144,7 @@ struct stat_info {
if (R->result. t < 0) R->result. t = 0; }
setDECL(noop) { (void)R; (void)S; (void)T; }
setDECL(extra) { (void)R; (void)S; (void)T; }
setDECL(extra) { (void)S; (void)T; R->result.ull_int = 0; }
setDECL(TIC_ID) { (void)S; R->result.s_int = T->id; }
setDECL(TIC_NUMA_NODE) { (void)S; R->result.s_int = T->numa_node; }
@ -365,34 +364,6 @@ static inline void stat_assign_results (
} // end: stat_assign_results
static inline void stat_cleanup_stack (
struct stat_result *this)
{
for (;;) {
if (this->item >= STAT_logical_end)
break;
if (this->item > STAT_noop)
this->result.ull_int = 0;
++this;
}
} // end: stat_cleanup_stack
static inline void stat_cleanup_stacks_all (
struct ext_support *this)
{
struct stacks_extent *ext = this->extents;
int i;
while (ext) {
for (i = 0; ext->stacks[i]; i++)
stat_cleanup_stack(ext->stacks[i]->head);
ext = ext->next;
};
this->dirty_stacks = 0;
} // end: stat_cleanup_stacks_all
static inline void stat_derive_unique (
struct hist_tic *this)
{
@ -449,7 +420,6 @@ static inline struct stat_result *stat_itemize_stack (
for (i = 0; i < depth; i++) {
p->item = items[i];
p->result.ull_int = 0;
++p;
}
return p_sav;
@ -762,8 +732,6 @@ static int stat_stacks_fetch (
return -1; // here, errno was set to ENOMEM
memcpy(this->anchor, ext->stacks, sizeof(void *) * n_alloc);
}
if (this->fetch.dirty_stacks)
stat_cleanup_stacks_all(&this->fetch);
// iterate stuff --------------------------------------
for (i = 0; i < n_inuse; i++) {
@ -790,7 +758,6 @@ static int stat_stacks_fetch (
memcpy(this->result.stacks, this->anchor, sizeof(void *) * i);
this->result.stacks[i] = NULL;
this->result.total = i;
this->fetch.dirty_stacks = 1;
// callers beware, this might be zero (maybe no libnuma.so) ...
return this->result.total;
@ -832,11 +799,7 @@ static struct stat_stack *stat_update_single_stack (
&& !(stat_stacks_alloc(this, 1)))
return NULL;
if (this->dirty_stacks)
stat_cleanup_stacks_all(this);
stat_assign_results(this->extents->stacks[0], &info->sys_hist, &info->cpu_hist);
this->dirty_stacks = 1;
return this->extents->stacks[0];
} // end: stat_update_single_stack

View File

@ -203,7 +203,6 @@ struct stacks_extent {
struct vmstat_info {
int refcount;
int vmstat_fd;
int dirty_stacks;
struct vmstat_hist hist;
int numitems;
enum vmstat_item *items;
@ -225,7 +224,7 @@ struct vmstat_info {
#define HST_set(e,x) setDECL(e) { R->result.sl_int = ( H->new . x - H->old. x ); }
setDECL(noop) { (void)R; (void)H; }
setDECL(extra) { (void)R; (void)H; }
setDECL(extra) { (void)H; R->result.ul_int = 0; }
REG_set(ALLOCSTALL_DMA, allocstall_dma)
REG_set(ALLOCSTALL_DMA32, allocstall_dma32)
@ -844,34 +843,6 @@ static inline void vmstat_assign_results (
} // end: vmstat_assign_results
static inline void vmstat_cleanup_stack (
struct vmstat_result *this)
{
for (;;) {
if (this->item >= VMSTAT_logical_end)
break;
if (this->item > VMSTAT_noop)
this->result.ul_int = 0;
++this;
}
} // end: vmstat_cleanup_stack
static inline void vmstat_cleanup_stacks_all (
struct vmstat_info *info)
{
struct stacks_extent *ext = info->extents;
int i;
while (ext) {
for (i = 0; ext->stacks[i]; i++)
vmstat_cleanup_stack(ext->stacks[i]->head);
ext = ext->next;
};
info->dirty_stacks = 0;
} // end: vmstat_cleanup_stacks_all
static void vmstat_extents_free_all (
struct vmstat_info *info)
{
@ -893,7 +864,6 @@ static inline struct vmstat_result *vmstat_itemize_stack (
for (i = 0; i < depth; i++) {
p->item = items[i];
p->result.ul_int = 0;
++p;
}
return p_sav;
@ -1369,13 +1339,9 @@ PROCPS_EXPORT struct vmstat_stack *procps_vmstat_select (
&& (!vmstat_stacks_alloc(info, 1)))
return NULL;
if (info->dirty_stacks)
vmstat_cleanup_stacks_all(info);
if (vmstat_read_failed(info))
return NULL;
vmstat_assign_results(info->extents->stacks[0], &info->hist);
info->dirty_stacks = 1;
return info->extents->stacks[0];
} // end: procps_vmstat_select