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>
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 _U_, const void *dummy _U_)
73 io_stat_item_t *mit = arg;
79 current_time=((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 it->counter+=fvalue_get_integer(&((field_info *)gp->pdata[i])->value);
128 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
134 type=proto_registrar_get_ftype(it->hf_index);
135 for(i=0;i<gp->len;i++){
141 val=fvalue_get_integer(&((field_info *)gp->pdata[i])->value);
142 if((it->frames==1)&&(i==0)){
144 } else if(val<it->counter){
152 val=fvalue_get_integer(&((field_info *)gp->pdata[i])->value);
153 if((it->frames==1)&&(i==0)){
155 } else if((gint32)val<(gint32)(it->counter)){
159 case FT_RELATIVE_TIME:
160 new_time=fvalue_get(&((field_info *)gp->pdata[i])->value);
161 val=new_time->secs*1000+new_time->nsecs/1000000;
162 if((it->frames==1)&&(i==0)){
164 } else if(val<it->counter){
173 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
179 type=proto_registrar_get_ftype(it->hf_index);
180 for(i=0;i<gp->len;i++){
186 val=fvalue_get_integer(&((field_info *)gp->pdata[i])->value);
187 if((it->frames==1)&&(i==0)){
189 } else if(val>it->counter){
197 val=fvalue_get_integer(&((field_info *)gp->pdata[i])->value);
198 if((it->frames==1)&&(i==0)){
200 } else if((gint32)val>(gint32)(it->counter)){
204 case FT_RELATIVE_TIME:
205 new_time=fvalue_get(&((field_info *)gp->pdata[i])->value);
206 val=new_time->secs*1000+new_time->nsecs/1000000;
207 if((it->frames==1)&&(i==0)){
209 } else if(val>it->counter){
218 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
224 type=proto_registrar_get_ftype(it->hf_index);
225 for(i=0;i<gp->len;i++){
236 val=fvalue_get_integer(&((field_info *)gp->pdata[i])->value);
239 case FT_RELATIVE_TIME:
240 new_time=fvalue_get(&((field_info *)gp->pdata[i])->value);
241 val=new_time->secs*1000+new_time->nsecs/1000000;
254 iostat_draw(void *arg)
256 io_stat_item_t *mit = arg;
258 io_stat_item_t **items;
262 guint32 i,more_items;
268 printf("===================================================================\n");
269 printf("IO Statistics\n");
270 printf("Interval: %d.%03d secs\n", iot->interval/1000, iot->interval%1000);
271 for(i=0;i<iot->num_items;i++){
272 printf("Column #%d: %s\n",i,iot->filters[i]?iot->filters[i]:"");
275 for(i=0;i<iot->num_items;i++){
276 printf("| Column #%-2d ",i);
280 for(i=0;i<iot->num_items;i++){
281 switch(iot->items[i].calc_type){
282 case CALC_TYPE_BYTES:
283 printf("|frames| bytes ");
285 case CALC_TYPE_COUNT:
304 items=g_malloc(sizeof(io_stat_item_t *)*iot->num_items);
305 frames=g_malloc(sizeof(guint32)*iot->num_items);
306 counters=g_malloc(sizeof(guint32)*iot->num_items);
307 num=g_malloc(sizeof(guint32)*iot->num_items);
308 /* preset all items at the first interval */
309 for(i=0;i<iot->num_items;i++){
310 items[i]=&iot->items[i];
313 /* loop the items until we run out of them all */
317 for(i=0;i<iot->num_items;i++){
322 for(i=0;i<iot->num_items;i++){
323 if(items[i] && (t>=(items[i]->time+iot->interval))){
324 items[i]=items[i]->next;
327 if(items[i] && (t<(items[i]->time+iot->interval)) && (t>=items[i]->time) ){
328 frames[i]=items[i]->frames;
329 counters[i]=items[i]->counter;
330 num[i]=items[i]->num;
339 printf("%03d.%03d-%03d.%03d ",
341 (t+iot->interval)/1000,(t+iot->interval)%1000);
342 for(i=0;i<iot->num_items;i++){
343 switch(iot->items[i].calc_type){
344 case CALC_TYPE_BYTES:
345 printf("%6d %9d ",frames[i],counters[i]);
347 case CALC_TYPE_COUNT:
348 printf(" %8d ", counters[i]);
351 printf(" %8d ", counters[i]);
354 switch(proto_registrar_get_ftype(iot->items[i].hf_index)){
359 printf(" %8u ", counters[i]);
365 printf(" %8d ", counters[i]);
367 case FT_RELATIVE_TIME:
368 printf(" %6d.%03d ", counters[i]/1000, counters[i]%1000);
373 switch(proto_registrar_get_ftype(iot->items[i].hf_index)){
378 printf(" %8u ", counters[i]);
384 printf(" %8d ", counters[i]);
386 case FT_RELATIVE_TIME:
387 printf(" %6d.%03d ", counters[i]/1000, counters[i]%1000);
395 switch(proto_registrar_get_ftype(iot->items[i].hf_index)){
400 printf(" %8u ", counters[i]/num[i]);
406 printf(" %8d ", counters[i]/num[i]);
408 case FT_RELATIVE_TIME:
410 printf(" %6d.%03d ", counters[i]/1000, counters[i]%1000);
423 printf("===================================================================\n");
433 const char *func_name;
437 static calc_type_ent_t calc_type_table[] = {
438 { "COUNT", CALC_TYPE_COUNT },
439 { "SUM", CALC_TYPE_SUM },
440 { "MIN", CALC_TYPE_MIN },
441 { "MAX", CALC_TYPE_MAX },
442 { "AVG", CALC_TYPE_AVG },
447 register_io_tap(io_stat_t *io, int i, const char *filter)
449 GString *error_string;
453 const char *p, *parenp;
455 header_field_info *hfi;
457 io->items[i].prev=&io->items[i];
458 io->items[i].next=NULL;
459 io->items[i].parent=io;
461 io->items[i].calc_type=CALC_TYPE_BYTES;
462 io->items[i].frames=0;
463 io->items[i].counter=0;
465 io->filters[i]=filter;
473 for(j=0; calc_type_table[j].func_name; j++){
474 namelen=strlen(calc_type_table[j].func_name);
475 if(strncmp(filter, calc_type_table[j].func_name, namelen) == 0
476 && *(filter+namelen)=='('){
477 io->items[i].calc_type=calc_type_table[j].calc_type;
480 parenp=strchr(p, ')');
482 fprintf(stderr, "tshark: Closing parenthesis missing from calculated expression.\n");
485 /* bail out if there was no field specified */
487 fprintf(stderr, "tshark: You didn't specify a field name for %s(*).\n",
488 calc_type_table[j].func_name);
491 field=malloc(parenp-p+1);
493 fprintf(stderr, "tshark: Out of memory.\n");
496 memcpy(field, p, parenp-p);
497 field[parenp-p] = '\0';
500 hfi=proto_registrar_get_byname(field);
502 fprintf(stderr, "tshark: There is no field named '%s'.\n",
508 io->items[i].hf_index=hfi->id;
512 if(io->items[i].calc_type!=CALC_TYPE_BYTES){
513 /* check that the type is compatible */
523 /* these types support all calculations */
525 case FT_RELATIVE_TIME:
526 /* this type only supports SUM, COUNT, MAX, MIN, AVG */
527 switch(io->items[i].calc_type){
529 case CALC_TYPE_COUNT:
536 "tshark: %s is a relative-time field, so %s(*) calculations are not supported on it.",
538 calc_type_table[j].func_name);
545 * XXX - support this if gint64/guint64 are
548 if(io->items[i].calc_type!=CALC_TYPE_COUNT){
550 "tshark: %s is a 64-bit integer, so %s(*) calculations are not supported on it.",
552 calc_type_table[j].func_name);
558 * XXX - support all operations on floating-point
561 if(io->items[i].calc_type!=CALC_TYPE_COUNT){
563 "tshark: %s doesn't have integral values, so %s(*) calculations are not supported on it.\n",
565 calc_type_table[j].func_name);
580 error_string=register_tap_listener("frame", &io->items[i], flt, NULL, iostat_packet, i?NULL:iostat_draw);
584 fprintf(stderr, "tshark: Couldn't register io,stat tap: %s\n",
586 g_string_free(error_string, TRUE);
592 iostat_init(const char *optarg, void* userdata _U_)
594 float interval_float;
598 const char *filter=NULL;
600 if(sscanf(optarg,"io,stat,%f,%n",&interval_float,&pos)==1){
607 fprintf(stderr, "tshark: invalid \"-z io,stat,<interval>[,<filter>]\" argument\n");
612 /* make interval be number of ms */
613 interval=(gint32)(interval_float*1000.0+0.9);
615 fprintf(stderr, "tshark: \"-z\" interval must be >=0.001 seconds.\n");
619 io=g_malloc(sizeof(io_stat_t));
620 io->interval=interval;
621 if((!filter)||(filter[0]==0)){
623 io->items=g_malloc(sizeof(io_stat_item_t)*io->num_items);
624 io->filters=g_malloc(sizeof(char *)*io->num_items);
626 register_io_tap(io, 0, NULL);
628 const char *str,*pos;
631 /* find how many ',' separated filters we have */
634 while((str=strchr(str,','))){
639 io->items=g_malloc(sizeof(io_stat_item_t)*io->num_items);
640 io->filters=g_malloc(sizeof(char *)*io->num_items);
642 /* for each filter, register a tap listener */
648 register_io_tap(io, i, NULL);
649 } else if(pos==NULL) {
650 tmp=g_malloc(strlen(str)+1);
652 register_io_tap(io, i, tmp);
654 tmp=g_malloc((pos-str)+1);
655 strncpy(tmp,str,(pos-str));
657 register_io_tap(io, i, tmp);
666 register_tap_listener_iostat(void)
668 register_stat_cmd_arg("io,stat,", iostat_init, NULL);