#include <sys/param.h>
#include <sys/sysctl.h>
#include <sys/user.h>
#include <curses.h>
#include <libprocstat.h>
#include <libutil.h>
#include <pwd.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "systat.h"
#include "extern.h"
static struct swapvm {
uint64_t kvo_me;
uint32_t swapped;
uint64_t next;
pid_t pid;
} *swobj = NULL;
static int nswobj = 0;
static struct procstat *prstat = NULL;
static struct proc_usage {
pid_t pid;
uid_t uid;
char command[COMMLEN + 1];
uint64_t total;
uint32_t pages;
} **pu = NULL;
static int nproc;
static int proc_compar(const void *, const void *);
static void
display_proc_line(int idx, int y, uint64_t totalswappages)
{
int offset = 0, rate;
const char *uname;
char buf[30];
uint64_t swapbytes;
wmove(wnd, y, 0);
wclrtoeol(wnd);
if (idx >= nproc)
return;
uname = user_from_uid(pu[idx]->uid, 0);
swapbytes = ptoa(pu[idx]->pages);
snprintf(buf, sizeof(buf), "%6d %-10s %-10.10s", pu[idx]->pid, uname,
pu[idx]->command);
offset = 6 + 1 + 10 + 1 + 10 + 1;
mvwaddstr(wnd, y, 0, buf);
sysputuint64(wnd, y, offset, 4, swapbytes, 0);
offset += 4;
mvwaddstr(wnd, y, offset, " / ");
offset += 3;
sysputuint64(wnd, y, offset, 4, pu[idx]->total, 0);
offset += 4;
rate = pu[idx]->total > 1 ? 100 * swapbytes / pu[idx]->total : 0;
snprintf(buf, sizeof(buf), "%3d%%", rate);
mvwaddstr(wnd, y, offset, buf);
if (rate > 100)
rate = 100;
sysputXs(wnd, y, offset + 5, rate / 10);
rate = 100 * pu[idx]->pages / totalswappages;
snprintf(buf, sizeof(buf), "%3d%%", rate);
mvwaddstr(wnd, y, offset + 16, buf);
if (rate > 100)
rate = 100;
sysputXs(wnd, y, offset + 21, rate / 10);
}
static int
swobj_search(const void *a, const void *b)
{
const uint64_t *aa = a;
const struct swapvm *bb = b;
if (*aa == bb->kvo_me)
return (0);
return (*aa > bb->kvo_me ? -1 : 1);
}
static int
swobj_sort(const void *a, const void *b)
{
return ((((const struct swapvm *) a)->kvo_me >
((const struct swapvm *) b)->kvo_me) ? -1 : 1);
}
static bool
get_swap_vmobjects(void)
{
static int maxnobj;
int cnt, i, next_i, last_nswobj;
struct kinfo_vmobject *kvo;
next_i = nswobj = 0;
kvo = kinfo_getswapvmobject(&cnt);
if (kvo == NULL) {
error("kinfo_getswapvmobject()");
return (false);
}
do {
for (i = next_i; i < cnt; i++) {
if (kvo[i].kvo_type != KVME_TYPE_DEFAULT &&
kvo[i].kvo_type != KVME_TYPE_SWAP)
continue;
if (nswobj < maxnobj) {
swobj[nswobj].kvo_me = kvo[i].kvo_me;
swobj[nswobj].swapped = kvo[i].kvo_swapped;
swobj[nswobj].next = kvo[i].kvo_backing_obj;
swobj[nswobj].pid = 0;
next_i = i + 1;
}
nswobj++;
}
if (nswobj <= maxnobj)
break;
last_nswobj = maxnobj;
maxnobj = nswobj;
nswobj = last_nswobj;
if ((swobj = reallocf(swobj, maxnobj * sizeof(*swobj))) ==
NULL) {
error("Out of memory");
die(0);
}
} while (i <= cnt);
free(kvo);
if (nswobj > 1)
qsort(swobj, nswobj, sizeof(swobj[0]), swobj_sort);
return (nswobj > 0);
}
static uint32_t
per_proc_swap_usage(struct kinfo_proc *kipp)
{
int i, cnt;
uint32_t pages = 0;
uint64_t vmobj;
struct kinfo_vmentry *freep, *kve;
struct swapvm *vm;
freep = procstat_getvmmap(prstat, kipp, &cnt);
if (freep == NULL)
return (pages);
for (i = 0; i < cnt; i++) {
kve = &freep[i];
if (kve->kve_type == KVME_TYPE_DEFAULT ||
kve->kve_type == KVME_TYPE_SWAP) {
vmobj = kve->kve_obj;
do {
vm = bsearch(&vmobj, swobj, nswobj,
sizeof(swobj[0]), swobj_search);
if (vm != NULL && vm->pid != kipp->ki_pid) {
pages += vm->swapped;
vmobj = vm->next;
vm->pid = kipp->ki_pid;
} else
break;
} while (vmobj != 0);
}
}
procstat_freevmmap(prstat, freep);
return (pages);
}
void
procshow(int lcol, int hight, uint64_t totalswappages)
{
int i, y;
for (i = 0, y = lcol + 1 ; i < hight; i++, y++)
display_proc_line(i, y, totalswappages);
}
int
procinit(void)
{
if (prstat == NULL)
prstat = procstat_open_sysctl();
return (prstat != NULL);
}
void
procgetinfo(void)
{
static int maxnproc = 0;
int cnt, i;
uint32_t pages;
struct kinfo_proc *kipp;
nproc = 0;
if ( ! get_swap_vmobjects() )
return;
kipp = procstat_getprocs(prstat, KERN_PROC_PROC, 0, &cnt);
if (kipp == NULL) {
error("procstat_getprocs()");
return;
}
if (maxnproc < cnt) {
if ((pu = realloc(pu, cnt * sizeof(*pu))) == NULL) {
error("Out of memory");
die(0);
}
memset(&pu[maxnproc], 0, (cnt - maxnproc) * sizeof(pu[0]));
maxnproc = cnt;
}
for (i = 0; i < cnt; i++) {
pages = per_proc_swap_usage(&kipp[i]);
if (pages == 0)
continue;
if (pu[nproc] == NULL &&
(pu[nproc] = malloc(sizeof(**pu))) == NULL) {
error("Out of memory");
die(0);
}
strlcpy(pu[nproc]->command, kipp[i].ki_comm,
sizeof(pu[nproc]->command));
pu[nproc]->pid = kipp[i].ki_pid;
pu[nproc]->uid = kipp[i].ki_uid;
pu[nproc]->pages = pages;
pu[nproc]->total = kipp[i].ki_size;
nproc++;
}
if (nproc > 1)
qsort(pu, nproc, sizeof(*pu), proc_compar);
procstat_freeprocs(prstat, kipp);
}
void
proclabel(int lcol)
{
wmove(wnd, lcol, 0);
wclrtoeol(wnd);
mvwaddstr(wnd, lcol, 0,
"Pid Username Command Swap/Total "
"Per-Process Per-System");
}
int
proc_compar(const void *a, const void *b)
{
const struct proc_usage *aa = *((const struct proc_usage **)a);
const struct proc_usage *bb = *((const struct proc_usage **)b);
return (aa->pages > bb->pages ? -1 : 1);
}