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