|
|
@@ -0,0 +1,1434 @@
|
|
|
+/* process: graphical task manager, the ngui counterpart of `top`.
|
|
|
+ *
|
|
|
+ * One sample per second drives three history graphs (CPU busy, physical
|
|
|
+ * memory in use and NIC throughput) and a sortable live process table.
|
|
|
+ * CPU% comes from the per-process PIT counters (SYS_PROCINFO) sampled once
|
|
|
+ * per second; the overall CPU graph uses the summed idle ticks from
|
|
|
+ * SYS_SCHEDINFO, so it covers every CPU. Network rates are the deltas of the
|
|
|
+ * netdev frame byte counters exposed through SYS_NET_INFO.
|
|
|
+ *
|
|
|
+ * The window is a normal canvas window. ngui only redraws when input or a
|
|
|
+ * sample asks for it, so the app sleeps in 50 ms slices and wakes the
|
|
|
+ * renderer once per sample. Esc closes it; column headers sort the table.
|
|
|
+ */
|
|
|
+
|
|
|
+#include "ucmd_lib.h"
|
|
|
+#include "ngui.h"
|
|
|
+
|
|
|
+/* PROC_MAX in kernel/process.h (SYS_PROCINFO indexes live processes). */
|
|
|
+#define PMAX 64
|
|
|
+#define HIST 60
|
|
|
+#define SAMPLE_MS 1000
|
|
|
+#define LOOP_MS 50
|
|
|
+#define NET_KB_MAX 2000000u /* ~2 GB/s history ceiling */
|
|
|
+#define ROW_H 20
|
|
|
+#define PAD 10
|
|
|
+#define GAP 8
|
|
|
+#define HEAD_H 24
|
|
|
+#define TABLE_HEAD 22
|
|
|
+#define FOOTER_H 22
|
|
|
+
|
|
|
+enum {
|
|
|
+ COL_PID,
|
|
|
+ COL_STATE,
|
|
|
+ COL_CPU,
|
|
|
+ COL_MEM,
|
|
|
+ COL_TIME,
|
|
|
+ COL_NAME,
|
|
|
+ COL_COUNT
|
|
|
+};
|
|
|
+
|
|
|
+enum {
|
|
|
+ SORT_CPU,
|
|
|
+ SORT_MEM,
|
|
|
+ SORT_PID,
|
|
|
+ SORT_TIME,
|
|
|
+ SORT_NAME,
|
|
|
+ SORT_STATE
|
|
|
+};
|
|
|
+
|
|
|
+enum {
|
|
|
+ ID_KILL = 1
|
|
|
+};
|
|
|
+
|
|
|
+struct prow {
|
|
|
+ unsigned int pid;
|
|
|
+ unsigned int state;
|
|
|
+ unsigned int weight;
|
|
|
+ unsigned int cpu_pm;
|
|
|
+ unsigned int cpu_ticks;
|
|
|
+ unsigned int pages;
|
|
|
+ unsigned int kernel;
|
|
|
+ char name[12];
|
|
|
+};
|
|
|
+
|
|
|
+static struct ngui ui;
|
|
|
+static struct ngui_scroll table_scroll;
|
|
|
+static int page_req; /* PgUp/PgDn pages requested by the key ring */
|
|
|
+
|
|
|
+static struct sys_procinfo pinfo[PMAX];
|
|
|
+static struct sys_procinfo prev_info[PMAX];
|
|
|
+static unsigned int nprev;
|
|
|
+static struct prow rows[PMAX];
|
|
|
+static unsigned int nrows;
|
|
|
+static unsigned int ntotal;
|
|
|
+
|
|
|
+static unsigned int cpu_hist[HIST];
|
|
|
+static unsigned int mem_hist[HIST];
|
|
|
+static unsigned int rx_hist[HIST];
|
|
|
+static unsigned int tx_hist[HIST];
|
|
|
+
|
|
|
+static unsigned int last_sample_ms;
|
|
|
+static unsigned int last_idle_ticks;
|
|
|
+static unsigned int last_net_rx, last_net_tx;
|
|
|
+static int net_present;
|
|
|
+static int net_have_base;
|
|
|
+
|
|
|
+static unsigned int cpu_busy_pm;
|
|
|
+static unsigned int mem_used_pm;
|
|
|
+static unsigned int mem_used_kb;
|
|
|
+static unsigned int mem_total_kb;
|
|
|
+static unsigned int page_kb = 4;
|
|
|
+static unsigned int ncpus;
|
|
|
+static unsigned int runnable;
|
|
|
+static unsigned int net_rx_rate;
|
|
|
+static unsigned int net_tx_rate;
|
|
|
+
|
|
|
+static unsigned int selected_pid;
|
|
|
+static unsigned int own_pid;
|
|
|
+static int sort_mode = SORT_CPU;
|
|
|
+static int sort_desc = 1;
|
|
|
+
|
|
|
+static unsigned int kill_confirm_pid;
|
|
|
+static unsigned int kill_confirm_until;
|
|
|
+static char status_msg[128];
|
|
|
+static unsigned int status_until;
|
|
|
+static int status_error;
|
|
|
+
|
|
|
+/* ---- freestanding string/format helpers ---- */
|
|
|
+
|
|
|
+static unsigned int s_len(const char *s)
|
|
|
+{
|
|
|
+ unsigned int n = 0;
|
|
|
+
|
|
|
+ while (s && s[n])
|
|
|
+ n++;
|
|
|
+ return n;
|
|
|
+}
|
|
|
+
|
|
|
+static void s_copy(char *d, unsigned int cap, const char *s)
|
|
|
+{
|
|
|
+ unsigned int i = 0;
|
|
|
+
|
|
|
+ if (!cap)
|
|
|
+ return;
|
|
|
+ while (s && s[i] && i + 1 < cap) {
|
|
|
+ d[i] = s[i];
|
|
|
+ i++;
|
|
|
+ }
|
|
|
+ d[i] = '\0';
|
|
|
+}
|
|
|
+
|
|
|
+static void s_append(char *d, unsigned int *n, const char *s, unsigned int cap)
|
|
|
+{
|
|
|
+ while (s && *s && *n + 1 < cap)
|
|
|
+ d[(*n)++] = *s++;
|
|
|
+ d[*n] = '\0';
|
|
|
+}
|
|
|
+
|
|
|
+static void s_append_ch(char *d, unsigned int *n, char c, unsigned int cap)
|
|
|
+{
|
|
|
+ if (*n + 1 < cap) {
|
|
|
+ d[(*n)++] = c;
|
|
|
+ d[*n] = '\0';
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+static void s_append_uint(char *d, unsigned int *n, unsigned int v,
|
|
|
+ unsigned int cap)
|
|
|
+{
|
|
|
+ char tmp[12];
|
|
|
+ unsigned int i = 0, j;
|
|
|
+
|
|
|
+ do {
|
|
|
+ tmp[i++] = (char)('0' + v % 10);
|
|
|
+ v /= 10;
|
|
|
+ } while (v && i < sizeof(tmp));
|
|
|
+ for (j = 0; j < i && *n + 1 < cap; j++)
|
|
|
+ d[(*n)++] = tmp[i - 1 - j];
|
|
|
+ d[*n] = '\0';
|
|
|
+}
|
|
|
+
|
|
|
+static void s_append_uint2(char *d, unsigned int *n, unsigned int v,
|
|
|
+ unsigned int cap)
|
|
|
+{
|
|
|
+ if (v < 10)
|
|
|
+ s_append_ch(d, n, '0', cap);
|
|
|
+ s_append_uint(d, n, v, cap);
|
|
|
+}
|
|
|
+
|
|
|
+static void s_append_pm(char *d, unsigned int *n, unsigned int pm,
|
|
|
+ unsigned int cap)
|
|
|
+{
|
|
|
+ if (pm > 1000)
|
|
|
+ pm = 1000;
|
|
|
+ s_append_uint(d, n, pm / 10, cap);
|
|
|
+ s_append_ch(d, n, '.', cap);
|
|
|
+ s_append_ch(d, n, (char)('0' + pm % 10), cap);
|
|
|
+ s_append_ch(d, n, '%', cap);
|
|
|
+}
|
|
|
+
|
|
|
+static void fmt_pct(char *buf, unsigned int cap, unsigned int pm)
|
|
|
+{
|
|
|
+ unsigned int n = 0;
|
|
|
+
|
|
|
+ if (!cap)
|
|
|
+ return;
|
|
|
+ buf[0] = '\0';
|
|
|
+ s_append_pm(buf, &n, pm, cap);
|
|
|
+}
|
|
|
+
|
|
|
+/* 12345 KB -> "12.1 MB"; 456 KB -> "456 KB"; 2 GB -> "2.0 GB". */
|
|
|
+static void fmt_kb(char *buf, unsigned int cap, unsigned int kb)
|
|
|
+{
|
|
|
+ unsigned int n = 0, t;
|
|
|
+
|
|
|
+ if (!cap)
|
|
|
+ return;
|
|
|
+ buf[0] = '\0';
|
|
|
+ if (kb >= 1024u * 1024u) {
|
|
|
+ t = (kb * 10u + 524288u) / (1024u * 1024u);
|
|
|
+ s_append_uint(buf, &n, t / 10, cap);
|
|
|
+ s_append_ch(buf, &n, '.', cap);
|
|
|
+ s_append_ch(buf, &n, (char)('0' + t % 10), cap);
|
|
|
+ s_append(buf, &n, " GB", cap);
|
|
|
+ } else if (kb >= 1024) {
|
|
|
+ t = (kb * 10u + 512u) / 1024u;
|
|
|
+ s_append_uint(buf, &n, t / 10, cap);
|
|
|
+ s_append_ch(buf, &n, '.', cap);
|
|
|
+ s_append_ch(buf, &n, (char)('0' + t % 10), cap);
|
|
|
+ s_append(buf, &n, " MB", cap);
|
|
|
+ } else {
|
|
|
+ s_append_uint(buf, &n, kb, cap);
|
|
|
+ s_append(buf, &n, " KB", cap);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/* Bytes per second -> "812 B/s", "12.3 KB/s", "1.4 MB/s". */
|
|
|
+static void fmt_rate(char *buf, unsigned int cap, unsigned int bps)
|
|
|
+{
|
|
|
+ unsigned int n = 0, t;
|
|
|
+
|
|
|
+ if (!cap)
|
|
|
+ return;
|
|
|
+ buf[0] = '\0';
|
|
|
+ if (bps < 1024) {
|
|
|
+ s_append_uint(buf, &n, bps, cap);
|
|
|
+ s_append(buf, &n, " B/s", cap);
|
|
|
+ } else if (bps < 1024u * 1024u) {
|
|
|
+ t = (bps * 10u + 512u) / 1024u;
|
|
|
+ s_append_uint(buf, &n, t / 10, cap);
|
|
|
+ s_append_ch(buf, &n, '.', cap);
|
|
|
+ s_append_ch(buf, &n, (char)('0' + t % 10), cap);
|
|
|
+ s_append(buf, &n, " KB/s", cap);
|
|
|
+ } else {
|
|
|
+ unsigned int mb = bps / (1024u * 1024u);
|
|
|
+ unsigned int rem = bps % (1024u * 1024u);
|
|
|
+
|
|
|
+ t = mb * 10u + (rem * 10u + 524288u) / (1024u * 1024u);
|
|
|
+ s_append_uint(buf, &n, t / 10, cap);
|
|
|
+ s_append_ch(buf, &n, '.', cap);
|
|
|
+ s_append_ch(buf, &n, (char)('0' + t % 10), cap);
|
|
|
+ s_append(buf, &n, " MB/s", cap);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/* KB/s history value -> a compact graph scale label. */
|
|
|
+static void fmt_rate_kb(char *buf, unsigned int cap, unsigned int kb)
|
|
|
+{
|
|
|
+ fmt_rate(buf, cap, kb * 1024u);
|
|
|
+}
|
|
|
+
|
|
|
+static void fmt_span(char *buf, unsigned int cap, unsigned int ms)
|
|
|
+{
|
|
|
+ unsigned int n = 0;
|
|
|
+
|
|
|
+ if (!cap)
|
|
|
+ return;
|
|
|
+ buf[0] = '\0';
|
|
|
+ if (ms < 60000) {
|
|
|
+ unsigned int tenths = ms / 100;
|
|
|
+
|
|
|
+ s_append_uint(buf, &n, tenths / 10, cap);
|
|
|
+ s_append_ch(buf, &n, '.', cap);
|
|
|
+ s_append_ch(buf, &n, (char)('0' + tenths % 10), cap);
|
|
|
+ s_append_ch(buf, &n, 's', cap);
|
|
|
+ } else if (ms < 3600000) {
|
|
|
+ s_append_uint(buf, &n, ms / 60000, cap);
|
|
|
+ s_append_ch(buf, &n, 'm', cap);
|
|
|
+ s_append_uint2(buf, &n, (ms / 1000) % 60, cap);
|
|
|
+ s_append_ch(buf, &n, 's', cap);
|
|
|
+ } else {
|
|
|
+ s_append_uint(buf, &n, ms / 3600000, cap);
|
|
|
+ s_append_ch(buf, &n, 'h', cap);
|
|
|
+ s_append_uint2(buf, &n, (ms / 60000) % 60, cap);
|
|
|
+ s_append_ch(buf, &n, 'm', cap);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+static void fmt_uptime(char *buf, unsigned int cap, unsigned int s)
|
|
|
+{
|
|
|
+ unsigned int n = 0;
|
|
|
+
|
|
|
+ if (!cap)
|
|
|
+ return;
|
|
|
+ buf[0] = '\0';
|
|
|
+ if (s >= 3600) {
|
|
|
+ s_append_uint(buf, &n, s / 3600, cap);
|
|
|
+ s_append_ch(buf, &n, 'h', cap);
|
|
|
+ s_append_ch(buf, &n, ' ', cap);
|
|
|
+ s_append_uint2(buf, &n, (s / 60) % 60, cap);
|
|
|
+ s_append_ch(buf, &n, 'm', cap);
|
|
|
+ } else if (s >= 60) {
|
|
|
+ s_append_uint(buf, &n, s / 60, cap);
|
|
|
+ s_append_ch(buf, &n, 'm', cap);
|
|
|
+ s_append_ch(buf, &n, ' ', cap);
|
|
|
+ s_append_uint2(buf, &n, s % 60, cap);
|
|
|
+ s_append_ch(buf, &n, 's', cap);
|
|
|
+ } else {
|
|
|
+ s_append_uint(buf, &n, s, cap);
|
|
|
+ s_append_ch(buf, &n, 's', cap);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+static int s_ci_cmp(const char *a, const char *b)
|
|
|
+{
|
|
|
+ for (;;) {
|
|
|
+ unsigned char ca = (unsigned char)*a++;
|
|
|
+ unsigned char cb = (unsigned char)*b++;
|
|
|
+
|
|
|
+ if (ca >= 'A' && ca <= 'Z')
|
|
|
+ ca = (unsigned char)(ca + 32);
|
|
|
+ if (cb >= 'A' && cb <= 'Z')
|
|
|
+ cb = (unsigned char)(cb + 32);
|
|
|
+ if (ca != cb)
|
|
|
+ return (int)ca - (int)cb;
|
|
|
+ if (!ca)
|
|
|
+ return 0;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+static const char *state_name(unsigned int s)
|
|
|
+{
|
|
|
+ if (s == 2)
|
|
|
+ return "running";
|
|
|
+ if (s == 1)
|
|
|
+ return "ready";
|
|
|
+ if (s == 4)
|
|
|
+ return "sleep";
|
|
|
+ if (s == 5)
|
|
|
+ return "block";
|
|
|
+ return "?";
|
|
|
+}
|
|
|
+
|
|
|
+/* Active first, then runnable, blocked, sleeping. */
|
|
|
+static unsigned int state_rank(unsigned int s)
|
|
|
+{
|
|
|
+ if (s == 2)
|
|
|
+ return 0;
|
|
|
+ if (s == 1)
|
|
|
+ return 1;
|
|
|
+ if (s == 5)
|
|
|
+ return 2;
|
|
|
+ if (s == 4)
|
|
|
+ return 3;
|
|
|
+ return 4;
|
|
|
+}
|
|
|
+
|
|
|
+/* ---- colour helpers ---- */
|
|
|
+
|
|
|
+static unsigned short shade(unsigned short c, unsigned int num,
|
|
|
+ unsigned int den)
|
|
|
+{
|
|
|
+ unsigned int r = (c >> 11) & 0x1F;
|
|
|
+ unsigned int g = (c >> 5) & 0x3F;
|
|
|
+ unsigned int b = c & 0x1F;
|
|
|
+
|
|
|
+ r = r * num / den;
|
|
|
+ g = g * num / den;
|
|
|
+ b = b * num / den;
|
|
|
+ return (unsigned short)((r << 11) | (g << 5) | b);
|
|
|
+}
|
|
|
+
|
|
|
+static unsigned short mem_color(unsigned int pm)
|
|
|
+{
|
|
|
+ if (pm >= 900)
|
|
|
+ return ui.theme.error;
|
|
|
+ if (pm >= 700)
|
|
|
+ return ui.theme.warning;
|
|
|
+ return ui.theme.success;
|
|
|
+}
|
|
|
+
|
|
|
+/* ---- sampling ---- */
|
|
|
+
|
|
|
+static unsigned int snapshot_procs(struct sys_procinfo *out)
|
|
|
+{
|
|
|
+ struct sys_procinfo p;
|
|
|
+ unsigned int i, n = 0;
|
|
|
+
|
|
|
+ for (i = 0; i < PMAX; i++) {
|
|
|
+ if (u_syscall2(SYS_PROCINFO, (int)i, (int)&p) != 1)
|
|
|
+ break;
|
|
|
+ out[n++] = p;
|
|
|
+ }
|
|
|
+ return n;
|
|
|
+}
|
|
|
+
|
|
|
+/* Ascending comparator; `sort_desc` inverts the whole order below. */
|
|
|
+static int row_cmp(const struct prow *a, const struct prow *b)
|
|
|
+{
|
|
|
+ int c = 0;
|
|
|
+
|
|
|
+ switch (sort_mode) {
|
|
|
+ case SORT_MEM:
|
|
|
+ if (a->pages != b->pages)
|
|
|
+ c = a->pages < b->pages ? -1 : 1;
|
|
|
+ break;
|
|
|
+ case SORT_PID:
|
|
|
+ if (a->pid != b->pid)
|
|
|
+ c = a->pid < b->pid ? -1 : 1;
|
|
|
+ break;
|
|
|
+ case SORT_TIME:
|
|
|
+ if (a->cpu_ticks != b->cpu_ticks)
|
|
|
+ c = a->cpu_ticks < b->cpu_ticks ? -1 : 1;
|
|
|
+ break;
|
|
|
+ case SORT_NAME:
|
|
|
+ c = s_ci_cmp(a->name, b->name);
|
|
|
+ break;
|
|
|
+ case SORT_STATE:
|
|
|
+ if (state_rank(a->state) != state_rank(b->state))
|
|
|
+ c = state_rank(a->state) < state_rank(b->state) ?
|
|
|
+ -1 : 1;
|
|
|
+ break;
|
|
|
+ case SORT_CPU:
|
|
|
+ default:
|
|
|
+ if (a->cpu_pm != b->cpu_pm)
|
|
|
+ c = a->cpu_pm < b->cpu_pm ? -1 : 1;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ if (!c) {
|
|
|
+ if (a->pid != b->pid)
|
|
|
+ c = a->pid < b->pid ? -1 : 1;
|
|
|
+ else
|
|
|
+ c = s_ci_cmp(a->name, b->name);
|
|
|
+ }
|
|
|
+ return sort_desc ? -c : c;
|
|
|
+}
|
|
|
+
|
|
|
+static void sort_rows(void)
|
|
|
+{
|
|
|
+ unsigned int i, j;
|
|
|
+
|
|
|
+ for (i = 1; i < nrows; i++) {
|
|
|
+ struct prow key = rows[i];
|
|
|
+
|
|
|
+ j = i;
|
|
|
+ while (j > 0 && row_cmp(&rows[j - 1], &key) > 0) {
|
|
|
+ rows[j] = rows[j - 1];
|
|
|
+ j--;
|
|
|
+ }
|
|
|
+ rows[j] = key;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/* Turn a snapshot into table rows, computing CPU% from the previous sample. */
|
|
|
+static void build_rows(const struct sys_procinfo *cur, unsigned int n,
|
|
|
+ unsigned int wall, int with_delta)
|
|
|
+{
|
|
|
+ unsigned int i, j;
|
|
|
+
|
|
|
+ nrows = n;
|
|
|
+ ntotal = n;
|
|
|
+ for (i = 0; i < n; i++) {
|
|
|
+ struct prow *r = &rows[i];
|
|
|
+ unsigned int pm = 0;
|
|
|
+
|
|
|
+ if (with_delta && wall) {
|
|
|
+ for (j = 0; j < nprev; j++)
|
|
|
+ if (prev_info[j].pid == cur[i].pid) {
|
|
|
+ unsigned int delta =
|
|
|
+ cur[i].cpu_ticks -
|
|
|
+ prev_info[j].cpu_ticks;
|
|
|
+
|
|
|
+ pm = delta * 1000u / wall;
|
|
|
+ if (pm > 1000u)
|
|
|
+ pm = 1000u;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ r->pid = cur[i].pid;
|
|
|
+ r->state = cur[i].state;
|
|
|
+ r->weight = cur[i].weight;
|
|
|
+ r->cpu_pm = pm;
|
|
|
+ r->cpu_ticks = cur[i].cpu_ticks;
|
|
|
+ r->pages = cur[i].pages;
|
|
|
+ r->kernel = cur[i].kernel_thread;
|
|
|
+ s_copy(r->name, sizeof(r->name), cur[i].name);
|
|
|
+ }
|
|
|
+ sort_rows();
|
|
|
+}
|
|
|
+
|
|
|
+/* Bytes over `wall` ms -> bytes per second without overflowing the
|
|
|
+ * intermediate product at gigabit rates. */
|
|
|
+static unsigned int net_rate(unsigned int bytes, unsigned int wall)
|
|
|
+{
|
|
|
+ if (!wall)
|
|
|
+ wall = 1;
|
|
|
+ return (bytes / wall) * 1000u + (bytes % wall) * 1000u / wall;
|
|
|
+}
|
|
|
+
|
|
|
+static void sample_net(unsigned int wall)
|
|
|
+{
|
|
|
+ struct sys_netinfo ni;
|
|
|
+ unsigned int drx, dtx;
|
|
|
+
|
|
|
+ if (u_net_info(&ni) != 1 || !ni.present) {
|
|
|
+ net_present = 0;
|
|
|
+ net_have_base = 0;
|
|
|
+ net_rx_rate = 0;
|
|
|
+ net_tx_rate = 0;
|
|
|
+ last_net_rx = 0;
|
|
|
+ last_net_tx = 0;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ net_present = 1;
|
|
|
+ if (!net_have_base) {
|
|
|
+ /* A NIC that appeared after the app started must not count its
|
|
|
+ * whole boot-time traffic as one sample. */
|
|
|
+ net_have_base = 1;
|
|
|
+ last_net_rx = ni.rx_bytes;
|
|
|
+ last_net_tx = ni.tx_bytes;
|
|
|
+ net_rx_rate = 0;
|
|
|
+ net_tx_rate = 0;
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (!wall)
|
|
|
+ wall = 1;
|
|
|
+ drx = ni.rx_bytes - last_net_rx;
|
|
|
+ dtx = ni.tx_bytes - last_net_tx;
|
|
|
+ last_net_rx = ni.rx_bytes;
|
|
|
+ last_net_tx = ni.tx_bytes;
|
|
|
+ net_rx_rate = net_rate(drx, wall);
|
|
|
+ net_tx_rate = net_rate(dtx, wall);
|
|
|
+}
|
|
|
+
|
|
|
+static void hist_push(unsigned int *h, unsigned int v)
|
|
|
+{
|
|
|
+ unsigned int i;
|
|
|
+
|
|
|
+ for (i = 0; i + 1 < HIST; i++)
|
|
|
+ h[i] = h[i + 1];
|
|
|
+ h[HIST - 1] = v;
|
|
|
+}
|
|
|
+
|
|
|
+static void sample(void)
|
|
|
+{
|
|
|
+ struct sys_schedinfo s;
|
|
|
+ struct sys_meminfo m;
|
|
|
+ unsigned int now = u_ticks_ms();
|
|
|
+ unsigned int wall = now - last_sample_ms;
|
|
|
+ unsigned int ncur, i, rx_kb, tx_kb;
|
|
|
+
|
|
|
+ if (!wall)
|
|
|
+ wall = 1;
|
|
|
+
|
|
|
+ /* Process table: compare this snapshot with the previous second. */
|
|
|
+ ncur = snapshot_procs(pinfo);
|
|
|
+ build_rows(pinfo, ncur, wall, 1);
|
|
|
+ for (i = 0; i < ncur; i++)
|
|
|
+ prev_info[i] = pinfo[i];
|
|
|
+ nprev = ncur;
|
|
|
+
|
|
|
+ /* CPU: summed idle ticks across all CPUs over the sample window. */
|
|
|
+ if (u_schedinfo(&s) == 0) {
|
|
|
+ unsigned int diedle = s.idle_ticks - last_idle_ticks;
|
|
|
+ unsigned int cpus = s.ncpus ? s.ncpus : 1;
|
|
|
+ unsigned int idle_pm = diedle * 1000u / (wall * cpus);
|
|
|
+
|
|
|
+ if (idle_pm > 1000u)
|
|
|
+ idle_pm = 1000u;
|
|
|
+ cpu_busy_pm = 1000u - idle_pm;
|
|
|
+ ncpus = s.ncpus;
|
|
|
+ runnable = s.runnable;
|
|
|
+ last_idle_ticks = s.idle_ticks;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* Physical memory: total minus free pages. */
|
|
|
+ if (u_syscall1(SYS_MEMINFO, (int)&m) == 0) {
|
|
|
+ unsigned int free_kb = m.free_pages * (m.page_size / 1024);
|
|
|
+
|
|
|
+ if (m.page_size >= 1024)
|
|
|
+ page_kb = m.page_size / 1024;
|
|
|
+ mem_total_kb = m.total_kb;
|
|
|
+ mem_used_kb = mem_total_kb > free_kb ?
|
|
|
+ mem_total_kb - free_kb : 0;
|
|
|
+ mem_used_pm = mem_total_kb ?
|
|
|
+ mem_used_kb * 1000u / mem_total_kb : 0;
|
|
|
+ if (mem_used_pm > 1000u)
|
|
|
+ mem_used_pm = 1000u;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* Network deltas are in bytes per second; the graph keeps KB/s. */
|
|
|
+ sample_net(wall);
|
|
|
+ rx_kb = (net_rx_rate + 1023u) / 1024u;
|
|
|
+ tx_kb = (net_tx_rate + 1023u) / 1024u;
|
|
|
+ if (rx_kb > NET_KB_MAX)
|
|
|
+ rx_kb = NET_KB_MAX;
|
|
|
+ if (tx_kb > NET_KB_MAX)
|
|
|
+ tx_kb = NET_KB_MAX;
|
|
|
+
|
|
|
+ hist_push(cpu_hist, cpu_busy_pm);
|
|
|
+ hist_push(mem_hist, mem_used_pm);
|
|
|
+ hist_push(rx_hist, rx_kb);
|
|
|
+ hist_push(tx_hist, tx_kb);
|
|
|
+
|
|
|
+ if (selected_pid) {
|
|
|
+ unsigned int i;
|
|
|
+ int found = 0;
|
|
|
+
|
|
|
+ for (i = 0; i < nrows; i++)
|
|
|
+ if (rows[i].pid == selected_pid) {
|
|
|
+ found = 1;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ if (!found) {
|
|
|
+ selected_pid = 0;
|
|
|
+ kill_confirm_pid = 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ last_sample_ms = now;
|
|
|
+}
|
|
|
+
|
|
|
+/* ---- drawing helpers ---- */
|
|
|
+
|
|
|
+static void text_right(struct ngui *u, int right, int y, const char *s,
|
|
|
+ unsigned short color)
|
|
|
+{
|
|
|
+ int w = (int)ngui_text_width_ui(u, s);
|
|
|
+
|
|
|
+ ngui_text(u, right - w, y, s, color);
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+static void fill_rect_(struct ngui *u, int x, int y, int w, int h,
|
|
|
+ unsigned short c)
|
|
|
+{
|
|
|
+ struct ngui_rect r;
|
|
|
+
|
|
|
+ r.x = x;
|
|
|
+ r.y = y;
|
|
|
+ r.w = w;
|
|
|
+ r.h = h;
|
|
|
+ ngui_fill_rect(u, r, c);
|
|
|
+}
|
|
|
+
|
|
|
+static void draw_card(struct ngui *u, struct ngui_rect r, const char *caption,
|
|
|
+ const char *right)
|
|
|
+{
|
|
|
+ ngui_fill_round_rect(u, r, 8, 8, 8, 8, u->theme.panel);
|
|
|
+ ngui_text(u, r.x + 10, r.y + 7, caption, u->theme.text_dim);
|
|
|
+ if (right && r.w > 160)
|
|
|
+ text_right(u, r.x + r.w - 10, r.y + 7, right,
|
|
|
+ u->theme.text_dim);
|
|
|
+}
|
|
|
+
|
|
|
+static int value_y(const struct ngui_rect *r, unsigned int v,
|
|
|
+ unsigned int max)
|
|
|
+{
|
|
|
+ unsigned int h;
|
|
|
+
|
|
|
+ if (!max)
|
|
|
+ max = 1;
|
|
|
+ if (v > max)
|
|
|
+ v = max;
|
|
|
+ h = v * (unsigned int)r->h / max;
|
|
|
+ return r->y + r->h - (int)h;
|
|
|
+}
|
|
|
+
|
|
|
+static void graph_grid(struct ngui *u, struct ngui_rect r)
|
|
|
+{
|
|
|
+ int i;
|
|
|
+
|
|
|
+ fill_rect_(u, r.x, r.y, r.w, r.h, NGUI_RGB(28, 32, 44));
|
|
|
+ for (i = 1; i < 4; i++)
|
|
|
+ fill_rect_(u, r.x, r.y + r.h * i / 4, r.w, 1,
|
|
|
+ NGUI_RGB(46, 51, 66));
|
|
|
+ ngui_frame(u, r, NGUI_RGB(56, 62, 80));
|
|
|
+}
|
|
|
+
|
|
|
+/* Filled history bars with a 2 px bright cap. */
|
|
|
+static void graph_bars(struct ngui *u, struct ngui_rect r,
|
|
|
+ const unsigned int *hist, unsigned int max,
|
|
|
+ unsigned short color)
|
|
|
+{
|
|
|
+ int i;
|
|
|
+ unsigned short dim = shade(color, 45, 100);
|
|
|
+
|
|
|
+ if (max == 0)
|
|
|
+ max = 1;
|
|
|
+ for (i = 0; i < HIST; i++) {
|
|
|
+ int x0 = r.x + i * r.w / HIST;
|
|
|
+ int x1 = r.x + (i + 1) * r.w / HIST;
|
|
|
+ int bw = x1 - x0;
|
|
|
+ int y = value_y(&r, hist[i], max);
|
|
|
+ int bh = r.y + r.h - y;
|
|
|
+
|
|
|
+ if (bw < 1)
|
|
|
+ bw = 1;
|
|
|
+ if (bh <= 0)
|
|
|
+ continue;
|
|
|
+ fill_rect_(u, x0, y, bw - 1 > 0 ? bw - 1 : 1, bh, dim);
|
|
|
+ fill_rect_(u, x0, y, bw - 1 > 0 ? bw - 1 : 1,
|
|
|
+ bh < 2 ? bh : 2, color);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/* A one-pixel history line (used for the TX overlay). */
|
|
|
+static void graph_line(struct ngui *u, struct ngui_rect r,
|
|
|
+ const unsigned int *hist, unsigned int max,
|
|
|
+ unsigned short color)
|
|
|
+{
|
|
|
+ int i, px = 0, py = 0;
|
|
|
+
|
|
|
+ if (max == 0)
|
|
|
+ max = 1;
|
|
|
+ for (i = 0; i < HIST; i++) {
|
|
|
+ int x = r.x + i * (r.w - 1) / (HIST - 1);
|
|
|
+ int y = value_y(&r, hist[i], max);
|
|
|
+
|
|
|
+ if (i)
|
|
|
+ ngui_line(u, px, py, x, y, color);
|
|
|
+ if (i)
|
|
|
+ ngui_line(u, px, py + 1, x, y + 1, color);
|
|
|
+ px = x;
|
|
|
+ py = y;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+static unsigned int history_peak(const unsigned int *a,
|
|
|
+ const unsigned int *b)
|
|
|
+{
|
|
|
+ unsigned int i, max = 0;
|
|
|
+
|
|
|
+ for (i = 0; i < HIST; i++) {
|
|
|
+ if (a[i] > max)
|
|
|
+ max = a[i];
|
|
|
+ if (b && b[i] > max)
|
|
|
+ max = b[i];
|
|
|
+ }
|
|
|
+ return max;
|
|
|
+}
|
|
|
+
|
|
|
+static unsigned int history_max(const unsigned int *a, const unsigned int *b)
|
|
|
+{
|
|
|
+ unsigned int max = history_peak(a, b);
|
|
|
+
|
|
|
+ return max + max / 4 + 1;
|
|
|
+}
|
|
|
+
|
|
|
+static void draw_cpu_card(struct ngui *u, struct ngui_rect r)
|
|
|
+{
|
|
|
+ struct ngui_rect g;
|
|
|
+ char b[16], right[24];
|
|
|
+ unsigned int n = 0;
|
|
|
+
|
|
|
+ right[0] = '\0';
|
|
|
+ if (ncpus) {
|
|
|
+ s_append_uint(right, &n, ncpus, sizeof(right));
|
|
|
+ s_append(right, &n, ncpus == 1 ? " CPU" : " CPUs", sizeof(right));
|
|
|
+ }
|
|
|
+ draw_card(u, r, "CPU", right);
|
|
|
+ fmt_pct(b, sizeof(b), cpu_busy_pm);
|
|
|
+ ngui_text_px(u, r.x + 10, r.y + 24, b, s_len(b), u->theme.accent,
|
|
|
+ 24, 1, 0, 0);
|
|
|
+ text_right(u, r.x + r.w - 10, r.y + 33, "busy", u->theme.text_dim);
|
|
|
+ g.x = r.x + 10;
|
|
|
+ g.y = r.y + 52;
|
|
|
+ g.w = r.w - 20;
|
|
|
+ g.h = r.h - g.y + r.y - 8;
|
|
|
+ if (g.w < 10 || g.h < 10)
|
|
|
+ return;
|
|
|
+ graph_grid(u, g);
|
|
|
+ graph_bars(u, g, cpu_hist, 1000, u->theme.accent);
|
|
|
+}
|
|
|
+
|
|
|
+static void draw_mem_card(struct ngui *u, struct ngui_rect r)
|
|
|
+{
|
|
|
+ struct ngui_rect g;
|
|
|
+ char b[16], used[16], total[16], detail[40];
|
|
|
+ unsigned int n = 0;
|
|
|
+ unsigned short color = mem_color(mem_used_pm);
|
|
|
+
|
|
|
+ fmt_pct(b, sizeof(b), mem_used_pm);
|
|
|
+ fmt_kb(used, sizeof(used), mem_used_kb);
|
|
|
+ fmt_kb(total, sizeof(total), mem_total_kb);
|
|
|
+ detail[0] = '\0';
|
|
|
+ s_append(detail, &n, used, sizeof(detail));
|
|
|
+ s_append(detail, &n, " / ", sizeof(detail));
|
|
|
+ s_append(detail, &n, total, sizeof(detail));
|
|
|
+ draw_card(u, r, "Memory", detail);
|
|
|
+ ngui_text_px(u, r.x + 10, r.y + 24, b, s_len(b), color,
|
|
|
+ 24, 1, 0, 0);
|
|
|
+ text_right(u, r.x + r.w - 10, r.y + 33, "in use",
|
|
|
+ u->theme.text_dim);
|
|
|
+ g.x = r.x + 10;
|
|
|
+ g.y = r.y + 52;
|
|
|
+ g.w = r.w - 20;
|
|
|
+ g.h = r.h - g.y + r.y - 8;
|
|
|
+ if (g.w < 10 || g.h < 10)
|
|
|
+ return;
|
|
|
+ graph_grid(u, g);
|
|
|
+ graph_bars(u, g, mem_hist, 1000, color);
|
|
|
+}
|
|
|
+
|
|
|
+static void draw_net_card(struct ngui *u, struct ngui_rect r)
|
|
|
+{
|
|
|
+ struct ngui_rect g;
|
|
|
+ char rx[18], tx[18], scale[18];
|
|
|
+ unsigned int peak = history_peak(rx_hist, tx_hist);
|
|
|
+ unsigned int max = history_max(rx_hist, tx_hist);
|
|
|
+
|
|
|
+ fmt_rate(rx, sizeof(rx), net_rx_rate);
|
|
|
+ fmt_rate(tx, sizeof(tx), net_tx_rate);
|
|
|
+ if (!net_present)
|
|
|
+ s_copy(scale, sizeof(scale), "no NIC");
|
|
|
+ else if (!peak)
|
|
|
+ s_copy(scale, sizeof(scale), "idle");
|
|
|
+ else
|
|
|
+ fmt_rate_kb(scale, sizeof(scale), max);
|
|
|
+ draw_card(u, r, "Network", scale);
|
|
|
+
|
|
|
+ if (net_present) {
|
|
|
+ ngui_text(u, r.x + 10, r.y + 27, "RX", u->theme.success);
|
|
|
+ ngui_text(u, r.x + 34, r.y + 27, rx, u->theme.text);
|
|
|
+ {
|
|
|
+ int txw = (int)ngui_text_width_ui(u, rx) + 52;
|
|
|
+
|
|
|
+ ngui_text(u, r.x + txw, r.y + 27, "TX",
|
|
|
+ u->theme.warning);
|
|
|
+ ngui_text(u, r.x + txw + 24, r.y + 27, tx,
|
|
|
+ u->theme.text);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ ngui_text(u, r.x + 10, r.y + 27, "no network device",
|
|
|
+ u->theme.text_dim);
|
|
|
+ }
|
|
|
+ g.x = r.x + 10;
|
|
|
+ g.y = r.y + 50;
|
|
|
+ g.w = r.w - 20;
|
|
|
+ g.h = r.h - g.y + r.y - 8;
|
|
|
+ if (g.w < 10 || g.h < 10)
|
|
|
+ return;
|
|
|
+ graph_grid(u, g);
|
|
|
+ if (net_present) {
|
|
|
+ graph_bars(u, g, rx_hist, max, u->theme.success);
|
|
|
+ graph_line(u, g, tx_hist, max, u->theme.warning);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+/* Column positions shared by the header, rows and click handling. */
|
|
|
+static void table_columns(const struct ngui_rect *r, int xs[COL_COUNT],
|
|
|
+ int ws[COL_COUNT])
|
|
|
+{
|
|
|
+ static const int fixed[COL_COUNT] = { 54, 62, 58, 66, 74, 0 };
|
|
|
+ int x = r->x + 6;
|
|
|
+ int i;
|
|
|
+
|
|
|
+ for (i = 0; i < COL_COUNT; i++)
|
|
|
+ ws[i] = fixed[i];
|
|
|
+ ws[COL_NAME] = r->w - 6 - (54 + 62 + 58 + 66 + 74) - 20;
|
|
|
+ if (ws[COL_NAME] < 30)
|
|
|
+ ws[COL_NAME] = 30;
|
|
|
+ for (i = 0; i < COL_COUNT; i++) {
|
|
|
+ xs[i] = x;
|
|
|
+ x += ws[i];
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+static int sort_col(int col)
|
|
|
+{
|
|
|
+ if (col == COL_PID)
|
|
|
+ return SORT_PID;
|
|
|
+ if (col == COL_CPU)
|
|
|
+ return SORT_CPU;
|
|
|
+ if (col == COL_MEM)
|
|
|
+ return SORT_MEM;
|
|
|
+ if (col == COL_TIME)
|
|
|
+ return SORT_TIME;
|
|
|
+ if (col == COL_NAME)
|
|
|
+ return SORT_NAME;
|
|
|
+ if (col == COL_STATE)
|
|
|
+ return SORT_STATE;
|
|
|
+ return -1;
|
|
|
+}
|
|
|
+
|
|
|
+static int sort_default_desc(int mode)
|
|
|
+{
|
|
|
+ return mode == SORT_CPU || mode == SORT_MEM || mode == SORT_TIME;
|
|
|
+}
|
|
|
+
|
|
|
+static void set_sort(int col)
|
|
|
+{
|
|
|
+ int mode = sort_col(col);
|
|
|
+
|
|
|
+ if (mode < 0)
|
|
|
+ return;
|
|
|
+ if (mode == sort_mode)
|
|
|
+ sort_desc = !sort_desc;
|
|
|
+ else {
|
|
|
+ sort_mode = mode;
|
|
|
+ sort_desc = sort_default_desc(mode);
|
|
|
+ }
|
|
|
+ sort_rows();
|
|
|
+ ngui_invalidate(&ui);
|
|
|
+}
|
|
|
+
|
|
|
+static void draw_table_header(struct ngui *u, struct ngui_rect r)
|
|
|
+{
|
|
|
+ static const char *labels[COL_COUNT] = {
|
|
|
+ "PID", "STATE", "CPU%", "MEM", "TIME", "NAME"
|
|
|
+ };
|
|
|
+ int xs[COL_COUNT], ws[COL_COUNT], i;
|
|
|
+
|
|
|
+ fill_rect_(u, r.x, r.y, r.w, r.h, u->theme.panel_alt);
|
|
|
+ table_columns(&r, xs, ws);
|
|
|
+ for (i = 0; i < COL_COUNT; i++) {
|
|
|
+ struct ngui_rect hr;
|
|
|
+ char label[16];
|
|
|
+ unsigned int n = 0;
|
|
|
+ int mode = sort_col(i);
|
|
|
+ int active = mode >= 0 && sort_mode == mode;
|
|
|
+ int right = (i == COL_PID || i == COL_CPU || i == COL_MEM ||
|
|
|
+ i == COL_TIME);
|
|
|
+
|
|
|
+ label[0] = '\0';
|
|
|
+ if (active && mode >= 0) {
|
|
|
+ s_copy(label, sizeof(label), labels[i]);
|
|
|
+ n = s_len(label);
|
|
|
+ s_append_ch(label, &n, ' ', sizeof(label));
|
|
|
+ s_append_ch(label, &n, sort_desc ? 'v' : '^',
|
|
|
+ sizeof(label));
|
|
|
+ } else {
|
|
|
+ s_copy(label, sizeof(label), labels[i]);
|
|
|
+ }
|
|
|
+ hr.x = xs[i];
|
|
|
+ hr.y = r.y;
|
|
|
+ hr.w = ws[i];
|
|
|
+ hr.h = r.h;
|
|
|
+ if (mode >= 0)
|
|
|
+ ngui_cursor_rect(u, hr, CURSOR_POINTER);
|
|
|
+ if (right)
|
|
|
+ text_right(u, xs[i] + ws[i] - 8, r.y + 3, label,
|
|
|
+ active ? u->theme.accent : u->theme.text_dim);
|
|
|
+ else
|
|
|
+ ngui_text(u, xs[i], r.y + 3, label,
|
|
|
+ active ? u->theme.accent : u->theme.text_dim);
|
|
|
+ if (active)
|
|
|
+ fill_rect_(u, xs[i], r.y + r.h - 2, ws[i] - 8, 2,
|
|
|
+ u->theme.accent);
|
|
|
+ if (mode >= 0 && u->pressed &&
|
|
|
+ ngui_rect_contains(&hr, u->press_x, u->press_y))
|
|
|
+ set_sort(i);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+static struct prow *selected_row(void)
|
|
|
+{
|
|
|
+ unsigned int i;
|
|
|
+
|
|
|
+ if (!selected_pid)
|
|
|
+ return 0;
|
|
|
+ for (i = 0; i < nrows; i++)
|
|
|
+ if (rows[i].pid == selected_pid)
|
|
|
+ return &rows[i];
|
|
|
+ return 0;
|
|
|
+}
|
|
|
+
|
|
|
+static void draw_process_row(struct ngui *u, const struct ngui_rect *list,
|
|
|
+ int row_index, int y, int selected, int hover)
|
|
|
+{
|
|
|
+ int xs[COL_COUNT], ws[COL_COUNT];
|
|
|
+ const struct prow *p = &rows[row_index];
|
|
|
+ unsigned short fg = selected ? NGUI_RGB(255, 255, 255) :
|
|
|
+ u->theme.text;
|
|
|
+ char b[32];
|
|
|
+ unsigned int n = 0;
|
|
|
+
|
|
|
+ table_columns(list, xs, ws);
|
|
|
+ if (selected)
|
|
|
+ fill_rect_(u, list->x, y, list->w, ROW_H, u->theme.selection);
|
|
|
+ else if (hover)
|
|
|
+ fill_rect_(u, list->x, y, list->w, ROW_H,
|
|
|
+ u->theme.panel_alt);
|
|
|
+
|
|
|
+ s_append_uint(b, &n, p->pid, sizeof(b));
|
|
|
+ ngui_text(u, xs[COL_PID] + ws[COL_PID] - 8 -
|
|
|
+ (int)ngui_text_width_ui(u, b), y + 2, b, fg);
|
|
|
+
|
|
|
+ ngui_text(u, xs[COL_STATE], y + 2, state_name(p->state),
|
|
|
+ selected ? fg : (p->state == 2 ? u->theme.success :
|
|
|
+ p->state == 5 ? u->theme.warning :
|
|
|
+ u->theme.text_dim));
|
|
|
+
|
|
|
+ b[0] = '\0';
|
|
|
+ n = 0;
|
|
|
+ s_append_pm(b, &n, p->cpu_pm, sizeof(b));
|
|
|
+ text_right(u, xs[COL_CPU] + ws[COL_CPU] - 8, y + 2, b, fg);
|
|
|
+
|
|
|
+ b[0] = '\0';
|
|
|
+ n = 0;
|
|
|
+ s_append_uint(b, &n, p->pages * page_kb, sizeof(b));
|
|
|
+ s_append_ch(b, &n, 'K', sizeof(b));
|
|
|
+ text_right(u, xs[COL_MEM] + ws[COL_MEM] - 8, y + 2, b, fg);
|
|
|
+
|
|
|
+ fmt_span(b, sizeof(b), p->cpu_ticks);
|
|
|
+ text_right(u, xs[COL_TIME] + ws[COL_TIME] - 8, y + 2, b, fg);
|
|
|
+
|
|
|
+ ngui_text(u, xs[COL_NAME], y + 2, p->name,
|
|
|
+ p->kernel && !selected ? u->theme.text_dim : fg);
|
|
|
+}
|
|
|
+
|
|
|
+static void draw_table(struct ngui *u, struct ngui_rect list)
|
|
|
+{
|
|
|
+ unsigned int i;
|
|
|
+ int first, last, y;
|
|
|
+ int pressed_row = -1;
|
|
|
+
|
|
|
+ table_scroll.content_h = (int)nrows * ROW_H;
|
|
|
+ ngui_scroll_begin(u, list, &table_scroll);
|
|
|
+ ngui_scroll_wheel(u, list, &table_scroll);
|
|
|
+ if (page_req) {
|
|
|
+ int page = table_scroll.view_h - 22;
|
|
|
+
|
|
|
+ if (page < 22)
|
|
|
+ page = 22;
|
|
|
+ ngui_scroll_to(&table_scroll,
|
|
|
+ table_scroll.offset + page_req * page);
|
|
|
+ page_req = 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ /* After ngui_scroll_begin(): a press on the overlay bar has been
|
|
|
+ * consumed, so it cannot also select the row underneath. */
|
|
|
+ if (u->pressed && ngui_rect_contains(&list, u->press_x, u->press_y)) {
|
|
|
+ int idx = (u->press_y - list.y + table_scroll.offset) / ROW_H;
|
|
|
+
|
|
|
+ if (idx >= 0 && (unsigned int)idx < nrows)
|
|
|
+ pressed_row = idx;
|
|
|
+ }
|
|
|
+ if (pressed_row >= 0) {
|
|
|
+ if (selected_pid != rows[pressed_row].pid) {
|
|
|
+ selected_pid = rows[pressed_row].pid;
|
|
|
+ kill_confirm_pid = 0;
|
|
|
+ ngui_invalidate(u);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ fill_rect_(u, list.x, list.y, list.w, list.h, u->theme.panel);
|
|
|
+ first = table_scroll.offset / ROW_H;
|
|
|
+ if (first < 0)
|
|
|
+ first = 0;
|
|
|
+ last = (table_scroll.offset + list.h + ROW_H - 1) / ROW_H;
|
|
|
+ if (last > (int)nrows)
|
|
|
+ last = (int)nrows;
|
|
|
+ for (i = (unsigned int)first; i < (unsigned int)last; i++) {
|
|
|
+ int hover = u->inside &&
|
|
|
+ ngui_rect_contains(&list, u->mouse_x, u->mouse_y) &&
|
|
|
+ (u->mouse_y - list.y + table_scroll.offset) / ROW_H ==
|
|
|
+ (int)i;
|
|
|
+ int selected = rows[i].pid == selected_pid;
|
|
|
+
|
|
|
+ y = list.y + (int)i * ROW_H - table_scroll.offset;
|
|
|
+ draw_process_row(u, &list, (int)i, y, selected, hover);
|
|
|
+ }
|
|
|
+ ngui_scroll_end(u, list, &table_scroll);
|
|
|
+}
|
|
|
+
|
|
|
+static void draw_kill_button(struct ngui *u, struct ngui_rect r)
|
|
|
+{
|
|
|
+ struct prow *sel = selected_row();
|
|
|
+ struct ngui_rect br;
|
|
|
+ char label[32];
|
|
|
+ unsigned int now = u_ticks_ms();
|
|
|
+
|
|
|
+ br.x = r.x + r.w - 150;
|
|
|
+ br.y = r.y;
|
|
|
+ br.w = 150;
|
|
|
+ br.h = 24;
|
|
|
+
|
|
|
+ if (!sel) {
|
|
|
+ text_right(u, r.x + r.w, r.y + 4, "Select a process",
|
|
|
+ u->theme.text_dim);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (sel->kernel || sel->pid == own_pid) {
|
|
|
+ ngui_fill_round_rect(u, br, 6, 6, 6, 6, u->theme.panel_alt);
|
|
|
+ text_right(u, br.x + br.w - 10, br.y + 4,
|
|
|
+ sel->kernel ? "kernel thread" : "this window",
|
|
|
+ u->theme.text_dim);
|
|
|
+ ngui_cursor_rect(u, br, CURSOR_NOT_ALLOWED);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (kill_confirm_pid == sel->pid &&
|
|
|
+ (int)(now - kill_confirm_until) < 0) {
|
|
|
+ s_copy(label, sizeof(label), "Confirm end task");
|
|
|
+ } else {
|
|
|
+ s_copy(label, sizeof(label), "End task");
|
|
|
+ }
|
|
|
+ if (ngui_button(u, ID_KILL, br, label) == 1) {
|
|
|
+ if (kill_confirm_pid == sel->pid &&
|
|
|
+ (int)(now - kill_confirm_until) < 0) {
|
|
|
+ if (u_kill(sel->pid) == 0) {
|
|
|
+ char sb[64];
|
|
|
+ unsigned int sn = 0;
|
|
|
+
|
|
|
+ sb[0] = '\0';
|
|
|
+ s_append(sb, &sn, "Ended ", sizeof(sb));
|
|
|
+ s_append(sb, &sn, sel->name, sizeof(sb));
|
|
|
+ s_append(sb, &sn, " (pid ", sizeof(sb));
|
|
|
+ s_append_uint(sb, &sn, sel->pid, sizeof(sb));
|
|
|
+ s_append_ch(sb, &sn, ')', sizeof(sb));
|
|
|
+ s_copy(status_msg, sizeof(status_msg), sb);
|
|
|
+ status_error = 0;
|
|
|
+ status_until = now + 4000u;
|
|
|
+ selected_pid = 0;
|
|
|
+ } else {
|
|
|
+ s_copy(status_msg, sizeof(status_msg),
|
|
|
+ "Could not end that process");
|
|
|
+ status_error = 1;
|
|
|
+ status_until = now + 4000u;
|
|
|
+ }
|
|
|
+ kill_confirm_pid = 0;
|
|
|
+ } else {
|
|
|
+ kill_confirm_pid = sel->pid;
|
|
|
+ kill_confirm_until = now + 3000u;
|
|
|
+ s_copy(status_msg, sizeof(status_msg),
|
|
|
+ "Press End task again to confirm");
|
|
|
+ status_error = 1;
|
|
|
+ status_until = now + 3000u;
|
|
|
+ }
|
|
|
+ ngui_invalidate(u);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+static void draw_footer(struct ngui *u, struct ngui_rect r)
|
|
|
+{
|
|
|
+ struct ngui_rect text = r;
|
|
|
+ char line[192];
|
|
|
+ struct prow *sel = selected_row();
|
|
|
+
|
|
|
+ text.x += 2;
|
|
|
+ text.w -= 4;
|
|
|
+ fill_rect_(u, r.x, r.y, r.w, r.h, u->theme.panel);
|
|
|
+ if (status_until && (int)(u_ticks_ms() - status_until) < 0) {
|
|
|
+ ngui_text_ellipsis(u, text, text.x, text.y + 3, status_msg,
|
|
|
+ status_error ? u->theme.warning :
|
|
|
+ u->theme.success);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (sel) {
|
|
|
+ unsigned int n = 0;
|
|
|
+ char pct[16], mem[16], span[16];
|
|
|
+
|
|
|
+ fmt_pct(pct, sizeof(pct), sel->cpu_pm);
|
|
|
+ fmt_kb(mem, sizeof(mem), sel->pages * page_kb);
|
|
|
+ fmt_span(span, sizeof(span), sel->cpu_ticks);
|
|
|
+ line[0] = '\0';
|
|
|
+ s_append(line, &n, "pid ", sizeof(line));
|
|
|
+ s_append_uint(line, &n, sel->pid, sizeof(line));
|
|
|
+ s_append(line, &n, " ", sizeof(line));
|
|
|
+ s_append(line, &n, sel->name, sizeof(line));
|
|
|
+ s_append(line, &n, " ", sizeof(line));
|
|
|
+ s_append(line, &n, state_name(sel->state), sizeof(line));
|
|
|
+ s_append(line, &n, sel->kernel ? " (kernel thread)" : "",
|
|
|
+ sizeof(line));
|
|
|
+ s_append(line, &n, " cpu ", sizeof(line));
|
|
|
+ s_append(line, &n, pct, sizeof(line));
|
|
|
+ s_append(line, &n, " mem ", sizeof(line));
|
|
|
+ s_append(line, &n, mem, sizeof(line));
|
|
|
+ s_append(line, &n, " weight ", sizeof(line));
|
|
|
+ s_append_uint(line, &n, sel->weight, sizeof(line));
|
|
|
+ s_append(line, &n, " time ", sizeof(line));
|
|
|
+ s_append(line, &n, span, sizeof(line));
|
|
|
+ ngui_text_ellipsis(u, text, text.x, text.y + 3, line,
|
|
|
+ u->theme.text);
|
|
|
+ } else {
|
|
|
+ ngui_text_ellipsis(u, text, text.x, text.y + 3,
|
|
|
+ "Click a header to sort | PgUp/PgDn scroll | Esc close",
|
|
|
+ u->theme.text_dim);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+static void draw_header(struct ngui *u, struct ngui_rect r)
|
|
|
+{
|
|
|
+ struct ngui_rect text = r;
|
|
|
+ unsigned int uptime = u_ticks_ms() / 1000u;
|
|
|
+ char up[24], line[128];
|
|
|
+ unsigned int n = 0;
|
|
|
+
|
|
|
+ fmt_uptime(up, sizeof(up), uptime);
|
|
|
+ line[0] = '\0';
|
|
|
+ s_append(line, &n, "up ", sizeof(line));
|
|
|
+ s_append(line, &n, up, sizeof(line));
|
|
|
+ s_append(line, &n, " | ", sizeof(line));
|
|
|
+ s_append_uint(line, &n, ntotal, sizeof(line));
|
|
|
+ s_append(line, &n, ntotal == 1 ? " process" : " processes",
|
|
|
+ sizeof(line));
|
|
|
+ if (ncpus) {
|
|
|
+ s_append(line, &n, " | ", sizeof(line));
|
|
|
+ s_append_uint(line, &n, ncpus, sizeof(line));
|
|
|
+ s_append(line, &n, ncpus == 1 ? " CPU" : " CPUs", sizeof(line));
|
|
|
+ }
|
|
|
+ s_append(line, &n, " | runnable ", sizeof(line));
|
|
|
+ s_append_uint(line, &n, runnable, sizeof(line));
|
|
|
+ if (r.w > 560) {
|
|
|
+ text.w -= 100;
|
|
|
+ text_right(u, r.x + r.w, r.y, "1 s sample", u->theme.text_dim);
|
|
|
+ }
|
|
|
+ ngui_text_ellipsis(u, text, r.x, r.y, line, u->theme.text);
|
|
|
+}
|
|
|
+
|
|
|
+static void draw_graphs(struct ngui *u, int x, int y, int w, int h)
|
|
|
+{
|
|
|
+ int cw = (w - 2 * GAP) / 3;
|
|
|
+ int cw2 = w - 2 * (cw + GAP);
|
|
|
+ struct ngui_rect r;
|
|
|
+
|
|
|
+ r.x = x;
|
|
|
+ r.y = y;
|
|
|
+ r.w = cw;
|
|
|
+ r.h = h;
|
|
|
+ draw_cpu_card(u, r);
|
|
|
+ r.x = x + cw + GAP;
|
|
|
+ r.w = cw;
|
|
|
+ draw_mem_card(u, r);
|
|
|
+ r.x = x + 2 * (cw + GAP);
|
|
|
+ r.w = cw2;
|
|
|
+ draw_net_card(u, r);
|
|
|
+}
|
|
|
+
|
|
|
+static void draw(void)
|
|
|
+{
|
|
|
+ struct ngui_rect r;
|
|
|
+ int y, card_h, table_y, table_h;
|
|
|
+
|
|
|
+ fill_rect_(&ui, 0, 0, ui.w, ui.h, ui.theme.bg);
|
|
|
+
|
|
|
+ y = PAD;
|
|
|
+ r.x = PAD;
|
|
|
+ r.y = y;
|
|
|
+ r.w = ui.w - 2 * PAD;
|
|
|
+ r.h = HEAD_H;
|
|
|
+ draw_header(&ui, r);
|
|
|
+ y += HEAD_H;
|
|
|
+
|
|
|
+ card_h = ui.h < 470 ? 104 : 132;
|
|
|
+ if (ui.h < 390)
|
|
|
+ card_h = 84;
|
|
|
+ draw_graphs(&ui, PAD, y, ui.w - 2 * PAD, card_h);
|
|
|
+ y += card_h + 8;
|
|
|
+
|
|
|
+ r.x = PAD;
|
|
|
+ r.y = y;
|
|
|
+ r.w = ui.w - 2 * PAD;
|
|
|
+ r.h = 24;
|
|
|
+ draw_kill_button(&ui, r);
|
|
|
+ y += 28;
|
|
|
+
|
|
|
+ r.x = PAD;
|
|
|
+ r.y = y;
|
|
|
+ r.w = ui.w - 2 * PAD;
|
|
|
+ r.h = TABLE_HEAD;
|
|
|
+ draw_table_header(&ui, r);
|
|
|
+ y += TABLE_HEAD;
|
|
|
+
|
|
|
+ table_y = y;
|
|
|
+ table_h = ui.h - FOOTER_H - table_y - PAD / 2;
|
|
|
+ if (table_h < ROW_H)
|
|
|
+ table_h = ROW_H;
|
|
|
+ {
|
|
|
+ struct ngui_rect list;
|
|
|
+
|
|
|
+ list.x = PAD;
|
|
|
+ list.y = table_y;
|
|
|
+ list.w = ui.w - 2 * PAD;
|
|
|
+ list.h = table_h;
|
|
|
+ draw_table(&ui, list);
|
|
|
+ }
|
|
|
+
|
|
|
+ r.x = PAD;
|
|
|
+ r.y = ui.h - FOOTER_H;
|
|
|
+ r.w = ui.w - 2 * PAD;
|
|
|
+ r.h = FOOTER_H;
|
|
|
+ draw_footer(&ui, r);
|
|
|
+}
|
|
|
+
|
|
|
+/* ---- lifecycle ---- */
|
|
|
+
|
|
|
+static void clear_status(void)
|
|
|
+{
|
|
|
+ status_msg[0] = '\0';
|
|
|
+ status_until = 0;
|
|
|
+ status_error = 0;
|
|
|
+ kill_confirm_pid = 0;
|
|
|
+}
|
|
|
+
|
|
|
+static void tick_timers(void)
|
|
|
+{
|
|
|
+ unsigned int now = u_ticks_ms();
|
|
|
+
|
|
|
+ if (kill_confirm_pid && (int)(now - kill_confirm_until) >= 0) {
|
|
|
+ kill_confirm_pid = 0;
|
|
|
+ if (status_until && (int)(now - status_until) < 0)
|
|
|
+ clear_status();
|
|
|
+ ngui_invalidate(&ui);
|
|
|
+ }
|
|
|
+ if (status_until && (int)(now - status_until) >= 0) {
|
|
|
+ clear_status();
|
|
|
+ ngui_invalidate(&ui);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+static void move_selection(int step)
|
|
|
+{
|
|
|
+ int sel = -1;
|
|
|
+ unsigned int j;
|
|
|
+
|
|
|
+ for (j = 0; j < nrows; j++)
|
|
|
+ if (rows[j].pid == selected_pid) {
|
|
|
+ sel = (int)j;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ if (sel < 0)
|
|
|
+ sel = step > 0 ? -1 : (int)nrows;
|
|
|
+ sel += step;
|
|
|
+ if (sel < 0)
|
|
|
+ sel = 0;
|
|
|
+ if ((unsigned int)sel >= nrows)
|
|
|
+ sel = nrows ? (int)nrows - 1 : 0;
|
|
|
+ if (!nrows)
|
|
|
+ return;
|
|
|
+ selected_pid = rows[sel].pid;
|
|
|
+ kill_confirm_pid = 0;
|
|
|
+ ngui_scroll_ensure_visible(&table_scroll, sel * ROW_H, ROW_H);
|
|
|
+ ngui_invalidate(&ui);
|
|
|
+}
|
|
|
+
|
|
|
+void _start(const char *arg)
|
|
|
+{
|
|
|
+ struct win_info wi;
|
|
|
+ struct sys_schedinfo s;
|
|
|
+ struct sys_meminfo m;
|
|
|
+ struct sys_netinfo ni;
|
|
|
+ unsigned int sw = 0, sh = 0;
|
|
|
+ unsigned int w = 780, h = 560;
|
|
|
+ unsigned int i, ncur;
|
|
|
+
|
|
|
+ (void)arg;
|
|
|
+ if (u_screen_size(&sw, &sh) == 0) {
|
|
|
+ if (sw && w > sw - 30)
|
|
|
+ w = sw - 30;
|
|
|
+ if (sh && h > sh - 40)
|
|
|
+ h = sh - 40;
|
|
|
+ }
|
|
|
+ if (w < 260)
|
|
|
+ w = 260;
|
|
|
+ if (h < 260)
|
|
|
+ h = 260;
|
|
|
+ if (u_win_create(w, h, "Processes", &wi) != 0) {
|
|
|
+ u_puts("process: no graphics window\n");
|
|
|
+ u_exit(1);
|
|
|
+ }
|
|
|
+ ngui_init(&ui, (unsigned short *)wi.fb, (int)wi.width, (int)wi.height);
|
|
|
+ ngui_font_attach(&ui);
|
|
|
+ table_scroll.hide_bar = 0;
|
|
|
+
|
|
|
+ own_pid = (unsigned int)u_getpid();
|
|
|
+ u_syscall1(SYS_MEMINFO, (int)&m);
|
|
|
+ if (m.page_size >= 1024)
|
|
|
+ page_kb = m.page_size / 1024;
|
|
|
+ mem_total_kb = m.total_kb;
|
|
|
+ {
|
|
|
+ unsigned int free_kb = m.free_pages * (m.page_size / 1024);
|
|
|
+
|
|
|
+ mem_used_kb = mem_total_kb > free_kb ?
|
|
|
+ mem_total_kb - free_kb : 0;
|
|
|
+ mem_used_pm = mem_total_kb ?
|
|
|
+ mem_used_kb * 1000u / mem_total_kb : 0;
|
|
|
+ if (mem_used_pm > 1000u)
|
|
|
+ mem_used_pm = 1000u;
|
|
|
+ }
|
|
|
+ for (i = 0; i < PMAX; i++)
|
|
|
+ prev_info[i].name[0] = '\0';
|
|
|
+ nprev = 0;
|
|
|
+ ncur = snapshot_procs(pinfo);
|
|
|
+ for (i = 0; i < ncur; i++)
|
|
|
+ prev_info[i] = pinfo[i];
|
|
|
+ nprev = ncur;
|
|
|
+ build_rows(prev_info, nprev, 1, 0);
|
|
|
+
|
|
|
+ if (u_schedinfo(&s) == 0) {
|
|
|
+ last_idle_ticks = s.idle_ticks;
|
|
|
+ ncpus = s.ncpus;
|
|
|
+ runnable = s.runnable;
|
|
|
+ }
|
|
|
+ if (u_net_info(&ni) == 1 && ni.present) {
|
|
|
+ net_present = 1;
|
|
|
+ net_have_base = 1;
|
|
|
+ last_net_rx = ni.rx_bytes;
|
|
|
+ last_net_tx = ni.tx_bytes;
|
|
|
+ }
|
|
|
+ last_sample_ms = u_ticks_ms();
|
|
|
+
|
|
|
+ for (;;) {
|
|
|
+ int quit = 0;
|
|
|
+
|
|
|
+ tick_timers();
|
|
|
+ if ((int)(u_ticks_ms() - last_sample_ms) >= SAMPLE_MS) {
|
|
|
+ sample();
|
|
|
+ ngui_invalidate(&ui);
|
|
|
+ }
|
|
|
+ if (!ngui_begin(&ui)) {
|
|
|
+ ngui_end_ex(&ui, 0);
|
|
|
+ u_sleep_ms(LOOP_MS);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ {
|
|
|
+ int k;
|
|
|
+
|
|
|
+ /* Drain the key ring: a key left at the head would wedge
|
|
|
+ * every later one and eventually fill the 32-entry ring. */
|
|
|
+ while ((k = ngui_key(&ui)) != 0) {
|
|
|
+ if (k == NGUI_KEY_ESC) {
|
|
|
+ quit = 1;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ if (k == NGUI_KEY_UP)
|
|
|
+ move_selection(-1);
|
|
|
+ else if (k == NGUI_KEY_DOWN)
|
|
|
+ move_selection(1);
|
|
|
+ else if (k == NGUI_KEY_PAGE_UP)
|
|
|
+ page_req--;
|
|
|
+ else if (k == NGUI_KEY_PAGE_DOWN)
|
|
|
+ page_req++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ draw();
|
|
|
+ ngui_end(&ui);
|
|
|
+ if (quit)
|
|
|
+ break;
|
|
|
+ u_sleep_ms(LOOP_MS);
|
|
|
+ }
|
|
|
+ u_win_close();
|
|
|
+ u_exit(0);
|
|
|
+}
|