Open Bug 2070900 Opened 2 months ago Updated 1 hour ago

The deferred purge never releases the dirty pages of an arena that keeps reusing a few of them

Categories

(Core :: Memory Allocator, enhancement)

enhancement

Tracking

()

ASSIGNED

People

(Reporter: jstutte, Assigned: jstutte)

References

(Blocks 2 open bugs)

Details

Attachments

(5 external files, 1 obsolete file)

The deferred purge even after Bug 2066416 skips an arena for as long as it reused a dirty page within the grace period. Any allocation that comes back more often than that, a repaint, a timer, a GC cycle, keeps the arena ineligible indefinitely, and everything it freed since stays resident.

Measured locally on the Bug 2066416 stack with the default 500 ms grace: a chrome script allocating 4 MB and forcing a GC every 100 ms holds about 26 MB of dirty pages in the parent process for as long as it runs, against about 1 MB once the tick stops. A longer foreground grace period (Bug 2067590) turns this from a corner case into the normal state of the parent process: a 2 Hz repaint or the profiler's own allocations suffice, and AWSY shows the parent retaining 20 MB more at every checkpoint.

Proposed fix: track the lowest dirty page count an arena reaches between two purge decisions. Once a grace period has passed, nothing that ran in the meantime needed those pages, so purge the arena down to that mark and leave the pages it did reuse in place. Such a target purge must stop at the first reuse after it started; a version that ignored reuse released the reload working set mid-load and cost 2.5x on twitter warm fcp on macOS aarch64.

WebRenderBridgeParent currently raises the max dirty page modifier while transactions flow and, 100 ms after the last one, resets it and drains every arena above its threshold. That masks the repaint case in the parent process today and is one of the last callers of the modifier. With the target purge in place the drain is redundant, so it goes in the same bug.

Type: defect → enhancement

WebRenderBridgeParent raised the max dirty page modifier while transactions
were flowing and, 100 ms after the last one, reset it and purged every arena
above its threshold. Without a GPU process that runs in the parent process
and drains it after every compositing burst, regardless of the grace period
the deferred purge gives pages to be reused. Only the render thread's idle
notification remains, which purges WebRender's own chunk pool.

The deferred purge skips an arena for as long as it keeps reusing dirty
pages within the grace period. Any allocation that comes back more often
than that, a repaint, a timer, a GC cycle, keeps the arena ineligible
indefinitely, and everything it freed since stays resident. A longer
foreground grace period makes this the normal state of the parent process
rather than a corner case.

Track the lowest dirty page count an arena reaches between two purge
decisions. Once a grace period has passed, nothing that ran in the
meantime needed those pages, so purge the arena down to that mark and
leave the pages it did reuse in place.

Assignee: nobody → jstutte
Attachment #9641866 - Attachment description: WIP: Bug 2070900 - Purge the dirty pages an arena did not touch during a grace period even while it stays busy. r?pbone → Bug 2070900 - Purge the dirty pages a busy arena left untouched for a few GC cycles. r?pbone
Status: NEW → ASSIGNED
Attachment #9641865 - Attachment description: WIP: Bug 2070900 - Let WebRender leave releasing dirty pages to the deferred purge. r?#gfx-reviewers → Bug 2070900 - Let WebRender leave releasing dirty pages to the deferred purge. r?#gfx-reviewers
See Also: → 2086187

How the numbers in the D325797 test plan were measured, and what we tried later instead of the low water mark.

All local runs were on Linux (Core Ultra 9 285K, 122 GB), with opt builds of the patch and its parent, speedometer3 driven by Marionette with 3 cycles per run, and 4 to 8 runs per arm, interleaved.

  • Minor faults ("+10% for a 3 s delay" and so on) are whole-process counts: minflt from /proc/<pid>/stat, summed over the parent and all of its child processes at the end of the run.
  • Refaults of released pages ("27-46% of the pages it releases fault back in") come from allocator bookkeeping, not from the kernel. A try-only patch tags each page in the page map when a purge releases it, with an extra tag for target purges, and counts tagged pages that the allocator hands out again. On Linux mozjemalloc uses MADV_DONTNEED (MADV_FREE is redefined to it), so every such page means a zero-fill fault on first touch. These numbers are page counts per process: an upper bound on faults on Linux and Windows, not a fault count on macOS.
  • Parent process dirty pages in the comment 0 scenario come from about:memory (heap/committed/unused-pages/dirty) after 30 s idle, from shippable builds of the try pushes.

