From Thomas Anders:
[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_cmd_args.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         nstime_delta(&delta, &pinfo->fd->abs_ts, &ri->req_time);
139
140         if((rp->max.secs==0)
141         && (rp->max.nsecs==0) ){
142                 rp->max.secs=delta.secs;
143                 rp->max.nsecs=delta.nsecs;
144         }
145
146         if((rp->min.secs==0)
147         && (rp->min.nsecs==0) ){
148                 rp->min.secs=delta.secs;
149                 rp->min.nsecs=delta.nsecs;
150         }
151
152         if( (delta.secs<rp->min.secs)
153         ||( (delta.secs==rp->min.secs)
154           &&(delta.nsecs<rp->min.nsecs) ) ){
155                 rp->min.secs=delta.secs;
156                 rp->min.nsecs=delta.nsecs;
157         }
158
159         if( (delta.secs>rp->max.secs)
160         ||( (delta.secs==rp->max.secs)
161           &&(delta.nsecs>rp->max.nsecs) ) ){
162                 rp->max.secs=delta.secs;
163                 rp->max.nsecs=delta.nsecs;
164         }
165         
166         rp->tot.secs += delta.secs;
167         rp->tot.nsecs += delta.nsecs;
168         if(rp->tot.nsecs>1000000000){
169                 rp->tot.nsecs-=1000000000;
170                 rp->tot.secs++;
171         }
172         rp->num++;
173
174         return 1;
175 }
176
177
178 static void
179 rpcprogs_draw(void *dummy _U_)
180 {
181 #ifdef G_HAVE_UINT64
182         guint64 td;
183 #else
184         guint32 td;
185 #endif
186         rpc_program_t *rp;
187         char str[64];
188
189         printf("\n");
190         printf("===================================================================\n");
191         printf("ONC-RPC Program Statistics:\n");
192         printf("Program    Version  Calls   Min SRT   Max SRT   Avg SRT\n");
193         for(rp=prog_list;rp;rp=rp->next){
194                 /* scale it to units of 10us.*/
195                 /* for long captures with a large tot time, this can overflow on 32bit */
196                 td=(int)rp->tot.secs;
197                 td=td*100000+(int)rp->tot.nsecs/10000;
198                 if(rp->num){
199                         td/=rp->num;
200                 } else {
201                         td=0;
202                 }
203
204                 g_snprintf(str, sizeof(str), "%s(%d)",rpc_prog_name(rp->program),rp->program);
205                 printf("%-15s %2d %6d %3d.%05d %3d.%05d %3d.%05d\n",
206                         str,
207                         rp->version,
208                         rp->num,
209                         (int)rp->min.secs,rp->min.nsecs/10000,
210                         (int)rp->max.secs,rp->max.nsecs/10000,
211                         td/100000, td%100000
212                 );
213         }
214         printf("===================================================================\n");
215 }
216
217
218 static void
219 rpcprogs_init(const char *optarg _U_, void* userdata _U_)
220 {
221         GString *error_string;
222
223         if(already_enabled){
224                 return;
225         }
226         already_enabled=1;
227
228         error_string=register_tap_listener("rpc", NULL, NULL, NULL, rpcprogs_packet, rpcprogs_draw);
229         if(error_string){
230                 fprintf(stderr,"tethereal: Couldn't register rpc,programs tap: %s\n",
231                     error_string->str);
232                 g_string_free(error_string, TRUE);
233                 exit(1);
234         }
235 }
236
237
238 void
239 register_tap_listener_rpcprogs(void)
240 {
241         register_stat_cmd_arg("rpc,programs", rpcprogs_init, NULL);
242 }
243
244