xref: /freebsd/tools/test/stress2/misc/burnin.sh (revision 608c97bff921c8936eb71f4b1a177625cfa60250)
18a272653SPeter Holm#!/bin/sh
28a272653SPeter Holm
38a272653SPeter Holm#
48a272653SPeter Holm# Copyright (c) 2016 EMC Corp.
58a272653SPeter Holm# All rights reserved.
68a272653SPeter Holm#
78a272653SPeter Holm# Redistribution and use in source and binary forms, with or without
88a272653SPeter Holm# modification, are permitted provided that the following conditions
98a272653SPeter Holm# are met:
108a272653SPeter Holm# 1. Redistributions of source code must retain the above copyright
118a272653SPeter Holm#    notice, this list of conditions and the following disclaimer.
128a272653SPeter Holm# 2. Redistributions in binary form must reproduce the above copyright
138a272653SPeter Holm#    notice, this list of conditions and the following disclaimer in the
148a272653SPeter Holm#    documentation and/or other materials provided with the distribution.
158a272653SPeter Holm#
168a272653SPeter Holm# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
178a272653SPeter Holm# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
188a272653SPeter Holm# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
198a272653SPeter Holm# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
208a272653SPeter Holm# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
218a272653SPeter Holm# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
228a272653SPeter Holm# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
238a272653SPeter Holm# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
248a272653SPeter Holm# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
258a272653SPeter Holm# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
268a272653SPeter Holm# SUCH DAMAGE.
278a272653SPeter Holm#
288a272653SPeter Holm
298a272653SPeter Holm# Time creating and deleting a number of files once a minute.
308a272653SPeter Holm
318a272653SPeter Holm. ../default.cfg
328a272653SPeter Holm[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
338a272653SPeter Holm
348a272653SPeter Holmexport LANG=C
358a272653SPeter Holmdir=/tmp
368a272653SPeter Holmruntime=1200	# default
378a272653SPeter Holm[ $# -eq 1 ] && runtime=$1
388a272653SPeter Holmodir=`pwd`
398a272653SPeter Holmcd $dir
408a272653SPeter Holmsed '1,/^EOF/d' < $odir/$0 > $dir/burnin.c
418a272653SPeter Holmmycc -o burnin -Wall -Wextra -O0 -g burnin.c || exit 1
428a272653SPeter Holmrm -f burnin.c
438a272653SPeter Holmcd $odir
448a272653SPeter Holm
458a272653SPeter Holmmount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
468a272653SPeter Holmmdconfig -l | grep -q md$mdstart &&  mdconfig -d -u $mdstart
478a272653SPeter Holmmdconfig -a -t swap -s 1g -u $mdstart || exit 1
48*608c97bfSPeter Holmnewfs -n md$mdstart > /dev/null
49*608c97bfSPeter Holmmount /dev/md$mdstart $mntpoint
508a272653SPeter Holmd=`date '+%Y%m%dT%H%M%S'`
518a272653SPeter Holmlog=/tmp/burnin.$d.log
528a272653SPeter Holmmode=`pgrep -q cron && echo "Multi-user" || echo "Single-user"`
538a272653SPeter Holmecho "# `uname -a` $mode mode `hostname`" > $log
548a272653SPeter Holm
558a272653SPeter Holm/tmp/burnin -r 10 -d $mntpoint > /dev/null 2>&1
568a272653SPeter Holm/tmp/burnin -r $runtime -d $mntpoint >> $log
578a272653SPeter Holm
588a272653SPeter Holmministat -A -C 2 -w 72 $log | tail -1 | awk '{if ($NF > .1) exit(1)}'
598a272653SPeter Holms=$?
608a272653SPeter Holm[ $s -ne 0 ] && ministat -C 2 -w 72 $log
618a272653SPeter Holm
628a272653SPeter Holmwhile mount | grep "on $mntpoint " | grep -q /dev/md; do
638a272653SPeter Holm	umount $mntpoint || sleep 1
648a272653SPeter Holmdone
658a272653SPeter Holmmdconfig -d -u $mdstart
668a272653SPeter Holmrm -rf /tmp/burnin $log
678a272653SPeter Holmexit 0
688a272653SPeter HolmEOF
698a272653SPeter Holm#include <sys/param.h>
708a272653SPeter Holm#include <sys/mman.h>
718a272653SPeter Holm#include <sys/stat.h>
728a272653SPeter Holm#include <sys/wait.h>
738a272653SPeter Holm
748a272653SPeter Holm#include <machine/atomic.h>
758a272653SPeter Holm
768a272653SPeter Holm#include <err.h>
778a272653SPeter Holm#include <errno.h>
788a272653SPeter Holm#include <fcntl.h>
798a272653SPeter Holm#include <stdio.h>
808a272653SPeter Holm#include <stdlib.h>
818a272653SPeter Holm#include <time.h>
828a272653SPeter Holm#include <unistd.h>
838a272653SPeter Holm
848a272653SPeter Holm#define DELAY 60
858a272653SPeter Holm#define SYNC 0
868a272653SPeter Holm
878a272653SPeter Holmvolatile u_int *share;
888a272653SPeter Holmint bufsize, files, parallel, runtime;
898a272653SPeter Holmchar *buf, *dir;
908a272653SPeter Holm
918a272653SPeter Holmvoid
928a272653SPeter Holmusage(void)
938a272653SPeter Holm{
948a272653SPeter Holm	fprintf(stderr, "Usage: %s [-b buf size] [-d directory] [-p parallel] "
958a272653SPeter Holm	    "[-r runtime]\n",
968a272653SPeter Holm	    getprogname());
978a272653SPeter Holm	_exit(1);
988a272653SPeter Holm}
998a272653SPeter Holm
1008a272653SPeter Holmvoid
1018a272653SPeter Holmtest(void)
1028a272653SPeter Holm{
1038a272653SPeter Holm	pid_t pid;
1048a272653SPeter Holm	int fd, i;
1058a272653SPeter Holm	char path[MAXPATHLEN + 1];
1068a272653SPeter Holm
1078a272653SPeter Holm	atomic_add_int(&share[SYNC], 1);
1088a272653SPeter Holm	while (share[SYNC] != (volatile u_int)parallel)
1098a272653SPeter Holm		;
1108a272653SPeter Holm
1118a272653SPeter Holm	pid =getpid();
1128a272653SPeter Holm	for (i = 0; i < files; i++) {
1138a272653SPeter Holm		snprintf(path, sizeof(path), "%s/f%06d.%06d", dir, pid, i);
1148a272653SPeter Holm		if ((fd = open(path, O_RDWR | O_CREAT | O_TRUNC, DEFFILEMODE)) ==
1158a272653SPeter Holm		    -1)
1168a272653SPeter Holm			err(1, "open(%s)", path);
1178a272653SPeter Holm		if (write(fd, buf, bufsize) != bufsize)
1188a272653SPeter Holm			err(1, "write()");
1198a272653SPeter Holm		if (close(fd) == -1)
1208a272653SPeter Holm			err(1, "close(%d)", fd);
1218a272653SPeter Holm		if (unlink(path) == -1)
1228a272653SPeter Holm			err(1, "unlink(%s)", path);
1238a272653SPeter Holm	}
1248a272653SPeter Holm
1258a272653SPeter Holm	_exit(0);
1268a272653SPeter Holm}
1278a272653SPeter Holm
1288a272653SPeter Holmint
1298a272653SPeter Holmmain(int argc, char *argv[])
1308a272653SPeter Holm{
1318a272653SPeter Holm	struct timeval t1, t2, diff;
1328a272653SPeter Holm	struct tm *tp;
1338a272653SPeter Holm	size_t len;
1348a272653SPeter Holm	time_t start, now;
1358a272653SPeter Holm	int ch, e, i, *pids, status;
1368a272653SPeter Holm	char help[80];
1378a272653SPeter Holm
1388a272653SPeter Holm	bufsize = 8 * 1024;
1398a272653SPeter Holm	dir = "/tmp";
1408a272653SPeter Holm	files = 5000;
1418a272653SPeter Holm	parallel = 4;
1428a272653SPeter Holm	runtime = 1 * 60 * 60 * 24;
1438a272653SPeter Holm
1448a272653SPeter Holm	while ((ch = getopt(argc, argv, "b:d:f:r:")) != -1)
1458a272653SPeter Holm		switch(ch) {
1468a272653SPeter Holm		case 'b':	/* bufsize */
1478a272653SPeter Holm			if (sscanf(optarg, "%d", &bufsize) != 1)
1488a272653SPeter Holm				usage();
1498a272653SPeter Holm			break;
1508a272653SPeter Holm		case 'd':	/* dir */
1518a272653SPeter Holm			dir = optarg;
1528a272653SPeter Holm			break;
1538a272653SPeter Holm		case 'f':	/* files */
1548a272653SPeter Holm			if (sscanf(optarg, "%d", &files) != 1)
1558a272653SPeter Holm				usage();
1568a272653SPeter Holm			break;
1578a272653SPeter Holm		case 'p':	/* parallel */
1588a272653SPeter Holm			if (sscanf(optarg, "%d", &parallel) != 1)
1598a272653SPeter Holm				usage();
1608a272653SPeter Holm			break;
1618a272653SPeter Holm		case 'r':	/* runtime */
1628a272653SPeter Holm			if (sscanf(optarg, "%d", &runtime) != 1)
1638a272653SPeter Holm				usage();
1648a272653SPeter Holm			break;
1658a272653SPeter Holm		default:
1668a272653SPeter Holm			usage();
1678a272653SPeter Holm		}
1688a272653SPeter Holm	argc -= optind;
1698a272653SPeter Holm	argv += optind;
1708a272653SPeter Holm
1718a272653SPeter Holm	printf("# Options used: dir=%s, bufsize=%d, files=%d, parallel=%d, "
1728a272653SPeter Holm	    "runtime=%d\n",
1738a272653SPeter Holm	    dir, bufsize, files, parallel, runtime);
1748a272653SPeter Holm	if ((buf = malloc(bufsize)) == NULL)
1758a272653SPeter Holm		err(1, "malloc(%d)", bufsize);
1768a272653SPeter Holm	if ((pids = malloc(sizeof(pid_t) * parallel)) == NULL)
1778a272653SPeter Holm		err(1, "malloc(%d)", (int)(sizeof(pid_t) * parallel));
1788a272653SPeter Holm	e = 0;
1798a272653SPeter Holm	len = PAGE_SIZE;
1808a272653SPeter Holm	if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
1818a272653SPeter Holm	    MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
1828a272653SPeter Holm		err(1, "mmap");
1838a272653SPeter Holm
1848a272653SPeter Holm	start = time(NULL);
1858a272653SPeter Holm	while ((time(NULL) - start) < runtime && e == 0) {
1868a272653SPeter Holm		share[SYNC] = 0;
1878a272653SPeter Holm		gettimeofday(&t1, NULL);
1888a272653SPeter Holm		for (i = 0; i < parallel; i++) {
1898a272653SPeter Holm			if ((pids[i] = fork()) == 0)
1908a272653SPeter Holm				test();
1918a272653SPeter Holm		}
1928a272653SPeter Holm		for (i = 0; i < parallel; i++) {
1938a272653SPeter Holm			waitpid(pids[i], &status, 0);
1948a272653SPeter Holm			e += status == 0 ? 0 : 1;
1958a272653SPeter Holm		}
1968a272653SPeter Holm		gettimeofday(&t2, NULL);
1978a272653SPeter Holm		timersub(&t2, &t1, &diff);
1988a272653SPeter Holm		now = time(NULL);
1998a272653SPeter Holm		tp = localtime(&now);
2008a272653SPeter Holm		strftime(help, sizeof(help), "%Y%m%d%H%M%S", tp);
2018a272653SPeter Holm		printf("%s %ld.%06ld\n", help, (long)diff.tv_sec,
2028a272653SPeter Holm		    diff.tv_usec);
2038a272653SPeter Holm		fflush(stdout);
2048a272653SPeter Holm		if (runtime > DELAY)
2058a272653SPeter Holm			sleep(DELAY);
2068a272653SPeter Holm	}
2078a272653SPeter Holm
2088a272653SPeter Holm	return (e);
2098a272653SPeter Holm}
210