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 / exec / test_exec_native_cache.c
| 1 | /* omega_exec_native_f3 keeps executable pages for recently seen realizations. |
| 2 | * Contract: same bytes -> same mapping (hit), different bytes -> new mapping (miss), |
| 3 | * results identical to a fresh mapping, eviction frees without breaking later calls. */ |
| 4 | #include <stdio.h> |
| 5 | #include <string.h> |
| 6 | #include <stdlib.h> |
| 7 | #include "omega_program.h" |
| 8 | #include "omega_exec.h" |
| 9 | |
| 10 | static int fails; |
| 11 | #define CHECK(c, ...) do{ if(!(c)){ fails++; printf("FAIL: " __VA_ARGS__); printf("\n"); } }while(0) |
| 12 | |
| 13 | static void mk(OmegaProgram *p, OpCode op, uint64_t imm) { |
| 14 | if (omega_program_build_unary_op(p, "f", op, imm) != 0 || omega_program_realize(p) != 0) { puts("build fail"); exit(2); } |
| 15 | } |
| 16 | |
| 17 | int main(void) { |
| 18 | OmegaExecCacheStats s0, s1; |
| 19 | OmegaProgram a, b; |
| 20 | uint64_t y = 0; |
| 21 | mk(&a, OP_ADD, 5); |
| 22 | mk(&b, OP_MUL, 7); |
| 23 | omega_exec_native_cache_reset(); |
| 24 | omega_exec_native_cache_stats(&s0); |
| 25 | CHECK(s0.hits == 0 && s0.misses == 0 && s0.resident == 0, "reset clears counters"); |
| 26 | |
| 27 | CHECK(omega_exec_native_f3(&a.realization, 10, 0, 0, &y) == 0 && y == 15, "a first call result"); |
| 28 | CHECK(omega_exec_native_f3(&a.realization, 11, 0, 0, &y) == 0 && y == 16, "a second call result"); |
| 29 | omega_exec_native_cache_stats(&s1); |
| 30 | CHECK(s1.misses == 1 && s1.hits == 1 && s1.resident == 1, "same bytes: 1 miss then 1 hit (got m=%llu h=%llu r=%llu)", |
| 31 | (unsigned long long)s1.misses, (unsigned long long)s1.hits, (unsigned long long)s1.resident); |
| 32 | |
| 33 | CHECK(omega_exec_native_f3(&b.realization, 3, 0, 0, &y) == 0 && y == 21, "b result"); |
| 34 | omega_exec_native_cache_stats(&s1); |
| 35 | CHECK(s1.misses == 2 && s1.resident == 2, "different bytes: a miss, 2 resident"); |
| 36 | |
| 37 | /* a copy with identical bytes but a different RealizationObject address still hits */ |
| 38 | RealizationObject copy = a.realization; |
| 39 | CHECK(omega_exec_native_f3(©, 1, 0, 0, &y) == 0 && y == 6, "copy result"); |
| 40 | omega_exec_native_cache_stats(&s1); |
| 41 | CHECK(s1.hits == 2, "identity is the bytes, not the pointer"); |
| 42 | |
| 43 | /* Invalidation. A program of the same length whose bytes differ (ADD #6 vs ADD #5) must map fresh |
| 44 | * pages and compute its own result; a cache keyed by length or address would return a + 5. */ |
| 45 | OmegaProgram a6; |
| 46 | mk(&a6, OP_ADD, 6); |
| 47 | CHECK(a6.realization.code_len == a.realization.code_len && |
| 48 | memcmp(a6.realization.code_bytes, a.realization.code_bytes, a.realization.code_len) != 0, |
| 49 | "fixture: ADD #6 has the same length as ADD #5 and different bytes"); |
| 50 | omega_exec_native_cache_stats(&s0); |
| 51 | CHECK(omega_exec_native_f3(&a6.realization, 5, 0, 0, &y) == 0 && y == 11, "same length, other bytes: own result (got %llu)", |
| 52 | (unsigned long long)y); |
| 53 | omega_exec_native_cache_stats(&s1); |
| 54 | CHECK(s1.hits == s0.hits && s1.misses == s0.misses + 1, "same length, other bytes: a miss, not a hit"); |
| 55 | |
| 56 | /* The same RealizationObject rewritten in place (same address, same length, new bytes) must not run |
| 57 | * the stale mapping; rewriting it back to the original bytes hits the mapping still resident. */ |
| 58 | RealizationObject *ra = &a.realization; |
| 59 | mk(&a, OP_ADD, 9); |
| 60 | CHECK(ra == &a.realization && omega_exec_native_f3(&a.realization, 5, 0, 0, &y) == 0 && y == 14, |
| 61 | "rewritten in place: new code runs (got %llu, stale mapping gives 10)", (unsigned long long)y); |
| 62 | mk(&a, OP_ADD, 5); |
| 63 | omega_exec_native_cache_stats(&s0); |
| 64 | CHECK(omega_exec_native_f3(&a.realization, 5, 0, 0, &y) == 0 && y == 10, "rewritten back: original result"); |
| 65 | omega_exec_native_cache_stats(&s1); |
| 66 | CHECK(s1.hits == s0.hits + 1 && s1.misses == s0.misses, "rewritten back: hit on the resident mapping"); |
| 67 | |
| 68 | /* fill past capacity: every program still computes right after eviction */ |
| 69 | omega_exec_native_cache_reset(); |
| 70 | for (unsigned i = 0; i < 2 * OMEGA_EXEC_CACHE_SLOTS + 3; i++) { |
| 71 | OmegaProgram p; mk(&p, OP_ADD, 1000 + i); |
| 72 | CHECK(omega_exec_native_f3(&p.realization, i, 0, 0, &y) == 0 && y == 1000 + 2ull * i, "evict loop %u", i); |
| 73 | } |
| 74 | omega_exec_native_cache_stats(&s1); |
| 75 | CHECK(s1.resident == OMEGA_EXEC_CACHE_SLOTS, "resident capped at %u (got %llu)", OMEGA_EXEC_CACHE_SLOTS, (unsigned long long)s1.resident); |
| 76 | CHECK(omega_exec_native_f3(&a.realization, 10, 0, 0, &y) == 0 && y == 15, "a still right after eviction"); |
| 77 | |
| 78 | /* LRU victim: fill every slot, touch the oldest, add one more. The second oldest is evicted and |
| 79 | * the touched one stays resident. */ |
| 80 | static OmegaProgram lru[OMEGA_EXEC_CACHE_SLOTS + 1]; |
| 81 | omega_exec_native_cache_reset(); |
| 82 | for (unsigned i = 0; i <= OMEGA_EXEC_CACHE_SLOTS; i++) mk(&lru[i], OP_ADD, 2000 + i); |
| 83 | for (unsigned i = 0; i < OMEGA_EXEC_CACHE_SLOTS; i++) |
| 84 | CHECK(omega_exec_native_f3(&lru[i].realization, 1, 0, 0, &y) == 0 && y == 2001ull + i, "lru fill %u", i); |
| 85 | CHECK(omega_exec_native_f3(&lru[0].realization, 1, 0, 0, &y) == 0 && y == 2001, "lru touch oldest"); |
| 86 | CHECK(omega_exec_native_f3(&lru[OMEGA_EXEC_CACHE_SLOTS].realization, 1, 0, 0, &y) == 0 && y == 2001ull + OMEGA_EXEC_CACHE_SLOTS, |
| 87 | "lru one past capacity"); |
| 88 | omega_exec_native_cache_stats(&s0); |
| 89 | CHECK(s0.evictions == 1 && s0.resident == OMEGA_EXEC_CACHE_SLOTS, "lru: exactly one eviction (got %llu)", |
| 90 | (unsigned long long)s0.evictions); |
| 91 | CHECK(omega_exec_native_f3(&lru[0].realization, 1, 0, 0, &y) == 0 && y == 2001, "lru touched entry result"); |
| 92 | omega_exec_native_cache_stats(&s1); |
| 93 | CHECK(s1.hits == s0.hits + 1 && s1.misses == s0.misses, "lru: the recently touched entry is still resident"); |
| 94 | CHECK(omega_exec_native_f3(&lru[1].realization, 1, 0, 0, &y) == 0 && y == 2002, "lru evicted entry result"); |
| 95 | omega_exec_native_cache_stats(&s0); |
| 96 | CHECK(s0.misses == s1.misses + 1 && s0.hits == s1.hits, "lru: the least recently used entry was the one evicted"); |
| 97 | |
| 98 | printf("EXEC_NATIVE_CACHE: %s (%d failures)\n", fails ? "FAIL" : "PASS", fails); |
| 99 | return fails ? 1 : 0; |
| 100 | } |