pull-prototypes-to-head-20020821
[openafs.git] / src / libadmin / samples / rxstat_get_process.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 /*
11  * This file contains sample code for the rxstats interface 
12  */
13
14 #include <afsconfig.h>
15 #include <afs/param.h>
16
17 RCSID("$Header$");
18
19 #ifdef AFS_NT40_ENV
20 #include <winsock2.h>
21 #include <pthread.h>
22 #endif
23
24 #ifdef HAVE_STRING_H
25 #include <string.h>
26 #else
27 #ifdef HAVE_STRINGS_H
28 #include <strings.h>
29 #endif
30 #endif
31
32 #include <afs/afs_Admin.h>
33 #include <afs/afs_AdminErrors.h>
34 #include <afs/afs_clientAdmin.h>
35 #include <afs/afs_utilAdmin.h>
36 #include <rx/rxstat.h>
37 #include <afs/afsint.h>
38 #define FSINT_COMMON_XG
39 #include <afs/afscbint.h>
40 #include <afs/kauth.h>
41 #include <afs/kautils.h>
42 #include <afs/ptint.h>
43 #include <afs/ptserver.h>
44 #include <afs/vldbint.h>
45 #include <afs/bosint.h>
46 #include <afs/volint.h>
47 #include <afs/volser.h>
48 #include <afs/bosint.h>
49 #include <ubik.h>
50 #include <ubik_int.h>
51
52 extern int RXSTATS_RetrieveProcessRPCStats();
53
54 void Usage()
55 {
56     fprintf(stderr,
57             "Usage: rxstat_get_process <cell> <host> <port>\n");
58     exit(1);
59 }
60
61 void ParseArgs(
62     int argc,
63     char *argv[],
64     char **srvrName,
65     long *srvrPort)
66 {
67     char **argp = argv;
68
69     if (!*(++argp))
70         Usage();
71     *srvrName = *(argp++);
72     if (!*(argp))
73         Usage();
74     *srvrPort = strtol(*(argp++), NULL, 0);
75     if (*srvrPort <= 0 || *srvrPort >= 65536)
76         Usage();
77     if (*(argp))
78         Usage();
79 }
80
81 void GetPrintStrings(
82     afs_RPCStats_p statp,
83     char *ifName,
84     char *ifRole,
85     const char ***funcList,
86     int *funcListLen)
87 {
88     if (statp->s.stats_v1.remote_is_server) {
89         strcpy(ifRole, "client");
90     } else {
91         strcpy(ifRole, "server");
92     }
93
94     switch(statp->s.stats_v1.interfaceId) {
95       case RXSTATS_STATINDEX:
96         strcpy(ifName, "rxstats interface");
97         *funcList = RXSTATS_function_names;
98         *funcListLen = RXSTATS_NO_OF_STAT_FUNCS;
99         break;
100       case RXAFS_STATINDEX:
101         strcpy(ifName, "fileserver interface");
102         *funcList = RXAFS_function_names;
103         *funcListLen = RXAFS_NO_OF_STAT_FUNCS;
104         break;
105       case RXAFSCB_STATINDEX:
106         strcpy(ifName, "callback interface");
107         *funcList = RXAFSCB_function_names;
108         *funcListLen = RXAFSCB_NO_OF_STAT_FUNCS;
109         break;
110       case PR_STATINDEX:
111         strcpy(ifName, "ptserver interface");
112         *funcList = PR_function_names;
113         *funcListLen = PR_NO_OF_STAT_FUNCS;
114         break;
115       case DISK_STATINDEX:
116         strcpy(ifName, "ubik disk interface");
117         *funcList = DISK_function_names;
118         *funcListLen = DISK_NO_OF_STAT_FUNCS;
119         break;
120       case VOTE_STATINDEX:
121         strcpy(ifName, "ubik vote interface");
122         *funcList = VOTE_function_names;
123         *funcListLen = VOTE_NO_OF_STAT_FUNCS;
124         break;
125       case VL_STATINDEX:
126         strcpy(ifName, "volserver interface");
127         *funcList = VL_function_names;
128         *funcListLen = VL_NO_OF_STAT_FUNCS;
129         break;
130       case AFSVolSTATINDEX:
131         strcpy(ifName, "volserver interface");
132         *funcList = AFSVolfunction_names;
133         *funcListLen = AFSVolNO_OF_STAT_FUNCS;
134         break;
135       case BOZO_STATINDEX:
136         strcpy(ifName, "bosserver interface");
137         *funcList = BOZO_function_names;
138         *funcListLen = BOZO_NO_OF_STAT_FUNCS;
139         break;
140       case KAA_STATINDEX:
141         strcpy(ifName, "KAA interface");
142         *funcList = KAA_function_names;
143         *funcListLen = KAA_NO_OF_STAT_FUNCS;
144         break;
145       case KAM_STATINDEX:
146         strcpy(ifName, "KAM interface");
147         *funcList = KAM_function_names;
148         *funcListLen = KAM_NO_OF_STAT_FUNCS;
149         break;
150       case KAT_STATINDEX:
151         strcpy(ifName, "KAT interface");
152         *funcList = KAT_function_names;
153         *funcListLen = KAT_NO_OF_STAT_FUNCS;
154         break;
155       default:
156         sprintf(ifName, "interface 0x%x", statp->s.stats_v1.interfaceId);
157         *funcList = NULL;
158         *funcListLen = 0;
159         break;
160     }
161 }
162
163 int main(int argc, char *argv[])
164 {
165     int rc;
166     afs_status_t st = 0;
167     struct rx_connection *conn;
168     char *srvrName;
169     long srvrPort;
170     void *cellHandle;
171     void *iterator;
172     afs_RPCStats_t stats;
173     char ifName[128];
174     char role[8];
175     const char **funcList;
176     int funcListLen;
177     int index;
178
179     ParseArgs(argc, argv, &srvrName, &srvrPort);
180
181     rc = afsclient_Init(&st);
182     if (!rc) {
183         fprintf(stderr, "afsclient_Init, status %d\n", st);
184         exit(1);
185     }
186
187     rc = afsclient_NullCellOpen(&cellHandle, &st);
188     if (!rc) {
189         fprintf(stderr, "afsclient_NullCellOpen, status %d\n", st);
190         exit(1);
191     }
192
193     rc = afsclient_RPCStatOpenPort(cellHandle, srvrName, srvrPort, &conn, &st);
194     if (!rc) {
195         fprintf(stderr, "afsclient_RPCStatOpenPort, status %d\n", st);
196         exit(1);
197     }
198
199     rc = util_RPCStatsGetBegin(conn, RXSTATS_RetrieveProcessRPCStats,
200                                &iterator, &st);
201     if (!rc) {
202         fprintf(stderr, "util_RPCStatsGetBegin, status %d\n", st);
203         exit(1);
204     }
205
206     while (util_RPCStatsGetNext(iterator, &stats, &st)) {
207         index = stats.s.stats_v1.func_index;
208
209         if (index == 0) {
210             GetPrintStrings(&stats, ifName, role, &funcList, &funcListLen);
211             printf("\nProcess RPC stats for %s accessed as a %s\n\n",
212                    ifName, role);
213         }
214
215         if (index >= funcListLen) {
216             printf("    Function index %d\n", index);
217         } else {
218             printf("    %s\n", funcList[index]);
219         }
220
221         if (!hiszero(stats.s.stats_v1.invocations)) {
222             printf("\tinvoc (%u.%u) bytes_sent (%u.%u) bytes_rcvd (%u.%u)\n",
223                    hgethi(stats.s.stats_v1.invocations),
224                    hgetlo(stats.s.stats_v1.invocations),
225                    hgethi(stats.s.stats_v1.bytes_sent),
226                    hgetlo(stats.s.stats_v1.bytes_sent),
227                    hgethi(stats.s.stats_v1.bytes_rcvd),
228                    hgetlo(stats.s.stats_v1.bytes_rcvd));
229             printf("\tqsum %d.%06d qsqr %d.%06d"
230                    " qmin %d.%06d qmax %d.%06d\n",
231                    stats.s.stats_v1.queue_time_sum.sec,
232                    stats.s.stats_v1.queue_time_sum.usec,
233                    stats.s.stats_v1.queue_time_sum_sqr.sec,
234                    stats.s.stats_v1.queue_time_sum_sqr.usec,
235                    stats.s.stats_v1.queue_time_min.sec,
236                    stats.s.stats_v1.queue_time_min.usec,
237                    stats.s.stats_v1.queue_time_max.sec,
238                    stats.s.stats_v1.queue_time_max.usec);
239             printf("\txsum %d.%06d xsqr %d.%06d"
240                    " xmin %d.%06d xmax %d.%06d\n",
241                    stats.s.stats_v1.execution_time_sum.sec,
242                    stats.s.stats_v1.execution_time_sum.usec,
243                    stats.s.stats_v1.execution_time_sum_sqr.sec,
244                    stats.s.stats_v1.execution_time_sum_sqr.usec,
245                    stats.s.stats_v1.execution_time_min.sec,
246                    stats.s.stats_v1.execution_time_min.usec,
247                    stats.s.stats_v1.execution_time_max.sec,
248                    stats.s.stats_v1.execution_time_max.usec);
249         } else {
250             printf("\tNever invoked\n");
251         }
252     }
253     if (st != ADMITERATORDONE) {
254         fprintf(stderr, "util_RPCStatsGetNext, status %d\n", st);
255         exit(1);
256     }
257     printf("\n");
258
259     rc = util_RPCStatsGetDone(iterator, &st);
260     if (!rc) {
261         fprintf(stderr, "util_RPCStatsGetDone, status %d\n", st);
262         exit(1);
263     }
264
265     rc = afsclient_RPCStatClose(conn, &st);
266     if (!rc) {
267         fprintf(stderr, "afsclient_RPCStatClose, status %d\n", st);
268         exit(1);
269     }
270
271     rc = afsclient_CellClose(cellHandle, &st);
272     if (!rc) {
273         fprintf(stderr, "afsclient_CellClose, status %d\n", st);
274         exit(1);
275     }
276
277     exit(0);
278 }