2 * Copyright 2000, International Business Machines Corporation and others.
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
12 * Implementation of the client side of the AFS Cache Manager
13 * extended statistics facility.
15 *------------------------------------------------------------------------*/
17 #include <afsconfig.h>
18 #include <afs/param.h>
22 #include "xstat_cm.h" /*Interface for this module*/
23 #include <lwp.h> /*Lightweight process package*/
33 #define LWP_STACK_SIZE (16 * 1024)
36 * Routines we need that don't have explicit include file definitions.
38 extern char *hostutil_GetNameByINet(); /*Host parsing utility*/
43 int xstat_cm_numServers; /*Num connected servers*/
44 struct xstat_cm_ConnectionInfo
45 *xstat_cm_ConnInfo; /*Ptr to connection array*/
46 int numCollections; /*Number of data collections*/
47 struct xstat_cm_ProbeResults xstat_cm_Results; /*Latest probe results*/
48 char terminationEvent; /*One-shot termination event*/
50 afs_int32 xstat_cmData[AFSCB_MAX_XSTAT_LONGS]; /*Buffer for collected data*/
55 static int xstat_cm_ProbeFreqInSecs; /*Probe freq. in seconds*/
56 static int xstat_cm_initflag = 0; /*Was init routine called?*/
57 static int xstat_cm_debug = 0; /*Debugging output enabled?*/
58 static int xstat_cm_oneShot = 0; /*One-shot operation?*/
59 static int (*xstat_cm_Handler)(); /*Probe handler routine*/
60 static PROCESS probeLWP_ID; /*Probe LWP process ID*/
61 static int xstat_cm_numCollections; /*Number of desired collections*/
62 static afs_int32 *xstat_cm_collIDP; /*Ptr to collection IDs desired*/
65 /*------------------------------------------------------------------------
66 * [private] xstat_cm_CleanupInit
69 * Set up for recovery after an error in initialization (i.e.,
70 * during a call to xstat_cm_Init.
77 * Error value otherwise.
80 * This routine is private to the module.
83 * Zeros out basic data structures.
84 *------------------------------------------------------------------------*/
86 static int xstat_cm_CleanupInit()
88 xstat_cm_ConnInfo = (struct xstat_cm_ConnectionInfo *)0;
89 xstat_cm_Results.probeNum = 0;
90 xstat_cm_Results.probeTime = 0;
91 xstat_cm_Results.connP = (struct xstat_cm_ConnectionInfo *)0;
92 xstat_cm_Results.collectionNumber = 0;
93 xstat_cm_Results.data.AFSCB_CollData_len = AFSCB_MAX_XSTAT_LONGS;
94 xstat_cm_Results.data.AFSCB_CollData_val = (afs_int32 *)xstat_cmData;
95 xstat_cm_Results.probeOK = 0;
101 /*------------------------------------------------------------------------
102 * [exported] xstat_cm_Cleanup
105 * Clean up our memory and connection state.
108 * int a_releaseMem : Should we free up malloc'ed areas?
111 * 0 on total success,
112 * -1 if the module was never initialized, or there was a problem
113 * with the xstat_cm connection array.
116 * xstat_cm_numServers should be properly set. We don't do anything
117 * unless xstat_cm_Init() has already been called.
120 * Shuts down Rx connections gracefully, frees allocated space
122 *------------------------------------------------------------------------*/
124 int xstat_cm_Cleanup(int a_releaseMem)
126 static char rn[] = "xstat_cm_Cleanup"; /*Routine name*/
127 int code; /*Return code*/
128 int conn_idx; /*Current connection index*/
129 struct xstat_cm_ConnectionInfo *curr_conn; /*Ptr to xstat_cm connection*/
132 * Assume the best, but check the worst.
134 if (!xstat_cm_initflag) {
135 fprintf(stderr, "[%s] Refused; module not initialized\n", rn);
142 * Take care of all Rx connections first. Check to see that the
143 * server count is a legal value.
145 if (xstat_cm_numServers <= 0) {
147 "[%s] Illegal number of servers (xstat_cm_numServers = %d)\n",
148 rn, xstat_cm_numServers);
152 if (xstat_cm_ConnInfo != (struct xstat_cm_ConnectionInfo *)0) {
154 * The xstat_cm connection structure array exists. Go through
155 * it and close up any Rx connections it holds.
157 curr_conn = xstat_cm_ConnInfo;
158 for (conn_idx = 0; conn_idx < xstat_cm_numServers; conn_idx++) {
159 if (curr_conn->rxconn != (struct rx_connection *)0) {
160 rx_DestroyConnection(curr_conn->rxconn);
161 curr_conn->rxconn = (struct rx_connection *)0;
164 } /*for each xstat_cm connection*/
165 } /*xstat_cm connection structure exists*/
166 } /*Legal number of servers*/
169 * If asked to, release the space we've allocated.
172 if (xstat_cm_ConnInfo != (struct xstat_cm_ConnectionInfo *)0)
173 free(xstat_cm_ConnInfo);
177 * Return the news, whatever it is.
183 /*------------------------------------------------------------------------
184 * [private] xstat_cm_LWP
187 * This LWP iterates over the server connections and gathers up
188 * the desired statistics from each one on a regular basis, for
189 * all known data collections. The associated handler function
190 * is called each time a new data collection is received.
199 * Started by xstat_cm_Init(), uses global structures and the
200 * global private xstat_cm_oneShot variable.
204 *------------------------------------------------------------------------*/
205 static void xstat_cm_LWP()
207 static char rn[] = "xstat_cm_LWP"; /*Routine name*/
208 register afs_int32 code; /*Results of calls*/
209 int oneShotCode; /*Result of one-shot signal*/
210 struct timeval tv; /*Time structure*/
211 int conn_idx; /*Connection index*/
212 struct xstat_cm_ConnectionInfo *curr_conn; /*Current connection*/
213 afs_int32 srvVersionNumber; /*Xstat version #*/
214 afs_int32 clientVersionNumber; /*Client xstat version*/
215 afs_int32 numColls; /*Number of collections to get*/
216 afs_int32 *currCollIDP; /*Curr collection ID desired*/
219 * Set up some numbers we'll need.
221 clientVersionNumber = AFSCB_XSTAT_VERSION;
223 while (1) { /*Service loop*/
225 * Iterate through the server connections, gathering data.
226 * Don't forget to bump the probe count and zero the statistics
227 * areas before calling the servers.
230 printf("[%s] Waking up, getting data from %d server(s)\n",
231 rn, xstat_cm_numServers);
232 curr_conn = xstat_cm_ConnInfo;
233 xstat_cm_Results.probeNum++;
235 for (conn_idx = 0; conn_idx < xstat_cm_numServers; conn_idx++) {
237 * Grab the statistics for the current Cache Manager, if the
238 * connection is valid.
241 printf("[%s] Getting collections from Cache Manager '%s'\n",
242 rn, curr_conn->hostName);
243 if (curr_conn->rxconn != (struct rx_connection *)0) {
245 printf("[%s] Connection OK, calling RXAFSCB_GetXStats\n",
249 * Probe the given CM for each desired collection.
251 currCollIDP = xstat_cm_collIDP;
253 numColls < xstat_cm_numCollections;
254 numColls++, currCollIDP++) {
256 * Initialize the per-probe values.
259 printf("[%s] Asking for data collection %d\n",
261 xstat_cm_Results.collectionNumber = *currCollIDP;
262 xstat_cm_Results.data.AFSCB_CollData_len =
263 AFSCB_MAX_XSTAT_LONGS;
264 memset(xstat_cm_Results.data.AFSCB_CollData_val, 0, AFSCB_MAX_XSTAT_LONGS * 4);
266 xstat_cm_Results.connP = curr_conn;
268 if (xstat_cm_debug) {
269 printf("%s: Calling RXAFSCB_GetXStats, conn=0x%x, clientVersionNumber=%d, collectionNumber=%d, srvVersionNumberP=0x%x, timeP=0x%x, dataP=0x%x\n",
270 rn, curr_conn->rxconn,
274 &(xstat_cm_Results.probeTime),
275 &(xstat_cm_Results.data));
276 printf("%s: [bufflen=%d, buffer at 0x%x]\n",
278 xstat_cm_Results.data.AFSCB_CollData_len,
279 xstat_cm_Results.data.AFSCB_CollData_val);
282 xstat_cm_Results.probeOK =
283 RXAFSCB_GetXStats(curr_conn->rxconn,
287 &(xstat_cm_Results.probeTime),
288 &(xstat_cm_Results.data));
291 * Now that we (may) have the data for this connection,
292 * call the associated handler function. The handler
293 * does not take any explicit parameters, but rather
294 * gets to the goodies via some of the objects exported
298 printf("[%s] Calling handler routine.\n",
300 code = xstat_cm_Handler();
303 "[%s] Handler routine got error code %d\n",
305 } /*For each collection*/
306 } /*Valid Rx connection*/
309 * Advance the xstat_cm connection pointer.
313 } /*For each xstat_cm connection*/
316 * All (valid) connections have been probed. Fall asleep for the
317 * prescribed number of seconds, unless we're a one-shot. In
318 * that case, we need to signal our caller that we're done.
321 printf("[%s] Polling complete for probe round %d.\n",
322 rn, xstat_cm_Results.probeNum);
324 if (xstat_cm_oneShot) {
326 * One-shot execution desired. Signal our main procedure
327 * that we've finished our collection round.
330 printf("[%s] Signalling main process at 0x%x\n",
331 rn, &terminationEvent);
332 oneShotCode = LWP_SignalProcess(&terminationEvent);
335 "[%s] Error %d from LWP_SignalProcess()",
337 break; /*from the perpetual while loop*/
338 } /*One-shot execution*/
341 * Continuous execution desired. Sleep for the required
344 tv.tv_sec = xstat_cm_ProbeFreqInSecs;
347 printf("[%s] Falling asleep for %d seconds\n",
348 rn, xstat_cm_ProbeFreqInSecs);
349 code = IOMGR_Select(0, /*Num fids*/
350 0, /*Descs ready for reading*/
351 0, /*Descs ready for writing*/
352 0, /*Descs w/exceptional conditions*/
353 &tv); /*Ptr to timeout structure*/
356 "[%s] IOMGR_Select returned code %d\n",
358 } /*Continuous execution*/
363 /*------------------------------------------------------------------------
364 * [exported] xstat_cm_Init
367 * Initialize the xstat_cm module: set up Rx connections to the
368 * given set of Cache Managers, start up the probe LWP, and
369 * associate the routine to be called when a probe completes.
370 * Also, let it know which collections you're interested in.
373 * int a_numServers : Num. servers to connect to.
374 * struct sockaddr_in *a_socketArray : Array of server sockets.
375 * int a_ProbeFreqInSecs : Probe frequency in seconds.
376 * int (*a_ProbeHandler)() : Ptr to probe handler fcn.
377 * int a_flags; : Various flags.
378 * int a_numCollections : Number of collections desired.
379 * afs_int32 *a_collIDP : Ptr to collection IDs.
383 * -2 for (at least one) connection error,
384 * LWP process creation code, if it failed,
385 * -1 for other fatal errors.
388 * *** MUST BE THE FIRST ROUTINE CALLED FROM THIS PACKAGE ***
391 * Sets up just about everything.
392 *------------------------------------------------------------------------*/
394 int xstat_cm_Init(int a_numServers, struct sockaddr_in *a_socketArray,
395 int a_ProbeFreqInSecs, int (*a_ProbeHandler)(), int a_flags,
396 int a_numCollections, afs_int32 *a_collIDP)
399 static char rn[] = "xstat_cm_Init"; /*Routine name*/
400 register afs_int32 code; /*Return value*/
401 struct rx_securityClass *secobj; /*Client security object*/
402 int arg_errfound; /*Argument error found?*/
403 int curr_srv; /*Current server idx*/
404 struct xstat_cm_ConnectionInfo *curr_conn; /*Ptr to current conn*/
405 char *hostNameFound; /*Ptr to returned host name*/
406 int conn_err; /*Connection error?*/
407 int collIDBytes; /*Num bytes in coll ID array*/
410 * If we've already been called, snicker at the bozo, gently
411 * remind him of his doubtful heritage, and return success.
413 if (xstat_cm_initflag) {
414 fprintf(stderr, "[%s] Called multiple times!\n", rn);
418 xstat_cm_initflag = 1;
421 * Check the parameters for bogosities.
424 if (a_numServers <= 0) {
425 fprintf(stderr, "[%s] Illegal number of servers: %d\n",
429 if (a_socketArray == (struct sockaddr_in *)0) {
430 fprintf(stderr, "[%s] Null server socket array argument\n", rn);
433 if (a_ProbeFreqInSecs <= 0) {
434 fprintf(stderr, "[%s] Illegal probe frequency: %d\n",
435 rn, a_ProbeFreqInSecs);
438 if (a_ProbeHandler == (int (*)())0) {
439 fprintf(stderr, "[%s] Null probe handler function argument\n",
443 if (a_numCollections <= 0) {
444 fprintf(stderr, "[%s] Illegal collection count argument: %d\n",
448 if (a_collIDP == NULL) {
449 fprintf(stderr, "[%s] Null collection ID array argument\n", rn);
456 * Record our passed-in info.
458 xstat_cm_debug = (a_flags & XSTAT_CM_INITFLAG_DEBUGGING);
459 xstat_cm_oneShot = (a_flags & XSTAT_CM_INITFLAG_ONE_SHOT);
460 xstat_cm_numServers = a_numServers;
461 xstat_cm_Handler = a_ProbeHandler;
462 xstat_cm_ProbeFreqInSecs = a_ProbeFreqInSecs;
463 xstat_cm_numCollections = a_numCollections;
464 collIDBytes = xstat_cm_numCollections * sizeof(afs_int32);
465 xstat_cm_collIDP = (afs_int32 *)(malloc(collIDBytes));
466 memcpy(xstat_cm_collIDP, a_collIDP, collIDBytes);
467 if (xstat_cm_debug) {
468 printf("[%s] Asking for %d collection(s): ", rn, xstat_cm_numCollections);
469 for (curr_srv = 0; curr_srv < xstat_cm_numCollections; curr_srv++)
470 printf("%d ", *(xstat_cm_collIDP+curr_srv));
475 * Get ready in case we have to do a cleanup - basically, zero
478 code = xstat_cm_CleanupInit();
483 * Allocate the necessary data structures and initialize everything
487 (struct xstat_cm_ConnectionInfo *)
488 malloc(a_numServers * sizeof(struct xstat_cm_ConnectionInfo));
489 if (xstat_cm_ConnInfo == (struct xstat_cm_ConnectionInfo *)0) {
491 "[%s] Can't allocate %d connection info structs (%d bytes)\n",
493 (a_numServers * sizeof(struct xstat_cm_ConnectionInfo)));
494 return(-1); /*No cleanup needs to be done yet*/
498 * Initialize the Rx subsystem, just in case nobody's done it.
501 printf("[%s] Initializing Rx on port 0\n", rn);
502 code = rx_Init(htons(0));
504 fprintf(stderr, "[%s] Fatal error in rx_Init(), error=%d\n",
510 printf("[%s] Rx initialized on port 0\n", rn);
513 * Create a null Rx client security object, to be used by the
516 secobj = rxnull_NewClientSecurityObject();
517 if (secobj == (struct rx_securityClass *)0) {
519 "[%s] Can't create probe LWP client security object.\n",
521 xstat_cm_Cleanup(1); /*Delete already-malloc'ed areas*/
525 printf("[%s] Probe LWP client security object created\n",
528 curr_conn = xstat_cm_ConnInfo;
530 for (curr_srv = 0; curr_srv < a_numServers; curr_srv++) {
532 * Copy in the socket info for the current server, resolve its
533 * printable name if possible.
535 if (xstat_cm_debug) {
536 printf("[%s] Copying in the following socket info:\n",
538 printf("[%s] IP addr 0x%lx, port %d\n", rn,
539 (a_socketArray + curr_srv)->sin_addr.s_addr,
540 (a_socketArray + curr_srv)->sin_port);
542 memcpy(&(curr_conn->skt), a_socketArray + curr_srv, sizeof(struct sockaddr_in));
545 hostutil_GetNameByINet(curr_conn->skt.sin_addr.s_addr);
546 if (hostNameFound == NULL) {
548 "[%s] Can't map Internet address %lu to a string name\n",
549 rn, curr_conn->skt.sin_addr.s_addr);
550 curr_conn->hostName[0] = '\0';
553 strcpy(curr_conn->hostName, hostNameFound);
555 printf("[%s] Host name for server index %d is %s\n",
556 rn, curr_srv, curr_conn->hostName);
560 * Make an Rx connection to the current server.
563 printf("[%s] Connecting to srv idx %d, IP addr 0x%lx, port %d, service 1\n",
564 rn, curr_srv, curr_conn->skt.sin_addr.s_addr,
565 curr_conn->skt.sin_port);
567 rx_NewConnection(curr_conn->skt.sin_addr.s_addr, /*Server addr*/
568 curr_conn->skt.sin_port, /*Server port*/
570 secobj, /*Security obj*/
572 if (curr_conn->rxconn == (struct rx_connection *)0) {
574 "[%s] Can't create Rx connection to server '%s' (%lu)\n",
575 rn, curr_conn->hostName, curr_conn->skt.sin_addr.s_addr);
579 printf("[%s] New connection at 0x%lx\n",
580 rn, curr_conn->rxconn);
583 * Bump the current xstat_cm connection to set up.
590 * Start up the probe LWP.
593 printf("[%s] Creating the probe LWP\n", rn);
595 LWP_CreateProcess(xstat_cm_LWP, /*Function to start up*/
596 LWP_STACK_SIZE, /*Stack size in bytes*/
598 (void *) 0, /*Parameters*/
599 "xstat_cm Worker", /*Name to use*/
600 &probeLWP_ID); /*Returned LWP process ID*/
603 "[%s] Can't create xstat_cm LWP! Error is %d\n",
605 xstat_cm_Cleanup(1); /*Delete already-malloc'ed areas*/
609 printf("[%s] Probe LWP process structure located at 0x%x\n",
613 * Return the final results.
622 /*------------------------------------------------------------------------
623 * [exported] xstat_cm_ForceProbeNow
626 * Wake up the probe LWP, forcing it to execute a probe immediately.
633 * Error value otherwise.
636 * The module must have been initialized.
640 *------------------------------------------------------------------------*/
642 int xstat_cm_ForceProbeNow()
644 static char rn[] = "xstat_cm_ForceProbeNow"; /*Routine name*/
647 * There isn't a prayer unless we've been initialized.
649 if (!xstat_cm_initflag) {
650 fprintf(stderr, "[%s] Must call xstat_cm_Init first!\n", rn);
655 * Kick the sucker in the side.
657 IOMGR_Cancel(probeLWP_ID);
660 * We did it, so report the happy news.