ATF_TC_BODY(setrlimit_nthr, tc) { struct rlimit res; lwpid_t lwpid; ucontext_t c; /* * Set RLIMIT_NTHR to zero and try to create a thread. */ res.rlim_cur = 0; res.rlim_max = 0; ATF_REQUIRE(setrlimit(RLIMIT_NTHR, &res) == 0); ATF_REQUIRE(getcontext(&c) == 0); c.uc_link = NULL; sigemptyset(&c.uc_sigmask); c.uc_stack.ss_flags = 0; c.uc_stack.ss_size = 4096; ATF_REQUIRE((c.uc_stack.ss_sp = malloc(c.uc_stack.ss_size)) != NULL); makecontext(&c, func, 1, &lwpid); ATF_CHECK_ERRNO(EAGAIN, _lwp_create(&c, 0, &lwpid) == -1); }
int main(int argc, char *argv[]) { int i, ch, count; struct timeval before, after; unsigned long elapsed; int use_lwps = 0; memset(elapsed_times, 0, ITERATIONS); while ((ch = getopt(argc, argv, "hl")) != -1) { switch (ch) { case 'l': #if defined(LWP) use_lwps = 1; #else errx(1, "not supported"); #endif break; case 'h': usage(); } } argc -= optind; argv += optind; sleep(1); if (pipe(fd0) != 0) errx(1, "Unable to create pipe"); if (pipe(fd1) != 0) errx(1, "Unable to create pipe"); /* * Determine overhead */ for (count=0; count<2; count++) { gettimeofday(&before, NULL); for (i=0; i<2*(PASSES/2); i++) { ch = 0; write(fd0[1], &ch, 1); read(fd0[0], &ch, 1); } gettimeofday(&after, NULL); overhead = 1000000 * (after.tv_sec - before.tv_sec); overhead += after.tv_usec - before.tv_usec; } if (use_lwps) { #if defined(LWP) ucontext_t u; ucontext_t *contextp; int stacksize = 65536; void *stackbase; lwpid_t l; int error; getcontext(&u); contextp = (ucontext_t *)malloc(sizeof(ucontext_t)); stackbase = malloc(stacksize); sigprocmask(SIG_SETMASK, NULL, &contextp->uc_sigmask); _lwp_makecontext(contextp, child, NULL, NULL, stackbase, stacksize); error = _lwp_create(contextp, 0, &l); if (error) errx(1, "error _lwp_create"); #endif } else { switch (childpid = fork()) { case 0: /* child */ child(); case -1: /* error */ errx(1, "error forking"); break; } } ch = 0; if (read(fd1[0], &ch, 1) != 1) errx(1, "parent read failed"); for (count=0; count<ITERATIONS; count++) { gettimeofday(&before, NULL); for (i=0; i<PASSES/2; i++) { ch = 0; if (write(fd0[1], &ch, 1) != 1) errx(1, "parent write failed"); if (read(fd1[0], &ch, 1) != 1) errx(1, "parent read failed"); } gettimeofday(&after, NULL); elapsed = 1000000 * (after.tv_sec - before.tv_sec); elapsed += after.tv_usec - before.tv_usec; elapsed_times[count] = elapsed; } if (!use_lwps) kill(childpid, SIGTERM); dump_results(); return (0); }