stats.c 27 KB

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