library: sysstat and vmstat api changes
Use the standard libc declarations. For protecting the headers for C++ procps used to have its own defines, this change makes them use the standard libc ones. getstat() -> procps_stat_* vminfo() -> procps_vmstat_* These two components of the library now use the newer version of the API with less exposed global variables. The old methods are there for now. Signed-off-by: Craig Small <csmall@enc.com.au>
This commit is contained in:
parent
6a0850be7b
commit
a410e236ab
@ -163,6 +163,8 @@ proc_libprocps_la_SOURCES = \
|
||||
proc/pwcache.h \
|
||||
proc/readproc.c \
|
||||
proc/readproc.h \
|
||||
proc/readstat.c \
|
||||
proc/readstat.h \
|
||||
proc/sig.c \
|
||||
proc/sig.h \
|
||||
proc/slab.c \
|
||||
@ -171,6 +173,8 @@ proc_libprocps_la_SOURCES = \
|
||||
proc/sysinfo.h \
|
||||
proc/version.c \
|
||||
proc/version.h \
|
||||
proc/vmstat.c \
|
||||
proc/vmstat.h \
|
||||
proc/wchan.c \
|
||||
proc/wchan.h \
|
||||
proc/whattime.c \
|
||||
|
@ -1,9 +1,9 @@
|
||||
#ifndef PROCPS_PROC_ALLOC_H
|
||||
#define PROCPS_PROC_ALLOC_H
|
||||
|
||||
#include "procps.h"
|
||||
#include <proc/procps.h>
|
||||
|
||||
EXTERN_C_BEGIN
|
||||
__BEGIN_DECLS
|
||||
|
||||
/* change xalloc_err_handler to override the default fprintf(stderr... */
|
||||
extern message_fn xalloc_err_handler;
|
||||
@ -13,6 +13,6 @@ extern void *xmalloc(size_t size) MALLOC;
|
||||
extern void *xrealloc(void *oldp, unsigned int size) MALLOC;
|
||||
extern char *xstrdup(const char *str) MALLOC;
|
||||
|
||||
EXTERN_C_END
|
||||
__END_DECLS
|
||||
|
||||
#endif
|
||||
|
@ -1,10 +1,9 @@
|
||||
#ifndef PROC_DEVNAME_H
|
||||
#define PROC_DEVNAME_H
|
||||
|
||||
#include "procps.h"
|
||||
#include "readproc.h"
|
||||
#include <proc/procps.h>
|
||||
|
||||
EXTERN_C_BEGIN
|
||||
__BEGIN_DECLS
|
||||
|
||||
#define ABBREV_DEV 1 /* remove /dev/ */
|
||||
#define ABBREV_TTY 2 /* remove tty */
|
||||
@ -14,5 +13,5 @@ extern unsigned dev_to_tty(char *__restrict ret, unsigned chop, dev_t dev_t_dev,
|
||||
|
||||
extern int tty_to_dev(const char *__restrict const name);
|
||||
|
||||
EXTERN_C_END
|
||||
__END_DECLS
|
||||
#endif
|
||||
|
@ -1,12 +1,10 @@
|
||||
#ifndef PROCPS_PROC_ESCAPE_H
|
||||
#define PROCPS_PROC_ESCAPE_H
|
||||
|
||||
//#include <stdio.h>
|
||||
#include <sys/types.h>
|
||||
#include "procps.h"
|
||||
#include "readproc.h"
|
||||
#include <proc/procps.h>
|
||||
#include <proc/readproc.h>
|
||||
|
||||
EXTERN_C_BEGIN
|
||||
__BEGIN_DECLS
|
||||
|
||||
#define ESC_STRETCH 1 // since we mangle to '?' this is 1 (would be 4 for octal escapes)
|
||||
|
||||
@ -19,5 +17,5 @@ extern int escape_str(char *__restrict dst, const char *__restrict src, int bufs
|
||||
extern int escape_command(char *__restrict const outbuf, const proc_t *__restrict const pp, int bytes, int *cells, unsigned flags);
|
||||
extern int escaped_copy(char *__restrict dst, const char *__restrict src, int bufsize, int *maxroom);
|
||||
|
||||
EXTERN_C_END
|
||||
__END_DECLS
|
||||
#endif
|
||||
|
@ -60,6 +60,17 @@ global:
|
||||
uptime;
|
||||
user_from_uid;
|
||||
procps_linux_version;
|
||||
procps_stat_new;
|
||||
procps_stat_read;
|
||||
procps_stat_ref;
|
||||
procps_stat_unref;
|
||||
procps_stat_get_cpu;
|
||||
procps_stat_get;
|
||||
procps_vmstat_new;
|
||||
procps_vmstat_read;
|
||||
procps_vmstat_ref;
|
||||
procps_vmstat_unref;
|
||||
procps_vmstat_get;
|
||||
local:
|
||||
*;
|
||||
};
|
||||
|
@ -1,13 +1,7 @@
|
||||
#ifndef PROCPS_PROC_PROCPS_H
|
||||
#define PROCPS_PROC_PROCPS_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
#define EXTERN_C_BEGIN extern "C" {
|
||||
#define EXTERN_C_END }
|
||||
#else
|
||||
#define EXTERN_C_BEGIN
|
||||
#define EXTERN_C_END
|
||||
#endif
|
||||
#include <features.h>
|
||||
|
||||
// Some ports make the mistake of running a 32-bit userspace
|
||||
// on a 64-bit kernel. Shame on them. It's not at all OK to
|
||||
|
@ -2,9 +2,9 @@
|
||||
#define PROCPS_PROC_PWCACHE_H
|
||||
|
||||
#include <sys/types.h>
|
||||
#include "procps.h"
|
||||
#include <proc/procps.h>
|
||||
|
||||
EXTERN_C_BEGIN
|
||||
__BEGIN_DECLS
|
||||
|
||||
// used in pwcache and in readproc to set size of username or groupname
|
||||
#define P_G_SZ 33
|
||||
@ -12,6 +12,6 @@ EXTERN_C_BEGIN
|
||||
extern char *user_from_uid(uid_t uid);
|
||||
extern char *group_from_gid(gid_t gid);
|
||||
|
||||
EXTERN_C_END
|
||||
__END_DECLS
|
||||
|
||||
#endif
|
||||
|
@ -10,13 +10,13 @@
|
||||
// in the file COPYING
|
||||
|
||||
|
||||
#include "procps.h"
|
||||
#include "pwcache.h"
|
||||
#include <proc/procps.h>
|
||||
#include <proc/pwcache.h>
|
||||
|
||||
#define SIGNAL_STRING
|
||||
#define QUICK_THREADS /* copy (vs. read) some thread info from parent proc_t */
|
||||
|
||||
EXTERN_C_BEGIN
|
||||
__BEGIN_DECLS
|
||||
|
||||
// ld cutime, cstime, priority, nice, timeout, alarm, rss,
|
||||
// c state,
|
||||
@ -311,5 +311,5 @@ extern proc_t * get_proc_stats(pid_t pid, proc_t *p);
|
||||
#define PROC_SPARE_3 0x04000000
|
||||
#define PROC_SPARE_4 0x08000000
|
||||
|
||||
EXTERN_C_END
|
||||
__END_DECLS
|
||||
#endif
|
||||
|
205
proc/readstat.c
Normal file
205
proc/readstat.c
Normal file
@ -0,0 +1,205 @@
|
||||
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <proc/readstat.h>
|
||||
#include "procps-private.h"
|
||||
|
||||
#define STAT_FILE "/proc/stat"
|
||||
|
||||
struct stat_data {
|
||||
jiff cpu_user;
|
||||
jiff cpu_nice;
|
||||
jiff cpu_sys;
|
||||
jiff cpu_idle;
|
||||
jiff cpu_iowait;
|
||||
jiff cpu_irq;
|
||||
jiff cpu_sirq;
|
||||
jiff cpu_stol;
|
||||
jiff cpu_guest;
|
||||
jiff cpu_gnice;
|
||||
unsigned int intr;
|
||||
unsigned int ctxt;
|
||||
unsigned int btime;
|
||||
unsigned int procs;
|
||||
unsigned int procs_blocked;
|
||||
unsigned int procs_running;
|
||||
};
|
||||
|
||||
struct procps_stat_info {
|
||||
int refcount;
|
||||
int stat_fd;
|
||||
struct stat_data data;
|
||||
};
|
||||
|
||||
/*
|
||||
* procps_stat_new:
|
||||
*
|
||||
* Create a new container to hold the stat information
|
||||
*
|
||||
* The initial refcount is 1, and needs to be decremented
|
||||
* to release the resources of the structure.
|
||||
*
|
||||
* Returns: a new stat info container
|
||||
*/
|
||||
PROCPS_EXPORT int procps_stat_new(struct procps_stat_info **info)
|
||||
{
|
||||
struct procps_stat_info *v;
|
||||
v = calloc(1, sizeof(struct procps_stat_info));
|
||||
if (!v)
|
||||
return -ENOMEM;
|
||||
|
||||
v->refcount = 1;
|
||||
v->stat_fd = -1;
|
||||
*info = v;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* procps_stat_read:
|
||||
*
|
||||
* Read the data out of /proc/stat putting the information
|
||||
* into the supplied info structure.
|
||||
*
|
||||
* If CPU stats only needed, set cpu_only to non-zero
|
||||
*/
|
||||
PROCPS_EXPORT int procps_stat_read(struct procps_stat_info *info, const int cpu_only)
|
||||
{
|
||||
char buf[8192];
|
||||
char *head, *tail;
|
||||
int size;
|
||||
|
||||
if (info == NULL)
|
||||
return -1;
|
||||
|
||||
memset(&(info->data), 0, sizeof(struct stat_data));
|
||||
/* read in the data */
|
||||
|
||||
if (-1 == info->stat_fd && (info->stat_fd = open(STAT_FILE, O_RDONLY)) == -1) {
|
||||
return -errno;
|
||||
}
|
||||
if (lseek(info->stat_fd, 0L, SEEK_SET) == -1) {
|
||||
return -errno;
|
||||
}
|
||||
if ((size = read(info->stat_fd, buf, sizeof(buf)-1)) < 0) {
|
||||
return -1;
|
||||
}
|
||||
buf[size] = '\0';
|
||||
|
||||
/* Scan the file */
|
||||
head = buf;
|
||||
do {
|
||||
tail = strchr(head, ' ');
|
||||
if (!tail)
|
||||
break;
|
||||
*tail = '\0';
|
||||
if (0 == strcmp(head, "cpu")) {
|
||||
if (sscanf(tail+1, "%Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu %Lu",
|
||||
&(info->data.cpu_user),
|
||||
&(info->data.cpu_nice),
|
||||
&(info->data.cpu_sys),
|
||||
&(info->data.cpu_idle),
|
||||
&(info->data.cpu_iowait),
|
||||
&(info->data.cpu_irq),
|
||||
&(info->data.cpu_sirq),
|
||||
&(info->data.cpu_stol),
|
||||
&(info->data.cpu_guest),
|
||||
&(info->data.cpu_nice)
|
||||
) != 10)
|
||||
return -1;
|
||||
|
||||
if (cpu_only)
|
||||
return 0; // we got what we need
|
||||
} else if (0 == strcmp(head, "intr")) {
|
||||
info->data.intr = strtoul(tail+1, &tail, 10);
|
||||
} else if (0 == strcmp(head, "ctxt")) {
|
||||
info->data.ctxt = strtoul(tail+1, &tail, 10);
|
||||
} else if (0 == strcmp(head, "btime")) {
|
||||
info->data.btime = strtoul(tail+1, &tail, 10);
|
||||
} else if (0 == strcmp(head, "processes")) {
|
||||
info->data.procs = strtoul(tail+1, &tail, 10);
|
||||
} else if (0 == strcmp(head, "procs_blocked")) {
|
||||
info->data.procs_blocked = strtoul(tail+1, &tail, 10);
|
||||
} else if (0 == strcmp(head, "procs_running")) {
|
||||
info->data.procs_running = strtoul(tail+1, &tail, 10);
|
||||
}
|
||||
if (tail[0] != '\n')
|
||||
tail = strchr(tail+1, '\n');
|
||||
if (!tail)
|
||||
break;
|
||||
head = tail + 1;
|
||||
} while(tail);
|
||||
if (info->data.procs)
|
||||
info->data.procs--; // exclude this process
|
||||
return 0;
|
||||
}
|
||||
|
||||
PROCPS_EXPORT struct procps_stat_info *procps_stat_ref(struct procps_stat_info *info)
|
||||
{
|
||||
if (info == NULL)
|
||||
return NULL;
|
||||
info->refcount++;
|
||||
return info;
|
||||
}
|
||||
|
||||
PROCPS_EXPORT struct procps_stat_info *procps_stat_unref(struct procps_stat_info *info)
|
||||
{
|
||||
if (info == NULL)
|
||||
return NULL;
|
||||
info->refcount--;
|
||||
if (info->refcount > 0)
|
||||
return NULL;
|
||||
free(info);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
PROCPS_EXPORT jiff procps_stat_get_cpu(struct procps_stat_info *info, enum procps_cpu_item item)
|
||||
{
|
||||
switch(item) {
|
||||
case PROCPS_CPU_USER:
|
||||
return info->data.cpu_user;
|
||||
case PROCPS_CPU_NICE:
|
||||
return info->data.cpu_nice;
|
||||
case PROCPS_CPU_SYSTEM:
|
||||
return info->data.cpu_sys;
|
||||
case PROCPS_CPU_IDLE:
|
||||
return info->data.cpu_idle;
|
||||
case PROCPS_CPU_IOWAIT:
|
||||
return info->data.cpu_iowait;
|
||||
case PROCPS_CPU_IRQ:
|
||||
return info->data.cpu_irq;
|
||||
case PROCPS_CPU_SIRQ:
|
||||
return info->data.cpu_sirq;
|
||||
case PROCPS_CPU_STOLEN:
|
||||
return info->data.cpu_stol;
|
||||
case PROCPS_CPU_GUEST:
|
||||
return info->data.cpu_guest;
|
||||
case PROCPS_CPU_GNICE:
|
||||
return info->data.cpu_gnice;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
PROCPS_EXPORT unsigned int procps_stat_get(struct procps_stat_info *info, enum procps_stat_item item)
|
||||
{
|
||||
switch(item) {
|
||||
case PROCPS_STAT_INTERRUPTS:
|
||||
return info->data.intr;
|
||||
case PROCPS_STAT_CONTEXT:
|
||||
return info->data.ctxt;
|
||||
case PROCPS_STAT_BTIME:
|
||||
return info->data.btime;
|
||||
case PROCPS_STAT_PROCS:
|
||||
return info->data.procs;
|
||||
case PROCPS_STAT_PROCS_BLK:
|
||||
return info->data.procs_blocked;
|
||||
case PROCPS_STAT_PROCS_RUN:
|
||||
return info->data.procs_running;
|
||||
}
|
||||
return 0;
|
||||
}
|
56
proc/readstat.h
Normal file
56
proc/readstat.h
Normal file
@ -0,0 +1,56 @@
|
||||
/*
|
||||
* libprocps - Library to read proc filesystem
|
||||
*
|
||||
* 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
|
||||
*/
|
||||
#ifndef PROC_READ_STAT_H
|
||||
#define PROC_READ_STAT_H
|
||||
|
||||
#include <proc/procps.h>
|
||||
|
||||
__BEGIN_DECLS
|
||||
|
||||
enum procps_cpu_item {
|
||||
PROCPS_CPU_USER,
|
||||
PROCPS_CPU_NICE,
|
||||
PROCPS_CPU_SYSTEM,
|
||||
PROCPS_CPU_IDLE,
|
||||
PROCPS_CPU_IOWAIT,
|
||||
PROCPS_CPU_IRQ,
|
||||
PROCPS_CPU_SIRQ,
|
||||
PROCPS_CPU_STOLEN,
|
||||
PROCPS_CPU_GUEST,
|
||||
PROCPS_CPU_GNICE
|
||||
};
|
||||
|
||||
enum procps_stat_item {
|
||||
PROCPS_STAT_INTERRUPTS,
|
||||
PROCPS_STAT_CONTEXT,
|
||||
PROCPS_STAT_BTIME,
|
||||
PROCPS_STAT_PROCS,
|
||||
PROCPS_STAT_PROCS_BLK,
|
||||
PROCPS_STAT_PROCS_RUN
|
||||
};
|
||||
|
||||
typedef unsigned long long jiff;
|
||||
struct procps_stat_info;
|
||||
int procps_stat_new(struct procps_stat_info **info);
|
||||
int procps_stat_read(struct procps_stat_info *info, const int cpu_only);
|
||||
struct procps_stat_info *procps_stat_ref(struct procps_stat_info *info);
|
||||
struct procps_stat_info *procps_stat_unref(struct procps_stat_info *info);
|
||||
jiff procps_stat_get_cpu(struct procps_stat_info *info, enum procps_cpu_item item);
|
||||
unsigned int procps_stat_get(struct procps_stat_info *info, enum procps_stat_item item);
|
||||
__END_DECLS
|
||||
#endif
|
@ -11,9 +11,9 @@
|
||||
* GNU Library General Public License for more details.
|
||||
*/
|
||||
|
||||
#include "procps.h"
|
||||
#include <proc/procps.h>
|
||||
|
||||
EXTERN_C_BEGIN
|
||||
__BEGIN_DECLS
|
||||
|
||||
/* return -1 on failure */
|
||||
extern int signal_name_to_number(const char *__restrict name);
|
||||
@ -28,5 +28,5 @@ extern void pretty_print_signals(void);
|
||||
|
||||
extern void unix_print_signals(void);
|
||||
|
||||
EXTERN_C_END
|
||||
__END_DECLS
|
||||
#endif
|
||||
|
@ -2,21 +2,15 @@
|
||||
#define PROC_SYSINFO_H
|
||||
#include <sys/types.h>
|
||||
#include <dirent.h>
|
||||
#include "procps.h"
|
||||
#include <proc/procps.h>
|
||||
|
||||
EXTERN_C_BEGIN
|
||||
__BEGIN_DECLS
|
||||
|
||||
extern unsigned long long Hertz; /* clock tick frequency */
|
||||
extern long smp_num_cpus; /* number of CPUs */
|
||||
extern int have_privs; /* boolean, true if setuid or similar */
|
||||
extern long page_bytes; /* this architecture's bytes per page */
|
||||
|
||||
#if 0
|
||||
#define JT double
|
||||
extern void eight_cpu_numbers(JT *uret, JT *nret, JT *sret, JT *iret, JT *wret, JT *xret, JT *yret, JT *zret);
|
||||
#undef JT
|
||||
#endif
|
||||
|
||||
extern int uptime (double *uptime_secs, double *idle_secs);
|
||||
extern unsigned long getbtime(void);
|
||||
extern void loadavg(double *av1, double *av5, double *av15);
|
||||
@ -140,5 +134,5 @@ extern unsigned get_pid_digits(void) FUNCTION;
|
||||
|
||||
extern void cpuinfo (void);
|
||||
|
||||
EXTERN_C_END
|
||||
__END_DECLS
|
||||
#endif /* SYSINFO_H */
|
||||
|
@ -23,9 +23,9 @@
|
||||
#ifndef PROC_VERSION_H
|
||||
#define PROC_VERSION_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
#include <proc/procps.h>
|
||||
|
||||
__BEGIN_DECLS
|
||||
|
||||
int procps_linux_version(void);
|
||||
|
||||
@ -35,8 +35,6 @@ int procps_linux_version(void);
|
||||
#define LINUX_VERSION_MINOR(x) (((x)>> 8) & 0xFF)
|
||||
#define LINUX_VERSION_PATCH(x) ( (x) & 0xFF)
|
||||
|
||||
#ifdef __cplusplus
|
||||
} /* extern "C" */
|
||||
#endif
|
||||
__END_DECLS
|
||||
|
||||
#endif /* PROC_VERSION_H */
|
||||
|
153
proc/vmstat.c
Normal file
153
proc/vmstat.c
Normal file
@ -0,0 +1,153 @@
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#include <proc/vmstat.h>
|
||||
#include "procps-private.h"
|
||||
|
||||
#define VMSTAT_FILE "/proc/vmstat"
|
||||
#define ROW_NAME_LEN 32
|
||||
|
||||
struct vmstat_data {
|
||||
unsigned long pgpgin;
|
||||
unsigned long pgpgout;
|
||||
unsigned long pswpin;
|
||||
unsigned long pswpout;
|
||||
};
|
||||
|
||||
struct mem_table_struct {
|
||||
const char *name;
|
||||
unsigned long *slot;
|
||||
};
|
||||
|
||||
struct vmstat_info {
|
||||
int refcount;
|
||||
int vmstat_fd;
|
||||
struct vmstat_data data;
|
||||
};
|
||||
|
||||
/*
|
||||
* procps_vmstat_new:
|
||||
*
|
||||
* Create a new container to hold the vmstat information
|
||||
*
|
||||
* The initial refcount is 1, and needs to be decremented
|
||||
* to release the resources of the structure.
|
||||
*
|
||||
* Returns: a new vmstat info container
|
||||
*/
|
||||
PROCPS_EXPORT int procps_vmstat_new(struct vmstat_info **info)
|
||||
{
|
||||
struct vmstat_info *v;
|
||||
v = calloc(1, sizeof(struct vmstat_info));
|
||||
if (!v)
|
||||
return -ENOMEM;
|
||||
|
||||
v->refcount = 1;
|
||||
v->vmstat_fd = -1;
|
||||
*info = v;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* procps_vmstat_read:
|
||||
*
|
||||
* Read the data out of /proc/vmstat putting the information
|
||||
* into the supplied info structure
|
||||
*/
|
||||
PROCPS_EXPORT int procps_vmstat_read(struct vmstat_info *info)
|
||||
{
|
||||
char buf[8192];
|
||||
char *head, *tail;
|
||||
int size;
|
||||
unsigned long *valptr;
|
||||
|
||||
if (info == NULL)
|
||||
return -1;
|
||||
|
||||
memset(&(info->data), 0, sizeof(struct vmstat_data));
|
||||
/* read in the data */
|
||||
|
||||
if (-1 == info->vmstat_fd && (info->vmstat_fd = open(VMSTAT_FILE, O_RDONLY)) == -1) {
|
||||
return -errno;
|
||||
}
|
||||
if (lseek(info->vmstat_fd, 0L, SEEK_SET) == -1) {
|
||||
return -errno;
|
||||
}
|
||||
if ((size = read(info->vmstat_fd, buf, sizeof(buf)-1)) < 0) {
|
||||
return -1;
|
||||
}
|
||||
buf[size] = '\0';
|
||||
|
||||
/* Scan the file */
|
||||
head = buf;
|
||||
do {
|
||||
tail = strchr(head, ' ');
|
||||
if (!tail)
|
||||
break;
|
||||
*tail = '\0';
|
||||
valptr = NULL;
|
||||
if (0 == strcmp(head, "pgpgin")) {
|
||||
valptr = &(info->data.pgpgin);
|
||||
}else if (0 == strcmp(head, "pgpgout")) {
|
||||
valptr = &(info->data.pgpgout);
|
||||
}else if (0 == strcmp(head, "pswpin")) {
|
||||
valptr = &(info->data.pswpin);
|
||||
}else if (0 == strcmp(head, "pswpout")) {
|
||||
valptr = &(info->data.pswpout);
|
||||
}
|
||||
head = tail+1;
|
||||
if (valptr) {
|
||||
*valptr = strtoul(head, &tail, 10);
|
||||
}
|
||||
|
||||
tail = strchr(head, '\n');
|
||||
if (!tail)
|
||||
break;
|
||||
head = tail + 1;
|
||||
} while(tail);
|
||||
return 0;
|
||||
}
|
||||
|
||||
PROCPS_EXPORT struct vmstat_info *procps_vmstat_ref(struct vmstat_info *info)
|
||||
{
|
||||
if (info == NULL)
|
||||
return NULL;
|
||||
info->refcount++;
|
||||
return info;
|
||||
}
|
||||
|
||||
PROCPS_EXPORT struct vmstat_info *procps_vmstat_unref(struct vmstat_info *info)
|
||||
{
|
||||
if (info == NULL)
|
||||
return NULL;
|
||||
info->refcount--;
|
||||
if (info->refcount > 0)
|
||||
return NULL;
|
||||
free(info);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Accessor functions */
|
||||
PROCPS_EXPORT unsigned long procps_vmstat_get(
|
||||
struct vmstat_info *info,
|
||||
enum vmstat_item item)
|
||||
{
|
||||
switch(item) {
|
||||
case VMSTAT_INFO_PGPGIN:
|
||||
return info->data.pgpgin;
|
||||
case VMSTAT_INFO_PGPGOUT:
|
||||
return info->data.pgpgout;
|
||||
case VMSTAT_INFO_PSWPIN:
|
||||
return info->data.pswpin;
|
||||
case VMSTAT_INFO_PSWPOUT:
|
||||
return info->data.pswpout;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
44
proc/vmstat.h
Normal file
44
proc/vmstat.h
Normal file
@ -0,0 +1,44 @@
|
||||
/*
|
||||
* libprocps - Library to read proc filesystem
|
||||
*
|
||||
* Copyright (C) 1995 Martin Schulze <joey@infodrom.north.de>
|
||||
* Copyright (C) 1996 Charles Blake <cblake@bbn.com>
|
||||
* Copyright (C) 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
|
||||
*/
|
||||
#ifndef PROC_VMSTAT_H
|
||||
#define PROC_VMSTAT_H
|
||||
|
||||
#include <proc/procps.h>
|
||||
|
||||
__BEGIN_DECLS
|
||||
|
||||
struct vmstat_info;
|
||||
int procps_vmstat_new(struct vmstat_info **info);
|
||||
int procps_vmstat_read(struct vmstat_info *info);
|
||||
struct vmstat_info *procps_vmstat_ref(struct vmstat_info *info);
|
||||
struct vmstat_info *procps_vmstat_unref(struct vmstat_info *info);
|
||||
enum vmstat_item {
|
||||
VMSTAT_INFO_PGPGIN,
|
||||
VMSTAT_INFO_PGPGOUT,
|
||||
VMSTAT_INFO_PSWPIN,
|
||||
VMSTAT_INFO_PSWPOUT
|
||||
};
|
||||
unsigned long procps_vmstat_get(struct vmstat_info *info, enum vmstat_item item);
|
||||
|
||||
__END_DECLS
|
||||
#endif
|
@ -1,12 +1,12 @@
|
||||
#ifndef PROCPS_PROC_WCHAN_H
|
||||
#define PROCPS_PROC_WCHAN_H
|
||||
|
||||
#include "procps.h"
|
||||
#include <proc/procps.h>
|
||||
|
||||
EXTERN_C_BEGIN
|
||||
__BEGIN_DECLS
|
||||
|
||||
extern const char * lookup_wchan (int pid);
|
||||
|
||||
EXTERN_C_END
|
||||
__END_DECLS
|
||||
|
||||
#endif
|
||||
|
@ -1,13 +1,13 @@
|
||||
#ifndef PROC_WHATTIME_H
|
||||
#define PROC_WHATTIME_H
|
||||
|
||||
#include "procps.h"
|
||||
#include <proc/procps.h>
|
||||
|
||||
EXTERN_C_BEGIN
|
||||
__BEGIN_DECLS
|
||||
|
||||
extern void print_uptime(int human_readable);
|
||||
extern char *sprint_uptime(int human_readable);
|
||||
|
||||
EXTERN_C_END
|
||||
__END_DECLS
|
||||
|
||||
#endif
|
||||
|
148
vmstat.c
148
vmstat.c
@ -50,6 +50,8 @@
|
||||
#include "strutils.h"
|
||||
#include "proc/sysinfo.h"
|
||||
#include "proc/version.h"
|
||||
#include <proc/vmstat.h>
|
||||
#include <proc/readstat.h>
|
||||
|
||||
#define UNIT_B 1
|
||||
#define UNIT_k 1000
|
||||
@ -285,7 +287,7 @@ static void new_format(void)
|
||||
unsigned int hz = Hertz;
|
||||
unsigned int running, blocked, dummy_1, dummy_2;
|
||||
jiff cpu_use[2], cpu_nic[2], cpu_sys[2], cpu_idl[2], cpu_iow[2],
|
||||
cpu_xxx[2], cpu_yyy[2], cpu_zzz[2];
|
||||
cpu_xxx[2], cpu_yyy[2], cpu_sto[2];
|
||||
jiff duse, dsys, didl, diow, dstl, Div, divo2;
|
||||
unsigned long pgpgin[2], pgpgout[2], pswpin[2], pswpout[2];
|
||||
unsigned int intr[2], ctxt[2];
|
||||
@ -295,14 +297,25 @@ static void new_format(void)
|
||||
struct tm *tm_ptr;
|
||||
time_t the_time;
|
||||
char timebuf[32];
|
||||
struct vmstat_info *vm_info;
|
||||
struct procps_stat_info *sys_info;
|
||||
|
||||
sleep_half = (sleep_time / 2);
|
||||
new_header();
|
||||
meminfo();
|
||||
|
||||
getstat(cpu_use, cpu_nic, cpu_sys, cpu_idl, cpu_iow, cpu_xxx, cpu_yyy,
|
||||
cpu_zzz, pgpgin, pgpgout, pswpin, pswpout, intr, ctxt, &running,
|
||||
&blocked, &dummy_1, &dummy_2);
|
||||
if (procps_vmstat_new(&vm_info) < 0)
|
||||
xerrx(EXIT_FAILURE,
|
||||
_("Unable to create vmstat structure"));
|
||||
if (procps_vmstat_read(vm_info) < 0)
|
||||
xerrx(EXIT_FAILURE,
|
||||
_("Unable to read vmstat information"));
|
||||
if (procps_stat_new(&sys_info) < 0)
|
||||
xerrx(EXIT_FAILURE,
|
||||
_("Unable to create system stat structure"));
|
||||
if (procps_stat_read(sys_info, 0) < 0)
|
||||
xerrx(EXIT_FAILURE,
|
||||
_("Unable to read system stat information"));
|
||||
|
||||
if (t_option) {
|
||||
(void) time( &the_time );
|
||||
@ -310,11 +323,14 @@ static void new_format(void)
|
||||
strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S", tm_ptr);
|
||||
}
|
||||
|
||||
duse = *cpu_use + *cpu_nic;
|
||||
dsys = *cpu_sys + *cpu_xxx + *cpu_yyy;
|
||||
didl = *cpu_idl;
|
||||
diow = *cpu_iow;
|
||||
dstl = *cpu_zzz;
|
||||
duse = procps_stat_get_cpu(sys_info, PROCPS_CPU_USER) +
|
||||
procps_stat_get_cpu(sys_info, PROCPS_CPU_NICE);
|
||||
dsys = procps_stat_get_cpu(sys_info, PROCPS_CPU_SYSTEM) +
|
||||
procps_stat_get_cpu(sys_info, PROCPS_CPU_IRQ) +
|
||||
procps_stat_get_cpu(sys_info, PROCPS_CPU_SIRQ);
|
||||
didl = procps_stat_get_cpu(sys_info, PROCPS_CPU_IDLE);
|
||||
diow = procps_stat_get_cpu(sys_info, PROCPS_CPU_IOWAIT);
|
||||
dstl = procps_stat_get_cpu(sys_info, PROCPS_CPU_STOLEN);
|
||||
Div = duse + dsys + didl + diow + dstl;
|
||||
if (!Div) Div = 1, didl = 1;
|
||||
divo2 = Div / 2UL;
|
||||
@ -323,12 +339,12 @@ static void new_format(void)
|
||||
unitConvert(kb_swap_used), unitConvert(kb_main_free),
|
||||
unitConvert(a_option?kb_inactive:kb_main_buffers),
|
||||
unitConvert(a_option?kb_active:kb_main_cached),
|
||||
(unsigned)( (unitConvert(*pswpin * kb_per_page) * hz + divo2) / Div ),
|
||||
(unsigned)( (unitConvert(*pswpout * kb_per_page) * hz + divo2) / Div ),
|
||||
(unsigned)( (*pgpgin * hz + divo2) / Div ),
|
||||
(unsigned)( (*pgpgout * hz + divo2) / Div ),
|
||||
(unsigned)( (*intr * hz + divo2) / Div ),
|
||||
(unsigned)( (*ctxt * hz + divo2) / Div ),
|
||||
(unsigned)( (unitConvert(procps_vmstat_get(vm_info, VMSTAT_INFO_PSWPIN) * kb_per_page) * hz + divo2) / Div ),
|
||||
(unsigned)( (unitConvert(procps_vmstat_get(vm_info, VMSTAT_INFO_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, VMSTAT_INFO_PGPGOUT) * 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)( (100*duse + divo2) / Div ),
|
||||
(unsigned)( (100*dsys + divo2) / Div ),
|
||||
(unsigned)( (100*didl + divo2) / Div ),
|
||||
@ -351,11 +367,26 @@ static void new_format(void)
|
||||
|
||||
meminfo();
|
||||
|
||||
getstat(cpu_use + tog, cpu_nic + tog, cpu_sys + tog,
|
||||
cpu_idl + tog, cpu_iow + tog, cpu_xxx + tog,
|
||||
cpu_yyy + tog, cpu_zzz + tog, pgpgin + tog,
|
||||
pgpgout + tog, pswpin + tog, pswpout + tog, intr + tog,
|
||||
ctxt + tog, &running, &blocked, &dummy_1, &dummy_2);
|
||||
if (procps_stat_read(sys_info,0) < 0)
|
||||
xerrx(EXIT_FAILURE,
|
||||
_("Unable to read system stat information"));
|
||||
cpu_use[tog] = procps_stat_get_cpu(sys_info, PROCPS_CPU_USER);
|
||||
cpu_nic[tog] = procps_stat_get_cpu(sys_info, PROCPS_CPU_NICE);
|
||||
cpu_sys[tog] = procps_stat_get_cpu(sys_info, PROCPS_CPU_SYSTEM);
|
||||
procps_stat_get_cpu(sys_info, PROCPS_CPU_IRQ) +
|
||||
procps_stat_get_cpu(sys_info, PROCPS_CPU_SIRQ);
|
||||
cpu_idl[tog] = procps_stat_get_cpu(sys_info, PROCPS_CPU_IDLE);
|
||||
cpu_iow[tog] = procps_stat_get_cpu(sys_info, PROCPS_CPU_IOWAIT);
|
||||
cpu_sto[tog] = procps_stat_get_cpu(sys_info, PROCPS_CPU_STOLEN);
|
||||
|
||||
if (procps_vmstat_read(vm_info) < 0)
|
||||
xerrx(EXIT_FAILURE,
|
||||
_("Unable to read vmstat information"));
|
||||
pgpgin[tog] = procps_vmstat_get(vm_info, VMSTAT_INFO_PGPGIN);
|
||||
pgpgout[tog] = procps_vmstat_get(vm_info, VMSTAT_INFO_PGPGOUT);
|
||||
pswpin[tog] = procps_vmstat_get(vm_info, VMSTAT_INFO_PSWPIN);
|
||||
pswpout[tog] = procps_vmstat_get(vm_info, VMSTAT_INFO_PSWPOUT);
|
||||
|
||||
|
||||
if (t_option) {
|
||||
(void) time( &the_time );
|
||||
@ -370,7 +401,7 @@ static void new_format(void)
|
||||
cpu_xxx[!tog] + cpu_yyy[tog] - cpu_yyy[!tog];
|
||||
didl = cpu_idl[tog] - cpu_idl[!tog];
|
||||
diow = cpu_iow[tog] - cpu_iow[!tog];
|
||||
dstl = cpu_zzz[tog] - cpu_zzz[!tog];
|
||||
dstl = cpu_sto[tog] - cpu_sto[!tog];
|
||||
|
||||
/* idle can run backwards for a moment -- kernel "feature" */
|
||||
if (debt) {
|
||||
@ -783,17 +814,23 @@ static void disksum_format(void)
|
||||
|
||||
static void sum_format(void)
|
||||
{
|
||||
unsigned int running, blocked, btime, processes;
|
||||
jiff cpu_use, cpu_nic, cpu_sys, cpu_idl, cpu_iow, cpu_xxx, cpu_yyy, cpu_zzz;
|
||||
unsigned long pgpgin, pgpgout, pswpin, pswpout;
|
||||
unsigned int intr, ctxt;
|
||||
struct procps_stat_info *sys_info;
|
||||
struct vmstat_info *vm_info;
|
||||
|
||||
meminfo();
|
||||
|
||||
getstat(&cpu_use, &cpu_nic, &cpu_sys, &cpu_idl,
|
||||
&cpu_iow, &cpu_xxx, &cpu_yyy, &cpu_zzz,
|
||||
&pgpgin, &pgpgout, &pswpin, &pswpout,
|
||||
&intr, &ctxt, &running, &blocked, &btime, &processes);
|
||||
if (procps_stat_new(&sys_info) < 0)
|
||||
xerrx(EXIT_FAILURE,
|
||||
_("Unable to create system stat structure"));
|
||||
if (procps_stat_read(sys_info,0) < 0)
|
||||
xerrx(EXIT_FAILURE,
|
||||
_("Unable to read system stat information"));
|
||||
if (procps_vmstat_new(&vm_info) < 0)
|
||||
xerrx(EXIT_FAILURE,
|
||||
_("Unable to create vmstat structure"));
|
||||
if (procps_vmstat_read(vm_info) < 0)
|
||||
xerrx(EXIT_FAILURE,
|
||||
_("Unable to read vmstat information"));
|
||||
|
||||
printf(_("%13lu %s total memory\n"), unitConvert(kb_main_total), szDataUnit);
|
||||
printf(_("%13lu %s used memory\n"), unitConvert(kb_main_used), szDataUnit);
|
||||
@ -805,37 +842,38 @@ static void sum_format(void)
|
||||
printf(_("%13lu %s total swap\n"), unitConvert(kb_swap_total), szDataUnit);
|
||||
printf(_("%13lu %s used swap\n"), unitConvert(kb_swap_used), szDataUnit);
|
||||
printf(_("%13lu %s free swap\n"), unitConvert(kb_swap_free), szDataUnit);
|
||||
printf(_("%13lld non-nice user cpu ticks\n"), cpu_use);
|
||||
printf(_("%13lld nice user cpu ticks\n"), cpu_nic);
|
||||
printf(_("%13lld system cpu ticks\n"), cpu_sys);
|
||||
printf(_("%13lld idle cpu ticks\n"), cpu_idl);
|
||||
printf(_("%13lld IO-wait cpu ticks\n"), cpu_iow);
|
||||
printf(_("%13lld IRQ cpu ticks\n"), cpu_xxx);
|
||||
printf(_("%13lld softirq cpu ticks\n"), cpu_yyy);
|
||||
printf(_("%13lld stolen cpu ticks\n"), cpu_zzz);
|
||||
printf(_("%13lu pages paged in\n"), pgpgin);
|
||||
printf(_("%13lu pages paged out\n"), pgpgout);
|
||||
printf(_("%13lu pages swapped in\n"), pswpin);
|
||||
printf(_("%13lu pages swapped out\n"), pswpout);
|
||||
printf(_("%13u interrupts\n"), intr);
|
||||
printf(_("%13u CPU context switches\n"), ctxt);
|
||||
printf(_("%13u boot time\n"), btime);
|
||||
printf(_("%13u forks\n"), processes);
|
||||
printf(_("%13lld non-nice user cpu ticks\n"), procps_stat_get_cpu(sys_info, PROCPS_CPU_USER));
|
||||
printf(_("%13lld nice user cpu ticks\n"), procps_stat_get_cpu(sys_info, PROCPS_CPU_NICE));
|
||||
printf(_("%13lld system cpu ticks\n"), procps_stat_get_cpu(sys_info, PROCPS_CPU_SYSTEM));
|
||||
printf(_("%13lld idle cpu ticks\n"), procps_stat_get_cpu(sys_info, PROCPS_CPU_IDLE));
|
||||
printf(_("%13lld IO-wait cpu ticks\n"), procps_stat_get_cpu(sys_info, PROCPS_CPU_IOWAIT));
|
||||
printf(_("%13lld IRQ cpu ticks\n"), procps_stat_get_cpu(sys_info, PROCPS_CPU_IRQ));
|
||||
printf(_("%13lld softirq cpu ticks\n"), procps_stat_get_cpu(sys_info, PROCPS_CPU_SIRQ));
|
||||
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 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 out\n"), procps_vmstat_get(vm_info, VMSTAT_INFO_PGPGOUT));
|
||||
printf(_("%13lu pages swapped in\n"), procps_vmstat_get(vm_info, VMSTAT_INFO_PSWPIN));
|
||||
printf(_("%13lu pages swapped out\n"), procps_vmstat_get(vm_info, VMSTAT_INFO_PSWPOUT));
|
||||
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 boot time\n"), procps_stat_get(sys_info, PROCPS_STAT_BTIME));
|
||||
printf(_("%13u forks\n"), procps_stat_get(sys_info, PROCPS_STAT_PROCS));
|
||||
}
|
||||
|
||||
static void fork_format(void)
|
||||
{
|
||||
unsigned int running, blocked, btime, processes;
|
||||
jiff cpu_use, cpu_nic, cpu_sys, cpu_idl, cpu_iow, cpu_xxx, cpu_yyy, cpu_zzz;
|
||||
unsigned long pgpgin, pgpgout, pswpin, pswpout;
|
||||
unsigned int intr, ctxt;
|
||||
struct procps_stat_info *sys_info;
|
||||
|
||||
getstat(&cpu_use, &cpu_nic, &cpu_sys, &cpu_idl,
|
||||
&cpu_iow, &cpu_xxx, &cpu_yyy, &cpu_zzz,
|
||||
&pgpgin, &pgpgout, &pswpin, &pswpout,
|
||||
&intr, &ctxt, &running, &blocked, &btime, &processes);
|
||||
|
||||
printf(_("%13u forks\n"), processes);
|
||||
if (procps_stat_new(&sys_info) < 0)
|
||||
xerrx(EXIT_FAILURE,
|
||||
_("Unable to create system stat structure"));
|
||||
if (procps_stat_read(sys_info,0) < 0)
|
||||
xerrx(EXIT_FAILURE,
|
||||
_("Unable to read system stat information"));
|
||||
|
||||
printf(_("%13u forks\n"), procps_stat_get(sys_info, PROCPS_STAT_PROCS));
|
||||
}
|
||||
|
||||
static int winhi(void)
|
||||
|
Loading…
Reference in New Issue
Block a user