Debian Codegraph @

tools/xentop/xentop.c

raw
1 /*
2 * Copyright (C) International Business Machines Corp., 2005
3 * Author(s): Judy Fischbach <[email protected]>
4 * David Hendricks <[email protected]>
5 * Josh Triplett <[email protected]>
6 * based on code from Anthony Liguori <[email protected]>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; under version 2 of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 /* get curses header from configure */
22 #include INCLUDE_CURSES_H
23
24 #include <ctype.h>
25 #include <errno.h>
26 #include <math.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <stdarg.h>
30 #include <string.h>
31 #include <sys/time.h>
32 #include <time.h>
33 #include <unistd.h>
34 #include <signal.h>
35 #if defined(__linux__)
36 #include <linux/kdev_t.h>
37 #endif
38
39 #include <xenstat.h>
40
41 #define XENTOP_VERSION "1.0"
42
43 #define XENTOP_DISCLAIMER \
44 "Copyright (C) 2005 International Business Machines Corp\n"\
45 "This is free software; see the source for copying conditions.There is NO\n"\
46 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
47 #define XENTOP_BUGSTO "Report bugs to <[email protected]>.\n"
48
49 #define _GNU_SOURCE
50 #include <getopt.h>
51
52 #if !defined(__GNUC__) && !defined(__GNUG__)
53 #define __attribute__(arg) /* empty */
54 #endif
55
56 #define KEY_ESCAPE '\x1B'
57 #define KEY_REPAINT '\x0C'
58
59 #ifdef HOST_SunOS
60 /* Old curses library on Solaris takes non-const strings. Also, ERR interferes
61 * with curse's definition.
62 */
63 #undef ERR
64 #define ERR (-1)
65 #define curses_str_t char *
66 #else
67 #define curses_str_t const char *
68 #endif
69
70 #define INT_FIELD_WIDTH(n) ((unsigned int)(log10(n) + 1))
71
72 /*
73 * Function prototypes
74 */
75 /* Utility functions */
76 static void usage(const char *);
77 static void version(void);
78 static void cleanup(void);
79 static void fail(const char *);
80 static int current_row(void);
81 static int lines(void);
82 static void print(const char *, ...) __attribute__((format(printf,1,2)));
83 static void attr_addstr(int attr, const char *str);
84 static void set_delay(const char *value);
85 static void set_prompt(const char *new_prompt, void (*func)(const char *));
86 static int handle_key(int);
87 static int compare(unsigned long long, unsigned long long);
88 static int compare_dbl(double, double);
89 static int compare_domains(xenstat_domain **, xenstat_domain **);
90 static unsigned long long tot_net_bytes( xenstat_domain *, int);
91 static bool tot_vbd_reqs(xenstat_domain *, int, unsigned long long *);
92
93 /* Field functions */
94 static int compare_state(xenstat_domain *domain1, xenstat_domain *domain2);
95 static void print_state(xenstat_domain *domain);
96 static int compare_cpu(xenstat_domain *domain1, xenstat_domain *domain2);
97 static void print_cpu(xenstat_domain *domain);
98 static int compare_cpu_pct(xenstat_domain *domain1, xenstat_domain *domain2);
99 static void print_cpu_pct(xenstat_domain *domain);
100 static int compare_mem(xenstat_domain *domain1, xenstat_domain *domain2);
101 static void print_mem(xenstat_domain *domain);
102 static void print_mem_pct(xenstat_domain *domain);
103 static int compare_maxmem(xenstat_domain *domain1, xenstat_domain *domain2);
104 static void print_maxmem(xenstat_domain *domain);
105 static void print_max_pct(xenstat_domain *domain);
106 static int compare_vcpus(xenstat_domain *domain1, xenstat_domain *domain2);
107 static void print_vcpus(xenstat_domain *domain);
108 static int compare_nets(xenstat_domain *domain1, xenstat_domain *domain2);
109 static void print_nets(xenstat_domain *domain);
110 static int compare_net_tx(xenstat_domain *domain1, xenstat_domain *domain2);
111 static void print_net_tx(xenstat_domain *domain);
112 static int compare_net_rx(xenstat_domain *domain1, xenstat_domain *domain2);
113 static void print_net_rx(xenstat_domain *domain);
114 static int compare_ssid(xenstat_domain *domain1, xenstat_domain *domain2);
115 static void print_ssid(xenstat_domain *domain);
116 static int compare_name(xenstat_domain *domain1, xenstat_domain *domain2);
117 static void print_name(xenstat_domain *domain);
118 static int compare_vbds(xenstat_domain *domain1, xenstat_domain *domain2);
119 static void print_vbds(xenstat_domain *domain);
120 static int compare_vbd_oo(xenstat_domain *domain1, xenstat_domain *domain2);
121 static void print_vbd_oo(xenstat_domain *domain);
122 static int compare_vbd_rd(xenstat_domain *domain1, xenstat_domain *domain2);
123 static void print_vbd_rd(xenstat_domain *domain);
124 static int compare_vbd_wr(xenstat_domain *domain1, xenstat_domain *domain2);
125 static void print_vbd_wr(xenstat_domain *domain);
126 static int compare_vbd_rsect(xenstat_domain *domain1, xenstat_domain *domain2);
127 static void print_vbd_rsect(xenstat_domain *domain);
128 static int compare_vbd_wsect(xenstat_domain *domain1, xenstat_domain *domain2);
129 static void print_vbd_wsect(xenstat_domain *domain);
130 static void reset_field_widths(void);
131 static void adjust_field_widths(xenstat_domain *domain);
132
133 /* Section printing functions */
134 static void do_summary(void);
135 static void do_header(void);
136 static void do_bottom_line(void);
137 static void do_domain(xenstat_domain *);
138 static void do_vcpu(xenstat_domain *);
139 static void do_network(xenstat_domain *);
140 static void do_vbd(xenstat_domain *);
141 static void top(void);
142
143 /* Field types */
144 typedef enum field_id {
145 FIELD_DOMID,
146 FIELD_NAME,
147 FIELD_STATE,
148 FIELD_CPU,
149 FIELD_CPU_PCT,
150 FIELD_MEM,
151 FIELD_MEM_PCT,
152 FIELD_MAXMEM,
153 FIELD_MAX_PCT,
154 FIELD_VCPUS,
155 FIELD_NETS,
156 FIELD_NET_TX,
157 FIELD_NET_RX,
158 FIELD_VBDS,
159 FIELD_VBD_OO,
160 FIELD_VBD_RD,
161 FIELD_VBD_WR,
162 FIELD_VBD_RSECT,
163 FIELD_VBD_WSECT,
164 FIELD_SSID
165 } field_id;
166
167 typedef struct field {
168 field_id num;
169 const char *header;
170 unsigned int default_width;
171 int (*compare)(xenstat_domain *domain1, xenstat_domain *domain2);
172 void (*print)(xenstat_domain *domain);
173 } field;
174
175 field fields[] = {
176 { FIELD_NAME, "NAME", 10, compare_name, print_name },
177 { FIELD_STATE, "STATE", 6, compare_state, print_state },
178 { FIELD_CPU, "CPU(sec)", 10, compare_cpu, print_cpu },
179 { FIELD_CPU_PCT, "CPU(%)", 6, compare_cpu_pct, print_cpu_pct },
180 { FIELD_MEM, "MEM(k)", 10, compare_mem, print_mem },
181 { FIELD_MEM_PCT, "MEM(%)", 6, compare_mem, print_mem_pct },
182 { FIELD_MAXMEM, "MAXMEM(k)", 10, compare_maxmem, print_maxmem },
183 { FIELD_MAX_PCT, "MAXMEM(%)", 9, compare_maxmem, print_max_pct },
184 { FIELD_VCPUS, "VCPUS", 5, compare_vcpus, print_vcpus },
185 { FIELD_NETS, "NETS", 4, compare_nets, print_nets },
186 { FIELD_NET_TX, "NETTX(k)", 8, compare_net_tx, print_net_tx },
187 { FIELD_NET_RX, "NETRX(k)", 8, compare_net_rx, print_net_rx },
188 { FIELD_VBDS, "VBDS", 4, compare_vbds, print_vbds },
189 { FIELD_VBD_OO, "VBD_OO", 8, compare_vbd_oo, print_vbd_oo },
190 { FIELD_VBD_RD, "VBD_RD", 8, compare_vbd_rd, print_vbd_rd },
191 { FIELD_VBD_WR, "VBD_WR", 8, compare_vbd_wr, print_vbd_wr },
192 { FIELD_VBD_RSECT, "VBD_RSECT", 10, compare_vbd_rsect, print_vbd_rsect },
193 { FIELD_VBD_WSECT, "VBD_WSECT", 10, compare_vbd_wsect, print_vbd_wsect },
194 { FIELD_SSID, "SSID", 4, compare_ssid, print_ssid }
195 };
196
197 const unsigned int NUM_FIELDS = sizeof(fields)/sizeof(field);
198
199 /* Globals */
200 struct timeval curtime, oldtime;
201 xenstat_handle *xhandle = NULL;
202 xenstat_node *prev_node = NULL;
203 xenstat_node *cur_node = NULL;
204 field_id sort_field = FIELD_DOMID;
205 unsigned int first_domain_index = 0;
206 unsigned int delay = 3;
207 unsigned int batch = 0;
208 unsigned int loop = 1;
209 unsigned int iterations = 0;
210 int show_vcpus = 0;
211 int show_networks = 0;
212 int show_vbds = 0;
213 int repeat_header = 0;
214 int show_full_name = 0;
215 int dom0_first = 0;
216 #define PROMPT_VAL_LEN 80
217 const char *prompt = NULL;
218 char prompt_val[PROMPT_VAL_LEN];
219 int prompt_val_len = 0;
220 void (*prompt_complete_func)(const char *);
221
222 static WINDOW *cwin;
223
224 /*
225 * Function definitions
226 */
227
228 /* Utility functions */
229
230 /* Print usage message, using given program name */
231 static void usage(const char *program)
232 {
233 printf("Usage: %s [OPTION]\n"
234 "Displays ongoing information about xen vm resources \n\n"
235 "-h, --help display this help and exit\n"
236 "-V, --version output version information and exit\n"
237 "-d, --delay=SECONDS seconds between updates (default 3)\n"
238 "-n, --networks output vif network data\n"
239 "-x, --vbds output vbd block device data\n"
240 "-r, --repeat-header repeat table header before each domain\n"
241 "-v, --vcpus output vcpu data\n"
242 "-b, --batch output in batch mode, no user input accepted\n"
243 "-i, --iterations number of iterations before exiting\n"
244 "-f, --full-name output the full domain name (not truncated)\n"
245 "-z, --dom0-first display dom0 first (ignore sorting)\n"
246 "\n" XENTOP_BUGSTO,
247 program);
248 return;
249 }
250
251 /* Print program version information */
252 static void version(void)
253 {
254 printf("xentop " XENTOP_VERSION "\n"
255 "Written by Judy Fischbach, David Hendricks, Josh Triplett\n"
256 "\n" XENTOP_DISCLAIMER);
257 }
258
259 /* Clean up any open resources */
260 static void cleanup(void)
261 {
262 if(cwin != NULL && !isendwin())
263 endwin();
264 if(prev_node != NULL)
265 xenstat_free_node(prev_node);
266 if(cur_node != NULL)
267 xenstat_free_node(cur_node);
268 if(xhandle != NULL)
269 xenstat_uninit(xhandle);
270 }
271
272 /* Display the given message and gracefully exit */
273 static void fail(const char *str)
274 {
275 if(cwin != NULL && !isendwin())
276 endwin();
277 fprintf(stderr, "%s", str);
278 exit(1);
279 }
280
281 /* Return the row containing the cursor. */
282 static int current_row(void)
283 {
284 int y, x;
285 getyx(stdscr, y, x);
286 return y;
287 }
288
289 /* Return the number of lines on the screen. */
290 static int lines(void)
291 {
292 int y, x;
293 getmaxyx(stdscr, y, x);
294 return y;
295 }
296
297 /* printf-style print function which calls printw, but only if the cursor is
298 * not on the last line. */
299 static void print(const char *fmt, ...)
300 {
301 va_list args;
302
303 if (!batch) {
304 if((current_row() < lines()-1)) {
305 va_start(args, fmt);
306 vw_printw(stdscr, (curses_str_t)fmt, args);
307 va_end(args);
308 }
309 } else {
310 va_start(args, fmt);
311 vprintf(fmt, args);
312 va_end(args);
313 }
314 }
315
316 static void xentop_attron(int attr)
317 {
318 if (!batch)
319 attron(attr);
320 }
321
322 static void xentop_attroff(int attr)
323 {
324 if (!batch)
325 attroff(attr);
326 }
327
328 /* Print a string with the given attributes set. */
329 static void attr_addstr(int attr, const char *str)
330 {
331 xentop_attron(attr);
332 addstr((curses_str_t)str);
333 xentop_attroff(attr);
334 }
335
336 /* Handle setting the delay from the user-supplied value in prompt_val */
337 static void set_delay(const char *value)
338 {
339 int new_delay;
340 new_delay = atoi(value);
341 if(new_delay > 0)
342 delay = new_delay;
343 }
344
345 /* Enable prompting mode with the given prompt string; call the given function
346 * when a value is available. */
347 static void set_prompt(const char *new_prompt, void (*func)(const char *))
348 {
349 prompt = new_prompt;
350 prompt_val[0] = '\0';
351 prompt_val_len = 0;
352 prompt_complete_func = func;
353 }
354
355 /* Handle user input, return 0 if the program should quit, or 1 if not */
356 static int handle_key(int ch)
357 {
358 if(prompt == NULL) {
359 /* Not prompting for input; handle interactive commands */
360 switch(ch) {
361 case 'n': case 'N':
362 show_networks ^= 1;
363 break;
364 case 'b': case 'B':
365 show_vbds ^= 1;
366 break;
367 case 'r': case 'R':
368 repeat_header ^= 1;
369 break;
370 case 's': case 'S':
371 sort_field = (sort_field + 1) % NUM_FIELDS;
372 break;
373 case 'v': case 'V':
374 show_vcpus ^= 1;
375 break;
376 case KEY_DOWN:
377 first_domain_index++;
378 break;
379 case KEY_UP:
380 if(first_domain_index > 0)
381 first_domain_index--;
382 break;
383 case 'd': case 'D':
384 set_prompt("Delay(sec)", set_delay);
385 break;
386 case KEY_REPAINT:
387 clear();
388 break;
389 case 'q': case 'Q': case KEY_ESCAPE:
390 return 0;
391 }
392 } else {
393 /* Prompting for input; handle line editing */
394 switch(ch) {
395 case '\r':
396 prompt_complete_func(prompt_val);
397 set_prompt(NULL, NULL);
398 break;
399 case KEY_ESCAPE:
400 set_prompt(NULL, NULL);
401 break;
402 case KEY_BACKSPACE:
403 if(prompt_val_len > 0)
404 prompt_val[--prompt_val_len] = '\0';
405 break;
406 default:
407 if((prompt_val_len+1) < PROMPT_VAL_LEN
408 && isprint(ch)) {
409 prompt_val[prompt_val_len++] = (char)ch;
410 prompt_val[prompt_val_len] = '\0';
411 }
412 }
413 }
414
415 return 1;
416 }
417
418 /* Compares two integers, returning -1,0,1 for <,=,> */
419 static int compare(unsigned long long i1, unsigned long long i2)
420 {
421 if(i1 < i2)
422 return -1;
423 if(i1 > i2)
424 return 1;
425 return 0;
426 }
427
428 /* Compares two double precision numbers, returning -1,0,1 for <,=,> */
429 static int compare_dbl(double d1, double d2)
430 {
431 if (d1 < d2)
432 return -1;
433 if (d1 > d2)
434 return 1;
435 return 0;
436 }
437
438 /* Comparison function for use with qsort. Compares two domains using the
439 * current sort field. */
440 static int compare_domains(xenstat_domain **domain1, xenstat_domain **domain2)
441 {
442 return fields[sort_field].compare(*domain1, *domain2);
443 }
444
445 /* Field functions */
446
447 /* Compare domain names, returning -1,0,1 for <,=,> */
448 int compare_name(xenstat_domain *domain1, xenstat_domain *domain2)
449 {
450 return strcasecmp(xenstat_domain_name(domain1), xenstat_domain_name(domain2));
451 }
452
453 /* Prints domain name */
454 void print_name(xenstat_domain *domain)
455 {
456 if(show_full_name)
457 print("%*s", fields[FIELD_NAME-1].default_width, xenstat_domain_name(domain));
458 else
459 print("%10.10s", xenstat_domain_name(domain));
460 }
461
462 struct {
463 unsigned int (*get)(xenstat_domain *);
464 char ch;
465 } state_funcs[] = {
466 { xenstat_domain_dying, 'd' },
467 { xenstat_domain_shutdown, 's' },
468 { xenstat_domain_blocked, 'b' },
469 { xenstat_domain_crashed, 'c' },
470 { xenstat_domain_paused, 'p' },
471 { xenstat_domain_running, 'r' }
472 };
473 const unsigned int NUM_STATES = sizeof(state_funcs)/sizeof(*state_funcs);
474
475 /* Compare states of two domains, returning -1,0,1 for <,=,> */
476 static int compare_state(xenstat_domain *domain1, xenstat_domain *domain2)
477 {
478 unsigned int i, d1s, d2s;
479 for(i = 0; i < NUM_STATES; i++) {
480 d1s = state_funcs[i].get(domain1);
481 d2s = state_funcs[i].get(domain2);
482 if(d1s && !d2s)
483 return -1;
484 if(d2s && !d1s)
485 return 1;
486 }
487 return 0;
488 }
489
490 /* Prints domain state in abbreviated letter format */
491 static void print_state(xenstat_domain *domain)
492 {
493 unsigned int i;
494 for(i = 0; i < NUM_STATES; i++)
495 print("%c", state_funcs[i].get(domain) ? state_funcs[i].ch
496 : '-');
497 }
498
499 /* Compares cpu usage of two domains, returning -1,0,1 for <,=,> */
500 static int compare_cpu(xenstat_domain *domain1, xenstat_domain *domain2)
501 {
502 return -compare(xenstat_domain_cpu_ns(domain1),
503 xenstat_domain_cpu_ns(domain2));
504 }
505
506 /* Prints domain cpu usage in seconds */
507 static void print_cpu(xenstat_domain *domain)
508 {
509 print("%10llu", xenstat_domain_cpu_ns(domain)/1000000000);
510 }
511
512 /* Computes the CPU percentage used for a specified domain */
513 static double get_cpu_pct(xenstat_domain *domain)
514 {
515 xenstat_domain *old_domain;
516 double us_elapsed;
517
518 /* Can't calculate CPU percentage without a previous sample. */
519 if(prev_node == NULL)
520 return 0.0;
521
522 old_domain = xenstat_node_domain(prev_node, xenstat_domain_id(domain));
523 if(old_domain == NULL)
524 return 0.0;
525
526 /* Calculate the time elapsed in microseconds */
527 us_elapsed = ((curtime.tv_sec-oldtime.tv_sec)*1000000.0
528 +(curtime.tv_usec - oldtime.tv_usec));
529
530 /* In the following, nanoseconds must be multiplied by 1000.0 to
531 * convert to microseconds, then divided by 100.0 to get a percentage,
532 * resulting in a multiplication by 10.0 */
533 return ((xenstat_domain_cpu_ns(domain)
534 -xenstat_domain_cpu_ns(old_domain))/10.0)/us_elapsed;
535 }
536
537 static int compare_cpu_pct(xenstat_domain *domain1, xenstat_domain *domain2)
538 {
539 return -compare_dbl(get_cpu_pct(domain1), get_cpu_pct(domain2));
540 }
541
542 /* Prints cpu percentage statistic */
543 static void print_cpu_pct(xenstat_domain *domain)
544 {
545 print("%6.1f", get_cpu_pct(domain));
546 }
547
548 /* Compares current memory of two domains, returning -1,0,1 for <,=,> */
549 static int compare_mem(xenstat_domain *domain1, xenstat_domain *domain2)
550 {
551 return -compare(xenstat_domain_cur_mem(domain1),
552 xenstat_domain_cur_mem(domain2));
553 }
554
555 /* Prints current memory statistic */
556 static void print_mem(xenstat_domain *domain)
557 {
558 print("%10llu", xenstat_domain_cur_mem(domain)/1024);
559 }
560
561 /* Prints memory percentage statistic, ratio of current domain memory to total
562 * node memory */
563 static void print_mem_pct(xenstat_domain *domain)
564 {
565 print("%6.1f", (double)xenstat_domain_cur_mem(domain) /
566 (double)xenstat_node_tot_mem(cur_node) * 100);
567 }
568
569 /* Compares maximum memory of two domains, returning -1,0,1 for <,=,> */
570 static int compare_maxmem(xenstat_domain *domain1, xenstat_domain *domain2)
571 {
572 return -compare(xenstat_domain_max_mem(domain1),
573 xenstat_domain_max_mem(domain2));
574 }
575
576 /* Prints maximum domain memory statistic in KB */
577 static void print_maxmem(xenstat_domain *domain)
578 {
579 unsigned long long max_mem = xenstat_domain_max_mem(domain);
580 if(max_mem == ((unsigned long long)-1))
581 print("%10s", "no limit");
582 else
583 print("%10llu", max_mem/1024);
584 }
585
586 /* Prints memory percentage statistic, ratio of current domain memory to total
587 * node memory */
588 static void print_max_pct(xenstat_domain *domain)
589 {
590 if (xenstat_domain_max_mem(domain) == (unsigned long long)-1)
591 print("%9s", "n/a");
592 else
593 print("%9.1f", (double)xenstat_domain_max_mem(domain) /
594 (double)xenstat_node_tot_mem(cur_node) * 100);
595 }
596
597 /* Compares number of virtual CPUs of two domains, returning -1,0,1 for
598 * <,=,> */
599 static int compare_vcpus(xenstat_domain *domain1, xenstat_domain *domain2)
600 {
601 return -compare(xenstat_domain_num_vcpus(domain1),
602 xenstat_domain_num_vcpus(domain2));
603 }
604
605 /* Prints number of virtual CPUs statistic */
606 static void print_vcpus(xenstat_domain *domain)
607 {
608 print("%5u", xenstat_domain_num_vcpus(domain));
609 }
610
611 /* Compares number of virtual networks of two domains, returning -1,0,1 for
612 * <,=,> */
613 static int compare_nets(xenstat_domain *domain1, xenstat_domain *domain2)
614 {
615 return -compare(xenstat_domain_num_networks(domain1),
616 xenstat_domain_num_networks(domain2));
617 }
618
619 /* Prints number of virtual networks statistic */
620 static void print_nets(xenstat_domain *domain)
621 {
622 print("%4u", xenstat_domain_num_networks(domain));
623 }
624
625 /* Compares number of total network tx bytes of two domains, returning -1,0,1
626 * for <,=,> */
627 static int compare_net_tx(xenstat_domain *domain1, xenstat_domain *domain2)
628 {
629 return -compare(tot_net_bytes(domain1, FALSE),
630 tot_net_bytes(domain2, FALSE));
631 }
632
633 /* Prints number of total network tx bytes statistic */
634 static void print_net_tx(xenstat_domain *domain)
635 {
636 print("%*llu", fields[FIELD_NET_TX-1].default_width, tot_net_bytes(domain, FALSE)/1024);
637 }
638
639 /* Compares number of total network rx bytes of two domains, returning -1,0,1
640 * for <,=,> */
641 static int compare_net_rx(xenstat_domain *domain1, xenstat_domain *domain2)
642 {
643 return -compare(tot_net_bytes(domain1, TRUE),
644 tot_net_bytes(domain2, TRUE));
645 }
646
647 /* Prints number of total network rx bytes statistic */
648 static void print_net_rx(xenstat_domain *domain)
649 {
650 print("%*llu", fields[FIELD_NET_RX-1].default_width, tot_net_bytes(domain, TRUE)/1024);
651 }
652
653 /* Gets number of total network bytes statistic, if rx true, then rx bytes
654 * otherwise tx bytes
655 */
656 static unsigned long long tot_net_bytes(xenstat_domain *domain, int rx_flag)
657 {
658 int i = 0;
659 xenstat_network *network;
660 unsigned num_networks = 0;
661 unsigned long long total = 0;
662
663 /* How many networks? */
664 num_networks = xenstat_domain_num_networks(domain);
665
666 /* Dump information for each network */
667 for (i=0; i < num_networks; i++) {
668 /* Next get the network information */
669 network = xenstat_domain_network(domain,i);
670 if (rx_flag)
671 total += xenstat_network_rbytes(network);
672 else
673 total += xenstat_network_tbytes(network);
674 }
675
676 return total;
677 }
678
679 /* Compares number of virtual block devices of two domains,
680 returning -1,0,1 for * <,=,> */
681 static int compare_vbds(xenstat_domain *domain1, xenstat_domain *domain2)
682 {
683 return -compare(xenstat_domain_num_vbds(domain1),
684 xenstat_domain_num_vbds(domain2));
685 }
686
687 /* Prints number of virtual block devices statistic */
688 static void print_vbds(xenstat_domain *domain)
689 {
690 print("%4u", xenstat_domain_num_vbds(domain));
691 }
692
693 /* Compares number of total VBD OO requests of two domains,
694 returning -1,0,1 * for <,=,> */
695 static int compare_vbd_oo(xenstat_domain *domain1, xenstat_domain *domain2)
696 {
697 unsigned long long dom1_vbd_oo = 0, dom2_vbd_oo = 0;
698
699 tot_vbd_reqs(domain1, FIELD_VBD_OO, &dom1_vbd_oo);
700 tot_vbd_reqs(domain2, FIELD_VBD_OO, &dom2_vbd_oo);
701
702 return -compare(dom1_vbd_oo, dom2_vbd_oo);
703 }
704
705 /* Prints number of total VBD OO requests statistic */
706 static void print_vbd_oo(xenstat_domain *domain)
707 {
708 unsigned long long vbd_oo;
709
710 if (tot_vbd_reqs(domain, FIELD_VBD_OO, &vbd_oo))
711 {
712 print("%8llu", vbd_oo);
713 }
714 else
715 {
716 print("%8c", '-');
717 }
718 }
719
720 /* Compares number of total VBD READ requests of two domains,
721 returning -1,0,1 * for <,=,> */
722 static int compare_vbd_rd(xenstat_domain *domain1, xenstat_domain *domain2)
723 {
724 unsigned long long dom1_vbd_rd = 0, dom2_vbd_rd = 0;
725
726 tot_vbd_reqs(domain1, FIELD_VBD_RD, &dom1_vbd_rd);
727 tot_vbd_reqs(domain2, FIELD_VBD_RD, &dom2_vbd_rd);
728
729 return -compare(dom1_vbd_rd, dom2_vbd_rd);
730 }
731
732 /* Prints number of total VBD READ requests statistic */
733 static void print_vbd_rd(xenstat_domain *domain)
734 {
735 unsigned long long vbd_rd;
736
737 if (tot_vbd_reqs(domain, FIELD_VBD_RD, &vbd_rd))
738 {
739 print("%*llu", fields[FIELD_VBD_RD-1].default_width, vbd_rd);
740 }
741 else
742 {
743 print("%*c", fields[FIELD_VBD_RD-1].default_width, '-');
744 }
745 }
746
747 /* Compares number of total VBD WRITE requests of two domains,
748 returning -1,0,1 * for <,=,> */
749 static int compare_vbd_wr(xenstat_domain *domain1, xenstat_domain *domain2)
750 {
751 unsigned long long dom1_vbd_wr = 0, dom2_vbd_wr = 0;
752
753 tot_vbd_reqs(domain1, FIELD_VBD_WR, &dom1_vbd_wr);
754 tot_vbd_reqs(domain2, FIELD_VBD_WR, &dom2_vbd_wr);
755
756 return -compare(dom1_vbd_wr, dom2_vbd_wr);
757 }
758
759 /* Prints number of total VBD WRITE requests statistic */
760 static void print_vbd_wr(xenstat_domain *domain)
761 {
762 unsigned long long vbd_wr;
763
764 if (tot_vbd_reqs(domain, FIELD_VBD_WR, &vbd_wr))
765 {
766 print("%*llu", fields[FIELD_VBD_WR-1].default_width, vbd_wr);
767 }
768 else
769 {
770 print("%*c", fields[FIELD_VBD_WR-1].default_width, '-');
771 }
772 }
773
774 /* Compares number of total VBD READ sectors of two domains,
775 returning -1,0,1 * for <,=,> */
776 static int compare_vbd_rsect(xenstat_domain *domain1, xenstat_domain *domain2)
777 {
778 unsigned long long dom1_vbd_rsect = 0, dom2_vbd_rsect = 0;
779
780 tot_vbd_reqs(domain1, FIELD_VBD_RSECT, &dom1_vbd_rsect);
781 tot_vbd_reqs(domain2, FIELD_VBD_RSECT, &dom2_vbd_rsect);
782
783 return -compare(dom1_vbd_rsect, dom2_vbd_rsect);
784 }
785
786 /* Prints number of total VBD READ sectors statistic */
787 static void print_vbd_rsect(xenstat_domain *domain)
788 {
789 unsigned long long vbd_rsect;
790
791 if (tot_vbd_reqs(domain, FIELD_VBD_RSECT, &vbd_rsect))
792 {
793 print("%*llu", fields[FIELD_VBD_RSECT-1].default_width, vbd_rsect);
794 }
795 else
796 {
797 print("%*c", fields[FIELD_VBD_RSECT-1].default_width, '-');
798 }
799 }
800
801 /* Compares number of total VBD WRITE sectors of two domains,
802 returning -1,0,1 * for <,=,> */
803 static int compare_vbd_wsect(xenstat_domain *domain1, xenstat_domain *domain2)
804 {
805 unsigned long long dom1_vbd_wsect = 0, dom2_vbd_wsect = 0;
806
807 tot_vbd_reqs(domain1, FIELD_VBD_WSECT, &dom1_vbd_wsect);
808 tot_vbd_reqs(domain2, FIELD_VBD_WSECT, &dom2_vbd_wsect);
809
810 return -compare(dom1_vbd_wsect, dom2_vbd_wsect);
811 }
812
813 /* Prints number of total VBD WRITE sectors statistic */
814 static void print_vbd_wsect(xenstat_domain *domain)
815 {
816 unsigned long long vbd_wsect;
817
818 if (tot_vbd_reqs(domain, FIELD_VBD_WSECT, &vbd_wsect))
819 {
820 print("%*llu", fields[FIELD_VBD_WSECT-1].default_width, vbd_wsect);
821 }
822 else
823 {
824 print("%*c", fields[FIELD_VBD_WSECT-1].default_width, '-');
825 }
826 }
827
828
829 /* Gets number of total VBD requests statistic,
830 * if flag is FIELD_VBD_OO, then OO requests,
831 * if flag is FIELD_VBD_RD, then READ requests,
832 * if flag is FIELD_VBD_WR, then WRITE requests,
833 * if flag is FIELD_VBD_RSECT, then READ sectors,
834 * if flag is FIELD_VBD_WSECT, then WRITE sectors.
835 */
836 static bool tot_vbd_reqs(xenstat_domain *domain, int flag, unsigned long long *res)
837 {
838 int i = 0;
839 xenstat_vbd *vbd;
840 unsigned num_vbds = 0;
841 unsigned long long total = 0;
842 bool show_stats = false;
843
844 num_vbds = xenstat_domain_num_vbds(domain);
845 if (!num_vbds)
846 show_stats = true;
847
848 for ( i=0 ; i < num_vbds ; i++) {
849 vbd = xenstat_domain_vbd(domain,i);
850 if (xenstat_vbd_error(vbd))
851 continue;
852 else
853 show_stats = true;
854
855 switch(flag) {
856 case FIELD_VBD_OO:
857 total += xenstat_vbd_oo_reqs(vbd);
858 break;
859 case FIELD_VBD_RD:
860 total += xenstat_vbd_rd_reqs(vbd);
861 break;
862 case FIELD_VBD_WR:
863 total += xenstat_vbd_wr_reqs(vbd);
864 break;
865 case FIELD_VBD_RSECT:
866 total += xenstat_vbd_rd_sects(vbd);
867 break;
868 case FIELD_VBD_WSECT:
869 total += xenstat_vbd_wr_sects(vbd);
870 break;
871 default:
872 break;
873 }
874 }
875
876 *res = total;
877
878 return show_stats;
879 }
880
881 /* Compares security id (ssid) of two domains, returning -1,0,1 for <,=,> */
882 static int compare_ssid(xenstat_domain *domain1, xenstat_domain *domain2)
883 {
884 return compare(xenstat_domain_ssid(domain1),
885 xenstat_domain_ssid(domain2));
886 }
887
888 /* Prints ssid statistic */
889 static void print_ssid(xenstat_domain *domain)
890 {
891 print("%4u", xenstat_domain_ssid(domain));
892 }
893
894 /* Resets default_width for fields with potentially large numbers */
895 void reset_field_widths(void)
896 {
897 fields[FIELD_NET_TX-1].default_width = 8;
898 fields[FIELD_NET_RX-1].default_width = 8;
899 fields[FIELD_VBD_RD-1].default_width = 8;
900 fields[FIELD_VBD_WR-1].default_width = 8;
901 fields[FIELD_VBD_RSECT-1].default_width = 10;
902 fields[FIELD_VBD_WSECT-1].default_width = 10;
903 }
904
905 /* Adjusts default_width for fields with potentially large numbers */
906 void adjust_field_widths(xenstat_domain *domain)
907 {
908 unsigned int length;
909 unsigned long long vbd_rd, vbd_wr, vbd_rsect, vbd_wsect;
910
911 if (show_full_name) {
912 length = strlen(xenstat_domain_name(domain));
913 if (length > fields[FIELD_NAME-1].default_width)
914 fields[FIELD_NAME-1].default_width = length;
915 }
916
917 length = INT_FIELD_WIDTH((tot_net_bytes(domain, FALSE)/1024) + 1);
918 if (length > fields[FIELD_NET_TX-1].default_width)
919 fields[FIELD_NET_TX-1].default_width = length;
920
921 length = INT_FIELD_WIDTH((tot_net_bytes(domain, TRUE)/1024) + 1);
922 if (length > fields[FIELD_NET_RX-1].default_width)
923 fields[FIELD_NET_RX-1].default_width = length;
924
925 tot_vbd_reqs(domain, FIELD_VBD_RD, &vbd_rd);
926 length = INT_FIELD_WIDTH(vbd_rd + 1);
927 if (length > fields[FIELD_VBD_RD-1].default_width)
928 fields[FIELD_VBD_RD-1].default_width = length;
929
930 tot_vbd_reqs(domain, FIELD_VBD_WR, &vbd_wr);
931 length = INT_FIELD_WIDTH(vbd_wr + 1);
932 if (length > fields[FIELD_VBD_WR-1].default_width)
933 fields[FIELD_VBD_WR-1].default_width = length;
934
935 tot_vbd_reqs(domain, FIELD_VBD_RSECT, &vbd_rsect);
936 length = INT_FIELD_WIDTH(vbd_rsect + 1);
937 if (length > fields[FIELD_VBD_RSECT-1].default_width)
938 fields[FIELD_VBD_RSECT-1].default_width = length;
939
940 tot_vbd_reqs(domain, FIELD_VBD_WSECT, &vbd_wsect);
941 length = INT_FIELD_WIDTH(vbd_wsect + 1);
942 if (length > fields[FIELD_VBD_WSECT-1].default_width)
943 fields[FIELD_VBD_WSECT-1].default_width = length;
944 }
945
946
947 /* Section printing functions */
948 /* Prints the top summary, above the domain table */
949 void do_summary(void)
950 {
951 #define TIME_STR_LEN 9
952 const char *TIME_STR_FORMAT = "%H:%M:%S";
953 char time_str[TIME_STR_LEN];
954 const char *ver_str;
955 unsigned run = 0, block = 0, pause = 0,
956 crash = 0, dying = 0, shutdown = 0;
957 unsigned i, num_domains = 0;
958 unsigned long long used = 0;
959 xenstat_domain *domain;
960 time_t curt;
961
962 /* Print program name, current time, and number of domains */
963 curt = curtime.tv_sec;
964 strftime(time_str, TIME_STR_LEN, TIME_STR_FORMAT, localtime(&curt));
965 num_domains = xenstat_node_num_domains(cur_node);
966 ver_str = xenstat_node_xen_version(cur_node);
967 print("xentop - %s Xen %s\n", time_str, ver_str);
968
969 /* Tabulate what states domains are in for summary */
970 for (i=0; i < num_domains; i++) {
971 domain = xenstat_node_domain_by_index(cur_node,i);
972 if (xenstat_domain_running(domain)) run++;
973 else if (xenstat_domain_blocked(domain)) block++;
974 else if (xenstat_domain_paused(domain)) pause++;
975 else if (xenstat_domain_shutdown(domain)) shutdown++;
976 else if (xenstat_domain_crashed(domain)) crash++;
977 else if (xenstat_domain_dying(domain)) dying++;
978 }
979
980 print("%u domains: %u running, %u blocked, %u paused, "
981 "%u crashed, %u dying, %u shutdown \n",
982 num_domains, run, block, pause, crash, dying, shutdown);
983
984 used = xenstat_node_tot_mem(cur_node) - xenstat_node_free_mem(cur_node);
985
986 /* Dump node memory and cpu information */
987 print("Mem: %lluk total, %lluk used, %lluk free ",
988 xenstat_node_tot_mem(cur_node)/1024, used/1024,
989 xenstat_node_free_mem(cur_node)/1024);
990
991 print("CPUs: %u @ %lluMHz\n",
992 xenstat_node_num_cpus(cur_node),
993 xenstat_node_cpu_hz(cur_node)/1000000);
994 }
995
996 /* Display the top header for the domain table */
997 void do_header(void)
998 {
999 field_id i;
1000
1001 /* Turn on REVERSE highlight attribute for headings */
1002 xentop_attron(A_REVERSE);
1003 for(i = 0; i < NUM_FIELDS; i++) {
1004 if (i != 0)
1005 print(" ");
1006 /* The BOLD attribute is turned on for the sort column */
1007 if (i == sort_field)
1008 xentop_attron(A_BOLD);
1009 print("%*s", fields[i].default_width, fields[i].header);
1010 if (i == sort_field)
1011 xentop_attroff(A_BOLD);
1012 }
1013 xentop_attroff(A_REVERSE);
1014 print("\n");
1015 }
1016
1017 /* Displays bottom status line or current prompt */
1018 void do_bottom_line(void)
1019 {
1020 move(lines()-1, 2);
1021
1022 if (prompt != NULL) {
1023 printw("%s: %s", prompt, prompt_val);
1024 } else {
1025 addch(A_REVERSE | 'D'); addstr("elay ");
1026
1027 /* network */
1028 addch(A_REVERSE | 'N');
1029 attr_addstr(show_networks ? COLOR_PAIR(1) : 0, "etworks");
1030 addstr(" ");
1031
1032 /* VBDs */
1033 attr_addstr(show_vbds ? COLOR_PAIR(1) : 0, "v");
1034 addch(A_REVERSE | 'B');
1035 attr_addstr(show_vbds ? COLOR_PAIR(1) : 0, "ds");
1036 addstr(" ");
1037
1038 /* vcpus */
1039 addch(A_REVERSE | 'V');
1040 attr_addstr(show_vcpus ? COLOR_PAIR(1) : 0, "CPUs");
1041 addstr(" ");
1042
1043 /* repeat */
1044 addch(A_REVERSE | 'R');
1045 attr_addstr(repeat_header ? COLOR_PAIR(1) : 0, "epeat header");
1046 addstr(" ");
1047
1048 /* sort order */
1049 addch(A_REVERSE | 'S'); addstr("ort order ");
1050
1051 addch(A_REVERSE | 'Q'); addstr("uit ");
1052 }
1053 }
1054
1055 /* Prints Domain information */
1056 void do_domain(xenstat_domain *domain)
1057 {
1058 unsigned int i;
1059 for (i = 0; i < NUM_FIELDS; i++) {
1060 if (i != 0)
1061 print(" ");
1062 if (i == sort_field)
1063 xentop_attron(A_BOLD);
1064 fields[i].print(domain);
1065 if (i == sort_field)
1066 xentop_attroff(A_BOLD);
1067 }
1068 print("\n");
1069 }
1070
1071 /* Output all vcpu information */
1072 void do_vcpu(xenstat_domain *domain)
1073 {
1074 int i = 0;
1075 unsigned num_vcpus = 0;
1076 xenstat_vcpu *vcpu;
1077
1078 print("VCPUs(sec): ");
1079
1080 num_vcpus = xenstat_domain_num_vcpus(domain);
1081
1082 /* for all online vcpus dump out values */
1083 for (i=0; i< num_vcpus; i++) {
1084 vcpu = xenstat_domain_vcpu(domain,i);
1085
1086 if (xenstat_vcpu_online(vcpu) > 0) {
1087 if (i != 0 && (i%5)==0)
1088 print("\n ");
1089 print(" %2u: %10llus", i,
1090 xenstat_vcpu_ns(vcpu)/1000000000);
1091 }
1092 }
1093 print("\n");
1094 }
1095
1096 /* Output all network information */
1097 void do_network(xenstat_domain *domain)
1098 {
1099 int i = 0;
1100 xenstat_network *network;
1101 unsigned num_networks = 0;
1102
1103 /* How many networks? */
1104 num_networks = xenstat_domain_num_networks(domain);
1105
1106 /* Dump information for each network */
1107 for (i=0; i < num_networks; i++) {
1108 /* Next get the network information */
1109 network = xenstat_domain_network(domain,i);
1110
1111 print("Net%d RX: %8llubytes %8llupkts %8lluerr %8lludrop ",
1112 i,
1113 xenstat_network_rbytes(network),
1114 xenstat_network_rpackets(network),
1115 xenstat_network_rerrs(network),
1116 xenstat_network_rdrop(network));
1117
1118 print("TX: %8llubytes %8llupkts %8lluerr %8lludrop\n",
1119 xenstat_network_tbytes(network),
1120 xenstat_network_tpackets(network),
1121 xenstat_network_terrs(network),
1122 xenstat_network_tdrop(network));
1123 }
1124 }
1125
1126
1127 /* Output all VBD information */
1128 void do_vbd(xenstat_domain *domain)
1129 {
1130 int i = 0;
1131 xenstat_vbd *vbd;
1132 unsigned num_vbds = 0;
1133
1134 const char *vbd_type[] = {
1135 "Unidentified", /* number 0 */
1136 "BlkBack", /* number 1 */
1137 "BlkTap", /* number 2 */
1138 "Tapdisk3" /* number 3 */
1139 };
1140
1141 num_vbds = xenstat_domain_num_vbds(domain);
1142
1143 for (i=0 ; i< num_vbds; i++) {
1144 char details[20];
1145
1146 vbd = xenstat_domain_vbd(domain,i);
1147
1148 #if !defined(__linux__)
1149 details[0] = '\0';
1150 #else
1151 snprintf(details, 20, "[%2x:%2x] ",
1152 MAJOR(xenstat_vbd_dev(vbd)),
1153 MINOR(xenstat_vbd_dev(vbd)));
1154 #endif
1155 if (xenstat_vbd_error(vbd))
1156 {
1157 print("VBD %s %4d %s OO: %8c RD: %8c WR: %8c RSECT: %10c WSECT: %10c\n",
1158 vbd_type[xenstat_vbd_type(vbd)],
1159 xenstat_vbd_dev(vbd), details,
1160 '-', '-', '-', '-','-');
1161 }
1162 else
1163 {
1164 print("VBD %s %4d %s OO: %8llu RD: %8llu WR: %8llu RSECT: %10llu WSECT: %10llu\n",
1165 vbd_type[xenstat_vbd_type(vbd)],
1166 xenstat_vbd_dev(vbd), details,
1167 xenstat_vbd_oo_reqs(vbd),
1168 xenstat_vbd_rd_reqs(vbd),
1169 xenstat_vbd_wr_reqs(vbd),
1170 xenstat_vbd_rd_sects(vbd),
1171 xenstat_vbd_wr_sects(vbd));
1172 }
1173 }
1174 }
1175
1176 static void top(void)
1177 {
1178 xenstat_domain **domains;
1179 unsigned int i, num_domains = 0;
1180 int dom0_index = -1;
1181 int sort_start = 0, sort_count = 0;
1182
1183 /* Now get the node information */
1184 if (prev_node != NULL)
1185 xenstat_free_node(prev_node);
1186 prev_node = cur_node;
1187 cur_node = xenstat_get_node(xhandle, XENSTAT_ALL);
1188 if (cur_node == NULL)
1189 fail("Failed to retrieve statistics from libxenstat\n");
1190
1191 /* dump summary top information */
1192 if (!batch)
1193 do_summary();
1194
1195 /* Count the number of domains for which to report data */
1196 num_domains = xenstat_node_num_domains(cur_node);
1197
1198 domains = calloc(num_domains, sizeof(xenstat_domain *));
1199 if(domains == NULL)
1200 fail("Failed to allocate memory\n");
1201
1202 for (i=0; i < num_domains; i++) {
1203 domains[i] = xenstat_node_domain_by_index(cur_node, i);
1204 if ( strcmp(xenstat_domain_name(domains[i]), "Domain-0") == 0 )
1205 dom0_index = i;
1206 }
1207
1208 /* Handle dom0 position, not for dom0-less */
1209 if ( dom0_first == 1 && dom0_index != -1 ){
1210 /* if dom0 is not first in domains, swap it there */
1211 if ( dom0_index != 0 ){
1212 xenstat_domain *tmp;
1213 tmp = domains[0];
1214 domains[0] = domains[dom0_index];
1215 domains[dom0_index] = tmp;
1216 }
1217 sort_start = 1;
1218 sort_count = 1;
1219 }
1220
1221 /* Sort */
1222 qsort((domains+sort_start), (num_domains-sort_count), sizeof(xenstat_domain *),
1223 (int(*)(const void *, const void *))compare_domains);
1224
1225 if(first_domain_index >= num_domains)
1226 first_domain_index = num_domains-1;
1227
1228 /* Adjust default_width for fields with potentially large numbers */
1229 reset_field_widths();
1230 for (i = first_domain_index; i < num_domains; i++) {
1231 adjust_field_widths(domains[i]);
1232 }
1233
1234 for (i = first_domain_index; i < num_domains; i++) {
1235 if(!batch && current_row() == lines()-1)
1236 break;
1237 if (i == first_domain_index || repeat_header)
1238 do_header();
1239 do_domain(domains[i]);
1240 if (show_vcpus)
1241 do_vcpu(domains[i]);
1242 if (show_networks)
1243 do_network(domains[i]);
1244 if (show_vbds)
1245 do_vbd(domains[i]);
1246 }
1247
1248 if (!batch)
1249 do_bottom_line();
1250
1251 free(domains);
1252 }
1253
1254 static int signal_exit;
1255
1256 static void signal_exit_handler(int sig)
1257 {
1258 signal_exit = 1;
1259 }
1260
1261 int main(int argc, char **argv)
1262 {
1263 int opt, optind = 0;
1264 int ch = ERR;
1265
1266 struct option lopts[] = {
1267 { "help", no_argument, NULL, 'h' },
1268 { "version", no_argument, NULL, 'V' },
1269 { "networks", no_argument, NULL, 'n' },
1270 { "vbds", no_argument, NULL, 'x' },
1271 { "repeat-header", no_argument, NULL, 'r' },
1272 { "vcpus", no_argument, NULL, 'v' },
1273 { "delay", required_argument, NULL, 'd' },
1274 { "batch", no_argument, NULL, 'b' },
1275 { "iterations", required_argument, NULL, 'i' },
1276 { "full-name", no_argument, NULL, 'f' },
1277 { "dom0-first", no_argument, NULL, 'z' },
1278 { 0, 0, 0, 0 },
1279 };
1280 const char *sopts = "hVnxrvd:bi:fz";
1281
1282 if (atexit(cleanup) != 0)
1283 fail("Failed to install cleanup handler.\n");
1284
1285 while ((opt = getopt_long(argc, argv, sopts, lopts, &optind)) != -1) {
1286 switch (opt) {
1287 default:
1288 usage(argv[0]);
1289 exit(1);
1290 case '?':
1291 case 'h':
1292 usage(argv[0]);
1293 exit(0);
1294 case 'V':
1295 version();
1296 exit(0);
1297 case 'n':
1298 show_networks = 1;
1299 break;
1300 case 'x':
1301 show_vbds = 1;
1302 break;
1303 case 'r':
1304 repeat_header = 1;
1305 break;
1306 case 'v':
1307 show_vcpus = 1;
1308 break;
1309 case 'd':
1310 delay = atoi(optarg);
1311 break;
1312 case 'b':
1313 batch = 1;
1314 break;
1315 case 'i':
1316 iterations = atoi(optarg);
1317 loop = 0;
1318 break;
1319 case 'f':
1320 show_full_name = 1;
1321 break;
1322 case 'z':
1323 dom0_first = 1;
1324 break;
1325 }
1326 }
1327
1328 /* Get xenstat handle */
1329 xhandle = xenstat_init();
1330 if (xhandle == NULL)
1331 fail("Failed to initialize xenstat library\n");
1332
1333 if (!batch) {
1334 /* Begin curses stuff */
1335 cwin = initscr();
1336 start_color();
1337 cbreak();
1338 noecho();
1339 nonl();
1340 keypad(stdscr, TRUE);
1341 halfdelay(5);
1342 #ifndef __sun__
1343 use_default_colors();
1344 #endif
1345 init_pair(1, -1, COLOR_YELLOW);
1346
1347 do {
1348 gettimeofday(&curtime, NULL);
1349 if(ch != ERR || (curtime.tv_sec - oldtime.tv_sec) >= delay) {
1350 erase();
1351 top();
1352 oldtime = curtime;
1353 refresh();
1354 if ((!loop) && !(--iterations))
1355 break;
1356 }
1357 ch = getch();
1358 } while (handle_key(ch));
1359 } else {
1360 struct sigaction sa = {
1361 .sa_handler = signal_exit_handler,
1362 .sa_flags = 0
1363 };
1364 sigemptyset(&sa.sa_mask);
1365 sigaction(SIGINT, &sa, NULL);
1366 sigaction(SIGTERM, &sa, NULL);
1367
1368 do {
1369 gettimeofday(&curtime, NULL);
1370 top();
1371 fflush(stdout);
1372 oldtime = curtime;
1373 if ((!loop) && !(--iterations))
1374 break;
1375 sleep(delay);
1376 } while (!signal_exit);
1377 }
1378
1379 /* Cleanup occurs in cleanup(), so no work to do here. */
1380
1381 return 0;
1382 }