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