Closed Bug 1734867 Opened 4 years ago Closed 4 years ago

Considerable overhead from allocating temporary profiler buffer when capturing markers with stack

Categories

(Core :: Gecko Profiler, enhancement, P2)

enhancement

Tracking

()

RESOLVED FIXED
97 Branch
Tracking Status
firefox97 --- fixed

People

(Reporter: bas.schouten, Assigned: mozbugz)

References

(Depends on 1 open bug, Blocks 4 open bugs)

Details

Attachments

(3 files)

We're seeing capturing markers with stack cause significant overhead due to memory allocation.

Example profiles:

https://share.firefox.dev/3FrP5rW

https://share.firefox.dev/3ljM19l

https://share.firefox.dev/3ljMkkv

I believe this is due to the last allocation (that I know of) in markers, see some details in bug 1578792 comment 4. I'll copy your profiles there.

We use a ProfilerBufferChunkManagerSingle in AddMarkerToBuffer, and that allocates a chunk on the heap.

Ideally, we'd like to allocate this buffer on the stack, if there is enough space left -- that last part may be difficult to implement on all platform(?)

An alternative would be to have one or more pre-allocated buffers that could be used.
The best would be one per profiled thread, but that could chew quite a bit of memory if profiling lots of threads.
Otherwise a pool of available buffers, at some small synchronization cost, with the worst case being that they're all being used and block more markers.

Severity: -- → S3
Depends on: 1578792
Priority: -- → P2

(In reply to Gerald Squelart [:gerald] (he/him) from comment #1)

The best would be one per profiled thread, but that could chew quite a bit of memory if profiling lots of threads.

For the example profiles provided in comment 0, it seems only the main thread matters, so special casing it might make sense.

On it now.

I will add a main-thread path in AddMarkerToBuffer using a single static buffer that's only allocated once, when needed by the very first main-thread marker.

And while working on this, I discovered/remembered that all ProfileBuffers allocated a chunk that was only really needed in some situations, so we can avoid this allocation in temporary ProfileBuffers like in AddMarkerToBuffer; this will benefit all threads. 🎉

Assignee: nobody → gsquelart

Some ProfileBuffers are temporary and don't actually need this allocation.

The resulting code should be the same. The factored lambda will be called in the next patch with a different chunked buffer.

Depends on D133723

The main thread is the busiest, so it benefits the most from having its own chunked buffer. This removes all but one allocation when capturing a marker stack on the main thread of each process.

Note: Further improvements are possible (e.g.: Pool of pre-allocated buffers, attempting to use a stack-based buffer, etc.), but they are more complex and will require more work in future bugs.

Depends on D133724

Pushed by gsquelart@mozilla.com: https://hg.mozilla.org/integration/autoland/rev/46bfc82686ac Allocate ProfilerBuffer's worker ChunkManager only when first needed - r=florian https://hg.mozilla.org/integration/autoland/rev/17219f7b60f5 Refactor stack-capturing path in AddMarkerToBuffer - r=florian https://hg.mozilla.org/integration/autoland/rev/63b2fd522aa8 Use single static buffer when capturing stacks for main-thread markers - r=florian

Backed out for causing xpcshell failures on ProfileBufferChunkManager.h

Flags: needinfo?(gsquelart)

Thank you Cristian. Strange that I didn't catch that one in my Try. I'll investigate...

Flags: needinfo?(gsquelart)

Oh, it's because I didn't run xpcshell tests! 😅

Pushed by gsquelart@mozilla.com: https://hg.mozilla.org/integration/autoland/rev/8b3b1ab8d743 Allocate ProfilerBuffer's worker ChunkManager only when first needed - r=florian https://hg.mozilla.org/integration/autoland/rev/7e01973ce58a Refactor stack-capturing path in AddMarkerToBuffer - r=florian https://hg.mozilla.org/integration/autoland/rev/bcddb167725c Use single static buffer when capturing stacks for main-thread markers - r=florian
Pushed by gsquelart@mozilla.com: https://hg.mozilla.org/integration/autoland/rev/ce69ff8b2ab1 Allocate ProfilerBuffer's worker ChunkManager only when first needed - r=florian https://hg.mozilla.org/integration/autoland/rev/ec0aef0c5580 Refactor stack-capturing path in AddMarkerToBuffer - r=florian https://hg.mozilla.org/integration/autoland/rev/5bd128b532ea Use single static buffer when capturing stacks for main-thread markers - r=florian
Flags: needinfo?(gsquelart)
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: