interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / kernel / tests / test_pipe_irq.c

109 lines · 3842 bytes Raw Blame History
1/* test_pipe_irq.c -- race test for DRA-556: a pipe op reached from an interrupt on the CPU that holds the pool lock.
2 *
3 * Uses only the public pipe API (pipe.h), so the same file builds against the tree before and after DRA-556.
4 * On a host a thread's interrupt is SIGUSR1 (core/spinlock.h, CK_SPIN_HOST_IRQ_SIGNAL). A periodic POSIX timer
5 * (CLOCK_MONOTONIC, every 20 us) raises SIGUSR1 at the main thread itself (SIGEV_THREAD_ID), so it lands wherever
6 * the main thread is, asynchronously, on any number of CPUs. The main thread calls ck_pipe_pool_empty in a loop (it
7 * holds the pool lock while it scans every pipe); the handler calls ck_pipe_pool_empty too.
8 * before (private ticket lock, interrupts open): a tick lands inside the lock, the handler takes the next ticket and
9 * spins forever on its own thread -> no progress -> "test_pipe_irq: FAIL deadlock".
10 * after (shared spinlock, interrupts masked while held): the tick waits for the unlock -> PASS.
11 * PASS also needs at least MIN_HANDLED interrupts handled, so a run where the timer barely fired is a FAIL, not a
12 * pass. The watchdog thread (SIGUSR1 blocked) is an alarm on stalled progress, not a cap on the run.
13 */
14#define _GNU_SOURCE
15#include <pthread.h>
16#include <signal.h>
17#include <stdint.h>
18#include <stdio.h>
19#include <string.h>
20#include <sys/syscall.h>
21#include <time.h>
22#include <unistd.h>
23
24#include "pipe.h"
25
26#define ROUNDS 200000u
27#define MIN_HANDLED 1000u
28#define TICK_NS 20000L
29#define STALL_S 5
30
31static struct ck_pipe_pool pool;
32static volatile uint64_t progress, handled;
33static volatile int done;
34
35static void on_irq(int sig)
36{
37 (void)sig;
38 (void)ck_pipe_pool_empty(&pool);
39 handled++;
40 __atomic_add_fetch(&progress, 1, __ATOMIC_RELAXED);
41}
42
43static void *watchdog(void *arg)
44{
45 (void)arg;
46 uint64_t last = (uint64_t)-1;
47 int still = 0;
48 while (!__atomic_load_n(&done, __ATOMIC_ACQUIRE)) {
49 sleep(1);
50 uint64_t now = __atomic_load_n(&progress, __ATOMIC_RELAXED);
51 still = now == last ? still + 1 : 0;
52 last = now;
53 if (still >= STALL_S) {
54 printf("test_pipe_irq: FAIL deadlock: no progress for %d s (handled=%llu)\n", STALL_S,
55 (unsigned long long)handled);
56 fflush(stdout);
57 _exit(1);
58 }
59 }
60 return 0;
61}
62
63int main(void)
64{
65 ck_pipe_pool_init(&pool, 0);
66 struct sigaction sa;
67 memset(&sa, 0, sizeof sa);
68 sa.sa_handler = on_irq;
69 sigemptyset(&sa.sa_mask);
70 sa.sa_flags = SA_RESTART;
71 if (sigaction(SIGUSR1, &sa, 0) != 0)
72 return 2;
73
74 /* The watchdog never takes the interrupt: block SIGUSR1 before creating it (it inherits the mask). */
75 sigset_t one;
76 sigemptyset(&one);
77 sigaddset(&one, SIGUSR1);
78 pthread_sigmask(SIG_BLOCK, &one, 0);
79 pthread_t wd;
80 if (pthread_create(&wd, 0, watchdog, 0) != 0)
81 return 2;
82 pthread_sigmask(SIG_UNBLOCK, &one, 0);
83
84 struct sigevent ev;
85 memset(&ev, 0, sizeof ev);
86 ev.sigev_notify = SIGEV_THREAD_ID;
87 ev.sigev_signo = SIGUSR1;
88 ev._sigev_un._tid = (pid_t)syscall(SYS_gettid);
89 timer_t tm;
90 if (timer_create(CLOCK_MONOTONIC, &ev, &tm) != 0)
91 return 2;
92 struct itimerspec it = { { 0, TICK_NS }, { 0, TICK_NS } };
93 if (timer_settime(tm, 0, &it, 0) != 0)
94 return 2;
95
96 int bad = 0;
97 for (unsigned r = 0; r < ROUNDS; r++) {
98 if (!ck_pipe_pool_empty(&pool))
99 bad++;
100 __atomic_add_fetch(&progress, 1, __ATOMIC_RELAXED);
101 }
102 timer_delete(tm);
103 __atomic_store_n(&done, 1, __ATOMIC_RELEASE);
104 pthread_join(wd, 0);
105 int ok = bad == 0 && handled >= MIN_HANDLED;
106 printf("test_pipe_irq: %s rounds=%u handled=%llu (min %u) bad=%d\n", ok ? "PASS" : "FAIL", ROUNDS,
107 (unsigned long long)handled, MIN_HANDLED, bad);
108 return ok ? 0 : 1;
109}