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>
23 #include "xstat_cm.h" /*Interface for this module */
24 #include <lwp.h> /*Lightweight process package */
26 #include <afs/afsutil.h>
36 #define LWP_STACK_SIZE (16 * 1024)
39 * Routines we need that don't have explicit include file definitions.
41 extern char *hostutil_GetNameByINet(); /*Host parsing utility */
46 int xstat_cm_numServers; /*Num connected servers */
47 struct xstat_cm_ConnectionInfo
48 *xstat_cm_ConnInfo; /*Ptr to connection array */
49 int numCollections; /*Number of data collections */
50 struct xstat_cm_ProbeResults xstat_cm_Results; /*Latest probe results */
51 char terminationEvent; /*One-shot termination event */
53 afs_int32 xstat_cmData[AFSCB_MAX_XSTAT_LONGS]; /*Buffer for collected data */
58 static int xstat_cm_ProbeFreqInSecs; /*Probe freq. in seconds */
59 static int xstat_cm_initflag = 0; /*Was init routine called? */
60 static int xstat_cm_debug = 0; /*Debugging output enabled? */
61 static int xstat_cm_oneShot = 0; /*One-shot operation? */
62 static int (*xstat_cm_Handler) (); /*Probe handler routine */
63 static PROCESS probeLWP_ID; /*Probe LWP process ID */
64 static int xstat_cm_numCollections; /*Number of desired collections */
65 static afs_int32 *xstat_cm_collIDP; /*Ptr to collection IDs desired */
68 /*------------------------------------------------------------------------
69 * [private] xstat_cm_CleanupInit
72 * Set up for recovery after an error in initialization (i.e.,
73 * during a call to xstat_cm_Init.
80 * Error value otherwise.
83 * This routine is private to the module.
86 * Zeros out basic data structures.
87 *------------------------------------------------------------------------*/
90 xstat_cm_CleanupInit()
92 xstat_cm_ConnInfo = (struct xstat_cm_ConnectionInfo *)0;
93 xstat_cm_Results.probeNum = 0;
94 xstat_cm_Results.probeTime = 0;
95 xstat_cm_Results.connP = (struct xstat_cm_ConnectionInfo *)0;
96 xstat_cm_Results.collectionNumber = 0;
97 xstat_cm_Results.data.AFSCB_CollData_len = AFSCB_MAX_XSTAT_LONGS;
98 xstat_cm_Results.data.AFSCB_CollData_val = (afs_int32 *) xstat_cmData;
99 xstat_cm_Results.probeOK = 0;
105 /*------------------------------------------------------------------------
106 * [exported] xstat_cm_Cleanup
109 * Clean up our memory and connection state.
112 * int a_releaseMem : Should we free up malloc'ed areas?
115 * 0 on total success,
116 * -1 if the module was never initialized, or there was a problem
117 * with the xstat_cm connection array.
120 * xstat_cm_numServers should be properly set. We don't do anything
121 * unless xstat_cm_Init() has already been called.
124 * Shuts down Rx connections gracefully, frees allocated space
126 *------------------------------------------------------------------------*/
129 xstat_cm_Cleanup(int a_releaseMem)
131 static char rn[] = "xstat_cm_Cleanup"; /*Routine name */
132 int code; /*Return code */
133 int conn_idx; /*Current connection index */
134 struct xstat_cm_ConnectionInfo *curr_conn; /*Ptr to xstat_cm connection */
137 * Assume the best, but check the worst.
139 if (!xstat_cm_initflag) {
140 fprintf(stderr, "[%s] Refused; module not initialized\n", rn);
146 * Take care of all Rx connections first. Check to see that the
147 * server count is a legal value.
149 if (xstat_cm_numServers <= 0) {
151 "[%s] Illegal number of servers (xstat_cm_numServers = %d)\n",
152 rn, xstat_cm_numServers);
155 if (xstat_cm_ConnInfo != (struct xstat_cm_ConnectionInfo *)0) {
157 * The xstat_cm connection structure array exists. Go through
158 * it and close up any Rx connections it holds.
160 curr_conn = xstat_cm_ConnInfo;
161 for (conn_idx = 0; conn_idx < xstat_cm_numServers; conn_idx++) {
162 if (curr_conn->rxconn != (struct rx_connection *)0) {
163 rx_DestroyConnection(curr_conn->rxconn);
164 curr_conn->rxconn = (struct rx_connection *)0;
167 } /*for each xstat_cm connection */
168 } /*xstat_cm connection structure exists */
169 } /*Legal number of servers */
172 * If asked to, release the space we've allocated.
175 if (xstat_cm_ConnInfo != (struct xstat_cm_ConnectionInfo *)0)
176 free(xstat_cm_ConnInfo);
180 * Return the news, whatever it is.
186 /*------------------------------------------------------------------------
187 * [private] xstat_cm_LWP
190 * This LWP iterates over the server connections and gathers up
191 * the desired statistics from each one on a regular basis, for
192 * all known data collections. The associated handler function
193 * is called each time a new data collection is received.
202 * Started by xstat_cm_Init(), uses global structures and the
203 * global private xstat_cm_oneShot variable.
207 *------------------------------------------------------------------------*/
211 static char rn[] = "xstat_cm_LWP"; /*Routine name */
212 register afs_int32 code; /*Results of calls */
213 int oneShotCode; /*Result of one-shot signal */
214 struct timeval tv; /*Time structure */
215 int conn_idx; /*Connection index */
216 struct xstat_cm_ConnectionInfo *curr_conn; /*Current connection */
217 afs_int32 srvVersionNumber; /*Xstat version # */
218 afs_int32 clientVersionNumber; /*Client xstat version */
219 afs_int32 numColls; /*Number of collections to get */
220 afs_int32 *currCollIDP; /*Curr collection ID desired */
223 * Set up some numbers we'll need.
225 clientVersionNumber = AFSCB_XSTAT_VERSION;
227 while (1) { /*Service loop */
229 * Iterate through the server connections, gathering data.
230 * Don't forget to bump the probe count and zero the statistics
231 * areas before calling the servers.
234 printf("[%s] Waking up, getting data from %d server(s)\n", rn,
235 xstat_cm_numServers);
236 curr_conn = xstat_cm_ConnInfo;
237 xstat_cm_Results.probeNum++;
239 for (conn_idx = 0; conn_idx < xstat_cm_numServers; conn_idx++) {
241 * Grab the statistics for the current Cache Manager, if the
242 * connection is valid.
245 printf("[%s] Getting collections from Cache Manager '%s'\n",
246 rn, curr_conn->hostName);
247 if (curr_conn->rxconn != (struct rx_connection *)0) {
249 printf("[%s] Connection OK, calling RXAFSCB_GetXStats\n",
253 * Probe the given CM for each desired collection.
255 currCollIDP = xstat_cm_collIDP;
256 for (numColls = 0; numColls < xstat_cm_numCollections;
257 numColls++, currCollIDP++) {
259 * Initialize the per-probe values.
262 printf("[%s] Asking for data collection %d\n", rn,
264 xstat_cm_Results.collectionNumber = *currCollIDP;
265 xstat_cm_Results.data.AFSCB_CollData_len =
266 AFSCB_MAX_XSTAT_LONGS;
267 memset(xstat_cm_Results.data.AFSCB_CollData_val, 0,
268 AFSCB_MAX_XSTAT_LONGS * 4);
270 xstat_cm_Results.connP = curr_conn;
272 if (xstat_cm_debug) {
274 ("%s: Calling RXAFSCB_GetXStats, conn=0x%x, clientVersionNumber=%d, collectionNumber=%d, srvVersionNumberP=0x%x, timeP=0x%x, dataP=0x%x\n",
275 rn, curr_conn->rxconn, clientVersionNumber,
276 *currCollIDP, &srvVersionNumber,
277 &(xstat_cm_Results.probeTime),
278 &(xstat_cm_Results.data));
279 printf("%s: [bufflen=%d, buffer at 0x%x]\n", rn,
280 xstat_cm_Results.data.AFSCB_CollData_len,
281 xstat_cm_Results.data.AFSCB_CollData_val);
284 xstat_cm_Results.probeOK =
285 RXAFSCB_GetXStats(curr_conn->rxconn,
286 clientVersionNumber, *currCollIDP,
288 &(xstat_cm_Results.probeTime),
289 &(xstat_cm_Results.data));
292 * Now that we (may) have the data for this connection,
293 * call the associated handler function. The handler
294 * does not take any explicit parameters, but rather
295 * gets to the goodies via some of the objects exported
299 printf("[%s] Calling handler routine.\n", rn);
300 code = xstat_cm_Handler();
303 "[%s] Handler routine got error code %d\n",
305 } /*For each collection */
308 /*Valid Rx connection */
310 * Advance the xstat_cm connection pointer.
314 } /*For each xstat_cm connection */
317 * All (valid) connections have been probed. Fall asleep for the
318 * prescribed number of seconds, unless we're a one-shot. In
319 * that case, we need to signal our caller that we're done.
322 printf("[%s] Polling complete for probe round %d.\n", rn,
323 xstat_cm_Results.probeNum);
325 if (xstat_cm_oneShot) {
327 * One-shot execution desired. Signal our main procedure
328 * that we've finished our collection round.
331 printf("[%s] Signalling main process at 0x%x\n", rn,
333 oneShotCode = LWP_SignalProcess(&terminationEvent);
335 fprintf(stderr, "[%s] Error %d from LWP_SignalProcess()", rn,
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", rn,
348 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 */
355 fprintf(stderr, "[%s] IOMGR_Select returned code %d\n", rn,
357 } /*Continuous execution */
362 /*------------------------------------------------------------------------
363 * [exported] xstat_cm_Init
366 * Initialize the xstat_cm module: set up Rx connections to the
367 * given set of Cache Managers, start up the probe LWP, and
368 * associate the routine to be called when a probe completes.
369 * Also, let it know which collections you're interested in.
372 * int a_numServers : Num. servers to connect to.
373 * struct sockaddr_in *a_socketArray : Array of server sockets.
374 * int a_ProbeFreqInSecs : Probe frequency in seconds.
375 * int (*a_ProbeHandler)() : Ptr to probe handler fcn.
376 * int a_flags; : Various flags.
377 * int a_numCollections : Number of collections desired.
378 * afs_int32 *a_collIDP : Ptr to collection IDs.
382 * -2 for (at least one) connection error,
383 * LWP process creation code, if it failed,
384 * -1 for other fatal errors.
387 * *** MUST BE THE FIRST ROUTINE CALLED FROM THIS PACKAGE ***
390 * Sets up just about everything.
391 *------------------------------------------------------------------------*/
394 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 */
411 * If we've already been called, snicker at the bozo, gently
412 * remind him of his doubtful heritage, and return success.
414 if (xstat_cm_initflag) {
415 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", rn,
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", rn,
438 if (a_ProbeHandler == (int (*)())0) {
439 fprintf(stderr, "[%s] Null probe handler function argument\n", rn);
442 if (a_numCollections <= 0) {
443 fprintf(stderr, "[%s] Illegal collection count argument: %d\n", rn,
447 if (a_collIDP == NULL) {
448 fprintf(stderr, "[%s] Null collection ID array argument\n", rn);
455 * Record our passed-in info.
457 xstat_cm_debug = (a_flags & XSTAT_CM_INITFLAG_DEBUGGING);
458 xstat_cm_oneShot = (a_flags & XSTAT_CM_INITFLAG_ONE_SHOT);
459 xstat_cm_numServers = a_numServers;
460 xstat_cm_Handler = a_ProbeHandler;
461 xstat_cm_ProbeFreqInSecs = a_ProbeFreqInSecs;
462 xstat_cm_numCollections = a_numCollections;
463 collIDBytes = xstat_cm_numCollections * sizeof(afs_int32);
464 xstat_cm_collIDP = (afs_int32 *) (malloc(collIDBytes));
465 memcpy(xstat_cm_collIDP, a_collIDP, collIDBytes);
466 if (xstat_cm_debug) {
467 printf("[%s] Asking for %d collection(s): ", rn,
468 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
486 xstat_cm_ConnInfo = (struct xstat_cm_ConnectionInfo *)
487 malloc(a_numServers * sizeof(struct xstat_cm_ConnectionInfo));
488 if (xstat_cm_ConnInfo == (struct xstat_cm_ConnectionInfo *)0) {
490 "[%s] Can't allocate %d connection info structs (%d bytes)\n",
492 (a_numServers * sizeof(struct xstat_cm_ConnectionInfo)));
493 return (-1); /*No cleanup needs to be done yet */
497 * Initialize the Rx subsystem, just in case nobody's done it.
500 printf("[%s] Initializing Rx on port 0\n", rn);
501 code = rx_Init(htons(0));
503 fprintf(stderr, "[%s] Fatal error in rx_Init(), error=%d\n", rn,
509 printf("[%s] Rx initialized on port 0\n", rn);
512 * Create a null Rx client security object, to be used by the
515 secobj = rxnull_NewClientSecurityObject();
516 if (secobj == (struct rx_securityClass *)0) {
518 "[%s] Can't create probe LWP client security object.\n", rn);
519 xstat_cm_Cleanup(1); /*Delete already-malloc'ed areas */
523 printf("[%s] Probe LWP client security object created\n", rn);
525 curr_conn = xstat_cm_ConnInfo;
527 for (curr_srv = 0; curr_srv < a_numServers; curr_srv++) {
529 * Copy in the socket info for the current server, resolve its
530 * printable name if possible.
532 if (xstat_cm_debug) {
533 printf("[%s] Copying in the following socket info:\n", rn);
534 printf("[%s] IP addr 0s, port %d\n", rn,
535 afs_inet_ntoa_r((a_socketArray + curr_srv)->sin_addr.s_addr,hoststr),
536 ntohs((a_socketArray + curr_srv)->sin_port));
538 memcpy(&(curr_conn->skt), a_socketArray + curr_srv,
539 sizeof(struct sockaddr_in));
542 hostutil_GetNameByINet(curr_conn->skt.sin_addr.s_addr);
543 if (hostNameFound == NULL) {
545 "[%s] Can't map Internet address %s to a string name\n",
546 rn, afs_inet_ntoa_r(curr_conn->skt.sin_addr.s_addr,hoststr));
547 curr_conn->hostName[0] = '\0';
549 strcpy(curr_conn->hostName, hostNameFound);
551 printf("[%s] Host name for server index %d is %s\n", rn,
552 curr_srv, curr_conn->hostName);
556 * Make an Rx connection to the current server.
560 ("[%s] Connecting to srv idx %d, IP addr %s, port %d, service 1\n",
561 rn, curr_srv, afs_inet_ntoa_r(curr_conn->skt.sin_addr.s_addr,hoststr),
562 ntohs(curr_conn->skt.sin_port));
563 curr_conn->rxconn = rx_NewConnection(curr_conn->skt.sin_addr.s_addr, /*Server addr */
564 curr_conn->skt.sin_port, /*Server port */
565 1, /*AFS service # */
566 secobj, /*Security obj */
568 if (curr_conn->rxconn == (struct rx_connection *)0) {
570 "[%s] Can't create Rx connection to server '%s' (%s)\n",
571 rn, curr_conn->hostName, afs_inet_ntoa_r(curr_conn->skt.sin_addr.s_addr,hoststr));
575 printf("[%s] New connection at 0x%lx\n", rn, curr_conn->rxconn);
578 * Bump the current xstat_cm connection to set up.
585 * Start up the probe LWP.
588 printf("[%s] Creating the probe LWP\n", rn);
589 code = LWP_CreateProcess(xstat_cm_LWP, /*Function to start up */
590 LWP_STACK_SIZE, /*Stack size in bytes */
592 (void *)0, /*Parameters */
593 "xstat_cm Worker", /*Name to use */
594 &probeLWP_ID); /*Returned LWP process ID */
596 fprintf(stderr, "[%s] Can't create xstat_cm LWP! Error is %d\n", rn,
598 xstat_cm_Cleanup(1); /*Delete already-malloc'ed areas */
602 printf("[%s] Probe LWP process structure located at 0x%x\n", rn,
606 * Return the final results.
615 /*------------------------------------------------------------------------
616 * [exported] xstat_cm_ForceProbeNow
619 * Wake up the probe LWP, forcing it to execute a probe immediately.
626 * Error value otherwise.
629 * The module must have been initialized.
633 *------------------------------------------------------------------------*/
636 xstat_cm_ForceProbeNow()
638 static char rn[] = "xstat_cm_ForceProbeNow"; /*Routine name */
641 * There isn't a prayer unless we've been initialized.
643 if (!xstat_cm_initflag) {
644 fprintf(stderr, "[%s] Must call xstat_cm_Init first!\n", rn);
649 * Kick the sucker in the side.
651 IOMGR_Cancel(probeLWP_ID);
654 * We did it, so report the happy news.