okami.mom/strata

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

Niko1221merge f-split-load (pin_budget reserves on Windows) into f-split-int90a5d082b

main
3.5 KiB77 linesraw
1// pin_budget_test: the pinned-memory headroom (include/strata/core/pin_budget.hpp) with the simulated ceiling.
2// Needs a GPU for the pinned calls; without one it prints SKIP and passes.
3#include "strata/core/pin_budget.hpp"
4
5#include <cstdio>
6#include <cstdlib>
7#include <cstring>
8
9static int g_fail = 0;
10#define CHECK(c)                                                              \
11    do {                                                                      \
12        if (!(c)) { std::fprintf(stderr, "FAIL %s:%d  %s\n", __FILE__, __LINE__, #c); ++g_fail; } \
13    } while (0)
14
15#ifdef _WIN32
16#define SETENV(k, v) _putenv_s(k, v)
17#else
18#define SETENV(k, v) setenv(k, v, 1)
19#endif
20
21int main() {
22    // the singleton reads its environment on first use: set everything first
23    SETENV("STRATA_TEST_PIN_BUDGET_MIB", "100");
24    SETENV("STRATA_PIN_RESERVE_MIB", "32");   // soft 32 MiB, hard 64 MiB (a quarter of 32 is below the 64 MiB floor)
25    using namespace strata::core;
26    if (cudaFree(0) != cudaSuccess) {
27        std::printf("pin_budget_test: SKIP (no GPU)\n");
28        return 0;
29    }
30    (void) cudaGetLastError();
31
32    // 1. a registration beyond the ceiling is refused, one inside it works and is charged
33    {
34        void* big = std::malloc(160u << 20);
35        void* small = std::malloc(8u << 20);
36        CHECK(pin_register(big, 160u << 20, cudaHostRegisterPortable) == cudaErrorMemoryAllocation);
37        CHECK(pin_register(small, 8u << 20, cudaHostRegisterPortable) == cudaSuccess);
38        CHECK(pin_detail::st().sim_used == (8u << 20));
39        CHECK(pin_unregister(small) == cudaSuccess);
40        CHECK(pin_detail::st().sim_used == 0);
41        std::free(big);
42        std::free(small);
43    }
44    // 2. without a reserve a refused guarded allocation stays refused
45    {
46        void* p = nullptr;
47        CHECK(host_alloc_guarded(&p, 200u << 20, cudaHostAllocMapped) != cudaSuccess);
48        (void) cudaGetLastError();
49    }
50    // 3. the reserves are held, the arena (here: a registration) takes what is left, a required buffer is then
51    //    refused, the held headroom is handed back and the retry works
52    {
53        pin_reserve_take();
54        CHECK(pin_detail::st().hard != nullptr && pin_detail::st().hard_bytes == (64u << 20));
55        CHECK(pin_detail::st().soft != nullptr && pin_detail::st().soft_bytes == (32u << 20));
56        CHECK(pin_detail::st().sim_used == (96u << 20));
57        void* arena = std::malloc(4u << 20);
58        CHECK(pin_register(arena, 4u << 20, cudaHostRegisterPortable) == cudaSuccess);   // 100 MiB used: the ceiling
59        pin_reserve_release_soft();
60        CHECK(pin_detail::st().soft == nullptr);
61        CHECK(pin_detail::st().sim_used == (68u << 20));
62        void* a = nullptr;
63        CHECK(host_alloc_guarded(&a, 16u << 20, cudaHostAllocMapped) == cudaSuccess);   // fits beside the hard reserve
64        void* b = nullptr;
65        CHECK(host_alloc_guarded(&b, 40u << 20, cudaHostAllocMapped) == cudaSuccess);   // refused first, then the hard reserve goes
66        CHECK(pin_detail::st().hard == nullptr);
67        CHECK(pin_detail::st().hard_retries == 1);
68        void* c = nullptr;
69        CHECK(host_alloc_guarded(&c, 60u << 20, cudaHostAllocMapped) != cudaSuccess);   // truly out: reported, not hidden
70        (void) cudaGetLastError();
71        CHECK(pin_exhausted_hint().find("[pinned-memory-exhausted]") != std::string::npos);
72        (void) pin_unregister(arena);
73        std::free(arena);
74    }
75    std::printf(g_fail == 0 ? "pin_budget_test: OK\n" : "pin_budget_test: %d FAILED\n", g_fail);
76    return g_fail == 0 ? 0 : 1;
77}