Move the stats.[ch] stuff into epan, so plugins can use it.
[obnox/wireshark/wip.git] / tap-rpcprogs.c
1 /* tap-rpcprogs.c
2  * rpcstat   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 /* This module provides rpc call/reply SRT statistics to tethereal.
26  * It is only used by tethereal and not ethereal
27  */
28
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <stdio.h>
34
35 #ifdef HAVE_SYS_TYPES_H
36 # include <sys/types.h>
37 #endif
38
39 #include <string.h>
40 #include "epan/packet_info.h"
41 #include <epan/tap.h>
42 #include <epan/stat.h>
43 #include <epan/dissectors/packet-rpc.h>
44 #include "register.h"
45
46 /* used to keep track of statistics for a specific program/version */
47 typedef struct _rpc_program_t {
48         struct _rpc_program_t *next;
49         guint32 program;
50         guint32 version;
51         int num;
52         nstime_t min;
53         nstime_t max;
54         nstime_t tot;
55 } rpc_program_t;
56
57 static rpc_program_t *prog_list=NULL;
58 static int already_enabled=0;
59
60 static int
61 rpcprogs_packet(void *dummy1 _U_, packet_info *pinfo, epan_dissect_t *edt _U_, const void *pri)
62 {
63         const rpc_call_info_value *ri=pri;
64         nstime_t delta;
65         rpc_program_t *rp=NULL;
66
67         if(!prog_list){
68                 /* the list was empty */
69                 rp=g_malloc(sizeof(rpc_program_t));
70                 rp->next=NULL;
71                 rp->program=ri->prog;
72                 rp->version=ri->vers;
73                 rp->num=0;
74                 rp->min.secs=0;
75                 rp->min.nsecs=0;
76                 rp->max.secs=0;
77                 rp->max.nsecs=0;
78                 rp->tot.secs=0;
79                 rp->tot.nsecs=0;
80                 prog_list=rp;
81         } else if((ri->prog==prog_list->program)
82                 &&(ri->vers==prog_list->version)){
83                 rp=prog_list;
84         } else if( (ri->prog<prog_list->program)
85                 ||((ri->prog==prog_list->program)&&(ri->vers<prog_list->version))){
86                 /* we should be first entry in list */
87                 rp=g_malloc(sizeof(rpc_program_t));
88                 rp->next=prog_list;
89                 rp->program=ri->prog;
90                 rp->version=ri->vers;
91                 rp->num=0;
92                 rp->min.secs=0;
93                 rp->min.nsecs=0;
94                 rp->max.secs=0;
95                 rp->max.nsecs=0;
96                 rp->tot.secs=0;
97                 rp->tot.nsecs=0;
98                 prog_list=rp;
99         } else {
100                 /* we go somewhere else in the list */
101                 for(rp=prog_list;rp;rp=rp->next){
102                         if((rp->next)
103                         && (rp->next->program==ri->prog)
104                         && (rp->next->version==ri->vers)){
105                                 rp=rp->next;
106                                 break;
107                         }
108                         if((!rp->next)
109                         || (rp->next->program>ri->prog)
110                         || (  (rp->next->program==ri->prog)
111                             &&(rp->next->version>ri->vers))){
112                                 rpc_program_t *trp;
113                                 trp=g_malloc(sizeof(rpc_program_t));
114                                 trp->next=rp->next;
115                                 trp->program=ri->prog;
116                                 trp->version=ri->vers;
117                                 trp->num=0;
118                                 trp->min.secs=0;
119                                 trp->min.nsecs=0;
120                                 trp->max.secs=0;
121                                 trp->max.nsecs=0;
122                                 trp->tot.secs=0;
123                                 trp->tot.nsecs=0;
124                                 rp->next=trp;
125                                 rp=trp;
126                                 break;
127                         }
128                 }
129         }
130
131         
132         /* we are only interested in reply packets */
133         if(ri->request){
134                 return 0;
135         }
136
137         /* calculate time delta between request and reply */
138         delta.secs=pinfo->fd->abs_secs-ri->req_time.secs;
139         delta.nsecs=pinfo->fd->abs_usecs*1000-ri->req_time.nsecs;
140         if(delta.nsecs<0){
141                 delta.nsecs+=1000000000;
142                 delta.secs--;
143         }
144
145         if((rp->max.secs==0)
146         && (rp->max.nsecs==0) ){
147                 rp->max.secs=delta.secs;
148                 rp->max.nsecs=delta.nsecs;
149         }
150
151         if((rp->min.secs==0)
152         && (rp->min.nsecs==0) ){
153                 rp->min.secs=delta.secs;
154                 rp->min.nsecs=delta.nsecs;
155         }
156
157         if( (delta.secs<rp->min.secs)
158         ||( (delta.secs==rp->min.secs)
159           &&(delta.nsecs<rp->min.nsecs) ) ){
160                 rp->min.secs=delta.secs;
161                 rp->min.nsecs=delta.nsecs;
162         }
163
164         if( (delta.secs>rp->max.secs)
165         ||( (delta.secs==rp->max.secs)
166           &&(delta.nsecs>rp->max.nsecs) ) ){
167                 rp->max.secs=delta.secs;
168                 rp->max.nsecs=delta.nsecs;
169         }
170         
171         rp->tot.secs += delta.secs;
172         rp->tot.nsecs += delta.nsecs;
173         if(rp->tot.nsecs>1000000000){
174                 rp->tot.nsecs-=1000000000;
175                 rp->tot.secs++;
176         }
177         rp->num++;
178
179         return 1;
180 }
181
182
183 static void
184 rpcprogs_draw(void *dummy _U_)
185 {
186 #ifdef G_HAVE_UINT64
187         guint64 td;
188 #else
189         guint32 td;
190 #endif
191         rpc_program_t *rp;
192         char str[64];
193
194         printf("\n");
195         printf("===================================================================\n");
196         printf("ONC-RPC Program Statistics:\n");
197         printf("Program    Version  Calls   Min SRT   Max SRT   Avg SRT\n");
198         for(rp=prog_list;rp;rp=rp->next){
199                 /* scale it to units of 10us.*/
200                 /* for long captures with a large tot time, this can overflow on 32bit */
201                 td=(int)rp->tot.secs;
202                 td=td*100000+(int)rp->tot.nsecs/10000;
203                 if(rp->num){
204                         td/=rp->num;
205                 } else {
206                         td=0;
207                 }
208
209                 g_snprintf(str, sizeof(str), "%s(%d)",rpc_prog_name(rp->program),rp->program);
210                 printf("%-15s %2d %6d %3d.%05d %3d.%05d %3d.%05d\n",
211                         str,
212                         rp->version,
213                         rp->num,
214                         (int)rp->min.secs,rp->min.nsecs/10000,
215                         (int)rp->max.secs,rp->max.nsecs/10000,
216                         td/100000, td%100000
217                 );
218         }
219         printf("===================================================================\n");
220 }
221
222
223 static void
224 rpcprogs_init(const char *optarg _U_)
225 {
226         GString *error_string;
227
228         if(already_enabled){
229                 return;
230         }
231         already_enabled=1;
232
233         error_string=register_tap_listener("rpc", NULL, NULL, NULL, rpcprogs_packet, rpcprogs_draw);
234         if(error_string){
235                 fprintf(stderr,"tethereal: Couldn't register rpc,programs tap: %s\n",
236                     error_string->str);
237                 g_string_free(error_string, TRUE);
238                 exit(1);
239         }
240 }
241
242
243 void
244 register_tap_listener_rpcprogs(void)
245 {
246         register_stat_cmd_arg("rpc,programs", rpcprogs_init);
247 }
248
249