xref: /freebsd/tools/test/stress2/misc/oovm.sh (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1#!/bin/sh
2
3#
4# Copyright (c) 2015 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# Out of VM deadlock seen. Introduced by r285808.
30# https://people.freebsd.org/~pho/stress/log/oovm.txt
31# https://people.freebsd.org/~pho/stress/log/oovm-2.txt
32
33# Fixed by r290047 and <alc's PQ_LAUNDRY patch>
34
35# Test scenario suggestion by alc@
36
37. ../default.cfg
38
39[ `swapinfo | wc -l` -eq 1 ] && exit 0
40maxsize=$((2 * 1024)) # Limit size due to runtime reasons
41size=$((`sysctl -n hw.physmem` / 1024 / 1024))
42[ $size -gt $((4 * 1024)) ] &&
43    echo "RAM should be capped to 4GB for this test."
44[ $size -gt $maxsize ] && size=$maxsize
45need=$((size * 2))
46d1=$diskimage.1
47d2=$diskimage.2
48rm -f $d1 $d2
49[ `df -k $(dirname $diskimage) | tail -1 | awk '{print int($4 / 1024)}'` -lt \
50    $need ] && printf "Need %d MB on %s.\n" $need `dirname $diskimage` && exit
51dd if=/dev/zero of=$d1 bs=1m count=$size status=none
52cp $d1 $d2 || exit
53trap "rm -f $d1 $d2" EXIT INT
54
55dir=/tmp
56odir=`pwd`
57cd $dir
58sed '1,/^EOF/d' < $odir/$0 > $dir/oovm.c
59mycc -o oovm -Wall -Wextra -g oovm.c || exit 1
60rm -f oovm.c
61cd $odir
62
63(cd /tmp; /tmp/oovm $d1) &
64(cd /tmp; /tmp/oovm $d2) &
65wait
66
67rm -f /tmp/oovm /tmp/oovm.core
68exit
69
70EOF
71#include <sys/param.h>
72#include <sys/fcntl.h>
73#include <sys/mman.h>
74#include <sys/stat.h>
75
76#include <err.h>
77#include <errno.h>
78#include <stdio.h>
79#include <stdlib.h>
80#include <time.h>
81#include <unistd.h>
82
83const char *file;
84
85#define RUNTIME 600
86
87void
88test(void)
89{
90	struct stat st;
91	size_t i, olen, len;
92	time_t start;
93	int error, fd, ps;
94	char *p;
95
96	ps = getpagesize();
97	if ((fd = open(file, O_RDWR)) == -1)
98		err(1, "open(%s)", file);
99	if ((error = fstat(fd, &st)) == -1)
100		err(1, "stat(%s)", file);
101	len = olen = round_page(st.st_size);
102	do {
103		if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED,
104		    fd, 0)) == MAP_FAILED) {
105			if (errno == ENOMEM)
106				len -= ps;
107			else
108				err(1, "mmap");
109		}
110	} while (p == MAP_FAILED);
111
112	start = time(NULL);
113	/* Touch all pages of the file. */
114	for (i = 0; i < len; i += ps)
115		p[i] = 1;
116	while (time(NULL) - start < RUNTIME)
117		p[arc4random() % len] = 1;
118
119	if (munmap(p, len) == -1)
120		err(1, "unmap()");
121	close(fd);
122}
123
124int
125main(int argc, char *argv[])
126{
127	if (argc != 2)
128		errx(1, "Usage: %s <file>", argv[0]);
129	file = argv[1];
130
131	test();
132
133	return (0);
134}
135