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 */
26 #include <afs/afsutil.h>
31 int xstat_cm_numServers; /*Num connected servers */
32 struct xstat_cm_ConnectionInfo
33 *xstat_cm_ConnInfo; /*Ptr to connection array */
34 struct xstat_cm_ProbeResults xstat_cm_Results; /*Latest probe results */
36 afs_int32 xstat_cmData[AFSCB_MAX_XSTAT_LONGS]; /*Buffer for collected data */
41 static int xstat_cm_ProbeFreqInSecs; /*Probe freq. in seconds */
42 static int xstat_cm_initflag = 0; /*Was init routine called? */
43 static int xstat_cm_debug = 0; /*Debugging output enabled? */
44 static int xstat_cm_oneShot = 0; /*One-shot operation? */
45 static int (*xstat_cm_Handler) (void); /*Probe handler routine */
46 static pthread_t xstat_cm_thread; /*Probe thread */
47 static int xstat_cm_numCollections; /*Number of desired collections */
48 static afs_int32 *xstat_cm_collIDP; /*Ptr to collection IDs desired */
49 static opr_mutex_t xstat_cm_force_lock; /*Lock to wakeup probe */
50 static opr_cv_t xstat_cm_force_cv; /*Condvar to wakeup probe */
53 /*------------------------------------------------------------------------
54 * [private] xstat_cm_CleanupInit
57 * Set up for recovery after an error in initialization (i.e.,
58 * during a call to xstat_cm_Init.
65 * Error value otherwise.
68 * This routine is private to the module.
71 * Zeros out basic data structures.
72 *------------------------------------------------------------------------*/
75 xstat_cm_CleanupInit(void)
77 xstat_cm_ConnInfo = (struct xstat_cm_ConnectionInfo *)0;
78 xstat_cm_Results.probeNum = 0;
79 xstat_cm_Results.probeTime = 0;
80 xstat_cm_Results.connP = (struct xstat_cm_ConnectionInfo *)0;
81 xstat_cm_Results.collectionNumber = 0;
82 xstat_cm_Results.data.AFSCB_CollData_len = AFSCB_MAX_XSTAT_LONGS;
83 xstat_cm_Results.data.AFSCB_CollData_val = (afs_int32 *) xstat_cmData;
84 xstat_cm_Results.probeOK = 0;
90 /*------------------------------------------------------------------------
91 * [exported] xstat_cm_Cleanup
94 * Clean up our memory and connection state.
97 * int a_releaseMem : Should we free up malloc'ed areas?
100 * 0 on total success,
101 * -1 if the module was never initialized, or there was a problem
102 * with the xstat_cm connection array.
105 * xstat_cm_numServers should be properly set. We don't do anything
106 * unless xstat_cm_Init() has already been called.
109 * Shuts down Rx connections gracefully, frees allocated space
111 *------------------------------------------------------------------------*/
114 xstat_cm_Cleanup(int a_releaseMem)
116 static char rn[] = "xstat_cm_Cleanup"; /*Routine name */
117 int code; /*Return code */
118 int conn_idx; /*Current connection index */
119 struct xstat_cm_ConnectionInfo *curr_conn; /*Ptr to xstat_cm connection */
122 * Assume the best, but check the worst.
124 if (!xstat_cm_initflag) {
125 fprintf(stderr, "[%s] Refused; module not initialized\n", rn);
131 * Take care of all Rx connections first. Check to see that the
132 * server count is a legal value.
134 if (xstat_cm_numServers <= 0) {
136 "[%s] Illegal number of servers (xstat_cm_numServers = %d)\n",
137 rn, xstat_cm_numServers);
140 if (xstat_cm_ConnInfo != (struct xstat_cm_ConnectionInfo *)0) {
142 * The xstat_cm connection structure array exists. Go through
143 * it and close up any Rx connections it holds.
145 curr_conn = xstat_cm_ConnInfo;
146 for (conn_idx = 0; conn_idx < xstat_cm_numServers; conn_idx++) {
147 if (curr_conn->rxconn != (struct rx_connection *)0) {
148 rx_DestroyConnection(curr_conn->rxconn);
149 curr_conn->rxconn = (struct rx_connection *)0;
152 } /*for each xstat_cm connection */
153 } /*xstat_cm connection structure exists */
154 } /*Legal number of servers */
157 * If asked to, release the space we've allocated.
160 if (xstat_cm_ConnInfo != (struct xstat_cm_ConnectionInfo *)0)
161 free(xstat_cm_ConnInfo);
165 * Return the news, whatever it is.
171 /*------------------------------------------------------------------------
172 * [private] xstat_cm_LWP
175 * This thread iterates over the server connections and gathers up
176 * the desired statistics from each one on a regular basis, for
177 * all known data collections. The associated handler function
178 * is called each time a new data collection is received.
187 * Started by xstat_cm_Init(), uses global structures and the
188 * global private xstat_cm_oneShot variable.
192 *------------------------------------------------------------------------*/
194 xstat_cm_LWP(void *unused)
196 static char rn[] = "xstat_cm_LWP"; /*Routine name */
197 afs_int32 code; /*Results of calls */
198 struct timeval tv; /*Time structure */
199 struct timespec wait; /*Time to wait */
200 int conn_idx; /*Connection index */
201 struct xstat_cm_ConnectionInfo *curr_conn; /*Current connection */
202 afs_int32 srvVersionNumber; /*Xstat version # */
203 afs_int32 clientVersionNumber; /*Client xstat version */
204 afs_int32 numColls; /*Number of collections to get */
205 afs_int32 *currCollIDP; /*Curr collection ID desired */
208 * Set up some numbers we'll need.
210 clientVersionNumber = AFSCB_XSTAT_VERSION;
212 while (1) { /*Service loop */
214 * Iterate through the server connections, gathering data.
215 * Don't forget to bump the probe count and zero the statistics
216 * areas before calling the servers.
219 printf("[%s] Waking up, getting data from %d server(s)\n", rn,
220 xstat_cm_numServers);
221 curr_conn = xstat_cm_ConnInfo;
222 xstat_cm_Results.probeNum++;
224 for (conn_idx = 0; conn_idx < xstat_cm_numServers; conn_idx++) {
226 * Grab the statistics for the current Cache Manager, if the
227 * connection is valid.
230 printf("[%s] Getting collections from Cache Manager '%s'\n",
231 rn, curr_conn->hostName);
232 if (curr_conn->rxconn != (struct rx_connection *)0) {
234 printf("[%s] Connection OK, calling RXAFSCB_GetXStats\n",
238 * Probe the given CM for each desired collection.
240 currCollIDP = xstat_cm_collIDP;
241 for (numColls = 0; numColls < xstat_cm_numCollections;
242 numColls++, currCollIDP++) {
244 * Initialize the per-probe values.
247 printf("[%s] Asking for data collection %d\n", rn,
249 xstat_cm_Results.collectionNumber = *currCollIDP;
250 xstat_cm_Results.data.AFSCB_CollData_len =
251 AFSCB_MAX_XSTAT_LONGS;
252 memset(xstat_cm_Results.data.AFSCB_CollData_val, 0,
253 AFSCB_MAX_XSTAT_LONGS * 4);
255 xstat_cm_Results.connP = curr_conn;
257 if (xstat_cm_debug) {
259 ("%s: Calling RXAFSCB_GetXStats, conn=%p, clientVersionNumber=%d, collectionNumber=%d, srvVersionNumberP=%p, timeP=%p, dataP=%p\n",
260 rn, curr_conn->rxconn, clientVersionNumber,
261 *currCollIDP, &srvVersionNumber,
262 &(xstat_cm_Results.probeTime),
263 &(xstat_cm_Results.data));
264 printf("%s: [bufflen=%d, buffer at %p]\n", rn,
265 xstat_cm_Results.data.AFSCB_CollData_len,
266 xstat_cm_Results.data.AFSCB_CollData_val);
269 xstat_cm_Results.probeOK =
270 RXAFSCB_GetXStats(curr_conn->rxconn,
271 clientVersionNumber, *currCollIDP,
273 &(xstat_cm_Results.probeTime),
274 &(xstat_cm_Results.data));
277 * Now that we (may) have the data for this connection,
278 * call the associated handler function. The handler
279 * does not take any explicit parameters, but rather
280 * gets to the goodies via some of the objects exported
284 printf("[%s] Calling handler routine.\n", rn);
285 code = xstat_cm_Handler();
288 "[%s] Handler routine got error code %d\n",
290 } /*For each collection */
293 /*Valid Rx connection */
295 * Advance the xstat_cm connection pointer.
299 } /*For each xstat_cm connection */
302 * All (valid) connections have been probed. Fall asleep for the
303 * prescribed number of seconds, unless we're a one-shot. In
304 * that case, we need to signal our caller that we're done.
307 printf("[%s] Polling complete for probe round %d.\n", rn,
308 xstat_cm_Results.probeNum);
310 if (xstat_cm_oneShot) {
312 * One-shot execution desired.
314 break; /*from the perpetual while loop */
315 } /*One-shot execution */
318 * Continuous execution desired. Sleep for the required
319 * number of seconds or wakeup sooner if forced.
322 printf("[%s] Falling asleep for %d seconds\n", rn,
323 xstat_cm_ProbeFreqInSecs);
324 gettimeofday(&tv, NULL);
325 wait.tv_sec = tv.tv_sec + xstat_cm_ProbeFreqInSecs;
326 wait.tv_nsec = tv.tv_usec * 1000;
327 opr_mutex_enter(&xstat_cm_force_lock);
328 code = opr_cv_timedwait(&xstat_cm_force_cv, &xstat_cm_force_lock, &wait);
329 opr_mutex_exit(&xstat_cm_force_lock);
330 } /*Continuous execution */
336 /*------------------------------------------------------------------------
337 * [exported] xstat_cm_Init
340 * Initialize the xstat_cm module: set up Rx connections to the
341 * given set of Cache Managers, start up the probe thread, and
342 * associate the routine to be called when a probe completes.
343 * Also, let it know which collections you're interested in.
346 * int a_numServers : Num. servers to connect to.
347 * struct sockaddr_in *a_socketArray : Array of server sockets.
348 * int a_ProbeFreqInSecs : Probe frequency in seconds.
349 * int (*a_ProbeHandler)() : Ptr to probe handler fcn.
350 * int a_flags; : Various flags.
351 * int a_numCollections : Number of collections desired.
352 * afs_int32 *a_collIDP : Ptr to collection IDs.
356 * -2 for (at least one) connection error,
357 * thread process creation code, if it failed,
358 * -1 for other fatal errors.
361 * *** MUST BE THE FIRST ROUTINE CALLED FROM THIS PACKAGE ***
364 * Sets up just about everything.
365 *------------------------------------------------------------------------*/
368 xstat_cm_Init(int a_numServers, struct sockaddr_in *a_socketArray,
369 int a_ProbeFreqInSecs, int (*a_ProbeHandler) (void), int a_flags,
370 int a_numCollections, afs_int32 * a_collIDP)
373 static char rn[] = "xstat_cm_Init"; /*Routine name */
374 afs_int32 code; /*Return value */
375 struct rx_securityClass *secobj; /*Client security object */
376 int arg_errfound; /*Argument error found? */
377 int curr_srv; /*Current server idx */
378 struct xstat_cm_ConnectionInfo *curr_conn; /*Ptr to current conn */
379 char *hostNameFound; /*Ptr to returned host name */
380 int conn_err; /*Connection error? */
381 int collIDBytes; /*Num bytes in coll ID array */
385 * If we've already been called, snicker at the bozo, gently
386 * remind him of his doubtful heritage, and return success.
388 if (xstat_cm_initflag) {
389 fprintf(stderr, "[%s] Called multiple times!\n", rn);
392 xstat_cm_initflag = 1;
394 opr_mutex_init(&xstat_cm_force_lock);
395 opr_cv_init(&xstat_cm_force_cv);
398 * Check the parameters for bogosities.
401 if (a_numServers <= 0) {
402 fprintf(stderr, "[%s] Illegal number of servers: %d\n", rn,
406 if (a_socketArray == (struct sockaddr_in *)0) {
407 fprintf(stderr, "[%s] Null server socket array argument\n", rn);
410 if (a_ProbeFreqInSecs <= 0) {
411 fprintf(stderr, "[%s] Illegal probe frequency: %d\n", rn,
415 if (a_ProbeHandler == NULL) {
416 fprintf(stderr, "[%s] Null probe handler function argument\n", rn);
419 if (a_numCollections <= 0) {
420 fprintf(stderr, "[%s] Illegal collection count argument: %d\n", rn,
424 if (a_collIDP == NULL) {
425 fprintf(stderr, "[%s] Null collection ID array argument\n", rn);
432 * Record our passed-in info.
434 xstat_cm_debug = (a_flags & XSTAT_CM_INITFLAG_DEBUGGING);
435 xstat_cm_oneShot = (a_flags & XSTAT_CM_INITFLAG_ONE_SHOT);
436 xstat_cm_numServers = a_numServers;
437 xstat_cm_Handler = a_ProbeHandler;
438 xstat_cm_ProbeFreqInSecs = a_ProbeFreqInSecs;
439 xstat_cm_numCollections = a_numCollections;
440 collIDBytes = xstat_cm_numCollections * sizeof(afs_int32);
441 xstat_cm_collIDP = malloc(collIDBytes);
442 memcpy(xstat_cm_collIDP, a_collIDP, collIDBytes);
443 if (xstat_cm_debug) {
444 printf("[%s] Asking for %d collection(s): ", rn,
445 xstat_cm_numCollections);
446 for (curr_srv = 0; curr_srv < xstat_cm_numCollections; curr_srv++)
447 printf("%d ", *(xstat_cm_collIDP + curr_srv));
452 * Get ready in case we have to do a cleanup - basically, zero
455 code = xstat_cm_CleanupInit();
460 * Allocate the necessary data structures and initialize everything
463 xstat_cm_ConnInfo = malloc(a_numServers
464 * sizeof(struct xstat_cm_ConnectionInfo));
465 if (xstat_cm_ConnInfo == (struct xstat_cm_ConnectionInfo *)0) {
467 "[%s] Can't allocate %d connection info structs (%" AFS_SIZET_FMT " bytes)\n",
469 (a_numServers * sizeof(struct xstat_cm_ConnectionInfo)));
470 return (-1); /*No cleanup needs to be done yet */
474 * Initialize the Rx subsystem, just in case nobody's done it.
477 printf("[%s] Initializing Rx on port 0\n", rn);
478 code = rx_Init(htons(0));
480 fprintf(stderr, "[%s] Fatal error in rx_Init(), error=%d\n", rn,
486 printf("[%s] Rx initialized on port 0\n", rn);
489 * Create a null Rx client security object, to be used by the
492 secobj = rxnull_NewClientSecurityObject();
493 if (secobj == (struct rx_securityClass *)0) {
495 "[%s] Can't create probe thread client security object.\n", rn);
496 xstat_cm_Cleanup(1); /*Delete already-malloc'ed areas */
500 printf("[%s] Probe thread client security object created\n", rn);
502 curr_conn = xstat_cm_ConnInfo;
504 for (curr_srv = 0; curr_srv < a_numServers; curr_srv++) {
506 * Copy in the socket info for the current server, resolve its
507 * printable name if possible.
509 if (xstat_cm_debug) {
510 printf("[%s] Copying in the following socket info:\n", rn);
511 printf("[%s] IP addr %s, port %d\n", rn,
512 afs_inet_ntoa_r((a_socketArray + curr_srv)->sin_addr.s_addr,hoststr),
513 ntohs((a_socketArray + curr_srv)->sin_port));
515 memcpy(&(curr_conn->skt), a_socketArray + curr_srv,
516 sizeof(struct sockaddr_in));
519 hostutil_GetNameByINet(curr_conn->skt.sin_addr.s_addr);
520 if (hostNameFound == NULL) {
522 "[%s] Can't map Internet address %s to a string name\n",
523 rn, afs_inet_ntoa_r(curr_conn->skt.sin_addr.s_addr,hoststr));
524 curr_conn->hostName[0] = '\0';
526 strcpy(curr_conn->hostName, hostNameFound);
528 printf("[%s] Host name for server index %d is %s\n", rn,
529 curr_srv, curr_conn->hostName);
533 * Make an Rx connection to the current server.
537 ("[%s] Connecting to srv idx %d, IP addr %s, port %d, service 1\n",
538 rn, curr_srv, afs_inet_ntoa_r(curr_conn->skt.sin_addr.s_addr,hoststr),
539 ntohs(curr_conn->skt.sin_port));
540 curr_conn->rxconn = rx_NewConnection(curr_conn->skt.sin_addr.s_addr, /*Server addr */
541 curr_conn->skt.sin_port, /*Server port */
542 1, /*AFS service # */
543 secobj, /*Security obj */
545 if (curr_conn->rxconn == (struct rx_connection *)0) {
547 "[%s] Can't create Rx connection to server '%s' (%s)\n",
548 rn, curr_conn->hostName, afs_inet_ntoa_r(curr_conn->skt.sin_addr.s_addr,hoststr));
552 printf("[%s] New connection at %p\n", rn, curr_conn->rxconn);
555 * Bump the current xstat_cm connection to set up.
562 * Start up the probe thread.
565 printf("[%s] Creating the probe thread\n", rn);
566 code = pthread_create(&xstat_cm_thread, NULL, xstat_cm_LWP, NULL);
568 fprintf(stderr, "[%s] Can't create xstat_cm thread! Error is %d\n", rn,
570 xstat_cm_Cleanup(1); /*Delete already-malloc'ed areas */
575 * Return the final results.
584 /*------------------------------------------------------------------------
585 * [exported] xstat_cm_ForceProbeNow
588 * Wake up the probe thread, forcing it to execute a probe immediately.
595 * Error value otherwise.
598 * The module must have been initialized.
602 *------------------------------------------------------------------------*/
605 xstat_cm_ForceProbeNow(void)
607 static char rn[] = "xstat_cm_ForceProbeNow"; /*Routine name */
610 * There isn't a prayer unless we've been initialized.
612 if (!xstat_cm_initflag) {
613 fprintf(stderr, "[%s] Must call xstat_cm_Init first!\n", rn);
618 * Kick the sucker in the side.
620 opr_mutex_enter(&xstat_cm_force_lock);
621 opr_cv_signal(&xstat_cm_force_cv);
622 opr_mutex_exit(&xstat_cm_force_lock);
625 * We did it, so report the happy news.
631 * Wait for the collection to complete. Returns after one cycle if running in
632 * one-shot mode, otherwise wait for a given amount of time.
635 * int sleep_secs : time to wait in seconds when running
636 * in continuous mode. 0 means wait forever.
642 xstat_cm_Wait(int sleep_secs)
644 static char rn[] = "xstat_cm_Wait"; /*Routine name */
646 struct timeval tv; /*Time structure */
648 if (xstat_cm_oneShot) {
650 * One-shot operation; just wait for the collection to be done.
653 printf("[%s] Calling pthread_join()\n", rn);
654 code = pthread_join(xstat_cm_thread, NULL);
656 printf("[%s] Returned from pthread_join()\n", rn);
659 "[%s] Error %d encountered by pthread_join()\n",
662 } else if (sleep_secs == 0) {
665 fprintf(stderr, "[%s] going to sleep ...\n", rn);
667 code = select(0, /*Num fds */
668 0, /*Descriptors ready for reading */
669 0, /*Descriptors ready for writing */
670 0, /*Descriptors with exceptional conditions */
671 NULL); /* NULL timeout means "forever" */
673 fprintf(stderr, "[%s] select() error %d\n", rn, errno);
678 /* Let's just fall asleep while. */
681 ("xstat_cm service started, main thread sleeping for %d secs.\n",
683 tv.tv_sec = sleep_secs;
685 code = select(0, /*Num fds */
686 0, /*Descriptors ready for reading */
687 0, /*Descriptors ready for writing */
688 0, /*Descriptors with exceptional conditions */
689 &tv); /*Timeout structure */
691 fprintf(stderr, "[%s] select() error: %d\n", rn, errno);