This is to test the waters.
[obnox/wireshark/wip.git] / tap-camelsrt.c
1 /* tap_camelsrt.c
2  * CAMEL Service Response Time statistics for tshark
3  * Copyright 2006 Florent Drouin (based on tap_h225rassrt.c from Lars Roland)
4  *
5  * Wireshark - Network traffic analyzer
6  * By Gerald Combs <gerald@wireshark.org>
7  * Copyright 1998 Gerald Combs
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28 #include <stdio.h>
29
30 #ifdef HAVE_SYS_TYPES_H
31 # include <sys/types.h>
32 #endif
33
34 #include <string.h>
35 #include "epan/packet_info.h"
36 #include <epan/tap.h>
37 #include "epan/value_string.h"
38 #include "register.h"
39 #include "epan/dissectors/packet-camel.h"
40 #include "epan/camel-persistentdata.h"
41 #include "timestats.h"
42 #include "epan/stat_cmd_args.h"
43
44 #undef  MIN
45 #define MIN(x,y) ((x) < (y) ? (x) : (y))
46
47 void register_tap_listener_camelsrt(void);
48
49 /* Save the the first NUM_RAS_STATS stats in the array to calculate percentile */
50 #define NUM_RAS_STATS 500000
51
52 /* Number of couple message Request/Response to analyze*/
53 #define NB_CRITERIA 7
54
55 /* used to keep track of the statistics for an entire program interface */
56 struct camelsrt_t {
57   char *filter;
58   guint32 count[NB_CAMELSRT_CATEGORY];
59   timestat_t stats[NB_CAMELSRT_CATEGORY];
60   nstime_t delta_time[NB_CAMELSRT_CATEGORY][NUM_RAS_STATS];
61 };
62
63 /* Check if we have to inhibit the display or not 
64  * cannot be static because it's exported in libwireshark.def
65  */
66 gboolean gcamel_StatSRT;
67 gboolean gtcap_StatSRT;
68
69 /* Reset the counter */
70 static void camelsrt_reset(void *phs)
71 {
72   struct camelsrt_t *hs=(struct camelsrt_t *)phs;
73   memset(hs,0,sizeof(struct camelsrt_t));
74 }
75
76
77 static int camelsrt_packet(void *phs, 
78                            packet_info *pinfo _U_, 
79                            epan_dissect_t *edt _U_,
80                            const void *phi)
81 {
82   struct camelsrt_t *hs=(struct camelsrt_t *)phs;
83   const struct camelsrt_info_t * pi=phi;
84   int i;
85
86   for (i=0; i<NB_CAMELSRT_CATEGORY; i++) {
87     if (pi->bool_msginfo[i] &&
88         pi->msginfo[i].is_delta_time 
89         && pi->msginfo[i].request_available
90         && !pi->msginfo[i].is_duplicate ) {
91       
92       time_stat_update(&(hs->stats[i]),
93                        &(pi->msginfo[i].delta_time),
94                        pinfo);
95         
96       if (hs->count[i] < NUM_RAS_STATS) {
97         hs->delta_time[i][hs->count[i]++] 
98           = pi->msginfo[i].delta_time;
99       }
100     }
101   }
102   return 1;
103 }
104
105
106 static void camelsrt_draw(void *phs)
107 {
108   struct camelsrt_t *hs=(struct camelsrt_t *)phs;
109   guint j,z;
110   guint32 li;
111   int somme,iteration=0;
112   timestat_t *rtd_temp;
113   double x,delay,delay_max,delay_min,delta;
114   double criteria[NB_CRITERIA]={ 5.0, 10.0, 75.0, 90.0, 95.0,99.0,99.90 };
115   double delay_criteria[NB_CRITERIA];
116
117   printf("\n");
118   printf("Camel Service Response Time (SRT) Statistics:\n");
119   printf("=================================================================================================\n");
120   printf("|        Category         | Measure |  Min SRT  |  Max SRT  |  Avg SRT  | Min frame | Max frame |\n");
121   printf("|-------------------------|---------|-----------|-----------|-----------|-----------|-----------|\n");
122  
123   j=1;
124   printf("|%24s |%8u |%8.2f s |%8.2f s |%8.2f s |%10u |%10u |\n",
125          val_to_str(j,camelSRTtype_naming,"Unknown Message 0x%02x"),
126          hs->stats[j].num,
127          nstime_to_sec(&(hs->stats[j].min)),
128          nstime_to_sec(&(hs->stats[j].max)),
129          get_average(&(hs->stats[j].tot),hs->stats[j].num)/1000.0,
130          hs->stats[j].min_num, 
131          hs->stats[j].max_num
132          );
133   for(j=2; j<NB_CAMELSRT_CATEGORY; j++) { 
134     if(hs->stats[j].num==0){ 
135       printf("|%24s |%8u |%8.2f ms|%8.2f ms|%8.2f ms|%10u |%10u |\n",
136              val_to_str(j,camelSRTtype_naming,"Unknown Message 0x%02x"),
137              0, 0.0, 0.0, 0.0, 0, 0);
138       continue;
139     }
140     
141     printf("|%24s |%8u |%8.2f ms|%8.2f ms|%8.2f ms|%10u |%10u |\n",
142            val_to_str(j,camelSRTtype_naming,"Unknown Message 0x%02x"),
143            hs->stats[j].num,
144            MIN(9999,nstime_to_msec(&(hs->stats[j].min))),
145            MIN(9999,nstime_to_msec(&(hs->stats[j].max))),
146            MIN(9999,get_average(&(hs->stats[j].tot),hs->stats[j].num)),
147            hs->stats[j].min_num, 
148            hs->stats[j].max_num
149            );
150   } /* j category */ 
151
152   printf("=================================================================================================\n");
153   /*
154    * Display 95%
155    */
156
157   printf("|   Category/Criteria     |");
158   for(z=0; z<NB_CRITERIA; z++) printf("%7.2f%% |", criteria[z]);
159   printf("\n");
160
161   printf("|-------------------------|");
162   for(z=0; z<NB_CRITERIA; z++) printf("---------|");
163   printf("\n");
164   /* calculate the delay max to have a given number of messages (in percentage) */
165   for(j=2;j<NB_CAMELSRT_CATEGORY;j++) {
166     
167     rtd_temp = &(hs->stats[j]);
168     
169     if (hs->count[j]>0) { 
170       /* Calculate the delay to answer to p% of the MS */
171       for(z=0; z<NB_CRITERIA; z++) {
172         iteration=0;
173         delay_max=(double)rtd_temp->max.secs*1000 +(double)rtd_temp->max.nsecs/1000000;
174         delay_min=(double)rtd_temp->min.secs*1000 +(double)rtd_temp->min.nsecs/1000000;
175         delay=delay_min;
176         delta=delay_max-delay_min;
177         while( (delta > 0.001) && (iteration < 10000) ) {
178           somme=0;
179           iteration++;
180           
181           for(li=0;li<hs->count[j];li++) {
182             x=hs->delta_time[j][li].secs*1000 
183               + (double)hs->delta_time[j][li].nsecs/1000000;
184             if (x <= delay) somme++;
185           }
186           if ( somme*100 > hs->count[j]*criteria[z] ) { /* trop grand */
187             delay_max=delay;
188             delay=(delay_max+delay_min)/2;
189             delta=delay_max-delay_min;
190           } else { /* trop petit */
191             delay_min=delay;
192             delay=(delay_max+delay_min)/2;
193             delta=delay_max-delay_min;
194           }
195         } /* while */
196         delay_criteria[z]=delay;
197       } /* z criteria */
198       /* Append the result to the table */
199       printf("X%24s |", val_to_str(j, camelSRTtype_naming, "Unknown") );
200       for(z=0; z<NB_CRITERIA; z++) printf("%8.2f |", MIN(9999,delay_criteria[z]));
201       printf("\n");
202     } else { /* count */ 
203       printf("X%24s |", val_to_str(j, camelSRTtype_naming, "Unknown") );
204       for(z=0; z<NB_CRITERIA; z++) printf("%8.2f |", 0.0);
205       printf("\n");
206     } /* count */ 
207   }/* j category */ 
208   printf("===========================");
209   for(z=0; z<NB_CRITERIA; z++) printf("==========");
210   printf("\n");
211 }
212
213 static void camelsrt_init(const char *optarg, void* userdata _U_)
214 {
215   struct camelsrt_t *p_camelsrt;
216   const char *filter=NULL;
217   const char *emptyfilter=""; 
218
219   GString *error_string;
220
221   if(!strncmp(optarg,"camel,srt,",9)){
222     filter=optarg+9;
223   } else {
224     filter=NULL;
225   }
226
227   p_camelsrt = g_malloc(sizeof(struct camelsrt_t));
228   if(filter){
229     p_camelsrt->filter=g_malloc(strlen(filter)+1);
230     strcpy(p_camelsrt->filter,filter);
231   } else {
232     p_camelsrt->filter=NULL;
233   }
234   camelsrt_reset(p_camelsrt);
235   
236   if (filter) {
237     error_string=register_tap_listener("CAMEL",
238                                        p_camelsrt,
239                                        filter,
240                                        NULL,
241                                        camelsrt_packet,
242                                        camelsrt_draw);
243   } else { 
244     error_string=register_tap_listener("CAMEL",
245                                        p_camelsrt,
246                                        emptyfilter,
247                                        NULL,
248                                        camelsrt_packet,
249                                        camelsrt_draw);
250   }
251   
252   if(error_string){
253     /* error, we failed to attach to the tap. clean up */
254     g_free(p_camelsrt->filter);
255     g_free(p_camelsrt);
256     
257     fprintf(stderr, "tshark: Couldn't register camel,srt tap: %s\n",
258             error_string->str);
259     g_string_free(error_string, TRUE);
260     exit(1);
261   }
262
263   /*
264    * If we are using tshark, we have to display the stats, even if the stats are not persistent
265    * As the frame are proceeded in the chronological order, we do not need persistent stats
266    * Whereas, with wireshark, it is not possible to have the correct display, if the stats are
267    * not saved along the analyze
268    */ 
269   gtcap_StatSRT=TRUE;
270   gcamel_StatSRT=TRUE;
271 }
272
273
274 void /* Next line mandatory */
275 register_tap_listener_camelsrt(void)
276 {
277   register_stat_cmd_arg("camel,srt", camelsrt_init, NULL);
278 }