/*
* pg_top - a top PostgreSQL users display for Unix
*
* SYNOPSIS: Linux 1.2.x, 1.3.x, 2.x, using the /proc filesystem
*
* DESCRIPTION:
* This is the machine-dependent module for Linux 1.2.x, 1.3.x or 2.x.
*
* LIBS:
*
* CFLAGS: -DHAVE_GETOPT -DHAVE_STRERROR -DORDER
*
* TERMCAP: -lcurses
*
* AUTHOR: Richard Henderson <rth@tamu.edu>
* Order support added by Alexey Klimkin <kad@klon.tme.mcst.ru>
* Ported to 2.4 by William LeFebvre
*
* Portions Copyright (c) 2013 VMware, Inc. All Rights Reserved.
*/
#include "config.h"
#include <sys/types.h>
#include <time.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#ifdef __linux__
#include <bsd/stdlib.h>
#include <bsd/sys/tree.h>
#endif /* __linux__ */
#include <errno.h>
#include <dirent.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/vfs.h>
#include <sys/param.h> /* for HZ */
#if 0
#include <linux/proc_fs.h> /* for PROC_SUPER_MAGIC */
#else
#define PROC_SUPER_MAGIC 0x9fa0
#endif
#define BUFFERLEN 255
#define GET_VALUE(v) \
p = strchr(p, ':'); \
++p; \
++p; \
q = strchr(p, '\n'); \
len = q - p; \
if (len >= BUFFERLEN) \
{ \
printf("ERROR - value is larger than the buffer: %d\n", __LINE__); \
exit(1); \
} \
strncpy(value, p, len); \
value[len] = '\0'; \
v = atoll(value);
#include "machine.h"
#include "utils.h"
#define PROCFS "/proc"
extern char *myname;
/*=PROCESS INFORMATION==================================================*/
struct top_proc
{
RB_ENTRY(top_proc) entry;
pid_t pid;
/* Data from /proc/<pid>/stat. */
char *name;
char *usename;
unsigned long size,
rss; /* in k */
int state;
int pgstate;
unsigned long time;
unsigned long start_time;
unsigned long xtime;
unsigned long qtime;
double pcpu,
wcpu;
/* Data from /proc/<pid>/io. */
long long rchar;
long long wchar;
long long syscr;
long long syscw;
long long read_bytes;
long long write_bytes;
long long cancelled_write_bytes;
/* The change in the previous values and current values. */
long long diff_rchar;
long long diff_wchar;
long long diff_syscr;
long long diff_syscw;
long long diff_read_bytes;
long long diff_write_bytes;
long long diff_cancelled_write_bytes;
};
int topproccmp(struct top_proc *, struct top_proc *);
RB_HEAD(pgproc, top_proc) head_proc = RB_INITIALIZER(&head_proc);
RB_PROTOTYPE(pgproc, top_proc, entry, topproccmp)
RB_GENERATE(pgproc, top_proc,