library: add chaining provisions to meminfo and vmstat

If a caller chooses to reduce the overhead of repeated
function calls, this commit provides for acquiring all
the desired information in just a single library call.

Signed-off-by: Jim Warner <james.warner@comcast.net>
This commit is contained in:
Jim Warner 2015-06-28 00:00:00 -05:00 committed by Craig Small
parent 7a98cab187
commit a05084f381
5 changed files with 127 additions and 0 deletions

View File

@ -67,6 +67,7 @@ global:
procps_meminfo_ref;
procps_meminfo_unref;
procps_meminfo_get;
procps_meminfo_get_chain;
procps_stat_new;
procps_stat_read;
procps_stat_ref;
@ -78,6 +79,7 @@ global:
procps_vmstat_ref;
procps_vmstat_unref;
procps_vmstat_get;
procps_vmstat_get_chain;
local:
*;
};

View File

@ -279,3 +279,85 @@ PROCPS_EXPORT unsigned long procps_meminfo_get (
}
return 0;
}
PROCPS_EXPORT int procps_meminfo_get_chain (
struct procps_meminfo *info,
struct meminfo_result *item)
{
if (item == NULL)
return -EINVAL;
do {
switch (item->item) {
case PROCPS_MEM_ACTIVE:
item->result = info->data.active;
break;
case PROCPS_MEM_INACTIVE:
item->result = info->data.inactive;
break;
case PROCPS_MEMHI_FREE:
item->result = info->data.high_free;
break;
case PROCPS_MEMHI_TOTAL:
item->result = info->data.high_total;
break;
case PROCPS_MEMHI_USED:
if (info->data.high_free > info->data.high_total)
item->result = 0;
else
item->result = info->data.high_total - info->data.high_free;
break;
case PROCPS_MEMLO_FREE:
item->result = info->data.low_free;
break;
case PROCPS_MEMLO_TOTAL:
item->result = info->data.low_total;
break;
case PROCPS_MEMLO_USED:
if (info->data.low_free > info->data.low_total)
item->result = 0;
else
item->result = info->data.low_total - info->data.low_free;
break;
case PROCPS_MEM_AVAILABLE:
item->result = info->data.available;
break;
case PROCPS_MEM_BUFFERS:
item->result = info->data.buffers;
break;
case PROCPS_MEM_CACHED:
item->result = info->data.cached;
break;
case PROCPS_MEM_FREE:
item->result = info->data.free;
break;
case PROCPS_MEM_SHARED:
item->result = info->data.shared;
break;
case PROCPS_MEM_TOTAL:
item->result = info->data.total;
break;
case PROCPS_MEM_USED:
item->result = info->data.used;
break;
case PROCPS_SWAP_FREE:
item->result = info->data.swap_free;
break;
case PROCPS_SWAP_TOTAL:
item->result = info->data.swap_total;
break;
case PROCPS_SWAP_USED:
if (info->data.swap_free > info->data.swap_total)
item->result = 0;
else
item->result = info->data.swap_total - info->data.swap_free;
break;
default:
return -EINVAL;
}
item = item->next;
} while (item);
return 0;
}

View File

@ -47,6 +47,12 @@ enum meminfo_item {
PROCPS_SWAP_USED
};
struct meminfo_result {
enum meminfo_item item;
unsigned long result;
struct meminfo_result *next;
};
struct procps_meminfo;
int procps_meminfo_new (struct procps_meminfo **info);
@ -56,6 +62,7 @@ struct procps_meminfo *procps_meminfo_ref (struct procps_meminfo *info);
struct procps_meminfo *procps_meminfo_unref (struct procps_meminfo *info);
unsigned long procps_meminfo_get (struct procps_meminfo *info, enum meminfo_item item);
int procps_meminfo_get_chain (struct procps_meminfo *info, struct meminfo_result *item);
__END_DECLS
#endif

View File

@ -157,3 +157,32 @@ PROCPS_EXPORT unsigned long procps_vmstat_get (
return 0;
}
PROCPS_EXPORT int procps_vmstat_get_chain (
struct procps_vmstat *info,
struct vmstat_result *item)
{
if (item == NULL)
return -EINVAL;
do {
switch (item->item) {
case PROCPS_VMSTAT_PGPGIN:
item->result = info->data.pgpgin;
break;
case PROCPS_VMSTAT_PGPGOUT:
item->result = info->data.pgpgout;
break;
case PROCPS_VMSTAT_PSWPIN:
item->result = info->data.pswpin;
break;
case PROCPS_VMSTAT_PSWPOUT:
item->result = info->data.pswpout;
break;
default:
return -EINVAL;
}
item = item->next;
} while (item);
return 0;
}

View File

@ -34,6 +34,12 @@ enum vmstat_item {
PROCPS_VMSTAT_PSWPOUT
};
struct vmstat_result {
enum vmstat_item item;
unsigned long result;
struct vmstat_result *next;
};
struct procps_vmstat;
int procps_vmstat_new (struct procps_vmstat **info);
@ -43,6 +49,7 @@ struct procps_vmstat *procps_vmstat_ref (struct procps_vmstat *info);
struct procps_vmstat *procps_vmstat_unref (struct procps_vmstat *info);
unsigned long procps_vmstat_get (struct procps_vmstat *info, enum vmstat_item item);
int procps_vmstat_get_chain (struct procps_vmstat *info, struct vmstat_result *item);
__END_DECLS
#endif