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>
44 typedef struct _io_stat_t {
45 gint32 interval; /* unit is ms */
47 struct _io_stat_item_t *items;
51 #define CALC_TYPE_BYTES 0
52 #define CALC_TYPE_COUNT 1
53 #define CALC_TYPE_SUM 2
54 #define CALC_TYPE_MIN 3
55 #define CALC_TYPE_MAX 4
56 #define CALC_TYPE_AVG 5
58 typedef struct _io_stat_item_t {
60 struct _io_stat_item_t *next;
61 struct _io_stat_item_t *prev;
62 gint32 time; /* unit is ms since start of capture */
72 iostat_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt, const void *dummy _U_)
74 io_stat_item_t *mit = arg;
80 current_time=(gint32) ((pinfo->fd->rel_ts.secs*1000)+(pinfo->fd->rel_ts.nsecs/1000000));
82 /* the prev item before the main one is always the last interval we saw packets for */
85 /* XXX for the time being, just ignore all frames that are in the past.
86 should be fixed in the future but hopefully it is uncommon */
87 if(current_time<it->time){
91 /* we have moved into a new interval, we need to create a new struct */
92 if(current_time>=(it->time+mit->parent->interval)){
93 it->next=g_malloc(sizeof(io_stat_item_t));
99 it->time=(current_time / mit->parent->interval) * mit->parent->interval;
103 it->calc_type=it->prev->calc_type;
104 it->hf_index=it->prev->hf_index;
107 /* it will now give us the current structure to use to store the data in */
110 switch(it->calc_type){
111 case CALC_TYPE_BYTES:
112 it->counter+=pinfo->fd->pkt_len;
114 case CALC_TYPE_COUNT:
115 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
117 it->counter+=gp->len;
121 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
123 for(i=0;i<gp->len;i++){
124 switch(proto_registrar_get_ftype(it->hf_index)){
129 it->counter+=fvalue_get_uinteger(&((field_info *)gp->pdata[i])->value);
132 it->counter+=fvalue_get_integer64(&((field_info *)gp->pdata[i])->value);
138 it->counter+=fvalue_get_sinteger(&((field_info *)gp->pdata[i])->value);
141 it->counter+=(gint64)fvalue_get_integer64(&((field_info *)gp->pdata[i])->value);
148 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
154 type=proto_registrar_get_ftype(it->hf_index);
155 for(i=0;i<gp->len;i++){
161 val=fvalue_get_uinteger(&((field_info *)gp->pdata[i])->value);
162 if((it->frames==1)&&(i==0)){
164 } else if(val<it->counter){
169 val=fvalue_get_integer64(&((field_info *)gp->pdata[i])->value);
170 if((it->frames==1)&&(i==0)){
172 } else if(val<it->counter){
180 val=fvalue_get_sinteger(&((field_info *)gp->pdata[i])->value);
181 if((it->frames==1)&&(i==0)){
183 } else if((gint32)val<(gint32)(it->counter)){
188 val=fvalue_get_integer64(&((field_info *)gp->pdata[i])->value);
189 if((it->frames==1)&&(i==0)){
191 } else if((gint64)val<(gint64)(it->counter)){
195 case FT_RELATIVE_TIME:
196 new_time=fvalue_get(&((field_info *)gp->pdata[i])->value);
197 val=(guint64) (new_time->secs*1000+new_time->nsecs/1000000);
198 if((it->frames==1)&&(i==0)){
200 } else if(val<it->counter){
209 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
215 type=proto_registrar_get_ftype(it->hf_index);
216 for(i=0;i<gp->len;i++){
222 val=fvalue_get_uinteger(&((field_info *)gp->pdata[i])->value);
223 if((it->frames==1)&&(i==0)){
225 } else if(val>it->counter){
230 val=fvalue_get_integer64(&((field_info *)gp->pdata[i])->value);
231 if((it->frames==1)&&(i==0)){
233 } else if(val>it->counter){
241 val=fvalue_get_sinteger(&((field_info *)gp->pdata[i])->value);
242 if((it->frames==1)&&(i==0)){
244 } else if((gint32)val>(gint32)(it->counter)){
249 val=fvalue_get_integer64(&((field_info *)gp->pdata[i])->value);
250 if((it->frames==1)&&(i==0)){
252 } else if((gint64)val>(gint64)(it->counter)){
256 case FT_RELATIVE_TIME:
257 new_time=fvalue_get(&((field_info *)gp->pdata[i])->value);
258 val=(guint64) (new_time->secs*1000+new_time->nsecs/1000000);
259 if((it->frames==1)&&(i==0)){
261 } else if(val>it->counter){
270 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
276 type=proto_registrar_get_ftype(it->hf_index);
277 for(i=0;i<gp->len;i++){
284 val=fvalue_get_uinteger(&((field_info *)gp->pdata[i])->value);
289 val=fvalue_get_integer64(&((field_info *)gp->pdata[i])->value);
296 val=fvalue_get_sinteger(&((field_info *)gp->pdata[i])->value);
299 case FT_RELATIVE_TIME:
300 new_time=fvalue_get(&((field_info *)gp->pdata[i])->value);
301 val=(guint64) (new_time->secs*1000+new_time->nsecs/1000000);
314 iostat_draw(void *arg)
316 io_stat_item_t *mit = arg;
318 io_stat_item_t **items;
329 printf("===================================================================\n");
330 printf("IO Statistics\n");
331 if(iot->interval!=G_MAXINT32)
332 printf("Interval: %d.%03d secs\n", iot->interval/1000, iot->interval%1000);
333 for(i=0;i<iot->num_items;i++){
334 printf("Column #%u: %s\n",i,iot->filters[i]?iot->filters[i]:"");
337 for(i=0;i<iot->num_items;i++){
338 printf("| Column #%-2u ",i);
342 for(i=0;i<iot->num_items;i++){
343 switch(iot->items[i].calc_type){
344 case CALC_TYPE_BYTES:
345 printf("|frames| bytes ");
347 case CALC_TYPE_COUNT:
366 items=g_malloc(sizeof(io_stat_item_t *)*iot->num_items);
367 frames=g_malloc(sizeof(guint64)*iot->num_items);
368 counters=g_malloc(sizeof(guint64)*iot->num_items);
369 num=g_malloc(sizeof(guint64)*iot->num_items);
370 /* preset all items at the first interval */
371 for(i=0;i<iot->num_items;i++){
372 items[i]=&iot->items[i];
375 /* loop the items until we run out of them all */
379 for(i=0;i<iot->num_items;i++){
384 for(i=0;i<iot->num_items;i++){
385 if(items[i] && (t>=(items[i]->time+iot->interval))){
386 items[i]=items[i]->next;
389 if(items[i] && (t<(items[i]->time+iot->interval)) && (t>=items[i]->time) ){
390 frames[i]=items[i]->frames;
391 counters[i]=items[i]->counter;
392 num[i]=items[i]->num;
401 if(iot->interval==G_MAXINT32) {
404 printf("%03d.%03d-%03d.%03d ",
406 (t+iot->interval)/1000,
407 (t+iot->interval)%1000);
409 for(i=0;i<iot->num_items;i++){
410 switch(iot->items[i].calc_type){
411 case CALC_TYPE_BYTES:
412 printf("%6" G_GINT64_MODIFIER "u %9" G_GINT64_MODIFIER "u ",frames[i], counters[i]);
414 case CALC_TYPE_COUNT:
415 printf(" %15" G_GINT64_MODIFIER "u ", counters[i]);
418 printf(" %15" G_GINT64_MODIFIER "u ", counters[i]);
421 switch(proto_registrar_get_ftype(iot->items[i].hf_index)){
427 printf(" %15" G_GINT64_MODIFIER "u ", counters[i]);
434 printf(" %15" G_GINT64_MODIFIER "d ", counters[i]);
436 case FT_RELATIVE_TIME:
437 printf(" %11" G_GINT64_MODIFIER "d.%03d ", counters[i]/1000, (gint)counters[i]%1000);
442 switch(proto_registrar_get_ftype(iot->items[i].hf_index)){
448 printf(" %15" G_GINT64_MODIFIER "u ", counters[i]);
455 printf(" %15" G_GINT64_MODIFIER "d ", counters[i]);
457 case FT_RELATIVE_TIME:
458 printf(" %11" G_GINT64_MODIFIER "d.%03d ", counters[i]/1000, (gint)counters[i]%1000);
466 switch(proto_registrar_get_ftype(iot->items[i].hf_index)){
472 printf(" %15" G_GINT64_MODIFIER "u ", counters[i]/num[i]);
479 printf(" %15" G_GINT64_MODIFIER "d ", counters[i]/num[i]);
481 case FT_RELATIVE_TIME:
483 printf(" %11" G_GINT64_MODIFIER "d.%03d ", counters[i]/1000, (gint)counters[i]%1000);
496 printf("===================================================================\n");
506 const char *func_name;
510 static calc_type_ent_t calc_type_table[] = {
511 { "COUNT", CALC_TYPE_COUNT },
512 { "SUM", CALC_TYPE_SUM },
513 { "MIN", CALC_TYPE_MIN },
514 { "MAX", CALC_TYPE_MAX },
515 { "AVG", CALC_TYPE_AVG },
520 register_io_tap(io_stat_t *io, int i, const char *filter)
522 GString *error_string;
526 const char *p, *parenp;
528 header_field_info *hfi;
530 io->items[i].prev=&io->items[i];
531 io->items[i].next=NULL;
532 io->items[i].parent=io;
534 io->items[i].calc_type=CALC_TYPE_BYTES;
535 io->items[i].frames=0;
536 io->items[i].counter=0;
538 io->filters[i]=filter;
543 for(j=0; calc_type_table[j].func_name; j++){
544 namelen=strlen(calc_type_table[j].func_name);
546 && strncmp(filter, calc_type_table[j].func_name, namelen) == 0
547 && *(filter+namelen)=='('){
548 io->items[i].calc_type=calc_type_table[j].calc_type;
551 parenp=strchr(p, ')');
553 fprintf(stderr, "tshark: Closing parenthesis missing from calculated expression.\n");
556 /* bail out if there was no field specified */
558 fprintf(stderr, "tshark: You didn't specify a field name for %s(*).\n",
559 calc_type_table[j].func_name);
562 field=g_malloc(parenp-p+1);
564 fprintf(stderr, "tshark: Out of memory.\n");
567 memcpy(field, p, parenp-p);
568 field[parenp-p] = '\0';
571 hfi=proto_registrar_get_byname(field);
573 fprintf(stderr, "tshark: There is no field named '%s'.\n",
579 io->items[i].hf_index=hfi->id;
583 if(hfi && io->items[i].calc_type!=CALC_TYPE_BYTES){
584 /* check that the type is compatible */
596 /* these types support all calculations */
598 case FT_RELATIVE_TIME:
599 /* this type only supports SUM, COUNT, MAX, MIN, AVG */
600 switch(io->items[i].calc_type){
602 case CALC_TYPE_COUNT:
609 "tshark: %s is a relative-time field, so %s(*) calculations are not supported on it.",
611 calc_type_table[j].func_name);
617 * XXX - support all operations on floating-point
620 if(io->items[i].calc_type!=CALC_TYPE_COUNT){
622 "tshark: %s doesn't have integral values, so %s(*) calculations are not supported on it.\n",
624 calc_type_table[j].func_name);
639 error_string=register_tap_listener("frame", &io->items[i], flt, TL_REQUIRES_PROTO_TREE, NULL, iostat_packet, i?NULL:iostat_draw);
643 fprintf(stderr, "tshark: Couldn't register io,stat tap: %s\n",
645 g_string_free(error_string, TRUE);
651 iostat_init(const char *optarg, void* userdata _U_)
653 float interval_float;
657 const char *filter=NULL;
659 if(sscanf(optarg,"io,stat,%f,%n",&interval_float,&idx)==1){
666 fprintf(stderr, "tshark: invalid \"-z io,stat,<interval>[,<filter>]\" argument\n");
670 /* if interval is 0, calculate statistics over the whole file
671 * by setting the interval to G_MAXINT32
673 if(interval_float==0) {
676 /* make interval be number of ms */
677 interval=(gint32)(interval_float*1000.0+0.9);
681 fprintf(stderr, "tshark: \"-z\" interval must be >=0.001 seconds or 0.\n");
685 io=g_malloc(sizeof(io_stat_t));
686 io->interval=interval;
687 if((!filter)||(filter[0]==0)){
689 io->items=g_malloc(sizeof(io_stat_item_t)*io->num_items);
690 io->filters=g_malloc(sizeof(char *)*io->num_items);
692 register_io_tap(io, 0, NULL);
694 const char *str,*pos;
697 /* find how many ',' separated filters we have */
700 while((str=strchr(str,','))){
705 io->items=g_malloc(sizeof(io_stat_item_t)*io->num_items);
706 io->filters=g_malloc(sizeof(char *)*io->num_items);
708 /* for each filter, register a tap listener */
714 register_io_tap(io, i, NULL);
715 } else if(pos==NULL) {
717 register_io_tap(io, i, tmp);
719 tmp=g_malloc((pos-str)+1);
720 g_strlcpy(tmp,str,(pos-str)+1);
721 register_io_tap(io, i, tmp);
730 register_tap_listener_iostat(void)
732 register_stat_cmd_arg("io,stat,", iostat_init, NULL);