Bug 2086187 turns that patch into a purge log that stays in the tree, compiled out by default.

Releasing spare chunks instead. We tried two variants without the low water mark: release all spare chunks but one once the maximum purge delay has passed, or whenever the reuse grace holds a purge back. Each arm had 4 sp3 runs and 2 runs of the comment 0 scenario:

patch spares at the maximum purge delay spares whenever the grace holds a purge back
parent dirty while ticking (MB) 4.2 / 4.4 11.8 / 12.2 11.4 / 12.2
parent dirty, quiet (MB) 1.0-1.4 1.0-1.4 1.0-1.4
sp3 score 28.22 +- 0.04 28.20 +- 0.04 28.15 +- 0.05
minor faults, whole tree 2015k 2037k 2317k
new chunks mapped, sp3 tab 2137 2361 3297

Spare chunks hold most of what the target purges release in the parent's default arena. But the parent's JS arena has no spare chunks, and its dirty pages stay, which leaves the parent at about three times the dirty pages of the patch. Releasing spares whenever the grace holds a purge back costs 15% more faults under load. So the proposed patch keeps the low water mark.

Attachment #9641866 - Attachment description: Bug 2070900 - Purge the dirty pages a busy arena left untouched for a few GC cycles. r?pbone → Bug 2070900 - Purge as many dirty pages as a busy arena left untouched for a few GC cycles. r?pbone

