2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
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.
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:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
33 /* $Id: parse_opts.c,v 1.14 2009/08/28 09:59:52 vapier Exp $ */
35 /**********************************************************
37 * OS Testing - Silicon Graphics, Inc.
39 * FUNCTION NAME : parse_opts
41 * FUNCTION TITLE : parse standard & user options for system call tests
44 * #include "usctest.h"
46 * char *parse_opts(ac, av, user_optarr, uhf)
49 * option_t user_optarr[];
52 * AUTHOR : William Roske/Richard Logan
54 * INITIAL RELEASE : UNICOS 7.0
57 * The parse_opts library routine takes that argc and argv parameters
58 * recevied by main() and an array of structures defining user options.
59 * It parses the command line setting flag and argument locations
60 * associated with the options. It uses getopt to do the actual cmd line
61 * parsing. uhf() is a function to print user define help
63 * This module contains the functions usc_global_setup_hook and
64 * usc_test_looping, which are called by marcos defined in usctest.h.
67 * parse_opts returns a pointer to an error message if an error occurs.
68 * This pointer is (char *)NULL if parsing is successful.
70 *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/
74 #include <sys/param.h>
75 #include <sys/signal.h>
76 #include <sys/types.h>
83 #endif /* UNIT_TEST */
86 #define _USC_LIB_ 1 /* indicates we are the library to the usctest.h include */
90 #define USC_COPIES "USC_COPIES"
101 /* The timing information block. */
102 struct tblock tblock
={0,((long) -1)>>1,0,0};
105 /* Define flags and args for standard options */
106 int STD_FUNCTIONAL_TEST
=1, /* flag indicating to do functional testing code */
107 STD_TIMING_ON
=0, /* flag indicating to print timing stats */
108 STD_PAUSE
=0, /* flag indicating to pause before actual start, */
109 /* for contention mode */
110 STD_INFINITE
=0, /* flag indciating to loop forever */
111 STD_LOOP_COUNT
=1, /* number of iterations */
112 STD_COPIES
=1, /* number of copies */
113 STD_ERRNO_LOG
=0; /* flag indicating to do errno logging */
115 float STD_LOOP_DURATION
=0.0, /* duration value in fractional seconds */
116 STD_LOOP_DELAY
=0.0; /* loop delay value in fractional seconds */
119 char **STD_opt_arr
= NULL
; /* array of option strings */
120 int STD_nopts
=0, /* number of elements in STD_opt_arr */
121 STD_argind
=1; /* argv index to next argv element */
122 /* (first argument) */
123 /* To getopt users, it is like optind */
126 * The following variables are to support system testing additions.
128 static int STD_TP_barrier
=0; /* flag to do barrier in TEST_PAUSE */
129 /* 2 - wait_barrier(), 3 - set_barrier(), * - barrier() */
130 static int STD_LP_barrier
=0; /* flag to do barrier in TEST_LOOPING */
131 /* 2 - wait_barrier(), 3 - set_barrier(), * - barrier() */
132 static int STD_TP_shmem_sz
=0; /* shmalloc this many words per pe in TEST_PAUSE */
133 static int STD_LD_shmem
=0; /* flag to do shmem_puts and shmem_gets during delay */
134 static int STD_LP_shmem
=0; /* flag to do shmem_puts and gets during TEST_LOOPING */
135 static int STD_LD_recfun
=0; /* do recressive function calls in loop delay */
136 static int STD_LP_recfun
=0; /* do recressive function calls in TEST_LOOPING */
137 static int STD_TP_sbrk
=0; /* do sbrk in TEST_PAUSE */
138 static int STD_LP_sbrk
=0; /* do sbrk in TEST_LOOPING */
139 static char *STD_start_break
=0; /* original sbrk size */
142 struct std_option_t
{
148 { "c:", " -c n Run n copies concurrently\n", NULL
, NULL
},
149 { "e" , " -e Turn on errno logging\n", NULL
, NULL
},
150 { "f" , " -f Turn off functional testing\n", NULL
, NULL
},
151 { "h" , " -h Show this help screen\n", NULL
, NULL
},
152 { "i:", " -i n Execute test n times\n", NULL
, NULL
},
153 { "I:", " -I x Execute test for x seconds\n", NULL
, NULL
},
154 { "p" , " -p Pause for SIGUSR1 before starting\n", NULL
, NULL
},
155 { "P:", " -P x Pause for x seconds between iterations\n", NULL
, NULL
},
156 { "t" , " -t Turn on syscall timing\n", NULL
, NULL
},
158 { "C:", " -C ARG Run the child process with arguments ARG (for internal use)\n",
161 {NULL
, NULL
, NULL
, NULL
}};
163 void print_help(void (*user_help
)());
166 * Structure for usc_recressive_func argument
168 struct usc_bigstack_t
{
172 static struct usc_bigstack_t
*STD_bigstack
=NULL
;
175 * Counter of errnos returned (-e option). Indexed by errno.
176 * Make the array USC_MAX_ERRNO long. That is the first Fortran
177 * Lib errno. No syscall should return an errno that high.
179 int STD_ERRNO_LIST
[USC_MAX_ERRNO
];
181 /* define the string length for Mesg and Mesg2 strings */
184 static char Mesg2
[STRLEN
]; /* holds possible return string */
185 static void usc_recressive_func();
188 * Define bits for options that might have env variable default
190 #define OPT_iteration 01
191 #define OPT_nofunccheck 02
192 #define OPT_duration 04
193 #define OPT_delay 010
194 #define OPT_copies 020
197 /* Allocated and used in self_exec.c: */
198 extern char *child_args
; /* Arguments to child when -C is used */
201 /**********************************************************************
203 **********************************************************************/
205 parse_opts(int ac
, char **av
, option_t
*user_optarr
, void (*uhf
)())
207 int found
; /* flag to indicate that an option specified was */
208 /* found in the user's list */
209 int k
; /* scratch integer for returns and short time usage */
210 float ftmp
; /* tmp float for parsing env variables */
211 char *ptr
; /* used in getting env variables */
212 int options
=0; /* no options specified */
215 int opt
; /* return of getopt */
218 * If not the first time this function is called, release the old STD_opt_arr
222 if ( STD_opt_arr
!= NULL
) {
226 /* Calculate how much space we need for the option string */
228 for (i
= 0; std_options
[i
].optstr
; ++i
)
229 optstrlen
+= strlen(std_options
[i
].optstr
);
231 for (i
= 0; user_optarr
[i
].option
; ++i
) {
232 if (strlen(user_optarr
[i
].option
) > 2)
233 return "parse_opts: ERROR - Only short options are allowed";
234 optstrlen
+= strlen(user_optarr
[i
].option
);
238 /* Create the option string for getopt */
239 optionstr
= (char *)malloc(optstrlen
);
241 return "parse_opts: ERROR - Could not allocate memory for optionstr";
245 for (i
= 0; std_options
[i
].optstr
; ++i
)
246 strcat(optionstr
, std_options
[i
].optstr
);
248 for (i
= 0; user_optarr
[i
].option
; ++i
)
249 /* only add the option if it wasn't there already */
250 if (strchr(optionstr
, user_optarr
[i
].option
[0]) == NULL
)
251 strcat(optionstr
, user_optarr
[i
].option
);
254 printf("STD_nopts = %d\n", STD_nopts
);
258 * Loop through av parsing options.
260 while ( (opt
= getopt(ac
, av
, optionstr
)) > 0) {
264 printf("parse_opts: getopt returned '%c'\n", opt
);
268 case '?': /* Unknown option */
269 return "Unknown option";
271 case ':': /* Missing Arg */
272 return "Missing argument";
274 case 'i': /* Iterations */
275 options
|= OPT_iteration
;
276 STD_LOOP_COUNT
= atoi(optarg
);
277 if (STD_LOOP_COUNT
== 0) STD_INFINITE
= 1;
279 case 'P': /* Delay between iterations */
280 options
|= OPT_delay
;
281 STD_LOOP_DELAY
= atof(optarg
);
283 case 'I': /* Time duration */
284 options
|= OPT_duration
;
285 STD_LOOP_DURATION
= atof(optarg
);
286 if ( STD_LOOP_DURATION
== 0.0 ) STD_INFINITE
=1;
288 case 'c': /* Copies */
289 options
|= OPT_copies
;
290 STD_COPIES
= atoi(optarg
);
292 case 'f': /* Functional testing */
293 STD_FUNCTIONAL_TEST
= 0;
295 case 'p': /* Pause for SIGUSR1 */
298 case 't': /* syscall timing */
301 case 'e': /* errno loggin */
309 case 'C': /* Run child */
315 /* Check all the user specified options */
317 for(i
= 0; user_optarr
[i
].option
; ++i
) {
319 if (opt
== user_optarr
[i
].option
[0]) {
320 /* Yup, This is a user option, set the flag and look for argument */
321 if ( user_optarr
[i
].flag
) {
322 *user_optarr
[i
].flag
=1;
326 /* save the argument at the user's location */
327 if ( user_optarr
[i
].option
[strlen(user_optarr
[i
].option
)-1] == ':' ) {
328 *user_optarr
[i
].arg
=optarg
;
330 break; /* option found - break out of the for loop */
333 /* This condition "should never happen". SO CHECK FOR IT!!!! */
336 "parse_opts: ERROR - option:\"%c\" NOT FOUND... INTERNAL ERROR", opt
);
349 if ( (ptr
=getenv("USC_DEBUG")) != NULL
) {
351 printf("env USC_DEBUG is defined, turning on debug\n");
353 if ( (ptr
=getenv("USC_VERBOSE")) != NULL
) {
355 printf("env USC_VERBOSE is defined, turning on debug\n");
359 * If the USC_ITERATION_ENV environmental variable is set to
360 * a number, use that number as iteration count (same as -c option).
361 * The -c option with arg will be used even if this env var is set.
363 if ( !(options
& OPT_iteration
) && (ptr
=getenv(USC_ITERATION_ENV
)) != NULL
) {
364 if ( sscanf(ptr
, "%i", &k
) == 1) {
365 if ( k
== 0 ) { /* if arg is 0, set infinite loop flag */
368 printf("Using env %s, set STD_INFINITE to 1\n",
370 } else { /* else, set the loop count to the arguement */
373 printf("Using env %s, set STD_LOOP_COUNT to %d\n",
374 USC_ITERATION_ENV
, k
);
380 * If the USC_NO_FUNC_CHECK environmental variable is set, we'll
381 * unset the STD_FUNCTIONAL_TEST variable.
383 if ( !(options
& OPT_nofunccheck
) && (ptr
=getenv(USC_NO_FUNC_CHECK
)) != NULL
) {
384 STD_FUNCTIONAL_TEST
=0; /* Turn off functional testing */
386 printf("Using env %s, set STD_FUNCTIONAL_TEST to 0\n",
391 * If the USC_LOOP_WALLTIME environmental variable is set,
392 * use that number as duration (same as -I option).
393 * The -I option with arg will be used even if this env var is set.
396 if ( !(options
& OPT_duration
) && (ptr
=getenv(USC_LOOP_WALLTIME
)) != NULL
) {
397 if ( sscanf(ptr
, "%f", &ftmp
) == 1 && ftmp
>= 0.0 ) {
398 STD_LOOP_DURATION
=ftmp
;
400 printf("Using env %s, set STD_LOOP_DURATION to %f\n",
401 USC_LOOP_WALLTIME
, ftmp
);
402 if ( STD_LOOP_DURATION
== 0.0 ) { /* if arg is 0, set infinite loop flag */
405 printf("Using env %s, set STD_INFINITE to 1\n", USC_LOOP_WALLTIME
);
409 if ( !(options
& OPT_duration
) && (ptr
=getenv("USC_DURATION")) != NULL
) {
410 if ( sscanf(ptr
, "%f", &ftmp
) == 1 && ftmp
>= 0.0 ) {
411 STD_LOOP_DURATION
=ftmp
;
413 printf("Using env USC_DURATION, set STD_LOOP_DURATION to %f\n", ftmp
);
414 if ( STD_LOOP_DURATION
== 0.0 ) { /* if arg is 0, set infinite loop flag */
417 printf("Using env USC_DURATION, set STD_INFINITE to 1\n");
422 * If the USC_LOOP_DELAY environmental variable is set,
423 * use that number as delay in factional seconds (same as -P option).
424 * The -P option with arg will be used even if this env var is set.
426 if ( !(options
& OPT_delay
) && (ptr
=getenv(USC_LOOP_DELAY
)) != NULL
) {
427 if ( sscanf(ptr
, "%f", &ftmp
) == 1 && ftmp
>= 0.0 ) {
430 printf("Using env %s, set STD_LOOP_DELAY = %f\n",
431 USC_LOOP_DELAY
, ftmp
);
436 * If the USC_COPIES environmental variable is set,
437 * use that number as copies (same as -c option).
438 * The -c option with arg will be used even if this env var is set.
440 if ( !(options
& OPT_copies
) && (ptr
=getenv(USC_COPIES
)) != NULL
) {
441 if ( sscanf(ptr
, "%d", &STD_COPIES
) == 1 && STD_COPIES
>= 0 ) {
443 printf("Using env %s, set STD_COPIES = %d\n",
444 USC_COPIES
, STD_COPIES
);
449 * The following are special system testing envs to turn on special
452 if ( (ptr
=getenv("USC_TP_BARRIER")) != NULL
) {
453 if ( sscanf(ptr
, "%i", &k
) == 1 && k
>= 0 ) {
459 printf("using env USC_TP_BARRIER, Set STD_TP_barrier to %d\n",
463 if ( (ptr
=getenv("USC_LP_BARRIER")) != NULL
) {
464 if ( sscanf(ptr
, "%i", &k
) == 1 && k
>= 0 ) {
470 printf("using env USC_LP_BARRIER, Set STD_LP_barrier to %d\n",
474 if ( (ptr
=getenv("USC_TP_SHMEM")) != NULL
) {
475 if ( sscanf(ptr
, "%i", &k
) == 1 && k
>= 0 ) {
478 printf("Using env USC_TP_SHMEM, Set STD_TP_shmem_sz to %d\n",
483 if ( (ptr
=getenv("USC_LP_SHMEM")) != NULL
) {
484 if ( sscanf(ptr
, "%i", &k
) == 1 && k
>= 0 ) {
487 printf("Using env USC_LP_SHMEM, Set STD_LP_shmem to %d\n",
492 if ( (ptr
=getenv("USC_LD_SHMEM")) != NULL
) {
493 if ( sscanf(ptr
, "%i", &k
) == 1 && k
>= 0 ) {
496 printf("Using env USC_LD_SHMEM, Set STD_LD_shmem to %d\n",
501 if ( (ptr
=getenv("USC_TP_SBRK")) != NULL
) {
502 if ( sscanf(ptr
, "%i", &k
) == 1 && k
>= 0 ) {
505 printf("Using env USC_TP_SBRK, Set STD_TP_sbrk to %d\n",
510 #if !defined(UCLINUX)
511 if ( (ptr
=getenv("USC_LP_SBRK")) != NULL
) {
512 if ( sscanf(ptr
, "%i", &k
) == 1 && k
>= 0 ) {
515 printf("Using env USC_LP_SBRK, Set STD_LP_sbrk to %d\n",
519 #endif /* if !defined(UCLINUX) */
521 if ( (ptr
=getenv("USC_LP_RECFUN")) != NULL
) {
522 if ( sscanf(ptr
, "%i", &k
) == 1 && k
>= 0 ) {
524 if ( STD_bigstack
!= (struct usc_bigstack_t
*)NULL
)
525 STD_bigstack
=(struct usc_bigstack_t
*)
526 malloc(sizeof(struct usc_bigstack_t
));
528 printf("Using env USC_LP_RECFUN, Set STD_LP_recfun to %d\n",
533 if ( (ptr
=getenv("USC_LD_RECFUN")) != NULL
) {
534 if ( sscanf(ptr
, "%i", &k
) == 1 && k
>= 0 ) {
536 if ( STD_bigstack
!= (struct usc_bigstack_t
*)NULL
)
537 STD_bigstack
=(struct usc_bigstack_t
*)
538 malloc(sizeof(struct usc_bigstack_t
));
540 printf("Using env USC_LD_RECFUN, Set STD_LD_recfun to %d\n",
546 printf("The following variables after option and env parsing:\n");
547 printf("STD_FUNCTIONAL_TEST = %d\n", STD_FUNCTIONAL_TEST
);
548 printf("STD_LOOP_DURATION = %f\n", STD_LOOP_DURATION
);
549 printf("STD_LOOP_DELAY = %f\n", STD_LOOP_DELAY
);
550 printf("STD_COPIES = %d\n", STD_COPIES
);
551 printf("STD_LOOP_COUNT = %d\n", STD_LOOP_COUNT
);
552 printf("STD_INFINITE = %d\n", STD_INFINITE
);
553 printf("STD_TIMING_ON = %d\n", STD_TIMING_ON
);
554 printf("STD_ERRNO_LOG = %d\n", STD_ERRNO_LOG
);
555 printf("STD_PAUSE = %d\n", STD_PAUSE
);
558 return((char *) NULL
);
560 } /* end of parse_opts */
562 /*********************************************************************
563 * print_help() - print help message and user help message
564 *********************************************************************/
565 void print_help(void (*user_help
)())
569 if (user_help
) user_help();
572 /*********************************************************************
573 * STD_opts_help() - return a help string for the STD_OPTIONS.
574 *********************************************************************/
580 for(i
= 0; std_options
[i
].optstr
; ++i
) {
581 if (std_options
[i
].help
)
582 printf("%s", std_options
[i
].help
);
587 * routine to goto when we get the SIGUSR1 for STD_PAUSE
594 /***********************************************************************
595 * This function will do desired end of global setup test
597 * Currently it will only do a pause waiting for sigusr1 if
600 ***********************************************************************/
602 usc_global_setup_hook()
606 /* temp variable to store old signal action to be restored after pause */
607 int (*_TMP_FUNC
)(void);
610 * Fork STD_COPIES-1 copies.
612 for(cnt
=1;cnt
<STD_COPIES
;cnt
++) {
615 fprintf(stderr
, "%s: fork() failed, errno:%d %s\n",
616 __FILE__
, errno
, strerror(errno
));
619 cnt
=STD_COPIES
; /* to stop the forking */
622 default: /* parent */
628 * pause waiting for sigusr1.
631 _TMP_FUNC
= (int (*)())signal(SIGUSR1
, STD_go
);
633 signal(SIGUSR1
, (void (*)())_TMP_FUNC
);
636 #if !defined(UCLINUX)
638 if ( STD_TP_sbrk
|| STD_LP_sbrk
) {
639 STD_start_break
=sbrk(0); /* get original sbreak size */
645 printf("after sbrk(%d)\n", STD_TP_sbrk
);
648 #endif /* if !defined(UCLINUX) */
653 #define USECS_PER_SEC 1000000 /* microseconds per second */
655 /***********************************************************************
656 * This function returns the number of get_current_time()'s return
658 ***********************************************************************/
663 return USECS_PER_SEC
; /* microseconds per second */
667 /***********************************************************************
668 * this function will get current time in microseconds since 1970.
669 ***********************************************************************/
673 struct timeval curtime
;
675 gettimeofday(&curtime
, NULL
);
677 /* microseconds since 1970 */
678 return (curtime
.tv_sec
*USECS_PER_SEC
) + curtime
.tv_usec
;
683 /***********************************************************************
685 * This function will determine if test should continue iterating
686 * If the STD_INFINITE flag is set, return 1.
687 * If the STD_LOOP_COUNT variable is set, compare it against
689 * If the STD_LOOP_DURATION variable is set, compare current time against
690 * calculated stop_time.
691 * This function will return 1 until all desired looping methods
694 * counter integer is supplied by the user program.
695 ***********************************************************************/
697 usc_test_looping(counter
)
700 static int first_time
= 1;
701 static int stop_time
= 0; /* stop time in rtc or usecs */
702 static int delay
; /* delay in clocks or usecs */
703 int hertz
=0; /* clocks per second or usecs per second */
704 int ct
, end
; /* current time, end delay time */
705 int keepgoing
=0; /* used to determine return value */
708 * If this is the first iteration and we are looping for
709 * duration of STD_LOOP_DURATION seconds (fractional) or
710 * doing loop delays, get the clocks per second.
715 if ( STD_LOOP_DELAY
|| STD_LOOP_DURATION
) {
716 hertz
= get_timepersec();
720 * If looping for duration, calculate stop time in
724 if ( STD_LOOP_DURATION
) {
725 ct
=get_current_time();
726 stop_time
=(int)((float)hertz
* STD_LOOP_DURATION
) + ct
;
730 * If doing delay each iteration, calcuate the number
731 * of clocks for each delay.
733 if ( STD_LOOP_DELAY
) {
734 delay
=(int)((float)hertz
* STD_LOOP_DELAY
);
740 * if delay each iteration, loop for delay clocks.
741 * This will not be done on first iteration.
742 * The delay will happen before determining if
743 * there will be another iteration.
745 else if ( STD_LOOP_DELAY
) {
746 ct
=get_current_time();
750 * The following are special test hooks in the delay loop.
752 if ( STD_LD_recfun
) {
754 printf("calling usc_recressive_func(0, %d, *STD_bigstack)\n",
756 usc_recressive_func(0, STD_LD_recfun
, *STD_bigstack
);
759 ct
=get_current_time();
763 if ( STD_INFINITE
) {
767 if ( STD_LOOP_COUNT
&& counter
< STD_LOOP_COUNT
) {
771 if ( STD_LOOP_DURATION
!= 0.0 && get_current_time() < stop_time
) {
775 if ( keepgoing
== 0 )
779 * The following code allows special system testing hooks.
782 if ( STD_LP_recfun
) {
784 printf("calling usc_recressive_func(0, %d, *STD_bigstack)\n",
786 usc_recressive_func(0, STD_LP_recfun
, *STD_bigstack
);
789 #if !defined(UCLINUX)
793 printf("about to do sbrk(%d)\n", STD_LP_sbrk
);
802 return 0; /* done - stop iterating */
807 * This function recressively calls itself max times.
810 usc_recressive_func(cnt
, max
, bstack
)
813 struct usc_bigstack_t bstack
;
816 usc_recressive_func(cnt
+1, max
, bstack
);
821 /******************************************************************************
825 * this following code is provide so that unit testing can
826 * be done fairly easily.
827 ******************************************************************************/
834 * Code from usctest.h that not part of this file since we are the library.
837 struct usc_errno_t TEST_VALID_ENO
[USC_MAX_ERRNO
];
839 /***********************************************************************
840 * Globals for returning the return code and errno from the system call
842 ***********************************************************************/
846 /* for test specific parse_opts options */
847 option_t Options
[] = {
848 { "help", &Help2
, NULL
}, /* -help option */
849 { "h", &Help
, NULL
}, /* -h option */
850 { TIMING
, NULL
, NULL
}, /* disable -timing option */
852 #if INVALID_TEST_CASES
853 { "missingflag", NULL
, &ptr
}, /* error */
854 { "missingarg:", &Help
, NULL
}, /* error */
855 #endif /* INVALID_TEST_CASES */
870 if ( (msg
=parse_opts(argc
, argv
,
871 (option_t
*) Options
)) != (char *) NULL
) {
872 printf("ERROR : %s\n", msg
);
878 for (lc
=0; TEST_LOOPING(lc
); lc
++) {
880 TEST( gettimeofday(&t
, NULL
) );
881 printf("iter=%d: sec:%d, usec:%6.6d %s", lc
+1, t
.tv_sec
,
882 t
.tv_usec
, ctime(&t
.tv_sec
));
891 #endif /* UNIT_TEST */