xref: /freebsd/tools/test/stress2/misc/mmap7.sh (revision 43a5ec4eb41567cc92586503212743d89686d78f)
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# Test scenario inspired by alc@
30# Threaded version in order to "use the same pmap", as pointed out by kib@
31
32# https://people.freebsd.org/~pho/stress/log/kostik601.txt
33# Fixed by r255396
34
35. ../default.cfg
36
37dir=/tmp
38odir=`pwd`
39cd $dir
40sed '1,/^EOF/d' < $odir/$0 > $dir/wire_no_page.c
41mycc -o mmap7 -Wall -Wextra wire_no_page.c -lpthread || exit 1
42rm -f wire_no_page.c
43cd $odir
44
45(cd ../testcases/swap; ./swap -t 1m -i 2) &
46sleep 1
47cp /tmp/mmap7 /tmp/mmap7.inputfile
48/tmp/mmap7 /tmp/mmap7.inputfile
49while pkill -9 swap; do :; done
50wait
51rm -f /tmp/mmap7 /tmp/mmap7.inputfile
52exit
53
54EOF
55#include <err.h>
56#include <errno.h>
57#include <pthread.h>
58#include <stdio.h>
59#include <stdlib.h>
60#include <sys/fcntl.h>
61#include <sys/mman.h>
62#include <sys/param.h>
63#include <sys/stat.h>
64#include <sys/types.h>
65#include <sys/wait.h>
66#include <unistd.h>
67
68char *p1, *p2, *p3;
69char c;
70const char *file;
71int fd;
72size_t len;
73struct stat st;
74
75void *
76test2(void *arg __unused)
77{
78	int error, i;
79
80	p1[arc4random() % len] = 1;
81	p2[arc4random() % len] = 1;
82
83	if (arc4random() % 100 < 30)
84		i = p3[arc4random() % len];
85
86	if (arc4random() % 100 < 50)
87		if ((error = mlock(p1, len)) == -1)
88			err(1, "mlock");
89	if (arc4random() % 100 < 50)
90		if ((error = msync(p1, len, MS_SYNC | MS_INVALIDATE)) == -1)
91			if (errno != EBUSY)
92				err(1, "msync");
93	return (0);
94}
95
96void
97test(void)
98{
99	pthread_t cp[3];
100	int e, error, i;
101
102	if ((fd = open(file, O_RDWR)) == -1)
103		err(1, "open %s", file);
104	if ((error = fstat(fd, &st)) == -1)
105		err(1, "stat(%s)", file);
106	len = round_page(st.st_size);
107	if ((p1 = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED)
108		err(1, "mmap");
109	if ((p2 = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED)
110		err(1, "mmap");
111	if ((p3 = mmap(NULL, len, PROT_READ             , MAP_SHARED, fd, 0)) == MAP_FAILED)
112		err(1, "mmap");
113
114	for (i = 0; i < 3; i++)
115		if ((e = pthread_create(&cp[i], NULL, test2, NULL)) != 0)
116			errc(1, e, "pthread_create");
117	for (i = 0; i < 3; i++)
118		pthread_join(cp[i], NULL);
119
120	if (munmap(p3, len) == -1)
121		err(1, "unmap()");
122	if (munmap(p2, len) == -1)
123		err(1, "unmap()");
124	if (munmap(p1, len) == -1)
125		err(1, "unmap()");
126	close(fd);
127
128	_exit(0);
129}
130
131int
132main(int argc, char *argv[])
133{
134	int i;
135
136	if (argc != 2)
137		errx(1, "Usage: %s <file>", argv[0]);
138	file = argv[1];
139
140	for (i = 0; i < 30000; i++) {
141		if (fork() == 0)
142			test();
143		wait(NULL);
144	}
145
146	return (0);
147}
148