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