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