library: tweak formatting style for meminfo and vmstat

This patch mostly just eliminates darn tab characters.

Plus the library function declarations and definitions
have been standardized. Most visibly, the input params
now have all been indented on their own separate line.

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 7a7cf686ec
commit 500a901475
4 changed files with 178 additions and 163 deletions

View File

@ -26,6 +26,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#include <proc/meminfo.h> #include <proc/meminfo.h>
#include "procps-private.h" #include "procps-private.h"
@ -67,12 +68,13 @@ struct procps_meminfo {
* *
* Returns: a new meminfo info container * Returns: a new meminfo info container
*/ */
PROCPS_EXPORT int procps_meminfo_new(struct procps_meminfo **info) PROCPS_EXPORT int procps_meminfo_new (
struct procps_meminfo **info)
{ {
struct procps_meminfo *m; struct procps_meminfo *m;
m = calloc(1, sizeof(struct procps_meminfo)); m = calloc(1, sizeof(struct procps_meminfo));
if (!m) if (!m)
return -ENOMEM; return -ENOMEM;
m->refcount = 1; m->refcount = 1;
m->meminfo_fd = -1; m->meminfo_fd = -1;
@ -86,7 +88,8 @@ PROCPS_EXPORT int procps_meminfo_new(struct procps_meminfo **info)
* Read the data out of /proc/meminfo putting the information * Read the data out of /proc/meminfo putting the information
* into the supplied info structure * into the supplied info structure
*/ */
PROCPS_EXPORT int procps_meminfo_read(struct procps_meminfo *info) PROCPS_EXPORT int procps_meminfo_read (
struct procps_meminfo *info)
{ {
char buf[8192]; char buf[8192];
char *head, *tail; char *head, *tail;
@ -94,140 +97,142 @@ PROCPS_EXPORT int procps_meminfo_read(struct procps_meminfo *info)
unsigned long *valptr; unsigned long *valptr;
if (info == NULL) if (info == NULL)
return -1; return -1;
memset(&(info->data), 0, sizeof(struct meminfo_data)); memset(&(info->data), 0, sizeof(struct meminfo_data));
/* read in the data */ /* read in the data */
if (-1 == info->meminfo_fd && (info->meminfo_fd = open(MEMINFO_FILE, O_RDONLY)) == -1) { if (-1 == info->meminfo_fd && (info->meminfo_fd = open(MEMINFO_FILE, O_RDONLY)) == -1) {
return -errno; return -errno;
} }
if (lseek(info->meminfo_fd, 0L, SEEK_SET) == -1) { if (lseek(info->meminfo_fd, 0L, SEEK_SET) == -1) {
return -errno; return -errno;
} }
if ((size = read(info->meminfo_fd, buf, sizeof(buf)-1)) < 0) { if ((size = read(info->meminfo_fd, buf, sizeof(buf)-1)) < 0) {
return -1; return -1;
} }
buf[size] = '\0'; buf[size] = '\0';
/* Scan the file */ /* Scan the file */
head = buf; head = buf;
do { do {
tail = strchr(head, ' '); tail = strchr(head, ' ');
if (!tail) if (!tail)
break; break;
*tail = '\0'; *tail = '\0';
valptr = NULL; valptr = NULL;
if (0 == strcmp(head, "Active:")) { if (0 == strcmp(head, "Active:")) {
valptr = &(info->data.active); valptr = &(info->data.active);
} else if (0 == strcmp(head, "Inactive:")) { } else if (0 == strcmp(head, "Inactive:")) {
valptr = &(info->data.inactive); valptr = &(info->data.inactive);
} else if (0 == strcmp(head, "HighFree:")) { } else if (0 == strcmp(head, "HighFree:")) {
valptr = &(info->data.high_free); valptr = &(info->data.high_free);
} else if (0 == strcmp(head, "HighTotal:")) { } else if (0 == strcmp(head, "HighTotal:")) {
valptr = &(info->data.high_total); valptr = &(info->data.high_total);
} else if (0 == strcmp(head, "LowFree:")) { } else if (0 == strcmp(head, "LowFree:")) {
valptr = &(info->data.low_free); valptr = &(info->data.low_free);
} else if (0 == strcmp(head, "LowTotal:")) { } else if (0 == strcmp(head, "LowTotal:")) {
valptr = &(info->data.low_total); valptr = &(info->data.low_total);
} else if (0 == strcmp(head, "MemAvailable:")) { } else if (0 == strcmp(head, "MemAvailable:")) {
valptr = &(info->data.available); valptr = &(info->data.available);
} else if (0 == strcmp(head, "Buffers:")) { } else if (0 == strcmp(head, "Buffers:")) {
valptr = &(info->data.buffers); valptr = &(info->data.buffers);
} else if (0 == strcmp(head, "Cached:")) { } else if (0 == strcmp(head, "Cached:")) {
valptr = &(info->data.cached); valptr = &(info->data.cached);
} else if (0 == strcmp(head, "MemFree:")) { } else if (0 == strcmp(head, "MemFree:")) {
valptr = &(info->data.free); valptr = &(info->data.free);
} else if (0 == strcmp(head, "Shmem:")) { } else if (0 == strcmp(head, "Shmem:")) {
valptr = &(info->data.shared); valptr = &(info->data.shared);
} else if (0 == strcmp(head, "MemTotal:")) { } else if (0 == strcmp(head, "MemTotal:")) {
valptr = &(info->data.total); valptr = &(info->data.total);
} else if (0 == strcmp(head, "SwapFree:")) { } else if (0 == strcmp(head, "SwapFree:")) {
valptr = &(info->data.swap_free); valptr = &(info->data.swap_free);
} else if (0 == strcmp(head, "SwapTotal:")) { } else if (0 == strcmp(head, "SwapTotal:")) {
valptr = &(info->data.swap_total); valptr = &(info->data.swap_total);
} }
head = tail+1; head = tail+1;
if (valptr) { if (valptr) {
*valptr = strtoul(head, &tail, 10); *valptr = strtoul(head, &tail, 10);
} }
tail = strchr(head, '\n'); tail = strchr(head, '\n');
if (!tail) if (!tail)
break; break;
head = tail + 1; head = tail + 1;
} while(tail); } while(tail);
return 0; return 0;
} }
PROCPS_EXPORT struct procps_meminfo *procps_meminfo_ref(struct procps_meminfo *info) PROCPS_EXPORT struct procps_meminfo *procps_meminfo_ref (
struct procps_meminfo *info)
{ {
if (info == NULL) if (info == NULL)
return NULL; return NULL;
info->refcount++; info->refcount++;
return info; return info;
} }
PROCPS_EXPORT struct procps_meminfo *procps_meminfo_unref(struct procps_meminfo *info) PROCPS_EXPORT struct procps_meminfo *procps_meminfo_unref (
struct procps_meminfo *info)
{ {
if (info == NULL) if (info == NULL)
return NULL; return NULL;
info->refcount--; info->refcount--;
if (info->refcount > 0) if (info->refcount > 0)
return NULL; return NULL;
free(info); free(info);
return NULL; return NULL;
} }
/* Accessor functions */ /* Accessor functions */
PROCPS_EXPORT unsigned long procps_meminfo_get( PROCPS_EXPORT unsigned long procps_meminfo_get (
struct procps_meminfo *info, struct procps_meminfo *info,
enum meminfo_item item) enum meminfo_item item)
{ {
switch(item) { switch (item) {
case PROCPS_MEM_ACTIVE: case PROCPS_MEM_ACTIVE:
return info->data.active; return info->data.active;
case PROCPS_MEM_INACTIVE: case PROCPS_MEM_INACTIVE:
return info->data.inactive; return info->data.inactive;
case PROCPS_MEMHI_FREE: case PROCPS_MEMHI_FREE:
return info->data.high_free; return info->data.high_free;
case PROCPS_MEMHI_TOTAL: case PROCPS_MEMHI_TOTAL:
return info->data.high_total; return info->data.high_total;
case PROCPS_MEMHI_USED: case PROCPS_MEMHI_USED:
if (info->data.high_free > info->data.high_total) if (info->data.high_free > info->data.high_total)
return 0; return 0;
return info->data.high_total - info->data.high_free; return info->data.high_total - info->data.high_free;
case PROCPS_MEMLO_FREE: case PROCPS_MEMLO_FREE:
return info->data.low_free; return info->data.low_free;
case PROCPS_MEMLO_TOTAL: case PROCPS_MEMLO_TOTAL:
return info->data.low_total; return info->data.low_total;
case PROCPS_MEMLO_USED: case PROCPS_MEMLO_USED:
if (info->data.low_free > info->data.low_total) if (info->data.low_free > info->data.low_total)
return 0; return 0;
return info->data.low_total - info->data.low_free; return info->data.low_total - info->data.low_free;
case PROCPS_MEM_AVAILABLE: case PROCPS_MEM_AVAILABLE:
return info->data.available; return info->data.available;
case PROCPS_MEM_BUFFERS: case PROCPS_MEM_BUFFERS:
return info->data.buffers; return info->data.buffers;
case PROCPS_MEM_CACHED: case PROCPS_MEM_CACHED:
return info->data.cached; return info->data.cached;
case PROCPS_MEM_FREE: case PROCPS_MEM_FREE:
return info->data.free; return info->data.free;
case PROCPS_MEM_SHARED: case PROCPS_MEM_SHARED:
return info->data.shared; return info->data.shared;
case PROCPS_MEM_TOTAL: case PROCPS_MEM_TOTAL:
return info->data.total; return info->data.total;
case PROCPS_MEM_USED: case PROCPS_MEM_USED:
return info->data.used; return info->data.used;
case PROCPS_SWAP_FREE: case PROCPS_SWAP_FREE:
return info->data.swap_free; return info->data.swap_free;
case PROCPS_SWAP_TOTAL: case PROCPS_SWAP_TOTAL:
return info->data.swap_total; return info->data.swap_total;
case PROCPS_SWAP_USED: case PROCPS_SWAP_USED:
if (info->data.swap_free > info->data.swap_total) if (info->data.swap_free > info->data.swap_total)
return 0; return 0;
return info->data.swap_total - info->data.swap_free; return info->data.swap_total - info->data.swap_free;
} }
return 0; return 0;
} }

View File

@ -26,33 +26,36 @@
__BEGIN_DECLS __BEGIN_DECLS
struct procps_meminfo;
int procps_meminfo_new(struct procps_meminfo **info);
int procps_meminfo_read(struct procps_meminfo *info);
struct procps_meminfo *procps_meminfo_ref(struct procps_meminfo *info);
struct procps_meminfo *procps_meminfo_unref(struct procps_meminfo *info);
enum meminfo_item { enum meminfo_item {
PROCPS_MEM_ACTIVE,
PROCPS_MEM_INACTIVE,
PROCPS_MEMHI_FREE, PROCPS_MEMHI_FREE,
PROCPS_MEMHI_TOTAL, PROCPS_MEMHI_TOTAL,
PROCPS_MEMHI_USED, PROCPS_MEMHI_USED,
PROCPS_MEMLO_FREE, PROCPS_MEMLO_FREE,
PROCPS_MEMLO_TOTAL, PROCPS_MEMLO_TOTAL,
PROCPS_MEMLO_USED, PROCPS_MEMLO_USED,
PROCPS_MEM_ACTIVE,
PROCPS_MEM_AVAILABLE, PROCPS_MEM_AVAILABLE,
PROCPS_MEM_BUFFERS, PROCPS_MEM_BUFFERS,
PROCPS_MEM_CACHED, PROCPS_MEM_CACHED,
PROCPS_MEM_FREE, PROCPS_MEM_FREE,
PROCPS_MEM_INACTIVE,
PROCPS_MEM_SHARED, PROCPS_MEM_SHARED,
PROCPS_MEM_TOTAL, PROCPS_MEM_TOTAL,
PROCPS_MEM_USED, PROCPS_MEM_USED,
PROCPS_SWAP_FREE, PROCPS_SWAP_FREE,
PROCPS_SWAP_TOTAL, PROCPS_SWAP_TOTAL,
PROCPS_SWAP_USED, PROCPS_SWAP_USED
}; };
unsigned long procps_meminfo_get(struct procps_meminfo *info, enum meminfo_item item);
struct procps_meminfo;
int procps_meminfo_new (struct procps_meminfo **info);
int procps_meminfo_read (struct procps_meminfo *info);
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);
__END_DECLS __END_DECLS
#endif #endif

View File

@ -41,12 +41,13 @@ struct procps_vmstat {
* *
* Returns: a new procps_vmstat container * Returns: a new procps_vmstat container
*/ */
PROCPS_EXPORT int procps_vmstat_new(struct procps_vmstat **info) PROCPS_EXPORT int procps_vmstat_new (
struct procps_vmstat **info)
{ {
struct procps_vmstat *v; struct procps_vmstat *v;
v = calloc(1, sizeof(struct procps_vmstat)); v = calloc(1, sizeof(struct procps_vmstat));
if (!v) if (!v)
return -ENOMEM; return -ENOMEM;
v->refcount = 1; v->refcount = 1;
v->vmstat_fd = -1; v->vmstat_fd = -1;
@ -62,7 +63,8 @@ PROCPS_EXPORT int procps_vmstat_new(struct procps_vmstat **info)
* *
* Returns: 0 on success, negative on error * Returns: 0 on success, negative on error
*/ */
PROCPS_EXPORT int procps_vmstat_read(struct procps_vmstat *info) PROCPS_EXPORT int procps_vmstat_read (
struct procps_vmstat *info)
{ {
char buf[8192]; char buf[8192];
char *head, *tail; char *head, *tail;
@ -70,85 +72,87 @@ PROCPS_EXPORT int procps_vmstat_read(struct procps_vmstat *info)
unsigned long *valptr; unsigned long *valptr;
if (info == NULL) if (info == NULL)
return -1; return -1;
memset(&(info->data), 0, sizeof(struct vmstat_data)); memset(&(info->data), 0, sizeof(struct vmstat_data));
/* read in the data */ /* read in the data */
if (-1 == info->vmstat_fd && (info->vmstat_fd = open(VMSTAT_FILE, O_RDONLY)) == -1) { if (-1 == info->vmstat_fd && (info->vmstat_fd = open(VMSTAT_FILE, O_RDONLY)) == -1) {
return -errno; return -errno;
} }
if (lseek(info->vmstat_fd, 0L, SEEK_SET) == -1) { if (lseek(info->vmstat_fd, 0L, SEEK_SET) == -1) {
return -errno; return -errno;
} }
if ((size = read(info->vmstat_fd, buf, sizeof(buf)-1)) < 0) { if ((size = read(info->vmstat_fd, buf, sizeof(buf)-1)) < 0) {
return -1; return -1;
} }
buf[size] = '\0'; buf[size] = '\0';
/* Scan the file */ /* Scan the file */
head = buf; head = buf;
do { do {
tail = strchr(head, ' '); tail = strchr(head, ' ');
if (!tail) if (!tail)
break; break;
*tail = '\0'; *tail = '\0';
valptr = NULL; valptr = NULL;
if (0 == strcmp(head, "pgpgin")) { if (0 == strcmp(head, "pgpgin")) {
valptr = &(info->data.pgpgin); valptr = &(info->data.pgpgin);
}else if (0 == strcmp(head, "pgpgout")) { }else if (0 == strcmp(head, "pgpgout")) {
valptr = &(info->data.pgpgout); valptr = &(info->data.pgpgout);
}else if (0 == strcmp(head, "pswpin")) { }else if (0 == strcmp(head, "pswpin")) {
valptr = &(info->data.pswpin); valptr = &(info->data.pswpin);
}else if (0 == strcmp(head, "pswpout")) { }else if (0 == strcmp(head, "pswpout")) {
valptr = &(info->data.pswpout); valptr = &(info->data.pswpout);
} }
head = tail+1; head = tail+1;
if (valptr) { if (valptr) {
*valptr = strtoul(head, &tail, 10); *valptr = strtoul(head, &tail, 10);
} }
tail = strchr(head, '\n'); tail = strchr(head, '\n');
if (!tail) if (!tail)
break; break;
head = tail + 1; head = tail + 1;
} while(tail); } while(tail);
return 0; return 0;
} }
PROCPS_EXPORT struct procps_vmstat *procps_vmstat_ref(struct procps_vmstat *info) PROCPS_EXPORT struct procps_vmstat *procps_vmstat_ref (
struct procps_vmstat *info)
{ {
if (info == NULL) if (info == NULL)
return NULL; return NULL;
info->refcount++; info->refcount++;
return info; return info;
} }
PROCPS_EXPORT struct procps_vmstat *procps_vmstat_unref(struct procps_vmstat *info) PROCPS_EXPORT struct procps_vmstat *procps_vmstat_unref (
struct procps_vmstat *info)
{ {
if (info == NULL) if (info == NULL)
return NULL; return NULL;
info->refcount--; info->refcount--;
if (info->refcount > 0) if (info->refcount > 0)
return NULL; return NULL;
free(info); free(info);
return NULL; return NULL;
} }
/* Accessor functions */ /* Accessor functions */
PROCPS_EXPORT unsigned long procps_vmstat_get( PROCPS_EXPORT unsigned long procps_vmstat_get (
struct procps_vmstat *info, struct procps_vmstat *info,
enum vmstat_item item) enum vmstat_item item)
{ {
switch(item) { switch (item) {
case PROCPS_VMSTAT_PGPGIN: case PROCPS_VMSTAT_PGPGIN:
return info->data.pgpgin; return info->data.pgpgin;
case PROCPS_VMSTAT_PGPGOUT: case PROCPS_VMSTAT_PGPGOUT:
return info->data.pgpgout; return info->data.pgpgout;
case PROCPS_VMSTAT_PSWPIN: case PROCPS_VMSTAT_PSWPIN:
return info->data.pswpin; return info->data.pswpin;
case PROCPS_VMSTAT_PSWPOUT: case PROCPS_VMSTAT_PSWPOUT:
return info->data.pswpout; return info->data.pswpout;
} }
return 0; return 0;
} }

View File

@ -27,19 +27,22 @@
__BEGIN_DECLS __BEGIN_DECLS
struct procps_vmstat;
int procps_vmstat_new(struct procps_vmstat **info);
int procps_vmstat_read(struct procps_vmstat *info);
struct procps_vmstat *procps_vmstat_ref(struct procps_vmstat *info);
struct procps_vmstat *procps_vmstat_unref(struct procps_vmstat *info);
enum vmstat_item { enum vmstat_item {
PROCPS_VMSTAT_PGPGIN, PROCPS_VMSTAT_PGPGIN,
PROCPS_VMSTAT_PGPGOUT, PROCPS_VMSTAT_PGPGOUT,
PROCPS_VMSTAT_PSWPIN, PROCPS_VMSTAT_PSWPIN,
PROCPS_VMSTAT_PSWPOUT PROCPS_VMSTAT_PSWPOUT
}; };
unsigned long procps_vmstat_get(struct procps_vmstat *info, enum vmstat_item item);
struct procps_vmstat;
int procps_vmstat_new (struct procps_vmstat **info);
int procps_vmstat_read (struct procps_vmstat *info);
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);
__END_DECLS __END_DECLS
#endif #endif