From abb6d796df2f41cac590d99dc9e999954ea550a1 Mon Sep 17 00:00:00 2001 From: Martin Vassilev Date: Sun, 8 Nov 2026 09:40:52 +0200 Subject: [PATCH] Keep ROOT's cluster buffers in ramdump's heap On 0.2.0 a full read of NA12878's HiSeq chromosome 20 took 135,000 to 902,000 minor page faults and up to 3 s more of system time than at 530933b, with the same user time. They were in pread from ROOT's cluster pool into the buffer of each cluster, about 15 MB: glibc gave the buffer back to the kernel when it was freed, more or less often with the heap's layout, which #128 changed, and the next cluster faulted its pages in. ramdump now sets glibc's mmap threshold to 32 MB and its trim threshold to 64 MB, in the tool, since both hold for the whole process. On z840, against 0.2.0: HiSeq's full read 23.7 to 24.8 s instead of 24.3 to 27.5 s, 49,000 to 98,000 faults; chromosome 20 on eight threads 1.0 s of system time instead of 3.2 s, a 10 Mb region 0.15 s instead of 0.56 s. Peak memory grows on eight threads, 640 MB instead of 580 MB, and for the region, 67 MB instead of 60 MB. --- CHANGELOG.md | 12 ++++++++++++ tools/ramdump.cxx | 28 ++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e75be1..9984a38 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,17 @@ the README for what that means before 1.0. ## [Unreleased] +### Fixed + +- `ramdump` keeps the buffers ROOT reads clusters into, of about 15 MB, in the + heap. glibc gave them back to the kernel, and the next cluster faulted its + pages in again, more or less often with the order in which threads freed + memory: a full read of NA12878's HiSeq chromosome 20 took 135,000 to 902,000 + page faults and 24.3 to 27.5 s, and now 49,000 to 98,000 and 23.7 to 24.8 s. + On chromosome 20, eight threads spend 1.0 s in the kernel instead of 3.2 s and + a 10 Mb region 0.15 s instead of 0.56 s, for more peak memory: 640 MB instead + of 580 MB, and 67 MB instead of 60 MB ([#131]). + ## [0.2.0] - 2026-11-07 The format of this release stores the typed SAM tags in columns of their own, the @@ -372,3 +383,4 @@ it stay readable by every later release. [#126]: https://github.com/compiler-research/ramtools/pull/126 [#127]: https://github.com/compiler-research/ramtools/pull/127 [#128]: https://github.com/compiler-research/ramtools/pull/128 +[#131]: https://github.com/compiler-research/ramtools/pull/131 diff --git a/tools/ramdump.cxx b/tools/ramdump.cxx index 9c4c2bc..c400415 100644 --- a/tools/ramdump.cxx +++ b/tools/ramdump.cxx @@ -36,6 +36,10 @@ #include #include +#if defined(__GLIBC__) +#include +#endif + namespace { // Fields go into one buffer handed to fwrite in 1 MB blocks; formatting @@ -440,10 +444,34 @@ bool ParseThreads(const std::string &text, int &out) return true; } +// ROOT reads each cluster into a buffer of its own, about 15 MB in the file of +// NA12878's HiSeq chromosome 20. With glibc's defaults a freed buffer often +// goes back to the kernel and the next one faults its pages in again, more or +// less often with the order in which the threads free memory: a full read of +// that file took 135,000 to 902,000 page faults, 6.2 to 9.1 s of system time +// and 24.3 to 27.5 s. With buffers up to 32 MB (glibc's largest mmap +// threshold) kept in the heap, and up to 64 MB of it kept free (32 MB is too +// little): 49,000 to 98,000 faults, 5.9 to 6.2 s and 23.7 to 24.8 s. On +// chromosome 20, eight threads spend 1.0 s in the kernel instead of 3.2 s and +// a 10 Mb region 0.15 s instead of 0.56 s, for more peak memory: 640 MB +// instead of 580 MB, and 67 MB instead of 60 MB. Setting either stops glibc +// from raising the mmap threshold itself, so both are set. Here, not in the +// library, since they hold for the whole process. +void KeepClusterBuffers() +{ +#if defined(__GLIBC__) + constexpr int kMmapThreshold = 32 << 20; + constexpr int kTrimThreshold = 64 << 20; + mallopt(M_MMAP_THRESHOLD, kMmapThreshold); + mallopt(M_TRIM_THRESHOLD, kTrimThreshold); +#endif +} + } // namespace int main(int argc, char *argv[]) { + KeepClusterBuffers(); // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic) const std::vector args(argv + 1, argv + argc);