diff --git a/src/memray/reporters/assets/temporal_flamegraph.js b/src/memray/reporters/assets/temporal_flamegraph.js index 076307b9bd..9fd131e08d 100644 --- a/src/memray/reporters/assets/temporal_flamegraph.js +++ b/src/memray/reporters/assets/temporal_flamegraph.js @@ -17,18 +17,22 @@ import { var active_plot = null; var current_dimensions = null; -var parent_index_by_child_index = (function () { - let ret = new Array(packed_data.nodes.children.length); +var parent_index_by_child_index = generateParentIndexes(packed_data.nodes); +var inverted_no_imports_parent_index_by_child_index = inverted + ? generateParentIndexes(packed_data.inverted_no_imports_nodes) + : null; + +function generateParentIndexes(nodes) { + let ret = new Array(nodes.children.length); console.log("finding parent index for each node"); - for (const [parentIndex, children] of packed_data.nodes.children.entries()) { + for (const [parentIndex, children] of nodes.children.entries()) { children.forEach((idx) => (ret[idx] = parentIndex)); } console.assert(ret[0] === undefined, "root node has a parent"); return ret; -})(); +} -function generateNodeObjects(packedData) { - const { strings, nodes } = packedData; +function generateNodeObjects(strings, nodes) { console.log("constructing nodes"); const node_objects = nodes.name.map((_, i) => ({ name: strings[nodes["name"][i]], @@ -62,18 +66,10 @@ function initTrees(packedData) { intervals, no_imports_interval_list, } = packedData; - const flamegraphNodeObjects = generateNodeObjects({ - strings: strings, - nodes: nodes, - unique_threads: unique_threads, - }); + const flamegraphNodeObjects = generateNodeObjects(strings, nodes); const invertedNoImportsNodeObjects = inverted - ? generateNodeObjects({ - strings: strings, - nodes: inverted_no_imports_nodes, - unique_threads: unique_threads, - }) + ? generateNodeObjects(strings, inverted_no_imports_nodes) : null; flamegraphIntervals = intervals; @@ -85,11 +81,12 @@ function initTrees(packedData) { }; } -function findHWMAllocations(intervals, node_objects, hwmSnapshot) { - if (!node_objects) { - return; - } - +function findHWMAllocations( + intervals, + node_objects, + hwmSnapshot, + parent_index_by_child_index +) { intervals.forEach((interval) => { let [allocBefore, deallocBefore, nodeIndex, count, bytes] = interval; @@ -106,11 +103,13 @@ function findHWMAllocations(intervals, node_objects, hwmSnapshot) { }); } -function findLeakedAllocations(intervals, node_objects, rangeStart, rangeEnd) { - if (!node_objects) { - return; - } - +function findLeakedAllocations( + intervals, + node_objects, + rangeStart, + rangeEnd, + parent_index_by_child_index +) { intervals.forEach((interval) => { let [allocBefore, deallocBefore, nodeIndex, count, bytes] = interval; @@ -194,12 +193,20 @@ function packedDataToTree(packedData, rangeStart, rangeEnd) { // We could binary search rather than using a linear scan... console.log("finding hwm allocations"); - findHWMAllocations(flamegraphIntervals, flamegraphNodeObjects, hwmSnapshot); findHWMAllocations( - invertedNoImportsIntervals, - invertedNoImportsNodeObjects, - hwmSnapshot + flamegraphIntervals, + flamegraphNodeObjects, + hwmSnapshot, + parent_index_by_child_index ); + if (inverted) { + findHWMAllocations( + invertedNoImportsIntervals, + invertedNoImportsNodeObjects, + hwmSnapshot, + inverted_no_imports_parent_index_by_child_index + ); + } } else { // We could binary search rather than using a linear scan... console.log("finding leaked allocations"); @@ -207,26 +214,29 @@ function packedDataToTree(packedData, rangeStart, rangeEnd) { flamegraphIntervals, flamegraphNodeObjects, rangeStart, - rangeEnd - ); - findLeakedAllocations( - invertedNoImportsIntervals, - invertedNoImportsNodeObjects, - rangeStart, - rangeEnd + rangeEnd, + parent_index_by_child_index ); + if (inverted) { + findLeakedAllocations( + invertedNoImportsIntervals, + invertedNoImportsNodeObjects, + rangeStart, + rangeEnd, + inverted_no_imports_parent_index_by_child_index + ); + } } flamegraphNodeObjects.forEach((node) => { node.children = node.children.filter((node) => node.n_allocations > 0); }); - // if(inverted){ - inverted && + if (inverted) { invertedNoImportsNodeObjects.forEach((node) => { node.children = node.children.filter((node) => node.n_allocations > 0); }); - // } + } flamegraphData = flamegraphNodeObjects[0]; invertedNoImportsData = inverted ? invertedNoImportsNodeObjects[0] : null; @@ -337,7 +347,8 @@ function refreshFlamegraph(event) { packedDataToTree(packed_data, idx0, idx1); data = inverted && hideImports ? invertedNoImportsData : flamegraphData; - intervals = inverted && hideImports ? c : flamegraphIntervals; + intervals = + inverted && hideImports ? invertedNoImportsIntervals : flamegraphIntervals; console.log("total allocations in range: " + data.n_allocations); console.log("total bytes in range: " + data.value); diff --git a/src/memray/reporters/templates/base.html b/src/memray/reporters/templates/base.html index 7b57f88fde..949c305a6e 100644 --- a/src/memray/reporters/templates/base.html +++ b/src/memray/reporters/templates/base.html @@ -144,7 +144,7 @@