rtstats.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <thread.h>
  4. #include <ip.h>
  5. #include <bio.h>
  6. #include <stdio.h>
  7. #include <draw.h>
  8. #include <mouse.h>
  9. #include <cursor.h>
  10. #include <keyboard.h>
  11. #include "realtime.h"
  12. #include "time.h"
  13. char *T = "200ms";
  14. char *Dl = "80ms";
  15. char *C = "40ms";
  16. #define numblocks(a, b) (((a) + (b) - 1) / (b))
  17. #define roundup(a, b) (numblocks((a), (b)) * (b))
  18. typedef struct {
  19. ulong tid;
  20. char *settings;
  21. int nevents;
  22. Schedevent *events;
  23. } Task;
  24. enum {
  25. Nevents = 1000,
  26. Ncolor = 6,
  27. K = 1024,
  28. STACKSIZE = 8 * K,
  29. };
  30. char *taskdir = "#R/realtime/task";
  31. char *clonedev = "#R/realtime/clone";
  32. char *profdev = "#R/realtime/nblog";
  33. char *timedev = "#R/realtime/time";
  34. Time prevts;
  35. int newwin;
  36. int Width = 1000;
  37. int Height = 100; // Per task
  38. int topmargin = 8;
  39. int bottommargin = 4;
  40. int lineht = 12;
  41. Schedevent *event;
  42. void drawrt(void);
  43. int schedparse(char*, char*, char*);
  44. static char *schedstatename[] = {
  45. [SRelease] = "Release",
  46. [SRun] = "Run",
  47. [SPreempt] = "Preempt",
  48. [SBlock] = "Block",
  49. [SResume] = "Resume",
  50. [SDeadline] = "Deadline",
  51. [SYield] = "Yield",
  52. [SSlice] = "Slice",
  53. [SExpel] = "Expel",
  54. };
  55. static int ntasks, besteffort, verbose;
  56. static Task *tasks;
  57. static Image *cols[Ncolor][3];
  58. static Font *mediumfont, *tinyfont;
  59. static Image *grey, *red, *green, *bg, *fg;
  60. char missstr[] = "sys: deadline miss: runtime";
  61. int schedfd;
  62. int
  63. rthandler(void*, char *s)
  64. {
  65. if (s && strncmp(missstr, s, strlen(missstr)) == 0)
  66. return 1;
  67. /* On any other note */
  68. if (!besteffort && fprint(schedfd, "remove") < 0)
  69. sysfatal("Could not remove task: %r");
  70. sysfatal("Removed task");
  71. return 1;
  72. }
  73. static void
  74. usage(void)
  75. {
  76. fprint(2, "Usage: %s [-d profdev] [-t timedev] [-b] [-v]\n", argv0);
  77. exits(nil);
  78. }
  79. void
  80. threadmain(int argc, char **argv)
  81. {
  82. ARGBEGIN {
  83. case 'T':
  84. T = EARGF(usage());
  85. break;
  86. case 'D':
  87. Dl = EARGF(usage());
  88. break;
  89. case 'C':
  90. C = EARGF(usage());
  91. break;
  92. case 'd':
  93. profdev = EARGF(usage());
  94. break;
  95. case 't':
  96. timedev = EARGF(usage());
  97. break;
  98. case 'b':
  99. besteffort++;
  100. break;
  101. case 'v':
  102. verbose++;
  103. break;
  104. case 'w':
  105. newwin++;
  106. break;
  107. default:
  108. usage();
  109. }
  110. ARGEND;
  111. if (argc != 0)
  112. usage();
  113. fmtinstall('T', timeconv);
  114. drawrt();
  115. }
  116. static void
  117. mkcol(int i, int c0, int c1, int c2)
  118. {
  119. cols[i][0] = allocimagemix(display, c0, DWhite);
  120. cols[i][1] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, c1);
  121. cols[i][2] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, c2);
  122. }
  123. static void
  124. colinit(void)
  125. {
  126. mediumfont = openfont(display, "/lib/font/bit/lucidasans/unicode.10.font");
  127. if(mediumfont == nil)
  128. mediumfont = font;
  129. tinyfont = openfont(display, "/lib/font/bit/lucidasans/unicode.7.font");
  130. if(tinyfont == nil)
  131. tinyfont = font;
  132. topmargin = mediumfont->height+2;
  133. bottommargin = tinyfont->height+2;
  134. /* Peach */
  135. mkcol(0, 0xFFAAAAFF, 0xFFAAAAFF, 0xBB5D5DFF);
  136. /* Aqua */
  137. mkcol(1, DPalebluegreen, DPalegreygreen, DPurpleblue);
  138. /* Yellow */
  139. mkcol(2, DPaleyellow, DDarkyellow, DYellowgreen);
  140. /* Green */
  141. mkcol(3, DPalegreen, DMedgreen, DDarkgreen);
  142. /* Blue */
  143. mkcol(4, 0x00AAFFFF, 0x00AAFFFF, 0x0088CCFF);
  144. /* Grey */
  145. cols[5][0] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0xEEEEEEFF);
  146. cols[5][1] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0xCCCCCCFF);
  147. cols[5][2] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0x888888FF);
  148. grey = cols[5][2];
  149. red = allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0xFF0000FF);
  150. green = allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0x00FF00FF);
  151. bg = display->white;
  152. fg = display->black;
  153. }
  154. static void
  155. redraw(int timefd, float scale)
  156. {
  157. int n, i, x;
  158. char buf[256];
  159. Time ts, now, period, ppp, oldestts;
  160. Point p, q;
  161. Rectangle r, rtime;
  162. Task *t;
  163. # define time2x(t) ((int)(((t) - oldestts) / ppp))
  164. if ((n = read(timefd, &now, sizeof(Time))) < 0)
  165. sysfatal("redraw: Cannot get time: %r\n");
  166. assert(n == sizeof(Time));
  167. period = (Time)(Onesecond * scale);
  168. ppp = (Time)(Onesecond * scale) / Width; // period per pixel.
  169. oldestts = now - period;
  170. /* white out time */
  171. rtime = screen->r;
  172. rtime.min.x = rtime.max.x - stringwidth(mediumfont, "0000000.000s");
  173. rtime.max.y = rtime.min.y + mediumfont->height;
  174. draw(screen, rtime, bg, nil, ZP);
  175. if (prevts < oldestts){
  176. prevts = oldestts;
  177. draw(screen, screen->r, bg, nil, ZP);
  178. p = screen->r.min;
  179. for (n = 0; n != ntasks; n++) {
  180. t = &tasks[n];
  181. /* p is upper left corner for this task */
  182. snprint(buf, sizeof(buf), "%ld ", t->tid);
  183. q = string(screen, p, fg, ZP, mediumfont, buf);
  184. snprint(buf, sizeof(buf), " %s", t->settings);
  185. string(screen, q, fg, ZP, tinyfont, buf);
  186. p.y += Height;
  187. }
  188. }
  189. x = time2x(prevts);
  190. p = screen->r.min;
  191. for (n = 0; n != ntasks; n++) {
  192. t = &tasks[n];
  193. /* p is upper left corner for this task */
  194. /* Move part already drawn */
  195. r = Rect(p.x, p.y + topmargin, p.x + x, p.y+Height);
  196. draw(screen, r, screen, nil, Pt(p.x + Width - x, p.y + topmargin));
  197. r.max.x = screen->r.max.x;
  198. r.min.x += x;
  199. draw(screen, r, bg, nil, ZP);
  200. line(screen, addpt(p, Pt(x, Height - lineht)), Pt(screen->r.max.x, p.y + Height - lineht),
  201. Endsquare, Endsquare, 0, cols[n % Ncolor][1], ZP);
  202. for (i = 0; i < t->nevents-1; i++)
  203. if (prevts < t->events[i + 1].ts)
  204. break;
  205. if (i > 0) {
  206. memmove(t->events, t->events + i,
  207. (t->nevents - i) * sizeof(Schedevent));
  208. t->nevents -= i;
  209. }
  210. for (i = 0; i != t->nevents; i++) {
  211. Schedevent *e = &t->events[i];
  212. int sx, ex, j;
  213. switch (e->etype) {
  214. case SRelease:
  215. if (e->ts > prevts && e->ts <= now) {
  216. sx = time2x(e->ts);
  217. line(screen, addpt(p, Pt(sx, topmargin)), addpt(p, Pt(sx, Height - bottommargin)),
  218. Endarrow, Endsquare, 1, fg, ZP);
  219. }
  220. break;
  221. case SDeadline:
  222. if (e->ts > prevts && e->ts <= now) {
  223. sx = time2x(e->ts);
  224. line(screen, addpt(p, Pt(sx, topmargin)), addpt(p, Pt(sx, Height - bottommargin)),
  225. Endsquare, Endarrow, 1, fg, ZP);
  226. }
  227. break;
  228. case SYield:
  229. if (e->ts > prevts && e->ts <= now) {
  230. sx = time2x(e->ts);
  231. line(screen, addpt(p, Pt(sx, topmargin)), addpt(p, Pt(sx, Height - bottommargin)),
  232. Endsquare, Endarrow, 0, green, ZP);
  233. }
  234. break;
  235. case SSlice:
  236. if (e->ts > prevts && e->ts <= now) {
  237. sx = time2x(e->ts);
  238. line(screen, addpt(p, Pt(sx, topmargin)), addpt(p, Pt(sx, Height - bottommargin)),
  239. Endsquare, Endarrow, 0, red, ZP);
  240. }
  241. break;
  242. case SBlock:
  243. case SExpel:
  244. break;
  245. case SPreempt:
  246. for (j = i + 1; j < t->nevents; j++) {
  247. if (t->events[j].etype == SRun)
  248. break;
  249. }
  250. if (j >= t->nevents)
  251. break;
  252. if (t->events[j].ts < prevts)
  253. break;
  254. sx = time2x(e->ts);
  255. ex = time2x(t->events[j].ts);
  256. r = Rect(sx, Height - lineht - 8, ex, Height - lineht);
  257. r = rectaddpt(r, p);
  258. draw(screen, r, cols[n % Ncolor][1], nil, ZP);
  259. break;
  260. case SRun:
  261. case SResume:
  262. for (j = i + 1; j < t->nevents; j++) {
  263. if (t->events[j].etype == SBlock
  264. || t->events[j].etype == SPreempt
  265. || t->events[j].etype == SYield
  266. || t->events[j].etype == SSlice)
  267. break;
  268. }
  269. if (j >= t->nevents)
  270. break;
  271. if (t->events[j].ts < prevts)
  272. break;
  273. sx = time2x(e->ts);
  274. ex = time2x(t->events[j].ts);
  275. r = Rect(sx, topmargin + 8, ex, Height - lineht);
  276. r = rectaddpt(r, p);
  277. draw(screen, r, cols[n % Ncolor][1], nil, ZP);
  278. break;
  279. }
  280. }
  281. p.y += Height;
  282. }
  283. ts = roundup(prevts, period / 10);
  284. x = time2x(ts);
  285. while (x < Dx(screen->r)) {
  286. p = screen->r.min;
  287. for (n = 0; n < ntasks; n++) {
  288. int height, width;
  289. /* p is upper left corner for this task */
  290. if ((ts % (period / 2)) == 0) {
  291. height = Height / 2;
  292. width = 1;
  293. }
  294. else {
  295. height = 3 * Height / 4;
  296. width = 0;
  297. }
  298. line(screen, addpt(p, Pt(x, height)), addpt(p, Pt(x, Height - lineht)),
  299. Endsquare, Endsquare, width, cols[n % Ncolor][2], ZP);
  300. p.y += Height;
  301. }
  302. if ((ts % (period / 2)) == 0) {
  303. snprint(buf, sizeof(buf), "%T", ts);
  304. string(screen, addpt(p, Pt(x - stringwidth(tinyfont, buf)/2, - tinyfont->height - 1)),
  305. fg, ZP, tinyfont, buf);
  306. }
  307. ts += period / 10;
  308. x += Width / 10;
  309. }
  310. snprint(buf, sizeof(buf), "%T ", now);
  311. string(screen, Pt(screen->r.max.x - stringwidth(mediumfont, buf), screen->r.min.y), fg, ZP, mediumfont, buf);
  312. flushimage(display, 1);
  313. prevts = now;
  314. }
  315. void
  316. drawrt(void)
  317. {
  318. char *wsys, line[256];
  319. int wfd, wctlfd, timefd, logfd, fd;
  320. Mousectl *mousectl;
  321. Keyboardctl *keyboardctl;
  322. int paused;
  323. float scale;
  324. Rune r;
  325. int i, m, n;
  326. Task *t;
  327. event = malloc(sizeof(Schedevent));
  328. assert(event);
  329. if ((logfd = open(profdev, OREAD)) < 0)
  330. sysfatal("%s: Cannot open %s: %r\n", argv0, profdev);
  331. if ((timefd = open(timedev, OREAD)) < 0)
  332. sysfatal("%s: Cannot open %s: %r\n", argv0, timedev);
  333. if (newwin){
  334. if ((wsys = getenv("wsys")) == nil)
  335. sysfatal("drawrt: Cannot find windowing system: %r\n");
  336. if ((wfd = open(wsys, ORDWR)) < 0)
  337. sysfatal("drawrt: Cannot open windowing system: %r\n");
  338. snprint(line, sizeof(line), "new -pid %d -dx %d -dy %d", getpid(), Width + 20, Height + 5);
  339. line[sizeof(line) - 1] = '\0';
  340. rfork(RFNAMEG);
  341. if (mount(wfd, -1, "/mnt/wsys", MREPL, line) < 0)
  342. sysfatal("drawrt: Cannot mount %s under /mnt/wsys: %r\n", line);
  343. if (bind("/mnt/wsys", "/dev", MBEFORE) < 0)
  344. sysfatal("drawrt: Cannot bind /mnt/wsys in /dev: %r\n");
  345. }
  346. if ((wctlfd = open("/dev/wctl", OWRITE)) < 0)
  347. sysfatal("drawrt: Cannot open /dev/wctl: %r\n");
  348. if (initdraw(nil, nil, "rtprof") < 0)
  349. sysfatal("drawrt: initdraw failure: %r\n");
  350. Width = Dx(screen->r);
  351. Height = Dy(screen->r);
  352. if ((mousectl = initmouse(nil, screen)) == nil)
  353. sysfatal("drawrt: cannot initialize mouse: %r\n");
  354. if ((keyboardctl = initkeyboard(nil)) == nil)
  355. sysfatal("drawrt: cannot initialize keyboard: %r\n");
  356. colinit();
  357. SET(schedfd);
  358. if (!besteffort){
  359. schedfd = schedparse(T, Dl, C);
  360. atnotify(rthandler, 1);
  361. }
  362. paused = 0;
  363. scale = 1.;
  364. while (1) {
  365. Alt a[] = {
  366. { mousectl->c, nil, CHANRCV },
  367. { mousectl->resizec, nil, CHANRCV },
  368. { keyboardctl->c, &r, CHANRCV },
  369. { nil, nil, CHANNOBLK },
  370. };
  371. switch (alt(a)) {
  372. case 0:
  373. continue;
  374. case 1:
  375. if (getwindow(display, Refnone) < 0)
  376. sysfatal("drawrt: Cannot re-attach window\n");
  377. if (newwin){
  378. if (Dx(screen->r) != Width || Dy(screen->r) != (ntasks * Height)){
  379. fprint(2, "resize: x: have %d, need %d; y: have %d, need %d\n",
  380. Dx(screen->r), Width + 8, Dy(screen->r), (ntasks * Height) + 8);
  381. fprint(wctlfd, "resize -dx %d -dy %d\n", Width + 8, (ntasks * Height) + 8);
  382. }
  383. }else{
  384. Width = Dx(screen->r);
  385. Height = Dy(screen->r)/ntasks;
  386. }
  387. break;
  388. case 2:
  389. switch (r) {
  390. case 'p':
  391. if (!paused)
  392. paused = 1;
  393. else
  394. paused = 0;
  395. prevts = 0;
  396. break;
  397. case '+':
  398. scale /= 2.;
  399. prevts = 0;
  400. break;
  401. case '-':
  402. scale *= 2.;
  403. prevts = 0;
  404. break;
  405. case 'q':
  406. rthandler(nil, nil);
  407. threadexitsall(nil);
  408. default:
  409. break;
  410. }
  411. break;
  412. case 3:
  413. while((n = read(logfd, event, sizeof(Schedevent))) > 0){
  414. assert(n == sizeof(Schedevent));
  415. if (verbose)
  416. print("%ud %T %s\n", event->tid, event->ts, schedstatename[event->etype]);
  417. for (i = 0; i < ntasks; i++)
  418. if (tasks[i].tid == event->tid)
  419. break;
  420. if (i == ntasks) {
  421. tasks = realloc(tasks, (ntasks + 1) * sizeof(Task));
  422. assert(tasks);
  423. t = &tasks[ntasks++];
  424. t->events = nil;
  425. t->settings = nil;
  426. t->nevents = 0;
  427. t->events = nil;
  428. t->tid = event->tid;
  429. snprint(line, sizeof line, "%s/%ud", taskdir, event->tid);
  430. if ((fd = open(line, OREAD)) < 0)
  431. goto expel;
  432. m = read(fd, line, sizeof line - 1);
  433. close(fd);
  434. if (m < 0)
  435. goto expel;
  436. line[m] = 0;
  437. if (m && line[--m] == '\n')
  438. line[m] = 0;
  439. t->settings = strdup(line);
  440. prevts = 0;
  441. if (newwin){
  442. fprint(wctlfd, "resize -dx %d -dy %d\n",
  443. Width + 20, (ntasks * Height) + 5);
  444. }else
  445. Height = Dy(screen->r)/ntasks;
  446. } else
  447. t = &tasks[i];
  448. if (event->etype == SExpel){
  449. expel:
  450. free(t->events);
  451. free(t->settings);
  452. ntasks--;
  453. memmove(t, t+1, sizeof(Task)*(ntasks-i));
  454. if (newwin)
  455. fprint(wctlfd, "resize -dx %d -dy %d\n",
  456. Width + 20, (ntasks * Height) + 5);
  457. else
  458. Height = Dy(screen->r)/ntasks;
  459. prevts = 0;
  460. }else{
  461. t->events = realloc(t->events, (t->nevents+1)*sizeof(Schedevent));
  462. assert(t->events);
  463. memcpy(&t->events[t->nevents], event, sizeof(Schedevent));
  464. t->nevents++;
  465. }
  466. }
  467. if (!paused)
  468. redraw(timefd, scale);
  469. }
  470. if (besteffort)
  471. sleep(100);
  472. else if (fprint(schedfd, "yield") < 0)
  473. sysfatal("yield: %r");
  474. }
  475. }
  476. int
  477. schedparse(char *period, char *deadline, char *slice)
  478. {
  479. int schedfd;
  480. if ((schedfd = open(clonedev, ORDWR)) < 0)
  481. sysfatal("%s: %r", clonedev);
  482. fprint(2, "T=%s D=%s C=%s procs=%d admit\n", period, deadline, slice, getpid());
  483. if (fprint(schedfd, "T=%s D=%s C=%s procs=%d admit", period, deadline, slice, getpid()) < 0)
  484. sysfatal("%s: %r", clonedev);
  485. return schedfd;
  486. }