xref: /freebsd/tools/test/stress2/misc/mmap18.sh (revision cc68614da8232d8baaca0ae0d0dd8f890f06623e)
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 core dump disabled.
30# http://people.freebsd.org/~pho/stress/log/kostik711.txt
31
32# panic: vm_fault_copy_entry: main object missing page
33# http://people.freebsd.org/~pho/stress/log/mmap18.txt
34# Fixed by: r316689
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 > mmap18.c
43mycc -o mmap18 -Wall -Wextra -O2 mmap18.c -lpthread || exit 1
44rm -f mmap18.c
45
46s=0
47wire=$((`sysctl -n vm.max_user_wired` - \
48    `sysctl -n vm.stats.vm.v_user_wire_count`))
49/tmp/mmap18 $wire &
50start=`date +%s`
51while true; do
52	e=$((`date +%s` - start))
53	pgrep -q mmap18 || break
54	if [ $e -gt 900 ]; then
55		pgrep mmap18 | xargs ps -lHp
56		pkill mmap18
57		break;
58	fi
59	sleep 10
60done
61wait $!; s=$?
62
63rm -f /tmp/mmap18 /tmp/mmap18.core
64exit $s
65EOF
66#include <sys/types.h>
67#include <sys/mman.h>
68#include <sys/resource.h>
69#include <sys/stat.h>
70#include <sys/time.h>
71#include <sys/wait.h>
72
73#include <err.h>
74#include <errno.h>
75#include <fcntl.h>
76#include <pthread.h>
77#include <pthread_np.h>
78#include <sched.h>
79#include <signal.h>
80#include <stdio.h>
81#include <stdlib.h>
82#include <string.h>
83#include <time.h>
84#include <unistd.h>
85
86#define LOOPS 50
87#define N (128 * 1024 / (int)sizeof(u_int32_t))
88#define PARALLEL 50
89
90static u_int32_t r[N];
91static void *p;
92
93static unsigned long
94makearg(void)
95{
96	unsigned long val;
97	unsigned int i;
98
99	val = arc4random();
100	i   = arc4random() % 100;
101	if (i < 20)
102		val = val & 0xff;
103	if (i >= 20 && i < 40)
104		val = val & 0xffff;
105	if (i >= 40 && i < 60)
106		val = (unsigned long)(r) | (val & 0xffff);
107#if defined(__LP64__)
108	if (i >= 60) {
109		val = (val << 32) | arc4random();
110		if (i > 80)
111			val = val & 0x00007fffffffffffUL;
112	}
113#endif
114
115	return (val);
116}
117
118static void *
119makeptr(void)
120{
121	unsigned long val;
122
123	if (p != MAP_FAILED && p != NULL)
124		val = (unsigned long)p + arc4random();
125	else
126		val = makearg();
127	val = trunc_page(val);
128
129	return ((void *)val);
130}
131
132static void *
133tmmap(void *arg __unused)
134{
135	size_t len;
136	int i, fd;
137
138	pthread_set_name_np(pthread_self(), __func__);
139	len = 1LL * 1024 * 1024 * 1024;
140
141	for (i = 0; i < 100; i++) {
142		if ((fd = open("/dev/zero", O_RDWR)) == -1)
143			err(1,"open()");
144
145		if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE,
146		    fd, 0)) != MAP_FAILED) {
147			usleep(100);
148			munmap(p, len);
149		}
150
151		if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_ANON,
152		    -1, 0)) != MAP_FAILED) {
153			usleep(100);
154			munmap(p, len);
155		}
156		close(fd);
157	}
158
159	return (NULL);
160}
161
162static void *
163tmlock(void *arg __unused)
164{
165	int i, n;
166	size_t len;
167
168	pthread_set_name_np(pthread_self(), __func__);
169	n = 0;
170	for (i = 0; i < 200; i++) {
171		len = trunc_page(makearg());
172		if (mlock(makeptr(), len) == 0)
173			n++;
174		len = trunc_page(makearg());
175		if (arc4random() % 100 < 50)
176			if (munlock(makeptr(), len) == 0)
177				n++;
178	}
179#if defined(DEBUG)
180	if (n < 10)
181		fprintf(stderr, "Note: tmlock() only succeeded %d "
182		    "times.\n", n);
183#endif
184
185	return (NULL);
186}
187
188static void *
189tmprotect(void *arg __unused)
190{
191	void *addr;
192	size_t len;
193	int i, n, prot;
194
195	pthread_set_name_np(pthread_self(), __func__);
196	n = 0;
197	for (i = 0; i < 200; i++) {
198		addr = makeptr();
199		len = trunc_page(makearg());
200		prot = makearg();
201		if (mprotect(addr, len, prot) == 0)
202			n++;
203		usleep(1000);
204	}
205#if defined(DEBUG)
206	if (n < 10)
207		fprintf(stderr, "Note: tmprotect() only succeeded %d "
208		    "times.\n", n);
209#endif
210
211	return (NULL);
212}
213
214static void *
215tmlockall(void *arg __unused)
216{
217	int flags, i, n;
218
219	pthread_set_name_np(pthread_self(), __func__);
220	n = 0;
221	for (i = 0; i < 200; i++) {
222		flags = makearg();
223		if (mlockall(flags) == 0)
224			n++;
225		usleep(100);
226		munlockall();
227		usleep(1000);
228	}
229#if defined(DEBUG)
230	if (n < 10)
231		fprintf(stderr, "Note: tmlockall() only succeeded %d "
232		    "times.\n", n);
233#endif
234
235	return (NULL);
236}
237
238static void
239test(void)
240{
241	pthread_t tid[4];
242	int i, rc;
243
244	if ((rc = pthread_create(&tid[0], NULL, tmmap, NULL)) != 0)
245		errc(1, rc, "tmmap()");
246	if ((rc = pthread_create(&tid[1], NULL, tmlock, NULL)) != 0)
247		errc(1, rc, "tmlock()");
248	if ((rc = pthread_create(&tid[2], NULL, tmprotect, NULL)) != 0)
249		errc(1, rc, "tmprotect()");
250	if ((rc = pthread_create(&tid[3], NULL, tmlockall, NULL)) != 0)
251		errc(1, rc, "tmlockall()");
252
253	for (i = 0; i < 100; i++) {
254		if (fork() == 0) {
255			usleep(10000);
256			_exit(0);
257		}
258		wait(NULL);
259	}
260
261	for (i = 0; i < 4; i++)
262		if ((rc = pthread_join(tid[i], NULL)) != 0)
263			errc(1, rc, "pthread_join(%d)", i);
264	_exit(0);
265}
266
267int
268main(int argc, char *argv[])
269{
270	struct rlimit rl;
271	rlim_t maxlock;
272	int i, j;
273
274	if (argc != 2) {
275		fprintf(stderr, "Usage:%s <max pages to lock.>\n", argv[0]);
276		exit(1);
277	}
278	rl.rlim_max = rl.rlim_cur = 0;
279	if (setrlimit(RLIMIT_CORE, &rl) == -1)
280		warn("setrlimit");
281
282	if (getrlimit(RLIMIT_MEMLOCK, &rl) == -1)
283		warn("getrlimit");
284	maxlock = atol(argv[1]);
285	if (maxlock <= 0)
286		errx(1, "Bad argument %jd", maxlock);
287	maxlock = (maxlock / 10 * 8) / PARALLEL * PAGE_SIZE;
288	if (maxlock < rl.rlim_cur) {
289		rl.rlim_max = rl.rlim_cur = maxlock;
290		if (setrlimit(RLIMIT_MEMLOCK, &rl) == -1)
291			warn("setrlimit");
292	}
293
294	for (i = 0; i < N; i++)
295		r[i] = arc4random();
296
297	for (i = 0; i < LOOPS; i++) {
298		for (j = 0; j < PARALLEL; j++) {
299			if (fork() == 0)
300				test();
301		}
302
303		for (j = 0; j < PARALLEL; j++)
304			wait(NULL);
305	}
306
307	return (0);
308}
309