Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include array length and promise status in V8 heap snapshots #75

Merged
merged 2 commits into from
Jan 3, 2025
Merged
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
48 changes: 28 additions & 20 deletions Source/JavaScriptCore/heap/BunV8HeapSnapshotBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ void BunV8HeapSnapshotBuilder::analyzeNode(JSCell* cell)
}
}


unsigned BunV8HeapSnapshotBuilder::analyzeNodeInternal(JSCell* cell, void* optionalHashId)
{

Expand Down Expand Up @@ -230,7 +229,6 @@ void BunV8HeapSnapshotBuilder::analyzeVariableNameEdge(JSCell* from, JSCell* to,
edge.typeIndex = static_cast<unsigned>(V8EdgeType::Context);
edge.name = String(variableName);


m_edges.append(WTFMove(edge));
}

Expand All @@ -250,9 +248,10 @@ void BunV8HeapSnapshotBuilder::analyzeIndexEdge(JSCell* from, JSCell* to, uint32
}

void BunV8HeapSnapshotBuilder::setOpaqueRootReachabilityReasonForCell(JSCell*, ASCIILiteral) {}
void BunV8HeapSnapshotBuilder::setWrappedObjectForCell(JSCell* cell, void* wrappedObject) {
void BunV8HeapSnapshotBuilder::setWrappedObjectForCell(JSCell* cell, void* wrappedObject)
{
unsigned id = getOrCreateNodeId(cell, wrappedObject);

// TODO: make this one lock instead of two.
Locker locker { m_buildingNodeMutex };
m_nodes[id].id = generateHashID(cell, wrappedObject);
Expand All @@ -277,7 +276,6 @@ unsigned BunV8HeapSnapshotBuilder::getOrCreateNodeId(JSCell* cell, void* optiona
if (it != m_cellToNodeId.end())
return it->value;


unsigned id = analyzeNodeInternal(cell, optionalHashId);
m_cellToNodeId.set(cell, id);
return id;
Expand Down Expand Up @@ -356,7 +354,7 @@ unsigned BunV8HeapSnapshotBuilder::getNodeTypeIndex(JSCell* cell)
return static_cast<unsigned>(V8NodeType::Native);
}

String BunV8HeapSnapshotBuilder::getDetailedNodeType(JSCell* cell)
String BunV8HeapSnapshotBuilder::getDetailedNodeType(JSCell* cell, bool recurse)
{
if (!cell)
return "(root)"_s;
Expand Down Expand Up @@ -422,15 +420,29 @@ String BunV8HeapSnapshotBuilder::getDetailedNodeType(JSCell* cell)
}

if (JSPromise* promise = jsDynamicCast<JSPromise*>(cell)) {
return "Promise"_s;
auto& vm = promise->globalObject()->vm();
switch (promise->status(vm)) {
case JSPromise::Status::Pending:
return "Promise (pending)"_s;
case JSPromise::Status::Fulfilled: {
JSValue result = promise->result(vm);
if (result.isCell() && recurse) {
// set recurse to false to make sure we don't infinitely expand promises
return makeString("Promise (fulfilled: "_s, getDetailedNodeType(result.asCell(), false), ")"_s);
}
return "Promise (fulfilled)"_s;
}
case JSPromise::Status::Rejected:
return "Promise (rejected)"_s;
}
}

auto* object = cell->getObject();

if (object) {
// For arrays, include the length
if (JSArray* array = jsDynamicCast<JSArray*>(cell)) {
return "Array"_s;
return makeString("Array ("_s, array->length(), ")"_s);
}

// For functions, try to get the display name
Expand Down Expand Up @@ -565,20 +577,19 @@ String BunV8HeapSnapshotBuilder::generateV8HeapSnapshot()
// First sort by fromNodeId
if (a.fromNodeId != b.fromNodeId)
return a.fromNodeId < b.fromNodeId;

// Then by typeIndex
if (a.typeIndex != b.typeIndex)
return a.typeIndex < b.typeIndex;

// Then by toNodeId
if (a.toNodeId != b.toNodeId)
return a.toNodeId < b.toNodeId;

// For element/hidden edges, compare by index
if (a.typeIndex == static_cast<unsigned>(V8EdgeType::Element) ||
a.typeIndex == static_cast<unsigned>(V8EdgeType::Hidden))
if (a.typeIndex == static_cast<unsigned>(V8EdgeType::Element) || a.typeIndex == static_cast<unsigned>(V8EdgeType::Hidden))
return a.index < b.index;

// For named edges, compare by name
return WTF::codePointCompareLessThan(a.name, b.name);
});
Expand All @@ -593,13 +604,10 @@ String BunV8HeapSnapshotBuilder::generateV8HeapSnapshot()
const auto& curr = m_edges[readIndex];

// Check if this is a duplicate edge
bool isDuplicate = prev.fromNodeId == curr.fromNodeId &&
prev.toNodeId == curr.toNodeId &&
prev.typeIndex == curr.typeIndex;
bool isDuplicate = prev.fromNodeId == curr.fromNodeId && prev.toNodeId == curr.toNodeId && prev.typeIndex == curr.typeIndex;

if (isDuplicate) {
if (prev.typeIndex == static_cast<unsigned>(V8EdgeType::Element) ||
prev.typeIndex == static_cast<unsigned>(V8EdgeType::Hidden)) {
if (prev.typeIndex == static_cast<unsigned>(V8EdgeType::Element) || prev.typeIndex == static_cast<unsigned>(V8EdgeType::Hidden)) {
isDuplicate = prev.index == curr.index;
} else {
isDuplicate = prev.name == curr.name;
Expand Down Expand Up @@ -702,7 +710,7 @@ String BunV8HeapSnapshotBuilder::generateV8HeapSnapshot()
json.append("\"edges\":["_s);
for (unsigned i = 0; i < m_edges.size(); ++i) {
const auto& edge = m_edges[i];

// Validate node IDs
ASSERT(edge.fromNodeId < m_nodes.size());
ASSERT(edge.toNodeId < m_nodes.size());
Expand Down Expand Up @@ -768,4 +776,4 @@ String BunV8HeapSnapshotBuilder::generateV8HeapSnapshot()

} // namespace JSC

#endif // USE(BUN_JSC_ADDITIONS)
#endif // USE(BUN_JSC_ADDITIONS)
2 changes: 1 addition & 1 deletion Source/JavaScriptCore/heap/BunV8HeapSnapshotBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class JS_EXPORT_PRIVATE BunV8HeapSnapshotBuilder final : public HeapAnalyzer {
unsigned getEdgeTypeIndex(const String& type);
unsigned addString(const String&);
void initializeTypeNames();
String getDetailedNodeType(JSCell*);
String getDetailedNodeType(JSCell*, bool recurse = true);
std::optional<TraceLocation> getTraceLocation(JSCell*);
};

Expand Down
Loading