memusage: let PoolResource keep track of all allocated/deallocated memory #28939

pull martinus wants to merge 2 commits into bitcoin:master from martinus:2023-11-more-accurate-pool-memory changing 4 files +147 −31
  1. martinus commented at 6:54 pm on November 25, 2023: contributor

    We recently (in #28906) had OOM problems due to incorrect memory usage estimation. When PoolResource is used the estimation can be brittle because when implementing memusage estimation for a container it is not obvious when the pool can be used.

    As suggested here #28906 (comment), to prevent these problems in future we can simply let the PoolResource do all the accounting. Then the memory usage estimation is always accurate, even when the pool’s memory chunks cannot be used.

  2. DrahtBot commented at 6:54 pm on November 25, 2023: contributor

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

    Code Coverage

    For detailed information about the code coverage, see the test coverage report.

    Reviews

    See the guideline for information on the review process.

    Type Reviewers
    Concept ACK hebasto, jonatack

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

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #31102 (Don’t wipe coins cache when full and instead evict LRU clean entries by andrewtoth)
    • #28531 (improve MallocUsage() accuracy by LarryRuane)

    If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

  3. hebasto commented at 6:57 pm on November 25, 2023: member
    Concept ACK.
  4. in src/malloc_usage.h:1 in 67d885cf63 outdated
    0@@ -0,0 +1,30 @@
    1+// Copyright (c) 2015-2023 The Bitcoin Core developers
    


    jonatack commented at 3:18 pm on November 28, 2023:

    Per iwyu in https://cirrus-ci.com/task/6429107576111104 (“run ci”):

    • add #include <malloc_usage.h> to src/test/disconnected_transactions.cpp, src/test/pool_tests.cpp, and src/txmempool.cpp

    • grep the output also for memusage.h

    nit, start year, and I think we’re doing this now to avoid future need to update

    0// Copyright (c) 2023-present The Bitcoin Core developers
    

    martinus commented at 5:42 pm on November 28, 2023:
    Cool, I didn’t know that iwyu is run in the CI, I’ll change these :+1:
  5. in src/support/allocators/pool.h:282 in 67d885cf63 outdated
    277@@ -266,6 +278,11 @@ class PoolResource final
    278     {
    279         return m_chunk_size_bytes;
    280     }
    281+
    282+    [[nodiscard]] size_t DynamicMemoryUsage() const
    


    jonatack commented at 4:22 pm on November 28, 2023:
    Naming question, would it be a good idea to use a different name (say, PoolMemoryUsage) to distinguish it while grepping from the DynamicMemoryUsage elsewhere in the codebase?
  6. jonatack commented at 4:28 pm on November 28, 2023: member

    Concept ACK

    A couple of minor comments/questions on first look, feel free to defer until you need to update for more substantive feedback.

  7. in src/malloc_usage.h:16 in 67d885cf63 outdated
    11+namespace memusage {
    12+
    13+/** Compute the total memory used by allocating alloc bytes. */
    14+static inline size_t MallocUsage(size_t alloc)
    15+{
    16+    // Measured on libc6 2.19 on Linux.
    


    jonatack commented at 4:33 pm on November 28, 2023:

    martinus commented at 7:42 pm on November 28, 2023:
    I still think that comment provides value, it shows where the data came from. E.g. it makes sense to update it to newer glibc versions of they differ (they do a bit) or to at least check if they are doing something dramatically different than the version it is based on.
  8. DrahtBot added the label CI failed on Jan 16, 2024
  9. achow101 requested review from laanwj on Apr 9, 2024
  10. achow101 requested review from hebasto on Apr 9, 2024
  11. martinus force-pushed on Apr 18, 2024
  12. DrahtBot removed the label CI failed on Apr 18, 2024
  13. DrahtBot added the label Needs rebase on Sep 2, 2024
  14. memusage: move MallocUsage into separate file
    That way it becomes usable in other code like allocators/pool
    8cf21689e6
  15. memusage: pool keeps track of its memory usage
    That way all containers that use the pool have accurate memory tracking.
    
    Add test to show memory is accurately tracked, even when nodes can't be allocated by the pool.
    The more accurate memory estimation interferes shows that our memory estimation for Windows
    is off, as the real allocated memory is much higher. This adapts the memusage_test test so it still
    works with the more correct estimation.
    818fbf03a6
  16. in src/Makefile.am:200 in 46fa057e5f outdated
    196@@ -197,6 +197,7 @@ BITCOIN_CORE_H = \
    197   key_io.h \
    198   logging.h \
    199   logging/timer.h \
    200+  malloc_usage.h \
    


    jonatack commented at 3:57 pm on September 12, 2024:
    @martinus PR needs rebase/update to cmake. This makefile (src/Makefile.am) has been removed.
  17. martinus force-pushed on Sep 19, 2024
  18. DrahtBot removed the label Needs rebase on Sep 19, 2024
  19. andrewtoth commented at 3:46 pm on September 22, 2024: contributor
    Could we also track available memory in the freelist and expose it publicly? Right now when the dbcache fills up, our only option is to clear it and reallocate. We don’t have any insight into how much memory is available in the freelist. We can’t just evict a bunch of least recently added clean entries during a periodic flush, because the memory usage reported by the map will be the same. Having the amount of freed memory can let us make more granular decisions on when to actually reallocate the cache map.
  20. l0rinc commented at 7:58 am on October 17, 2024: contributor
    @martinus are you still working on this or should we review the cherry-picked changes in #31102 instead?
  21. martinus commented at 8:14 am on October 17, 2024: contributor
    Hi @l0rinc, sorry I most likely can’t work on this or anything else in the forseeable future
  22. fanquake added the label Up for grabs on Oct 17, 2024
  23. fanquake closed this on Oct 17, 2024

  24. l0rinc commented at 8:18 am on October 17, 2024: contributor
    Thanks for the quick response, @andrewtoth cherry-picked your changes and we’ll continue there
  25. martinus deleted the branch on Oct 17, 2024
  26. fanquake removed the label Up for grabs on Oct 21, 2024

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: 2024-12-04 06:12 UTC

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