The same three paints meant different things on the four tabs (user/system/nice, used/cached, read/write, receive/send) and nothing said which; each tab has a "Graph: ..." line under its numbers now. The Disk and Network graphs stack read on write but were scaled to the larger of the two, so a second with both could clip: they scale to the highest stacked column (Peak sums the two). Seen in a PID-namespace session with a disk writer: the line fits under the numbers, the red writes fill the height at the peak. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1052 lines
28 KiB
C
1052 lines
28 KiB
C
/*
|
|
* dtactivity.c -- MCDE: the activity window (t0091)
|
|
*
|
|
* Copyright (c) 2026 The Makronix project. GNU LGPL 2.0 or later, as CDE.
|
|
*
|
|
* The reference's activity monitor: a toolbar (Quit Process, Inspect, a Show
|
|
* menu and a filter), a table of the processes that sorts by the column you
|
|
* click, and under it four tabs -- CPU, System Memory, Disk Activity and
|
|
* Network -- each with the numbers and a history graph. Everything the
|
|
* window knows comes from McdeSys (mcde_sys_processes, _cpu_times, _memory,
|
|
* _disk_io, _net_io, _signal): no call of ps(1) or top(1).
|
|
*
|
|
* Frugal (tasksd t0080's rules): one timer, every two seconds, and only while
|
|
* the window is on the screen -- not iconified (UnmapNotify), not fully
|
|
* covered (VisibilityNotify). Hidden, it asks the system for nothing and
|
|
* sends nothing to the X server. No animation; the graphs are drawn on
|
|
* Expose and after a sample.
|
|
*
|
|
* Quit Process asks, then sends SIGTERM; Force Quit sends SIGKILL. A
|
|
* process of another user answers "belongs to somebody else": the
|
|
* administrator's way (t0079) is not wired in yet.
|
|
*
|
|
* C99, POSIX, Xt and Motif only (ext/src/mcde/AGENTS.md).
|
|
*/
|
|
|
|
#define _POSIX_C_SOURCE 200809L
|
|
|
|
#include <ctype.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <strings.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
#include <sys/types.h>
|
|
|
|
#include <X11/Intrinsic.h>
|
|
#include <Xm/DrawingA.h>
|
|
#include <Xm/Form.h>
|
|
#include <Xm/Label.h>
|
|
#include <Xm/List.h>
|
|
#include <Xm/MessageB.h>
|
|
#include <Xm/PushB.h>
|
|
#include <Xm/RowColumn.h>
|
|
#include <Xm/TextF.h>
|
|
#include <Xm/ToggleB.h>
|
|
|
|
#include <Mcde/McdeSys.h>
|
|
|
|
#define TICK_MS 2000
|
|
#define HISTORY 90 /* samples in a graph: three minutes */
|
|
#define GRAPH_H 96
|
|
|
|
/* one process with its share of the CPU since the last sample */
|
|
typedef struct {
|
|
McdeSysProcess p;
|
|
double cpu; /* per cent of one processor */
|
|
} Row;
|
|
|
|
enum { COL_PID, COL_NAME, COL_USER, COL_CPU, COL_THREADS, COL_REAL,
|
|
COL_VIRT, NCOLS };
|
|
enum { SHOW_ALL, SHOW_MINE, SHOW_SYSTEM };
|
|
enum { TAB_CPU, TAB_MEMORY, TAB_DISK, TAB_NET, NTABS };
|
|
|
|
/* column titles, widths in characters, and where each starts (see Format) */
|
|
static const char *const titles[NCOLS] = { "PID", "Process Name", "User",
|
|
"% CPU", "Threads", "Real Mem", "Virtual Mem" };
|
|
/* each column is this many characters with its gap; text left, numbers right */
|
|
static const int widths[NCOLS] = { 8, 24, 11, 8, 9, 11, 12 };
|
|
|
|
static XtAppContext app;
|
|
static Widget shell, list, header, filterField, graph, values;
|
|
static Widget tabButtons[NTABS];
|
|
static int sortCol = COL_CPU, sortDesc = 1, show = SHOW_ALL, tab = TAB_CPU;
|
|
static char filter[64];
|
|
static int charW = 6;
|
|
|
|
/* the samples */
|
|
static Row *all;
|
|
static size_t nall;
|
|
static McdeSysProcess *prev; /* the last sample, sorted by pid */
|
|
static size_t nprev;
|
|
static Row **shown; /* the table, in order */
|
|
static size_t nshown;
|
|
static double prevT;
|
|
static McdeSysCpu *prevCpu;
|
|
static size_t nprevCpu;
|
|
static McdeSysDiskIo *prevDisk;
|
|
static size_t nprevDisk;
|
|
static McdeSysNetIo *prevNet;
|
|
static size_t nprevNet;
|
|
static double lastUser, lastSystem, lastNice, lastIdle;
|
|
static McdeSysMemory mem;
|
|
static long nthreads;
|
|
static int nhist;
|
|
static double hUser[HISTORY], hSystem[HISTORY], hNice[HISTORY];
|
|
static double hUsed[HISTORY], hCached[HISTORY];
|
|
static double hDiskR[HISTORY], hDiskW[HISTORY];
|
|
static double hNetR[HISTORY], hNetT[HISTORY];
|
|
static double rateDiskR, rateDiskW, rateNetR, rateNetT;
|
|
static unsigned long long totalDiskR, totalDiskW, totalNetR, totalNetT;
|
|
|
|
static Boolean mapped, obscured;
|
|
static XtIntervalId tick;
|
|
static GC gc;
|
|
static Pixel cUser, cSystem, cNice, cBack, cGrid;
|
|
|
|
static double
|
|
Now(void)
|
|
{
|
|
struct timespec t;
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &t);
|
|
return (double) t.tv_sec + (double) t.tv_nsec / 1e9;
|
|
}
|
|
|
|
/* 1536 -> "1.5 KB" */
|
|
static void
|
|
Human(unsigned long long b, char *out, size_t len)
|
|
{
|
|
static const char *const unit[] = { "B", "KB", "MB", "GB", "TB" };
|
|
double v = (double) b;
|
|
int u = 0;
|
|
|
|
while (v >= 1024.0 && u < 4) {
|
|
v /= 1024.0;
|
|
u++;
|
|
}
|
|
if (u == 0)
|
|
snprintf(out, len, "%llu %s", b, unit[u]);
|
|
else
|
|
snprintf(out, len, "%.1f %s", v, unit[u]);
|
|
}
|
|
|
|
static void
|
|
SetLabel(Widget w, const char *text)
|
|
{
|
|
XmString s = XmStringCreateLtoR((char *) text,
|
|
XmFONTLIST_DEFAULT_TAG);
|
|
|
|
XtVaSetValues(w, XmNlabelString, s, NULL);
|
|
XmStringFree(s);
|
|
}
|
|
|
|
/* ---- sampling --------------------------------------------------------- */
|
|
|
|
static int
|
|
ByPid(const void *a, const void *b)
|
|
{
|
|
long x = ((const McdeSysProcess *) a)->pid;
|
|
long y = ((const McdeSysProcess *) b)->pid;
|
|
|
|
return x < y ? -1 : x > y;
|
|
}
|
|
|
|
static void
|
|
Push(double *h, double v)
|
|
{
|
|
memmove(h, h + 1, (HISTORY - 1) * sizeof *h);
|
|
h[HISTORY - 1] = v;
|
|
}
|
|
|
|
/* a disk that stands on another one is not added up: device-mapper and md
|
|
* volumes (Linux), wedges (NetBSD) */
|
|
static int
|
|
Stacked(const char *name)
|
|
{
|
|
return strncmp(name, "dm-", 3) == 0 || strncmp(name, "md", 2) == 0 ||
|
|
strncmp(name, "dk", 2) == 0;
|
|
}
|
|
|
|
static void
|
|
SampleProcesses(double t)
|
|
{
|
|
McdeSysProcess *v;
|
|
size_t n, i;
|
|
double dt = t - prevT;
|
|
|
|
if (mcde_sys_processes(&v, &n) != MCDE_SYS_OK)
|
|
return;
|
|
free(all);
|
|
all = malloc((n != 0 ? n : 1) * sizeof *all);
|
|
nthreads = 0;
|
|
for (i = 0; i < n && all != NULL; i++) {
|
|
McdeSysProcess key, *old;
|
|
|
|
all[i].p = v[i];
|
|
all[i].cpu = 0;
|
|
nthreads += v[i].nlwp;
|
|
key.pid = v[i].pid;
|
|
old = prev != NULL ? bsearch(&key, prev, nprev, sizeof *prev,
|
|
ByPid) : NULL;
|
|
if (old != NULL && dt > 0.1 && v[i].cpu_ms >= old->cpu_ms)
|
|
all[i].cpu = (double) (v[i].cpu_ms - old->cpu_ms) /
|
|
(dt * 10.0); /* ms per second -> per cent */
|
|
}
|
|
nall = all != NULL ? n : 0;
|
|
qsort(v, n, sizeof *v, ByPid);
|
|
free(prev);
|
|
prev = v;
|
|
nprev = n;
|
|
}
|
|
|
|
static void
|
|
SampleCpu(void)
|
|
{
|
|
McdeSysCpu *c;
|
|
size_t n, i;
|
|
double u = 0, s = 0, ni = 0, id = 0, ot = 0, tot;
|
|
|
|
if (mcde_sys_cpu_times(&c, &n) != MCDE_SYS_OK)
|
|
return;
|
|
if (prevCpu != NULL && n == nprevCpu) {
|
|
for (i = 0; i < n; i++) {
|
|
u += (double) (c[i].user - prevCpu[i].user);
|
|
s += (double) (c[i].system - prevCpu[i].system);
|
|
ni += (double) (c[i].nice - prevCpu[i].nice);
|
|
id += (double) (c[i].idle - prevCpu[i].idle);
|
|
ot += (double) (c[i].other - prevCpu[i].other);
|
|
}
|
|
tot = u + s + ni + id + ot;
|
|
if (tot > 0) {
|
|
lastUser = 100.0 * u / tot;
|
|
lastSystem = 100.0 * (s + ot) / tot;
|
|
lastNice = 100.0 * ni / tot;
|
|
lastIdle = 100.0 * id / tot;
|
|
Push(hUser, lastUser);
|
|
Push(hSystem, lastSystem);
|
|
Push(hNice, lastNice);
|
|
}
|
|
}
|
|
free(prevCpu);
|
|
prevCpu = c;
|
|
nprevCpu = n;
|
|
}
|
|
|
|
static void
|
|
SampleDisk(double dt)
|
|
{
|
|
McdeSysDiskIo *d;
|
|
size_t n, i, j;
|
|
unsigned long long r = 0, w = 0, dr = 0, dw = 0;
|
|
|
|
if (mcde_sys_disk_io(&d, &n) != MCDE_SYS_OK)
|
|
return;
|
|
for (i = 0; i < n; i++) {
|
|
if (Stacked(d[i].name))
|
|
continue;
|
|
r += d[i].read_bytes;
|
|
w += d[i].write_bytes;
|
|
for (j = 0; j < nprevDisk; j++)
|
|
if (strcmp(d[i].name, prevDisk[j].name) == 0) {
|
|
if (d[i].read_bytes >= prevDisk[j].read_bytes)
|
|
dr += d[i].read_bytes -
|
|
prevDisk[j].read_bytes;
|
|
if (d[i].write_bytes >= prevDisk[j].write_bytes)
|
|
dw += d[i].write_bytes -
|
|
prevDisk[j].write_bytes;
|
|
}
|
|
}
|
|
totalDiskR = r;
|
|
totalDiskW = w;
|
|
if (prevDisk != NULL && dt > 0.1) {
|
|
rateDiskR = (double) dr / dt;
|
|
rateDiskW = (double) dw / dt;
|
|
Push(hDiskR, rateDiskR);
|
|
Push(hDiskW, rateDiskW);
|
|
}
|
|
free(prevDisk);
|
|
prevDisk = d;
|
|
nprevDisk = n;
|
|
}
|
|
|
|
static void
|
|
SampleNet(double dt)
|
|
{
|
|
McdeSysNetIo *d;
|
|
size_t n, i, j;
|
|
unsigned long long r = 0, t = 0, dr = 0, dt2 = 0;
|
|
|
|
if (mcde_sys_net_io(&d, &n) != MCDE_SYS_OK)
|
|
return;
|
|
for (i = 0; i < n; i++) {
|
|
if (strncmp(d[i].name, "lo", 2) == 0)
|
|
continue;
|
|
r += d[i].rx_bytes;
|
|
t += d[i].tx_bytes;
|
|
for (j = 0; j < nprevNet; j++)
|
|
if (strcmp(d[i].name, prevNet[j].name) == 0) {
|
|
if (d[i].rx_bytes >= prevNet[j].rx_bytes)
|
|
dr += d[i].rx_bytes -
|
|
prevNet[j].rx_bytes;
|
|
if (d[i].tx_bytes >= prevNet[j].tx_bytes)
|
|
dt2 += d[i].tx_bytes -
|
|
prevNet[j].tx_bytes;
|
|
}
|
|
}
|
|
totalNetR = r;
|
|
totalNetT = t;
|
|
if (prevNet != NULL && dt > 0.1) {
|
|
rateNetR = (double) dr / dt;
|
|
rateNetT = (double) dt2 / dt;
|
|
Push(hNetR, rateNetR);
|
|
Push(hNetT, rateNetT);
|
|
}
|
|
free(prevNet);
|
|
prevNet = d;
|
|
nprevNet = n;
|
|
}
|
|
|
|
static void
|
|
Sample(void)
|
|
{
|
|
double t = Now(), dt = t - prevT;
|
|
|
|
SampleProcesses(t);
|
|
SampleCpu();
|
|
if (mcde_sys_memory(&mem) == MCDE_SYS_OK && mem.total > 0) {
|
|
Push(hUsed, (double) mem.used / (double) mem.total);
|
|
Push(hCached, (double) mem.cached / (double) mem.total);
|
|
}
|
|
SampleDisk(dt);
|
|
SampleNet(dt);
|
|
prevT = t;
|
|
if (nhist < HISTORY)
|
|
nhist++;
|
|
}
|
|
|
|
/* ---- the table -------------------------------------------------------- */
|
|
|
|
/* HAY contains NEEDLE, upper and lower case alike (strcasestr(3) is not
|
|
* POSIX) */
|
|
static int
|
|
Contains(const char *hay, const char *needle)
|
|
{
|
|
size_t n = strlen(needle), i;
|
|
|
|
if (n == 0)
|
|
return 1;
|
|
for (; *hay != '\0'; hay++) {
|
|
for (i = 0; i < n; i++)
|
|
if (hay[i] == '\0' || tolower((unsigned char) hay[i]) !=
|
|
tolower((unsigned char) needle[i]))
|
|
break;
|
|
if (i == n)
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static int
|
|
Matches(const Row *r)
|
|
{
|
|
char num[32];
|
|
|
|
if (show == SHOW_MINE && r->p.uid != (unsigned long) getuid())
|
|
return 0;
|
|
if (show == SHOW_SYSTEM && r->p.uid >= 1000)
|
|
return 0;
|
|
if (filter[0] == '\0')
|
|
return 1;
|
|
snprintf(num, sizeof num, "%ld", r->p.pid);
|
|
return Contains(r->p.comm, filter) || Contains(r->p.user, filter) ||
|
|
strstr(num, filter) != NULL;
|
|
}
|
|
|
|
static int
|
|
Compare(const void *a, const void *b)
|
|
{
|
|
const Row *x = *(Row *const *) a, *y = *(Row *const *) b;
|
|
int c = 0;
|
|
|
|
switch (sortCol) {
|
|
case COL_PID:
|
|
c = x->p.pid < y->p.pid ? -1 : x->p.pid > y->p.pid;
|
|
break;
|
|
case COL_NAME:
|
|
c = strcasecmp(x->p.comm, y->p.comm);
|
|
break;
|
|
case COL_USER:
|
|
c = strcasecmp(x->p.user, y->p.user);
|
|
break;
|
|
case COL_CPU:
|
|
c = x->cpu < y->cpu ? -1 : x->cpu > y->cpu;
|
|
break;
|
|
case COL_THREADS:
|
|
c = x->p.nlwp < y->p.nlwp ? -1 : x->p.nlwp > y->p.nlwp;
|
|
break;
|
|
case COL_REAL:
|
|
c = x->p.rss < y->p.rss ? -1 : x->p.rss > y->p.rss;
|
|
break;
|
|
case COL_VIRT:
|
|
c = x->p.vsz < y->p.vsz ? -1 : x->p.vsz > y->p.vsz;
|
|
break;
|
|
}
|
|
if (c == 0) /* a steady order: by pid */
|
|
c = x->p.pid < y->p.pid ? -1 : x->p.pid > y->p.pid;
|
|
return sortDesc ? -c : c;
|
|
}
|
|
|
|
/* a row of the table as text; the columns are fixed width (fixed font) */
|
|
static void
|
|
Format(const Row *r, char *out, size_t len)
|
|
{
|
|
char real[24], virt[24];
|
|
|
|
Human(r->p.rss, real, sizeof real);
|
|
Human(r->p.vsz, virt, sizeof virt);
|
|
snprintf(out, len, "%*ld %-*.*s %-*.*s %*.1f %*d %*s %*s",
|
|
widths[COL_PID] - 1, r->p.pid,
|
|
widths[COL_NAME] - 1, widths[COL_NAME] - 1, r->p.comm,
|
|
widths[COL_USER] - 1, widths[COL_USER] - 1, r->p.user,
|
|
widths[COL_CPU] - 1, r->cpu,
|
|
widths[COL_THREADS] - 1, r->p.nlwp,
|
|
widths[COL_REAL] - 1, real,
|
|
widths[COL_VIRT] - 1, virt);
|
|
}
|
|
|
|
static void
|
|
DrawHeader(void)
|
|
{
|
|
char text[256], cell[64];
|
|
int c, pos = 0;
|
|
|
|
text[0] = '\0';
|
|
for (c = 0; c < NCOLS; c++) {
|
|
int left = c == COL_NAME || c == COL_USER;
|
|
|
|
snprintf(cell, sizeof cell, "%s%s", titles[c],
|
|
c == sortCol ? (sortDesc ? " v" : " ^") : "");
|
|
/* the width without the gap, then the gap */
|
|
pos += snprintf(text + pos, sizeof text - (size_t) pos,
|
|
left ? "%-*.*s " : "%*.*s ", widths[c] - 1, widths[c] - 1,
|
|
cell);
|
|
}
|
|
SetLabel(header, text);
|
|
}
|
|
|
|
static long
|
|
SelectedPid(void)
|
|
{
|
|
int *pos, count;
|
|
long pid = -1;
|
|
|
|
if (XmListGetSelectedPos(list, &pos, &count)) {
|
|
if (pos[0] >= 1 && (size_t) pos[0] <= nshown)
|
|
pid = shown[pos[0] - 1]->p.pid;
|
|
XtFree((char *) pos);
|
|
}
|
|
return pid;
|
|
}
|
|
|
|
static void
|
|
RefreshTable(void)
|
|
{
|
|
long keep = SelectedPid();
|
|
int top = 0, at = 0;
|
|
size_t i, n = 0;
|
|
XmString *items;
|
|
char line[256];
|
|
|
|
XtVaGetValues(list, XmNtopItemPosition, &top, NULL);
|
|
free(shown);
|
|
shown = malloc((nall != 0 ? nall : 1) * sizeof *shown);
|
|
if (shown == NULL)
|
|
return;
|
|
for (i = 0; i < nall; i++)
|
|
if (Matches(&all[i]))
|
|
shown[n++] = &all[i];
|
|
nshown = n;
|
|
qsort(shown, n, sizeof *shown, Compare);
|
|
items = malloc((n != 0 ? n : 1) * sizeof *items);
|
|
if (items == NULL)
|
|
return;
|
|
for (i = 0; i < n; i++) {
|
|
Format(shown[i], line, sizeof line);
|
|
items[i] = XmStringCreateLocalized(line);
|
|
if (shown[i]->p.pid == keep)
|
|
at = (int) i + 1;
|
|
}
|
|
XtVaSetValues(list, XmNitems, items, XmNitemCount, (int) n, NULL);
|
|
for (i = 0; i < n; i++)
|
|
XmStringFree(items[i]);
|
|
free(items);
|
|
if (at > 0)
|
|
XmListSelectPos(list, at, False);
|
|
if (top > 0 && n > 0)
|
|
XmListSetPos(list, top);
|
|
DrawHeader();
|
|
}
|
|
|
|
/* ---- the graphs ------------------------------------------------------- */
|
|
|
|
static Pixel
|
|
Colour(Widget w, const char *name, Pixel fallback)
|
|
{
|
|
XColor c, exact;
|
|
Display *dpy = XtDisplay(w);
|
|
|
|
if (XAllocNamedColor(dpy, DefaultColormapOfScreen(XtScreen(w)), name,
|
|
&c, &exact))
|
|
return c.pixel;
|
|
return fallback;
|
|
}
|
|
|
|
/* columns of a stacked history: A at the bottom, then B, then C; SCALE is the
|
|
* value of the full height */
|
|
static void
|
|
Columns(Display *dpy, Window win, int w, int h, const double *a,
|
|
const double *b, const double *c, double scale)
|
|
{
|
|
int i, x, ha, hb, hc, base = h - 1;
|
|
int cw = w / HISTORY > 0 ? w / HISTORY : 1;
|
|
|
|
if (scale <= 0)
|
|
scale = 1;
|
|
for (i = 0; i < HISTORY; i++) {
|
|
x = w - (HISTORY - i) * cw;
|
|
if (x < 0)
|
|
continue;
|
|
ha = (int) (a[i] / scale * (double) (h - 2));
|
|
hb = b != NULL ? (int) (b[i] / scale * (double) (h - 2)) : 0;
|
|
hc = c != NULL ? (int) (c[i] / scale * (double) (h - 2)) : 0;
|
|
if (ha + hb + hc > h - 2) {
|
|
hc = 0;
|
|
if (ha + hb > h - 2)
|
|
hb = h - 2 - ha;
|
|
}
|
|
XSetForeground(dpy, gc, cUser);
|
|
XFillRectangle(dpy, win, gc, x, base - ha, (unsigned) cw, ha > 0 ?
|
|
(unsigned) ha : 0);
|
|
XSetForeground(dpy, gc, cSystem);
|
|
XFillRectangle(dpy, win, gc, x, base - ha - hb, (unsigned) cw,
|
|
(unsigned) hb);
|
|
XSetForeground(dpy, gc, cNice);
|
|
XFillRectangle(dpy, win, gc, x, base - ha - hb - hc,
|
|
(unsigned) cw, (unsigned) hc);
|
|
}
|
|
}
|
|
|
|
/* the highest column of two stacked histories: what the graph must hold */
|
|
static double
|
|
Peak(const double *a, const double *b)
|
|
{
|
|
double m = 0, v;
|
|
int i;
|
|
|
|
for (i = 0; i < HISTORY; i++) {
|
|
v = a[i] + (b != NULL ? b[i] : 0);
|
|
if (v > m)
|
|
m = v;
|
|
}
|
|
return m;
|
|
}
|
|
|
|
/* the numbers of the tab above its graph */
|
|
static void
|
|
DrawValues(void)
|
|
{
|
|
char text[512], a[32], b[32], c[32], d[32];
|
|
|
|
switch (tab) {
|
|
case TAB_CPU:
|
|
snprintf(text, sizeof text, "%% User: %5.2f %% System: "
|
|
"%5.2f %% Nice: %5.2f %% Idle: %5.2f\nThreads: %ld"
|
|
" Processes: %lu\nGraph: blue user, red system, green "
|
|
"nice", lastUser, lastSystem, lastNice, lastIdle, nthreads,
|
|
(unsigned long) nall);
|
|
break;
|
|
case TAB_MEMORY:
|
|
Human(mem.free, a, sizeof a);
|
|
Human(mem.cached, b, sizeof b);
|
|
Human(mem.used, c, sizeof c);
|
|
Human(mem.total, d, sizeof d);
|
|
snprintf(text, sizeof text, "Free: %s Cached: %s Used: "
|
|
"%s Total: %s", a, b, c, d);
|
|
Human(mem.swap_used, a, sizeof a);
|
|
Human(mem.swap_total, b, sizeof b);
|
|
snprintf(text + strlen(text), sizeof text - strlen(text),
|
|
"\nSwap used: %s of %s\nGraph: blue used, red cached "
|
|
"(the rest is free)", a, b);
|
|
break;
|
|
case TAB_DISK:
|
|
Human((unsigned long long) rateDiskR, a, sizeof a);
|
|
Human((unsigned long long) rateDiskW, b, sizeof b);
|
|
Human(totalDiskR, c, sizeof c);
|
|
Human(totalDiskW, d, sizeof d);
|
|
snprintf(text, sizeof text, "Reading: %s/s Writing: %s/s"
|
|
"\nRead in all: %s Written in all: %s\nGraph: blue "
|
|
"reading, red writing (scaled to the highest second)", a, b,
|
|
c, d);
|
|
break;
|
|
default:
|
|
Human((unsigned long long) rateNetR, a, sizeof a);
|
|
Human((unsigned long long) rateNetT, b, sizeof b);
|
|
Human(totalNetR, c, sizeof c);
|
|
Human(totalNetT, d, sizeof d);
|
|
snprintf(text, sizeof text, "Receiving: %s/s Sending: %s/s"
|
|
"\nReceived in all: %s Sent in all: %s\nGraph: blue "
|
|
"receiving, red sending (scaled to the highest second)", a,
|
|
b, c, d);
|
|
break;
|
|
}
|
|
SetLabel(values, text);
|
|
}
|
|
|
|
static void
|
|
DrawGraph(Widget w, XtPointer data, XEvent *ev, Boolean *cont)
|
|
{
|
|
Display *dpy = XtDisplay(graph);
|
|
Window win = XtWindow(graph);
|
|
Dimension dw, dh;
|
|
int y;
|
|
|
|
(void) w;
|
|
(void) data;
|
|
(void) cont;
|
|
if (ev != NULL && ev->type == Expose && ev->xexpose.count != 0)
|
|
return;
|
|
if (win == None)
|
|
return;
|
|
XtVaGetValues(graph, XmNwidth, &dw, XmNheight, &dh, NULL);
|
|
if (gc == NULL) {
|
|
gc = XCreateGC(dpy, win, 0, NULL);
|
|
cUser = Colour(graph, "#3465a4", BlackPixelOfScreen(
|
|
XtScreen(graph)));
|
|
cSystem = Colour(graph, "#cc0000", BlackPixelOfScreen(
|
|
XtScreen(graph)));
|
|
cNice = Colour(graph, "#4e9a06", BlackPixelOfScreen(
|
|
XtScreen(graph)));
|
|
cBack = Colour(graph, "#fdfdfd", WhitePixelOfScreen(
|
|
XtScreen(graph)));
|
|
cGrid = Colour(graph, "#c8c8c8", BlackPixelOfScreen(
|
|
XtScreen(graph)));
|
|
}
|
|
XSetForeground(dpy, gc, cBack);
|
|
XFillRectangle(dpy, win, gc, 0, 0, dw, dh);
|
|
XSetForeground(dpy, gc, cGrid);
|
|
for (y = 1; y < 4; y++)
|
|
XDrawLine(dpy, win, gc, 0, y * dh / 4, dw, y * dh / 4);
|
|
switch (tab) {
|
|
case TAB_CPU:
|
|
Columns(dpy, win, dw, dh, hUser, hSystem, hNice, 100.0);
|
|
break;
|
|
case TAB_MEMORY:
|
|
Columns(dpy, win, dw, dh, hUsed, hCached, NULL, 1.0);
|
|
break;
|
|
case TAB_DISK:
|
|
Columns(dpy, win, dw, dh, hDiskR, hDiskW, NULL,
|
|
Peak(hDiskR, hDiskW) > 102400 ? Peak(hDiskR, hDiskW) :
|
|
102400.0);
|
|
break;
|
|
default:
|
|
Columns(dpy, win, dw, dh, hNetR, hNetT, NULL,
|
|
Peak(hNetR, hNetT) > 10240 ? Peak(hNetR, hNetT) : 10240.0);
|
|
break;
|
|
}
|
|
DrawValues();
|
|
}
|
|
|
|
/* ---- the timer: only while the window is on the screen --------------- */
|
|
|
|
static void Tick(XtPointer client, XtIntervalId *id);
|
|
|
|
static void
|
|
UpdateTimer(void)
|
|
{
|
|
Boolean want = mapped && !obscured;
|
|
|
|
if (want && tick == 0) {
|
|
Sample();
|
|
RefreshTable();
|
|
DrawGraph(NULL, NULL, NULL, NULL);
|
|
tick = XtAppAddTimeOut(app, TICK_MS, Tick, NULL);
|
|
} else if (!want && tick != 0) {
|
|
XtRemoveTimeOut(tick);
|
|
tick = 0;
|
|
}
|
|
}
|
|
|
|
static void
|
|
Tick(XtPointer client, XtIntervalId *id)
|
|
{
|
|
(void) client;
|
|
(void) id;
|
|
tick = 0;
|
|
Sample();
|
|
RefreshTable();
|
|
DrawGraph(NULL, NULL, NULL, NULL);
|
|
tick = XtAppAddTimeOut(app, TICK_MS, Tick, NULL);
|
|
}
|
|
|
|
static void
|
|
Structure(Widget w, XtPointer data, XEvent *ev, Boolean *cont)
|
|
{
|
|
(void) w;
|
|
(void) data;
|
|
(void) cont;
|
|
switch (ev->type) {
|
|
case MapNotify:
|
|
mapped = True;
|
|
break;
|
|
case UnmapNotify:
|
|
mapped = False;
|
|
break;
|
|
case VisibilityNotify:
|
|
obscured = ev->xvisibility.state == VisibilityFullyObscured;
|
|
break;
|
|
default:
|
|
return;
|
|
}
|
|
UpdateTimer();
|
|
}
|
|
|
|
/* ---- the buttons ------------------------------------------------------ */
|
|
|
|
static Widget quitDialog;
|
|
static long quitPid;
|
|
|
|
static void
|
|
DestroyDialog(Widget w, XtPointer data, XtPointer call)
|
|
{
|
|
(void) data;
|
|
(void) call;
|
|
XtDestroyWidget(XtParent(w));
|
|
}
|
|
|
|
static void
|
|
Message(const char *title, const char *text)
|
|
{
|
|
Widget d = XmCreateInformationDialog(shell, "message", NULL, 0);
|
|
XmString t = XmStringCreateLtoR((char *) text, XmFONTLIST_DEFAULT_TAG);
|
|
XmString h = XmStringCreateLocalized((char *) title);
|
|
|
|
XtVaSetValues(d, XmNmessageString, t, XmNdialogTitle, h, NULL);
|
|
XmStringFree(t);
|
|
XmStringFree(h);
|
|
XtUnmanageChild(XmMessageBoxGetChild(d, XmDIALOG_CANCEL_BUTTON));
|
|
XtUnmanageChild(XmMessageBoxGetChild(d, XmDIALOG_HELP_BUTTON));
|
|
XtAddCallback(d, XmNokCallback, DestroyDialog, NULL);
|
|
XtManageChild(d);
|
|
}
|
|
|
|
static void
|
|
Signal(Widget w, XtPointer data, XtPointer call)
|
|
{
|
|
McdeSysSignal sig = (McdeSysSignal) (long) data;
|
|
|
|
(void) w;
|
|
(void) call;
|
|
if (mcde_sys_signal(quitPid, sig) != MCDE_SYS_OK)
|
|
Message("Quit Process", mcde_sys_error());
|
|
else if (tick != 0) {
|
|
/* show what happened without waiting for the next tick */
|
|
XtRemoveTimeOut(tick);
|
|
tick = XtAppAddTimeOut(app, 400, Tick, NULL);
|
|
}
|
|
}
|
|
|
|
static void
|
|
QuitProcess(Widget w, XtPointer data, XtPointer call)
|
|
{
|
|
long pid = SelectedPid();
|
|
char text[200];
|
|
size_t i;
|
|
const char *name = "";
|
|
XmString m;
|
|
|
|
(void) w;
|
|
(void) data;
|
|
(void) call;
|
|
if (pid < 0) {
|
|
Message("Quit Process", "Choose a process first.");
|
|
return;
|
|
}
|
|
for (i = 0; i < nshown; i++)
|
|
if (shown[i]->p.pid == pid)
|
|
name = shown[i]->p.comm;
|
|
quitPid = pid;
|
|
snprintf(text, sizeof text, "Do you want to quit the process\n"
|
|
"\"%s\" (%ld)?\n\nForce Quit ends it without asking it first: "
|
|
"what it has not saved is lost.", name, pid);
|
|
if (quitDialog == NULL) {
|
|
XmString ok = XmStringCreateLocalized((char *) "Quit");
|
|
XmString force = XmStringCreateLocalized((char *)
|
|
"Force Quit");
|
|
XmString title = XmStringCreateLocalized((char *)
|
|
"Quit Process");
|
|
|
|
quitDialog = XmCreateQuestionDialog(shell, "quit", NULL, 0);
|
|
XtVaSetValues(quitDialog, XmNokLabelString, ok,
|
|
XmNhelpLabelString, force, XmNdialogTitle, title,
|
|
XmNdefaultButtonType, XmDIALOG_CANCEL_BUTTON, NULL);
|
|
XmStringFree(ok);
|
|
XmStringFree(force);
|
|
XmStringFree(title);
|
|
XtAddCallback(quitDialog, XmNokCallback, Signal,
|
|
(XtPointer) (long) MCDE_SIGNAL_TERM);
|
|
XtAddCallback(quitDialog, XmNhelpCallback, Signal,
|
|
(XtPointer) (long) MCDE_SIGNAL_KILL);
|
|
}
|
|
m = XmStringCreateLtoR(text, XmFONTLIST_DEFAULT_TAG);
|
|
XtVaSetValues(quitDialog, XmNmessageString, m, NULL);
|
|
XmStringFree(m);
|
|
XtManageChild(quitDialog);
|
|
}
|
|
|
|
static void
|
|
Inspect(Widget w, XtPointer data, XtPointer call)
|
|
{
|
|
long pid = SelectedPid();
|
|
size_t i;
|
|
const Row *r = NULL;
|
|
char text[1400], cmd[900], real[24], virt[24];
|
|
|
|
(void) w;
|
|
(void) data;
|
|
(void) call;
|
|
for (i = 0; i < nshown && pid >= 0; i++)
|
|
if (shown[i]->p.pid == pid)
|
|
r = shown[i];
|
|
if (r == NULL) {
|
|
Message("Inspect", "Choose a process first.");
|
|
return;
|
|
}
|
|
if (mcde_sys_process_command((pid_t) pid, cmd, sizeof cmd) !=
|
|
MCDE_SYS_OK)
|
|
snprintf(cmd, sizeof cmd, "(not told)");
|
|
Human(r->p.rss, real, sizeof real);
|
|
Human(r->p.vsz, virt, sizeof virt);
|
|
snprintf(text, sizeof text, "%s\n\nProcess ID: %ld\nParent: %ld\n"
|
|
"User: %s\nState: %c\nThreads: %d\nReal memory: %s\nVirtual "
|
|
"memory: %s\nCPU time: %.1f s\n\nCommand: %s", r->p.comm,
|
|
r->p.pid, r->p.ppid, r->p.user, r->p.state, r->p.nlwp, real, virt,
|
|
(double) r->p.cpu_ms / 1000.0, cmd);
|
|
Message("Inspect", text);
|
|
}
|
|
|
|
static void
|
|
ShowChosen(Widget w, XtPointer data, XtPointer call)
|
|
{
|
|
(void) w;
|
|
(void) call;
|
|
show = (int) (long) data;
|
|
RefreshTable();
|
|
}
|
|
|
|
static void
|
|
FilterChanged(Widget w, XtPointer data, XtPointer call)
|
|
{
|
|
char *s = XmTextFieldGetString(filterField);
|
|
|
|
(void) w;
|
|
(void) data;
|
|
(void) call;
|
|
snprintf(filter, sizeof filter, "%s", s);
|
|
XtFree(s);
|
|
RefreshTable();
|
|
}
|
|
|
|
static void
|
|
TabChosen(Widget w, XtPointer data, XtPointer call)
|
|
{
|
|
XmToggleButtonCallbackStruct *c = (XmToggleButtonCallbackStruct *) call;
|
|
|
|
(void) w;
|
|
if (!c->set)
|
|
return;
|
|
tab = (int) (long) data;
|
|
DrawGraph(NULL, NULL, NULL, NULL);
|
|
}
|
|
|
|
/* a click on the title of a column sorts by it; again, the other way */
|
|
static void
|
|
HeaderClick(Widget w, XtPointer data, XEvent *ev, Boolean *cont)
|
|
{
|
|
int x, c, start = 0, margin;
|
|
Dimension mw = 0;
|
|
|
|
(void) data;
|
|
(void) cont;
|
|
if (ev->type != ButtonPress)
|
|
return;
|
|
XtVaGetValues(w, XmNmarginWidth, &mw, NULL);
|
|
margin = (int) mw;
|
|
x = ev->xbutton.x - margin;
|
|
for (c = 0; c < NCOLS; c++) {
|
|
if (x < start + widths[c] * charW) {
|
|
if (c == sortCol)
|
|
sortDesc = !sortDesc;
|
|
else {
|
|
sortCol = c;
|
|
sortDesc = (c == COL_CPU || c == COL_THREADS ||
|
|
c == COL_REAL || c == COL_VIRT);
|
|
}
|
|
RefreshTable();
|
|
return;
|
|
}
|
|
start += widths[c] * charW;
|
|
}
|
|
}
|
|
|
|
static Widget
|
|
Button(Widget parent, const char *name, XtCallbackProc cb)
|
|
{
|
|
Widget b = XtVaCreateManagedWidget(name, xmPushButtonWidgetClass,
|
|
parent, NULL);
|
|
|
|
XtAddCallback(b, XmNactivateCallback, cb, NULL);
|
|
return b;
|
|
}
|
|
|
|
static void
|
|
MakeShowMenu(Widget parent)
|
|
{
|
|
static const char *const names[] = { "All Processes", "My Processes",
|
|
"System Processes" };
|
|
Widget pulldown, option;
|
|
Arg a[2];
|
|
int i;
|
|
|
|
pulldown = XmCreatePulldownMenu(parent, "showMenu", NULL, 0);
|
|
for (i = 0; i < 3; i++) {
|
|
Widget item = XtVaCreateManagedWidget(names[i],
|
|
xmPushButtonWidgetClass, pulldown, NULL);
|
|
|
|
XtAddCallback(item, XmNactivateCallback, ShowChosen,
|
|
(XtPointer) (long) i);
|
|
}
|
|
XtSetArg(a[0], XmNsubMenuId, pulldown);
|
|
XtSetArg(a[1], XmNlabelString, XmStringCreateLocalized((char *)
|
|
"Show:"));
|
|
option = XmCreateOptionMenu(parent, "show", a, 2);
|
|
XtManageChild(option);
|
|
}
|
|
|
|
/* the width of a character of the list's font: the columns line up on it */
|
|
static void
|
|
MeasureFont(void)
|
|
{
|
|
XmFontList fl = NULL;
|
|
XmFontContext ctx;
|
|
XmFontListEntry e;
|
|
XmFontType type;
|
|
XtPointer f;
|
|
|
|
XtVaGetValues(header, XmNfontList, &fl, NULL);
|
|
if (fl != NULL && XmFontListInitFontContext(&ctx, fl)) {
|
|
if ((e = XmFontListNextEntry(ctx)) != NULL) {
|
|
f = XmFontListEntryGetFont(e, &type);
|
|
if (type == XmFONT_IS_FONT && f != NULL &&
|
|
((XFontStruct *) f)->max_bounds.width > 0)
|
|
charW = ((XFontStruct *) f)->max_bounds.width;
|
|
}
|
|
XmFontListFreeFontContext(ctx);
|
|
}
|
|
}
|
|
|
|
int
|
|
main(int argc, char **argv)
|
|
{
|
|
Widget form, bar, tabs, row;
|
|
static String fallback[] = {
|
|
"*renderTable: mcde",
|
|
"Dtactivity*list.fontList: fixed",
|
|
"Dtactivity*header.fontList: fixed",
|
|
"Dtactivity*values.fontList: fixed",
|
|
"Dtactivity*filter.columns: 16",
|
|
NULL
|
|
};
|
|
int i;
|
|
static const char *const tabNames[NTABS] = { "CPU", "System Memory",
|
|
"Disk Activity", "Network" };
|
|
|
|
XtSetLanguageProc(NULL, NULL, NULL);
|
|
shell = XtVaAppInitialize(&app, "Dtactivity", NULL, 0, &argc, argv,
|
|
(String *) fallback, XmNtitle, "Activity", NULL);
|
|
|
|
form = XtVaCreateManagedWidget("form", xmFormWidgetClass, shell,
|
|
XmNwidth, 700, XmNheight, 560, NULL);
|
|
bar = XtVaCreateManagedWidget("toolbar", xmRowColumnWidgetClass, form,
|
|
XmNorientation, XmHORIZONTAL, XmNspacing, 10, XmNmarginHeight, 6,
|
|
XmNtopAttachment, XmATTACH_FORM, XmNleftAttachment, XmATTACH_FORM,
|
|
XmNrightAttachment, XmATTACH_FORM, NULL);
|
|
{
|
|
Widget q = Button(bar, "Quit Process", QuitProcess);
|
|
XmString at = XmStringCreateLocalized((char *) "Cmd+Opt+Q");
|
|
|
|
XtVaSetValues(q, XmNaccelerator, "Mod4 Mod1<Key>q",
|
|
XmNacceleratorText, at, NULL);
|
|
XmStringFree(at);
|
|
}
|
|
(void) Button(bar, "Inspect", Inspect);
|
|
MakeShowMenu(bar);
|
|
(void) XtVaCreateManagedWidget("Filter:", xmLabelWidgetClass, bar,
|
|
NULL);
|
|
filterField = XtVaCreateManagedWidget("filter", xmTextFieldWidgetClass,
|
|
bar, NULL);
|
|
XtAddCallback(filterField, XmNvalueChangedCallback, FilterChanged,
|
|
NULL);
|
|
|
|
/* the tabs and the graph at the bottom */
|
|
tabs = XtVaCreateManagedWidget("tabs", xmRowColumnWidgetClass, form,
|
|
XmNorientation, XmHORIZONTAL, XmNradioBehavior, True,
|
|
XmNspacing, 6, XmNbottomAttachment, XmATTACH_FORM,
|
|
XmNleftAttachment, XmATTACH_FORM, XmNbottomOffset, 4, NULL);
|
|
row = XtVaCreateManagedWidget("graphBox", xmFormWidgetClass, form,
|
|
XmNbottomAttachment, XmATTACH_WIDGET, XmNbottomWidget, tabs,
|
|
XmNleftAttachment, XmATTACH_FORM, XmNrightAttachment,
|
|
XmATTACH_FORM, XmNheight, GRAPH_H + 54, NULL);
|
|
values = XtVaCreateManagedWidget("values", xmLabelWidgetClass, row,
|
|
XmNalignment, XmALIGNMENT_BEGINNING, XmNtopAttachment,
|
|
XmATTACH_FORM, XmNleftAttachment, XmATTACH_FORM,
|
|
XmNrightAttachment, XmATTACH_FORM, XmNmarginWidth, 6, NULL);
|
|
graph = XtVaCreateManagedWidget("graph", xmDrawingAreaWidgetClass, row,
|
|
XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, values,
|
|
XmNleftAttachment, XmATTACH_FORM, XmNrightAttachment,
|
|
XmATTACH_FORM, XmNbottomAttachment, XmATTACH_FORM,
|
|
XmNheight, GRAPH_H, XmNmarginWidth, 0, XmNmarginHeight, 0, NULL);
|
|
XtAddEventHandler(graph, ExposureMask, False, DrawGraph, NULL);
|
|
for (i = 0; i < NTABS; i++) {
|
|
tabButtons[i] = XtVaCreateManagedWidget(tabNames[i],
|
|
xmToggleButtonWidgetClass, tabs, XmNset, i == TAB_CPU,
|
|
NULL);
|
|
XtAddCallback(tabButtons[i], XmNvalueChangedCallback,
|
|
TabChosen, (XtPointer) (long) i);
|
|
}
|
|
|
|
/* the table: the header, then the list */
|
|
header = XtVaCreateManagedWidget("header", xmLabelWidgetClass, form,
|
|
XmNalignment, XmALIGNMENT_BEGINNING, XmNmarginWidth, 6,
|
|
XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, bar,
|
|
XmNleftAttachment, XmATTACH_FORM, XmNrightAttachment,
|
|
XmATTACH_FORM, NULL);
|
|
XtAddEventHandler(header, ButtonPressMask, False, HeaderClick, NULL);
|
|
list = XmCreateScrolledList(form, "list", NULL, 0);
|
|
XtVaSetValues(XtParent(list), XmNtopAttachment, XmATTACH_WIDGET,
|
|
XmNtopWidget, header, XmNleftAttachment, XmATTACH_FORM,
|
|
XmNrightAttachment, XmATTACH_FORM, XmNbottomAttachment,
|
|
XmATTACH_WIDGET, XmNbottomWidget, row, NULL);
|
|
XtVaSetValues(list, XmNselectionPolicy, XmBROWSE_SELECT, NULL);
|
|
XtManageChild(list);
|
|
|
|
XtAddEventHandler(shell, StructureNotifyMask | VisibilityChangeMask,
|
|
False, Structure, NULL);
|
|
XtRealizeWidget(shell);
|
|
MeasureFont();
|
|
DrawHeader();
|
|
XtAppMainLoop(app);
|
|
return 0;
|
|
}
|