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