trace.c 18 KB

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