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 File Server extended
13 * statistics facility.
15 *------------------------------------------------------------------------*/
17 #include <afsconfig.h>
18 #include <afs/param.h>
21 #include "xstat_fs.h" /*Interface for this module */
22 #include <lwp.h> /*Lightweight process package */
24 #include <afs/afsutil.h>
25 #include <afs/afscbint.h>
28 #define LWP_STACK_SIZE (16 * 1024)
30 /* This should really be in a header file */
31 extern int RXAFSCB_ExecuteRequest(struct rx_call *z_call);
36 int xstat_fs_numServers; /*Num connected servers */
37 struct xstat_fs_ConnectionInfo
38 *xstat_fs_ConnInfo; /*Ptr to connection array */
39 int numCollections; /*Number of data collections */
40 struct xstat_fs_ProbeResults xstat_fs_Results; /*Latest probe results */
41 char terminationEvent; /*One-shot termination event */
43 afs_int32 xstat_fsData[AFS_MAX_XSTAT_LONGS]; /*Buffer for collected data */
48 static int xstat_fs_ProbeFreqInSecs; /*Probe freq. in seconds */
49 static int xstat_fs_initflag = 0; /*Was init routine called? */
50 static int xstat_fs_debug = 0; /*Debugging output enabled? */
51 static int xstat_fs_oneShot = 0; /*One-shot operation? */
52 static int (*xstat_fs_Handler) (void); /*Probe handler routine */
53 static PROCESS probeLWP_ID; /*Probe LWP process ID */
54 static int xstat_fs_numCollections; /*Number of desired collections */
55 static afs_int32 *xstat_fs_collIDP; /*Ptr to collection IDs desired */
58 * We have to pass a port to Rx to start up our callback listener
59 * service, but 7001 is already taken up by the Cache Manager. So,
62 #define XSTAT_FS_CBPORT 7101
65 /*------------------------------------------------------------------------
66 * [private] xstat_fs_CleanupInit
69 * Set up for recovery after an error in initialization (i.e.,
70 * during a call to xstat_fs_Init.
77 * Error value otherwise.
80 * This routine is private to the module.
83 * Zeros out basic data structures.
84 *------------------------------------------------------------------------*/
87 xstat_fs_CleanupInit(void)
89 afs_int32 code; /*Return code from callback stubs */
90 struct rx_call *rxcall; /*Bogus param */
91 AFSCBFids *Fids_Array; /*Bogus param */
92 AFSCBs *CallBack_Array; /*Bogus param */
94 xstat_fs_ConnInfo = (struct xstat_fs_ConnectionInfo *)0;
95 xstat_fs_Results.probeNum = 0;
96 xstat_fs_Results.probeTime = 0;
97 xstat_fs_Results.connP = (struct xstat_fs_ConnectionInfo *)0;
98 xstat_fs_Results.collectionNumber = 0;
99 xstat_fs_Results.data.AFS_CollData_len = AFS_MAX_XSTAT_LONGS;
100 xstat_fs_Results.data.AFS_CollData_val = (afs_int32 *) xstat_fsData;
101 xstat_fs_Results.probeOK = 0;
103 rxcall = (struct rx_call *)0;
104 Fids_Array = (AFSCBFids *) 0;
105 CallBack_Array = (AFSCBs *) 0;
108 * Call each of the callback routines our module provides (in
109 * xstat_fs_callback.c) to make sure they're all there.
111 code = SRXAFSCB_CallBack(rxcall, Fids_Array, CallBack_Array);
114 code = SRXAFSCB_InitCallBackState3(rxcall, (afsUUID *) 0);
117 code = SRXAFSCB_Probe(rxcall);
122 /*------------------------------------------------------------------------
123 * [exported] xstat_fs_Cleanup
126 * Clean up our memory and connection state.
129 * int a_releaseMem : Should we free up malloc'ed areas?
132 * 0 on total success,
133 * -1 if the module was never initialized, or there was a problem
134 * with the xstat_fs connection array.
137 * xstat_fs_numServers should be properly set. We don't do anything
138 * unless xstat_fs_Init() has already been called.
141 * Shuts down Rx connections gracefully, frees allocated space
143 *------------------------------------------------------------------------*/
146 xstat_fs_Cleanup(int a_releaseMem)
148 static char rn[] = "xstat_fs_Cleanup"; /*Routine name */
149 int code; /*Return code */
150 int conn_idx; /*Current connection index */
151 struct xstat_fs_ConnectionInfo *curr_conn; /*Ptr to xstat_fs connection */
154 * Assume the best, but check the worst.
156 if (!xstat_fs_initflag) {
157 fprintf(stderr, "[%s] Refused; module not initialized\n", rn);
163 * Take care of all Rx connections first. Check to see that the
164 * server count is a legal value.
166 if (xstat_fs_numServers <= 0) {
168 "[%s] Illegal number of servers (xstat_fs_numServers = %d)\n",
169 rn, xstat_fs_numServers);
172 if (xstat_fs_ConnInfo != (struct xstat_fs_ConnectionInfo *)0) {
174 * The xstat_fs connection structure array exists. Go through
175 * it and close up any Rx connections it holds.
177 curr_conn = xstat_fs_ConnInfo;
178 for (conn_idx = 0; conn_idx < xstat_fs_numServers; conn_idx++) {
179 if (curr_conn->rxconn != (struct rx_connection *)0) {
180 rx_DestroyConnection(curr_conn->rxconn);
181 curr_conn->rxconn = (struct rx_connection *)0;
184 } /*for each xstat_fs connection */
185 } /*xstat_fs connection structure exists */
186 } /*Legal number of servers */
189 * If asked to, release the space we've allocated.
192 if (xstat_fs_ConnInfo != (struct xstat_fs_ConnectionInfo *)0)
193 free(xstat_fs_ConnInfo);
197 * Return the news, whatever it is.
203 /*------------------------------------------------------------------------
204 * [private] xstat_fs_LWP
207 * This LWP iterates over the server connections and gathers up
208 * the desired statistics from each one on a regular basis. When
209 * the sweep is done, the associated handler function is called
210 * to process the new data.
219 * Started by xstat_fs_Init(), uses global structures and the
220 * global private xstat_fs_oneShot variable.
223 * Nothing interesting.
224 *------------------------------------------------------------------------*/
227 xstat_fs_LWP(void *unused)
229 static char rn[] = "xstat_fs_LWP"; /*Routine name */
230 register afs_int32 code; /*Results of calls */
231 int oneShotCode; /*Result of one-shot signal */
232 struct timeval tv; /*Time structure */
233 int conn_idx; /*Connection index */
234 struct xstat_fs_ConnectionInfo *curr_conn; /*Current connection */
235 afs_int32 srvVersionNumber; /*Xstat version # */
236 afs_int32 clientVersionNumber; /*Client xstat version */
237 afs_int32 numColls; /*Number of collections to get */
238 afs_int32 *currCollIDP; /*Curr collection ID desired */
241 * Set up some numbers we'll need.
243 clientVersionNumber = AFS_XSTAT_VERSION;
245 while (1) { /*Service loop */
247 * Iterate through the server connections, gathering data.
248 * Don't forget to bump the probe count and zero the statistics
249 * areas before calling the servers.
252 printf("[%s] Waking up, getting data from %d server(s)\n", rn,
253 xstat_fs_numServers);
254 curr_conn = xstat_fs_ConnInfo;
255 xstat_fs_Results.probeNum++;
257 for (conn_idx = 0; conn_idx < xstat_fs_numServers; conn_idx++) {
259 * Grab the statistics for the current File Server, if the
260 * connection is valid.
263 printf("[%s] Getting collections from File Server '%s'\n", rn,
264 curr_conn->hostName);
265 if (curr_conn->rxconn != (struct rx_connection *)0) {
267 printf("[%s] Connection OK, calling RXAFS_GetXStats\n",
270 currCollIDP = xstat_fs_collIDP;
271 for (numColls = 0; numColls < xstat_fs_numCollections;
272 numColls++, currCollIDP++) {
274 * Initialize the per-probe values.
277 printf("[%s] Asking for data collection %d\n", rn,
279 xstat_fs_Results.collectionNumber = *currCollIDP;
280 xstat_fs_Results.data.AFS_CollData_len =
282 memset(xstat_fs_Results.data.AFS_CollData_val, 0,
283 AFS_MAX_XSTAT_LONGS * 4);
285 xstat_fs_Results.connP = curr_conn;
287 if (xstat_fs_debug) {
289 ("%s: Calling RXAFS_GetXStats, conn=%" AFS_PTR_FMT ", clientVersionNumber=%d, collectionNumber=%d, srvVersionNumberP=%" AFS_PTR_FMT ", timeP=%" AFS_PTR_FMT ", dataP=%" AFS_PTR_FMT "\n",
290 rn, curr_conn->rxconn, clientVersionNumber,
291 *currCollIDP, &srvVersionNumber,
292 &(xstat_fs_Results.probeTime),
293 &(xstat_fs_Results.data));
294 printf("%s: [bufflen=%d, buffer at %" AFS_PTR_FMT "]\n", rn,
295 xstat_fs_Results.data.AFS_CollData_len,
296 xstat_fs_Results.data.AFS_CollData_val);
299 xstat_fs_Results.probeOK =
300 RXAFS_GetXStats(curr_conn->rxconn,
301 clientVersionNumber, *currCollIDP,
303 &(xstat_fs_Results.probeTime),
304 &(xstat_fs_Results.data));
307 * Now that we (may) have the data for this connection,
308 * call the associated handler function. The handler does
309 * not take any explicit parameters, but rather gets to the
310 * goodies via some of the objects exported by this module.
313 printf("[%s] Calling handler routine.\n", rn);
314 code = xstat_fs_Handler();
317 "[%s] Handler returned error code %d\n", rn,
320 } /*For each collection */
323 /*Valid Rx connection */
325 * Advance the xstat_fs connection pointer.
329 } /*For each xstat_fs connection */
332 * All (valid) connections have been probed. Fall asleep for the
333 * prescribed number of seconds, unless we're a one-shot. In
334 * that case, we need to signal our caller that we're done.
337 printf("[%s] Polling complete for probe round %d.\n", rn,
338 xstat_fs_Results.probeNum);
340 if (xstat_fs_oneShot) {
342 * One-shot execution desired. Signal our main procedure
343 * that we've finished our collection round.
346 printf("[%s] Signalling main process at %" AFS_PTR_FMT "\n", rn,
348 oneShotCode = LWP_SignalProcess(&terminationEvent);
350 fprintf(stderr, "[%s] Error %d from LWP_SignalProcess()", rn,
352 break; /*from the perpetual while loop */
353 } /*One-shot execution */
356 * Continuous execution desired. Sleep for the required
359 tv.tv_sec = xstat_fs_ProbeFreqInSecs;
362 printf("[%s] Falling asleep for %d seconds\n", rn,
363 xstat_fs_ProbeFreqInSecs);
364 code = IOMGR_Select(0, /*Num fids */
365 0, /*Descs ready for reading */
366 0, /*Descs ready for writing */
367 0, /*Descs w/exceptional conditions */
368 &tv); /*Ptr to timeout structure */
370 fprintf(stderr, "[%s] IOMGR_Select returned code %d\n", rn,
372 } /*Continuous execution */
377 /*------------------------------------------------------------------------
378 * [exported] xstat_fs_Init
381 * Initialize the xstat_fs module: set up Rx connections to the
382 * given set of File Servers, start up the probe and callback LWPs,
383 * and associate the routine to be called when a probe completes.
384 * Also, let it know which collections you're interested in.
387 * int a_numServers : Num. servers to connect to.
388 * struct sockaddr_in *a_socketArray : Array of server sockets.
389 * int a_ProbeFreqInSecs : Probe frequency in seconds.
390 * int (*a_ProbeHandler)() : Ptr to probe handler fcn.
391 * int a_flags : Various flags.
392 * int a_numCollections : Number of collections desired.
393 * afs_int32 *a_collIDP : Ptr to collection IDs.
397 * -2 for (at least one) connection error,
398 * LWP process creation code, if it failed,
399 * -1 for other fatal errors.
402 * *** MUST BE THE FIRST ROUTINE CALLED FROM THIS PACKAGE ***
403 * Also, the server security object CBsecobj MUST be a static,
404 * since it has to stick around after this routine exits.
407 * Sets up just about everything.
408 *------------------------------------------------------------------------*/
411 xstat_fs_Init(int a_numServers, struct sockaddr_in *a_socketArray,
412 int a_ProbeFreqInSecs, int (*a_ProbeHandler) (void), int a_flags,
413 int a_numCollections, afs_int32 * a_collIDP)
415 static char rn[] = "xstat_fs_Init"; /*Routine name */
416 register afs_int32 code; /*Return value */
417 static struct rx_securityClass *CBsecobj; /*Callback security object */
418 struct rx_securityClass *secobj; /*Client security object */
419 struct rx_service *rxsrv_afsserver; /*Server for AFS */
420 int arg_errfound; /*Argument error found? */
421 int curr_srv; /*Current server idx */
422 struct xstat_fs_ConnectionInfo *curr_conn; /*Ptr to current conn */
423 char *hostNameFound; /*Ptr to returned host name */
424 int conn_err; /*Connection error? */
425 int PortToUse; /*Callback port to use */
426 int collIDBytes; /*Num bytes in coll ID array */
430 * If we've already been called, snicker at the bozo, gently
431 * remind him of his doubtful heritage, and return success.
433 if (xstat_fs_initflag) {
434 fprintf(stderr, "[%s] Called multiple times!\n", rn);
437 xstat_fs_initflag = 1;
440 * Check the parameters for bogosities.
443 if (a_numServers <= 0) {
444 fprintf(stderr, "[%s] Illegal number of servers: %d\n", rn,
448 if (a_socketArray == (struct sockaddr_in *)0) {
449 fprintf(stderr, "[%s] Null server socket array argument\n", rn);
452 if (a_ProbeFreqInSecs <= 0) {
453 fprintf(stderr, "[%s] Illegal probe frequency: %d\n", rn,
457 if (a_ProbeHandler == (int (*)())0) {
458 fprintf(stderr, "[%s] Null probe handler function argument\n", rn);
461 if (a_numCollections <= 0) {
462 fprintf(stderr, "[%s] Illegal collection count argument: %d\n", rn,
466 if (a_collIDP == NULL) {
467 fprintf(stderr, "[%s] Null collection ID array argument\n", rn);
474 * Record our passed-in info.
476 xstat_fs_debug = (a_flags & XSTAT_FS_INITFLAG_DEBUGGING);
477 xstat_fs_oneShot = (a_flags & XSTAT_FS_INITFLAG_ONE_SHOT);
478 xstat_fs_numServers = a_numServers;
479 xstat_fs_Handler = a_ProbeHandler;
480 xstat_fs_ProbeFreqInSecs = a_ProbeFreqInSecs;
481 xstat_fs_numCollections = a_numCollections;
482 collIDBytes = xstat_fs_numCollections * sizeof(afs_int32);
483 xstat_fs_collIDP = (afs_int32 *) (malloc(collIDBytes));
484 memcpy(xstat_fs_collIDP, a_collIDP, collIDBytes);
485 if (xstat_fs_debug) {
486 printf("[%s] Asking for %d collection(s): ", rn,
487 xstat_fs_numCollections);
488 for (curr_srv = 0; curr_srv < xstat_fs_numCollections; curr_srv++)
489 printf("%d ", *(xstat_fs_collIDP + curr_srv));
494 * Get ready in case we have to do a cleanup - basically, zero
497 code = xstat_fs_CleanupInit();
502 * Allocate the necessary data structures and initialize everything
505 xstat_fs_ConnInfo = (struct xstat_fs_ConnectionInfo *)
506 malloc(a_numServers * sizeof(struct xstat_fs_ConnectionInfo));
507 if (xstat_fs_ConnInfo == (struct xstat_fs_ConnectionInfo *)0) {
509 "[%s] Can't allocate %d connection info structs (%lu bytes)\n",
511 (a_numServers * sizeof(struct xstat_fs_ConnectionInfo)));
512 return (-1); /*No cleanup needs to be done yet */
516 * Initialize the Rx subsystem, just in case nobody's done it.
519 printf("[%s] Initializing Rx\n", rn);
520 PortToUse = XSTAT_FS_CBPORT;
523 code = rx_Init(htons(PortToUse));
525 if (code == RX_ADDRINUSE) {
528 "[%s] Callback port %d in use, advancing\n", rn,
532 fprintf(stderr, "[%s] Fatal error in rx_Init()\n", rn);
538 printf("[%s] Rx initialized on port %d\n", rn, PortToUse);
541 * Create a null Rx server security object, to be used by the
544 CBsecobj = (struct rx_securityClass *)
545 rxnull_NewServerSecurityObject();
546 if (CBsecobj == (struct rx_securityClass *)0) {
548 "[%s] Can't create callback listener's security object.\n",
550 xstat_fs_Cleanup(1); /*Delete already-malloc'ed areas */
554 printf("[%s] Callback server security object created\n", rn);
557 * Create a null Rx client security object, to be used by the
560 secobj = rxnull_NewClientSecurityObject();
561 if (secobj == (struct rx_securityClass *)0) {
563 "[%s] Can't create probe LWP client security object.\n", rn);
564 xstat_fs_Cleanup(1); /*Delete already-malloc'ed areas */
568 printf("[%s] Probe LWP client security object created\n", rn);
570 curr_conn = xstat_fs_ConnInfo;
572 for (curr_srv = 0; curr_srv < a_numServers; curr_srv++) {
574 * Copy in the socket info for the current server, resolve its
575 * printable name if possible.
577 if (xstat_fs_debug) {
579 printf("[%s] Copying in the following socket info:\n", rn);
580 printf("[%s] IP addr %s, port %d\n", rn,
581 afs_inet_ntoa_r((a_socketArray + curr_srv)->sin_addr.s_addr,hoststr),
582 ntohs((a_socketArray + curr_srv)->sin_port));
584 memcpy(&(curr_conn->skt), a_socketArray + curr_srv,
585 sizeof(struct sockaddr_in));
588 hostutil_GetNameByINet(curr_conn->skt.sin_addr.s_addr);
589 if (hostNameFound == NULL) {
591 "[%s] Can't map Internet address %s to a string name\n",
592 rn, afs_inet_ntoa_r(curr_conn->skt.sin_addr.s_addr,hoststr));
593 curr_conn->hostName[0] = '\0';
595 strcpy(curr_conn->hostName, hostNameFound);
597 printf("[%s] Host name for server index %d is %s\n", rn,
598 curr_srv, curr_conn->hostName);
602 * Make an Rx connection to the current server.
606 ("[%s] Connecting to srv idx %d, IP addr %s, port %d, service 1\n",
607 rn, curr_srv, afs_inet_ntoa_r(curr_conn->skt.sin_addr.s_addr,hoststr),
608 ntohs(curr_conn->skt.sin_port));
610 curr_conn->rxconn = rx_NewConnection(curr_conn->skt.sin_addr.s_addr, /*Server addr */
611 curr_conn->skt.sin_port, /*Server port */
612 1, /*AFS service # */
613 secobj, /*Security obj */
615 if (curr_conn->rxconn == (struct rx_connection *)0) {
617 "[%s] Can't create Rx connection to server '%s' (%s)\n",
618 rn, curr_conn->hostName, afs_inet_ntoa_r(curr_conn->skt.sin_addr.s_addr,hoststr));
622 printf("[%s] New connection at %" AFS_PTR_FMT "\n", rn, curr_conn->rxconn);
625 * Bump the current xstat_fs connection to set up.
632 * Create the AFS callback service (listener).
635 printf("[%s] Creating AFS callback listener\n", rn);
636 rxsrv_afsserver = rx_NewService(0, /*Use default port */
638 "afs", /*Service name */
639 &CBsecobj, /*Ptr to security object(s) */
640 1, /*# of security objects */
641 RXAFSCB_ExecuteRequest); /*Dispatcher */
642 if (rxsrv_afsserver == (struct rx_service *)0) {
643 fprintf(stderr, "[%s] Can't create callback Rx service/listener\n",
645 xstat_fs_Cleanup(1); /*Delete already-malloc'ed areas */
649 printf("[%s] Callback listener created\n", rn);
652 * Start up the AFS callback service.
655 printf("[%s] Starting up callback listener.\n", rn);
656 rx_StartServer(0); /*Don't donate yourself to LWP pool */
659 * Start up the probe LWP.
662 printf("[%s] Creating the probe LWP\n", rn);
663 code = LWP_CreateProcess(xstat_fs_LWP, /*Function to start up */
664 LWP_STACK_SIZE, /*Stack size in bytes */
666 (void *)0, /*Parameters */
667 "xstat_fs Worker", /*Name to use */
668 &probeLWP_ID); /*Returned LWP process ID */
670 fprintf(stderr, "[%s] Can't create xstat_fs LWP! Error is %d\n", rn,
672 xstat_fs_Cleanup(1); /*Delete already-malloc'ed areas */
676 printf("[%s] Probe LWP process structure located at %" AFS_PTR_FMT "\n", rn,
680 * Return the final results.
689 /*------------------------------------------------------------------------
690 * [exported] xstat_fs_ForceProbeNow
693 * Wake up the probe LWP, forcing it to execute a probe immediately.
700 * Error value otherwise.
703 * The module must have been initialized.
707 *------------------------------------------------------------------------*/
710 xstat_fs_ForceProbeNow(void)
712 static char rn[] = "xstat_fs_ForceProbeNow"; /*Routine name */
715 * There isn't a prayer unless we've been initialized.
717 if (!xstat_fs_initflag) {
718 fprintf(stderr, "[%s] Must call xstat_fs_Init first!\n", rn);
723 * Kick the sucker in the side.
725 IOMGR_Cancel(probeLWP_ID);
728 * We did it, so report the happy news.