ucmd_process.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510
  1. /* process: graphical task manager, the ngui counterpart of `top`.
  2. *
  3. * One sample per second drives three history graphs (CPU busy, physical
  4. * memory in use and NIC throughput) and a sortable live process table.
  5. * CPU% comes from the per-process PIT counters (SYS_PROCINFO) sampled once
  6. * per second; the overall CPU graph uses the summed idle ticks from
  7. * SYS_SCHEDINFO, so it covers every CPU. Network rates are the deltas of the
  8. * netdev frame byte counters exposed through SYS_NET_INFO.
  9. *
  10. * The window is a normal canvas window. ngui only redraws when input or a
  11. * sample asks for it, so the app sleeps in 50 ms slices and wakes the
  12. * renderer once per sample. Esc closes it; column headers sort the table.
  13. */
  14. #include "ucmd_lib.h"
  15. #include "ngui.h"
  16. /* PROC_MAX in kernel/process.h (SYS_PROCINFO indexes live processes). */
  17. #define PMAX 64
  18. #define HIST 60
  19. #define SAMPLE_MS 1000
  20. #define LOOP_MS 50
  21. #define NET_KB_MAX 2000000u /* ~2 GB/s history ceiling */
  22. #define ROW_H 20
  23. #define PAD 10
  24. #define GAP 8
  25. #define HEAD_H 24
  26. #define TABLE_HEAD 22
  27. #define FOOTER_H 22
  28. enum {
  29. COL_PID,
  30. COL_STATE,
  31. COL_CPU,
  32. COL_MEM,
  33. COL_TIME,
  34. COL_NAME,
  35. COL_COUNT
  36. };
  37. enum {
  38. SORT_CPU,
  39. SORT_MEM,
  40. SORT_PID,
  41. SORT_TIME,
  42. SORT_NAME,
  43. SORT_STATE
  44. };
  45. enum {
  46. ID_KILL = 1
  47. };
  48. struct prow {
  49. unsigned int pid;
  50. unsigned int parent_pid;
  51. unsigned int depth; /* tree indentation, 0 for roots */
  52. unsigned int state;
  53. unsigned int weight;
  54. unsigned int cpu_pm;
  55. unsigned int cpu_ticks;
  56. unsigned int pages;
  57. unsigned int kernel;
  58. char name[12];
  59. };
  60. static struct ngui ui;
  61. static struct ngui_scroll table_scroll;
  62. static int page_req; /* PgUp/PgDn pages requested by the key ring */
  63. static struct sys_procinfo pinfo[PMAX];
  64. static struct sys_procinfo prev_info[PMAX];
  65. static unsigned int nprev;
  66. static struct prow rows[PMAX];
  67. static unsigned int nrows;
  68. static unsigned int ntotal;
  69. static unsigned int cpu_hist[HIST];
  70. static unsigned int mem_hist[HIST];
  71. static unsigned int rx_hist[HIST];
  72. static unsigned int tx_hist[HIST];
  73. static unsigned int last_sample_ms;
  74. static unsigned int last_idle_ticks;
  75. static unsigned int last_net_rx, last_net_tx;
  76. static int net_present;
  77. static int net_have_base;
  78. static unsigned int cpu_busy_pm;
  79. static unsigned int mem_used_pm;
  80. static unsigned int mem_used_kb;
  81. static unsigned int mem_total_kb;
  82. static unsigned int page_kb = 4;
  83. static unsigned int ncpus;
  84. static unsigned int runnable;
  85. static unsigned int net_rx_rate;
  86. static unsigned int net_tx_rate;
  87. static unsigned int selected_pid;
  88. static unsigned int own_pid;
  89. static int sort_mode = SORT_CPU;
  90. static int sort_desc = 1;
  91. static unsigned int kill_confirm_pid;
  92. static unsigned int kill_confirm_until;
  93. static char status_msg[128];
  94. static unsigned int status_until;
  95. static int status_error;
  96. /* ---- freestanding string/format helpers ---- */
  97. static unsigned int s_len(const char *s)
  98. {
  99. unsigned int n = 0;
  100. while (s && s[n])
  101. n++;
  102. return n;
  103. }
  104. static void s_copy(char *d, unsigned int cap, const char *s)
  105. {
  106. unsigned int i = 0;
  107. if (!cap)
  108. return;
  109. while (s && s[i] && i + 1 < cap) {
  110. d[i] = s[i];
  111. i++;
  112. }
  113. d[i] = '\0';
  114. }
  115. static void s_append(char *d, unsigned int *n, const char *s, unsigned int cap)
  116. {
  117. while (s && *s && *n + 1 < cap)
  118. d[(*n)++] = *s++;
  119. d[*n] = '\0';
  120. }
  121. static void s_append_ch(char *d, unsigned int *n, char c, unsigned int cap)
  122. {
  123. if (*n + 1 < cap) {
  124. d[(*n)++] = c;
  125. d[*n] = '\0';
  126. }
  127. }
  128. static void s_append_uint(char *d, unsigned int *n, unsigned int v,
  129. unsigned int cap)
  130. {
  131. char tmp[12];
  132. unsigned int i = 0, j;
  133. do {
  134. tmp[i++] = (char)('0' + v % 10);
  135. v /= 10;
  136. } while (v && i < sizeof(tmp));
  137. for (j = 0; j < i && *n + 1 < cap; j++)
  138. d[(*n)++] = tmp[i - 1 - j];
  139. d[*n] = '\0';
  140. }
  141. static void s_append_uint2(char *d, unsigned int *n, unsigned int v,
  142. unsigned int cap)
  143. {
  144. if (v < 10)
  145. s_append_ch(d, n, '0', cap);
  146. s_append_uint(d, n, v, cap);
  147. }
  148. static void s_append_pm(char *d, unsigned int *n, unsigned int pm,
  149. unsigned int cap)
  150. {
  151. if (pm > 1000)
  152. pm = 1000;
  153. s_append_uint(d, n, pm / 10, cap);
  154. s_append_ch(d, n, '.', cap);
  155. s_append_ch(d, n, (char)('0' + pm % 10), cap);
  156. s_append_ch(d, n, '%', cap);
  157. }
  158. static void fmt_pct(char *buf, unsigned int cap, unsigned int pm)
  159. {
  160. unsigned int n = 0;
  161. if (!cap)
  162. return;
  163. buf[0] = '\0';
  164. s_append_pm(buf, &n, pm, cap);
  165. }
  166. /* 12345 KB -> "12.1 MB"; 456 KB -> "456 KB"; 2 GB -> "2.0 GB". */
  167. static void fmt_kb(char *buf, unsigned int cap, unsigned int kb)
  168. {
  169. unsigned int n = 0, t;
  170. if (!cap)
  171. return;
  172. buf[0] = '\0';
  173. if (kb >= 1024u * 1024u) {
  174. t = (kb * 10u + 524288u) / (1024u * 1024u);
  175. s_append_uint(buf, &n, t / 10, cap);
  176. s_append_ch(buf, &n, '.', cap);
  177. s_append_ch(buf, &n, (char)('0' + t % 10), cap);
  178. s_append(buf, &n, " GB", cap);
  179. } else if (kb >= 1024) {
  180. t = (kb * 10u + 512u) / 1024u;
  181. s_append_uint(buf, &n, t / 10, cap);
  182. s_append_ch(buf, &n, '.', cap);
  183. s_append_ch(buf, &n, (char)('0' + t % 10), cap);
  184. s_append(buf, &n, " MB", cap);
  185. } else {
  186. s_append_uint(buf, &n, kb, cap);
  187. s_append(buf, &n, " KB", cap);
  188. }
  189. }
  190. /* Bytes per second -> "812 B/s", "12.3 KB/s", "1.4 MB/s". */
  191. static void fmt_rate(char *buf, unsigned int cap, unsigned int bps)
  192. {
  193. unsigned int n = 0, t;
  194. if (!cap)
  195. return;
  196. buf[0] = '\0';
  197. if (bps < 1024) {
  198. s_append_uint(buf, &n, bps, cap);
  199. s_append(buf, &n, " B/s", cap);
  200. } else if (bps < 1024u * 1024u) {
  201. t = (bps * 10u + 512u) / 1024u;
  202. s_append_uint(buf, &n, t / 10, cap);
  203. s_append_ch(buf, &n, '.', cap);
  204. s_append_ch(buf, &n, (char)('0' + t % 10), cap);
  205. s_append(buf, &n, " KB/s", cap);
  206. } else {
  207. unsigned int mb = bps / (1024u * 1024u);
  208. unsigned int rem = bps % (1024u * 1024u);
  209. t = mb * 10u + (rem * 10u + 524288u) / (1024u * 1024u);
  210. s_append_uint(buf, &n, t / 10, cap);
  211. s_append_ch(buf, &n, '.', cap);
  212. s_append_ch(buf, &n, (char)('0' + t % 10), cap);
  213. s_append(buf, &n, " MB/s", cap);
  214. }
  215. }
  216. /* KB/s history value -> a compact graph scale label. */
  217. static void fmt_rate_kb(char *buf, unsigned int cap, unsigned int kb)
  218. {
  219. fmt_rate(buf, cap, kb * 1024u);
  220. }
  221. static void fmt_span(char *buf, unsigned int cap, unsigned int ms)
  222. {
  223. unsigned int n = 0;
  224. if (!cap)
  225. return;
  226. buf[0] = '\0';
  227. if (ms < 60000) {
  228. unsigned int tenths = ms / 100;
  229. s_append_uint(buf, &n, tenths / 10, cap);
  230. s_append_ch(buf, &n, '.', cap);
  231. s_append_ch(buf, &n, (char)('0' + tenths % 10), cap);
  232. s_append_ch(buf, &n, 's', cap);
  233. } else if (ms < 3600000) {
  234. s_append_uint(buf, &n, ms / 60000, cap);
  235. s_append_ch(buf, &n, 'm', cap);
  236. s_append_uint2(buf, &n, (ms / 1000) % 60, cap);
  237. s_append_ch(buf, &n, 's', cap);
  238. } else {
  239. s_append_uint(buf, &n, ms / 3600000, cap);
  240. s_append_ch(buf, &n, 'h', cap);
  241. s_append_uint2(buf, &n, (ms / 60000) % 60, cap);
  242. s_append_ch(buf, &n, 'm', cap);
  243. }
  244. }
  245. static void fmt_uptime(char *buf, unsigned int cap, unsigned int s)
  246. {
  247. unsigned int n = 0;
  248. if (!cap)
  249. return;
  250. buf[0] = '\0';
  251. if (s >= 3600) {
  252. s_append_uint(buf, &n, s / 3600, cap);
  253. s_append_ch(buf, &n, 'h', cap);
  254. s_append_ch(buf, &n, ' ', cap);
  255. s_append_uint2(buf, &n, (s / 60) % 60, cap);
  256. s_append_ch(buf, &n, 'm', cap);
  257. } else if (s >= 60) {
  258. s_append_uint(buf, &n, s / 60, cap);
  259. s_append_ch(buf, &n, 'm', cap);
  260. s_append_ch(buf, &n, ' ', cap);
  261. s_append_uint2(buf, &n, s % 60, cap);
  262. s_append_ch(buf, &n, 's', cap);
  263. } else {
  264. s_append_uint(buf, &n, s, cap);
  265. s_append_ch(buf, &n, 's', cap);
  266. }
  267. }
  268. static int s_ci_cmp(const char *a, const char *b)
  269. {
  270. for (;;) {
  271. unsigned char ca = (unsigned char)*a++;
  272. unsigned char cb = (unsigned char)*b++;
  273. if (ca >= 'A' && ca <= 'Z')
  274. ca = (unsigned char)(ca + 32);
  275. if (cb >= 'A' && cb <= 'Z')
  276. cb = (unsigned char)(cb + 32);
  277. if (ca != cb)
  278. return (int)ca - (int)cb;
  279. if (!ca)
  280. return 0;
  281. }
  282. }
  283. static const char *state_name(unsigned int s)
  284. {
  285. if (s == 2)
  286. return "running";
  287. if (s == 1)
  288. return "ready";
  289. if (s == 4)
  290. return "sleep";
  291. if (s == 5)
  292. return "block";
  293. return "?";
  294. }
  295. /* Active first, then runnable, blocked, sleeping. */
  296. static unsigned int state_rank(unsigned int s)
  297. {
  298. if (s == 2)
  299. return 0;
  300. if (s == 1)
  301. return 1;
  302. if (s == 5)
  303. return 2;
  304. if (s == 4)
  305. return 3;
  306. return 4;
  307. }
  308. /* ---- colour helpers ---- */
  309. static unsigned short shade(unsigned short c, unsigned int num,
  310. unsigned int den)
  311. {
  312. unsigned int r = (c >> 11) & 0x1F;
  313. unsigned int g = (c >> 5) & 0x3F;
  314. unsigned int b = c & 0x1F;
  315. r = r * num / den;
  316. g = g * num / den;
  317. b = b * num / den;
  318. return (unsigned short)((r << 11) | (g << 5) | b);
  319. }
  320. static unsigned short mem_color(unsigned int pm)
  321. {
  322. if (pm >= 900)
  323. return ui.theme.error;
  324. if (pm >= 700)
  325. return ui.theme.warning;
  326. return ui.theme.success;
  327. }
  328. /* ---- sampling ---- */
  329. static unsigned int snapshot_procs(struct sys_procinfo *out)
  330. {
  331. struct sys_procinfo p;
  332. unsigned int i, n = 0;
  333. for (i = 0; i < PMAX; i++) {
  334. if (u_syscall2(SYS_PROCINFO, (int)i, (int)&p) != 1)
  335. break;
  336. out[n++] = p;
  337. }
  338. return n;
  339. }
  340. /* Ascending comparator; `sort_desc` inverts the whole order below. */
  341. static int row_cmp(const struct prow *a, const struct prow *b)
  342. {
  343. int c = 0;
  344. switch (sort_mode) {
  345. case SORT_MEM:
  346. if (a->pages != b->pages)
  347. c = a->pages < b->pages ? -1 : 1;
  348. break;
  349. case SORT_PID:
  350. if (a->pid != b->pid)
  351. c = a->pid < b->pid ? -1 : 1;
  352. break;
  353. case SORT_TIME:
  354. if (a->cpu_ticks != b->cpu_ticks)
  355. c = a->cpu_ticks < b->cpu_ticks ? -1 : 1;
  356. break;
  357. case SORT_NAME:
  358. c = s_ci_cmp(a->name, b->name);
  359. break;
  360. case SORT_STATE:
  361. if (state_rank(a->state) != state_rank(b->state))
  362. c = state_rank(a->state) < state_rank(b->state) ?
  363. -1 : 1;
  364. break;
  365. case SORT_CPU:
  366. default:
  367. if (a->cpu_pm != b->cpu_pm)
  368. c = a->cpu_pm < b->cpu_pm ? -1 : 1;
  369. break;
  370. }
  371. if (!c) {
  372. if (a->pid != b->pid)
  373. c = a->pid < b->pid ? -1 : 1;
  374. else
  375. c = s_ci_cmp(a->name, b->name);
  376. }
  377. return sort_desc ? -c : c;
  378. }
  379. static void sort_rows(void)
  380. {
  381. unsigned int i, j;
  382. for (i = 1; i < nrows; i++) {
  383. struct prow key = rows[i];
  384. j = i;
  385. while (j > 0 && row_cmp(&rows[j - 1], &key) > 0) {
  386. rows[j] = rows[j - 1];
  387. j--;
  388. }
  389. rows[j] = key;
  390. }
  391. }
  392. static int row_index_of_pid(unsigned int pid)
  393. {
  394. unsigned int i;
  395. for (i = 0; i < nrows; i++)
  396. if (rows[i].pid == pid)
  397. return (int)i;
  398. return -1;
  399. }
  400. /* Append `pid` and its descendants depth-first. The flat rows were sorted
  401. * first, so siblings keep the selected column order. */
  402. static void append_subtree(unsigned int pid, unsigned int depth,
  403. struct prow *out, unsigned int *nout,
  404. unsigned char *seen)
  405. {
  406. int idx = row_index_of_pid(pid);
  407. unsigned int i;
  408. if (idx < 0 || *nout >= PMAX || seen[idx])
  409. return;
  410. seen[idx] = 1;
  411. out[*nout] = rows[idx];
  412. out[*nout].depth = depth;
  413. (*nout)++;
  414. for (i = 0; i < nrows; i++)
  415. if (!seen[i] && rows[i].parent_pid == pid)
  416. append_subtree(rows[i].pid, depth + 1, out, nout, seen);
  417. }
  418. /* Reorder the sorted flat table as a process tree. A process whose parent is
  419. * not in the snapshot is treated as a root; cycles are broken by the seen
  420. * mask. */
  421. static void tree_order(void)
  422. {
  423. struct prow out[PMAX];
  424. unsigned char seen[PMAX];
  425. unsigned int i, n = 0;
  426. if (nrows > PMAX)
  427. nrows = PMAX;
  428. for (i = 0; i < nrows; i++)
  429. seen[i] = 0;
  430. for (i = 0; i < nrows; i++) {
  431. unsigned int p = rows[i].parent_pid;
  432. if (!seen[i] && (!p || row_index_of_pid(p) < 0))
  433. append_subtree(rows[i].pid, 0, out, &n, seen);
  434. }
  435. for (i = 0; i < nrows; i++)
  436. if (!seen[i])
  437. append_subtree(rows[i].pid, 0, out, &n, seen);
  438. for (i = 0; i < n; i++)
  439. rows[i] = out[i];
  440. }
  441. /* Turn a snapshot into table rows, computing CPU% from the previous sample. */
  442. static void build_rows(const struct sys_procinfo *cur, unsigned int n,
  443. unsigned int wall, int with_delta)
  444. {
  445. unsigned int i, j;
  446. nrows = n;
  447. ntotal = n;
  448. for (i = 0; i < n; i++) {
  449. struct prow *r = &rows[i];
  450. unsigned int pm = 0;
  451. if (with_delta && wall) {
  452. for (j = 0; j < nprev; j++)
  453. if (prev_info[j].pid == cur[i].pid) {
  454. unsigned int delta =
  455. cur[i].cpu_ticks -
  456. prev_info[j].cpu_ticks;
  457. pm = delta * 1000u / wall;
  458. if (pm > 1000u)
  459. pm = 1000u;
  460. break;
  461. }
  462. }
  463. r->pid = cur[i].pid;
  464. r->parent_pid = cur[i].parent_pid;
  465. r->depth = 0;
  466. r->state = cur[i].state;
  467. r->weight = cur[i].weight;
  468. r->cpu_pm = pm;
  469. r->cpu_ticks = cur[i].cpu_ticks;
  470. r->pages = cur[i].pages;
  471. r->kernel = cur[i].kernel_thread;
  472. s_copy(r->name, sizeof(r->name), cur[i].name);
  473. }
  474. sort_rows();
  475. tree_order();
  476. }
  477. /* Bytes over `wall` ms -> bytes per second without overflowing the
  478. * intermediate product at gigabit rates. */
  479. static unsigned int net_rate(unsigned int bytes, unsigned int wall)
  480. {
  481. if (!wall)
  482. wall = 1;
  483. return (bytes / wall) * 1000u + (bytes % wall) * 1000u / wall;
  484. }
  485. static void sample_net(unsigned int wall)
  486. {
  487. struct sys_netinfo ni;
  488. unsigned int drx, dtx;
  489. if (u_net_info(&ni) != 1 || !ni.present) {
  490. net_present = 0;
  491. net_have_base = 0;
  492. net_rx_rate = 0;
  493. net_tx_rate = 0;
  494. last_net_rx = 0;
  495. last_net_tx = 0;
  496. return;
  497. }
  498. net_present = 1;
  499. if (!net_have_base) {
  500. /* A NIC that appeared after the app started must not count its
  501. * whole boot-time traffic as one sample. */
  502. net_have_base = 1;
  503. last_net_rx = ni.rx_bytes;
  504. last_net_tx = ni.tx_bytes;
  505. net_rx_rate = 0;
  506. net_tx_rate = 0;
  507. return;
  508. }
  509. if (!wall)
  510. wall = 1;
  511. drx = ni.rx_bytes - last_net_rx;
  512. dtx = ni.tx_bytes - last_net_tx;
  513. last_net_rx = ni.rx_bytes;
  514. last_net_tx = ni.tx_bytes;
  515. net_rx_rate = net_rate(drx, wall);
  516. net_tx_rate = net_rate(dtx, wall);
  517. }
  518. static void hist_push(unsigned int *h, unsigned int v)
  519. {
  520. unsigned int i;
  521. for (i = 0; i + 1 < HIST; i++)
  522. h[i] = h[i + 1];
  523. h[HIST - 1] = v;
  524. }
  525. static void sample(void)
  526. {
  527. struct sys_schedinfo s;
  528. struct sys_meminfo m;
  529. unsigned int now = u_ticks_ms();
  530. unsigned int wall = now - last_sample_ms;
  531. unsigned int ncur, i, rx_kb, tx_kb;
  532. if (!wall)
  533. wall = 1;
  534. /* Process table: compare this snapshot with the previous second. */
  535. ncur = snapshot_procs(pinfo);
  536. build_rows(pinfo, ncur, wall, 1);
  537. for (i = 0; i < ncur; i++)
  538. prev_info[i] = pinfo[i];
  539. nprev = ncur;
  540. /* CPU: summed idle ticks across all CPUs over the sample window. */
  541. if (u_schedinfo(&s) == 0) {
  542. unsigned int diedle = s.idle_ticks - last_idle_ticks;
  543. unsigned int cpus = s.ncpus ? s.ncpus : 1;
  544. unsigned int idle_pm = diedle * 1000u / (wall * cpus);
  545. if (idle_pm > 1000u)
  546. idle_pm = 1000u;
  547. cpu_busy_pm = 1000u - idle_pm;
  548. ncpus = s.ncpus;
  549. runnable = s.runnable;
  550. last_idle_ticks = s.idle_ticks;
  551. }
  552. /* Physical memory: total minus free pages. */
  553. if (u_syscall1(SYS_MEMINFO, (int)&m) == 0) {
  554. unsigned int free_kb = m.free_pages * (m.page_size / 1024);
  555. if (m.page_size >= 1024)
  556. page_kb = m.page_size / 1024;
  557. mem_total_kb = m.total_kb;
  558. mem_used_kb = mem_total_kb > free_kb ?
  559. mem_total_kb - free_kb : 0;
  560. mem_used_pm = mem_total_kb ?
  561. mem_used_kb * 1000u / mem_total_kb : 0;
  562. if (mem_used_pm > 1000u)
  563. mem_used_pm = 1000u;
  564. }
  565. /* Network deltas are in bytes per second; the graph keeps KB/s. */
  566. sample_net(wall);
  567. rx_kb = (net_rx_rate + 1023u) / 1024u;
  568. tx_kb = (net_tx_rate + 1023u) / 1024u;
  569. if (rx_kb > NET_KB_MAX)
  570. rx_kb = NET_KB_MAX;
  571. if (tx_kb > NET_KB_MAX)
  572. tx_kb = NET_KB_MAX;
  573. hist_push(cpu_hist, cpu_busy_pm);
  574. hist_push(mem_hist, mem_used_pm);
  575. hist_push(rx_hist, rx_kb);
  576. hist_push(tx_hist, tx_kb);
  577. if (selected_pid) {
  578. unsigned int i;
  579. int found = 0;
  580. for (i = 0; i < nrows; i++)
  581. if (rows[i].pid == selected_pid) {
  582. found = 1;
  583. break;
  584. }
  585. if (!found) {
  586. selected_pid = 0;
  587. kill_confirm_pid = 0;
  588. }
  589. }
  590. last_sample_ms = now;
  591. }
  592. /* ---- drawing helpers ---- */
  593. static void text_right(struct ngui *u, int right, int y, const char *s,
  594. unsigned short color)
  595. {
  596. int w = (int)ngui_text_width_ui(u, s);
  597. ngui_text(u, right - w, y, s, color);
  598. }
  599. static void fill_rect_(struct ngui *u, int x, int y, int w, int h,
  600. unsigned short c)
  601. {
  602. struct ngui_rect r;
  603. r.x = x;
  604. r.y = y;
  605. r.w = w;
  606. r.h = h;
  607. ngui_fill_rect(u, r, c);
  608. }
  609. static void draw_card(struct ngui *u, struct ngui_rect r, const char *caption,
  610. const char *right)
  611. {
  612. ngui_fill_round_rect(u, r, 8, 8, 8, 8, u->theme.panel);
  613. ngui_text(u, r.x + 10, r.y + 7, caption, u->theme.text_dim);
  614. if (right && r.w > 160)
  615. text_right(u, r.x + r.w - 10, r.y + 7, right,
  616. u->theme.text_dim);
  617. }
  618. static int value_y(const struct ngui_rect *r, unsigned int v,
  619. unsigned int max)
  620. {
  621. unsigned int h;
  622. if (!max)
  623. max = 1;
  624. if (v > max)
  625. v = max;
  626. h = v * (unsigned int)r->h / max;
  627. return r->y + r->h - (int)h;
  628. }
  629. static void graph_grid(struct ngui *u, struct ngui_rect r)
  630. {
  631. int i;
  632. fill_rect_(u, r.x, r.y, r.w, r.h, NGUI_RGB(28, 32, 44));
  633. for (i = 1; i < 4; i++)
  634. fill_rect_(u, r.x, r.y + r.h * i / 4, r.w, 1,
  635. NGUI_RGB(46, 51, 66));
  636. ngui_frame(u, r, NGUI_RGB(56, 62, 80));
  637. }
  638. /* Filled history bars with a 2 px bright cap. */
  639. static void graph_bars(struct ngui *u, struct ngui_rect r,
  640. const unsigned int *hist, unsigned int max,
  641. unsigned short color)
  642. {
  643. int i;
  644. unsigned short dim = shade(color, 45, 100);
  645. if (max == 0)
  646. max = 1;
  647. for (i = 0; i < HIST; i++) {
  648. int x0 = r.x + i * r.w / HIST;
  649. int x1 = r.x + (i + 1) * r.w / HIST;
  650. int bw = x1 - x0;
  651. int y = value_y(&r, hist[i], max);
  652. int bh = r.y + r.h - y;
  653. if (bw < 1)
  654. bw = 1;
  655. if (bh <= 0)
  656. continue;
  657. fill_rect_(u, x0, y, bw - 1 > 0 ? bw - 1 : 1, bh, dim);
  658. fill_rect_(u, x0, y, bw - 1 > 0 ? bw - 1 : 1,
  659. bh < 2 ? bh : 2, color);
  660. }
  661. }
  662. /* A one-pixel history line (used for the TX overlay). */
  663. static void graph_line(struct ngui *u, struct ngui_rect r,
  664. const unsigned int *hist, unsigned int max,
  665. unsigned short color)
  666. {
  667. int i, px = 0, py = 0;
  668. if (max == 0)
  669. max = 1;
  670. for (i = 0; i < HIST; i++) {
  671. int x = r.x + i * (r.w - 1) / (HIST - 1);
  672. int y = value_y(&r, hist[i], max);
  673. if (i)
  674. ngui_line(u, px, py, x, y, color);
  675. if (i)
  676. ngui_line(u, px, py + 1, x, y + 1, color);
  677. px = x;
  678. py = y;
  679. }
  680. }
  681. static unsigned int history_peak(const unsigned int *a,
  682. const unsigned int *b)
  683. {
  684. unsigned int i, max = 0;
  685. for (i = 0; i < HIST; i++) {
  686. if (a[i] > max)
  687. max = a[i];
  688. if (b && b[i] > max)
  689. max = b[i];
  690. }
  691. return max;
  692. }
  693. static unsigned int history_max(const unsigned int *a, const unsigned int *b)
  694. {
  695. unsigned int max = history_peak(a, b);
  696. return max + max / 4 + 1;
  697. }
  698. static void draw_cpu_card(struct ngui *u, struct ngui_rect r)
  699. {
  700. struct ngui_rect g;
  701. char b[16], right[24];
  702. unsigned int n = 0;
  703. right[0] = '\0';
  704. if (ncpus) {
  705. s_append_uint(right, &n, ncpus, sizeof(right));
  706. s_append(right, &n, ncpus == 1 ? " CPU" : " CPUs", sizeof(right));
  707. }
  708. draw_card(u, r, "CPU", right);
  709. fmt_pct(b, sizeof(b), cpu_busy_pm);
  710. ngui_text_px(u, r.x + 10, r.y + 24, b, s_len(b), u->theme.accent,
  711. 24, 1, 0, 0);
  712. text_right(u, r.x + r.w - 10, r.y + 33, "busy", u->theme.text_dim);
  713. g.x = r.x + 10;
  714. g.y = r.y + 52;
  715. g.w = r.w - 20;
  716. g.h = r.h - g.y + r.y - 8;
  717. if (g.w < 10 || g.h < 10)
  718. return;
  719. graph_grid(u, g);
  720. graph_bars(u, g, cpu_hist, 1000, u->theme.accent);
  721. }
  722. static void draw_mem_card(struct ngui *u, struct ngui_rect r)
  723. {
  724. struct ngui_rect g;
  725. char b[16], used[16], total[16], detail[40];
  726. unsigned int n = 0;
  727. unsigned short color = mem_color(mem_used_pm);
  728. fmt_pct(b, sizeof(b), mem_used_pm);
  729. fmt_kb(used, sizeof(used), mem_used_kb);
  730. fmt_kb(total, sizeof(total), mem_total_kb);
  731. detail[0] = '\0';
  732. s_append(detail, &n, used, sizeof(detail));
  733. s_append(detail, &n, " / ", sizeof(detail));
  734. s_append(detail, &n, total, sizeof(detail));
  735. draw_card(u, r, "Memory", detail);
  736. ngui_text_px(u, r.x + 10, r.y + 24, b, s_len(b), color,
  737. 24, 1, 0, 0);
  738. text_right(u, r.x + r.w - 10, r.y + 33, "in use",
  739. u->theme.text_dim);
  740. g.x = r.x + 10;
  741. g.y = r.y + 52;
  742. g.w = r.w - 20;
  743. g.h = r.h - g.y + r.y - 8;
  744. if (g.w < 10 || g.h < 10)
  745. return;
  746. graph_grid(u, g);
  747. graph_bars(u, g, mem_hist, 1000, color);
  748. }
  749. static void draw_net_card(struct ngui *u, struct ngui_rect r)
  750. {
  751. struct ngui_rect g;
  752. char rx[18], tx[18], scale[18];
  753. unsigned int peak = history_peak(rx_hist, tx_hist);
  754. unsigned int max = history_max(rx_hist, tx_hist);
  755. fmt_rate(rx, sizeof(rx), net_rx_rate);
  756. fmt_rate(tx, sizeof(tx), net_tx_rate);
  757. if (!net_present)
  758. s_copy(scale, sizeof(scale), "no NIC");
  759. else if (!peak)
  760. s_copy(scale, sizeof(scale), "idle");
  761. else
  762. fmt_rate_kb(scale, sizeof(scale), max);
  763. draw_card(u, r, "Network", scale);
  764. if (net_present) {
  765. ngui_text(u, r.x + 10, r.y + 27, "RX", u->theme.success);
  766. ngui_text(u, r.x + 34, r.y + 27, rx, u->theme.text);
  767. {
  768. int txw = (int)ngui_text_width_ui(u, rx) + 52;
  769. ngui_text(u, r.x + txw, r.y + 27, "TX",
  770. u->theme.warning);
  771. ngui_text(u, r.x + txw + 24, r.y + 27, tx,
  772. u->theme.text);
  773. }
  774. } else {
  775. ngui_text(u, r.x + 10, r.y + 27, "no network device",
  776. u->theme.text_dim);
  777. }
  778. g.x = r.x + 10;
  779. g.y = r.y + 50;
  780. g.w = r.w - 20;
  781. g.h = r.h - g.y + r.y - 8;
  782. if (g.w < 10 || g.h < 10)
  783. return;
  784. graph_grid(u, g);
  785. if (net_present) {
  786. graph_bars(u, g, rx_hist, max, u->theme.success);
  787. graph_line(u, g, tx_hist, max, u->theme.warning);
  788. }
  789. }
  790. /* Column positions shared by the header, rows and click handling. */
  791. static void table_columns(const struct ngui_rect *r, int xs[COL_COUNT],
  792. int ws[COL_COUNT])
  793. {
  794. static const int fixed[COL_COUNT] = { 54, 62, 58, 66, 74, 0 };
  795. int x = r->x + 6;
  796. int i;
  797. for (i = 0; i < COL_COUNT; i++)
  798. ws[i] = fixed[i];
  799. ws[COL_NAME] = r->w - 6 - (54 + 62 + 58 + 66 + 74) - 20;
  800. if (ws[COL_NAME] < 30)
  801. ws[COL_NAME] = 30;
  802. for (i = 0; i < COL_COUNT; i++) {
  803. xs[i] = x;
  804. x += ws[i];
  805. }
  806. }
  807. static int sort_col(int col)
  808. {
  809. if (col == COL_PID)
  810. return SORT_PID;
  811. if (col == COL_CPU)
  812. return SORT_CPU;
  813. if (col == COL_MEM)
  814. return SORT_MEM;
  815. if (col == COL_TIME)
  816. return SORT_TIME;
  817. if (col == COL_NAME)
  818. return SORT_NAME;
  819. if (col == COL_STATE)
  820. return SORT_STATE;
  821. return -1;
  822. }
  823. static int sort_default_desc(int mode)
  824. {
  825. return mode == SORT_CPU || mode == SORT_MEM || mode == SORT_TIME;
  826. }
  827. static void set_sort(int col)
  828. {
  829. int mode = sort_col(col);
  830. if (mode < 0)
  831. return;
  832. if (mode == sort_mode)
  833. sort_desc = !sort_desc;
  834. else {
  835. sort_mode = mode;
  836. sort_desc = sort_default_desc(mode);
  837. }
  838. sort_rows();
  839. ngui_invalidate(&ui);
  840. }
  841. static void draw_table_header(struct ngui *u, struct ngui_rect r)
  842. {
  843. static const char *labels[COL_COUNT] = {
  844. "PID", "STATE", "CPU%", "MEM", "TIME", "NAME"
  845. };
  846. int xs[COL_COUNT], ws[COL_COUNT], i;
  847. fill_rect_(u, r.x, r.y, r.w, r.h, u->theme.panel_alt);
  848. table_columns(&r, xs, ws);
  849. for (i = 0; i < COL_COUNT; i++) {
  850. struct ngui_rect hr;
  851. char label[16];
  852. unsigned int n = 0;
  853. int mode = sort_col(i);
  854. int active = mode >= 0 && sort_mode == mode;
  855. int right = (i == COL_PID || i == COL_CPU || i == COL_MEM ||
  856. i == COL_TIME);
  857. label[0] = '\0';
  858. if (active && mode >= 0) {
  859. s_copy(label, sizeof(label), labels[i]);
  860. n = s_len(label);
  861. s_append_ch(label, &n, ' ', sizeof(label));
  862. s_append_ch(label, &n, sort_desc ? 'v' : '^',
  863. sizeof(label));
  864. } else {
  865. s_copy(label, sizeof(label), labels[i]);
  866. }
  867. hr.x = xs[i];
  868. hr.y = r.y;
  869. hr.w = ws[i];
  870. hr.h = r.h;
  871. if (mode >= 0)
  872. ngui_cursor_rect(u, hr, CURSOR_POINTER);
  873. if (right)
  874. text_right(u, xs[i] + ws[i] - 8, r.y + 3, label,
  875. active ? u->theme.accent : u->theme.text_dim);
  876. else
  877. ngui_text(u, xs[i], r.y + 3, label,
  878. active ? u->theme.accent : u->theme.text_dim);
  879. if (active)
  880. fill_rect_(u, xs[i], r.y + r.h - 2, ws[i] - 8, 2,
  881. u->theme.accent);
  882. if (mode >= 0 && u->pressed &&
  883. ngui_rect_contains(&hr, u->press_x, u->press_y))
  884. set_sort(i);
  885. }
  886. }
  887. static struct prow *selected_row(void)
  888. {
  889. unsigned int i;
  890. if (!selected_pid)
  891. return 0;
  892. for (i = 0; i < nrows; i++)
  893. if (rows[i].pid == selected_pid)
  894. return &rows[i];
  895. return 0;
  896. }
  897. static void draw_process_row(struct ngui *u, const struct ngui_rect *list,
  898. int row_index, int y, int selected, int hover)
  899. {
  900. int xs[COL_COUNT], ws[COL_COUNT];
  901. const struct prow *p = &rows[row_index];
  902. unsigned short fg = selected ? NGUI_RGB(255, 255, 255) :
  903. u->theme.text;
  904. char b[32];
  905. unsigned int n = 0;
  906. table_columns(list, xs, ws);
  907. if (selected)
  908. fill_rect_(u, list->x, y, list->w, ROW_H, u->theme.selection);
  909. else if (hover)
  910. fill_rect_(u, list->x, y, list->w, ROW_H,
  911. u->theme.panel_alt);
  912. s_append_uint(b, &n, p->pid, sizeof(b));
  913. ngui_text(u, xs[COL_PID] + ws[COL_PID] - 8 -
  914. (int)ngui_text_width_ui(u, b), y + 2, b, fg);
  915. ngui_text(u, xs[COL_STATE], y + 2, state_name(p->state),
  916. selected ? fg : (p->state == 2 ? u->theme.success :
  917. p->state == 5 ? u->theme.warning :
  918. u->theme.text_dim));
  919. b[0] = '\0';
  920. n = 0;
  921. s_append_pm(b, &n, p->cpu_pm, sizeof(b));
  922. text_right(u, xs[COL_CPU] + ws[COL_CPU] - 8, y + 2, b, fg);
  923. b[0] = '\0';
  924. n = 0;
  925. s_append_uint(b, &n, p->pages * page_kb, sizeof(b));
  926. s_append_ch(b, &n, 'K', sizeof(b));
  927. text_right(u, xs[COL_MEM] + ws[COL_MEM] - 8, y + 2, b, fg);
  928. fmt_span(b, sizeof(b), p->cpu_ticks);
  929. text_right(u, xs[COL_TIME] + ws[COL_TIME] - 8, y + 2, b, fg);
  930. {
  931. int nx = xs[COL_NAME];
  932. unsigned int d;
  933. for (d = 0; d + 1 < p->depth; d++)
  934. ngui_text(u, nx + (int)d * 14, y + 2, "|",
  935. selected ? fg : u->theme.text_dim);
  936. if (p->depth)
  937. ngui_text(u, nx + (int)(p->depth - 1) * 14, y + 2,
  938. "|-", selected ? fg : u->theme.text_dim);
  939. ngui_text(u, nx + (int)p->depth * 14, y + 2, p->name,
  940. p->kernel && !selected ? u->theme.text_dim : fg);
  941. }
  942. }
  943. static void draw_table(struct ngui *u, struct ngui_rect list)
  944. {
  945. unsigned int i;
  946. int first, last, y;
  947. int pressed_row = -1;
  948. table_scroll.content_h = (int)nrows * ROW_H;
  949. ngui_scroll_begin(u, list, &table_scroll);
  950. ngui_scroll_wheel(u, list, &table_scroll);
  951. if (page_req) {
  952. int page = table_scroll.view_h - 22;
  953. if (page < 22)
  954. page = 22;
  955. ngui_scroll_to(&table_scroll,
  956. table_scroll.offset + page_req * page);
  957. page_req = 0;
  958. }
  959. /* After ngui_scroll_begin(): a press on the overlay bar has been
  960. * consumed, so it cannot also select the row underneath. */
  961. if (u->pressed && ngui_rect_contains(&list, u->press_x, u->press_y)) {
  962. int idx = (u->press_y - list.y + table_scroll.offset) / ROW_H;
  963. if (idx >= 0 && (unsigned int)idx < nrows)
  964. pressed_row = idx;
  965. }
  966. if (pressed_row >= 0) {
  967. if (selected_pid != rows[pressed_row].pid) {
  968. selected_pid = rows[pressed_row].pid;
  969. kill_confirm_pid = 0;
  970. ngui_invalidate(u);
  971. }
  972. }
  973. fill_rect_(u, list.x, list.y, list.w, list.h, u->theme.panel);
  974. first = table_scroll.offset / ROW_H;
  975. if (first < 0)
  976. first = 0;
  977. last = (table_scroll.offset + list.h + ROW_H - 1) / ROW_H;
  978. if (last > (int)nrows)
  979. last = (int)nrows;
  980. for (i = (unsigned int)first; i < (unsigned int)last; i++) {
  981. int hover = u->inside &&
  982. ngui_rect_contains(&list, u->mouse_x, u->mouse_y) &&
  983. (u->mouse_y - list.y + table_scroll.offset) / ROW_H ==
  984. (int)i;
  985. int selected = rows[i].pid == selected_pid;
  986. y = list.y + (int)i * ROW_H - table_scroll.offset;
  987. draw_process_row(u, &list, (int)i, y, selected, hover);
  988. }
  989. ngui_scroll_end(u, list, &table_scroll);
  990. }
  991. static void draw_kill_button(struct ngui *u, struct ngui_rect r)
  992. {
  993. struct prow *sel = selected_row();
  994. struct ngui_rect br;
  995. char label[32];
  996. unsigned int now = u_ticks_ms();
  997. br.x = r.x + r.w - 150;
  998. br.y = r.y;
  999. br.w = 150;
  1000. br.h = 24;
  1001. if (!sel) {
  1002. text_right(u, r.x + r.w, r.y + 4, "Select a process",
  1003. u->theme.text_dim);
  1004. return;
  1005. }
  1006. if (sel->kernel || sel->pid == own_pid ||
  1007. s_ci_cmp(sel->name, "init") == 0) {
  1008. ngui_fill_round_rect(u, br, 6, 6, 6, 6, u->theme.panel_alt);
  1009. text_right(u, br.x + br.w - 10, br.y + 4,
  1010. sel->kernel ? "kernel thread" :
  1011. sel->pid == own_pid ? "this window" : "system init",
  1012. u->theme.text_dim);
  1013. ngui_cursor_rect(u, br, CURSOR_NOT_ALLOWED);
  1014. return;
  1015. }
  1016. if (kill_confirm_pid == sel->pid &&
  1017. (int)(now - kill_confirm_until) < 0) {
  1018. s_copy(label, sizeof(label), "Confirm end task");
  1019. } else {
  1020. s_copy(label, sizeof(label), "End task");
  1021. }
  1022. if (ngui_button(u, ID_KILL, br, label) == 1) {
  1023. if (kill_confirm_pid == sel->pid &&
  1024. (int)(now - kill_confirm_until) < 0) {
  1025. if (u_kill(sel->pid) == 0) {
  1026. char sb[64];
  1027. unsigned int sn = 0;
  1028. sb[0] = '\0';
  1029. s_append(sb, &sn, "Ended ", sizeof(sb));
  1030. s_append(sb, &sn, sel->name, sizeof(sb));
  1031. s_append(sb, &sn, " (pid ", sizeof(sb));
  1032. s_append_uint(sb, &sn, sel->pid, sizeof(sb));
  1033. s_append_ch(sb, &sn, ')', sizeof(sb));
  1034. s_copy(status_msg, sizeof(status_msg), sb);
  1035. status_error = 0;
  1036. status_until = now + 4000u;
  1037. selected_pid = 0;
  1038. } else {
  1039. s_copy(status_msg, sizeof(status_msg),
  1040. "Could not end that process");
  1041. status_error = 1;
  1042. status_until = now + 4000u;
  1043. }
  1044. kill_confirm_pid = 0;
  1045. } else {
  1046. kill_confirm_pid = sel->pid;
  1047. kill_confirm_until = now + 3000u;
  1048. s_copy(status_msg, sizeof(status_msg),
  1049. "Press End task again to confirm");
  1050. status_error = 1;
  1051. status_until = now + 3000u;
  1052. }
  1053. ngui_invalidate(u);
  1054. }
  1055. }
  1056. static void draw_footer(struct ngui *u, struct ngui_rect r)
  1057. {
  1058. struct ngui_rect text = r;
  1059. char line[192];
  1060. struct prow *sel = selected_row();
  1061. text.x += 2;
  1062. text.w -= 4;
  1063. fill_rect_(u, r.x, r.y, r.w, r.h, u->theme.panel);
  1064. if (status_until && (int)(u_ticks_ms() - status_until) < 0) {
  1065. ngui_text_ellipsis(u, text, text.x, text.y + 3, status_msg,
  1066. status_error ? u->theme.warning :
  1067. u->theme.success);
  1068. return;
  1069. }
  1070. if (sel) {
  1071. unsigned int n = 0;
  1072. char pct[16], mem[16], span[16];
  1073. fmt_pct(pct, sizeof(pct), sel->cpu_pm);
  1074. fmt_kb(mem, sizeof(mem), sel->pages * page_kb);
  1075. fmt_span(span, sizeof(span), sel->cpu_ticks);
  1076. line[0] = '\0';
  1077. s_append(line, &n, "pid ", sizeof(line));
  1078. s_append_uint(line, &n, sel->pid, sizeof(line));
  1079. s_append(line, &n, " ppid ", sizeof(line));
  1080. s_append_uint(line, &n, sel->parent_pid, sizeof(line));
  1081. s_append(line, &n, " ", sizeof(line));
  1082. s_append(line, &n, sel->name, sizeof(line));
  1083. s_append(line, &n, " ", sizeof(line));
  1084. s_append(line, &n, state_name(sel->state), sizeof(line));
  1085. s_append(line, &n, sel->kernel ? " (kernel thread)" : "",
  1086. sizeof(line));
  1087. s_append(line, &n, " cpu ", sizeof(line));
  1088. s_append(line, &n, pct, sizeof(line));
  1089. s_append(line, &n, " mem ", sizeof(line));
  1090. s_append(line, &n, mem, sizeof(line));
  1091. s_append(line, &n, " weight ", sizeof(line));
  1092. s_append_uint(line, &n, sel->weight, sizeof(line));
  1093. s_append(line, &n, " time ", sizeof(line));
  1094. s_append(line, &n, span, sizeof(line));
  1095. ngui_text_ellipsis(u, text, text.x, text.y + 3, line,
  1096. u->theme.text);
  1097. } else {
  1098. ngui_text_ellipsis(u, text, text.x, text.y + 3,
  1099. "Click a header to sort | PgUp/PgDn scroll | Esc close",
  1100. u->theme.text_dim);
  1101. }
  1102. }
  1103. static void draw_header(struct ngui *u, struct ngui_rect r)
  1104. {
  1105. struct ngui_rect text = r;
  1106. unsigned int uptime = u_ticks_ms() / 1000u;
  1107. char up[24], line[128];
  1108. unsigned int n = 0;
  1109. fmt_uptime(up, sizeof(up), uptime);
  1110. line[0] = '\0';
  1111. s_append(line, &n, "up ", sizeof(line));
  1112. s_append(line, &n, up, sizeof(line));
  1113. s_append(line, &n, " | ", sizeof(line));
  1114. s_append_uint(line, &n, ntotal, sizeof(line));
  1115. s_append(line, &n, ntotal == 1 ? " process" : " processes",
  1116. sizeof(line));
  1117. if (ncpus) {
  1118. s_append(line, &n, " | ", sizeof(line));
  1119. s_append_uint(line, &n, ncpus, sizeof(line));
  1120. s_append(line, &n, ncpus == 1 ? " CPU" : " CPUs", sizeof(line));
  1121. }
  1122. s_append(line, &n, " | runnable ", sizeof(line));
  1123. s_append_uint(line, &n, runnable, sizeof(line));
  1124. if (r.w > 560) {
  1125. text.w -= 100;
  1126. text_right(u, r.x + r.w, r.y, "1 s sample", u->theme.text_dim);
  1127. }
  1128. ngui_text_ellipsis(u, text, r.x, r.y, line, u->theme.text);
  1129. }
  1130. static void draw_graphs(struct ngui *u, int x, int y, int w, int h)
  1131. {
  1132. int cw = (w - 2 * GAP) / 3;
  1133. int cw2 = w - 2 * (cw + GAP);
  1134. struct ngui_rect r;
  1135. r.x = x;
  1136. r.y = y;
  1137. r.w = cw;
  1138. r.h = h;
  1139. draw_cpu_card(u, r);
  1140. r.x = x + cw + GAP;
  1141. r.w = cw;
  1142. draw_mem_card(u, r);
  1143. r.x = x + 2 * (cw + GAP);
  1144. r.w = cw2;
  1145. draw_net_card(u, r);
  1146. }
  1147. static void draw(void)
  1148. {
  1149. struct ngui_rect r;
  1150. int y, card_h, table_y, table_h;
  1151. fill_rect_(&ui, 0, 0, ui.w, ui.h, ui.theme.bg);
  1152. y = PAD;
  1153. r.x = PAD;
  1154. r.y = y;
  1155. r.w = ui.w - 2 * PAD;
  1156. r.h = HEAD_H;
  1157. draw_header(&ui, r);
  1158. y += HEAD_H;
  1159. card_h = ui.h < 470 ? 104 : 132;
  1160. if (ui.h < 390)
  1161. card_h = 84;
  1162. draw_graphs(&ui, PAD, y, ui.w - 2 * PAD, card_h);
  1163. y += card_h + 8;
  1164. r.x = PAD;
  1165. r.y = y;
  1166. r.w = ui.w - 2 * PAD;
  1167. r.h = 24;
  1168. draw_kill_button(&ui, r);
  1169. y += 28;
  1170. r.x = PAD;
  1171. r.y = y;
  1172. r.w = ui.w - 2 * PAD;
  1173. r.h = TABLE_HEAD;
  1174. draw_table_header(&ui, r);
  1175. y += TABLE_HEAD;
  1176. table_y = y;
  1177. table_h = ui.h - FOOTER_H - table_y - PAD / 2;
  1178. if (table_h < ROW_H)
  1179. table_h = ROW_H;
  1180. {
  1181. struct ngui_rect list;
  1182. list.x = PAD;
  1183. list.y = table_y;
  1184. list.w = ui.w - 2 * PAD;
  1185. list.h = table_h;
  1186. draw_table(&ui, list);
  1187. }
  1188. r.x = PAD;
  1189. r.y = ui.h - FOOTER_H;
  1190. r.w = ui.w - 2 * PAD;
  1191. r.h = FOOTER_H;
  1192. draw_footer(&ui, r);
  1193. }
  1194. /* ---- lifecycle ---- */
  1195. static void clear_status(void)
  1196. {
  1197. status_msg[0] = '\0';
  1198. status_until = 0;
  1199. status_error = 0;
  1200. kill_confirm_pid = 0;
  1201. }
  1202. static void tick_timers(void)
  1203. {
  1204. unsigned int now = u_ticks_ms();
  1205. if (kill_confirm_pid && (int)(now - kill_confirm_until) >= 0) {
  1206. kill_confirm_pid = 0;
  1207. if (status_until && (int)(now - status_until) < 0)
  1208. clear_status();
  1209. ngui_invalidate(&ui);
  1210. }
  1211. if (status_until && (int)(now - status_until) >= 0) {
  1212. clear_status();
  1213. ngui_invalidate(&ui);
  1214. }
  1215. }
  1216. static void move_selection(int step)
  1217. {
  1218. int sel = -1;
  1219. unsigned int j;
  1220. for (j = 0; j < nrows; j++)
  1221. if (rows[j].pid == selected_pid) {
  1222. sel = (int)j;
  1223. break;
  1224. }
  1225. if (sel < 0)
  1226. sel = step > 0 ? -1 : (int)nrows;
  1227. sel += step;
  1228. if (sel < 0)
  1229. sel = 0;
  1230. if ((unsigned int)sel >= nrows)
  1231. sel = nrows ? (int)nrows - 1 : 0;
  1232. if (!nrows)
  1233. return;
  1234. selected_pid = rows[sel].pid;
  1235. kill_confirm_pid = 0;
  1236. ngui_scroll_ensure_visible(&table_scroll, sel * ROW_H, ROW_H);
  1237. ngui_invalidate(&ui);
  1238. }
  1239. void _start(const char *arg)
  1240. {
  1241. struct win_info wi;
  1242. struct sys_schedinfo s;
  1243. struct sys_meminfo m;
  1244. struct sys_netinfo ni;
  1245. unsigned int sw = 0, sh = 0;
  1246. unsigned int w = 780, h = 560;
  1247. unsigned int i, ncur;
  1248. (void)arg;
  1249. if (u_screen_size(&sw, &sh) == 0) {
  1250. if (sw && w > sw - 30)
  1251. w = sw - 30;
  1252. if (sh && h > sh - 40)
  1253. h = sh - 40;
  1254. }
  1255. if (w < 260)
  1256. w = 260;
  1257. if (h < 260)
  1258. h = 260;
  1259. if (u_win_create(w, h, "Processes", &wi) != 0) {
  1260. u_puts("process: no graphics window\n");
  1261. u_exit(1);
  1262. }
  1263. ngui_init(&ui, (unsigned short *)wi.fb, (int)wi.width, (int)wi.height);
  1264. ngui_font_attach(&ui);
  1265. table_scroll.hide_bar = 0;
  1266. own_pid = (unsigned int)u_getpid();
  1267. u_syscall1(SYS_MEMINFO, (int)&m);
  1268. if (m.page_size >= 1024)
  1269. page_kb = m.page_size / 1024;
  1270. mem_total_kb = m.total_kb;
  1271. {
  1272. unsigned int free_kb = m.free_pages * (m.page_size / 1024);
  1273. mem_used_kb = mem_total_kb > free_kb ?
  1274. mem_total_kb - free_kb : 0;
  1275. mem_used_pm = mem_total_kb ?
  1276. mem_used_kb * 1000u / mem_total_kb : 0;
  1277. if (mem_used_pm > 1000u)
  1278. mem_used_pm = 1000u;
  1279. }
  1280. for (i = 0; i < PMAX; i++)
  1281. prev_info[i].name[0] = '\0';
  1282. nprev = 0;
  1283. ncur = snapshot_procs(pinfo);
  1284. for (i = 0; i < ncur; i++)
  1285. prev_info[i] = pinfo[i];
  1286. nprev = ncur;
  1287. build_rows(prev_info, nprev, 1, 0);
  1288. if (u_schedinfo(&s) == 0) {
  1289. last_idle_ticks = s.idle_ticks;
  1290. ncpus = s.ncpus;
  1291. runnable = s.runnable;
  1292. }
  1293. if (u_net_info(&ni) == 1 && ni.present) {
  1294. net_present = 1;
  1295. net_have_base = 1;
  1296. last_net_rx = ni.rx_bytes;
  1297. last_net_tx = ni.tx_bytes;
  1298. }
  1299. last_sample_ms = u_ticks_ms();
  1300. for (;;) {
  1301. int quit = 0;
  1302. tick_timers();
  1303. if ((int)(u_ticks_ms() - last_sample_ms) >= SAMPLE_MS) {
  1304. sample();
  1305. ngui_invalidate(&ui);
  1306. }
  1307. if (!ngui_begin(&ui)) {
  1308. ngui_end_ex(&ui, 0);
  1309. u_sleep_ms(LOOP_MS);
  1310. continue;
  1311. }
  1312. {
  1313. int k;
  1314. /* Drain the key ring: a key left at the head would wedge
  1315. * every later one and eventually fill the 32-entry ring. */
  1316. while ((k = ngui_key(&ui)) != 0) {
  1317. if (k == NGUI_KEY_ESC) {
  1318. quit = 1;
  1319. break;
  1320. }
  1321. if (k == NGUI_KEY_UP)
  1322. move_selection(-1);
  1323. else if (k == NGUI_KEY_DOWN)
  1324. move_selection(1);
  1325. else if (k == NGUI_KEY_PAGE_UP)
  1326. page_req--;
  1327. else if (k == NGUI_KEY_PAGE_DOWN)
  1328. page_req++;
  1329. }
  1330. }
  1331. draw();
  1332. ngui_end(&ui);
  1333. if (quit)
  1334. break;
  1335. u_sleep_ms(LOOP_MS);
  1336. }
  1337. u_win_close();
  1338. u_exit(0);
  1339. }