From nobody Sat Oct 18 07:59:22 2025 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4cpYxQ49d4z6ChNn; Sat, 18 Oct 2025 07:59:22 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R12" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4cpYxQ3fnqz3nCj; Sat, 18 Oct 2025 07:59:22 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1760774362; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=duTmT0lsFuNe9Q6aHNoLC/kobq3TuqmLrrISYgLiAa4=; b=k+dYouDUHzJvi68KrIg/tF9F+i5wBg2Ug8Kj4r0pmw5Ej8UxsuL3gJok1iYuywreLAp6LK nzEQLhheOlHEW5J7G1wZXlqlICvDCS2/+I1e3pdMUgMog1ihIpyMSYOrf56KOZsUszT5B8 Y8UKPtS7hYW3K+JPzqhoT1kk2m9m1GhJ8y6WmaZQDsYfhJemVTxEs/R6RLKurX/8o+wh/u r1owDT6cyEm06Ow+228Nleu8UB6wMBm37VrBEFUrAY9kevyn3ncCXxAbHDEGIh4HHtiKkA 7KmxWCkd637T6RCgh22/Wig6pmt2hSVr0pg1v5h6FRldAJ0mlCHfXqhColxv9g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1760774362; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=duTmT0lsFuNe9Q6aHNoLC/kobq3TuqmLrrISYgLiAa4=; b=PFyXbwHS9M6psXlP9uMgfQCi/ZRlBmro3KfYofIxbpEYNNHQWZw8YpDwe/1TMFrmrZhM1B b73kvN74zVXtHgK4IG2cUmHyP/9z6cVfQCyWPfH1bD12u2ToKggXUyXq4YzlZa8mcxTAxP 3epkdBTdRH7bnOPpPTHNtzs2SJfabYg3WuNomDO3FAGy1+oqcaXE2fqDRvh+KZ4qXjPxK1 RzXxMiPtoDjhAk4WyCnQedKfF/Pe1qZmeT3zk9mPovwr6OkQ3d6Pw8T6EE6UhS/k3EuO8/ oGw7xbVJIfVqcWCnmpNaGxuu403e3+PQz2A3UkQLlNPU12oDfOT5kTrXr+CN3w== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1760774362; a=rsa-sha256; cv=none; b=KMyKeIyLUf2vmSL0kb0XFg5IoA+3XlcQ7r5Xca49uGkxUPV2xC2hglITlQbsqEpI5nH47G dLbuQQjLxrs+t0de9oZoV3UrqA5D1Y7FdMK5tCUGEjS0r9ZGiqR8qApNBGN+Ca24Nfrvrc 1PFd/iQo2IhjKG35J/Smcu7Rlmf//BSMrTUL8LzJcnoW/xkWOQvvVslWfHwoJ4SaLtijsk Chu33taJ9dhtuNQUnaDTmWZxJz+9JllPa+asJeO9pk23lFE4bBS2p5rm+EDGEI8kYarfqi jLXGfhpEINbAh+yMSW4e/d4LDFDElsMR+k+lNPLRom4Y0grbjWh5goQydnASmA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4cpYxQ2YK2zYrl; Sat, 18 Oct 2025 07:59:22 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.18.1/8.18.1) with ESMTP id 59I7xMTn062930; Sat, 18 Oct 2025 07:59:22 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.18.1/8.18.1/Submit) id 59I7xMK5062927; Sat, 18 Oct 2025 07:59:22 GMT (envelope-from git) Date: Sat, 18 Oct 2025 07:59:22 GMT Message-Id: <202510180759.59I7xMK5062927@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Peter Holm Subject: git: d7ff2ded4748 - main - stress2: Added kqueuex(KQUEUE_CPONFORK) test scenarios List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: pho X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: d7ff2ded474871d69f19b0d03deed5f66d78435e Auto-Submitted: auto-generated The branch main has been updated by pho: URL: https://cgit.FreeBSD.org/src/commit/?id=d7ff2ded474871d69f19b0d03deed5f66d78435e commit d7ff2ded474871d69f19b0d03deed5f66d78435e Author: Peter Holm AuthorDate: 2025-10-18 07:58:50 +0000 Commit: Peter Holm CommitDate: 2025-10-18 07:58:50 +0000 stress2: Added kqueuex(KQUEUE_CPONFORK) test scenarios --- tools/test/stress2/misc/kevent17.sh | 176 +++++++++++++++++++++++++++++++++ tools/test/stress2/misc/kevent18.sh | 152 +++++++++++++++++++++++++++++ tools/test/stress2/misc/kevent19.sh | 187 ++++++++++++++++++++++++++++++++++++ 3 files changed, 515 insertions(+) diff --git a/tools/test/stress2/misc/kevent17.sh b/tools/test/stress2/misc/kevent17.sh new file mode 100755 index 000000000000..e7b8f1a0a00c --- /dev/null +++ b/tools/test/stress2/misc/kevent17.sh @@ -0,0 +1,176 @@ +#!/bin/sh + +# +# Copyright (c) 2025 Peter Holm +# +# SPDX-License-Identifier: BSD-2-Clause +# + +# A kqueuex(KQUEUE_CPONFORK) test scenario +# Test scenario suggestion by: kib + +. ../default.cfg +[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1 + +dir=/tmp +odir=`pwd` +prog=$(basename "$0" .sh) +cd $dir +sed '1,/^EOF/d' < $odir/$0 > $dir/$prog.c +mycc -o $prog -Wall -Wextra -O0 -g $prog.c || exit 1 +rm -f $prog.c +cd $odir + +set -e +mount | grep "on $mntpoint " | grep -q /dev/md && umount -f $mntpoint +[ -c /dev/md$mdstart ] && mdconfig -d -u $mdstart +mdconfig -a -t swap -s 2g -u $mdstart +newfs $newfs_flags md$mdstart > /dev/null +mount /dev/md$mdstart $mntpoint +set +e + +(cd $odir/../testcases/swap; ./swap -t 5m -i 20 -h -l 100 > /dev/null) & +cd $mntpoint +$dir/$prog +s=$? +[ -f $prog.core -a $s -eq 0 ] && + { ls -l $prog.core; mv $prog.core /tmp; s=1; } +cd $odir +while pkill -9 swap; do :; done +wait + +for i in `jot 6`; do + mount | grep -q "on $mntpoint " || break + umount $mntpoint && break || sleep 10 +done +[ $i -eq 6 ] && exit 1 +mdconfig -d -u $mdstart +rm -rf $dir/$prog +exit $s + +EOF +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +static volatile u_int *share; +static int loops; +static char *file = "file"; + +#define MAXLOOPS 100 +#define PARALLEL 1 +#define RUNTIME (2 * 60) +#define SYNC 0 + +static void +test(void) +{ + pid_t pid; + struct kevent ev[2]; + struct timespec ts; + int kq, fd, n; + + if ((fd = open(file, O_RDONLY, 0)) == -1) + err(1, "open(%s). %s:%d", file, __func__, __LINE__); + + atomic_add_int(&share[SYNC], 1); + while (share[SYNC] != PARALLEL) + usleep(10000); + + if ((kq = kqueuex(KQUEUE_CPONFORK)) < 0) + err(1, "kqueuex"); + + ts.tv_sec = 5; + ts.tv_nsec = 0; + n = 0; + EV_SET(&ev[n], fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR, + NOTE_DELETE, 0, 0); + n++; + + if (kevent(kq, ev, n, NULL, 0, NULL) < 0) + err(1, "kevent()"); + if (loops >= MAXLOOPS) { /* start using fork(2) */ + if ((pid = fork()) == 0) { + n = kevent(kq, NULL, 0, ev, 1, &ts); + if (n == -1) + err(1, "kevent() in fork\n"); + close(fd); + close(kq); + _exit(0); + } + if (waitpid(pid, NULL, 0) != pid) + err(1, "waitpid(%d)\n", pid); + } + + n = kevent(kq, NULL, 0, ev, 1, &ts); + if (n == -1) + err(1, "kevent()"); + close(fd); + close(kq); + + _exit(0); +} + +int +main(void) +{ + pid_t pids[PARALLEL]; + size_t len; + time_t start; + int e, fd, i, status; + + e = 0; + len = PAGE_SIZE; + loops = 0; + if ((share = mmap(NULL, len, PROT_READ | PROT_WRITE, + MAP_ANON | MAP_SHARED, -1, 0)) == MAP_FAILED) + err(1, "mmap"); + + start = time(NULL); + while ((time(NULL) - start) < RUNTIME && e == 0) { + loops++; + if ((fd = open(file, O_CREAT | O_TRUNC | O_RDWR, 0660)) == + -1) + err(1, "open(%s)", file); + close(fd); + share[SYNC] = 0; + for (i = 0; i < PARALLEL; i++) { + + if ((pids[i] = fork()) == 0) + test(); + if (pids[i] == -1) + err(1, "fork()"); + } + while (share[SYNC] != PARALLEL) + usleep(10000); + if (unlink(file) == -1) + err(1, "unlink(%s). %s:%d\n", file, + __FILE__, __LINE__); + for (i = 0; i < PARALLEL; i++) { + if (waitpid(pids[i], &status, 0) == -1) + err(1, "waitpid(%d)", pids[i]); + if (status != 0) { + if (WIFSIGNALED(status)) + fprintf(stderr, + "pid %d exit signal %d\n", + pids[i], WTERMSIG(status)); + } + e += status == 0 ? 0 : 1; + } + } + + return (e); +} diff --git a/tools/test/stress2/misc/kevent18.sh b/tools/test/stress2/misc/kevent18.sh new file mode 100755 index 000000000000..1492c49e2921 --- /dev/null +++ b/tools/test/stress2/misc/kevent18.sh @@ -0,0 +1,152 @@ +#!/bin/sh + +# +# Copyright (c) 2025 Peter Holm +# +# SPDX-License-Identifier: BSD-2-Clause +# + +# A kqueuex(KQUEUE_CPONFORK) test scenario + +# Sleeping thread seen in WiP code: +# https://people.freebsd.org/~pho/stress/log/log0615.txt + +. ../default.cfg + +[ `id -u ` -ne 0 ] && echo "Must be root!" && exit 1 +ulimit -k 5000 || { echo FAIL; exit 1; } + +odir=`pwd` +prog=$(basename "$0" .sh) + +cd /tmp +sed '1,/^EOF/d' < $odir/$0 > $prog.c +mycc -o $prog -Wall -Wextra -O2 -g $prog.c -lpthread || exit 1 +rm -f $prog.c +cd $odir + +mount | grep "on $mntpoint " | grep -q md$mdstart && umount -f $mntpoint +mdconfig -l | grep -q $mdstart && mdconfig -d -u $mdstart + +mdconfig -a -t swap -s 2g -u $mdstart +newfs $newfs_flags md$mdstart > /dev/null +mount /dev/md$mdstart $mntpoint +chmod 777 $mntpoint + +su $testuser -c "(cd $mntpoint; /tmp/$prog)" & +for i in `jot 99`; do + sleep 1 + kill -0 $! 2>/dev/null || break +done +pkill $prog +wait +umount -f $mntpoint + +while mount | grep -q $mntpoint; do + umount $mntpoint || sleep 1 +done +mdconfig -d -u $mdstart +rm -f /tmp/$prog + +exit 0 +EOF +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define PARALLEL 64 + +static int fd; +static char path[80]; + +static void * +spin(void *arg __unused) +{ + int i; + + for (i= 0;; i++) { + snprintf(path, sizeof(path), "file.%06d.%d", getpid(), i); + fd = open(path, O_CREAT | O_TRUNC | O_RDWR, 0622); + if (fd == -1 && errno == ENOTDIR) + break; + if (fd == -1) + err(1, "open(%s)", path); + close(fd); + fd = 0; + unlink(path); + } + fprintf(stderr, "spin loops: %d\n", i + 1); + return (NULL); +} + +static void * +test(void *arg __unused) +{ + struct kevent ev; + struct timespec ts; + pid_t pid; + int i, kq, n; + + for (i = 0; i < 500000; i++) { + if ((kq = kqueuex(KQUEUE_CPONFORK)) < 0) + err(1, "kqueueex(KQUEUE_CPONFORK)"); + + n = 0; + memset(&ev, 0, sizeof(ev)); + EV_SET(&ev, fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR, + NOTE_DELETE|NOTE_RENAME|NOTE_EXTEND, 0, 0); + n++; + + if ((pid = fork()) == 0) { + kevent(kq, &ev, n, NULL, 0, NULL); + _exit(0); + } + if (waitpid(pid, NULL, 0) != pid) + err(1, "waitpid(%d)", pid); + + kevent(kq, &ev, n, NULL, 0, NULL); + memset(&ev, 0, sizeof(ev)); + ts.tv_sec = 0; + ts.tv_nsec = 1000000; + if ((n = kevent(kq, NULL, 0, &ev, 1, &ts)) == -1) + err(1, "kevent()"); + + close(kq); + } + return (NULL); +} + +int +main(void) +{ + pthread_t cp[PARALLEL], sp; + int e, i; + + if ((e = pthread_create(&sp, NULL, spin, NULL)) != 0) + errc(1, e, "pthread_create"); + + for (i = 0; i < PARALLEL; i++) { + if ((e = pthread_create(&cp[i], NULL, test, NULL)) != 0) + errc(1, e, "pthread_create"); + } + + for (i = 0; i < PARALLEL; i++) + pthread_join(cp[i], NULL); + pthread_join(sp, NULL); + + close(fd); + + return (0); +} diff --git a/tools/test/stress2/misc/kevent19.sh b/tools/test/stress2/misc/kevent19.sh new file mode 100755 index 000000000000..0170fe0d93c8 --- /dev/null +++ b/tools/test/stress2/misc/kevent19.sh @@ -0,0 +1,187 @@ +#!/bin/sh + +# A kqueuex(KQUEUE_CPONFORK) test scenario + +set -u +prog=$(basename "$0" .sh) + +cat > /tmp/$prog.c < +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef KQUEUE_CPONFORK +#define KQUEUE_CPONFORK 0x2 +#endif + +static pid_t pid_pipe_beat; +static pid_t pid_controller; + +static void +sighup_handler(int sig __unused) +{ + kill(pid_pipe_beat, SIGKILL); + _exit(1); +} + +static void +pipe_beat(int wp) +{ + static const char a[1] = { 'a' }; + ssize_t s; + + for (;;) { + sleep(1); + s = write(wp, a, 1); + if (s < 0) + err(1, "pipe write"); + if (s == 0) + errx(1, "short pipe write"); + } +} + +static void +worker(int kq, int rp) +{ + struct kevent ev[1]; + char a[1]; + ssize_t s; + int n; + + for (;;) { + n = kevent(kq, NULL, 0, ev, nitems(ev), NULL); + if (n == -1) { + kill(pid_controller, SIGHUP); + err(1, "kqueue"); + } + if (n == 0) + continue; // XXXKIB + switch (ev[0].filter) { + case EVFILT_TIMER: + printf("tick\n"); + break; + case EVFILT_READ: + if (ev[0].ident != (uintptr_t)rp) { + kill(pid_controller, SIGHUP); + errx(1, "unknown read ident %d\n", (int)ev[0].ident); + } + s = read(rp, a, sizeof(a)); + if (s == -1) { + kill(pid_controller, SIGHUP); + err(1, "read"); + } + if (s == 0) { + kill(pid_controller, SIGHUP); + errx(1, "EOF"); + } + printf("%c\n", a[0]); + break; + default: + kill(pid_controller, SIGHUP); + errx(1, "unknown fiter %d\n", ev[0].filter); + break; + } + } +} + +static void +usage(void) +{ + fprintf(stderr, "Usage: kqfork [fork]\n"); + exit(2); +} + +int +main(int argc, char *argv[]) +{ + struct kevent ev[2]; + struct sigaction sa; + int kq, n, pp[2]; + pid_t pid_worker; + bool do_fork; + + do_fork = false; + if (argc != 1 && argc != 2) + usage(); + if (argc == 2) { + if (strcmp(argv[1], "fork") != 0) + usage(); + do_fork = true; + } + + memset(&sa, 0, sizeof(sa)); + sa.sa_handler = sighup_handler; + if (sigaction(SIGHUP, &sa, NULL) == -1) + err(1, "sigaction(SIGHUP)"); + + memset(&sa, 0, sizeof(sa)); + sa.sa_flags = SA_NOCLDWAIT | SA_NOCLDSTOP; + sa.sa_handler = SIG_IGN; + if (sigaction(SIGCHLD, &sa, NULL) == -1) + err(1, "sigaction(SIGCHLD)"); + + if (pipe(pp) == -1) + err(1, "pipe"); + + pid_pipe_beat = fork(); + if (pid_pipe_beat == -1) + err(1, "fork"); + if (pid_pipe_beat == 0) { + close(pp[0]); + pipe_beat(pp[1]); + } + + kq = kqueuex(do_fork ? KQUEUE_CPONFORK : 0); + if (kq == -1) + err(1, "kqueuex"); + + EV_SET(&ev[0], 1, EVFILT_TIMER, EV_ADD, NOTE_SECONDS, 1, NULL); + EV_SET(&ev[1], pp[0], EVFILT_READ, EV_ADD, 0, 0, NULL); + n = kevent(kq, ev, nitems(ev), NULL, 0, NULL); + if (n == -1) { + kill(pid_pipe_beat, SIGKILL); + err(1, "kevent reg"); + } + if (n != 0) { + kill(pid_pipe_beat, SIGKILL); + errx(1, "kevent reg %d", n); + } + + pid_controller = getpid(); + + if (do_fork) { + pid_worker = fork(); + if (pid_worker == -1) { + kill(pid_pipe_beat, SIGKILL); + err(1, "fork"); + } + if (pid_worker == 0) { + close(pp[1]); + worker(kq, pp[0]); + } + + for (;;) + pause(); + } else { + worker(kq, pp[0]); + } + exit(0); // unreachable +} +EOF +cc -o /tmp/$prog -Wall -Wextra -O2 /tmp/$prog.c || exit 1 + +echo "--> No fork" +timeout 4s /tmp/$prog +echo "--> fork" +timeout 4s /tmp/$prog fork + +rm -f /tmp/$prog.c $prog +exit 0