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