From Xiao Xiangquan:
[obnox/wireshark/wip.git] / tap-rpcprogs.c
1 /* tap-rpcprogs.c
2  * rpcstat   2002 Ronnie Sahlberg
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
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 tshark.
26  * It is only used by tshark and not wireshark
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
45 #define MICROSECS_PER_SEC   1000000
46 #define NANOSECS_PER_SEC    1000000000
47
48 /* used to keep track of statistics for a specific program/version */
49 typedef struct _rpc_program_t {
50         struct _rpc_program_t *next;
51         guint32 program;
52         guint32 version;
53         int num;
54         nstime_t min;
55         nstime_t max;
56         nstime_t tot;
57 } rpc_program_t;
58
59 static rpc_program_t *prog_list=NULL;
60 static int already_enabled=0;
61
62 static int
63 rpcprogs_packet(void *dummy1 _U_, packet_info *pinfo, epan_dissect_t *edt _U_, const void *pri)
64 {
65         const rpc_call_info_value *ri=pri;
66         nstime_t delta;
67         rpc_program_t *rp=NULL;
68
69         if(!prog_list){
70                 /* the list was empty */
71                 rp=g_malloc(sizeof(rpc_program_t));
72                 rp->next=NULL;
73                 rp->program=ri->prog;
74                 rp->version=ri->vers;
75                 rp->num=0;
76                 rp->min.secs=0;
77                 rp->min.nsecs=0;
78                 rp->max.secs=0;
79                 rp->max.nsecs=0;
80                 rp->tot.secs=0;
81                 rp->tot.nsecs=0;
82                 prog_list=rp;
83         } else if((ri->prog==prog_list->program)
84                 &&(ri->vers==prog_list->version)){
85                 rp=prog_list;
86         } else if( (ri->prog<prog_list->program)
87                 ||((ri->prog==prog_list->program)&&(ri->vers<prog_list->version))){
88                 /* we should be first entry in list */
89                 rp=g_malloc(sizeof(rpc_program_t));
90                 rp->next=prog_list;
91                 rp->program=ri->prog;
92                 rp->version=ri->vers;
93                 rp->num=0;
94                 rp->min.secs=0;
95                 rp->min.nsecs=0;
96                 rp->max.secs=0;
97                 rp->max.nsecs=0;
98                 rp->tot.secs=0;
99                 rp->tot.nsecs=0;
100                 prog_list=rp;
101         } else {
102                 /* we go somewhere else in the list */
103                 for(rp=prog_list;rp;rp=rp->next){
104                         if((rp->next)
105                         && (rp->next->program==ri->prog)
106                         && (rp->next->version==ri->vers)){
107                                 rp=rp->next;
108                                 break;
109                         }
110                         if((!rp->next)
111                         || (rp->next->program>ri->prog)
112                         || (  (rp->next->program==ri->prog)
113                             &&(rp->next->version>ri->vers))){
114                                 rpc_program_t *trp;
115                                 trp=g_malloc(sizeof(rpc_program_t));
116                                 trp->next=rp->next;
117                                 trp->program=ri->prog;
118                                 trp->version=ri->vers;
119                                 trp->num=0;
120                                 trp->min.secs=0;
121                                 trp->min.nsecs=0;
122                                 trp->max.secs=0;
123                                 trp->max.nsecs=0;
124                                 trp->tot.secs=0;
125                                 trp->tot.nsecs=0;
126                                 rp->next=trp;
127                                 rp=trp;
128                                 break;
129                         }
130                 }
131         }
132
133
134         /* we are only interested in reply packets */
135         if(ri->request || !rp){
136                 return 0;
137         }
138
139         /* calculate time delta between request and reply */
140         nstime_delta(&delta, &pinfo->fd->abs_ts, &ri->req_time);
141
142         if((rp->max.secs==0)
143         && (rp->max.nsecs==0) ){
144                 rp->max.secs=delta.secs;
145                 rp->max.nsecs=delta.nsecs;
146         }
147
148         if((rp->min.secs==0)
149         && (rp->min.nsecs==0) ){
150                 rp->min.secs=delta.secs;
151                 rp->min.nsecs=delta.nsecs;
152         }
153
154         if( (delta.secs<rp->min.secs)
155         ||( (delta.secs==rp->min.secs)
156           &&(delta.nsecs<rp->min.nsecs) ) ){
157                 rp->min.secs=delta.secs;
158                 rp->min.nsecs=delta.nsecs;
159         }
160
161         if( (delta.secs>rp->max.secs)
162         ||( (delta.secs==rp->max.secs)
163           &&(delta.nsecs>rp->max.nsecs) ) ){
164                 rp->max.secs=delta.secs;
165                 rp->max.nsecs=delta.nsecs;
166         }
167
168         rp->tot.secs += delta.secs;
169         rp->tot.nsecs += delta.nsecs;
170         if(rp->tot.nsecs > NANOSECS_PER_SEC){
171                 rp->tot.nsecs -= NANOSECS_PER_SEC;
172                 rp->tot.secs++;
173         }
174         rp->num++;
175
176         return 1;
177 }
178
179
180 static void
181 rpcprogs_draw(void *dummy _U_)
182 {
183         guint64 td;
184         rpc_program_t *rp;
185         char str[64];
186
187         printf("\n");
188         printf("==========================================================\n");
189         printf("ONC-RPC Program Statistics:\n");
190         printf("Program    Version  Calls    Min SRT    Max SRT    Avg SRT\n");
191         for(rp=prog_list;rp;rp=rp->next){
192                 /* Only display procs with non-zero calls */
193                 if(rp->num==0){
194                         continue;
195                 }
196                 /* Scale the average SRT in units of 1us and round to the nearest us. */
197                 td = ((guint64)(rp->tot.secs)) * NANOSECS_PER_SEC + rp->tot.nsecs;
198                 td = ((td / rp->num) + 500) / 1000;
199
200                 g_snprintf(str, sizeof(str), "%s(%d)",rpc_prog_name(rp->program),rp->program);
201                 printf("%-15s %2d %6d %3d.%06d %3d.%06d %3" G_GINT64_MODIFIER "u.%06" G_GINT64_MODIFIER "u\n",
202                         str,
203                         rp->version,
204                         rp->num,
205                         (int)(rp->min.secs),(rp->min.nsecs+500)/1000,
206                         (int)(rp->max.secs),(rp->max.nsecs+500)/1000,
207                         td/MICROSECS_PER_SEC, td%MICROSECS_PER_SEC
208                 );
209         }
210         printf("===================================================================\n");
211 }
212
213
214 static void
215 rpcprogs_init(const char *optarg _U_, void* userdata _U_)
216 {
217         GString *error_string;
218
219         if(already_enabled){
220                 return;
221         }
222         already_enabled=1;
223
224         error_string=register_tap_listener("rpc", NULL, NULL, 0, NULL, rpcprogs_packet, rpcprogs_draw);
225         if(error_string){
226                 fprintf(stderr,"tshark: Couldn't register rpc,programs tap: %s\n",
227                     error_string->str);
228                 g_string_free(error_string, TRUE);
229                 exit(1);
230         }
231 }
232
233
234 void
235 register_tap_listener_rpcprogs(void)
236 {
237         register_stat_cmd_arg("rpc,programs", rpcprogs_init, NULL);
238 }
239
240