trace.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <tos.h>
  11. #include <libc.h>
  12. #include <thread.h>
  13. #include <ip.h>
  14. #include <bio.h>
  15. #include <draw.h>
  16. #include <mouse.h>
  17. #include <cursor.h>
  18. #include <keyboard.h>
  19. #include "trace.h"
  20. #pragma varargck type "t" int64_t
  21. #pragma varargck type "U" uint64_t
  22. #define NS(x) ((int64_t)x)
  23. #define US(x) (NS(x) * 1000ULL)
  24. #define MS(x) (US(x) * 1000ULL)
  25. #define S(x) (MS(x) * 1000ULL)
  26. #define numblocks(a, b) (((a) + (b) - 1) / (b))
  27. #define roundup(a, b) (numblocks((a), (b)) * (b))
  28. enum {
  29. OneRound = MS(1)/2LL,
  30. MilliRound = US(1)/2LL,
  31. };
  32. typedef struct Event Event;
  33. typedef struct Task Task;
  34. struct Event {
  35. Traceevent;
  36. int64_t etime; /* length of block to draw */
  37. };
  38. struct Task {
  39. int pid;
  40. char *name;
  41. int nevents;
  42. Event *events;
  43. int64_t tstart;
  44. int64_t total;
  45. int64_t runtime;
  46. int64_t runmax;
  47. int64_t runthis;
  48. int32_t runs;
  49. uint32_t tevents[Nevent];
  50. };
  51. enum {
  52. Nevents = 1024,
  53. Ncolor = 6,
  54. K = 1024,
  55. };
  56. int64_t now, prevts;
  57. int newwin;
  58. int Width = 1000;
  59. int Height = 100; // Per task
  60. int topmargin = 8;
  61. int bottommargin = 4;
  62. int lineht = 12;
  63. int wctlfd;
  64. int nevents;
  65. Traceevent *eventbuf;
  66. Event *event;
  67. void drawtrace(void);
  68. int schedparse(char*, char*, char*);
  69. int timeconv(Fmt*);
  70. char *schedstatename[] = {
  71. [SAdmit] = "Admit",
  72. [SSleep] = "Sleep",
  73. [SDead] = "Dead",
  74. [SDeadline] = "Deadline",
  75. [SEdf] = "Edf",
  76. [SExpel] = "Expel",
  77. [SReady] = "Ready",
  78. [SRelease] = "Release",
  79. [SRun] = "Run",
  80. [SSlice] = "Slice",
  81. [SInts] = "Ints",
  82. [SInte] = "Inte",
  83. [SUser] = "User",
  84. [SYield] = "Yield",
  85. };
  86. struct {
  87. int64_t scale;
  88. int64_t bigtics;
  89. int64_t littletics;
  90. int sleep;
  91. } scales[] = {
  92. { US(500), US(100), US(50), 0},
  93. { US(1000), US(500), US(100), 0},
  94. { US(2000), US(1000), US(200), 0},
  95. { US(5000), US(1000), US(500), 0},
  96. { MS(10), MS(5), MS(1), 20},
  97. { MS(20), MS(10), MS(2), 20},
  98. { MS(50), MS(10), MS(5), 20},
  99. { MS(100), MS(50), MS(10), 20}, /* starting scaleno */
  100. { MS(200), MS(100), MS(20), 20},
  101. { MS(500), MS(100), MS(50), 50},
  102. { MS(1000), MS(500), MS(100), 100},
  103. { MS(2000), MS(1000), MS(200), 100},
  104. { MS(5000), MS(1000), MS(500), 100},
  105. { S(10), S(50), S(1), 100},
  106. { S(20), S(10), S(2), 100},
  107. { S(50), S(10), S(5), 100},
  108. { S(100), S(50), S(10), 100},
  109. { S(200), S(100), S(20), 100},
  110. { S(500), S(100), S(50), 100},
  111. { S(1000), S(500), S(100), 100},
  112. };
  113. int ntasks, verbose, triggerproc, paused;
  114. Task *tasks;
  115. Image *cols[Ncolor][4];
  116. Font *mediumfont, *tinyfont;
  117. Image *grey, *red, *green, *blue, *bg, *fg;
  118. char*profdev = "/proc/trace";
  119. static void
  120. usage(void)
  121. {
  122. fprint(2, "Usage: %s [-d profdev] [-w] [-v] [-t triggerproc] [processes]\n", argv0);
  123. exits(nil);
  124. }
  125. void
  126. threadmain(int argc, char **argv)
  127. {
  128. int fd, i;
  129. char fname[80];
  130. fmtinstall('t', timeconv);
  131. ARGBEGIN {
  132. case 'd':
  133. profdev = EARGF(usage());
  134. break;
  135. case 'v':
  136. verbose = 1;
  137. break;
  138. case 'w':
  139. newwin++;
  140. break;
  141. case 't':
  142. triggerproc = (int)strtol(EARGF(usage()), nil, 0);
  143. break;
  144. default:
  145. usage();
  146. }
  147. ARGEND;
  148. fname[sizeof fname - 1] = 0;
  149. for(i = 0; i < argc; i++){
  150. snprint(fname, sizeof fname - 2, "/proc/%s/ctl",
  151. argv[i]);
  152. if((fd = open(fname, OWRITE)) < 0){
  153. fprint(2, "%s: cannot open %s: %r\n",
  154. argv[0], fname);
  155. continue;
  156. }
  157. if(fprint(fd, "trace 1") < 0)
  158. fprint(2, "%s: cannot enable tracing on %s: %r\n",
  159. argv[0], fname);
  160. close(fd);
  161. }
  162. drawtrace();
  163. }
  164. static void
  165. mkcol(int i, int c0, int c1, int c2)
  166. {
  167. cols[i][0] = allocimagemix(display, c0, DWhite);
  168. cols[i][1] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, c1);
  169. cols[i][2] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, c2);
  170. cols[i][3] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, c0);
  171. }
  172. static void
  173. colinit(void)
  174. {
  175. mediumfont = openfont(display, "/lib/font/bit/lucidasans/unicode.10.font");
  176. if(mediumfont == nil)
  177. mediumfont = font;
  178. tinyfont = openfont(display, "/lib/font/bit/lucidasans/unicode.7.font");
  179. if(tinyfont == nil)
  180. tinyfont = font;
  181. topmargin = mediumfont->height+2;
  182. bottommargin = tinyfont->height+2;
  183. /* Peach */
  184. mkcol(0, 0xFFAAAAFF, 0xFFAAAAFF, 0xBB5D5DFF);
  185. /* Aqua */
  186. mkcol(1, DPalebluegreen, DPalegreygreen, DPurpleblue);
  187. /* Yellow */
  188. mkcol(2, DPaleyellow, DDarkyellow, DYellowgreen);
  189. /* Green */
  190. mkcol(3, DPalegreen, DMedgreen, DDarkgreen);
  191. /* Blue */
  192. mkcol(4, 0x00AAFFFF, 0x00AAFFFF, 0x0088CCFF);
  193. /* Grey */
  194. cols[5][0] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0xEEEEEEFF);
  195. cols[5][1] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0xCCCCCCFF);
  196. cols[5][2] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0x888888FF);
  197. cols[5][3] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0xAAAAAAFF);
  198. grey = cols[5][2];
  199. red = allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0xFF0000FF);
  200. green = allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0x00FF00FF);
  201. blue = allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0x0000FFFF);
  202. bg = display->white;
  203. fg = display->black;
  204. }
  205. static void
  206. redraw(int scaleno)
  207. {
  208. int n, i, j, x;
  209. char buf[256];
  210. Point p, q;
  211. Rectangle r, rtime;
  212. Task *t;
  213. int64_t ts, oldestts, newestts, period, ppp, scale, s, ss;
  214. # define time2x(t) ((int)(((t) - oldestts) / ppp))
  215. scale = scales[scaleno].scale;
  216. period = scale + scales[scaleno].littletics;
  217. ppp = period / Width; // period per pixel.
  218. /* Round `now' to a nice number */
  219. newestts = now - (now % scales[scaleno].bigtics) +
  220. (scales[scaleno].littletics>>1);
  221. oldestts = newestts - period;
  222. //print("newestts %t, period %t, %d-%d\n", newestts, period, time2x(oldestts), time2x(newestts));
  223. if (prevts < oldestts){
  224. oldestts = newestts - period;
  225. prevts = oldestts;
  226. draw(screen, screen->r, bg, nil, ZP);
  227. }else{
  228. /* just white out time */
  229. rtime = screen->r;
  230. rtime.min.x = rtime.max.x - stringwidth(mediumfont, "00000000000.000s");
  231. rtime.max.y = rtime.min.y + mediumfont->height;
  232. draw(screen, rtime, bg, nil, ZP);
  233. }
  234. p = screen->r.min;
  235. for (n = 0; n != ntasks; n++) {
  236. t = &tasks[n];
  237. /* p is upper left corner for this task */
  238. rtime = Rpt(p, addpt(p, Pt(500, mediumfont->height)));
  239. draw(screen, rtime, bg, nil, ZP);
  240. snprint(buf, sizeof(buf), "%d %s", t->pid, t->name);
  241. q = string(screen, p, fg, ZP, mediumfont, buf);
  242. s = now - t->tstart;
  243. if(t->tevents[SRelease])
  244. snprint(buf, sizeof(buf), " per %t — avg: %t max: %t",
  245. (int64_t)(s/t->tevents[SRelease]),
  246. (int64_t)(t->runtime/t->tevents[SRelease]),
  247. t->runmax);
  248. else if((s /=1000000000LL) != 0)
  249. snprint(buf, sizeof(buf), " per 1s — avg: %t total: %t",
  250. t->total/s,
  251. t->total);
  252. else
  253. snprint(buf, sizeof(buf), " total: %t", t->total);
  254. string(screen, q, fg, ZP, tinyfont, buf);
  255. p.y += Height;
  256. }
  257. x = time2x(prevts);
  258. p = screen->r.min;
  259. for (n = 0; n != ntasks; n++) {
  260. t = &tasks[n];
  261. /* p is upper left corner for this task */
  262. /* Move part already drawn */
  263. r = Rect(p.x, p.y + topmargin, p.x + x, p.y+Height);
  264. draw(screen, r, screen, nil, Pt(p.x + Width - x, p.y + topmargin));
  265. r.max.x = screen->r.max.x;
  266. r.min.x += x;
  267. draw(screen, r, bg, nil, ZP);
  268. line(screen, addpt(p, Pt(x, Height - lineht)), Pt(screen->r.max.x, p.y + Height - lineht),
  269. Endsquare, Endsquare, 0, cols[n % Ncolor][1], ZP);
  270. for (i = 0; i < t->nevents-1; i++)
  271. if (prevts < t->events[i + 1].time)
  272. break;
  273. if (i > 0) {
  274. memmove(t->events, t->events + i, (t->nevents - i) * sizeof(Event));
  275. t->nevents -= i;
  276. }
  277. for (i = 0; i != t->nevents; i++) {
  278. Event *e = &t->events[i], *_e;
  279. int sx, ex;
  280. switch (e->etype & 0xffff) {
  281. case SAdmit:
  282. if (e->time > prevts && e->time <= newestts) {
  283. sx = time2x(e->time);
  284. line(screen, addpt(p, Pt(sx, topmargin)),
  285. addpt(p, Pt(sx, Height - bottommargin)),
  286. Endarrow, Endsquare, 1, green, ZP);
  287. }
  288. break;
  289. case SExpel:
  290. if (e->time > prevts && e->time <= newestts) {
  291. sx = time2x(e->time);
  292. line(screen, addpt(p, Pt(sx, topmargin)),
  293. addpt(p, Pt(sx, Height - bottommargin)),
  294. Endsquare, Endarrow, 1, red, ZP);
  295. }
  296. break;
  297. case SRelease:
  298. if (e->time > prevts && e->time <= newestts) {
  299. sx = time2x(e->time);
  300. line(screen, addpt(p, Pt(sx, topmargin)),
  301. addpt(p, Pt(sx, Height - bottommargin)),
  302. Endarrow, Endsquare, 1, fg, ZP);
  303. }
  304. break;
  305. case SDeadline:
  306. if (e->time > prevts && e->time <= newestts) {
  307. sx = time2x(e->time);
  308. line(screen, addpt(p, Pt(sx, topmargin)),
  309. addpt(p, Pt(sx, Height - bottommargin)),
  310. Endsquare, Endarrow, 1, fg, ZP);
  311. }
  312. break;
  313. case SYield:
  314. case SUser:
  315. if (e->time > prevts && e->time <= newestts) {
  316. sx = time2x(e->time);
  317. line(screen, addpt(p, Pt(sx, topmargin)),
  318. addpt(p, Pt(sx, Height - bottommargin)),
  319. Endsquare, Endarrow, 0,
  320. (e->etype == SYield)? green: blue, ZP);
  321. }
  322. break;
  323. case SSlice:
  324. if (e->time > prevts && e->time <= newestts) {
  325. sx = time2x(e->time);
  326. line(screen, addpt(p, Pt(sx, topmargin)),
  327. addpt(p, Pt(sx, Height - bottommargin)),
  328. Endsquare, Endarrow, 0, red, ZP);
  329. }
  330. break;
  331. case SRun:
  332. case SEdf:
  333. sx = time2x(e->time);
  334. ex = time2x(e->etime);
  335. if(ex == sx)
  336. ex++;
  337. r = Rect(sx, topmargin + 8, ex, Height - lineht);
  338. r = rectaddpt(r, p);
  339. draw(screen, r, cols[n % Ncolor][e->etype==SRun?1:3], nil, ZP);
  340. if(t->pid == triggerproc && ex < Width)
  341. paused ^= 1;
  342. for(j = 0; j < t->nevents; j++){
  343. _e = &t->events[j];
  344. switch(_e->etype & 0xffff){
  345. case SInts:
  346. if (_e->time > prevts && _e->time <= newestts){
  347. sx = time2x(_e->time);
  348. line(screen, addpt(p, Pt(sx, topmargin)),
  349. addpt(p, Pt(sx, Height / 2 - bottommargin)),
  350. Endsquare, Endsquare, 0,
  351. green, ZP);
  352. }
  353. break;
  354. case SInte:
  355. if (_e->time > prevts && _e->time <= newestts) {
  356. sx = time2x(_e->time);
  357. line(screen, addpt(p, Pt(sx, Height / 2 - bottommargin)),
  358. addpt(p, Pt(sx, Height - bottommargin)),
  359. Endsquare, Endsquare, 0,
  360. blue, ZP);
  361. }
  362. break;
  363. }
  364. }
  365. break;
  366. }
  367. }
  368. p.y += Height;
  369. }
  370. ts = prevts + scales[scaleno].littletics - (prevts % scales[scaleno].littletics);
  371. x = time2x(ts);
  372. while(x < Width){
  373. p = screen->r.min;
  374. for(n = 0; n < ntasks; n++){
  375. int height, width;
  376. /* p is upper left corner for this task */
  377. if ((ts % scales[scaleno].scale) == 0){
  378. height = 10 * Height;
  379. width = 1;
  380. }else if ((ts % scales[scaleno].bigtics) == 0){
  381. height = 12 * Height;
  382. width = 0;
  383. }else{
  384. height = 13 * Height;
  385. width = 0;
  386. }
  387. height >>= 4;
  388. line(screen, addpt(p, Pt(x, height)), addpt(p, Pt(x, Height - lineht)),
  389. Endsquare, Endsquare, width, cols[n % Ncolor][2], ZP);
  390. p.y += Height;
  391. }
  392. ts += scales[scaleno].littletics;
  393. x = time2x(ts);
  394. }
  395. rtime = screen->r;
  396. rtime.min.y = rtime.max.y - tinyfont->height + 2;
  397. draw(screen, rtime, bg, nil, ZP);
  398. ts = oldestts + scales[scaleno].bigtics - (oldestts % scales[scaleno].bigtics);
  399. x = time2x(ts);
  400. ss = 0;
  401. while(x < Width){
  402. snprint(buf, sizeof(buf), "%t", ss);
  403. string(screen, addpt(p, Pt(x - stringwidth(tinyfont, buf)/2, - tinyfont->height - 1)),
  404. fg, ZP, tinyfont, buf);
  405. ts += scales[scaleno].bigtics;
  406. ss += scales[scaleno].bigtics;
  407. x = time2x(ts);
  408. }
  409. snprint(buf, sizeof(buf), "%t", now);
  410. string(screen, Pt(screen->r.max.x - stringwidth(mediumfont, buf), screen->r.min.y),
  411. fg, ZP, mediumfont, buf);
  412. flushimage(display, 1);
  413. prevts = newestts;
  414. }
  415. Task*
  416. newtask(uint32_t pid)
  417. {
  418. Task *t;
  419. char buf[64], *p;
  420. int fd,n;
  421. tasks = realloc(tasks, (ntasks + 1) * sizeof(Task));
  422. assert(tasks);
  423. t = &tasks[ntasks++];
  424. memset(t, 0, sizeof(Task));
  425. t->events = nil;
  426. snprint(buf, sizeof buf, "/proc/%ld/status", pid);
  427. t->name = nil;
  428. fd = open(buf, OREAD);
  429. if (fd >= 0){
  430. n = read(fd, buf, sizeof buf);
  431. if(n > 0){
  432. p = buf + sizeof buf - 1;
  433. *p = 0;
  434. p = strchr(buf, ' ');
  435. if (p) *p = 0;
  436. t->name = strdup(buf);
  437. }else
  438. print("%s: %r\n", buf);
  439. close(fd);
  440. }else
  441. print("%s: %r\n", buf);
  442. t->pid = pid;
  443. prevts = 0;
  444. if (newwin){
  445. fprint(wctlfd, "resize -dx %d -dy %d\n",
  446. Width + 20, (ntasks * Height) + 5);
  447. }else
  448. Height = ntasks ? Dy(screen->r)/ntasks : Dy(screen->r);
  449. return t;
  450. }
  451. void
  452. doevent(Task *t, Traceevent *ep)
  453. {
  454. int i, n;
  455. Event *event;
  456. int64_t runt;
  457. t->tevents[ep->etype & 0xffff]++;
  458. n = t->nevents++;
  459. t->events = realloc(t->events, t->nevents*sizeof(Event));
  460. assert(t->events);
  461. event = &t->events[n];
  462. memmove(event, ep, sizeof(Traceevent));
  463. event->etime = 0;
  464. switch(event->etype & 0xffff){
  465. case SRelease:
  466. if (t->runthis > t->runmax)
  467. t->runmax = t->runthis;
  468. t->runthis = 0;
  469. break;
  470. case SSleep:
  471. case SYield:
  472. case SReady:
  473. case SSlice:
  474. for(i = n-1; i >= 0; i--)
  475. if (t->events[i].etype == SRun ||
  476. t->events[i].etype == SEdf)
  477. break;
  478. if(i < 0 || t->events[i].etime != 0)
  479. break;
  480. runt = event->time - t->events[i].time;
  481. if(runt > 0){
  482. t->events[i].etime = event->time;
  483. t->runtime += runt;
  484. t->total += runt;
  485. t->runthis += runt;
  486. t->runs++;
  487. }
  488. break;
  489. case SDead:
  490. print("task died %ld %t %s\n", event->pid, event->time, schedstatename[event->etype & 0xffff]);
  491. free(t->events);
  492. free(t->name);
  493. ntasks--;
  494. memmove(t, t+1, sizeof(Task)*(&tasks[ntasks]-t));
  495. if (newwin)
  496. fprint(wctlfd, "resize -dx %d -dy %d\n",
  497. Width + 20, (ntasks * Height) + 5);
  498. else
  499. Height = ntasks ? Dy(screen->r)/ntasks : Dy(screen->r);
  500. prevts = 0;
  501. }
  502. }
  503. void
  504. drawtrace(void)
  505. {
  506. char *wsys, line[256];
  507. int wfd, logfd;
  508. Mousectl *mousectl;
  509. Keyboardctl *keyboardctl;
  510. int scaleno;
  511. Rune r;
  512. int i, n;
  513. Task *t;
  514. Traceevent *ep;
  515. eventbuf = malloc(Nevents*sizeof(Traceevent));
  516. assert(eventbuf);
  517. if((logfd = open(profdev, OREAD)) < 0)
  518. sysfatal("%s: Cannot open %s: %r", argv0, profdev);
  519. if(newwin){
  520. if((wsys = getenv("wsys")) == nil)
  521. sysfatal("%s: Cannot find windowing system: %r",
  522. argv0);
  523. if((wfd = open(wsys, ORDWR)) < 0)
  524. sysfatal("%s: Cannot open windowing system: %r",
  525. argv0);
  526. snprint(line, sizeof(line), "new -pid %d -dx %d -dy %d",
  527. getpid(), Width + 20, Height + 5);
  528. line[sizeof(line) - 1] = '\0';
  529. rfork(RFNAMEG);
  530. if(mount(wfd, -1, "/mnt/wsys", MREPL, line) < 0)
  531. sysfatal("%s: Cannot mount %s under /mnt/wsys: %r",
  532. argv0, line);
  533. if(bind("/mnt/wsys", "/dev", MBEFORE) < 0)
  534. sysfatal("%s: Cannot bind /mnt/wsys in /dev: %r",
  535. argv0);
  536. }
  537. if((wctlfd = open("/dev/wctl", OWRITE)) < 0)
  538. sysfatal("%s: Cannot open /dev/wctl: %r", argv0);
  539. if(initdraw(nil, nil, "trace") < 0)
  540. sysfatal("%s: initdraw failure: %r", argv0);
  541. Width = Dx(screen->r);
  542. Height = Dy(screen->r);
  543. if((mousectl = initmouse(nil, screen)) == nil)
  544. sysfatal("%s: cannot initialize mouse: %r", argv0);
  545. if((keyboardctl = initkeyboard(nil)) == nil)
  546. sysfatal("%s: cannot initialize keyboard: %r", argv0);
  547. colinit();
  548. paused = 0;
  549. scaleno = 7; /* 100 milliseconds */
  550. now = nsec();
  551. for(;;) {
  552. Alt a[] = {
  553. { mousectl->c, nil, CHANRCV },
  554. { mousectl->resizec, nil, CHANRCV },
  555. { keyboardctl->c, &r, CHANRCV },
  556. { nil, nil, CHANNOBLK },
  557. };
  558. switch (alt(a)) {
  559. case 0:
  560. continue;
  561. case 1:
  562. if(getwindow(display, Refnone) < 0)
  563. sysfatal("drawrt: Cannot re-attach window");
  564. if(newwin){
  565. if(Dx(screen->r) != Width ||
  566. Dy(screen->r) != (ntasks * Height)){
  567. fprint(2, "resize: x: have %d, need %d; y: have %d, need %d\n",
  568. Dx(screen->r), Width + 8, Dy(screen->r), (ntasks * Height) + 8);
  569. fprint(wctlfd, "resize -dx %d -dy %d\n",
  570. Width + 8, (ntasks * Height) + 8);
  571. }
  572. }
  573. else{
  574. Width = Dx(screen->r);
  575. Height = ntasks? Dy(screen->r)/ntasks:
  576. Dy(screen->r);
  577. }
  578. break;
  579. case 2:
  580. switch(r){
  581. case 'r':
  582. for(i = 0; i < ntasks; i++){
  583. tasks[i].tstart = now;
  584. tasks[i].total = 0;
  585. tasks[i].runtime = 0;
  586. tasks[i].runmax = 0;
  587. tasks[i].runthis = 0;
  588. tasks[i].runs = 0;
  589. memset(tasks[i].tevents, 0,
  590. Nevent*sizeof(uint32_t));
  591. }
  592. break;
  593. case 'p':
  594. paused ^= 1;
  595. prevts = 0;
  596. break;
  597. case '-':
  598. if (scaleno < nelem(scales) - 1)
  599. scaleno++;
  600. prevts = 0;
  601. break;
  602. case '+':
  603. if (scaleno > 0)
  604. scaleno--;
  605. prevts = 0;
  606. break;
  607. case 'q':
  608. threadexitsall(nil);
  609. case 'v':
  610. verbose ^= 1;
  611. default:
  612. break;
  613. }
  614. break;
  615. case 3:
  616. now = nsec();
  617. while((n = read(logfd, eventbuf, Nevents*sizeof(Traceevent))) > 0){
  618. assert((n % sizeof(Traceevent)) == 0);
  619. nevents = n / sizeof(Traceevent);
  620. for (ep = eventbuf; ep < eventbuf + nevents; ep++){
  621. if ((ep->etype & 0xffff) >= Nevent){
  622. print("%ld %t Illegal event %ld\n",
  623. ep->pid, ep->time, ep->etype & 0xffff);
  624. continue;
  625. }
  626. if (verbose)
  627. print("%ld %t %s\n",
  628. ep->pid, ep->time, schedstatename[ep->etype & 0xffff]);
  629. for(i = 0; i < ntasks; i++)
  630. if(tasks[i].pid == ep->pid)
  631. break;
  632. if(i == ntasks){
  633. t = newtask(ep->pid);
  634. t->tstart = ep->time;
  635. }else
  636. t = &tasks[i];
  637. doevent(t, ep);
  638. }
  639. }
  640. if(!paused)
  641. redraw(scaleno);
  642. }
  643. sleep(scales[scaleno].sleep);
  644. }
  645. }
  646. int
  647. timeconv(Fmt *f)
  648. {
  649. char buf[128], *sign;
  650. int64_t t;
  651. buf[0] = 0;
  652. switch(f->r) {
  653. case 'U':
  654. t = va_arg(f->args, int64_t);
  655. break;
  656. case 't': // int64_t in nanoseconds
  657. t = va_arg(f->args, int64_t);
  658. break;
  659. default:
  660. return fmtstrcpy(f, "(timeconv)");
  661. }
  662. if (t < 0) {
  663. sign = "-";
  664. t = -t;
  665. }else
  666. sign = "";
  667. if (t > S(1)){
  668. t += OneRound;
  669. sprint(buf, "%s%d.%.3ds", sign, (int)(t / S(1)), (int)(t % S(1))/1000000);
  670. }else if (t > MS(1)){
  671. t += MilliRound;
  672. sprint(buf, "%s%d.%.3dms", sign, (int)(t / MS(1)), (int)(t % MS(1))/1000);
  673. }else if (t > US(1))
  674. sprint(buf, "%s%d.%.3dµs", sign, (int)(t / US(1)), (int)(t % US(1)));
  675. else
  676. sprint(buf, "%s%dns", sign, (int)t);
  677. return fmtstrcpy(f, buf);
  678. }