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