xref: /freebsd/tools/test/stress2/misc/bio.sh (revision 608c97bff921c8936eb71f4b1a177625cfa60250)
18a272653SPeter Holm#!/bin/sh
28a272653SPeter Holm
38a272653SPeter Holm#
48a272653SPeter Holm# Copyright (c) 2015 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# mmap() & read()/write() on same file.
308a272653SPeter Holm# Test scenario suggestion by: jeff@
318a272653SPeter Holm
328a272653SPeter Holm# Out of VM deadlock seen:
338a272653SPeter Holm# https://people.freebsd.org/~pho/stress/log/jeff114.txt
348a272653SPeter Holm
358a272653SPeter Holm# panic: deadlkres: possible deadlock detected:
368a272653SPeter Holm# https://people.freebsd.org/~pho/stress/log/jeff115.txt
378a272653SPeter Holm
388a272653SPeter Holm. ../default.cfg
398a272653SPeter Holm[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
408a272653SPeter Holm
418a272653SPeter Holmdir=/tmp
428a272653SPeter Holmodir=`pwd`
438a272653SPeter Holmcd $dir
448a272653SPeter Holmps=`sysctl -n hw.pagesize`
458a272653SPeter Holmsed "1,/^EOF/d;s/\$ps/$ps/" < $odir/$0 > $dir/bio.c
468a272653SPeter Holmmycc -o bio -Wall -Wextra -O0 -g bio.c || exit 1
478a272653SPeter Holmrm -f bio.c
488a272653SPeter Holmcd $odir
498a272653SPeter Holm
508a272653SPeter Holmmount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
518a272653SPeter Holm[ -c /dev/md$mdstart ] &&  mdconfig -d -u $mdstart
528a272653SPeter Holmmdconfig -a -t swap -s 2g -u $mdstart || exit 1
53*608c97bfSPeter Holmnewfs -n md$mdstart > /dev/null
54*608c97bfSPeter Holmmount /dev/md$mdstart $mntpoint
558a272653SPeter Holm
568a272653SPeter Holm(cd $mntpoint; /tmp/bio) &
578a272653SPeter Holmpid1=$!
588a272653SPeter Holmsleep 5
598a272653SPeter Holm(cd ../testcases/swap; ./swap -t 5m -i 20 -k -l 100 -h) &
608a272653SPeter Holmpid2=$!
618a272653SPeter Holm
628a272653SPeter Holmwhile pgrep -q bio; do
638a272653SPeter Holm	sleep 2
648a272653SPeter Holmdone
658a272653SPeter Holm
668a272653SPeter Holmwhile pgrep -q swap; do
678a272653SPeter Holm	pkill -9 swap
688a272653SPeter Holmdone
698a272653SPeter Holmwait $pid2
708a272653SPeter Holmwait $pid1
718a272653SPeter Holms=$?
728a272653SPeter Holm
738a272653SPeter Holmwhile mount | grep "on $mntpoint " | grep -q /dev/md; do
748a272653SPeter Holm	umount $mntpoint || sleep 1
758a272653SPeter Holmdone
768a272653SPeter Holmmdconfig -d -u $mdstart
778a272653SPeter Holmrm -rf /tmp/bio
788a272653SPeter Holmexit $s
798a272653SPeter Holm
808a272653SPeter HolmEOF
818a272653SPeter Holm#include <sys/param.h>
828a272653SPeter Holm#include <sys/mman.h>
838a272653SPeter Holm#include <sys/wait.h>
848a272653SPeter Holm
858a272653SPeter Holm#include <machine/atomic.h>
868a272653SPeter Holm
878a272653SPeter Holm#include <err.h>
888a272653SPeter Holm#include <errno.h>
898a272653SPeter Holm#include <fcntl.h>
908a272653SPeter Holm#include <sched.h>
918a272653SPeter Holm#include <stdio.h>
928a272653SPeter Holm#include <stdlib.h>
938a272653SPeter Holm#include <time.h>
948a272653SPeter Holm#include <unistd.h>
958a272653SPeter Holm
968a272653SPeter Holm#define PS $ps	/* From hw.pagesize */
978a272653SPeter Holm#define SYN1 0
988a272653SPeter Holm#define SYN2 1
998a272653SPeter Holm#define INDX 2
1008a272653SPeter Holm
1018a272653SPeter Holm#define PAGES (512 * 1024 * 1024 / PS)	/* 512MB file size */
1028a272653SPeter Holm#define PARALLEL 3
1038a272653SPeter Holm#define RUNTIME (5 * 60)
1048a272653SPeter Holm#define TIMEOUT (30 * 60)
1058a272653SPeter Holm
1068a272653SPeter Holmchar buf[PS];
1078a272653SPeter Holm
1088a272653SPeter Holmvoid
1098a272653SPeter Holmtest(int inx)
1108a272653SPeter Holm{
1118a272653SPeter Holm	pid_t pid;
1128a272653SPeter Holm	size_t i, len, slen;
1138a272653SPeter Holm	time_t start;
1148a272653SPeter Holm	volatile u_int *share;
1158a272653SPeter Holm	int fd, r;
1168a272653SPeter Holm	u_int *ip, val;
1178a272653SPeter Holm	char file[80];
1188a272653SPeter Holm
1198a272653SPeter Holm	slen = PS;
1208a272653SPeter Holm	if ((share = mmap(NULL, slen, PROT_READ | PROT_WRITE, MAP_ANON |
1218a272653SPeter Holm	    MAP_SHARED, -1, 0)) == MAP_FAILED)
1228a272653SPeter Holm		err(1, "mmap");
1238a272653SPeter Holm
1248a272653SPeter Holm	snprintf(file, sizeof(file), "file.%06d", inx);
1258a272653SPeter Holm	if ((fd = open(file, O_RDWR | O_CREAT | O_TRUNC, 0640)) < 0)
1268a272653SPeter Holm		err(1, "%s", file);
1278a272653SPeter Holm
1288a272653SPeter Holm	for (i = 0; i < PAGES; i++) {
1298a272653SPeter Holm		if (write(fd, buf, sizeof(buf)) != sizeof(buf))
1308a272653SPeter Holm			err(1, "write error");
1318a272653SPeter Holm	}
1328a272653SPeter Holm
1338a272653SPeter Holm	len = PS * PAGES * sizeof(u_int);
1348a272653SPeter Holm	if ((ip = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED,
1358a272653SPeter Holm	    fd, 0)) == MAP_FAILED)
1368a272653SPeter Holm			err(1, "mmap");
1378a272653SPeter Holm
1388a272653SPeter Holm	start = time(NULL);
1398a272653SPeter Holm	if ((pid = fork()) == 0) {
1408a272653SPeter Holm		alarm(2 * RUNTIME);
1418a272653SPeter Holm		/* mmap read / write access */
1428a272653SPeter Holm		for (i = 0; i < (size_t)(PAGES * PS); i += PS) {
1438a272653SPeter Holm			while (share[SYN1] == 0)
1448a272653SPeter Holm				sched_yield();
1458a272653SPeter Holm			atomic_add_int(&share[SYN1], -1);
1468a272653SPeter Holm			if (ip[share[INDX] / sizeof(u_int)] != share[INDX])
1478a272653SPeter Holm				warn("child expected %d, but got %d\n",
1488a272653SPeter Holm				    ip[share[INDX] / sizeof(u_int)],
1498a272653SPeter Holm				    share[INDX]);
1508a272653SPeter Holm			share[INDX] += PS;
1518a272653SPeter Holm			ip[share[INDX] / sizeof(u_int)] = share[INDX];
1528a272653SPeter Holm			atomic_add_int(&share[SYN2], 1); /* signal parent */
1538a272653SPeter Holm			if (i % 1000 == 0 && time(NULL) - start > TIMEOUT)
1548a272653SPeter Holm				errx(1, "Timed out");;
1558a272653SPeter Holm		}
1568a272653SPeter Holm		_exit(0);
1578a272653SPeter Holm	}
1588a272653SPeter Holm	if (pid == -1)
1598a272653SPeter Holm		err(1, "fork()");
1608a272653SPeter Holm	share[INDX] = 0;
1618a272653SPeter Holm	atomic_add_int(&share[SYN2], 1);
1628a272653SPeter Holm	alarm(2 * RUNTIME);
1638a272653SPeter Holm	for (i = 0; i < (size_t)(PAGES * PS); i += PS) {
1648a272653SPeter Holm		while (share[SYN2] == 0)
1658a272653SPeter Holm			sched_yield();
1668a272653SPeter Holm		atomic_add_int(&share[SYN2], -1);
1678a272653SPeter Holm		if (lseek(fd, share[INDX], SEEK_SET) == -1)
1688a272653SPeter Holm			err(1, "lseek error");
1698a272653SPeter Holm		if ((r = read(fd, &val, sizeof(val))) != sizeof(val))
1708a272653SPeter Holm			err(1, "parent read read %d bytes", r);
1718a272653SPeter Holm		if (val != share[INDX])
1728a272653SPeter Holm			warn("parent expected %d, but got %d\n",
1738a272653SPeter Holm			    share[INDX], val);
1748a272653SPeter Holm		val += PS;
1758a272653SPeter Holm		if (lseek(fd, val, SEEK_SET) == -1)
1768a272653SPeter Holm			err(1, "lseek error");
1778a272653SPeter Holm		if (write(fd, &val, sizeof(val)) != sizeof(val))
1788a272653SPeter Holm			err(1, "write");
1798a272653SPeter Holm
1808a272653SPeter Holm		atomic_add_int(&share[SYN1], 1); /* signal child */
1818a272653SPeter Holm		if (i % 1000 == 0 && time(NULL) - start > TIMEOUT)
1828a272653SPeter Holm			errx(1, "Timed out");;
1838a272653SPeter Holm	}
1848a272653SPeter Holm	atomic_add_int(&share[SYN2], -1);
1858a272653SPeter Holm	if (waitpid(pid, NULL, 0) != pid)
1868a272653SPeter Holm		err(1, "wait");
1878a272653SPeter Holm
1888a272653SPeter Holm	if (munmap(ip, len) == -1)
1898a272653SPeter Holm		err(1, "unmap()");
1908a272653SPeter Holm	if (munmap((void *)share, slen) == -1)
1918a272653SPeter Holm		err(1, "unmap()");
1928a272653SPeter Holm	close(fd);
1938a272653SPeter Holm	if (unlink(file) == -1)
1948a272653SPeter Holm		err(1, "unlink(%s)", file);
1958a272653SPeter Holm
1968a272653SPeter Holm	_exit(0);
1978a272653SPeter Holm}
1988a272653SPeter Holm
1998a272653SPeter Holmint
2008a272653SPeter Holmmain(void)
2018a272653SPeter Holm{
2028a272653SPeter Holm	pid_t pids[PARALLEL];
2038a272653SPeter Holm	time_t start;
2048a272653SPeter Holm	int i, s, status;
2058a272653SPeter Holm
2068a272653SPeter Holm	start = time(NULL);
2078a272653SPeter Holm	s = 0;
2088a272653SPeter Holm	while ((time(NULL) - start) < RUNTIME && s == 0) {
2098a272653SPeter Holm		for (i = 0; i < PARALLEL; i++) {
2108a272653SPeter Holm			if ((pids[i] = fork()) == 0)
2118a272653SPeter Holm				test(i);
2128a272653SPeter Holm		}
2138a272653SPeter Holm		for (i = 0; i < PARALLEL; i++) {
2148a272653SPeter Holm			if (waitpid(pids[i], &status, 0) == -1)
2158a272653SPeter Holm				err(1, "waitpid(%d)", pids[i]);
2168a272653SPeter Holm			if (status != 0)
2178a272653SPeter Holm				fprintf(stderr, "Child exit status = %d\n",
2188a272653SPeter Holm					status);
2198a272653SPeter Holm			s += status == 0 ? 0 : 1;
2208a272653SPeter Holm		}
2218a272653SPeter Holm	}
2228a272653SPeter Holm
2238a272653SPeter Holm	return (s);
2248a272653SPeter Holm}
225