xref: /freebsd/tools/test/stress2/misc/kevent15.sh (revision dd41de95a84d979615a2ef11df6850622bf6184e)
1#!/bin/sh
2
3#
4# SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5#
6# Copyright (c) 2020 Peter Holm <pho@FreeBSD.org>
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27# SUCH DAMAGE.
28#
29
30# No problems seen.
31
32. ../default.cfg
33
34dir=/tmp
35odir=`pwd`
36cd $dir
37sed '1,/^EOF/d' < $odir/$0 > $dir/kevent15.c
38mycc -o kevent15 -Wall -Wextra -O0 -g kevent15.c || exit 1
39rm -f kevent15.c
40cd $odir
41
42(cd ../testcases/swap; ./swap -t 3m -i 20 -l 80) &
43sleep 2
44cd $dir
45timeout 5m ./kevent15
46s=$?
47while pkill swap; do sleep .1; done
48wait
49[ -f kevent15.core -a $s -eq 0 ] &&
50    { ls -l kevent15.core; s=1; }
51cd $odir
52
53rm -rf $dir/kevent15
54exit $s
55
56EOF
57#include <sys/param.h>
58#include <sys/event.h>
59#include <sys/mman.h>
60#include <sys/stat.h>
61#include <sys/wait.h>
62
63#include <err.h>
64#include <errno.h>
65#include <fcntl.h>
66#include <signal.h>
67#include <stdatomic.h>
68#include <stdio.h>
69#include <stdlib.h>
70#include <time.h>
71#include <unistd.h>
72
73static _Atomic(int) *share;
74
75#define PARALLEL 800
76#define RUNTIME (3 * 60)
77#define SYNC 0
78#define ACT 1
79
80static void
81handler(int i __unused) {
82	_exit(0);
83}
84
85static void
86test(void)
87{
88	struct kevent ev[2];
89	struct timespec ts;
90	time_t start;
91	int kq, ret;
92
93	signal(SIGUSR1, handler);
94	(void)atomic_fetch_add(&share[SYNC], 1);
95	while (atomic_load(&share[SYNC]) != PARALLEL)
96		usleep(1);
97	(void)atomic_fetch_add(&share[ACT], 1);
98
99	if ((kq = kqueue()) < 0)
100		err(1, "kqueue");
101
102	EV_SET(&ev[0], 42, EVFILT_TIMER, EV_ADD | EV_ENABLE | EV_ONESHOT, 0,
103	    0, 0);
104
105	start = time(NULL);
106	while (time(NULL) - start < 30) {
107		ts.tv_sec  = 0;
108		ts.tv_nsec = arc4random() % 2000;
109		if ((ret = kevent(kq, &ev[0], 1, &ev[1], 1, &ts)) == -1)
110			err(1, "kevent");
111		if (ret == 1 && (ev[1].flags & EV_ERROR) != 0)
112			errc(1, ev[1].data, "kevent");
113	}
114
115	_exit(0);
116}
117
118int
119main(void)
120{
121	pid_t pids[PARALLEL];
122	size_t len;
123	time_t start;
124	int i, status;
125
126	len = PAGE_SIZE;
127	if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE,
128	    MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED)
129		err(1, "mmap");
130
131	start = time(NULL);
132	while ((time(NULL) - start) < RUNTIME) {
133		share[ACT] = share[SYNC] = 0;
134		for (i = 0; i < PARALLEL; i++) {
135			if ((pids[i] = fork()) == 0)
136				test();
137			if (pids[i] == -1)
138				err(1, "fork()");
139		}
140		while (share[ACT] != PARALLEL)
141			usleep(1);
142		usleep(arc4random() % 50000);
143		for (i = 0; i < PARALLEL; i++) {
144			if (kill(pids[i], SIGUSR1) == -1)
145				err(1, "kill");
146		}
147
148		for (i = 0; i < PARALLEL; i++) {
149			if (waitpid(pids[i], &status, 0) == -1)
150				err(1, "waitpid(%d)", pids[i]);
151			if (status != 0) {
152				if (WIFSIGNALED(status) && WTERMSIG(status) != 2)
153					fprintf(stderr,
154					    "pid %d exit signal %d\n",
155					    pids[i], WTERMSIG(status));
156			}
157		}
158	}
159
160	return (0);
161}
162