rx: Use native 64bit data counters
[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 #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 #ifdef AFS_DARWIN_ENV
33 pthread_mutex_t des_init_mutex = PTHREAD_MUTEX_INITIALIZER;
34 pthread_mutex_t des_random_mutex = PTHREAD_MUTEX_INITIALIZER;
35 pthread_mutex_t rxkad_random_mutex = PTHREAD_MUTEX_INITIALIZER;
36 #endif /* AFS_DARWIN_ENV */
37
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 void
54 Usage(void)
55 {
56     fprintf(stderr, "Usage: rxstat_get_process <cell> <host> <port>\n");
57     exit(1);
58 }
59
60 void
61 ParseArgs(int argc, char *argv[], char **srvrName, long *srvrPort)
62 {
63     char **argp = argv;
64
65     if (!*(++argp))
66         Usage();
67     *srvrName = *(argp++);
68     if (!*(argp))
69         Usage();
70     *srvrPort = strtol(*(argp++), NULL, 0);
71     if (*srvrPort <= 0 || *srvrPort >= 65536)
72         Usage();
73     if (*(argp))
74         Usage();
75 }
76
77 void
78 GetPrintStrings(afs_RPCStats_p statp, char *ifName, char *ifRole,
79                 const char ***funcList, int *funcListLen)
80 {
81     if (statp->s.stats_v1.remote_is_server) {
82         strcpy(ifRole, "client");
83     } else {
84         strcpy(ifRole, "server");
85     }
86
87     switch (statp->s.stats_v1.interfaceId) {
88     case RXSTATS_STATINDEX:
89         strcpy(ifName, "rxstats interface");
90         *funcList = RXSTATS_function_names;
91         *funcListLen = RXSTATS_NO_OF_STAT_FUNCS;
92         break;
93     case RXAFS_STATINDEX:
94         strcpy(ifName, "fileserver interface");
95         *funcList = RXAFS_function_names;
96         *funcListLen = RXAFS_NO_OF_STAT_FUNCS;
97         break;
98     case RXAFSCB_STATINDEX:
99         strcpy(ifName, "callback interface");
100         *funcList = RXAFSCB_function_names;
101         *funcListLen = RXAFSCB_NO_OF_STAT_FUNCS;
102         break;
103     case PR_STATINDEX:
104         strcpy(ifName, "ptserver interface");
105         *funcList = PR_function_names;
106         *funcListLen = PR_NO_OF_STAT_FUNCS;
107         break;
108     case DISK_STATINDEX:
109         strcpy(ifName, "ubik disk interface");
110         *funcList = DISK_function_names;
111         *funcListLen = DISK_NO_OF_STAT_FUNCS;
112         break;
113     case VOTE_STATINDEX:
114         strcpy(ifName, "ubik vote interface");
115         *funcList = VOTE_function_names;
116         *funcListLen = VOTE_NO_OF_STAT_FUNCS;
117         break;
118     case VL_STATINDEX:
119         strcpy(ifName, "volserver interface");
120         *funcList = VL_function_names;
121         *funcListLen = VL_NO_OF_STAT_FUNCS;
122         break;
123     case AFSVolSTATINDEX:
124         strcpy(ifName, "volserver interface");
125         *funcList = AFSVolfunction_names;
126         *funcListLen = AFSVolNO_OF_STAT_FUNCS;
127         break;
128     case BOZO_STATINDEX:
129         strcpy(ifName, "bosserver interface");
130         *funcList = BOZO_function_names;
131         *funcListLen = BOZO_NO_OF_STAT_FUNCS;
132         break;
133     case KAA_STATINDEX:
134         strcpy(ifName, "KAA interface");
135         *funcList = KAA_function_names;
136         *funcListLen = KAA_NO_OF_STAT_FUNCS;
137         break;
138     case KAM_STATINDEX:
139         strcpy(ifName, "KAM interface");
140         *funcList = KAM_function_names;
141         *funcListLen = KAM_NO_OF_STAT_FUNCS;
142         break;
143     case KAT_STATINDEX:
144         strcpy(ifName, "KAT interface");
145         *funcList = KAT_function_names;
146         *funcListLen = KAT_NO_OF_STAT_FUNCS;
147         break;
148     default:
149         sprintf(ifName, "interface 0x%x", statp->s.stats_v1.interfaceId);
150         *funcList = NULL;
151         *funcListLen = 0;
152         break;
153     }
154 }
155
156 int
157 main(int argc, char *argv[])
158 {
159     int rc;
160     afs_status_t st = 0;
161     struct rx_connection *conn;
162     char *srvrName;
163     long srvrPort;
164     void *cellHandle;
165     void *iterator;
166     afs_RPCStats_t stats;
167     char ifName[128];
168     char role[8];
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_RetrieveProcessRPCStats,
195                                &iterator, &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, &funcList, &funcListLen);
206             printf("\nProcess RPC stats for %s accessed as a %s\n\n", ifName,
207                    role);
208         }
209
210         if (index >= funcListLen) {
211             printf("    Function index %d\n", index);
212         } else {
213             printf("    %s\n", funcList[index]);
214         }
215
216         if (stats.s.stats_v1.invocations != 0) {
217             printf("\tinvoc %"AFS_UINT64_FMT
218                    " bytes_sent %"AFS_UINT64_FMT
219                    " bytes_rcvd %"AFS_UINT64_FMT"\n",
220                    stats.s.stats_v1.invocations,
221                    stats.s.stats_v1.bytes_sent,
222                    stats.s.stats_v1.bytes_rcvd);
223             printf("\tqsum %d.%06d qsqr %d.%06d"
224                    " qmin %d.%06d qmax %d.%06d\n",
225                    stats.s.stats_v1.queue_time_sum.sec,
226                    stats.s.stats_v1.queue_time_sum.usec,
227                    stats.s.stats_v1.queue_time_sum_sqr.sec,
228                    stats.s.stats_v1.queue_time_sum_sqr.usec,
229                    stats.s.stats_v1.queue_time_min.sec,
230                    stats.s.stats_v1.queue_time_min.usec,
231                    stats.s.stats_v1.queue_time_max.sec,
232                    stats.s.stats_v1.queue_time_max.usec);
233             printf("\txsum %d.%06d xsqr %d.%06d"
234                    " xmin %d.%06d xmax %d.%06d\n",
235                    stats.s.stats_v1.execution_time_sum.sec,
236                    stats.s.stats_v1.execution_time_sum.usec,
237                    stats.s.stats_v1.execution_time_sum_sqr.sec,
238                    stats.s.stats_v1.execution_time_sum_sqr.usec,
239                    stats.s.stats_v1.execution_time_min.sec,
240                    stats.s.stats_v1.execution_time_min.usec,
241                    stats.s.stats_v1.execution_time_max.sec,
242                    stats.s.stats_v1.execution_time_max.usec);
243         } else {
244             printf("\tNever invoked\n");
245         }
246     }
247     if (st != ADMITERATORDONE) {
248         fprintf(stderr, "util_RPCStatsGetNext, status %d\n", st);
249         exit(1);
250     }
251     printf("\n");
252
253     rc = util_RPCStatsGetDone(iterator, &st);
254     if (!rc) {
255         fprintf(stderr, "util_RPCStatsGetDone, status %d\n", st);
256         exit(1);
257     }
258
259     rc = afsclient_RPCStatClose(conn, &st);
260     if (!rc) {
261         fprintf(stderr, "afsclient_RPCStatClose, status %d\n", st);
262         exit(1);
263     }
264
265     rc = afsclient_CellClose(cellHandle, &st);
266     if (!rc) {
267         fprintf(stderr, "afsclient_CellClose, status %d\n", st);
268         exit(1);
269     }
270
271     exit(0);
272 }