convert xstat and friends to pthreads
[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 #include <roken.h>
20
21 #include "xstat_fs.h"           /*Interface for xstat_fs module */
22 #include <afs/cmd.h>            /*Command line interpreter */
23 #include <afs/afsutil.h>
24 #include <opr/softsig.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     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 %u, %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[%u entries returned at %" AFS_PTR_FMT "]\n\n", numInt32s, currInt32);
127
128     for (i = 0; i < numInt32s; i++)
129         printf("%u ", *currInt32++);
130     printf("\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%10u numPerfCalls\n\n", a_ovP->numPerfCalls);
158
159     /*
160      * Vnode cache section.
161      */
162     printf("\t%10u vcache_L_Entries\n", a_ovP->vcache_L_Entries);
163     printf("\t%10u vcache_L_Allocs\n", a_ovP->vcache_L_Allocs);
164     printf("\t%10u vcache_L_Gets\n", a_ovP->vcache_L_Gets);
165     printf("\t%10u vcache_L_Reads\n", a_ovP->vcache_L_Reads);
166     printf("\t%10u vcache_L_Writes\n\n", a_ovP->vcache_L_Writes);
167
168     printf("\t%10u vcache_S_Entries\n", a_ovP->vcache_S_Entries);
169     printf("\t%10u vcache_S_Allocs\n", a_ovP->vcache_S_Allocs);
170     printf("\t%10u vcache_S_Gets\n", a_ovP->vcache_S_Gets);
171     printf("\t%10u vcache_S_Reads\n", a_ovP->vcache_S_Reads);
172     printf("\t%10u vcache_S_Writes\n\n", a_ovP->vcache_S_Writes);
173
174     printf("\t%10u vcache_H_Entries\n", a_ovP->vcache_H_Entries);
175     printf("\t%10u vcache_H_Gets\n", a_ovP->vcache_H_Gets);
176     printf("\t%10u vcache_H_Replacements\n\n", a_ovP->vcache_H_Replacements);
177
178     /*
179      * Directory package section.
180      */
181     printf("\t%10u dir_Buffers\n", a_ovP->dir_Buffers);
182     printf("\t%10u dir_Calls\n", a_ovP->dir_Calls);
183     printf("\t%10u dir_IOs\n\n", a_ovP->dir_IOs);
184
185     /*
186      * Rx section.
187      */
188     printf("\t%10u rx_packetRequests\n", a_ovP->rx_packetRequests);
189     printf("\t%10u rx_noPackets_RcvClass\n", a_ovP->rx_noPackets_RcvClass);
190     printf("\t%10u rx_noPackets_SendClass\n", a_ovP->rx_noPackets_SendClass);
191     printf("\t%10u rx_noPackets_SpecialClass\n",
192            a_ovP->rx_noPackets_SpecialClass);
193     printf("\t%10u rx_socketGreedy\n", a_ovP->rx_socketGreedy);
194     printf("\t%10u rx_bogusPacketOnRead\n", a_ovP->rx_bogusPacketOnRead);
195     printf("\t%10u rx_bogusHost\n", a_ovP->rx_bogusHost);
196     printf("\t%10u rx_noPacketOnRead\n", a_ovP->rx_noPacketOnRead);
197     printf("\t%10u rx_noPacketBuffersOnRead\n",
198            a_ovP->rx_noPacketBuffersOnRead);
199     printf("\t%10u rx_selects\n", a_ovP->rx_selects);
200     printf("\t%10u rx_sendSelects\n", a_ovP->rx_sendSelects);
201     printf("\t%10u rx_packetsRead_RcvClass\n",
202            a_ovP->rx_packetsRead_RcvClass);
203     printf("\t%10u rx_packetsRead_SendClass\n",
204            a_ovP->rx_packetsRead_SendClass);
205     printf("\t%10u rx_packetsRead_SpecialClass\n",
206            a_ovP->rx_packetsRead_SpecialClass);
207     printf("\t%10u rx_dataPacketsRead\n", a_ovP->rx_dataPacketsRead);
208     printf("\t%10u rx_ackPacketsRead\n", a_ovP->rx_ackPacketsRead);
209     printf("\t%10u rx_dupPacketsRead\n", a_ovP->rx_dupPacketsRead);
210     printf("\t%10u rx_spuriousPacketsRead\n", a_ovP->rx_spuriousPacketsRead);
211     printf("\t%10u rx_packetsSent_RcvClass\n",
212            a_ovP->rx_packetsSent_RcvClass);
213     printf("\t%10u rx_packetsSent_SendClass\n",
214            a_ovP->rx_packetsSent_SendClass);
215     printf("\t%10u rx_packetsSent_SpecialClass\n",
216            a_ovP->rx_packetsSent_SpecialClass);
217     printf("\t%10u rx_ackPacketsSent\n", a_ovP->rx_ackPacketsSent);
218     printf("\t%10u rx_pingPacketsSent\n", a_ovP->rx_pingPacketsSent);
219     printf("\t%10u rx_abortPacketsSent\n", a_ovP->rx_abortPacketsSent);
220     printf("\t%10u rx_busyPacketsSent\n", a_ovP->rx_busyPacketsSent);
221     printf("\t%10u rx_dataPacketsSent\n", a_ovP->rx_dataPacketsSent);
222     printf("\t%10u rx_dataPacketsReSent\n", a_ovP->rx_dataPacketsReSent);
223     printf("\t%10u rx_dataPacketsPushed\n", a_ovP->rx_dataPacketsPushed);
224     printf("\t%10u rx_ignoreAckedPacket\n", a_ovP->rx_ignoreAckedPacket);
225     printf("\t%10u rx_totalRtt_Sec\n", a_ovP->rx_totalRtt_Sec);
226     printf("\t%10u rx_totalRtt_Usec\n", a_ovP->rx_totalRtt_Usec);
227     printf("\t%10u rx_minRtt_Sec\n", a_ovP->rx_minRtt_Sec);
228     printf("\t%10u rx_minRtt_Usec\n", a_ovP->rx_minRtt_Usec);
229     printf("\t%10u rx_maxRtt_Sec\n", a_ovP->rx_maxRtt_Sec);
230     printf("\t%10u rx_maxRtt_Usec\n", a_ovP->rx_maxRtt_Usec);
231     printf("\t%10u rx_nRttSamples\n", a_ovP->rx_nRttSamples);
232     printf("\t%10u rx_nServerConns\n", a_ovP->rx_nServerConns);
233     printf("\t%10u rx_nClientConns\n", a_ovP->rx_nClientConns);
234     printf("\t%10u rx_nPeerStructs\n", a_ovP->rx_nPeerStructs);
235     printf("\t%10u rx_nCallStructs\n", a_ovP->rx_nCallStructs);
236     printf("\t%10u rx_nFreeCallStructs\n", a_ovP->rx_nFreeCallStructs);
237     printf("\t%10u rx_nBusies\n\n", a_ovP->rx_nBusies);
238
239     printf("\t%10u fs_nBusies\n", a_ovP->fs_nBusies);
240     printf("\t%10u fs_GetCapabilities\n\n", a_ovP->fs_nGetCaps);
241     /*
242      * Host module fields.
243      */
244     printf("\t%10u host_NumHostEntries\n", a_ovP->host_NumHostEntries);
245     printf("\t%10u host_HostBlocks\n", a_ovP->host_HostBlocks);
246     printf("\t%10u host_NonDeletedHosts\n", a_ovP->host_NonDeletedHosts);
247     printf("\t%10u host_HostsInSameNetOrSubnet\n",
248            a_ovP->host_HostsInSameNetOrSubnet);
249     printf("\t%10u host_HostsInDiffSubnet\n", a_ovP->host_HostsInDiffSubnet);
250     printf("\t%10u host_HostsInDiffNetwork\n",
251            a_ovP->host_HostsInDiffNetwork);
252     printf("\t%10u host_NumClients\n", a_ovP->host_NumClients);
253     printf("\t%10u host_ClientBlocks\n\n", a_ovP->host_ClientBlocks);
254
255     printf("\t%10u 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     printf
283         ("%15s: %u ops (%u OK); sum=%lu.%06lu, sqr=%lu.%06lu, min=%lu.%06lu, max=%lu.%06lu\n",
284          opNames[a_opIdx], a_opTimeP->numOps, a_opTimeP->numSuccesses,
285          (long)a_opTimeP->sumTime.tv_sec, (long)a_opTimeP->sumTime.tv_usec,
286          (long)a_opTimeP->sqrTime.tv_sec, (long)a_opTimeP->sqrTime.tv_usec,
287          (long)a_opTimeP->minTime.tv_sec, (long)a_opTimeP->minTime.tv_usec,
288          (long)a_opTimeP->maxTime.tv_sec, (long)a_opTimeP->maxTime.tv_usec);
289 }
290
291
292 /*------------------------------------------------------------------------
293  * PrintXferTiming
294  *
295  * Description:
296  *      Print out the contents of a data transfer structure.
297  *
298  * Arguments:
299  *      a_opIdx : Index of the AFS operation we're printing number on.
300  *      a_xferP : Ptr to the data transfer structure to print.
301  *
302  * Returns:
303  *      Nothing.
304  *
305  * Environment:
306  *      Nothing interesting.
307  *
308  * Side Effects:
309  *      As advertised.
310  *------------------------------------------------------------------------*/
311
312 void
313 PrintXferTiming(int a_opIdx, struct fs_stats_xferData *a_xferP)
314 {
315     printf
316         ("%s: %u xfers (%u OK), time sum=%lu.%06lu, sqr=%lu.%06lu, min=%lu.%06lu, max=%lu.%06lu\n",
317          xferOpNames[a_opIdx], a_xferP->numXfers, a_xferP->numSuccesses,
318          (long)a_xferP->sumTime.tv_sec, (long)a_xferP->sumTime.tv_usec,
319          (long)a_xferP->sqrTime.tv_sec, (long)a_xferP->sqrTime.tv_usec,
320          (long)a_xferP->minTime.tv_sec, (long)a_xferP->minTime.tv_usec,
321          (long)a_xferP->maxTime.tv_sec, (long)a_xferP->maxTime.tv_usec);
322     printf("\t[bytes: sum=%u, min=%u, max=%u]\n", a_xferP->sumBytes,
323            a_xferP->minBytes, a_xferP->maxBytes);
324     printf
325         ("\t[buckets: 0: %u, 1: %u, 2: %u, 3: %u, 4: %u, 5: %u, 6: %u, 7: %u, 8: %u]\n",
326          a_xferP->count[0], a_xferP->count[1], a_xferP->count[2],
327          a_xferP->count[3], a_xferP->count[4], a_xferP->count[5],
328          a_xferP->count[6], a_xferP->count[7], a_xferP->count[8]);
329 }
330
331
332 /*------------------------------------------------------------------------
333  * PrintDetailedPerfInfo
334  *
335  * Description:
336  *      Print out a set of detailed performance numbers.
337  *
338  * Arguments:
339  *      a_detP : Ptr to detailed perf numbers to print.
340  *
341  * Returns:
342  *      Nothing.
343  *
344  * Environment:
345  *      Nothing interesting.
346  *
347  * Side Effects:
348  *      As advertised.
349  *------------------------------------------------------------------------*/
350
351 void
352 PrintDetailedPerfInfo(struct fs_stats_DetailedStats *a_detP)
353 {
354     int currIdx;                /*Loop variable */
355
356     printf("\t%10lu epoch\n", (long) a_detP->epoch.tv_sec);
357
358     for (currIdx = 0; currIdx < FS_STATS_NUM_RPC_OPS; currIdx++)
359         PrintOpTiming(currIdx, &(a_detP->rpcOpTimes[currIdx]));
360
361     for (currIdx = 0; currIdx < FS_STATS_NUM_XFER_OPS; currIdx++)
362         PrintXferTiming(currIdx, &(a_detP->xferOpTimes[currIdx]));
363 }
364
365
366 /*------------------------------------------------------------------------
367  * PrintFullPerfInfo
368  *
369  * Description:
370  *      Print out the AFS_XSTATSCOLL_FULL_PERF_INFO collection we just
371  *      received.
372  *
373  * Arguments:
374  *      None.
375  *
376  * Returns:
377  *      Nothing.
378  *
379  * Environment:
380  *      All the info we need is nestled into xstat_fs_Results.
381  *
382  * Side Effects:
383  *      As advertised.
384  *------------------------------------------------------------------------*/
385
386 void
387 PrintFullPerfInfo(void)
388 {
389     int code;
390     struct fs_stats_FullPerfStats *fullPerfP;   /*Ptr to full perf stats */
391     struct fs_stats_FullPerfStats buffer;  /* to decode the stats */
392     char *printableTime;        /*Ptr to printable time
393                                  * string */
394     time_t probeTime = xstat_fs_Results.probeTime;
395     static afs_int32 fullPerfInt32s = (sizeof(struct fs_stats_FullPerfStats) >> 2);     /*Correct # int32s to rcv */
396
397     printableTime = ctime(&probeTime);
398     printableTime[strlen(printableTime) - 1] = '\0';
399     printf
400         ("AFS_XSTATSCOLL_FULL_PERF_INFO (coll %d) for FS %s\n[Probe %u, %s]\n\n",
401          xstat_fs_Results.collectionNumber, xstat_fs_Results.connP->hostName,
402          xstat_fs_Results.probeNum, printableTime);
403
404     code =
405         xstat_fs_DecodeFullPerfStats(&fullPerfP,
406                                      xstat_fs_Results.data.AFS_CollData_val,
407                                      xstat_fs_Results.data.AFS_CollData_len,
408                                      &buffer);
409     if (code) {
410         afs_int32 numInt32s = xstat_fs_Results.data.AFS_CollData_len;   /*# int32words received */
411         printf("** Data size mismatch in full performance collection!\n");
412         printf("** Expecting %u, got %u\n", fullPerfInt32s, numInt32s);
413     } else {
414         PrintOverallPerfInfo(&(fullPerfP->overall));
415         PrintDetailedPerfInfo(&(fullPerfP->det));
416     }
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     if (debugging_on) {
541         int i;
542         int numInt32s = xstat_fs_Results.data.AFS_CollData_len;
543         afs_int32 *entry = xstat_fs_Results.data.AFS_CollData_val;
544
545         printf("debug: got collection number %d\n", xstat_fs_Results.collectionNumber);
546         printf("debug: collection data length is %d\n", numInt32s);
547         for (i = 0; i < numInt32s; i++) {
548             printf("debug: entry %d %u\n", i, entry[i]);
549         }
550         printf("\n");
551     }
552
553     switch (xstat_fs_Results.collectionNumber) {
554     case AFS_XSTATSCOLL_CALL_INFO:
555         PrintCallInfo();
556         break;
557
558     case AFS_XSTATSCOLL_PERF_INFO:
559         PrintPerfInfo();
560         break;
561
562     case AFS_XSTATSCOLL_FULL_PERF_INFO:
563         PrintFullPerfInfo();
564         break;
565
566     case AFS_XSTATSCOLL_CBSTATS:
567         PrintCbCounters();
568         break;
569
570     default:
571         printf("** Unknown collection: %d\n",
572                xstat_fs_Results.collectionNumber);
573     }
574
575     /*
576      * Return the happy news.
577      */
578     return (0);
579 }
580
581
582 /*------------------------------------------------------------------------
583  * CountListItems
584  *
585  * Description:
586  *      Given a pointer to the list of File Servers we'll be polling
587  *      (or, in fact, any list at all), compute the length of the list.
588  *
589  * Arguments:
590  *      struct cmd_item *a_firstItem : Ptr to first item in list.
591  *
592  * Returns:
593  *      Length of the above list.
594  *
595  * Environment:
596  *      Nothing interesting.
597  *
598  * Side Effects:
599  *      As advertised.
600  *------------------------------------------------------------------------*/
601
602 static int
603 CountListItems(struct cmd_item *a_firstItem)
604 {
605
606     int list_len;               /*List length */
607     struct cmd_item *curr_item; /*Ptr to current item */
608
609     list_len = 0;
610     curr_item = a_firstItem;
611
612     /*
613      * Count 'em up.
614      */
615     while (curr_item) {
616         list_len++;
617         curr_item = curr_item->next;
618     }
619
620     /*
621      * Return our tally.
622      */
623     return (list_len);
624 }
625
626
627 /*------------------------------------------------------------------------
628  * RunTheTest
629  *
630  * Description:
631  *      Routine called by the command line interpreter to execute the
632  *      meat of the program.  We count the number of File Servers
633  *      to watch, allocate enough space to remember all the connection
634  *      info for them, then go for it.
635  *
636  *
637  * Arguments:
638  *      a_s : Ptr to the command line syntax descriptor.
639  *
640  * Returns:
641  *      0, but may exit the whole program on an error!
642  *
643  * Environment:
644  *      Nothing interesting.
645  *
646  * Side Effects:
647  *      As advertised.
648  *------------------------------------------------------------------------*/
649
650 int
651 RunTheTest(struct cmd_syndesc *a_s, void *dummy)
652 {
653     static char rn[] = "RunTheTest";    /*Routine name */
654     int code;                   /*Return code */
655     int numFSs;                 /*# File Servers to monitor */
656     int numCollIDs;             /*# collections to fetch */
657     int currFS;                 /*Loop index */
658     int currCollIDIdx;          /*Index of current collection ID */
659     afs_int32 *collIDP;         /*Ptr to array of collection IDs */
660     afs_int32 *currCollIDP;     /*Ptr to current collection ID */
661     struct cmd_item *curr_item; /*Current FS cmd line record */
662     struct sockaddr_in FSSktArray[20];  /*File Server socket array - FIX! */
663     struct hostent *he;         /*Host entry */
664     int initFlags;              /*Flags passed to the init fcn */
665     int freq;                   /*Frequency of polls */
666     int period;                 /*Time in minutes of data collection */
667
668     opr_softsig_Init();
669
670     /*
671      * Are we doing one-shot measurements?
672      */
673     if (a_s->parms[P_ONESHOT].items != 0)
674         one_shot = 1;
675
676     /*
677      * Are we doing debugging output?
678      */
679     if (a_s->parms[P_DEBUG].items != 0)
680         debugging_on = 1;
681
682     /*
683      * Pull out the number of File Servers to watch and the number of
684      * collections to get.
685      */
686     numFSs = CountListItems(a_s->parms[P_FS_NAMES].items);
687     numCollIDs = CountListItems(a_s->parms[P_COLL_IDS].items);
688
689     /* Get the polling frequency */
690     if (a_s->parms[P_FREQUENCY].items != 0)
691         freq = atoi(a_s->parms[P_FREQUENCY].items->data);
692     else
693         freq = 30;              /* default to 30 seconds */
694
695     /* Get the time duration to run the tests */
696     if (a_s->parms[P_PERIOD].items != 0)
697         period = atoi(a_s->parms[P_PERIOD].items->data);
698     else
699         period = 10;            /* default to 10 minutes */
700
701
702     /*
703      * Fill in the socket array for each of the File Servers listed.
704      */
705     curr_item = a_s->parms[P_FS_NAMES].items;
706     for (currFS = 0; currFS < numFSs; currFS++) {
707         FSSktArray[currFS].sin_family = AF_INET;
708         FSSktArray[currFS].sin_port = htons(7000);      /* FileServer port */
709         he = hostutil_GetHostByName(curr_item->data);
710         if (he == NULL) {
711             fprintf(stderr, "[%s] Can't get host info for '%s'\n", rn,
712                     curr_item->data);
713             exit(-1);
714         }
715         memcpy(&(FSSktArray[currFS].sin_addr.s_addr), he->h_addr, 4);
716
717         /*
718          * Move to the next File Server name.
719          */
720         curr_item = curr_item->next;
721
722     }                           /*Get socket info for each File Server */
723
724     /*
725      * Create and fill up the array of desired collection IDs.
726      */
727     if (debugging_on)
728         printf("Allocating %d long(s) for coll ID\n", numCollIDs);
729
730     if (numCollIDs > 0)
731         collIDP = calloc(numCollIDs, sizeof(afs_int32));
732     else
733         collIDP = NULL;
734
735     currCollIDP = collIDP;
736     curr_item = a_s->parms[P_COLL_IDS].items;
737     for (currCollIDIdx = 0; currCollIDIdx < numCollIDs; currCollIDIdx++) {
738         *currCollIDP = (afs_int32) (atoi(curr_item->data));
739         if (debugging_on)
740             printf("CollID at index %d is %d\n", currCollIDIdx, *currCollIDP);
741         curr_item = curr_item->next;
742         currCollIDP++;
743     };
744
745     /*
746      * Crank up the File Server prober, then sit back and have fun.
747      */
748     printf("\nStarting up the xstat_fs service, ");
749     initFlags = 0;
750     if (debugging_on) {
751         initFlags |= XSTAT_FS_INITFLAG_DEBUGGING;
752         printf("debugging enabled, ");
753     } else
754         printf("no debugging, ");
755     if (one_shot) {
756         initFlags |= XSTAT_FS_INITFLAG_ONE_SHOT;
757         printf("one-shot operation\n");
758     } else
759         printf("continuous operation\n");
760
761     code = xstat_fs_Init(numFSs,        /*Num servers */
762                          FSSktArray,    /*File Server socket array */
763                          freq,  /*Probe frequency */
764                          FS_Handler,    /*Handler routine */
765                          initFlags,     /*Initialization flags */
766                          numCollIDs,    /*Number of collection IDs */
767                          collIDP);      /*Ptr to collection ID array */
768     if (code) {
769         fprintf(stderr, "[%s] Error returned by xstat_fs_Init: %d\n", rn,
770                 code);
771         xstat_fs_Cleanup(1);    /*Get rid of malloc'ed structures */
772         exit(-1);
773     }
774
775     /* Wait for the collection complete. */
776     xstat_fs_Wait(60 * period);
777
778     /*
779      * We're all done.  Clean up, put the last nail in Rx, then
780      * exit happily.
781      */
782     if (debugging_on)
783         printf("\nYawn, main thread just woke up.  Cleaning things out...\n");
784
785     xstat_fs_Cleanup(1);        /*Get rid of malloc'ed data */
786     rx_Finalize();
787     return (0);
788 }
789
790
791 #include "AFS_component_version_number.c"
792
793 int
794 main(int argc, char **argv)
795 {
796     static char rn[] = "xstat_fs_test"; /*Routine name */
797     afs_int32 code;     /*Return code */
798     struct cmd_syndesc *ts;     /*Ptr to cmd line syntax desc */
799
800     /*
801      * Set up the commands we understand.
802      */
803     ts = cmd_CreateSyntax("initcmd", RunTheTest, 0, 0, "initialize the program");
804     cmd_AddParm(ts, "-fsname", CMD_LIST, CMD_REQUIRED,
805                 "File Server name(s) to monitor");
806     cmd_AddParm(ts, "-collID", CMD_LIST, CMD_REQUIRED,
807                 "Collection(s) to fetch");
808     cmd_AddParm(ts, "-onceonly", CMD_FLAG, CMD_OPTIONAL,
809                 "Collect results exactly once, then quit");
810     cmd_AddParm(ts, "-frequency", CMD_SINGLE, CMD_OPTIONAL,
811                 "poll frequency, in seconds");
812     cmd_AddParm(ts, "-period", CMD_SINGLE, CMD_OPTIONAL,
813                 "data collection time, in minutes");
814     cmd_AddParm(ts, "-debug", CMD_FLAG, CMD_OPTIONAL,
815                 "turn on debugging output");
816
817     /*
818      * Parse command-line switches & execute the test, then get the
819      * heck out of here.
820      */
821     code = cmd_Dispatch(argc, argv);
822     if (code) {
823         fprintf(stderr, "[%s] Call to cmd_Dispatch() failed; code is %d\n",
824                 rn, code);
825     }
826
827     exit(code);
828 }