library: memory and vmstat API changes

Created new set of functions for meminfo related calls. Liked the
format of that better so changed vmstat around so the look similar.
Missed the makefile change for uptime so added it in now.
This commit is contained in:
Craig Small 2015-06-21 18:22:28 +10:00
parent d7932b9a13
commit ca4a09c432
6 changed files with 68 additions and 42 deletions

View File

@ -179,8 +179,8 @@ proc_libprocps_la_SOURCES = \
proc/vmstat.h \ proc/vmstat.h \
proc/wchan.c \ proc/wchan.c \
proc/wchan.h \ proc/wchan.h \
proc/whattime.c \ proc/uptime.c \
proc/whattime.h proc/uptime.h
proc_libprocps_la_includedir = $(includedir)/proc/ proc_libprocps_la_includedir = $(includedir)/proc/
proc_libprocps_la_include_HEADERS = \ proc_libprocps_la_include_HEADERS = \

View File

@ -1,4 +1,24 @@
/*
* meminfo - Memory statistics part of procps
*
* Copyright (C) 1992-1998 by Michael K. Johnson <johnsonm@redhat.com>
* Copyright (C) 1998-2003 Albert Cahalan
* Copyright (C) 2015 Craig Small <csmall@enc.com.au>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <errno.h> #include <errno.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>

View File

@ -1,6 +1,9 @@
/* /*
* libprocps - Library to read proc filesystem * meminfo - Memory statistics part of procps
* meminfo - Parse /proc/meminfo *
* Copyright (C) 1992-1998 by Michael K. Johnson <johnsonm@redhat.com>
* Copyright (C) 1998-2003 Albert Cahalan
* Copyright (C) 2015 Craig Small <csmall@enc.com.au>
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public * modify it under the terms of the GNU Lesser General Public

View File

@ -25,7 +25,7 @@ struct mem_table_struct {
unsigned long *slot; unsigned long *slot;
}; };
struct vmstat_info { struct procps_vmstat {
int refcount; int refcount;
int vmstat_fd; int vmstat_fd;
struct vmstat_data data; struct vmstat_data data;
@ -39,12 +39,12 @@ struct vmstat_info {
* The initial refcount is 1, and needs to be decremented * The initial refcount is 1, and needs to be decremented
* to release the resources of the structure. * to release the resources of the structure.
* *
* Returns: a new vmstat info container * Returns: a new procps_vmstat container
*/ */
PROCPS_EXPORT int procps_vmstat_new(struct vmstat_info **info) PROCPS_EXPORT int procps_vmstat_new(struct procps_vmstat **info)
{ {
struct vmstat_info *v; struct procps_vmstat *v;
v = calloc(1, sizeof(struct vmstat_info)); v = calloc(1, sizeof(struct procps_vmstat));
if (!v) if (!v)
return -ENOMEM; return -ENOMEM;
@ -59,8 +59,10 @@ PROCPS_EXPORT int procps_vmstat_new(struct vmstat_info **info)
* *
* Read the data out of /proc/vmstat putting the information * Read the data out of /proc/vmstat putting the information
* into the supplied info structure * into the supplied info structure
*
* Returns: 0 on success, negative on error
*/ */
PROCPS_EXPORT int procps_vmstat_read(struct vmstat_info *info) PROCPS_EXPORT int procps_vmstat_read(struct procps_vmstat *info)
{ {
char buf[8192]; char buf[8192];
char *head, *tail; char *head, *tail;
@ -114,7 +116,7 @@ PROCPS_EXPORT int procps_vmstat_read(struct vmstat_info *info)
return 0; return 0;
} }
PROCPS_EXPORT struct vmstat_info *procps_vmstat_ref(struct vmstat_info *info) PROCPS_EXPORT struct procps_vmstat *procps_vmstat_ref(struct procps_vmstat *info)
{ {
if (info == NULL) if (info == NULL)
return NULL; return NULL;
@ -122,7 +124,7 @@ PROCPS_EXPORT struct vmstat_info *procps_vmstat_ref(struct vmstat_info *info)
return info; return info;
} }
PROCPS_EXPORT struct vmstat_info *procps_vmstat_unref(struct vmstat_info *info) PROCPS_EXPORT struct procps_vmstat *procps_vmstat_unref(struct procps_vmstat *info)
{ {
if (info == NULL) if (info == NULL)
return NULL; return NULL;
@ -135,17 +137,17 @@ PROCPS_EXPORT struct vmstat_info *procps_vmstat_unref(struct vmstat_info *info)
/* Accessor functions */ /* Accessor functions */
PROCPS_EXPORT unsigned long procps_vmstat_get( PROCPS_EXPORT unsigned long procps_vmstat_get(
struct vmstat_info *info, struct procps_vmstat *info,
enum vmstat_item item) enum vmstat_item item)
{ {
switch(item) { switch(item) {
case VMSTAT_INFO_PGPGIN: case PROCPS_VMSTAT_PGPGIN:
return info->data.pgpgin; return info->data.pgpgin;
case VMSTAT_INFO_PGPGOUT: case PROCPS_VMSTAT_PGPGOUT:
return info->data.pgpgout; return info->data.pgpgout;
case VMSTAT_INFO_PSWPIN: case PROCPS_VMSTAT_PSWPIN:
return info->data.pswpin; return info->data.pswpin;
case VMSTAT_INFO_PSWPOUT: case PROCPS_VMSTAT_PSWPOUT:
return info->data.pswpout; return info->data.pswpout;
} }
return 0; return 0;

View File

@ -27,18 +27,19 @@
__BEGIN_DECLS __BEGIN_DECLS
struct vmstat_info; struct procps_vmstat;
int procps_vmstat_new(struct vmstat_info **info); int procps_vmstat_new(struct procps_vmstat **info);
int procps_vmstat_read(struct vmstat_info *info); int procps_vmstat_read(struct procps_vmstat *info);
struct vmstat_info *procps_vmstat_ref(struct vmstat_info *info); struct procps_vmstat *procps_vmstat_ref(struct procps_vmstat *info);
struct vmstat_info *procps_vmstat_unref(struct vmstat_info *info); struct procps_vmstat *procps_vmstat_unref(struct procps_vmstat *info);
enum vmstat_item { enum vmstat_item {
VMSTAT_INFO_PGPGIN, PROCPS_VMSTAT_PGPGIN,
VMSTAT_INFO_PGPGOUT, PROCPS_VMSTAT_PGPGOUT,
VMSTAT_INFO_PSWPIN, PROCPS_VMSTAT_PSWPIN,
VMSTAT_INFO_PSWPOUT PROCPS_VMSTAT_PSWPOUT
}; };
unsigned long procps_vmstat_get(struct vmstat_info *info, enum vmstat_item item); unsigned long procps_vmstat_get(struct procps_vmstat *info, enum vmstat_item item);
__END_DECLS __END_DECLS
#endif #endif

View File

@ -297,7 +297,7 @@ static void new_format(void)
struct tm *tm_ptr; struct tm *tm_ptr;
time_t the_time; time_t the_time;
char timebuf[32]; char timebuf[32];
struct vmstat_info *vm_info; struct procps_vmstat *vm_info;
struct procps_stat_info *sys_info; struct procps_stat_info *sys_info;
sleep_half = (sleep_time / 2); sleep_half = (sleep_time / 2);
@ -339,10 +339,10 @@ static void new_format(void)
unitConvert(kb_swap_used), unitConvert(kb_main_free), unitConvert(kb_swap_used), unitConvert(kb_main_free),
unitConvert(a_option?kb_inactive:kb_main_buffers), unitConvert(a_option?kb_inactive:kb_main_buffers),
unitConvert(a_option?kb_active:kb_main_cached), unitConvert(a_option?kb_active:kb_main_cached),
(unsigned)( (unitConvert(procps_vmstat_get(vm_info, VMSTAT_INFO_PSWPIN) * kb_per_page) * hz + divo2) / Div ), (unsigned)( (unitConvert(procps_vmstat_get(vm_info, PROCPS_VMSTAT_PSWPIN) * kb_per_page) * hz + divo2) / Div ),
(unsigned)( (unitConvert(procps_vmstat_get(vm_info, VMSTAT_INFO_PSWPOUT) * kb_per_page) * hz + divo2) / Div ), (unsigned)( (unitConvert(procps_vmstat_get(vm_info, PROCPS_VMSTAT_PSWPOUT) * kb_per_page) * hz + divo2) / Div ),
(unsigned)( (procps_vmstat_get(vm_info, VMSTAT_INFO_PGPGIN) * hz + divo2) / Div ), (unsigned)( (procps_vmstat_get(vm_info, PROCPS_VMSTAT_PGPGIN) * hz + divo2) / Div ),
(unsigned)( (procps_vmstat_get(vm_info, VMSTAT_INFO_PGPGOUT) * hz + divo2) / Div ), (unsigned)( (procps_vmstat_get(vm_info, PROCPS_VMSTAT_PGPGOUT) * hz + divo2) / Div ),
(unsigned)( (procps_stat_get(sys_info, PROCPS_STAT_INTERRUPTS) * hz + divo2) / Div ), (unsigned)( (procps_stat_get(sys_info, PROCPS_STAT_INTERRUPTS) * hz + divo2) / Div ),
(unsigned)( (procps_stat_get(sys_info, PROCPS_STAT_CONTEXT) * hz + divo2) / Div ), (unsigned)( (procps_stat_get(sys_info, PROCPS_STAT_CONTEXT) * hz + divo2) / Div ),
(unsigned)( (100*duse + divo2) / Div ), (unsigned)( (100*duse + divo2) / Div ),
@ -382,10 +382,10 @@ static void new_format(void)
if (procps_vmstat_read(vm_info) < 0) if (procps_vmstat_read(vm_info) < 0)
xerrx(EXIT_FAILURE, xerrx(EXIT_FAILURE,
_("Unable to read vmstat information")); _("Unable to read vmstat information"));
pgpgin[tog] = procps_vmstat_get(vm_info, VMSTAT_INFO_PGPGIN); pgpgin[tog] = procps_vmstat_get(vm_info, PROCPS_VMSTAT_PGPGIN);
pgpgout[tog] = procps_vmstat_get(vm_info, VMSTAT_INFO_PGPGOUT); pgpgout[tog] = procps_vmstat_get(vm_info, PROCPS_VMSTAT_PGPGOUT);
pswpin[tog] = procps_vmstat_get(vm_info, VMSTAT_INFO_PSWPIN); pswpin[tog] = procps_vmstat_get(vm_info, PROCPS_VMSTAT_PSWPIN);
pswpout[tog] = procps_vmstat_get(vm_info, VMSTAT_INFO_PSWPOUT); pswpout[tog] = procps_vmstat_get(vm_info, PROCPS_VMSTAT_PSWPOUT);
if (t_option) { if (t_option) {
@ -815,7 +815,7 @@ static void disksum_format(void)
static void sum_format(void) static void sum_format(void)
{ {
struct procps_stat_info *sys_info; struct procps_stat_info *sys_info;
struct vmstat_info *vm_info; struct procps_vmstat *vm_info;
meminfo(); meminfo();
@ -852,10 +852,10 @@ static void sum_format(void)
printf(_("%13lld stolen cpu ticks\n"), procps_stat_get_cpu(sys_info, PROCPS_CPU_STOLEN)); printf(_("%13lld stolen cpu ticks\n"), procps_stat_get_cpu(sys_info, PROCPS_CPU_STOLEN));
printf(_("%13lld non-nice guest cpu ticks\n"), procps_stat_get_cpu(sys_info, PROCPS_CPU_GUEST)); printf(_("%13lld non-nice guest cpu ticks\n"), procps_stat_get_cpu(sys_info, PROCPS_CPU_GUEST));
printf(_("%13lld nice guest cpu ticks\n"), procps_stat_get_cpu(sys_info, PROCPS_CPU_GNICE)); printf(_("%13lld nice guest cpu ticks\n"), procps_stat_get_cpu(sys_info, PROCPS_CPU_GNICE));
printf(_("%13lu pages paged in\n"), procps_vmstat_get(vm_info, VMSTAT_INFO_PGPGIN)); printf(_("%13lu pages paged in\n"), procps_vmstat_get(vm_info, PROCPS_VMSTAT_PGPGIN));
printf(_("%13lu pages paged out\n"), procps_vmstat_get(vm_info, VMSTAT_INFO_PGPGOUT)); printf(_("%13lu pages paged out\n"), procps_vmstat_get(vm_info, PROCPS_VMSTAT_PGPGOUT));
printf(_("%13lu pages swapped in\n"), procps_vmstat_get(vm_info, VMSTAT_INFO_PSWPIN)); printf(_("%13lu pages swapped in\n"), procps_vmstat_get(vm_info, PROCPS_VMSTAT_PSWPIN));
printf(_("%13lu pages swapped out\n"), procps_vmstat_get(vm_info, VMSTAT_INFO_PSWPOUT)); printf(_("%13lu pages swapped out\n"), procps_vmstat_get(vm_info, PROCPS_VMSTAT_PSWPOUT));
printf(_("%13u interrupts\n"), procps_stat_get(sys_info, PROCPS_STAT_INTERRUPTS)); printf(_("%13u interrupts\n"), procps_stat_get(sys_info, PROCPS_STAT_INTERRUPTS));
printf(_("%13u CPU context switches\n"), procps_stat_get(sys_info, PROCPS_STAT_CONTEXT)); printf(_("%13u CPU context switches\n"), procps_stat_get(sys_info, PROCPS_STAT_CONTEXT));
printf(_("%13u boot time\n"), procps_stat_get(sys_info, PROCPS_STAT_BTIME)); printf(_("%13u boot time\n"), procps_stat_get(sys_info, PROCPS_STAT_BTIME));