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=(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);
134 it->counter+=fvalue_get_sinteger(&((field_info *)gp->pdata[i])->value);
141 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
147 type=proto_registrar_get_ftype(it->hf_index);
148 for(i=0;i<gp->len;i++){
154 val=fvalue_get_uinteger(&((field_info *)gp->pdata[i])->value);
155 if((it->frames==1)&&(i==0)){
157 } else if(val<it->counter){
165 val=fvalue_get_sinteger(&((field_info *)gp->pdata[i])->value);
166 if((it->frames==1)&&(i==0)){
168 } else if((gint32)val<(gint32)(it->counter)){
172 case FT_RELATIVE_TIME:
173 new_time=fvalue_get(&((field_info *)gp->pdata[i])->value);
174 val=(guint32) (new_time->secs*1000+new_time->nsecs/1000000);
175 if((it->frames==1)&&(i==0)){
177 } else if(val<it->counter){
186 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
192 type=proto_registrar_get_ftype(it->hf_index);
193 for(i=0;i<gp->len;i++){
199 val=fvalue_get_uinteger(&((field_info *)gp->pdata[i])->value);
200 if((it->frames==1)&&(i==0)){
202 } else if(val>it->counter){
210 val=fvalue_get_sinteger(&((field_info *)gp->pdata[i])->value);
211 if((it->frames==1)&&(i==0)){
213 } else if((gint32)val>(gint32)(it->counter)){
217 case FT_RELATIVE_TIME:
218 new_time=fvalue_get(&((field_info *)gp->pdata[i])->value);
219 val=(guint32) (new_time->secs*1000+new_time->nsecs/1000000);
220 if((it->frames==1)&&(i==0)){
222 } else if(val>it->counter){
231 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
237 type=proto_registrar_get_ftype(it->hf_index);
238 for(i=0;i<gp->len;i++){
245 val=fvalue_get_uinteger(&((field_info *)gp->pdata[i])->value);
252 val=fvalue_get_sinteger(&((field_info *)gp->pdata[i])->value);
255 case FT_RELATIVE_TIME:
256 new_time=fvalue_get(&((field_info *)gp->pdata[i])->value);
257 val=(guint32) (new_time->secs*1000+new_time->nsecs/1000000);
270 iostat_draw(void *arg)
272 io_stat_item_t *mit = arg;
274 io_stat_item_t **items;
278 guint32 i,more_items;
284 printf("===================================================================\n");
285 printf("IO Statistics\n");
286 printf("Interval: %d.%03d secs\n", iot->interval/1000, iot->interval%1000);
287 for(i=0;i<iot->num_items;i++){
288 printf("Column #%d: %s\n",i,iot->filters[i]?iot->filters[i]:"");
291 for(i=0;i<iot->num_items;i++){
292 printf("| Column #%-2d ",i);
296 for(i=0;i<iot->num_items;i++){
297 switch(iot->items[i].calc_type){
298 case CALC_TYPE_BYTES:
299 printf("|frames| bytes ");
301 case CALC_TYPE_COUNT:
320 items=g_malloc(sizeof(io_stat_item_t *)*iot->num_items);
321 frames=g_malloc(sizeof(guint32)*iot->num_items);
322 counters=g_malloc(sizeof(guint32)*iot->num_items);
323 num=g_malloc(sizeof(guint32)*iot->num_items);
324 /* preset all items at the first interval */
325 for(i=0;i<iot->num_items;i++){
326 items[i]=&iot->items[i];
329 /* loop the items until we run out of them all */
333 for(i=0;i<iot->num_items;i++){
338 for(i=0;i<iot->num_items;i++){
339 if(items[i] && (t>=(items[i]->time+iot->interval))){
340 items[i]=items[i]->next;
343 if(items[i] && (t<(items[i]->time+iot->interval)) && (t>=items[i]->time) ){
344 frames[i]=items[i]->frames;
345 counters[i]=items[i]->counter;
346 num[i]=items[i]->num;
355 printf("%03d.%03d-%03d.%03d ",
357 (t+iot->interval)/1000,(t+iot->interval)%1000);
358 for(i=0;i<iot->num_items;i++){
359 switch(iot->items[i].calc_type){
360 case CALC_TYPE_BYTES:
361 printf("%6d %9d ",frames[i],counters[i]);
363 case CALC_TYPE_COUNT:
364 printf(" %8d ", counters[i]);
367 printf(" %8d ", counters[i]);
370 switch(proto_registrar_get_ftype(iot->items[i].hf_index)){
375 printf(" %8u ", counters[i]);
381 printf(" %8d ", counters[i]);
383 case FT_RELATIVE_TIME:
384 printf(" %6d.%03d ", counters[i]/1000, counters[i]%1000);
389 switch(proto_registrar_get_ftype(iot->items[i].hf_index)){
394 printf(" %8u ", counters[i]);
400 printf(" %8d ", counters[i]);
402 case FT_RELATIVE_TIME:
403 printf(" %6d.%03d ", counters[i]/1000, counters[i]%1000);
411 switch(proto_registrar_get_ftype(iot->items[i].hf_index)){
416 printf(" %8u ", counters[i]/num[i]);
422 printf(" %8d ", counters[i]/num[i]);
424 case FT_RELATIVE_TIME:
426 printf(" %6d.%03d ", counters[i]/1000, counters[i]%1000);
439 printf("===================================================================\n");
449 const char *func_name;
453 static calc_type_ent_t calc_type_table[] = {
454 { "COUNT", CALC_TYPE_COUNT },
455 { "SUM", CALC_TYPE_SUM },
456 { "MIN", CALC_TYPE_MIN },
457 { "MAX", CALC_TYPE_MAX },
458 { "AVG", CALC_TYPE_AVG },
463 register_io_tap(io_stat_t *io, int i, const char *filter)
465 GString *error_string;
469 const char *p, *parenp;
471 header_field_info *hfi;
473 io->items[i].prev=&io->items[i];
474 io->items[i].next=NULL;
475 io->items[i].parent=io;
477 io->items[i].calc_type=CALC_TYPE_BYTES;
478 io->items[i].frames=0;
479 io->items[i].counter=0;
481 io->filters[i]=filter;
489 for(j=0; calc_type_table[j].func_name; j++){
490 namelen=strlen(calc_type_table[j].func_name);
491 if(strncmp(filter, calc_type_table[j].func_name, namelen) == 0
492 && *(filter+namelen)=='('){
493 io->items[i].calc_type=calc_type_table[j].calc_type;
496 parenp=strchr(p, ')');
498 fprintf(stderr, "tshark: Closing parenthesis missing from calculated expression.\n");
501 /* bail out if there was no field specified */
503 fprintf(stderr, "tshark: You didn't specify a field name for %s(*).\n",
504 calc_type_table[j].func_name);
507 field=g_malloc(parenp-p+1);
509 fprintf(stderr, "tshark: Out of memory.\n");
512 memcpy(field, p, parenp-p);
513 field[parenp-p] = '\0';
516 hfi=proto_registrar_get_byname(field);
518 fprintf(stderr, "tshark: There is no field named '%s'.\n",
524 io->items[i].hf_index=hfi->id;
528 if(io->items[i].calc_type!=CALC_TYPE_BYTES){
529 /* check that the type is compatible */
539 /* these types support all calculations */
541 case FT_RELATIVE_TIME:
542 /* this type only supports SUM, COUNT, MAX, MIN, AVG */
543 switch(io->items[i].calc_type){
545 case CALC_TYPE_COUNT:
552 "tshark: %s is a relative-time field, so %s(*) calculations are not supported on it.",
554 calc_type_table[j].func_name);
561 * XXX - support this if gint64/guint64 are
564 if(io->items[i].calc_type!=CALC_TYPE_COUNT){
566 "tshark: %s is a 64-bit integer, so %s(*) calculations are not supported on it.",
568 calc_type_table[j].func_name);
574 * XXX - support all operations on floating-point
577 if(io->items[i].calc_type!=CALC_TYPE_COUNT){
579 "tshark: %s doesn't have integral values, so %s(*) calculations are not supported on it.\n",
581 calc_type_table[j].func_name);
596 error_string=register_tap_listener("frame", &io->items[i], flt, NULL, iostat_packet, i?NULL:iostat_draw);
600 fprintf(stderr, "tshark: Couldn't register io,stat tap: %s\n",
602 g_string_free(error_string, TRUE);
608 iostat_init(const char *optarg, void* userdata _U_)
610 float interval_float;
614 const char *filter=NULL;
616 if(sscanf(optarg,"io,stat,%f,%n",&interval_float,&pos)==1){
623 fprintf(stderr, "tshark: invalid \"-z io,stat,<interval>[,<filter>]\" argument\n");
628 /* make interval be number of ms */
629 interval=(gint32)(interval_float*1000.0+0.9);
631 fprintf(stderr, "tshark: \"-z\" interval must be >=0.001 seconds.\n");
635 io=g_malloc(sizeof(io_stat_t));
636 io->interval=interval;
637 if((!filter)||(filter[0]==0)){
639 io->items=g_malloc(sizeof(io_stat_item_t)*io->num_items);
640 io->filters=g_malloc(sizeof(char *)*io->num_items);
642 register_io_tap(io, 0, NULL);
644 const char *str,*pos;
647 /* find how many ',' separated filters we have */
650 while((str=strchr(str,','))){
655 io->items=g_malloc(sizeof(io_stat_item_t)*io->num_items);
656 io->filters=g_malloc(sizeof(char *)*io->num_items);
658 /* for each filter, register a tap listener */
664 register_io_tap(io, i, NULL);
665 } else if(pos==NULL) {
667 register_io_tap(io, i, tmp);
669 tmp=g_malloc((pos-str)+1);
670 strncpy(tmp,str,(pos-str));
672 register_io_tap(io, i, tmp);
681 register_tap_listener_iostat(void)
683 register_stat_cmd_arg("io,stat,", iostat_init, NULL);