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 gint32 interval; /* unit is ms */
46 struct _io_stat_item_t *items;
50 #define CALC_TYPE_BYTES 0
51 #define CALC_TYPE_COUNT 1
52 #define CALC_TYPE_SUM 2
53 #define CALC_TYPE_MIN 3
54 #define CALC_TYPE_MAX 4
55 #define CALC_TYPE_AVG 5
57 typedef struct _io_stat_item_t {
59 struct _io_stat_item_t *next;
60 struct _io_stat_item_t *prev;
61 gint32 time; /* unit is ms since start of capture */
71 iostat_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt, const void *dummy _U_)
73 io_stat_item_t *mit = arg;
79 current_time=(gint32) ((pinfo->fd->rel_ts.secs*1000)+(pinfo->fd->rel_ts.nsecs/1000000));
81 /* the prev item before the main one is always the last interval we saw packets for */
84 /* XXX for the time being, just ignore all frames that are in the past.
85 should be fixed in the future but hopefully it is uncommon */
86 if(current_time<it->time){
90 /* we have moved into a new interval, we need to create a new struct */
91 if(current_time>=(it->time+mit->parent->interval)){
92 it->next=g_malloc(sizeof(io_stat_item_t));
98 it->time=(current_time / mit->parent->interval) * mit->parent->interval;
102 it->calc_type=it->prev->calc_type;
103 it->hf_index=it->prev->hf_index;
106 /* it will now give us the current structure to use to store the data in */
109 switch(it->calc_type){
110 case CALC_TYPE_BYTES:
111 it->counter+=pinfo->fd->pkt_len;
113 case CALC_TYPE_COUNT:
114 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
116 it->counter+=gp->len;
120 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
122 for(i=0;i<gp->len;i++){
123 switch(proto_registrar_get_ftype(it->hf_index)){
128 it->counter+=fvalue_get_uinteger(&((field_info *)gp->pdata[i])->value);
131 it->counter+=fvalue_get_integer64(&((field_info *)gp->pdata[i])->value);
137 it->counter+=fvalue_get_sinteger(&((field_info *)gp->pdata[i])->value);
140 it->counter+=(gint64)fvalue_get_integer64(&((field_info *)gp->pdata[i])->value);
147 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
153 type=proto_registrar_get_ftype(it->hf_index);
154 for(i=0;i<gp->len;i++){
160 val=fvalue_get_uinteger(&((field_info *)gp->pdata[i])->value);
161 if((it->frames==1)&&(i==0)){
163 } else if(val<it->counter){
168 val=fvalue_get_integer64(&((field_info *)gp->pdata[i])->value);
169 if((it->frames==1)&&(i==0)){
171 } else if(val<it->counter){
179 val=fvalue_get_sinteger(&((field_info *)gp->pdata[i])->value);
180 if((it->frames==1)&&(i==0)){
182 } else if((gint32)val<(gint32)(it->counter)){
187 val=fvalue_get_integer64(&((field_info *)gp->pdata[i])->value);
188 if((it->frames==1)&&(i==0)){
190 } else if((gint64)val<(gint64)(it->counter)){
194 case FT_RELATIVE_TIME:
195 new_time=fvalue_get(&((field_info *)gp->pdata[i])->value);
196 val=(guint64) (new_time->secs*1000+new_time->nsecs/1000000);
197 if((it->frames==1)&&(i==0)){
199 } else if(val<it->counter){
208 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
214 type=proto_registrar_get_ftype(it->hf_index);
215 for(i=0;i<gp->len;i++){
221 val=fvalue_get_uinteger(&((field_info *)gp->pdata[i])->value);
222 if((it->frames==1)&&(i==0)){
224 } else if(val>it->counter){
229 val=fvalue_get_integer64(&((field_info *)gp->pdata[i])->value);
230 if((it->frames==1)&&(i==0)){
232 } else if(val>it->counter){
240 val=fvalue_get_sinteger(&((field_info *)gp->pdata[i])->value);
241 if((it->frames==1)&&(i==0)){
243 } else if((gint32)val>(gint32)(it->counter)){
248 val=fvalue_get_integer64(&((field_info *)gp->pdata[i])->value);
249 if((it->frames==1)&&(i==0)){
251 } else if((gint64)val>(gint64)(it->counter)){
255 case FT_RELATIVE_TIME:
256 new_time=fvalue_get(&((field_info *)gp->pdata[i])->value);
257 val=(guint64) (new_time->secs*1000+new_time->nsecs/1000000);
258 if((it->frames==1)&&(i==0)){
260 } else if(val>it->counter){
269 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
275 type=proto_registrar_get_ftype(it->hf_index);
276 for(i=0;i<gp->len;i++){
283 val=fvalue_get_uinteger(&((field_info *)gp->pdata[i])->value);
288 val=fvalue_get_integer64(&((field_info *)gp->pdata[i])->value);
295 val=fvalue_get_sinteger(&((field_info *)gp->pdata[i])->value);
298 case FT_RELATIVE_TIME:
299 new_time=fvalue_get(&((field_info *)gp->pdata[i])->value);
300 val=(guint64) (new_time->secs*1000+new_time->nsecs/1000000);
313 iostat_draw(void *arg)
315 io_stat_item_t *mit = arg;
317 io_stat_item_t **items;
328 printf("===================================================================\n");
329 printf("IO Statistics\n");
330 if(iot->interval!=G_MAXINT32)
331 printf("Interval: %d.%03d secs\n", iot->interval/1000, iot->interval%1000);
332 for(i=0;i<iot->num_items;i++){
333 printf("Column #%u: %s\n",i,iot->filters[i]?iot->filters[i]:"");
336 for(i=0;i<iot->num_items;i++){
337 printf("| Column #%-2u ",i);
341 for(i=0;i<iot->num_items;i++){
342 switch(iot->items[i].calc_type){
343 case CALC_TYPE_BYTES:
344 printf("|frames| bytes ");
346 case CALC_TYPE_COUNT:
365 items=g_malloc(sizeof(io_stat_item_t *)*iot->num_items);
366 frames=g_malloc(sizeof(guint64)*iot->num_items);
367 counters=g_malloc(sizeof(guint64)*iot->num_items);
368 num=g_malloc(sizeof(guint64)*iot->num_items);
369 /* preset all items at the first interval */
370 for(i=0;i<iot->num_items;i++){
371 items[i]=&iot->items[i];
374 /* loop the items until we run out of them all */
378 for(i=0;i<iot->num_items;i++){
383 for(i=0;i<iot->num_items;i++){
384 if(items[i] && (t>=(items[i]->time+iot->interval))){
385 items[i]=items[i]->next;
388 if(items[i] && (t<(items[i]->time+iot->interval)) && (t>=items[i]->time) ){
389 frames[i]=items[i]->frames;
390 counters[i]=items[i]->counter;
391 num[i]=items[i]->num;
400 if(iot->interval==G_MAXINT32) {
403 printf("%03d.%03d-%03d.%03d ",
405 (t+iot->interval)/1000,
406 (t+iot->interval)%1000);
408 for(i=0;i<iot->num_items;i++){
409 switch(iot->items[i].calc_type){
410 case CALC_TYPE_BYTES:
411 printf("%6" G_GINT64_MODIFIER "u %9" G_GINT64_MODIFIER "u ",frames[i], counters[i]);
413 case CALC_TYPE_COUNT:
414 printf(" %15" G_GINT64_MODIFIER "u ", counters[i]);
417 printf(" %15" G_GINT64_MODIFIER "u ", counters[i]);
420 switch(proto_registrar_get_ftype(iot->items[i].hf_index)){
426 printf(" %15" G_GINT64_MODIFIER "u ", counters[i]);
433 printf(" %15" G_GINT64_MODIFIER "d ", counters[i]);
435 case FT_RELATIVE_TIME:
436 printf(" %11" G_GINT64_MODIFIER "d.%03d ", counters[i]/1000, (gint)counters[i]%1000);
441 switch(proto_registrar_get_ftype(iot->items[i].hf_index)){
447 printf(" %15" G_GINT64_MODIFIER "u ", counters[i]);
454 printf(" %15" G_GINT64_MODIFIER "d ", counters[i]);
456 case FT_RELATIVE_TIME:
457 printf(" %11" G_GINT64_MODIFIER "d.%03d ", counters[i]/1000, (gint)counters[i]%1000);
465 switch(proto_registrar_get_ftype(iot->items[i].hf_index)){
471 printf(" %15" G_GINT64_MODIFIER "u ", counters[i]/num[i]);
478 printf(" %15" G_GINT64_MODIFIER "d ", counters[i]/num[i]);
480 case FT_RELATIVE_TIME:
482 printf(" %11" G_GINT64_MODIFIER "d.%03d ", counters[i]/1000, (gint)counters[i]%1000);
495 printf("===================================================================\n");
505 const char *func_name;
509 static calc_type_ent_t calc_type_table[] = {
510 { "COUNT", CALC_TYPE_COUNT },
511 { "SUM", CALC_TYPE_SUM },
512 { "MIN", CALC_TYPE_MIN },
513 { "MAX", CALC_TYPE_MAX },
514 { "AVG", CALC_TYPE_AVG },
519 register_io_tap(io_stat_t *io, int i, const char *filter)
521 GString *error_string;
525 const char *p, *parenp;
527 header_field_info *hfi;
529 io->items[i].prev=&io->items[i];
530 io->items[i].next=NULL;
531 io->items[i].parent=io;
533 io->items[i].calc_type=CALC_TYPE_BYTES;
534 io->items[i].frames=0;
535 io->items[i].counter=0;
537 io->filters[i]=filter;
542 for(j=0; calc_type_table[j].func_name; j++){
543 namelen=strlen(calc_type_table[j].func_name);
545 && strncmp(filter, calc_type_table[j].func_name, namelen) == 0
546 && *(filter+namelen)=='('){
547 io->items[i].calc_type=calc_type_table[j].calc_type;
550 parenp=strchr(p, ')');
552 fprintf(stderr, "tshark: Closing parenthesis missing from calculated expression.\n");
555 /* bail out if there was no field specified */
557 fprintf(stderr, "tshark: You didn't specify a field name for %s(*).\n",
558 calc_type_table[j].func_name);
561 field=g_malloc(parenp-p+1);
562 memcpy(field, p, parenp-p);
563 field[parenp-p] = '\0';
566 hfi=proto_registrar_get_byname(field);
568 fprintf(stderr, "tshark: There is no field named '%s'.\n",
574 io->items[i].hf_index=hfi->id;
578 if(hfi && io->items[i].calc_type!=CALC_TYPE_BYTES){
579 /* check that the type is compatible */
591 /* these types support all calculations */
593 case FT_RELATIVE_TIME:
594 /* this type only supports SUM, COUNT, MAX, MIN, AVG */
595 switch(io->items[i].calc_type){
597 case CALC_TYPE_COUNT:
604 "tshark: %s is a relative-time field, so %s(*) calculations are not supported on it.",
606 calc_type_table[j].func_name);
612 * XXX - support all operations on floating-point
615 if(io->items[i].calc_type!=CALC_TYPE_COUNT){
617 "tshark: %s doesn't have integral values, so %s(*) calculations are not supported on it.\n",
619 calc_type_table[j].func_name);
634 error_string=register_tap_listener("frame", &io->items[i], flt, TL_REQUIRES_PROTO_TREE, NULL, iostat_packet, i?NULL:iostat_draw);
638 fprintf(stderr, "tshark: Couldn't register io,stat tap: %s\n",
640 g_string_free(error_string, TRUE);
646 iostat_init(const char *optarg, void* userdata _U_)
648 float interval_float;
652 const char *filter=NULL;
654 if(sscanf(optarg,"io,stat,%f%n",&interval_float,&idx)==1){
656 if(*(optarg+idx)==',')
664 fprintf(stderr, "tshark: invalid \"-z io,stat,<interval>[,<filter>]\" argument\n");
668 /* if interval is 0, calculate statistics over the whole file
669 * by setting the interval to G_MAXINT32
671 if(interval_float==0) {
674 /* make interval be number of ms */
675 interval=(gint32)(interval_float*1000.0+0.9);
679 fprintf(stderr, "tshark: \"-z\" interval must be >=0.001 seconds or 0.\n");
683 io=g_malloc(sizeof(io_stat_t));
684 io->interval=interval;
685 if((!filter)||(filter[0]==0)){
687 io->items=g_malloc(sizeof(io_stat_item_t)*io->num_items);
688 io->filters=g_malloc(sizeof(char *)*io->num_items);
690 register_io_tap(io, 0, NULL);
692 const char *str,*pos;
695 /* find how many ',' separated filters we have */
698 while((str=strchr(str,','))){
703 io->items=g_malloc(sizeof(io_stat_item_t)*io->num_items);
704 io->filters=g_malloc(sizeof(char *)*io->num_items);
706 /* for each filter, register a tap listener */
712 register_io_tap(io, i, NULL);
713 } else if(pos==NULL) {
715 register_io_tap(io, i, tmp);
717 tmp=g_malloc((pos-str)+1);
718 g_strlcpy(tmp,str,(pos-str)+1);
719 register_io_tap(io, i, tmp);
728 register_tap_listener_iostat(void)
730 register_stat_cmd_arg("io,stat,", iostat_init, NULL);