xref: /freebsd/tools/test/stress2/misc/pageout.sh (revision 089104e0e01f080c9cd45dc5f34c4f636dea4ca7)
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# Fixed by: 7aeea73e3078
39
40[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
41. ../default.cfg
42
43dir=/tmp
44odir=`pwd`
45cd $dir
46sed '1,/^EOF/d' < $odir/$0 > $dir/pageout.c
47mycc -o pageout -Wall -Wextra -g pageout.c || exit 1
48rm -f pageout.c
49cd $odir
50
51mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
52mdconfig -l | grep -q md$mdstart &&  mdconfig -d -u $mdstart
53
54mdconfig -a -t swap -s 2g -u $mdstart || exit 1
55
56[ "$newfs_flags" = "-U" ] && [ `jot -r 1 0 1` -eq 1 ] && newfs_flags="-j"
57[ $# -eq 1 ] && newfs_flags="$1" # or use script argument
58newfs $newfs_flags md$mdstart > /dev/null
59
60mount /dev/md$mdstart $mntpoint
61chmod 777 $mntpoint
62
63f1=$mntpoint/f1
64dd if=/dev/zero of=$f1 bs=1m count=1k status=none
65
66daemon sh -c "(cd ../testcases/swap; ./swap -t 5m -i 20 -l 100 -h)" > /dev/null
67(cd /tmp; /tmp/pageout $f1) &
68sleep .2
69while kill -0 $! 2> /dev/null; do
70	rm -f $mntpoint/.snap/stress2
71	mksnap_ffs $mntpoint $mntpoint/.snap/stress2
72done
73while pgrep -q swap; do
74	pkill swap
75done
76wait
77
78while mount | grep $mntpoint | grep -q /dev/md; do
79	umount $mntpoint || sleep 1
80done
81mdconfig -d -u $mdstart
82rm -f /tmp/pageout /tmp/pageout.core
83exit
84
85EOF
86#include <sys/param.h>
87#include <sys/fcntl.h>
88#include <sys/mman.h>
89#include <sys/stat.h>
90
91#include <err.h>
92#include <errno.h>
93#include <stdio.h>
94#include <stdlib.h>
95#include <time.h>
96#include <unistd.h>
97
98const char *file;
99
100#define RUNTIME 600
101
102void
103test(void)
104{
105	struct stat st;
106	size_t i, len;
107	time_t start;
108	int error, fd, ps;
109	char *p;
110
111	ps = getpagesize();
112	if ((fd = open(file, O_RDWR)) == -1)
113		err(1, "open(%s)", file);
114	if ((error = fstat(fd, &st)) == -1)
115		err(1, "stat(%s)", file);
116	len = round_page(st.st_size);
117	do {
118		if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED,
119		    fd, 0)) == MAP_FAILED) {
120			if (errno == ENOMEM)
121				len -= ps;
122			else
123				err(1, "mmap");
124		}
125	} while (p == MAP_FAILED);
126
127	start = time(NULL);
128	/* Touch all pages of the file. */
129	for (i = 0; i < len; i += ps)
130		p[i] = 1;
131	while (time(NULL) - start < RUNTIME)
132		p[arc4random() % len] = 1;
133
134	if (munmap(p, len) == -1)
135		err(1, "unmap()");
136	close(fd);
137}
138
139int
140main(int argc, char *argv[])
141{
142	if (argc != 2)
143		errx(1, "Usage: %s <file>", argv[0]);
144	file = argv[1];
145
146	test();
147
148	return (0);
149}
150