okami.mom/strata

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

Niko1221Merge f-gfx11-arrival 6d8e42886 into f-p4-int (the owner's wording is final: the [DRAFT] marks come off the two log lines and the register note)a1bbe27b4

main
7.8 KiB177 linesraw
1// the file-cache give-back during and after the expert load (include/strata/core/page_cache_drop.hpp).  No GPU.  Cases: the flag and the
2// switch, one range and one whole file of a temporary file, every error path (empty path, missing file, bad descriptor, empty range) ignored,
3// the final call over a mixed list (a missing file and a repeated path among good ones), the meminfo reader, the draft line.  (The device rule
4// is checked in src/core/arch_defaults_test.cpp.)
5#include "strata/core/page_cache_drop.hpp"
6
7#include <cstdint>
8#include <cstdio>
9#include <cstdlib>
10#include <string>
11#include <vector>
12
13#if defined(__linux__)
14#include <fcntl.h>
15#include <sys/mman.h>
16#include <sys/stat.h>
17#include <unistd.h>
18#endif
19
20using namespace strata::core;
21namespace pcd = strata::core::page_cache_drop;
22
23namespace {
24int failed = 0;
25#define CHECK(x) do { if (!(x)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #x); ++failed; } } while (0)
26
27#if defined(__linux__)
28// a temporary file of `bytes` bytes of a pattern, written through, flushed and read back once (so its pages are clean and cached); "" on failure
29std::string make_file(size_t bytes) {
30    char path[] = "/tmp/page_cache_drop_test_XXXXXX";
31    const int fd = ::mkstemp(path);
32    if (fd < 0) return std::string();
33    std::vector<unsigned char> buf(bytes);
34    for (size_t i = 0; i < bytes; ++i) buf[i] = (unsigned char) (i * 131 + 7);
35    bool ok = ::write(fd, buf.data(), bytes) == (ssize_t) bytes && ::fdatasync(fd) == 0;
36    std::vector<unsigned char> back(bytes);
37    ok = ok && ::pread(fd, back.data(), bytes, 0) == (ssize_t) bytes;
38    ::close(fd);
39    if (!ok) { ::unlink(path); return std::string(); }
40    return std::string(path);
41}
42// how many pages of the file are resident (mincore); -1 when it cannot be told
43long resident_pages(const std::string& path) {
44    const int fd = ::open(path.c_str(), O_RDONLY);
45    if (fd < 0) return -1;
46    struct stat st {};
47    if (::fstat(fd, &st) != 0 || st.st_size == 0) { ::close(fd); return -1; }
48    void* m = ::mmap(nullptr, (size_t) st.st_size, PROT_READ, MAP_SHARED, fd, 0);
49    if (m == MAP_FAILED) { ::close(fd); return -1; }
50    const size_t page = (size_t) ::sysconf(_SC_PAGESIZE);
51    const size_t n = ((size_t) st.st_size + page - 1) / page;
52    std::vector<unsigned char> vec(n);
53    long r = ::mincore(m, (size_t) st.st_size, vec.data()) == 0 ? 0 : -1;
54    if (r == 0) for (unsigned char c : vec) r += c & 1;
55    ::munmap(m, (size_t) st.st_size);
56    ::close(fd);
57    return r;
58}
59#endif
60}  // namespace
61
62int main() {
63    // 1. the flag and the switch
64    {
65        arena_cache_drop_flag().store(false);
66        CHECK(!pcd::enabled());
67#if defined(__linux__)
68        ::unsetenv("STRATA_NO_ARENA_CACHE_DROP");
69        arena_cache_drop_flag().store(true);
70        CHECK(pcd::enabled());
71        ::setenv("STRATA_NO_ARENA_CACHE_DROP", "1", 1);
72        CHECK(!pcd::enabled() && pcd::off_by_env());
73        ::setenv("STRATA_NO_ARENA_CACHE_DROP", "0", 1);
74        CHECK(pcd::enabled() && !pcd::off_by_env());
75        ::setenv("STRATA_NO_ARENA_CACHE_DROP", "", 1);
76        CHECK(pcd::enabled() && !pcd::off_by_env());
77        ::unsetenv("STRATA_NO_ARENA_CACHE_DROP");
78#else
79        arena_cache_drop_flag().store(true);
80        CHECK(!pcd::enabled());   // never off Linux
81#endif
82        arena_cache_drop_flag().store(false);
83    }
84#if defined(__linux__)
85    // 2. one range and one whole file of a real file: the calls work, the advised bytes add up, the file is unchanged
86    {
87        const size_t sz = 4u << 20;
88        const std::string p = make_file(sz);
89        CHECK(!p.empty());
90        if (!p.empty()) {
91            const int fd = ::open(p.c_str(), O_RDONLY);
92            CHECK(fd >= 0);
93            const uint64_t a0 = pcd::advised_bytes().load();
94            CHECK(pcd::drop_range(fd, 0, 1u << 20));
95            CHECK(pcd::advised_bytes().load() == a0 + (1u << 20));
96            CHECK(pcd::drop_range(fd, 1u << 20, sz - (1u << 20)));
97            CHECK(pcd::advised_bytes().load() == a0 + sz);
98            CHECK(pcd::drop_range(fd, 100u << 20, 4096));   // beyond the end: advice, not an error
99            const long before = resident_pages(p);
100            CHECK(pcd::drop_file(p));
101            const long after = resident_pages(p);
102            CHECK(before < 0 || after < 0 || after <= before);   // (tmpfs keeps its pages: only "not more" is required)
103            std::vector<unsigned char> back(sz);
104            CHECK(::pread(fd, back.data(), sz, 0) == (ssize_t) sz);
105            bool same = true;
106            for (size_t i = 0; i < sz && same; ++i) same = back[i] == (unsigned char) (i * 131 + 7);
107            CHECK(same);   // what a later reader needs is read again, unchanged
108            ::close(fd);
109            ::unlink(p.c_str());
110        }
111    }
112    // 3. every error path is ignored and returns false; nothing is added to the advised bytes
113    {
114        const uint64_t a0 = pcd::advised_bytes().load();
115        CHECK(!pcd::drop_range(-1, 0, 4096));
116        CHECK(!pcd::drop_range(0x7fffffff, 0, 4096));   // not an open descriptor
117        const std::string p = make_file(8192);
118        const int fd = p.empty() ? -1 : ::open(p.c_str(), O_RDONLY);
119        CHECK(fd >= 0);
120        CHECK(!pcd::drop_range(fd, 0, 0));              // an empty range
121        ::close(fd);
122        CHECK(!pcd::drop_range(fd, 0, 4096));           // closed
123        if (!p.empty()) ::unlink(p.c_str());
124        CHECK(pcd::advised_bytes().load() == a0);
125        CHECK(!pcd::drop_file(""));
126        CHECK(!pcd::drop_file("/nonexistent/page_cache_drop_test/file.bin"));
127        CHECK(!pcd::drop_file("/proc/self/mem/x"));      // a path that cannot be opened
128    }
129    // 4. the final call over a mixed list: a missing file, an empty path and a repeated path among good ones
130    {
131        const std::string a = make_file(1u << 20), b = make_file(1u << 20);
132        CHECK(!a.empty() && !b.empty());
133        const pcd::Outcome o = pcd::give_back({a, "/nonexistent/page_cache_drop_test/x.bin", "", b, a});
134        CHECK(o.files == 3);    // a, the missing one, b (the empty path and the repeat are left out)
135        CHECK(o.failed == 1);   // the missing one, ignored
136        CHECK(o.ms >= 0.0);
137        CHECK(o.cached_before > 0 && o.cached_after > 0);   // /proc/meminfo is readable
138        const pcd::Outcome none = pcd::give_back({});
139        CHECK(none.files == 0 && none.failed == 0);
140        if (!a.empty()) ::unlink(a.c_str());
141        if (!b.empty()) ::unlink(b.c_str());
142    }
143    // 5. the meminfo reader
144    {
145        CHECK(pcd::meminfo_bytes("Cached") > 0);
146        CHECK(pcd::meminfo_bytes("MemFree") > 0);
147        CHECK(pcd::meminfo_bytes("NoSuchKey") == 0);
148        CHECK(pcd::meminfo_bytes("Cach") == 0);   // a prefix is not a key
149    }
150#endif
151    // 6. the draft line: nothing when nothing was given back, a line otherwise, with the numbers and the switch
152    {
153        pcd::Outcome none;
154        CHECK(pcd::draft_line(none).empty());
155        pcd::Outcome o;
156        o.advised_behind = 9ull << 30;
157        o.cached_before = 7ull << 30;
158        o.cached_after = 1ull << 30;
159        o.ms = 520.0;
160        const std::string s = pcd::draft_line(o);
161        CHECK(s.rfind("the model files' cached pages were given back after the expert load: ", 0) == 0);
162        CHECK(s.find("9.0 GiB") != std::string::npos);
163        CHECK(s.find("6.0 GiB") != std::string::npos);
164        CHECK(s.find("520 ms") != std::string::npos);
165        CHECK(s.find("STRATA_NO_ARENA_CACHE_DROP=1") != std::string::npos);
166        pcd::Outcome grew;   // the cache grew meanwhile and nothing was advised behind: nothing to say
167        grew.cached_before = 1ull << 30;
168        grew.cached_after = 2ull << 30;
169        CHECK(pcd::draft_line(grew).empty());
170    }
171    if (failed != 0) {
172        std::fprintf(stderr, "page_cache_drop_test: %d check(s) failed\n", failed);
173        return 1;
174    }
175    std::printf("page_cache_drop_test: ok\n");
176    return 0;
177}