okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge main 6c642df75 into f-own-int (round 16: network waits, robustness, Windows kill-on-close job start, GitHub and transfer fixes)cd9745050

main
4.1 KiB68 linesraw
1// STRATA_RESIDENT_PIN and the automatic RAM copy (the engine's fallback when the expert arena does not fit RAM).
2// (a) The variable was read only while parsing --resident-experts / --resident-budget-gib; the automatic RAM copy set the pin
3//     unconditionally (generate.cpp:7776 at dbb2d168c), so STRATA_RESIDENT_PIN=0 left it page-locked (the #5718 cap test's
4//     arm Z: "page-locked and mapped" with the variable set).  Here the fallback's mode must read it like the flags do.
5// (b) The line printed when that copy's budget is cut to the available RAM said "--resident-budget-gib 47.07 exceeds
6//     available RAM" although no such flag was given.  The flag's own line stays byte for byte; the automatic one names no flag.
7// No GPU, no model: the rules are header-only.
8#include "strata/core/resident_ram_copy.hpp"
9
10#include <cstdint>
11#include <cstdio>
12#include <string>
13
14using namespace strata::core;
15
16namespace {
17int failed = 0;
18#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); ++failed; } } while (0)
19
20// the cap test's numbers (r1-Z-engine.log line 46): budget 47.07 GiB, 49.49 GiB available, 4 GiB headroom, cut to 45.24
21const uint64_t budget = (uint64_t) (47.07 * 1073741824.0);
22const uint64_t available = (uint64_t) (49.49 * 1073741824.0);
23const uint64_t headroom = 4ull << 30;
24const uint64_t clamped = (uint64_t) (45.24 * 1073741824.0);
25}  // namespace
26
27int main() {
28    // (a) the variable's reading, shared by the flags and the fallback
29    CHECK(resident_pin_from_env(nullptr));           // unset: page-locked, as before
30    CHECK(!resident_pin_from_env("0"));              // "0": pageable
31    CHECK(resident_pin_from_env("1"));
32    CHECK(resident_pin_from_env(""));
33    CHECK(resident_pin_from_env("00"));              // only "0" turns it off, as the old string compare did
34    CHECK(resident_pin_from_env("0 "));
35
36    // (a) the automatic RAM copy's mode
37    const AutoRamCopyMode unset = auto_ram_copy_mode(nullptr);
38    CHECK(unset.resident_cpu_experts && unset.pin && unset.soft);       // unchanged default
39    const AutoRamCopyMode off = auto_ram_copy_mode("0");
40    if (off.pin)
41        std::fprintf(stderr, "the automatic RAM copy with STRATA_RESIDENT_PIN=0: page-locked (the variable was not read)\n");
42    CHECK(off.resident_cpu_experts && !off.pin && off.soft);             // the variable acts on the fallback too
43    const AutoRamCopyMode one = auto_ram_copy_mode("1");
44    CHECK(one.pin);
45
46    // (b) the clamp line: the flag's text byte for byte as before
47    const std::string flag = resident_clamp_line(ResidentBudgetFrom::Flag, budget, "RAM", available, headroom, clamped);
48    CHECK(flag == "FileExpertSource: --resident-budget-gib 47.07 exceeds available RAM (49.49 GiB minus 4 GiB headroom "
49                  "and a 0.25 GiB margin): 45.24 GiB\n");
50    const std::string fits = resident_clamp_line(ResidentBudgetFrom::WhatFits, budget, "commit", available, headroom, clamped);
51    CHECK(fits == "FileExpertSource: room for the complement: 45.24 GiB (49.49 GiB available commit minus 4 GiB headroom "
52                  "and a 0.25 GiB margin)\n");
53    // (b) the automatic copy: no flag named, the copy and its numbers are
54    const std::string automatic = resident_clamp_line(ResidentBudgetFrom::Automatic, budget, "RAM", available, headroom, clamped);
55    std::fprintf(stderr, "automatic: %s", automatic.c_str());
56    CHECK(automatic.find("--resident-budget-gib") == std::string::npos);
57    CHECK(automatic.find("--") == std::string::npos);
58    CHECK(automatic.find("automatic RAM copy of 47.07 GiB") != std::string::npos);
59    CHECK(automatic.find("49.49 GiB minus 4 GiB headroom and a 0.25 GiB margin") != std::string::npos);
60    CHECK(automatic.find("using 45.24 GiB") != std::string::npos);
61    CHECK(!automatic.empty() && automatic.back() == '\n');
62    CHECK(automatic == "strata: the automatic RAM copy of 47.07 GiB does not fit the available RAM (49.49 GiB minus 4 GiB "
63                       "headroom and a 0.25 GiB margin); using 45.24 GiB\n");
64
65    if (failed) { std::fprintf(stderr, "resident_ram_copy_test: %d FAILED\n", failed); return 1; }
66    std::printf("resident_ram_copy_test: ok\n");
67    return 0;
68}