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