Skip to content

Commit 54c87f3

Browse files
committed
deps: cherry-pick 6ee8345 from upstream V8
Original commit message: [heap-profiler] Allow embedder to specify edge names This patch adds a variant of EmbedderGraph::AddEdge() which allows the embedder to specify the name of an edge. The edges added without name are element edges with auto-incremented indexes while the edges added with names will be internal edges with the specified names for more meaningful output in the heap snapshot. Refs: #21741 Bug: v8:7938 Cq-Include-Trybots: luci.chromium.try:linux_chromium_rel_ng Change-Id: I8feefa2cf6911743e24b3b2024e0e849b0c65cd3 Reviewed-on: https://chromium-review.googlesource.com/1133299 Commit-Queue: Ulan Degenbaev <ulan@chromium.org> Reviewed-by: Ulan Degenbaev <ulan@chromium.org> Cr-Commit-Position: refs/heads/master@{#54412} Refs: v8/v8@6ee8345 PR-URL: #22106 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 44d04a8 commit 54c87f3

File tree

4 files changed

+101
-7
lines changed

4 files changed

+101
-7
lines changed

common.gypi

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
# Reset this number to 0 on major V8 upgrades.
3131
# Increment by one for each non-official patch applied to deps/v8.
32-
'v8_embedder_string': '-node.15',
32+
'v8_embedder_string': '-node.16',
3333

3434
# Enable disassembler for `--print-code` v8 options
3535
'v8_enable_disassembler': 1,

deps/v8/include/v8-profiler.h

+6-3
Original file line numberDiff line numberDiff line change
@@ -686,11 +686,14 @@ class V8_EXPORT EmbedderGraph {
686686
virtual Node* AddNode(std::unique_ptr<Node> node) = 0;
687687

688688
/**
689-
* Adds an edge that represents a strong reference from the given node
690-
* |from| to the given node |to|. The nodes must be added to the graph
689+
* Adds an edge that represents a strong reference from the given
690+
* node |from| to the given node |to|. The nodes must be added to the graph
691691
* before calling this function.
692+
*
693+
* If name is nullptr, the edge will have auto-increment indexes, otherwise
694+
* it will be named accordingly.
692695
*/
693-
virtual void AddEdge(Node* from, Node* to) = 0;
696+
virtual void AddEdge(Node* from, Node* to, const char* name = nullptr) = 0;
694697

695698
virtual ~EmbedderGraph() = default;
696699
};

deps/v8/src/profiler/heap-snapshot-generator.cc

