ucmd_top.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. The
  6. * WM slot ("wm") runs the idle loop, so on an idle system it owns nearly
  7. * every tick -- the footer says as much.
  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_CPU 6
  23. #define COL_TIME 6
  24. #define COL_MEM 6
  25. /* five single-space separators between the six columns */
  26. #define NAME_W (FRAME_COLS - COL_PID - COL_STATE - COL_CPU - COL_TIME - COL_MEM - 5)
  27. #define PRINT_ROWS 20 /* process rows (header + footer excluded) */
  28. #define MAX_SAMPLE 32 /* processes captured per sample */
  29. #define FRAME_ROWS (3 + PRINT_ROWS)
  30. #define FRAME_CHARS (FRAME_ROWS * 64 - 1) /* 63 chars + '\n' per row, no final '\n' */
  31. /* CPUs ticks before the sampling window, matched by pid. */
  32. struct sample {
  33. unsigned int pid;
  34. unsigned int cpu_ticks;
  35. };
  36. static struct sample before[MAX_SAMPLE];
  37. static unsigned int nbefore;
  38. struct prow {
  39. unsigned int pid;
  40. unsigned int state;
  41. unsigned int cpu_pm; /* CPU per mille over the window */
  42. unsigned int cpu_ticks; /* total ticks spent running */
  43. unsigned int pages; /* resident pages */
  44. char name[12];
  45. };
  46. static struct prow rows[PRINT_ROWS];
  47. static unsigned int nrows, ntotal;
  48. static unsigned int wall; /* sample window, ms */
  49. static unsigned int uptime_s;
  50. static unsigned int free_kb, total_kb;
  51. static unsigned int line_chars; /* chars written into the current row */
  52. static void putc_(char c)
  53. {
  54. u_putc(c);
  55. line_chars++;
  56. }
  57. static void puts_(const char *s)
  58. {
  59. while (*s)
  60. putc_(*s++);
  61. }
  62. static void dec_(unsigned int v)
  63. {
  64. char buf[10];
  65. unsigned int n = 0;
  66. if (v == 0)
  67. buf[n++] = '0';
  68. while (v) {
  69. buf[n++] = (char)('0' + v % 10);
  70. v /= 10;
  71. }
  72. while (n)
  73. putc_(buf[--n]);
  74. }
  75. static void pad_(unsigned int n)
  76. {
  77. while (n--)
  78. putc_(' ');
  79. }
  80. /* Right-aligned decimal in `width` columns. */
  81. static void dec_w(unsigned int v, unsigned int width)
  82. {
  83. unsigned int digits = 1, t = v;
  84. while (t >= 10) {
  85. t /= 10;
  86. digits++;
  87. }
  88. if (digits < width)
  89. pad_(width - digits);
  90. dec_(v);
  91. }
  92. /* Left-aligned string, truncated/padded to exactly `width` columns. */
  93. static void str_w(const char *s, unsigned int width)
  94. {
  95. unsigned int n = 0;
  96. while (s[n] && n < width)
  97. putc_(s[n++]);
  98. if (n < width)
  99. pad_(width - n);
  100. }
  101. /* Right-aligned string (for the column headers, to sit over right-aligned
  102. * numbers). */
  103. static void str_wr(const char *s, unsigned int width)
  104. {
  105. unsigned int n = 0, i;
  106. while (s[n])
  107. n++;
  108. if (n < width)
  109. pad_(width - n);
  110. for (i = 0; i < n; i++)
  111. putc_(s[i]);
  112. }
  113. /* Right-aligned "x.y%" in COL_CPU columns. */
  114. static void cpu_w(unsigned int per_mille)
  115. {
  116. unsigned int whole = per_mille / 10;
  117. unsigned int tenth = per_mille % 10;
  118. unsigned int digits = 1, t = whole;
  119. while (t >= 10) {
  120. t /= 10;
  121. digits++;
  122. }
  123. pad_(COL_CPU - (digits + 3));
  124. dec_(whole);
  125. putc_('.');
  126. putc_((char)('0' + tenth));
  127. putc_('%');
  128. }
  129. static const char *state_name(unsigned int s)
  130. {
  131. if (s == 2)
  132. return "run";
  133. if (s == 1)
  134. return "ready";
  135. if (s == 4)
  136. return "sleep";
  137. return "?";
  138. }
  139. /* Pad the current row to full width; rows other than the last end with a
  140. * newline, which combined with the padding keeps every frame the same size. */
  141. static void end_line(int last)
  142. {
  143. while (line_chars < FRAME_COLS)
  144. putc_(' ');
  145. if (!last) {
  146. u_putc('\n');
  147. line_chars = 0;
  148. }
  149. }
  150. /* Take the "after" sample and turn it into rows, CPU% = ticks during the
  151. * window (a process that appeared during the window shows 0%). */
  152. static void gather(void)
  153. {
  154. struct sys_procinfo p;
  155. unsigned int i, j;
  156. ntotal = 0;
  157. nrows = 0;
  158. for (i = 0; ; i++) {
  159. if (u_syscall2(SYS_PROCINFO, (int)i, (int)&p) != 1)
  160. break;
  161. if (ntotal < PRINT_ROWS) {
  162. struct prow *r = &rows[ntotal];
  163. unsigned int delta = 0, pm = 0;
  164. for (j = 0; j < nbefore; j++)
  165. if (before[j].pid == p.pid) {
  166. delta = p.cpu_ticks - before[j].cpu_ticks;
  167. break;
  168. }
  169. if (j < nbefore) {
  170. pm = delta * 1000u / wall;
  171. if (pm > 1000u)
  172. pm = 1000u;
  173. }
  174. r->pid = p.pid;
  175. r->state = p.state;
  176. r->cpu_pm = pm;
  177. r->cpu_ticks = p.cpu_ticks;
  178. r->pages = p.pages;
  179. for (j = 0; j < sizeof(r->name); j++)
  180. r->name[j] = p.name[j];
  181. nrows++;
  182. }
  183. ntotal++;
  184. }
  185. }
  186. static void paint(void)
  187. {
  188. unsigned int i;
  189. line_chars = 0;
  190. /* status line */
  191. puts_("top: up ");
  192. dec_(uptime_s);
  193. puts_("s ");
  194. dec_(ntotal);
  195. puts_(ntotal == 1 ? " proc " : " procs ");
  196. puts_("mem ");
  197. dec_(free_kb);
  198. puts_("K free / ");
  199. dec_(total_kb);
  200. puts_("K");
  201. end_line(0);
  202. /* column headers */
  203. str_wr("PID", COL_PID);
  204. putc_(' ');
  205. str_w("STATE", COL_STATE);
  206. putc_(' ');
  207. str_wr("CPU%", COL_CPU);
  208. putc_(' ');
  209. str_wr("TIME", COL_TIME);
  210. putc_(' ');
  211. str_wr("MEM", COL_MEM);
  212. putc_(' ');
  213. str_w("NAME", NAME_W);
  214. end_line(0);
  215. for (i = 0; i < PRINT_ROWS; i++) {
  216. if (i < nrows) {
  217. const struct prow *r = &rows[i];
  218. dec_w(r->pid, COL_PID);
  219. putc_(' ');
  220. str_w(state_name(r->state), COL_STATE);
  221. putc_(' ');
  222. cpu_w(r->cpu_pm);
  223. putc_(' ');
  224. dec_w(r->cpu_ticks / 1000, COL_TIME - 1);
  225. putc_('s');
  226. putc_(' ');
  227. dec_w(r->pages * 4, COL_MEM - 1);
  228. putc_('K');
  229. putc_(' ');
  230. str_w(r->name, NAME_W);
  231. }
  232. end_line(0);
  233. }
  234. if (ntotal > nrows) {
  235. putc_('+');
  236. dec_(ntotal - nrows);
  237. puts_(" more ");
  238. }
  239. puts_("Ctrl+C to quit - wm = idle loop");
  240. end_line(1);
  241. }
  242. static void frame(void)
  243. {
  244. gather();
  245. paint();
  246. }
  247. void _start(const char *arg)
  248. {
  249. struct sys_meminfo m;
  250. struct sys_procinfo p;
  251. unsigned int i, t0, t1;
  252. (void)arg;
  253. u_syscall1(SYS_MEMINFO, (int)&m);
  254. total_kb = m.total_kb;
  255. free_kb = m.free_pages * (m.page_size / 1024);
  256. for (;;) {
  257. /* Sample before the wait, repaint after it. */
  258. nbefore = 0;
  259. for (i = 0; i < MAX_SAMPLE; i++) {
  260. if (u_syscall2(SYS_PROCINFO, (int)i, (int)&p) != 1)
  261. break;
  262. before[nbefore].pid = p.pid;
  263. before[nbefore].cpu_ticks = p.cpu_ticks;
  264. nbefore++;
  265. }
  266. t0 = u_ticks_ms();
  267. u_sleep_ms(1000);
  268. t1 = u_ticks_ms();
  269. wall = t1 - t0;
  270. if (!wall)
  271. wall = 1;
  272. uptime_s = t1 / 1000;
  273. u_puts("\x1b[2J\x1b[H");
  274. frame();
  275. }
  276. }