df4a1a149089afb5d38d3b209950913c67d6c6ce
[obnox/wireshark/wip.git] / tap-iostat.c
1 /* tap-iostat.c
2  * iostat   2002 Ronnie Sahlberg
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
8  * Copyright 1998 Gerald Combs
9  * 
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.
14  * 
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.
19  * 
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.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <stdio.h>
30
31 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
33 #endif
34
35 #include <string.h>
36 #include "epan/epan_dissect.h"
37 #include "epan/packet_info.h"
38 #include <epan/tap.h>
39 #include <epan/stat_cmd_args.h>
40 #include <epan/strutil.h>
41 #include "register.h"
42
43
44 typedef struct _io_stat_t {
45         gint32 interval;        /* unit is ms */
46         guint32 num_items;
47         struct _io_stat_item_t *items;
48         const char **filters;
49 } io_stat_t;    
50
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
57
58 typedef struct _io_stat_item_t {
59         io_stat_t *parent;
60         struct _io_stat_item_t *next;
61         struct _io_stat_item_t *prev;
62         gint32 time;            /* unit is ms since start of capture */
63         int calc_type;
64         int hf_index;
65         guint32 frames;
66         guint32 num;
67         guint32 counter;
68 } io_stat_item_t;
69
70
71 static int
72 iostat_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void *dummy _U_)
73 {
74         io_stat_item_t *mit = arg;
75         io_stat_item_t *it;
76         gint32 current_time;
77         GPtrArray *gp;
78         guint i;
79
80         current_time=(gint32) ((pinfo->fd->rel_ts.secs*1000)+(pinfo->fd->rel_ts.nsecs/1000000));
81
82         /* the prev item before the main one is always the last interval we saw packets for */
83         it=mit->prev;
84
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){
88                 return FALSE;
89         }
90
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));
94                 it->next->prev=it;
95                 it->next->next=NULL;
96                 it=it->next;
97                 mit->prev=it;
98
99                 it->time=(current_time / mit->parent->interval) * mit->parent->interval;
100                 it->frames=0;
101                 it->counter=0;
102                 it->num=0;
103                 it->calc_type=it->prev->calc_type;
104                 it->hf_index=it->prev->hf_index;
105         }
106
107         /* it will now give us the current structure to use to store the data in */
108         it->frames++;
109
110         switch(it->calc_type){
111         case CALC_TYPE_BYTES:
112                 it->counter+=pinfo->fd->pkt_len;
113                 break;
114         case CALC_TYPE_COUNT:
115                 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
116                 if(gp){
117                         it->counter+=gp->len;
118                 }
119                 break;
120         case CALC_TYPE_SUM:
121                 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
122                 if(gp){
123                         for(i=0;i<gp->len;i++){
124                                 switch(proto_registrar_get_ftype(it->hf_index)){
125                                 case FT_UINT8:
126                                 case FT_UINT16:
127                                 case FT_UINT24:
128                                 case FT_UINT32:
129                                         it->counter+=fvalue_get_uinteger(&((field_info *)gp->pdata[i])->value);
130                                         break;
131                                 case FT_INT8:
132                                 case FT_INT16:
133                                 case FT_INT24:
134                                 case FT_INT32:
135                                         it->counter+=fvalue_get_sinteger(&((field_info *)gp->pdata[i])->value);
136                                         break;
137                                 }
138                         }
139                 }
140                 break;
141         case CALC_TYPE_MIN:
142                 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
143                 if(gp){
144                         int type;
145                         guint32 val;
146                         nstime_t *new_time;
147
148                         type=proto_registrar_get_ftype(it->hf_index);
149                         for(i=0;i<gp->len;i++){
150                                 switch(type){
151                                 case FT_UINT8:
152                                 case FT_UINT16:
153                                 case FT_UINT24:
154                                 case FT_UINT32:
155                                         val=fvalue_get_uinteger(&((field_info *)gp->pdata[i])->value);
156                                         if((it->frames==1)&&(i==0)){
157                                                 it->counter=val;
158                                         } else if(val<it->counter){
159                                                 it->counter=val;
160                                         }                               
161                                         break;
162                                 case FT_INT8:
163                                 case FT_INT16:
164                                 case FT_INT24:
165                                 case FT_INT32:
166                                         val=fvalue_get_sinteger(&((field_info *)gp->pdata[i])->value);
167                                         if((it->frames==1)&&(i==0)){
168                                                 it->counter=val;
169                                         } else if((gint32)val<(gint32)(it->counter)){
170                                                 it->counter=val;
171                                         }
172                                         break;
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)){
177                                                 it->counter=val;
178                                         } else if(val<it->counter){
179                                                 it->counter=val;
180                                         }                               
181                                         break;
182                                 }
183                         }
184                 }
185                 break;
186         case CALC_TYPE_MAX:
187                 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
188                 if(gp){
189                         int type;
190                         guint32 val;
191                         nstime_t *new_time;
192
193                         type=proto_registrar_get_ftype(it->hf_index);
194                         for(i=0;i<gp->len;i++){
195                                 switch(type){
196                                 case FT_UINT8:
197                                 case FT_UINT16:
198                                 case FT_UINT24:
199                                 case FT_UINT32:
200                                         val=fvalue_get_uinteger(&((field_info *)gp->pdata[i])->value);
201                                         if((it->frames==1)&&(i==0)){
202                                                 it->counter=val;
203                                         } else if(val>it->counter){
204                                                 it->counter=val;
205                                         }                               
206                                         break;
207                                 case FT_INT8:
208                                 case FT_INT16:
209                                 case FT_INT24:
210                                 case FT_INT32:
211                                         val=fvalue_get_sinteger(&((field_info *)gp->pdata[i])->value);
212                                         if((it->frames==1)&&(i==0)){
213                                                 it->counter=val;
214                                         } else if((gint32)val>(gint32)(it->counter)){
215                                                 it->counter=val;
216                                         }                               
217                                         break;
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)){
222                                                 it->counter=val;
223                                         } else if(val>it->counter){
224                                                 it->counter=val;
225                                         }                               
226                                         break;
227                                 }
228                         }
229                 }
230                 break;
231         case CALC_TYPE_AVG:
232                 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
233                 if(gp){
234                         int type;
235                         guint32 val;
236                         nstime_t *new_time;
237
238                         type=proto_registrar_get_ftype(it->hf_index);
239                         for(i=0;i<gp->len;i++){
240                                 it->num++;
241                                 switch(type){
242                                 case FT_UINT8:
243                                 case FT_UINT16:
244                                 case FT_UINT24:
245                                 case FT_UINT32:
246                                         val=fvalue_get_uinteger(&((field_info *)gp->pdata[i])->value);
247                                         it->counter+=val;
248                                         break;
249                                 case FT_INT8:
250                                 case FT_INT16:
251                                 case FT_INT24:
252                                 case FT_INT32:
253                                         val=fvalue_get_sinteger(&((field_info *)gp->pdata[i])->value);
254                                         it->counter+=val;
255                                         break;
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);
259                                         it->counter+=val;
260                                         break;
261                                 }
262                         }
263                 }
264                 break;
265         }
266
267         return TRUE;
268 }
269
270 static void
271 iostat_draw(void *arg)
272 {
273         io_stat_item_t *mit = arg;
274         io_stat_t *iot;
275         io_stat_item_t **items;
276         guint32 *frames;
277         guint32 *counters;
278         guint32 *num;
279         guint32 i,more_items;
280         gint t;
281
282         iot=mit->parent;
283
284         printf("\n");
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]:"");
291         }
292         printf("                ");
293         for(i=0;i<iot->num_items;i++){
294                 printf("|   Column #%-2d   ",i);
295         }
296         printf("\n");
297         printf("Time            ");
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  ");
302                         break;
303                 case CALC_TYPE_COUNT:
304                         printf("|          COUNT ");
305                         break;
306                 case CALC_TYPE_SUM:
307                         printf("|            SUM ");
308                         break;
309                 case CALC_TYPE_MIN:
310                         printf("|            MIN ");
311                         break;
312                 case CALC_TYPE_MAX:
313                         printf("|            MAX ");
314                         break;
315                 case CALC_TYPE_AVG:
316                         printf("|            AVG ");
317                         break;
318                 }
319         }
320         printf("\n");
321
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];
329         }
330
331         /* loop the items until we run out of them all */
332         t=0;
333         do {
334                 more_items=0;
335                 for(i=0;i<iot->num_items;i++){
336                         frames[i]=0;
337                         counters[i]=0;
338                         num[i]=0;
339                 }
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;
343                         }
344
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;
349                         }
350
351                         if(items[i]){
352                                 more_items=1;
353                         }
354                 }
355
356                 if(more_items){
357                         if(iot->interval==G_MAXINT32) {
358                                 printf("000.000-         ");
359                         } else {
360                                 printf("%03d.%03d-%03d.%03d  ",
361                                         t/1000,t%1000,
362                                         (t+iot->interval)/1000,
363                                         (t+iot->interval)%1000);
364                         }
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]);
369                                         break;
370                                 case CALC_TYPE_COUNT:
371                                         printf("        %8d ", counters[i]);
372                                         break;
373                                 case CALC_TYPE_SUM:
374                                         printf("        %8d ", counters[i]);
375                                         break;
376                                 case CALC_TYPE_MIN:
377                                         switch(proto_registrar_get_ftype(iot->items[i].hf_index)){
378                                         case FT_UINT8:
379                                         case FT_UINT16:
380                                         case FT_UINT24:
381                                         case FT_UINT32:
382                                                 printf("        %8u ", counters[i]);
383                                                 break;
384                                         case FT_INT8:
385                                         case FT_INT16:
386                                         case FT_INT24:
387                                         case FT_INT32:
388                                                 printf("        %8d ", counters[i]);
389                                                 break;
390                                         case FT_RELATIVE_TIME:
391                                                 printf("      %6d.%03d ", counters[i]/1000, counters[i]%1000);
392                                                 break;
393                                         }
394                                         break;
395                                 case CALC_TYPE_MAX:
396                                         switch(proto_registrar_get_ftype(iot->items[i].hf_index)){
397                                         case FT_UINT8:
398                                         case FT_UINT16:
399                                         case FT_UINT24:
400                                         case FT_UINT32:
401                                                 printf("        %8u ", counters[i]);
402                                                 break;
403                                         case FT_INT8:
404                                         case FT_INT16:
405                                         case FT_INT24:
406                                         case FT_INT32:
407                                                 printf("        %8d ", counters[i]);
408                                                 break;
409                                         case FT_RELATIVE_TIME:
410                                                 printf("      %6d.%03d ", counters[i]/1000, counters[i]%1000);
411                                                 break;
412                                         }
413                                         break;
414                                 case CALC_TYPE_AVG:
415                                         if(num[i]==0){
416                                                 num[i]=1;
417                                         }
418                                         switch(proto_registrar_get_ftype(iot->items[i].hf_index)){
419                                         case FT_UINT8:
420                                         case FT_UINT16:
421                                         case FT_UINT24:
422                                         case FT_UINT32:
423                                                 printf("        %8u ", counters[i]/num[i]);
424                                                 break;
425                                         case FT_INT8:
426                                         case FT_INT16:
427                                         case FT_INT24:
428                                         case FT_INT32:
429                                                 printf("        %8d ", counters[i]/num[i]);
430                                                 break;
431                                         case FT_RELATIVE_TIME:
432                                                 counters[i]/=num[i];
433                                                 printf("      %6d.%03d ", counters[i]/1000, counters[i]%1000);
434                                                 break;
435                                         }
436                                         break;
437
438                                 }
439                         }
440                         printf("\n");
441                 }
442
443                 t+=iot->interval;
444         } while(more_items);
445
446         printf("===================================================================\n");
447
448         g_free(items);
449         g_free(frames);
450         g_free(counters);
451         g_free(num);
452 }
453
454
455 typedef struct {
456         const char *func_name;
457         int calc_type;
458 } calc_type_ent_t;
459
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 },
466         { NULL, 0 }
467 };
468
469 static void
470 register_io_tap(io_stat_t *io, int i, const char *filter)
471 {
472         GString *error_string;
473         const char *flt;
474         int j;
475         size_t namelen;
476         const char *p, *parenp;
477         char *field;
478         header_field_info *hfi;
479
480         io->items[i].prev=&io->items[i];
481         io->items[i].next=NULL;
482         io->items[i].parent=io;
483         io->items[i].time=0;
484         io->items[i].calc_type=CALC_TYPE_BYTES;
485         io->items[i].frames=0;
486         io->items[i].counter=0;
487         io->items[i].num=0;
488         io->filters[i]=filter;
489         flt=filter;
490
491         if(!filter){
492                 filter="";
493         }
494         field=NULL;
495         hfi=NULL;
496         for(j=0; calc_type_table[j].func_name; j++){
497                 namelen=strlen(calc_type_table[j].func_name);
498                 if(strncmp(filter, calc_type_table[j].func_name, namelen) == 0
499                     && *(filter+namelen)=='('){
500                         io->items[i].calc_type=calc_type_table[j].calc_type;
501
502                         p=filter+namelen+1;
503                         parenp=strchr(p, ')');
504                         if(!parenp){
505                                 fprintf(stderr, "tshark: Closing parenthesis missing from calculated expression.\n");
506                                 exit(10);
507                         }
508                         /* bail out if there was no field specified */
509                         if(parenp==p){
510                                 fprintf(stderr, "tshark: You didn't specify a field name for %s(*).\n",
511                                     calc_type_table[j].func_name);
512                                 exit(10);
513                         }
514                         field=g_malloc(parenp-p+1);
515                         if(!field){
516                                 fprintf(stderr, "tshark: Out of memory.\n");
517                                 exit(10);
518                         }
519                         memcpy(field, p, parenp-p);
520                         field[parenp-p] = '\0';
521                         flt=parenp + 1;
522
523                         hfi=proto_registrar_get_byname(field);
524                         if(!hfi){
525                                 fprintf(stderr, "tshark: There is no field named '%s'.\n",
526                                     field);
527                                 g_free(field);
528                                 exit(10);
529                         }
530         
531                         io->items[i].hf_index=hfi->id;
532                         break;
533                 }
534         }
535         if(io->items[i].calc_type!=CALC_TYPE_BYTES){
536                 /* check that the type is compatible */
537                 switch(hfi->type){
538                 case FT_UINT8:
539                 case FT_UINT16:
540                 case FT_UINT24:
541                 case FT_UINT32:
542                 case FT_INT8:
543                 case FT_INT16:
544                 case FT_INT24:
545                 case FT_INT32:
546                         /* these types support all calculations */
547                         break;
548                 case FT_RELATIVE_TIME:
549                         /* this type only supports SUM, COUNT, MAX, MIN, AVG */
550                         switch(io->items[i].calc_type){
551                         case CALC_TYPE_SUM:
552                         case CALC_TYPE_COUNT:
553                         case CALC_TYPE_MAX:
554                         case CALC_TYPE_MIN:
555                         case CALC_TYPE_AVG:
556                                 break;
557                         default:
558                                 fprintf(stderr,
559                                     "tshark: %s is a relative-time field, so %s(*) calculations are not supported on it.",
560                                     field,
561                                     calc_type_table[j].func_name);
562                                 exit(10);
563                         }
564                         break;
565                 case FT_UINT64:
566                 case FT_INT64:
567                         /*
568                          * XXX - support this if gint64/guint64 are
569                          * available?
570                          */
571                         if(io->items[i].calc_type!=CALC_TYPE_COUNT){
572                                 fprintf(stderr,
573                                     "tshark: %s is a 64-bit integer, so %s(*) calculations are not supported on it.",
574                                     field,
575                                     calc_type_table[j].func_name);
576                                 exit(10);
577                         }
578                         break;
579                 default:
580                         /*
581                          * XXX - support all operations on floating-point
582                          * numbers?
583                          */
584                         if(io->items[i].calc_type!=CALC_TYPE_COUNT){
585                                 fprintf(stderr,
586                                     "tshark: %s doesn't have integral values, so %s(*) calculations are not supported on it.\n",
587                                     field,
588                                     calc_type_table[j].func_name);
589                                 exit(10);
590                         }
591                         break;
592                 }
593                 g_free(field);
594         }
595
596 /*
597 CALC_TYPE_SUM   2
598 CALC_TYPE_MIN   3
599 CALC_TYPE_MAX   4
600 CALC_TYPE_AVG   5
601 */
602
603         error_string=register_tap_listener("frame", &io->items[i], flt, NULL, iostat_packet, i?NULL:iostat_draw);
604         if(error_string){
605                 g_free(io->items);
606                 g_free(io);
607                 fprintf(stderr, "tshark: Couldn't register io,stat tap: %s\n",
608                     error_string->str);
609                 g_string_free(error_string, TRUE);
610                 exit(1);
611         }
612 }
613
614 void
615 iostat_init(const char *optarg, void* userdata _U_)
616 {
617         float interval_float;
618         gint32 interval; 
619         int pos=0;
620         io_stat_t *io;
621         const char *filter=NULL;
622
623         if(sscanf(optarg,"io,stat,%f,%n",&interval_float,&pos)==1){
624                 if(pos){
625                         filter=optarg+pos;
626                 } else {
627                         filter=NULL;
628                 }
629         } else {
630                 fprintf(stderr, "tshark: invalid \"-z io,stat,<interval>[,<filter>]\" argument\n");
631                 exit(1);
632         }
633
634         /* if interval is 0, calculate statistics over the whole file
635          * by setting the interval to G_MAXINT32
636          */
637         if(interval_float==0) {
638                 interval=G_MAXINT32;
639         } else {
640                 /* make interval be number of ms */
641                 interval=(gint32)(interval_float*1000.0+0.9);   
642         }
643
644         if(interval<1){
645                 fprintf(stderr, "tshark: \"-z\" interval must be >=0.001 seconds or 0.\n");
646                 exit(10);
647         }
648         
649         io=g_malloc(sizeof(io_stat_t));
650         io->interval=interval;
651         if((!filter)||(filter[0]==0)){
652                 io->num_items=1;
653                 io->items=g_malloc(sizeof(io_stat_item_t)*io->num_items);
654                 io->filters=g_malloc(sizeof(char *)*io->num_items);
655
656                 register_io_tap(io, 0, NULL);
657         } else {
658                 const char *str,*pos;
659                 char *tmp;
660                 int i;
661                 /* find how many ',' separated filters we have */
662                 str=filter;
663                 io->num_items=1;
664                 while((str=strchr(str,','))){
665                         io->num_items++;
666                         str++;
667                 }
668
669                 io->items=g_malloc(sizeof(io_stat_item_t)*io->num_items);
670                 io->filters=g_malloc(sizeof(char *)*io->num_items);
671
672                 /* for each filter, register a tap listener */          
673                 i=0;
674                 str=filter;
675                 do{
676                         pos=strchr(str,',');
677                         if(pos==str){
678                                 register_io_tap(io, i, NULL);
679                         } else if(pos==NULL) {
680                                 tmp=g_strdup(str);
681                                 register_io_tap(io, i, tmp);
682                         } else {
683                                 tmp=g_malloc((pos-str)+1);
684                                 g_strlcpy(tmp,str,(pos-str)+1);
685                                 register_io_tap(io, i, tmp);
686                         }
687                         str=pos+1;
688                         i++;                    
689                 } while(pos);
690         }                       
691 }
692
693 void
694 register_tap_listener_iostat(void)
695 {
696         register_stat_cmd_arg("io,stat,", iostat_init, NULL);
697 }