xref: /freebsd/tools/test/stress2/misc/mmap27.sh (revision 19261079)
1#!/bin/sh
2
3#
4# Copyright (c) 2016 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# 'WARNING: A device driver has set "memattr" inconsistently.' seen on
30# console.
31# https://people.freebsd.org/~pho/stress/log/mmap27.txt
32# Fixed by r298891.
33
34. ../default.cfg
35
36odir=`pwd`
37cd /tmp
38sed '1,/^EOF/d' < $odir/$0 > mmap27.c
39mycc -o mmap27 -Wall -Wextra -g -O0 mmap27.c || exit 1
40rm -f mmap27.c
41cd $odir
42
43daemon sh -c '(cd ../testcases/swap; ./swap -t 2m -i 20 -l 100)' > /dev/null 2>&1
44sleep 2
45/tmp/mmap27
46while pgrep -q swap; do
47	pkill -9 swap
48done
49rm -f ./mmap27 /tmp/mmap27.0* /tmp/mmap27
50exit 0
51
52EOF
53#include <sys/param.h>
54#include <sys/mman.h>
55#include <sys/wait.h>
56
57#include <err.h>
58#include <errno.h>
59#include <fcntl.h>
60#include <stdio.h>
61#include <stdlib.h>
62#include <string.h>
63#include <time.h>
64#include <unistd.h>
65
66int fd;
67
68#define ADRSPACE  (256 * 1024 * 1024 )
69#define PARALLEL 64
70#define RUNTIME 120
71#define STARTADDR 0x50000000U
72
73static void
74work(void)
75{
76	size_t left, len;
77	int i;
78	char *p;
79	volatile char val;
80
81	if ((fd = open("/dev/mem", O_RDWR)) == -1)
82		err(1,"open()");
83
84	p = (void *)STARTADDR + trunc_page(arc4random() % ADRSPACE);
85	left = ADRSPACE - (size_t)p + STARTADDR;
86	len = trunc_page(arc4random() % left) + PAGE_SIZE;
87
88	if ((p = mmap(p, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) ==
89			MAP_FAILED) {
90		if (errno == ENOMEM)
91			return;
92		err(1, "mmap()");
93	}
94
95	for (i = 0; i < 100; i++)
96		val = p[arc4random() % len];
97
98	if (munmap(p, len) == -1)
99		err(1, "munmap(%p)", p);
100
101	_exit(0);
102}
103
104int
105main(void)
106{
107	pid_t pids[PARALLEL];
108	time_t start;
109	int i, n;
110
111	start = time(NULL);
112	while (time(NULL) - start < RUNTIME) {
113		n = arc4random() % PARALLEL + 1;
114		for (i = 0; i < n; i++) {
115			if ((pids[i] = fork()) == 0)
116				work();
117		}
118
119		for (i = 0; i < n; i++)
120			if (waitpid(pids[i], NULL, 0) != pids[i])
121				err(1, "waitpid(%d)", pids[i]);
122	}
123	close(fd);
124
125	return (0);
126}
127