xref: /freebsd/tools/test/stress2/misc/mmap40.sh (revision e0c4386e)
1#!/bin/sh
2
3#
4# SPDX-License-Identifier: BSD-2-Clause
5#
6# Copyright (c) 2020 Peter Holm <pho@FreeBSD.org>
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27# SUCH DAMAGE.
28#
29
30# Attempt to reproduce "vm_page_assert_xbusied: page XXXX not exclusive busy"
31# No problems seen.
32
33# Test scenario idea by markj@
34
35. ../default.cfg
36[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
37[ `sysctl -n vm.swap_total` -eq 0 ] && exit 0
38
39dir=/tmp
40odir=`pwd`
41cd $dir
42sed '1,/^EOF/d' < $odir/$0 > $dir/mmap40.c
43mycc -o mmap40 -Wall -Wextra -O0 -g mmap40.c || exit 1
44rm -f mmap40.c
45cd $odir
46
47set -e
48mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
49[ -c /dev/md$mdstart ] &&  mdconfig -d -u $mdstart
50mdconfig -a -t swap -s 2g -u $mdstart
51newfs $newfs_flags md$mdstart > /dev/null
52mount /dev/md$mdstart $mntpoint
53set +e
54
55u1=`swapinfo | tail -1 | awk '{print $3}'`
56(nice $odir/../testcases/swap/swap -t 10m -i 30 -h -l 100) &
57while [ $((`swapinfo | tail -1 | awk '{print $3}'` - $u1)) -le 100 ]; do
58	sleep 1
59done
60
61$dir/mmap40
62s=0
63while pkill swap; do :; done
64wait
65[ -f mmap40.core -a $s -eq 0 ] &&
66    { ls -l mmap40.core; mv mmap40.core $dir; s=1; }
67cd $odir
68
69for i in `jot 6`; do
70	mount | grep -q "on $mntpoint " || break
71	umount $mntpoint && break || sleep 10
72	[ $i -eq 6 ] &&
73	    { echo FATAL; fstat -mf $mntpoint; exit 1; }
74done
75mdconfig -d -u $mdstart
76rm -rf $dir/mmap40
77exit $s
78
79EOF
80#include <sys/param.h>
81#include <sys/mman.h>
82#include <sys/wait.h>
83
84#include <err.h>
85#include <stdlib.h>
86#include <stdio.h>
87#include <string.h>
88#include <time.h>
89#include <unistd.h>
90
91#define PARALLEL 512
92#define RUNTIME 300
93
94void
95test(void)
96{
97	pid_t pid;
98	size_t i, len;
99	time_t start;
100	void *p;
101	char *vec;
102
103	len = 1024 * 1024;
104	if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0)) ==
105	    MAP_FAILED)
106		err(1, "mmap");
107	memset(p, 0, len); /* dirty the memory */
108	if ((vec = malloc(len / PAGE_SIZE)) == NULL)
109		err(1, "malloc");
110
111	start = time(NULL);
112	while (time(NULL) - start < RUNTIME) {
113		usleep(arc4random() % 1000);
114		if (mincore(p, len, vec) == -1)
115			err(1, "mincore");
116		for (i = 0; i < len / PAGE_SIZE; i++) {
117			if ((vec[i] & MINCORE_MODIFIED) == 0) {
118				_exit(0);
119			}
120		}
121		if ((pid = fork()) == 0) {
122			_exit(0);
123		}
124		if (waitpid(pid, NULL, 0) != pid)
125			err(1, "waitpid)");
126	}
127
128	_exit(0);
129}
130
131int
132main(void)
133{
134	pid_t pids[PARALLEL];
135	time_t start;
136	int i;
137
138	for (i = 0; i < PARALLEL; i++) {
139		if ((pids[i] = fork()) == 0)
140			test();
141	}
142
143	start = time(NULL);
144	while (time(NULL) - start < RUNTIME) {
145		for (i = 0; i < PARALLEL; i++) {
146			if (waitpid(pids[i], NULL, WNOHANG) == pids[i]) {
147				if ((pids[i] = fork()) == 0)
148					test();
149			}
150		}
151	}
152
153	for (i = 0; i < PARALLEL; i++) {
154		if (waitpid(pids[i], NULL, 0) != pids[i])
155			err(1, "waitpid");
156	}
157
158	return (0);
159}
160