Index: projects/stress2/include/stress.h =================================================================== --- projects/stress2/include/stress.h (revision 208414) +++ projects/stress2/include/stress.h (revision 208415) @@ -1,65 +1,66 @@ /*- * Copyright (c) 2008 Peter Holm * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * $FreeBSD$ */ #ifndef _STRESS_H_ #define _STRESS_H_ extern int setup(int); extern int test(void); extern void cleanup(void); extern void options(int, char **); extern int random_int(int, int); /*extern void limits(void);*/ typedef struct { int argc; char **argv; int run_time; int load; char *wd; char *cd; int verbose; int incarnations; int hog; int nodelay; int kill; int kblocks; int inodes; } opt_t; extern opt_t *op; extern volatile int done_testing; extern char *home; extern void rmval(void); extern void putval(unsigned long); extern unsigned long getval(void); extern void getdf(int64_t *, int64_t *); extern void reservedf(int64_t, int64_t); +extern void show_status(void); extern int64_t swap(void); extern unsigned long usermem(void); #endif Index: projects/stress2/lib/main.c =================================================================== --- projects/stress2/lib/main.c (revision 208414) +++ projects/stress2/lib/main.c (revision 208415) @@ -1,205 +1,207 @@ /*- * Copyright (c) 2008 Peter Holm * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * */ /* Main program for all test programs */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include "stress.h" static char const rcsid[] = "$Name: $ $FreeBSD$"; volatile int done_testing; static int cleanupcalled = 0; char *home; static pid_t *r; void handler(int i) { int j; done_testing = 1; for (j = 0; j < op->incarnations; j++) { if (op->verbose > 2) printf("handler: kill -HUP %d\n", r[j]); if (r[j] != 0 && kill(r[j], SIGHUP) == -1) if (errno != ESRCH) warn("kill(%d, SIGHUP), %s:%d", r[j], __FILE__, __LINE__); } if (op->kill == 1) { sleep(5); /* test programs may have blocked for the SIGHUP, so try harder */ for (j = 0; j < op->incarnations; j++) { if (op->verbose > 2) printf("handler: kill -KILL %d\n", r[j]); if (r[j] != 0) (void) kill(r[j], SIGKILL); } } } void run_test_handler(int i) { done_testing = 1; } void exit_handler(int i) { exit(1); } void callcleanup(void) { if (cleanupcalled == 0) cleanup(); cleanupcalled = 1; } static void run_tests(int i) { time_t start; signal(SIGHUP, run_test_handler); signal(SIGINT, exit_handler); atexit(callcleanup); setup(i); if ((strcmp(getprogname(), "run") != 0) && (op->nodelay == 0)) sleep(random_int(1,10)); start = time(NULL); while (done_testing == 0 && (time(NULL) - start) < op->run_time) { test(); } callcleanup(); exit(EXIT_SUCCESS); } static void run_incarnations(void) { int i; int s; signal(SIGHUP, handler); for (i = 0; i < op->incarnations && done_testing == 0; i++) { if ((r[i] = fork()) == 0) { run_tests(i); } if (r[i] < 0) { warn("fork(), %s:%d", __FILE__, __LINE__); r[i] = 0; break; } } for (i = 0; i < op->incarnations; i++) if (r[i] != 0 && waitpid(r[i], &s, 0) == -1) warn("waitpid(%d), %s:%d", r[i], __FILE__, __LINE__); exit(EXIT_SUCCESS); } static int run_test(void) { pid_t p; time_t start; int status = 0; if (random_int(1,100) > op->load) return (status); + show_status(); + start = time(NULL); done_testing = 0; fflush(stdout); rmval(); p = fork(); if (p == 0) run_incarnations(); if (p < 0) err(1, "fork() in %s:%d", __FILE__, __LINE__); while (done_testing != 1 && (time(NULL) - start) < op->run_time) sleep(1); if (kill(p, SIGHUP) == -1) warn("kill(%d, SIGHUP), %s:%d", p, __FILE__, __LINE__); if (waitpid(p, &status, 0) == -1) err(1, "waitpid(%d), %s:%d", p, __FILE__, __LINE__); return (status); } int main(int argc, char **argv) { struct stat sb; int status = 0; options(argc, argv); umask(0); if (stat(op->wd, &sb) == -1) { if (mkdir(op->wd, 0770) == -1) if (errno != EEXIST) err(1, "mkdir(%s) %s:%d", op->wd, __FILE__, __LINE__); } if (stat(op->cd, &sb) == -1) { if (mkdir(op->cd, 0770) == -1) if (errno != EEXIST) err(1, "mkdir(%s) %s:%d", op->cd, __FILE__, __LINE__); } if ((home = getcwd(NULL, 0)) == NULL) err(1, "getcwd(), %s:%d", __FILE__, __LINE__); if (chdir(op->wd) == -1) err(1, "chdir(%s) %s:%d", op->wd, __FILE__, __LINE__); r = (pid_t *)calloc(1, op->incarnations * sizeof(pid_t)); status = run_test(); return (status); } Index: projects/stress2/lib/options.c =================================================================== --- projects/stress2/lib/options.c (revision 208414) +++ projects/stress2/lib/options.c (revision 208415) @@ -1,261 +1,265 @@ /*- * Copyright (c) 2008 Peter Holm * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include "stress.h" opt_t opt; opt_t *op; static char path[64]; static void usage(char *where) { char *help; if (where != NULL) printf("Error in \"%s\"\n", where); fprintf(stderr, "Usage: %s [-t | -l | -i | -d | -h | -k | -v]\n", getprogname()); help = " t : time to run test\n" " l : load factor 0 - 100%\n" " i : max # of parallel incarnations\n" " d : working directory\n" " h : hog resources\n" " k : terminate with SIGHUP + SIGKILL\n" " n : no startup delay\n" " v : verbose\n"; printf(help); exit(EX_USAGE); } static int time2sec(const char *string) { int r, s = 0; char modifier; r = sscanf(string, "%d%c", &s, &modifier); if (r == 2) switch(modifier) { case 's': break; case 'm': s = s * 60; break; case 'h': s = s * 60 * 60; break; case 'd': s = s * 60 * 60 * 24; break; default: usage("-t"); } else usage("-t"); return (s); } static char *gete(char *name) { char *cp; char help[128]; snprintf(help, sizeof(help), "%s%s", getprogname(), name); cp = getenv(help); if (cp == NULL) cp = getenv(name); return (cp); } static void environment(void) { char *cp; if ((cp = gete("INCARNATIONS")) != NULL) { if (sscanf(cp, "%d", &op->incarnations) != 1) usage("INCARNATIONS"); } if ((cp = gete("LOAD")) != NULL) { if (sscanf(cp, "%d", &op->load) != 1) usage("LOAD"); } if ((cp = gete("RUNTIME")) != NULL) { op->run_time = time2sec(cp); } if ((cp = gete("RUNDIR")) != NULL) { op->wd = cp; } if ((cp = gete("CTRLDIR")) != NULL) { op->cd = cp; } if ((cp = gete("HOG")) != NULL) { op->hog = 1; } if ((cp = gete("KILL")) != NULL) { op->kill = 1; } if ((cp = gete("NODELAY")) != NULL) { op->nodelay = 1; } if ((cp = gete("VERBOSE")) != NULL) { if (sscanf(cp, "%d", &op->verbose) != 1) usage("VERBOSE"); } if ((cp = gete("KBLOCKS")) != NULL) { if (sscanf(cp, "%d", &op->kblocks) != 1) usage("KBLOCKS"); } if ((cp = gete("INODES")) != NULL) { if (sscanf(cp, "%d", &op->inodes) != 1) usage("INODES"); } } void options(int argc, char **argv) { int ch; - int days; - time_t t; - char buf[80]; op = &opt; op->run_time = 60; op->load = 100; op->wd = "/tmp/stressX"; op->cd = "/tmp/stressX.control"; op->incarnations = 1; op->hog = 0; op->kill = 0; op->nodelay = 0; op->verbose = 0; op->kblocks = 0; op->inodes = 0; environment(); while ((ch = getopt(argc, argv, "t:l:i:d:hknv")) != -1) switch(ch) { case 't': /* run time */ op->run_time = time2sec(optarg); break; case 'l': /* load factor in pct */ if (sscanf(optarg, "%d", &op->load) != 1) usage("-l"); break; case 'i': /* max incarnations */ if (sscanf(optarg, "%d", &op->incarnations) != 1) usage("-i"); break; case 'd': /* working directory */ op->wd = strdup(optarg); break; case 'h': /* hog flag */ op->hog += 1; break; case 'k': /* kill flag */ op->kill = 1; break; case 'n': /* no delay flag */ op->nodelay = 1; break; case 'v': /* verbose flag */ op->verbose += 1; break; default: usage(NULL); } op->argc = argc -= optind; op->argv = argv += optind; if (op->incarnations < 1) op->incarnations = 1; if (op->hog == 0) op->incarnations = random_int(1, op->incarnations); if (op->run_time < 15) op->run_time = 15; if (op->load < 0 || op->load > 100) op->load = 100; +} +void +show_status(void) +{ + char buf[80]; + int days; + time_t t; + if (op->verbose > 0) { t = op->run_time; days = t / (60 * 60 * 24); t = t % (60 * 60 * 24); strftime(buf, sizeof(buf), "%T", gmtime(&t)); printf("%8s: run time %2d+%s, incarnations %3d, load %3d, verbose %d\n", getprogname(), days, buf, op->incarnations, op->load, op->verbose); } } - void rmval(void) { if (snprintf(path, sizeof(path), "%s/%s.conf", op->cd, getprogname()) < 0) err(1, "snprintf path"); (void) unlink(path); } void putval(unsigned long v) { char buf[64]; rmval(); snprintf(buf, sizeof(buf), "%lu", v); if (symlink(buf, path) < 0) err(1, "symlink(%s, %s)", path, buf); } unsigned long getval(void) { int i, n; unsigned long val; char buf[64]; if ((n = readlink(path, buf, sizeof(buf) -1)) < 0) { for (i = 0; i < 60; i++) { sleep(1); if ((n = readlink(path, buf, sizeof(buf) -1)) > 0) break; } if (n < 0) err(1, "readlink(%s). %s:%d", path, __FILE__, __LINE__); } buf[n] = '\0'; if (sscanf(buf, "%ld", &val) != 1) err(1, "sscanf(%s)", buf); return val; }