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