okami.mom/strata

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

Niko1221platform_memory_test: report "skipped" with the reason when the system refuses to lock the region5a6d637c6

main
7.1 KiB132 linesraw
1// src/platform/memory_test.cpp - plan v0.3 P0.1: lock_resident on a 256 MiB region (CPU only, no GPU, no model).
2#include "strata/platform/memory.hpp"
3
4#include <cstdio>
5#include <cstdlib>
6#include <cstring>
7
8#if defined(_WIN32)
9#define WIN32_LEAN_AND_MEAN
10#define NOMINMAX
11#include <windows.h>
12#include <psapi.h>
13#pragma comment(lib, "psapi.lib")
14#endif
15
16namespace {
17uint64_t gib(double g) { return (uint64_t) (g * 1073741824.0); }
18
19// #577: the file tier's unbuffered choice (true = the file cache keeps its reads, so they stay buffered)
20int file_cache_keeps_cases() {
21    using strata::platform::file_cache_keeps;
22    struct Case { const char* what; double avail, arena, read; bool keeps; };
23    const double experts = 71.7;   // UD-Q4_K_XL's routed experts
24    const Case cases[] = {
25        // the reporter's 96 GB PC (83.0 GiB available at start): 0.1.38 passed the budget asked for and every
26        // shard's bytes (103.7 GiB) and went unbuffered
27        {"96 GB, 0.1.38's inputs (budget 72, all shards)", 83.0, 72.0, 103.7, false},
28        {"96 GB, budget 72, before the copy (arena = all experts)", 83.0, experts, 0.0, true},
29        {"96 GB, budget 72, copy built (48.94 resident)", 83.0 - 48.94, 0.0, experts - 48.94, true},
30        {"96 GB, budget 40, before the copy", 83.0, 40.0, experts - 40.0, true},
31        {"96 GB, budget 40, copy built", 83.0 - 40.0, 0.0, experts - 40.0, true},
32        // #357/#362's low-RAM PCs stay unbuffered
33        {"64 GB, budget 40, before the copy", 55.0, 40.0, experts - 40.0, false},
34        {"64 GB, budget 40, copy built", 15.0, 0.0, experts - 40.0, false},
35        {"32 GB, budget 16, before the copy", 26.0, 16.0, experts - 16.0, false},
36        {"32 GB, budget 16, copy built", 10.0, 0.0, experts - 16.0, false},
37        {"32 GB, Q2_0 pack (34 GiB experts), budget 8", 26.0, 8.0, 34.0 - 8.0, false},
38        {"nothing available", 0.0, 0.0, 1.0, false},
39        {"arena larger than the RAM", 10.0, 20.0, 0.0, true},
40    };
41    int fail = 0;
42    for (const Case& c : cases) {
43        const bool got = file_cache_keeps(gib(c.avail), gib(c.arena), gib(c.read));
44        std::printf("file_cache_keeps %-58s -> %s%s\n", c.what, got ? "buffered" : "unbuffered",
45                    got == c.keeps ? "" : "   <-- WRONG");
46        fail |= got != c.keeps;
47    }
48    // #1194, the Linux file tier: a skewed read set, so a twentieth of it (at least 1.5 GiB) is room enough
49    const Case linux_cases[] = {
50        {"#1194: 31 GB + 12 GB GPU, budget 18 built (6.4 avail, 13.6 read)", 6.4, 0.0, 13.6, true},
51        {"#1194: before the copy (avail 25, arena 18, read 13.6)", 25.0, 18.0, 13.6, true},
52        {"32 GB cgroup, budget 18, copy built (12.1 avail, 22 read)", 12.1, 0.0, 22.0, true},
53        {"32 GB cgroup, budget 24, copy built (3.8 avail, 16 read)", 3.8, 0.0, 16.0, true},
54        {"20 GB cgroup, budget 12, copy built (4.7 avail, 28 read)", 4.7, 0.0, 28.0, true},
55        {"12 GB cgroup, budget 6, copy built (3.5 avail, 34 read)", 3.5, 0.0, 34.0, true},
56        {"no room beside the margin (1 avail, 47 read)", 1.0, 0.0, 47.0, false},
57        {"room under the 1.5 GiB floor (2.4 avail, 47 read)", 2.4, 0.0, 47.0, false},
58        {"room a twentieth of the reads (3.4 avail, 47 read)", 3.4, 0.0, 47.0, true},
59        {"room just under that (3.3 avail, 47 read)", 3.3, 0.0, 47.0, false},
60        {"a big read set needs its twentieth (4 avail, 200 read)", 4.0, 0.0, 200.0, false},
61        {"a big read set with that room (12 avail, 200 read)", 12.0, 0.0, 200.0, true},
62        {"read less than the floor (2.5 avail, 1 read)", 2.5, 0.0, 1.0, true},
63        {"nothing available", 0.0, 0.0, 1.0, false},
64        {"arena larger than the RAM", 10.0, 20.0, 5.0, false},
65    };
66    for (const Case& c : linux_cases) {
67        const bool got = file_cache_keeps(gib(c.avail), gib(c.arena), gib(c.read), 1ull << 30, 0.05, 3ull << 29);
68        std::printf("file_cache_keeps (Linux file tier) %-52s -> %s%s\n", c.what, got ? "buffered" : "unbuffered",
69                    got == c.keeps ? "" : "   <-- WRONG");
70        fail |= got != c.keeps;
71    }
72    return fail;
73}
74}  // namespace
75
76// #3237: the drive probe's decision
77int file_tier_prefers_direct_cases() {
78    using strata::platform::file_tier_prefers_direct;
79    struct Case { const char* what; double held, direct, cached; bool direct_wins; };
80    const Case cases[] = {
81        {"#3237: 13% held, 8.18 vs 4.93 GB/s", 0.13, 8.18e9, 4.93e9, true},
82        {"P1004 (measured): 1.65 vs 0.81 GB/s, 19% held: a slow drive keeps the cache", 0.19, 1.65e9, 0.81e9, false},
83        {"a fast drive, the same ratio as P1004 (8 vs 4 GB/s)", 0.19, 8.0e9, 4.0e9, true},
84        {"just under the 4 GB/s floor", 0.10, 3.9e9, 1.0e9, false},
85        {"a third held, direct 1.4x: not enough", 0.35, 5.6e9, 4.0e9, false},
86        {"a third held, direct 1.6x", 0.35, 6.4e9, 4.0e9, true},
87        {"under 30% held, direct 1.3x", 0.20, 5.2e9, 4.0e9, true},
88        {"under 30% held, direct 1.2x", 0.20, 4.8e9, 4.0e9, false},
89        {"half held or more: the cache, however fast the drive", 0.5, 9e9, 1e9, false},
90        {"no cached-path figure", 0.1, 5e9, 0.0, false},
91        {"no direct figure", 0.1, 0.0, 5e9, false},
92        {"a hard disk (direct 2x, under the floor): the cache", 0.1, 0.2e9, 0.1e9, false},
93    };
94    int fail = 0;
95    for (const Case& c : cases) {
96        const bool got = file_tier_prefers_direct(c.held, c.direct, c.cached);
97        std::printf("file_tier_prefers_direct %-62s -> %s%s\n", c.what, got ? "unbuffered" : "cache",
98                    got == c.direct_wins ? "" : "   <-- WRONG");
99        fail |= got != c.direct_wins;
100    }
101    return fail;
102}
103
104int main() {
105    int fail_keeps = file_cache_keeps_cases() | file_tier_prefers_direct_cases();
106    const uint64_t bytes = 256ull << 20;
107    void* p = std::malloc(bytes);
108    if (p == nullptr) return 2;
109    std::memset(p, 1, bytes);
110    const strata::platform::LockResult r = strata::platform::lock_resident(p, bytes);
111    std::printf("lock_resident: ok=%d locked=%llu MiB (%s)\n", (int) r.ok, (unsigned long long) (r.locked_bytes >> 20),
112                r.note.c_str());
113    // The system refused the lock (a container or a shell with a small `ulimit -l`: RLIMIT_MEMLOCK of 8 MiB, no CAP_IPC_LOCK): nothing was locked, so the
114    // lock itself cannot be tested here.  The cases above still count; the test then reports "skipped" (exit 77) with the reason, not a failure.
115    const bool lock_refused = !r.ok && r.locked_bytes == 0;
116    int fail = (lock_refused ? 0 : !(r.ok && r.locked_bytes == bytes)) | fail_keeps;
117#if defined(_WIN32)
118    PROCESS_MEMORY_COUNTERS pmc{};
119    GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof pmc);
120    std::printf("working set %llu MiB\n", (unsigned long long) (pmc.WorkingSetSize >> 20));
121    fail |= pmc.WorkingSetSize < bytes;
122#endif
123    strata::platform::unlock_resident(p, bytes);
124    std::free(p);
125    if (!fail && lock_refused) {
126        std::printf("platform_memory_test: SKIPPED, the system refused to lock %llu MiB (%s); raise the memory-lock limit to run it\n",
127                    (unsigned long long) (bytes >> 20), r.note.c_str());
128        return 77;
129    }
130    std::printf("platform_memory_test: %s\n", fail ? "FAILED" : "OK");
131    return fail;
132}