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