blob: 8276a2ee0b21cefa8d1dbfa9abe868777038ca19 [file] [log] [blame]
alaffincc2e5552000-07-27 17:13:18 +00001/*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
vapier45a8ba02009-07-20 10:59:32 +00003 *
alaffincc2e5552000-07-27 17:13:18 +00004 * 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.
vapier45a8ba02009-07-20 10:59:32 +00007 *
alaffincc2e5552000-07-27 17:13:18 +00008 * 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.
vapier45a8ba02009-07-20 10:59:32 +000011 *
alaffincc2e5552000-07-27 17:13:18 +000012 * 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.
vapier45a8ba02009-07-20 10:59:32 +000018 *
alaffincc2e5552000-07-27 17:13:18 +000019 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
vapier45a8ba02009-07-20 10:59:32 +000022 *
alaffincc2e5552000-07-27 17:13:18 +000023 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
vapier45a8ba02009-07-20 10:59:32 +000025 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
alaffincc2e5552000-07-27 17:13:18 +000030 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
31 */
32
vapier3dc04702009-08-28 09:59:52 +000033/* $Id: parse_opts.c,v 1.14 2009/08/28 09:59:52 vapier Exp $ */
alaffincc2e5552000-07-27 17:13:18 +000034
35/**********************************************************
vapier45a8ba02009-07-20 10:59:32 +000036 *
alaffincc2e5552000-07-27 17:13:18 +000037 * OS Testing - Silicon Graphics, Inc.
vapier45a8ba02009-07-20 10:59:32 +000038 *
alaffincc2e5552000-07-27 17:13:18 +000039 * FUNCTION NAME : parse_opts
vapier45a8ba02009-07-20 10:59:32 +000040 *
alaffincc2e5552000-07-27 17:13:18 +000041 * FUNCTION TITLE : parse standard & user options for system call tests
vapier45a8ba02009-07-20 10:59:32 +000042 *
alaffincc2e5552000-07-27 17:13:18 +000043 * SYNOPSIS:
44 * #include "usctest.h"
45 *
nstraz94181082000-08-30 18:43:38 +000046 * char *parse_opts(ac, av, user_optarr, uhf)
alaffincc2e5552000-07-27 17:13:18 +000047 * int ac;
48 * char **av;
49 * option_t user_optarr[];
nstraz94181082000-08-30 18:43:38 +000050 * void (*uhf)();
vapier45a8ba02009-07-20 10:59:32 +000051 *
alaffincc2e5552000-07-27 17:13:18 +000052 * AUTHOR : William Roske/Richard Logan
vapier45a8ba02009-07-20 10:59:32 +000053 *
alaffincc2e5552000-07-27 17:13:18 +000054 * INITIAL RELEASE : UNICOS 7.0
vapier45a8ba02009-07-20 10:59:32 +000055 *
alaffincc2e5552000-07-27 17:13:18 +000056 * 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
nstraz94181082000-08-30 18:43:38 +000060 * associated with the options. It uses getopt to do the actual cmd line
61 * parsing. uhf() is a function to print user define help
alaffincc2e5552000-07-27 17:13:18 +000062 *
63 * This module contains the functions usc_global_setup_hook and
64 * usc_test_looping, which are called by marcos defined in usctest.h.
vapier45a8ba02009-07-20 10:59:32 +000065 *
alaffincc2e5552000-07-27 17:13:18 +000066 * 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.
vapier45a8ba02009-07-20 10:59:32 +000069 *
alaffincc2e5552000-07-27 17:13:18 +000070 *#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#**/
Mike Frysinger28606c12010-08-17 17:22:45 -040071#include "config.h"
alaffincc2e5552000-07-27 17:13:18 +000072#include <errno.h>
73#include <stdlib.h>
74#include <string.h>
75#include <sys/param.h>
76#include <sys/signal.h>
77#include <sys/types.h>
78#include <unistd.h>
79#include <sys/time.h>
80
81
alaffincc2e5552000-07-27 17:13:18 +000082#if UNIT_TEST
83#include <time.h>
84#endif /* UNIT_TEST */
85
86#include "test.h"
87#define _USC_LIB_ 1 /* indicates we are the library to the usctest.h include */
88#include "usctest.h"
alaffincc2e5552000-07-27 17:13:18 +000089
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
alaffincc2e5552000-07-27 17:13:18 +0000102/* The timing information block. */
103struct tblock tblock={0,((long) -1)>>1,0,0};
104
alaffincc2e5552000-07-27 17:13:18 +0000105
106/* Define flags and args for standard options */
107int STD_FUNCTIONAL_TEST=1, /* flag indicating to do functional testing code */
108 STD_TIMING_ON=0, /* flag indicating to print timing stats */
109 STD_PAUSE=0, /* flag indicating to pause before actual start, */
110 /* for contention mode */
111 STD_INFINITE=0, /* flag indciating to loop forever */
112 STD_LOOP_COUNT=1, /* number of iterations */
113 STD_COPIES=1, /* number of copies */
114 STD_ERRNO_LOG=0; /* flag indicating to do errno logging */
115
116float STD_LOOP_DURATION=0.0, /* duration value in fractional seconds */
117 STD_LOOP_DELAY=0.0; /* loop delay value in fractional seconds */
118
119
120char **STD_opt_arr = NULL; /* array of option strings */
121int STD_nopts=0, /* number of elements in STD_opt_arr */
122 STD_argind=1; /* argv index to next argv element */
123 /* (first argument) */
124 /* To getopt users, it is like optind */
125
126/*
127 * The following variables are to support system testing additions.
128 */
129static int STD_TP_barrier=0; /* flag to do barrier in TEST_PAUSE */
130 /* 2 - wait_barrier(), 3 - set_barrier(), * - barrier() */
131static int STD_LP_barrier=0; /* flag to do barrier in TEST_LOOPING */
132 /* 2 - wait_barrier(), 3 - set_barrier(), * - barrier() */
133static int STD_TP_shmem_sz=0; /* shmalloc this many words per pe in TEST_PAUSE */
134static int STD_LD_shmem=0; /* flag to do shmem_puts and shmem_gets during delay */
135static int STD_LP_shmem=0; /* flag to do shmem_puts and gets during TEST_LOOPING */
136static int STD_LD_recfun=0; /* do recressive function calls in loop delay */
137static int STD_LP_recfun=0; /* do recressive function calls in TEST_LOOPING */
138static int STD_TP_sbrk=0; /* do sbrk in TEST_PAUSE */
139static int STD_LP_sbrk=0; /* do sbrk in TEST_LOOPING */
140static char *STD_start_break=0; /* original sbrk size */
alaffincc2e5552000-07-27 17:13:18 +0000141static int Debug=0;
142
nstraz94181082000-08-30 18:43:38 +0000143struct std_option_t {
144 char *optstr;
145 char *help;
146 char *flag;
147 char **arg;
148} std_options[] = {
nstrazdef354d2000-08-31 18:40:28 +0000149 { "c:", " -c n Run n copies concurrently\n", NULL, NULL},
150 { "e" , " -e Turn on errno logging\n", NULL, NULL},
151 { "f" , " -f Turn off functional testing\n", NULL, NULL},
152 { "h" , " -h Show this help screen\n", NULL, NULL},
153 { "i:", " -i n Execute test n times\n", NULL, NULL},
154 { "I:", " -I x Execute test for x seconds\n", NULL, NULL},
vapier45a8ba02009-07-20 10:59:32 +0000155 { "p" , " -p Pause for SIGUSR1 before starting\n", NULL, NULL},
nstrazdef354d2000-08-31 18:40:28 +0000156 { "P:", " -P x Pause for x seconds between iterations\n", NULL, NULL},
157 { "t" , " -t Turn on syscall timing\n", NULL, NULL},
robbiewd34d5812005-07-11 22:28:09 +0000158#ifdef UCLINUX
159 { "C:", " -C ARG Run the child process with arguments ARG (for internal use)\n",
160 NULL, NULL},
161#endif
nstraz94181082000-08-30 18:43:38 +0000162 {NULL, NULL, NULL, NULL}};
163
164void print_help(void (*user_help)());
165
alaffincc2e5552000-07-27 17:13:18 +0000166/*
167 * Structure for usc_recressive_func argument
168 */
169struct usc_bigstack_t {
170 char space[4096];
171};
172
173static struct usc_bigstack_t *STD_bigstack=NULL;
174
alaffincc2e5552000-07-27 17:13:18 +0000175/*
176 * Counter of errnos returned (-e option). Indexed by errno.
177 * Make the array USC_MAX_ERRNO long. That is the first Fortran
178 * Lib errno. No syscall should return an errno that high.
179 */
180int STD_ERRNO_LIST[USC_MAX_ERRNO];
181
182/* define the string length for Mesg and Mesg2 strings */
183#define STRLEN 2048
184
alaffincc2e5552000-07-27 17:13:18 +0000185static char Mesg2[STRLEN]; /* holds possible return string */
186static void usc_recressive_func();
187
188/*
189 * Define bits for options that might have env variable default
190 */
191#define OPT_iteration 01
192#define OPT_nofunccheck 02
193#define OPT_duration 04
194#define OPT_delay 010
195#define OPT_copies 020
196
robbiewd34d5812005-07-11 22:28:09 +0000197#ifdef UCLINUX
198/* Allocated and used in self_exec.c: */
199extern char *child_args; /* Arguments to child when -C is used */
200#endif
alaffincc2e5552000-07-27 17:13:18 +0000201
202/**********************************************************************
vapier45a8ba02009-07-20 10:59:32 +0000203 * parse_opts:
alaffincc2e5552000-07-27 17:13:18 +0000204 **********************************************************************/
205char *
nstraz94181082000-08-30 18:43:38 +0000206parse_opts(int ac, char **av, option_t *user_optarr, void (*uhf)())
alaffincc2e5552000-07-27 17:13:18 +0000207{
208 int found; /* flag to indicate that an option specified was */
209 /* found in the user's list */
alaffincc2e5552000-07-27 17:13:18 +0000210 int k; /* scratch integer for returns and short time usage */
211 float ftmp; /* tmp float for parsing env variables */
212 char *ptr; /* used in getting env variables */
213 int options=0; /* no options specified */
nstraz94181082000-08-30 18:43:38 +0000214 int optstrlen, i;
215 char *optionstr;
216 int opt; /* return of getopt */
alaffincc2e5552000-07-27 17:13:18 +0000217
218 /*
219 * If not the first time this function is called, release the old STD_opt_arr
220 * vector.
221 */
222
223 if ( STD_opt_arr != NULL ) {
224 free(STD_opt_arr);
225 STD_opt_arr=NULL;
226 }
nstraz94181082000-08-30 18:43:38 +0000227 /* Calculate how much space we need for the option string */
228 optstrlen = 0;
vapier45a8ba02009-07-20 10:59:32 +0000229 for (i = 0; std_options[i].optstr; ++i)
nstraz94181082000-08-30 18:43:38 +0000230 optstrlen += strlen(std_options[i].optstr);
231 if (user_optarr)
232 for (i = 0; user_optarr[i].option; ++i) {
233 if (strlen(user_optarr[i].option) > 2)
234 return "parse_opts: ERROR - Only short options are allowed";
235 optstrlen += strlen(user_optarr[i].option);
236 }
237 optstrlen += 1;
alaffincc2e5552000-07-27 17:13:18 +0000238
nstraz94181082000-08-30 18:43:38 +0000239 /* Create the option string for getopt */
240 optionstr = (char *)malloc(optstrlen);
vapier45a8ba02009-07-20 10:59:32 +0000241 if (!optionstr)
nstraz94181082000-08-30 18:43:38 +0000242 return "parse_opts: ERROR - Could not allocate memory for optionstr";
alaffincc2e5552000-07-27 17:13:18 +0000243
nstraz829b40a2000-09-06 14:33:29 +0000244 optionstr[0] = '\0';
245
nstraz94181082000-08-30 18:43:38 +0000246 for (i = 0; std_options[i].optstr; ++i)
247 strcat(optionstr, std_options[i].optstr);
248 if (user_optarr)
249 for (i = 0; user_optarr[i].option; ++i)
250 /* only add the option if it wasn't there already */
vapier45a8ba02009-07-20 10:59:32 +0000251 if (strchr(optionstr, user_optarr[i].option[0]) == NULL)
nstraz94181082000-08-30 18:43:38 +0000252 strcat(optionstr, user_optarr[i].option);
alaffincc2e5552000-07-27 17:13:18 +0000253
254#if DEBUG > 1
255 printf("STD_nopts = %d\n", STD_nopts);
256#endif
257
alaffincc2e5552000-07-27 17:13:18 +0000258 /*
259 * Loop through av parsing options.
260 */
nstraz94181082000-08-30 18:43:38 +0000261 while ( (opt = getopt(ac, av, optionstr)) > 0) {
alaffincc2e5552000-07-27 17:13:18 +0000262
nstraz94181082000-08-30 18:43:38 +0000263 STD_argind = optind;
alaffincc2e5552000-07-27 17:13:18 +0000264#if DEBUG > 0
nstraz94181082000-08-30 18:43:38 +0000265 printf("parse_opts: getopt returned '%c'\n", opt);
alaffincc2e5552000-07-27 17:13:18 +0000266#endif
alaffincc2e5552000-07-27 17:13:18 +0000267
nstraz94181082000-08-30 18:43:38 +0000268 switch (opt) {
269 case '?': /* Unknown option */
270 return "Unknown option";
271 break;
272 case ':': /* Missing Arg */
273 return "Missing argument";
274 break;
275 case 'i': /* Iterations */
276 options |= OPT_iteration;
277 STD_LOOP_COUNT = atoi(optarg);
278 if (STD_LOOP_COUNT == 0) STD_INFINITE = 1;
279 break;
280 case 'P': /* Delay between iterations */
281 options |= OPT_delay;
282 STD_LOOP_DELAY = atof(optarg);
283 break;
284 case 'I': /* Time duration */
285 options |= OPT_duration;
286 STD_LOOP_DURATION = atof(optarg);
vapier45a8ba02009-07-20 10:59:32 +0000287 if ( STD_LOOP_DURATION == 0.0 ) STD_INFINITE=1;
nstraz94181082000-08-30 18:43:38 +0000288 break;
289 case 'c': /* Copies */
290 options |= OPT_copies;
291 STD_COPIES = atoi(optarg);
292 break;
293 case 'f': /* Functional testing */
294 STD_FUNCTIONAL_TEST = 0;
295 break;
296 case 'p': /* Pause for SIGUSR1 */
297 STD_PAUSE = 1;
298 break;
299 case 't': /* syscall timing */
300 STD_TIMING_ON = 1;
301 break;
302 case 'e': /* errno loggin */
303 STD_ERRNO_LOG = 1;
304 break;
305 case 'h': /* Help */
306 print_help(uhf);
307 exit(0);
308 break;
robbiewd34d5812005-07-11 22:28:09 +0000309#ifdef UCLINUX
310 case 'C': /* Run child */
311 child_args = optarg;
312 break;
313#endif
nstraz94181082000-08-30 18:43:38 +0000314 default:
vapier45a8ba02009-07-20 10:59:32 +0000315
alaffincc2e5552000-07-27 17:13:18 +0000316 /* Check all the user specified options */
317 found=0;
nstraz94181082000-08-30 18:43:38 +0000318 for(i = 0; user_optarr[i].option; ++i) {
alaffincc2e5552000-07-27 17:13:18 +0000319
nstraz94181082000-08-30 18:43:38 +0000320 if (opt == user_optarr[i].option[0]) {
alaffincc2e5552000-07-27 17:13:18 +0000321 /* Yup, This is a user option, set the flag and look for argument */
nstraz94181082000-08-30 18:43:38 +0000322 if ( user_optarr[i].flag ) {
323 *user_optarr[i].flag=1;
alaffincc2e5552000-07-27 17:13:18 +0000324 }
325 found++;
326
alaffincc2e5552000-07-27 17:13:18 +0000327 /* save the argument at the user's location */
vapier45a8ba02009-07-20 10:59:32 +0000328 if ( user_optarr[i].option[strlen(user_optarr[i].option)-1] == ':' ) {
nstraz94181082000-08-30 18:43:38 +0000329 *user_optarr[i].arg=optarg;
alaffincc2e5552000-07-27 17:13:18 +0000330 }
331 break; /* option found - break out of the for loop */
332 }
333 }
334 /* This condition "should never happen". SO CHECK FOR IT!!!! */
vapier45a8ba02009-07-20 10:59:32 +0000335 if ( ! found ) {
alaffincc2e5552000-07-27 17:13:18 +0000336 sprintf(Mesg2,
nstraz94181082000-08-30 18:43:38 +0000337 "parse_opts: ERROR - option:\"%c\" NOT FOUND... INTERNAL ERROR", opt);
alaffincc2e5552000-07-27 17:13:18 +0000338 return(Mesg2);
339 }
340 }
341
342 } /* end of while */
robbiew0ea4e1b2004-01-05 21:12:51 +0000343 free(optionstr);
alaffincc2e5552000-07-27 17:13:18 +0000344
nstraz94181082000-08-30 18:43:38 +0000345 STD_argind = optind;
alaffincc2e5552000-07-27 17:13:18 +0000346
347 /*
348 * Turn on debug
349 */
350 if ( (ptr=getenv("USC_DEBUG")) != NULL ) {
351 Debug=1;
352 printf("env USC_DEBUG is defined, turning on debug\n");
353 }
354 if ( (ptr=getenv("USC_VERBOSE")) != NULL ) {
355 Debug=1;
356 printf("env USC_VERBOSE is defined, turning on debug\n");
357 }
358
359 /*
360 * If the USC_ITERATION_ENV environmental variable is set to
361 * a number, use that number as iteration count (same as -c option).
362 * The -c option with arg will be used even if this env var is set.
363 */
364 if ( !(options & OPT_iteration) && (ptr=getenv(USC_ITERATION_ENV)) != NULL ) {
365 if ( sscanf(ptr, "%i", &k) == 1) {
366 if ( k == 0 ) { /* if arg is 0, set infinite loop flag */
367 STD_INFINITE=1;
368 if ( Debug )
369 printf("Using env %s, set STD_INFINITE to 1\n",
370 USC_ITERATION_ENV);
371 } else { /* else, set the loop count to the arguement */
372 STD_LOOP_COUNT=k;
373 if ( Debug )
374 printf("Using env %s, set STD_LOOP_COUNT to %d\n",
375 USC_ITERATION_ENV, k);
376 }
377 }
378 }
379
380 /*
381 * If the USC_NO_FUNC_CHECK environmental variable is set, we'll
382 * unset the STD_FUNCTIONAL_TEST variable.
383 */
384 if ( !(options & OPT_nofunccheck) && (ptr=getenv(USC_NO_FUNC_CHECK)) != NULL ) {
385 STD_FUNCTIONAL_TEST=0; /* Turn off functional testing */
386 if ( Debug )
387 printf("Using env %s, set STD_FUNCTIONAL_TEST to 0\n",
388 USC_NO_FUNC_CHECK);
389 }
390
391 /*
392 * If the USC_LOOP_WALLTIME environmental variable is set,
393 * use that number as duration (same as -I option).
394 * The -I option with arg will be used even if this env var is set.
395 */
396
397 if ( !(options & OPT_duration) && (ptr=getenv(USC_LOOP_WALLTIME)) != NULL ) {
398 if ( sscanf(ptr, "%f", &ftmp) == 1 && ftmp >= 0.0 ) {
399 STD_LOOP_DURATION=ftmp;
400 if ( Debug )
401 printf("Using env %s, set STD_LOOP_DURATION to %f\n",
402 USC_LOOP_WALLTIME, ftmp);
403 if ( STD_LOOP_DURATION == 0.0 ) { /* if arg is 0, set infinite loop flag */
404 STD_INFINITE=1;
405 if ( Debug )
406 printf("Using env %s, set STD_INFINITE to 1\n", USC_LOOP_WALLTIME);
407 }
408 }
409 }
410 if ( !(options & OPT_duration) && (ptr=getenv("USC_DURATION")) != NULL ) {
411 if ( sscanf(ptr, "%f", &ftmp) == 1 && ftmp >= 0.0 ) {
412 STD_LOOP_DURATION=ftmp;
413 if ( Debug )
414 printf("Using env USC_DURATION, set STD_LOOP_DURATION to %f\n", ftmp);
415 if ( STD_LOOP_DURATION == 0.0 ) { /* if arg is 0, set infinite loop flag */
416 STD_INFINITE=1;
417 if ( Debug )
418 printf("Using env USC_DURATION, set STD_INFINITE to 1\n");
419 }
420 }
421 }
422 /*
423 * If the USC_LOOP_DELAY environmental variable is set,
424 * use that number as delay in factional seconds (same as -P option).
425 * The -P option with arg will be used even if this env var is set.
426 */
427 if ( !(options & OPT_delay) && (ptr=getenv(USC_LOOP_DELAY)) != NULL ) {
428 if ( sscanf(ptr, "%f", &ftmp) == 1 && ftmp >= 0.0 ) {
429 STD_LOOP_DELAY=ftmp;
430 if ( Debug )
431 printf("Using env %s, set STD_LOOP_DELAY = %f\n",
432 USC_LOOP_DELAY, ftmp);
433 }
434 }
435
436 /*
437 * If the USC_COPIES environmental variable is set,
438 * use that number as copies (same as -c option).
439 * The -c option with arg will be used even if this env var is set.
440 */
441 if ( !(options & OPT_copies) && (ptr=getenv(USC_COPIES)) != NULL ) {
442 if ( sscanf(ptr, "%d", &STD_COPIES) == 1 && STD_COPIES >= 0 ) {
443 if ( Debug )
444 printf("Using env %s, set STD_COPIES = %d\n",
445 USC_COPIES, STD_COPIES);
446 }
447 }
448
449 /*
450 * The following are special system testing envs to turn on special
451 * hooks in the code.
452 */
453 if ( (ptr=getenv("USC_TP_BARRIER")) != NULL ) {
454 if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) {
455 STD_TP_barrier=k;
456 }
457 else
458 STD_TP_barrier=1;
459 if ( Debug )
460 printf("using env USC_TP_BARRIER, Set STD_TP_barrier to %d\n",
461 STD_TP_barrier);
462 }
463
464 if ( (ptr=getenv("USC_LP_BARRIER")) != NULL ) {
465 if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) {
466 STD_LP_barrier=k;
467 }
468 else
469 STD_LP_barrier=1;
vapier45a8ba02009-07-20 10:59:32 +0000470 if ( Debug )
alaffincc2e5552000-07-27 17:13:18 +0000471 printf("using env USC_LP_BARRIER, Set STD_LP_barrier to %d\n",
472 STD_LP_barrier);
473 }
474
475 if ( (ptr=getenv("USC_TP_SHMEM")) != NULL ) {
476 if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) {
477 STD_TP_shmem_sz=k;
478 if ( Debug )
479 printf("Using env USC_TP_SHMEM, Set STD_TP_shmem_sz to %d\n",
480 STD_TP_shmem_sz);
481 }
482 }
483
484 if ( (ptr=getenv("USC_LP_SHMEM")) != NULL ) {
485 if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) {
486 STD_LP_shmem=k;
487 if ( Debug )
488 printf("Using env USC_LP_SHMEM, Set STD_LP_shmem to %d\n",
489 STD_LP_shmem);
490 }
491 }
492
493 if ( (ptr=getenv("USC_LD_SHMEM")) != NULL ) {
494 if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) {
495 STD_LD_shmem=k;
496 if ( Debug )
497 printf("Using env USC_LD_SHMEM, Set STD_LD_shmem to %d\n",
498 STD_LD_shmem);
499 }
500 }
501
502 if ( (ptr=getenv("USC_TP_SBRK")) != NULL ) {
503 if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) {
504 STD_TP_sbrk=k;
505 if ( Debug )
506 printf("Using env USC_TP_SBRK, Set STD_TP_sbrk to %d\n",
507 STD_TP_sbrk);
508 }
509 }
510
robbiewd34d5812005-07-11 22:28:09 +0000511#if !defined(UCLINUX)
alaffincc2e5552000-07-27 17:13:18 +0000512 if ( (ptr=getenv("USC_LP_SBRK")) != NULL ) {
513 if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) {
514 STD_LP_sbrk=k;
515 if ( Debug )
516 printf("Using env USC_LP_SBRK, Set STD_LP_sbrk to %d\n",
517 STD_LP_sbrk);
518 }
519 }
robbiewd34d5812005-07-11 22:28:09 +0000520#endif /* if !defined(UCLINUX) */
alaffincc2e5552000-07-27 17:13:18 +0000521
522 if ( (ptr=getenv("USC_LP_RECFUN")) != NULL ) {
523 if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) {
524 STD_LP_recfun=k;
525 if ( STD_bigstack != (struct usc_bigstack_t *)NULL )
526 STD_bigstack=(struct usc_bigstack_t *)
527 malloc(sizeof(struct usc_bigstack_t));
528 if ( Debug )
529 printf("Using env USC_LP_RECFUN, Set STD_LP_recfun to %d\n",
530 STD_LP_recfun);
531 }
532 }
533
534 if ( (ptr=getenv("USC_LD_RECFUN")) != NULL ) {
535 if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) {
536 STD_LD_recfun=k;
537 if ( STD_bigstack != (struct usc_bigstack_t *)NULL )
538 STD_bigstack=(struct usc_bigstack_t *)
539 malloc(sizeof(struct usc_bigstack_t));
540 if ( Debug )
541 printf("Using env USC_LD_RECFUN, Set STD_LD_recfun to %d\n",
542 STD_LD_recfun);
543 }
544 }
vapier45a8ba02009-07-20 10:59:32 +0000545
alaffincc2e5552000-07-27 17:13:18 +0000546#if UNIT_TEST
547 printf("The following variables after option and env parsing:\n");
548 printf("STD_FUNCTIONAL_TEST = %d\n", STD_FUNCTIONAL_TEST);
549 printf("STD_LOOP_DURATION = %f\n", STD_LOOP_DURATION);
550 printf("STD_LOOP_DELAY = %f\n", STD_LOOP_DELAY);
551 printf("STD_COPIES = %d\n", STD_COPIES);
552 printf("STD_LOOP_COUNT = %d\n", STD_LOOP_COUNT);
553 printf("STD_INFINITE = %d\n", STD_INFINITE);
554 printf("STD_TIMING_ON = %d\n", STD_TIMING_ON);
555 printf("STD_ERRNO_LOG = %d\n", STD_ERRNO_LOG);
556 printf("STD_PAUSE = %d\n", STD_PAUSE);
557#endif
558
559 return((char *) NULL);
560
561} /* end of parse_opts */
562
alaffincc2e5552000-07-27 17:13:18 +0000563/*********************************************************************
nstraz94181082000-08-30 18:43:38 +0000564 * print_help() - print help message and user help message
alaffincc2e5552000-07-27 17:13:18 +0000565 *********************************************************************/
nstraz94181082000-08-30 18:43:38 +0000566void print_help(void (*user_help)())
alaffincc2e5552000-07-27 17:13:18 +0000567{
nstraz94181082000-08-30 18:43:38 +0000568 STD_opts_help();
alaffincc2e5552000-07-27 17:13:18 +0000569
nstraz94181082000-08-30 18:43:38 +0000570 if (user_help) user_help();
alaffincc2e5552000-07-27 17:13:18 +0000571}
572
573/*********************************************************************
574 * STD_opts_help() - return a help string for the STD_OPTIONS.
575 *********************************************************************/
nstraz94181082000-08-30 18:43:38 +0000576void
alaffincc2e5552000-07-27 17:13:18 +0000577STD_opts_help()
578{
nstraz94181082000-08-30 18:43:38 +0000579 int i;
alaffincc2e5552000-07-27 17:13:18 +0000580
nstraz94181082000-08-30 18:43:38 +0000581 for(i = 0; std_options[i].optstr; ++i) {
582 if (std_options[i].help)
vapier1820d282009-07-20 04:48:17 +0000583 printf("%s", std_options[i].help);
alaffincc2e5552000-07-27 17:13:18 +0000584 }
alaffincc2e5552000-07-27 17:13:18 +0000585}
586
vapier45a8ba02009-07-20 10:59:32 +0000587/*
alaffincc2e5552000-07-27 17:13:18 +0000588 * routine to goto when we get the SIGUSR1 for STD_PAUSE
589 */
590void STD_go(int sig)
591{
592 return;
593}
594
595/***********************************************************************
596 * This function will do desired end of global setup test
597 * hooks.
598 * Currently it will only do a pause waiting for sigusr1 if
599 * STD_PAUSE is set.
600 *
601 ***********************************************************************/
602int
603usc_global_setup_hook()
604{
robbiewd34d5812005-07-11 22:28:09 +0000605#ifndef UCLINUX
alaffincc2e5552000-07-27 17:13:18 +0000606 int cnt;
nstraz94181082000-08-30 18:43:38 +0000607 /* temp variable to store old signal action to be restored after pause */
608 int (*_TMP_FUNC)(void);
alaffincc2e5552000-07-27 17:13:18 +0000609
610 /*
611 * Fork STD_COPIES-1 copies.
612 */
613 for(cnt=1;cnt<STD_COPIES;cnt++) {
614 switch(fork() ) {
615 case -1:
616 fprintf(stderr, "%s: fork() failed, errno:%d %s\n",
617 __FILE__, errno, strerror(errno));
618 break;
619 case 0: /* child */
620 cnt=STD_COPIES; /* to stop the forking */
621 break;
622
623 default: /* parent */
624 break;
625 }
626 }
vapier45a8ba02009-07-20 10:59:32 +0000627
alaffincc2e5552000-07-27 17:13:18 +0000628 /*
629 * pause waiting for sigusr1.
630 */
vapier45a8ba02009-07-20 10:59:32 +0000631 if ( STD_PAUSE ) {
632 _TMP_FUNC = (int (*)())signal(SIGUSR1, STD_go);
633 pause();
634 signal(SIGUSR1, (void (*)())_TMP_FUNC);
alaffincc2e5552000-07-27 17:13:18 +0000635 }
636
robbiewd34d5812005-07-11 22:28:09 +0000637#if !defined(UCLINUX)
alaffincc2e5552000-07-27 17:13:18 +0000638
639 if ( STD_TP_sbrk || STD_LP_sbrk) {
640 STD_start_break=sbrk(0); /* get original sbreak size */
641 }
642
643 if ( STD_TP_sbrk ) {
644 sbrk(STD_TP_sbrk);
vapier45a8ba02009-07-20 10:59:32 +0000645 if ( Debug )
alaffincc2e5552000-07-27 17:13:18 +0000646 printf("after sbrk(%d)\n", STD_TP_sbrk);
647 }
robbiewd34d5812005-07-11 22:28:09 +0000648
649#endif /* if !defined(UCLINUX) */
650#endif
alaffincc2e5552000-07-27 17:13:18 +0000651 return 0;
652}
653
654#define USECS_PER_SEC 1000000 /* microseconds per second */
655
656/***********************************************************************
657 * This function returns the number of get_current_time()'s return
658 * per second.
659 ***********************************************************************/
660
661static int
662get_timepersec()
663{
alaffincc2e5552000-07-27 17:13:18 +0000664 return USECS_PER_SEC; /* microseconds per second */
alaffincc2e5552000-07-27 17:13:18 +0000665
666}
667
668/***********************************************************************
nstraz94181082000-08-30 18:43:38 +0000669 * this function will get current time in microseconds since 1970.
alaffincc2e5552000-07-27 17:13:18 +0000670 ***********************************************************************/
671static int
672get_current_time()
673{
674 struct timeval curtime;
alaffincc2e5552000-07-27 17:13:18 +0000675
676 gettimeofday(&curtime, NULL);
677
678 /* microseconds since 1970 */
679 return (curtime.tv_sec*USECS_PER_SEC) + curtime.tv_usec;
680
alaffincc2e5552000-07-27 17:13:18 +0000681
682}
683
684/***********************************************************************
685 *
686 * This function will determine if test should continue iterating
687 * If the STD_INFINITE flag is set, return 1.
688 * If the STD_LOOP_COUNT variable is set, compare it against
689 * the counter.
690 * If the STD_LOOP_DURATION variable is set, compare current time against
691 * calculated stop_time.
692 * This function will return 1 until all desired looping methods
vapier45a8ba02009-07-20 10:59:32 +0000693 * have been met.
alaffincc2e5552000-07-27 17:13:18 +0000694 *
695 * counter integer is supplied by the user program.
696 ***********************************************************************/
697int
698usc_test_looping(counter)
699int counter;
700{
701 static int first_time = 1;
702 static int stop_time = 0; /* stop time in rtc or usecs */
703 static int delay; /* delay in clocks or usecs */
mridgee6508f82005-01-04 21:00:17 +0000704 int hertz=0; /* clocks per second or usecs per second */
alaffincc2e5552000-07-27 17:13:18 +0000705 int ct, end; /* current time, end delay time */
706 int keepgoing=0; /* used to determine return value */
alaffincc2e5552000-07-27 17:13:18 +0000707
708 /*
vapier45a8ba02009-07-20 10:59:32 +0000709 * If this is the first iteration and we are looping for
alaffincc2e5552000-07-27 17:13:18 +0000710 * duration of STD_LOOP_DURATION seconds (fractional) or
711 * doing loop delays, get the clocks per second.
712 */
713 if ( first_time ) {
714
715 first_time=0;
716 if ( STD_LOOP_DELAY || STD_LOOP_DURATION ) {
mridgee6508f82005-01-04 21:00:17 +0000717 hertz = get_timepersec();
alaffincc2e5552000-07-27 17:13:18 +0000718 }
719
720 /*
721 * If looping for duration, calculate stop time in
722 * clocks.
723 */
vapier45a8ba02009-07-20 10:59:32 +0000724
alaffincc2e5552000-07-27 17:13:18 +0000725 if ( STD_LOOP_DURATION) {
726 ct=get_current_time();
mridgee6508f82005-01-04 21:00:17 +0000727 stop_time=(int)((float)hertz * STD_LOOP_DURATION) + ct;
alaffincc2e5552000-07-27 17:13:18 +0000728 }
729
730 /*
731 * If doing delay each iteration, calcuate the number
732 * of clocks for each delay.
733 */
734 if ( STD_LOOP_DELAY ) {
mridgee6508f82005-01-04 21:00:17 +0000735 delay=(int)((float)hertz * STD_LOOP_DELAY);
alaffincc2e5552000-07-27 17:13:18 +0000736 }
737
738 }
vapier45a8ba02009-07-20 10:59:32 +0000739
alaffincc2e5552000-07-27 17:13:18 +0000740 /*
741 * if delay each iteration, loop for delay clocks.
742 * This will not be done on first iteration.
743 * The delay will happen before determining if
744 * there will be another iteration.
745 */
746 else if ( STD_LOOP_DELAY ) {
747 ct=get_current_time();
748 end=ct+delay;
749 while ( ct < end ) {
750 /*
751 * The following are special test hooks in the delay loop.
752 */
753 if ( STD_LD_recfun ) {
754 if ( Debug )
vapier45a8ba02009-07-20 10:59:32 +0000755 printf("calling usc_recressive_func(0, %d, *STD_bigstack)\n",
alaffincc2e5552000-07-27 17:13:18 +0000756 STD_LD_recfun);
subrata_modakb2f47f32007-04-11 04:17:56 +0000757 usc_recressive_func(0, STD_LD_recfun, *STD_bigstack);
alaffincc2e5552000-07-27 17:13:18 +0000758 }
759
760 ct=get_current_time();
761 }
762 }
763
764 if ( STD_INFINITE ) {
mreed10ef038182006-04-19 18:23:40 +0000765 keepgoing++;
alaffincc2e5552000-07-27 17:13:18 +0000766 }
767
768 if ( STD_LOOP_COUNT && counter < STD_LOOP_COUNT ) {
769 keepgoing++;
mreed10ef038182006-04-19 18:23:40 +0000770 }
alaffincc2e5552000-07-27 17:13:18 +0000771
772 if ( STD_LOOP_DURATION != 0.0 && get_current_time() < stop_time ) {
773 keepgoing++;
mreed10ef038182006-04-19 18:23:40 +0000774 }
alaffincc2e5552000-07-27 17:13:18 +0000775
776 if ( keepgoing == 0 )
777 return 0;
778
779 /*
780 * The following code allows special system testing hooks.
781 */
782
783 if ( STD_LP_recfun ) {
784 if ( Debug )
vapier45a8ba02009-07-20 10:59:32 +0000785 printf("calling usc_recressive_func(0, %d, *STD_bigstack)\n",
alaffincc2e5552000-07-27 17:13:18 +0000786 STD_LP_recfun);
subrata_modakb2f47f32007-04-11 04:17:56 +0000787 usc_recressive_func(0, STD_LP_recfun, *STD_bigstack);
alaffincc2e5552000-07-27 17:13:18 +0000788 }
789
robbiewd34d5812005-07-11 22:28:09 +0000790#if !defined(UCLINUX)
791
alaffincc2e5552000-07-27 17:13:18 +0000792 if ( STD_LP_sbrk ) {
793 if ( Debug )
794 printf("about to do sbrk(%d)\n", STD_LP_sbrk);
795 sbrk(STD_LP_sbrk);
796 }
robbiewd34d5812005-07-11 22:28:09 +0000797#endif
alaffincc2e5552000-07-27 17:13:18 +0000798
alaffincc2e5552000-07-27 17:13:18 +0000799
800 if ( keepgoing )
801 return 1;
802 else
803 return 0; /* done - stop iterating */
804}
805
806
807/*
808 * This function recressively calls itself max times.
vapier45a8ba02009-07-20 10:59:32 +0000809 */
alaffincc2e5552000-07-27 17:13:18 +0000810static void
811usc_recressive_func(cnt, max, bstack)
812int cnt;
813int max;
814struct usc_bigstack_t bstack;
815{
816 if ( cnt < max )
817 usc_recressive_func(cnt+1, max, bstack);
818
819}
820
821#if UNIT_TEST
822/******************************************************************************
823 * UNIT TEST CODE
824 * UNIT TEST CODE
vapier45a8ba02009-07-20 10:59:32 +0000825 *
alaffincc2e5552000-07-27 17:13:18 +0000826 * this following code is provide so that unit testing can
827 * be done fairly easily.
828 ******************************************************************************/
829
830int Help = 0;
831int Help2 = 0;
832char *ptr;
833
834/*
835 * Code from usctest.h that not part of this file since we are the library.
836 */
837
838struct usc_errno_t TEST_VALID_ENO[USC_MAX_ERRNO];
839
840 /***********************************************************************
841 * Globals for returning the return code and errno from the system call
842 * test macros.
843 ***********************************************************************/
844int TEST_RETURN;
845int TEST_ERRNO;
846
alaffincc2e5552000-07-27 17:13:18 +0000847/* for test specific parse_opts options */
848option_t Options[] = {
849 { "help", &Help2, NULL }, /* -help option */
850 { "h", &Help, NULL }, /* -h option */
851 { TIMING, NULL, NULL}, /* disable -timing option */
852
853#if INVALID_TEST_CASES
854 { "missingflag", NULL, &ptr }, /* error */
855 { "missingarg:", &Help, NULL }, /* error */
856#endif /* INVALID_TEST_CASES */
857
858 { NULL, NULL, NULL }
859};
860
861
862main(argc, argv)
863int argc;
864char **argv;
865{
866 int lc;
867 char *msg;
868 struct timeval t;
869 int cnt;
870
871 if ( (msg=parse_opts(argc, argv,
872 (option_t *) Options)) != (char *) NULL ) {
873 printf("ERROR : %s\n", msg);
874 exit(1);
875 }
876
alaffincc2e5552000-07-27 17:13:18 +0000877 TEST_PAUSE;
878
879 for (lc=0; TEST_LOOPING(lc); lc++) {
vapier45a8ba02009-07-20 10:59:32 +0000880
alaffincc2e5552000-07-27 17:13:18 +0000881 TEST( gettimeofday(&t, NULL) );
882 printf("iter=%d: sec:%d, usec:%6.6d %s", lc+1, t.tv_sec,
883 t.tv_usec, ctime(&t.tv_sec));
884 }
885
886
887 TEST_CLEANUP;
888
889 exit(0);
890}
891
892#endif /* UNIT_TEST */