alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 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. |
| 11 | * |
| 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. |
| 18 | * |
| 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. |
| 22 | * |
| 23 | * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy, |
| 24 | * Mountain View, CA 94043, or: |
| 25 | * |
| 26 | * http://www.sgi.com |
| 27 | * |
| 28 | * For further information regarding this notice, see: |
| 29 | * |
| 30 | * http://oss.sgi.com/projects/GenInfo/NoticeExplan/ |
| 31 | */ |
| 32 | |
nstraz | 829b40a | 2000-09-06 14:33:29 +0000 | [diff] [blame^] | 33 | /* $Id: parse_opts.c,v 1.4 2000/09/06 14:33:29 nstraz Exp $ */ |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 34 | |
| 35 | /********************************************************** |
| 36 | * |
| 37 | * OS Testing - Silicon Graphics, Inc. |
| 38 | * |
| 39 | * FUNCTION NAME : parse_opts |
| 40 | * |
| 41 | * FUNCTION TITLE : parse standard & user options for system call tests |
| 42 | * |
| 43 | * SYNOPSIS: |
| 44 | * #include "usctest.h" |
| 45 | * |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame] | 46 | * char *parse_opts(ac, av, user_optarr, uhf) |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 47 | * int ac; |
| 48 | * char **av; |
| 49 | * option_t user_optarr[]; |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame] | 50 | * void (*uhf)(); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 51 | * |
| 52 | * AUTHOR : William Roske/Richard Logan |
| 53 | * |
| 54 | * INITIAL RELEASE : UNICOS 7.0 |
| 55 | * |
| 56 | * DESCRIPTION |
| 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 |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame] | 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 |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 62 | * |
| 63 | * This module contains the functions usc_global_setup_hook and |
| 64 | * usc_test_looping, which are called by marcos defined in usctest.h. |
| 65 | * |
| 66 | * RETURN VALUE |
| 67 | * parse_opts returns a pointer to an error message if an error occurs. |
| 68 | * This pointer is (char *)NULL if parsing is successful. |
| 69 | * |
| 70 | *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/ |
| 71 | #include <errno.h> |
| 72 | #include <stdlib.h> |
| 73 | #include <string.h> |
| 74 | #include <sys/param.h> |
| 75 | #include <sys/signal.h> |
| 76 | #include <sys/types.h> |
| 77 | #include <unistd.h> |
| 78 | #include <sys/time.h> |
| 79 | |
| 80 | |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 81 | #if UNIT_TEST |
| 82 | #include <time.h> |
| 83 | #endif /* UNIT_TEST */ |
| 84 | |
| 85 | #include "test.h" |
| 86 | #define _USC_LIB_ 1 /* indicates we are the library to the usctest.h include */ |
| 87 | #include "usctest.h" |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 88 | |
| 89 | #ifndef USC_COPIES |
| 90 | #define USC_COPIES "USC_COPIES" |
| 91 | #endif |
| 92 | |
| 93 | #ifndef UNIT_TEST |
| 94 | #define UNIT_TEST 0 |
| 95 | #endif |
| 96 | |
| 97 | #ifndef DEBUG |
| 98 | #define DEBUG 0 |
| 99 | #endif |
| 100 | |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 101 | /* The timing information block. */ |
| 102 | struct tblock tblock={0,((long) -1)>>1,0,0}; |
| 103 | |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 104 | |
| 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 */ |
| 114 | |
| 115 | float STD_LOOP_DURATION=0.0, /* duration value in fractional seconds */ |
| 116 | STD_LOOP_DELAY=0.0; /* loop delay value in fractional seconds */ |
| 117 | |
| 118 | |
| 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 */ |
| 124 | |
| 125 | /* |
| 126 | * The following variables are to support system testing additions. |
| 127 | */ |
| 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 */ |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 140 | static int Debug=0; |
| 141 | |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame] | 142 | struct std_option_t { |
| 143 | char *optstr; |
| 144 | char *help; |
| 145 | char *flag; |
| 146 | char **arg; |
| 147 | } std_options[] = { |
nstraz | def354d | 2000-08-31 18:40:28 +0000 | [diff] [blame] | 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}, |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame] | 157 | {NULL, NULL, NULL, NULL}}; |
| 158 | |
| 159 | void print_help(void (*user_help)()); |
| 160 | |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 161 | /* |
| 162 | * Structure for usc_recressive_func argument |
| 163 | */ |
| 164 | struct usc_bigstack_t { |
| 165 | char space[4096]; |
| 166 | }; |
| 167 | |
| 168 | static struct usc_bigstack_t *STD_bigstack=NULL; |
| 169 | |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 170 | /* |
| 171 | * Counter of errnos returned (-e option). Indexed by errno. |
| 172 | * Make the array USC_MAX_ERRNO long. That is the first Fortran |
| 173 | * Lib errno. No syscall should return an errno that high. |
| 174 | */ |
| 175 | int STD_ERRNO_LIST[USC_MAX_ERRNO]; |
| 176 | |
| 177 | /* define the string length for Mesg and Mesg2 strings */ |
| 178 | #define STRLEN 2048 |
| 179 | |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 180 | static char Mesg2[STRLEN]; /* holds possible return string */ |
| 181 | static void usc_recressive_func(); |
| 182 | |
| 183 | /* |
| 184 | * Define bits for options that might have env variable default |
| 185 | */ |
| 186 | #define OPT_iteration 01 |
| 187 | #define OPT_nofunccheck 02 |
| 188 | #define OPT_duration 04 |
| 189 | #define OPT_delay 010 |
| 190 | #define OPT_copies 020 |
| 191 | |
| 192 | |
| 193 | /********************************************************************** |
| 194 | * parse_opts: |
| 195 | **********************************************************************/ |
| 196 | char * |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame] | 197 | parse_opts(int ac, char **av, option_t *user_optarr, void (*uhf)()) |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 198 | { |
| 199 | int found; /* flag to indicate that an option specified was */ |
| 200 | /* found in the user's list */ |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 201 | int k; /* scratch integer for returns and short time usage */ |
| 202 | float ftmp; /* tmp float for parsing env variables */ |
| 203 | char *ptr; /* used in getting env variables */ |
| 204 | int options=0; /* no options specified */ |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame] | 205 | int optstrlen, i; |
| 206 | char *optionstr; |
| 207 | int opt; /* return of getopt */ |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 208 | |
| 209 | /* |
| 210 | * If not the first time this function is called, release the old STD_opt_arr |
| 211 | * vector. |
| 212 | */ |
| 213 | |
| 214 | if ( STD_opt_arr != NULL ) { |
| 215 | free(STD_opt_arr); |
| 216 | STD_opt_arr=NULL; |
| 217 | } |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame] | 218 | /* Calculate how much space we need for the option string */ |
| 219 | optstrlen = 0; |
| 220 | for (i = 0; std_options[i].optstr; ++i) |
| 221 | optstrlen += strlen(std_options[i].optstr); |
| 222 | if (user_optarr) |
| 223 | for (i = 0; user_optarr[i].option; ++i) { |
| 224 | if (strlen(user_optarr[i].option) > 2) |
| 225 | return "parse_opts: ERROR - Only short options are allowed"; |
| 226 | optstrlen += strlen(user_optarr[i].option); |
| 227 | } |
| 228 | optstrlen += 1; |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 229 | |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame] | 230 | /* Create the option string for getopt */ |
| 231 | optionstr = (char *)malloc(optstrlen); |
| 232 | if (!optionstr) |
| 233 | return "parse_opts: ERROR - Could not allocate memory for optionstr"; |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 234 | |
nstraz | 829b40a | 2000-09-06 14:33:29 +0000 | [diff] [blame^] | 235 | optionstr[0] = '\0'; |
| 236 | |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame] | 237 | for (i = 0; std_options[i].optstr; ++i) |
| 238 | strcat(optionstr, std_options[i].optstr); |
| 239 | if (user_optarr) |
| 240 | for (i = 0; user_optarr[i].option; ++i) |
| 241 | /* only add the option if it wasn't there already */ |
| 242 | if (strchr(optionstr, user_optarr[i].option[0]) == NULL) |
| 243 | strcat(optionstr, user_optarr[i].option); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 244 | |
| 245 | #if DEBUG > 1 |
| 246 | printf("STD_nopts = %d\n", STD_nopts); |
| 247 | #endif |
| 248 | |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 249 | /* |
| 250 | * Loop through av parsing options. |
| 251 | */ |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame] | 252 | while ( (opt = getopt(ac, av, optionstr)) > 0) { |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 253 | |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame] | 254 | STD_argind = optind; |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 255 | #if DEBUG > 0 |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame] | 256 | printf("parse_opts: getopt returned '%c'\n", opt); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 257 | #endif |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 258 | |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame] | 259 | switch (opt) { |
| 260 | case '?': /* Unknown option */ |
| 261 | return "Unknown option"; |
| 262 | break; |
| 263 | case ':': /* Missing Arg */ |
| 264 | return "Missing argument"; |
| 265 | break; |
| 266 | case 'i': /* Iterations */ |
| 267 | options |= OPT_iteration; |
| 268 | STD_LOOP_COUNT = atoi(optarg); |
| 269 | if (STD_LOOP_COUNT == 0) STD_INFINITE = 1; |
| 270 | break; |
| 271 | case 'P': /* Delay between iterations */ |
| 272 | options |= OPT_delay; |
| 273 | STD_LOOP_DELAY = atof(optarg); |
| 274 | break; |
| 275 | case 'I': /* Time duration */ |
| 276 | options |= OPT_duration; |
| 277 | STD_LOOP_DURATION = atof(optarg); |
| 278 | if ( STD_LOOP_DURATION == 0.0 ) STD_INFINITE=1; |
| 279 | break; |
| 280 | case 'c': /* Copies */ |
| 281 | options |= OPT_copies; |
| 282 | STD_COPIES = atoi(optarg); |
| 283 | break; |
| 284 | case 'f': /* Functional testing */ |
| 285 | STD_FUNCTIONAL_TEST = 0; |
| 286 | break; |
| 287 | case 'p': /* Pause for SIGUSR1 */ |
| 288 | STD_PAUSE = 1; |
| 289 | break; |
| 290 | case 't': /* syscall timing */ |
| 291 | STD_TIMING_ON = 1; |
| 292 | break; |
| 293 | case 'e': /* errno loggin */ |
| 294 | STD_ERRNO_LOG = 1; |
| 295 | break; |
| 296 | case 'h': /* Help */ |
| 297 | print_help(uhf); |
| 298 | exit(0); |
| 299 | break; |
| 300 | default: |
| 301 | |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 302 | /* Check all the user specified options */ |
| 303 | found=0; |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame] | 304 | for(i = 0; user_optarr[i].option; ++i) { |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 305 | |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame] | 306 | if (opt == user_optarr[i].option[0]) { |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 307 | /* Yup, This is a user option, set the flag and look for argument */ |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame] | 308 | if ( user_optarr[i].flag ) { |
| 309 | *user_optarr[i].flag=1; |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 310 | } |
| 311 | found++; |
| 312 | |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 313 | /* save the argument at the user's location */ |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame] | 314 | if ( user_optarr[i].option[strlen(user_optarr[i].option)-1] == ':' ) { |
| 315 | *user_optarr[i].arg=optarg; |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 316 | } |
| 317 | break; /* option found - break out of the for loop */ |
| 318 | } |
| 319 | } |
| 320 | /* This condition "should never happen". SO CHECK FOR IT!!!! */ |
| 321 | if ( ! found ) { |
| 322 | sprintf(Mesg2, |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame] | 323 | "parse_opts: ERROR - option:\"%c\" NOT FOUND... INTERNAL ERROR", opt); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 324 | return(Mesg2); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | } /* end of while */ |
| 329 | |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame] | 330 | STD_argind = optind; |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 331 | |
| 332 | /* |
| 333 | * Turn on debug |
| 334 | */ |
| 335 | if ( (ptr=getenv("USC_DEBUG")) != NULL ) { |
| 336 | Debug=1; |
| 337 | printf("env USC_DEBUG is defined, turning on debug\n"); |
| 338 | } |
| 339 | if ( (ptr=getenv("USC_VERBOSE")) != NULL ) { |
| 340 | Debug=1; |
| 341 | printf("env USC_VERBOSE is defined, turning on debug\n"); |
| 342 | } |
| 343 | |
| 344 | /* |
| 345 | * If the USC_ITERATION_ENV environmental variable is set to |
| 346 | * a number, use that number as iteration count (same as -c option). |
| 347 | * The -c option with arg will be used even if this env var is set. |
| 348 | */ |
| 349 | if ( !(options & OPT_iteration) && (ptr=getenv(USC_ITERATION_ENV)) != NULL ) { |
| 350 | if ( sscanf(ptr, "%i", &k) == 1) { |
| 351 | if ( k == 0 ) { /* if arg is 0, set infinite loop flag */ |
| 352 | STD_INFINITE=1; |
| 353 | if ( Debug ) |
| 354 | printf("Using env %s, set STD_INFINITE to 1\n", |
| 355 | USC_ITERATION_ENV); |
| 356 | } else { /* else, set the loop count to the arguement */ |
| 357 | STD_LOOP_COUNT=k; |
| 358 | if ( Debug ) |
| 359 | printf("Using env %s, set STD_LOOP_COUNT to %d\n", |
| 360 | USC_ITERATION_ENV, k); |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | |
| 365 | /* |
| 366 | * If the USC_NO_FUNC_CHECK environmental variable is set, we'll |
| 367 | * unset the STD_FUNCTIONAL_TEST variable. |
| 368 | */ |
| 369 | if ( !(options & OPT_nofunccheck) && (ptr=getenv(USC_NO_FUNC_CHECK)) != NULL ) { |
| 370 | STD_FUNCTIONAL_TEST=0; /* Turn off functional testing */ |
| 371 | if ( Debug ) |
| 372 | printf("Using env %s, set STD_FUNCTIONAL_TEST to 0\n", |
| 373 | USC_NO_FUNC_CHECK); |
| 374 | } |
| 375 | |
| 376 | /* |
| 377 | * If the USC_LOOP_WALLTIME environmental variable is set, |
| 378 | * use that number as duration (same as -I option). |
| 379 | * The -I option with arg will be used even if this env var is set. |
| 380 | */ |
| 381 | |
| 382 | if ( !(options & OPT_duration) && (ptr=getenv(USC_LOOP_WALLTIME)) != NULL ) { |
| 383 | if ( sscanf(ptr, "%f", &ftmp) == 1 && ftmp >= 0.0 ) { |
| 384 | STD_LOOP_DURATION=ftmp; |
| 385 | if ( Debug ) |
| 386 | printf("Using env %s, set STD_LOOP_DURATION to %f\n", |
| 387 | USC_LOOP_WALLTIME, ftmp); |
| 388 | if ( STD_LOOP_DURATION == 0.0 ) { /* if arg is 0, set infinite loop flag */ |
| 389 | STD_INFINITE=1; |
| 390 | if ( Debug ) |
| 391 | printf("Using env %s, set STD_INFINITE to 1\n", USC_LOOP_WALLTIME); |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | if ( !(options & OPT_duration) && (ptr=getenv("USC_DURATION")) != NULL ) { |
| 396 | if ( sscanf(ptr, "%f", &ftmp) == 1 && ftmp >= 0.0 ) { |
| 397 | STD_LOOP_DURATION=ftmp; |
| 398 | if ( Debug ) |
| 399 | printf("Using env USC_DURATION, set STD_LOOP_DURATION to %f\n", 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 USC_DURATION, set STD_INFINITE to 1\n"); |
| 404 | } |
| 405 | } |
| 406 | } |
| 407 | /* |
| 408 | * If the USC_LOOP_DELAY environmental variable is set, |
| 409 | * use that number as delay in factional seconds (same as -P option). |
| 410 | * The -P option with arg will be used even if this env var is set. |
| 411 | */ |
| 412 | if ( !(options & OPT_delay) && (ptr=getenv(USC_LOOP_DELAY)) != NULL ) { |
| 413 | if ( sscanf(ptr, "%f", &ftmp) == 1 && ftmp >= 0.0 ) { |
| 414 | STD_LOOP_DELAY=ftmp; |
| 415 | if ( Debug ) |
| 416 | printf("Using env %s, set STD_LOOP_DELAY = %f\n", |
| 417 | USC_LOOP_DELAY, ftmp); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | /* |
| 422 | * If the USC_COPIES environmental variable is set, |
| 423 | * use that number as copies (same as -c option). |
| 424 | * The -c option with arg will be used even if this env var is set. |
| 425 | */ |
| 426 | if ( !(options & OPT_copies) && (ptr=getenv(USC_COPIES)) != NULL ) { |
| 427 | if ( sscanf(ptr, "%d", &STD_COPIES) == 1 && STD_COPIES >= 0 ) { |
| 428 | if ( Debug ) |
| 429 | printf("Using env %s, set STD_COPIES = %d\n", |
| 430 | USC_COPIES, STD_COPIES); |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | /* |
| 435 | * The following are special system testing envs to turn on special |
| 436 | * hooks in the code. |
| 437 | */ |
| 438 | if ( (ptr=getenv("USC_TP_BARRIER")) != NULL ) { |
| 439 | if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) { |
| 440 | STD_TP_barrier=k; |
| 441 | } |
| 442 | else |
| 443 | STD_TP_barrier=1; |
| 444 | if ( Debug ) |
| 445 | printf("using env USC_TP_BARRIER, Set STD_TP_barrier to %d\n", |
| 446 | STD_TP_barrier); |
| 447 | } |
| 448 | |
| 449 | if ( (ptr=getenv("USC_LP_BARRIER")) != NULL ) { |
| 450 | if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) { |
| 451 | STD_LP_barrier=k; |
| 452 | } |
| 453 | else |
| 454 | STD_LP_barrier=1; |
| 455 | if ( Debug ) |
| 456 | printf("using env USC_LP_BARRIER, Set STD_LP_barrier to %d\n", |
| 457 | STD_LP_barrier); |
| 458 | } |
| 459 | |
| 460 | if ( (ptr=getenv("USC_TP_SHMEM")) != NULL ) { |
| 461 | if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) { |
| 462 | STD_TP_shmem_sz=k; |
| 463 | if ( Debug ) |
| 464 | printf("Using env USC_TP_SHMEM, Set STD_TP_shmem_sz to %d\n", |
| 465 | STD_TP_shmem_sz); |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | if ( (ptr=getenv("USC_LP_SHMEM")) != NULL ) { |
| 470 | if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) { |
| 471 | STD_LP_shmem=k; |
| 472 | if ( Debug ) |
| 473 | printf("Using env USC_LP_SHMEM, Set STD_LP_shmem to %d\n", |
| 474 | STD_LP_shmem); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | if ( (ptr=getenv("USC_LD_SHMEM")) != NULL ) { |
| 479 | if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) { |
| 480 | STD_LD_shmem=k; |
| 481 | if ( Debug ) |
| 482 | printf("Using env USC_LD_SHMEM, Set STD_LD_shmem to %d\n", |
| 483 | STD_LD_shmem); |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | if ( (ptr=getenv("USC_TP_SBRK")) != NULL ) { |
| 488 | if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) { |
| 489 | STD_TP_sbrk=k; |
| 490 | if ( Debug ) |
| 491 | printf("Using env USC_TP_SBRK, Set STD_TP_sbrk to %d\n", |
| 492 | STD_TP_sbrk); |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | if ( (ptr=getenv("USC_LP_SBRK")) != NULL ) { |
| 497 | if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) { |
| 498 | STD_LP_sbrk=k; |
| 499 | if ( Debug ) |
| 500 | printf("Using env USC_LP_SBRK, Set STD_LP_sbrk to %d\n", |
| 501 | STD_LP_sbrk); |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | if ( (ptr=getenv("USC_LP_RECFUN")) != NULL ) { |
| 506 | if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) { |
| 507 | STD_LP_recfun=k; |
| 508 | if ( STD_bigstack != (struct usc_bigstack_t *)NULL ) |
| 509 | STD_bigstack=(struct usc_bigstack_t *) |
| 510 | malloc(sizeof(struct usc_bigstack_t)); |
| 511 | if ( Debug ) |
| 512 | printf("Using env USC_LP_RECFUN, Set STD_LP_recfun to %d\n", |
| 513 | STD_LP_recfun); |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | if ( (ptr=getenv("USC_LD_RECFUN")) != NULL ) { |
| 518 | if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) { |
| 519 | STD_LD_recfun=k; |
| 520 | if ( STD_bigstack != (struct usc_bigstack_t *)NULL ) |
| 521 | STD_bigstack=(struct usc_bigstack_t *) |
| 522 | malloc(sizeof(struct usc_bigstack_t)); |
| 523 | if ( Debug ) |
| 524 | printf("Using env USC_LD_RECFUN, Set STD_LD_recfun to %d\n", |
| 525 | STD_LD_recfun); |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | #if UNIT_TEST |
| 530 | printf("The following variables after option and env parsing:\n"); |
| 531 | printf("STD_FUNCTIONAL_TEST = %d\n", STD_FUNCTIONAL_TEST); |
| 532 | printf("STD_LOOP_DURATION = %f\n", STD_LOOP_DURATION); |
| 533 | printf("STD_LOOP_DELAY = %f\n", STD_LOOP_DELAY); |
| 534 | printf("STD_COPIES = %d\n", STD_COPIES); |
| 535 | printf("STD_LOOP_COUNT = %d\n", STD_LOOP_COUNT); |
| 536 | printf("STD_INFINITE = %d\n", STD_INFINITE); |
| 537 | printf("STD_TIMING_ON = %d\n", STD_TIMING_ON); |
| 538 | printf("STD_ERRNO_LOG = %d\n", STD_ERRNO_LOG); |
| 539 | printf("STD_PAUSE = %d\n", STD_PAUSE); |
| 540 | #endif |
| 541 | |
| 542 | return((char *) NULL); |
| 543 | |
| 544 | } /* end of parse_opts */ |
| 545 | |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 546 | /********************************************************************* |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame] | 547 | * print_help() - print help message and user help message |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 548 | *********************************************************************/ |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame] | 549 | void print_help(void (*user_help)()) |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 550 | { |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame] | 551 | STD_opts_help(); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 552 | |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame] | 553 | if (user_help) user_help(); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | /********************************************************************* |
| 557 | * STD_opts_help() - return a help string for the STD_OPTIONS. |
| 558 | *********************************************************************/ |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame] | 559 | void |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 560 | STD_opts_help() |
| 561 | { |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame] | 562 | int i; |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 563 | |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame] | 564 | for(i = 0; std_options[i].optstr; ++i) { |
| 565 | if (std_options[i].help) |
| 566 | printf(std_options[i].help); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 567 | } |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | /* |
| 571 | * routine to goto when we get the SIGUSR1 for STD_PAUSE |
| 572 | */ |
| 573 | void STD_go(int sig) |
| 574 | { |
| 575 | return; |
| 576 | } |
| 577 | |
| 578 | /*********************************************************************** |
| 579 | * This function will do desired end of global setup test |
| 580 | * hooks. |
| 581 | * Currently it will only do a pause waiting for sigusr1 if |
| 582 | * STD_PAUSE is set. |
| 583 | * |
| 584 | ***********************************************************************/ |
| 585 | int |
| 586 | usc_global_setup_hook() |
| 587 | { |
| 588 | int cnt; |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame] | 589 | /* temp variable to store old signal action to be restored after pause */ |
| 590 | int (*_TMP_FUNC)(void); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 591 | |
| 592 | /* |
| 593 | * Fork STD_COPIES-1 copies. |
| 594 | */ |
| 595 | for(cnt=1;cnt<STD_COPIES;cnt++) { |
| 596 | switch(fork() ) { |
| 597 | case -1: |
| 598 | fprintf(stderr, "%s: fork() failed, errno:%d %s\n", |
| 599 | __FILE__, errno, strerror(errno)); |
| 600 | break; |
| 601 | case 0: /* child */ |
| 602 | cnt=STD_COPIES; /* to stop the forking */ |
| 603 | break; |
| 604 | |
| 605 | default: /* parent */ |
| 606 | break; |
| 607 | } |
| 608 | } |
| 609 | |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 610 | /* |
| 611 | * pause waiting for sigusr1. |
| 612 | */ |
| 613 | if ( STD_PAUSE ) { |
| 614 | _TMP_FUNC = (int (*)())signal(SIGUSR1, STD_go); |
| 615 | pause(); |
| 616 | signal(SIGUSR1, (void (*)())_TMP_FUNC); |
| 617 | } |
| 618 | |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 619 | |
| 620 | if ( STD_TP_sbrk || STD_LP_sbrk) { |
| 621 | STD_start_break=sbrk(0); /* get original sbreak size */ |
| 622 | } |
| 623 | |
| 624 | if ( STD_TP_sbrk ) { |
| 625 | sbrk(STD_TP_sbrk); |
| 626 | if ( Debug ) |
| 627 | printf("after sbrk(%d)\n", STD_TP_sbrk); |
| 628 | } |
| 629 | return 0; |
| 630 | } |
| 631 | |
| 632 | #define USECS_PER_SEC 1000000 /* microseconds per second */ |
| 633 | |
| 634 | /*********************************************************************** |
| 635 | * This function returns the number of get_current_time()'s return |
| 636 | * per second. |
| 637 | ***********************************************************************/ |
| 638 | |
| 639 | static int |
| 640 | get_timepersec() |
| 641 | { |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 642 | return USECS_PER_SEC; /* microseconds per second */ |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 643 | |
| 644 | } |
| 645 | |
| 646 | /*********************************************************************** |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame] | 647 | * this function will get current time in microseconds since 1970. |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 648 | ***********************************************************************/ |
| 649 | static int |
| 650 | get_current_time() |
| 651 | { |
| 652 | struct timeval curtime; |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 653 | |
| 654 | gettimeofday(&curtime, NULL); |
| 655 | |
| 656 | /* microseconds since 1970 */ |
| 657 | return (curtime.tv_sec*USECS_PER_SEC) + curtime.tv_usec; |
| 658 | |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 659 | |
| 660 | } |
| 661 | |
| 662 | /*********************************************************************** |
| 663 | * |
| 664 | * This function will determine if test should continue iterating |
| 665 | * If the STD_INFINITE flag is set, return 1. |
| 666 | * If the STD_LOOP_COUNT variable is set, compare it against |
| 667 | * the counter. |
| 668 | * If the STD_LOOP_DURATION variable is set, compare current time against |
| 669 | * calculated stop_time. |
| 670 | * This function will return 1 until all desired looping methods |
| 671 | * have been met. |
| 672 | * |
| 673 | * counter integer is supplied by the user program. |
| 674 | ***********************************************************************/ |
| 675 | int |
| 676 | usc_test_looping(counter) |
| 677 | int counter; |
| 678 | { |
| 679 | static int first_time = 1; |
| 680 | static int stop_time = 0; /* stop time in rtc or usecs */ |
| 681 | static int delay; /* delay in clocks or usecs */ |
| 682 | int hz=0; /* clocks per second or usecs per second */ |
| 683 | int ct, end; /* current time, end delay time */ |
| 684 | int keepgoing=0; /* used to determine return value */ |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 685 | |
| 686 | /* |
| 687 | * If this is the first iteration and we are looping for |
| 688 | * duration of STD_LOOP_DURATION seconds (fractional) or |
| 689 | * doing loop delays, get the clocks per second. |
| 690 | */ |
| 691 | if ( first_time ) { |
| 692 | |
| 693 | first_time=0; |
| 694 | if ( STD_LOOP_DELAY || STD_LOOP_DURATION ) { |
| 695 | hz = get_timepersec(); |
| 696 | } |
| 697 | |
| 698 | /* |
| 699 | * If looping for duration, calculate stop time in |
| 700 | * clocks. |
| 701 | */ |
| 702 | |
| 703 | if ( STD_LOOP_DURATION) { |
| 704 | ct=get_current_time(); |
| 705 | stop_time=(int)((float)hz * STD_LOOP_DURATION) + ct; |
| 706 | } |
| 707 | |
| 708 | /* |
| 709 | * If doing delay each iteration, calcuate the number |
| 710 | * of clocks for each delay. |
| 711 | */ |
| 712 | if ( STD_LOOP_DELAY ) { |
| 713 | delay=(int)((float)hz * STD_LOOP_DELAY); |
| 714 | } |
| 715 | |
| 716 | } |
| 717 | |
| 718 | /* |
| 719 | * if delay each iteration, loop for delay clocks. |
| 720 | * This will not be done on first iteration. |
| 721 | * The delay will happen before determining if |
| 722 | * there will be another iteration. |
| 723 | */ |
| 724 | else if ( STD_LOOP_DELAY ) { |
| 725 | ct=get_current_time(); |
| 726 | end=ct+delay; |
| 727 | while ( ct < end ) { |
| 728 | /* |
| 729 | * The following are special test hooks in the delay loop. |
| 730 | */ |
| 731 | if ( STD_LD_recfun ) { |
| 732 | if ( Debug ) |
| 733 | printf("calling usc_recressive_func(0, %d, &STD_bigstack)\n", |
| 734 | STD_LD_recfun); |
| 735 | usc_recressive_func(0, STD_LD_recfun, &STD_bigstack); |
| 736 | } |
| 737 | |
| 738 | ct=get_current_time(); |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | if ( STD_INFINITE ) { |
| 743 | keepgoing++; |
| 744 | } |
| 745 | |
| 746 | if ( STD_LOOP_COUNT && counter < STD_LOOP_COUNT ) { |
| 747 | keepgoing++; |
| 748 | } |
| 749 | |
| 750 | if ( STD_LOOP_DURATION != 0.0 && get_current_time() < stop_time ) { |
| 751 | keepgoing++; |
| 752 | } |
| 753 | |
| 754 | if ( keepgoing == 0 ) |
| 755 | return 0; |
| 756 | |
| 757 | /* |
| 758 | * The following code allows special system testing hooks. |
| 759 | */ |
| 760 | |
| 761 | if ( STD_LP_recfun ) { |
| 762 | if ( Debug ) |
| 763 | printf("calling usc_recressive_func(0, %d, &STD_bigstack)\n", |
| 764 | STD_LP_recfun); |
| 765 | usc_recressive_func(0, STD_LP_recfun, &STD_bigstack); |
| 766 | } |
| 767 | |
| 768 | if ( STD_LP_sbrk ) { |
| 769 | if ( Debug ) |
| 770 | printf("about to do sbrk(%d)\n", STD_LP_sbrk); |
| 771 | sbrk(STD_LP_sbrk); |
| 772 | } |
| 773 | |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 774 | |
| 775 | if ( keepgoing ) |
| 776 | return 1; |
| 777 | else |
| 778 | return 0; /* done - stop iterating */ |
| 779 | } |
| 780 | |
| 781 | |
| 782 | /* |
| 783 | * This function recressively calls itself max times. |
| 784 | */ |
| 785 | static void |
| 786 | usc_recressive_func(cnt, max, bstack) |
| 787 | int cnt; |
| 788 | int max; |
| 789 | struct usc_bigstack_t bstack; |
| 790 | { |
| 791 | if ( cnt < max ) |
| 792 | usc_recressive_func(cnt+1, max, bstack); |
| 793 | |
| 794 | } |
| 795 | |
| 796 | #if UNIT_TEST |
| 797 | /****************************************************************************** |
| 798 | * UNIT TEST CODE |
| 799 | * UNIT TEST CODE |
| 800 | * |
| 801 | * this following code is provide so that unit testing can |
| 802 | * be done fairly easily. |
| 803 | ******************************************************************************/ |
| 804 | |
| 805 | int Help = 0; |
| 806 | int Help2 = 0; |
| 807 | char *ptr; |
| 808 | |
| 809 | /* |
| 810 | * Code from usctest.h that not part of this file since we are the library. |
| 811 | */ |
| 812 | |
| 813 | struct usc_errno_t TEST_VALID_ENO[USC_MAX_ERRNO]; |
| 814 | |
| 815 | /*********************************************************************** |
| 816 | * Globals for returning the return code and errno from the system call |
| 817 | * test macros. |
| 818 | ***********************************************************************/ |
| 819 | int TEST_RETURN; |
| 820 | int TEST_ERRNO; |
| 821 | |
| 822 | /*********************************************************************** |
| 823 | * temporary variables for determining max and min times in TEST macro |
| 824 | ***********************************************************************/ |
| 825 | long btime, etime, tmptime; |
| 826 | |
| 827 | |
| 828 | |
| 829 | /* for test specific parse_opts options */ |
| 830 | option_t Options[] = { |
| 831 | { "help", &Help2, NULL }, /* -help option */ |
| 832 | { "h", &Help, NULL }, /* -h option */ |
| 833 | { TIMING, NULL, NULL}, /* disable -timing option */ |
| 834 | |
| 835 | #if INVALID_TEST_CASES |
| 836 | { "missingflag", NULL, &ptr }, /* error */ |
| 837 | { "missingarg:", &Help, NULL }, /* error */ |
| 838 | #endif /* INVALID_TEST_CASES */ |
| 839 | |
| 840 | { NULL, NULL, NULL } |
| 841 | }; |
| 842 | |
| 843 | |
| 844 | main(argc, argv) |
| 845 | int argc; |
| 846 | char **argv; |
| 847 | { |
| 848 | int lc; |
| 849 | char *msg; |
| 850 | struct timeval t; |
| 851 | int cnt; |
| 852 | |
| 853 | if ( (msg=parse_opts(argc, argv, |
| 854 | (option_t *) Options)) != (char *) NULL ) { |
| 855 | printf("ERROR : %s\n", msg); |
| 856 | exit(1); |
| 857 | } |
| 858 | |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 859 | TEST_PAUSE; |
| 860 | |
| 861 | for (lc=0; TEST_LOOPING(lc); lc++) { |
| 862 | |
| 863 | TEST( gettimeofday(&t, NULL) ); |
| 864 | printf("iter=%d: sec:%d, usec:%6.6d %s", lc+1, t.tv_sec, |
| 865 | t.tv_usec, ctime(&t.tv_sec)); |
| 866 | } |
| 867 | |
| 868 | |
| 869 | TEST_CLEANUP; |
| 870 | |
| 871 | exit(0); |
| 872 | } |
| 873 | |
| 874 | #endif /* UNIT_TEST */ |