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 typedef struct _io_stat_t {
44 gint64 interval; /* unit is us */
46 struct _io_stat_item_t *items;
50 #define CALC_TYPE_FRAMES 0
51 #define CALC_TYPE_BYTES 1
52 #define CALC_TYPE_FRAMES_AND_BYTES 2
53 #define CALC_TYPE_COUNT 3
54 #define CALC_TYPE_SUM 4
55 #define CALC_TYPE_MIN 5
56 #define CALC_TYPE_MAX 6
57 #define CALC_TYPE_AVG 7
59 typedef struct _io_stat_item_t {
61 struct _io_stat_item_t *next;
62 struct _io_stat_item_t *prev;
63 gint64 time; /* unit is us since start of capture */
71 #define NANOSECS_PER_SEC 1000000000
74 iostat_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt, const void *dummy _U_)
76 io_stat_item_t *mit = arg;
82 current_time = (gint64)(pinfo->fd->rel_ts.secs*1000000) + (pinfo->fd->rel_ts.nsecs+500)/1000;
84 /* the prev item before the main one is always the last interval we saw packets for */
87 /* XXX for the time being, just ignore all frames that are in the past.
88 should be fixed in the future but hopefully it is uncommon */
89 if(current_time<it->time){
93 /* we have moved into a new interval, we need to create a new struct */
94 if(current_time>=(it->time+mit->parent->interval)){
95 it->next=g_malloc(sizeof(io_stat_item_t));
101 it->time=(current_time / mit->parent->interval) * mit->parent->interval;
105 it->calc_type=it->prev->calc_type;
106 it->hf_index=it->prev->hf_index;
109 /* it will now give us the current structure to use to store the data in */
112 switch(it->calc_type){
113 case CALC_TYPE_BYTES:
114 case CALC_TYPE_FRAMES:
115 case CALC_TYPE_FRAMES_AND_BYTES:
116 it->counter+=pinfo->fd->pkt_len;
118 case CALC_TYPE_COUNT:
119 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
121 it->counter+=gp->len;
125 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
130 for(i=0;i<gp->len;i++){
131 switch(proto_registrar_get_ftype(it->hf_index)){
136 it->counter+=fvalue_get_uinteger(&((field_info *)gp->pdata[i])->value);
139 it->counter+=fvalue_get_integer64(&((field_info *)gp->pdata[i])->value);
145 it->counter+=fvalue_get_sinteger(&((field_info *)gp->pdata[i])->value);
148 it->counter+=(gint64)fvalue_get_integer64(&((field_info *)gp->pdata[i])->value);
150 case FT_RELATIVE_TIME:
151 new_time = fvalue_get(&((field_info *)gp->pdata[i])->value);
152 val=(guint64)(new_time->secs) * NANOSECS_PER_SEC + new_time->nsecs;
160 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
166 type=proto_registrar_get_ftype(it->hf_index);
167 for(i=0;i<gp->len;i++){
173 val=fvalue_get_uinteger(&((field_info *)gp->pdata[i])->value);
174 if((it->frames==1)&&(i==0)){
176 } else if(val<it->counter){
181 val=fvalue_get_integer64(&((field_info *)gp->pdata[i])->value);
182 if((it->frames==1)&&(i==0)){
184 } else if(val<it->counter){
192 val=fvalue_get_sinteger(&((field_info *)gp->pdata[i])->value);
193 if((it->frames==1)&&(i==0)){
195 } else if((gint32)val<(gint32)(it->counter)){
200 val=fvalue_get_integer64(&((field_info *)gp->pdata[i])->value);
201 if((it->frames==1)&&(i==0)){
203 } else if((gint64)val<(gint64)(it->counter)){
207 case FT_RELATIVE_TIME:
208 new_time=fvalue_get(&((field_info *)gp->pdata[i])->value);
209 val=(guint64)(new_time->secs) * NANOSECS_PER_SEC + new_time->nsecs;
210 if((it->frames==1)&&(i==0)){
212 } else if(val<it->counter){
221 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
227 type=proto_registrar_get_ftype(it->hf_index);
228 for(i=0;i<gp->len;i++){
234 val=fvalue_get_uinteger(&((field_info *)gp->pdata[i])->value);
235 if((it->frames==1)&&(i==0)){
237 } else if(val>it->counter){
242 val=fvalue_get_integer64(&((field_info *)gp->pdata[i])->value);
243 if((it->frames==1)&&(i==0)){
245 } else if(val>it->counter){
253 val=fvalue_get_sinteger(&((field_info *)gp->pdata[i])->value);
254 if((it->frames==1)&&(i==0)){
256 } else if((gint32)val>(gint32)(it->counter)){
261 val=fvalue_get_integer64(&((field_info *)gp->pdata[i])->value);
262 if((it->frames==1)&&(i==0)){
264 } else if((gint64)val>(gint64)(it->counter)){
268 case FT_RELATIVE_TIME:
269 new_time=fvalue_get(&((field_info *)gp->pdata[i])->value);
270 val=(guint64)(new_time->secs) * NANOSECS_PER_SEC + new_time->nsecs;
271 if((it->frames==1)&&(i==0)){
273 } else if(val>it->counter){
282 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
288 type=proto_registrar_get_ftype(it->hf_index);
289 for(i=0;i<gp->len;i++){
296 val=fvalue_get_uinteger(&((field_info *)gp->pdata[i])->value);
301 val=fvalue_get_integer64(&((field_info *)gp->pdata[i])->value);
308 val=fvalue_get_sinteger(&((field_info *)gp->pdata[i])->value);
311 case FT_RELATIVE_TIME:
312 new_time=fvalue_get(&((field_info *)gp->pdata[i])->value);
313 val=(guint64)(new_time->secs) * NANOSECS_PER_SEC + new_time->nsecs;
326 iostat_draw(void *arg)
328 io_stat_item_t *mit = arg;
330 io_stat_item_t **items;
335 guint32 borderLen=68;
343 /* Display the table border */
344 for(i=0;i<iot->num_items;i++){
345 if(iot->items[i].calc_type==CALC_TYPE_FRAMES_AND_BYTES)
348 if(iot->interval!=G_MAXINT32)
351 borderLen+=(iot->num_items-3)*17;
352 for(i=0;i<borderLen;i++){
358 printf("IO Statistics\n");
359 if(iot->interval!=G_MAXINT32)
360 printf("Interval: %3" G_GINT64_MODIFIER "u.%06" G_GINT64_MODIFIER "u secs\n",
361 iot->interval/1000000, iot->interval%1000000);
363 for(i=0;i<iot->num_items;i++){
364 printf("Column #%u: %s\n",i,iot->filters[i]?iot->filters[i]:"");
366 if(iot->interval==G_MAXINT32){
371 for(i=0;i<iot->num_items;i++){
372 if(iot->items[i].calc_type==CALC_TYPE_FRAMES_AND_BYTES){
373 printf(" Column #%-2u |",i);
375 printf(" Column #%-2u |",i);
380 if(iot->interval==G_MAXINT32) {
385 for(i=0;i<iot->num_items;i++){
386 switch(iot->items[i].calc_type){
387 case CALC_TYPE_FRAMES:
390 case CALC_TYPE_BYTES:
393 case CALC_TYPE_FRAMES_AND_BYTES:
394 printf(" Frames | Bytes |");
396 case CALC_TYPE_COUNT:
415 items=g_malloc(sizeof(io_stat_item_t *)*iot->num_items);
416 frames=g_malloc(sizeof(guint64)*iot->num_items);
417 counters=g_malloc(sizeof(guint64)*iot->num_items);
418 num=g_malloc(sizeof(guint64)*iot->num_items);
419 /* preset all items at the first interval */
420 for(i=0;i<iot->num_items;i++){
421 items[i]=&iot->items[i];
424 /* loop the items until we run out of them all */
428 for(i=0;i<iot->num_items;i++){
433 for(i=0;i<iot->num_items;i++){
434 if(items[i] && (t>=(items[i]->time+iot->interval))){
435 items[i]=items[i]->next;
438 if(items[i] && (t<(items[i]->time+iot->interval)) && (t>=items[i]->time) ){
439 frames[i]=items[i]->frames;
440 counters[i]=items[i]->counter;
441 num[i]=items[i]->num;
450 if(iot->interval==G_MAXINT32) {
453 printf("%04u.%06u-%04u.%06u ",
454 (int)(t/1000000),(int)(t%1000000),
455 (int)((t+iot->interval)/1000000),
456 (int)((t+iot->interval)%1000000));
458 for(i=0;i<iot->num_items;i++){
459 switch(iot->items[i].calc_type){
460 case CALC_TYPE_FRAMES:
461 printf(" %15" G_GINT64_MODIFIER "u ", frames[i]);
463 case CALC_TYPE_BYTES:
464 printf(" %15" G_GINT64_MODIFIER "u ", counters[i]);
466 case CALC_TYPE_FRAMES_AND_BYTES:
467 printf(" %15" G_GINT64_MODIFIER "u %15" G_GINT64_MODIFIER "u ", frames[i], counters[i]);
469 case CALC_TYPE_COUNT:
470 printf(" %15" G_GINT64_MODIFIER "u ", counters[i]);
473 switch(proto_registrar_get_ftype(iot->items[i].hf_index)){
474 case FT_RELATIVE_TIME:
475 counters[i] = (counters[i]+500)/1000;
477 (int)(counters[i]/1000000), (int)(counters[i]%1000000));
480 printf(" %15" G_GINT64_MODIFIER "u ", counters[i]);
485 switch(proto_registrar_get_ftype(iot->items[i].hf_index)){
491 printf(" %15" G_GINT64_MODIFIER "u ", counters[i]);
498 printf(" %15" G_GINT64_MODIFIER "d ", counters[i]);
500 case FT_RELATIVE_TIME:
501 counters[i]=(counters[i]+500)/1000;
503 (int)(counters[i]/1000000), (int)(counters[i]%1000000));
508 switch(proto_registrar_get_ftype(iot->items[i].hf_index)){
514 printf(" %15u ", (int)(counters[i]));
521 printf(" %15" G_GINT64_MODIFIER "d ", counters[i]);
523 case FT_RELATIVE_TIME:
524 counters[i]=(counters[i]+500)/1000;
526 (int)(counters[i]/1000000), (int)(counters[i]%1000000));
534 switch(proto_registrar_get_ftype(iot->items[i].hf_index)){
540 printf(" %15" G_GINT64_MODIFIER "u ", counters[i]/num[i]);
547 printf(" %15" G_GINT64_MODIFIER "d ", counters[i]/num[i]);
549 case FT_RELATIVE_TIME:
550 counters[i]=((counters[i]/num[i])+500)/1000;
552 (int)(counters[i]/1000000), (int)(counters[i]%1000000));
565 for(i=0;i<borderLen;i++){
578 const char *func_name;
582 static calc_type_ent_t calc_type_table[] = {
583 { "FRAMES", CALC_TYPE_FRAMES },
584 { "BYTES", CALC_TYPE_BYTES },
585 { "COUNT", CALC_TYPE_COUNT },
586 { "SUM", CALC_TYPE_SUM },
587 { "MIN", CALC_TYPE_MIN },
588 { "MAX", CALC_TYPE_MAX },
589 { "AVG", CALC_TYPE_AVG },
594 register_io_tap(io_stat_t *io, int i, const char *filter)
596 GString *error_string;
600 const char *p, *parenp;
602 header_field_info *hfi;
604 io->items[i].prev=&io->items[i];
605 io->items[i].next=NULL;
606 io->items[i].parent=io;
608 io->items[i].calc_type=CALC_TYPE_FRAMES_AND_BYTES;
609 io->items[i].frames=0;
610 io->items[i].counter=0;
612 io->filters[i]=filter;
617 for(j=0; calc_type_table[j].func_name; j++){
618 namelen=strlen(calc_type_table[j].func_name);
619 if(filter && strncmp(filter, calc_type_table[j].func_name, namelen) == 0) {
620 io->items[i].calc_type=calc_type_table[j].calc_type;
621 if(*(filter+namelen)=='(') {
623 parenp=strchr(p, ')');
625 fprintf(stderr, "\ntshark: Closing parenthesis missing from calculated expression.\n");
630 if(io->items[i].calc_type==CALC_TYPE_FRAMES || io->items[i].calc_type==CALC_TYPE_BYTES){
632 fprintf(stderr, "\ntshark: %s does require or allow a field name within the parens.\n",
633 calc_type_table[j].func_name);
638 /* bail out if a field name was not specified */
639 fprintf(stderr, "\ntshark: You didn't specify a field name for %s(*).\n",
640 calc_type_table[j].func_name);
645 field=g_malloc(parenp-p+1);
646 memcpy(field, p, parenp-p);
647 field[parenp-p] = '\0';
649 if (io->items[i].calc_type==CALC_TYPE_FRAMES || io->items[i].calc_type==CALC_TYPE_BYTES)
651 hfi=proto_registrar_get_byname(field);
653 fprintf(stderr, "\ntshark: There is no field named '%s'.\n",
659 io->items[i].hf_index=hfi->id;
663 if (io->items[i].calc_type==CALC_TYPE_FRAMES || io->items[i].calc_type==CALC_TYPE_BYTES)
667 if(hfi && !(io->items[i].calc_type==CALC_TYPE_BYTES ||
668 io->items[i].calc_type==CALC_TYPE_FRAMES ||
669 io->items[i].calc_type==CALC_TYPE_FRAMES_AND_BYTES)){
670 /* check that the type is compatible */
682 /* these types support all calculations */
684 case FT_RELATIVE_TIME:
685 /* this type only supports SUM, COUNT, MAX, MIN, AVG */
686 switch(io->items[i].calc_type){
688 case CALC_TYPE_COUNT:
695 "\ntshark: %s is a relative-time field, so %s(*) calculations are not supported on it.",
697 calc_type_table[j].func_name);
703 * XXX - support all operations on floating-point
706 if(io->items[i].calc_type!=CALC_TYPE_COUNT){
708 "\ntshark: %s doesn't have integral values, so %s(*) calculations are not supported on it.\n",
710 calc_type_table[j].func_name);
725 error_string=register_tap_listener("frame", &io->items[i], flt, TL_REQUIRES_PROTO_TREE, NULL, iostat_packet, i?NULL:iostat_draw);
729 fprintf(stderr, "\ntshark: Couldn't register io,stat tap: %s\n",
731 g_string_free(error_string, TRUE);
737 iostat_init(const char *optarg, void* userdata _U_)
739 gdouble interval_float;
743 const char *filter=NULL;
745 if(sscanf(optarg,"io,stat,%lf,%n",&interval_float,&idx)==1){
747 if(*(optarg+idx)==',')
755 fprintf(stderr, "\ntshark: invalid \"-z io,stat,<interval>[,<filter>]\" argument\n");
759 /* if interval is 0, calculate statistics over the whole file
760 * by setting the interval to G_MAXINT32
762 if(interval_float==0) {
765 /* make interval be number of us rounded to the nearest integer*/
766 interval=(gint64)(interval_float*1000000.0+0.5);
771 "\ntshark: \"-z\" interval must be >=0.000001 seconds or \"0\" for the entire capture duration.\n");
775 io=g_malloc(sizeof(io_stat_t));
776 io->interval=interval;
777 if((!filter)||(filter[0]==0)){
779 io->items=g_malloc(sizeof(io_stat_item_t)*io->num_items);
780 io->filters=g_malloc(sizeof(char *)*io->num_items);
782 register_io_tap(io, 0, NULL);
784 const char *str,*pos;
787 /* find how many ',' separated filters we have */
790 while((str=strchr(str,','))){
795 io->items=g_malloc(sizeof(io_stat_item_t)*io->num_items);
796 io->filters=g_malloc(sizeof(char *)*io->num_items);
798 /* for each filter, register a tap listener */
804 register_io_tap(io, i, NULL);
805 } else if(pos==NULL) {
807 register_io_tap(io, i, tmp);
809 tmp=g_malloc((pos-str)+1);
810 g_strlcpy(tmp,str,(pos-str)+1);
811 register_io_tap(io, i, tmp);
820 register_tap_listener_iostat(void)
822 register_stat_cmd_arg("io,stat,", iostat_init, NULL);