1#include <xrpl/basics/MallocTrim.h>
3#include <xrpl/basics/Log.h>
4#include <xrpl/beast/utility/Journal.h>
6#include <boost/predef.h>
10#if defined(__GLIBC__) && BOOST_OS_LINUX
11#include <sys/resource.h>
26#error "MallocTrim rusage instrumentation requires RUSAGE_THREAD on Linux/glibc"
32getRusageThread(
struct rusage& ru)
34 return ::getrusage(RUSAGE_THREAD, &ru) == 0;
46#if defined(__GLIBC__) && BOOST_OS_LINUX
49mallocTrimWithPad(std::size_t padBytes)
51 return ::malloc_trim(padBytes);
55parseStatmRSSkB(std::string
const& statm)
59 std::istringstream iss(statm);
60 long size = 0, resident = 0;
61 if (!(iss >> size >> resident))
65 long const pageSize = ::sysconf(_SC_PAGESIZE);
69 return (resident * pageSize) / 1024;
83#if !(defined(__GLIBC__) && BOOST_OS_LINUX)
84 JLOG(journal.
debug()) <<
"malloc_trim not supported on this platform (tag=" << tag <<
")";
109 auto const statmBefore = readFile(statmPath);
110 long const rssBeforeKB = detail::parseStatmRSSkB(statmBefore);
113 bool const haveRu0 = getRusageThread(ru0);
117 report.
trimResult = detail::mallocTrimWithPad(kTrimPad);
122 bool const haveRu1 = getRusageThread(ru1);
124 auto const statmAfter = readFile(statmPath);
125 long const rssAfterKB = detail::parseStatmRSSkB(statmAfter);
132 if (haveRu0 && haveRu1)
134 report.
minfltDelta = ru1.ru_minflt - ru0.ru_minflt;
135 report.
majfltDelta = ru1.ru_majflt - ru0.ru_majflt;
138 std::int64_t const deltaKB = (rssBeforeKB < 0 || rssAfterKB < 0)
142 JLOG(journal.
debug()) <<
"malloc_trim tag=" << tagStr <<
" result=" << report.
trimResult
143 <<
" pad=" << kTrimPad <<
" bytes"
144 <<
" rss_before=" << rssBeforeKB <<
"kB"
145 <<
" rss_after=" << rssAfterKB <<
"kB"
146 <<
" delta=" << deltaKB <<
"kB"
153 report.
trimResult = detail::mallocTrimWithPad(kTrimPad);
A generic endpoint for log messages.
T duration_cast(T... args)
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
MallocTrimReport mallocTrim(std::string_view tag, beast::Journal journal)
Attempt to return freed memory to the operating system.
std::chrono::microseconds durationUs