xref: /freebsd/tools/test/stress2/misc/oovm2.sh (revision 8a272653d9fbd9fc37691c9aad6a05089b4ecb4d)
1*8a272653SPeter Holm#!/bin/sh
2*8a272653SPeter Holm
3*8a272653SPeter Holm#
4*8a272653SPeter Holm# Copyright (c) 2015 EMC Corp.
5*8a272653SPeter Holm# All rights reserved.
6*8a272653SPeter Holm#
7*8a272653SPeter Holm# Redistribution and use in source and binary forms, with or without
8*8a272653SPeter Holm# modification, are permitted provided that the following conditions
9*8a272653SPeter Holm# are met:
10*8a272653SPeter Holm# 1. Redistributions of source code must retain the above copyright
11*8a272653SPeter Holm#    notice, this list of conditions and the following disclaimer.
12*8a272653SPeter Holm# 2. Redistributions in binary form must reproduce the above copyright
13*8a272653SPeter Holm#    notice, this list of conditions and the following disclaimer in the
14*8a272653SPeter Holm#    documentation and/or other materials provided with the distribution.
15*8a272653SPeter Holm#
16*8a272653SPeter Holm# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17*8a272653SPeter Holm# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*8a272653SPeter Holm# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*8a272653SPeter Holm# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20*8a272653SPeter Holm# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*8a272653SPeter Holm# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*8a272653SPeter Holm# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*8a272653SPeter Holm# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*8a272653SPeter Holm# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*8a272653SPeter Holm# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*8a272653SPeter Holm# SUCH DAMAGE.
27*8a272653SPeter Holm#
28*8a272653SPeter Holm
29*8a272653SPeter Holm# Out of VM deadlock seen. Introduced by r285808. Variation of oovm.sh
30*8a272653SPeter Holm# https://people.freebsd.org/~pho/stress/log/oovm2.txt
31*8a272653SPeter Holm
32*8a272653SPeter Holm# Fixed by r290047 and <alc's PQ_LAUNDRY patch>
33*8a272653SPeter Holm
34*8a272653SPeter Holm# Test scenario suggestion by alc@
35*8a272653SPeter Holm
36*8a272653SPeter Holm. ../default.cfg
37*8a272653SPeter Holm
38*8a272653SPeter Holm[ `swapinfo | wc -l` -eq 1 ] && exit 0
39*8a272653SPeter Holmmaxsize=$((2 * 1024)) # Limit size due to runtime reasons
40*8a272653SPeter Holmsize=$((`sysctl -n hw.physmem` / 1024 / 1024))
41*8a272653SPeter Holm[ $size -gt $maxsize ] && size=$maxsize
42*8a272653SPeter Holmd1=$diskimage.1
43*8a272653SPeter Holmd2=$diskimage.2
44*8a272653SPeter Holmd3=$diskimage.3
45*8a272653SPeter Holmd4=$diskimage.4
46*8a272653SPeter Holmrm -f $d1 $d2 $d3 $d4
47*8a272653SPeter Holm[ `df -k $(dirname $diskimage) | tail -1 | awk '{print int($4 / 1024)}'` -lt \
48*8a272653SPeter Holm    $size ] && printf "Need %d MB on %s.\n" $size `dirname $diskimage` && exit
49*8a272653SPeter Holmdd if=/dev/zero of=$d1 bs=1m count=$((size / 4)) status=none
50*8a272653SPeter Holmcp $d1 $d2 || exit
51*8a272653SPeter Holmcp $d1 $d3 || exit
52*8a272653SPeter Holmcp $d1 $d4 || exit
53*8a272653SPeter Holmtrap "rm -f $d1 $d2 $d3 $d4" EXIT INT
54*8a272653SPeter Holm
55*8a272653SPeter Holmdir=/tmp
56*8a272653SPeter Holmodir=`pwd`
57*8a272653SPeter Holmcd $dir
58*8a272653SPeter Holmsed '1,/^EOF/d' < $odir/$0 > $dir/oovm2.c
59*8a272653SPeter Holmmycc -o oovm2 -Wall -Wextra -g oovm2.c || exit 1
60*8a272653SPeter Holmrm -f oovm2.c
61*8a272653SPeter Holmcd $odir
62*8a272653SPeter Holm
63*8a272653SPeter Holm(cd /tmp; /tmp/oovm2 $d1) &
64*8a272653SPeter Holm(cd /tmp; /tmp/oovm2 $d2) &
65*8a272653SPeter Holm(cd /tmp; /tmp/oovm2 $d3) &
66*8a272653SPeter Holm(cd /tmp; /tmp/oovm2 $d4) &
67*8a272653SPeter Holmwait
68*8a272653SPeter Holm
69*8a272653SPeter Holmrm -f /tmp/oovm2 /tmp/oovm2.core
70*8a272653SPeter Holmexit
71*8a272653SPeter Holm
72*8a272653SPeter HolmEOF
73*8a272653SPeter Holm#include <sys/param.h>
74*8a272653SPeter Holm#include <sys/fcntl.h>
75*8a272653SPeter Holm#include <sys/mman.h>
76*8a272653SPeter Holm#include <sys/stat.h>
77*8a272653SPeter Holm
78*8a272653SPeter Holm#include <err.h>
79*8a272653SPeter Holm#include <errno.h>
80*8a272653SPeter Holm#include <stdio.h>
81*8a272653SPeter Holm#include <stdlib.h>
82*8a272653SPeter Holm#include <time.h>
83*8a272653SPeter Holm#include <unistd.h>
84*8a272653SPeter Holm
85*8a272653SPeter Holmconst char *file;
86*8a272653SPeter Holm
87*8a272653SPeter Holm#define RUNTIME 600
88*8a272653SPeter Holm
89*8a272653SPeter Holmvoid
90*8a272653SPeter Holmtest(void)
91*8a272653SPeter Holm{
92*8a272653SPeter Holm	struct stat st;
93*8a272653SPeter Holm	size_t i, olen, len;
94*8a272653SPeter Holm	time_t start;
95*8a272653SPeter Holm	int error, fd, ps;
96*8a272653SPeter Holm	char *p;
97*8a272653SPeter Holm
98*8a272653SPeter Holm	ps = getpagesize();
99*8a272653SPeter Holm	if ((fd = open(file, O_RDWR)) == -1)
100*8a272653SPeter Holm		err(1, "open(%s)", file);
101*8a272653SPeter Holm	if ((error = fstat(fd, &st)) == -1)
102*8a272653SPeter Holm		err(1, "stat(%s)", file);
103*8a272653SPeter Holm	len = olen = round_page(st.st_size);
104*8a272653SPeter Holm	do {
105*8a272653SPeter Holm		if ((p = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED,
106*8a272653SPeter Holm		    fd, 0)) == MAP_FAILED) {
107*8a272653SPeter Holm			if (errno == ENOMEM)
108*8a272653SPeter Holm				len -= ps;
109*8a272653SPeter Holm			else
110*8a272653SPeter Holm				err(1, "mmap");
111*8a272653SPeter Holm		}
112*8a272653SPeter Holm	} while (p == MAP_FAILED);
113*8a272653SPeter Holm
114*8a272653SPeter Holm	start = time(NULL);
115*8a272653SPeter Holm	/* Touch all pages of the file. */
116*8a272653SPeter Holm	for (i = 0; i < len; i += ps)
117*8a272653SPeter Holm		p[i] = 1;
118*8a272653SPeter Holm	while (time(NULL) - start < RUNTIME)
119*8a272653SPeter Holm		p[arc4random() % len] = 1;
120*8a272653SPeter Holm
121*8a272653SPeter Holm	if (munmap(p, len) == -1)
122*8a272653SPeter Holm		err(1, "unmap()");
123*8a272653SPeter Holm	close(fd);
124*8a272653SPeter Holm}
125*8a272653SPeter Holm
126*8a272653SPeter Holmint
127*8a272653SPeter Holmmain(int argc, char *argv[])
128*8a272653SPeter Holm{
129*8a272653SPeter Holm	if (argc != 2)
130*8a272653SPeter Holm		errx(1, "Usage: %s <file>", argv[0]);
131*8a272653SPeter Holm	file = argv[1];
132*8a272653SPeter Holm
133*8a272653SPeter Holm	test();
134*8a272653SPeter Holm
135*8a272653SPeter Holm	return (0);
136*8a272653SPeter Holm}
137