Skip to content

Commit

Permalink
Use samples for V8 Heap Profiler instead
Browse files Browse the repository at this point in the history
  • Loading branch information
krsh732 committed Feb 13, 2023
1 parent 44bc595 commit 6820f49
Showing 1 changed file with 42 additions and 5 deletions.
47 changes: 42 additions & 5 deletions src/profile-logic/import/v8-heap-profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import type {
Profile,
Bytes,
IndexIntoStackTable,
IndexIntoCategoryList,
IndexIntoSubcategoryListForCategory,
} from 'firefox-profiler/types';
Expand Down Expand Up @@ -187,10 +188,12 @@ export function attemptToConvertV8HeapProfile(json: mixed): Profile | null {
thread.name = 'Total Allocated Bytes';

const funcKeyToFuncId = new Map<string, number>();
// KTODO: This map is not necessary if we rely on node ids being incremental numbers.
const nodeIdToStackId = new Map<NodeId, IndexIntoStackTable>();
const allocationsTable = getEmptyUnbalancedNativeAllocationsTable();
const { funcTable, stringTable, frameTable, stackTable } = thread;

const { head } = coerce<mixed, SamplingHeapProfile>(json);
const { head, samples } = coerce<mixed, SamplingHeapProfile>(json);
// Traverse the tree and populate the tables.
// Each node of the traversal stack is a pair (heap node, stack table index of parent node).
const traversalStack = [[head, null]];
Expand Down Expand Up @@ -233,10 +236,14 @@ export function attemptToConvertV8HeapProfile(json: mixed): Profile | null {
frameTable.length++;
}

allocationsTable.time.push(0);
allocationsTable.stack.push(stackTable.length);
allocationsTable.weight.push(node.selfSize);
allocationsTable.length++;
if (samples.length) {
nodeIdToStackId.set(node.id, stackTable.length);
} else {
allocationsTable.time.push(0);
allocationsTable.stack.push(stackTable.length);
allocationsTable.weight.push(node.selfSize);
allocationsTable.length++;
}

stackTable.frame.push(funcId);
stackTable.category.push(ensureExists(frameTable.category[funcId]));
Expand All @@ -248,7 +255,37 @@ export function attemptToConvertV8HeapProfile(json: mixed): Profile | null {
stackTable.length++;
}

// Go over the samples by ascending ordinals since allocationsTable needs to be ordered.
for (const sample of [...samples].sort((a, b) => a.ordinal - b.ordinal)) {
const { ordinal, nodeId, size } = sample;
allocationsTable.time.push(ordinal);
allocationsTable.stack.push(ensureExists(nodeIdToStackId.get(nodeId)));
allocationsTable.weight.push(size);
allocationsTable.length++;
}

thread.nativeAllocations = allocationsTable;
profile.threads = [thread];
if (samples.length) {
profile.counters = [
{
name: 'Memory',
category: 'Memory',
description: 'Graph of total allocated memory',
pid: 0,
mainThreadIndex: 0,
sampleGroups: [
{
id: 0,
samples: {
time: allocationsTable.time,
count: allocationsTable.weight,
length: allocationsTable.length,
},
},
],
},
];
}
return profile;
}

0 comments on commit 6820f49

Please sign in to comment.