trace.c 18 KB

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