make vfs & filesystems use failable copying
[minix3.git] / test / ipc / lib / parse_opts.c
blob89f4b07b79716a79172752ca6827a8793f5bbf27
1 /*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
26 * http://www.sgi.com
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
33 /**********************************************************
35 * OS Testing - Silicon Graphics, Inc.
37 * FUNCTION NAME : parse_opts
39 * FUNCTION TITLE : parse standard & user options for system call tests
41 * SYNOPSIS:
42 * #include "usctest.h"
44 * char *parse_opts(ac, av, user_optarr, uhf)
45 * int ac;
46 * char **av;
47 * option_t user_optarr[];
48 * void (*uhf)();
50 * AUTHOR : William Roske/Richard Logan
52 * INITIAL RELEASE : UNICOS 7.0
54 * DESCRIPTION
55 * The parse_opts library routine takes that argc and argv parameters
56 * recevied by main() and an array of structures defining user options.
57 * It parses the command line setting flag and argument locations
58 * associated with the options. It uses getopt to do the actual cmd line
59 * parsing. uhf() is a function to print user define help
61 * This module contains the functions usc_global_setup_hook and
62 * usc_test_looping, which are called by marcos defined in usctest.h.
64 * RETURN VALUE
65 * parse_opts returns a pointer to an error message if an error occurs.
66 * This pointer is (char *)NULL if parsing is successful.
68 *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/
69 #include <errno.h>
70 #include <stdlib.h>
71 #include <string.h>
72 #include <sys/param.h>
73 #include <sys/signal.h>
74 #include <sys/types.h>
75 #include <unistd.h>
76 #include <sys/time.h>
79 #if UNIT_TEST
80 #include <time.h>
81 #endif /* UNIT_TEST */
83 #include "test.h"
84 #define _USC_LIB_ 1 /* indicates we are the library to the usctest.h include */
85 #include "usctest.h"
87 #ifndef USC_COPIES
88 #define USC_COPIES "USC_COPIES"
89 #endif
91 #ifndef UNIT_TEST
92 #define UNIT_TEST 0
93 #endif
95 #ifndef DEBUG
96 #define DEBUG 0
97 #endif
99 /* The timing information block. */
100 struct tblock tblock={0,((long) -1)>>1,0,0};
103 /* Define flags and args for standard options */
104 int STD_FUNCTIONAL_TEST=1, /* flag indicating to do functional testing code */
105 STD_TIMING_ON=0, /* flag indicating to print timing stats */
106 STD_PAUSE=0, /* flag indicating to pause before actual start, */
107 /* for contention mode */
108 STD_INFINITE=0, /* flag indciating to loop forever */
109 STD_LOOP_COUNT=1, /* number of iterations */
110 STD_COPIES=1, /* number of copies */
111 STD_ERRNO_LOG=0; /* flag indicating to do errno logging */
113 float STD_LOOP_DURATION=0.0, /* duration value in fractional seconds */
114 STD_LOOP_DELAY=0.0; /* loop delay value in fractional seconds */
117 char **STD_opt_arr = NULL; /* array of option strings */
118 int STD_nopts=0, /* number of elements in STD_opt_arr */
119 STD_argind=1; /* argv index to next argv element */
120 /* (first argument) */
121 /* To getopt users, it is like optind */
124 * The following variables are to support system testing additions.
126 static int STD_TP_barrier=0; /* flag to do barrier in TEST_PAUSE */
127 /* 2 - wait_barrier(), 3 - set_barrier(), * - barrier() */
128 static int STD_LP_barrier=0; /* flag to do barrier in TEST_LOOPING */
129 /* 2 - wait_barrier(), 3 - set_barrier(), * - barrier() */
130 static int STD_TP_shmem_sz=0; /* shmalloc this many words per pe in TEST_PAUSE */
131 static int STD_LD_shmem=0; /* flag to do shmem_puts and shmem_gets during delay */
132 static int STD_LP_shmem=0; /* flag to do shmem_puts and gets during TEST_LOOPING */
133 static int STD_LD_recfun=0; /* do recressive function calls in loop delay */
134 static int STD_LP_recfun=0; /* do recressive function calls in TEST_LOOPING */
135 static int STD_TP_sbrk=0; /* do sbrk in TEST_PAUSE */
136 static int STD_LP_sbrk=0; /* do sbrk in TEST_LOOPING */
137 static char *STD_start_break=0; /* original sbrk size */
138 static int Debug=0;
140 struct std_option_t {
141 char *optstr;
142 char *help;
143 char *flag;
144 char **arg;
145 } std_options[] = {
146 { "c:", " -c n Run n copies concurrently\n", NULL, NULL},
147 { "e" , " -e Turn on errno logging\n", NULL, NULL},
148 { "f" , " -f Turn off functional testing\n", NULL, NULL},
149 { "h" , " -h Show this help screen\n", NULL, NULL},
150 { "i:", " -i n Execute test n times\n", NULL, NULL},
151 { "I:", " -I x Execute test for x seconds\n", NULL, NULL},
152 { "p" , " -p Pause for SIGUSR1 before starting\n", NULL, NULL},
153 { "P:", " -P x Pause for x seconds between iterations\n", NULL, NULL},
154 { "t" , " -t Turn on syscall timing\n", NULL, NULL},
155 #ifdef UCLINUX
156 { "C:", " -C ARG Run the child process with arguments ARG (for internal use)\n",
157 NULL, NULL},
158 #endif
159 {NULL, NULL, NULL, NULL}};
161 void print_help(void (*user_help)(void));
164 * Structure for usc_recressive_func argument
166 struct usc_bigstack_t {
167 char space[4096];
170 static struct usc_bigstack_t *STD_bigstack=NULL;
173 * Counter of errnos returned (-e option). Indexed by errno.
174 * Make the array USC_MAX_ERRNO long. That is the first Fortran
175 * Lib errno. No syscall should return an errno that high.
177 int STD_ERRNO_LIST[USC_MAX_ERRNO];
179 /* define the string length for Mesg and Mesg2 strings */
180 #define STRLEN 2048
182 static char Mesg2[STRLEN]; /* holds possible return string */
183 static void usc_recressive_func(int, int, struct usc_bigstack_t *);
186 * Define bits for options that might have env variable default
188 #define OPT_iteration 01
189 #define OPT_nofunccheck 02
190 #define OPT_duration 04
191 #define OPT_delay 010
192 #define OPT_copies 020
194 #ifdef UCLINUX
195 /* Allocated and used in self_exec.c: */
196 extern char *child_args; /* Arguments to child when -C is used */
197 #endif
199 /**********************************************************************
200 * parse_opts:
201 **********************************************************************/
202 char *
203 parse_opts(int ac, char **av, option_t *user_optarr, void (*uhf)(void))
205 int found; /* flag to indicate that an option specified was */
206 /* found in the user's list */
207 int k; /* scratch integer for returns and short time usage */
208 float ftmp; /* tmp float for parsing env variables */
209 char *ptr; /* used in getting env variables */
210 int options=0; /* no options specified */
211 int optstrlen, i;
212 char *optionstr;
213 int opt; /* return of getopt */
216 * If not the first time this function is called, release the old STD_opt_arr
217 * vector.
220 if ( STD_opt_arr != NULL ) {
221 free(STD_opt_arr);
222 STD_opt_arr=NULL;
224 /* Calculate how much space we need for the option string */
225 optstrlen = 0;
226 for (i = 0; std_options[i].optstr; ++i)
227 optstrlen += strlen(std_options[i].optstr);
228 if (user_optarr)
229 for (i = 0; user_optarr[i].option; ++i) {
230 if (strlen(user_optarr[i].option) > 2)
231 return "parse_opts: ERROR - Only short options are allowed";
232 optstrlen += strlen(user_optarr[i].option);
234 optstrlen += 1;
236 /* Create the option string for getopt */
237 optionstr = (char *)malloc(optstrlen);
238 if (!optionstr)
239 return "parse_opts: ERROR - Could not allocate memory for optionstr";
241 optionstr[0] = '\0';
243 for (i = 0; std_options[i].optstr; ++i)
244 strcat(optionstr, std_options[i].optstr);
245 if (user_optarr)
246 for (i = 0; user_optarr[i].option; ++i)
247 /* only add the option if it wasn't there already */
248 if (strchr(optionstr, user_optarr[i].option[0]) == NULL)
249 strcat(optionstr, user_optarr[i].option);
251 #if DEBUG > 1
252 printf("STD_nopts = %d\n", STD_nopts);
253 #endif
256 * Loop through av parsing options.
258 while ( (opt = getopt(ac, av, optionstr)) > 0) {
260 STD_argind = optind;
261 #if DEBUG > 0
262 printf("parse_opts: getopt returned '%c'\n", opt);
263 #endif
265 switch (opt) {
266 case '?': /* Unknown option */
267 return "Unknown option";
268 break;
269 case ':': /* Missing Arg */
270 return "Missing argument";
271 break;
272 case 'i': /* Iterations */
273 options |= OPT_iteration;
274 STD_LOOP_COUNT = atoi(optarg);
275 if (STD_LOOP_COUNT == 0) STD_INFINITE = 1;
276 break;
277 case 'P': /* Delay between iterations */
278 options |= OPT_delay;
279 STD_LOOP_DELAY = atof(optarg);
280 break;
281 case 'I': /* Time duration */
282 options |= OPT_duration;
283 STD_LOOP_DURATION = atof(optarg);
284 if ( STD_LOOP_DURATION == 0.0 ) STD_INFINITE=1;
285 break;
286 case 'c': /* Copies */
287 options |= OPT_copies;
288 STD_COPIES = atoi(optarg);
289 break;
290 case 'f': /* Functional testing */
291 STD_FUNCTIONAL_TEST = 0;
292 break;
293 case 'p': /* Pause for SIGUSR1 */
294 STD_PAUSE = 1;
295 break;
296 case 't': /* syscall timing */
297 STD_TIMING_ON = 1;
298 break;
299 case 'e': /* errno loggin */
300 STD_ERRNO_LOG = 1;
301 break;
302 case 'h': /* Help */
303 print_help(uhf);
304 exit(0);
305 break;
306 #ifdef UCLINUX
307 case 'C': /* Run child */
308 child_args = optarg;
309 break;
310 #endif
311 default:
313 /* Check all the user specified options */
314 found=0;
315 for(i = 0; user_optarr[i].option; ++i) {
317 if (opt == user_optarr[i].option[0]) {
318 /* Yup, This is a user option, set the flag and look for argument */
319 if ( user_optarr[i].flag ) {
320 *user_optarr[i].flag=1;
322 found++;
324 /* save the argument at the user's location */
325 if ( user_optarr[i].option[strlen(user_optarr[i].option)-1] == ':' ) {
326 *user_optarr[i].arg=optarg;
328 break; /* option found - break out of the for loop */
331 /* This condition "should never happen". SO CHECK FOR IT!!!! */
332 if ( ! found ) {
333 sprintf(Mesg2,
334 "parse_opts: ERROR - option:\"%c\" NOT FOUND... INTERNAL ERROR", opt);
335 return(Mesg2);
339 } /* end of while */
340 free(optionstr);
342 STD_argind = optind;
345 * Turn on debug
347 if ( (ptr=getenv("USC_DEBUG")) != NULL ) {
348 Debug=1;
349 printf("env USC_DEBUG is defined, turning on debug\n");
351 if ( (ptr=getenv("USC_VERBOSE")) != NULL ) {
352 Debug=1;
353 printf("env USC_VERBOSE is defined, turning on debug\n");
357 * If the USC_ITERATION_ENV environmental variable is set to
358 * a number, use that number as iteration count (same as -c option).
359 * The -c option with arg will be used even if this env var is set.
361 if ( !(options & OPT_iteration) && (ptr=getenv(USC_ITERATION_ENV)) != NULL ) {
362 if ( sscanf(ptr, "%i", &k) == 1) {
363 if ( k == 0 ) { /* if arg is 0, set infinite loop flag */
364 STD_INFINITE=1;
365 if ( Debug )
366 printf("Using env %s, set STD_INFINITE to 1\n",
367 USC_ITERATION_ENV);
368 } else { /* else, set the loop count to the arguement */
369 STD_LOOP_COUNT=k;
370 if ( Debug )
371 printf("Using env %s, set STD_LOOP_COUNT to %d\n",
372 USC_ITERATION_ENV, k);
378 * If the USC_NO_FUNC_CHECK environmental variable is set, we'll
379 * unset the STD_FUNCTIONAL_TEST variable.
381 if ( !(options & OPT_nofunccheck) && (ptr=getenv(USC_NO_FUNC_CHECK)) != NULL ) {
382 STD_FUNCTIONAL_TEST=0; /* Turn off functional testing */
383 if ( Debug )
384 printf("Using env %s, set STD_FUNCTIONAL_TEST to 0\n",
385 USC_NO_FUNC_CHECK);
389 * If the USC_LOOP_WALLTIME environmental variable is set,
390 * use that number as duration (same as -I option).
391 * The -I option with arg will be used even if this env var is set.
394 if ( !(options & OPT_duration) && (ptr=getenv(USC_LOOP_WALLTIME)) != NULL ) {
395 if ( sscanf(ptr, "%f", &ftmp) == 1 && ftmp >= 0.0 ) {
396 STD_LOOP_DURATION=ftmp;
397 if ( Debug )
398 printf("Using env %s, set STD_LOOP_DURATION to %f\n",
399 USC_LOOP_WALLTIME, ftmp);
400 if ( STD_LOOP_DURATION == 0.0 ) { /* if arg is 0, set infinite loop flag */
401 STD_INFINITE=1;
402 if ( Debug )
403 printf("Using env %s, set STD_INFINITE to 1\n", USC_LOOP_WALLTIME);
407 if ( !(options & OPT_duration) && (ptr=getenv("USC_DURATION")) != NULL ) {
408 if ( sscanf(ptr, "%f", &ftmp) == 1 && ftmp >= 0.0 ) {
409 STD_LOOP_DURATION=ftmp;
410 if ( Debug )
411 printf("Using env USC_DURATION, set STD_LOOP_DURATION to %f\n", ftmp);
412 if ( STD_LOOP_DURATION == 0.0 ) { /* if arg is 0, set infinite loop flag */
413 STD_INFINITE=1;
414 if ( Debug )
415 printf("Using env USC_DURATION, set STD_INFINITE to 1\n");
420 * If the USC_LOOP_DELAY environmental variable is set,
421 * use that number as delay in factional seconds (same as -P option).
422 * The -P option with arg will be used even if this env var is set.
424 if ( !(options & OPT_delay) && (ptr=getenv(USC_LOOP_DELAY)) != NULL ) {
425 if ( sscanf(ptr, "%f", &ftmp) == 1 && ftmp >= 0.0 ) {
426 STD_LOOP_DELAY=ftmp;
427 if ( Debug )
428 printf("Using env %s, set STD_LOOP_DELAY = %f\n",
429 USC_LOOP_DELAY, ftmp);
434 * If the USC_COPIES environmental variable is set,
435 * use that number as copies (same as -c option).
436 * The -c option with arg will be used even if this env var is set.
438 if ( !(options & OPT_copies) && (ptr=getenv(USC_COPIES)) != NULL ) {
439 if ( sscanf(ptr, "%d", &STD_COPIES) == 1 && STD_COPIES >= 0 ) {
440 if ( Debug )
441 printf("Using env %s, set STD_COPIES = %d\n",
442 USC_COPIES, STD_COPIES);
447 * The following are special system testing envs to turn on special
448 * hooks in the code.
450 if ( (ptr=getenv("USC_TP_BARRIER")) != NULL ) {
451 if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) {
452 STD_TP_barrier=k;
454 else
455 STD_TP_barrier=1;
456 if ( Debug )
457 printf("using env USC_TP_BARRIER, Set STD_TP_barrier to %d\n",
458 STD_TP_barrier);
461 if ( (ptr=getenv("USC_LP_BARRIER")) != NULL ) {
462 if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) {
463 STD_LP_barrier=k;
465 else
466 STD_LP_barrier=1;
467 if ( Debug )
468 printf("using env USC_LP_BARRIER, Set STD_LP_barrier to %d\n",
469 STD_LP_barrier);
472 if ( (ptr=getenv("USC_TP_SHMEM")) != NULL ) {
473 if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) {
474 STD_TP_shmem_sz=k;
475 if ( Debug )
476 printf("Using env USC_TP_SHMEM, Set STD_TP_shmem_sz to %d\n",
477 STD_TP_shmem_sz);
481 if ( (ptr=getenv("USC_LP_SHMEM")) != NULL ) {
482 if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) {
483 STD_LP_shmem=k;
484 if ( Debug )
485 printf("Using env USC_LP_SHMEM, Set STD_LP_shmem to %d\n",
486 STD_LP_shmem);
490 if ( (ptr=getenv("USC_LD_SHMEM")) != NULL ) {
491 if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) {
492 STD_LD_shmem=k;
493 if ( Debug )
494 printf("Using env USC_LD_SHMEM, Set STD_LD_shmem to %d\n",
495 STD_LD_shmem);
499 if ( (ptr=getenv("USC_TP_SBRK")) != NULL ) {
500 if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) {
501 STD_TP_sbrk=k;
502 if ( Debug )
503 printf("Using env USC_TP_SBRK, Set STD_TP_sbrk to %d\n",
504 STD_TP_sbrk);
508 #if !defined(UCLINUX)
509 if ( (ptr=getenv("USC_LP_SBRK")) != NULL ) {
510 if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) {
511 STD_LP_sbrk=k;
512 if ( Debug )
513 printf("Using env USC_LP_SBRK, Set STD_LP_sbrk to %d\n",
514 STD_LP_sbrk);
517 #endif /* if !defined(UCLINUX) */
519 if ( (ptr=getenv("USC_LP_RECFUN")) != NULL ) {
520 if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) {
521 STD_LP_recfun=k;
522 if ( STD_bigstack != (struct usc_bigstack_t *)NULL )
523 STD_bigstack=(struct usc_bigstack_t *)
524 malloc(sizeof(struct usc_bigstack_t));
525 if ( Debug )
526 printf("Using env USC_LP_RECFUN, Set STD_LP_recfun to %d\n",
527 STD_LP_recfun);
531 if ( (ptr=getenv("USC_LD_RECFUN")) != NULL ) {
532 if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) {
533 STD_LD_recfun=k;
534 if ( STD_bigstack != (struct usc_bigstack_t *)NULL )
535 STD_bigstack=(struct usc_bigstack_t *)
536 malloc(sizeof(struct usc_bigstack_t));
537 if ( Debug )
538 printf("Using env USC_LD_RECFUN, Set STD_LD_recfun to %d\n",
539 STD_LD_recfun);
543 #if UNIT_TEST
544 printf("The following variables after option and env parsing:\n");
545 printf("STD_FUNCTIONAL_TEST = %d\n", STD_FUNCTIONAL_TEST);
546 printf("STD_LOOP_DURATION = %f\n", STD_LOOP_DURATION);
547 printf("STD_LOOP_DELAY = %f\n", STD_LOOP_DELAY);
548 printf("STD_COPIES = %d\n", STD_COPIES);
549 printf("STD_LOOP_COUNT = %d\n", STD_LOOP_COUNT);
550 printf("STD_INFINITE = %d\n", STD_INFINITE);
551 printf("STD_TIMING_ON = %d\n", STD_TIMING_ON);
552 printf("STD_ERRNO_LOG = %d\n", STD_ERRNO_LOG);
553 printf("STD_PAUSE = %d\n", STD_PAUSE);
554 #endif
556 return((char *) NULL);
558 } /* end of parse_opts */
560 /*********************************************************************
561 * print_help() - print help message and user help message
562 *********************************************************************/
563 void print_help(void (*user_help)(void))
565 STD_opts_help();
567 if (user_help) user_help();
570 /*********************************************************************
571 * STD_opts_help() - return a help string for the STD_OPTIONS.
572 *********************************************************************/
573 void
574 STD_opts_help(void)
576 int i;
578 for(i = 0; std_options[i].optstr; ++i) {
579 if (std_options[i].help)
580 fputs(std_options[i].help, stdout);
585 * routine to goto when we get the SIGUSR1 for STD_PAUSE
587 void STD_go(int sig)
589 return;
592 /***********************************************************************
593 * This function will do desired end of global setup test
594 * hooks.
595 * Currently it will only do a pause waiting for sigusr1 if
596 * STD_PAUSE is set.
598 ***********************************************************************/
600 usc_global_setup_hook(void)
602 #ifndef UCLINUX
603 int cnt;
604 /* temp variable to store old signal action to be restored after pause */
605 int (*_TMP_FUNC)(void);
608 * Fork STD_COPIES-1 copies.
610 for(cnt=1;cnt<STD_COPIES;cnt++) {
611 switch(fork() ) {
612 case -1:
613 fprintf(stderr, "%s: fork() failed, errno:%d %s\n",
614 __FILE__, errno, strerror(errno));
615 break;
616 case 0: /* child */
617 cnt=STD_COPIES; /* to stop the forking */
618 break;
620 default: /* parent */
621 break;
626 * pause waiting for sigusr1.
628 if ( STD_PAUSE ) {
629 _TMP_FUNC = (int (*)(void))signal(SIGUSR1, STD_go);
630 pause();
631 signal(SIGUSR1, (void (*)(int))_TMP_FUNC);
634 #if !defined(UCLINUX)
636 if ( STD_TP_sbrk || STD_LP_sbrk) {
637 STD_start_break=sbrk(0); /* get original sbreak size */
640 if ( STD_TP_sbrk ) {
641 sbrk(STD_TP_sbrk);
642 if ( Debug )
643 printf("after sbrk(%d)\n", STD_TP_sbrk);
646 #endif /* if !defined(UCLINUX) */
647 #endif
648 return 0;
651 #define USECS_PER_SEC 1000000 /* microseconds per second */
653 /***********************************************************************
654 * This function returns the number of get_current_time()'s return
655 * per second.
656 ***********************************************************************/
658 static int
659 get_timepersec(void)
661 return USECS_PER_SEC; /* microseconds per second */
665 /***********************************************************************
666 * this function will get current time in microseconds since 1970.
667 ***********************************************************************/
668 static int
669 get_current_time(void)
671 struct timeval curtime;
673 gettimeofday(&curtime, NULL);
675 /* microseconds since 1970 */
676 return (curtime.tv_sec*USECS_PER_SEC) + curtime.tv_usec;
681 /***********************************************************************
683 * This function will determine if test should continue iterating
684 * If the STD_INFINITE flag is set, return 1.
685 * If the STD_LOOP_COUNT variable is set, compare it against
686 * the counter.
687 * If the STD_LOOP_DURATION variable is set, compare current time against
688 * calculated stop_time.
689 * This function will return 1 until all desired looping methods
690 * have been met.
692 * counter integer is supplied by the user program.
693 ***********************************************************************/
695 usc_test_looping(counter)
696 int counter;
698 static int first_time = 1;
699 static int stop_time = 0; /* stop time in rtc or usecs */
700 static int delay; /* delay in clocks or usecs */
701 int hertz=0; /* clocks per second or usecs per second */
702 int ct, end; /* current time, end delay time */
703 int keepgoing=0; /* used to determine return value */
706 * If this is the first iteration and we are looping for
707 * duration of STD_LOOP_DURATION seconds (fractional) or
708 * doing loop delays, get the clocks per second.
710 if ( first_time ) {
712 first_time=0;
713 if ( STD_LOOP_DELAY || STD_LOOP_DURATION ) {
714 hertz = get_timepersec();
718 * If looping for duration, calculate stop time in
719 * clocks.
722 if ( STD_LOOP_DURATION) {
723 ct=get_current_time();
724 stop_time=(int)((float)hertz * STD_LOOP_DURATION) + ct;
728 * If doing delay each iteration, calcuate the number
729 * of clocks for each delay.
731 if ( STD_LOOP_DELAY ) {
732 delay=(int)((float)hertz * STD_LOOP_DELAY);
738 * if delay each iteration, loop for delay clocks.
739 * This will not be done on first iteration.
740 * The delay will happen before determining if
741 * there will be another iteration.
743 else if ( STD_LOOP_DELAY ) {
744 ct=get_current_time();
745 end=ct+delay;
746 while ( ct < end ) {
748 * The following are special test hooks in the delay loop.
750 if ( STD_LD_recfun ) {
751 if ( Debug )
752 printf("calling usc_recressive_func(0, %d, STD_bigstack)\n",
753 STD_LD_recfun);
754 usc_recressive_func(0, STD_LD_recfun, STD_bigstack);
757 ct=get_current_time();
761 if ( STD_INFINITE ) {
762 keepgoing++;
765 if ( STD_LOOP_COUNT && counter < STD_LOOP_COUNT ) {
766 keepgoing++;
769 if ( STD_LOOP_DURATION != 0.0 && get_current_time() < stop_time ) {
770 keepgoing++;
773 if ( keepgoing == 0 )
774 return 0;
777 * The following code allows special system testing hooks.
780 if ( STD_LP_recfun ) {
781 if ( Debug )
782 printf("calling usc_recressive_func(0, %d, STD_bigstack)\n",
783 STD_LP_recfun);
784 usc_recressive_func(0, STD_LP_recfun, STD_bigstack);
787 #if !defined(UCLINUX)
789 if ( STD_LP_sbrk ) {
790 if ( Debug )
791 printf("about to do sbrk(%d)\n", STD_LP_sbrk);
792 sbrk(STD_LP_sbrk);
794 #endif
797 if ( keepgoing )
798 return 1;
799 else
800 return 0; /* done - stop iterating */
805 * This function recressively calls itself max times.
807 static void
808 usc_recressive_func(cnt, max, bstack)
809 int cnt;
810 int max;
811 struct usc_bigstack_t *bstack;
813 if ( cnt < max )
814 usc_recressive_func(cnt+1, max, bstack);
818 #if UNIT_TEST
819 /******************************************************************************
820 * UNIT TEST CODE
821 * UNIT TEST CODE
823 * this following code is provide so that unit testing can
824 * be done fairly easily.
825 ******************************************************************************/
827 int Help = 0;
828 int Help2 = 0;
829 char *ptr;
832 * Code from usctest.h that not part of this file since we are the library.
835 struct usc_errno_t TEST_VALID_ENO[USC_MAX_ERRNO];
837 /***********************************************************************
838 * Globals for returning the return code and errno from the system call
839 * test macros.
840 ***********************************************************************/
841 int TEST_RETURN;
842 int TEST_ERRNO;
844 /***********************************************************************
845 * temporary variables for determining max and min times in TEST macro
846 ***********************************************************************/
847 long btime, etime, tmptime;
851 /* for test specific parse_opts options */
852 option_t Options[] = {
853 { "help", &Help2, NULL }, /* -help option */
854 { "h", &Help, NULL }, /* -h option */
855 { TIMING, NULL, NULL}, /* disable -timing option */
857 #if INVALID_TEST_CASES
858 { "missingflag", NULL, &ptr }, /* error */
859 { "missingarg:", &Help, NULL }, /* error */
860 #endif /* INVALID_TEST_CASES */
862 { NULL, NULL, NULL }
866 main(argc, argv)
867 int argc;
868 char **argv;
870 int lc;
871 char *msg;
872 struct timeval t;
873 int cnt;
875 if ( (msg=parse_opts(argc, argv,
876 (option_t *) Options)) != (char *) NULL ) {
877 printf("ERROR : %s\n", msg);
878 exit(1);
881 TEST_PAUSE;
883 for (lc=0; TEST_LOOPING(lc); lc++) {
885 TEST( gettimeofday(&t, NULL) );
886 printf("iter=%d: sec:%d, usec:%6.6d %s", lc+1, t.tv_sec,
887 t.tv_usec, ctime(&t.tv_sec));
891 TEST_CLEANUP;
893 exit(0);
896 #endif /* UNIT_TEST */