blob: 3a879e44e38e5e9e20f1caa14a1fb6e5162bcb9f [file] [log] [blame]
alaffincc2e5552000-07-27 17:13:18 +00001/*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
Cyril Hrubis2a81c752014-05-27 14:54:09 +02003 * AUTHOR : William Roske/Richard Logan
vapier45a8ba02009-07-20 10:59:32 +00004 *
alaffincc2e5552000-07-27 17:13:18 +00005 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
vapier45a8ba02009-07-20 10:59:32 +00008 *
alaffincc2e5552000-07-27 17:13:18 +00009 * This program is distributed in the hope that it would be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
vapier45a8ba02009-07-20 10:59:32 +000012 *
alaffincc2e5552000-07-27 17:13:18 +000013 * Further, this software is distributed without any warranty that it is
14 * free of the rightful claim of any third person regarding infringement
15 * or the like. Any license provided herein, whether implied or
16 * otherwise, applies only to this software file. Patent licenses, if
17 * any, provided herein do not apply to combinations of this program with
18 * other software, or any other product whatsoever.
vapier45a8ba02009-07-20 10:59:32 +000019 *
alaffincc2e5552000-07-27 17:13:18 +000020 * You should have received a copy of the GNU General Public License along
Wanlong Gaofed96412012-10-24 10:10:29 +080021 * with this program; if not, write the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
vapier45a8ba02009-07-20 10:59:32 +000023 *
alaffincc2e5552000-07-27 17:13:18 +000024 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
25 * Mountain View, CA 94043, or:
vapier45a8ba02009-07-20 10:59:32 +000026 *
27 * http://www.sgi.com
28 *
29 * For further information regarding this notice, see:
30 *
alaffincc2e5552000-07-27 17:13:18 +000031 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
32 */
33
Mike Frysinger28606c12010-08-17 17:22:45 -040034#include "config.h"
alaffincc2e5552000-07-27 17:13:18 +000035#include <errno.h>
36#include <stdlib.h>
37#include <string.h>
38#include <sys/param.h>
39#include <sys/signal.h>
40#include <sys/types.h>
41#include <unistd.h>
42#include <sys/time.h>
Cyril Hrubis4d964f62012-03-07 18:02:37 +010043#include <stdint.h>
alaffincc2e5552000-07-27 17:13:18 +000044
alaffincc2e5552000-07-27 17:13:18 +000045#include "test.h"
Cyril Hrubis06f9fe42013-06-26 14:55:48 +020046#include "ltp_priv.h"
alaffincc2e5552000-07-27 17:13:18 +000047#include "usctest.h"
alaffincc2e5552000-07-27 17:13:18 +000048
alaffincc2e5552000-07-27 17:13:18 +000049#ifndef UNIT_TEST
50#define UNIT_TEST 0
51#endif
52
alaffincc2e5552000-07-27 17:13:18 +000053/* Define flags and args for standard options */
Cyril Hrubisd5d39082014-05-27 15:28:09 +020054static int STD_INFINITE = 0; /* flag indciating to loop forever */
55int STD_LOOP_COUNT = 1; /* number of iterations */
alaffincc2e5552000-07-27 17:13:18 +000056
Cyril Hrubisd5d39082014-05-27 15:28:09 +020057static float STD_LOOP_DURATION = 0.0; /* duration value in fractional seconds */
alaffincc2e5552000-07-27 17:13:18 +000058
Cyril Hrubisd5d39082014-05-27 15:28:09 +020059static char **STD_opt_arr = NULL; /* array of option strings */
60static int STD_argind = 1; /* argv index to next argv element */
alaffincc2e5552000-07-27 17:13:18 +000061 /* (first argument) */
62 /* To getopt users, it is like optind */
63
64/*
65 * The following variables are to support system testing additions.
66 */
Wanlong Gao354ebb42012-12-07 10:10:04 +080067static int STD_TP_barrier = 0; /* flag to do barrier in TEST_PAUSE */
alaffincc2e5552000-07-27 17:13:18 +000068 /* 2 - wait_barrier(), 3 - set_barrier(), * - barrier() */
Wanlong Gao354ebb42012-12-07 10:10:04 +080069static int STD_LP_barrier = 0; /* flag to do barrier in TEST_LOOPING */
alaffincc2e5552000-07-27 17:13:18 +000070 /* 2 - wait_barrier(), 3 - set_barrier(), * - barrier() */
Wanlong Gao354ebb42012-12-07 10:10:04 +080071static int STD_TP_shmem_sz = 0; /* shmalloc this many words per pe in TEST_PAUSE */
72static int STD_LD_shmem = 0; /* flag to do shmem_puts and shmem_gets during delay */
73static int STD_LP_shmem = 0; /* flag to do shmem_puts and gets during TEST_LOOPING */
74static int STD_LD_recfun = 0; /* do recressive function calls in loop delay */
75static int STD_LP_recfun = 0; /* do recressive function calls in TEST_LOOPING */
76static int STD_TP_sbrk = 0; /* do sbrk in TEST_PAUSE */
77static int STD_LP_sbrk = 0; /* do sbrk in TEST_LOOPING */
78static char *STD_start_break = 0; /* original sbrk size */
79static int Debug = 0;
alaffincc2e5552000-07-27 17:13:18 +000080
Cyril Hrubisd5d39082014-05-27 15:28:09 +020081static struct std_option_t {
Wanlong Gao354ebb42012-12-07 10:10:04 +080082 char *optstr;
83 char *help;
84 char *flag;
85 char **arg;
nstraz94181082000-08-30 18:43:38 +000086} std_options[] = {
Cyril Hrubis2a81c752014-05-27 14:54:09 +020087 {"h", " -h Show this help screen\n", NULL, NULL},
88 {"i:", " -i n Execute test n times\n", NULL, NULL},
89 {"I:", " -I x Execute test for x seconds\n", NULL, NULL},
robbiewd34d5812005-07-11 22:28:09 +000090#ifdef UCLINUX
Cyril Hrubis2a81c752014-05-27 14:54:09 +020091 {"C:",
92 " -C ARG Run the child process with arguments ARG (for internal use)\n",
Wanlong Gao354ebb42012-12-07 10:10:04 +080093 NULL, NULL},
robbiewd34d5812005-07-11 22:28:09 +000094#endif
Cyril Hrubisd5d39082014-05-27 15:28:09 +020095 {NULL, NULL, NULL, NULL}
96};
nstraz94181082000-08-30 18:43:38 +000097
alaffincc2e5552000-07-27 17:13:18 +000098/*
99 * Structure for usc_recressive_func argument
100 */
101struct usc_bigstack_t {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800102 char space[4096];
alaffincc2e5552000-07-27 17:13:18 +0000103};
104
Wanlong Gao354ebb42012-12-07 10:10:04 +0800105static struct usc_bigstack_t *STD_bigstack = NULL;
alaffincc2e5552000-07-27 17:13:18 +0000106
alaffincc2e5552000-07-27 17:13:18 +0000107/* define the string length for Mesg and Mesg2 strings */
108#define STRLEN 2048
109
alaffincc2e5552000-07-27 17:13:18 +0000110static char Mesg2[STRLEN]; /* holds possible return string */
111static void usc_recressive_func();
112
113/*
114 * Define bits for options that might have env variable default
115 */
Cyril Hrubis2a81c752014-05-27 14:54:09 +0200116#define OPT_iteration 01
Cyril Hrubis2a81c752014-05-27 14:54:09 +0200117#define OPT_duration 04
118#define OPT_delay 010
alaffincc2e5552000-07-27 17:13:18 +0000119
robbiewd34d5812005-07-11 22:28:09 +0000120#ifdef UCLINUX
121/* Allocated and used in self_exec.c: */
122extern char *child_args; /* Arguments to child when -C is used */
123#endif
alaffincc2e5552000-07-27 17:13:18 +0000124
Cyril Hrubis2a81c752014-05-27 14:54:09 +0200125static void print_help(void (*user_help)(void))
126{
127 int i;
128
129 for (i = 0; std_options[i].optstr; ++i) {
130 if (std_options[i].help)
131 printf("%s", std_options[i].help);
132 }
133
134 if (user_help)
135 user_help();
136}
137
alaffincc2e5552000-07-27 17:13:18 +0000138/**********************************************************************
vapier45a8ba02009-07-20 10:59:32 +0000139 * parse_opts:
alaffincc2e5552000-07-27 17:13:18 +0000140 **********************************************************************/
Cyril Hrubis2a81c752014-05-27 14:54:09 +0200141const char *parse_opts(int ac, char **av, const option_t * user_optarr,
142 void (*uhf)(void))
alaffincc2e5552000-07-27 17:13:18 +0000143{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800144 int found; /* flag to indicate that an option specified was */
145 /* found in the user's list */
146 int k; /* scratch integer for returns and short time usage */
147 float ftmp; /* tmp float for parsing env variables */
148 char *ptr; /* used in getting env variables */
149 int options = 0; /* no options specified */
150 int optstrlen, i;
151 char *optionstr;
Cyril Hrubis2a81c752014-05-27 14:54:09 +0200152 int opt;
alaffincc2e5552000-07-27 17:13:18 +0000153
Wanlong Gao354ebb42012-12-07 10:10:04 +0800154 /*
155 * If not the first time this function is called, release the old STD_opt_arr
156 * vector.
157 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800158 if (STD_opt_arr != NULL) {
159 free(STD_opt_arr);
160 STD_opt_arr = NULL;
nstraz94181082000-08-30 18:43:38 +0000161 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800162 /* Calculate how much space we need for the option string */
163 optstrlen = 0;
164 for (i = 0; std_options[i].optstr; ++i)
165 optstrlen += strlen(std_options[i].optstr);
166 if (user_optarr)
167 for (i = 0; user_optarr[i].option; ++i) {
168 if (strlen(user_optarr[i].option) > 2)
169 return
170 "parse_opts: ERROR - Only short options are allowed";
171 optstrlen += strlen(user_optarr[i].option);
172 }
173 optstrlen += 1;
alaffincc2e5552000-07-27 17:13:18 +0000174
Wanlong Gao354ebb42012-12-07 10:10:04 +0800175 /* Create the option string for getopt */
176 optionstr = malloc(optstrlen);
177 if (!optionstr)
178 return
179 "parse_opts: ERROR - Could not allocate memory for optionstr";
alaffincc2e5552000-07-27 17:13:18 +0000180
Wanlong Gao354ebb42012-12-07 10:10:04 +0800181 optionstr[0] = '\0';
nstraz829b40a2000-09-06 14:33:29 +0000182
Wanlong Gao354ebb42012-12-07 10:10:04 +0800183 for (i = 0; std_options[i].optstr; ++i)
184 strcat(optionstr, std_options[i].optstr);
185 if (user_optarr)
186 for (i = 0; user_optarr[i].option; ++i)
187 /* only add the option if it wasn't there already */
188 if (strchr(optionstr, user_optarr[i].option[0]) == NULL)
189 strcat(optionstr, user_optarr[i].option);
alaffincc2e5552000-07-27 17:13:18 +0000190
Wanlong Gao354ebb42012-12-07 10:10:04 +0800191 /*
192 * Loop through av parsing options.
193 */
194 while ((opt = getopt(ac, av, optionstr)) > 0) {
alaffincc2e5552000-07-27 17:13:18 +0000195
Wanlong Gao354ebb42012-12-07 10:10:04 +0800196 STD_argind = optind;
alaffincc2e5552000-07-27 17:13:18 +0000197
Wanlong Gao354ebb42012-12-07 10:10:04 +0800198 switch (opt) {
199 case '?': /* Unknown option */
nstraz94181082000-08-30 18:43:38 +0000200 return "Unknown option";
201 break;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800202 case ':': /* Missing Arg */
nstraz94181082000-08-30 18:43:38 +0000203 return "Missing argument";
204 break;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800205 case 'i': /* Iterations */
nstraz94181082000-08-30 18:43:38 +0000206 options |= OPT_iteration;
207 STD_LOOP_COUNT = atoi(optarg);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800208 if (STD_LOOP_COUNT == 0)
209 STD_INFINITE = 1;
nstraz94181082000-08-30 18:43:38 +0000210 break;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800211 case 'I': /* Time duration */
nstraz94181082000-08-30 18:43:38 +0000212 options |= OPT_duration;
213 STD_LOOP_DURATION = atof(optarg);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800214 if (STD_LOOP_DURATION == 0.0)
215 STD_INFINITE = 1;
nstraz94181082000-08-30 18:43:38 +0000216 break;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800217 case 'h': /* Help */
nstraz94181082000-08-30 18:43:38 +0000218 print_help(uhf);
219 exit(0);
220 break;
robbiewd34d5812005-07-11 22:28:09 +0000221#ifdef UCLINUX
Wanlong Gao354ebb42012-12-07 10:10:04 +0800222 case 'C': /* Run child */
robbiewd34d5812005-07-11 22:28:09 +0000223 child_args = optarg;
224 break;
225#endif
nstraz94181082000-08-30 18:43:38 +0000226 default:
vapier45a8ba02009-07-20 10:59:32 +0000227
Wanlong Gao354ebb42012-12-07 10:10:04 +0800228 /* Check all the user specified options */
229 found = 0;
230 for (i = 0; user_optarr[i].option; ++i) {
alaffincc2e5552000-07-27 17:13:18 +0000231
Wanlong Gao354ebb42012-12-07 10:10:04 +0800232 if (opt == user_optarr[i].option[0]) {
233 /* Yup, This is a user option, set the flag and look for argument */
234 if (user_optarr[i].flag) {
235 *user_optarr[i].flag = 1;
236 }
237 found++;
alaffincc2e5552000-07-27 17:13:18 +0000238
Wanlong Gao354ebb42012-12-07 10:10:04 +0800239 /* save the argument at the user's location */
240 if (user_optarr[i].
241 option[strlen(user_optarr[i].option)
242 - 1] == ':') {
243 *user_optarr[i].arg = optarg;
244 }
245 break; /* option found - break out of the for loop */
246 }
247 }
248 /* This condition "should never happen". SO CHECK FOR IT!!!! */
249 if (!found) {
250 sprintf(Mesg2,
251 "parse_opts: ERROR - option:\"%c\" NOT FOUND... INTERNAL "
252 "ERROR", opt);
253 return (Mesg2);
254 }
255 }
256
Cyril Hrubis2a81c752014-05-27 14:54:09 +0200257 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800258 free(optionstr);
259
260 STD_argind = optind;
261
262 /*
263 * Turn on debug
264 */
Cyril Hrubis8dc262a2013-06-04 19:12:59 +0200265 if (getenv("USC_DEBUG") != NULL) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800266 Debug = 1;
267 printf("env USC_DEBUG is defined, turning on debug\n");
268 }
Cyril Hrubis8dc262a2013-06-04 19:12:59 +0200269 if (getenv("USC_VERBOSE") != NULL) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800270 Debug = 1;
271 printf("env USC_VERBOSE is defined, turning on debug\n");
alaffincc2e5552000-07-27 17:13:18 +0000272 }
273
Wanlong Gao354ebb42012-12-07 10:10:04 +0800274 /*
275 * If the USC_ITERATION_ENV environmental variable is set to
276 * a number, use that number as iteration count (same as -c option).
277 * The -c option with arg will be used even if this env var is set.
278 */
279 if (!(options & OPT_iteration)
280 && (ptr = getenv(USC_ITERATION_ENV)) != NULL) {
281 if (sscanf(ptr, "%i", &k) == 1) {
282 if (k == 0) { /* if arg is 0, set infinite loop flag */
283 STD_INFINITE = 1;
284 if (Debug)
285 printf
286 ("Using env %s, set STD_INFINITE to 1\n",
287 USC_ITERATION_ENV);
288 } else { /* else, set the loop count to the arguement */
289 STD_LOOP_COUNT = k;
290 if (Debug)
291 printf
292 ("Using env %s, set STD_LOOP_COUNT to %d\n",
293 USC_ITERATION_ENV, k);
294 }
295 }
296 }
alaffincc2e5552000-07-27 17:13:18 +0000297
Wanlong Gao354ebb42012-12-07 10:10:04 +0800298 /*
Wanlong Gao354ebb42012-12-07 10:10:04 +0800299 * If the USC_LOOP_WALLTIME environmental variable is set,
300 * use that number as duration (same as -I option).
301 * The -I option with arg will be used even if this env var is set.
302 */
303
304 if (!(options & OPT_duration) &&
305 (ptr = getenv(USC_LOOP_WALLTIME)) != NULL) {
306 if (sscanf(ptr, "%f", &ftmp) == 1 && ftmp >= 0.0) {
307 STD_LOOP_DURATION = ftmp;
308 if (Debug)
309 printf
310 ("Using env %s, set STD_LOOP_DURATION to %f\n",
311 USC_LOOP_WALLTIME, ftmp);
312 if (STD_LOOP_DURATION == 0.0) { /* if arg is 0, set infinite loop flag */
313 STD_INFINITE = 1;
314 if (Debug)
315 printf
316 ("Using env %s, set STD_INFINITE to 1\n",
317 USC_LOOP_WALLTIME);
318 }
319 }
320 }
321 if (!(options & OPT_duration) && (ptr = getenv("USC_DURATION")) != NULL) {
322 if (sscanf(ptr, "%f", &ftmp) == 1 && ftmp >= 0.0) {
323 STD_LOOP_DURATION = ftmp;
324 if (Debug)
325 printf
326 ("Using env USC_DURATION, set STD_LOOP_DURATION to %f\n",
327 ftmp);
328 if (STD_LOOP_DURATION == 0.0) { /* if arg is 0, set infinite loop flag */
329 STD_INFINITE = 1;
330 if (Debug)
331 printf
332 ("Using env USC_DURATION, set STD_INFINITE to 1\n");
333 }
334 }
335 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800336
337 /*
Wanlong Gao354ebb42012-12-07 10:10:04 +0800338 * The following are special system testing envs to turn on special
339 * hooks in the code.
340 */
341 if ((ptr = getenv("USC_TP_BARRIER")) != NULL) {
342 if (sscanf(ptr, "%i", &k) == 1 && k >= 0)
343 STD_TP_barrier = k;
344 else
345 STD_TP_barrier = 1;
Garrett Cooper903910d2010-11-23 09:27:44 -0800346 if (Debug)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800347 printf
348 ("using env USC_TP_BARRIER, Set STD_TP_barrier to %d\n",
349 STD_TP_barrier);
350 }
alaffincc2e5552000-07-27 17:13:18 +0000351
Wanlong Gao354ebb42012-12-07 10:10:04 +0800352 if ((ptr = getenv("USC_LP_BARRIER")) != NULL) {
353 if (sscanf(ptr, "%i", &k) == 1 && k >= 0)
354 STD_LP_barrier = k;
355 else
356 STD_LP_barrier = 1;
357 if (Debug)
358 printf
359 ("using env USC_LP_BARRIER, Set STD_LP_barrier to %d\n",
360 STD_LP_barrier);
361 }
alaffincc2e5552000-07-27 17:13:18 +0000362
Wanlong Gao354ebb42012-12-07 10:10:04 +0800363 if ((ptr = getenv("USC_TP_SHMEM")) != NULL) {
364 if (sscanf(ptr, "%i", &k) == 1 && k >= 0) {
365 STD_TP_shmem_sz = k;
366 if (Debug)
367 printf
368 ("Using env USC_TP_SHMEM, Set STD_TP_shmem_sz to %d\n",
369 STD_TP_shmem_sz);
370 }
371 }
alaffincc2e5552000-07-27 17:13:18 +0000372
Wanlong Gao354ebb42012-12-07 10:10:04 +0800373 if ((ptr = getenv("USC_LP_SHMEM")) != NULL) {
374 if (sscanf(ptr, "%i", &k) == 1 && k >= 0) {
375 STD_LP_shmem = k;
376 if (Debug)
377 printf
378 ("Using env USC_LP_SHMEM, Set STD_LP_shmem to %d\n",
379 STD_LP_shmem);
380 }
381 }
alaffincc2e5552000-07-27 17:13:18 +0000382
Wanlong Gao354ebb42012-12-07 10:10:04 +0800383 if ((ptr = getenv("USC_LD_SHMEM")) != NULL) {
384 if (sscanf(ptr, "%i", &k) == 1 && k >= 0) {
385 STD_LD_shmem = k;
386 if (Debug)
387 printf
388 ("Using env USC_LD_SHMEM, Set STD_LD_shmem to %d\n",
389 STD_LD_shmem);
390 }
391 }
alaffincc2e5552000-07-27 17:13:18 +0000392
Wanlong Gao354ebb42012-12-07 10:10:04 +0800393 if ((ptr = getenv("USC_TP_SBRK")) != NULL) {
394 if (sscanf(ptr, "%i", &k) == 1 && k >= 0) {
395 STD_TP_sbrk = k;
396 if (Debug)
397 printf
398 ("Using env USC_TP_SBRK, Set STD_TP_sbrk to %d\n",
399 STD_TP_sbrk);
400 }
401 }
robbiewd34d5812005-07-11 22:28:09 +0000402#if !defined(UCLINUX)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800403 if ((ptr = getenv("USC_LP_SBRK")) != NULL) {
404 if (sscanf(ptr, "%i", &k) == 1 && k >= 0) {
405 STD_LP_sbrk = k;
406 if (Debug)
407 printf
408 ("Using env USC_LP_SBRK, Set STD_LP_sbrk to %d\n",
409 STD_LP_sbrk);
410 }
411 }
robbiewd34d5812005-07-11 22:28:09 +0000412#endif /* if !defined(UCLINUX) */
alaffincc2e5552000-07-27 17:13:18 +0000413
Wanlong Gao354ebb42012-12-07 10:10:04 +0800414 if ((ptr = getenv("USC_LP_RECFUN")) != NULL) {
415 if (sscanf(ptr, "%i", &k) == 1 && k >= 0) {
416 STD_LP_recfun = k;
417 if (STD_bigstack != NULL)
418 STD_bigstack =
419 malloc(sizeof(struct usc_bigstack_t));
420 if (Debug)
421 printf
422 ("Using env USC_LP_RECFUN, Set STD_LP_recfun to %d\n",
423 STD_LP_recfun);
424 }
425 }
alaffincc2e5552000-07-27 17:13:18 +0000426
Wanlong Gao354ebb42012-12-07 10:10:04 +0800427 if ((ptr = getenv("USC_LD_RECFUN")) != NULL) {
428 if (sscanf(ptr, "%i", &k) == 1 && k >= 0) {
429 STD_LD_recfun = k;
430 if (STD_bigstack != NULL)
431 STD_bigstack =
432 malloc(sizeof(struct usc_bigstack_t));
433 if (Debug)
434 printf
435 ("Using env USC_LD_RECFUN, Set STD_LD_recfun to %d\n",
436 STD_LD_recfun);
437 }
438 }
alaffincc2e5552000-07-27 17:13:18 +0000439#if UNIT_TEST
Wanlong Gao354ebb42012-12-07 10:10:04 +0800440 printf("The following variables after option and env parsing:\n");
Wanlong Gao354ebb42012-12-07 10:10:04 +0800441 printf("STD_LOOP_DURATION = %f\n", STD_LOOP_DURATION);
Wanlong Gao354ebb42012-12-07 10:10:04 +0800442 printf("STD_LOOP_COUNT = %d\n", STD_LOOP_COUNT);
443 printf("STD_INFINITE = %d\n", STD_INFINITE);
alaffincc2e5552000-07-27 17:13:18 +0000444#endif
445
Cyril Hrubis2a81c752014-05-27 14:54:09 +0200446 return NULL;
alaffincc2e5552000-07-27 17:13:18 +0000447}
448
alaffincc2e5552000-07-27 17:13:18 +0000449/***********************************************************************
450 * This function will do desired end of global setup test
451 * hooks.
alaffincc2e5552000-07-27 17:13:18 +0000452 ***********************************************************************/
Cyril Hrubisee75ce32014-02-05 14:05:06 +0100453int usc_global_setup_hook(void)
alaffincc2e5552000-07-27 17:13:18 +0000454{
robbiewd34d5812005-07-11 22:28:09 +0000455#ifndef UCLINUX
Cyril Hrubis2a81c752014-05-27 14:54:09 +0200456 if (STD_TP_sbrk || STD_LP_sbrk)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800457 STD_start_break = sbrk(0); /* get original sbreak size */
alaffincc2e5552000-07-27 17:13:18 +0000458
Wanlong Gao354ebb42012-12-07 10:10:04 +0800459 if (STD_TP_sbrk) {
460 sbrk(STD_TP_sbrk);
461 if (Debug)
462 printf("after sbrk(%d)\n", STD_TP_sbrk);
463 }
robbiewd34d5812005-07-11 22:28:09 +0000464#endif
Wanlong Gao354ebb42012-12-07 10:10:04 +0800465 return 0;
alaffincc2e5552000-07-27 17:13:18 +0000466}
467
Wanlong Gao354ebb42012-12-07 10:10:04 +0800468#define USECS_PER_SEC 1000000 /* microseconds per second */
alaffincc2e5552000-07-27 17:13:18 +0000469
470/***********************************************************************
Cyril Hrubis4d964f62012-03-07 18:02:37 +0100471 * Returns current time in microseconds since 1970.
alaffincc2e5552000-07-27 17:13:18 +0000472 ***********************************************************************/
Cyril Hrubis4d964f62012-03-07 18:02:37 +0100473static uint64_t get_current_time(void)
alaffincc2e5552000-07-27 17:13:18 +0000474{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800475 struct timeval curtime;
alaffincc2e5552000-07-27 17:13:18 +0000476
Wanlong Gao354ebb42012-12-07 10:10:04 +0800477 gettimeofday(&curtime, NULL);
alaffincc2e5552000-07-27 17:13:18 +0000478
Wanlong Gao354ebb42012-12-07 10:10:04 +0800479 return (((uint64_t) curtime.tv_sec) * USECS_PER_SEC) + curtime.tv_usec;
alaffincc2e5552000-07-27 17:13:18 +0000480}
481
482/***********************************************************************
483 *
484 * This function will determine if test should continue iterating
485 * If the STD_INFINITE flag is set, return 1.
486 * If the STD_LOOP_COUNT variable is set, compare it against
487 * the counter.
488 * If the STD_LOOP_DURATION variable is set, compare current time against
489 * calculated stop_time.
490 * This function will return 1 until all desired looping methods
vapier45a8ba02009-07-20 10:59:32 +0000491 * have been met.
alaffincc2e5552000-07-27 17:13:18 +0000492 *
493 * counter integer is supplied by the user program.
494 ***********************************************************************/
Garrett Cooper8b02c1c2011-01-26 01:34:49 -0800495int usc_test_looping(int counter)
alaffincc2e5552000-07-27 17:13:18 +0000496{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800497 static int first_time = 1;
498 static uint64_t stop_time = 0;
Wanlong Gao354ebb42012-12-07 10:10:04 +0800499 int keepgoing = 0;
alaffincc2e5552000-07-27 17:13:18 +0000500
501 /*
Wanlong Gao354ebb42012-12-07 10:10:04 +0800502 * If this is the first iteration and we are looping for
503 * duration of STD_LOOP_DURATION seconds (fractional) or
504 * doing loop delays, get the clocks per second.
alaffincc2e5552000-07-27 17:13:18 +0000505 */
Wanlong Gao354ebb42012-12-07 10:10:04 +0800506 if (first_time) {
507 first_time = 0;
508
509 /*
510 * If looping for duration, calculate stop time in
511 * clocks.
512 */
513 if (STD_LOOP_DURATION) {
514 stop_time =
515 (uint64_t) (USECS_PER_SEC * STD_LOOP_DURATION)
516 + get_current_time();
517 }
Wanlong Gao354ebb42012-12-07 10:10:04 +0800518 }
519
520 if (STD_INFINITE)
521 keepgoing++;
522
523 if (STD_LOOP_COUNT && counter < STD_LOOP_COUNT)
524 keepgoing++;
525
526 if (STD_LOOP_DURATION != 0.0 && get_current_time() < stop_time)
527 keepgoing++;
528
529 if (keepgoing == 0)
530 return 0;
531
532 /*
533 * The following code allows special system testing hooks.
534 */
535
536 if (STD_LP_recfun) {
Garrett Cooper903910d2010-11-23 09:27:44 -0800537 if (Debug)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800538 printf
539 ("calling usc_recressive_func(0, %d, *STD_bigstack)\n",
540 STD_LP_recfun);
541 usc_recressive_func(0, STD_LP_recfun, *STD_bigstack);
alaffincc2e5552000-07-27 17:13:18 +0000542 }
robbiewd34d5812005-07-11 22:28:09 +0000543#if !defined(UCLINUX)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800544 if (STD_LP_sbrk) {
545 if (Debug)
546 printf("about to do sbrk(%d)\n", STD_LP_sbrk);
547 sbrk(STD_LP_sbrk);
548 }
robbiewd34d5812005-07-11 22:28:09 +0000549#endif
alaffincc2e5552000-07-27 17:13:18 +0000550
Wanlong Gao354ebb42012-12-07 10:10:04 +0800551 if (keepgoing)
552 return 1;
553 else
Cyril Hrubis2a81c752014-05-27 14:54:09 +0200554 return 0;
alaffincc2e5552000-07-27 17:13:18 +0000555}
556
alaffincc2e5552000-07-27 17:13:18 +0000557/*
558 * This function recressively calls itself max times.
vapier45a8ba02009-07-20 10:59:32 +0000559 */
Garrett Cooper8b02c1c2011-01-26 01:34:49 -0800560static void usc_recressive_func(int cnt, int max, struct usc_bigstack_t bstack)
alaffincc2e5552000-07-27 17:13:18 +0000561{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800562 if (cnt < max)
563 usc_recressive_func(cnt + 1, max, bstack);
alaffincc2e5552000-07-27 17:13:18 +0000564
565}
566
567#if UNIT_TEST
Cyril Hrubis15899192014-05-27 16:57:45 +0200568#include <time.h>
569
alaffincc2e5552000-07-27 17:13:18 +0000570/******************************************************************************
571 * UNIT TEST CODE
572 * UNIT TEST CODE
vapier45a8ba02009-07-20 10:59:32 +0000573 *
alaffincc2e5552000-07-27 17:13:18 +0000574 * this following code is provide so that unit testing can
575 * be done fairly easily.
576 ******************************************************************************/
577
578int Help = 0;
579int Help2 = 0;
580char *ptr;
581
Markos Chandrasfd4ada72012-01-03 11:10:37 +0000582long TEST_RETURN;
alaffincc2e5552000-07-27 17:13:18 +0000583int TEST_ERRNO;
584
alaffincc2e5552000-07-27 17:13:18 +0000585/* for test specific parse_opts options */
586option_t Options[] = {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800587 {"help", &Help2, NULL}, /* -help option */
588 {"h", &Help, NULL}, /* -h option */
alaffincc2e5552000-07-27 17:13:18 +0000589
590#if INVALID_TEST_CASES
Wanlong Gao354ebb42012-12-07 10:10:04 +0800591 {"missingflag", NULL, &ptr}, /* error */
592 {"missingarg:", &Help, NULL}, /* error */
593#endif /* INVALID_TEST_CASES */
alaffincc2e5552000-07-27 17:13:18 +0000594
Wanlong Gao354ebb42012-12-07 10:10:04 +0800595 {NULL, NULL, NULL}
alaffincc2e5552000-07-27 17:13:18 +0000596};
597
Garrett Cooper8b02c1c2011-01-26 01:34:49 -0800598int main(int argc, char **argv)
alaffincc2e5552000-07-27 17:13:18 +0000599{
Wanlong Gao354ebb42012-12-07 10:10:04 +0800600 int lc;
601 char *msg;
602 struct timeval t;
603 int cnt;
alaffincc2e5552000-07-27 17:13:18 +0000604
Wanlong Gao354ebb42012-12-07 10:10:04 +0800605 if ((msg = parse_opts(argc, argv, Options, NULL)) != NULL) {
606 printf("ERROR: %s\n", msg);
607 exit(1);
608 }
alaffincc2e5552000-07-27 17:13:18 +0000609
Wanlong Gao354ebb42012-12-07 10:10:04 +0800610 TEST_PAUSE;
alaffincc2e5552000-07-27 17:13:18 +0000611
Wanlong Gao354ebb42012-12-07 10:10:04 +0800612 for (lc = 0; TEST_LOOPING(lc); lc++) {
vapier45a8ba02009-07-20 10:59:32 +0000613
Wanlong Gao354ebb42012-12-07 10:10:04 +0800614 TEST(gettimeofday(&t, NULL));
615 printf("iter=%d: sec:%d, usec:%6.6d %s", lc + 1, t.tv_sec,
616 t.tv_usec, ctime(&t.tv_sec));
617 }
alaffincc2e5552000-07-27 17:13:18 +0000618
Wanlong Gao354ebb42012-12-07 10:10:04 +0800619 TEST_CLEANUP;
alaffincc2e5552000-07-27 17:13:18 +0000620
Wanlong Gao354ebb42012-12-07 10:10:04 +0800621 exit(0);
alaffincc2e5552000-07-27 17:13:18 +0000622}
623
Garrett Cooper8b02c1c2011-01-26 01:34:49 -0800624#endif /* UNIT_TEST */