stats.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358
  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, ulong 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[48];
  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, "%lud", 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", nil);
  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", nil);
  473. } else
  474. execl("/bin/srvold9p", "srvold9p", "-s", nil);
  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, "999999999999");
  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. if (j == 0)
  1138. exits("connect");
  1139. }
  1140. for(i=0; i<nargs; i++)
  1141. switch(args[i]){
  1142. default:
  1143. fprint(2, "stats: internal error: unknown arg %c\n", args[i]);
  1144. usage();
  1145. case 'b':
  1146. addgraph(Mbattery);
  1147. break;
  1148. case 'c':
  1149. addgraph(Mcontext);
  1150. break;
  1151. case 'e':
  1152. addgraph(Mether);
  1153. break;
  1154. case 'E':
  1155. addgraph(Metherin);
  1156. addgraph(Metherout);
  1157. break;
  1158. case 'f':
  1159. addgraph(Mfault);
  1160. break;
  1161. case 'i':
  1162. addgraph(Mintr);
  1163. break;
  1164. case 'I':
  1165. addgraph(Mload);
  1166. addgraph(Midle);
  1167. addgraph(Minintr);
  1168. break;
  1169. case 'l':
  1170. addgraph(Mload);
  1171. break;
  1172. case 'm':
  1173. addgraph(Mmem);
  1174. break;
  1175. case 'n':
  1176. addgraph(Metherin);
  1177. addgraph(Metherout);
  1178. addgraph(Methererr);
  1179. break;
  1180. case 'p':
  1181. addgraph(Mtlbpurge);
  1182. break;
  1183. case 's':
  1184. addgraph(Msyscall);
  1185. break;
  1186. case 't':
  1187. addgraph(Mtlbmiss);
  1188. addgraph(Mtlbpurge);
  1189. break;
  1190. case '8':
  1191. addgraph(Msignal);
  1192. break;
  1193. case 'w':
  1194. addgraph(Mswap);
  1195. break;
  1196. }
  1197. if(ngraph == 0)
  1198. addgraph(Mload);
  1199. for(i=0; i<nmach; i++)
  1200. for(j=0; j<ngraph; j++)
  1201. graph[i*ngraph+j].mach = &mach[i];
  1202. if(initdraw(nil, nil, "stats") < 0){
  1203. fprint(2, "stats: initdraw failed: %r\n");
  1204. exits("initdraw");
  1205. }
  1206. colinit();
  1207. einit(Emouse);
  1208. notify(nil);
  1209. startproc(mouseproc, Mouseproc);
  1210. pids[Mainproc] = getpid();
  1211. display->locking = 1; /* tell library we're using the display lock */
  1212. resize();
  1213. unlockdisplay(display); /* display is still locked from initdraw() */
  1214. for(;;){
  1215. for(i=0; i<nmach; i++)
  1216. readmach(&mach[i], 0);
  1217. lockdisplay(display);
  1218. parity = 1-parity;
  1219. for(i=0; i<nmach*ngraph; i++){
  1220. graph[i].newvalue(graph[i].mach, &v, &vmax, 0);
  1221. graph[i].update(&graph[i], v, vmax);
  1222. }
  1223. flushimage(display, 1);
  1224. unlockdisplay(display);
  1225. sleep(sleeptime);
  1226. }
  1227. }