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