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