EVERYTHING IN THE BUILDBOT IS GOING TO BE RED!!! Sorry!
[obnox/wireshark/wip.git] / tap-iostat.c
1 /* tap-iostat.c
2  * iostat   2002 Ronnie Sahlberg
3  *
4  * $Id$
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
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 "register.h"
41
42
43 typedef struct _io_stat_t {
44         gint32 interval;        /* unit is ms */
45         guint32 num_items;
46         struct _io_stat_item_t *items;
47         const char **filters;
48 } io_stat_t;    
49
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
56
57 typedef struct _io_stat_item_t {
58         io_stat_t *parent;
59         struct _io_stat_item_t *next;
60         struct _io_stat_item_t *prev;
61         gint32 time;            /* unit is ms since start of capture */
62         int calc_type;
63         int hf_index;
64         guint32 frames;
65         guint32 num;
66         guint32 counter;
67 } io_stat_item_t;
68
69
70 static int
71 iostat_packet(void *arg, packet_info *pinfo, epan_dissect_t *edt _U_, const void *dummy _U_)
72 {
73         io_stat_item_t *mit = arg;
74         io_stat_item_t *it;
75         gint32 current_time;
76         GPtrArray *gp;
77         guint i;
78
79         current_time=((pinfo->fd->rel_ts.secs*1000)+(pinfo->fd->rel_ts.nsecs/1000000));
80
81         /* the prev item before the main one is always the last interval we saw packets for */
82         it=mit->prev;
83
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){
87                 return FALSE;
88         }
89
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));
93                 it->next->prev=it;
94                 it->next->next=NULL;
95                 it=it->next;
96                 mit->prev=it;
97
98                 it->time=(current_time / mit->parent->interval) * mit->parent->interval;
99                 it->frames=0;
100                 it->counter=0;
101                 it->num=0;
102                 it->calc_type=it->prev->calc_type;
103                 it->hf_index=it->prev->hf_index;
104         }
105
106         /* it will now give us the current structure to use to store the data in */
107         it->frames++;
108
109         switch(it->calc_type){
110         case CALC_TYPE_BYTES:
111                 it->counter+=pinfo->fd->pkt_len;
112                 break;
113         case CALC_TYPE_COUNT:
114                 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
115                 if(gp){
116                         it->counter+=gp->len;
117                 }
118                 break;
119         case CALC_TYPE_SUM:
120                 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
121                 if(gp){
122                         for(i=0;i<gp->len;i++){
123                                 it->counter+=fvalue_get_integer(&((field_info *)gp->pdata[i])->value);
124                         }
125                 }
126                 break;
127         case CALC_TYPE_MIN:
128                 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
129                 if(gp){
130                         int type;
131                         guint32 val;
132                         nstime_t *new_time;
133
134                         type=proto_registrar_get_ftype(it->hf_index);
135                         for(i=0;i<gp->len;i++){
136                                 switch(type){
137                                 case FT_UINT8:
138                                 case FT_UINT16:
139                                 case FT_UINT24:
140                                 case FT_UINT32:
141                                         val=fvalue_get_integer(&((field_info *)gp->pdata[i])->value);
142                                         if((it->frames==1)&&(i==0)){
143                                                 it->counter=val;
144                                         } else if(val<it->counter){
145                                                 it->counter=val;
146                                         }                               
147                                         break;
148                                 case FT_INT8:
149                                 case FT_INT16:
150                                 case FT_INT24:
151                                 case FT_INT32:
152                                         val=fvalue_get_integer(&((field_info *)gp->pdata[i])->value);
153                                         if((it->frames==1)&&(i==0)){
154                                                 it->counter=val;
155                                         } else if((gint32)val<(gint32)(it->counter)){
156                                                 it->counter=val;
157                                         }                               
158                                         break;
159                                 case FT_RELATIVE_TIME:
160                                         new_time=fvalue_get(&((field_info *)gp->pdata[i])->value);
161                                         val=new_time->secs*1000+new_time->nsecs/1000000;
162                                         if((it->frames==1)&&(i==0)){
163                                                 it->counter=val;
164                                         } else if(val<it->counter){
165                                                 it->counter=val;
166                                         }                               
167                                         break;
168                                 }
169                         }
170                 }
171                 break;
172         case CALC_TYPE_MAX:
173                 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
174                 if(gp){
175                         int type;
176                         guint32 val;
177                         nstime_t *new_time;
178
179                         type=proto_registrar_get_ftype(it->hf_index);
180                         for(i=0;i<gp->len;i++){
181                                 switch(type){
182                                 case FT_UINT8:
183                                 case FT_UINT16:
184                                 case FT_UINT24:
185                                 case FT_UINT32:
186                                         val=fvalue_get_integer(&((field_info *)gp->pdata[i])->value);
187                                         if((it->frames==1)&&(i==0)){
188                                                 it->counter=val;
189                                         } else if(val>it->counter){
190                                                 it->counter=val;
191                                         }                               
192                                         break;
193                                 case FT_INT8:
194                                 case FT_INT16:
195                                 case FT_INT24:
196                                 case FT_INT32:
197                                         val=fvalue_get_integer(&((field_info *)gp->pdata[i])->value);
198                                         if((it->frames==1)&&(i==0)){
199                                                 it->counter=val;
200                                         } else if((gint32)val>(gint32)(it->counter)){
201                                                 it->counter=val;
202                                         }                               
203                                         break;
204                                 case FT_RELATIVE_TIME:
205                                         new_time=fvalue_get(&((field_info *)gp->pdata[i])->value);
206                                         val=new_time->secs*1000+new_time->nsecs/1000000;
207                                         if((it->frames==1)&&(i==0)){
208                                                 it->counter=val;
209                                         } else if(val>it->counter){
210                                                 it->counter=val;
211                                         }                               
212                                         break;
213                                 }
214                         }
215                 }
216                 break;
217         case CALC_TYPE_AVG:
218                 gp=proto_get_finfo_ptr_array(edt->tree, it->hf_index);
219                 if(gp){
220                         int type;
221                         guint32 val;
222                         nstime_t *new_time;
223
224                         type=proto_registrar_get_ftype(it->hf_index);
225                         for(i=0;i<gp->len;i++){
226                                 it->num++;
227                                 switch(type){
228                                 case FT_UINT8:
229                                 case FT_UINT16:
230                                 case FT_UINT24:
231                                 case FT_UINT32:
232                                 case FT_INT8:
233                                 case FT_INT16:
234                                 case FT_INT24:
235                                 case FT_INT32:
236                                         val=fvalue_get_integer(&((field_info *)gp->pdata[i])->value);
237                                         it->counter+=val;
238                                         break;
239                                 case FT_RELATIVE_TIME:
240                                         new_time=fvalue_get(&((field_info *)gp->pdata[i])->value);
241                                         val=new_time->secs*1000+new_time->nsecs/1000000;
242                                         it->counter+=val;
243                                         break;
244                                 }
245                         }
246                 }
247                 break;
248         }
249
250         return TRUE;
251 }
252
253 static void
254 iostat_draw(void *arg)
255 {
256         io_stat_item_t *mit = arg;
257         io_stat_t *iot;
258         io_stat_item_t **items;
259         guint32 *frames;
260         guint32 *counters;
261         guint32 *num;
262         guint32 i,more_items;
263         gint t;
264
265         iot=mit->parent;
266
267         printf("\n");
268         printf("===================================================================\n");
269         printf("IO Statistics\n");
270         printf("Interval: %d.%03d secs\n", iot->interval/1000, iot->interval%1000);
271         for(i=0;i<iot->num_items;i++){
272                 printf("Column #%d: %s\n",i,iot->filters[i]?iot->filters[i]:"");
273         }
274         printf("                ");
275         for(i=0;i<iot->num_items;i++){
276                 printf("|   Column #%-2d   ",i);
277         }
278         printf("\n");
279         printf("Time            ");
280         for(i=0;i<iot->num_items;i++){
281                 switch(iot->items[i].calc_type){
282                 case CALC_TYPE_BYTES:
283                         printf("|frames|  bytes  ");
284                         break;
285                 case CALC_TYPE_COUNT:
286                         printf("|          COUNT ");
287                         break;
288                 case CALC_TYPE_SUM:
289                         printf("|            SUM ");
290                         break;
291                 case CALC_TYPE_MIN:
292                         printf("|            MIN ");
293                         break;
294                 case CALC_TYPE_MAX:
295                         printf("|            MAX ");
296                         break;
297                 case CALC_TYPE_AVG:
298                         printf("|            AVG ");
299                         break;
300                 }
301         }
302         printf("\n");
303
304         items=g_malloc(sizeof(io_stat_item_t *)*iot->num_items);
305         frames=g_malloc(sizeof(guint32)*iot->num_items);
306         counters=g_malloc(sizeof(guint32)*iot->num_items);
307         num=g_malloc(sizeof(guint32)*iot->num_items);
308         /* preset all items at the first interval */
309         for(i=0;i<iot->num_items;i++){
310                 items[i]=&iot->items[i];
311         }
312
313         /* loop the items until we run out of them all */
314         t=0;
315         do {
316                 more_items=0;
317                 for(i=0;i<iot->num_items;i++){
318                         frames[i]=0;
319                         counters[i]=0;
320                         num[i]=0;
321                 }
322                 for(i=0;i<iot->num_items;i++){
323                         if(items[i] && (t>=(items[i]->time+iot->interval))){
324                                 items[i]=items[i]->next;
325                         }
326
327                         if(items[i] && (t<(items[i]->time+iot->interval)) && (t>=items[i]->time) ){
328                                 frames[i]=items[i]->frames;
329                                 counters[i]=items[i]->counter;
330                                 num[i]=items[i]->num;
331                         }
332
333                         if(items[i]){
334                                 more_items=1;
335                         }
336                 }
337
338                 if(more_items){
339                         printf("%03d.%03d-%03d.%03d  ",
340                                 t/1000,t%1000,
341                                 (t+iot->interval)/1000,(t+iot->interval)%1000);
342                         for(i=0;i<iot->num_items;i++){
343                                 switch(iot->items[i].calc_type){
344                                 case CALC_TYPE_BYTES:
345                                         printf("%6d %9d ",frames[i],counters[i]);
346                                         break;
347                                 case CALC_TYPE_COUNT:
348                                         printf("        %8d ", counters[i]);
349                                         break;
350                                 case CALC_TYPE_SUM:
351                                         printf("        %8d ", counters[i]);
352                                         break;
353                                 case CALC_TYPE_MIN:
354                                         switch(proto_registrar_get_ftype(iot->items[i].hf_index)){
355                                         case FT_UINT8:
356                                         case FT_UINT16:
357                                         case FT_UINT24:
358                                         case FT_UINT32:
359                                                 printf("        %8u ", counters[i]);
360                                                 break;
361                                         case FT_INT8:
362                                         case FT_INT16:
363                                         case FT_INT24:
364                                         case FT_INT32:
365                                                 printf("        %8d ", counters[i]);
366                                                 break;
367                                         case FT_RELATIVE_TIME:
368                                                 printf("      %6d.%03d ", counters[i]/1000, counters[i]%1000);
369                                                 break;
370                                         }
371                                         break;
372                                 case CALC_TYPE_MAX:
373                                         switch(proto_registrar_get_ftype(iot->items[i].hf_index)){
374                                         case FT_UINT8:
375                                         case FT_UINT16:
376                                         case FT_UINT24:
377                                         case FT_UINT32:
378                                                 printf("        %8u ", counters[i]);
379                                                 break;
380                                         case FT_INT8:
381                                         case FT_INT16:
382                                         case FT_INT24:
383                                         case FT_INT32:
384                                                 printf("        %8d ", counters[i]);
385                                                 break;
386                                         case FT_RELATIVE_TIME:
387                                                 printf("      %6d.%03d ", counters[i]/1000, counters[i]%1000);
388                                                 break;
389                                         }
390                                         break;
391                                 case CALC_TYPE_AVG:
392                                         if(num[i]==0){
393                                                 num[i]=1;
394                                         }
395                                         switch(proto_registrar_get_ftype(iot->items[i].hf_index)){
396                                         case FT_UINT8:
397                                         case FT_UINT16:
398                                         case FT_UINT24:
399                                         case FT_UINT32:
400                                                 printf("        %8u ", counters[i]/num[i]);
401                                                 break;
402                                         case FT_INT8:
403                                         case FT_INT16:
404                                         case FT_INT24:
405                                         case FT_INT32:
406                                                 printf("        %8d ", counters[i]/num[i]);
407                                                 break;
408                                         case FT_RELATIVE_TIME:
409                                                 counters[i]/=num[i];
410                                                 printf("      %6d.%03d ", counters[i]/1000, counters[i]%1000);
411                                                 break;
412                                         }
413                                         break;
414
415                                 }
416                         }
417                         printf("\n");
418                 }
419
420                 t+=iot->interval;
421         } while(more_items);
422
423         printf("===================================================================\n");
424
425         g_free(items);
426         g_free(frames);
427         g_free(counters);
428         g_free(num);
429 }
430
431
432 static int
433 get_calc_field(const char *filter, const char **flt)
434 {
435         char field[256];
436         int i;
437         header_field_info *hfi;
438
439         *flt="";
440         for(i=0;filter[i];i++){
441                 if(i>=255){
442                         fprintf(stderr,"get_calc_field(): Too long field name: %s\n", filter);
443                         exit(10);
444                 }
445                 if(filter[i]==')'){
446                         break;
447                 }
448                 field[i]=filter[i];
449                 field[i+1]=0;
450         }
451         if(filter[i]==')'){
452                 *flt=&filter[i+1];
453         }
454
455         hfi=proto_registrar_get_byname(field);
456         if(!hfi){
457                 fprintf(stderr, "get_calc_field(): No such field %s\n", field);
458                 exit(10);
459         }
460         
461         return hfi->id;
462 }
463
464 static void
465 register_io_tap(io_stat_t *io, int i, const char *filter)
466 {
467         GString *error_string;
468         const char *flt;
469
470         io->items[i].prev=&io->items[i];
471         io->items[i].next=NULL;
472         io->items[i].parent=io;
473         io->items[i].time=0;
474         io->items[i].calc_type=CALC_TYPE_BYTES;
475         io->items[i].frames=0;
476         io->items[i].counter=0;
477         io->items[i].num=0;
478         io->filters[i]=filter;
479         flt=filter;
480
481         if(!filter){
482                 filter="";
483         }
484         if(!strncmp("COUNT(", filter, 6)){
485                 io->items[i].calc_type=CALC_TYPE_COUNT;
486                 io->items[i].hf_index=get_calc_field(filter+6, &flt);
487         } else if (!strncmp("SUM(", filter, 4)){
488                 io->items[i].calc_type=CALC_TYPE_SUM;
489                 io->items[i].hf_index=get_calc_field(filter+4, &flt);
490                 switch(proto_registrar_get_nth(io->items[i].hf_index)->type){
491                 case FT_UINT8:
492                 case FT_UINT16:
493                 case FT_UINT24:
494                 case FT_UINT32:
495                 case FT_INT8:
496                 case FT_INT16:
497                 case FT_INT24:
498                 case FT_INT32:
499                         break;
500                 default:
501                         fprintf(stderr, "register_io_tap(): Invalid field type. SUM(x) only supports 8,16,24 and 32 byte integer fields\n");
502                         exit(10);
503                 }
504         } else if (!strncmp("MIN(", filter, 4)){
505                 io->items[i].calc_type=CALC_TYPE_MIN;
506                 io->items[i].hf_index=get_calc_field(filter+4, &flt);
507                 switch(proto_registrar_get_nth(io->items[i].hf_index)->type){
508                 case FT_UINT8:
509                 case FT_UINT16:
510                 case FT_UINT24:
511                 case FT_UINT32:
512                 case FT_INT8:
513                 case FT_INT16:
514                 case FT_INT24:
515                 case FT_INT32:
516                 case FT_RELATIVE_TIME:
517                         break;
518                 default:
519                         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");
520                         exit(10);
521                 }
522         } else if (!strncmp("MAX(", filter, 4)){
523                 io->items[i].calc_type=CALC_TYPE_MAX;
524                 io->items[i].hf_index=get_calc_field(filter+4, &flt);
525                 switch(proto_registrar_get_nth(io->items[i].hf_index)->type){
526                 case FT_UINT8:
527                 case FT_UINT16:
528                 case FT_UINT24:
529                 case FT_UINT32:
530                 case FT_INT8:
531                 case FT_INT16:
532                 case FT_INT24:
533                 case FT_INT32:
534                 case FT_RELATIVE_TIME:
535                         break;
536                 default:
537                         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");
538                         exit(10);
539                 }
540         } else if (!strncmp("AVG(", filter, 4)){
541                 io->items[i].calc_type=CALC_TYPE_AVG;
542                 io->items[i].hf_index=get_calc_field(filter+4, &flt);
543                 switch(proto_registrar_get_nth(io->items[i].hf_index)->type){
544                 case FT_UINT8:
545                 case FT_UINT16:
546                 case FT_UINT24:
547                 case FT_UINT32:
548                 case FT_INT8:
549                 case FT_INT16:
550                 case FT_INT24:
551                 case FT_INT32:
552                 case FT_RELATIVE_TIME:
553                         break;
554                 default:
555                         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");
556                         exit(10);
557                 }
558         }
559
560 /*
561 CALC_TYPE_SUM   2
562 CALC_TYPE_MIN   3
563 CALC_TYPE_MAX   4
564 CALC_TYPE_AVG   5
565 */
566
567         error_string=register_tap_listener("frame", &io->items[i], flt, NULL, iostat_packet, i?NULL:iostat_draw);
568         if(error_string){
569                 g_free(io->items);
570                 g_free(io);
571                 fprintf(stderr, "tethereal: Couldn't register io,stat tap: %s\n",
572                     error_string->str);
573                 g_string_free(error_string, TRUE);
574                 exit(1);
575         }
576 }
577
578 void
579 iostat_init(const char *optarg)
580 {
581         float interval_float;
582         gint32 interval; 
583         int pos=0;
584         io_stat_t *io;
585         const char *filter=NULL;
586
587         if(sscanf(optarg,"io,stat,%f,%n",&interval_float,&pos)==1){
588                 if(pos){
589                         filter=optarg+pos;
590                 } else {
591                         filter=NULL;
592                 }
593         } else {
594                 fprintf(stderr, "tethereal: invalid \"-z io,stat,<interval>[,<filter>]\" argument\n");
595                 exit(1);
596         }
597
598
599         /* make interval be number of ms */
600         interval=(gint32)(interval_float*1000.0+0.9);   
601         if(interval<1){
602                 fprintf(stderr, "tethereal:iostat_init()  interval must be >=0.001 seconds\n");
603                 exit(10);
604         }
605         
606         io=g_malloc(sizeof(io_stat_t));
607         io->interval=interval;
608         if((!filter)||(filter[0]==0)){
609                 io->num_items=1;
610                 io->items=g_malloc(sizeof(io_stat_item_t)*io->num_items);
611                 io->filters=g_malloc(sizeof(char *)*io->num_items);
612
613                 register_io_tap(io, 0, NULL);
614         } else {
615                 const char *str,*pos;
616                 char *tmp;
617                 int i;
618                 /* find how many ',' separated filters we have */
619                 str=filter;
620                 io->num_items=1;
621                 while((str=strchr(str,','))){
622                         io->num_items++;
623                         str++;
624                 }
625
626                 io->items=g_malloc(sizeof(io_stat_item_t)*io->num_items);
627                 io->filters=g_malloc(sizeof(char *)*io->num_items);
628
629                 /* for each filter, register a tap listener */          
630                 i=0;
631                 str=filter;
632                 do{
633                         pos=strchr(str,',');
634                         if(pos==str){
635                                 register_io_tap(io, i, NULL);
636                         } else if(pos==NULL) {
637                                 tmp=g_malloc(strlen(str)+1);
638                                 strcpy(tmp,str);
639                                 register_io_tap(io, i, tmp);
640                         } else {
641                                 tmp=g_malloc((pos-str)+1);
642                                 strncpy(tmp,str,(pos-str));
643                                 tmp[pos-str]=0;
644                                 register_io_tap(io, i, tmp);
645                         }
646                         str=pos+1;
647                         i++;                    
648                 } while(pos);
649         }                       
650 }
651
652 void
653 register_tap_listener_iostat(void)
654 {
655         register_stat_cmd_arg("io,stat,", iostat_init);
656 }