Change "DCE RPC" to "Distributed Computing Environment / Remote Procedure
[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 "epan/asn1.h"
42 #include "epan/dissectors/packet-camel.h"
43 #include "epan/camel-persistentdata.h"
44 #include "timestats.h"
45 #include "epan/stat_cmd_args.h"
46
47
48 void register_tap_listener_camelsrt(void);
49
50 /* Save the the first NUM_RAS_STATS stats in the array to calculate percentile */
51 #define NUM_RAS_STATS 500000
52
53 /* Number of couple message Request/Response to analyze*/
54 #define NB_CRITERIA 7
55
56 /* used to keep track of the statistics for an entire program interface */
57 struct camelsrt_t {
58   char *filter;
59   guint32 count[NB_CAMELSRT_CATEGORY];
60   timestat_t stats[NB_CAMELSRT_CATEGORY];
61   nstime_t delta_time[NB_CAMELSRT_CATEGORY][NUM_RAS_STATS];
62 };
63
64 /* Reset the counter */
65 static void camelsrt_reset(void *phs)
66 {
67   struct camelsrt_t *hs=(struct camelsrt_t *)phs;
68   memset(hs,0,sizeof(struct camelsrt_t));
69 }
70
71
72 static int camelsrt_packet(void *phs,
73                            packet_info *pinfo _U_,
74                            epan_dissect_t *edt _U_,
75                            const void *phi)
76 {
77   struct camelsrt_t *hs=(struct camelsrt_t *)phs;
78   const struct camelsrt_info_t * pi=phi;
79   int i;
80
81   for (i=0; i<NB_CAMELSRT_CATEGORY; i++) {
82     if (pi->bool_msginfo[i] &&
83         pi->msginfo[i].is_delta_time
84         && pi->msginfo[i].request_available
85         && !pi->msginfo[i].is_duplicate ) {
86
87       time_stat_update(&(hs->stats[i]),
88                        &(pi->msginfo[i].delta_time),
89                        pinfo);
90
91       if (hs->count[i] < NUM_RAS_STATS) {
92         hs->delta_time[i][hs->count[i]++]
93           = pi->msginfo[i].delta_time;
94       }
95     }
96   }
97   return 1;
98 }
99
100
101 static void camelsrt_draw(void *phs)
102 {
103   struct camelsrt_t *hs=(struct camelsrt_t *)phs;
104   guint j,z;
105   guint32 li;
106   int somme,iteration=0;
107   timestat_t *rtd_temp;
108   double x,delay,delay_max,delay_min,delta;
109   double criteria[NB_CRITERIA]={ 5.0, 10.0, 75.0, 90.0, 95.0,99.0,99.90 };
110   double delay_criteria[NB_CRITERIA];
111
112   printf("\n");
113   printf("Camel Service Response Time (SRT) Statistics:\n");
114   printf("=================================================================================================\n");
115   printf("|        Category         | Measure |  Min SRT  |  Max SRT  |  Avg SRT  | Min frame | Max frame |\n");
116   printf("|-------------------------|---------|-----------|-----------|-----------|-----------|-----------|\n");
117
118   j=1;
119   printf("|%24s |%8u |%8.2f s |%8.2f s |%8.2f s |%10u |%10u |\n",
120          val_to_str(j,camelSRTtype_naming,"Unknown Message 0x%02x"),
121          hs->stats[j].num,
122          nstime_to_sec(&(hs->stats[j].min)),
123          nstime_to_sec(&(hs->stats[j].max)),
124          get_average(&(hs->stats[j].tot),hs->stats[j].num)/1000.0,
125          hs->stats[j].min_num,
126          hs->stats[j].max_num
127          );
128   for(j=2; j<NB_CAMELSRT_CATEGORY; j++) {
129     if(hs->stats[j].num==0){
130       printf("|%24s |%8u |%8.2f ms|%8.2f ms|%8.2f ms|%10u |%10u |\n",
131              val_to_str(j,camelSRTtype_naming,"Unknown Message 0x%02x"),
132              0, 0.0, 0.0, 0.0, 0, 0);
133       continue;
134     }
135
136     printf("|%24s |%8u |%8.2f ms|%8.2f ms|%8.2f ms|%10u |%10u |\n",
137            val_to_str(j,camelSRTtype_naming,"Unknown Message 0x%02x"),
138            hs->stats[j].num,
139            MIN(9999,nstime_to_msec(&(hs->stats[j].min))),
140            MIN(9999,nstime_to_msec(&(hs->stats[j].max))),
141            MIN(9999,get_average(&(hs->stats[j].tot),hs->stats[j].num)),
142            hs->stats[j].min_num,
143            hs->stats[j].max_num
144            );
145   } /* j category */
146
147   printf("=================================================================================================\n");
148   /*
149    * Display 95%
150    */
151
152   printf("|   Category/Criteria     |");
153   for(z=0; z<NB_CRITERIA; z++) printf("%7.2f%% |", criteria[z]);
154   printf("\n");
155
156   printf("|-------------------------|");
157   for(z=0; z<NB_CRITERIA; z++) printf("---------|");
158   printf("\n");
159   /* calculate the delay max to have a given number of messages (in percentage) */
160   for(j=2;j<NB_CAMELSRT_CATEGORY;j++) {
161
162     rtd_temp = &(hs->stats[j]);
163
164     if (hs->count[j]>0) {
165       /* Calculate the delay to answer to p% of the MS */
166       for(z=0; z<NB_CRITERIA; z++) {
167         iteration=0;
168         delay_max=(double)rtd_temp->max.secs*1000 +(double)rtd_temp->max.nsecs/1000000;
169         delay_min=(double)rtd_temp->min.secs*1000 +(double)rtd_temp->min.nsecs/1000000;
170         delay=delay_min;
171         delta=delay_max-delay_min;
172         while( (delta > 0.001) && (iteration < 10000) ) {
173           somme=0;
174           iteration++;
175
176           for(li=0;li<hs->count[j];li++) {
177             x=hs->delta_time[j][li].secs*1000
178               + (double)hs->delta_time[j][li].nsecs/1000000;
179             if (x <= delay) somme++;
180           }
181           if ( somme*100 > hs->count[j]*criteria[z] ) { /* trop grand */
182             delay_max=delay;
183             delay=(delay_max+delay_min)/2;
184             delta=delay_max-delay_min;
185           } else { /* trop petit */
186             delay_min=delay;
187             delay=(delay_max+delay_min)/2;
188             delta=delay_max-delay_min;
189           }
190         } /* while */
191         delay_criteria[z]=delay;
192       } /* z criteria */
193       /* Append the result to the table */
194       printf("X%24s |", val_to_str(j, camelSRTtype_naming, "Unknown") );
195       for(z=0; z<NB_CRITERIA; z++) printf("%8.2f |", MIN(9999,delay_criteria[z]));
196       printf("\n");
197     } else { /* count */
198       printf("X%24s |", val_to_str(j, camelSRTtype_naming, "Unknown") );
199       for(z=0; z<NB_CRITERIA; z++) printf("%8.2f |", 0.0);
200       printf("\n");
201     } /* count */
202   }/* j category */
203   printf("===========================");
204   for(z=0; z<NB_CRITERIA; z++) printf("==========");
205   printf("\n");
206 }
207
208 static void camelsrt_init(const char *optarg, void* userdata _U_)
209 {
210   struct camelsrt_t *p_camelsrt;
211   GString *error_string;
212
213   p_camelsrt = g_malloc(sizeof(struct camelsrt_t));
214   if(!strncmp(optarg,"camel,srt,",9)){
215     p_camelsrt->filter=g_strdup(optarg+9);
216   } else {
217     p_camelsrt->filter=NULL;
218   }
219   camelsrt_reset(p_camelsrt);
220
221   error_string=register_tap_listener("CAMEL",
222                                      p_camelsrt,
223                                      p_camelsrt->filter,
224                                      0,
225                                      NULL,
226                                      camelsrt_packet,
227                                      camelsrt_draw);
228
229   if(error_string){
230     /* error, we failed to attach to the tap. clean up */
231     g_free(p_camelsrt->filter);
232     g_free(p_camelsrt);
233
234     fprintf(stderr, "tshark: Couldn't register camel,srt tap: %s\n",
235             error_string->str);
236     g_string_free(error_string, TRUE);
237     exit(1);
238   }
239
240   /*
241    * If we are using tshark, we have to display the stats, even if the stats are not persistent
242    * As the frame are proceeded in the chronological order, we do not need persistent stats
243    * Whereas, with wireshark, it is not possible to have the correct display, if the stats are
244    * not saved along the analyze
245    */
246   gtcap_StatSRT=TRUE;
247   gcamel_StatSRT=TRUE;
248 }
249
250
251 void /* Next line mandatory */
252 register_tap_listener_camelsrt(void)
253 {
254   register_stat_cmd_arg("camel,srt", camelsrt_init, NULL);
255 }