Skip to content

Add jemalloc profiling via HTTP API #7746

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: unstable
Choose a base branch
from

Conversation

jimmygchen
Copy link
Member

@jimmygchen jimmygchen commented Jul 16, 2025

Issue Addressed

Add jemalloc profiling via HTTP API.

This is included at build time if jemalloc is enabled, however not activated by default. User will need to enable profiling via the curl command below.

Commands

# Enables profiling
curl -X POST http://localhost:4000/lighthouse/malloc/prof_active -H "Content-Type: application/json" -d "true"

# Produce a dump
curl -X POST http://localhost:4000/lighthouse/malloc/prof_dump \
     -H "Content-Type: application/json" \
     -d '"/home/ubuntu/prof.dump"'

Additional notes

  • DOCUMENTED: debug symbol needs to be enabled when building the binary
  • needs to confirm it works on different targets
    • so far this works on ubuntu and macOS
  • needs to confirm profiling is inactive on startup - confirmed the memory growth was due to a sync bug (Fix leak in range sync components_by_range_requests #7767)
  • might need a way to purge samples in memory - I don't think this is strictly necessary at this point. We can add it if we need it later.

let cstr = CStr::from_bytes_with_nul(&terminated_filename)
.map_err(|e| format!("Cannot convert filename to CStr: {e:?}"))?;

unsafe { raw::write("prof.dump".as_ref(), cstr) }
Copy link
Member

Choose a reason for hiding this comment

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

LGTM, looking at the source this corresponds to mallctl("prof.dump", NULL, NULL, filename, size) which is exactly what we want

Ref:

@michaelsproul michaelsproul changed the title Add jemalloc-profilg feature Add jemalloc-profiling feature Jul 16, 2025
@michaelsproul michaelsproul added waiting-on-author The reviewer has suggested changes and awaits thier implementation. and removed waiting-on-author The reviewer has suggested changes and awaits thier implementation. labels Jul 16, 2025
Copy link
Member

@michaelsproul michaelsproul left a comment

Choose a reason for hiding this comment

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

LGTM! Let's merge before this bitrots (again)!!

@michaelsproul michaelsproul added the ready-for-merge This PR is ready to merge. label Jul 16, 2025
@michaelsproul
Copy link
Member

Actually, don't we need some way to trigger this? Paul had an HTTP API I think

@michaelsproul michaelsproul removed the ready-for-merge This PR is ready to merge. label Jul 16, 2025
Copy link

mergify bot commented Jul 16, 2025

This pull request has been removed from the queue for the following reason: pull request dequeued.

Pull request #7746 has been dequeued. The pull request rule doesn't match anymore. The following conditions don't match anymore:

  • label=ready-for-merge.

You should look at the reason for the failure and decide if the pull request needs to be fixed or if you want to requeue it.
If you do update this pull request, it will automatically be requeued once the queue conditions match again.
If you think this was a flaky issue instead, you can requeue the pull request, without updating it, by posting a @mergifyio requeue comment.

@jimmygchen jimmygchen force-pushed the fusaka-devnet-2-prof branch 2 times, most recently from 89a8ff3 to e1a7178 Compare July 16, 2025 05:56
@jimmygchen
Copy link
Member Author

Sorry, I didn't include everything - I've pushed all changes now and I'm going to test it first as there were quite a bit of conflicts and changes that I had to make.

@jimmygchen jimmygchen force-pushed the fusaka-devnet-2-prof branch from a73c418 to adfc523 Compare July 17, 2025 03:27
@jimmygchen jimmygchen changed the title Add jemalloc-profiling feature Add jemalloc profiling via HTTP API Jul 17, 2025
@jimmygchen jimmygchen added ready-for-review The code is ready for review HTTP-API and removed ready-for-review The code is ready for review labels Jul 17, 2025
@jimmygchen jimmygchen marked this pull request as draft July 17, 2025 04:42
@jimmygchen
Copy link
Member Author

Got a bit stuck with getting debug symbols in the dump, will come back to this later.

@jimmygchen jimmygchen added the work-in-progress PR is a work-in-progress label Jul 17, 2025
@jimmygchen jimmygchen added ready-for-review The code is ready for review and removed work-in-progress PR is a work-in-progress labels Jul 18, 2025
@jimmygchen
Copy link
Member Author

Managed to get this to work, I've added a page to the lighthouse book with the instructions.

@jimmygchen jimmygchen requested a review from michaelsproul July 18, 2025 05:10
@jimmygchen jimmygchen marked this pull request as ready for review July 18, 2025 05:10
mergify bot pushed a commit that referenced this pull request Jul 21, 2025
Although we're working on jemalloc profiler support in #7746, heaptrack seems to be producing more sensible results.

This PR adds a heaptrack profile and a heaptrack feature so that we no longer need to patch the code in order to use heaptrack. This may prove complementary to jemalloc profiling, so I think there is no harm in having both.
@jimmygchen jimmygchen requested a review from chong-he July 22, 2025 04:58
@jimmygchen
Copy link
Member Author

jimmygchen commented Jul 22, 2025

@michaelsproul I feel like we probably should try to merge this even if the output wasn't very useful this time - it's likely at some point in the future we're going to come back and look for this branch and try to revive it (happened twice already!)
I was a bit unsure because of the memory growth, but now we've identified the OOM cause, I feel more comfortable with this PR now.

We can probably try both jemalloc profiling and heaptrack again after the OOM fix.

Copy link
Member

@chong-he chong-he left a comment

Choose a reason for hiding this comment

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

Looks good now for the documentation side of the change

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
HTTP-API ready-for-review The code is ready for review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants