xref: /freebsd/tools/test/stress2/misc/pageout.sh (revision 9c9dfbf2569811fa8232411fd0c95e07ea089e98)
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# Trigger the two EDEADLK in vm/vm_pageout.c
308a272653SPeter Holm# OOVM deadlock seen
318a272653SPeter Holm# https://people.freebsd.org/~pho/stress/log/pageout.txt
328a272653SPeter Holm
338a272653SPeter Holm# "panic: handle_written_filepage: not started" seen:
348a272653SPeter Holm# https://people.freebsd.org/~pho/stress/log/pageout-2.txt
358a272653SPeter Holm
360ece3355SPeter Holm# "panic: ffs_geom_strategy: bad I/O" seen:
370ece3355SPeter Holm# https://people.freebsd.org/~pho/stress/log/log0434.txt
38*9c9dfbf2SPeter Holm# Fixed by: 7aeea73e3078
390ece3355SPeter Holm
408a272653SPeter Holm[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
418a272653SPeter Holm. ../default.cfg
428a272653SPeter Holm
438a272653SPeter Holmdir=/tmp
448a272653SPeter Holmodir=`pwd`
458a272653SPeter Holmcd $dir
468a272653SPeter Holmsed '1,/^EOF/d' < $odir/$0 > $dir/pageout.c
478a272653SPeter Holmmycc -o pageout -Wall -Wextra -g pageout.c || exit 1
488a272653SPeter Holmrm -f pageout.c
498a272653SPeter Holmcd $odir
508a272653SPeter Holm
518a272653SPeter Holmmount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
528a272653SPeter Holmmdconfig -l | grep -q md$mdstart &&  mdconfig -d -u $mdstart
538a272653SPeter Holm
548a272653SPeter Holmmdconfig -a -t swap -s 2g -u $mdstart || exit 1
558a272653SPeter Holm
560ece3355SPeter Holm[ "$newfs_flags" = "-U" ] && [ `jot -r 1 0 1` -eq 1 ] && newfs_flags="-j"
570ece3355SPeter Holm[ $# -eq 1 ] && newfs_flags="$1" # or use script argument
58608c97bfSPeter Holmnewfs $newfs_flags md$mdstart > /dev/null
598a272653SPeter Holm
60608c97bfSPeter Holmmount /dev/md$mdstart $mntpoint
618a272653SPeter Holmchmod 777 $mntpoint
628a272653SPeter Holm
638a272653SPeter Holmf1=$mntpoint/f1
648a272653SPeter Holmdd if=/dev/zero of=$f1 bs=1m count=1k status=none
658a272653SPeter Holm
668a272653SPeter Holmdaemon sh -c "(cd ../testcases/swap; ./swap -t 5m -i 20 -l 100 -h)" > /dev/null
678a272653SPeter Holm(cd /tmp; /tmp/pageout $f1) &
688a272653SPeter Holmsleep .2
698a272653SPeter Holmwhile kill -0 $! 2> /dev/null; do
708a272653SPeter Holm	rm -f $mntpoint/.snap/stress2
710ece3355SPeter Holm	mksnap_ffs $mntpoint $mntpoint/.snap/stress2
728a272653SPeter Holmdone
738a272653SPeter Holmwhile pgrep -q swap; do
748a272653SPeter Holm	pkill swap
758a272653SPeter Holmdone
768a272653SPeter Holmwait
778a272653SPeter Holm
788a272653SPeter Holmwhile mount | grep $mntpoint | grep -q /dev/md; do
798a272653SPeter Holm	umount $mntpoint || sleep 1
808a272653SPeter Holmdone
818a272653SPeter Holmmdconfig -d -u $mdstart
828a272653SPeter Holmrm -f /tmp/pageout /tmp/pageout.core
838a272653SPeter Holmexit
848a272653SPeter Holm
858a272653SPeter HolmEOF
868a272653SPeter Holm#include <sys/param.h>
878a272653SPeter Holm#include <sys/fcntl.h>
888a272653SPeter Holm#include <sys/mman.h>
898a272653SPeter Holm#include <sys/stat.h>
908a272653SPeter Holm
918a272653SPeter Holm#include <err.h>
928a272653SPeter Holm#include <errno.h>
938a272653SPeter Holm#include <stdio.h>
948a272653SPeter Holm#include <stdlib.h>
958a272653SPeter Holm#include <time.h>
968a272653SPeter Holm#include <unistd.h>
978a272653SPeter Holm
988a272653SPeter Holmconst char *file;
998a272653SPeter Holm
1008a272653SPeter Holm#define RUNTIME 600
1018a272653SPeter Holm
1028a272653SPeter Holmvoid
1038a272653SPeter Holmtest(void)
1048a272653SPeter Holm{
1058a272653SPeter Holm	struct stat st;
1068a272653SPeter Holm	size_t i, len;
1078a272653SPeter Holm	time_t start;
1088a272653SPeter Holm	int error, fd, ps;
1098a272653SPeter Holm	char *p;
1108a272653SPeter Holm
1118a272653SPeter Holm	ps = getpagesize();
1128a272653SPeter Holm	if ((fd = open(file, O_RDWR)) == -1)
1138a272653SPeter Holm		err(1, "open(%s)", file);
1148a272653SPeter Holm	if ((error = fstat(fd, &st)) == -1)
1158a272653SPeter Holm		err(1, "stat(%s)", file);
1168a272653SPeter Holm	len = round_page(st.st_size);
1178a272653SPeter Holm	do {
1188a272653SPeter Holm		if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED,
1198a272653SPeter Holm		    fd, 0)) == MAP_FAILED) {
1208a272653SPeter Holm			if (errno == ENOMEM)
1218a272653SPeter Holm				len -= ps;
1228a272653SPeter Holm			else
1238a272653SPeter Holm				err(1, "mmap");
1248a272653SPeter Holm		}
1258a272653SPeter Holm	} while (p == MAP_FAILED);
1268a272653SPeter Holm
1278a272653SPeter Holm	start = time(NULL);
1288a272653SPeter Holm	/* Touch all pages of the file. */
1298a272653SPeter Holm	for (i = 0; i < len; i += ps)
1308a272653SPeter Holm		p[i] = 1;
1318a272653SPeter Holm	while (time(NULL) - start < RUNTIME)
1328a272653SPeter Holm		p[arc4random() % len] = 1;
1338a272653SPeter Holm
1348a272653SPeter Holm	if (munmap(p, len) == -1)
1358a272653SPeter Holm		err(1, "unmap()");
1368a272653SPeter Holm	close(fd);
1378a272653SPeter Holm}
1388a272653SPeter Holm
1398a272653SPeter Holmint
1408a272653SPeter Holmmain(int argc, char *argv[])
1418a272653SPeter Holm{
1428a272653SPeter Holm	if (argc != 2)
1438a272653SPeter Holm		errx(1, "Usage: %s <file>", argv[0]);
1448a272653SPeter Holm	file = argv[1];
1458a272653SPeter Holm
1468a272653SPeter Holm	test();
1478a272653SPeter Holm
1488a272653SPeter Holm	return (0);
1498a272653SPeter Holm}
150