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