Skip to content

[memoryprofiler] Show allocations from threads#26175

Merged
sbc100 merged 6 commits intoemscripten-core:mainfrom
matzf:feature/memoryprofiler-pthread
Feb 27, 2026
Merged

[memoryprofiler] Show allocations from threads#26175
sbc100 merged 6 commits intoemscripten-core:mainfrom
matzf:feature/memoryprofiler-pthread

Conversation

@matzf
Copy link
Contributor

@matzf matzf commented Jan 27, 2026

Support multi-threaded applications in memoryprofiler.

Fixes #18107.

@matzf matzf force-pushed the feature/memoryprofiler-pthread branch 2 times, most recently from 95777be to ba2463e Compare January 27, 2026 15:52
@sbc100 sbc100 requested a review from juj January 27, 2026 17:12
@matzf
Copy link
Contributor Author

matzf commented Feb 23, 2026

@juj, friendly ping, you were assigned as reviewer for this PR. Please let me know if you need anything from me to move this forward.

Copy link
Collaborator

@sbc100 sbc100 left a comment

Choose a reason for hiding this comment

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

Sorry I forgot to send these comments from earlier.

emscripten_trace_record_free: (address) => {
Module['onFree']?.(address);
#if MEMORYPROFILER
Module['onFree']?.(address) || postMessage({cmd: 'callHandler', handler: 'onFree', args: [address]});
Copy link
Collaborator

Choose a reason for hiding this comment

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

Seems like this could be abstracted into a helper function.

var loc = new Error().stack.toString();
if(loc == undefined) {
loc = new Error().stack.toString();
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

How about we just expect loc to be defined by the caller? i.e. can you instead assert(loc) here, and then all the callsites can be in charge of calling new Error().stack.toString()

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@sbc100 sbc100 changed the title memoryprofiler: show allocations from threads [memoryprofiler] Show allocations from threads Feb 23, 2026
@juj
Copy link
Collaborator

juj commented Feb 23, 2026

LGTM. One note of concern I have though is that we still have an unfixed design problem/regression from #19683 (comment) , which revolves around the callHandlers abstraction.

In that discussion I had made the recommendation to remove the callHandlers mechanism altogether (with the thinking that we'd do it while we still can). This adds one more usage of it, effectively making the semantics of shutting down a program more difficult.

Still, this is "only" for debug code, and I don't have a good solution for the above issue, so maybe that is not a good thing to block on for this.

@matzf matzf force-pushed the feature/memoryprofiler-pthread branch from 4b806ed to d1eb5c7 Compare February 27, 2026 18:27
@matzf matzf force-pushed the feature/memoryprofiler-pthread branch from d1eb5c7 to 72ddb60 Compare February 27, 2026 18:46
@matzf
Copy link
Contributor Author

matzf commented Feb 27, 2026

Thanks for your reviews! I found that the existing automatic-ish proxying of the Module methods in libpthread can be used to forward the trace/memoryprofiler hooks to the main thread. With this, I don't need the postMessage calls scattered around.

@sbc100 sbc100 enabled auto-merge (squash) February 27, 2026 21:21
@sbc100
Copy link
Collaborator

sbc100 commented Feb 27, 2026

I can update the codesize expectation for this PR

@sbc100 sbc100 disabled auto-merge February 27, 2026 22:03
@sbc100 sbc100 merged commit 375bba6 into emscripten-core:main Feb 27, 2026
20 of 36 checks passed
@matzf
Copy link
Contributor Author

matzf commented Feb 27, 2026

Great! Thanks a lot for your help, @sbc100 and @juj!

sbc100 added a commit that referenced this pull request Feb 27, 2026
…fault. NFC (#26363)

These are only ever used by EMSCRIPTEN_TRACING so we know exactly when
to include them.

I noticed this while reviewing #26175
sbc100 added a commit that referenced this pull request Feb 28, 2026
This allows for the complete removal of `libmemoryprofiler.js` since the
memory profiler can now just use the same libtrace-based hooks for all
events.

I noticed this while reviewing #26175
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.

Memoryprofiler doesn't support multi-threaded application

3 participants