Debian Codegraph @

utils/dtop.c

raw
1 /*****************************************************************************
2 * Copyright (C) 2010-14 Lawrence Livermore National Security, LLC.
3 * Written by Jim Garlick <[email protected]> LLNL-CODE-423279
4 * All Rights Reserved.
5 *
6 * This file is part of the Distributed I/O Daemon (diod).
7 * For details, see http://code.google.com/p/diod.
8 *
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by the Free
11 * Software Foundation; either version 2 of the license, or (at your option)
12 * any later version.
13 *
14 * This program is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
16 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 * for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation,
21 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 * See also: http://www.gnu.org/licenses
23 *****************************************************************************/
24
25 /* dtop.c - watch diod performance */
26
27 #if HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <fcntl.h>
33 #include <unistd.h>
34 #include <stdlib.h>
35 #include <stdint.h>
36 #include <stdarg.h>
37 #include <stdio.h>
38 #if HAVE_GETOPT_H
39 #include <getopt.h>
40 #endif
41 #include <string.h>
42 #include <curses.h>
43 #include <errno.h>
44 #include <ctype.h>
45 #include <libgen.h>
46 #include <curses.h>
47 #include <signal.h>
48 #include <sys/time.h>
49 #include <pthread.h>
50 #include <inttypes.h>
51 #include <dirent.h>
52 #include <limits.h>
53
54 #include "9p.h"
55 #include "npfs.h"
56 #include "npclient.h"
57 #include "xpthread.h"
58
59 #include "list.h"
60 #include "hostlist.h"
61 #include "diod_log.h"
62 #include "diod_sock.h"
63 #include "diod_auth.h"
64 #include "sample.h"
65
66 int mvwprintw(WINDOW *win, int y, int x, const char *fmt, ...)
67 __attribute__ ((format (printf, 4, 5)));
68
69 #define OPTIONS "h:P:p:"
70 #if HAVE_GETOPT_LONG
71 #define GETOPT(ac,av,opt,lopt) getopt_long (ac,av,opt,lopt,NULL)
72 static const struct option longopts[] = {
73 {"hostlist", required_argument, 0, 'h'},
74 {"poll-period",required_argument, 0, 'P'},
75 {"port", required_argument, 0, 'p'},
76 {0, 0, 0, 0},
77 };
78 #else
79 #define GETOPT(ac,av,opt,lopt) getopt (ac,av,opt)
80 #endif
81
82 #ifndef MAXHOSTNAMELEN
83 #define MAXHOSTNAMELEN 64
84 #endif
85
86 typedef struct {
87 char host[MAXHOSTNAMELEN];
88 char aname[PATH_MAX];
89 } Tpoolkey;
90
91 /* Tpool view.
92 * This is the actual data we sample.
93 */
94 typedef struct {
95 Tpoolkey key;
96 sample_t rbytes;
97 sample_t wbytes;
98 sample_t iops;
99 sample_t numfids;
100 sample_t numreqs;
101 sample_t totreqs;
102 sample_t nreqs[P9_RWSTAT + 1];
103 sample_t rcount[NPSTATS_RWCOUNT_BINS];
104 sample_t wcount[NPSTATS_RWCOUNT_BINS];
105 } TpoolStats;
106 static List tpools = NULL;
107
108 /* Aname stats.
109 * These are derived from tpool data in _update_display_aname ().
110 */
111 typedef struct {
112 char *aname;
113 double rmbps;
114 double wmbps;
115 double iops;
116 double numfids;
117 double numreqs;
118 double totreqs;
119 } AnameStats;
120
121 /* Server stats.
122 * The first part is state for polling servers.
123 * The second part is stats for servers, derived from Tpool stats as needed.
124 */
125 typedef struct {
126 char *host;
127 char *port;
128 double poll_sec;
129 int fd;
130 Npcfid *root;
131 time_t last_poll;
132 pthread_t thread;
133 /* The following data is only updated when in 'server' view.
134 * It is derived from tpool data in _update_display_server ().
135 */
136 int numanames;
137 double rmbps;
138 double wmbps;
139 double numfids;
140 double numreqs;
141 double totreqs;
142 double numconns;
143 sample_t mem_cached;
144 sample_t mem_dirty;
145 sample_t nfs_ops;
146 } Server;
147 static List servers = NULL; /* static list of servers we are polling */
148
149 #define TOPWIN_LINES 7
150
151 static pthread_mutex_t dtop_lock = PTHREAD_MUTEX_INITIALIZER;
152
153 static Server *_create_server (char *host, char *port, double poll_sec);
154 static void _destroy_server (Server *sp);
155 static int _compare_aname(AnameStats *ap1, AnameStats *ap2);
156 static int _match_aname(AnameStats *ap, char *aname);
157 static AnameStats *_create_aname (char *aname);
158 static void _destroy_aname (AnameStats *ap);
159 static void _destroy_tpool (TpoolStats *tp);
160 static void _curses_watcher (double update_secs);
161
162 static int stale_secs = 5;
163 static int exiting = 0;
164
165 static void
166 usage (void)
167 {
168 fprintf (stderr,
169 "Usage: dtop [-p sec] -h hostlist\n"
170 " -h,--hostlist HOSTS hostnames to monitor (default localhost)\n"
171 " -P,--poll-period SEC polling period in seconds (default 1.0)\n"
172 " -p,--port PORT port (default 564)\n"
173 );
174 exit (1);
175 }
176
177 int
178 main (int argc, char *argv[])
179 {
180 hostlist_t hl = NULL;
181 hostlist_iterator_t itr;
182 double poll_sec = 1;
183 char *host, *port = "564";
184 int c;
185 sigset_t sigs;
186 Server *sp;
187 ListIterator si;
188
189 diod_log_init (argv[0]);
190
191 opterr = 0;
192 while ((c = GETOPT (argc, argv, OPTIONS, longopts)) != -1) {
193 switch (c) {
194 case 'h': /* --hostlist NAME */
195 if (!hl) {
196 if (!(hl = hostlist_create (optarg)))
197 err_exit ("failed to create hostlist");
198 } else {
199 if (hostlist_push (hl, optarg) == 0)
200 err_exit ("failed to append to hostlist");
201 }
202 break;
203 case 'P': /* --poll-period SEC */
204 poll_sec = strtod (optarg, NULL);
205 if (poll_sec <= 0.0)
206 msg_exit ("polling period should be positive");
207 break;
208 case 'p': /* --port PORT */
209 port = optarg;
210 break;
211 default:
212 usage ();
213 }
214 }
215 if (optind < argc)
216 usage ();
217 if (!hl) {
218 if (!(hl = hostlist_create ("localhost")))
219 err_exit ("failed to create hostlist");
220 }
221
222 if (!(tpools = list_create ((ListDelF)_destroy_tpool)))
223 err_exit ("out of memory");
224
225 sigemptyset (&sigs);
226 sigaddset (&sigs, SIGPIPE);
227 if (sigprocmask (SIG_BLOCK, &sigs, NULL) < 0)
228 err_exit ("sigprocmask");
229
230 /* Launch readers - create a list of Servers.
231 */
232 if (!(servers = list_create (NULL)))
233 err_exit ("out of memory");
234 if (!(itr = hostlist_iterator_create (hl)))
235 err_exit ("out of memory");
236 while ((host = hostlist_next (itr))) {
237 if (!(list_append (servers, _create_server (host, port, poll_sec))))
238 err_exit ("out of memory");
239 }
240 hostlist_iterator_destroy (itr);
241
242 _curses_watcher (poll_sec/2);
243
244 if (!(si = list_iterator_create (servers)))
245 msg_exit ("out of memory");
246 while ((sp = list_next (si)))
247 _destroy_server (sp);
248 list_iterator_destroy (si);
249
250 diod_log_fini ();
251
252 exit (0);
253 }
254
255 static void
256 _update_display_topwin (WINDOW *win)
257 {
258 int y = 0;
259 time_t now = time (NULL);
260 char *ts = ctime (&now);
261 int nservers = 0, upservers = 0;
262 double statfs=0, lopen=0, lcreate=0, symlink=0, mknod=0;
263 double rename=0, readlink=0, getattr=0, setattr=0, readdir=0;
264 double fsync=0, lock=0, getlock=0, link=0, mkdir=0;
265 double version=0, auth=0, attach=0, flush=0, walk=0;
266 double read=0, write=0, clunk=0, remove=0;
267 double rmbps=0, wmbps=0;
268 ListIterator itr;
269 TpoolStats *tp;
270 Server *sp;
271
272 xpthread_mutex_lock (&dtop_lock);
273 if (!(itr = list_iterator_create (tpools)))
274 msg_exit ("out of memory");
275 while ((tp = list_next (itr))) {
276 statfs += sample_rate (tp->nreqs[P9_TSTATFS], now);
277 lopen += sample_rate (tp->nreqs[P9_TLOPEN], now);
278 lcreate += sample_rate (tp->nreqs[P9_TLCREATE], now);
279 symlink += sample_rate (tp->nreqs[P9_TSYMLINK], now);
280 mknod += sample_rate (tp->nreqs[P9_TMKNOD], now);
281 rename += sample_rate (tp->nreqs[P9_TRENAME], now);
282 readlink += sample_rate (tp->nreqs[P9_TREADLINK], now);
283 getattr += sample_rate (tp->nreqs[P9_TGETATTR], now);
284 setattr += sample_rate (tp->nreqs[P9_TSETATTR], now);
285 readdir += sample_rate (tp->nreqs[P9_TREADDIR], now);
286 fsync += sample_rate (tp->nreqs[P9_TFSYNC], now);
287 lock += sample_rate (tp->nreqs[P9_TLOCK], now);
288 getlock += sample_rate (tp->nreqs[P9_TGETLOCK], now);
289 link += sample_rate (tp->nreqs[P9_TLINK], now);
290 mkdir += sample_rate (tp->nreqs[P9_TMKDIR], now);
291 version += sample_rate (tp->nreqs[P9_TVERSION], now);
292 auth += sample_rate (tp->nreqs[P9_TAUTH], now);
293 attach += sample_rate (tp->nreqs[P9_TATTACH], now);
294 flush += sample_rate (tp->nreqs[P9_TFLUSH], now);
295 walk += sample_rate (tp->nreqs[P9_TWALK], now);
296 read += sample_rate (tp->nreqs[P9_TREAD], now);
297 write += sample_rate (tp->nreqs[P9_TWRITE], now);
298 clunk += sample_rate (tp->nreqs[P9_TCLUNK], now);
299 remove += sample_rate (tp->nreqs[P9_TREMOVE], now);
300
301 rmbps += sample_rate (tp->rbytes, now) / (1024*1024);
302 wmbps += sample_rate (tp->wbytes, now) / (1024*1024);
303 }
304 list_iterator_destroy (itr);
305 if (!(itr = list_iterator_create (servers)))
306 msg_exit ("out of memory");
307 while ((sp = list_next (itr))) {
308 if (now - sp->last_poll < stale_secs)
309 upservers++;
310 nservers++;
311 }
312 list_iterator_destroy (itr);
313
314 xpthread_mutex_unlock (&dtop_lock);
315
316 wclear (win);
317 mvwprintw (win, y, 0, "%s", "DIOD - Distributed I/O Daemon");
318 mvwprintw (win, y++, 55, "%*s", (int)(strlen (ts) - 1), ts);
319 y++;
320
321 mvwprintw (win, y++, 0,
322 "Ops/s: %5.0f statfs %6.0f lopen %6.0f lcreate %6.0f symlink %6.0f mknod",
323 statfs, lopen, lcreate, symlink, mknod);
324 mvwprintw (win, y++, 0,
325 " %6.0f rename %6.0f readlink %6.0f getattr %6.0f setattr %6.0f link",
326 rename, readlink, getattr, setattr, link);
327 mvwprintw (win, y++, 0,
328 " %6.0f fsync %6.0f lock %6.0f getlock %6.0f readdir %6.0f mkdir",
329 fsync, lock, getlock, readdir, mkdir);
330 mvwprintw (win, y++, 0,
331 " %6.0f version %6.0f auth %6.0f attach %6.0f flush %6.0f walk",
332 version, auth, attach, flush, walk);
333 mvwprintw (win, y++, 0,
334 " %6.0f read %6.0f write %6.0f clunk %6.0f remove",
335 read, write, clunk, remove);
336 y++;
337 mvwprintw (win, y, 0,
338 "GB/s:%7.3f read %7.3f write",
339 rmbps / 1024, wmbps / 1024);
340 mvwprintw (win, y, 57,
341 "Live servers: %d/%d", upservers, nservers);
342 wrefresh (win);
343 }
344
345 static void
346 _update_display_tpool (WINDOW *win)
347 {
348 ListIterator itr;
349 TpoolStats *tp;
350 int y = 0;
351 time_t now = time(NULL);
352
353 wclear (win);
354 wmove (win, y++, 0);
355
356 wattron (win, A_REVERSE);
357 wprintw (win,
358 "%10.10s %14.14s %6.6s %5.5s %5.5s %5.5s %5.5s %5.5s\n",
359 "server", "aname", "fids", "reqs", "queue", "rMB/s", "wMB/s",
360 "IOPS");
361 wattroff (win, A_REVERSE);
362
363 xpthread_mutex_lock (&dtop_lock);
364 if (!(itr = list_iterator_create (tpools)))
365 msg_exit ("out of memory");
366 while ((tp = list_next (itr))) {
367 if (sample_val (tp->numfids, now) == 0)
368 continue;
369 mvwprintw (win, y++, 0,
370 "%10.10s %14.14s %6.0f %5.0f %5.0f %5.0f %5.0f %5.0f",
371 tp->key.host, tp->key.aname,
372 sample_val (tp->numfids, now),
373 sample_rate (tp->totreqs, now),
374 sample_val (tp->numreqs, now),
375 sample_rate (tp->rbytes, now) / (1024*1024),
376 sample_rate (tp->wbytes, now) / (1024*1024),
377 sample_rate (tp->iops, now));
378 }
379 list_iterator_destroy (itr);
380 xpthread_mutex_unlock (&dtop_lock);
381 wrefresh (win);
382 }
383
384 static void
385 _update_display_aname (WINDOW *win)
386 {
387 static List anames = NULL;
388 ListIterator itr;
389 TpoolStats *tp;
390 AnameStats *ap;
391 int y = 0;
392 time_t now = time(NULL);
393 int sort_needed = 0;
394
395 wclear (win);
396 wmove (win, y++, 0);
397
398 wattron (win, A_REVERSE);
399 wprintw (win,
400 "%25.25s %6.6s %5.5s %5.5s %5.5s %5.5s %5.5s\n",
401 "aname", "fids", "reqs", "queue", "rMB/s", "wMB/s", "IOPS");
402 wattroff (win, A_REVERSE);
403
404 if (!anames && !(anames = list_create ((ListDelF)_destroy_aname)))
405 err_exit ("out of memory");
406 if (!(itr = list_iterator_create (anames)))
407 msg_exit ("out of memory");
408 while ((ap = list_next (itr))) {
409 ap->rmbps = ap->wmbps = ap->iops = ap->numfids
410 = ap->numreqs = ap->totreqs = 0;
411 }
412 list_iterator_destroy (itr);
413
414 xpthread_mutex_lock (&dtop_lock);
415 if (!(itr = list_iterator_create (tpools)))
416 msg_exit ("out of memory");
417 while ((tp = list_next (itr))) {
418 ap = list_find_first (anames, (ListFindF)_match_aname, tp->key.aname);
419 if (!ap) {
420 ap = _create_aname (tp->key.aname);
421 if (!list_append (anames, ap))
422 msg_exit ("out of memory");
423 sort_needed = 1;
424 }
425 ap->rmbps += sample_rate (tp->rbytes, now) / (1024*1024);
426 ap->wmbps += sample_rate (tp->wbytes, now) / (1024*1024);
427 ap->iops += sample_rate (tp->iops, now);
428 ap->numfids += sample_val (tp->numfids, now);
429 ap->numreqs += sample_val (tp->numreqs, now);
430 ap->totreqs += sample_rate (tp->totreqs, now);
431 }
432 list_iterator_destroy (itr);
433 xpthread_mutex_unlock (&dtop_lock);
434
435 if (sort_needed)
436 list_sort (anames, (ListCmpF)_compare_aname);
437 if (!(itr = list_iterator_create (anames)))
438 msg_exit ("out of memory");
439 while ((ap = list_next (itr))) {
440 if (ap->numfids == 0)
441 continue;
442 mvwprintw (win, y++, 0,
443 "%25.25s %6.0f %5.0f %5.0f %5.0f %5.0f %5.0f",
444 ap->aname,
445 ap->numfids,
446 ap->totreqs,
447 ap->numreqs,
448 ap->rmbps,
449 ap->wmbps,
450 ap->iops);
451 }
452 list_iterator_destroy (itr);
453 wrefresh (win);
454 }
455
456
457 static void
458 _update_display_rwcount (WINDOW *win)
459 {
460 ListIterator itr;
461 TpoolStats *tp;
462 int y = 0;
463 time_t now = time(NULL);
464
465 wclear (win);
466 wmove (win, y++, 0);
467
468 wattron (win, A_REVERSE);
469 wprintw (win,
470 "%9.9s %10.10s "
471 "%5.5s %5.5s %5.5s %5.5s %5.5s "
472 "%5.5s %5.5s %5.5s %5.5s %5.5s",
473 "server", "aname",
474 "r<4K", "r<8K", "r<16K", "r<32K", "r<64K",
475 "w<4K", "w<8K", "w<16K", "w<32K", "w<64K");
476 wattroff (win, A_REVERSE);
477
478 xpthread_mutex_lock (&dtop_lock);
479 if (!(itr = list_iterator_create (tpools)))
480 msg_exit ("out of memory");
481 while ((tp = list_next (itr))) {
482 if (sample_val (tp->numfids, now) == 0)
483 continue;
484 mvwprintw (win, y++, 0,
485 "%9.9s %10.10s "
486 "%5.0f %5.0f %5.0f %5.0f %5.0f "
487 "%5.0f %5.0f %5.0f %5.0f %5.0f",
488 tp->key.host, tp->key.aname,
489 sample_rate (tp->rcount[0], now),
490 sample_rate (tp->rcount[1], now),
491 sample_rate (tp->rcount[2], now),
492 sample_rate (tp->rcount[3], now),
493 sample_rate (tp->rcount[4], now),
494 sample_rate (tp->wcount[0], now),
495 sample_rate (tp->wcount[1], now),
496 sample_rate (tp->wcount[2], now),
497 sample_rate (tp->wcount[3], now),
498 sample_rate (tp->wcount[4], now));
499 }
500 list_iterator_destroy (itr);
501 xpthread_mutex_unlock (&dtop_lock);
502 wrefresh (win);
503 }
504
505 static int
506 _match_serverhost (Server *sp, char *host)
507 {
508 if (!strcmp (sp->host, host))
509 return 1;
510 return 0;
511 }
512
513 static void
514 _update_display_server (WINDOW *win)
515 {
516 ListIterator itr;
517 Server *sp;
518 TpoolStats *tp;
519 time_t now = time (NULL);
520 int y = 0;
521
522 wclear (win);
523 wmove (win, y++, 0);
524
525 wattron (win, A_REVERSE);
526 wprintw (win,
527 "%10.10s %5.5s %5.5s %6.6s %5.5s %5.5s %5.5s %5.5s %5.5s %5.5s %5.5s\n",
528 "server", "aname", "conns", "fids", "reqs", "queue", "rMB/s", "wMB/s",
529 "cache", "dirty", "nfs/s");
530 wattroff (win, A_REVERSE);
531
532 /* zero server stats */
533 xpthread_mutex_lock (&dtop_lock);
534 if (!(itr = list_iterator_create (servers)))
535 msg_exit ("out of memory");
536 while ((sp = list_next (itr))) {
537 sp->numanames = sp->numfids = sp->numreqs = sp->totreqs = 0;
538 sp->rmbps = sp->wmbps = 0;
539 }
540 list_iterator_destroy (itr);
541
542 if (!(itr = list_iterator_create (tpools)))
543 msg_exit ("out of memory");
544 while ((tp = list_next (itr))) {
545 if (sample_val (tp->numfids, now) == 0)
546 continue;
547 sp = list_find_first (servers, (ListFindF)_match_serverhost,
548 tp->key.host);
549 if (sp) {
550 sp->numanames++;
551 sp->numfids += sample_val (tp->numfids, now);
552 sp->totreqs += sample_rate (tp->totreqs, now);
553 sp->numreqs += sample_val (tp->numreqs, now); /* queue */
554 sp->rmbps += sample_rate (tp->rbytes, now) / (1024*1024);
555 sp->wmbps += sample_rate (tp->wbytes, now) / (1024*1024);
556 }
557 }
558 list_iterator_destroy (itr);
559
560 /* iterate thru servers displaying data */
561 if (!(itr = list_iterator_create (servers)))
562 msg_exit ("out of memory");
563 while ((sp = list_next (itr))) {
564 mvwprintw (win, y, 0, "%10.10s %5.0d %5.0f ",
565 sp->host, sp->numanames, sp->numconns);
566 if (now - sp->last_poll < stale_secs) {
567 mvwprintw (win, y, 23,
568 "%6.0f %5.0f %5.0f %5.0f %5.0f %5.0f %5.0f %5.0f",
569 sp->numfids,
570 sp->totreqs,
571 sp->numreqs,
572 sp->rmbps,
573 sp->wmbps,
574 sample_val (sp->mem_cached, now) / 1024,
575 sample_val (sp->mem_dirty, now) / 1024,
576 sample_rate (sp->nfs_ops, now));
577 } else {
578 char *s = strdup (ctime (&sp->last_poll));
579 if (!s)
580 msg_exit ("out of memory");
581 s[strlen (s) - 1] = '\0';
582 mvwprintw (win, y, 23, "[last heard from on %s]", s);
583 free (s);
584 }
585 y++;
586 }
587 list_iterator_destroy (itr);
588 xpthread_mutex_unlock (&dtop_lock);
589
590 wrefresh (win);
591 }
592
593 static void
594 _update_display_help (WINDOW *win)
595 {
596 int y = 0;
597
598 wclear (win);
599 mvwprintw (win, y++, 0, "Help for Interactive Commands - dtop "
600 "version %s.%s", META_VERSION, META_RELEASE);
601 y++;
602 mvwprintw (win, y++, 2, "a (default) Aname view");
603 mvwprintw (win, y++, 2, "t Tpool server/aname view");
604 mvwprintw (win, y++, 2, "s Diod server view");
605 mvwprintw (win, y++, 2, "c Display I/O size histograms ");
606 mvwprintw (win, y++, 2, "h|? Display this help screen");
607 mvwprintw (win, y++, 2, "q Quit");
608 wrefresh (win);
609 }
610
611 typedef enum {
612 VIEW_TPOOL, VIEW_SERVER, VIEW_ANAME, VIEW_RWCOUNT, VIEW_HELP
613 } view_t;
614
615 static void
616 _curses_watcher (double update_secs)
617 {
618 WINDOW *topwin;
619 WINDOW *subwin;
620 int c;
621 view_t view = VIEW_ANAME;
622
623 if (!(topwin = initscr ()))
624 err_exit ("error initializing parent window");
625 if (!(subwin = newwin (256, 80, 9, 0)))
626 err_exit ("error initializing subwindow");
627
628 raw ();
629 noecho ();
630 timeout (update_secs * 1000);
631 keypad (topwin, TRUE);
632 curs_set (0);
633
634 while (!isendwin ()) {
635 switch (view) {
636 case VIEW_TPOOL:
637 _update_display_topwin (topwin);
638 _update_display_tpool (subwin);
639 break;
640 case VIEW_SERVER:
641 _update_display_topwin (topwin);
642 _update_display_server (subwin);
643 break;
644 case VIEW_ANAME:
645 _update_display_topwin (topwin);
646 _update_display_aname (subwin);
647 break;
648 case VIEW_RWCOUNT:
649 _update_display_topwin (topwin);
650 _update_display_rwcount (subwin);
651 break;
652 case VIEW_HELP:
653 _update_display_help (topwin);
654 break;
655 }
656 switch ((c = getch ())) {
657 case 'q':
658 case 0x03:
659 delwin (subwin);
660 endwin ();
661 break;
662 case 's': /* server view */
663 view = VIEW_SERVER;
664 break;
665 case 't': /* tpool view */
666 view = VIEW_TPOOL;
667 break;
668 case 'a': /* aname view */
669 view = VIEW_ANAME;
670 break;
671 case 'c': /* rwcount view */
672 view = VIEW_RWCOUNT;
673 break;
674 case 'h': /* help view */
675 case '?':
676 view = VIEW_HELP;
677 break;
678 case ERR: /* timeout */
679 break;
680 }
681 }
682 exiting = 1;
683 }
684
685 static int
686 _match_tpool (TpoolStats *x, Tpoolkey *key)
687 {
688 if (!strcmp (key->host, x->key.host) && !strcmp(key->aname, x->key.aname))
689 return 1;
690 return 0;
691 }
692
693 static TpoolStats *
694 _create_tpool (Tpoolkey *key)
695 {
696 TpoolStats *tp;
697 int i;
698
699 if (!(tp = malloc (sizeof (*tp))))
700 msg_exit ("out of memory");
701 memset (tp, 0, sizeof (*tp));
702 strcpy (tp->key.host, key->host);
703 strcpy (tp->key.aname, key->aname);
704 tp->rbytes = sample_create (stale_secs);
705 tp->wbytes = sample_create (stale_secs);
706 tp->iops = sample_create (stale_secs);
707 tp->numfids = sample_create (stale_secs);
708 tp->totreqs = sample_create (stale_secs);
709 tp->numreqs = sample_create (stale_secs); /* in queue */
710 for (i = 0; i < sizeof(tp->nreqs)/sizeof(tp->nreqs[0]); i++)
711 tp->nreqs[i] = sample_create (stale_secs);
712 for (i = 0; i < NPSTATS_RWCOUNT_BINS; i++) {
713 tp->rcount[i] = sample_create (stale_secs);
714 tp->wcount[i] = sample_create (stale_secs);
715 }
716
717 return tp;
718 }
719
720 static void
721 _destroy_tpool (TpoolStats *tp)
722 {
723 int i;
724
725 for (i = 0; i < NPSTATS_RWCOUNT_BINS; i++) {
726 sample_destroy (tp->rcount[i]);
727 sample_destroy (tp->wcount[i]);
728 }
729 for (i = 0; i < sizeof(tp->nreqs)/sizeof(tp->nreqs[0]); i++)
730 sample_destroy (tp->nreqs[i]);
731 sample_destroy (tp->numreqs);
732 sample_destroy (tp->totreqs);
733 sample_destroy (tp->numfids);
734 sample_destroy (tp->iops);
735 sample_destroy (tp->rbytes);
736 sample_destroy (tp->wbytes);
737 free (tp);
738 }
739
740 static char *
741 _numerical_suffix (char *s, unsigned long *np)
742 {
743 char *p = s + strlen (s);
744
745 while (p > s && isdigit (*(p - 1)))
746 p--;
747 if (*p)
748 *np = strtoul (p, NULL, 10);
749 return p;
750 }
751
752 /* Used for list_sort () of AnameStats list by aname.
753 * Like strcmp, but handle variable-width (unpadded) numerical suffixes, if any.
754 */
755 static int
756 _compare_aname(AnameStats *ap1, AnameStats *ap2)
757 {
758 unsigned long n1, n2;
759 char *p1 = _numerical_suffix (ap1->aname, &n1);
760 char *p2 = _numerical_suffix (ap2->aname, &n2);
761
762 if (*p1 && *p2
763 && (p1 - ap1->aname) == (p2 - ap2->aname)
764 && !strncmp (ap1->aname, ap2->aname, p1 - ap1->aname)) {
765 return (n1 < n2 ? -1
766 : n1 > n2 ? 1 : 0);
767 }
768 return strcmp (ap1->aname, ap2->aname);
769
770 }
771
772 static int
773 _match_aname(AnameStats *ap, char *aname)
774 {
775 if (!strcmp (aname, ap->aname))
776 return 1;
777 return 0;
778 }
779
780 static AnameStats *
781 _create_aname (char *aname)
782 {
783 AnameStats *ap;
784
785 if (!(ap = malloc (sizeof (*ap))))
786 msg_exit ("out of memory");
787 memset (ap, 0, sizeof (*ap));
788 if (!(ap->aname = strdup (aname)))
789 msg_exit ("out of memory");
790 return ap;
791 }
792
793 static void
794 _destroy_aname (AnameStats *ap)
795 {
796 free (ap->aname);
797 free (ap);
798 }
799
800 static u64
801 _sum_nreqs (Npstats *sp)
802 {
803 u64 res = 0;
804 int i;
805
806 for (i = 0; i < sizeof(sp->nreqs)/sizeof(sp->nreqs[0]); i++)
807 res += sp->nreqs[i];
808 return res;
809 }
810
811 static void
812 _update (char *host, time_t t, char *s)
813 {
814 Npstats stats;
815 Tpoolkey key;
816 TpoolStats *tp;
817 int i;
818
819 memset (stats.nreqs, 0, sizeof(stats.nreqs));
820 if (np_decode_tpools_str (s, &stats) < 0) /* mallocs stats.name */
821 return;
822 snprintf (key.host, sizeof(key.host), "%s", host);
823 snprintf (key.aname, sizeof(key.aname), "%s", stats.name);
824
825 xpthread_mutex_lock (&dtop_lock);
826 if (!(tp = list_find_first (tpools, (ListFindF)_match_tpool, &key))) {
827 tp = _create_tpool (&key);
828 if (!list_append (tpools, tp))
829 msg_exit ("out of memory");
830 }
831 sample_update (tp->rbytes, (double)stats.rbytes, t);
832 sample_update (tp->wbytes, (double)stats.wbytes, t);
833 sample_update (tp->iops,
834 (double)(stats.nreqs[P9_TREAD] + stats.nreqs[P9_TWRITE]), t);
835 sample_update (tp->numfids, (double)stats.numfids, t);
836 sample_update (tp->numreqs, (double)stats.numreqs, t);
837 sample_update (tp->totreqs, (double)_sum_nreqs(&stats), t);
838 for (i = 0; i < sizeof(tp->nreqs)/sizeof(tp->nreqs[0]); i++)
839 sample_update (tp->nreqs[i], (double)stats.nreqs[i], t);
840 for (i = 0; i < NPSTATS_RWCOUNT_BINS; i++) {
841 sample_update (tp->rcount[i], (double)stats.rcount[i], t);
842 sample_update (tp->wcount[i], (double)stats.wcount[i], t);
843 }
844 xpthread_mutex_unlock (&dtop_lock);
845
846 if (stats.name)
847 free (stats.name);
848 }
849
850 static int
851 _read_ctl_tpools (Server *sp)
852 {
853 time_t now;
854 char *buf, *s, *p;
855
856 if (!(buf = npc_aget (sp->root, "tpools")))
857 return -1;
858 now = time (NULL);
859 for (s = buf; s && *s; s = p) {
860 p = strchr (s, '\n');
861 if (p)
862 *p++ = '\0';
863 _update (sp->host, now, s);
864 }
865 free (buf);
866 return 0;
867 }
868
869 static int
870 _read_ctl_meminfo (Server *sp)
871 {
872 time_t now;
873 char *buf, *s, *p;
874
875 if (!(buf = npc_aget (sp->root, "meminfo")))
876 return -1;
877 now = time (NULL);
878 for (s = buf; s && *s; s = p) {
879 p = strchr (s, '\n');
880 if (p)
881 *p++ = '\0';
882 if (!strncmp (s, "Cached:", 7)) {
883 sample_update (sp->mem_cached, strtod (s + 7, NULL), now);
884 } else if (!strncmp (s, "Dirty:", 6)) {
885 sample_update (sp->mem_dirty, strtod (s + 6, NULL), now);
886 break;
887 }
888 }
889 free (buf);
890 return 0;
891 }
892
893 /* nfs module may not be loaded, so this is not fatal like the others.
894 */
895 static int
896 _read_ctl_nfsops (Server *sp)
897 {
898 time_t now;
899 char *buf, *s, *p;
900
901 if ((buf = npc_aget (sp->root, "net.rpc.nfs"))) {
902 now = time (NULL);
903 for (s = buf; s && *s; s = p) {
904 p = strchr (s, '\n');
905 if (p)
906 *p++ = '\0';
907 if (!strncmp (s, "rpc", 3)) {
908 sample_update (sp->nfs_ops, strtod (s + 3, NULL), now);
909 break;
910 }
911 }
912 free (buf);
913 }
914 return 0;
915 }
916
917 static int
918 _read_ctl_connections (Server *sp)
919 {
920 char *buf, *s, *p;
921 int count = 0;
922
923 if (!(buf = npc_aget (sp->root, "connections")))
924 return -1;
925 for (s = buf; s && *s; s = p) {
926 p = strchr (s, '\n');
927 if (p)
928 *p++ = '\0';
929 count++;
930 }
931 free (buf);
932 sp->numconns = count;
933 return 0;
934 }
935
936 static void *
937 _reader (void *arg)
938 {
939 Server *sp = (Server *)arg;
940
941 while (!exiting) {
942 if (sp->fd == -1) {
943 if (sp->host[0] == '/')
944 sp->fd = diod_sock_connect_unix (sp->host, DIOD_SOCK_QUIET);
945 else
946 sp->fd = diod_sock_connect_inet (sp->host, sp->port,
947 DIOD_SOCK_QUIET);
948 }
949 if (sp->fd == -1)
950 goto skip;
951 if (sp->root == NULL)
952 sp->root = npc_mount (sp->fd, sp->fd, 65536, "ctl", diod_auth);
953 if (sp->root == NULL) {
954 (void)close (sp->fd);
955 sp->fd = -1;
956 goto skip;
957 }
958 if (_read_ctl_tpools (sp) < 0 || _read_ctl_meminfo (sp) < 0
959 || _read_ctl_nfsops (sp) < 0 || _read_ctl_connections (sp) < 0) {
960 (void)npc_umount (sp->root); /* closes fd */
961 sp->root = NULL;
962 sp->fd = -1;
963 goto skip;
964 }
965 sp->last_poll = time (NULL);
966 skip:
967 usleep ((useconds_t)(sp->poll_sec * 1E6));
968 }
969 if (sp->root) {
970 (void)npc_umount (sp->root);
971 sp->root = NULL;
972 sp->fd = -1;
973 } else if (sp->fd >= 0) {
974 (void)close (sp->fd);
975 sp->fd = -1;
976 }
977 return NULL;
978 }
979
980 static Server *
981 _create_server (char *host, char *port, double poll_sec)
982 {
983 Server *sp;
984 int err;
985
986 if (!(sp = malloc (sizeof (Server))))
987 err_exit ("out of memory");
988 memset (sp, 0, sizeof (*sp));
989 sp->poll_sec = poll_sec;
990 if (!(sp->host = strdup (host)))
991 err_exit ("out of memory");
992 if (!(sp->port = strdup (port)))
993 err_exit ("out of memory");
994 sp->fd = -1;
995 sp->root = NULL;
996 sp->mem_cached = sample_create (stale_secs);
997 sp->mem_dirty = sample_create (stale_secs);
998 sp->nfs_ops = sample_create (stale_secs);
999 if ((err = pthread_create (&sp->thread, NULL, _reader, sp)))
1000 errn_exit (err, "pthread_create");
1001 return sp;
1002 }
1003
1004 static void
1005 _destroy_server (Server *sp)
1006 {
1007 int err;
1008
1009 if ((err = pthread_join (sp->thread, NULL)))
1010 errn_exit (err, "pthread_join");
1011 sample_destroy (sp->nfs_ops);
1012 sample_destroy (sp->mem_dirty);
1013 sample_destroy (sp->mem_cached);
1014 free (sp->port);
1015 free (sp->host);
1016 free (sp);
1017 }
1018
1019 /*
1020 * vi:tabstop=4 shiftwidth=4 expandtab
1021 */