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 / scripts / ck_courier_udp_test.sh

97 lines · 4641 bytes Raw Blame History
1#!/usr/bin/env bash
2# ck_courier_udp_test.sh -- host test of Allen's Ethernet courier.
3#
4# The kernel's own courier code (native/kernel/x86/courier_udp_x86.c, built with
5# -DCK_COURIER_HOST) talks UDP to the real relay (crates/allen-courier --udp),
6# which asks a model server. Five runs: a clean wire; wires that lose about 1 frame
7# in 3 and 1 in 2 each way; a model server that answers "busy" (503) twice before
8# it answers; and one that sits silent for 4 s before it starts (the relay must
9# keep sending WAIT meanwhile). The model here is a small stand-in so the run is
10# exact and quick. With ALLEN_MODEL=host:port (the real server) a last run
11# sends the same job to the real model. The real kernel cannot run this in QEMU
12# (QEMU has no Broadcom chip), so this is the check of the courier's protocol.
13# Ends with "AIENOS_COURIER_UDP: PASS" or "AIENOS_COURIER_UDP: FAIL <why>".
14set -u
15cd "$(dirname "$0")/.."
16K=native/kernel
17tag=AIENOS_COURIER_UDP
18fail() { echo "${tag}: FAIL $*"; exit 1; }
19W=$(mktemp -d "${TMPDIR:-/tmp}/ck_courier_udp.XXXXXX")
20pids=()
21cleanup() {
22 for p in ${pids[@]+"${pids[@]}"}; do kill "$p" 2>/dev/null || true; done
23 [ -n "${KEEP_LOGS:-}" ] && cp -r "$W" "${KEEP_LOGS}" 2>/dev/null
24 rm -rf "$W"
25}
26trap cleanup EXIT
27
28${HOSTCC:-cc} -std=c11 -D_DEFAULT_SOURCE -O2 -Wall -Wextra -Werror -DCK_COURIER_HOST -I$K/x86 -Inative/net \
29 -o "$W/test" $K/x86/tests/courier_udp_test.c $K/x86/courier_udp_x86.c native/net/aienos_net.c ||
30 fail "the test does not build"
31if [ -n "${ALLEN_RELAY:-}" ]; then
32 relay="$ALLEN_RELAY" # a relay built elsewhere
33else
34 cargo build -q --release --offline -p allen-courier 2>"$W/cargo.err" || { tail -5 "$W/cargo.err"; fail "the relay does not build"; }
35 relay="${CARGO_TARGET_DIR:-target}/release/allen-courier"
36fi
37
38rustc --edition=2021 -D warnings -O -o "$W/model-fixture" "$K/x86/tests/courier_model_fixture.rs" ||
39 fail "the model fixture does not build"
40free_port() { "$W/model-fixture" --free-udp-port; }
41
42# The fixture binds loopback before publishing its port. Its readiness wait
43# is bounded and also notices an early failure, instead of guessing a delay.
44start_model() { # port-file busy-count delay-seconds
45 "$W/model-fixture" "$1" "$2" "$3" >"$1.log" 2>&1 &
46 model_pid=$!
47 pids+=("$model_pid")
48 for _ in {1..100}; do
49 [[ -s "$1" ]] && return 0
50 kill -0 "$model_pid" 2>/dev/null || fail "model fixture exited before binding"
51 sleep 0.05
52 done
53 fail "model fixture did not publish its port"
54}
55
56printf 'JOB 1 chat Answer as Allen.\thello allen, how was your day\tself=Allen\tname=Drake\tstarts=20\tnote asked: hi / said: Hello\n' >"$W/job.txt"
57
58run() { # label model drop_every
59 local label="$1" model="$2" drop="$3" rp
60 rp=$(free_port)
61 "$relay" --udp "$rp" --model "$model" --work "$W/work" --log-dir "$W/log-$label" --busy-wait "${ALLEN_WAIT:-90}" >"$W/relay-$label.log" 2>&1 &
62 rpid=$!
63 pids+=("$rpid")
64 sleep 0.5
65 "$W/test" "$rp" "$drop" "$W/job.txt" "${ALLEN_WAIT:-90}" | tee "$W/test-$label.log" | grep -a -E "hello after|first part|PASS|FAIL" | sed "s/^/[$label] /"
66 grep -aq "COURIER_UDP: PASS" "$W/test-$label.log" || fail "run $label"
67 sleep 0.4 # the relay writes its log line a moment after the last part
68 kill "$rpid" 2>/dev/null || true
69 wait "$rpid" 2>/dev/null
70}
71
72start_model "$W/clean.port" 0 0
73mp=$(cat "$W/clean.port")
74fake1=$model_pid
75run clean "127.0.0.1:$mp" 0
76run lossy "127.0.0.1:$mp" 3
77run lossy2 "127.0.0.1:$mp" 2
78grep -aq "job resends [1-9]" "$W/test-lossy2.log" || fail "the lost job was not sent again"
79kill "$fake1" 2>/dev/null || true
80wait "$fake1" 2>/dev/null
81start_model "$W/busy.port" 2 0
82mp2=$(cat "$W/busy.port")
83run busy "127.0.0.1:$mp2" 0
84grep -aqE "waits 2|waits [2-9]" "$W/relay-busy.log" || fail "the relay did not wait out the busy answers"
85echo "[busy] relay waited out the busy answers: $(grep -a 'waits' "$W/relay-busy.log" | head -1 | sed 's/.*first in/first in/')"
86start_model "$W/slow.port" 0 4
87mp3=$(cat "$W/slow.port")
88run slow "127.0.0.1:$mp3" 0
89grep -aqE "[1-9][0-9]* waits" "$W/test-slow.log" || fail "the relay kept no word going while the model sat on the request"
90echo "[slow] the relay sent WAIT while the model sat silent: $(grep -aoE '[0-9]+ waits' "$W/test-slow.log" | head -1)"
91[ -s "$W"/log-clean/*.jsonl ] || fail "no line in the learning log"
92grep -aq '"ask":"hello allen, how was your day"' "$W"/log-clean/*.jsonl || fail "the learning log lacks the question"
93echo "learning log line: $(head -c 300 "$W"/log-clean/*.jsonl)"
94if [ -n "${ALLEN_MODEL:-}" ]; then
95 run real "$ALLEN_MODEL" 0
96fi
97echo "${tag}: PASS"