okami.mom/strata

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

Niko1221Merge f-023-int into f-023-guifcb0c2d0e

main
4.3 KiB76 linesraw
1#include "strata/core/conversation_memory.hpp"
2
3#include <cstdio>
4#include <cstdlib>
5#include <limits>
6#include <sstream>
7
8#if defined(_WIN32)
9#ifndef NOMINMAX
10#define NOMINMAX
11#endif
12#include <windows.h>
13#endif
14
15using namespace strata::core;
16namespace {
17int checks = 0;
18void check(bool ok, const char* label) {
19    ++checks;
20    if (!ok) { std::fprintf(stderr, "FAIL: %s\n", label); std::exit(1); }
21}
22}
23
24int main() {
25    for (const char* text : {"", "MemFree: 100 kB\n", "MemAvailable: -1 kB\n",
26                            "MemAvailable: +1 kB\n", "MemAvailable: 1 MB\n",
27                            "MemAvailable: 1\n", "MemAvailable: 1x kB\n",
28                            "MemAvailable: 18446744073709551615 kB\n",
29                            "MemAvailable: 18446744073709551616 kB\n",
30                            "MemAvailable: 1 kB trailing\n",
31                            "MemAvailable: 1 kB\nMemAvailable: 2 kB\n"}) {
32        std::istringstream input(text);
33        check(!conversation_mem_available(input), "missing/malformed telemetry is unknown");
34    }
35    std::istringstream normal("MemTotal: 999999 kB\nMemAvailable:    12345 kB\nSwapFree: 777 kB\n");
36    check(conversation_mem_available(normal) == 12345ULL * 1024, "only MemAvailable is counted");
37    std::istringstream zero("MemAvailable: 0 kB");
38    check(conversation_mem_available(zero) == 0, "zero available memory is known");
39    std::istringstream broken("MemAvailable: 123 kB\n");
40    broken.setstate(std::ios::badbit);
41    check(!conversation_mem_available(broken), "I/O failure declines admission");
42    check(!conversation_memory_admit({}, 0, 0), "unknown fails closed even with zero floor");
43    check(conversation_memory_admit(100, 40, 60), "exact allocation plus floor fits");
44    check(!conversation_memory_admit(99, 40, 60), "one byte below required memory rejected");
45    check(!conversation_memory_admit(59, 0, 60), "floor subtraction cannot underflow");
46    check(conversation_memory_admit(60, 0, 60), "post-capture floor check");
47    check(!conversation_memory_admit(100, std::numeric_limits<uint64_t>::max(), 1), "allocation arithmetic cannot overflow");
48    check(conversation_memory_admit(std::numeric_limits<uint64_t>::max(),
49                                   std::numeric_limits<uint64_t>::max(), 0), "maximal exact bound");
50    check(!conversation_memory_admit(std::numeric_limits<uint64_t>::max(),
51                                    std::numeric_limits<uint64_t>::max(), 1), "maximal sum overflow rejected");
52    // #3493: the budget used when --conversation-cache-mib is not given
53    const auto GiB = [](uint64_t n) { return n << 30; };
54    check(auto_conversation_cache_mib({}, 2560) == 0, "unknown telemetry: no cache");
55    check(auto_conversation_cache_mib(GiB(2), 2560) == 0, "below the parking floor: off");
56    check(auto_conversation_cache_mib(GiB(4), 2560) == 0, "4 GiB free: 40% of 1.5 GiB is under 1 GiB: off");
57    check(auto_conversation_cache_mib(GiB(8), 2560) == 2048, "8 GiB free: 40% of 5.5 GiB, in steps of 256");
58    check(auto_conversation_cache_mib(GiB(20), 2560) == 7168, "20 GiB free: 40% of 17.5 GiB");
59    check(auto_conversation_cache_mib(GiB(40), 2560) == 8192, "capped at 8 GiB");
60    check(auto_conversation_cache_mib(GiB(1000), 0) == 8192, "a big server: still the cap");
61    check(auto_conversation_cache_mib(GiB(40), 1000000) == 0, "a floor above the RAM: off, no underflow");
62    // Test the real provider without assuming any particular amount of free RAM.
63    const auto available = available_host_bytes();
64    check(!available || conversation_memory_admit(available, 0, 0), "provider returns bytes or unknown");
65#if defined(_WIN32)
66    // Unknown must fail closed in production, but must not let a broken Windows provider pass this test.
67    // Compare with total physical memory, not a second available reading: other processes can allocate
68    // between calls. No pressure allocation or fixed free-memory assumption is needed.
69    MEMORYSTATUSEX physical{};
70    physical.dwLength = sizeof physical;
71    check(GlobalMemoryStatusEx(&physical) != 0, "Windows physical-memory API is available");
72    check(available.has_value() && *available <= physical.ullTotalPhys,
73          "Windows provider returns a known, physically bounded sample");
74#endif
75    std::printf("conversation_memory_test: %d checks passed\n", checks);
76}