[memoryprofiler] Show allocations from threads#26175
[memoryprofiler] Show allocations from threads#26175matzf wants to merge 1 commit intoemscripten-core:mainfrom
Conversation
532aa6f to
95777be
Compare
95777be to
ba2463e
Compare
|
@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. |
sbc100
left a comment
There was a problem hiding this comment.
Sorry I forgot to send these comments from earlier.
| emscripten_trace_record_allocation: (address, size) => { | ||
| Module['onMalloc']?.(address, size); | ||
| #if MEMORYPROFILER | ||
| Module['onMalloc']?.(address, size) || postMessage({cmd: 'callHandler', handler: 'onMalloc', args: [address, size, new Error().stack.toString()]}); |
There was a problem hiding this comment.
I think you can you the JS library function jsStackTrace() instead of new Error().stack.toString()
| emscripten_trace_record_free: (address) => { | ||
| Module['onFree']?.(address); | ||
| #if MEMORYPROFILER | ||
| Module['onFree']?.(address) || postMessage({cmd: 'callHandler', handler: 'onFree', args: [address]}); |
There was a problem hiding this comment.
Seems like this could be abstracted into a helper function.
| var loc = new Error().stack.toString(); | ||
| if(loc == undefined) { | ||
| loc = new Error().stack.toString(); | ||
| } |
There was a problem hiding this comment.
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()
|
|
||
| emscripten_trace_record_allocation: (address, size) => { | ||
| Module['onMalloc']?.(address, size); | ||
| #if MEMORYPROFILER |
There was a problem hiding this comment.
It seems like this tracing API could be used for things other than the MEMORYPROFILER before. Is the onMalloc callback only for MEMORYPROFILER?
There was a problem hiding this comment.
Originally the tracing api was used for a generic user facing thing. We had another collaborator develop a custom tracing api for example. So onMalloc is something that was intended to be a generic notification.
MEMORYPROFILER then grew as a feature that was bundled into the repository itself.
There was a problem hiding this comment.
At Unity, I've implemented a separate memory allocation profiler that also uses these onMalloc hooks, that is separate from the MEMORYPROFILER feature.
There was a problem hiding this comment.
So this code here should not be wrapped in #if MEMORYPROFILER right?
There was a problem hiding this comment.
@juj, presumably your mechanism still uses this file? i.e. it uses -sEMSCRIPTEN_TRACING?
There was a problem hiding this comment.
Yeah, good point - this code location should not be gated inside MEMORYPROFILER: this is the generic support path, and MEMORYPROFILER is just one implementation that uses the tracing API.
|
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 In that discussion I had made the recommendation to remove the 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. |
| emscripten_trace_record_reallocation: (old_address, new_address, size) => { | ||
| Module['onRealloc']?.(old_address, new_address, size); | ||
| #if MEMORYPROFILER | ||
| Module['onRealloc']?.(old_address, new_address, size) || postMessage({cmd: 'callHandler', handler: 'onRealloc', args: [old_address, new_address, size, new Error().stack.toString()]}); |
There was a problem hiding this comment.
JS syntax question/clarification: how does Module['onRealloc']?.(...) || postMessage(...) work exactly?
Here it is used with the intent of saying
if (Module['onRealloc']) Module['onRealloc'](...); // if we are on main thread, call onRealloc
else postMessage(...); // otherwise, dispatch to call handlerthough my understanding of JS parsing here says that the written code construct instead does this:
var ret = Module['onRealloc'] && Module['onRealloc'](...);
if (!ret) postMessage(...);which does not seem the correct thing to do?
On worker thread, ret will always be undefined, so the postMessage() will fire.
But on the main thread, it seems like if the return value of Module['onRealloc']() function is undefined, then the main thread will *also* postMessage(), which should not happen. (I think it will cause a JS error, since main thread does not have a "postmessage-to-parent" construct?)
Support multi-threaded applications in memoryprofiler.
Fixes #18107.