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>
29 #define LWP_STACK_SIZE (16 * 1024)
32 * Routines we need that don't have explicit include file definitions.
34 extern char *hostutil_GetNameByINet(); /*Host parsing utility */
39 int xstat_cm_numServers; /*Num connected servers */
40 struct xstat_cm_ConnectionInfo
41 *xstat_cm_ConnInfo; /*Ptr to connection array */
42 int numCollections; /*Number of data collections */
43 struct xstat_cm_ProbeResults xstat_cm_Results; /*Latest probe results */
44 char terminationEvent; /*One-shot termination event */
46 afs_int32 xstat_cmData[AFSCB_MAX_XSTAT_LONGS]; /*Buffer for collected data */
51 static int xstat_cm_ProbeFreqInSecs; /*Probe freq. in seconds */
52 static int xstat_cm_initflag = 0; /*Was init routine called? */
53 static int xstat_cm_debug = 0; /*Debugging output enabled? */
54 static int xstat_cm_oneShot = 0; /*One-shot operation? */
55 static int (*xstat_cm_Handler) (); /*Probe handler routine */
56 static PROCESS probeLWP_ID; /*Probe LWP process ID */
57 static int xstat_cm_numCollections; /*Number of desired collections */
58 static afs_int32 *xstat_cm_collIDP; /*Ptr to collection IDs desired */
61 /*------------------------------------------------------------------------
62 * [private] xstat_cm_CleanupInit
65 * Set up for recovery after an error in initialization (i.e.,
66 * during a call to xstat_cm_Init.
73 * Error value otherwise.
76 * This routine is private to the module.
79 * Zeros out basic data structures.
80 *------------------------------------------------------------------------*/
83 xstat_cm_CleanupInit()
85 xstat_cm_ConnInfo = (struct xstat_cm_ConnectionInfo *)0;
86 xstat_cm_Results.probeNum = 0;
87 xstat_cm_Results.probeTime = 0;
88 xstat_cm_Results.connP = (struct xstat_cm_ConnectionInfo *)0;
89 xstat_cm_Results.collectionNumber = 0;
90 xstat_cm_Results.data.AFSCB_CollData_len = AFSCB_MAX_XSTAT_LONGS;
91 xstat_cm_Results.data.AFSCB_CollData_val = (afs_int32 *) xstat_cmData;
92 xstat_cm_Results.probeOK = 0;
98 /*------------------------------------------------------------------------
99 * [exported] xstat_cm_Cleanup
102 * Clean up our memory and connection state.
105 * int a_releaseMem : Should we free up malloc'ed areas?
108 * 0 on total success,
109 * -1 if the module was never initialized, or there was a problem
110 * with the xstat_cm connection array.
113 * xstat_cm_numServers should be properly set. We don't do anything
114 * unless xstat_cm_Init() has already been called.
117 * Shuts down Rx connections gracefully, frees allocated space
119 *------------------------------------------------------------------------*/
122 xstat_cm_Cleanup(int a_releaseMem)
124 static char rn[] = "xstat_cm_Cleanup"; /*Routine name */
125 int code; /*Return code */
126 int conn_idx; /*Current connection index */
127 struct xstat_cm_ConnectionInfo *curr_conn; /*Ptr to xstat_cm connection */
130 * Assume the best, but check the worst.
132 if (!xstat_cm_initflag) {
133 fprintf(stderr, "[%s] Refused; module not initialized\n", rn);
139 * Take care of all Rx connections first. Check to see that the
140 * server count is a legal value.
142 if (xstat_cm_numServers <= 0) {
144 "[%s] Illegal number of servers (xstat_cm_numServers = %d)\n",
145 rn, xstat_cm_numServers);
148 if (xstat_cm_ConnInfo != (struct xstat_cm_ConnectionInfo *)0) {
150 * The xstat_cm connection structure array exists. Go through
151 * it and close up any Rx connections it holds.
153 curr_conn = xstat_cm_ConnInfo;
154 for (conn_idx = 0; conn_idx < xstat_cm_numServers; conn_idx++) {
155 if (curr_conn->rxconn != (struct rx_connection *)0) {
156 rx_DestroyConnection(curr_conn->rxconn);
157 curr_conn->rxconn = (struct rx_connection *)0;
160 } /*for each xstat_cm connection */
161 } /*xstat_cm connection structure exists */
162 } /*Legal number of servers */
165 * If asked to, release the space we've allocated.
168 if (xstat_cm_ConnInfo != (struct xstat_cm_ConnectionInfo *)0)
169 free(xstat_cm_ConnInfo);
173 * Return the news, whatever it is.
179 /*------------------------------------------------------------------------
180 * [private] xstat_cm_LWP
183 * This LWP iterates over the server connections and gathers up
184 * the desired statistics from each one on a regular basis, for
185 * all known data collections. The associated handler function
186 * is called each time a new data collection is received.
195 * Started by xstat_cm_Init(), uses global structures and the
196 * global private xstat_cm_oneShot variable.
200 *------------------------------------------------------------------------*/
204 static char rn[] = "xstat_cm_LWP"; /*Routine name */
205 register afs_int32 code; /*Results of calls */
206 int oneShotCode; /*Result of one-shot signal */
207 struct timeval tv; /*Time structure */
208 int conn_idx; /*Connection index */
209 struct xstat_cm_ConnectionInfo *curr_conn; /*Current connection */
210 afs_int32 srvVersionNumber; /*Xstat version # */
211 afs_int32 clientVersionNumber; /*Client xstat version */
212 afs_int32 numColls; /*Number of collections to get */
213 afs_int32 *currCollIDP; /*Curr collection ID desired */
216 * Set up some numbers we'll need.
218 clientVersionNumber = AFSCB_XSTAT_VERSION;
220 while (1) { /*Service loop */
222 * Iterate through the server connections, gathering data.
223 * Don't forget to bump the probe count and zero the statistics
224 * areas before calling the servers.
227 printf("[%s] Waking up, getting data from %d server(s)\n", rn,
228 xstat_cm_numServers);
229 curr_conn = xstat_cm_ConnInfo;
230 xstat_cm_Results.probeNum++;
232 for (conn_idx = 0; conn_idx < xstat_cm_numServers; conn_idx++) {
234 * Grab the statistics for the current Cache Manager, if the
235 * connection is valid.
238 printf("[%s] Getting collections from Cache Manager '%s'\n",
239 rn, curr_conn->hostName);
240 if (curr_conn->rxconn != (struct rx_connection *)0) {
242 printf("[%s] Connection OK, calling RXAFSCB_GetXStats\n",
246 * Probe the given CM for each desired collection.
248 currCollIDP = xstat_cm_collIDP;
249 for (numColls = 0; numColls < xstat_cm_numCollections;
250 numColls++, currCollIDP++) {
252 * Initialize the per-probe values.
255 printf("[%s] Asking for data collection %d\n", rn,
257 xstat_cm_Results.collectionNumber = *currCollIDP;
258 xstat_cm_Results.data.AFSCB_CollData_len =
259 AFSCB_MAX_XSTAT_LONGS;
260 memset(xstat_cm_Results.data.AFSCB_CollData_val, 0,
261 AFSCB_MAX_XSTAT_LONGS * 4);
263 xstat_cm_Results.connP = curr_conn;
265 if (xstat_cm_debug) {
267 ("%s: Calling RXAFSCB_GetXStats, conn=0x%x, clientVersionNumber=%d, collectionNumber=%d, srvVersionNumberP=0x%x, timeP=0x%x, dataP=0x%x\n",
268 rn, curr_conn->rxconn, clientVersionNumber,
269 *currCollIDP, &srvVersionNumber,
270 &(xstat_cm_Results.probeTime),
271 &(xstat_cm_Results.data));
272 printf("%s: [bufflen=%d, buffer at 0x%x]\n", rn,
273 xstat_cm_Results.data.AFSCB_CollData_len,
274 xstat_cm_Results.data.AFSCB_CollData_val);
277 xstat_cm_Results.probeOK =
278 RXAFSCB_GetXStats(curr_conn->rxconn,
279 clientVersionNumber, *currCollIDP,
281 &(xstat_cm_Results.probeTime),
282 &(xstat_cm_Results.data));
285 * Now that we (may) have the data for this connection,
286 * call the associated handler function. The handler
287 * does not take any explicit parameters, but rather
288 * gets to the goodies via some of the objects exported
292 printf("[%s] Calling handler routine.\n", rn);
293 code = xstat_cm_Handler();
296 "[%s] Handler routine got error code %d\n",
298 } /*For each collection */
301 /*Valid Rx connection */
303 * Advance the xstat_cm connection pointer.
307 } /*For each xstat_cm connection */
310 * All (valid) connections have been probed. Fall asleep for the
311 * prescribed number of seconds, unless we're a one-shot. In
312 * that case, we need to signal our caller that we're done.
315 printf("[%s] Polling complete for probe round %d.\n", rn,
316 xstat_cm_Results.probeNum);
318 if (xstat_cm_oneShot) {
320 * One-shot execution desired. Signal our main procedure
321 * that we've finished our collection round.
324 printf("[%s] Signalling main process at 0x%x\n", rn,
326 oneShotCode = LWP_SignalProcess(&terminationEvent);
328 fprintf(stderr, "[%s] Error %d from LWP_SignalProcess()", rn,
330 break; /*from the perpetual while loop */
331 } /*One-shot execution */
334 * Continuous execution desired. Sleep for the required
337 tv.tv_sec = xstat_cm_ProbeFreqInSecs;
340 printf("[%s] Falling asleep for %d seconds\n", rn,
341 xstat_cm_ProbeFreqInSecs);
342 code = IOMGR_Select(0, /*Num fids */
343 0, /*Descs ready for reading */
344 0, /*Descs ready for writing */
345 0, /*Descs w/exceptional conditions */
346 &tv); /*Ptr to timeout structure */
348 fprintf(stderr, "[%s] IOMGR_Select returned code %d\n", rn,
350 } /*Continuous execution */
355 /*------------------------------------------------------------------------
356 * [exported] xstat_cm_Init
359 * Initialize the xstat_cm module: set up Rx connections to the
360 * given set of Cache Managers, start up the probe LWP, and
361 * associate the routine to be called when a probe completes.
362 * Also, let it know which collections you're interested in.
365 * int a_numServers : Num. servers to connect to.
366 * struct sockaddr_in *a_socketArray : Array of server sockets.
367 * int a_ProbeFreqInSecs : Probe frequency in seconds.
368 * int (*a_ProbeHandler)() : Ptr to probe handler fcn.
369 * int a_flags; : Various flags.
370 * int a_numCollections : Number of collections desired.
371 * afs_int32 *a_collIDP : Ptr to collection IDs.
375 * -2 for (at least one) connection error,
376 * LWP process creation code, if it failed,
377 * -1 for other fatal errors.
380 * *** MUST BE THE FIRST ROUTINE CALLED FROM THIS PACKAGE ***
383 * Sets up just about everything.
384 *------------------------------------------------------------------------*/
387 xstat_cm_Init(int a_numServers, struct sockaddr_in *a_socketArray,
388 int a_ProbeFreqInSecs, int (*a_ProbeHandler) (), int a_flags,
389 int a_numCollections, afs_int32 * a_collIDP)
392 static char rn[] = "xstat_cm_Init"; /*Routine name */
393 register afs_int32 code; /*Return value */
394 struct rx_securityClass *secobj; /*Client security object */
395 int arg_errfound; /*Argument error found? */
396 int curr_srv; /*Current server idx */
397 struct xstat_cm_ConnectionInfo *curr_conn; /*Ptr to current conn */
398 char *hostNameFound; /*Ptr to returned host name */
399 int conn_err; /*Connection error? */
400 int collIDBytes; /*Num bytes in coll ID array */
404 * If we've already been called, snicker at the bozo, gently
405 * remind him of his doubtful heritage, and return success.
407 if (xstat_cm_initflag) {
408 fprintf(stderr, "[%s] Called multiple times!\n", rn);
411 xstat_cm_initflag = 1;
414 * Check the parameters for bogosities.
417 if (a_numServers <= 0) {
418 fprintf(stderr, "[%s] Illegal number of servers: %d\n", rn,
422 if (a_socketArray == (struct sockaddr_in *)0) {
423 fprintf(stderr, "[%s] Null server socket array argument\n", rn);
426 if (a_ProbeFreqInSecs <= 0) {
427 fprintf(stderr, "[%s] Illegal probe frequency: %d\n", rn,
431 if (a_ProbeHandler == (int (*)())0) {
432 fprintf(stderr, "[%s] Null probe handler function argument\n", rn);
435 if (a_numCollections <= 0) {
436 fprintf(stderr, "[%s] Illegal collection count argument: %d\n", rn,
440 if (a_collIDP == NULL) {
441 fprintf(stderr, "[%s] Null collection ID array argument\n", rn);
448 * Record our passed-in info.
450 xstat_cm_debug = (a_flags & XSTAT_CM_INITFLAG_DEBUGGING);
451 xstat_cm_oneShot = (a_flags & XSTAT_CM_INITFLAG_ONE_SHOT);
452 xstat_cm_numServers = a_numServers;
453 xstat_cm_Handler = a_ProbeHandler;
454 xstat_cm_ProbeFreqInSecs = a_ProbeFreqInSecs;
455 xstat_cm_numCollections = a_numCollections;
456 collIDBytes = xstat_cm_numCollections * sizeof(afs_int32);
457 xstat_cm_collIDP = (afs_int32 *) (malloc(collIDBytes));
458 memcpy(xstat_cm_collIDP, a_collIDP, collIDBytes);
459 if (xstat_cm_debug) {
460 printf("[%s] Asking for %d collection(s): ", rn,
461 xstat_cm_numCollections);
462 for (curr_srv = 0; curr_srv < xstat_cm_numCollections; curr_srv++)
463 printf("%d ", *(xstat_cm_collIDP + curr_srv));
468 * Get ready in case we have to do a cleanup - basically, zero
471 code = xstat_cm_CleanupInit();
476 * Allocate the necessary data structures and initialize everything
479 xstat_cm_ConnInfo = (struct xstat_cm_ConnectionInfo *)
480 malloc(a_numServers * sizeof(struct xstat_cm_ConnectionInfo));
481 if (xstat_cm_ConnInfo == (struct xstat_cm_ConnectionInfo *)0) {
483 "[%s] Can't allocate %d connection info structs (%d bytes)\n",
485 (a_numServers * sizeof(struct xstat_cm_ConnectionInfo)));
486 return (-1); /*No cleanup needs to be done yet */
490 * Initialize the Rx subsystem, just in case nobody's done it.
493 printf("[%s] Initializing Rx on port 0\n", rn);
494 code = rx_Init(htons(0));
496 fprintf(stderr, "[%s] Fatal error in rx_Init(), error=%d\n", rn,
502 printf("[%s] Rx initialized on port 0\n", rn);
505 * Create a null Rx client security object, to be used by the
508 secobj = rxnull_NewClientSecurityObject();
509 if (secobj == (struct rx_securityClass *)0) {
511 "[%s] Can't create probe LWP client security object.\n", rn);
512 xstat_cm_Cleanup(1); /*Delete already-malloc'ed areas */
516 printf("[%s] Probe LWP client security object created\n", rn);
518 curr_conn = xstat_cm_ConnInfo;
520 for (curr_srv = 0; curr_srv < a_numServers; curr_srv++) {
522 * Copy in the socket info for the current server, resolve its
523 * printable name if possible.
525 if (xstat_cm_debug) {
526 printf("[%s] Copying in the following socket info:\n", rn);
527 printf("[%s] IP addr 0s, port %d\n", rn,
528 afs_inet_ntoa_r((a_socketArray + curr_srv)->sin_addr.s_addr,hoststr),
529 ntohs((a_socketArray + curr_srv)->sin_port));
531 memcpy(&(curr_conn->skt), a_socketArray + curr_srv,
532 sizeof(struct sockaddr_in));
535 hostutil_GetNameByINet(curr_conn->skt.sin_addr.s_addr);
536 if (hostNameFound == NULL) {
538 "[%s] Can't map Internet address %s to a string name\n",
539 rn, afs_inet_ntoa_r(curr_conn->skt.sin_addr.s_addr,hoststr));
540 curr_conn->hostName[0] = '\0';
542 strcpy(curr_conn->hostName, hostNameFound);
544 printf("[%s] Host name for server index %d is %s\n", rn,
545 curr_srv, curr_conn->hostName);
549 * Make an Rx connection to the current server.
553 ("[%s] Connecting to srv idx %d, IP addr %s, port %d, service 1\n",
554 rn, curr_srv, afs_inet_ntoa_r(curr_conn->skt.sin_addr.s_addr,hoststr),
555 ntohs(curr_conn->skt.sin_port));
556 curr_conn->rxconn = rx_NewConnection(curr_conn->skt.sin_addr.s_addr, /*Server addr */
557 curr_conn->skt.sin_port, /*Server port */
558 1, /*AFS service # */
559 secobj, /*Security obj */
561 if (curr_conn->rxconn == (struct rx_connection *)0) {
563 "[%s] Can't create Rx connection to server '%s' (%s)\n",
564 rn, curr_conn->hostName, afs_inet_ntoa_r(curr_conn->skt.sin_addr.s_addr,hoststr));
568 printf("[%s] New connection at 0x%lx\n", rn, curr_conn->rxconn);
571 * Bump the current xstat_cm connection to set up.
578 * Start up the probe LWP.
581 printf("[%s] Creating the probe LWP\n", rn);
582 code = LWP_CreateProcess(xstat_cm_LWP, /*Function to start up */
583 LWP_STACK_SIZE, /*Stack size in bytes */
585 (void *)0, /*Parameters */
586 "xstat_cm Worker", /*Name to use */
587 &probeLWP_ID); /*Returned LWP process ID */
589 fprintf(stderr, "[%s] Can't create xstat_cm LWP! Error is %d\n", rn,
591 xstat_cm_Cleanup(1); /*Delete already-malloc'ed areas */
595 printf("[%s] Probe LWP process structure located at 0x%x\n", rn,
599 * Return the final results.
608 /*------------------------------------------------------------------------
609 * [exported] xstat_cm_ForceProbeNow
612 * Wake up the probe LWP, forcing it to execute a probe immediately.
619 * Error value otherwise.
622 * The module must have been initialized.
626 *------------------------------------------------------------------------*/
629 xstat_cm_ForceProbeNow()
631 static char rn[] = "xstat_cm_ForceProbeNow"; /*Routine name */
634 * There isn't a prayer unless we've been initialized.
636 if (!xstat_cm_initflag) {
637 fprintf(stderr, "[%s] Must call xstat_cm_Init first!\n", rn);
642 * Kick the sucker in the side.
644 IOMGR_Cancel(probeLWP_ID);
647 * We did it, so report the happy news.