Use AFS_PTR_FMT to format pointers
[openafs.git] / src / xstat / xstat_fs_test.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10 /*
11  * Description:
12  *      Test of the xstat_fs module.
13  *
14  *------------------------------------------------------------------------*/
15
16 #include <afsconfig.h>
17 #include <afs/param.h>
18
19
20 #include "xstat_fs.h"           /*Interface for xstat_fs module */
21 #include <cmd.h>                /*Command line interpreter */
22 #include <time.h>
23 #include <string.h>
24 #include <afs/afsutil.h>
25
26 /*
27  * Command line parameter indices.
28  *      P_FS_NAMES : List of FileServer names.
29  *      P_COLL_IDS : List of collection IDs to pick up.
30  *      P_ONESHOT  : Are we gathering exactly one round of data?
31  *      P_DEBUG    : Enable debugging output?
32  */
33 #define P_FS_NAMES      0
34 #define P_COLL_IDS      1
35 #define P_ONESHOT       2
36 #define P_FREQUENCY     3
37 #define P_PERIOD        4
38 #define P_DEBUG         5
39
40 /*
41  * Private globals.
42  */
43 static int debugging_on = 0;    /*Are we debugging? */
44 static int one_shot = 0;        /*Single round of data collection? */
45
46 static char *opNames[] = {
47     "FetchData",
48     "FetchACL",
49     "FetchStatus",
50     "StoreData",
51     "StoreACL",
52     "StoreStatus",
53     "RemoveFile",
54     "CreateFile",
55     "Rename",
56     "Symlink",
57     "Link",
58     "MakeDir",
59     "RemoveDir",
60     "SetLock",
61     "ExtendLock",
62     "ReleaseLock",
63     "GetStatistics",
64     "GiveUpCallbacks",
65     "GetVolumeInfo",
66     "GetVolumeStatus",
67     "SetVolumeStatus",
68     "GetRootVolume",
69     "CheckToken",
70     "GetTime",
71     "NGetVolumeInfo",
72     "BulkStatus",
73     "XStatsVersion",
74     "GetXStats"
75 };
76
77 static char *xferOpNames[] = {
78     "FetchData",
79     "StoreData"
80 };
81
82
83 /*------------------------------------------------------------------------
84  * PrintCallInfo
85  *
86  * Description:
87  *      Print out the AFS_XSTATSCOLL_CALL_INFO collection we just
88  *      received.
89  *
90  * Arguments:
91  *      None.
92  *
93  * Returns:
94  *      Nothing.
95  *
96  * Environment:
97  *      All the info we need is nestled into xstat_fs_Results.
98  *
99  * Side Effects:
100  *      As advertised.
101  *------------------------------------------------------------------------*/
102
103 void
104 PrintCallInfo(void)
105 {                               /*PrintCallInfo */
106     register int i;             /*Loop variable */
107     int numInt32s;              /*# int32words returned */
108     afs_int32 *currInt32;       /*Ptr to current afs_int32 value */
109     char *printableTime;        /*Ptr to printable time string */
110     time_t probeTime = xstat_fs_Results.probeTime;
111
112     /*
113      * Just print out the results of the particular probe.
114      */
115     numInt32s = xstat_fs_Results.data.AFS_CollData_len;
116     currInt32 = (afs_int32 *) (xstat_fs_Results.data.AFS_CollData_val);
117     printableTime = ctime(&probeTime);
118     printableTime[strlen(printableTime) - 1] = '\0';
119
120     printf("AFS_XSTATSCOLL_CALL_INFO (coll %d) for FS %s\n[Probe %d, %s]\n\n",
121            xstat_fs_Results.collectionNumber,
122            xstat_fs_Results.connP->hostName, xstat_fs_Results.probeNum,
123            printableTime);
124
125     if (debugging_on)
126         printf("\n[%d entries returned at %" AFS_PTR_FMT "]\n\n", numInt32s, currInt32);
127
128     for (i = 0; i < numInt32s; i++)
129         printf("%d ", *currInt32++);
130     fprintf(stderr, "\n");
131
132 }                               /*PrintCallInfo */
133
134
135 /*------------------------------------------------------------------------
136  * PrintOverallPerfInfo
137  *
138  * Description:
139  *      Print out overall performance numbers.
140  *
141  * Arguments:
142  *      a_ovP : Ptr to the overall performance numbers.
143  *
144  * Returns:
145  *      Nothing.
146  *
147  * Environment:
148  *      Nothing interesting.
149  *
150  * Side Effects:
151  *      As advertised.
152  *------------------------------------------------------------------------*/
153
154 void
155 PrintOverallPerfInfo(struct afs_PerfStats *a_ovP)
156 {
157     printf("\t%10d numPerfCalls\n\n", a_ovP->numPerfCalls);
158
159     /*
160      * Vnode cache section.
161      */
162     printf("\t%10d vcache_L_Entries\n", a_ovP->vcache_L_Entries);
163     printf("\t%10d vcache_L_Allocs\n", a_ovP->vcache_L_Allocs);
164     printf("\t%10d vcache_L_Gets\n", a_ovP->vcache_L_Gets);
165     printf("\t%10d vcache_L_Reads\n", a_ovP->vcache_L_Reads);
166     printf("\t%10d vcache_L_Writes\n\n", a_ovP->vcache_L_Writes);
167
168     printf("\t%10d vcache_S_Entries\n", a_ovP->vcache_S_Entries);
169     printf("\t%10d vcache_S_Allocs\n", a_ovP->vcache_S_Allocs);
170     printf("\t%10d vcache_S_Gets\n", a_ovP->vcache_S_Gets);
171     printf("\t%10d vcache_S_Reads\n", a_ovP->vcache_S_Reads);
172     printf("\t%10d vcache_S_Writes\n\n", a_ovP->vcache_S_Writes);
173
174     printf("\t%10d vcache_H_Entries\n", a_ovP->vcache_H_Entries);
175     printf("\t%10d vcache_H_Gets\n", a_ovP->vcache_H_Gets);
176     printf("\t%10d vcache_H_Replacements\n\n", a_ovP->vcache_H_Replacements);
177
178     /*
179      * Directory package section.
180      */
181     printf("\t%10d dir_Buffers\n", a_ovP->dir_Buffers);
182     printf("\t%10d dir_Calls\n", a_ovP->dir_Calls);
183     printf("\t%10d dir_IOs\n\n", a_ovP->dir_IOs);
184
185     /*
186      * Rx section.
187      */
188     printf("\t%10d rx_packetRequests\n", a_ovP->rx_packetRequests);
189     printf("\t%10d rx_noPackets_RcvClass\n", a_ovP->rx_noPackets_RcvClass);
190     printf("\t%10d rx_noPackets_SendClass\n", a_ovP->rx_noPackets_SendClass);
191     printf("\t%10d rx_noPackets_SpecialClass\n",
192            a_ovP->rx_noPackets_SpecialClass);
193     printf("\t%10d rx_socketGreedy\n", a_ovP->rx_socketGreedy);
194     printf("\t%10d rx_bogusPacketOnRead\n", a_ovP->rx_bogusPacketOnRead);
195     printf("\t%10d rx_bogusHost\n", a_ovP->rx_bogusHost);
196     printf("\t%10d rx_noPacketOnRead\n", a_ovP->rx_noPacketOnRead);
197     printf("\t%10d rx_noPacketBuffersOnRead\n",
198            a_ovP->rx_noPacketBuffersOnRead);
199     printf("\t%10d rx_selects\n", a_ovP->rx_selects);
200     printf("\t%10d rx_sendSelects\n", a_ovP->rx_sendSelects);
201     printf("\t%10d rx_packetsRead_RcvClass\n",
202            a_ovP->rx_packetsRead_RcvClass);
203     printf("\t%10d rx_packetsRead_SendClass\n",
204            a_ovP->rx_packetsRead_SendClass);
205     printf("\t%10d rx_packetsRead_SpecialClass\n",
206            a_ovP->rx_packetsRead_SpecialClass);
207     printf("\t%10d rx_dataPacketsRead\n", a_ovP->rx_dataPacketsRead);
208     printf("\t%10d rx_ackPacketsRead\n", a_ovP->rx_ackPacketsRead);
209     printf("\t%10d rx_dupPacketsRead\n", a_ovP->rx_dupPacketsRead);
210     printf("\t%10d rx_spuriousPacketsRead\n", a_ovP->rx_spuriousPacketsRead);
211     printf("\t%10d rx_packetsSent_RcvClass\n",
212            a_ovP->rx_packetsSent_RcvClass);
213     printf("\t%10d rx_packetsSent_SendClass\n",
214            a_ovP->rx_packetsSent_SendClass);
215     printf("\t%10d rx_packetsSent_SpecialClass\n",
216            a_ovP->rx_packetsSent_SpecialClass);
217     printf("\t%10d rx_ackPacketsSent\n", a_ovP->rx_ackPacketsSent);
218     printf("\t%10d rx_pingPacketsSent\n", a_ovP->rx_pingPacketsSent);
219     printf("\t%10d rx_abortPacketsSent\n", a_ovP->rx_abortPacketsSent);
220     printf("\t%10d rx_busyPacketsSent\n", a_ovP->rx_busyPacketsSent);
221     printf("\t%10d rx_dataPacketsSent\n", a_ovP->rx_dataPacketsSent);
222     printf("\t%10d rx_dataPacketsReSent\n", a_ovP->rx_dataPacketsReSent);
223     printf("\t%10d rx_dataPacketsPushed\n", a_ovP->rx_dataPacketsPushed);
224     printf("\t%10d rx_ignoreAckedPacket\n", a_ovP->rx_ignoreAckedPacket);
225     printf("\t%10d rx_totalRtt_Sec\n", a_ovP->rx_totalRtt_Sec);
226     printf("\t%10d rx_totalRtt_Usec\n", a_ovP->rx_totalRtt_Usec);
227     printf("\t%10d rx_minRtt_Sec\n", a_ovP->rx_minRtt_Sec);
228     printf("\t%10d rx_minRtt_Usec\n", a_ovP->rx_minRtt_Usec);
229     printf("\t%10d rx_maxRtt_Sec\n", a_ovP->rx_maxRtt_Sec);
230     printf("\t%10d rx_maxRtt_Usec\n", a_ovP->rx_maxRtt_Usec);
231     printf("\t%10d rx_nRttSamples\n", a_ovP->rx_nRttSamples);
232     printf("\t%10d rx_nServerConns\n", a_ovP->rx_nServerConns);
233     printf("\t%10d rx_nClientConns\n", a_ovP->rx_nClientConns);
234     printf("\t%10d rx_nPeerStructs\n", a_ovP->rx_nPeerStructs);
235     printf("\t%10d rx_nCallStructs\n", a_ovP->rx_nCallStructs);
236     printf("\t%10d rx_nFreeCallStructs\n", a_ovP->rx_nFreeCallStructs);
237     printf("\t%10d rx_nBusies\n\n", a_ovP->rx_nBusies);
238
239     printf("\t%10d fs_nBusies\n", a_ovP->fs_nBusies);
240     printf("\t%10d fs_GetCapabilities\n\n", a_ovP->fs_nGetCaps);
241     /*
242      * Host module fields.
243      */
244     printf("\t%10d host_NumHostEntries\n", a_ovP->host_NumHostEntries);
245     printf("\t%10d host_HostBlocks\n", a_ovP->host_HostBlocks);
246     printf("\t%10d host_NonDeletedHosts\n", a_ovP->host_NonDeletedHosts);
247     printf("\t%10d host_HostsInSameNetOrSubnet\n",
248            a_ovP->host_HostsInSameNetOrSubnet);
249     printf("\t%10d host_HostsInDiffSubnet\n", a_ovP->host_HostsInDiffSubnet);
250     printf("\t%10d host_HostsInDiffNetwork\n",
251            a_ovP->host_HostsInDiffNetwork);
252     printf("\t%10d host_NumClients\n", a_ovP->host_NumClients);
253     printf("\t%10d host_ClientBlocks\n\n", a_ovP->host_ClientBlocks);
254
255     printf("\t%10d sysname_ID\n", a_ovP->sysname_ID);
256 }
257
258
259 /*------------------------------------------------------------------------
260  * PrintOpTiming
261  *
262  * Description:
263  *      Print out the contents of an RPC op timing structure.
264  *
265  * Arguments:
266  *      a_opIdx   : Index of the AFS operation we're printing number on.
267  *      a_opTimeP : Ptr to the op timing structure to print.
268  *
269  * Returns:
270  *      Nothing.
271  *
272  * Environment:
273  *      Nothing interesting.
274  *
275  * Side Effects:
276  *      As advertised.
277  *------------------------------------------------------------------------*/
278
279 void
280 PrintOpTiming(int a_opIdx, struct fs_stats_opTimingData *a_opTimeP)
281 {
282     double fSumTime, avg;
283
284     fSumTime =
285         ((double)(a_opTimeP->sumTime.tv_sec)) +
286         (((double)(a_opTimeP->sumTime.tv_usec)) / ((double)(1000000)));
287 /*    printf("Double sum time is %f\n", fSumTime);*/
288     avg = fSumTime / ((double)(a_opTimeP->numSuccesses));
289
290     printf
291         ("%15s: %d ops (%d OK); sum=%d.%06d, sqr=%d.%06d, min=%d.%06d, max=%d.%06d\n",
292          opNames[a_opIdx], a_opTimeP->numOps, a_opTimeP->numSuccesses,
293          a_opTimeP->sumTime.tv_sec, a_opTimeP->sumTime.tv_usec,
294          a_opTimeP->sqrTime.tv_sec, a_opTimeP->sqrTime.tv_usec,
295          a_opTimeP->minTime.tv_sec, a_opTimeP->minTime.tv_usec,
296          a_opTimeP->maxTime.tv_sec, a_opTimeP->maxTime.tv_usec);
297 }
298
299
300 /*------------------------------------------------------------------------
301  * PrintXferTiming
302  *
303  * Description:
304  *      Print out the contents of a data transfer structure.
305  *
306  * Arguments:
307  *      a_opIdx : Index of the AFS operation we're printing number on.
308  *      a_xferP : Ptr to the data transfer structure to print.
309  *
310  * Returns:
311  *      Nothing.
312  *
313  * Environment:
314  *      Nothing interesting.
315  *
316  * Side Effects:
317  *      As advertised.
318  *------------------------------------------------------------------------*/
319
320 void
321 PrintXferTiming(int a_opIdx, struct fs_stats_xferData *a_xferP)
322 {
323     double fSumTime, avg;
324
325     fSumTime =
326         ((double)(a_xferP->sumTime.tv_sec)) +
327         ((double)(a_xferP->sumTime.tv_usec)) / ((double)(1000000));
328
329     avg = fSumTime / ((double)(a_xferP->numSuccesses));
330
331     printf
332         ("%s: %d xfers (%d OK), time sum=%d.%06d, sqr=%d.%06d, min=%d.%06d, max=%d.%06d\n",
333          xferOpNames[a_opIdx], a_xferP->numXfers, a_xferP->numSuccesses,
334          a_xferP->sumTime.tv_sec, a_xferP->sumTime.tv_usec,
335          a_xferP->sqrTime.tv_sec, a_xferP->sqrTime.tv_usec,
336          a_xferP->minTime.tv_sec, a_xferP->minTime.tv_usec,
337          a_xferP->maxTime.tv_sec, a_xferP->maxTime.tv_usec);
338     printf("\t[bytes: sum=%lu, min=%d, max=%d]\n", a_xferP->sumBytes,
339            a_xferP->minBytes, a_xferP->maxBytes);
340     printf
341         ("\t[buckets: 0: %d, 1: %d, 2: %d, 3: %d, 4: %d, 5: %d, 6: %d, 7: %d, 8: %d]\n",
342          a_xferP->count[0], a_xferP->count[1], a_xferP->count[2],
343          a_xferP->count[3], a_xferP->count[4], a_xferP->count[5],
344          a_xferP->count[6], a_xferP->count[7], a_xferP->count[8]);
345 }
346
347
348 /*------------------------------------------------------------------------
349  * PrintDetailedPerfInfo
350  *
351  * Description:
352  *      Print out a set of detailed performance numbers.
353  *
354  * Arguments:
355  *      a_detP : Ptr to detailed perf numbers to print.
356  *
357  * Returns:
358  *      Nothing.
359  *
360  * Environment:
361  *      Nothing interesting.
362  *
363  * Side Effects:
364  *      As advertised.
365  *------------------------------------------------------------------------*/
366
367 void
368 PrintDetailedPerfInfo(struct fs_stats_DetailedStats *a_detP)
369 {
370     int currIdx;                /*Loop variable */
371
372     printf("\t%10d epoch\n", a_detP->epoch);
373
374     for (currIdx = 0; currIdx < FS_STATS_NUM_RPC_OPS; currIdx++)
375         PrintOpTiming(currIdx, &(a_detP->rpcOpTimes[currIdx]));
376
377     for (currIdx = 0; currIdx < FS_STATS_NUM_XFER_OPS; currIdx++)
378         PrintXferTiming(currIdx, &(a_detP->xferOpTimes[currIdx]));
379 }
380
381
382 /*------------------------------------------------------------------------
383  * PrintFullPerfInfo
384  *
385  * Description:
386  *      Print out the AFS_XSTATSCOLL_FULL_PERF_INFO collection we just
387  *      received.
388  *
389  * Arguments:
390  *      None.
391  *
392  * Returns:
393  *      Nothing.
394  *
395  * Environment:
396  *      All the info we need is nestled into xstat_fs_Results.
397  *
398  * Side Effects:
399  *      As advertised.
400  *------------------------------------------------------------------------*/
401
402 void
403 PrintFullPerfInfo(void)
404 {
405
406     static afs_int32 fullPerfInt32s = (sizeof(struct fs_stats_FullPerfStats) >> 2);     /*Correct # int32s to rcv */
407     afs_int32 numInt32s;        /*# int32words received */
408     struct fs_stats_FullPerfStats *fullPerfP;   /*Ptr to full perf stats */
409     char *printableTime;        /*Ptr to printable time
410                                  * string */
411     time_t probeTime = xstat_fs_Results.probeTime;
412
413     numInt32s = xstat_fs_Results.data.AFS_CollData_len;
414     if (numInt32s != fullPerfInt32s) {
415         printf("** Data size mismatch in full performance collection!");
416         printf("** Expecting %d, got %d\n", fullPerfInt32s, numInt32s);
417         return;
418     }
419
420     printableTime = ctime(&probeTime);
421     printableTime[strlen(printableTime) - 1] = '\0';
422     fullPerfP = (struct fs_stats_FullPerfStats *)
423         (xstat_fs_Results.data.AFS_CollData_val);
424
425     printf
426         ("AFS_XSTATSCOLL_FULL_PERF_INFO (coll %d) for FS %s\n[Probe %d, %s]\n\n",
427          xstat_fs_Results.collectionNumber, xstat_fs_Results.connP->hostName,
428          xstat_fs_Results.probeNum, printableTime);
429
430     PrintOverallPerfInfo(&(fullPerfP->overall));
431     PrintDetailedPerfInfo(&(fullPerfP->det));
432 }
433
434
435 /*------------------------------------------------------------------------
436  * PrintPerfInfo
437  *
438  * Description:
439  *      Print out the AFS_XSTATSCOLL_PERF_INFO collection we just
440  *      received.
441  *
442  * Arguments:
443  *      None.
444  *
445  * Returns:
446  *      Nothing.
447  *
448  * Environment:
449  *      All the info we need is nestled into xstat_fs_Results.
450  *
451  * Side Effects:
452  *      As advertised.
453  *------------------------------------------------------------------------*/
454
455 void
456 PrintPerfInfo(void)
457 {
458     static afs_int32 perfInt32s = (sizeof(struct afs_PerfStats) >> 2);  /*Correct # int32s to rcv */
459     afs_int32 numInt32s;        /*# int32words received */
460     struct afs_PerfStats *perfP;        /*Ptr to performance stats */
461     char *printableTime;        /*Ptr to printable time string */
462     time_t probeTime = xstat_fs_Results.probeTime;
463
464     numInt32s = xstat_fs_Results.data.AFS_CollData_len;
465     if (numInt32s != perfInt32s) {
466         printf("** Data size mismatch in performance collection!");
467         printf("** Expecting %d, got %d\n", perfInt32s, numInt32s);
468         return;
469     }
470
471     printableTime = ctime(&probeTime);
472     printableTime[strlen(printableTime) - 1] = '\0';
473     perfP = (struct afs_PerfStats *)
474         (xstat_fs_Results.data.AFS_CollData_val);
475
476     printf("AFS_XSTATSCOLL_PERF_INFO (coll %d) for FS %s\n[Probe %d, %s]\n\n",
477            xstat_fs_Results.collectionNumber,
478            xstat_fs_Results.connP->hostName, xstat_fs_Results.probeNum,
479            printableTime);
480
481     PrintOverallPerfInfo(perfP);
482 }
483
484 static char *CbCounterStrings[] = {
485     "DeleteFiles",
486     "DeleteCallBacks",
487     "BreakCallBacks",
488     "AddCallBack",
489     "GotSomeSpaces",
490     "DeleteAllCallBacks",
491     "nFEs", "nCBs", "nblks",
492     "CBsTimedOut",
493     "nbreakers",
494     "GSS1", "GSS2", "GSS3", "GSS4", "GSS5"
495 };
496
497
498 void
499 PrintCbCounters(void) {
500     int numInt32s = sizeof(CbCounterStrings)/sizeof(char *);
501     int i;
502     afs_uint32 *val=xstat_fs_Results.data.AFS_CollData_val;
503
504     if (numInt32s > xstat_fs_Results.data.AFS_CollData_len)
505         numInt32s = xstat_fs_Results.data.AFS_CollData_len;
506
507     for (i=0; i<numInt32s; i++) {
508         printf("\t%10u %s\n", val[i], CbCounterStrings[i]);
509     }
510 }
511
512
513 /*------------------------------------------------------------------------
514  * FS_Handler
515  *
516  * Description:
517  *      Handler routine passed to the xstat_fs module.  This handler is
518  *      called immediately after a poll of one of the File Servers has
519  *      taken place.  All it needs to know is exported by the xstat_fs
520  *      module, namely the data structure where the probe results are
521  *      stored.
522  *
523  * Arguments:
524  *      None.
525  *
526  * Returns:
527  *      0 on success,
528  *      -1 otherwise.
529  *
530  * Environment:
531  *      See above.  All we do now is print out what we got.
532  *
533  * Side Effects:
534  *      As advertised.
535  *------------------------------------------------------------------------*/
536
537 int
538 FS_Handler(void)
539 {
540     static char rn[] = "FS_Handler";    /*Routine name */
541
542     printf
543         ("\n------------------------------------------------------------\n");
544
545     /*
546      * If the probe failed, there isn't much we can do except gripe.
547      */
548     if (xstat_fs_Results.probeOK) {
549         printf("%s: Probe %d to File Server '%s' failed, code=%d\n", rn,
550                xstat_fs_Results.probeNum, xstat_fs_Results.connP->hostName,
551                xstat_fs_Results.probeOK);
552         return (0);
553     }
554
555     switch (xstat_fs_Results.collectionNumber) {
556     case AFS_XSTATSCOLL_CALL_INFO:
557         PrintCallInfo();
558         break;
559
560     case AFS_XSTATSCOLL_PERF_INFO:
561         PrintPerfInfo();
562         break;
563
564     case AFS_XSTATSCOLL_FULL_PERF_INFO:
565         PrintFullPerfInfo();
566         break;
567
568     case AFS_XSTATSCOLL_CBSTATS:
569         PrintCbCounters();
570         break;
571
572     default:
573         printf("** Unknown collection: %d\n",
574                xstat_fs_Results.collectionNumber);
575     }
576
577     /*
578      * Return the happy news.
579      */
580     return (0);
581 }
582
583
584 /*------------------------------------------------------------------------
585  * CountListItems
586  *
587  * Description:
588  *      Given a pointer to the list of File Servers we'll be polling
589  *      (or, in fact, any list at all), compute the length of the list.
590  *
591  * Arguments:
592  *      struct cmd_item *a_firstItem : Ptr to first item in list.
593  *
594  * Returns:
595  *      Length of the above list.
596  *
597  * Environment:
598  *      Nothing interesting.
599  *
600  * Side Effects:
601  *      As advertised.
602  *------------------------------------------------------------------------*/
603
604 static int
605 CountListItems(struct cmd_item *a_firstItem)
606 {
607
608     int list_len;               /*List length */
609     struct cmd_item *curr_item; /*Ptr to current item */
610
611     list_len = 0;
612     curr_item = a_firstItem;
613
614     /*
615      * Count 'em up.
616      */
617     while (curr_item) {
618         list_len++;
619         curr_item = curr_item->next;
620     }
621
622     /*
623      * Return our tally.
624      */
625     return (list_len);
626 }
627
628
629 /*------------------------------------------------------------------------
630  * RunTheTest
631  *
632  * Description:
633  *      Routine called by the command line interpreter to execute the
634  *      meat of the program.  We count the number of File Servers
635  *      to watch, allocate enough space to remember all the connection
636  *      info for them, then go for it.
637  *      
638  *
639  * Arguments:
640  *      a_s : Ptr to the command line syntax descriptor.
641  *
642  * Returns:
643  *      0, but may exit the whole program on an error!
644  *
645  * Environment:
646  *      Nothing interesting.
647  *
648  * Side Effects:
649  *      As advertised.
650  *------------------------------------------------------------------------*/
651
652 int
653 RunTheTest(struct cmd_syndesc *a_s, void *dummy)
654 {
655     static char rn[] = "RunTheTest";    /*Routine name */
656     int code;                   /*Return code */
657     int numFSs;                 /*# File Servers to monitor */
658     int numCollIDs;             /*# collections to fetch */
659     int currFS;                 /*Loop index */
660     int currCollIDIdx;          /*Index of current collection ID */
661     afs_int32 *collIDP;         /*Ptr to array of collection IDs */
662     afs_int32 *currCollIDP;     /*Ptr to current collection ID */
663     struct cmd_item *curr_item; /*Current FS cmd line record */
664     struct sockaddr_in FSSktArray[20];  /*File Server socket array - FIX! */
665     struct hostent *he;         /*Host entry */
666     struct timeval tv;          /*Time structure */
667     int sleep_secs;             /*Number of seconds to sleep */
668     int initFlags;              /*Flags passed to the init fcn */
669     int waitCode;               /*Result of LWP_WaitProcess() */
670     int freq;                   /*Frequency of polls */
671     int period;                 /*Time in minutes of data collection */
672
673     /*
674      * Are we doing one-shot measurements?
675      */
676     if (a_s->parms[P_ONESHOT].items != 0)
677         one_shot = 1;
678
679     /*
680      * Are we doing debugging output?
681      */
682     if (a_s->parms[P_DEBUG].items != 0)
683         debugging_on = 1;
684
685     /*
686      * Pull out the number of File Servers to watch and the number of
687      * collections to get.
688      */
689     numFSs = CountListItems(a_s->parms[P_FS_NAMES].items);
690     numCollIDs = CountListItems(a_s->parms[P_COLL_IDS].items);
691
692     /* Get the polling frequency */
693     if (a_s->parms[P_FREQUENCY].items != 0)
694         freq = atoi(a_s->parms[P_FREQUENCY].items->data);
695     else
696         freq = 30;              /* default to 30 seconds */
697
698     /* Get the time duration to run the tests */
699     if (a_s->parms[P_PERIOD].items != 0)
700         period = atoi(a_s->parms[P_PERIOD].items->data);
701     else
702         period = 10;            /* default to 10 minutes */
703
704
705     /*
706      * Fill in the socket array for each of the File Servers listed.
707      */
708     curr_item = a_s->parms[P_FS_NAMES].items;
709     for (currFS = 0; currFS < numFSs; currFS++) {
710 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
711         FSSktArray[currFS].sin_family = AF_INET;        /*Internet family */
712 #else
713         FSSktArray[currFS].sin_family = htons(AF_INET); /*Internet family */
714 #endif
715         FSSktArray[currFS].sin_port = htons(7000);      /*FileServer port */
716         he = hostutil_GetHostByName(curr_item->data);
717         if (he == NULL) {
718             fprintf(stderr, "[%s] Can't get host info for '%s'\n", rn,
719                     curr_item->data);
720             exit(-1);
721         }
722         memcpy(&(FSSktArray[currFS].sin_addr.s_addr), he->h_addr, 4);
723
724         /*
725          * Move to the next File Server name.
726          */
727         curr_item = curr_item->next;
728
729     }                           /*Get socket info for each File Server */
730
731     /*
732      * Create and fill up the array of desired collection IDs.
733      */
734     if (debugging_on)
735         printf("Allocating %d long(s) for coll ID\n", numCollIDs);
736     collIDP = (afs_int32 *) (malloc(numCollIDs * sizeof(afs_int32)));
737     currCollIDP = collIDP;
738     curr_item = a_s->parms[P_COLL_IDS].items;
739     for (currCollIDIdx = 0; currCollIDIdx < numCollIDs; currCollIDIdx++) {
740         *currCollIDP = (afs_int32) (atoi(curr_item->data));
741         if (debugging_on)
742             printf("CollID at index %d is %d\n", currCollIDIdx, *currCollIDP);
743         curr_item = curr_item->next;
744         currCollIDP++;
745     };
746
747     /*
748      * Crank up the File Server prober, then sit back and have fun.
749      */
750     printf("\nStarting up the xstat_fs service, ");
751     initFlags = 0;
752     if (debugging_on) {
753         initFlags |= XSTAT_FS_INITFLAG_DEBUGGING;
754         printf("debugging enabled, ");
755     } else
756         printf("no debugging, ");
757     if (one_shot) {
758         initFlags |= XSTAT_FS_INITFLAG_ONE_SHOT;
759         printf("one-shot operation\n");
760     } else
761         printf("continuous operation\n");
762
763     code = xstat_fs_Init(numFSs,        /*Num servers */
764                          FSSktArray,    /*File Server socket array */
765                          freq,  /*Probe frequency */
766                          FS_Handler,    /*Handler routine */
767                          initFlags,     /*Initialization flags */
768                          numCollIDs,    /*Number of collection IDs */
769                          collIDP);      /*Ptr to collection ID array */
770     if (code) {
771         fprintf(stderr, "[%s] Error returned by xstat_fs_Init: %d\n", rn,
772                 code);
773         xstat_fs_Cleanup(1);    /*Get rid of malloc'ed structures */
774         exit(-1);
775     }
776
777     if (one_shot) {
778         /*
779          * One-shot operation; just wait for the collection to be done.
780          */
781         if (debugging_on)
782             printf("[%s] Calling LWP_WaitProcess() on event %" AFS_PTR_FMT "\n", rn,
783                    &terminationEvent);
784         waitCode = LWP_WaitProcess(&terminationEvent);
785         if (debugging_on)
786             printf("[%s] Returned from LWP_WaitProcess()\n", rn);
787         if (waitCode) {
788             if (debugging_on)
789                 fprintf(stderr,
790                         "[%s] Error %d encountered by LWP_WaitProcess()\n",
791                         rn, waitCode);
792         }
793     } else {
794         /*
795          * Continuous operation.
796          */
797         sleep_secs = 60 * period;       /*length of data collection */
798         printf
799             ("xstat_fs service started, main thread sleeping for %d secs.\n",
800              sleep_secs);
801
802         /*
803          * Let's just fall asleep for a while, then we'll clean up.
804          */
805         tv.tv_sec = sleep_secs;
806         tv.tv_usec = 0;
807         code = IOMGR_Select(0,  /*Num fds */
808                             0,  /*Descriptors ready for reading */
809                             0,  /*Descriptors ready for writing */
810                             0,  /*Descriptors with exceptional conditions */
811                             &tv);       /*Timeout structure */
812         if (code) {
813             fprintf(stderr,
814                     "[%s] IOMGR_Select() returned non-zero value: %d\n", rn,
815                     code);
816         }
817     }
818
819     /*
820      * We're all done.  Clean up, put the last nail in Rx, then
821      * exit happily.
822      */
823     if (debugging_on)
824         printf("\nYawn, main thread just woke up.  Cleaning things out...\n");
825
826     code = xstat_fs_Cleanup(1); /*Get rid of malloc'ed data */
827     rx_Finalize();
828     return (0);
829 }
830
831
832 #include "AFS_component_version_number.c"
833
834 int
835 main(int argc, char **argv)
836 {
837     static char rn[] = "xstat_fs_test"; /*Routine name */
838     register afs_int32 code;    /*Return code */
839     struct cmd_syndesc *ts;     /*Ptr to cmd line syntax desc */
840
841     /*
842      * Set up the commands we understand.
843      */
844     ts = cmd_CreateSyntax("initcmd", RunTheTest, 0, "initialize the program");
845     cmd_AddParm(ts, "-fsname", CMD_LIST, CMD_REQUIRED,
846                 "File Server name(s) to monitor");
847     cmd_AddParm(ts, "-collID", CMD_LIST, CMD_REQUIRED,
848                 "Collection(s) to fetch");
849     cmd_AddParm(ts, "-onceonly", CMD_FLAG, CMD_OPTIONAL,
850                 "Collect results exactly once, then quit");
851     cmd_AddParm(ts, "-frequency", CMD_SINGLE, CMD_OPTIONAL,
852                 "poll frequency, in seconds");
853     cmd_AddParm(ts, "-period", CMD_SINGLE, CMD_OPTIONAL,
854                 "data collection time, in minutes");
855     cmd_AddParm(ts, "-debug", CMD_FLAG, CMD_OPTIONAL,
856                 "turn on debugging output");
857
858     /*
859      * Parse command-line switches & execute the test, then get the
860      * heck out of here.
861      */
862     code = cmd_Dispatch(argc, argv);
863     if (code) {
864         fprintf(stderr, "[%s] Call to cmd_Dispatch() failed; code is %d\n",
865                 rn, code);
866     }
867
868     exit(code);
869 }