xref: /freebsd/tools/test/stress2/misc/swap2.sh (revision 410556f1f10fd35b350102725fd8504c3cb0afc8)
1#!/bin/sh
2
3#
4# Copyright (c) 2013 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# Swap test. Variation of swap.sh
30# panic: vm_radix_remove: impossible to locate the key
31# panic: vm_page_alloc: cache page 0xff... is missing from the free queue
32# panic: vm_radix_node_put: rnode 0xfffffe00099035a0 has a child
33# panic: vm_radix_remove: impossible to locate the key
34
35[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
36
37. ../default.cfg
38
39mount | grep $mntpoint | grep -q /dev/md && umount -f $mntpoint
40mdconfig -l | grep -q md$mdstart &&  mdconfig -d -u $mdstart
41mdconfig -a -t swap -s 2g -u $mdstart
42bsdlabel -w md$mdstart auto
43newfs $newfs_flags md${mdstart}$part > /dev/null
44mount /dev/md${mdstart}$part $mntpoint
45chmod 777 $mntpoint
46
47export RUNDIR=$mntpoint/stressX
48rm -rf /tmp/stressX.control
49dir=/tmp
50odir=`pwd`
51cd $dir
52sed '1,/^EOF/d' < $odir/$0 > $dir/swap2.c
53mycc -o swap2 -Wall -Wextra -O2 swap2.c || exit 1
54rm -f swap2.c
55cd $odir
56
57usermem=`sysctl -n hw.usermem`
58swap=`sysctl -n vm.swap_total`
59
60if [ $swap -gt 0 ]; then
61	size=$((usermem/10*11))
62else
63	size=$((usermem/10*8))
64fi
65
66/tmp/swap2 $((size / 4096)) &
67sleep 30
68su $testuser -c "(cd ../testcases/rw; ./rw -t 3m -i 40 -l 100 -v -h -h)" &
69wait
70
71while mount | grep $mntpoint | grep -q /dev/md; do
72	umount $mntpoint || sleep 1
73done
74mdconfig -d -u $mdstart
75rm -f /tmp/swap2
76exit
77EOF
78#include <sys/types.h>
79#include <err.h>
80#include <stdio.h>
81#include <stdlib.h>
82#include <sys/resource.h>
83#include <sys/sysctl.h>
84#include <sys/time.h>
85#include <sys/wait.h>
86#include <unistd.h>
87
88#define RUNTIME (5 * 60)
89#define INCARNATIONS 32
90
91static unsigned long size, original;
92
93void
94setup(void)
95{
96	struct rlimit rlp;
97
98	size = size / INCARNATIONS;
99	original = size;
100	if (size == 0)
101		errx(1, "Argument too small");
102
103	if (getrlimit(RLIMIT_DATA, &rlp) < 0)
104		err(1,"getrlimit");
105	rlp.rlim_cur -= 1024 * 1024;
106
107	if (size > (unsigned long)rlp.rlim_cur)
108		size = rlp.rlim_cur;
109
110#if 0
111	printf("setup: pid %d. Total %luMb\n",
112		getpid(), size / 1024 / 1024 * INCARNATIONS);
113#endif
114
115	if (size == 0)
116		errx(1, "Argument too small");
117
118	return;
119}
120
121int
122test(void)
123{
124	volatile char *c;
125	int page;
126	unsigned long i, j;
127	time_t start;
128
129	c = malloc(size);
130	while (c == NULL) {
131		size -=  1024 * 1024;
132		c = malloc(size);
133	}
134	if (size != original)
135		printf("Malloc size changed from %ld Mb to %ld Mb\n",
136		    original / 1024 / 1024, size / 1024 / 1024);
137	page = getpagesize();
138	start = time(NULL);
139	while ((time(NULL) - start) < RUNTIME) {
140		i = j = 0;
141		while (i < size) {
142			c[i] = 0;
143			i += page;
144			if (++j % 1024 == 0) {
145				if ((time(NULL) - start) >= RUNTIME)
146					break;
147				if (arc4random() % 100 < 5)
148					usleep(1000);
149			}
150		}
151	}
152	free((void *)c);
153
154	_exit(0);
155}
156
157int
158main(int argc, char **argv)
159{
160	int i;
161
162	if (argc != 2)
163		errx(1, "Usage: %s bytes", argv[0]);
164
165	size = atol(argv[1]) * 4096;
166	setup();
167
168	for (i = 0; i < INCARNATIONS; i++)
169		if (fork() == 0)
170			test();
171
172	for (i = 0; i < INCARNATIONS; i++)
173		wait(NULL);
174
175	return (0);
176}
177