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