xref: /freebsd/tools/test/stress2/misc/mmap18.sh (revision e0c4386e)
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;
92static int debug; /* set to 1 for debug output */
93
94static unsigned long
95makearg(void)
96{
97	unsigned long val;
98	unsigned int i;
99
100	val = arc4random();
101	i   = arc4random() % 100;
102	if (i < 20)
103		val = val & 0xff;
104	if (i >= 20 && i < 40)
105		val = val & 0xffff;
106	if (i >= 40 && i < 60)
107		val = (unsigned long)(r) | (val & 0xffff);
108#if defined(__LP64__)
109	if (i >= 60) {
110		val = (val << 32) | arc4random();
111		if (i > 80)
112			val = val & 0x00007fffffffffffUL;
113	}
114#endif
115
116	return (val);
117}
118
119static void *
120makeptr(void)
121{
122	unsigned long val;
123
124	if (p != MAP_FAILED && p != NULL)
125		val = (unsigned long)p + arc4random();
126	else
127		val = makearg();
128	val = trunc_page(val);
129
130	return ((void *)val);
131}
132
133static void *
134tmmap(void *arg __unused)
135{
136	size_t len;
137	int i, fd;
138
139	pthread_set_name_np(pthread_self(), __func__);
140	len = 1LL * 1024 * 1024 * 1024;
141
142	for (i = 0; i < 100; i++) {
143		if ((fd = open("/dev/zero", O_RDWR)) == -1)
144			err(1,"open()");
145
146		if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_PRIVATE,
147		    fd, 0)) != MAP_FAILED) {
148			usleep(100);
149			munmap(p, len);
150		}
151
152		if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_ANON,
153		    -1, 0)) != MAP_FAILED) {
154			usleep(100);
155			munmap(p, len);
156		}
157		close(fd);
158	}
159
160	return (NULL);
161}
162
163static void *
164tmlock(void *arg __unused)
165{
166	int i, n;
167	size_t len;
168
169	pthread_set_name_np(pthread_self(), __func__);
170	n = 0;
171	for (i = 0; i < 200; i++) {
172		len = trunc_page(makearg());
173		if (mlock(makeptr(), len) == 0)
174			n++;
175		len = trunc_page(makearg());
176		if (arc4random() % 100 < 50)
177			if (munlock(makeptr(), len) == 0)
178				n++;
179	}
180	if (debug != 0 && n < 10)
181		fprintf(stderr, "Note: tmlock() only succeeded %d "
182		    "times.\n", n);
183
184	return (NULL);
185}
186
187static void *
188tmprotect(void *arg __unused)
189{
190	void *addr;
191	size_t len;
192	int i, n, prot;
193
194	pthread_set_name_np(pthread_self(), __func__);
195	n = 0;
196	for (i = 0; i < 200; i++) {
197		addr = makeptr();
198		len = trunc_page(makearg());
199		prot = makearg();
200		if (mprotect(addr, len, prot) == 0)
201			n++;
202		usleep(1000);
203	}
204	if (debug != 0 && n < 10)
205		fprintf(stderr, "Note: tmprotect() only succeeded %d "
206		    "times.\n", n);
207
208	return (NULL);
209}
210
211static void *
212tmlockall(void *arg __unused)
213{
214	int flags, i, n;
215
216	pthread_set_name_np(pthread_self(), __func__);
217	n = 0;
218	for (i = 0; i < 200; i++) {
219		flags = makearg();
220		if (mlockall(flags) == 0)
221			n++;
222		usleep(100);
223		munlockall();
224		usleep(1000);
225	}
226	if (debug != 0 && n < 10)
227		fprintf(stderr, "Note: tmlockall() only succeeded %d "
228		    "times.\n", n);
229
230	return (NULL);
231}
232
233static void
234test(void)
235{
236	pthread_t tid[4];
237	int i, rc;
238
239	if ((rc = pthread_create(&tid[0], NULL, tmmap, NULL)) != 0)
240		errc(1, rc, "tmmap()");
241	if ((rc = pthread_create(&tid[1], NULL, tmlock, NULL)) != 0)
242		errc(1, rc, "tmlock()");
243	if ((rc = pthread_create(&tid[2], NULL, tmprotect, NULL)) != 0)
244		errc(1, rc, "tmprotect()");
245	if ((rc = pthread_create(&tid[3], NULL, tmlockall, NULL)) != 0)
246		errc(1, rc, "tmlockall()");
247
248	for (i = 0; i < 100; i++) {
249		if (fork() == 0) {
250			usleep(10000);
251			_exit(0);
252		}
253		wait(NULL);
254	}
255
256	for (i = 0; i < 4; i++)
257		if ((rc = pthread_join(tid[i], NULL)) != 0)
258			errc(1, rc, "pthread_join(%d)", i);
259	_exit(0);
260}
261
262int
263main(int argc, char *argv[])
264{
265	struct rlimit rl;
266	rlim_t maxlock;
267	int i, j;
268
269	if (argc != 2) {
270		fprintf(stderr, "Usage:%s <max pages to lock.>\n", argv[0]);
271		exit(1);
272	}
273	rl.rlim_max = rl.rlim_cur = 0;
274	if (setrlimit(RLIMIT_CORE, &rl) == -1)
275		warn("setrlimit");
276
277	if (getrlimit(RLIMIT_MEMLOCK, &rl) == -1)
278		warn("getrlimit");
279	maxlock = atol(argv[1]);
280	if (maxlock <= 0)
281		errx(1, "Bad argument %jd", maxlock);
282	maxlock = (maxlock / 10 * 8) / PARALLEL * PAGE_SIZE;
283	if (maxlock < rl.rlim_cur) {
284		rl.rlim_max = rl.rlim_cur = maxlock;
285		if (setrlimit(RLIMIT_MEMLOCK, &rl) == -1)
286			warn("setrlimit");
287	}
288
289	for (i = 0; i < N; i++)
290		r[i] = arc4random();
291
292	for (i = 0; i < LOOPS; i++) {
293		for (j = 0; j < PARALLEL; j++) {
294			if (fork() == 0)
295				test();
296		}
297
298		for (j = 0; j < PARALLEL; j++)
299			wait(NULL);
300	}
301
302	return (0);
303}
304