xref: /freebsd/tools/test/stress2/misc/swapoff3.sh (revision e32fecd0c2c3ee37c47ee100f169e7eb0282a873)
1#!/bin/sh
2
3#
4# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5#
6# Copyright (c) 2020 Mark Johnston
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# "panic: Swapdev not found" seen:
31# https://people.freebsd.org/~pho/stress/log/swapoff3.txt
32# Fixed by r358024-6
33
34. ../default.cfg
35[ `id -u` -ne 0 ] && echo "Must be root!" && exit 1
36dir=`dirname $diskimage`
37[ `df -k $dir | tail -1 | awk '{print $4}'` -lt \
38    $((20 * 1024 * 1024)) ] &&
39    { echo "Need 20GB on $dir"; exit 0; }
40
41cat > /tmp/swapoff3.c <<EOF
42#include <sys/param.h>
43#include <sys/mman.h>
44#include <sys/sysctl.h>
45
46#include <assert.h>
47#include <err.h>
48#include <stdlib.h>
49#include <unistd.h>
50
51int
52main(void)
53{
54	char *addr, *naddr, *res;
55	size_t i, len, swap, vsz;
56	u_int free;
57
58	vsz = sizeof(free);
59	if (sysctlbyname("vm.stats.vm.v_free_count", &free, &vsz, NULL, 0) != 0)
60		err(1, "sysctl(vm.stats.vm.v_free_count)");
61	vsz = sizeof(swap);
62	if (sysctlbyname("vm.swap_total", &swap, &vsz, NULL, 0) != 0)
63		err(1, "sysctl(vm.swap_total)");
64
65	len = (size_t)free * PAGE_SIZE + swap / 4;
66	addr = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_ANON | MAP_PRIVATE,
67	    -1, 0);
68	if (addr == MAP_FAILED)
69		err(1, "mmap");
70
71	res = malloc(howmany(len, PAGE_SIZE));
72	if (res == NULL)
73		err(1, "malloc");
74
75	for (i = 0; i < len; i += PAGE_SIZE)
76		addr[i] = 1;
77
78	for (;;) {
79		if (mincore(addr, len, res) != 0)
80			err(1, "mincore");
81		for (i = 0; i < howmany(len, PAGE_SIZE); i++)
82			if ((res[i] & MINCORE_INCORE) == 0) {
83				naddr = addr + i * PAGE_SIZE;
84				if (munmap(naddr, PAGE_SIZE) != 0)
85					err(1, "munmap");
86				if (mmap(naddr, PAGE_SIZE,
87				    PROT_READ | PROT_WRITE,
88				    MAP_ANON | MAP_PRIVATE | MAP_FIXED,
89				    -1, 0) == MAP_FAILED)
90					err(1, "mmap");
91				assert(*naddr == 0);
92				*naddr = 1;
93			}
94	}
95
96	return (0);
97}
98EOF
99mycc -o /tmp/swapoff3 -Wall -Wextra -O2 -g /tmp/swapoff3.c || exit
100
101set -e
102md1=$mdstart
103md2=$((md1 + 1))
104[ `sysctl -n vm.swap_total` -gt 0 ] && { swapoff -a > /dev/null; off=1; }
105truncate -s 10G $dir/swap1
106mdconfig -a -t vnode -f $dir/swap1 -u $md1
107swapon /dev/md$md1
108truncate -s 10G $dir/swap2
109mdconfig -a -t vnode -f $dir/swap2 -u $md2
110swapon /dev/md$md2
111set +e
112
113timeout 4m /tmp/swapoff3 &
114for i in `jot 60`; do
115	n=`swapinfo | tail -1 | awk '{print $3}'`
116	[ $n -gt 0 ] && break
117	sleep 1
118done
119echo "Total in use: $n"
120
121n=0
122start=`date +%s`
123while [ $((`date +%s` - start)) -lt 120 ]; do
124	for dev in /dev/md$md1 /dev/md$md2; do
125		for i in `jot 3`; do
126			swapoff $dev && break
127		done
128		swapon $dev
129		n=$((n + 1))
130	done
131	sleep .1
132done
133kill $!
134echo "$n swapoffs"
135[ $n -lt 1000 ] && { echo "Only performed $n swapoffs"; s=1; } || s=0
136wait
137for dev in /dev/md$md1 /dev/md$md2; do
138	swapoff $dev
139done
140mdconfig -d -u $md1
141mdconfig -d -u $md2
142rm -f $dir/swap1 $dir/swap2 /tmp/swapoff3 /tmp/swapoff3.c
143[ $off ] && swapon -a > /dev/null
144exit 0
145