File size: 3,523 Bytes
ef28ed2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#include "core/memory_governor.hpp"

#include <algorithm>
#include <fstream>
#include <sstream>
#include <unistd.h>

namespace astra {

MemoryGovernor::MemoryGovernor(uint64_t ceiling_bytes)
    : ceiling_bytes_(ceiling_bytes) {
    register_process(getpid(), "main");
}

void MemoryGovernor::set_ceiling_bytes(uint64_t ceiling_bytes) {
    ceiling_bytes_ = std::max<uint64_t>(ceiling_bytes, 16ULL * 1024 * 1024);
}

void MemoryGovernor::register_process(pid_t pid, std::string role) {
    for (auto& [p, r] : tracked_pids_) {
        if (p == pid) {
            r = std::move(role);
            return;
        }
    }
    tracked_pids_.emplace_back(pid, std::move(role));
}

void MemoryGovernor::unregister_process(pid_t pid) {
    tracked_pids_.erase(
        std::remove_if(tracked_pids_.begin(), tracked_pids_.end(),
                       [pid](const auto& entry) { return entry.first == pid; }),
        tracked_pids_.end());
}

uint64_t MemoryGovernor::read_process_rss_bytes(pid_t pid) {
    static const uint64_t page_size = static_cast<uint64_t>(sysconf(_SC_PAGESIZE));
    std::string path = "/proc/" + std::to_string(pid) + "/statm";
    std::ifstream f(path);
    if (!f) return 0;
    uint64_t size_pages = 0, resident_pages = 0;
    if (!(f >> size_pages >> resident_pages)) return 0;
    return resident_pages * page_size;
}

uint64_t MemoryGovernor::read_process_vsize_bytes(pid_t pid) {
    static const uint64_t page_size = static_cast<uint64_t>(sysconf(_SC_PAGESIZE));
    std::string path = "/proc/" + std::to_string(pid) + "/statm";
    std::ifstream f(path);
    if (!f) return 0;
    uint64_t size_pages = 0;
    if (!(f >> size_pages)) return 0;
    return size_pages * page_size;
}

MemorySnapshot MemoryGovernor::sample(uint64_t extra_tracked_bytes) const {
    MemorySnapshot snap;
    snap.ceiling_bytes = ceiling_bytes_;
    snap.soft_watermark_bytes = (ceiling_bytes_ * 70) / 100;
    snap.hard_watermark_bytes = (ceiling_bytes_ * 85) / 100;

    uint64_t total = extra_tracked_bytes;
    for (const auto& [pid, role] : tracked_pids_) {
        uint64_t rss = read_process_rss_bytes(pid);
        uint64_t vsize = read_process_vsize_bytes(pid);
        if (rss == 0 && vsize == 0) continue;
        snap.processes.push_back(ProcessMemoryInfo{pid, role, rss, vsize});
        total += rss;
    }
    snap.total_rss_bytes = total;

    if (total >= snap.hard_watermark_bytes) {
        snap.pressure = MemoryPressure::Critical;
    } else if (total >= snap.soft_watermark_bytes) {
        snap.pressure = MemoryPressure::Soft;
    } else {
        snap.pressure = MemoryPressure::Normal;
    }
    return snap;
}

MemoryGovernor::EnforceResult MemoryGovernor::enforce(
    const std::function<void()>& on_flush_caches,
    const std::function<bool()>& on_dehydrate_lru_tab,
    uint64_t extra_tracked_bytes) const {
    EnforceResult res;
    res.before = sample(extra_tracked_bytes);

    if (res.before.pressure == MemoryPressure::Normal) {
        res.after = res.before;
        return res;
    }

    if (on_flush_caches) {
        on_flush_caches();
        res.cache_flushed = true;
    }

    MemorySnapshot cur = sample(extra_tracked_bytes);
    size_t guard = 0;
    while (cur.pressure != MemoryPressure::Normal && guard < 1024) {
        ++guard;
        if (!on_dehydrate_lru_tab || !on_dehydrate_lru_tab()) {
            break;
        }
        ++res.tabs_dehydrated;
        cur = sample(extra_tracked_bytes);
    }
    res.after = cur;
    return res;
}

} // namespace astra