(In reply to Jens Stutte [:jstutte] from comment #3)

Releasing spare chunks instead. We tried two variants without the low water mark: release all spare chunks but one once the maximum purge delay has passed, or whenever the reuse grace holds a purge back. Each arm had 4 sp3 runs and 2 runs of the comment 0 scenario:

Which rows refer to which test, sp3 is obviously sp3, are the others also sp3, or the comment 0 scenario, or some combination?

patch spares at the maximum purge delay spares whenever the grace holds a purge back
parent dirty while ticking (MB) 4.2 / 4.4 11.8 / 12.2 11.4 / 12.2
parent dirty, quiet (MB) 1.0-1.4 1.0-1.4 1.0-1.4
sp3 score 28.22 +- 0.04 28.20 +- 0.04 28.15 +- 0.05
minor faults, whole tree 2015k 2037k 2317k
new chunks mapped, sp3 tab 2137 2361 3297

When does the 2nd column, "spares at the maximum purge delay", run its normal purge, wouldn't that also be at the maximum delay? It'd be good to add a column of the current mozilla-central too.

Was this not tested on the content process, wouldn't that be important for sp3?

The first row suggests that the majority of the dirty pages are kept in in-use chunks, not spare chunks.

Spare chunks hold most of what the target purges release in the parent's default arena. But the parent's JS arena has no spare chunks, and its dirty pages stay, which leaves the parent at about three times the dirty pages of the patch. Releasing spares whenever the grace holds a purge back costs 15% more faults under load. So the proposed patch keeps the low water mark.

How do you know which arenas have dirty pages in which areas. ( This is Bug 2076791 )?

Those extra page faults are telling me that the 3rd column is right-out. Thanks for testing.

Purge() now returns the arena's dirty page count along with its result, so
that its caller can act on it without taking the arena lock again.

MaybeEndPurge() replaces ShouldContinuePurge(): once the arena reaches the
threshold it ends the purge, which clears mIsPurgePending. No functional
change.

Attachment #9641866 - Attachment description: Bug 2070900 - Purge as many dirty pages as a busy arena left untouched for a few GC cycles. r?pbone → Bug 2070900 - Purge as many dirty pages as a busy arena left unused for a few GC cycles. r?pbone

Purge() was only called by PurgeLoop(). Doing its work in the loop lets a
purge keep state across steps and replaces the manual Lock()/Unlock() calls
with a scoped lock. No functional change.

MaybeEndPurge() ends the purge with the new EndPurge() once the arena
reached the threshold, which clears mIsPurgePending. No functional change.

Attachment #9677390 - Attachment description: Bug 2070900 - Let a mozjemalloc purge step return the dirty page count and end the purge in one place. r?pbone → Bug 2070900 - Let a mozjemalloc purge stop at a dirty page count. r?pbone
Attachment #9682134 - Attachment is obsolete: true
See Also: → 2070617
Blocks: 2093240
Attachment #9677390 - Attachment description: Bug 2070900 - Let a mozjemalloc purge stop at a dirty page count. r?pbone → Bug 2070900 - Compute the dirty page count a mozjemalloc purge stops at in advance. r?pbone

Apologies for the delay.

(In reply to Paul Bone [:pbone] from comment #4)

Which rows refer to which test

Here are the tables again with clearer names. Each column is a separate set of runs of the same build, with an environment variable choosing the purge behaviour, and the arms ran interleaved. Each row group comes from its own runs, as do the runs with and without the tick. The columns are:

  • patch: once the maximum purge delay has passed, a target purge releases as many dirty pages as stayed unused, from the lowest dirty page count since the last purge.
  • spares at max delay: once the maximum purge delay has passed, release all spare chunks but one instead of the target purge.
  • spares on grace: release all spare chunks but one whenever the reuse grace holds a purge back, instead of the target purge.

Comment 0 scenario (4 MB allocated and a GC every 100 ms), dirty pages in the parent process, 2 runs per arm:

MB patch spares at max delay spares on grace
with the GC tick, run 1 / run 2 4.2 / 4.4 11.8 / 12.2 11.4 / 12.2
without the tick, range 1.0-1.4 1.0-1.4 1.0-1.4

sp3, 4 runs per arm:

patch spares at max delay spares on grace
score 28.22 +- 0.04 28.20 +- 0.04 28.15 +- 0.05
minor faults, all processes 2015k 2037k 2317k
new chunks, sp3 tab 2137 2361 3297

When does the 2nd column, "spares at the maximum purge delay", run its normal purge, wouldn't that also be at the maximum delay?

No. The threshold purge works as on main in all three columns: it runs once the arena has not reused a page for the reuse grace (500 ms). "Spares at max delay" only replaces the target purge, which the patch runs once the maximum purge delay has passed, with releasing all spare chunks but one. In the comment 0 scenario the busy parent arenas rarely get past the grace, so that release is nearly all this variant does there.

It'd be good to add a column of the current mozilla-central too.

This set has no main arm. For the comment 0 scenario, the parent held about 26 MB dirty while ticking without the patch, measured on the bug 2066416 stack that has landed since. I can rerun sp3 with a main arm if you want the other rows, too.

Was this not tested on the content process, wouldn't that be important for sp3?

It was: the faults include the content processes and the new chunks row is the sp3 tab. The purge log shows why releasing spares costs there. At its target purges the tab had about 11 spare chunks on average, and all but the first held 370k dirty pages summed over the runs, while the target purges released 75k. The patch keeps the rest because the tab reuses them. Releasing the spares instead unmaps chunks the tab then maps again: 10% more new chunks with "spares at max delay" and 54% more with "spares on grace".

The first row suggests that the majority of the dirty pages are kept in in-use chunks, not spare chunks.

Of what the spare variants leave behind, yes. Per comment 0 run the parent's target purges released about 4950 pages from the default arena, 85% of them by unmapping spares, and about 1850 pages from the JS arena, which had no spare chunks. Those in-use JS arena pages are most of what the spare variants keep.

How do you know which arenas have dirty pages in which areas.

From the purge log of bug 2086187, which I extended locally for this: each purge logs its arena, the number of its spare chunks, the dirty pages in them and the pages released by unmapping spares. That only covers arenas at the moment they purge, so it is no replacement for bug 2076791.

You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: