1 /*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
3 * AUTHOR : William Roske/Richard Logan
4 *
5 * 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.
8 *
9 * 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.
12 *
13 * 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.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 *
24 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
25 * Mountain View, CA 94043, or:
26 *
27 * http://www.sgi.com
28 *
29 * For further information regarding this notice, see:
30 *
31 * http://oss.sgi.com/projects/GenInfo/NoticeExplan/
32 */
33
34 #include "config.h"
35 #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>
43 #include <stdint.h>
44
45 #include "test.h"
46 #include "ltp_priv.h"
47 #include "usctest.h"
48
49 #ifndef UNIT_TEST
50 #define UNIT_TEST 0
51 #endif
52
53 /* Define flags and args for standard options */
54 static int STD_PAUSE = 0; /* flag indicating to pause before actual start, */
55 /* for contention mode */
56 static int STD_INFINITE = 0; /* flag indciating to loop forever */
57 int STD_LOOP_COUNT = 1; /* number of iterations */
58
59 static float STD_LOOP_DURATION = 0.0; /* duration value in fractional seconds */
60 static float STD_LOOP_DELAY = 0.0; /* loop delay value in fractional seconds */
61
62 static char **STD_opt_arr = NULL; /* array of option strings */
63 static int STD_argind = 1; /* argv index to next argv element */
64 /* (first argument) */
65 /* To getopt users, it is like optind */
66
67 /*
68 * The following variables are to support system testing additions.
69 */
70 static int STD_TP_barrier = 0; /* flag to do barrier in TEST_PAUSE */
71 /* 2 - wait_barrier(), 3 - set_barrier(), * - barrier() */
72 static int STD_LP_barrier = 0; /* flag to do barrier in TEST_LOOPING */
73 /* 2 - wait_barrier(), 3 - set_barrier(), * - barrier() */
74 static int STD_TP_shmem_sz = 0; /* shmalloc this many words per pe in TEST_PAUSE */
75 static int STD_LD_shmem = 0; /* flag to do shmem_puts and shmem_gets during delay */
76 static int STD_LP_shmem = 0; /* flag to do shmem_puts and gets during TEST_LOOPING */
77 static int STD_LD_recfun = 0; /* do recressive function calls in loop delay */
78 static int STD_LP_recfun = 0; /* do recressive function calls in TEST_LOOPING */
79 static int STD_TP_sbrk = 0; /* do sbrk in TEST_PAUSE */
80 static int STD_LP_sbrk = 0; /* do sbrk in TEST_LOOPING */
81 static char *STD_start_break = 0; /* original sbrk size */
82 static int Debug = 0;
83
84 static struct std_option_t {
85 char *optstr;
86 char *help;
87 char *flag;
88 char **arg;
89 } std_options[] = {
90 {"h", " -h Show this help screen\n", NULL, NULL},
91 {"i:", " -i n Execute test n times\n", NULL, NULL},
92 {"I:", " -I x Execute test for x seconds\n", NULL, NULL},
93 {"p", " -p Pause for SIGUSR1 before starting\n", NULL, NULL},
94 {"P:", " -P x Pause for x seconds between iterations\n", NULL, NULL},
95 #ifdef UCLINUX
96 {"C:",
97 " -C ARG Run the child process with arguments ARG (for internal use)\n",
98 NULL, NULL},
99 #endif
100 {NULL, NULL, NULL, NULL}
101 };
102
103 /*
104 * Structure for usc_recressive_func argument
105 */
106 struct usc_bigstack_t {
107 char space[4096];
108 };
109
110 static struct usc_bigstack_t *STD_bigstack = NULL;
111
112 /* define the string length for Mesg and Mesg2 strings */
113 #define STRLEN 2048
114
115 static char Mesg2[STRLEN]; /* holds possible return string */
116 static void usc_recressive_func();
117
118 /*
119 * Define bits for options that might have env variable default
120 */
121 #define OPT_iteration 01
122 #define OPT_duration 04
123 #define OPT_delay 010
124
125 #ifdef UCLINUX
126 /* Allocated and used in self_exec.c: */
127 extern char *child_args; /* Arguments to child when -C is used */
128 #endif
129
print_help(void (* user_help)(void))130 static void print_help(void (*user_help)(void))
131 {
132 int i;
133
134 for (i = 0; std_options[i].optstr; ++i) {
135 if (std_options[i].help)
136 printf("%s", std_options[i].help);
137 }
138
139 if (user_help)
140 user_help();
141 }
142
143 /**********************************************************************
144 * parse_opts:
145 **********************************************************************/
parse_opts(int ac,char ** av,const option_t * user_optarr,void (* uhf)(void))146 const char *parse_opts(int ac, char **av, const option_t * user_optarr,
147 void (*uhf)(void))
148 {
149 int found; /* flag to indicate that an option specified was */
150 /* found in the user's list */
151 int k; /* scratch integer for returns and short time usage */
152 float ftmp; /* tmp float for parsing env variables */
153 char *ptr; /* used in getting env variables */
154 int options = 0; /* no options specified */
155 int optstrlen, i;
156 char *optionstr;
157 int opt;
158
159 /*
160 * If not the first time this function is called, release the old STD_opt_arr
161 * vector.
162 */
163 if (STD_opt_arr != NULL) {
164 free(STD_opt_arr);
165 STD_opt_arr = NULL;
166 }
167 /* Calculate how much space we need for the option string */
168 optstrlen = 0;
169 for (i = 0; std_options[i].optstr; ++i)
170 optstrlen += strlen(std_options[i].optstr);
171 if (user_optarr)
172 for (i = 0; user_optarr[i].option; ++i) {
173 if (strlen(user_optarr[i].option) > 2)
174 return
175 "parse_opts: ERROR - Only short options are allowed";
176 optstrlen += strlen(user_optarr[i].option);
177 }
178 optstrlen += 1;
179
180 /* Create the option string for getopt */
181 optionstr = malloc(optstrlen);
182 if (!optionstr)
183 return
184 "parse_opts: ERROR - Could not allocate memory for optionstr";
185
186 optionstr[0] = '\0';
187
188 for (i = 0; std_options[i].optstr; ++i)
189 strcat(optionstr, std_options[i].optstr);
190 if (user_optarr)
191 for (i = 0; user_optarr[i].option; ++i)
192 /* only add the option if it wasn't there already */
193 if (strchr(optionstr, user_optarr[i].option[0]) == NULL)
194 strcat(optionstr, user_optarr[i].option);
195
196 /*
197 * Loop through av parsing options.
198 */
199 while ((opt = getopt(ac, av, optionstr)) > 0) {
200
201 STD_argind = optind;
202
203 switch (opt) {
204 case '?': /* Unknown option */
205 return "Unknown option";
206 break;
207 case ':': /* Missing Arg */
208 return "Missing argument";
209 break;
210 case 'i': /* Iterations */
211 options |= OPT_iteration;
212 STD_LOOP_COUNT = atoi(optarg);
213 if (STD_LOOP_COUNT == 0)
214 STD_INFINITE = 1;
215 break;
216 case 'P': /* Delay between iterations */
217 options |= OPT_delay;
218 STD_LOOP_DELAY = atof(optarg);
219 break;
220 case 'I': /* Time duration */
221 options |= OPT_duration;
222 STD_LOOP_DURATION = atof(optarg);
223 if (STD_LOOP_DURATION == 0.0)
224 STD_INFINITE = 1;
225 break;
226 case 'p': /* Pause for SIGUSR1 */
227 STD_PAUSE = 1;
228 break;
229 case 'h': /* Help */
230 print_help(uhf);
231 exit(0);
232 break;
233 #ifdef UCLINUX
234 case 'C': /* Run child */
235 child_args = optarg;
236 break;
237 #endif
238 default:
239
240 /* Check all the user specified options */
241 found = 0;
242 for (i = 0; user_optarr[i].option; ++i) {
243
244 if (opt == user_optarr[i].option[0]) {
245 /* Yup, This is a user option, set the flag and look for argument */
246 if (user_optarr[i].flag) {
247 *user_optarr[i].flag = 1;
248 }
249 found++;
250
251 /* save the argument at the user's location */
252 if (user_optarr[i].
253 option[strlen(user_optarr[i].option)
254 - 1] == ':') {
255 *user_optarr[i].arg = optarg;
256 }
257 break; /* option found - break out of the for loop */
258 }
259 }
260 /* This condition "should never happen". SO CHECK FOR IT!!!! */
261 if (!found) {
262 sprintf(Mesg2,
263 "parse_opts: ERROR - option:\"%c\" NOT FOUND... INTERNAL "
264 "ERROR", opt);
265 return (Mesg2);
266 }
267 }
268
269 }
270 free(optionstr);
271
272 STD_argind = optind;
273
274 /*
275 * Turn on debug
276 */
277 if (getenv("USC_DEBUG") != NULL) {
278 Debug = 1;
279 printf("env USC_DEBUG is defined, turning on debug\n");
280 }
281 if (getenv("USC_VERBOSE") != NULL) {
282 Debug = 1;
283 printf("env USC_VERBOSE is defined, turning on debug\n");
284 }
285
286 /*
287 * If the USC_ITERATION_ENV environmental variable is set to
288 * a number, use that number as iteration count (same as -c option).
289 * The -c option with arg will be used even if this env var is set.
290 */
291 if (!(options & OPT_iteration)
292 && (ptr = getenv(USC_ITERATION_ENV)) != NULL) {
293 if (sscanf(ptr, "%i", &k) == 1) {
294 if (k == 0) { /* if arg is 0, set infinite loop flag */
295 STD_INFINITE = 1;
296 if (Debug)
297 printf
298 ("Using env %s, set STD_INFINITE to 1\n",
299 USC_ITERATION_ENV);
300 } else { /* else, set the loop count to the arguement */
301 STD_LOOP_COUNT = k;
302 if (Debug)
303 printf
304 ("Using env %s, set STD_LOOP_COUNT to %d\n",
305 USC_ITERATION_ENV, k);
306 }
307 }
308 }
309
310 /*
311 * If the USC_LOOP_WALLTIME environmental variable is set,
312 * use that number as duration (same as -I option).
313 * The -I option with arg will be used even if this env var is set.
314 */
315
316 if (!(options & OPT_duration) &&
317 (ptr = getenv(USC_LOOP_WALLTIME)) != NULL) {
318 if (sscanf(ptr, "%f", &ftmp) == 1 && ftmp >= 0.0) {
319 STD_LOOP_DURATION = ftmp;
320 if (Debug)
321 printf
322 ("Using env %s, set STD_LOOP_DURATION to %f\n",
323 USC_LOOP_WALLTIME, ftmp);
324 if (STD_LOOP_DURATION == 0.0) { /* if arg is 0, set infinite loop flag */
325 STD_INFINITE = 1;
326 if (Debug)
327 printf
328 ("Using env %s, set STD_INFINITE to 1\n",
329 USC_LOOP_WALLTIME);
330 }
331 }
332 }
333 if (!(options & OPT_duration) && (ptr = getenv("USC_DURATION")) != NULL) {
334 if (sscanf(ptr, "%f", &ftmp) == 1 && ftmp >= 0.0) {
335 STD_LOOP_DURATION = ftmp;
336 if (Debug)
337 printf
338 ("Using env USC_DURATION, set STD_LOOP_DURATION to %f\n",
339 ftmp);
340 if (STD_LOOP_DURATION == 0.0) { /* if arg is 0, set infinite loop flag */
341 STD_INFINITE = 1;
342 if (Debug)
343 printf
344 ("Using env USC_DURATION, set STD_INFINITE to 1\n");
345 }
346 }
347 }
348 /*
349 * If the USC_LOOP_DELAY environmental variable is set,
350 * use that number as delay in factional seconds (same as -P option).
351 * The -P option with arg will be used even if this env var is set.
352 */
353 if (!(options & OPT_delay) && (ptr = getenv(USC_LOOP_DELAY)) != NULL) {
354 if (sscanf(ptr, "%f", &ftmp) == 1 && ftmp >= 0.0) {
355 STD_LOOP_DELAY = ftmp;
356 if (Debug)
357 printf
358 ("Using env %s, set STD_LOOP_DELAY = %f\n",
359 USC_LOOP_DELAY, ftmp);
360 }
361 }
362
363 /*
364 * The following are special system testing envs to turn on special
365 * hooks in the code.
366 */
367 if ((ptr = getenv("USC_TP_BARRIER")) != NULL) {
368 if (sscanf(ptr, "%i", &k) == 1 && k >= 0)
369 STD_TP_barrier = k;
370 else
371 STD_TP_barrier = 1;
372 if (Debug)
373 printf
374 ("using env USC_TP_BARRIER, Set STD_TP_barrier to %d\n",
375 STD_TP_barrier);
376 }
377
378 if ((ptr = getenv("USC_LP_BARRIER")) != NULL) {
379 if (sscanf(ptr, "%i", &k) == 1 && k >= 0)
380 STD_LP_barrier = k;
381 else
382 STD_LP_barrier = 1;
383 if (Debug)
384 printf
385 ("using env USC_LP_BARRIER, Set STD_LP_barrier to %d\n",
386 STD_LP_barrier);
387 }
388
389 if ((ptr = getenv("USC_TP_SHMEM")) != NULL) {
390 if (sscanf(ptr, "%i", &k) == 1 && k >= 0) {
391 STD_TP_shmem_sz = k;
392 if (Debug)
393 printf
394 ("Using env USC_TP_SHMEM, Set STD_TP_shmem_sz to %d\n",
395 STD_TP_shmem_sz);
396 }
397 }
398
399 if ((ptr = getenv("USC_LP_SHMEM")) != NULL) {
400 if (sscanf(ptr, "%i", &k) == 1 && k >= 0) {
401 STD_LP_shmem = k;
402 if (Debug)
403 printf
404 ("Using env USC_LP_SHMEM, Set STD_LP_shmem to %d\n",
405 STD_LP_shmem);
406 }
407 }
408
409 if ((ptr = getenv("USC_LD_SHMEM")) != NULL) {
410 if (sscanf(ptr, "%i", &k) == 1 && k >= 0) {
411 STD_LD_shmem = k;
412 if (Debug)
413 printf
414 ("Using env USC_LD_SHMEM, Set STD_LD_shmem to %d\n",
415 STD_LD_shmem);
416 }
417 }
418
419 if ((ptr = getenv("USC_TP_SBRK")) != NULL) {
420 if (sscanf(ptr, "%i", &k) == 1 && k >= 0) {
421 STD_TP_sbrk = k;
422 if (Debug)
423 printf
424 ("Using env USC_TP_SBRK, Set STD_TP_sbrk to %d\n",
425 STD_TP_sbrk);
426 }
427 }
428 #if !defined(UCLINUX)
429 if ((ptr = getenv("USC_LP_SBRK")) != NULL) {
430 if (sscanf(ptr, "%i", &k) == 1 && k >= 0) {
431 STD_LP_sbrk = k;
432 if (Debug)
433 printf
434 ("Using env USC_LP_SBRK, Set STD_LP_sbrk to %d\n",
435 STD_LP_sbrk);
436 }
437 }
438 #endif /* if !defined(UCLINUX) */
439
440 if ((ptr = getenv("USC_LP_RECFUN")) != NULL) {
441 if (sscanf(ptr, "%i", &k) == 1 && k >= 0) {
442 STD_LP_recfun = k;
443 if (STD_bigstack != NULL)
444 STD_bigstack =
445 malloc(sizeof(struct usc_bigstack_t));
446 if (Debug)
447 printf
448 ("Using env USC_LP_RECFUN, Set STD_LP_recfun to %d\n",
449 STD_LP_recfun);
450 }
451 }
452
453 if ((ptr = getenv("USC_LD_RECFUN")) != NULL) {
454 if (sscanf(ptr, "%i", &k) == 1 && k >= 0) {
455 STD_LD_recfun = k;
456 if (STD_bigstack != NULL)
457 STD_bigstack =
458 malloc(sizeof(struct usc_bigstack_t));
459 if (Debug)
460 printf
461 ("Using env USC_LD_RECFUN, Set STD_LD_recfun to %d\n",
462 STD_LD_recfun);
463 }
464 }
465 #if UNIT_TEST
466 printf("The following variables after option and env parsing:\n");
467 printf("STD_LOOP_DURATION = %f\n", STD_LOOP_DURATION);
468 printf("STD_LOOP_DELAY = %f\n", STD_LOOP_DELAY);
469 printf("STD_LOOP_COUNT = %d\n", STD_LOOP_COUNT);
470 printf("STD_INFINITE = %d\n", STD_INFINITE);
471 printf("STD_PAUSE = %d\n", STD_PAUSE);
472 #endif
473
474 return NULL;
475 }
476
477 /*
478 * routine to goto when we get the SIGUSR1 for STD_PAUSE
479 */
STD_go(int sig)480 static void STD_go(int sig)
481 {
482 (void)sig;
483 return;
484 }
485
486 /***********************************************************************
487 * This function will do desired end of global setup test
488 * hooks.
489 * Currently it will only do a pause waiting for sigusr1 if
490 * STD_PAUSE is set.
491 *
492 ***********************************************************************/
usc_global_setup_hook(void)493 int usc_global_setup_hook(void)
494 {
495 #ifndef UCLINUX
496 /* temp variable to store old signal action to be restored after pause */
497 int (*_TMP_FUNC) (void);
498
499 /*
500 * pause waiting for sigusr1.
501 */
502 if (STD_PAUSE) {
503 _TMP_FUNC = (int (*)())signal(SIGUSR1, STD_go);
504 pause();
505 signal(SIGUSR1, (void (*)())_TMP_FUNC);
506 }
507
508 if (STD_TP_sbrk || STD_LP_sbrk)
509 STD_start_break = sbrk(0); /* get original sbreak size */
510
511 if (STD_TP_sbrk) {
512 sbrk(STD_TP_sbrk);
513 if (Debug)
514 printf("after sbrk(%d)\n", STD_TP_sbrk);
515 }
516 #endif
517 return 0;
518 }
519
520 #define USECS_PER_SEC 1000000 /* microseconds per second */
521
522 /***********************************************************************
523 * Returns current time in microseconds since 1970.
524 ***********************************************************************/
get_current_time(void)525 static uint64_t get_current_time(void)
526 {
527 struct timeval curtime;
528
529 gettimeofday(&curtime, NULL);
530
531 return (((uint64_t) curtime.tv_sec) * USECS_PER_SEC) + curtime.tv_usec;
532 }
533
534 /***********************************************************************
535 *
536 * This function will determine if test should continue iterating
537 * If the STD_INFINITE flag is set, return 1.
538 * If the STD_LOOP_COUNT variable is set, compare it against
539 * the counter.
540 * If the STD_LOOP_DURATION variable is set, compare current time against
541 * calculated stop_time.
542 * This function will return 1 until all desired looping methods
543 * have been met.
544 *
545 * counter integer is supplied by the user program.
546 ***********************************************************************/
usc_test_looping(int counter)547 int usc_test_looping(int counter)
548 {
549 static int first_time = 1;
550 static uint64_t stop_time = 0;
551 static uint64_t delay;
552 uint64_t ct, end;
553 int keepgoing = 0;
554
555 /*
556 * If this is the first iteration and we are looping for
557 * duration of STD_LOOP_DURATION seconds (fractional) or
558 * doing loop delays, get the clocks per second.
559 */
560 if (first_time) {
561 first_time = 0;
562
563 /*
564 * If looping for duration, calculate stop time in
565 * clocks.
566 */
567 if (STD_LOOP_DURATION) {
568 stop_time =
569 (uint64_t) (USECS_PER_SEC * STD_LOOP_DURATION)
570 + get_current_time();
571 }
572
573 /*
574 * If doing delay each iteration, calcuate the number
575 * of clocks for each delay.
576 */
577 if (STD_LOOP_DELAY)
578 delay = USECS_PER_SEC * STD_LOOP_DELAY;
579 }
580
581 /*
582 * if delay each iteration, loop for delay clocks.
583 * This will not be done on first iteration.
584 * The delay will happen before determining if
585 * there will be another iteration.
586 */
587 else if (STD_LOOP_DELAY) {
588 ct = get_current_time();
589 end = ct + delay;
590 while (ct < end) {
591 /*
592 * The following are special test hooks in the delay loop.
593 */
594 if (STD_LD_recfun) {
595 if (Debug)
596 printf
597 ("calling usc_recressive_func(0, %d, *STD_bigstack)\n",
598 STD_LD_recfun);
599 usc_recressive_func(0, STD_LD_recfun,
600 *STD_bigstack);
601 }
602
603 ct = get_current_time();
604 }
605 }
606
607 if (STD_INFINITE)
608 keepgoing++;
609
610 if (STD_LOOP_COUNT && counter < STD_LOOP_COUNT)
611 keepgoing++;
612
613 if (STD_LOOP_DURATION != 0.0 && get_current_time() < stop_time)
614 keepgoing++;
615
616 if (keepgoing == 0)
617 return 0;
618
619 /*
620 * The following code allows special system testing hooks.
621 */
622
623 if (STD_LP_recfun) {
624 if (Debug)
625 printf
626 ("calling usc_recressive_func(0, %d, *STD_bigstack)\n",
627 STD_LP_recfun);
628 usc_recressive_func(0, STD_LP_recfun, *STD_bigstack);
629 }
630 #if !defined(UCLINUX)
631 if (STD_LP_sbrk) {
632 if (Debug)
633 printf("about to do sbrk(%d)\n", STD_LP_sbrk);
634 sbrk(STD_LP_sbrk);
635 }
636 #endif
637
638 if (keepgoing)
639 return 1;
640 else
641 return 0;
642 }
643
644 /*
645 * This function recressively calls itself max times.
646 */
usc_recressive_func(int cnt,int max,struct usc_bigstack_t bstack)647 static void usc_recressive_func(int cnt, int max, struct usc_bigstack_t bstack)
648 {
649 if (cnt < max)
650 usc_recressive_func(cnt + 1, max, bstack);
651
652 }
653
654 #if UNIT_TEST
655 #include <time.h>
656
657 /******************************************************************************
658 * UNIT TEST CODE
659 * UNIT TEST CODE
660 *
661 * this following code is provide so that unit testing can
662 * be done fairly easily.
663 ******************************************************************************/
664
665 int Help = 0;
666 int Help2 = 0;
667 char *ptr;
668
669 long TEST_RETURN;
670 int TEST_ERRNO;
671
672 /* for test specific parse_opts options */
673 option_t Options[] = {
674 {"help", &Help2, NULL}, /* -help option */
675 {"h", &Help, NULL}, /* -h option */
676
677 #if INVALID_TEST_CASES
678 {"missingflag", NULL, &ptr}, /* error */
679 {"missingarg:", &Help, NULL}, /* error */
680 #endif /* INVALID_TEST_CASES */
681
682 {NULL, NULL, NULL}
683 };
684
main(int argc,char ** argv)685 int main(int argc, char **argv)
686 {
687 int lc;
688 char *msg;
689 struct timeval t;
690 int cnt;
691
692 if ((msg = parse_opts(argc, argv, Options, NULL)) != NULL) {
693 printf("ERROR: %s\n", msg);
694 exit(1);
695 }
696
697 TEST_PAUSE;
698
699 for (lc = 0; TEST_LOOPING(lc); lc++) {
700
701 TEST(gettimeofday(&t, NULL));
702 printf("iter=%d: sec:%d, usec:%6.6d %s", lc + 1, t.tv_sec,
703 t.tv_usec, ctime(&t.tv_sec));
704 }
705
706 TEST_CLEANUP;
707
708 exit(0);
709 }
710
711 #endif /* UNIT_TEST */
712