Skip to content

Keep ROOT's cluster buffers in ramdump's heap - #131

Merged
mvassilev merged 1 commit into
developfrom
keep-cluster-buffers
Nov 8, 2026
Merged

mvassilev merged 1 commit into
developfrom
keep-cluster-buffers

Conversation

@mvassilev

Copy link
Copy Markdown
Collaborator

On 0.2.0 a full read of NA12878's HiSeq chromosome 20 got slower than at 530933b, while
chromosome 20's got faster: 26.1 s and 41.0 s of CPU against 24.9 s and 38.7 s. Run in turn on
z840's socket 0, 530933b, 14b2aae (#128) and 0.2.0 spend the same user time, 32.6 to 33.5 s.
The difference is in the kernel: 73,000 to 100,000 minor page faults at 530933b, 124,000 to
1,075,000 at 14b2aae and 0.2.0, with up to 3 s more of system time. It is the same with the
whole genome's 72 GB in the page cache and without.

perf record -e page-faults puts 98% of them in pread from RClusterPool::ExecReadClusters,
into the buffer ROOT allocates for each cluster, about 15 MB in this file. glibc often gives
such a buffer back to the kernel when it is freed, and the next cluster faults its pages in
again. How often depends on the heap's layout, which #128 changed: the same code faults 124,000
times in one run and 1,075,000 in the next. ROOT's buffers are not ours to change, but the
process's malloc is.

The change

ramdump starts by setting glibc's mmap threshold to 32 MB, its largest, so that the buffers
come from the heap, and its trim threshold to 64 MB, so that the heap keeps them when they are
freed. Setting either stops glibc from raising the mmap threshold itself, so both are set. They
hold for the whole process, so they are in the tool and not in the library, which Python and
RDataFrame load too. Other C libraries, and macOS, are left as they are.

Tried with GLIBC_TUNABLES on 0.2.0's build first: a trim threshold of 32 MB still faulted
415,000 to 1,248,000 times; 64 MB, 128 MB and 1 GB gave the same times and memory. An mmap
threshold of 16 MB gave the same as 32 MB, memory included.

What it does

z840 socket 0, this build against 0.2.0's, in turn, on 0.2.0's files; the output of HiSeq's
full read has the same md5:

0.2.0 this change
HiSeq chr20, full read 24.31 to 27.54 s 23.72 to 24.75 s
system time 6.21 to 9.13 s 5.86 to 6.19 s
minor page faults 135,236 to 902,400 48,661 to 98,113
peak memory 155 to 185 MB 144 to 173 MB
chr20, full read 15.42 to 15.88 s 15.26 to 15.49 s
system time 1.45 to 1.49 s 1.30 to 1.37 s
peak memory 100 to 104 MB 102 to 105 MB
chr20, full read with -@ 8 5.24 to 5.42 s 4.97 to 5.37 s
system time 3.00 to 3.26 s 0.99 to 1.04 s
peak memory 560 to 584 MB 630 to 645 MB
chr20, a 10 Mb region 2.99 to 3.09 s 2.91 to 3.00 s
system time 0.52 to 0.69 s 0.13 to 0.16 s
peak memory 59 to 61 MB 65 to 70 MB
chr20, a 1 kb region, a process of its own 0.06 s 0.06 s
peak memory 36.3 to 37.0 MB 37.2 to 38.2 MB

Three runs each, five for the 10 Mb region and nine for the 1 kb one. The cost is memory the
process keeps instead of giving back: about 60 MB more on eight threads, 7 MB for the 10 Mb
region, 1 MB for a small one. HiSeq's full read peaks lower, since it no longer holds a buffer
being given back and a new one at once.

14 of 14 tests pass on z840 (ROOT 6.40.04, htslib 1.25).

Found on the way, not changed

A full read reads each cluster about three times: pread reads 6.50 GB of HiSeq's 2.20 GB file
at 0.2.0, 6.82 GB at 530933b. ROOT's cluster pool keeps the cluster asked for and the next, and
the block readers read ahead at the last row of a block, often in the next cluster, so the
cluster the records are still in is dropped and read again. Reading the block columns through
a page source of their own would avoid it; that is a change of its own.

