2 * iostat 2002 Ronnie Sahlberg
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
36 #include "epan/epan_dissect.h"
37 #include "epan/packet_info.h"
39 #include <epan/stat_cmd_args.h>
40 #include <epan/strutil.h>
43 #define CALC_TYPE_FRAMES 0
44 #define CALC_TYPE_BYTES 1
45 #define CALC_TYPE_FRAMES_AND_BYTES 2
46 #define CALC_TYPE_COUNT 3
47 #define CALC_TYPE_SUM 4
48 #define CALC_TYPE_MIN 5
49 #define CALC_TYPE_MAX 6
50 #define CALC_TYPE_AVG 7
51 #define CALC_TYPE_LOAD 8
54 const char *func_name;
58 static calc_type_ent_t calc_type_table[] = {
59 { "FRAMES", CALC_TYPE_FRAMES },
60 { "BYTES", CALC_TYPE_BYTES },
61 { "FRAMES BYTES", CALC_TYPE_FRAMES_AND_BYTES },
62 { "COUNT", CALC_TYPE_COUNT },
63 { "SUM", CALC_TYPE_SUM },
64 { "MIN", CALC_TYPE_MIN },
65 { "MAX", CALC_TYPE_MAX },
66 { "AVG", CALC_TYPE_AVG },
67 { "LOAD", CALC_TYPE_LOAD },
71 typedef struct _io_stat_t {
72 guint64 interval; /* The user-specified time interval (us) */
73 guint invl_prec; /* Decimal precision of the time interval (1=10s, 2=100s etc) */
74 guint32 num_cols; /* The number of columns of statistics in the table */
75 struct _io_stat_item_t
76 *items; /* Each item is a single cell in the table */
77 const char **filters; /* 'io,stat' cmd strings (e.g., "AVG(smb.time)smb.time") */
78 guint64 *max_vals; /* The max value sans the decimal or nsecs portion in each stat column */
79 guint32 *max_frame; /* The max frame number displayed in each stat column */
82 typedef struct _io_stat_item_t {
84 struct _io_stat_item_t *next;
85 struct _io_stat_item_t *prev;
86 guint64 time; /* Time since start of capture (us)*/
87 int calc_type; /* The statistic type */
88 int colnum; /* Column number of this stat (0 to n) */
91 guint32 num; /* The sample size of a given statistic (only needed for AVG) */
92 guint64 counter; /* The accumulated data for the calculation of that statistic */
94 gdouble double_counter;
97 #define NANOSECS_PER_SEC 1000000000
100 iostat_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt, const void *dummy _U_)
105 guint64 relative_time, rt;
111 mit = (io_stat_item_t *) arg;
112 parent = mit->parent;
113 relative_time = (guint64)((pinfo->fd->rel_ts.secs*1000000) + ((pinfo->fd->rel_ts.nsecs+500)/1000));
115 /* The prev item before the main one is always the last interval we saw packets for */
118 /* XXX for the time being, just ignore all frames that are in the past.
119 should be fixed in the future but hopefully it is uncommon */
120 if(relative_time < it->time){
124 /* If we have moved into a new interval (row), create a new io_stat_item_t struct for every interval
125 * between the last struct and this one. If an item was not found in a previous interval, an empty
126 * struct will be created for it. */
128 while (rt >= it->time + parent->interval) {
129 it->next = (io_stat_item_t *)g_malloc(sizeof(io_stat_item_t));
131 it->next->next = NULL;
135 it->time = it->prev->time + parent->interval;
138 it->float_counter = 0;
139 it->double_counter = 0;
141 it->calc_type = it->prev->calc_type;
142 it->hf_index = it->prev->hf_index;
143 it->colnum = it->prev->colnum;
146 /* Store info in the current structure */
149 switch(it->calc_type) {
150 case CALC_TYPE_FRAMES:
151 case CALC_TYPE_BYTES:
152 case CALC_TYPE_FRAMES_AND_BYTES:
153 it->counter += pinfo->fd->pkt_len;
155 case CALC_TYPE_COUNT:
156 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
158 it->counter += gp->len;
162 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
166 for(i=0;i<gp->len;i++){
167 switch(proto_registrar_get_ftype(it->hf_index)){
172 it->counter += fvalue_get_uinteger(&((field_info *)gp->pdata[i])->value);
175 it->counter += fvalue_get_integer64(&((field_info *)gp->pdata[i])->value);
181 it->counter += fvalue_get_sinteger(&((field_info *)gp->pdata[i])->value);
184 it->counter += (gint64)fvalue_get_integer64(&((field_info *)gp->pdata[i])->value);
188 (gfloat)fvalue_get_floating(&((field_info *)gp->pdata[i])->value);
191 it->double_counter += fvalue_get_floating(&((field_info *)gp->pdata[i])->value);
193 case FT_RELATIVE_TIME:
194 new_time = fvalue_get(&((field_info *)gp->pdata[i])->value);
195 val = (guint64)((new_time->secs * NANOSECS_PER_SEC) + new_time->nsecs);
203 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
209 ftype=proto_registrar_get_ftype(it->hf_index);
210 for(i=0;i<gp->len;i++){
216 val = fvalue_get_uinteger(&((field_info *)gp->pdata[i])->value);
217 if ((it->frames==1 && i==0) || (val < it->counter)) {
222 val = fvalue_get_integer64(&((field_info *)gp->pdata[i])->value);
223 if((it->frames==1 && i==0) || (val < it->counter)){
231 val = fvalue_get_sinteger(&((field_info *)gp->pdata[i])->value);
232 if((it->frames==1 && i==0) || ((gint32)val < (gint32)it->counter)) {
237 val = fvalue_get_integer64(&((field_info *)gp->pdata[i])->value);
238 if((it->frames==1 && i==0) || ((gint64)val < (gint64)it->counter)) {
243 float_val=(gfloat)fvalue_get_floating(&((field_info *)gp->pdata[i])->value);
244 if((it->frames==1 && i==0) || (float_val < it->float_counter)) {
245 it->float_counter=float_val;
249 double_val=fvalue_get_floating(&((field_info *)gp->pdata[i])->value);
250 if((it->frames==1 && i==0) || (double_val < it->double_counter)) {
251 it->double_counter=double_val;
254 case FT_RELATIVE_TIME:
255 new_time = (nstime_t *)fvalue_get(&((field_info *)gp->pdata[i])->value);
256 val = (guint64)new_time->secs * NANOSECS_PER_SEC + new_time->nsecs;
257 if((it->frames==1 && i==0) || (val < it->counter)) {
266 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
272 ftype=proto_registrar_get_ftype(it->hf_index);
273 for(i=0;i<gp->len;i++){
279 val = fvalue_get_uinteger(&((field_info *)gp->pdata[i])->value);
280 if(val > it->counter)
284 val = fvalue_get_integer64(&((field_info *)gp->pdata[i])->value);
285 if(val > it->counter)
292 val = fvalue_get_sinteger(&((field_info *)gp->pdata[i])->value);
293 if((gint32)val > (gint32)it->counter)
297 val = fvalue_get_integer64(&((field_info *)gp->pdata[i])->value);
298 if ((gint64)val > (gint64)it->counter)
302 float_val = (gfloat)fvalue_get_floating(&((field_info *)gp->pdata[i])->value);
303 if(float_val > it->float_counter)
304 it->float_counter=float_val;
307 double_val = fvalue_get_floating(&((field_info *)gp->pdata[i])->value);
308 if(double_val > it->double_counter)
309 it->double_counter=double_val;
311 case FT_RELATIVE_TIME:
312 new_time = (nstime_t *)fvalue_get(&((field_info *)gp->pdata[i])->value);
313 val = (guint64)((new_time->secs * NANOSECS_PER_SEC) + new_time->nsecs);
322 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
326 ftype=proto_registrar_get_ftype(it->hf_index);
327 for(i=0;i<gp->len;i++){
334 val = fvalue_get_uinteger(&((field_info *)gp->pdata[i])->value);
339 val = fvalue_get_integer64(&((field_info *)gp->pdata[i])->value);
346 val = fvalue_get_sinteger(&((field_info *)gp->pdata[i])->value);
350 it->float_counter += (gfloat)fvalue_get_floating(&((field_info *)gp->pdata[i])->value);
353 it->double_counter += fvalue_get_floating(&((field_info *)gp->pdata[i])->value);
355 case FT_RELATIVE_TIME:
356 new_time = (nstime_t *)fvalue_get(&((field_info *)gp->pdata[i])->value);
357 val = (guint64)((new_time->secs * NANOSECS_PER_SEC) + new_time->nsecs);
365 gp = proto_get_finfo_ptr_array(edt->tree, it->hf_index);
367 ftype = proto_registrar_get_ftype(it->hf_index);
368 if (ftype != FT_RELATIVE_TIME) {
370 "\ntshark: LOAD() is only supported for relative-time fields such as smb.time\n");
373 for(i=0;i<gp->len;i++){
378 new_time = (nstime_t *)fvalue_get(&((field_info *)gp->pdata[i])->value);
379 val = (guint64)((new_time->secs*1000000) + (new_time->nsecs/1000));
380 tival = (int)(val % parent->interval);
381 it->counter += tival;
385 if (val < (guint64)parent->interval) {
390 pit->counter += parent->interval;
391 val -= parent->interval;
398 /* Store the highest value for this item in order to determine the width of each stat column.
399 * For real numbers we only need to know its magnitude (the value to the left of the decimal point
400 * so round it up before storing it as an integer in max_vals. For AVG of RELATIVE_TIME fields,
401 * calc the average, round it to the next second and store the seconds. For all other calc types
402 * of RELATIVE_TIME fields, store the counters without modification.
404 switch(it->calc_type) {
405 case CALC_TYPE_FRAMES:
406 case CALC_TYPE_FRAMES_AND_BYTES:
407 parent->max_frame[it->colnum] =
408 MAX(parent->max_frame[it->colnum], it->frames);
409 if (it->calc_type==CALC_TYPE_FRAMES_AND_BYTES)
410 parent->max_vals[it->colnum] =
411 MAX(parent->max_vals[it->colnum], it->counter);
413 case CALC_TYPE_BYTES:
414 case CALC_TYPE_COUNT:
416 parent->max_vals[it->colnum] = MAX(parent->max_vals[it->colnum], it->counter);
421 ftype=proto_registrar_get_ftype(it->hf_index);
424 parent->max_vals[it->colnum] =
425 MAX(parent->max_vals[it->colnum], (guint64)(it->float_counter+0.5));
428 parent->max_vals[it->colnum] =
429 MAX(parent->max_vals[it->colnum],(guint64)(it->double_counter+0.5));
431 case FT_RELATIVE_TIME:
432 parent->max_vals[it->colnum] =
433 MAX(parent->max_vals[it->colnum], it->counter);
436 /* UINT16-64 and INT8-64 */
437 parent->max_vals[it->colnum] =
438 MAX(parent->max_vals[it->colnum], it->counter);
443 ftype=proto_registrar_get_ftype(it->hf_index);
446 parent->max_vals[it->colnum] =
447 MAX(parent->max_vals[it->colnum], (guint64)it->float_counter/it->num);
450 parent->max_vals[it->colnum] =
451 MAX(parent->max_vals[it->colnum],(guint64)it->double_counter/it->num);
453 case FT_RELATIVE_TIME:
454 parent->max_vals[it->colnum] =
455 MAX(parent->max_vals[it->colnum], ((it->counter/it->num) + 500000000) / NANOSECS_PER_SEC);
458 /* UINT16-64 and INT8-64 */
459 parent->max_vals[it->colnum] =
460 MAX(parent->max_vals[it->colnum], it->counter/it->num);
468 magnitude (guint64 val, int max_w)
472 for (i=0; i<max_w; i++) {
481 * Print the calc_type_table[] function label centered in the column header.
484 printcenter (const char *label, int lenval, int numpad)
486 int lenlab = (int) strlen(label), len;
487 const char spaces[]=" ", *spaces_ptr;
489 len = (int) (strlen(spaces)) - (((lenval-lenlab) / 2) + numpad);
490 if (len > 0 && len < 6) {
491 spaces_ptr = &spaces[len];
492 if ((lenval-lenlab)%2==0) {
493 printf("%s%s%s|", spaces_ptr, label, spaces_ptr);
495 printf("%s%s%s|", spaces_ptr-1, label, spaces_ptr);
497 } else if (len > 0 && len <= 15) {
498 printf("%s|", label);
503 int fr; /* Width of this FRAMES column sans padding and border chars */
504 int val; /* Width of this non-FRAMES column sans padding and border chars */
508 iostat_draw(void *arg)
511 guint64 interval, duration, t, invl_end;
512 int i, j, k, num_cols, num_rows, dv, dur_secs, dur_mag, invl_mag, invl_prec, tabrow_w,
513 borderlen, invl_col_w, numpad=1, namelen, len_filt, type, maxfltr_w, ftype;
514 int fr_mag; /* The magnitude of the max frame number in this column */
515 int val_mag; /* The magnitude of the max value in this column */
516 gboolean last_row=FALSE;
517 char *spaces, *spaces_s, *filler_s=NULL, **fmts, *fmt=NULL;
519 static gchar dur_mag_s[3], invl_mag_s[3], invl_prec_s[3], fr_mag_s[3], val_mag_s[3], *invl_fmt, *full_fmt;
520 io_stat_item_t *mit, **stat_cols, *item;
524 mit = (io_stat_item_t *)arg;
526 num_cols = iot->num_cols;
527 col_w = (column_width *)g_malloc(sizeof(column_width) * num_cols);
528 fmts = (char **)g_malloc(sizeof(char *) * num_cols);
529 duration = (guint64)((cfile.elapsed_time.secs*1000000) + ((cfile.elapsed_time.nsecs+500)/1000));
531 /* Store the pointer to each stat column */
532 stat_cols = (io_stat_item_t **) g_malloc(sizeof(io_stat_item_t *) * num_cols);
533 for (j=0; j<num_cols; j++)
534 stat_cols[j] = &iot->items[j];
536 /* The following prevents gross inaccuracies when the user specifies an interval that is greater
537 * than the capture duration. */
538 if (iot->interval > duration || iot->interval==G_MAXINT32) {
540 iot->interval = G_MAXINT32;
542 interval = iot->interval;
545 /* Calc the capture duration's magnitude (dur_mag) */
546 dur_secs = (int)duration/1000000;
547 dur_mag = magnitude((guint64)dur_secs, 5);
548 g_snprintf(dur_mag_s, 3, "%u", dur_mag);
550 /* Calc the interval's magnitude */
551 invl_mag = magnitude((guint64)interval/1000000, 5);
553 /* Set or get the interval precision */
554 if (interval==duration) {
556 * An interval arg of 0 or an interval size exceeding the capture duration was specified.
557 * Set the decimal precision of duration based on its magnitude. */
565 borderlen = 30 + dur_mag + (invl_prec==0 ? 0 : invl_prec+1);
567 invl_prec = iot->invl_prec;
568 borderlen = 24 + invl_mag + (invl_prec==0 ? 0 : invl_prec+1);
571 /* Round the duration according to invl_prec */
573 for (i=0; i<invl_prec; i++)
575 duration = duration + (5*(dv/10));
576 if (iot->interval==G_MAXINT32)
579 /* Recalc the dur_mag in case rounding has increased its magnitude */
580 dur_secs = (int)duration/1000000;
581 dur_mag = magnitude((guint64)dur_secs, 5);
583 /* Calc the width of the time interval column (incl borders and padding). */
585 invl_col_w = (2*dur_mag) + 8;
587 invl_col_w = (2*dur_mag) + (2*invl_prec) + 10;
588 invl_col_w = MAX(invl_col_w, 12);
589 borderlen = MAX(borderlen, invl_col_w);
591 /* Calc the total width of each row in the stats table and build the printf format string for each
592 * column based on its field type, width, and name length.
593 * NOTE: The magnitude of all types including float and double are stored in iot->max_vals which
594 * is an *integer*. */
595 tabrow_w = invl_col_w;
596 for (j=0; j<num_cols; j++) {
597 type = iot->items[j].calc_type;
598 if (type==CALC_TYPE_FRAMES_AND_BYTES) {
601 namelen = (int) strlen(calc_type_table[type].func_name);
603 if(type==CALC_TYPE_FRAMES
604 || type==CALC_TYPE_FRAMES_AND_BYTES) {
606 fr_mag = magnitude(iot->max_frame[j], 15);
607 fr_mag = MAX(6, fr_mag);
608 col_w[j].fr = fr_mag;
609 tabrow_w += col_w[j].fr + 3;
610 g_snprintf(fr_mag_s, 3, "%u", fr_mag);
612 if (type==CALC_TYPE_FRAMES) {
613 fmt = g_strconcat(" %", fr_mag_s, "u |", NULL);
615 /* CALC_TYPE_FRAMES_AND_BYTES
617 val_mag = magnitude(iot->max_vals[j], 15);
618 val_mag = MAX(5, val_mag);
619 col_w[j].val = val_mag;
620 tabrow_w += (col_w[j].val + 3);
621 g_snprintf(val_mag_s, 3, "%u", val_mag);
622 fmt = g_strconcat(" %", fr_mag_s, "u |", " %", val_mag_s, G_GINT64_MODIFIER, "u |", NULL);
629 case CALC_TYPE_BYTES:
630 case CALC_TYPE_COUNT:
632 val_mag = magnitude(iot->max_vals[j], 15);
633 val_mag = MAX(5, val_mag);
634 col_w[j].val = val_mag;
635 g_snprintf(val_mag_s, 3, "%u", val_mag);
636 fmt = g_strconcat(" %", val_mag_s, G_GINT64_MODIFIER, "u |", NULL);
640 ftype = proto_registrar_get_ftype(stat_cols[j]->hf_index);
644 val_mag = magnitude(iot->max_vals[j], 15);
645 g_snprintf(val_mag_s, 3, "%u", val_mag);
646 fmt = g_strconcat(" %", val_mag_s, ".6f |", NULL);
647 col_w[j].val = val_mag + 7;
649 case FT_RELATIVE_TIME:
650 /* Convert FT_RELATIVE_TIME field to seconds
651 * CALC_TYPE_LOAD was already converted in iostat_packet() ) */
652 if (type==CALC_TYPE_LOAD) {
653 iot->max_vals[j] /= interval;
655 iot->max_vals[j] = (iot->max_vals[j] + 500000000) / NANOSECS_PER_SEC;
657 val_mag = magnitude(iot->max_vals[j], 15);
658 g_snprintf(val_mag_s, 3, "%u", val_mag);
659 fmt = g_strconcat(" %", val_mag_s, "u.%06u |", NULL);
660 col_w[j].val = val_mag + 7;
664 val_mag = magnitude(iot->max_vals[j], 15);
665 val_mag = MAX(namelen, val_mag);
666 col_w[j].val = val_mag;
667 g_snprintf(val_mag_s, 3, "%u", val_mag);
675 fmt = g_strconcat(" %", val_mag_s, G_GINT64_MODIFIER, "u |", NULL);
682 fmt = g_strconcat(" %", val_mag_s, G_GINT64_MODIFIER, "d |", NULL);
685 } /* End of ftype switch */
686 } /* End of calc_type switch */
687 tabrow_w += col_w[j].val + 3;
690 } /* End of for loop (columns) */
692 borderlen = MAX(borderlen, tabrow_w);
694 /* Calc the max width of the list of filters. */
696 for(j=0; j<num_cols; j++) {
697 if (iot->filters[j]) {
698 k = (int) (strlen(iot->filters[j]) + 11);
699 maxfltr_w = MAX(maxfltr_w, k);
701 maxfltr_w = MAX(maxfltr_w, 26);
704 /* The stat table is not wrapped (by tshark) but filter is wrapped at the width of the stats table
705 * (which currently = borderlen); however, if the filter width exceeds the table width and the
706 * table width is less than 102 bytes, set borderlen to the lesser of the max filter width and 102.
707 * The filters will wrap at the lesser of borderlen-2 and the last space in the filter.
708 * NOTE: 102 is the typical size of a user window when the font is fixed width (e.g., COURIER 10).
709 * XXX: A pref could be added to change the max width from the default size of 102. */
710 if (maxfltr_w > borderlen && borderlen < 102)
711 borderlen = MIN(maxfltr_w, 102);
713 /* Prevent double right border by adding a space */
714 if (borderlen-tabrow_w==1)
717 /* Display the top border */
719 for (i=0; i<borderlen; i++)
722 spaces = (char*) g_malloc(borderlen+1);
723 for (i=0; i<borderlen; i++)
725 spaces[borderlen] = '\0';
727 spaces_s = &spaces[16];
728 printf("\n| IO Statistics%s|\n", spaces_s);
729 spaces_s = &spaces[2];
730 printf("|%s|\n", spaces_s);
732 g_snprintf(invl_mag_s, 3, "%u", invl_mag);
734 g_snprintf(invl_prec_s, 3, "%u", invl_prec);
735 invl_fmt = g_strconcat("%", invl_mag_s, "u.%0", invl_prec_s, "u", NULL);
736 if (interval==duration) {
737 full_fmt = g_strconcat("| Interval size: ", invl_fmt, " secs (dur)%s", NULL);
738 spaces_s = &spaces[30+invl_mag+invl_prec];
740 full_fmt = g_strconcat("| Interval size: ", invl_fmt, " secs%s", NULL);
741 spaces_s = &spaces[24+invl_mag+invl_prec];
743 printf(full_fmt, (guint32)interval/1000000,
744 (guint32)((interval%1000000)/dv), spaces_s);
746 invl_fmt = g_strconcat("%", invl_mag_s, "u", NULL);
747 full_fmt = g_strconcat("| Interval size: ", invl_fmt, " secs%s", NULL);
748 spaces_s = &spaces[23 + invl_mag];
749 printf(full_fmt, (guint32)interval/1000000, spaces_s);
755 invl_fmt = g_strconcat("%", dur_mag_s, "u.%0", invl_prec_s, "u", NULL);
757 invl_fmt = g_strconcat("%", dur_mag_s, "u", NULL);
759 /* Display the list of filters and their column numbers vertically */
761 for(j=0; j<num_cols; j++){
762 printf((j==0 ? "%2u: " : "| %2u: "), j+1);
763 if (!iot->filters[j] || (iot->filters[j]==0)) {
765 * An empty (no filter) comma field was specified */
766 spaces_s = &spaces[16 + 10];
767 printf("Frames and bytes%s|\n", spaces_s);
769 filter = iot->filters[j];
770 len_filt = (int) strlen(filter);
772 /* If the width of the widest filter exceeds the width of the stat table, borderlen has
773 * been set to 102 bytes above and filters wider than 102 will wrap at 91 bytes. */
774 if (len_filt+11 <= borderlen) {
775 printf("%s", filter);
776 if (len_filt+11 <= borderlen) {
777 spaces_s = &spaces[len_filt + 10];
778 printf("%s", spaces_s);
782 gchar *sfilter1, *sfilter2;
785 int next_start, max_w=borderlen-11;
788 if (len_filt > max_w) {
789 sfilter1 = g_strndup( (gchar *) filter, (gsize) max_w);
791 * Find the pos of the last space in sfilter1. If a space is found, set
792 * sfilter2 to the string prior to that space and print it; otherwise, wrap
793 * the filter at max_w. */
794 pos = g_strrstr(sfilter1, " ");
796 len = (gsize)(pos-sfilter1);
797 next_start = (int) len+1;
799 len = (gsize) strlen(sfilter1);
800 next_start = (int)len;
802 sfilter2 = g_strndup(sfilter1, len);
803 printf("%s%s|\n", sfilter2, &spaces[len+10]);
808 filter = &filter[next_start];
809 len_filt = (int) strlen(filter);
811 printf("%s%s|\n", filter, &spaces[((int)strlen(filter))+10]);
820 for(i=0;i<borderlen-3;i++){
825 /* Display spaces above "Interval (s)" label */
826 spaces_s = &spaces[borderlen-(invl_col_w-2)];
827 printf("|%s|", spaces_s);
829 /* Display column number headers */
830 for(j=0; j<num_cols; j++) {
832 if(item->calc_type==CALC_TYPE_FRAMES_AND_BYTES)
833 spaces_s = &spaces[borderlen - (col_w[j].fr + col_w[j].val)] - 3;
834 else if (item->calc_type==CALC_TYPE_FRAMES)
835 spaces_s = &spaces[borderlen - col_w[j].fr];
837 spaces_s = &spaces[borderlen - col_w[j].val];
839 printf("%-2u%s|", j+1, spaces_s);
841 if (tabrow_w < borderlen) {
842 filler_s = &spaces[tabrow_w+1];
843 printf("%s|", filler_s);
846 printf("\n| Interval");
847 spaces_s = &spaces[borderlen-(invl_col_w-11)];
848 printf("%s|", spaces_s);
850 /* Display the stat label in each column */
851 for(j=0; j<num_cols; j++) {
852 type = stat_cols[j]->calc_type;
853 if(type==CALC_TYPE_FRAMES) {
854 printcenter (calc_type_table[type].func_name, col_w[j].fr, numpad);
855 } else if (type==CALC_TYPE_FRAMES_AND_BYTES) {
856 printcenter ("Frames", col_w[j].fr, numpad);
857 printcenter ("Bytes", col_w[j].val, numpad);
859 printcenter (calc_type_table[type].func_name, col_w[j].val, numpad);
863 printf("%s|", filler_s);
866 for(i=0; i<tabrow_w-3; i++)
870 if (tabrow_w < borderlen)
871 printf("%s|", &spaces[tabrow_w+1]);
875 full_fmt = g_strconcat("| ", invl_fmt, " <> ", invl_fmt, " |", NULL);
876 num_rows = (int)(duration/interval) + (((duration%interval+500000)/1000000) > 0 ? 1 : 0);
878 /* Display the table values.
879 * The outer loop is for time interval rows and the inner loop is for stat column items.*/
880 for (i=0; i<num_rows; i++) {
885 /* Display the interval for this row */
887 invl_end = t + interval;
892 printf(full_fmt, (guint32)(t/1000000),
893 (guint32)(invl_end/1000000));
895 printf(full_fmt, (guint32)(t/1000000),
896 (guint32)(t%1000000) / dv,
897 (guint32) (invl_end/1000000),
898 (guint32)((invl_end%1000000) / dv));
901 /* Display all the stat values in this row */
902 for (j=0; j<num_cols; j++) {
904 * Point to the list for this stat (column). */
907 * Point to the item in the current row (time interval i) within this list. */
909 if (item && item->next)
916 switch(item->calc_type) {
917 case CALC_TYPE_FRAMES:
918 printf(fmt, item->frames);
920 case CALC_TYPE_BYTES:
921 case CALC_TYPE_COUNT:
922 printf(fmt, item->counter);
924 case CALC_TYPE_FRAMES_AND_BYTES:
925 printf(fmt, item->frames, item->counter);
931 ftype = proto_registrar_get_ftype(stat_cols[j]->hf_index);
934 printf(fmt, item->float_counter);
937 printf(fmt, item->double_counter);
939 case FT_RELATIVE_TIME:
940 item->counter = (item->counter + 500) / 1000;
941 printf(fmt, (int)(item->counter/1000000), (int)(item->counter%1000000));
944 printf(fmt, item->counter);
953 ftype = proto_registrar_get_ftype(stat_cols[j]->hf_index);
956 printf(fmt, item->float_counter/num);
959 printf(fmt, item->double_counter/num);
961 case FT_RELATIVE_TIME:
962 item->counter = ((item->counter/num) + 500) / 1000;
964 (int)(item->counter/1000000), (int)(item->counter%1000000));
967 printf(fmt, item->counter/num);
973 ftype = proto_registrar_get_ftype(stat_cols[j]->hf_index);
975 case FT_RELATIVE_TIME:
978 (int) (item->counter/interval),
979 (int)((item->counter%interval)*1000000 / interval));
982 (int) (item->counter/(invl_end-t)),
983 (int)((item->counter%(invl_end-t))*1000000 / (invl_end-t)));
994 printf(fmt, (guint64)0);
998 printf("%s|", filler_s);
1003 for(i=0;i<borderlen;i++){
1015 register_io_tap(io_stat_t *io, int i, const char *filter)
1017 GString *error_string;
1021 const char *p, *parenp;
1023 header_field_info *hfi;
1025 io->items[i].prev=&io->items[i];
1026 io->items[i].next=NULL;
1027 io->items[i].parent=io;
1028 io->items[i].time=0;
1029 io->items[i].calc_type=CALC_TYPE_FRAMES_AND_BYTES;
1030 io->items[i].frames=0;
1031 io->items[i].counter=0;
1034 io->filters[i]=filter;
1039 for(j=0; calc_type_table[j].func_name; j++){
1040 namelen=strlen(calc_type_table[j].func_name);
1041 if(filter && strncmp(filter, calc_type_table[j].func_name, namelen) == 0) {
1042 io->items[i].calc_type=calc_type_table[j].calc_type;
1043 io->items[i].colnum = i;
1044 if(*(filter+namelen)=='(') {
1046 parenp=strchr(p, ')');
1049 "\ntshark: Closing parenthesis missing from calculated expression.\n");
1053 if(io->items[i].calc_type==CALC_TYPE_FRAMES || io->items[i].calc_type==CALC_TYPE_BYTES){
1056 "\ntshark: %s does not require or allow a field name within the parens.\n",
1057 calc_type_table[j].func_name);
1062 /* bail out if a field name was not specified */
1063 fprintf(stderr, "\ntshark: You didn't specify a field name for %s(*).\n",
1064 calc_type_table[j].func_name);
1069 field = (char *) g_malloc(parenp-p+1);
1070 memcpy(field, p, parenp-p);
1071 field[parenp-p] = '\0';
1073 if (io->items[i].calc_type==CALC_TYPE_FRAMES || io->items[i].calc_type==CALC_TYPE_BYTES)
1075 hfi=proto_registrar_get_byname(field);
1077 fprintf(stderr, "\ntshark: There is no field named '%s'.\n",
1083 io->items[i].hf_index=hfi->id;
1087 if (io->items[i].calc_type==CALC_TYPE_FRAMES || io->items[i].calc_type==CALC_TYPE_BYTES)
1089 io->items[i].colnum = i;
1092 if(hfi && !(io->items[i].calc_type==CALC_TYPE_BYTES ||
1093 io->items[i].calc_type==CALC_TYPE_FRAMES ||
1094 io->items[i].calc_type==CALC_TYPE_FRAMES_AND_BYTES)){
1095 /* check that the type is compatible */
1107 /* these types support all calculations */
1111 /* these types only support SUM, COUNT, MAX, MIN, AVG */
1112 switch(io->items[i].calc_type){
1114 case CALC_TYPE_COUNT:
1121 "\ntshark: %s is a float field, so %s(*) calculations are not supported on it.",
1123 calc_type_table[j].func_name);
1127 case FT_RELATIVE_TIME:
1128 /* this type only supports SUM, COUNT, MAX, MIN, AVG, LOAD */
1129 switch(io->items[i].calc_type){
1131 case CALC_TYPE_COUNT:
1135 case CALC_TYPE_LOAD:
1139 "\ntshark: %s is a relative-time field, so %s(*) calculations are not supported on it.",
1141 calc_type_table[j].func_name);
1147 * XXX - support all operations on floating-point
1150 if(io->items[i].calc_type!=CALC_TYPE_COUNT){
1152 "\ntshark: %s doesn't have integral values, so %s(*) "
1153 "calculations are not supported on it.\n",
1155 calc_type_table[j].func_name);
1163 error_string=register_tap_listener("frame", &io->items[i], flt, TL_REQUIRES_PROTO_TREE, NULL,
1164 iostat_packet, i?NULL:iostat_draw);
1168 fprintf(stderr, "\ntshark: Couldn't register io,stat tap: %s\n",
1170 g_string_free(error_string, TRUE);
1176 iostat_init(const char *optarg, void* userdata _U_)
1178 gdouble interval_float;
1181 const gchar *filters=NULL, *str, *pos;
1183 if (sscanf(optarg, "io,stat,%lf,%n", &interval_float, (int *)&idx)==0) {
1184 fprintf(stderr, "\ntshark: invalid \"-z io,stat,<interval>[,<filter>]\" argument\n");
1188 io = (io_stat_t *) g_malloc(sizeof(io_stat_t));
1190 /* If interval is 0, calculate statistics over the whole file by setting the interval to
1192 if (interval_float==0) {
1193 io->interval = G_MAXINT32;
1196 /* Set interval to the number of us rounded to the nearest integer */
1197 io->interval = (gint64)(interval_float*1000000.0+0.5);
1199 * Determine what interval precision the user has specified */
1201 for (i=10; i<10000000; i*=10) {
1202 if (io->interval%i > 0)
1207 if (io->interval < 1){
1209 "\ntshark: \"-z\" interval must be >=0.000001 seconds or \"0\" for the entire capture duration.\n");
1213 /* Find how many ',' separated filters we have */
1217 filters = optarg + idx;
1218 if (strlen(filters) > 0 ) {
1220 while((str = strchr(str, ','))) {
1229 io->items = (io_stat_item_t *) g_malloc(sizeof(io_stat_item_t) * io->num_cols);
1230 io->filters = g_malloc(sizeof(char *) * io->num_cols);
1231 io->max_vals = (guint64 *) g_malloc(sizeof(guint64) * io->num_cols);
1232 io->max_frame = (guint32 *) g_malloc(sizeof(guint32) * io->num_cols);
1234 for (i=0; i<io->num_cols; i++) {
1235 io->max_vals[i] = 0;
1236 io->max_frame[i] = 0;
1239 /* Register a tap listener for each filter */
1240 if((!filters) || (filters[0]==0)) {
1241 register_io_tap(io, 0, NULL);
1247 pos = (gchar*) strchr(str, ',');
1249 register_io_tap(io, i, NULL);
1250 } else if (pos==NULL) {
1251 str = (char*) g_strstrip((gchar*)str);
1252 filter = g_strdup((gchar*) str);
1254 register_io_tap(io, i, filter);
1256 register_io_tap(io, i, NULL);
1258 filter = g_malloc((pos-str)+1);
1259 g_strlcpy( filter, str, (gsize) ((pos-str)+1));
1260 filter = g_strstrip(filter);
1261 register_io_tap(io, i, (char *) filter);
1270 register_tap_listener_iostat(void)
1272 register_stat_cmd_arg("io,stat,", iostat_init, NULL);