Skip to content

du: dedupe reflink bytes - #10081

Open
aaron-ang wants to merge 8 commits into
uutils:mainfrom
aaron-ang:du-reflink-count
Open

aaron-ang wants to merge 8 commits into
uutils:mainfrom
aaron-ang:du-reflink-count

Conversation

@aaron-ang

@aaron-ang aaron-ang commented Jan 5, 2026 •

Copy link
Copy Markdown
Contributor

Closes #3906.

Adds an opt-in --dedupe-reflinks flag to du (Linux only). It walks each regular file's extents with the FIEMAP ioctl and counts a shared {device, physical, length} extent only for the first file that references it. Directory entries are visited in name order so results are repeatable; command-line operands keep their given order. The flag conflicts with --apparent-size, --bytes, --inodes and --count-links. Default behavior is unchanged.

Only exact extent matches are deduplicated, so partially overlapping CoW extents are still counted more than once. Extents with no reliable physical address (inline, delayed allocation, etc.) are not deduplicated. Files that cannot be read, or that live on a filesystem without FIEMAP (tmpfs, NFS), are counted at full size, the same as without the flag. FIEMAP adds syscalls per file, which is why the flag is opt-in; du_dedupe_reflinks_wide_tree and du_dedupe_reflink_clones in du_bench.rs measure the cost.

@github-actions

github-actions Bot commented Jan 5, 2026

Copy link
Copy Markdown

GNU testsuite comparison:

Skipping an intermittent issue tests/timeout/timeout (passes in this run but fails in the 'main' branch)

@aaron-ang
aaron-ang force-pushed the du-reflink-count branch 2 times, most recently from afa86ce to 44ae977 Compare January 6, 2026 00:23
@aaron-ang
aaron-ang marked this pull request as ready for review January 6, 2026 00:37
@github-actions

github-actions Bot commented Jan 6, 2026

Copy link
Copy Markdown

GNU testsuite comparison:

GNU test failed: tests/shuf/shuf-reservoir. tests/shuf/shuf-reservoir is passing on 'main'. Maybe you have to rebase?
GNU test failed: tests/sort/sort-stale-thread-mem. tests/sort/sort-stale-thread-mem is passing on 'main'. Maybe you have to rebase?
Skipping an intermittent issue tests/timeout/timeout (passes in this run but fails in the 'main' branch)

@aaron-ang
aaron-ang force-pushed the du-reflink-count branch 2 times, most recently from 1ef975e to 5bc43b7 Compare January 7, 2026 23:54
@github-actions

github-actions Bot commented Jan 8, 2026

Copy link
Copy Markdown

GNU testsuite comparison:

Skipping an intermittent issue tests/timeout/timeout (passes in this run but fails in the 'main' branch)
Skipping an intermittent issue tests/tty/tty-eof (passes in this run but fails in the 'main' branch)

@codspeed

codspeed Bot commented Jan 8, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will degrade performance by 7.19%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 1 improved benchmark
❌ 2 regressed benchmarks
✅ 336 untouched benchmarks
🆕 2 new benchmarks
⏩ 46 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
❌ Memory cp_recursive_deep_tree[(120, 4)] 532.7 KB 699.2 KB -23.82%
❌ Simulation du_deep_tree[(100, 3)] 1 ms 1.1 ms -5.9%
⚡ Memory du_summarize_balanced_tree[(5, 4, 10)] 467.7 KB 419.4 KB +11.51%
🆕 Memory du_dedupe_reflinks_wide_tree[(5000, 500)] N/A 1 MB N/A
🆕 Simulation du_dedupe_reflinks_wide_tree[(5000, 500)] N/A 12.8 ms N/A

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing aaron-ang:du-reflink-count (ade4a5c) with main (49a56e2)

Open in CodSpeed

Footnotes

  1. 46 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@aaron-ang
aaron-ang marked this pull request as draft January 8, 2026 18:32
@aaron-ang
aaron-ang marked this pull request as ready for review January 9, 2026 00:00
@aaron-ang
aaron-ang marked this pull request as draft January 9, 2026 00:12
@aaron-ang
aaron-ang force-pushed the du-reflink-count branch 3 times, most recently from d28fb14 to e27dc67 Compare January 9, 2026 00:29
@github-actions

github-actions Bot commented Jan 9, 2026

