xref: /freebsd/tools/test/stress2/misc/kevent11.sh (revision 18054d0220cfc8df9c9568c437bd6fbb59d53c3c)
1#!/bin/sh
2
3#
4# Copyright (c) 2017 Dell EMC Isilon
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# Variation of kevent5.sh
30# OOM seen: https://people.freebsd.org/~pho/stress/log/mark018.txt
31
32. ../default.cfg
33[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1
34
35dir=/tmp
36odir=`pwd`
37cd $dir
38sed '1,/^EOF/d' < $odir/$0 > $dir/kevent11.c
39mycc -o kevent11 -Wall -Wextra -O0 -g kevent11.c || exit 1
40rm -f kevent11.c
41cd $odir
42
43set -e
44mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint
45[ -c /dev/md$mdstart ] &&  mdconfig -d -u $mdstart
46mdconfig -a -t swap -s 2g -u $mdstart
47bsdlabel -w md$mdstart auto
48newfs $newfs_flags md${mdstart}$part > /dev/null
49mount /dev/md${mdstart}$part $mntpoint
50set +e
51
52(cd $odir/../testcases/swap; ./swap -t 5m -i 20 -h -l 100 > /dev/null) &
53cd $mntpoint
54$dir/kevent11
55s=$?
56[ -f kevent11.core -a $s -eq 0 ] &&
57    { ls -l kevent11.core; mv kevent11.core /tmp; s=1; }
58cd $odir
59while pkill -9 swap; do :; done
60wait
61
62for i in `jot 6`; do
63	mount | grep -q "on $mntpoint " || break
64	umount $mntpoint && break || sleep 10
65done
66[ $i -eq 6 ] && exit 1
67mdconfig -d -u $mdstart
68rm -rf $dir/kevent11
69exit $s
70
71EOF
72#include <sys/param.h>
73#include <sys/event.h>
74#include <sys/mman.h>
75#include <sys/stat.h>
76#include <sys/wait.h>
77
78#include <machine/atomic.h>
79
80#include <err.h>
81#include <errno.h>
82#include <fcntl.h>
83#include <stdio.h>
84#include <stdlib.h>
85#include <string.h>
86#include <time.h>
87#include <unistd.h>
88
89static volatile u_int *share;
90static char *file = "file";
91
92#define PARALLEL 256
93#define RUNTIME (3 * 60)
94#define SYNC 0
95
96static void
97test(void)
98{
99        struct kevent ev[2];
100        struct timespec ts;
101        int kq, fd, n;
102
103	if ((fd = open(file, O_RDONLY, 0)) == -1)
104		err(1, "open(%s). %s:%d", file, __func__, __LINE__);
105
106	if ((kq = kqueue()) < 0)
107		err(1, "kqueue()");
108
109	n = 0;
110	EV_SET(&ev[n], fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR,
111		NOTE_DELETE, 0, 0);
112	n++;
113
114	if (kevent(kq, ev, n, NULL, 0, NULL) < 0)
115		err(1, "kevent()");
116
117	setproctitle("presync");
118	atomic_add_int(&share[SYNC], 1);
119	while (share[SYNC] != PARALLEL)
120		usleep(10000);
121	setproctitle("postsync");
122
123	memset(&ev, 0, sizeof(ev));
124	n = kevent(kq, NULL, 0, ev, 1, &ts);
125	close(fd);
126	close(kq);
127
128	_exit(0);
129}
130
131int
132main(void)
133{
134	pid_t pids[PARALLEL];
135	size_t len;
136	time_t start;
137	int e, fd, i, status;
138
139	e = 0;
140	len = PAGE_SIZE;
141	if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
142	    MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
143		err(1, "mmap");
144
145	start = time(NULL);
146	while ((time(NULL) - start) < RUNTIME && e == 0) {
147		if ((fd = open(file, O_CREAT | O_TRUNC | O_RDWR, 0660)) ==
148		    -1)
149			err(1, "open(%s)", file);
150		close(fd);
151		share[SYNC] = 0;
152		for (i = 0; i < PARALLEL; i++) {
153
154			if ((pids[i] = fork()) == 0)
155				test();
156			if (pids[i] == -1)
157				err(1, "fork()");
158		}
159		while (share[SYNC] != PARALLEL)
160			usleep(10000);
161		if (unlink(file) == -1)
162			err(1, "unlink(%s). %s:%d\n", file,
163				    __FILE__, __LINE__);
164		for (i = 0; i < PARALLEL; i++) {
165			if (waitpid(pids[i], &status, 0) == -1)
166				err(1, "waitpid(%d)", pids[i]);
167			if (status != 0) {
168				if (WIFSIGNALED(status))
169					fprintf(stderr,
170					    "pid %d exit signal %d\n",
171					    pids[i], WTERMSIG(status));
172			}
173			e += status == 0 ? 0 : 1;
174		}
175	}
176
177	return (e);
178}
179