📄 parse_opts.c
字号:
*/ if ( (ptr=getenv("USC_TP_BARRIER")) != NULL ) { if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) { STD_TP_barrier=k; } else STD_TP_barrier=1; if ( Debug ) printf("using env USC_TP_BARRIER, Set STD_TP_barrier to %d\n", STD_TP_barrier); } if ( (ptr=getenv("USC_LP_BARRIER")) != NULL ) { if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) { STD_LP_barrier=k; } else STD_LP_barrier=1; if ( Debug ) printf("using env USC_LP_BARRIER, Set STD_LP_barrier to %d\n", STD_LP_barrier); } if ( (ptr=getenv("USC_TP_SHMEM")) != NULL ) { if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) { STD_TP_shmem_sz=k; if ( Debug ) printf("Using env USC_TP_SHMEM, Set STD_TP_shmem_sz to %d\n", STD_TP_shmem_sz); } } if ( (ptr=getenv("USC_LP_SHMEM")) != NULL ) { if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) { STD_LP_shmem=k; if ( Debug ) printf("Using env USC_LP_SHMEM, Set STD_LP_shmem to %d\n", STD_LP_shmem); } } if ( (ptr=getenv("USC_LD_SHMEM")) != NULL ) { if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) { STD_LD_shmem=k; if ( Debug ) printf("Using env USC_LD_SHMEM, Set STD_LD_shmem to %d\n", STD_LD_shmem); } } if ( (ptr=getenv("USC_TP_SBRK")) != NULL ) { if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) { STD_TP_sbrk=k; if ( Debug ) printf("Using env USC_TP_SBRK, Set STD_TP_sbrk to %d\n", STD_TP_sbrk); } } if ( (ptr=getenv("USC_LP_SBRK")) != NULL ) { if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) { STD_LP_sbrk=k; if ( Debug ) printf("Using env USC_LP_SBRK, Set STD_LP_sbrk to %d\n", STD_LP_sbrk); } } if ( (ptr=getenv("USC_LP_RECFUN")) != NULL ) { if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) { STD_LP_recfun=k; if ( STD_bigstack != (struct usc_bigstack_t *)NULL ) STD_bigstack=(struct usc_bigstack_t *) malloc(sizeof(struct usc_bigstack_t)); if ( Debug ) printf("Using env USC_LP_RECFUN, Set STD_LP_recfun to %d\n", STD_LP_recfun); } } if ( (ptr=getenv("USC_LD_RECFUN")) != NULL ) { if ( sscanf(ptr, "%i", &k) == 1 && k >= 0 ) { STD_LD_recfun=k; if ( STD_bigstack != (struct usc_bigstack_t *)NULL ) STD_bigstack=(struct usc_bigstack_t *) malloc(sizeof(struct usc_bigstack_t)); if ( Debug ) printf("Using env USC_LD_RECFUN, Set STD_LD_recfun to %d\n", STD_LD_recfun); } } #if UNIT_TEST printf("The following variables after option and env parsing:\n"); printf("STD_FUNCTIONAL_TEST = %d\n", STD_FUNCTIONAL_TEST); printf("STD_LOOP_DURATION = %f\n", STD_LOOP_DURATION); printf("STD_LOOP_DELAY = %f\n", STD_LOOP_DELAY); printf("STD_COPIES = %d\n", STD_COPIES); printf("STD_LOOP_COUNT = %d\n", STD_LOOP_COUNT); printf("STD_INFINITE = %d\n", STD_INFINITE); printf("STD_TIMING_ON = %d\n", STD_TIMING_ON); printf("STD_ERRNO_LOG = %d\n", STD_ERRNO_LOG); printf("STD_PAUSE = %d\n", STD_PAUSE);#endif return((char *) NULL);} /* end of parse_opts *//********************************************************************* * print_help() - print help message and user help message *********************************************************************/void print_help(void (*user_help)()){ STD_opts_help(); if (user_help) user_help();}/********************************************************************* * STD_opts_help() - return a help string for the STD_OPTIONS. *********************************************************************/voidSTD_opts_help(){ int i; for(i = 0; std_options[i].optstr; ++i) { if (std_options[i].help) printf(std_options[i].help); }}/* * routine to goto when we get the SIGUSR1 for STD_PAUSE */void STD_go(int sig){ return;}/*********************************************************************** * This function will do desired end of global setup test * hooks. * Currently it will only do a pause waiting for sigusr1 if * STD_PAUSE is set. * ***********************************************************************/intusc_global_setup_hook(){ int cnt; /* temp variable to store old signal action to be restored after pause */ int (*_TMP_FUNC)(void); /* * Fork STD_COPIES-1 copies. */ for(cnt=1;cnt<STD_COPIES;cnt++) { switch(fork() ) { case -1: fprintf(stderr, "%s: fork() failed, errno:%d %s\n", __FILE__, errno, strerror(errno)); break; case 0: /* child */ cnt=STD_COPIES; /* to stop the forking */ break; default: /* parent */ break; } } /* * pause waiting for sigusr1. */ if ( STD_PAUSE ) { _TMP_FUNC = (int (*)())signal(SIGUSR1, STD_go); pause(); signal(SIGUSR1, (void (*)())_TMP_FUNC); } if ( STD_TP_sbrk || STD_LP_sbrk) { STD_start_break=sbrk(0); /* get original sbreak size */ } if ( STD_TP_sbrk ) { sbrk(STD_TP_sbrk); if ( Debug ) printf("after sbrk(%d)\n", STD_TP_sbrk); } return 0;}#define USECS_PER_SEC 1000000 /* microseconds per second *//*********************************************************************** * This function returns the number of get_current_time()'s return * per second. ***********************************************************************/static intget_timepersec(){ return USECS_PER_SEC; /* microseconds per second */}/*********************************************************************** * this function will get current time in microseconds since 1970. ***********************************************************************/static intget_current_time(){ struct timeval curtime; gettimeofday(&curtime, NULL); /* microseconds since 1970 */ return (curtime.tv_sec*USECS_PER_SEC) + curtime.tv_usec;}/*********************************************************************** * * This function will determine if test should continue iterating * If the STD_INFINITE flag is set, return 1. * If the STD_LOOP_COUNT variable is set, compare it against * the counter. * If the STD_LOOP_DURATION variable is set, compare current time against * calculated stop_time. * This function will return 1 until all desired looping methods * have been met. * * counter integer is supplied by the user program. ***********************************************************************/intusc_test_looping(counter)int counter;{ static int first_time = 1; static int stop_time = 0; /* stop time in rtc or usecs */ static int delay; /* delay in clocks or usecs */ int hz=0; /* clocks per second or usecs per second */ int ct, end; /* current time, end delay time */ int keepgoing=0; /* used to determine return value */ /* * If this is the first iteration and we are looping for * duration of STD_LOOP_DURATION seconds (fractional) or * doing loop delays, get the clocks per second. */ if ( first_time ) { first_time=0; if ( STD_LOOP_DELAY || STD_LOOP_DURATION ) { hz = get_timepersec(); } /* * If looping for duration, calculate stop time in * clocks. */ if ( STD_LOOP_DURATION) { ct=get_current_time(); stop_time=(int)((float)hz * STD_LOOP_DURATION) + ct; } /* * If doing delay each iteration, calcuate the number * of clocks for each delay. */ if ( STD_LOOP_DELAY ) { delay=(int)((float)hz * STD_LOOP_DELAY); } } /* * if delay each iteration, loop for delay clocks. * This will not be done on first iteration. * The delay will happen before determining if * there will be another iteration. */ else if ( STD_LOOP_DELAY ) { ct=get_current_time(); end=ct+delay; while ( ct < end ) { /* * The following are special test hooks in the delay loop. */ if ( STD_LD_recfun ) { if ( Debug ) printf("calling usc_recressive_func(0, %d, &STD_bigstack)\n", STD_LD_recfun); usc_recressive_func(0, STD_LD_recfun, &STD_bigstack); } ct=get_current_time(); } } if ( STD_INFINITE ) { keepgoing++; } if ( STD_LOOP_COUNT && counter < STD_LOOP_COUNT ) { keepgoing++; } if ( STD_LOOP_DURATION != 0.0 && get_current_time() < stop_time ) { keepgoing++; } if ( keepgoing == 0 ) return 0; /* * The following code allows special system testing hooks. */ if ( STD_LP_recfun ) { if ( Debug ) printf("calling usc_recressive_func(0, %d, &STD_bigstack)\n", STD_LP_recfun); usc_recressive_func(0, STD_LP_recfun, &STD_bigstack); } if ( STD_LP_sbrk ) { if ( Debug ) printf("about to do sbrk(%d)\n", STD_LP_sbrk); sbrk(STD_LP_sbrk); } if ( keepgoing ) return 1; else return 0; /* done - stop iterating */}/* * This function recressively calls itself max times. */ static voidusc_recressive_func(cnt, max, bstack)int cnt;int max;struct usc_bigstack_t bstack;{ if ( cnt < max ) usc_recressive_func(cnt+1, max, bstack);}#if UNIT_TEST/****************************************************************************** * UNIT TEST CODE * UNIT TEST CODE * * this following code is provide so that unit testing can * be done fairly easily. ******************************************************************************/int Help = 0;int Help2 = 0;char *ptr;/* * Code from usctest.h that not part of this file since we are the library. */struct usc_errno_t TEST_VALID_ENO[USC_MAX_ERRNO]; /*********************************************************************** * Globals for returning the return code and errno from the system call * test macros. ***********************************************************************/int TEST_RETURN;int TEST_ERRNO; /*********************************************************************** * temporary variables for determining max and min times in TEST macro ***********************************************************************/long btime, etime, tmptime;/* for test specific parse_opts options */option_t Options[] = { { "help", &Help2, NULL }, /* -help option */ { "h", &Help, NULL }, /* -h option */ { TIMING, NULL, NULL}, /* disable -timing option */#if INVALID_TEST_CASES { "missingflag", NULL, &ptr }, /* error */ { "missingarg:", &Help, NULL }, /* error */#endif /* INVALID_TEST_CASES */ { NULL, NULL, NULL }};main(argc, argv)int argc;char **argv;{ int lc; char *msg; struct timeval t; int cnt; if ( (msg=parse_opts(argc, argv, (option_t *) Options)) != (char *) NULL ) { printf("ERROR : %s\n", msg); exit(1); } TEST_PAUSE; for (lc=0; TEST_LOOPING(lc); lc++) { TEST( gettimeofday(&t, NULL) ); printf("iter=%d: sec:%d, usec:%6.6d %s", lc+1, t.tv_sec, t.tv_usec, ctime(&t.tv_sec)); } TEST_CLEANUP; exit(0);}#endif /* UNIT_TEST */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -