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);
135 it->counter+=fvalue_get_sinteger(&((field_info *)gp->pdata[i])->value);
142 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
148 type=proto_registrar_get_ftype(it->hf_index);
149 for(i=0;i<gp->len;i++){
155 val=fvalue_get_uinteger(&((field_info *)gp->pdata[i])->value);
156 if((it->frames==1)&&(i==0)){
158 } else if(val<it->counter){
166 val=fvalue_get_sinteger(&((field_info *)gp->pdata[i])->value);
167 if((it->frames==1)&&(i==0)){
169 } else if((gint32)val<(gint32)(it->counter)){
173 case FT_RELATIVE_TIME:
174 new_time=fvalue_get(&((field_info *)gp->pdata[i])->value);
175 val=(guint32) (new_time->secs*1000+new_time->nsecs/1000000);
176 if((it->frames==1)&&(i==0)){
178 } else if(val<it->counter){
187 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
193 type=proto_registrar_get_ftype(it->hf_index);
194 for(i=0;i<gp->len;i++){
200 val=fvalue_get_uinteger(&((field_info *)gp->pdata[i])->value);
201 if((it->frames==1)&&(i==0)){
203 } else if(val>it->counter){
211 val=fvalue_get_sinteger(&((field_info *)gp->pdata[i])->value);
212 if((it->frames==1)&&(i==0)){
214 } else if((gint32)val>(gint32)(it->counter)){
218 case FT_RELATIVE_TIME:
219 new_time=fvalue_get(&((field_info *)gp->pdata[i])->value);
220 val=(guint32) (new_time->secs*1000+new_time->nsecs/1000000);
221 if((it->frames==1)&&(i==0)){
223 } else if(val>it->counter){
232 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
238 type=proto_registrar_get_ftype(it->hf_index);
239 for(i=0;i<gp->len;i++){
246 val=fvalue_get_uinteger(&((field_info *)gp->pdata[i])->value);
253 val=fvalue_get_sinteger(&((field_info *)gp->pdata[i])->value);
256 case FT_RELATIVE_TIME:
257 new_time=fvalue_get(&((field_info *)gp->pdata[i])->value);
258 val=(guint32) (new_time->secs*1000+new_time->nsecs/1000000);
271 iostat_draw(void *arg)
273 io_stat_item_t *mit = arg;
275 io_stat_item_t **items;
279 guint32 i,more_items;
285 printf("===================================================================\n");
286 printf("IO Statistics\n");
287 if(iot->interval!=G_MAXINT32)
288 printf("Interval: %d.%03d secs\n", iot->interval/1000, iot->interval%1000);
289 for(i=0;i<iot->num_items;i++){
290 printf("Column #%d: %s\n",i,iot->filters[i]?iot->filters[i]:"");
293 for(i=0;i<iot->num_items;i++){
294 printf("| Column #%-2d ",i);
298 for(i=0;i<iot->num_items;i++){
299 switch(iot->items[i].calc_type){
300 case CALC_TYPE_BYTES:
301 printf("|frames| bytes ");
303 case CALC_TYPE_COUNT:
322 items=g_malloc(sizeof(io_stat_item_t *)*iot->num_items);
323 frames=g_malloc(sizeof(guint32)*iot->num_items);
324 counters=g_malloc(sizeof(guint32)*iot->num_items);
325 num=g_malloc(sizeof(guint32)*iot->num_items);
326 /* preset all items at the first interval */
327 for(i=0;i<iot->num_items;i++){
328 items[i]=&iot->items[i];
331 /* loop the items until we run out of them all */
335 for(i=0;i<iot->num_items;i++){
340 for(i=0;i<iot->num_items;i++){
341 if(items[i] && (t>=(items[i]->time+iot->interval))){
342 items[i]=items[i]->next;
345 if(items[i] && (t<(items[i]->time+iot->interval)) && (t>=items[i]->time) ){
346 frames[i]=items[i]->frames;
347 counters[i]=items[i]->counter;
348 num[i]=items[i]->num;
357 if(iot->interval==G_MAXINT32) {
360 printf("%03d.%03d-%03d.%03d ",
362 (t+iot->interval)/1000,
363 (t+iot->interval)%1000);
365 for(i=0;i<iot->num_items;i++){
366 switch(iot->items[i].calc_type){
367 case CALC_TYPE_BYTES:
368 printf("%6d %9d ",frames[i],counters[i]);
370 case CALC_TYPE_COUNT:
371 printf(" %8d ", counters[i]);
374 printf(" %8d ", counters[i]);
377 switch(proto_registrar_get_ftype(iot->items[i].hf_index)){
382 printf(" %8u ", counters[i]);
388 printf(" %8d ", counters[i]);
390 case FT_RELATIVE_TIME:
391 printf(" %6d.%03d ", counters[i]/1000, counters[i]%1000);
396 switch(proto_registrar_get_ftype(iot->items[i].hf_index)){
401 printf(" %8u ", counters[i]);
407 printf(" %8d ", counters[i]);
409 case FT_RELATIVE_TIME:
410 printf(" %6d.%03d ", counters[i]/1000, counters[i]%1000);
418 switch(proto_registrar_get_ftype(iot->items[i].hf_index)){
423 printf(" %8u ", counters[i]/num[i]);
429 printf(" %8d ", counters[i]/num[i]);
431 case FT_RELATIVE_TIME:
433 printf(" %6d.%03d ", counters[i]/1000, counters[i]%1000);
446 printf("===================================================================\n");
456 const char *func_name;
460 static calc_type_ent_t calc_type_table[] = {
461 { "COUNT", CALC_TYPE_COUNT },
462 { "SUM", CALC_TYPE_SUM },
463 { "MIN", CALC_TYPE_MIN },
464 { "MAX", CALC_TYPE_MAX },
465 { "AVG", CALC_TYPE_AVG },
470 register_io_tap(io_stat_t *io, int i, const char *filter)
472 GString *error_string;
476 const char *p, *parenp;
478 header_field_info *hfi;
480 io->items[i].prev=&io->items[i];
481 io->items[i].next=NULL;
482 io->items[i].parent=io;
484 io->items[i].calc_type=CALC_TYPE_BYTES;
485 io->items[i].frames=0;
486 io->items[i].counter=0;
488 io->filters[i]=filter;
493 for(j=0; calc_type_table[j].func_name; j++){
494 namelen=strlen(calc_type_table[j].func_name);
496 && strncmp(filter, calc_type_table[j].func_name, namelen) == 0
497 && *(filter+namelen)=='('){
498 io->items[i].calc_type=calc_type_table[j].calc_type;
501 parenp=strchr(p, ')');
503 fprintf(stderr, "tshark: Closing parenthesis missing from calculated expression.\n");
506 /* bail out if there was no field specified */
508 fprintf(stderr, "tshark: You didn't specify a field name for %s(*).\n",
509 calc_type_table[j].func_name);
512 field=g_malloc(parenp-p+1);
514 fprintf(stderr, "tshark: Out of memory.\n");
517 memcpy(field, p, parenp-p);
518 field[parenp-p] = '\0';
521 hfi=proto_registrar_get_byname(field);
523 fprintf(stderr, "tshark: There is no field named '%s'.\n",
529 io->items[i].hf_index=hfi->id;
533 if(io->items[i].calc_type!=CALC_TYPE_BYTES){
534 /* check that the type is compatible */
544 /* these types support all calculations */
546 case FT_RELATIVE_TIME:
547 /* this type only supports SUM, COUNT, MAX, MIN, AVG */
548 switch(io->items[i].calc_type){
550 case CALC_TYPE_COUNT:
557 "tshark: %s is a relative-time field, so %s(*) calculations are not supported on it.",
559 calc_type_table[j].func_name);
566 * XXX - support this if gint64/guint64 are
569 if(io->items[i].calc_type!=CALC_TYPE_COUNT){
571 "tshark: %s is a 64-bit integer, so %s(*) calculations are not supported on it.",
573 calc_type_table[j].func_name);
579 * XXX - support all operations on floating-point
582 if(io->items[i].calc_type!=CALC_TYPE_COUNT){
584 "tshark: %s doesn't have integral values, so %s(*) calculations are not supported on it.\n",
586 calc_type_table[j].func_name);
601 error_string=register_tap_listener("frame", &io->items[i], flt, TL_REQUIRES_PROTO_TREE, NULL, iostat_packet, i?NULL:iostat_draw);
605 fprintf(stderr, "tshark: Couldn't register io,stat tap: %s\n",
607 g_string_free(error_string, TRUE);
613 iostat_init(const char *optarg, void* userdata _U_)
615 float interval_float;
619 const char *filter=NULL;
621 if(sscanf(optarg,"io,stat,%f,%n",&interval_float,&pos)==1){
628 fprintf(stderr, "tshark: invalid \"-z io,stat,<interval>[,<filter>]\" argument\n");
632 /* if interval is 0, calculate statistics over the whole file
633 * by setting the interval to G_MAXINT32
635 if(interval_float==0) {
638 /* make interval be number of ms */
639 interval=(gint32)(interval_float*1000.0+0.9);
643 fprintf(stderr, "tshark: \"-z\" interval must be >=0.001 seconds or 0.\n");
647 io=g_malloc(sizeof(io_stat_t));
648 io->interval=interval;
649 if((!filter)||(filter[0]==0)){
651 io->items=g_malloc(sizeof(io_stat_item_t)*io->num_items);
652 io->filters=g_malloc(sizeof(char *)*io->num_items);
654 register_io_tap(io, 0, NULL);
656 const char *str,*pos;
659 /* find how many ',' separated filters we have */
662 while((str=strchr(str,','))){
667 io->items=g_malloc(sizeof(io_stat_item_t)*io->num_items);
668 io->filters=g_malloc(sizeof(char *)*io->num_items);
670 /* for each filter, register a tap listener */
676 register_io_tap(io, i, NULL);
677 } else if(pos==NULL) {
679 register_io_tap(io, i, tmp);
681 tmp=g_malloc((pos-str)+1);
682 g_strlcpy(tmp,str,(pos-str)+1);
683 register_io_tap(io, i, tmp);
692 register_tap_listener_iostat(void)
694 register_stat_cmd_arg("io,stat,", iostat_init, NULL);