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