ucmd_process.c 33 KB

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