rippled
Loading...
Searching...
No Matches
MallocTrim.h
1#pragma once
2
3#include <xrpl/beast/utility/Journal.h>
4
5#include <chrono>
6#include <cstdint>
7#include <string_view>
8
9namespace xrpl {
10
11// cSpell:ignore ptmalloc
12
13// -----------------------------------------------------------------------------
14// Allocator interaction note:
15// - This facility invokes glibc's malloc_trim(0) on Linux/glibc to request that
16// ptmalloc return free heap pages to the OS.
17// - If an alternative allocator (e.g. jemalloc or tcmalloc) is linked or
18// preloaded (LD_PRELOAD), calling glibc's malloc_trim typically has no effect
19// on the *active* heap. The call is harmless but may not reclaim memory
20// because those allocators manage their own arenas.
21// - Only glibc sbrk/arena space is eligible for trimming; large mmap-backed
22// allocations are usually returned to the OS on free regardless of trimming.
23// - Call at known reclamation points (e.g., after cache sweeps / online delete)
24// and consider rate limiting to avoid churn.
25// -----------------------------------------------------------------------------
26
28{
29 bool supported{false};
30 int trimResult{-1};
36
37 [[nodiscard]] std::int64_t
38 deltaKB() const noexcept
39 {
40 if (rssBeforeKB < 0 || rssAfterKB < 0)
41 return 0;
42 return rssAfterKB - rssBeforeKB;
43 }
44};
45
70MallocTrimReport
72
73} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:40
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
MallocTrimReport mallocTrim(std::string_view tag, beast::Journal journal)
Attempt to return freed memory to the operating system.
std::int64_t majfltDelta
Definition MallocTrim.h:35
std::int64_t minfltDelta
Definition MallocTrim.h:34
std::chrono::microseconds durationUs
Definition MallocTrim.h:33
std::int64_t deltaKB() const noexcept
Definition MallocTrim.h:38
std::int64_t rssAfterKB
Definition MallocTrim.h:32
std::int64_t rssBeforeKB
Definition MallocTrim.h:31