stats.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <ctype.h>
  4. #include <auth.h>
  5. #include <fcall.h>
  6. #include <draw.h>
  7. #include <event.h>
  8. #define MAXNUM 10 /* maximum number of numbers on data line */
  9. typedef struct Graph Graph;
  10. typedef struct Machine Machine;
  11. struct Graph
  12. {
  13. int colindex;
  14. Rectangle r;
  15. int *data;
  16. int ndata;
  17. char *label;
  18. void (*newvalue)(Machine*, uvlong*, uvlong*, int);
  19. void (*update)(Graph*, uvlong, uvlong);
  20. Machine *mach;
  21. int overflow;
  22. Image *overtmp;
  23. };
  24. enum
  25. {
  26. /* old /dev/swap */
  27. Mem = 0,
  28. Maxmem,
  29. Swap,
  30. Maxswap,
  31. /* /dev/sysstats */
  32. Procno = 0,
  33. Context,
  34. Interrupt,
  35. Syscall,
  36. Fault,
  37. TLBfault,
  38. TLBpurge,
  39. Load,
  40. Idle,
  41. InIntr,
  42. /* /net/ether0/stats */
  43. In = 0,
  44. Link,
  45. Out,
  46. Err0,
  47. };
  48. struct Machine
  49. {
  50. char *name;
  51. char *shortname;
  52. int remote;
  53. int statsfd;
  54. int swapfd;
  55. int etherfd;
  56. int ifstatsfd;
  57. int batteryfd;
  58. int bitsybatfd;
  59. int tempfd;
  60. int disable;
  61. uvlong devswap[4];
  62. uvlong devsysstat[10];
  63. uvlong prevsysstat[10];
  64. int nproc;
  65. int lgproc;
  66. uvlong netetherstats[8];
  67. uvlong prevetherstats[8];
  68. uvlong batterystats[2];
  69. uvlong netetherifstats[2];
  70. uvlong temp[10];
  71. /* big enough to hold /dev/sysstat even with many processors */
  72. char buf[8*1024];
  73. char *bufp;
  74. char *ebufp;
  75. };
  76. enum
  77. {
  78. Mainproc,
  79. Mouseproc,
  80. NPROC,
  81. };
  82. enum
  83. {
  84. Ncolor = 6,
  85. Ysqueeze = 2, /* vertical squeezing of label text */
  86. Labspace = 2, /* room around label */
  87. Dot = 2, /* height of dot */
  88. Opwid = 5, /* strlen("add ") or strlen("drop ") */
  89. Nlab = 3, /* max number of labels on y axis */
  90. Lablen = 16, /* max length of label */
  91. Lx = 4, /* label tick length */
  92. };
  93. enum Menu2
  94. {
  95. Mbattery,
  96. Mcontext,
  97. Mether,
  98. Methererr,
  99. Metherin,
  100. Metherout,
  101. Mfault,
  102. Midle,
  103. Minintr,
  104. Mintr,
  105. Mload,
  106. Mmem,
  107. Mswap,
  108. Msyscall,
  109. Mtlbmiss,
  110. Mtlbpurge,
  111. Msignal,
  112. Mtemp,
  113. Nmenu2,
  114. };
  115. char *menu2str[Nmenu2+1] = {
  116. "add battery ",
  117. "add context ",
  118. "add ether ",
  119. "add ethererr",
  120. "add etherin ",
  121. "add etherout",
  122. "add fault ",
  123. "add idle ",
  124. "add inintr ",
  125. "add intr ",
  126. "add load ",
  127. "add mem ",
  128. "add swap ",
  129. "add syscall ",
  130. "add tlbmiss ",
  131. "add tlbpurge",
  132. "add 802.11b ",
  133. "add temp ",
  134. nil,
  135. };
  136. void contextval(Machine*, uvlong*, uvlong*, int),
  137. etherval(Machine*, uvlong*, uvlong*, int),
  138. ethererrval(Machine*, uvlong*, uvlong*, int),
  139. etherinval(Machine*, uvlong*, uvlong*, int),
  140. etheroutval(Machine*, uvlong*, uvlong*, int),
  141. faultval(Machine*, uvlong*, uvlong*, int),
  142. intrval(Machine*, uvlong*, uvlong*, int),
  143. inintrval(Machine*, uvlong*, uvlong*, int),
  144. loadval(Machine*, uvlong*, uvlong*, int),
  145. idleval(Machine*, uvlong*, uvlong*, int),
  146. memval(Machine*, uvlong*, uvlong*, int),
  147. swapval(Machine*, uvlong*, uvlong*, int),
  148. syscallval(Machine*, uvlong*, uvlong*, int),
  149. tlbmissval(Machine*, uvlong*, uvlong*, int),
  150. tlbpurgeval(Machine*, uvlong*, uvlong*, int),
  151. batteryval(Machine*, uvlong*, uvlong*, int),
  152. signalval(Machine*, uvlong*, uvlong*, int),
  153. tempval(Machine*, uvlong*, uvlong*, int);
  154. Menu menu2 = {menu2str, nil};
  155. int present[Nmenu2];
  156. void (*newvaluefn[Nmenu2])(Machine*, uvlong*, uvlong*, int init) = {
  157. batteryval,
  158. contextval,
  159. etherval,
  160. ethererrval,
  161. etherinval,
  162. etheroutval,
  163. faultval,
  164. idleval,
  165. inintrval,
  166. intrval,
  167. loadval,
  168. memval,
  169. swapval,
  170. syscallval,
  171. tlbmissval,
  172. tlbpurgeval,
  173. signalval,
  174. tempval,
  175. };
  176. Image *cols[Ncolor][3];
  177. Graph *graph;
  178. Machine *mach;
  179. Font *mediumfont;
  180. char *mysysname;
  181. char argchars[] = "8bceEfiImlnpstwz";
  182. int pids[NPROC];
  183. int parity; /* toggled to avoid patterns in textured background */
  184. int nmach;
  185. int ngraph; /* totaly number is ngraph*nmach */
  186. double scale = 1.0;
  187. int logscale = 0;
  188. int ylabels = 0;
  189. int oldsystem = 0;
  190. int sleeptime = 1000;
  191. char *procnames[NPROC] = {"main", "mouse"};
  192. void
  193. killall(char *s)
  194. {
  195. int i, pid;
  196. pid = getpid();
  197. for(i=0; i<NPROC; i++)
  198. if(pids[i] && pids[i]!=pid)
  199. postnote(PNPROC, pids[i], "kill");
  200. exits(s);
  201. }
  202. void*
  203. emalloc(ulong sz)
  204. {
  205. void *v;
  206. v = malloc(sz);
  207. if(v == nil) {
  208. fprint(2, "stats: out of memory allocating %ld: %r\n", sz);
  209. killall("mem");
  210. }
  211. memset(v, 0, sz);
  212. return v;
  213. }
  214. void*
  215. erealloc(void *v, ulong sz)
  216. {
  217. v = realloc(v, sz);
  218. if(v == nil) {
  219. fprint(2, "stats: out of memory reallocating %ld: %r\n", sz);
  220. killall("mem");
  221. }
  222. return v;
  223. }
  224. char*
  225. estrdup(char *s)
  226. {
  227. char *t;
  228. if((t = strdup(s)) == nil) {
  229. fprint(2, "stats: out of memory in strdup(%.10s): %r\n", s);
  230. killall("mem");
  231. }
  232. return t;
  233. }
  234. void
  235. mkcol(int i, int c0, int c1, int c2)
  236. {
  237. cols[i][0] = allocimagemix(display, c0, DWhite);
  238. cols[i][1] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, c1);
  239. cols[i][2] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, c2);
  240. }
  241. void
  242. colinit(void)
  243. {
  244. mediumfont = openfont(display, "/lib/font/bit/pelm/latin1.8.font");
  245. if(mediumfont == nil)
  246. mediumfont = font;
  247. /* Peach */
  248. mkcol(0, 0xFFAAAAFF, 0xFFAAAAFF, 0xBB5D5DFF);
  249. /* Aqua */
  250. mkcol(1, DPalebluegreen, DPalegreygreen, DPurpleblue);
  251. /* Yellow */
  252. mkcol(2, DPaleyellow, DDarkyellow, DYellowgreen);
  253. /* Green */
  254. mkcol(3, DPalegreen, DMedgreen, DDarkgreen);
  255. /* Blue */
  256. mkcol(4, 0x00AAFFFF, 0x00AAFFFF, 0x0088CCFF);
  257. /* Grey */
  258. cols[5][0] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, 0xEEEEEEFF);
  259. cols[5][1] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, 0xCCCCCCFF);
  260. cols[5][2] = allocimage(display, Rect(0,0,1,1), CMAP8, 1, 0x888888FF);
  261. }
  262. int
  263. loadbuf(Machine *m, int *fd)
  264. {
  265. int n;
  266. if(*fd < 0)
  267. return 0;
  268. seek(*fd, 0, 0);
  269. n = read(*fd, m->buf, sizeof m->buf-1);
  270. if(n <= 0){
  271. close(*fd);
  272. *fd = -1;
  273. return 0;
  274. }
  275. m->bufp = m->buf;
  276. m->ebufp = m->buf+n;
  277. m->buf[n] = 0;
  278. return 1;
  279. }
  280. void
  281. label(Point p, int dy, char *text)
  282. {
  283. char *s;
  284. Rune r[2];
  285. int w, maxw, maxy;
  286. p.x += Labspace;
  287. maxy = p.y+dy;
  288. maxw = 0;
  289. r[1] = '\0';
  290. for(s=text; *s; ){
  291. if(p.y+mediumfont->height-Ysqueeze > maxy)
  292. break;
  293. w = chartorune(r, s);
  294. s += w;
  295. w = runestringwidth(mediumfont, r);
  296. if(w > maxw)
  297. maxw = w;
  298. runestring(screen, p, display->black, ZP, mediumfont, r);
  299. p.y += mediumfont->height-Ysqueeze;
  300. }
  301. }
  302. Point
  303. paritypt(int x)
  304. {
  305. return Pt(x+parity, 0);
  306. }
  307. Point
  308. datapoint(Graph *g, int x, uvlong v, uvlong vmax)
  309. {
  310. Point p;
  311. double y;
  312. p.x = x;
  313. y = ((double)v)/(vmax*scale);
  314. if(logscale){
  315. /*
  316. * Arrange scale to cover a factor of 1000.
  317. * vmax corresponds to the 100 mark.
  318. * 10*vmax is the top of the scale.
  319. */
  320. if(y <= 0.)
  321. y = 0;
  322. else{
  323. y = log10(y);
  324. /* 1 now corresponds to the top; -2 to the bottom; rescale */
  325. y = (y+2.)/3.;
  326. }
  327. }
  328. p.y = g->r.max.y - Dy(g->r)*y - Dot;
  329. if(p.y < g->r.min.y)
  330. p.y = g->r.min.y;
  331. if(p.y > g->r.max.y-Dot)
  332. p.y = g->r.max.y-Dot;
  333. return p;
  334. }
  335. void
  336. drawdatum(Graph *g, int x, uvlong prev, uvlong v, uvlong vmax)
  337. {
  338. int c;
  339. Point p, q;
  340. c = g->colindex;
  341. p = datapoint(g, x, v, vmax);
  342. q = datapoint(g, x, prev, vmax);
  343. if(p.y < q.y){
  344. draw(screen, Rect(p.x, g->r.min.y, p.x+1, p.y), cols[c][0], nil, paritypt(p.x));
  345. draw(screen, Rect(p.x, p.y, p.x+1, q.y+Dot), cols[c][2], nil, ZP);
  346. draw(screen, Rect(p.x, q.y+Dot, p.x+1, g->r.max.y), cols[c][1], nil, ZP);
  347. }else{
  348. draw(screen, Rect(p.x, g->r.min.y, p.x+1, q.y), cols[c][0], nil, paritypt(p.x));
  349. draw(screen, Rect(p.x, q.y, p.x+1, p.y+Dot), cols[c][2], nil, ZP);
  350. draw(screen, Rect(p.x, p.y+Dot, p.x+1, g->r.max.y), cols[c][1], nil, ZP);
  351. }
  352. }
  353. void
  354. redraw(Graph *g, uvlong vmax)
  355. {
  356. int i, c;
  357. c = g->colindex;
  358. draw(screen, g->r, cols[c][0], nil, paritypt(g->r.min.x));
  359. for(i=1; i<Dx(g->r); i++)
  360. drawdatum(g, g->r.max.x-i, g->data[i-1], g->data[i], vmax);
  361. drawdatum(g, g->r.min.x, g->data[i], g->data[i], vmax);
  362. g->overflow = 0;
  363. }
  364. void
  365. update1(Graph *g, uvlong v, uvlong vmax)
  366. {
  367. char buf[48];
  368. int overflow;
  369. if(g->overflow && g->overtmp!=nil)
  370. draw(screen, g->overtmp->r, g->overtmp, nil, g->overtmp->r.min);
  371. draw(screen, g->r, screen, nil, Pt(g->r.min.x+1, g->r.min.y));
  372. drawdatum(g, g->r.max.x-1, g->data[0], v, vmax);
  373. memmove(g->data+1, g->data, (g->ndata-1)*sizeof(g->data[0]));
  374. g->data[0] = v;
  375. g->overflow = 0;
  376. if(logscale)
  377. overflow = (v>10*vmax*scale);
  378. else
  379. overflow = (v>vmax*scale);
  380. if(overflow && g->overtmp!=nil){
  381. g->overflow = 1;
  382. draw(g->overtmp, g->overtmp->r, screen, nil, g->overtmp->r.min);
  383. sprint(buf, "%llud", v);
  384. string(screen, g->overtmp->r.min, display->black, ZP, mediumfont, buf);
  385. }
  386. }
  387. /* read one line of text from buffer and process integers */
  388. int
  389. readnums(Machine *m, int n, uvlong *a, int spanlines)
  390. {
  391. int i;
  392. char *p, *ep;
  393. if(spanlines)
  394. ep = m->ebufp;
  395. else
  396. for(ep=m->bufp; ep<m->ebufp; ep++)
  397. if(*ep == '\n')
  398. break;
  399. p = m->bufp;
  400. for(i=0; i<n && p<ep; i++){
  401. while(p<ep && (!isascii(*p) || !isdigit(*p)) && *p!='-')
  402. p++;
  403. if(p == ep)
  404. break;
  405. a[i] = strtoull(p, &p, 10);
  406. }
  407. if(ep < m->ebufp)
  408. ep++;
  409. m->bufp = ep;
  410. return i == n;
  411. }
  412. /* Network on fd1, mount driver on fd0 */
  413. static int
  414. filter(int fd)
  415. {
  416. int p[2];
  417. if(pipe(p) < 0){
  418. fprint(2, "stats: can't pipe: %r\n");
  419. killall("pipe");
  420. }
  421. switch(rfork(RFNOWAIT|RFPROC|RFFDG)) {
  422. case -1:
  423. sysfatal("rfork record module");
  424. case 0:
  425. dup(fd, 1);
  426. close(fd);
  427. dup(p[0], 0);
  428. close(p[0]);
  429. close(p[1]);
  430. execl("/bin/aux/fcall", "fcall", nil);
  431. fprint(2, "stats: can't exec fcall: %r\n");
  432. killall("fcall");
  433. default:
  434. close(fd);
  435. close(p[0]);
  436. }
  437. return p[1];
  438. }
  439. /*
  440. * 9fs
  441. */
  442. int
  443. connect9fs(char *addr)
  444. {
  445. char dir[256], *na;
  446. int fd;
  447. fprint(2, "connect9fs...");
  448. na = netmkaddr(addr, 0, "9fs");
  449. fprint(2, "dial %s...", na);
  450. if((fd = dial(na, 0, dir, 0)) < 0)
  451. return -1;
  452. fprint(2, "dir %s...", dir);
  453. // if(strstr(dir, "tcp"))
  454. // fd = filter(fd);
  455. return fd;
  456. }
  457. int
  458. old9p(int fd)
  459. {
  460. int p[2];
  461. if(pipe(p) < 0)
  462. return -1;
  463. switch(rfork(RFPROC|RFFDG|RFNAMEG)) {
  464. case -1:
  465. return -1;
  466. case 0:
  467. if(fd != 1){
  468. dup(fd, 1);
  469. close(fd);
  470. }
  471. if(p[0] != 0){
  472. dup(p[0], 0);
  473. close(p[0]);
  474. }
  475. close(p[1]);
  476. if(0){
  477. fd = open("/sys/log/cpu", OWRITE);
  478. if(fd != 2){
  479. dup(fd, 2);
  480. close(fd);
  481. }
  482. execl("/bin/srvold9p", "srvold9p", "-ds", nil);
  483. } else
  484. execl("/bin/srvold9p", "srvold9p", "-s", nil);
  485. return -1;
  486. default:
  487. close(fd);
  488. close(p[0]);
  489. }
  490. return p[1];
  491. }
  492. /*
  493. * exportfs
  494. */
  495. int
  496. connectexportfs(char *addr)
  497. {
  498. char buf[ERRMAX], dir[256], *na;
  499. int fd, n;
  500. char *tree;
  501. AuthInfo *ai;
  502. tree = "/";
  503. na = netmkaddr(addr, 0, "exportfs");
  504. if((fd = dial(na, 0, dir, 0)) < 0)
  505. return -1;
  506. ai = auth_proxy(fd, auth_getkey, "proto=p9any role=client");
  507. if(ai == nil)
  508. return -1;
  509. n = write(fd, tree, strlen(tree));
  510. if(n < 0){
  511. close(fd);
  512. return -1;
  513. }
  514. strcpy(buf, "can't read tree");
  515. n = read(fd, buf, sizeof buf - 1);
  516. if(n!=2 || buf[0]!='O' || buf[1]!='K'){
  517. buf[sizeof buf - 1] = '\0';
  518. werrstr("bad remote tree: %s\n", buf);
  519. close(fd);
  520. return -1;
  521. }
  522. // if(strstr(dir, "tcp"))
  523. // fd = filter(fd);
  524. if(oldsystem)
  525. return old9p(fd);
  526. return fd;
  527. }
  528. int
  529. readswap(Machine *m, uvlong *a)
  530. {
  531. if(strstr(m->buf, "memory\n")){
  532. /* new /dev/swap - skip first 3 numbers */
  533. if(!readnums(m, 7, a, 1))
  534. return 0;
  535. a[0] = a[3];
  536. a[1] = a[4];
  537. a[2] = a[5];
  538. a[3] = a[6];
  539. return 1;
  540. }
  541. return readnums(m, nelem(m->devswap), a, 0);
  542. }
  543. char*
  544. shortname(char *s)
  545. {
  546. char *p, *e;
  547. p = estrdup(s);
  548. e = strchr(p, '.');
  549. if(e)
  550. *e = 0;
  551. return p;
  552. }
  553. int
  554. ilog10(uvlong j)
  555. {
  556. int i;
  557. for(i = 0; j >= 10; i++)
  558. j /= 10;
  559. return i;
  560. }
  561. int
  562. initmach(Machine *m, char *name)
  563. {
  564. int n, fd;
  565. uvlong a[MAXNUM];
  566. char *p, mpt[256], buf[256];
  567. p = strchr(name, '!');
  568. if(p)
  569. p++;
  570. else
  571. p = name;
  572. m->name = estrdup(p);
  573. m->shortname = shortname(p);
  574. m->remote = (strcmp(p, mysysname) != 0);
  575. if(m->remote == 0)
  576. strcpy(mpt, "");
  577. else{
  578. snprint(mpt, sizeof mpt, "/n/%s", p);
  579. fd = connectexportfs(name);
  580. if(fd < 0){
  581. fprint(2, "can't connect to %s: %r\n", name);
  582. return 0;
  583. }
  584. /* BUG? need to use amount() now? */
  585. if(mount(fd, -1, mpt, MREPL, "") < 0){
  586. fprint(2, "stats: mount %s on %s failed (%r); trying /n/sid\n", name, mpt);
  587. strcpy(mpt, "/n/sid");
  588. if(mount(fd, -1, mpt, MREPL, "") < 0){
  589. fprint(2, "stats: mount %s on %s failed: %r\n", name, mpt);
  590. return 0;
  591. }
  592. }
  593. }
  594. snprint(buf, sizeof buf, "%s/dev/swap", mpt);
  595. m->swapfd = open(buf, OREAD);
  596. if(loadbuf(m, &m->swapfd) && readswap(m, a))
  597. memmove(m->devswap, a, sizeof m->devswap);
  598. else{
  599. m->devswap[Maxswap] = 100;
  600. m->devswap[Maxmem] = 100;
  601. }
  602. snprint(buf, sizeof buf, "%s/dev/sysstat", mpt);
  603. m->statsfd = open(buf, OREAD);
  604. if(loadbuf(m, &m->statsfd)){
  605. for(n=0; readnums(m, nelem(m->devsysstat), a, 0); n++)
  606. ;
  607. m->nproc = n;
  608. }else
  609. m->nproc = 1;
  610. m->lgproc = ilog10(m->nproc);
  611. snprint(buf, sizeof buf, "%s/net/ether0/stats", mpt);
  612. m->etherfd = open(buf, OREAD);
  613. if(loadbuf(m, &m->etherfd) && readnums(m, nelem(m->netetherstats), a, 1))
  614. memmove(m->netetherstats, a, sizeof m->netetherstats);
  615. snprint(buf, sizeof buf, "%s/net/ether0/ifstats", mpt);
  616. m->ifstatsfd = open(buf, OREAD);
  617. if(loadbuf(m, &m->ifstatsfd)){
  618. /* need to check that this is a wavelan interface */
  619. if(strncmp(m->buf, "Signal: ", 8) == 0 && readnums(m, nelem(m->netetherifstats), a, 1))
  620. memmove(m->netetherifstats, a, sizeof m->netetherifstats);
  621. }
  622. snprint(buf, sizeof buf, "%s/mnt/apm/battery", mpt);
  623. m->batteryfd = open(buf, OREAD);
  624. m->bitsybatfd = -1;
  625. if(m->batteryfd >= 0){
  626. if(loadbuf(m, &m->batteryfd) && readnums(m, nelem(m->batterystats), a, 0))
  627. memmove(m->batterystats, a, sizeof(m->batterystats));
  628. }else{
  629. snprint(buf, sizeof buf, "%s/dev/battery", mpt);
  630. m->bitsybatfd = open(buf, OREAD);
  631. if(loadbuf(m, &m->bitsybatfd) && readnums(m, 1, a, 0))
  632. memmove(m->batterystats, a, sizeof(m->batterystats));
  633. }
  634. snprint(buf, sizeof buf, "%s/dev/cputemp", mpt);
  635. m->tempfd = open(buf, OREAD);
  636. if(loadbuf(m, &m->tempfd))
  637. for(n=0; n < nelem(m->temp) && readnums(m, 2, a, 0); n++)
  638. m->temp[n] = a[0];
  639. return 1;
  640. }
  641. jmp_buf catchalarm;
  642. void
  643. alarmed(void *a, char *s)
  644. {
  645. if(strcmp(s, "alarm") == 0)
  646. notejmp(a, catchalarm, 1);
  647. noted(NDFLT);
  648. }
  649. int
  650. needswap(int init)
  651. {
  652. return init | present[Mmem] | present[Mswap];
  653. }
  654. int
  655. needstat(int init)
  656. {
  657. return init | present[Mcontext] | present[Mfault] | present[Mintr] | present[Mload] | present[Midle] |
  658. present[Minintr] | present[Msyscall] | present[Mtlbmiss] | present[Mtlbpurge];
  659. }
  660. int
  661. needether(int init)
  662. {
  663. return init | present[Mether] | present[Metherin] | present[Metherout] | present[Methererr];
  664. }
  665. int
  666. needbattery(int init)
  667. {
  668. return init | present[Mbattery];
  669. }
  670. int
  671. needsignal(int init)
  672. {
  673. return init | present[Msignal];
  674. }
  675. int
  676. needtemp(int init)
  677. {
  678. return init | present[Mtemp];
  679. }
  680. void
  681. readmach(Machine *m, int init)
  682. {
  683. int n, i;
  684. uvlong a[nelem(m->devsysstat)];
  685. char buf[32];
  686. if(m->remote && (m->disable || setjmp(catchalarm))){
  687. if (m->disable++ >= 5)
  688. m->disable = 0; /* give it another chance */
  689. memmove(m->devsysstat, m->prevsysstat, sizeof m->devsysstat);
  690. memmove(m->netetherstats, m->prevetherstats, sizeof m->netetherstats);
  691. return;
  692. }
  693. snprint(buf, sizeof buf, "%s", m->name);
  694. if (strcmp(m->name, buf) != 0){
  695. free(m->name);
  696. m->name = estrdup(buf);
  697. free(m->shortname);
  698. m->shortname = shortname(buf);
  699. if(display != nil) /* else we're still initializing */
  700. eresized(0);
  701. }
  702. if(m->remote){
  703. notify(alarmed);
  704. alarm(5000);
  705. }
  706. if(needswap(init) && loadbuf(m, &m->swapfd) && readswap(m, a))
  707. memmove(m->devswap, a, sizeof m->devswap);
  708. if(needstat(init) && loadbuf(m, &m->statsfd)){
  709. memmove(m->prevsysstat, m->devsysstat, sizeof m->devsysstat);
  710. memset(m->devsysstat, 0, sizeof m->devsysstat);
  711. for(n=0; n<m->nproc && readnums(m, nelem(m->devsysstat), a, 0); n++)
  712. for(i=0; i<nelem(m->devsysstat); i++)
  713. m->devsysstat[i] += a[i];
  714. }
  715. if(needether(init) && loadbuf(m, &m->etherfd) && readnums(m, nelem(m->netetherstats), a, 1)){
  716. memmove(m->prevetherstats, m->netetherstats, sizeof m->netetherstats);
  717. memmove(m->netetherstats, a, sizeof m->netetherstats);
  718. }
  719. if(needsignal(init) && loadbuf(m, &m->ifstatsfd) && strncmp(m->buf, "Signal: ", 8)==0 && readnums(m, nelem(m->netetherifstats), a, 1)){
  720. memmove(m->netetherifstats, a, sizeof m->netetherifstats);
  721. }
  722. if(needbattery(init) && loadbuf(m, &m->batteryfd) && readnums(m, nelem(m->batterystats), a, 0))
  723. memmove(m->batterystats, a, sizeof(m->batterystats));
  724. if(needbattery(init) && loadbuf(m, &m->bitsybatfd) && readnums(m, 1, a, 0))
  725. memmove(m->batterystats, a, sizeof(m->batterystats));
  726. if(needtemp(init) && loadbuf(m, &m->tempfd))
  727. for(n=0; n < nelem(m->temp) && readnums(m, 2, a, 0); n++)
  728. m->temp[n] = a[0];
  729. if(m->remote){
  730. alarm(0);
  731. notify(nil);
  732. }
  733. }
  734. void
  735. memval(Machine *m, uvlong *v, uvlong *vmax, int)
  736. {
  737. *v = m->devswap[Mem];
  738. *vmax = m->devswap[Maxmem];
  739. }
  740. void
  741. swapval(Machine *m, uvlong *v, uvlong *vmax, int)
  742. {
  743. *v = m->devswap[Swap];
  744. *vmax = m->devswap[Maxswap];
  745. }
  746. void
  747. contextval(Machine *m, uvlong *v, uvlong *vmax, int init)
  748. {
  749. *v = m->devsysstat[Context]-m->prevsysstat[Context];
  750. *vmax = sleeptime*m->nproc;
  751. if(init)
  752. *vmax = sleeptime;
  753. }
  754. /*
  755. * bug: need to factor in HZ
  756. */
  757. void
  758. intrval(Machine *m, uvlong *v, uvlong *vmax, int init)
  759. {
  760. *v = m->devsysstat[Interrupt]-m->prevsysstat[Interrupt];
  761. *vmax = sleeptime*m->nproc*10;
  762. if(init)
  763. *vmax = sleeptime*10;
  764. }
  765. void
  766. syscallval(Machine *m, uvlong *v, uvlong *vmax, int init)
  767. {
  768. *v = m->devsysstat[Syscall]-m->prevsysstat[Syscall];
  769. *vmax = sleeptime*m->nproc;
  770. if(init)
  771. *vmax = sleeptime;
  772. }
  773. void
  774. faultval(Machine *m, uvlong *v, uvlong *vmax, int init)
  775. {
  776. *v = m->devsysstat[Fault]-m->prevsysstat[Fault];
  777. *vmax = sleeptime*m->nproc;
  778. if(init)
  779. *vmax = sleeptime;
  780. }
  781. void
  782. tlbmissval(Machine *m, uvlong *v, uvlong *vmax, int init)
  783. {
  784. *v = m->devsysstat[TLBfault]-m->prevsysstat[TLBfault];
  785. *vmax = (sleeptime/1000)*10*m->nproc;
  786. if(init)
  787. *vmax = (sleeptime/1000)*10;
  788. }
  789. void
  790. tlbpurgeval(Machine *m, uvlong *v, uvlong *vmax, int init)
  791. {
  792. *v = m->devsysstat[TLBpurge]-m->prevsysstat[TLBpurge];
  793. *vmax = (sleeptime/1000)*10*m->nproc;
  794. if(init)
  795. *vmax = (sleeptime/1000)*10;
  796. }
  797. void
  798. loadval(Machine *m, uvlong *v, uvlong *vmax, int init)
  799. {
  800. *v = m->devsysstat[Load];
  801. *vmax = 1000*m->nproc;
  802. if(init)
  803. *vmax = 1000;
  804. }
  805. void
  806. idleval(Machine *m, uvlong *v, uvlong *vmax, int)
  807. {
  808. *v = m->devsysstat[Idle]/m->nproc;
  809. *vmax = 100;
  810. }
  811. void
  812. inintrval(Machine *m, uvlong *v, uvlong *vmax, int)
  813. {
  814. *v = m->devsysstat[InIntr]/m->nproc;
  815. *vmax = 100;
  816. }
  817. void
  818. etherval(Machine *m, uvlong *v, uvlong *vmax, int init)
  819. {
  820. *v = m->netetherstats[In]-m->prevetherstats[In] + m->netetherstats[Out]-m->prevetherstats[Out];
  821. *vmax = sleeptime*m->nproc;
  822. if(init)
  823. *vmax = sleeptime;
  824. }
  825. void
  826. etherinval(Machine *m, uvlong *v, uvlong *vmax, int init)
  827. {
  828. *v = m->netetherstats[In]-m->prevetherstats[In];
  829. *vmax = sleeptime*m->nproc;
  830. if(init)
  831. *vmax = sleeptime;
  832. }
  833. void
  834. etheroutval(Machine *m, uvlong *v, uvlong *vmax, int init)
  835. {
  836. *v = m->netetherstats[Out]-m->prevetherstats[Out];
  837. *vmax = sleeptime*m->nproc;
  838. if(init)
  839. *vmax = sleeptime;
  840. }
  841. void
  842. ethererrval(Machine *m, uvlong *v, uvlong *vmax, int init)
  843. {
  844. int i;
  845. *v = 0;
  846. for(i=Err0; i<nelem(m->netetherstats); i++)
  847. *v += m->netetherstats[i];
  848. *vmax = (sleeptime/1000)*10*m->nproc;
  849. if(init)
  850. *vmax = (sleeptime/1000)*10;
  851. }
  852. void
  853. batteryval(Machine *m, uvlong *v, uvlong *vmax, int)
  854. {
  855. *v = m->batterystats[0];
  856. if(m->bitsybatfd >= 0)
  857. *vmax = 184; // at least on my bitsy...
  858. else
  859. *vmax = 100;
  860. }
  861. void
  862. signalval(Machine *m, uvlong *v, uvlong *vmax, int)
  863. {
  864. ulong l;
  865. *vmax = sleeptime;
  866. l = m->netetherifstats[0];
  867. /*
  868. * Range is seen to be from about -45 (strong) to -95 (weak); rescale
  869. */
  870. if(l == 0){ /* probably not present */
  871. *v = 0;
  872. return;
  873. }
  874. *v = 20*(l+95);
  875. }
  876. void
  877. tempval(Machine *m, uvlong *v, uvlong *vmax, int)
  878. {
  879. ulong l;
  880. *vmax = sleeptime;
  881. l = m->temp[0];
  882. if(l == ~0 || l == 0)
  883. *v = 0;
  884. else
  885. *v = (l-20)*27;
  886. }
  887. void
  888. usage(void)
  889. {
  890. fprint(2, "usage: stats [-O] [-S scale] [-LY] [-%s] [machine...]\n", argchars);
  891. exits("usage");
  892. }
  893. void
  894. addgraph(int n)
  895. {
  896. Graph *g, *ograph;
  897. int i, j;
  898. static int nadd;
  899. if(n > nelem(menu2str))
  900. abort();
  901. /* avoid two adjacent graphs of same color */
  902. if(ngraph>0 && graph[ngraph-1].colindex==nadd%Ncolor)
  903. nadd++;
  904. ograph = graph;
  905. graph = emalloc(nmach*(ngraph+1)*sizeof(Graph));
  906. for(i=0; i<nmach; i++)
  907. for(j=0; j<ngraph; j++)
  908. graph[i*(ngraph+1)+j] = ograph[i*ngraph+j];
  909. free(ograph);
  910. ngraph++;
  911. for(i=0; i<nmach; i++){
  912. g = &graph[i*ngraph+(ngraph-1)];
  913. memset(g, 0, sizeof(Graph));
  914. g->label = menu2str[n]+Opwid;
  915. g->newvalue = newvaluefn[n];
  916. g->update = update1; /* no other update functions yet */
  917. g->mach = &mach[i];
  918. g->colindex = nadd%Ncolor;
  919. }
  920. present[n] = 1;
  921. nadd++;
  922. }
  923. void
  924. dropgraph(int which)
  925. {
  926. Graph *ograph;
  927. int i, j, n;
  928. if(which > nelem(menu2str))
  929. abort();
  930. /* convert n to index in graph table */
  931. n = -1;
  932. for(i=0; i<ngraph; i++)
  933. if(strcmp(menu2str[which]+Opwid, graph[i].label) == 0){
  934. n = i;
  935. break;
  936. }
  937. if(n < 0){
  938. fprint(2, "stats: internal error can't drop graph\n");
  939. killall("error");
  940. }
  941. ograph = graph;
  942. graph = emalloc(nmach*(ngraph-1)*sizeof(Graph));
  943. for(i=0; i<nmach; i++){
  944. for(j=0; j<n; j++)
  945. graph[i*(ngraph-1)+j] = ograph[i*ngraph+j];
  946. free(ograph[i*ngraph+j].data);
  947. freeimage(ograph[i*ngraph+j].overtmp);
  948. for(j++; j<ngraph; j++)
  949. graph[i*(ngraph-1)+j-1] = ograph[i*ngraph+j];
  950. }
  951. free(ograph);
  952. ngraph--;
  953. present[which] = 0;
  954. }
  955. int
  956. addmachine(char *name)
  957. {
  958. if(ngraph > 0){
  959. fprint(2, "stats: internal error: ngraph>0 in addmachine()\n");
  960. usage();
  961. }
  962. if(mach == nil)
  963. nmach = 0; /* a little dance to get us started with local machine by default */
  964. mach = erealloc(mach, (nmach+1)*sizeof(Machine));
  965. memset(mach+nmach, 0, sizeof(Machine));
  966. if (initmach(mach+nmach, name)){
  967. nmach++;
  968. return 1;
  969. } else
  970. return 0;
  971. }
  972. void
  973. labelstrs(Graph *g, char strs[Nlab][Lablen], int *np)
  974. {
  975. int j;
  976. uvlong v, vmax;
  977. g->newvalue(g->mach, &v, &vmax, 1);
  978. if(logscale){
  979. for(j=1; j<=2; j++)
  980. sprint(strs[j-1], "%g", scale*pow(10., j)*(double)vmax/100.);
  981. *np = 2;
  982. }else{
  983. for(j=1; j<=3; j++)
  984. sprint(strs[j-1], "%g", scale*(double)j*(double)vmax/4.0);
  985. *np = 3;
  986. }
  987. }
  988. int
  989. labelwidth(void)
  990. {
  991. int i, j, n, w, maxw;
  992. char strs[Nlab][Lablen];
  993. maxw = 0;
  994. for(i=0; i<ngraph; i++){
  995. /* choose value for rightmost graph */
  996. labelstrs(&graph[ngraph*(nmach-1)+i], strs, &n);
  997. for(j=0; j<n; j++){
  998. w = stringwidth(mediumfont, strs[j]);
  999. if(w > maxw)
  1000. maxw = w;
  1001. }
  1002. }
  1003. return maxw;
  1004. }
  1005. void
  1006. resize(void)
  1007. {
  1008. int i, j, k, n, startx, starty, x, y, dx, dy, ly, ondata, maxx, wid, nlab;
  1009. Graph *g;
  1010. Rectangle machr, r;
  1011. uvlong v, vmax;
  1012. char buf[128], labs[Nlab][Lablen];
  1013. draw(screen, screen->r, display->white, nil, ZP);
  1014. /* label left edge */
  1015. x = screen->r.min.x;
  1016. y = screen->r.min.y + Labspace+mediumfont->height+Labspace;
  1017. dy = (screen->r.max.y - y)/ngraph;
  1018. dx = Labspace+stringwidth(mediumfont, "0")+Labspace;
  1019. startx = x+dx+1;
  1020. starty = y;
  1021. for(i=0; i<ngraph; i++,y+=dy){
  1022. draw(screen, Rect(x, y-1, screen->r.max.x, y), display->black, nil, ZP);
  1023. draw(screen, Rect(x, y, x+dx, screen->r.max.y), cols[graph[i].colindex][0], nil, paritypt(x));
  1024. label(Pt(x, y), dy, graph[i].label);
  1025. draw(screen, Rect(x+dx, y, x+dx+1, screen->r.max.y), cols[graph[i].colindex][2], nil, ZP);
  1026. }
  1027. /* label top edge */
  1028. dx = (screen->r.max.x - startx)/nmach;
  1029. for(x=startx, i=0; i<nmach; i++,x+=dx){
  1030. draw(screen, Rect(x-1, starty-1, x, screen->r.max.y), display->black, nil, ZP);
  1031. j = dx/stringwidth(mediumfont, "0");
  1032. n = mach[i].nproc;
  1033. if(n>1 && j>=1+3+mach[i].lgproc){ /* first char of name + (n) */
  1034. j -= 3+mach[i].lgproc;
  1035. if(j <= 0)
  1036. j = 1;
  1037. snprint(buf, sizeof buf, "%.*s(%d)", j, mach[i].shortname, n);
  1038. }else
  1039. snprint(buf, sizeof buf, "%.*s", j, mach[i].shortname);
  1040. string(screen, Pt(x+Labspace, screen->r.min.y + Labspace), display->black, ZP, mediumfont, buf);
  1041. }
  1042. maxx = screen->r.max.x;
  1043. /* label right, if requested */
  1044. if(ylabels && dy>Nlab*(mediumfont->height+1)){
  1045. wid = labelwidth();
  1046. if(wid < (maxx-startx)-30){
  1047. /* else there's not enough room */
  1048. maxx -= 1+Lx+wid;
  1049. draw(screen, Rect(maxx, starty, maxx+1, screen->r.max.y), display->black, nil, ZP);
  1050. y = starty;
  1051. for(j=0; j<ngraph; j++, y+=dy){
  1052. /* choose value for rightmost graph */
  1053. g = &graph[ngraph*(nmach-1)+j];
  1054. labelstrs(g, labs, &nlab);
  1055. r = Rect(maxx+1, y, screen->r.max.x, y+dy-1);
  1056. if(j == ngraph-1)
  1057. r.max.y = screen->r.max.y;
  1058. draw(screen, r, cols[g->colindex][0], nil, paritypt(r.min.x));
  1059. for(k=0; k<nlab; k++){
  1060. ly = y + (dy*(nlab-k)/(nlab+1));
  1061. draw(screen, Rect(maxx+1, ly, maxx+1+Lx, ly+1), display->black, nil, ZP);
  1062. ly -= mediumfont->height/2;
  1063. string(screen, Pt(maxx+1+Lx, ly), display->black, ZP, mediumfont, labs[k]);
  1064. }
  1065. }
  1066. }
  1067. }
  1068. /* create graphs */
  1069. for(i=0; i<nmach; i++){
  1070. machr = Rect(startx+i*dx, starty, maxx, screen->r.max.y);
  1071. if(i < nmach-1)
  1072. machr.max.x = startx+(i+1)*dx - 1;
  1073. y = starty;
  1074. for(j=0; j<ngraph; j++, y+=dy){
  1075. g = &graph[i*ngraph+j];
  1076. /* allocate data */
  1077. ondata = g->ndata;
  1078. g->ndata = Dx(machr)+1; /* may be too many if label will be drawn here; so what? */
  1079. g->data = erealloc(g->data, g->ndata*sizeof(ulong));
  1080. if(g->ndata > ondata)
  1081. memset(g->data+ondata, 0, (g->ndata-ondata)*sizeof(ulong));
  1082. /* set geometry */
  1083. g->r = machr;
  1084. g->r.min.y = y;
  1085. g->r.max.y = y+dy - 1;
  1086. if(j == ngraph-1)
  1087. g->r.max.y = screen->r.max.y;
  1088. draw(screen, g->r, cols[g->colindex][0], nil, paritypt(g->r.min.x));
  1089. g->overflow = 0;
  1090. r = g->r;
  1091. r.max.y = r.min.y+mediumfont->height;
  1092. r.max.x = r.min.x+stringwidth(mediumfont, "999999999999");
  1093. freeimage(g->overtmp);
  1094. g->overtmp = nil;
  1095. if(r.max.x <= g->r.max.x)
  1096. g->overtmp = allocimage(display, r, screen->chan, 0, -1);
  1097. g->newvalue(g->mach, &v, &vmax, 0);
  1098. redraw(g, vmax);
  1099. }
  1100. }
  1101. flushimage(display, 1);
  1102. }
  1103. void
  1104. eresized(int new)
  1105. {
  1106. lockdisplay(display);
  1107. if(new && getwindow(display, Refnone) < 0) {
  1108. fprint(2, "stats: can't reattach to window\n");
  1109. killall("reattach");
  1110. }
  1111. resize();
  1112. unlockdisplay(display);
  1113. }
  1114. void
  1115. mouseproc(void)
  1116. {
  1117. Mouse mouse;
  1118. int i;
  1119. for(;;){
  1120. mouse = emouse();
  1121. if(mouse.buttons == 4){
  1122. lockdisplay(display);
  1123. for(i=0; i<Nmenu2; i++)
  1124. if(present[i])
  1125. memmove(menu2str[i], "drop ", Opwid);
  1126. else
  1127. memmove(menu2str[i], "add ", Opwid);
  1128. i = emenuhit(3, &mouse, &menu2);
  1129. if(i >= 0){
  1130. if(!present[i])
  1131. addgraph(i);
  1132. else if(ngraph > 1)
  1133. dropgraph(i);
  1134. resize();
  1135. }
  1136. unlockdisplay(display);
  1137. }
  1138. }
  1139. }
  1140. void
  1141. startproc(void (*f)(void), int index)
  1142. {
  1143. int pid;
  1144. switch(pid = rfork(RFPROC|RFMEM|RFNOWAIT)){
  1145. case -1:
  1146. fprint(2, "stats: fork failed: %r\n");
  1147. killall("fork failed");
  1148. case 0:
  1149. f();
  1150. fprint(2, "stats: %s process exits\n", procnames[index]);
  1151. if(index >= 0)
  1152. killall("process died");
  1153. exits(nil);
  1154. }
  1155. if(index >= 0)
  1156. pids[index] = pid;
  1157. }
  1158. void
  1159. main(int argc, char *argv[])
  1160. {
  1161. int i, j;
  1162. double secs;
  1163. uvlong v, vmax, nargs;
  1164. char args[100];
  1165. nmach = 1;
  1166. mysysname = getenv("sysname");
  1167. if(mysysname == nil){
  1168. fprint(2, "stats: can't find $sysname: %r\n");
  1169. exits("sysname");
  1170. }
  1171. mysysname = estrdup(mysysname);
  1172. nargs = 0;
  1173. ARGBEGIN{
  1174. case 'T':
  1175. secs = atof(EARGF(usage()));
  1176. if(secs > 0)
  1177. sleeptime = 1000*secs;
  1178. break;
  1179. case 'S':
  1180. scale = atof(EARGF(usage()));
  1181. if(scale <= 0)
  1182. usage();
  1183. break;
  1184. case 'L':
  1185. logscale++;
  1186. break;
  1187. case 'Y':
  1188. ylabels++;
  1189. break;
  1190. case 'O':
  1191. oldsystem = 1;
  1192. break;
  1193. default:
  1194. if(nargs>=sizeof args || strchr(argchars, ARGC())==nil)
  1195. usage();
  1196. args[nargs++] = ARGC();
  1197. }ARGEND
  1198. if(argc == 0){
  1199. mach = emalloc(nmach*sizeof(Machine));
  1200. initmach(&mach[0], mysysname);
  1201. readmach(&mach[0], 1);
  1202. }else{
  1203. for(i=j=0; i<argc; i++){
  1204. if (addmachine(argv[i]))
  1205. readmach(&mach[j++], 1);
  1206. }
  1207. if (j == 0)
  1208. exits("connect");
  1209. }
  1210. for(i=0; i<nargs; i++)
  1211. switch(args[i]){
  1212. default:
  1213. fprint(2, "stats: internal error: unknown arg %c\n", args[i]);
  1214. usage();
  1215. case 'b':
  1216. addgraph(Mbattery);
  1217. break;
  1218. case 'c':
  1219. addgraph(Mcontext);
  1220. break;
  1221. case 'e':
  1222. addgraph(Mether);
  1223. break;
  1224. case 'E':
  1225. addgraph(Metherin);
  1226. addgraph(Metherout);
  1227. break;
  1228. case 'f':
  1229. addgraph(Mfault);
  1230. break;
  1231. case 'i':
  1232. addgraph(Mintr);
  1233. break;
  1234. case 'I':
  1235. addgraph(Mload);
  1236. addgraph(Midle);
  1237. addgraph(Minintr);
  1238. break;
  1239. case 'l':
  1240. addgraph(Mload);
  1241. break;
  1242. case 'm':
  1243. addgraph(Mmem);
  1244. break;
  1245. case 'n':
  1246. addgraph(Metherin);
  1247. addgraph(Metherout);
  1248. addgraph(Methererr);
  1249. break;
  1250. case 'p':
  1251. addgraph(Mtlbpurge);
  1252. break;
  1253. case 's':
  1254. addgraph(Msyscall);
  1255. break;
  1256. case 't':
  1257. addgraph(Mtlbmiss);
  1258. addgraph(Mtlbpurge);
  1259. break;
  1260. case '8':
  1261. addgraph(Msignal);
  1262. break;
  1263. case 'w':
  1264. addgraph(Mswap);
  1265. break;
  1266. case 'z':
  1267. addgraph(Mtemp);
  1268. break;
  1269. }
  1270. if(ngraph == 0)
  1271. addgraph(Mload);
  1272. for(i=0; i<nmach; i++)
  1273. for(j=0; j<ngraph; j++)
  1274. graph[i*ngraph+j].mach = &mach[i];
  1275. if(initdraw(nil, nil, "stats") < 0){
  1276. fprint(2, "stats: initdraw failed: %r\n");
  1277. exits("initdraw");
  1278. }
  1279. colinit();
  1280. einit(Emouse);
  1281. notify(nil);
  1282. startproc(mouseproc, Mouseproc);
  1283. pids[Mainproc] = getpid();
  1284. display->locking = 1; /* tell library we're using the display lock */
  1285. resize();
  1286. unlockdisplay(display); /* display is still locked from initdraw() */
  1287. for(;;){
  1288. for(i=0; i<nmach; i++)
  1289. readmach(&mach[i], 0);
  1290. lockdisplay(display);
  1291. parity = 1-parity;
  1292. for(i=0; i<nmach*ngraph; i++){
  1293. graph[i].newvalue(graph[i].mach, &v, &vmax, 0);
  1294. graph[i].update(&graph[i], v, vmax);
  1295. }
  1296. flushimage(display, 1);
  1297. unlockdisplay(display);
  1298. sleep(sleeptime);
  1299. }
  1300. }