Copy link
Copy Markdown

GNU testsuite comparison:

GNU test failed: tests/tail/retry. tests/tail/retry is passing on 'main'. Maybe you have to rebase?

@aaron-ang
aaron-ang marked this pull request as ready for review January 9, 2026 00:47
Comment thread tests/by-util/test_du.rs

#[cfg(not(windows))]
use regex::Regex;
#[cfg(target_os = "linux")]

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Somehow this is the first time I've seen this notation, this is really nice and would be nice to cleanup our other cfg code

@ChrisDryden

Copy link
Copy Markdown
Collaborator

Just to clarify that my understanding matches yours. This would be extending past GNU's capabilities instead of matching GNU's capabilities right? Given that its a big change I imagine it would be something that would be reviewed after the issues with GNU compatibility are sorted out

Comment thread src/uu/du/src/du.rs Outdated
@@ -1094,6 +1206,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {

// Check existence of path provided in argument
let mut seen_inodes: HashSet<FileInfo> = HashSet::new();
let mut seen_shared_extents: HashSet<SharedExtentKey> = HashSet::new();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a pre-existing bug, theres another open PR about moving that seen_inodes out of this loop so that when you pass multiple file names it can deduplicate. Ideally this would be moved outside of the loop too

@aaron-ang aaron-ang Jan 18, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated it.

@aaron-ang aaron-ang changed the title du: count reflink disk usage only once du: resolve hard links Jan 18, 2026
@aaron-ang aaron-ang changed the title du: resolve hard links du: dedupe reflink bytes Jan 18, 2026
@aaron-ang

Copy link
Copy Markdown
Contributor Author

Just to clarify that my understanding matches yours. This would be extending past GNU's capabilities instead of matching GNU's capabilities right? Given that its a big change I imagine it would be something that would be reviewed after the issues with GNU compatibility are sorted out

yea that's right.

@aaron-ang
aaron-ang force-pushed the du-reflink-count branch 6 times, most recently from 1513fa4 to 9442287 Compare January 20, 2026 03:56
@github-actions

github-actions Bot commented May 10, 2026 •

Copy link
Copy Markdown

GNU testsuite comparison:

Skipping an intermittent issue tests/tail/retry (passes in this run but fails in the 'main' branch)
Skipping an intermittent issue tests/timeout/timeout-group (passes in this run but fails in the 'main' branch)
Note: The gnu test tests/seq/seq-epipe is now being skipped but was previously passing.

@aaron-ang

Copy link
Copy Markdown
Contributor Author

Parallel FIEMAP — bench results

Implemented mpsc and rayon variants of the dedup FIEMAP loop to evaluate cben's suggestion (10081#discussion_r... — issue comment). Compared against the current serial path on tmpfs/ext4 (aarch64, Linux 6.17, warm cache).

Micro-bench: isolated FIEMAP loop (5000 files / 500 dirs, 100 samples, divan)

impl median vs serial
serial 54.4 ms —
mpsc 52.2 ms -4%
rayon 48.9 ms -10%

Rayon wins when only the FIEMAP loop is measured.

End-to-end: du --dedupe-reflinks <wide_tree> (same fixture, 100 samples)

integration median vs serial
serial 46.3 ms —
rayon 142.3 ms 3× slower
mpsc 296.0 ms 6× slower

The parallel integrations need a pre-pass to collect (path, dev_id) for regular files (so workers can run concurrently), which doubles the per-file lstat cost. On tmpfs/ext4 where FIEMAP is cheap, that overhead dominates and serial wins.

On btrfs (cben's 5× scenario) the math may flip — FIEMAP is the bottleneck there, not stat. I don't have a btrfs system to verify. Keeping serial in this PR; leaving the parallel paths as a follow-up once someone can bench on btrfs.

What was tested

  • dedupe::extents_for (single-file FIEMAP)
  • dedupe::parallel_extents (rayon par_iter)
  • dedupe::parallel_extents_mpsc (Mutex-shared input queue, N=available_parallelism workers, single accumulator drain)
  • Integration via per-directory pre-pass + batch FIEMAP

The integration refactor + extra module were reverted; only serial inline FIEMAP ships in this PR (current HEAD).

@aaron-ang

aaron-ang commented May 10, 2026 •

Copy link
Copy Markdown
Contributor Author

hi @cben, quick map from your points to changes:

  • PR description / apparent_size logic confusion: you were right; the original description was inverted relative to the code. Now reworked: dedup is opt-in via --dedupe-reflinks and is a no-op with --apparent-size, --bytes, --inodes, --count-links. Default behavior matches GNU du exactly.
  • 5× perf overhead on btrfs: making it opt-in addresses the unconditional cost. For the parallel-FIEMAP question, I prototyped both mpsc and rayon and benched against serial — see the bench results comment ↑. tl;dr: rayon wins on isolated FIEMAP but both parallel integrations lose end-to-end on tmpfs/ext4 because the pre-pass to collect work doubles lstat cost. Your btrfs case may flip that — leaving parallelization for a follow-up PR once it can be measured on btrfs.
  • fe_physical == 0 (INLINE / unwritten): already guarded (extent.fe_physical != 0); now documented in adjust_blocks_for_reflinks.
  • Partial-overlap extents (your 1M–4M / 2M–3M / 3M–5M example): not deduped — only exact {dev, physical, length} matches. Documented as a known limitation in the same comment block.
  • Order-dependent attribution + multi-column btrfs fi du-style output: agreed these are real UX issues but they're a bigger redesign (and incompatible with GNU du's single-number output). I've left these as separate follow-up work rather than expanding this PR.
  • Add bench: done — du_dedupe_reflinks_wide_tree in du_bench.rs. Was useful for the parallel comparison.

CI is green on the current HEAD. Happy to iterate further if you spot anything else.

@oech3

oech3 commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

@aaron-ang Are you interested in adding fiemap wrapper to uucore also for other utils e.g. cp?

@oech3

oech3 commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

@sylvestre Do you still want to have this extension?

@sylvestre

Copy link
Copy Markdown
Contributor

the PR is hard to understand with the very long comments :(

what is the tldr and status ?

@oech3

oech3 commented Jul 7, 2026

Copy link
Copy Markdown
Contributor

This PR adds an expention to avoid counting size consumed by reflink 2+ times in my understanding.

@sylvestre

Copy link
Copy Markdown
Contributor

maybe ?
but it should be documented in the extension documentation

Comment thread src/uu/du/src/du.rs
pub const EXCLUDE_FROM: &str = "exclude-from";
pub const FILES0_FROM: &str = "files0-from";
pub const VERBOSE: &str = "verbose";
pub const DEDUPE_REFLINKS: &str = "dedupe-reflinks";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub const DEDUPE_REFLINKS: &str = "dedupe-reflinks";
pub const REFLINK: &str = "reflink";

I think du --reflink is enough.

@aaron-ang aaron-ang Jul 24, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i disagree. unlike cp where --reflink makes sense as an option (intuitively means copy reflinks also), for du it is not obvious how reflink applies, since it is also not in GNU convention.

@oech3

oech3 commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

I think this is btrfs-progs's role, not coreutils's role.

@oech3

oech3 commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

I am wondering if trying ioctl_fideduperange is simpler than using FIEMAP and show result.
Extension does not need to be read-only tool if it is properly documented.

@aaron-ang

Copy link
Copy Markdown
Contributor Author

I am wondering if trying ioctl_fideduperange is simpler than using FIEMAP and show result. Extension does not need to be read-only tool if it is properly documented.

du should be remain read-only. also, we are not actually doing any dedupe; the name could be misleading. we simply dont double-count shared extents.

@sylvestre

Copy link
Copy Markdown
Contributor

sorry but it needs to be rebased

Copilot AI balanced review requested due to automatic review settings November 29, 2026 23:51

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

Files with no allocated blocks have no extents to claim, so do not open
them. Record each inode before querying its extents, so a file that
fails is reported once instead of again for each later operand or hard
link.
With --dedupe-reflinks, a file that cannot be opened for reading, or
whose filesystem does not support FIEMAP, kept being reported as an
error and dropped from the totals. Plain du only needs stat, so count
such files at their full size instead, matching the output without the
option.

Also move the new reflink error messages into the locale files.
Copilot AI balanced review requested due to automatic review settings November 30, 2026 00:15

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

Copilot AI balanced review requested due to automatic review settings November 30, 2026 00:16

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

count reflink disk usage only once in du

6 participants