aboutsummaryrefslogtreecommitdiff
path: root/tools/test/stress2/misc/swapoff5.sh
blob: 9801c5956716f65bb3787226f72d25963cd7d2a5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/bin/sh

#
# SPDX-License-Identifier: BSD-2-Clause
#
# Copyright (c) 2020 Mark Johnston
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#

# Variation of swapoff3.sh

# Deadlock seen:
# https://people.freebsd.org/~pho/stress/log/log0005.txt
# This is a "known" low memory deadlocks caused by using md(4) as a swap
# device.

. ../default.cfg
[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
dir=`dirname $diskimage`
[ `df -k $dir | tail -1 | awk '{print $4}'` -lt \
    $((20 * 1024 * 1024)) ] &&
    { echo "Need 20GB on $dir"; exit 0; }

cat > /tmp/swapoff5.c <<EOF
#include <sys/param.h>
#include <sys/mman.h>
#include <sys/sysctl.h>

#include <assert.h>
#include <err.h>
#include <stdlib.h>
#include <unistd.h>

int
main(void)
{
	char *addr, *naddr, *res;
	size_t i, len, swap, vsz;
	u_int free;

	vsz = sizeof(free);
	if (sysctlbyname("vm.stats.vm.v_free_count", &free, &vsz, NULL, 0) != 0)
		err(1, "sysctl(vm.stats.vm.v_free_count)");
	vsz = sizeof(swap);
	if (sysctlbyname("vm.swap_total", &swap, &vsz, NULL, 0) != 0)
		err(1, "sysctl(vm.swap_total)");

	len = (size_t)free * PAGE_SIZE + swap / 4;
	addr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
	    -1, 0);
	if (addr == MAP_FAILED)
		err(1, "mmap");

	res = malloc(howmany(len, PAGE_SIZE));
	if (res == NULL)
		err(1, "malloc");

	for (i = 0; i < len; i += PAGE_SIZE)
		addr[i] = 1;

	for (;;) {
		if (mincore(addr, len, res) != 0)
			err(1, "mincore");
		for (i = 0; i < howmany(len, PAGE_SIZE); i++)
			if ((res[i] & MINCORE_INCORE) == 0) {
				naddr = addr + i * PAGE_SIZE;
				if (munmap(naddr, PAGE_SIZE) != 0)
					err(1, "munmap");
				if (mmap(naddr, PAGE_SIZE,
				    PROT_READ | PROT_WRITE,
				    MAP_ANON | MAP_PRIVATE | MAP_FIXED,
				    -1, 0) == MAP_FAILED)
					err(1, "mmap");
				assert(*naddr == 0);
				*naddr = 1;
			}
	}

	return (0);
}
EOF
mycc -o /tmp/swapoff5 -Wall -Wextra -O2 -g /tmp/swapoff5.c || exit

cat > /tmp/swapoff5x.c <<EOF
#include <sys/mman.h>

#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int
main(int argc, char *argv[])
{
	time_t start;
	int n, r;
	char off1[80], off2[80], on1[80], on2[80];

	if ( argc != 3) {
		fprintf(stderr, "Usage: %s <dev1> <dev2>\n", argv[0]);
		exit(1);
	}

	snprintf(off1, sizeof(off1), "swapoff %s", argv[1]);
	snprintf(off2, sizeof(off2), "swapoff %s", argv[2]);
	snprintf(on1, sizeof(on1), "swapon %s", argv[1]);
	snprintf(on2, sizeof(on2), "swapon %s", argv[2]);

	start = time(NULL);
	if (mlockall(MCL_CURRENT | MCL_FUTURE) == -1)
		err(1, "mlockall(MCL_CURRENT) | MCL_FUTURE");
	n = 0;
	while (time(NULL) - start < 240) {
		if ((r = system(off1)) != 0)
			errx(1, "%s failed with %d", off1, r);
		if ((r = system(on1)) != 0)
			errx(1, "%s failed with %d", on1, r);
		if ((r = system(off2)) != 0)
			errx(1, "%s failed with %d", off2, r);
		if ((r = system(on2)) != 0)
			errx(1, "%s failed with %d", on2, r);
		n++;
	}
	if (n < 100)
		fprintf(stderr, "%d swapoffs\n", n);

	return (0);
}
EOF
mycc -o /tmp/swapoff5x -Wall -Wextra -O2 -g /tmp/swapoff5x.c || exit

set -e
md1=$mdstart
md2=$((md1 + 1))
[ `sysctl -n vm.swap_total` -gt 0 ] && { swapoff -a > /dev/null; off=1; }
truncate -s 10G $dir/swap1
mdconfig -a -t vnode -f $dir/swap1 -u $md1
swapon /dev/md$md1
truncate -s 10G $dir/swap2
mdconfig -a -t vnode -f $dir/swap2 -u $md2
swapon /dev/md$md2
set +e

timeout 4m /tmp/swapoff5 &
for i in `jot 60`; do
	n=`swapinfo | tail -1 | awk '{print $3}'`
	[ $n -gt 0 ] && break
	sleep 1
done

/tmp/swapoff5x /dev/md$md1 /dev/md$md2
kill $! > /dev/null 2>&1
wait
for dev in /dev/md$md1 /dev/md$md2; do
	swapoff $dev
done
mdconfig -d -u $md1
mdconfig -d -u $md2
rm -f $dir/swap1 $dir/swap2 /tmp/swapoff5 /tmp/swapoff5.c /tmp/swapoff5x \
    /tmp/swapoff5x.c
[ $off ] && swapon -a > /dev/null
exit $s