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