2 * iostat 2002 Ronnie Sahlberg
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@ethereal.com>
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"
42 typedef struct _io_stat_t {
43 gint32 interval; /* unit is ms */
45 struct _io_stat_item_t *items;
49 #define CALC_TYPE_BYTES 0
50 #define CALC_TYPE_COUNT 1
51 #define CALC_TYPE_SUM 2
52 #define CALC_TYPE_MIN 3
53 #define CALC_TYPE_MAX 4
54 #define CALC_TYPE_AVG 5
56 typedef struct _io_stat_item_t {
58 struct _io_stat_item_t *next;
59 struct _io_stat_item_t *prev;
60 gint32 time; /* unit is ms since start of capture */
70 iostat_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void *dummy _U_)
72 io_stat_item_t *mit = arg;
78 current_time=((pinfo->fd->rel_secs*1000)+(pinfo->fd->rel_usecs/1000));
80 /* the prev item before the main one is always the last interval we saw packets for */
83 /* XXX for the time being, just ignore all frames that are in the past.
84 should be fixed in the future but hopefully it is uncommon */
85 if(current_time<it->time){
89 /* we have moved into a new interval, we need to create a new struct */
90 if(current_time>=(it->time+mit->parent->interval)){
91 it->next=g_malloc(sizeof(io_stat_item_t));
97 it->time=(current_time / mit->parent->interval) * mit->parent->interval;
101 it->calc_type=it->prev->calc_type;
102 it->hf_index=it->prev->hf_index;
105 /* it will now give us the current structure to use to store the data in */
108 switch(it->calc_type){
109 case CALC_TYPE_BYTES:
110 it->counter+=pinfo->fd->pkt_len;
112 case CALC_TYPE_COUNT:
113 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
115 it->counter+=gp->len;
119 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
121 for(i=0;i<gp->len;i++){
122 it->counter+=fvalue_get_integer(&((field_info *)gp->pdata[i])->value);
127 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
133 type=proto_registrar_get_ftype(it->hf_index);
134 for(i=0;i<gp->len;i++){
140 val=fvalue_get_integer(&((field_info *)gp->pdata[i])->value);
141 if((it->frames==1)&&(i==0)){
143 } else if(val<it->counter){
151 val=fvalue_get_integer(&((field_info *)gp->pdata[i])->value);
152 if((it->frames==1)&&(i==0)){
154 } else if((gint32)val<(gint32)(it->counter)){
158 case FT_RELATIVE_TIME:
159 new_time=fvalue_get(&((field_info *)gp->pdata[i])->value);
160 val=new_time->secs*1000+new_time->nsecs/1000000;
161 if((it->frames==1)&&(i==0)){
163 } else if(val<it->counter){
172 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
178 type=proto_registrar_get_ftype(it->hf_index);
179 for(i=0;i<gp->len;i++){
185 val=fvalue_get_integer(&((field_info *)gp->pdata[i])->value);
186 if((it->frames==1)&&(i==0)){
188 } else if(val>it->counter){
196 val=fvalue_get_integer(&((field_info *)gp->pdata[i])->value);
197 if((it->frames==1)&&(i==0)){
199 } else if((gint32)val>(gint32)(it->counter)){
203 case FT_RELATIVE_TIME:
204 new_time=fvalue_get(&((field_info *)gp->pdata[i])->value);
205 val=new_time->secs*1000+new_time->nsecs/1000000;
206 if((it->frames==1)&&(i==0)){
208 } else if(val>it->counter){
217 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
223 type=proto_registrar_get_ftype(it->hf_index);
224 for(i=0;i<gp->len;i++){
235 val=fvalue_get_integer(&((field_info *)gp->pdata[i])->value);
238 case FT_RELATIVE_TIME:
239 new_time=fvalue_get(&((field_info *)gp->pdata[i])->value);
240 val=new_time->secs*1000+new_time->nsecs/1000000;
253 iostat_draw(void *arg)
255 io_stat_item_t *mit = arg;
257 io_stat_item_t **items;
261 guint32 i,more_items;
267 printf("===================================================================\n");
268 printf("IO Statistics\n");
269 printf("Interval: %d.%03d secs\n", iot->interval/1000, iot->interval%1000);
270 for(i=0;i<iot->num_items;i++){
271 printf("Column #%d: %s\n",i,iot->filters[i]?iot->filters[i]:"");
274 for(i=0;i<iot->num_items;i++){
275 printf("| Column #%-2d ",i);
279 for(i=0;i<iot->num_items;i++){
280 switch(iot->items[i].calc_type){
281 case CALC_TYPE_BYTES:
282 printf("|frames| bytes ");
284 case CALC_TYPE_COUNT:
303 items=g_malloc(sizeof(io_stat_item_t *)*iot->num_items);
304 frames=g_malloc(sizeof(guint32)*iot->num_items);
305 counters=g_malloc(sizeof(guint32)*iot->num_items);
306 num=g_malloc(sizeof(guint32)*iot->num_items);
307 /* preset all items at the first interval */
308 for(i=0;i<iot->num_items;i++){
309 items[i]=&iot->items[i];
312 /* loop the items until we run out of them all */
316 for(i=0;i<iot->num_items;i++){
321 for(i=0;i<iot->num_items;i++){
322 if(items[i] && (t>=(items[i]->time+iot->interval))){
323 items[i]=items[i]->next;
326 if(items[i] && (t<(items[i]->time+iot->interval)) && (t>=items[i]->time) ){
327 frames[i]=items[i]->frames;
328 counters[i]=items[i]->counter;
329 num[i]=items[i]->num;
338 printf("%03d.%03d-%03d.%03d ",
340 (t+iot->interval)/1000,(t+iot->interval)%1000);
341 for(i=0;i<iot->num_items;i++){
342 switch(iot->items[i].calc_type){
343 case CALC_TYPE_BYTES:
344 printf("%6d %9d ",frames[i],counters[i]);
346 case CALC_TYPE_COUNT:
347 printf(" %8d ", counters[i]);
350 printf(" %8d ", counters[i]);
353 switch(proto_registrar_get_ftype(iot->items[i].hf_index)){
358 printf(" %8u ", counters[i]);
364 printf(" %8d ", counters[i]);
366 case FT_RELATIVE_TIME:
367 printf(" %6d.%03d ", counters[i]/1000, counters[i]%1000);
372 switch(proto_registrar_get_ftype(iot->items[i].hf_index)){
377 printf(" %8u ", counters[i]);
383 printf(" %8d ", counters[i]);
385 case FT_RELATIVE_TIME:
386 printf(" %6d.%03d ", counters[i]/1000, counters[i]%1000);
394 switch(proto_registrar_get_ftype(iot->items[i].hf_index)){
399 printf(" %8u ", counters[i]/num[i]);
405 printf(" %8d ", counters[i]/num[i]);
407 case FT_RELATIVE_TIME:
409 printf(" %6d.%03d ", counters[i]/1000, counters[i]%1000);
422 printf("===================================================================\n");
432 get_calc_field(char *filter, char **flt)
436 header_field_info *hfi;
439 for(i=0;filter[i];i++){
441 fprintf(stderr,"get_calc_field(): Too long field name: %s\n", filter);
454 hfi=proto_registrar_get_byname(field);
456 fprintf(stderr, "get_calc_field(): No such field %s\n", field);
464 register_io_tap(io_stat_t *io, int i, char *filter)
466 GString *error_string;
469 io->items[i].prev=&io->items[i];
470 io->items[i].next=NULL;
471 io->items[i].parent=io;
473 io->items[i].calc_type=CALC_TYPE_BYTES;
474 io->items[i].frames=0;
475 io->items[i].counter=0;
477 io->filters[i]=filter;
483 if(!strncmp("COUNT(", filter, 6)){
484 io->items[i].calc_type=CALC_TYPE_COUNT;
485 io->items[i].hf_index=get_calc_field(filter+6, &flt);
486 } else if (!strncmp("SUM(", filter, 4)){
487 io->items[i].calc_type=CALC_TYPE_SUM;
488 io->items[i].hf_index=get_calc_field(filter+4, &flt);
489 switch(proto_registrar_get_nth(io->items[i].hf_index)->type){
500 fprintf(stderr, "register_io_tap(): Invalid field type. SUM(x) only supports 8,16,24 and 32 byte integer fields\n");
503 } else if (!strncmp("MIN(", filter, 4)){
504 io->items[i].calc_type=CALC_TYPE_MIN;
505 io->items[i].hf_index=get_calc_field(filter+4, &flt);
506 switch(proto_registrar_get_nth(io->items[i].hf_index)->type){
515 case FT_RELATIVE_TIME:
518 fprintf(stderr, "register_io_tap(): Invalid field type. MIN(x) only supports 8,16,24 and 32 byte integer fields and relative time fields\n");
521 } else if (!strncmp("MAX(", filter, 4)){
522 io->items[i].calc_type=CALC_TYPE_MAX;
523 io->items[i].hf_index=get_calc_field(filter+4, &flt);
524 switch(proto_registrar_get_nth(io->items[i].hf_index)->type){
533 case FT_RELATIVE_TIME:
536 fprintf(stderr, "register_io_tap(): Invalid field type. MAX(x) only supports 8,16,24 and 32 byte integer fields and relative time fields\n");
539 } else if (!strncmp("AVG(", filter, 4)){
540 io->items[i].calc_type=CALC_TYPE_AVG;
541 io->items[i].hf_index=get_calc_field(filter+4, &flt);
542 switch(proto_registrar_get_nth(io->items[i].hf_index)->type){
551 case FT_RELATIVE_TIME:
554 fprintf(stderr, "register_io_tap(): Invalid field type. AVG(x) only supports 8,16,24 and 32 byte integer fields and relative time fields\n");
566 error_string=register_tap_listener("frame", &io->items[i], flt, NULL, iostat_packet, i?NULL:iostat_draw);
570 fprintf(stderr, "tethereal: Couldn't register io,stat tap: %s\n",
572 g_string_free(error_string, TRUE);
578 iostat_init(char *optarg)
580 float interval_float;
586 if(sscanf(optarg,"io,stat,%f,%n",&interval_float,&pos)==1){
593 fprintf(stderr, "tethereal: invalid \"-z io,stat,<interval>[,<filter>]\" argument\n");
598 /* make interval be number of ms */
599 interval=(gint32)(interval_float*1000.0+0.9);
601 fprintf(stderr, "tethereal:iostat_init() interval must be >=0.001 seconds\n");
605 io=g_malloc(sizeof(io_stat_t));
606 io->interval=interval;
607 if((!filter)||(filter[0]==0)){
609 io->items=g_malloc(sizeof(io_stat_item_t)*io->num_items);
610 io->filters=g_malloc(sizeof(char *)*io->num_items);
612 register_io_tap(io, 0, NULL);
616 /* find how many ',' separated filters we have */
619 while((str=strchr(str,','))){
624 io->items=g_malloc(sizeof(io_stat_item_t)*io->num_items);
625 io->filters=g_malloc(sizeof(char *)*io->num_items);
627 /* for each filter, register a tap listener */
633 register_io_tap(io, i, NULL);
634 } else if(pos==NULL) {
635 tmp=g_malloc(strlen(str)+1);
637 register_io_tap(io, i, tmp);
639 tmp=g_malloc((pos-str)+1);
640 strncpy(tmp,str,(pos-str));
642 register_io_tap(io, i, tmp);
651 register_tap_listener_iostat(void)
653 register_ethereal_tap("io,stat,", iostat_init);