system: make GetTotalRAM() cgroup-aware on Linux #34762

pull ViniciusCestarii wants to merge 1 commits into bitcoin:master from ViniciusCestarii:system-cgroup-aware-available-ram changing 1 files +25 −1
  1. ViniciusCestarii commented at 6:44 pm on March 6, 2026: none

    sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGE_SIZE) reports host-wide physical memory, ignoring cgroup limits. Inside containers, this causes GetTotalRAM() to return the full host RAM instead of what the process is actually allowed to use.

    This PR caps the reported value with the cgroup memory limit by reading cgroup v2 (memory.max) or v1 (memory.limit_in_bytes) on Linux. When no limit is set, the host-wide value is kept unchanged.

    It also helps with #34692 where dbcache defaults depend on detected system RAM.

  2. DrahtBot commented at 6:45 pm on March 6, 2026: contributor

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    Reviews

    See the guideline for information on the review process. A summary of reviews will appear here.

  3. in src/common/system.cpp:33 in 960c817e5e
    29@@ -29,6 +30,7 @@
    30 #include <cstddef>
    31 #include <cstdint>
    32 #include <cstdlib>
    33+#include <fstream>
    


    brunoerg commented at 6:57 pm on March 6, 2026:
    960c817e5eb3bffad4b0ff42e6656dfa4b1a8659: It is only needed in __linux__ path, right? So it is not needed on all platforms.

    ViniciusCestarii commented at 7:13 pm on March 6, 2026:
    Yes, #include <util/strencodings.h> is too. I fixed both
  4. system: make GetTotalRAM() cgroup-aware on Linux e12342c585
  5. in src/common/system.cpp:143 in 960c817e5e
    139+            if (cg2 >> val && val != "max") {
    140+                if (auto limit{ToIntegral<uint64_t>(val)}) physical_ram = std::min(physical_ram, *limit);
    141+            }
    142+        } else if (std::ifstream cg1{"/sys/fs/cgroup/memory/memory.limit_in_bytes"}; cg1) {
    143+            uint64_t cgroup_limit;
    144+            if (cg1 >> cgroup_limit) physical_ram = std::min(physical_ram, cgroup_limit);
    


    brunoerg commented at 7:02 pm on March 6, 2026:

    style nit (for consistency between cgroup v1 and v2):

     0diff --git a/src/common/system.cpp b/src/common/system.cpp
     1index 862bcd8a7e..6f309b3797 100644
     2--- a/src/common/system.cpp
     3+++ b/src/common/system.cpp
     4@@ -139,8 +139,10 @@ std::optional<size_t> GetTotalRAM()
     5                 if (auto limit{ToIntegral<uint64_t>(val)}) physical_ram = std::min(physical_ram, *limit);
     6             }
     7         } else if (std::ifstream cg1{"/sys/fs/cgroup/memory/memory.limit_in_bytes"}; cg1) {
     8-            uint64_t cgroup_limit;
     9-            if (cg1 >> cgroup_limit) physical_ram = std::min(physical_ram, cgroup_limit);
    10+            std::string val;
    11+            if (cg1 >> val) {
    12+                if (auto limit{ToIntegral<uint64_t>(val)}) physical_ram = std::min(physical_ram, *limit);
    13+            }
    14         }
    15 #endif // __linux__
    16         return clamp(physical_ram);
    
  6. ViniciusCestarii force-pushed on Mar 6, 2026
  7. l0rinc commented at 8:32 pm on March 6, 2026: contributor
    This was discussed before in #34641 (review)
  8. ViniciusCestarii commented at 10:41 am on March 7, 2026: none

    I wasn’t aware of this. I agree that making it container-aware may not be worth it, so I’ll close the PR.

    It might still be useful to add a short comment in the code explaining that this behavior is intentional and not meant to be container-aware, to avoid future confusion.

  9. ViniciusCestarii closed this on Mar 7, 2026


github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-03-09 09:13 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me