omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / tests / runtime / r15_seat_diag_test.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* Host-only test of the G15 sampler diagnostics (r15_seat_diag.h). Feeds |
| 18 | * synthetic sample streams and checks the diagnostics tell a stopped seat |
| 19 | * from a late sampler from a healthy run. No chip, no threads. */ |
| 20 | #define _POSIX_C_SOURCE 200809L |
| 21 | #include "r15_seat_diag.h" |
| 22 | #include "r15_stage_clock.h" |
| 23 | #include <string.h> |
| 24 | #include <stdio.h> |
| 25 | |
| 26 | static int fails; |
| 27 | #define CHECK(c) do { if (!(c)) { printf("[-] line %d: %s\n", __LINE__, #c); fails++; } } while (0) |
| 28 | |
| 29 | int main(void) { |
| 30 | SeatDiag d; |
| 31 | uint64_t t; |
| 32 | |
| 33 | /* healthy: 1000 samples 1.05 ms apart, heartbeat moves every time */ |
| 34 | d = (SeatDiag){0}; t = 1000000000ull; |
| 35 | for (int i = 0; i < 1000; i++, t += 1050000u) seat_diag_sample(&d, t, i > 0, (uint32_t)i); |
| 36 | CHECK(d.dead_runs == 1 && d.dead_longest == 1); /* only the first sample has no predecessor to compare */ |
| 37 | CHECK(d.late_2ms == 0 && d.late_10ms == 0 && d.max_gap_ns == 1050000u); |
| 38 | |
| 39 | /* seat stops: 600 live then 400 not live, sampler on time (the SEQ-06 shape) */ |
| 40 | d = (SeatDiag){0}; t = 1000000000ull; |
| 41 | for (int i = 0; i < 1000; i++, t += 1050000u) seat_diag_sample(&d, t, i >= 1 && i < 600, 7u); |
| 42 | CHECK(d.dead_runs == 2 && d.dead_longest == 400 && d.dead_cur == 400); |
| 43 | CHECK(d.late_10ms == 0 && d.max_gap_ns == 1050000u); |
| 44 | CHECK(d.first_dead_t == 1000000000ull); /* the opening sample */ |
| 45 | CHECK(d.last_live_t == 1000000000ull + 599u * 1050000ull); |
| 46 | CHECK(d.stale_max_ns == 399u * 1050000ull + 1050000ull); |
| 47 | |
| 48 | /* starved sampler, seat fine: every sample live but 50 ms apart */ |
| 49 | d = (SeatDiag){0}; t = 1000000000ull; |
| 50 | for (int i = 0; i < 100; i++, t += 50000000ull) seat_diag_sample(&d, t, i > 0, (uint32_t)i); |
| 51 | CHECK(d.late_10ms == 99 && d.late_2ms == 99 && d.max_gap_ns == 50000000ull); |
| 52 | CHECK(d.dead_longest == 1); |
| 53 | |
| 54 | /* scattered: a not-live sample every 10th, never two in a row */ |
| 55 | d = (SeatDiag){0}; t = 1000000000ull; |
| 56 | for (int i = 0; i < 1000; i++, t += 1050000u) seat_diag_sample(&d, t, i > 0 && i % 10 != 0, 1u); |
| 57 | CHECK(d.dead_runs == 100 && d.dead_longest == 1 && d.late_10ms == 0); |
| 58 | |
| 59 | /* stage clock: stamps become per-stage deltas that sum to the total; an unstamped clock writes nothing */ |
| 60 | { |
| 61 | R15StageClock c; char b[512]; |
| 62 | memset(&c, 0, sizeof c); |
| 63 | CHECK(r15_stage_json(&c, b, sizeof b) == 0); |
| 64 | c.t[0] = 1000; c.name[0] = "begin"; c.n = 1; |
| 65 | CHECK(r15_stage_json(&c, b, sizeof b) == 0); |
| 66 | c.t[1] = 1500; c.name[1] = "producers"; c.t[2] = 102000001500ull; c.name[2] = "seat_finish"; c.n = 3; |
| 67 | int w = r15_stage_json(&c, b, sizeof b); |
| 68 | CHECK(w > 0 && strcmp(b, ",\"teardown\":{\"total_ns\":102000000500,\"stages\":[[\"producers\",500],[\"seat_finish\",102000000000]]}") == 0); |
| 69 | R15StageClock f; memset(&f, 0, sizeof f); |
| 70 | for (int i = 0; i < R15_STAGE_MAX + 5; i++) r15_stage_mark(&f, "x"); |
| 71 | CHECK(f.n == R15_STAGE_MAX); |
| 72 | CHECK(r15_stage_json(&f, b, 40) == 0); /* too small a buffer is refused, never truncated */ |
| 73 | } |
| 74 | |
| 75 | printf("failures %d\n", fails); |
| 76 | return fails != 0; |
| 77 | } |