ucmd_top.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /* top: ring-3 command. Live process list with recent CPU and memory hints.
  2. *
  3. * The kernel credits every PIT tick to the process that was running it
  4. * (SYS_PROCINFO exposes the counters), so each frame samples the counters
  5. * twice, one second apart: CPU% is the share of ticks in that window. Idle
  6. * is not a process slot anymore, so an idle system simply shows little CPU
  7. * against the live processes.
  8. *
  9. * The frame is repainted in place. It has a fixed shape (23 rows x 63
  10. * columns + newlines), so the number of characters written per frame is a
  11. * constant; after the first paint the anchor is recomputed as
  12. * (cursor - FRAME_CHARS), which also corrects a scroll when the first frame
  13. * starts near the bottom of the screen. Every row is padded to 63 columns,
  14. * so stale text from a previous frame is erased, and the last row is written
  15. * without a trailing newline so a frame that ends on the bottom row cannot
  16. * scroll. Ctrl+C quits: the terminal kills its foreground command.
  17. */
  18. #include "ucmd_lib.h"
  19. #define FRAME_COLS 63 /* terminal screens are 64x32 (kernel screen.h) */
  20. #define COL_PID 4
  21. #define COL_STATE 7
  22. #define COL_W 3
  23. #define COL_CPU 6
  24. #define COL_TIME 6
  25. #define COL_MEM 6
  26. /* six single-space separators between the seven columns */
  27. #define NAME_W (FRAME_COLS - COL_PID - COL_STATE - COL_W - COL_CPU - \
  28. COL_TIME - COL_MEM - 6)
  29. #define PRINT_ROWS 20 /* process rows (header + footer excluded) */
  30. #define MAX_SAMPLE 32 /* processes captured per sample */
  31. #define FRAME_ROWS (3 + PRINT_ROWS)
  32. #define FRAME_CHARS (FRAME_ROWS * 64 - 1) /* 63 chars + '\n' per row, no final '\n' */
  33. /* CPUs ticks before the sampling window, matched by pid. */
  34. struct sample {
  35. unsigned int pid;
  36. unsigned int cpu_ticks;
  37. };
  38. static struct sample before[MAX_SAMPLE];
  39. static struct sample after[MAX_SAMPLE];
  40. static unsigned int nbefore, nafter;
  41. static struct sys_procinfo snaps[MAX_SAMPLE];
  42. struct prow {
  43. unsigned int pid;
  44. unsigned int state;
  45. unsigned int weight;
  46. unsigned int cpu_pm; /* CPU per mille over the window */
  47. unsigned int cpu_ticks; /* total ticks spent running */
  48. unsigned int pages; /* resident pages */
  49. char name[12];
  50. };
  51. static struct prow rows[PRINT_ROWS];
  52. static unsigned int nrows, ntotal;
  53. static unsigned int wall; /* sample window, ms */
  54. static unsigned int uptime_s;
  55. static unsigned int idle_pm; /* idle per mille over the window */
  56. static unsigned int free_kb, total_kb;
  57. static unsigned int line_chars; /* chars written into the current row */
  58. static void putc_(char c)
  59. {
  60. u_putc(c);
  61. line_chars++;
  62. }
  63. static void puts_(const char *s)
  64. {
  65. while (*s)
  66. putc_(*s++);
  67. }
  68. static void dec_(unsigned int v)
  69. {
  70. char buf[10];
  71. unsigned int n = 0;
  72. if (v == 0)
  73. buf[n++] = '0';
  74. while (v) {
  75. buf[n++] = (char)('0' + v % 10);
  76. v /= 10;
  77. }
  78. while (n)
  79. putc_(buf[--n]);
  80. }
  81. static void pad_(unsigned int n)
  82. {
  83. while (n--)
  84. putc_(' ');
  85. }
  86. /* Right-aligned decimal in `width` columns. */
  87. static void dec_w(unsigned int v, unsigned int width)
  88. {
  89. unsigned int digits = 1, t = v;
  90. while (t >= 10) {
  91. t /= 10;
  92. digits++;
  93. }
  94. if (digits < width)
  95. pad_(width - digits);
  96. dec_(v);
  97. }
  98. /* Left-aligned string, truncated/padded to exactly `width` columns. */
  99. static void str_w(const char *s, unsigned int width)
  100. {
  101. unsigned int n = 0;
  102. while (s[n] && n < width)
  103. putc_(s[n++]);
  104. if (n < width)
  105. pad_(width - n);
  106. }
  107. /* Right-aligned string (for the column headers, to sit over right-aligned
  108. * numbers). */
  109. static void str_wr(const char *s, unsigned int width)
  110. {
  111. unsigned int n = 0, i;
  112. while (s[n])
  113. n++;
  114. if (n < width)
  115. pad_(width - n);
  116. for (i = 0; i < n; i++)
  117. putc_(s[i]);
  118. }
  119. /* Right-aligned "x.y%" in COL_CPU columns. */
  120. static void cpu_w(unsigned int per_mille)
  121. {
  122. unsigned int whole = per_mille / 10;
  123. unsigned int tenth = per_mille % 10;
  124. unsigned int digits = 1, t = whole;
  125. while (t >= 10) {
  126. t /= 10;
  127. digits++;
  128. }
  129. pad_(COL_CPU - (digits + 3));
  130. dec_(whole);
  131. putc_('.');
  132. putc_((char)('0' + tenth));
  133. putc_('%');
  134. }
  135. static const char *state_name(unsigned int s)
  136. {
  137. if (s == 2)
  138. return "run";
  139. if (s == 1)
  140. return "ready";
  141. if (s == 4)
  142. return "sleep";
  143. if (s == 5)
  144. return "block";
  145. return "?";
  146. }
  147. /* Pad the current row to full width; rows other than the last end with a
  148. * newline, which combined with the padding keeps every frame the same size. */
  149. static void end_line(int last)
  150. {
  151. while (line_chars < FRAME_COLS)
  152. putc_(' ');
  153. if (!last) {
  154. u_putc('\n');
  155. line_chars = 0;
  156. }
  157. }
  158. /* Snapshot the live process list once. Both the "before" and "after"
  159. * samples go through this helper; no process-info syscall happens after the
  160. * window is closed, so CPU% and idle% refer to the same interval. */
  161. static unsigned int sample_procs(struct sample *dst, struct sys_procinfo *snap)
  162. {
  163. struct sys_procinfo p;
  164. unsigned int i, n = 0;
  165. for (i = 0; i < MAX_SAMPLE; i++) {
  166. if (u_syscall2(SYS_PROCINFO, (int)i, (int)&p) != 1)
  167. break;
  168. dst[n].pid = p.pid;
  169. dst[n].cpu_ticks = p.cpu_ticks;
  170. if (snap)
  171. snap[n] = p;
  172. n++;
  173. }
  174. return n;
  175. }
  176. /* Turn the snapshots into rows, CPU% = ticks during the window (a process
  177. * that appeared during the window shows 0%). */
  178. static void gather(void)
  179. {
  180. unsigned int i, j;
  181. ntotal = nafter;
  182. nrows = 0;
  183. for (i = 0; i < nafter && i < PRINT_ROWS; i++) {
  184. const struct sys_procinfo *p = &snaps[i];
  185. struct prow *r = &rows[i];
  186. unsigned int delta = 0, pm = 0;
  187. for (j = 0; j < nbefore; j++)
  188. if (before[j].pid == p->pid) {
  189. delta = p->cpu_ticks - before[j].cpu_ticks;
  190. break;
  191. }
  192. if (j < nbefore) {
  193. pm = delta * 1000u / wall;
  194. if (pm > 1000u)
  195. pm = 1000u;
  196. }
  197. r->pid = p->pid;
  198. r->state = p->state;
  199. r->weight = p->weight;
  200. r->cpu_pm = pm;
  201. r->cpu_ticks = p->cpu_ticks;
  202. r->pages = p->pages;
  203. for (j = 0; j < sizeof(r->name); j++)
  204. r->name[j] = p->name[j];
  205. nrows++;
  206. }
  207. }
  208. static void paint(void)
  209. {
  210. unsigned int i;
  211. line_chars = 0;
  212. /* status line */
  213. puts_("top: up ");
  214. dec_(uptime_s);
  215. puts_("s ");
  216. dec_(ntotal);
  217. puts_(ntotal == 1 ? " proc " : " procs ");
  218. puts_("mem ");
  219. dec_(free_kb);
  220. puts_("K free / ");
  221. dec_(total_kb);
  222. puts_("K");
  223. end_line(0);
  224. /* column headers */
  225. str_wr("PID", COL_PID);
  226. putc_(' ');
  227. str_w("STATE", COL_STATE);
  228. putc_(' ');
  229. str_wr("W", COL_W);
  230. putc_(' ');
  231. str_wr("CPU%", COL_CPU);
  232. putc_(' ');
  233. str_wr("TIME", COL_TIME);
  234. putc_(' ');
  235. str_wr("MEM", COL_MEM);
  236. putc_(' ');
  237. str_w("NAME", NAME_W);
  238. end_line(0);
  239. for (i = 0; i < PRINT_ROWS; i++) {
  240. if (i < nrows) {
  241. const struct prow *r = &rows[i];
  242. dec_w(r->pid, COL_PID);
  243. putc_(' ');
  244. str_w(state_name(r->state), COL_STATE);
  245. putc_(' ');
  246. dec_w(r->weight, COL_W);
  247. putc_(' ');
  248. cpu_w(r->cpu_pm);
  249. putc_(' ');
  250. dec_w(r->cpu_ticks / 1000, COL_TIME - 1);
  251. putc_('s');
  252. putc_(' ');
  253. dec_w(r->pages * 4, COL_MEM - 1);
  254. putc_('K');
  255. putc_(' ');
  256. str_w(r->name, NAME_W);
  257. }
  258. end_line(0);
  259. }
  260. if (ntotal > nrows) {
  261. putc_('+');
  262. dec_(ntotal - nrows);
  263. puts_(" more ");
  264. }
  265. puts_("Ctrl+C to quit idle ");
  266. dec_(idle_pm / 10);
  267. putc_('.');
  268. dec_(idle_pm % 10);
  269. putc_('%');
  270. end_line(1);
  271. }
  272. static void frame(void)
  273. {
  274. gather();
  275. paint();
  276. }
  277. void _start(const char *arg)
  278. {
  279. struct sys_meminfo m;
  280. struct sys_schedinfo s0, s1;
  281. unsigned int t0, t1;
  282. (void)arg;
  283. u_syscall1(SYS_MEMINFO, (int)&m);
  284. total_kb = m.total_kb;
  285. free_kb = m.free_pages * (m.page_size / 1024);
  286. for (;;) {
  287. /* Timestamp first: the sampling itself can be preempted, and the
  288. * per-process counters and idle ticks must refer to the same
  289. * window or the two percentages will not add up. */
  290. nbefore = sample_procs(before, 0);
  291. t0 = u_ticks_ms();
  292. u_schedinfo(&s0);
  293. u_sleep_ms(1000);
  294. nafter = sample_procs(after, snaps);
  295. t1 = u_ticks_ms();
  296. u_schedinfo(&s1);
  297. wall = t1 - t0;
  298. if (!wall)
  299. wall = 1;
  300. idle_pm = (s1.idle_ticks - s0.idle_ticks) * 1000u / wall;
  301. if (idle_pm > 1000u)
  302. idle_pm = 1000u;
  303. uptime_s = t1 / 1000;
  304. u_puts("\x1b[2J\x1b[H");
  305. frame();
  306. }
  307. }