xref: /freebsd/tools/test/stress2/misc/pageout.sh (revision 2b8331622f0b212cf3bb4fc4914a501e5321d506)
1#!/bin/sh
2
3#
4# Copyright (c) 2015 EMC Corp.
5# All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26# SUCH DAMAGE.
27#
28
29# Trigger the two EDEADLK in vm/vm_pageout.c
30# OOVM deadlock seen
31# https://people.freebsd.org/~pho/stress/log/pageout.txt
32
33# "panic: handle_written_filepage: not started" seen:
34# https://people.freebsd.org/~pho/stress/log/pageout-2.txt
35
36# "panic: ffs_geom_strategy: bad I/O" seen:
37# https://people.freebsd.org/~pho/stress/log/log0434.txt
38
39[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
40. ../default.cfg
41
42dir=/tmp
43odir=`pwd`
44cd $dir
45sed '1,/^EOF/d' < $odir/$0 > $dir/pageout.c
46mycc -o pageout -Wall -Wextra -g pageout.c || exit 1
47rm -f pageout.c
48cd $odir
49
50mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
51mdconfig -l | grep -q md$mdstart &&  mdconfig -d -u $mdstart
52
53mdconfig -a -t swap -s 2g -u $mdstart || exit 1
54
55[ "$newfs_flags" = "-U" ] && [ `jot -r 1 0 1` -eq 1 ] && newfs_flags="-j"
56[ $# -eq 1 ] && newfs_flags="$1" # or use script argument
57newfs $newfs_flags md$mdstart > /dev/null
58
59mount /dev/md$mdstart $mntpoint
60chmod 777 $mntpoint
61
62f1=$mntpoint/f1
63dd if=/dev/zero of=$f1 bs=1m count=1k status=none
64
65daemon sh -c "(cd ../testcases/swap; ./swap -t 5m -i 20 -l 100 -h)" > /dev/null
66(cd /tmp; /tmp/pageout $f1) &
67sleep .2
68while kill -0 $! 2> /dev/null; do
69	rm -f $mntpoint/.snap/stress2
70	mksnap_ffs $mntpoint $mntpoint/.snap/stress2
71done
72while pgrep -q swap; do
73	pkill swap
74done
75wait
76
77while mount | grep $mntpoint | grep -q /dev/md; do
78	umount $mntpoint || sleep 1
79done
80mdconfig -d -u $mdstart
81rm -f /tmp/pageout /tmp/pageout.core
82exit
83
84EOF
85#include <sys/param.h>
86#include <sys/fcntl.h>
87#include <sys/mman.h>
88#include <sys/stat.h>
89
90#include <err.h>
91#include <errno.h>
92#include <stdio.h>
93#include <stdlib.h>
94#include <time.h>
95#include <unistd.h>
96
97const char *file;
98
99#define RUNTIME 600
100
101void
102test(void)
103{
104	struct stat st;
105	size_t i, len;
106	time_t start;
107	int error, fd, ps;
108	char *p;
109
110	ps = getpagesize();
111	if ((fd = open(file, O_RDWR)) == -1)
112		err(1, "open(%s)", file);
113	if ((error = fstat(fd, &st)) == -1)
114		err(1, "stat(%s)", file);
115	len = round_page(st.st_size);
116	do {
117		if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED,
118		    fd, 0)) == MAP_FAILED) {
119			if (errno == ENOMEM)
120				len -= ps;
121			else
122				err(1, "mmap");
123		}
124	} while (p == MAP_FAILED);
125
126	start = time(NULL);
127	/* Touch all pages of the file. */
128	for (i = 0; i < len; i += ps)
129		p[i] = 1;
130	while (time(NULL) - start < RUNTIME)
131		p[arc4random() % len] = 1;
132
133	if (munmap(p, len) == -1)
134		err(1, "unmap()");
135	close(fd);
136}
137
138int
139main(int argc, char *argv[])
140{
141	if (argc != 2)
142		errx(1, "Usage: %s <file>", argv[0]);
143	file = argv[1];
144
145	test();
146
147	return (0);
148}
149