@@ -112,6 +112,12 @@ static const char* GetName(const v8::HeapGraphNode* node) {
112
112
->name ();
113
113
}
114
114
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
+
115
121
static size_t GetSize (const v8::HeapGraphNode* node) {
116
122
return const_cast <i::HeapEntry*>(reinterpret_cast <const i::HeapEntry*>(node))
117
123
->self_size ();
@@ -128,6 +134,18 @@ static const v8::HeapGraphNode* GetChildByName(const v8::HeapGraphNode* node,
128
134
return nullptr ;
129
135
}
130
136
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
+
131
149
static const v8::HeapGraphNode* GetRootChild (const v8::HeapSnapshot* snapshot,
132
150
const char * name) {
133
151
return GetChildByName (snapshot->GetRoot (), name);
@@ -2986,6 +3004,71 @@ TEST(EmbedderGraph) {
2986
3004
CheckEmbedderGraphSnapshot (env->GetIsolate (), snapshot);
2987
3005
}
2988
3006
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
+
2989
3072
struct GraphBuildingContext {
2990
3073
int counter = 0 ;
2991
3074
};
0 commit comments