Changelog

  • Fixed: ramdump keeps the buffers ROOT reads clusters into in the heap; HiSeq's full read
    takes 49,000 to 98,000 page faults instead of up to 902,000.

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.
@github-actions

github-actions Bot commented Nov 8, 2026

Copy link
Copy Markdown

clang-tidy review says "All clean, LGTM! 👍"

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.40%. Comparing base (7d71704) to head (abb6d79).

Additional details and impacted files

Impacted file tree graph

@@             Coverage Diff             @@
##           develop     #131      +/-   ##
===========================================
+ Coverage    89.38%   89.40%   +0.01%     
===========================================
  Files           26       26              
  Lines         3722     3728       +6     
  Branches      1267     1274       +7     
===========================================
+ Hits          3327     3333       +6     
  Misses         203      203              
  Partials       192      192              
Flag Coverage Δ
unittests 89.40% <100.00%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
tools/ramdump.cxx 86.84% <100.00%> (+0.23%) ⬆️
Files with missing lines Coverage Δ
tools/ramdump.cxx 86.84% <100.00%> (+0.23%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@mvassilev
mvassilev merged commit 9a369f2 into develop Nov 8, 2026
4 checks passed
@mvassilev
mvassilev deleted the keep-cluster-buffers branch November 8, 2026 08:52
mvassilev added a commit that referenced this pull request Nov 8, 2026
A full read read each cluster several times: 2.04 GB for chromosome 20's
418 MB file, 6.6 GB for HiSeq chromosome 20's 2.2 GB. ROOT's cluster
cache keeps the cluster it is asked for and the next one; the block
readers read ahead at the last row of each block, near the end of a
cluster in the next one, and so dropped the cluster the records were
still in, which they then read again.

Every block lies in one cluster, so the readers of names, qualities and
bases now read ahead only blocks of their block's cluster, when the file
is read with the cluster cache. A block of fqzcomp, 25 times slower to
decode, still reads ahead from the next cluster. RAMFile says whether
its reader uses the cache. A test checks with ROOT's metrics that a full
read reads no more than the file holds.

On z840, against #131: HiSeq chromosome 20 reads 2.17 GB, in 19.3 to
19.9 s instead of 23.8 to 24.4 s, with 1.3 s of system time instead of
6.0 s; chromosome 20 reads 0.42 GB, in 14.5 to 15.2 s instead of 15.1 to
16.0 s. Regions, -@ N and fqzcomp files take the same time.
mvassilev added a commit that referenced this pull request Nov 9, 2026
* Read ahead blocks only within the records' cluster

A full read read each cluster several times: 2.04 GB for chromosome 20's
418 MB file, 6.6 GB for HiSeq chromosome 20's 2.2 GB. ROOT's cluster
cache keeps the cluster it is asked for and the next one; the block
readers read ahead at the last row of each block, near the end of a
cluster in the next one, and so dropped the cluster the records were
still in, which they then read again.

Every block lies in one cluster, so the readers of names, qualities and
bases now read ahead only blocks of their block's cluster, when the file
is read with the cluster cache. A block of fqzcomp, 25 times slower to
decode, still reads ahead from the next cluster. RAMFile says whether
its reader uses the cache. A test checks with ROOT's metrics that a full
read reads no more than the file holds.

On z840, against #131: HiSeq chromosome 20 reads 2.17 GB, in 19.3 to
19.9 s instead of 23.8 to 24.4 s, with 1.3 s of system time instead of
6.0 s; chromosome 20 reads 0.42 GB, in 14.5 to 15.2 s instead of 15.1 to
16.0 s. Regions, -@ N and fqzcomp files take the same time.

* Format as clang-format 18 does; no array subscript in the test

* Parentheses and a reserve in the cluster test, as clang-tidy 19 asks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants