xref: /freebsd/tools/test/stress2/misc/mmap11.sh (revision 9768746b)
1#!/bin/sh
2
3#
4# Copyright (c) 2014 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# Copy of mmap10.sh with msync() added.
30# "panic: vm_map_protect: inaccessible wired map entry" seen:
31# http://people.freebsd.org/~pho/stress/log/mmap11.txt
32# No problems seen after r271681.
33
34# http://people.freebsd.org/~pho/stress/log/kostik730.txt, Fixed in r273784
35
36[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
37
38. ../default.cfg
39
40here=`pwd`
41cd /tmp
42sed '1,/^EOF/d' < $here/$0 > mmap11.c
43mycc -o mmap11 -Wall -Wextra -O2 -g mmap11.c -lpthread || exit 1
44rm -f mmap11.c
45
46daemon sh -c "(cd $here/../testcases/swap; ./swap -t 2m -i 20 -k)"
47sleep `jot -r 1 0 9`
48for i in `jot 2`; do
49	/tmp/mmap11 &
50done
51sleep 300
52while pgrep -q mmap11; do
53	pkill -9 mmap11
54	sleep 2
55done
56wait
57killall -q swap
58
59rm -f /tmp/mmap11 /tmp/mmap11.core
60exit 0
61EOF
62#include <sys/types.h>
63#include <sys/mman.h>
64#include <sys/stat.h>
65#include <sys/wait.h>
66
67#include <err.h>
68#include <errno.h>
69#include <fcntl.h>
70#include <pthread.h>
71#include <pthread_np.h>
72#include <stdio.h>
73#include <stdlib.h>
74#include <string.h>
75#include <unistd.h>
76
77#define LOOPS 2
78#define MMSIZE (192 * 1024 * 1024)
79#define N (128 * 1024 / (int)sizeof(u_int32_t))
80#define PARALLEL 50
81
82void *p;
83u_int32_t r[N];
84
85unsigned long
86makearg(void)
87{
88	unsigned long val;
89	unsigned int i;
90
91	val = arc4random();
92	i   = arc4random() % 100;
93	if (i < 20)
94		val = val & 0xff;
95	if (i >= 20 && i < 40)
96		val = val & 0xffff;
97	if (i >= 40 && i < 60)
98		val = (unsigned long)(r) | (val & 0xffff);
99#if defined(__LP64__)
100	if (i >= 60) {
101		val = (val << 32) | arc4random();
102		if (i > 80)
103			val = val & 0x00007fffffffffffUL;
104	}
105#endif
106
107	return(val);
108}
109
110void *
111makeptr(void)
112{
113	unsigned long val;
114
115	if (p != MAP_FAILED && p != NULL)
116		val = (unsigned long)p + arc4random();
117	else
118		val = makearg();
119	val = trunc_page(val);
120
121	return ((void *)val);
122}
123
124void *
125tmmap(void *arg __unused)
126{
127	size_t len;
128	int i, j, fd;
129
130	pthread_set_name_np(pthread_self(), __func__);
131	len = MMSIZE;
132
133	for (i = 0, j = 0; i < 100; i++) {
134		if ((fd = open("/dev/zero", O_RDWR)) == -1)
135			err(1,"open()");
136
137		if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE,
138		    fd, 0)) != MAP_FAILED) {
139			usleep(100);
140			munmap(p, len);
141			j++;
142		}
143
144		if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_ANON,
145		    -1, 0)) != MAP_FAILED) {
146			usleep(100);
147			munmap(p, len);
148			j++;
149		}
150		close(fd);
151	}
152	if (j == 0)
153		fprintf(stderr, "FAIL: all mmap(2) calls failed.\n");
154
155	return (NULL);
156}
157
158void *
159tmlock(void *arg __unused)
160{
161	size_t len;
162	int i, n;
163
164	pthread_set_name_np(pthread_self(), __func__);
165	n = 0;
166	for (i = 0; i < 200; i++) {
167		len = trunc_page(makearg());
168		if (mlock(makeptr(), len) == 0)
169			n++;
170		len = trunc_page(makearg());
171		if (arc4random() % 100 < 50)
172			if (munlock(makeptr(), len) == 0)
173				n++;
174	}
175	if (n < 10)
176		fprintf(stderr, "Note: tmlock() only succeeded %d times.\n",
177		    n);
178
179	return (NULL);
180}
181
182void *
183tmprotect(void *arg __unused)
184{
185	size_t len;
186	void *addr;
187	int i, n, prot;
188
189	pthread_set_name_np(pthread_self(), __func__);
190	n = 0;
191	for (i = 0; i < 200; i++) {
192		addr = makeptr();
193		len = trunc_page(makearg());
194		prot = makearg();
195		if (mprotect(addr, len, prot) == 0)
196			n++;
197		usleep(1000);
198	}
199	if (n < 10)
200		fprintf(stderr, "Note: tmprotect() only succeeded %d times.\n",
201		    n);
202	return (NULL);
203}
204
205void *
206tmlockall(void *arg __unused)
207{
208	int flags, i, n;
209
210	pthread_set_name_np(pthread_self(), __func__);
211	n = 0;
212	for (i = 0; i < 200; i++) {
213		flags = makearg();
214		if (mlockall(flags) == 0)
215			n++;
216		usleep(100);
217		munlockall();
218		usleep(1000);
219	}
220	if (n < 10)
221		fprintf(stderr, "Note: tmlockall() only succeeded %d times.\n",
222		    n);
223
224	return (NULL);
225}
226
227void *
228tmsync(void *arg __unused)
229{
230	size_t len;
231	void *addr;
232	int flags, i, n;
233
234	pthread_set_name_np(pthread_self(), __func__);
235	n = 0;
236	for (i = 0; i < 200; i++) {
237		addr = makeptr();
238		len = trunc_page(makearg());
239		flags = makearg();
240		if (msync(addr, len, flags) == 0)
241			n++;
242		usleep(2000);
243	}
244	if (n < 10)
245		fprintf(stderr, "Note: tmsync() only succeeded %d times.\n",
246		    n);
247
248	return (NULL);
249}
250
251void
252test(void)
253{
254	pthread_t tid[5];
255	int i, rc;
256
257	if ((rc = pthread_create(&tid[0], NULL, tmmap, NULL)) != 0)
258		errc(1, rc, "tmmap()");
259	if ((rc = pthread_create(&tid[1], NULL, tmlock, NULL)) != 0)
260		errc(1, rc, "tmlock()");
261	if ((rc = pthread_create(&tid[2], NULL, tmprotect, NULL)) != 0)
262		errc(1, rc, "tmprotect()");
263	if ((rc = pthread_create(&tid[3], NULL, tmlockall, NULL)) != 0)
264		errc(1, rc, "tmlockall()");
265	if ((rc = pthread_create(&tid[4], NULL, tmsync, NULL)) != 0)
266		errc(1, rc, "tmlockall()");
267
268	for (i = 0; i < 100; i++) {
269		if (fork() == 0) {
270			usleep(10000);
271			_exit(0);
272		}
273		wait(NULL);
274	}
275
276	for (i = 0; i < 5; i++)
277		if ((rc = pthread_join(tid[i], NULL)) != 0)
278			errc(1, rc, "pthread_join(%d)", i);
279	_exit(0);
280}
281
282int
283main(void)
284{
285	int i, j;
286
287	for (i = 0; i < N; i++)
288		r[i] = arc4random();
289
290	for (i = 0; i < LOOPS; i++) {
291		for (j = 0; j < PARALLEL; j++) {
292			if (fork() == 0)
293				test();
294		}
295
296		for (j = 0; j < PARALLEL; j++)
297			wait(NULL);
298	}
299
300	return (0);
301}
302