aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / core / pipe_test.c
| 1 | /* pipe_test.c -- AIENOS_CK_PIPE kernel self-test (TEST ONLY, CK_PIPE_TEST=1; gate scripts/qemu_ck_pipe_test.sh). |
| 2 | * |
| 3 | * Drives the pipe primitive (core/pipe.c, aienos-mv#428) inside the kernel: a producer and a consumer task, step |
| 4 | * functions interleaved by the run-queue scheduler (core/sched.c), each holding one end in its own capability table, |
| 5 | * through the same driver the host test uses (core/pipe_drive.h). The kernel's wait hook yields to the scheduler |
| 6 | * (it keeps ticking it, running the other task) until the wake hook fires; this is cooperative scheduling inside one |
| 7 | * kernel thread, not a context switch between M3 threads. Lines on the serial console: |
| 8 | * pipe_check: name=<check> result=PASS|FAIL <details>; <label> |
| 9 | * pipe_test: done checks=<n> failed=<n> capacity=<bytes> pool_pipes=<n>; <label> |
| 10 | * QEMU (aarch64 virt), TEST build, not physical: qualifies nothing physical. */ |
| 11 | #ifndef CK_PIPE_TEST |
| 12 | #error "pipe_test is test-only: build with make CK_PIPE_TEST=1" |
| 13 | #endif |
| 14 | #ifdef CK_HARDWARE_STAGING |
| 15 | #error "CK_PIPE_TEST cannot be combined with CK_HARDWARE_STAGING" |
| 16 | #endif |
| 17 | #include "ck_internal.h" |
| 18 | #include "pipe.h" |
| 19 | #include "pipe_drive.h" |
| 20 | |
| 21 | #define LABEL "QEMU (aarch64 virt), TEST build, not physical" |
| 22 | #define PAYLOAD (160u * 1024u) /* 2.5 pipe capacities */ |
| 23 | |
| 24 | static struct ck_pipe_pool pool; |
| 25 | static struct ck_cap_table ta, tb; |
| 26 | static struct pd_world world; |
| 27 | static uint8_t sink[PAYLOAD]; |
| 28 | static unsigned checks, failed; |
| 29 | |
| 30 | static void fresh(void) |
| 31 | { |
| 32 | ck_cap_init(&ta, 0x41, CK_CAP_SLOTS); |
| 33 | ck_cap_init(&tb, 0x42, CK_CAP_SLOTS); |
| 34 | } |
| 35 | |
| 36 | static int empty(void) |
| 37 | { |
| 38 | return pd_table_empty(&ta) && pd_table_empty(&tb) && ck_pipe_pool_empty(&pool); |
| 39 | } |
| 40 | |
| 41 | static void verdict(const char *name, int ok) |
| 42 | { |
| 43 | checks++; |
| 44 | failed += !ok; |
| 45 | ck_printf("pipe_check: name=%s result=%s tables_empty=%d; %s\n", name, ok ? "PASS" : "FAIL", empty(), LABEL); |
| 46 | } |
| 47 | |
| 48 | static size_t expected_nuls(size_t n) |
| 49 | { |
| 50 | size_t z = 0; |
| 51 | for (size_t i = 0; i < n; i++) |
| 52 | z += pd_byte(i) == 0; |
| 53 | return z; |
| 54 | } |
| 55 | |
| 56 | static void transfer(const char *name, unsigned flags) |
| 57 | { |
| 58 | fresh(); |
| 59 | pd_run(&world, &pool, &ta, &tb, sink, PAYLOAD, 8192, 1000, flags, 1, 4, 0); |
| 60 | const struct ck_pipe_stats *s = &pool.total; |
| 61 | size_t bad = pd_mismatches(&world), nul = pd_nul_count(&world); |
| 62 | ck_printf("pipe_detail: %s bytes=%lu sent=%lu nul=%lu expected_nul=%lu mismatches=%lu would_block=%llu waits=%llu " |
| 63 | "wakeups=%llu max_resident=%llu capacity=%u eof=%d prod_steps=%llu cons_steps=%llu ticks=%llu\n", |
| 64 | name, (unsigned long)world.recvd, (unsigned long)world.sent, (unsigned long)nul, |
| 65 | (unsigned long)expected_nuls(PAYLOAD), (unsigned long)bad, (unsigned long long)s->would_block, |
| 66 | (unsigned long long)s->waits, (unsigned long long)s->wakeups, (unsigned long long)s->max_resident, |
| 67 | CK_PIPE_CAPACITY, world.eof_seen, (unsigned long long)world.prod_steps, |
| 68 | (unsigned long long)world.cons_steps, (unsigned long long)world.ticks); |
| 69 | int moved = world.setup_err == OSH_E_OK && world.prod_err == OSH_E_OK && world.cons_err == OSH_E_OK && |
| 70 | world.recvd == PAYLOAD && world.sent == PAYLOAD && bad == 0 && nul == expected_nuls(PAYLOAD) && |
| 71 | nul > 0 && s->bytes_read == PAYLOAD; |
| 72 | if (!flags) { |
| 73 | verdict("payload_nul_byte_for_byte", moved); |
| 74 | verdict("backpressure_would_block", moved && s->would_block > 0 && s->max_resident == CK_PIPE_CAPACITY && |
| 75 | world.prod_steps > 1 && world.cons_steps > 1); |
| 76 | verdict("eof_after_last_writer", moved && world.eof_seen && s->eof == 1); |
| 77 | verdict("bounded_memory", s->max_resident <= CK_PIPE_CAPACITY && empty()); |
| 78 | } else { |
| 79 | verdict("blocking_wait_wake_through_scheduler", moved && s->waits > 0 && s->wakeups > 0 && |
| 80 | s->cancellations == 0 && world.eof_seen && empty()); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | static void broken_pipe(void) |
| 85 | { |
| 86 | fresh(); |
| 87 | ck_pipe_pool_init(&pool, 0); |
| 88 | struct ck_handle r, w; |
| 89 | size_t n = 1; |
| 90 | static const uint8_t b[3] = { 'a', 0, 'b' }; |
| 91 | int ok = ck_pipe_create(&pool, &ta, &r, &w) == OSH_E_OK; |
| 92 | ok = ok && ck_stream_close(&pool, &ta, r) == OSH_E_OK; |
| 93 | unsigned e = ck_stream_write(&pool, &ta, w, b, 3, 0, &n); |
| 94 | ok = ok && e == OSH_E_BROKEN_PIPE && n == 0 && pool.total.broken_pipe == 1; |
| 95 | ok = ok && ck_stream_close(&pool, &ta, w) == OSH_E_OK; |
| 96 | ck_printf("pipe_detail: broken_pipe write_after_reader_close=%s n=%lu\n", osh_err_label(e), (unsigned long)n); |
| 97 | verdict("broken_pipe_no_reader", ok && empty()); |
| 98 | } |
| 99 | |
| 100 | static void revoked_write(void) |
| 101 | { |
| 102 | fresh(); |
| 103 | ck_pipe_pool_init(&pool, 0); |
| 104 | struct ck_handle r, w; |
| 105 | size_t n = 0, m = 9; |
| 106 | uint8_t got[4]; |
| 107 | static const uint8_t b1[2] = { 'o', 'k' }, b2[2] = { 0, 'x' }; |
| 108 | int ok = ck_pipe_create(&pool, &ta, &r, &w) == OSH_E_OK; |
| 109 | ok = ok && ck_stream_write(&pool, &ta, w, b1, 2, 0, &n) == OSH_E_OK && n == 2; |
| 110 | ok = ok && ck_pipe_revoke(&pool, &ta, w) == OSH_E_OK; |
| 111 | unsigned ew = ck_stream_write(&pool, &ta, w, b2, 2, 0, &n); |
| 112 | unsigned er = ck_stream_read(&pool, &ta, r, got, 4, 0, &m); |
| 113 | ok = ok && ew == OSH_E_REVOKED && n == 0 && er == OSH_E_REVOKED && m == 0 && pool.total.bytes_read == 0; |
| 114 | ok = ok && ck_stream_close(&pool, &ta, r) == OSH_E_OK && ck_stream_close(&pool, &ta, w) == OSH_E_OK; |
| 115 | ck_printf("pipe_detail: revoked write=%s n=%lu read=%s n=%lu bytes_delivered=%llu\n", osh_err_label(ew), |
| 116 | (unsigned long)n, osh_err_label(er), (unsigned long)m, (unsigned long long)pool.total.bytes_read); |
| 117 | verdict("revoked_write_refused_no_bytes_after", ok && empty()); |
| 118 | |
| 119 | /* revoked mid-stream by the consumer through the scheduler: nothing is read after the revoke */ |
| 120 | fresh(); |
| 121 | pd_run(&world, &pool, &ta, &tb, sink, PAYLOAD, 8192, 1000, CK_PIPE_F_BLOCK, 1, 4, 50000); |
| 122 | ck_printf("pipe_detail: revoke_midstream recvd=%lu reads_after_revoke=%lu prod=%s cons=%s bytes_read=%llu\n", |
| 123 | (unsigned long)world.recvd, (unsigned long)world.reads_after_revoke, osh_err_label(world.prod_err), |
| 124 | osh_err_label(world.cons_err), (unsigned long long)pool.total.bytes_read); |
| 125 | verdict("revoke_midstream_no_data_after", world.revoked && world.reads_after_revoke == 0 && |
| 126 | world.prod_err == OSH_E_REVOKED && world.cons_err == OSH_E_REVOKED && |
| 127 | pool.total.bytes_read == world.recvd && pd_mismatches(&world) == 0 && |
| 128 | empty()); |
| 129 | } |
| 130 | |
| 131 | static void stale_and_ops(void) |
| 132 | { |
| 133 | fresh(); |
| 134 | ck_pipe_pool_init(&pool, 0); |
| 135 | struct ck_handle r, w, r2, w2; |
| 136 | size_t n = 0; |
| 137 | uint8_t b[2] = { 0, 0 }; |
| 138 | int ok = ck_pipe_create(&pool, &ta, &r, &w) == OSH_E_OK; |
| 139 | ok = ok && ck_stream_close(&pool, &ta, r) == OSH_E_OK && ck_stream_close(&pool, &ta, w) == OSH_E_OK; |
| 140 | ok = ok && ck_pipe_create(&pool, &ta, &r2, &w2) == OSH_E_OK && r2.index == r.index; |
| 141 | unsigned es = ck_stream_read(&pool, &ta, r, b, 2, 0, &n); |
| 142 | ok = ok && es == OSH_E_STALE && ck_pipe_release_table(&pool, &ta) == 2; |
| 143 | verdict("stale_handle_after_close", ok && empty()); |
| 144 | |
| 145 | struct ck_pipe_ret ret; |
| 146 | uint64_t unknown[4] = { CK_PIPE_FRAME_MAGIC, CK_PIPE_ABI_VERSION, 9, 0 }; |
| 147 | uint64_t v2[4] = { CK_PIPE_FRAME_MAGIC, 2, CK_PIPE_OP_CREATE, 0 }; |
| 148 | /* No slice. A length of 0 does not consult the window. */ |
| 149 | unsigned eu = ck_pipe_dispatch(&pool, &ta, &tb, unknown, 4, 0, 0, 0, 0, &ret); |
| 150 | unsigned ev = ck_pipe_dispatch(&pool, &ta, &tb, v2, 4, 0, 0, 0, 0, &ret); |
| 151 | ck_printf("pipe_detail: op_table abi_version=%u unknown_op=%s version_2=%s\n", CK_PIPE_ABI_VERSION, |
| 152 | osh_err_label(eu), osh_err_label(ev)); |
| 153 | verdict("unknown_op_not_supported", eu == OSH_E_NOT_SUPPORTED && ev == OSH_E_NOT_SUPPORTED && empty()); |
| 154 | |
| 155 | ok = ck_pipe_create(&pool, &ta, &r, &w) == OSH_E_OK && ck_pipe_cancel(&pool, &ta, w) == OSH_E_OK; |
| 156 | unsigned ei = ck_stream_write(&pool, &ta, w, b, 2, 0, &n); |
| 157 | ok = ok && ei == OSH_E_INTERRUPTED && n == 0 && ck_stream_write(&pool, &ta, w, b, 2, 0, &n) == OSH_E_OK && n == 2; |
| 158 | ok = ok && ck_pipe_release_table(&pool, &ta) == 2; |
| 159 | verdict("cancel_interrupts_once", ok && empty()); |
| 160 | } |
| 161 | |
| 162 | void ck_pipe_selftest(void) |
| 163 | { |
| 164 | ck_printf("pipe_test: TEST-ONLY pipe self-test begin abi_version=%u capacity=%u pool_pipes=%u payload=%u; %s\n", |
| 165 | CK_PIPE_ABI_VERSION, CK_PIPE_CAPACITY, CK_PIPE_POOL_PIPES, PAYLOAD, LABEL); |
| 166 | transfer("nonblocking", 0); |
| 167 | transfer("blocking", CK_PIPE_F_BLOCK); |
| 168 | broken_pipe(); |
| 169 | revoked_write(); |
| 170 | stale_and_ops(); |
| 171 | ck_printf("pipe_test: done checks=%u failed=%u capacity=%u pool_pipes=%u; %s\n", checks, failed, CK_PIPE_CAPACITY, |
| 172 | CK_PIPE_POOL_PIPES, LABEL); |
| 173 | } |