xref: /freebsd/tools/test/stress2/misc/mmap6.sh (revision 59c8e88e72633afbc47a4ace0d2170d00d51f7dc)
1#!/bin/sh
2
3#
4# Copyright (c) 2013 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# Test scenario inspired by alc@
30# "panic: vm_page_dirty: page is invalid!" seen.
31
32. ../default.cfg
33
34dir=/tmp
35odir=`pwd`
36cd $dir
37sed '1,/^EOF/d' < $odir/$0 > $dir/wire_no_page.c
38mycc -o mmap6  -Wall -Wextra wire_no_page.c || exit 1
39rm -f wire_no_page.c
40cd $odir
41
42cp /tmp/mmap6  /tmp/mmap6.inputfile
43(cd ../testcases/swap; ./swap -t 5m -i 2) &
44cp /tmp/mmap6 /tmp/mmap6.inputfile
45/tmp/mmap6  /tmp/mmap6.inputfile
46while killall -9 swap; do
47	sleep .1
48done > /dev/null 2>&1
49wait
50rm -f /tmp/mmap6  /tmp/mmap6.inputfile
51exit 0
52
53EOF
54#include <sys/param.h>
55#include <sys/fcntl.h>
56#include <sys/mman.h>
57#include <sys/stat.h>
58#include <sys/wait.h>
59
60#include <err.h>
61#include <errno.h>
62#include <stdlib.h>
63#include <time.h>
64#include <unistd.h>
65
66#define RUNTIME 300
67
68const char *file;
69char c;
70
71void
72rd(void)
73{
74	struct stat st;
75	char *p1, *p2;
76	size_t len;
77	int error, fd;
78
79	if ((fd = open(file, O_RDONLY)) == -1)
80		err(1, "open %s", file);
81	if ((error = fstat(fd, &st)) == -1)
82		err(1, "stat(%s)", file);
83	len = round_page(st.st_size);
84	if ((p1 = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED)
85		err(1, "mmap");
86	if ((p2 = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, 0)) == MAP_FAILED)
87		err(1, "mmap");
88	c = p1[arc4random() % len];
89	c = p2[arc4random() % len];
90	if (arc4random() % 100 < 50)
91		if ((error = mlock(p1, len)) == -1)
92			err(1, "mlock");
93	c = p1[arc4random() % len];
94	if (munmap(p2, len) == -1)
95		err(1, "unmap()");
96	if (munmap(p1, len) == -1)
97		err(1, "unmap()");
98	close(fd);
99
100}
101void
102wr(void)
103{
104	struct stat st;
105	char *p1, *p2;
106	size_t len;
107	int error, fd;
108
109	if ((fd = open(file, O_RDWR)) == -1)
110		err(1, "open %s", file);
111	if ((error = fstat(fd, &st)) == -1)
112		err(1, "stat(%s)", file);
113	len = round_page(st.st_size);
114	if ((p1 = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) ==
115	    MAP_FAILED)
116		err(1, "mmap");
117	if ((p2 = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) ==
118		    MAP_FAILED)
119		err(1, "mmap");
120	p1[arc4random() % len] = 1;
121	p2[arc4random() % len] = 1;
122	if (arc4random() % 100 < 50)
123		if ((error = mlock(p1, len)) == -1)
124			err(1, "mlock");
125	p1[arc4random() % len] = 1;
126	if (arc4random() % 100 < 50)
127		if ((error = msync(p1, len, MS_SYNC | MS_INVALIDATE)) == -1)
128			if (errno != EBUSY)
129				err(1, "msync");
130	if (munmap(p2, len) == -1)
131		err(1, "unmap()");
132	if (munmap(p1, len) == -1)
133		err(1, "unmap()");
134	close(fd);
135
136}
137
138void
139test2(void)
140{
141	if (arc4random() % 100 < 30)
142		rd();
143	else
144		wr();
145	_exit(0);
146}
147
148void
149test(void)
150{
151	int i;
152
153	for (i = 0; i < 3; i++)
154		if (fork() == 0)
155			test2();
156	for (i = 0; i < 3; i++)
157		wait(NULL);
158
159	_exit(0);
160}
161
162int
163main(int argc, char *argv[])
164{
165	time_t start;
166
167	if (argc != 2)
168		errx(1, "Usage: %s <file>", argv[0]);
169	file = argv[1];
170
171	start = time(NULL);
172	while (time(NULL) - start < RUNTIME) {
173		if (fork() == 0)
174			test();
175		wait(NULL);
176	}
177
178	return (0);
179}
180