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