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