+11-3
Original file line numberDiff line numberDiff line change
@@ -1989,6 +1989,7 @@ class EmbedderGraphImpl : public EmbedderGraph {
19891989
struct Edge {
19901990
Node* from;
19911991
Node* to;
1992+
const char* name;
19921993
};
19931994

19941995
class V8NodeImpl : public Node {
@@ -2025,7 +2026,9 @@ class EmbedderGraphImpl : public EmbedderGraph {
20252026
return result;
20262027
}
20272028

2028-
void AddEdge(Node* from, Node* to) final { edges_.push_back({from, to}); }
2029+
void AddEdge(Node* from, Node* to, const char* name) final {
2030+
edges_.push_back({from, to, name});
2031+
}
20292032

20302033
const std::vector<std::unique_ptr<Node>>& nodes() { return nodes_; }
20312034
const std::vector<Edge>& edges() { return edges_; }
@@ -2318,8 +2321,13 @@ bool NativeObjectsExplorer::IterateAndExtractReferences(
23182321
int from_index = from->index();
23192322
HeapEntry* to = EntryForEmbedderGraphNode(edge.to);
23202323
if (to) {
2321-
filler_->SetIndexedAutoIndexReference(HeapGraphEdge::kElement,
2322-
from_index, to);
2324+
if (edge.name == nullptr) {
2325+
filler_->SetIndexedAutoIndexReference(HeapGraphEdge::kElement,
2326+
from_index, to);
2327+
} else {
2328+
filler_->SetNamedReference(HeapGraphEdge::kInternal, from_index,
2329+
edge.name, to);
2330+
}
23232331
}
23242332
}
23252333
} else {

deps/v8/test/cctest/test-heap-profiler.cc

+83
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ static const char* GetName(const v8::HeapGraphNode* node) {
112112
->name();
113113
}
114114

115+
static const char* GetName(const v8::HeapGraphEdge* edge) {
116+
return const_cast<i::HeapGraphEdge*>(
117+
reinterpret_cast<const i::HeapGraphEdge*>(edge))
118+
->name();
119+
}
120+
115121
static size_t GetSize(const v8::HeapGraphNode* node) {
116122
return const_cast<i::HeapEntry*>(reinterpret_cast<const i::HeapEntry*>(node))
117123
->self_size();
@@ -128,6 +134,18 @@ static const v8::HeapGraphNode* GetChildByName(const v8::HeapGraphNode* node,
128134
return nullptr;
129135
}
130136

137+
static const v8::HeapGraphEdge* GetEdgeByChildName(
138+
const v8::HeapGraphNode* node, const char* name) {
139+
for (int i = 0, count = node->GetChildrenCount(); i < count; ++i) {
140+
const v8::HeapGraphEdge* edge = node->GetChild(i);
141+
const v8::HeapGraphNode* child = edge->GetToNode();
142+
if (!strcmp(name, GetName(child))) {
143+
return edge;
144+
}
145+
}
146+
return nullptr;
147+
}
148+
131149
static const v8::HeapGraphNode* GetRootChild(const v8::HeapSnapshot* snapshot,
132150
const char* name) {
133151
return GetChildByName(snapshot->GetRoot(), name);
@@ -2986,6 +3004,71 @@ TEST(EmbedderGraph) {
29863004
CheckEmbedderGraphSnapshot(env->GetIsolate(), snapshot);
29873005
}
29883006

3007+
void BuildEmbedderGraphWithNamedEdges(v8::Isolate* v8_isolate,
3008+
v8::EmbedderGraph* graph, void* data) {
3009+
using Node = v8::EmbedderGraph::Node;
3010+
Node* global_node = graph->V8Node(*global_object_pointer);
3011+
Node* embedder_node_A = graph->AddNode(
3012+
std::unique_ptr<Node>(new EmbedderNode("EmbedderNodeA", 10)));
3013+
Node* embedder_node_B = graph->AddNode(
3014+
std::unique_ptr<Node>(new EmbedderNode("EmbedderNodeB", 20)));
3015+
Node* embedder_node_C = graph->AddNode(
3016+
std::unique_ptr<Node>(new EmbedderNode("EmbedderNodeC", 30)));
3017+
graph->AddEdge(global_node, embedder_node_A, "global_to_a");
3018+
graph->AddEdge(embedder_node_A, embedder_node_B, "a_to_b");
3019+
graph->AddEdge(embedder_node_B, embedder_node_C);
3020+
}
3021+
3022+
void CheckEmbedderGraphWithNamedEdges(v8::Isolate* isolate,
3023+
const v8::HeapSnapshot* snapshot) {
3024+
const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
3025+
const v8::HeapGraphEdge* global_to_a =
3026+
GetEdgeByChildName(global, "EmbedderNodeA");
3027+
CHECK(global_to_a);
3028+
CHECK_EQ(v8::HeapGraphEdge::kInternal, global_to_a->GetType());
3029+
CHECK(global_to_a->GetName()->IsString());
3030+
CHECK_EQ(0, strcmp("global_to_a", GetName(global_to_a)));
3031+
const v8::HeapGraphNode* embedder_node_A = global_to_a->GetToNode();
3032+
CHECK_EQ(0, strcmp("EmbedderNodeA", GetName(embedder_node_A)));
3033+
CHECK_EQ(10, GetSize(embedder_node_A));
3034+
3035+
const v8::HeapGraphEdge* a_to_b =
3036+
GetEdgeByChildName(embedder_node_A, "EmbedderNodeB");
3037+
CHECK(a_to_b);
3038+
CHECK(a_to_b->GetName()->IsString());
3039+
CHECK_EQ(0, strcmp("a_to_b", GetName(a_to_b)));
3040+
CHECK_EQ(v8::HeapGraphEdge::kInternal, a_to_b->GetType());
3041+
const v8::HeapGraphNode* embedder_node_B = a_to_b->GetToNode();
3042+
CHECK_EQ(0, strcmp("EmbedderNodeB", GetName(embedder_node_B)));
3043+
CHECK_EQ(20, GetSize(embedder_node_B));
3044+
3045+
const v8::HeapGraphEdge* b_to_c =
3046+
GetEdgeByChildName(embedder_node_B, "EmbedderNodeC");
3047+
CHECK(b_to_c);
3048+
CHECK(b_to_c->GetName()->IsNumber());
3049+
CHECK_EQ(v8::HeapGraphEdge::kElement, b_to_c->GetType());
3050+
const v8::HeapGraphNode* embedder_node_C = b_to_c->GetToNode();
3051+
CHECK_EQ(0, strcmp("EmbedderNodeC", GetName(embedder_node_C)));
3052+
CHECK_EQ(30, GetSize(embedder_node_C));
3053+
}
3054+
3055+
TEST(EmbedderGraphWithNamedEdges) {
3056+
i::FLAG_heap_profiler_use_embedder_graph = true;
3057+
LocalContext env;
3058+
v8::HandleScope scope(env->GetIsolate());
3059+
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(env->GetIsolate());
3060+
v8::Local<v8::Value> global_object =
3061+
v8::Utils::ToLocal(i::Handle<i::JSObject>(
3062+
(isolate->context()->native_context()->global_object()), isolate));
3063+
global_object_pointer = &global_object;
3064+
v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
3065+
heap_profiler->AddBuildEmbedderGraphCallback(BuildEmbedderGraphWithNamedEdges,
3066+
nullptr);
3067+
const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot();
3068+
CHECK(ValidateSnapshot(snapshot));
3069+
CheckEmbedderGraphWithNamedEdges(env->GetIsolate(), snapshot);
3070+
}
3071+
29893072
struct GraphBuildingContext {
29903073
int counter = 0;
29913074
};

0 commit comments

Comments
 (0)