Path: blob/main/tools/test/stress2/misc/burnin.sh
106751 views
#!/bin/sh12#3# Copyright (c) 2016 EMC Corp.4# All rights reserved.5#6# Redistribution and use in source and binary forms, with or without7# modification, are permitted provided that the following conditions8# are met:9# 1. Redistributions of source code must retain the above copyright10# notice, this list of conditions and the following disclaimer.11# 2. Redistributions in binary form must reproduce the above copyright12# notice, this list of conditions and the following disclaimer in the13# documentation and/or other materials provided with the distribution.14#15# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND16# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE17# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE18# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF25# SUCH DAMAGE.26#2728# Time creating and deleting a number of files once a minute.2930. ../default.cfg31[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 13233export LANG=C34dir=/tmp35runtime=1200 # default36[ $# -eq 1 ] && runtime=$137odir=`pwd`38cd $dir39sed '1,/^EOF/d' < $odir/$0 > $dir/burnin.c40mycc -o burnin -Wall -Wextra -O0 -g burnin.c || exit 141rm -f burnin.c42cd $odir4344mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint45mdconfig -l | grep -q md$mdstart && mdconfig -d -u $mdstart46mdconfig -a -t swap -s 1g -u $mdstart || exit 147newfs -n md$mdstart > /dev/null48mount /dev/md$mdstart $mntpoint49d=`date '+%Y%m%dT%H%M%S'`50log=/tmp/burnin.$d.log51mode=`pgrep -q cron && echo "Multi-user" || echo "Single-user"`52echo "# `uname -a` $mode mode `hostname`" > $log5354/tmp/burnin -r 10 -d $mntpoint > /dev/null 2>&155/tmp/burnin -r $runtime -d $mntpoint >> $log5657ministat -A -C 2 -w 72 $log | tail -1 | awk '{if ($NF > .1) exit(1)}'58s=$?59[ $s -ne 0 ] && ministat -C 2 -w 72 $log6061while mount | grep "on $mntpoint " | grep -q /dev/md; do62umount $mntpoint || sleep 163done64mdconfig -d -u $mdstart65rm -rf /tmp/burnin $log66exit 067EOF68#include <sys/param.h>69#include <sys/mman.h>70#include <sys/stat.h>71#include <sys/wait.h>7273#include <machine/atomic.h>7475#include <err.h>76#include <errno.h>77#include <fcntl.h>78#include <stdio.h>79#include <stdlib.h>80#include <time.h>81#include <unistd.h>8283#define DELAY 6084#define SYNC 08586volatile u_int *share;87int bufsize, files, parallel, runtime;88char *buf, *dir;8990void91usage(void)92{93fprintf(stderr, "Usage: %s [-b buf size] [-d directory] [-p parallel] "94"[-r runtime]\n",95getprogname());96_exit(1);97}9899void100test(void)101{102pid_t pid;103int fd, i;104char path[MAXPATHLEN + 1];105106atomic_add_int(&share[SYNC], 1);107while (share[SYNC] != (volatile u_int)parallel)108;109110pid =getpid();111for (i = 0; i < files; i++) {112snprintf(path, sizeof(path), "%s/f%06d.%06d", dir, pid, i);113if ((fd = open(path, O_RDWR | O_CREAT | O_TRUNC, DEFFILEMODE)) ==114-1)115err(1, "open(%s)", path);116if (write(fd, buf, bufsize) != bufsize)117err(1, "write()");118if (close(fd) == -1)119err(1, "close(%d)", fd);120if (unlink(path) == -1)121err(1, "unlink(%s)", path);122}123124_exit(0);125}126127int128main(int argc, char *argv[])129{130struct timeval t1, t2, diff;131struct tm *tp;132size_t len;133time_t start, now;134int ch, e, i, *pids, status;135char help[80];136137bufsize = 8 * 1024;138dir = "/tmp";139files = 5000;140parallel = 4;141runtime = 1 * 60 * 60 * 24;142143while ((ch = getopt(argc, argv, "b:d:f:r:")) != -1)144switch(ch) {145case 'b': /* bufsize */146if (sscanf(optarg, "%d", &bufsize) != 1)147usage();148break;149case 'd': /* dir */150dir = optarg;151break;152case 'f': /* files */153if (sscanf(optarg, "%d", &files) != 1)154usage();155break;156case 'p': /* parallel */157if (sscanf(optarg, "%d", ¶llel) != 1)158usage();159break;160case 'r': /* runtime */161if (sscanf(optarg, "%d", &runtime) != 1)162usage();163break;164default:165usage();166}167argc -= optind;168argv += optind;169170printf("# Options used: dir=%s, bufsize=%d, files=%d, parallel=%d, "171"runtime=%d\n",172dir, bufsize, files, parallel, runtime);173if ((buf = malloc(bufsize)) == NULL)174err(1, "malloc(%d)", bufsize);175if ((pids = malloc(sizeof(pid_t) * parallel)) == NULL)176err(1, "malloc(%d)", (int)(sizeof(pid_t) * parallel));177e = 0;178len = PAGE_SIZE;179if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,180MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)181err(1, "mmap");182183start = time(NULL);184while ((time(NULL) - start) < runtime && e == 0) {185share[SYNC] = 0;186gettimeofday(&t1, NULL);187for (i = 0; i < parallel; i++) {188if ((pids[i] = fork()) == 0)189test();190}191for (i = 0; i < parallel; i++) {192waitpid(pids[i], &status, 0);193e += status == 0 ? 0 : 1;194}195gettimeofday(&t2, NULL);196timersub(&t2, &t1, &diff);197now = time(NULL);198tp = localtime(&now);199strftime(help, sizeof(help), "%Y%m%d%H%M%S", tp);200printf("%s %ld.%06ld\n", help, (long)diff.tv_sec,201diff.tv_usec);202fflush(stdout);203if (runtime > DELAY)204sleep(DELAY);205}206207return (e);208}209210211