Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib/libmemoryprofiler.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
var memoryProfiler = {
emscripten_memprof_sbrk_grow: (old_brk, new_brk) => {
#if MEMORYPROFILER
emscriptenMemoryProfiler.onSbrkGrow(old_brk, new_brk);
Module['onSbrkGrow']?.(old_brk, new_brk) || postMessage({cmd: 'callHandler', handler: 'onSbrkGrow', args: [old_brk, new_brk, new Error().stack.toString()]});
#endif
},
};
Expand Down
12 changes: 9 additions & 3 deletions src/lib/libtrace.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,9 @@ var LibraryTracing = {
},

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()]});
#endif
if (EmscriptenTrace.postEnabled) {
var now = EmscriptenTrace.now();
EmscriptenTrace.post([EmscriptenTrace.EVENT_ALLOCATE,
Expand All @@ -219,7 +221,9 @@ var LibraryTracing = {
},

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()]});
#endif
if (EmscriptenTrace.postEnabled) {
var now = EmscriptenTrace.now();
EmscriptenTrace.post([EmscriptenTrace.EVENT_REALLOCATE,
Expand All @@ -228,7 +232,9 @@ var LibraryTracing = {
},

emscripten_trace_record_free: (address) => {
Module['onFree']?.(address);
#if MEMORYPROFILER
Module['onFree']?.(address) || postMessage({cmd: 'callHandler', handler: 'onFree', args: [address]});
#endif
if (EmscriptenTrace.postEnabled) {
var now = EmscriptenTrace.now();
EmscriptenTrace.post([EmscriptenTrace.EVENT_FREE,
Expand Down
22 changes: 14 additions & 8 deletions src/memoryprofiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ var emscriptenMemoryProfiler = {
return '#' + toHex(r) + toHex(g) + toHex(b);
},

onSbrkGrow(oldLimit, newLimit) {
onSbrkGrow(oldLimit, newLimit, loc) {
var self = emscriptenMemoryProfiler;
// On first sbrk(), account for the initial size.
if (self.sbrkSources.length == 0) {
Expand All @@ -130,8 +130,11 @@ var emscriptenMemoryProfiler = {
});
}
if (newLimit <= oldLimit) return;
if (loc == undefined) {
loc = new Error().stack.toString();
}
self.sbrkSources.push({
stack: self.filterCallstackForHeapResize(new Error().stack.toString()),
stack: self.filterCallstackForHeapResize(loc),
begin: oldLimit,
end: newLimit,
color: self.hsvToRgb(self.sbrkSources.length * 0.618033988749895 % 1, 0.5, 0.95)
Expand Down Expand Up @@ -166,7 +169,7 @@ var emscriptenMemoryProfiler = {
}
},

onMalloc(ptr, size) {
onMalloc(ptr, size, loc) {
if (!ptr) return;
if (emscriptenMemoryProfiler.sizeOfAllocatedPtr[ptr])
{
Expand All @@ -189,7 +192,9 @@ var emscriptenMemoryProfiler = {
// Also track if this was a _malloc performed at preRun time.
if (!self.pagePreRunIsFinished) self.sizeOfPreRunAllocatedPtr[ptr] = size;

var loc = new Error().stack.toString();
if(loc == undefined) {
loc = new Error().stack.toString();
}
self.allocationsAtLoc[loc] ||= [0, 0, self.filterCallstackForMalloc(loc)];
self.allocationsAtLoc[loc][0] += 1;
self.allocationsAtLoc[loc][1] += size;
Expand Down Expand Up @@ -229,9 +234,9 @@ var emscriptenMemoryProfiler = {
++self.totalTimesFreeCalled;
},

onRealloc(oldAddress, newAddress, size) {
onRealloc(oldAddress, newAddress, size, loc) {
emscriptenMemoryProfiler.onFree(oldAddress);
emscriptenMemoryProfiler.onMalloc(newAddress, size);
emscriptenMemoryProfiler.onMalloc(newAddress, size, loc);
},

onPreloadComplete() {
Expand All @@ -241,9 +246,10 @@ var emscriptenMemoryProfiler = {
// Installs startup hook and periodic UI update timer.
initialize() {
// Inject the memoryprofiler hooks.
Module['onMalloc'] = (ptr, size) => emscriptenMemoryProfiler.onMalloc(ptr, size);
Module['onRealloc'] = (oldAddress, newAddress, size) => emscriptenMemoryProfiler.onRealloc(oldAddress, newAddress, size);;
Module['onMalloc'] = (ptr, size, loc) => emscriptenMemoryProfiler.onMalloc(ptr, size, loc);
Module['onRealloc'] = (oldAddress, newAddress, size, loc) => emscriptenMemoryProfiler.onRealloc(oldAddress, newAddress, size, loc);;
Module['onFree'] = (ptr) => emscriptenMemoryProfiler.onFree(ptr);
Module['onSbrkGrow'] = (old_brk, new_brk, loc) => emscriptenMemoryProfiler.onSbrkGrow(old_brk, new_brk, loc);
emscriptenMemoryProfiler.recordStackWatermark();

// Add a tracking mechanism to detect when VFS loading is complete.
Expand Down
2 changes: 1 addition & 1 deletion src/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -999,7 +999,7 @@ var INCOMING_MODULE_JS_API = [
'loadSplitModule', 'locateFile', 'logReadFiles', 'mainScriptUrlOrBlob', 'mem',
'monitorRunDependencies', 'noExitRuntime', 'noInitialRun', 'onAbort',
'onExit', 'onFree', 'onFullScreen', 'onMalloc',
'onRealloc', 'onRuntimeInitialized', 'postMainLoop', 'postRun', 'preInit',
'onRealloc', 'onRuntimeInitialized', 'onSbrkGrow', 'postMainLoop', 'postRun', 'preInit',
'preMainLoop', 'preRun',
'preinitializedWebGLContext', 'preloadPlugins',
'print', 'printErr', 'setStatus', 'statusMessage', 'stderr',
Expand Down