sin_family is not network order
[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 <afs/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     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     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: %u ops (%u OK); sum=%lu.%06lu, sqr=%lu.%06lu, min=%lu.%06lu, max=%lu.%06lu\n",
292          opNames[a_opIdx], a_opTimeP->numOps, a_opTimeP->numSuccesses,
293          (long)a_opTimeP->sumTime.tv_sec, (long)a_opTimeP->sumTime.tv_usec,
294          (long)a_opTimeP->sqrTime.tv_sec, (long)a_opTimeP->sqrTime.tv_usec,
295          (long)a_opTimeP->minTime.tv_sec, (long)a_opTimeP->minTime.tv_usec,
296          (long)a_opTimeP->maxTime.tv_sec, (long)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: %u xfers (%u OK), time sum=%lu.%06lu, sqr=%lu.%06lu, min=%lu.%06lu, max=%lu.%06lu\n",
333          xferOpNames[a_opIdx], a_xferP->numXfers, a_xferP->numSuccesses,
334          (long)a_xferP->sumTime.tv_sec, (long)a_xferP->sumTime.tv_usec,
335          (long)a_xferP->sqrTime.tv_sec, (long)a_xferP->sqrTime.tv_usec,
336          (long)a_xferP->minTime.tv_sec, (long)a_xferP->minTime.tv_usec,
337          (long)a_xferP->maxTime.tv_sec, (long)a_xferP->maxTime.tv_usec);
338     printf("\t[bytes: sum=%u, min=%u, max=%u]\n", a_xferP->sumBytes,
339            a_xferP->minBytes, a_xferP->maxBytes);
340     printf
341         ("\t[buckets: 0: %u, 1: %u, 2: %u, 3: %u, 4: %u, 5: %u, 6: %u, 7: %u, 8: %u]\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%10lu epoch\n", (long) a_detP->epoch.tv_sec);
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 %u, got %u\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 %u, %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 %u, got %u\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 %u, %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_int32 *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 %u 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         FSSktArray[currFS].sin_family = AF_INET;
711         FSSktArray[currFS].sin_port = htons(7000);      /* FileServer port */
712         he = hostutil_GetHostByName(curr_item->data);
713         if (he == NULL) {
714             fprintf(stderr, "[%s] Can't get host info for '%s'\n", rn,
715                     curr_item->data);
716             exit(-1);
717         }
718         memcpy(&(FSSktArray[currFS].sin_addr.s_addr), he->h_addr, 4);
719
720         /*
721          * Move to the next File Server name.
722          */
723         curr_item = curr_item->next;
724
725     }                           /*Get socket info for each File Server */
726
727     /*
728      * Create and fill up the array of desired collection IDs.
729      */
730     if (debugging_on)
731         printf("Allocating %d long(s) for coll ID\n", numCollIDs);
732     collIDP = (afs_int32 *) (malloc(numCollIDs * sizeof(afs_int32)));
733     currCollIDP = collIDP;
734     curr_item = a_s->parms[P_COLL_IDS].items;
735     for (currCollIDIdx = 0; currCollIDIdx < numCollIDs; currCollIDIdx++) {
736         *currCollIDP = (afs_int32) (atoi(curr_item->data));
737         if (debugging_on)
738             printf("CollID at index %d is %d\n", currCollIDIdx, *currCollIDP);
739         curr_item = curr_item->next;
740         currCollIDP++;
741     };
742
743     /*
744      * Crank up the File Server prober, then sit back and have fun.
745      */
746     printf("\nStarting up the xstat_fs service, ");
747     initFlags = 0;
748     if (debugging_on) {
749         initFlags |= XSTAT_FS_INITFLAG_DEBUGGING;
750         printf("debugging enabled, ");
751     } else
752         printf("no debugging, ");
753     if (one_shot) {
754         initFlags |= XSTAT_FS_INITFLAG_ONE_SHOT;
755         printf("one-shot operation\n");
756     } else
757         printf("continuous operation\n");
758
759     code = xstat_fs_Init(numFSs,        /*Num servers */
760                          FSSktArray,    /*File Server socket array */
761                          freq,  /*Probe frequency */
762                          FS_Handler,    /*Handler routine */
763                          initFlags,     /*Initialization flags */
764                          numCollIDs,    /*Number of collection IDs */
765                          collIDP);      /*Ptr to collection ID array */
766     if (code) {
767         fprintf(stderr, "[%s] Error returned by xstat_fs_Init: %d\n", rn,
768                 code);
769         xstat_fs_Cleanup(1);    /*Get rid of malloc'ed structures */
770         exit(-1);
771     }
772
773     if (one_shot) {
774         /*
775          * One-shot operation; just wait for the collection to be done.
776          */
777         if (debugging_on)
778             printf("[%s] Calling LWP_WaitProcess() on event %" AFS_PTR_FMT "\n", rn,
779                    &terminationEvent);
780         waitCode = LWP_WaitProcess(&terminationEvent);
781         if (debugging_on)
782             printf("[%s] Returned from LWP_WaitProcess()\n", rn);
783         if (waitCode) {
784             if (debugging_on)
785                 fprintf(stderr,
786                         "[%s] Error %d encountered by LWP_WaitProcess()\n",
787                         rn, waitCode);
788         }
789     } else {
790         /*
791          * Continuous operation.
792          */
793         sleep_secs = 60 * period;       /*length of data collection */
794         printf
795             ("xstat_fs service started, main thread sleeping for %d secs.\n",
796              sleep_secs);
797
798         /*
799          * Let's just fall asleep for a while, then we'll clean up.
800          */
801         tv.tv_sec = sleep_secs;
802         tv.tv_usec = 0;
803         code = IOMGR_Select(0,  /*Num fds */
804                             0,  /*Descriptors ready for reading */
805                             0,  /*Descriptors ready for writing */
806                             0,  /*Descriptors with exceptional conditions */
807                             &tv);       /*Timeout structure */
808         if (code) {
809             fprintf(stderr,
810                     "[%s] IOMGR_Select() returned non-zero value: %d\n", rn,
811                     code);
812         }
813     }
814
815     /*
816      * We're all done.  Clean up, put the last nail in Rx, then
817      * exit happily.
818      */
819     if (debugging_on)
820         printf("\nYawn, main thread just woke up.  Cleaning things out...\n");
821
822     code = xstat_fs_Cleanup(1); /*Get rid of malloc'ed data */
823     rx_Finalize();
824     return (0);
825 }
826
827
828 #include "AFS_component_version_number.c"
829
830 int
831 main(int argc, char **argv)
832 {
833     static char rn[] = "xstat_fs_test"; /*Routine name */
834     afs_int32 code;     /*Return code */
835     struct cmd_syndesc *ts;     /*Ptr to cmd line syntax desc */
836
837     /*
838      * Set up the commands we understand.
839      */
840     ts = cmd_CreateSyntax("initcmd", RunTheTest, 0, "initialize the program");
841     cmd_AddParm(ts, "-fsname", CMD_LIST, CMD_REQUIRED,
842                 "File Server name(s) to monitor");
843     cmd_AddParm(ts, "-collID", CMD_LIST, CMD_REQUIRED,
844                 "Collection(s) to fetch");
845     cmd_AddParm(ts, "-onceonly", CMD_FLAG, CMD_OPTIONAL,
846                 "Collect results exactly once, then quit");
847     cmd_AddParm(ts, "-frequency", CMD_SINGLE, CMD_OPTIONAL,
848                 "poll frequency, in seconds");
849     cmd_AddParm(ts, "-period", CMD_SINGLE, CMD_OPTIONAL,
850                 "data collection time, in minutes");
851     cmd_AddParm(ts, "-debug", CMD_FLAG, CMD_OPTIONAL,
852                 "turn on debugging output");
853
854     /*
855      * Parse command-line switches & execute the test, then get the
856      * heck out of here.
857      */
858     code = cmd_Dispatch(argc, argv);
859     if (code) {
860         fprintf(stderr, "[%s] Call to cmd_Dispatch() failed; code is %d\n",
861                 rn, code);
862     }
863
864     exit(code);
865 }