Skip to content

Commit

Permalink
fixup! Temporal inverted flame graph
Browse files Browse the repository at this point in the history
Signed-off-by: Ivona Stojanovic <[email protected]>
  • Loading branch information
ivonastojanovic committed Sep 8, 2023
1 parent 21bdff5 commit e65c1e9
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 42 deletions.
93 changes: 52 additions & 41 deletions src/memray/reporters/assets/temporal_flamegraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]],
Expand Down Expand Up @@ -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;
Expand All @@ -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;

Expand All @@ -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;

Expand Down Expand Up @@ -194,39 +193,50 @@ 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");
findLeakedAllocations(
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;
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/memray/reporters/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ <h5 class="modal-title" id="helpModalLabel">How to interpret {{ kind }} reports<
const merge_threads = {{ merge_threads|tojson }};
const memory_records = {{ memory_records|tojson }};
const inverted = {{ inverted|tojson }};
const temporal = packed_data["high_water_mark_by_snapshot"] != null;
const temporal = packed_data.intervals != null;
</script>
{% endblock scripts %}
</body>
Expand Down

0 comments on commit e65c1e9

Please sign in to comment.