system: improve handling around GetTotalRAM() #33435

pull vasild wants to merge 2 commits into bitcoin:master from vasild:fix_unused_variable_in_GetTotalRAM changing 4 files +44 −16
  1. vasild commented at 1:06 pm on September 19, 2025: contributor
    1. Fix unused variable warning (https://github.com/bitcoin/bitcoin/pull/33333#discussion_r2362493046)
    2. Enable GetTotalRAM() on other platforms where it was tested to work.
    3. Skip the GetTotalRAM() unit test on unsupported platforms.

    Prior discussion: #33333 (review)

  2. DrahtBot commented at 1:06 pm on September 19, 2025: contributor

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

    Code Coverage & Benchmarks

    For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/33435.

    Reviews

    See the guideline for information on the review process.

    Type Reviewers
    ACK hebasto, l0rinc

    If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

  3. in src/common/system.cpp:119 in 2b6497b035
    112@@ -113,10 +113,10 @@ int GetNumCores()
    113 
    114 std::optional<size_t> GetTotalRAM()
    115 {
    116-    auto clamp{[](uint64_t v) { return size_t(std::min(v, uint64_t{std::numeric_limits<size_t>::max()})); }};
    117+    [[maybe_unused]] auto clamp{[](uint64_t v) { return size_t(std::min(v, uint64_t{std::numeric_limits<size_t>::max()})); }};
    118 #ifdef WIN32
    119     if (MEMORYSTATUSEX m{}; (m.dwLength = sizeof(m), GlobalMemoryStatusEx(&m))) return clamp(m.ullTotalPhys);
    120-#elif defined(__linux__) || defined(__APPLE__)
    121+#elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__)
    


    hebasto commented at 1:35 pm on September 19, 2025:
    0#elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__illumos__)
    

    hebasto commented at 2:42 pm on September 19, 2025:

    Here are the relevant docs:


    l0rinc commented at 8:27 pm on September 19, 2025:

    Based on https://github.com/bitcoin/bitcoin/blob/56c6daa64f6bc29fb83f3ec8940c37ddc549edeb/src/common/system.cpp#L79 I also suggest something similar:

    0#elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
    

    hebasto commented at 8:33 pm on September 19, 2025:

    @l0rinc

    Why are you suggesting skipping illumos-based distros?


    l0rinc commented at 8:34 pm on September 19, 2025:
    I’m not suggesting that, I just cannot personally ACK since there’s no way for me to test that, but if you do, that’s enough for me.

    hebasto commented at 9:37 pm on September 19, 2025:

    I have also tested locally on the following system:

    0$ uname -snrv
    1SunOS openindiana 5.11 illumos-ee653ea2dd
    

    If ... || defined(__illumos__) is not appended, the following test fails:

    0$ ./build/bin/test_bitcoin -t system_tests/total_ram
    1Running 1 test case...
    2./test/system_tests.cpp(23): error: in "system_tests/total_ram": check GetTotalRAM() >= 1000_MiB has failed [std::nullopt < 1048576000]
    3
    4*** 1 failure is detected in the test module "Bitcoin Core Test Suite"
    

    l0rinc commented at 9:43 pm on September 19, 2025:

    Perfect, that’s why I have added the test so that we can detect these. Does it display the correct value if we print GetTotalRAM()?

     0diff --git a/src/test/system_tests.cpp b/src/test/system_tests.cpp
     1--- a/src/test/system_tests.cpp	(revision 50ba74ae2cb97aa9052f4e20385dff3afdb6d7cd)
     2+++ b/src/test/system_tests.cpp	(revision c968ae0b75785f6cc70d80f649a45b827a80b6b0)
     3@@ -21,6 +21,7 @@
     4 BOOST_AUTO_TEST_CASE(total_ram)
     5 {
     6     BOOST_CHECK_GE(GetTotalRAM(), 1000_MiB);
     7+    std::cout << "GetTotalRAM() = " << GetTotalRAM() << std::endl;
     8 
     9     if constexpr (SIZE_MAX == UINT64_MAX) {
    10         // Upper bound check only on 64-bit: 32-bit systems can reasonably have max memory,
    

    FreeBSD and OpenBSD seem to be detected correctly based on https://github.com/l0rinc/bitcoin/actions/runs/17870295239/job/50822293463?pr=39 (still waiting for NetBSD)


    hebasto commented at 9:56 pm on September 19, 2025:

    Does it display the correct value if we print GetTotalRAM()?

    Yes, it does.


    l0rinc commented at 10:26 pm on September 19, 2025:

    vasild commented at 5:30 am on September 20, 2025:
    Done.
  4. hebasto commented at 1:36 pm on September 19, 2025: member
    Concept ACK.
  5. hebasto added this to the milestone 30.0 on Sep 19, 2025
  6. l0rinc commented at 6:41 pm on September 19, 2025: contributor

    Thanks @hebasto for finding it, thanks @vasild for the quick PR.

    I’m working on trying to reproduce this on the mentioned platforms, but there’s a reason I didn’t add them originally since I couldn’t test them via godbolt (https://xania.org/202506/how-compiler-explorer-works indicates they don’t have native BSD or s390x servers). @vasild if you edit again, can you please add @hebasto as a coauthor for the change?

    Concept ACK for both changes in the PR (the maybe unused + adding BSD), I’m trying to find a way to test it myself before fully ACKing.

  7. l0rinc commented at 8:31 pm on September 19, 2025: contributor

    I was able to reproduce the problem with FreeBSD by adding a dummy CI job:

    error: unused variable ‘clamp’

    After applying the PR the test passes successfully:

    test/system_tests.cpp(23): info: check GetTotalRAM() >= 1000_MiB has passed GetTotalRAM() = 4258750464 test/system_tests.cpp(29): info: check GetTotalRAM() < 10'000'000_MiB has passed

    Which coincides with the set memory: 4G.

    Based on the BSD definitions in the same file I left a suggestion but I’m also fine with merging as-is.

    ACK 2b6497b03585922519a16f782be8f6c83e55ab29

  8. DrahtBot requested review from hebasto on Sep 19, 2025
  9. hebasto commented at 9:42 pm on September 19, 2025: member
    Additionally, I suggest guarding this check:https://github.com/bitcoin/bitcoin/blob/56c6daa64f6bc29fb83f3ec8940c37ddc549edeb/src/test/system_tests.cpp#L23 with the same #ifdefs as in the GetTotalRAM() implementation. Otherwise, this test will fail on any unmentioned system.
  10. DrahtBot requested review from hebasto on Sep 19, 2025
  11. l0rinc commented at 9:45 pm on September 19, 2025: contributor

    Otherwise, this test will fail on any unmentioned system

    That’s exactly why it was added so that we can detect and cover those. Is it too strict?

  12. hebasto commented at 9:54 pm on September 19, 2025: member

    Otherwise, this test will fail on any unmentioned system

    That’s exactly why it was added so that we can detect and cover those. Is it too strict?

    We usually avoid breaking people’s tests just to collect coverage data :)

  13. l0rinc commented at 9:57 pm on September 19, 2025: contributor
    I assumed we want to be notified about unsupported systems, is that not the case? Edit: If we do want to make sure unsupported systems pass gracefully, can we at least add a warning test log for those cases?
  14. hebasto commented at 10:11 pm on September 19, 2025: member

    If we do want to make sure unsupported systems pass gracefully, can we at least add a warning test log for those cases?

    I’d say that the total_ram test should be:

    1. Skipped on unsupported platforms with an explanatory message;
    2. Placed into its own test suite so that ctest can clearly distinguish and report the skipped test in the statistics.
  15. l0rinc commented at 10:41 pm on September 19, 2025: contributor
    Good idea @hebasto, @vasild can you please cherry-pick l0rinc/bitcoin@f011c2c (#39) on top of the PR? Please add @hebasto as a coauthor.
  16. vasild force-pushed on Sep 20, 2025
  17. vasild commented at 5:29 am on September 20, 2025: contributor

    2b6497b035...8fcf71ca00: pick suggestions

    cherry-pick l0rinc/bitcoin@f011c2c

    done with some minor changes, see git range-diff f011c2cd~..f011c2cd 8fcf71ca00~..8fcf71ca00

  18. vasild renamed this:
    system: silence unused variable warning and make GetTotalRAM() work on FreeBSD
    system: improve handling around GetTotalRAM()
    on Sep 20, 2025
  19. l0rinc approved
  20. l0rinc commented at 6:07 am on September 20, 2025: contributor

    ACK 8fcf71ca005449e639f189dec7ec0163a07e6d37

    Reproduced the unused clamp on FreeBSD and that the fix returns the correct memory on FreeBSD and NetBSD and OpenBSD as well (__illumos__ was tested by @hebasto). I have also tested that on systems that don’t support the memory query the test is gracefully skipped and reported as skipped.

    Thanks for the quick fix.

  21. in src/common/system.cpp:125 in 8fcf71ca00 outdated
    122+      defined(__FreeBSD__) || \
    123+      defined(__NetBSD__) || \
    124+      defined(__OpenBSD__) || \
    125+      defined(__illumos__) || \
    126+      defined(__linux__)
    127     if (long p{sysconf(_SC_PHYS_PAGES)}, s{sysconf(_SC_PAGESIZE)}; p > 0 && s > 0) return clamp(1ULL * p * s);
    


    vasild commented at 7:08 am on September 20, 2025:

    By the way:

    0warning: implicit conversion changes signedness: 'long' to 'unsigned long long' [-Wsign-conversion]
    1... return clamp(1ULL * p * s);
    2...                   ~ ^
    

    changing it to

    0clamp(static_cast<uint64_t>(p) * static_cast<uint64_t>(s))
    

    silences that. -Wsign-conversion also enabled by -Wconversion is not used by Bitcoin Core, so we don’t see it when compiling. In this case the code is ok because values are explicitly checked that they are greater than 0 before doing the implicit conversion. Maybe do nothing.

  22. in src/test/system_ram_tests.cpp:4 in 8fcf71ca00
    0@@ -0,0 +1,33 @@
    1+// Copyright (c) 2025-present The Bitcoin Core developers
    2+// Distributed under the MIT software license, see the accompanying
    3+// file COPYING or http://www.opensource.org/licenses/mit-license.php.
    4+//
    


    hebasto commented at 5:27 pm on September 20, 2025:
    0// file COPYING or https://opensource.org/license/mit/.
    

    vasild commented at 10:27 am on September 22, 2025:
    Done.
  23. in src/common/system.cpp:124 in 8fcf71ca00 outdated
    121+#elif defined(__APPLE__) || \
    122+      defined(__FreeBSD__) || \
    123+      defined(__NetBSD__) || \
    124+      defined(__OpenBSD__) || \
    125+      defined(__illumos__) || \
    126+      defined(__linux__)
    


    hebasto commented at 5:29 pm on September 20, 2025:

    4ebf456dc589a993faa73b8b377c0d1919fbd577

    Could you please update the commit message so it reflects the changes?


    vasild commented at 10:27 am on September 22, 2025:
    Done.
  24. hebasto approved
  25. hebasto commented at 5:29 pm on September 20, 2025: member
    ACK 8fcf71ca005449e639f189dec7ec0163a07e6d37.
  26. system: improve handling around GetTotalRAM()
    This patch achieves two things:
    1. Fix unused variable warning (https://github.com/bitcoin/bitcoin/pull/33333#discussion_r2362493046)
    2. Enable GetTotalRAM() on other platforms where it was tested to work.
    
    Co-authored-by: Hennadii Stepanov <32963518+hebasto@users.noreply.github.com>
    337a6e7386
  27. test: split out `system_ram_tests` to signal when total ram cannot be determined
    when `GetTotalRAM` returns an `std::nullopt` now we're getting:
    ```
    The following tests did not run:
            106 - system_ram_tests (Skipped)
    ```
    56791b5829
  28. vasild force-pushed on Sep 22, 2025
  29. vasild commented at 10:27 am on September 22, 2025: contributor
    8fcf71ca00...56791b5829: take suggestions
  30. hebasto approved
  31. hebasto commented at 10:31 am on September 22, 2025: member
    ACK 56791b582958e905e5ba5cbf172a8ea7dad1a8b0.
  32. DrahtBot requested review from l0rinc on Sep 22, 2025
  33. l0rinc commented at 3:00 pm on September 22, 2025: contributor
    ACK 56791b582958e905e5ba5cbf172a8ea7dad1a8b0
  34. hebasto merged this on Sep 22, 2025
  35. hebasto closed this on Sep 22, 2025

  36. hebasto added the label Needs backport (30.x) on Sep 22, 2025
  37. fanquake removed the label Needs backport (30.x) on Sep 22, 2025
  38. fanquake commented at 8:50 pm on September 22, 2025: member
    Backported to 30.x in #33435.
  39. fanquake referenced this in commit 1544d95cf5 on Sep 22, 2025
  40. fanquake referenced this in commit 2db6dde838 on Sep 22, 2025


vasild DrahtBot hebasto l0rinc fanquake


l0rinc

Milestone
30.0


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: 2025-09-26 15:13 UTC

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