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