stats.c 27 KB

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