• 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 <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_INFINITE = 0;	/* flag indciating to loop forever */
55 int STD_LOOP_COUNT = 1;		/* number of iterations */
56 
57 static float STD_LOOP_DURATION = 0.0;	/* duration value in fractional seconds */
58 
59 static char **STD_opt_arr = NULL;	/* array of option strings */
60 static int STD_argind = 1;	/* argv index to next argv element */
61 				/* (first argument) */
62 				/* To getopt users, it is like optind */
63 
64 /*
65  * The following variables are to support system testing additions.
66  */
67 static int STD_TP_barrier = 0;	/* flag to do barrier in TEST_PAUSE */
68 				/* 2 - wait_barrier(), 3 - set_barrier(), * - barrier() */
69 static int STD_LP_barrier = 0;	/* flag to do barrier in TEST_LOOPING */
70 				/* 2 - wait_barrier(), 3 - set_barrier(), * - barrier() */
71 static int STD_TP_shmem_sz = 0;	/* shmalloc this many words per pe in TEST_PAUSE */
72 static int STD_LD_shmem = 0;	/* flag to do shmem_puts and shmem_gets during delay */
73 static int STD_LP_shmem = 0;	/* flag to do shmem_puts and gets during TEST_LOOPING */
74 static int STD_LD_recfun = 0;	/* do recressive function calls in loop delay */
75 static int STD_LP_recfun = 0;	/* do recressive function calls in TEST_LOOPING */
76 static int STD_TP_sbrk = 0;	/* do sbrk in TEST_PAUSE */
77 static int STD_LP_sbrk = 0;	/* do sbrk in TEST_LOOPING */
78 static char *STD_start_break = 0;	/* original sbrk size */
79 static int Debug = 0;
80 
81 static struct std_option_t {
82 	char *optstr;
83 	char *help;
84 	char *flag;
85 	char **arg;
86 } std_options[] = {
87 	{"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},
90 #ifdef UCLINUX
91 	{"C:",
92 	      "  -C ARG  Run the child process with arguments ARG (for internal use)\n",
93 		    NULL, NULL},
94 #endif
95 	{NULL, NULL, NULL, NULL}
96 };
97 
98 /*
99  * Structure for usc_recressive_func argument
100  */
101 struct usc_bigstack_t {
102 	char space[4096];
103 };
104 
105 static struct usc_bigstack_t *STD_bigstack = NULL;
106 
107 /* define the string length for Mesg and Mesg2 strings */
108 #define STRLEN 2048
109 
110 static char Mesg2[STRLEN];	/* holds possible return string */
111 static void usc_recressive_func();
112 
113 /*
114  * Define bits for options that might have env variable default
115  */
116 #define OPT_iteration		01
117 #define OPT_duration		04
118 #define OPT_delay		010
119 
120 #ifdef UCLINUX
121 /* Allocated and used in self_exec.c: */
122 extern char *child_args;	/* Arguments to child when -C is used */
123 #endif
124 
print_help(void (* user_help)(void))125 static 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 
138 /**********************************************************************
139  * parse_opts:
140  **********************************************************************/
parse_opts(int ac,char ** av,const option_t * user_optarr,void (* uhf)(void))141 const char *parse_opts(int ac, char **av, const option_t * user_optarr,
142                        void (*uhf)(void))
143 {
144 	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;
152 	int opt;
153 
154 	/*
155 	 * If not the first time this function is called, release the old STD_opt_arr
156 	 * vector.
157 	 */
158 	if (STD_opt_arr != NULL) {
159 		free(STD_opt_arr);
160 		STD_opt_arr = NULL;
161 	}
162 	/* 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;
174 
175 	/* 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";
180 
181 	optionstr[0] = '\0';
182 
183 	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);
190 
191 	/*
192 	 *  Loop through av parsing options.
193 	 */
194 	while ((opt = getopt(ac, av, optionstr)) > 0) {
195 
196 		STD_argind = optind;
197 
198 		switch (opt) {
199 		case '?':	/* Unknown option */
200 			return "Unknown option";
201 			break;
202 		case ':':	/* Missing Arg */
203 			return "Missing argument";
204 			break;
205 		case 'i':	/* Iterations */
206 			options |= OPT_iteration;
207 			STD_LOOP_COUNT = atoi(optarg);
208 			if (STD_LOOP_COUNT == 0)
209 				STD_INFINITE = 1;
210 			break;
211 		case 'I':	/* Time duration */
212 			options |= OPT_duration;
213 			STD_LOOP_DURATION = atof(optarg);
214 			if (STD_LOOP_DURATION == 0.0)
215 				STD_INFINITE = 1;
216 			break;
217 		case 'h':	/* Help */
218 			print_help(uhf);
219 			exit(0);
220 			break;
221 #ifdef UCLINUX
222 		case 'C':	/* Run child */
223 			child_args = optarg;
224 			break;
225 #endif
226 		default:
227 
228 			/* Check all the user specified options */
229 			found = 0;
230 			for (i = 0; user_optarr[i].option; ++i) {
231 
232 				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++;
238 
239 					/* 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 
257 	}
258 	free(optionstr);
259 
260 	STD_argind = optind;
261 
262 	/*
263 	 * Turn on debug
264 	 */
265 	if (getenv("USC_DEBUG") != NULL) {
266 		Debug = 1;
267 		printf("env USC_DEBUG is defined, turning on debug\n");
268 	}
269 	if (getenv("USC_VERBOSE") != NULL) {
270 		Debug = 1;
271 		printf("env USC_VERBOSE is defined, turning on debug\n");
272 	}
273 
274 	/*
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 	}
297 
298 	/*
299 	 * 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 	}
336 
337 	/*
338 	 * 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;
346 		if (Debug)
347 			printf
348 			    ("using env USC_TP_BARRIER, Set STD_TP_barrier to %d\n",
349 			     STD_TP_barrier);
350 	}
351 
352 	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 	}
362 
363 	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 	}
372 
373 	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 	}
382 
383 	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 	}
392 
393 	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 	}
402 #if !defined(UCLINUX)
403 	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 	}
412 #endif /* if !defined(UCLINUX) */
413 
414 	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 	}
426 
427 	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 	}
439 #if UNIT_TEST
440 	printf("The following variables after option and env parsing:\n");
441 	printf("STD_LOOP_DURATION   = %f\n", STD_LOOP_DURATION);
442 	printf("STD_LOOP_COUNT      = %d\n", STD_LOOP_COUNT);
443 	printf("STD_INFINITE        = %d\n", STD_INFINITE);
444 #endif
445 
446 	return NULL;
447 }
448 
449 /***********************************************************************
450  * This function will do desired end of global setup test
451  * hooks.
452  ***********************************************************************/
usc_global_setup_hook(void)453 int usc_global_setup_hook(void)
454 {
455 #ifndef UCLINUX
456 	if (STD_TP_sbrk || STD_LP_sbrk)
457 		STD_start_break = sbrk(0);	/* get original sbreak size */
458 
459 	if (STD_TP_sbrk) {
460 		sbrk(STD_TP_sbrk);
461 		if (Debug)
462 			printf("after sbrk(%d)\n", STD_TP_sbrk);
463 	}
464 #endif
465 	return 0;
466 }
467 
468 #define USECS_PER_SEC	1000000	/* microseconds per second */
469 
470 /***********************************************************************
471  * Returns current time in microseconds since 1970.
472  ***********************************************************************/
get_current_time(void)473 static uint64_t get_current_time(void)
474 {
475 	struct timeval curtime;
476 
477 	gettimeofday(&curtime, NULL);
478 
479 	return (((uint64_t) curtime.tv_sec) * USECS_PER_SEC) + curtime.tv_usec;
480 }
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
491  * have been met.
492  *
493  * counter integer is supplied by the user program.
494  ***********************************************************************/
usc_test_looping(int counter)495 int usc_test_looping(int counter)
496 {
497 	static int first_time = 1;
498 	static uint64_t stop_time = 0;
499 	int keepgoing = 0;
500 
501 	/*
502 	 * 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.
505 	 */
506 	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 		}
518 	}
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) {
537 		if (Debug)
538 			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);
542 	}
543 #if !defined(UCLINUX)
544 	if (STD_LP_sbrk) {
545 		if (Debug)
546 			printf("about to do sbrk(%d)\n", STD_LP_sbrk);
547 		sbrk(STD_LP_sbrk);
548 	}
549 #endif
550 
551 	if (keepgoing)
552 		return 1;
553 	else
554 		return 0;
555 }
556 
557 /*
558  * This function recressively calls itself max times.
559  */
usc_recressive_func(int cnt,int max,struct usc_bigstack_t bstack)560 static void usc_recressive_func(int cnt, int max, struct usc_bigstack_t bstack)
561 {
562 	if (cnt < max)
563 		usc_recressive_func(cnt + 1, max, bstack);
564 
565 }
566 
567 #if UNIT_TEST
568 #include <time.h>
569 
570 /******************************************************************************
571  * UNIT TEST CODE
572  * UNIT TEST CODE
573  *
574  * this following code is provide so that unit testing can
575  * be done fairly easily.
576  ******************************************************************************/
577 
578 int Help = 0;
579 int Help2 = 0;
580 char *ptr;
581 
582 long TEST_RETURN;
583 int TEST_ERRNO;
584 
585 /* for test specific parse_opts options */
586 option_t Options[] = {
587 	{"help", &Help2, NULL},	/* -help option */
588 	{"h", &Help, NULL},	/* -h option */
589 
590 #if INVALID_TEST_CASES
591 	{"missingflag", NULL, &ptr},	/* error */
592 	{"missingarg:", &Help, NULL},	/* error */
593 #endif /* INVALID_TEST_CASES */
594 
595 	{NULL, NULL, NULL}
596 };
597 
main(int argc,char ** argv)598 int main(int argc, char **argv)
599 {
600 	int lc;
601 	char *msg;
602 	struct timeval t;
603 	int cnt;
604 
605 	if ((msg = parse_opts(argc, argv, Options, NULL)) != NULL) {
606 		printf("ERROR: %s\n", msg);
607 		exit(1);
608 	}
609 
610 	TEST_PAUSE;
611 
612 	for (lc = 0; TEST_LOOPING(lc); lc++) {
613 
614 		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 	}
618 
619 	TEST_CLEANUP;
620 
621 	exit(0);
622 }
623 
624 #endif /* UNIT_TEST */
625