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 | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame^] | 33 | /* $Id: parse_opts.c,v 1.2 2000/08/30 18:43:38 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[] = { |
| 148 | { "f" , " -f Turn off functional testing\n", NULL, NULL}, |
| 149 | { "i:", " -i n Execute test n times\n", NULL, NULL}, |
| 150 | { "I:", " -I n Execute test for n seconds\n", NULL, NULL}, |
| 151 | { "p" , " -p Pause for SIGUSR1 before starting\n", NULL, NULL}, |
| 152 | { "P:", " -P n Pause for n seconds between iterations\n", NULL, NULL}, |
| 153 | { "e" , " -e Turn on errno logging\n", NULL, NULL}, |
| 154 | { "t" , " -t Turn on syscall timing\n", NULL, NULL}, |
| 155 | { "c:", " -c n Run n copies concurrently\n", NULL, NULL}, |
| 156 | { "h" , " -h Show this help screen\n", NULL, NULL}, |
| 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 | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame^] | 235 | for (i = 0; std_options[i].optstr; ++i) |
| 236 | strcat(optionstr, std_options[i].optstr); |
| 237 | if (user_optarr) |
| 238 | for (i = 0; user_optarr[i].option; ++i) |
| 239 | /* only add the option if it wasn't there already */ |
| 240 | if (strchr(optionstr, user_optarr[i].option[0]) == NULL) |
| 241 | strcat(optionstr, user_optarr[i].option); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 242 | |
| 243 | #if DEBUG > 1 |
| 244 | printf("STD_nopts = %d\n", STD_nopts); |
| 245 | #endif |
| 246 | |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 247 | /* |
| 248 | * Loop through av parsing options. |
| 249 | */ |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame^] | 250 | while ( (opt = getopt(ac, av, optionstr)) > 0) { |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 251 | |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame^] | 252 | STD_argind = optind; |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 253 | #if DEBUG > 0 |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame^] | 254 | printf("parse_opts: getopt returned '%c'\n", opt); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 255 | #endif |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 256 | |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame^] | 257 | switch (opt) { |
| 258 | case '?': /* Unknown option */ |
| 259 | return "Unknown option"; |
| 260 | break; |
| 261 | case ':': /* Missing Arg */ |
| 262 | return "Missing argument"; |
| 263 | break; |
| 264 | case 'i': /* Iterations */ |
| 265 | options |= OPT_iteration; |
| 266 | STD_LOOP_COUNT = atoi(optarg); |
| 267 | if (STD_LOOP_COUNT == 0) STD_INFINITE = 1; |
| 268 | break; |
| 269 | case 'P': /* Delay between iterations */ |
| 270 | options |= OPT_delay; |
| 271 | STD_LOOP_DELAY = atof(optarg); |
| 272 | break; |
| 273 | case 'I': /* Time duration */ |
| 274 | options |= OPT_duration; |
| 275 | STD_LOOP_DURATION = atof(optarg); |
| 276 | if ( STD_LOOP_DURATION == 0.0 ) STD_INFINITE=1; |
| 277 | break; |
| 278 | case 'c': /* Copies */ |
| 279 | options |= OPT_copies; |
| 280 | STD_COPIES = atoi(optarg); |
| 281 | break; |
| 282 | case 'f': /* Functional testing */ |
| 283 | STD_FUNCTIONAL_TEST = 0; |
| 284 | break; |
| 285 | case 'p': /* Pause for SIGUSR1 */ |
| 286 | STD_PAUSE = 1; |
| 287 | break; |
| 288 | case 't': /* syscall timing */ |
| 289 | STD_TIMING_ON = 1; |
| 290 | break; |
| 291 | case 'e': /* errno loggin */ |
| 292 | STD_ERRNO_LOG = 1; |
| 293 | break; |
| 294 | case 'h': /* Help */ |
| 295 | print_help(uhf); |
| 296 | exit(0); |
| 297 | break; |
| 298 | default: |
| 299 | |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 300 | /* Check all the user specified options */ |
| 301 | found=0; |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame^] | 302 | for(i = 0; user_optarr[i].option; ++i) { |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 303 | |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame^] | 304 | if (opt == user_optarr[i].option[0]) { |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 305 | /* Yup, This is a user option, set the flag and look for argument */ |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame^] | 306 | if ( user_optarr[i].flag ) { |
| 307 | *user_optarr[i].flag=1; |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 308 | } |
| 309 | found++; |
| 310 | |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 311 | /* save the argument at the user's location */ |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame^] | 312 | if ( user_optarr[i].option[strlen(user_optarr[i].option)-1] == ':' ) { |
| 313 | *user_optarr[i].arg=optarg; |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 314 | } |
| 315 | break; /* option found - break out of the for loop */ |
| 316 | } |
| 317 | } |
| 318 | /* This condition "should never happen". SO CHECK FOR IT!!!! */ |
| 319 | if ( ! found ) { |
| 320 | sprintf(Mesg2, |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame^] | 321 | "parse_opts: ERROR - option:\"%c\" NOT FOUND... INTERNAL ERROR", opt); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 322 | return(Mesg2); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | } /* end of while */ |
| 327 | |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame^] | 328 | STD_argind = optind; |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 329 | |
| 330 | /* |
| 331 | * Turn on debug |
| 332 | */ |
| 333 | if ( (ptr=getenv("USC_DEBUG")) != NULL ) { |
| 334 | Debug=1; |
| 335 | printf("env USC_DEBUG is defined, turning on debug\n"); |
| 336 | } |
| 337 | if ( (ptr=getenv("USC_VERBOSE")) != NULL ) { |
| 338 | Debug=1; |
| 339 | printf("env USC_VERBOSE is defined, turning on debug\n"); |
| 340 | } |
| 341 | |
| 342 | /* |
| 343 | * If the USC_ITERATION_ENV environmental variable is set to |
| 344 | * a number, use that number as iteration count (same as -c option). |
| 345 | * The -c option with arg will be used even if this env var is set. |
| 346 | */ |
| 347 | if ( !(options & OPT_iteration) && (ptr=getenv(USC_ITERATION_ENV)) != NULL ) { |
| 348 | if ( sscanf(ptr, "%i", &k) == 1) { |
| 349 | if ( k == 0 ) { /* if arg is 0, set infinite loop flag */ |
| 350 | STD_INFINITE=1; |
| 351 | if ( Debug ) |
| 352 | printf("Using env %s, set STD_INFINITE to 1\n", |
| 353 | USC_ITERATION_ENV); |
| 354 | } else { /* else, set the loop count to the arguement */ |
| 355 | STD_LOOP_COUNT=k; |
| 356 | if ( Debug ) |
| 357 | printf("Using env %s, set STD_LOOP_COUNT to %d\n", |
| 358 | USC_ITERATION_ENV, k); |
| 359 | } |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | /* |
| 364 | * If the USC_NO_FUNC_CHECK environmental variable is set, we'll |
| 365 | * unset the STD_FUNCTIONAL_TEST variable. |
| 366 | */ |
| 367 | if ( !(options & OPT_nofunccheck) && (ptr=getenv(USC_NO_FUNC_CHECK)) != NULL ) { |
| 368 | STD_FUNCTIONAL_TEST=0; /* Turn off functional testing */ |
| 369 | if ( Debug ) |
| 370 | printf("Using env %s, set STD_FUNCTIONAL_TEST to 0\n", |
| 371 | USC_NO_FUNC_CHECK); |
| 372 | } |
| 373 | |
| 374 | /* |
| 375 | * If the USC_LOOP_WALLTIME environmental variable is set, |
| 376 | * use that number as duration (same as -I option). |
| 377 | * The -I option with arg will be used even if this env var is set. |
| 378 | */ |
| 379 | |
| 380 | if ( !(options & OPT_duration) && (ptr=getenv(USC_LOOP_WALLTIME)) != NULL ) { |
| 381 | if ( sscanf(ptr, "%f", &ftmp) == 1 && ftmp >= 0.0 ) { |
| 382 | STD_LOOP_DURATION=ftmp; |
| 383 | if ( Debug ) |
| 384 | printf("Using env %s, set STD_LOOP_DURATION to %f\n", |
| 385 | USC_LOOP_WALLTIME, ftmp); |
| 386 | if ( STD_LOOP_DURATION == 0.0 ) { /* if arg is 0, set infinite loop flag */ |
| 387 | STD_INFINITE=1; |
| 388 | if ( Debug ) |
| 389 | printf("Using env %s, set STD_INFINITE to 1\n", USC_LOOP_WALLTIME); |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | if ( !(options & OPT_duration) && (ptr=getenv("USC_DURATION")) != NULL ) { |
| 394 | if ( sscanf(ptr, "%f", &ftmp) == 1 && ftmp >= 0.0 ) { |
| 395 | STD_LOOP_DURATION=ftmp; |
| 396 | if ( Debug ) |
| 397 | printf("Using env USC_DURATION, set STD_LOOP_DURATION to %f\n", ftmp); |
| 398 | if ( STD_LOOP_DURATION == 0.0 ) { /* if arg is 0, set infinite loop flag */ |
| 399 | STD_INFINITE=1; |
| 400 | if ( Debug ) |
| 401 | printf("Using env USC_DURATION, set STD_INFINITE to 1\n"); |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | /* |
| 406 | * If the USC_LOOP_DELAY environmental variable is set, |
| 407 | * use that number as delay in factional seconds (same as -P option). |
| 408 | * The -P option with arg will be used even if this env var is set. |
| 409 | */ |
| 410 | if ( !(options & OPT_delay) && (ptr=getenv(USC_LOOP_DELAY)) != NULL ) { |
| 411 | if ( sscanf(ptr, "%f", &ftmp) == 1 && ftmp >= 0.0 ) { |
| 412 | STD_LOOP_DELAY=ftmp; |
| 413 | if ( Debug ) |
| 414 | printf("Using env %s, set STD_LOOP_DELAY = %f\n", |
| 415 | USC_LOOP_DELAY, ftmp); |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | /* |
| 420 | * If the USC_COPIES environmental variable is set, |
| 421 | * use that number as copies (same as -c option). |
| 422 | * The -c option with arg will be used even if this env var is set. |
| 423 | */ |
| 424 | if ( !(options & OPT_copies) && (ptr=getenv(USC_COPIES)) != NULL ) { |
| 425 | if ( sscanf(ptr, "%d", &STD_COPIES) == 1 && STD_COPIES >= 0 ) { |
| 426 | if ( Debug ) |
| 427 | printf("Using env %s, set STD_COPIES = %d\n", |
| 428 | USC_COPIES, STD_COPIES); |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | /* |
| 433 | * The following are special system testing envs to turn on special |
| 434 | * hooks in the code. |
| 435 | */ |
| 436 | if ( (ptr=getenv("USC_TP_BARRIER")) != NULL ) { |
| 437 | if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) { |
| 438 | STD_TP_barrier=k; |
| 439 | } |
| 440 | else |
| 441 | STD_TP_barrier=1; |
| 442 | if ( Debug ) |
| 443 | printf("using env USC_TP_BARRIER, Set STD_TP_barrier to %d\n", |
| 444 | STD_TP_barrier); |
| 445 | } |
| 446 | |
| 447 | if ( (ptr=getenv("USC_LP_BARRIER")) != NULL ) { |
| 448 | if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) { |
| 449 | STD_LP_barrier=k; |
| 450 | } |
| 451 | else |
| 452 | STD_LP_barrier=1; |
| 453 | if ( Debug ) |
| 454 | printf("using env USC_LP_BARRIER, Set STD_LP_barrier to %d\n", |
| 455 | STD_LP_barrier); |
| 456 | } |
| 457 | |
| 458 | if ( (ptr=getenv("USC_TP_SHMEM")) != NULL ) { |
| 459 | if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) { |
| 460 | STD_TP_shmem_sz=k; |
| 461 | if ( Debug ) |
| 462 | printf("Using env USC_TP_SHMEM, Set STD_TP_shmem_sz to %d\n", |
| 463 | STD_TP_shmem_sz); |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | if ( (ptr=getenv("USC_LP_SHMEM")) != NULL ) { |
| 468 | if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) { |
| 469 | STD_LP_shmem=k; |
| 470 | if ( Debug ) |
| 471 | printf("Using env USC_LP_SHMEM, Set STD_LP_shmem to %d\n", |
| 472 | STD_LP_shmem); |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | if ( (ptr=getenv("USC_LD_SHMEM")) != NULL ) { |
| 477 | if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) { |
| 478 | STD_LD_shmem=k; |
| 479 | if ( Debug ) |
| 480 | printf("Using env USC_LD_SHMEM, Set STD_LD_shmem to %d\n", |
| 481 | STD_LD_shmem); |
| 482 | } |
| 483 | } |
| 484 | |
| 485 | if ( (ptr=getenv("USC_TP_SBRK")) != NULL ) { |
| 486 | if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) { |
| 487 | STD_TP_sbrk=k; |
| 488 | if ( Debug ) |
| 489 | printf("Using env USC_TP_SBRK, Set STD_TP_sbrk to %d\n", |
| 490 | STD_TP_sbrk); |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | if ( (ptr=getenv("USC_LP_SBRK")) != NULL ) { |
| 495 | if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) { |
| 496 | STD_LP_sbrk=k; |
| 497 | if ( Debug ) |
| 498 | printf("Using env USC_LP_SBRK, Set STD_LP_sbrk to %d\n", |
| 499 | STD_LP_sbrk); |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | if ( (ptr=getenv("USC_LP_RECFUN")) != NULL ) { |
| 504 | if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) { |
| 505 | STD_LP_recfun=k; |
| 506 | if ( STD_bigstack != (struct usc_bigstack_t *)NULL ) |
| 507 | STD_bigstack=(struct usc_bigstack_t *) |
| 508 | malloc(sizeof(struct usc_bigstack_t)); |
| 509 | if ( Debug ) |
| 510 | printf("Using env USC_LP_RECFUN, Set STD_LP_recfun to %d\n", |
| 511 | STD_LP_recfun); |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | if ( (ptr=getenv("USC_LD_RECFUN")) != NULL ) { |
| 516 | if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) { |
| 517 | STD_LD_recfun=k; |
| 518 | if ( STD_bigstack != (struct usc_bigstack_t *)NULL ) |
| 519 | STD_bigstack=(struct usc_bigstack_t *) |
| 520 | malloc(sizeof(struct usc_bigstack_t)); |
| 521 | if ( Debug ) |
| 522 | printf("Using env USC_LD_RECFUN, Set STD_LD_recfun to %d\n", |
| 523 | STD_LD_recfun); |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | #if UNIT_TEST |
| 528 | printf("The following variables after option and env parsing:\n"); |
| 529 | printf("STD_FUNCTIONAL_TEST = %d\n", STD_FUNCTIONAL_TEST); |
| 530 | printf("STD_LOOP_DURATION = %f\n", STD_LOOP_DURATION); |
| 531 | printf("STD_LOOP_DELAY = %f\n", STD_LOOP_DELAY); |
| 532 | printf("STD_COPIES = %d\n", STD_COPIES); |
| 533 | printf("STD_LOOP_COUNT = %d\n", STD_LOOP_COUNT); |
| 534 | printf("STD_INFINITE = %d\n", STD_INFINITE); |
| 535 | printf("STD_TIMING_ON = %d\n", STD_TIMING_ON); |
| 536 | printf("STD_ERRNO_LOG = %d\n", STD_ERRNO_LOG); |
| 537 | printf("STD_PAUSE = %d\n", STD_PAUSE); |
| 538 | #endif |
| 539 | |
| 540 | return((char *) NULL); |
| 541 | |
| 542 | } /* end of parse_opts */ |
| 543 | |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 544 | /********************************************************************* |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame^] | 545 | * print_help() - print help message and user help message |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 546 | *********************************************************************/ |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame^] | 547 | void print_help(void (*user_help)()) |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 548 | { |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame^] | 549 | STD_opts_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 | if (user_help) user_help(); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | /********************************************************************* |
| 555 | * STD_opts_help() - return a help string for the STD_OPTIONS. |
| 556 | *********************************************************************/ |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame^] | 557 | void |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 558 | STD_opts_help() |
| 559 | { |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame^] | 560 | int i; |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 561 | |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame^] | 562 | for(i = 0; std_options[i].optstr; ++i) { |
| 563 | if (std_options[i].help) |
| 564 | printf(std_options[i].help); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 565 | } |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | /* |
| 569 | * routine to goto when we get the SIGUSR1 for STD_PAUSE |
| 570 | */ |
| 571 | void STD_go(int sig) |
| 572 | { |
| 573 | return; |
| 574 | } |
| 575 | |
| 576 | /*********************************************************************** |
| 577 | * This function will do desired end of global setup test |
| 578 | * hooks. |
| 579 | * Currently it will only do a pause waiting for sigusr1 if |
| 580 | * STD_PAUSE is set. |
| 581 | * |
| 582 | ***********************************************************************/ |
| 583 | int |
| 584 | usc_global_setup_hook() |
| 585 | { |
| 586 | int cnt; |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame^] | 587 | /* temp variable to store old signal action to be restored after pause */ |
| 588 | int (*_TMP_FUNC)(void); |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 589 | |
| 590 | /* |
| 591 | * Fork STD_COPIES-1 copies. |
| 592 | */ |
| 593 | for(cnt=1;cnt<STD_COPIES;cnt++) { |
| 594 | switch(fork() ) { |
| 595 | case -1: |
| 596 | fprintf(stderr, "%s: fork() failed, errno:%d %s\n", |
| 597 | __FILE__, errno, strerror(errno)); |
| 598 | break; |
| 599 | case 0: /* child */ |
| 600 | cnt=STD_COPIES; /* to stop the forking */ |
| 601 | break; |
| 602 | |
| 603 | default: /* parent */ |
| 604 | break; |
| 605 | } |
| 606 | } |
| 607 | |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 608 | /* |
| 609 | * pause waiting for sigusr1. |
| 610 | */ |
| 611 | if ( STD_PAUSE ) { |
| 612 | _TMP_FUNC = (int (*)())signal(SIGUSR1, STD_go); |
| 613 | pause(); |
| 614 | signal(SIGUSR1, (void (*)())_TMP_FUNC); |
| 615 | } |
| 616 | |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 617 | |
| 618 | if ( STD_TP_sbrk || STD_LP_sbrk) { |
| 619 | STD_start_break=sbrk(0); /* get original sbreak size */ |
| 620 | } |
| 621 | |
| 622 | if ( STD_TP_sbrk ) { |
| 623 | sbrk(STD_TP_sbrk); |
| 624 | if ( Debug ) |
| 625 | printf("after sbrk(%d)\n", STD_TP_sbrk); |
| 626 | } |
| 627 | return 0; |
| 628 | } |
| 629 | |
| 630 | #define USECS_PER_SEC 1000000 /* microseconds per second */ |
| 631 | |
| 632 | /*********************************************************************** |
| 633 | * This function returns the number of get_current_time()'s return |
| 634 | * per second. |
| 635 | ***********************************************************************/ |
| 636 | |
| 637 | static int |
| 638 | get_timepersec() |
| 639 | { |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 640 | return USECS_PER_SEC; /* microseconds per second */ |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 641 | |
| 642 | } |
| 643 | |
| 644 | /*********************************************************************** |
nstraz | 9418108 | 2000-08-30 18:43:38 +0000 | [diff] [blame^] | 645 | * this function will get current time in microseconds since 1970. |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 646 | ***********************************************************************/ |
| 647 | static int |
| 648 | get_current_time() |
| 649 | { |
| 650 | struct timeval curtime; |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 651 | |
| 652 | gettimeofday(&curtime, NULL); |
| 653 | |
| 654 | /* microseconds since 1970 */ |
| 655 | return (curtime.tv_sec*USECS_PER_SEC) + curtime.tv_usec; |
| 656 | |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 657 | |
| 658 | } |
| 659 | |
| 660 | /*********************************************************************** |
| 661 | * |
| 662 | * This function will determine if test should continue iterating |
| 663 | * If the STD_INFINITE flag is set, return 1. |
| 664 | * If the STD_LOOP_COUNT variable is set, compare it against |
| 665 | * the counter. |
| 666 | * If the STD_LOOP_DURATION variable is set, compare current time against |
| 667 | * calculated stop_time. |
| 668 | * This function will return 1 until all desired looping methods |
| 669 | * have been met. |
| 670 | * |
| 671 | * counter integer is supplied by the user program. |
| 672 | ***********************************************************************/ |
| 673 | int |
| 674 | usc_test_looping(counter) |
| 675 | int counter; |
| 676 | { |
| 677 | static int first_time = 1; |
| 678 | static int stop_time = 0; /* stop time in rtc or usecs */ |
| 679 | static int delay; /* delay in clocks or usecs */ |
| 680 | int hz=0; /* clocks per second or usecs per second */ |
| 681 | int ct, end; /* current time, end delay time */ |
| 682 | int keepgoing=0; /* used to determine return value */ |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 683 | |
| 684 | /* |
| 685 | * If this is the first iteration and we are looping for |
| 686 | * duration of STD_LOOP_DURATION seconds (fractional) or |
| 687 | * doing loop delays, get the clocks per second. |
| 688 | */ |
| 689 | if ( first_time ) { |
| 690 | |
| 691 | first_time=0; |
| 692 | if ( STD_LOOP_DELAY || STD_LOOP_DURATION ) { |
| 693 | hz = get_timepersec(); |
| 694 | } |
| 695 | |
| 696 | /* |
| 697 | * If looping for duration, calculate stop time in |
| 698 | * clocks. |
| 699 | */ |
| 700 | |
| 701 | if ( STD_LOOP_DURATION) { |
| 702 | ct=get_current_time(); |
| 703 | stop_time=(int)((float)hz * STD_LOOP_DURATION) + ct; |
| 704 | } |
| 705 | |
| 706 | /* |
| 707 | * If doing delay each iteration, calcuate the number |
| 708 | * of clocks for each delay. |
| 709 | */ |
| 710 | if ( STD_LOOP_DELAY ) { |
| 711 | delay=(int)((float)hz * STD_LOOP_DELAY); |
| 712 | } |
| 713 | |
| 714 | } |
| 715 | |
| 716 | /* |
| 717 | * if delay each iteration, loop for delay clocks. |
| 718 | * This will not be done on first iteration. |
| 719 | * The delay will happen before determining if |
| 720 | * there will be another iteration. |
| 721 | */ |
| 722 | else if ( STD_LOOP_DELAY ) { |
| 723 | ct=get_current_time(); |
| 724 | end=ct+delay; |
| 725 | while ( ct < end ) { |
| 726 | /* |
| 727 | * The following are special test hooks in the delay loop. |
| 728 | */ |
| 729 | if ( STD_LD_recfun ) { |
| 730 | if ( Debug ) |
| 731 | printf("calling usc_recressive_func(0, %d, &STD_bigstack)\n", |
| 732 | STD_LD_recfun); |
| 733 | usc_recressive_func(0, STD_LD_recfun, &STD_bigstack); |
| 734 | } |
| 735 | |
| 736 | ct=get_current_time(); |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | if ( STD_INFINITE ) { |
| 741 | keepgoing++; |
| 742 | } |
| 743 | |
| 744 | if ( STD_LOOP_COUNT && counter < STD_LOOP_COUNT ) { |
| 745 | keepgoing++; |
| 746 | } |
| 747 | |
| 748 | if ( STD_LOOP_DURATION != 0.0 && get_current_time() < stop_time ) { |
| 749 | keepgoing++; |
| 750 | } |
| 751 | |
| 752 | if ( keepgoing == 0 ) |
| 753 | return 0; |
| 754 | |
| 755 | /* |
| 756 | * The following code allows special system testing hooks. |
| 757 | */ |
| 758 | |
| 759 | if ( STD_LP_recfun ) { |
| 760 | if ( Debug ) |
| 761 | printf("calling usc_recressive_func(0, %d, &STD_bigstack)\n", |
| 762 | STD_LP_recfun); |
| 763 | usc_recressive_func(0, STD_LP_recfun, &STD_bigstack); |
| 764 | } |
| 765 | |
| 766 | if ( STD_LP_sbrk ) { |
| 767 | if ( Debug ) |
| 768 | printf("about to do sbrk(%d)\n", STD_LP_sbrk); |
| 769 | sbrk(STD_LP_sbrk); |
| 770 | } |
| 771 | |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 772 | |
| 773 | if ( keepgoing ) |
| 774 | return 1; |
| 775 | else |
| 776 | return 0; /* done - stop iterating */ |
| 777 | } |
| 778 | |
| 779 | |
| 780 | /* |
| 781 | * This function recressively calls itself max times. |
| 782 | */ |
| 783 | static void |
| 784 | usc_recressive_func(cnt, max, bstack) |
| 785 | int cnt; |
| 786 | int max; |
| 787 | struct usc_bigstack_t bstack; |
| 788 | { |
| 789 | if ( cnt < max ) |
| 790 | usc_recressive_func(cnt+1, max, bstack); |
| 791 | |
| 792 | } |
| 793 | |
| 794 | #if UNIT_TEST |
| 795 | /****************************************************************************** |
| 796 | * UNIT TEST CODE |
| 797 | * UNIT TEST CODE |
| 798 | * |
| 799 | * this following code is provide so that unit testing can |
| 800 | * be done fairly easily. |
| 801 | ******************************************************************************/ |
| 802 | |
| 803 | int Help = 0; |
| 804 | int Help2 = 0; |
| 805 | char *ptr; |
| 806 | |
| 807 | /* |
| 808 | * Code from usctest.h that not part of this file since we are the library. |
| 809 | */ |
| 810 | |
| 811 | struct usc_errno_t TEST_VALID_ENO[USC_MAX_ERRNO]; |
| 812 | |
| 813 | /*********************************************************************** |
| 814 | * Globals for returning the return code and errno from the system call |
| 815 | * test macros. |
| 816 | ***********************************************************************/ |
| 817 | int TEST_RETURN; |
| 818 | int TEST_ERRNO; |
| 819 | |
| 820 | /*********************************************************************** |
| 821 | * temporary variables for determining max and min times in TEST macro |
| 822 | ***********************************************************************/ |
| 823 | long btime, etime, tmptime; |
| 824 | |
| 825 | |
| 826 | |
| 827 | /* for test specific parse_opts options */ |
| 828 | option_t Options[] = { |
| 829 | { "help", &Help2, NULL }, /* -help option */ |
| 830 | { "h", &Help, NULL }, /* -h option */ |
| 831 | { TIMING, NULL, NULL}, /* disable -timing option */ |
| 832 | |
| 833 | #if INVALID_TEST_CASES |
| 834 | { "missingflag", NULL, &ptr }, /* error */ |
| 835 | { "missingarg:", &Help, NULL }, /* error */ |
| 836 | #endif /* INVALID_TEST_CASES */ |
| 837 | |
| 838 | { NULL, NULL, NULL } |
| 839 | }; |
| 840 | |
| 841 | |
| 842 | main(argc, argv) |
| 843 | int argc; |
| 844 | char **argv; |
| 845 | { |
| 846 | int lc; |
| 847 | char *msg; |
| 848 | struct timeval t; |
| 849 | int cnt; |
| 850 | |
| 851 | if ( (msg=parse_opts(argc, argv, |
| 852 | (option_t *) Options)) != (char *) NULL ) { |
| 853 | printf("ERROR : %s\n", msg); |
| 854 | exit(1); |
| 855 | } |
| 856 | |
alaffin | cc2e555 | 2000-07-27 17:13:18 +0000 | [diff] [blame] | 857 | TEST_PAUSE; |
| 858 | |
| 859 | for (lc=0; TEST_LOOPING(lc); lc++) { |
| 860 | |
| 861 | TEST( gettimeofday(&t, NULL) ); |
| 862 | printf("iter=%d: sec:%d, usec:%6.6d %s", lc+1, t.tv_sec, |
| 863 | t.tv_usec, ctime(&t.tv_sec)); |
| 864 | } |
| 865 | |
| 866 | |
| 867 | TEST_CLEANUP; |
| 868 | |
| 869 | exit(0); |
| 870 | } |
| 871 | |
| 872 | #endif /* UNIT_TEST */ |