okami.mom/strata

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

Niko1221Merge main 6c642df75 into f-own-int (round 16: network waits, robustness, Windows kill-on-close job start, GitHub and transfer fixes)cd9745050

main
3.8 KiB87 linesraw
1// #5697: an evicted parked conversation must give its RAM back to the system, because the physical-RAM gate that
2// evicted it reads MemAvailable right after.  With glibc the K/V segments of a parked conversation land in the heap
3// once the dynamic mmap threshold has risen (it does at the first freed segment), and free() hands back only the top of
4// the heap: an entry below a newer one stayed resident, so the gate evicted everything and still refused.
5//
6// The test parks three conversations, evicts the oldest (the one below the others) and reads the process RSS from
7// /proc/self/statm before and after.  Linux with glibc only; elsewhere it says SKIPPED.  It also prints what the
8// eviction costs in time.
9#include "strata/core/conversation_cache.hpp"
10
11#include <chrono>
12#include <cstdio>
13#include <cstdlib>
14#include <cstring>
15#include <vector>
16
17#if defined(__linux__) && defined(__GLIBC__)
18#include <unistd.h>
19
20using namespace strata::core;
21
22namespace {
23constexpr size_t MiB = size_t(1) << 20;
24constexpr size_t layers = 32, part = 8 * MiB;              // 32 layers x (K + V) x 8 MiB = 512 MiB per conversation
25
26size_t rss_bytes() {
27    std::FILE* f = std::fopen("/proc/self/statm", "r");
28    if (!f) return 0;
29    unsigned long size = 0, resident = 0;
30    const int n = std::fscanf(f, "%lu %lu", &size, &resident);
31    std::fclose(f);
32    return n == 2 ? size_t(resident) * size_t(sysconf(_SC_PAGESIZE)) : 0;
33}
34
35SavedConversation conversation(int32_t id, uint8_t fill) {
36    SavedConversation s;
37    s.live.ids = {id, id + 1, id + 2};
38    s.live.gdn.resize(64, fill);
39    s.kv.resize(layers);
40    for (auto& layer : s.kv) {
41        layer.k.resize(part, fill);                        // written: the pages are resident, as a parked image's are
42        layer.v.resize(part, fill);
43    }
44    return s;
45}
46}
47
48int main() {
49    // What the engine's first freed segment does: a freed mmapped block of this size raises glibc's mmap threshold,
50    // so the segments allocated after it come from the heap.
51    {
52        std::vector<uint8_t> warm(16 * MiB);
53        std::memset(warm.data(), 1, warm.size());
54    }
55    ConversationCache cache(size_t(8) << 30, 4);
56    for (int32_t i = 0; i < 3; ++i)
57        if (!cache.put(conversation(100 * (i + 1), uint8_t(i + 1)))) { std::fprintf(stderr, "FAIL: park %d\n", i); return 1; }
58    const size_t entry = cache.bytes() / 3;
59    const size_t before = rss_bytes();
60    const auto t0 = std::chrono::steady_clock::now();
61    const bool evicted = cache.evict_oldest();
62    const double ms = std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - t0).count();
63    const size_t after = rss_bytes();
64    const size_t returned = before > after ? before - after : 0;
65    std::printf("conversation_heap_test: parked 3 x %zu MiB, RSS %zu MiB -> %zu MiB after evicting the oldest "
66                "(%zu MiB returned to the system), eviction took %.2f ms\n",
67                entry / MiB, before / MiB, after / MiB, returned / MiB, ms);
68    if (!evicted || cache.size() != 2) { std::fprintf(stderr, "FAIL: evict_oldest\n"); return 1; }
69    if (returned < entry * 8 / 10) {
70        std::fprintf(stderr, "FAIL: the evicted conversation's RAM stayed with the process (%zu of %zu MiB returned)\n",
71                     returned / MiB, entry / MiB);
72        return 1;
73    }
74    // the second eviction: the heap holds the third conversation above it, nothing else freed
75    const auto t1 = std::chrono::steady_clock::now();
76    cache.evict_oldest();
77    std::printf("conversation_heap_test: the second eviction took %.2f ms, RSS now %zu MiB\n",
78                std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - t1).count(), rss_bytes() / MiB);
79    std::printf("conversation_heap_test: ok\n");
80    return 0;
81}
82#else
83int main() {
84    std::printf("SKIPPED: conversation_heap_test needs Linux with glibc\n");
85    return 0;
86}
87#endif