void checkopt(int argc, char **argv) { char c = '\0'; char *endptr = NULL; long nr_cpus = 0; long opt_value = 0; if (argc == 1) return; nr_cpus = sysconf(_SC_NPROCESSORS_ONLN); if (nr_cpus <= 0) { fprintf(stderr, "Error: sysconf failed\n"); report_result("2\n"); exit(1); } while ((c = getopt(argc, argv, "p:h")) != -1) { switch (c) { case 'p': if (optarg[0] == '-' && !isdigit(optarg[1])) OPT_MISSING(argv[0], c); else { opt_value = strtol(optarg, &endptr, DECIMAL); if (errno || (endptr != NULL && *endptr != '\0') || opt_value <= 0 || opt_value > MAX_NPROCS) ARG_WRONG(argv[0], c, optarg); nprocs = atoi(optarg); } break; case 'h': /* usage message */ usage(argv[0], 0); break; default: usage(argv[0], 1); break; } } if (nprocs == 0) nprocs = 2 * nr_cpus; }
int main(int argc, /* number of input parameters */ char **argv) { /* pointer to the command line arguments. */ int c; /* command line options */ int num_thrd = MAXT; /* number of threads to create */ int num_reps = MAXR; /* number of repatitions the test is run */ int thrd_ndx; /* index into the array of thread ids */ void *th_status; /* exit status of LWP's */ int map_size; /* size of the file mapped. */ int shmkey = 1969; /* key used to generate shmid by shmget() */ pthread_t thrdid[30]; /* maxinum of 30 threads allowed */ long chld_args[4]; /* arguments to the thread function */ char *map_address = NULL; /* address in memory of the mapped file */ extern int optopt; /* options to the program */ while ((c = getopt(argc, argv, "hl:t:")) != -1) { switch (c) { case 'h': usage(argv[0]); break; case 'l': /* how many repetitions of the test to exec */ if ((num_reps = atoi(optarg)) == 0) OPT_MISSING(argv[0], optopt); else if (num_reps < 0) { fprintf(stdout, "WARNING: bad argument. Using default\n"); num_reps = MAXR; } break; case 't': if ((num_thrd = atoi(optarg)) == 0) OPT_MISSING(argv[0], optopt); else if (num_thrd < 0) { fprintf(stdout, "WARNING: bad argument. Using default\n"); num_thrd = MAXT; } break; default: usage(argv[0]); break; } } chld_args[0] = num_reps; for (thrd_ndx = 0; thrd_ndx < num_thrd; thrd_ndx += 2) { srand(time(NULL) % 100); map_size = (1 + (int)(1000.0 * rand() / (RAND_MAX + 1.0))) * 4096; chld_args[1] = shmkey++; chld_args[2] = map_size; dprt("main(): thrd_ndx = %d map_address = %#x map_size = %d\n", thrd_ndx, map_address, map_size); chld_args[3] = WRITER; if (pthread_create (&thrdid[thrd_ndx], NULL, shmat_rd_wr, chld_args)) { perror("shmat_rd_wr(): pthread_create()"); exit(-1); } chld_args[3] = READER; if (pthread_create (&thrdid[thrd_ndx + 1], NULL, shmat_rd_wr, chld_args)) { perror("shmat_rd_wr(): pthread_create()"); exit(-1); } } sync(); for (thrd_ndx = 0; thrd_ndx < num_thrd; thrd_ndx++) { if (pthread_join(thrdid[thrd_ndx], &th_status) != 0) { perror("shmat_rd_wr(): pthread_join()"); exit(-1); } else { dprt("WE ARE HERE %d\n", __LINE__); if (th_status == (void *)-1) { fprintf(stderr, "thread [%ld] - process exited with errors\n", (long)thrdid[thrd_ndx]); exit(-1); } } } exit(0); }
int main(int argc, /* number of command line parameters */ char **argv) /* pointer to the array of the command line parameters. */ { extern char *optarg; /* getopt() function global variables */ extern int optind; /* index into argument */ extern int opterr; /* optarg error detection */ extern int optopt; /* stores bad option passed to the program */ static char *version_info = "mmstress V1.00 04/17/2001"; /* version of this program */ int (*(test_ptr)[])() = {NULL, test1, test2, test3, test4, test5, test6}; /* pointer to the array of test names */ int ch; /* command line flag character */ int test_num = 0; /* test number that is to be run */ int test_time = 0; /* duration for which test is to be run */ int sig_ndx = 0; /* index into signal handler structure. */ int run_once = TRUE; /* test/tests are run once by default. */ char *prog_name = argv[0]; /* name of this program */ int rc = 0; /* return value from tests. 0 - success */ int global_rc = 0; /* return value from tests. 0 - success */ int version_flag = FALSE; /* printf the program version */ struct sigaction sigptr; /* set up signal, for interval timer */ static struct signal_info { int signum; /* signal number that hasto be handled */ char *signame; /* name of the signal to be handled. */ } sig_info[] = { {SIGHUP,"SIGHUP"}, {SIGINT,"SIGINT"}, {SIGQUIT,"SIGQUIT"}, {SIGABRT,"SIGABRT"}, {SIGBUS,"SIGBUS"}, {SIGSEGV,"SIGSEGV"}, {SIGALRM, "SIGALRM"}, {SIGUSR1,"SIGUSR1"}, {SIGUSR2,"SIGUSR2"}, {SIGENDSIG,"ENDSIG"} }; setvbuf(stdout, NULL, _IONBF, 0); setvbuf(stderr, NULL, _IONBF, 0); optarg = NULL; opterr = 0; if (argc < 2) tst_resm(TINFO, "run %s -h for all options", argv[0]); while ((ch = getopt(argc, argv, "hn:p:t:vV")) != -1) { switch(ch) { case 'h': usage(argv[0]); break; case 'n': if (optarg) test_num = atoi(optarg); else OPT_MISSING(prog_name, optopt); break; case 'p': if (optarg) pages_num = atoi(optarg); else OPT_MISSING(prog_name, optopt); break; case 't': if (optarg) { tst_resm(TINFO, "Test is scheduled to run for %d hours", test_time = atoi(optarg)); run_once = FALSE; } else OPT_MISSING(prog_name, optopt); break; case 'v': verbose_print = TRUE; break; case 'V': if (!version_flag) { version_flag = TRUE; tst_resm(TINFO, "%s: %s", prog_name, version_info); } break; case '?': if (argc < optind) OPT_MISSING(prog_name, optopt); else fprintf(stderr, "%s: unknown option - %c ignored\n", prog_name, optopt); break; default: fprintf(stderr, "%s: getopt() failed!!!\n", prog_name); exit(2); } } /* duration for which the tests have to be run. test_time is converted to */ /* corresponding seconds and the tests are run as long as the current time*/ /* is less than the required time and test are successul (ie rc = 0) */ set_timer(test_time); /* set up signals */ sigptr.sa_handler = (void (*)(int signal))sig_handler; sigfillset(&sigptr.sa_mask); sigptr.sa_flags = 0; for (sig_ndx = 0; sig_info[sig_ndx].signum != -1; sig_ndx++) { sigaddset(&sigptr.sa_mask, sig_info[sig_ndx].signum); if (sigaction(sig_info[sig_ndx].signum, &sigptr, (struct sigaction *)NULL) == SIGENDSIG) { perror( "main(): sigaction()" ); fprintf(stderr, "could not set handler for SIGALRM, errno = %d\n", errno); exit(EXIT_FAILURE); } } /*************************************************/ /* The way this loop was, 5 of 6 tests could fail, * and as long test test 6 passed, the test passed. * The changes in this loop repair that problem. */ do { if (!test_num) { int test_ndx; /* index into the array of tests */ for (test_ndx = 1; test_ndx <= MAXTEST; test_ndx++) { rc = test_ptr[test_ndx](); if (rc == SUCCESS) { tst_resm(TPASS, "TEST %d Passed", test_ndx); } else { tst_resm(TFAIL, "TEST %d Failed", test_ndx); global_rc = rc; } } } else { global_rc = (test_num > MAXTEST) ? fprintf(stderr, "Invalid test number, must be in range [1 - %d]\n", MAXTEST): test_ptr[test_num](); } if (global_rc != SUCCESS) { tst_resm(TFAIL, "Test Failed"); exit(global_rc); } } while ((TRUE) && (!run_once)); if (global_rc != SUCCESS) { tst_resm(TFAIL, "Test Failed"); } else { tst_resm(TPASS, "Test Passed"); } exit(global_rc); }
int main(int argc, char **argv) { int c, i; int file_size; int num_iter; double exec_time; int fd; void *status; pthread_t thid[2]; long chld_args[3]; extern char *optarg; struct sigaction sigptr; int ret; /* set up the default values */ file_size = 1024; num_iter = 1000; exec_time = 24; while ((c = getopt(argc, argv, "hvl:s:x:")) != -1) { switch (c) { case 'h': usage(argv[0]); break; case 'l': if ((num_iter = atoi(optarg)) == 0) OPT_MISSING(argv[0], optopt); else if (num_iter < 0) printf ("WARNING: bad argument. Using default %d\n", (num_iter = 1000)); break; case 's': if ((file_size = atoi(optarg)) == 0) OPT_MISSING(argv[0], optopt); else if (file_size < 0) printf ("WARNING: bad argument. Using default %d\n", (file_size = 1024)); break; case 'v': verbose_print = 1; break; case 'x': exec_time = atof(optarg); if (exec_time == 0) OPT_MISSING(argv[0], optopt); else if (exec_time < 0) printf ("WARNING: bad argument. Using default %.0f\n", (exec_time = 24)); break; default: usage(argv[0]); break; } } /* We don't want other mmap calls to map into same area as is * used for test (mmap_address). The test expects read to return * test pattern or read must fail with SIGSEGV. Find an area * that we can use, which is unlikely to be chosen for other * mmap calls. */ distant_area = mmap(0, DISTANT_MMAP_SIZE, PROT_WRITE | PROT_READ, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); if (distant_area == (void *)-1) tst_brkm(TBROK | TERRNO, NULL, "distant_area: mmap()"); SAFE_MUNMAP(NULL, distant_area, (size_t)DISTANT_MMAP_SIZE); distant_area += DISTANT_MMAP_SIZE / 2; if (verbose_print) tst_resm(TINFO, "Input parameters are: File size: %d; " "Scheduled to run: %lf hours; " "Number of mmap/write/read: %d", file_size, exec_time, num_iter); alarm(exec_time * 3600); /* Do not mask SIGSEGV, as we are interested in handling it. */ sigptr.sa_sigaction = sig_handler; sigfillset(&sigptr.sa_mask); sigdelset(&sigptr.sa_mask, SIGSEGV); sigptr.sa_flags = SA_SIGINFO | SA_NODEFER; for (i = 0; sig_info[i].signum != -1; i++) { if (sigaction(sig_info[i].signum, &sigptr, NULL) == -1) { perror("man(): sigaction()"); fprintf(stderr, "could not set handler for %s, errno = %d\n", sig_info[i].signame, errno); exit(-1); } } tst_tmpdir(); for (;;) { if ((fd = mkfile(file_size)) == -1) tst_brkm(TBROK, NULL, "main(): mkfile(): Failed to create temp file"); if (verbose_print) tst_resm(TINFO, "Tmp file created"); chld_args[0] = fd; chld_args[1] = file_size; chld_args[2] = num_iter; if ((ret = pthread_create(&thid[0], NULL, map_write_unmap, chld_args))) tst_brkm(TBROK, NULL, "main(): pthread_create(): %s", strerror(ret)); tst_resm(TINFO, "created writing thread[%lu]", thid[0]); if ((ret = pthread_create(&thid[1], NULL, read_mem, chld_args))) tst_brkm(TBROK, NULL, "main(): pthread_create(): %s", strerror(ret)); tst_resm(TINFO, "created reading thread[%lu]", thid[1]); for (i = 0; i < 2; i++) { if ((ret = pthread_join(thid[i], &status))) tst_brkm(TBROK, NULL, "main(): pthread_join(): %s", strerror(ret)); if (status) tst_brkm(TFAIL, NULL, "thread [%lu] - process exited " "with %ld", thid[i], (long)status); } close(fd); } tst_rmdir(); exit(0); }