library: add sequential access to that pids collection

To ease the transition to the new interface, for other
than that top program, individual read provisions have
been added to the <proc/pids.h> API. This represents a
refinement of a position stated in a post noted below.

Reference(s):
http://www.freelists.org/post/procps/newlib-ps-fix

Signed-off-by: Jim Warner <james.warner@comcast.net>
This commit is contained in:
Jim Warner 2015-08-26 00:00:00 -05:00 committed by Craig Small
parent def5858810
commit 9ecc0c8473
3 changed files with 71 additions and 2 deletions

View File

@ -43,6 +43,9 @@ global:
procps_meminfo_stack_fill; procps_meminfo_stack_fill;
procps_meminfo_stack_alloc; procps_meminfo_stack_alloc;
procps_pids_new; procps_pids_new;
procps_pids_read_next;
procps_pids_read_open;
procps_pids_read_shut;
procps_pids_reap; procps_pids_reap;
procps_pids_ref; procps_pids_ref;
procps_pids_reset; procps_pids_reset;

View File

@ -47,6 +47,8 @@
#define FILL_ID_MAX 255 // upper limit for pid/uid fills #define FILL_ID_MAX 255 // upper limit for pid/uid fills
#define MEMORY_INCR 128 // amt by which allocations grow #define MEMORY_INCR 128 // amt by which allocations grow
#define READS_BEGUN (info->read) // a read is in progress
enum pids_item PROCPS_PIDS_logical_end = PROCPS_PIDS_noop + 1; enum pids_item PROCPS_PIDS_logical_end = PROCPS_PIDS_noop + 1;
enum pids_item PROCPS_PIDS_physical_end = PROCPS_PIDS_noop + 2; enum pids_item PROCPS_PIDS_physical_end = PROCPS_PIDS_noop + 2;
@ -70,6 +72,8 @@ struct procps_pidsinfo {
int history_yes; // need historical data int history_yes; // need historical data
struct history_info *hist; // pointer to historical support data struct history_info *hist; // pointer to historical support data
int dirty_stacks; // extents need dynamic storage clean int dirty_stacks; // extents need dynamic storage clean
struct stacks_extent *read; // an extent used for active reads
proc_t*(*read_something)(PROCTAB*, proc_t*); // readproc/readeither via which
unsigned pgs2k_shift; // to convert some proc vaules unsigned pgs2k_shift; // to convert some proc vaules
unsigned flags; // the old library PROC_FILL flagss unsigned flags; // the old library PROC_FILL flagss
PROCTAB *PT; // the old library essential interface PROCTAB *PT; // the old library essential interface
@ -942,6 +946,58 @@ PROCPS_EXPORT int procps_pids_new (
} // end: procps_pids_new } // end: procps_pids_new
PROCPS_EXPORT struct pids_stack *procps_pids_read_next (
struct procps_pidsinfo *info)
{
static proc_t task; // static for initial zeroes + later dynamic free(s)
if (info == NULL || ! READS_BEGUN)
return NULL;
if (info->dirty_stacks) {
cleanup_stack(info->read->stacks[0]->head, info->maxitems);
info->dirty_stacks = 0;
}
if (NULL == info->read_something(info->PT, &task))
return NULL;
assign_results(info, info->read->stacks[0], &task);
return info->read->stacks[0];
} // end: procps_pids_read_next
PROCPS_EXPORT int procps_pids_read_open (
struct procps_pidsinfo *info,
enum pids_reap_type which)
{
if (info == NULL || READS_BEGUN)
return -EINVAL;
if (!info->maxitems && !info->curitems)
return -EINVAL;
if (which != PROCPS_REAP_TASKS_ONLY && which != PROCPS_REAP_THREADS_TOO)
return -EINVAL;
if (!(info->read = (struct stacks_extent *)procps_pids_stacks_alloc(info, 1)))
return -ENOMEM;
if (!oldproc_open(info, 0))
return -1;
info->read_something = which ? readeither : readproc;
return 0;
} // end: procps_pids_read_open
PROCPS_EXPORT int procps_pids_read_shut (
struct procps_pidsinfo *info)
{
int rc;
if (info == NULL || ! READS_BEGUN)
return -EINVAL;
oldproc_close(info);
rc = free_extent(info, info->read);
info->read = NULL;
return rc;
} // end: procps_pids_read_shut
/* procps_pids_reap(): /* procps_pids_reap():
* *
* Harvest all the available tasks/threads and provide the result * Harvest all the available tasks/threads and provide the result
@ -960,7 +1016,7 @@ PROCPS_EXPORT struct pids_reap *procps_pids_reap (
struct pids_stacks *ext; struct pids_stacks *ext;
int n_save = n_alloc; int n_save = n_alloc;
if (info == NULL) if (info == NULL || READS_BEGUN)
return NULL; return NULL;
if (!info->maxitems && !info->curitems) if (!info->maxitems && !info->curitems)
return NULL; return NULL;
@ -1162,7 +1218,7 @@ PROCPS_EXPORT struct pids_counts *procps_pids_stacks_fill (
unsigned ids[FILL_ID_MAX + 1]; unsigned ids[FILL_ID_MAX + 1];
int i; int i;
if (info == NULL || these == NULL) if (info == NULL || these == NULL || READS_BEGUN)
return NULL; return NULL;
if (these->stacks == NULL || these->stacks[0] == NULL) if (these->stacks == NULL || these->stacks[0] == NULL)
return NULL; return NULL;

View File

@ -191,6 +191,16 @@ int procps_pids_new (
int maxitems, int maxitems,
enum pids_item *items); enum pids_item *items);
struct pids_stack *procps_pids_read_next (
struct procps_pidsinfo *info);
int procps_pids_read_open (
struct procps_pidsinfo *info,
enum pids_reap_type which);
int procps_pids_read_shut (
struct procps_pidsinfo *info);
struct pids_reap *procps_pids_reap ( struct pids_reap *procps_pids_reap (
struct procps_pidsinfo *info, struct procps_pidsinfo *info,
enum pids_reap_type which); enum pids_reap_type which);