upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/runtime/test/large_module_gate_test.cpp
blob: ed8d18a0cfaa2a4fde0c0508636c58510fde40ce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// vk_pipeline_check's memory budget for large modules (large_module_gate.h), without a Vulkan device.
#include "large_module_gate.h"

#include <atomic>
#include <string>
#ifndef _WIN32
#include <unistd.h>
#endif
#include <chrono>
#include <cstdio>
#include <thread>
#include <vector>

#define CHECK(condition) do {                                        \
    if (!(condition)) {                                              \
        std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); \
        return 1;                                                    \
    }                                                                \
} while (0)

using leonida::LargeModuleGate;

static int budget_and_count() {
    LargeModuleGate gate(10, 3);
    CHECK(gate.admits(4));
    gate.enter(4);
    CHECK(gate.admits(4));
    gate.enter(4);
    CHECK(!gate.admits(4));   // 12 > 10
    CHECK(gate.admits(2));    // 10 fits exactly
    gate.enter(2);
    CHECK(!gate.admits(0));   // three in: max_inflight
    gate.leave(4);
    CHECK(gate.admits(4));
    gate.leave(4);
    gate.leave(2);
    CHECK(gate.inflight() == 0);
    return 0;
}

static int oversized_module_still_gets_in_alone() {
    // Before: a module whose estimate exceeds the whole budget would wait forever.
    LargeModuleGate gate(10, 4);
    CHECK(gate.admits(100));
    gate.enter(100);
    CHECK(!gate.admits(1));   // nothing joins it
    gate.leave(100);
    CHECK(gate.admits(1));
    return 0;
}

static int max_inflight_zero_means_one() {
    LargeModuleGate gate(100, 0);
    CHECK(gate.max_inflight() == 1);
    gate.enter(1);
    CHECK(!gate.admits(1));
    gate.leave(1);
    return 0;
}

static int ticket_releases() {
    LargeModuleGate gate(10, 2);
    {
        LargeModuleGate::Ticket a(gate, 6);
        CHECK(gate.inflight() == 1);
        LargeModuleGate::Ticket moved = std::move(a);   // the moved-from ticket must not leave twice
        CHECK(gate.inflight() == 1);
        LargeModuleGate::Ticket b;
        b = LargeModuleGate::Ticket(gate, 4);
        CHECK(gate.inflight() == 2);
        b.release();
        CHECK(gate.inflight() == 1);
    }
    CHECK(gate.inflight() == 0);
    return 0;
}

static int threads_never_exceed_the_budget() {
    constexpr uint64_t kBudget = 10;
    LargeModuleGate gate(kBudget, 3);
    std::atomic<uint64_t> used{0};
    std::atomic<unsigned> in{0};
    std::atomic<bool> over{false};
    std::vector<std::thread> threads;
    for (int t = 0; t < 8; ++t) {
        threads.emplace_back([&, t] {
            for (int i = 0; i < 200; ++i) {
                const uint64_t est = 1 + uint64_t((t + i) % 5);   // 1..5
                LargeModuleGate::Ticket ticket(gate, est);
                const uint64_t u = used += est;
                const unsigned n = ++in;
                if (n > 3 || (n > 1 && u > kBudget)) over = true;
                std::this_thread::sleep_for(std::chrono::microseconds(50));
                --in;
                used -= est;
            }
        });
    }
    for (auto& th : threads) th.join();
    CHECK(!over);
    CHECK(gate.inflight() == 0);
    return 0;
}

static int estimate_and_memory_query() {
    CHECK(leonida::large_module_estimate(4u << 20) == (uint64_t(4) << 20) * leonida::kBytesPerSpirvByte);
#if defined(__linux__) || defined(_WIN32)
    CHECK(leonida::available_memory_bytes() > 0);   // Linux: MemAvailable; Windows: ullAvailPhys
#endif
    leonida::release_free_memory();   // must be callable anywhere
    return 0;
}

#if defined(__linux__)
#include <filesystem>
#include <fstream>

static void write_file(const std::filesystem::path& p, const char* text) {
    std::filesystem::create_directories(p.parent_path());
    std::ofstream(p) << text;
}

static int cgroup_limits_are_read_up_the_tree() {
    // Before: only MemAvailable was read, so under systemd-run -p MemoryMax=... (or in a container) the budget was
    // the whole machine's and the checker could be killed by its own cgroup.
    namespace fs = std::filesystem;
    const fs::path root = fs::temp_directory_path() / ("lmg_test_" + std::to_string(::getpid()));
    fs::remove_all(root);
    write_file(root / "self", "0::/user.slice/app.scope\n");
    write_file(root / "cg/user.slice/memory.max", "10000\n");
    write_file(root / "cg/user.slice/memory.current", "4000\n");          // room 6000
    write_file(root / "cg/user.slice/app.scope/memory.max", "max\n");     // no limit of its own
    write_file(root / "cg/user.slice/app.scope/memory.current", "3000\n");
    CHECK(leonida::cgroup_memory_room((root / "self").string(), (root / "cg").string()) == 6000);
    write_file(root / "cg/user.slice/app.scope/memory.max", "5000\n");    // room 2000, the tighter one
    CHECK(leonida::cgroup_memory_room((root / "self").string(), (root / "cg").string()) == 2000);
    write_file(root / "cg/user.slice/app.scope/memory.current", "7000\n"); // over its limit: no room
    CHECK(leonida::cgroup_memory_room((root / "self").string(), (root / "cg").string()) == 0);
    write_file(root / "self", "12:memory:/legacy\n");                      // cgroup v1 only: no limit read
    CHECK(leonida::cgroup_memory_room((root / "self").string(), (root / "cg").string()) == UINT64_MAX);
    CHECK(leonida::cgroup_memory_room((root / "missing").string(), (root / "cg").string()) == UINT64_MAX);
    fs::remove_all(root);
    return 0;
}
#endif

int main() {
#if defined(__linux__)
    if (int r = cgroup_limits_are_read_up_the_tree()) return r;
#endif
    if (int r = budget_and_count()) return r;
    if (int r = oversized_module_still_gets_in_alone()) return r;
    if (int r = max_inflight_zero_means_one()) return r;
    if (int r = ticket_releases()) return r;
    if (int r = threads_never_exceed_the_budget()) return r;
    if (int r = estimate_and_memory_query()) return r;
    std::puts("large_module_gate: ok");
    return 0;
}