Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/interplane-mv/blob/main/docs/external/nightdriver/565/harness/host/test_budget.cpp # interplane-mv INTERPLANE: open interoperability protocol between AI models and agent runtimes. Models express intent. Runtimes retain authority. Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `interplane-mv / docs / external / nightdriver / 565 / harness / host / test_budget.cpp` 71 lines ยท 4486 bytes Raw Blame History 1 // Logic-level test (MODEL of documented ESP-IDF semantics, not a device run, not the real allocator). 2 // Real project code under test: include/heapbudget.h (FreeInternalBytes8Bit) from the fork branch. 3 // Copied from src/systemcontainer.cpp at the same branch (arithmetic only, not compiled from source): 4 // memtouse = free > RESERVE ? free - RESERVE : 0; cBuffers = memtouse / memtoalloc; throw if < MIN; clamp to MAX. 5 #include 6 #include 7 #include "heapbudget.h" 8 using namespace heapmodel; 9 10 static const uint32_t RESERVE = 150000, MIN_BUF = 3, MAX_BUF = 24; 11 static int fails = 0; 12 #define CHECK(c, ...) do { if (!(c)) { fails++; printf("FAIL %s:%d ", __FILE__, __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0) 13 14 static uint32_t buffers_for(uint32_t freeMem, uint32_t setBytes, bool* admitted) { 15 uint32_t memtouse = freeMem > RESERVE ? freeMem - RESERVE : 0; 16 uint32_t c = memtouse / setBytes; 17 *admitted = c >= MIN_BUF; 18 return c > MAX_BUF ? MAX_BUF : c; 19 } 20 static uint32_t old_free() { return (uint32_t)heap_caps_get_free_size(MALLOC_CAP_INTERNAL); } // Esp.cpp:164 getFreeHeap 21 22 static void layout(size_t dram, size_t iram) { reset(); heaps().push_back(DRAM({dram})); heaps().push_back(IRAM({iram})); } 23 24 int main() { 25 // Case 1: documented classic-ESP32 region mix (mem_alloc.rst:91-93 shows 14+111 KiB D/IRAM and 90 KiB IRAM). Numbers illustrative. 26 layout(160000, 90000); 27 CHECK(old_free() == 250000, "old=%u", old_free()); 28 CHECK(FreeInternalBytes8Bit() == 160000, "new=%u", FreeInternalBytes8Bit()); 29 const uint32_t setBytes = 9000; 30 bool okOld, okNew; 31 uint32_t nOld = buffers_for(old_free(), setBytes, &okOld); 32 uint32_t nNew = buffers_for(FreeInternalBytes8Bit(), setBytes, &okNew); 33 printf("case1 old: admit=%d buffers=%u | new: admit=%d buffers=%u\n", okOld, nOld, okNew, nNew); 34 uint32_t left8 = (uint32_t)heap_caps_get_free_size(MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT) - (okOld ? nOld * setBytes : 0); 35 CHECK(okOld && left8 < RESERVE, "old query should over-admit (8-bit left %u vs reserve %u)", left8, RESERVE); 36 CHECK(!okNew, "new query should refuse cleanly"); 37 38 // Case 2: invariant sweep. If admitted, 8-bit free after taking the buffers must be >= RESERVE. 39 uint32_t seed = 12345, oldViol = 0, newViol = 0, n = 0; 40 for (int i = 0; i < 20000; i++) { 41 seed = seed * 1664525u + 1013904223u; 42 size_t dram = 100000 + (seed >> 8) % 200000; 43 seed = seed * 1664525u + 1013904223u; 44 size_t iram = (seed >> 8) % 100000; 45 seed = seed * 1664525u + 1013904223u; 46 uint32_t sb = 600 + (seed >> 8) % 12000; 47 layout(dram, iram); n++; 48 bool a, b; 49 uint32_t co = buffers_for(old_free(), sb, &a), cn = buffers_for(FreeInternalBytes8Bit(), sb, &b); 50 if (a && dram < RESERVE + (size_t)co * sb) oldViol++; 51 if (b && dram < RESERVE + (size_t)cn * sb) newViol++; 52 } 53 printf("case2 sweep n=%u old_invariant_violations=%u new_invariant_violations=%u\n", n, oldViol, newViol); 54 CHECK(oldViol > 0, "expected old query to violate the invariant somewhere"); 55 CHECK(newViol == 0, "new query violated the invariant %u times", newViol); 56 57 // Case 3: the issue's literal numbers as a MODEL shape (hypothesis only: the issue gives no caps or call site). 58 // 50K INTERNAL free, 34K INTERNAL largest, but the 34K block is in IRAM and DRAM has only small fragments. 59 reset(); heaps().push_back(DRAM({3000, 3000, 3000, 3000, 2000, 2000})); heaps().push_back(IRAM({34000})); 60 CHECK(old_free() == 50000, "free=%u", old_free()); 61 CHECK(heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL) == 34000, "largest internal"); 62 CHECK(heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT) == 3000, "largest 8bit"); 63 printf("case3 internal free/largest = %zu/%zu, 8bit free/largest = %zu/%zu\n", 64 heap_caps_get_free_size(MALLOC_CAP_INTERNAL), heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL), 65 heap_caps_get_free_size(MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT), heap_caps_get_largest_free_block(MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT)); 66 67 CHECK(heap_caps_malloc(4096, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT) == nullptr, "4K 8-bit alloc must fail in this state"); 68 CHECK(heap_caps_malloc(4096, MALLOC_CAP_INTERNAL | MALLOC_CAP_32BIT) != nullptr, "4K 32-bit alloc succeeds from IRAM"); 69 printf(fails ? "RESULT FAIL (%d)\n" : "RESULT PASS\n", fails); 70 return fails ? 1 : 0; 71 }