blob: f8f1586e3993ee64790465531a5150037d0d2851 [file] [log] [blame]
Ben Murdoch257744e2011-11-30 15:57:28 +00001// Copyright 2011 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Steve Blocka7e24c12009-10-30 11:49:00 +000027//
28// Tests for heap profiler
29
Ben Murdochb8a8cc12014-11-26 15:28:44 +000030#include <ctype.h>
Ben Murdoch257744e2011-11-30 15:57:28 +000031
Ben Murdochb8a8cc12014-11-26 15:28:44 +000032#include "src/v8.h"
33
34#include "include/v8-profiler.h"
Ben Murdoch61f157c2016-09-16 13:49:30 +010035#include "src/base/hashmap.h"
Ben Murdochda12d292016-06-02 14:46:10 +010036#include "src/collector.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000037#include "src/debug/debug.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000038#include "src/profiler/allocation-tracker.h"
39#include "src/profiler/heap-profiler.h"
40#include "src/profiler/heap-snapshot-generator-inl.h"
Ben Murdochb8a8cc12014-11-26 15:28:44 +000041#include "test/cctest/cctest.h"
42
43using i::AllocationTraceNode;
44using i::AllocationTraceTree;
45using i::AllocationTracker;
Ben Murdochc5610432016-08-08 18:44:38 +010046using i::ArrayVector;
Ben Murdochb8a8cc12014-11-26 15:28:44 +000047using i::Vector;
Steve Blocka7e24c12009-10-30 11:49:00 +000048
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010049namespace {
50
51class NamedEntriesDetector {
52 public:
53 NamedEntriesDetector()
Russell Brenner90bac252010-11-18 13:33:46 -080054 : has_A2(false), has_B2(false), has_C2(false) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010055 }
56
Ben Murdoch3ef787d2012-04-12 10:51:47 +010057 void CheckEntry(i::HeapEntry* entry) {
58 if (strcmp(entry->name(), "A2") == 0) has_A2 = true;
59 if (strcmp(entry->name(), "B2") == 0) has_B2 = true;
60 if (strcmp(entry->name(), "C2") == 0) has_C2 = true;
Iain Merrick75681382010-08-19 15:07:18 +010061 }
62
Ben Murdochb8a8cc12014-11-26 15:28:44 +000063 static bool AddressesMatch(void* key1, void* key2) {
64 return key1 == key2;
65 }
66
Ben Murdoch3ef787d2012-04-12 10:51:47 +010067 void CheckAllReachables(i::HeapEntry* root) {
Ben Murdoch61f157c2016-09-16 13:49:30 +010068 v8::base::HashMap visited(AddressesMatch);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010069 i::List<i::HeapEntry*> list(10);
70 list.Add(root);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010071 CheckEntry(root);
72 while (!list.is_empty()) {
73 i::HeapEntry* entry = list.RemoveLast();
Ben Murdochb8a8cc12014-11-26 15:28:44 +000074 i::Vector<i::HeapGraphEdge*> children = entry->children();
Ben Murdoch3ef787d2012-04-12 10:51:47 +010075 for (int i = 0; i < children.length(); ++i) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000076 if (children[i]->type() == i::HeapGraphEdge::kShortcut) continue;
77 i::HeapEntry* child = children[i]->to();
Ben Murdoch61f157c2016-09-16 13:49:30 +010078 v8::base::HashMap::Entry* entry = visited.LookupOrInsert(
Ben Murdochb8a8cc12014-11-26 15:28:44 +000079 reinterpret_cast<void*>(child),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000080 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(child)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000081 if (entry->value)
82 continue;
83 entry->value = reinterpret_cast<void*>(1);
84 list.Add(child);
85 CheckEntry(child);
Ben Murdoch3ef787d2012-04-12 10:51:47 +010086 }
87 }
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010088 }
89
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010090 bool has_A2;
91 bool has_B2;
92 bool has_C2;
93};
94
95} // namespace
96
97
98static const v8::HeapGraphNode* GetGlobalObject(
99 const v8::HeapSnapshot* snapshot) {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800100 CHECK_EQ(2, snapshot->GetRoot()->GetChildrenCount());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000101 // The 0th-child is (GC Roots), 1st is the user root.
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800102 const v8::HeapGraphNode* global_obj =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000103 snapshot->GetRoot()->GetChild(1)->GetToNode();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000104 CHECK_EQ(0, strncmp("Object", const_cast<i::HeapEntry*>(
105 reinterpret_cast<const i::HeapEntry*>(global_obj))->name(), 6));
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800106 return global_obj;
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100107}
108
109
110static const v8::HeapGraphNode* GetProperty(const v8::HeapGraphNode* node,
111 v8::HeapGraphEdge::Type type,
112 const char* name) {
113 for (int i = 0, count = node->GetChildrenCount(); i < count; ++i) {
114 const v8::HeapGraphEdge* prop = node->GetChild(i);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000115 v8::String::Utf8Value prop_name(prop->GetName());
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100116 if (prop->GetType() == type && strcmp(name, *prop_name) == 0)
117 return prop->GetToNode();
118 }
119 return NULL;
120}
121
122
123static bool HasString(const v8::HeapGraphNode* node, const char* contents) {
124 for (int i = 0, count = node->GetChildrenCount(); i < count; ++i) {
125 const v8::HeapGraphEdge* prop = node->GetChild(i);
126 const v8::HeapGraphNode* node = prop->GetToNode();
Iain Merrick75681382010-08-19 15:07:18 +0100127 if (node->GetType() == v8::HeapGraphNode::kString) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000128 v8::String::Utf8Value node_name(node->GetName());
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100129 if (strcmp(contents, *node_name) == 0) return true;
130 }
131 }
132 return false;
133}
134
135
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000136static bool AddressesMatch(void* key1, void* key2) {
137 return key1 == key2;
138}
139
140
141// Check that snapshot has no unretained entries except root.
142static bool ValidateSnapshot(const v8::HeapSnapshot* snapshot, int depth = 3) {
143 i::HeapSnapshot* heap_snapshot = const_cast<i::HeapSnapshot*>(
144 reinterpret_cast<const i::HeapSnapshot*>(snapshot));
145
Ben Murdoch61f157c2016-09-16 13:49:30 +0100146 v8::base::HashMap visited(AddressesMatch);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000147 i::List<i::HeapGraphEdge>& edges = heap_snapshot->edges();
148 for (int i = 0; i < edges.length(); ++i) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100149 v8::base::HashMap::Entry* entry = visited.LookupOrInsert(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000150 reinterpret_cast<void*>(edges[i].to()),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000151 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(edges[i].to())));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000152 uint32_t ref_count = static_cast<uint32_t>(
153 reinterpret_cast<uintptr_t>(entry->value));
154 entry->value = reinterpret_cast<void*>(ref_count + 1);
155 }
156 uint32_t unretained_entries_count = 0;
157 i::List<i::HeapEntry>& entries = heap_snapshot->entries();
158 for (int i = 0; i < entries.length(); ++i) {
Ben Murdoch61f157c2016-09-16 13:49:30 +0100159 v8::base::HashMap::Entry* entry = visited.Lookup(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000160 reinterpret_cast<void*>(&entries[i]),
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000161 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(&entries[i])));
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000162 if (!entry && entries[i].id() != 1) {
163 entries[i].Print("entry with no retainer", "", depth, 0);
164 ++unretained_entries_count;
165 }
166 }
167 return unretained_entries_count == 0;
168}
169
170
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100171TEST(HeapSnapshot) {
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100172 LocalContext env2;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000173 v8::HandleScope scope(env2->GetIsolate());
174 v8::HeapProfiler* heap_profiler = env2->GetIsolate()->GetHeapProfiler();
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100175
Ben Murdochf87a2032010-10-22 12:50:53 +0100176 CompileRun(
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100177 "function A2() {}\n"
178 "function B2(x) { return function() { return typeof x; }; }\n"
179 "function C2(x) { this.x1 = x; this.x2 = x; this[1] = x; }\n"
180 "var a2 = new A2();\n"
181 "var b2_1 = new B2(a2), b2_2 = new B2(a2);\n"
182 "var c2 = new C2(a2);");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000183 const v8::HeapSnapshot* snapshot_env2 = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000184 CHECK(ValidateSnapshot(snapshot_env2));
Iain Merrick75681382010-08-19 15:07:18 +0100185 const v8::HeapGraphNode* global_env2 = GetGlobalObject(snapshot_env2);
Iain Merrick75681382010-08-19 15:07:18 +0100186
Russell Brenner90bac252010-11-18 13:33:46 -0800187 // Verify, that JS global object of env2 has '..2' properties.
Iain Merrick75681382010-08-19 15:07:18 +0100188 const v8::HeapGraphNode* a2_node =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000189 GetProperty(global_env2, v8::HeapGraphEdge::kProperty, "a2");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000190 CHECK(a2_node);
191 CHECK(GetProperty(global_env2, v8::HeapGraphEdge::kProperty, "b2_1"));
192 CHECK(GetProperty(global_env2, v8::HeapGraphEdge::kProperty, "b2_2"));
193 CHECK(GetProperty(global_env2, v8::HeapGraphEdge::kProperty, "c2"));
Iain Merrick75681382010-08-19 15:07:18 +0100194
Iain Merrick75681382010-08-19 15:07:18 +0100195 NamedEntriesDetector det;
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100196 det.CheckAllReachables(const_cast<i::HeapEntry*>(
197 reinterpret_cast<const i::HeapEntry*>(global_env2)));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100198 CHECK(det.has_A2);
199 CHECK(det.has_B2);
200 CHECK(det.has_C2);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100201}
202
203
Iain Merrick75681382010-08-19 15:07:18 +0100204TEST(HeapSnapshotObjectSizes) {
Iain Merrick75681382010-08-19 15:07:18 +0100205 LocalContext env;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000206 v8::HandleScope scope(env->GetIsolate());
207 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
Iain Merrick75681382010-08-19 15:07:18 +0100208
209 // -a-> X1 --a
210 // x -b-> X2 <-|
Ben Murdochf87a2032010-10-22 12:50:53 +0100211 CompileRun(
Iain Merrick75681382010-08-19 15:07:18 +0100212 "function X(a, b) { this.a = a; this.b = b; }\n"
213 "x = new X(new X(), new X());\n"
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000214 "dummy = new X();\n"
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800215 "(function() { x.a.a = x.b; })();");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000216 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000217 CHECK(ValidateSnapshot(snapshot));
Iain Merrick75681382010-08-19 15:07:18 +0100218 const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
219 const v8::HeapGraphNode* x =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000220 GetProperty(global, v8::HeapGraphEdge::kProperty, "x");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000221 CHECK(x);
Iain Merrick75681382010-08-19 15:07:18 +0100222 const v8::HeapGraphNode* x1 =
223 GetProperty(x, v8::HeapGraphEdge::kProperty, "a");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000224 CHECK(x1);
Iain Merrick75681382010-08-19 15:07:18 +0100225 const v8::HeapGraphNode* x2 =
226 GetProperty(x, v8::HeapGraphEdge::kProperty, "b");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000227 CHECK(x2);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800228
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100229 // Test sizes.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000230 CHECK_NE(0, static_cast<int>(x->GetShallowSize()));
231 CHECK_NE(0, static_cast<int>(x1->GetShallowSize()));
232 CHECK_NE(0, static_cast<int>(x2->GetShallowSize()));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100233}
234
235
236TEST(BoundFunctionInSnapshot) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100237 LocalContext env;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000238 v8::HandleScope scope(env->GetIsolate());
239 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100240 CompileRun(
241 "function myFunction(a, b) { this.a = a; this.b = b; }\n"
242 "function AAAAA() {}\n"
243 "boundFunction = myFunction.bind(new AAAAA(), 20, new Number(12)); \n");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000244 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000245 CHECK(ValidateSnapshot(snapshot));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100246 const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
247 const v8::HeapGraphNode* f =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000248 GetProperty(global, v8::HeapGraphEdge::kProperty, "boundFunction");
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100249 CHECK(f);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000250 CHECK(v8_str("native_bind")->Equals(env.local(), f->GetName()).FromJust());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100251 const v8::HeapGraphNode* bindings =
252 GetProperty(f, v8::HeapGraphEdge::kInternal, "bindings");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000253 CHECK(bindings);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100254 CHECK_EQ(v8::HeapGraphNode::kArray, bindings->GetType());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000255 CHECK_EQ(1, bindings->GetChildrenCount());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100256
257 const v8::HeapGraphNode* bound_this = GetProperty(
258 f, v8::HeapGraphEdge::kShortcut, "bound_this");
259 CHECK(bound_this);
260 CHECK_EQ(v8::HeapGraphNode::kObject, bound_this->GetType());
261
262 const v8::HeapGraphNode* bound_function = GetProperty(
263 f, v8::HeapGraphEdge::kShortcut, "bound_function");
264 CHECK(bound_function);
265 CHECK_EQ(v8::HeapGraphNode::kClosure, bound_function->GetType());
266
267 const v8::HeapGraphNode* bound_argument = GetProperty(
268 f, v8::HeapGraphEdge::kShortcut, "bound_argument_1");
269 CHECK(bound_argument);
270 CHECK_EQ(v8::HeapGraphNode::kObject, bound_argument->GetType());
Iain Merrick75681382010-08-19 15:07:18 +0100271}
272
273
274TEST(HeapSnapshotEntryChildren) {
Iain Merrick75681382010-08-19 15:07:18 +0100275 LocalContext env;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000276 v8::HandleScope scope(env->GetIsolate());
277 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
Iain Merrick75681382010-08-19 15:07:18 +0100278
Ben Murdochf87a2032010-10-22 12:50:53 +0100279 CompileRun(
Iain Merrick75681382010-08-19 15:07:18 +0100280 "function A() { }\n"
281 "a = new A;");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000282 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000283 CHECK(ValidateSnapshot(snapshot));
Iain Merrick75681382010-08-19 15:07:18 +0100284 const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
285 for (int i = 0, count = global->GetChildrenCount(); i < count; ++i) {
286 const v8::HeapGraphEdge* prop = global->GetChild(i);
287 CHECK_EQ(global, prop->GetFromNode());
288 }
289 const v8::HeapGraphNode* a =
290 GetProperty(global, v8::HeapGraphEdge::kProperty, "a");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000291 CHECK(a);
Iain Merrick75681382010-08-19 15:07:18 +0100292 for (int i = 0, count = a->GetChildrenCount(); i < count; ++i) {
293 const v8::HeapGraphEdge* prop = a->GetChild(i);
294 CHECK_EQ(a, prop->GetFromNode());
295 }
296}
297
298
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100299TEST(HeapSnapshotCodeObjects) {
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100300 LocalContext env;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000301 v8::HandleScope scope(env->GetIsolate());
302 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100303
Ben Murdochf87a2032010-10-22 12:50:53 +0100304 CompileRun(
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100305 "function lazy(x) { return x - 1; }\n"
306 "function compiled(x) { return x + 1; }\n"
Steve Block791712a2010-08-27 10:21:07 +0100307 "var anonymous = (function() { return function() { return 0; } })();\n"
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100308 "compiled(1)");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000309 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000310 CHECK(ValidateSnapshot(snapshot));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100311
312 const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
313 const v8::HeapGraphNode* compiled =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000314 GetProperty(global, v8::HeapGraphEdge::kProperty, "compiled");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000315 CHECK(compiled);
Iain Merrick75681382010-08-19 15:07:18 +0100316 CHECK_EQ(v8::HeapGraphNode::kClosure, compiled->GetType());
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100317 const v8::HeapGraphNode* lazy =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000318 GetProperty(global, v8::HeapGraphEdge::kProperty, "lazy");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000319 CHECK(lazy);
Iain Merrick75681382010-08-19 15:07:18 +0100320 CHECK_EQ(v8::HeapGraphNode::kClosure, lazy->GetType());
Steve Block791712a2010-08-27 10:21:07 +0100321 const v8::HeapGraphNode* anonymous =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000322 GetProperty(global, v8::HeapGraphEdge::kProperty, "anonymous");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000323 CHECK(anonymous);
Steve Block791712a2010-08-27 10:21:07 +0100324 CHECK_EQ(v8::HeapGraphNode::kClosure, anonymous->GetType());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000325 v8::String::Utf8Value anonymous_name(anonymous->GetName());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000326 CHECK_EQ(0, strcmp("", *anonymous_name));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100327
328 // Find references to code.
329 const v8::HeapGraphNode* compiled_code =
Ben Murdoch8b112d22011-06-08 16:22:53 +0100330 GetProperty(compiled, v8::HeapGraphEdge::kInternal, "shared");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000331 CHECK(compiled_code);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100332 const v8::HeapGraphNode* lazy_code =
Ben Murdoch8b112d22011-06-08 16:22:53 +0100333 GetProperty(lazy, v8::HeapGraphEdge::kInternal, "shared");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000334 CHECK(lazy_code);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100335
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000336 // Check that there's no strong next_code_link. There might be a weak one
337 // but might be not, so we can't check that fact.
338 const v8::HeapGraphNode* code =
339 GetProperty(compiled_code, v8::HeapGraphEdge::kInternal, "code");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000340 CHECK(code);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000341 const v8::HeapGraphNode* next_code_link =
342 GetProperty(code, v8::HeapGraphEdge::kInternal, "code");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000343 CHECK(!next_code_link);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000344
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100345 // Verify that non-compiled code doesn't contain references to "x"
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100346 // literal, while compiled code does. The scope info is stored in FixedArray
347 // objects attached to the SharedFunctionInfo.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100348 bool compiled_references_x = false, lazy_references_x = false;
349 for (int i = 0, count = compiled_code->GetChildrenCount(); i < count; ++i) {
350 const v8::HeapGraphEdge* prop = compiled_code->GetChild(i);
351 const v8::HeapGraphNode* node = prop->GetToNode();
Iain Merrick75681382010-08-19 15:07:18 +0100352 if (node->GetType() == v8::HeapGraphNode::kArray) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100353 if (HasString(node, "x")) {
354 compiled_references_x = true;
355 break;
356 }
357 }
358 }
359 for (int i = 0, count = lazy_code->GetChildrenCount(); i < count; ++i) {
360 const v8::HeapGraphEdge* prop = lazy_code->GetChild(i);
361 const v8::HeapGraphNode* node = prop->GetToNode();
Iain Merrick75681382010-08-19 15:07:18 +0100362 if (node->GetType() == v8::HeapGraphNode::kArray) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100363 if (HasString(node, "x")) {
364 lazy_references_x = true;
365 break;
366 }
367 }
368 }
369 CHECK(compiled_references_x);
Ben Murdochda12d292016-06-02 14:46:10 +0100370 if (i::FLAG_lazy && !(i::FLAG_ignition && i::FLAG_ignition_eager)) {
371 CHECK(!lazy_references_x);
372 }
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100373}
374
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100375
Ben Murdochf87a2032010-10-22 12:50:53 +0100376TEST(HeapSnapshotHeapNumbers) {
Ben Murdochf87a2032010-10-22 12:50:53 +0100377 LocalContext env;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000378 v8::HandleScope scope(env->GetIsolate());
379 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
Ben Murdochf87a2032010-10-22 12:50:53 +0100380 CompileRun(
381 "a = 1; // a is Smi\n"
382 "b = 2.5; // b is HeapNumber");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000383 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000384 CHECK(ValidateSnapshot(snapshot));
Ben Murdochf87a2032010-10-22 12:50:53 +0100385 const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000386 CHECK(!GetProperty(global, v8::HeapGraphEdge::kProperty, "a"));
Ben Murdochf87a2032010-10-22 12:50:53 +0100387 const v8::HeapGraphNode* b =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000388 GetProperty(global, v8::HeapGraphEdge::kProperty, "b");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000389 CHECK(b);
Ben Murdochf87a2032010-10-22 12:50:53 +0100390 CHECK_EQ(v8::HeapGraphNode::kHeapNumber, b->GetType());
391}
392
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000393
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100394TEST(HeapSnapshotSlicedString) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100395 LocalContext env;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000396 v8::HandleScope scope(env->GetIsolate());
397 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100398 CompileRun(
399 "parent_string = \"123456789.123456789.123456789.123456789.123456789."
400 "123456789.123456789.123456789.123456789.123456789."
401 "123456789.123456789.123456789.123456789.123456789."
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000402 "123456789.123456789.123456789.123456789.123456789."
403 "123456789.123456789.123456789.123456789.123456789."
404 "123456789.123456789.123456789.123456789.123456789."
405 "123456789.123456789.123456789.123456789.123456789."
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100406 "123456789.123456789.123456789.123456789.123456789.\";"
407 "child_string = parent_string.slice(100);");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000408 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000409 CHECK(ValidateSnapshot(snapshot));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100410 const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
411 const v8::HeapGraphNode* parent_string =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000412 GetProperty(global, v8::HeapGraphEdge::kProperty, "parent_string");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000413 CHECK(parent_string);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100414 const v8::HeapGraphNode* child_string =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000415 GetProperty(global, v8::HeapGraphEdge::kProperty, "child_string");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000416 CHECK(child_string);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000417 CHECK_EQ(v8::HeapGraphNode::kSlicedString, child_string->GetType());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100418 const v8::HeapGraphNode* parent =
419 GetProperty(child_string, v8::HeapGraphEdge::kInternal, "parent");
420 CHECK_EQ(parent_string, parent);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000421 heap_profiler->DeleteAllHeapSnapshots();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100422}
Ben Murdochf87a2032010-10-22 12:50:53 +0100423
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000424
425TEST(HeapSnapshotConsString) {
426 v8::Isolate* isolate = CcTest::isolate();
427 v8::HandleScope scope(isolate);
428 v8::Local<v8::ObjectTemplate> global_template =
429 v8::ObjectTemplate::New(isolate);
430 global_template->SetInternalFieldCount(1);
431 LocalContext env(NULL, global_template);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000432 v8::Local<v8::Object> global_proxy = env->Global();
433 v8::Local<v8::Object> global = global_proxy->GetPrototype().As<v8::Object>();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000434 CHECK_EQ(1, global->InternalFieldCount());
435
436 i::Factory* factory = CcTest::i_isolate()->factory();
437 i::Handle<i::String> first = factory->NewStringFromStaticChars("0123456789");
438 i::Handle<i::String> second = factory->NewStringFromStaticChars("0123456789");
439 i::Handle<i::String> cons_string =
440 factory->NewConsString(first, second).ToHandleChecked();
441
442 global->SetInternalField(0, v8::ToApiHandle<v8::String>(cons_string));
443
444 v8::HeapProfiler* heap_profiler = isolate->GetHeapProfiler();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000445 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000446 CHECK(ValidateSnapshot(snapshot));
447 const v8::HeapGraphNode* global_node = GetGlobalObject(snapshot);
448
449 const v8::HeapGraphNode* string_node =
450 GetProperty(global_node, v8::HeapGraphEdge::kInternal, "0");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000451 CHECK(string_node);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000452 CHECK_EQ(v8::HeapGraphNode::kConsString, string_node->GetType());
453
454 const v8::HeapGraphNode* first_node =
455 GetProperty(string_node, v8::HeapGraphEdge::kInternal, "first");
456 CHECK_EQ(v8::HeapGraphNode::kString, first_node->GetType());
457
458 const v8::HeapGraphNode* second_node =
459 GetProperty(string_node, v8::HeapGraphEdge::kInternal, "second");
460 CHECK_EQ(v8::HeapGraphNode::kString, second_node->GetType());
461
462 heap_profiler->DeleteAllHeapSnapshots();
463}
464
465
466TEST(HeapSnapshotSymbol) {
467 LocalContext env;
468 v8::HandleScope scope(env->GetIsolate());
469 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
470
471 CompileRun("a = Symbol('mySymbol');\n");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000472 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000473 CHECK(ValidateSnapshot(snapshot));
474 const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
475 const v8::HeapGraphNode* a =
476 GetProperty(global, v8::HeapGraphEdge::kProperty, "a");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000477 CHECK(a);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000478 CHECK_EQ(a->GetType(), v8::HeapGraphNode::kSymbol);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000479 CHECK(v8_str("symbol")->Equals(env.local(), a->GetName()).FromJust());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000480 const v8::HeapGraphNode* name =
481 GetProperty(a, v8::HeapGraphEdge::kInternal, "name");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000482 CHECK(name);
483 CHECK(v8_str("mySymbol")->Equals(env.local(), name->GetName()).FromJust());
484}
485
486
487void CheckSimdSnapshot(const char* program, const char* var_name) {
488 i::FLAG_harmony_simd = true;
489 LocalContext env;
490 v8::HandleScope scope(env->GetIsolate());
491 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
492
493 CompileRun(program);
494 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot();
495 CHECK(ValidateSnapshot(snapshot));
496 const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
497 const v8::HeapGraphNode* var =
498 GetProperty(global, v8::HeapGraphEdge::kProperty, var_name);
499 CHECK(var);
500 CHECK_EQ(var->GetType(), v8::HeapGraphNode::kSimdValue);
501}
502
503
504TEST(HeapSnapshotSimd) {
505 CheckSimdSnapshot("a = SIMD.Float32x4();\n", "a");
506 CheckSimdSnapshot("a = SIMD.Int32x4();\n", "a");
507 CheckSimdSnapshot("a = SIMD.Uint32x4();\n", "a");
508 CheckSimdSnapshot("a = SIMD.Bool32x4();\n", "a");
509 CheckSimdSnapshot("a = SIMD.Int16x8();\n", "a");
510 CheckSimdSnapshot("a = SIMD.Uint16x8();\n", "a");
511 CheckSimdSnapshot("a = SIMD.Bool16x8();\n", "a");
512 CheckSimdSnapshot("a = SIMD.Int8x16();\n", "a");
513 CheckSimdSnapshot("a = SIMD.Uint8x16();\n", "a");
514 CheckSimdSnapshot("a = SIMD.Bool8x16();\n", "a");
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000515}
516
517
518TEST(HeapSnapshotWeakCollection) {
519 LocalContext env;
520 v8::HandleScope scope(env->GetIsolate());
521 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
522
523 CompileRun(
524 "k = {}; v = {}; s = 'str';\n"
525 "ws = new WeakSet(); ws.add(k); ws.add(v); ws[s] = s;\n"
526 "wm = new WeakMap(); wm.set(k, v); wm[s] = s;\n");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000527 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000528 CHECK(ValidateSnapshot(snapshot));
529 const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
530 const v8::HeapGraphNode* k =
531 GetProperty(global, v8::HeapGraphEdge::kProperty, "k");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000532 CHECK(k);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000533 const v8::HeapGraphNode* v =
534 GetProperty(global, v8::HeapGraphEdge::kProperty, "v");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000535 CHECK(v);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000536 const v8::HeapGraphNode* s =
537 GetProperty(global, v8::HeapGraphEdge::kProperty, "s");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000538 CHECK(s);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000539
540 const v8::HeapGraphNode* ws =
541 GetProperty(global, v8::HeapGraphEdge::kProperty, "ws");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000542 CHECK(ws);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000543 CHECK_EQ(v8::HeapGraphNode::kObject, ws->GetType());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000544 CHECK(v8_str("WeakSet")->Equals(env.local(), ws->GetName()).FromJust());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000545
546 const v8::HeapGraphNode* ws_table =
547 GetProperty(ws, v8::HeapGraphEdge::kInternal, "table");
548 CHECK_EQ(v8::HeapGraphNode::kArray, ws_table->GetType());
549 CHECK_GT(ws_table->GetChildrenCount(), 0);
550 int weak_entries = 0;
551 for (int i = 0, count = ws_table->GetChildrenCount(); i < count; ++i) {
552 const v8::HeapGraphEdge* prop = ws_table->GetChild(i);
553 if (prop->GetType() != v8::HeapGraphEdge::kWeak) continue;
554 if (k->GetId() == prop->GetToNode()->GetId()) {
555 ++weak_entries;
556 }
557 }
558 CHECK_EQ(1, weak_entries);
559 const v8::HeapGraphNode* ws_s =
560 GetProperty(ws, v8::HeapGraphEdge::kProperty, "str");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000561 CHECK(ws_s);
562 CHECK_EQ(s->GetId(), ws_s->GetId());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000563
564 const v8::HeapGraphNode* wm =
565 GetProperty(global, v8::HeapGraphEdge::kProperty, "wm");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000566 CHECK(wm);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000567 CHECK_EQ(v8::HeapGraphNode::kObject, wm->GetType());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000568 CHECK(v8_str("WeakMap")->Equals(env.local(), wm->GetName()).FromJust());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000569
570 const v8::HeapGraphNode* wm_table =
571 GetProperty(wm, v8::HeapGraphEdge::kInternal, "table");
572 CHECK_EQ(v8::HeapGraphNode::kArray, wm_table->GetType());
573 CHECK_GT(wm_table->GetChildrenCount(), 0);
574 weak_entries = 0;
575 for (int i = 0, count = wm_table->GetChildrenCount(); i < count; ++i) {
576 const v8::HeapGraphEdge* prop = wm_table->GetChild(i);
577 if (prop->GetType() != v8::HeapGraphEdge::kWeak) continue;
578 const v8::SnapshotObjectId to_node_id = prop->GetToNode()->GetId();
579 if (to_node_id == k->GetId() || to_node_id == v->GetId()) {
580 ++weak_entries;
581 }
582 }
583 CHECK_EQ(2, weak_entries);
584 const v8::HeapGraphNode* wm_s =
585 GetProperty(wm, v8::HeapGraphEdge::kProperty, "str");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000586 CHECK(wm_s);
587 CHECK_EQ(s->GetId(), wm_s->GetId());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000588}
589
590
591TEST(HeapSnapshotCollection) {
592 LocalContext env;
593 v8::HandleScope scope(env->GetIsolate());
594 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
595
596 CompileRun(
597 "k = {}; v = {}; s = 'str';\n"
598 "set = new Set(); set.add(k); set.add(v); set[s] = s;\n"
599 "map = new Map(); map.set(k, v); map[s] = s;\n");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000600 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000601 CHECK(ValidateSnapshot(snapshot));
602 const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
603 const v8::HeapGraphNode* k =
604 GetProperty(global, v8::HeapGraphEdge::kProperty, "k");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000605 CHECK(k);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000606 const v8::HeapGraphNode* v =
607 GetProperty(global, v8::HeapGraphEdge::kProperty, "v");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000608 CHECK(v);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000609 const v8::HeapGraphNode* s =
610 GetProperty(global, v8::HeapGraphEdge::kProperty, "s");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000611 CHECK(s);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000612
613 const v8::HeapGraphNode* set =
614 GetProperty(global, v8::HeapGraphEdge::kProperty, "set");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000615 CHECK(set);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000616 CHECK_EQ(v8::HeapGraphNode::kObject, set->GetType());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000617 CHECK(v8_str("Set")->Equals(env.local(), set->GetName()).FromJust());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000618
619 const v8::HeapGraphNode* set_table =
620 GetProperty(set, v8::HeapGraphEdge::kInternal, "table");
621 CHECK_EQ(v8::HeapGraphNode::kArray, set_table->GetType());
622 CHECK_GT(set_table->GetChildrenCount(), 0);
623 int entries = 0;
624 for (int i = 0, count = set_table->GetChildrenCount(); i < count; ++i) {
625 const v8::HeapGraphEdge* prop = set_table->GetChild(i);
626 const v8::SnapshotObjectId to_node_id = prop->GetToNode()->GetId();
627 if (to_node_id == k->GetId() || to_node_id == v->GetId()) {
628 ++entries;
629 }
630 }
631 CHECK_EQ(2, entries);
632 const v8::HeapGraphNode* set_s =
633 GetProperty(set, v8::HeapGraphEdge::kProperty, "str");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000634 CHECK(set_s);
635 CHECK_EQ(s->GetId(), set_s->GetId());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000636
637 const v8::HeapGraphNode* map =
638 GetProperty(global, v8::HeapGraphEdge::kProperty, "map");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000639 CHECK(map);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000640 CHECK_EQ(v8::HeapGraphNode::kObject, map->GetType());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000641 CHECK(v8_str("Map")->Equals(env.local(), map->GetName()).FromJust());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000642
643 const v8::HeapGraphNode* map_table =
644 GetProperty(map, v8::HeapGraphEdge::kInternal, "table");
645 CHECK_EQ(v8::HeapGraphNode::kArray, map_table->GetType());
646 CHECK_GT(map_table->GetChildrenCount(), 0);
647 entries = 0;
648 for (int i = 0, count = map_table->GetChildrenCount(); i < count; ++i) {
649 const v8::HeapGraphEdge* prop = map_table->GetChild(i);
650 const v8::SnapshotObjectId to_node_id = prop->GetToNode()->GetId();
651 if (to_node_id == k->GetId() || to_node_id == v->GetId()) {
652 ++entries;
653 }
654 }
655 CHECK_EQ(2, entries);
656 const v8::HeapGraphNode* map_s =
657 GetProperty(map, v8::HeapGraphEdge::kProperty, "str");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000658 CHECK(map_s);
659 CHECK_EQ(s->GetId(), map_s->GetId());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000660}
661
662
Ben Murdochf87a2032010-10-22 12:50:53 +0100663TEST(HeapSnapshotInternalReferences) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000664 v8::Isolate* isolate = CcTest::isolate();
665 v8::HandleScope scope(isolate);
666 v8::Local<v8::ObjectTemplate> global_template =
667 v8::ObjectTemplate::New(isolate);
Ben Murdochf87a2032010-10-22 12:50:53 +0100668 global_template->SetInternalFieldCount(2);
669 LocalContext env(NULL, global_template);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000670 v8::Local<v8::Object> global_proxy = env->Global();
671 v8::Local<v8::Object> global = global_proxy->GetPrototype().As<v8::Object>();
Ben Murdochf87a2032010-10-22 12:50:53 +0100672 CHECK_EQ(2, global->InternalFieldCount());
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000673 v8::Local<v8::Object> obj = v8::Object::New(isolate);
Ben Murdochf87a2032010-10-22 12:50:53 +0100674 global->SetInternalField(0, v8_num(17));
675 global->SetInternalField(1, obj);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000676 v8::HeapProfiler* heap_profiler = isolate->GetHeapProfiler();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000677 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000678 CHECK(ValidateSnapshot(snapshot));
Ben Murdochf87a2032010-10-22 12:50:53 +0100679 const v8::HeapGraphNode* global_node = GetGlobalObject(snapshot);
680 // The first reference will not present, because it's a Smi.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000681 CHECK(!GetProperty(global_node, v8::HeapGraphEdge::kInternal, "0"));
Ben Murdochf87a2032010-10-22 12:50:53 +0100682 // The second reference is to an object.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000683 CHECK(GetProperty(global_node, v8::HeapGraphEdge::kInternal, "1"));
Ben Murdochf87a2032010-10-22 12:50:53 +0100684}
685
686
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000687TEST(HeapSnapshotAddressReuse) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100688 LocalContext env;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000689 v8::HandleScope scope(env->GetIsolate());
690 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
691
692 CompileRun(
693 "function A() {}\n"
694 "var a = [];\n"
695 "for (var i = 0; i < 10000; ++i)\n"
696 " a[i] = new A();\n");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000697 const v8::HeapSnapshot* snapshot1 = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000698 CHECK(ValidateSnapshot(snapshot1));
699 v8::SnapshotObjectId maxId1 = snapshot1->GetMaxSnapshotJSObjectId();
700
701 CompileRun(
702 "for (var i = 0; i < 10000; ++i)\n"
703 " a[i] = new A();\n");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000704 CcTest::heap()->CollectAllGarbage();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000705
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000706 const v8::HeapSnapshot* snapshot2 = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000707 CHECK(ValidateSnapshot(snapshot2));
708 const v8::HeapGraphNode* global2 = GetGlobalObject(snapshot2);
709
710 const v8::HeapGraphNode* array_node =
711 GetProperty(global2, v8::HeapGraphEdge::kProperty, "a");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000712 CHECK(array_node);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000713 int wrong_count = 0;
714 for (int i = 0, count = array_node->GetChildrenCount(); i < count; ++i) {
715 const v8::HeapGraphEdge* prop = array_node->GetChild(i);
716 if (prop->GetType() != v8::HeapGraphEdge::kElement)
717 continue;
718 v8::SnapshotObjectId id = prop->GetToNode()->GetId();
719 if (id < maxId1)
720 ++wrong_count;
721 }
722 CHECK_EQ(0, wrong_count);
723}
724
725
726TEST(HeapEntryIdsAndArrayShift) {
727 LocalContext env;
728 v8::HandleScope scope(env->GetIsolate());
729 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100730
731 CompileRun(
732 "function AnObject() {\n"
733 " this.first = 'first';\n"
734 " this.second = 'second';\n"
735 "}\n"
736 "var a = new Array();\n"
737 "for (var i = 0; i < 10; ++i)\n"
738 " a.push(new AnObject());\n");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000739 const v8::HeapSnapshot* snapshot1 = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000740 CHECK(ValidateSnapshot(snapshot1));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100741
742 CompileRun(
743 "for (var i = 0; i < 1; ++i)\n"
744 " a.shift();\n");
745
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000746 CcTest::heap()->CollectAllGarbage();
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100747
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000748 const v8::HeapSnapshot* snapshot2 = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000749 CHECK(ValidateSnapshot(snapshot2));
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100750
751 const v8::HeapGraphNode* global1 = GetGlobalObject(snapshot1);
752 const v8::HeapGraphNode* global2 = GetGlobalObject(snapshot2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000753 CHECK_NE(0u, global1->GetId());
754 CHECK_EQ(global1->GetId(), global2->GetId());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100755
756 const v8::HeapGraphNode* a1 =
757 GetProperty(global1, v8::HeapGraphEdge::kProperty, "a");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000758 CHECK(a1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100759 const v8::HeapGraphNode* k1 =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000760 GetProperty(a1, v8::HeapGraphEdge::kInternal, "elements");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000761 CHECK(k1);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100762 const v8::HeapGraphNode* a2 =
763 GetProperty(global2, v8::HeapGraphEdge::kProperty, "a");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000764 CHECK(a2);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100765 const v8::HeapGraphNode* k2 =
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000766 GetProperty(a2, v8::HeapGraphEdge::kInternal, "elements");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000767 CHECK(k2);
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100768
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000769 CHECK_EQ(a1->GetId(), a2->GetId());
770 CHECK_EQ(k1->GetId(), k2->GetId());
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100771}
772
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000773
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100774TEST(HeapEntryIdsAndGC) {
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100775 LocalContext env;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000776 v8::HandleScope scope(env->GetIsolate());
777 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100778
Ben Murdochf87a2032010-10-22 12:50:53 +0100779 CompileRun(
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100780 "function A() {}\n"
781 "function B(x) { this.x = x; }\n"
782 "var a = new A();\n"
783 "var b = new B(a);");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000784 const v8::HeapSnapshot* snapshot1 = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000785 CHECK(ValidateSnapshot(snapshot1));
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100786
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000787 CcTest::heap()->CollectAllGarbage();
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100788
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000789 const v8::HeapSnapshot* snapshot2 = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000790 CHECK(ValidateSnapshot(snapshot2));
791
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000792 CHECK_GT(snapshot1->GetMaxSnapshotJSObjectId(), 7000u);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000793 CHECK(snapshot1->GetMaxSnapshotJSObjectId() <=
794 snapshot2->GetMaxSnapshotJSObjectId());
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100795
796 const v8::HeapGraphNode* global1 = GetGlobalObject(snapshot1);
797 const v8::HeapGraphNode* global2 = GetGlobalObject(snapshot2);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000798 CHECK_NE(0u, global1->GetId());
799 CHECK_EQ(global1->GetId(), global2->GetId());
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100800 const v8::HeapGraphNode* A1 =
Iain Merrick75681382010-08-19 15:07:18 +0100801 GetProperty(global1, v8::HeapGraphEdge::kProperty, "A");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000802 CHECK(A1);
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100803 const v8::HeapGraphNode* A2 =
Iain Merrick75681382010-08-19 15:07:18 +0100804 GetProperty(global2, v8::HeapGraphEdge::kProperty, "A");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000805 CHECK(A2);
806 CHECK_NE(0u, A1->GetId());
807 CHECK_EQ(A1->GetId(), A2->GetId());
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100808 const v8::HeapGraphNode* B1 =
Iain Merrick75681382010-08-19 15:07:18 +0100809 GetProperty(global1, v8::HeapGraphEdge::kProperty, "B");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000810 CHECK(B1);
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100811 const v8::HeapGraphNode* B2 =
Iain Merrick75681382010-08-19 15:07:18 +0100812 GetProperty(global2, v8::HeapGraphEdge::kProperty, "B");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000813 CHECK(B2);
814 CHECK_NE(0u, B1->GetId());
815 CHECK_EQ(B1->GetId(), B2->GetId());
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100816 const v8::HeapGraphNode* a1 =
Iain Merrick75681382010-08-19 15:07:18 +0100817 GetProperty(global1, v8::HeapGraphEdge::kProperty, "a");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000818 CHECK(a1);
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100819 const v8::HeapGraphNode* a2 =
Iain Merrick75681382010-08-19 15:07:18 +0100820 GetProperty(global2, v8::HeapGraphEdge::kProperty, "a");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000821 CHECK(a2);
822 CHECK_NE(0u, a1->GetId());
823 CHECK_EQ(a1->GetId(), a2->GetId());
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100824 const v8::HeapGraphNode* b1 =
Iain Merrick75681382010-08-19 15:07:18 +0100825 GetProperty(global1, v8::HeapGraphEdge::kProperty, "b");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000826 CHECK(b1);
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100827 const v8::HeapGraphNode* b2 =
Iain Merrick75681382010-08-19 15:07:18 +0100828 GetProperty(global2, v8::HeapGraphEdge::kProperty, "b");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000829 CHECK(b2);
830 CHECK_NE(0u, b1->GetId());
831 CHECK_EQ(b1->GetId(), b2->GetId());
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100832}
833
834
Ben Murdochf87a2032010-10-22 12:50:53 +0100835TEST(HeapSnapshotRootPreservedAfterSorting) {
Ben Murdochf87a2032010-10-22 12:50:53 +0100836 LocalContext env;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000837 v8::HandleScope scope(env->GetIsolate());
838 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000839 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000840 CHECK(ValidateSnapshot(snapshot));
Ben Murdochf87a2032010-10-22 12:50:53 +0100841 const v8::HeapGraphNode* root1 = snapshot->GetRoot();
842 const_cast<i::HeapSnapshot*>(reinterpret_cast<const i::HeapSnapshot*>(
843 snapshot))->GetSortedEntriesList();
844 const v8::HeapGraphNode* root2 = snapshot->GetRoot();
845 CHECK_EQ(root1, root2);
846}
847
848
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100849namespace {
850
851class TestJSONStream : public v8::OutputStream {
852 public:
853 TestJSONStream() : eos_signaled_(0), abort_countdown_(-1) {}
854 explicit TestJSONStream(int abort_countdown)
855 : eos_signaled_(0), abort_countdown_(abort_countdown) {}
856 virtual ~TestJSONStream() {}
857 virtual void EndOfStream() { ++eos_signaled_; }
858 virtual WriteResult WriteAsciiChunk(char* buffer, int chars_written) {
859 if (abort_countdown_ > 0) --abort_countdown_;
860 if (abort_countdown_ == 0) return kAbort;
861 CHECK_GT(chars_written, 0);
862 i::Vector<char> chunk = buffer_.AddBlock(chars_written, '\0');
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000863 i::MemCopy(chunk.start(), buffer, chars_written);
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100864 return kContinue;
865 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000866 virtual WriteResult WriteUint32Chunk(uint32_t* buffer, int chars_written) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000867 CHECK(false);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000868 return kAbort;
869 }
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100870 void WriteTo(i::Vector<char> dest) { buffer_.WriteTo(dest); }
871 int eos_signaled() { return eos_signaled_; }
872 int size() { return buffer_.size(); }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000873
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100874 private:
875 i::Collector<char> buffer_;
876 int eos_signaled_;
877 int abort_countdown_;
878};
879
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000880class OneByteResource : public v8::String::ExternalOneByteStringResource {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100881 public:
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000882 explicit OneByteResource(i::Vector<char> string) : data_(string.start()) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100883 length_ = string.length();
884 }
885 virtual const char* data() const { return data_; }
886 virtual size_t length() const { return length_; }
887 private:
888 const char* data_;
889 size_t length_;
890};
891
892} // namespace
893
894TEST(HeapSnapshotJSONSerialization) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400895 v8::Isolate* isolate = CcTest::isolate();
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100896 LocalContext env;
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400897 v8::HandleScope scope(isolate);
898 v8::HeapProfiler* heap_profiler = isolate->GetHeapProfiler();
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100899
900#define STRING_LITERAL_FOR_TEST \
901 "\"String \\n\\r\\u0008\\u0081\\u0101\\u0801\\u8001\""
Ben Murdochf87a2032010-10-22 12:50:53 +0100902 CompileRun(
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100903 "function A(s) { this.s = s; }\n"
904 "function B(x) { this.x = x; }\n"
905 "var a = new A(" STRING_LITERAL_FOR_TEST ");\n"
906 "var b = new B(a);");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000907 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000908 CHECK(ValidateSnapshot(snapshot));
909
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100910 TestJSONStream stream;
911 snapshot->Serialize(&stream, v8::HeapSnapshot::kJSON);
912 CHECK_GT(stream.size(), 0);
913 CHECK_EQ(1, stream.eos_signaled());
914 i::ScopedVector<char> json(stream.size());
915 stream.WriteTo(json);
916
917 // Verify that snapshot string is valid JSON.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000918 OneByteResource* json_res = new OneByteResource(json);
919 v8::Local<v8::String> json_string =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000920 v8::String::NewExternalOneByte(env->GetIsolate(), json_res)
921 .ToLocalChecked();
922 env->Global()
923 ->Set(env.local(), v8_str("json_snapshot"), json_string)
924 .FromJust();
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100925 v8::Local<v8::Value> snapshot_parse_result = CompileRun(
926 "var parsed = JSON.parse(json_snapshot); true;");
927 CHECK(!snapshot_parse_result.IsEmpty());
928
929 // Verify that snapshot object has required fields.
930 v8::Local<v8::Object> parsed_snapshot =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000931 env->Global()
932 ->Get(env.local(), v8_str("parsed"))
933 .ToLocalChecked()
934 ->ToObject(env.local())
935 .ToLocalChecked();
936 CHECK(parsed_snapshot->Has(env.local(), v8_str("snapshot")).FromJust());
937 CHECK(parsed_snapshot->Has(env.local(), v8_str("nodes")).FromJust());
938 CHECK(parsed_snapshot->Has(env.local(), v8_str("edges")).FromJust());
939 CHECK(parsed_snapshot->Has(env.local(), v8_str("strings")).FromJust());
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100940
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100941 // Get node and edge "member" offsets.
942 v8::Local<v8::Value> meta_analysis_result = CompileRun(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000943 "var meta = parsed.snapshot.meta;\n"
944 "var edge_count_offset = meta.node_fields.indexOf('edge_count');\n"
945 "var node_fields_count = meta.node_fields.length;\n"
946 "var edge_fields_count = meta.edge_fields.length;\n"
947 "var edge_type_offset = meta.edge_fields.indexOf('type');\n"
948 "var edge_name_offset = meta.edge_fields.indexOf('name_or_index');\n"
949 "var edge_to_node_offset = meta.edge_fields.indexOf('to_node');\n"
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100950 "var property_type ="
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000951 " meta.edge_types[edge_type_offset].indexOf('property');\n"
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800952 "var shortcut_type ="
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000953 " meta.edge_types[edge_type_offset].indexOf('shortcut');\n"
954 "var node_count = parsed.nodes.length / node_fields_count;\n"
955 "var first_edge_indexes = parsed.first_edge_indexes = [];\n"
956 "for (var i = 0, first_edge_index = 0; i < node_count; ++i) {\n"
957 " first_edge_indexes[i] = first_edge_index;\n"
958 " first_edge_index += edge_fields_count *\n"
959 " parsed.nodes[i * node_fields_count + edge_count_offset];\n"
960 "}\n"
961 "first_edge_indexes[node_count] = first_edge_index;\n");
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100962 CHECK(!meta_analysis_result.IsEmpty());
963
964 // A helper function for processing encoded nodes.
965 CompileRun(
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800966 "function GetChildPosByProperty(pos, prop_name, prop_type) {\n"
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100967 " var nodes = parsed.nodes;\n"
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000968 " var edges = parsed.edges;\n"
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100969 " var strings = parsed.strings;\n"
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000970 " var node_ordinal = pos / node_fields_count;\n"
971 " for (var i = parsed.first_edge_indexes[node_ordinal],\n"
972 " count = parsed.first_edge_indexes[node_ordinal + 1];\n"
973 " i < count; i += edge_fields_count) {\n"
974 " if (edges[i + edge_type_offset] === prop_type\n"
975 " && strings[edges[i + edge_name_offset]] === prop_name)\n"
976 " return edges[i + edge_to_node_offset];\n"
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100977 " }\n"
978 " return null;\n"
979 "}\n");
980 // Get the string index using the path: <root> -> <global>.b.x.s
981 v8::Local<v8::Value> string_obj_pos_val = CompileRun(
982 "GetChildPosByProperty(\n"
983 " GetChildPosByProperty(\n"
984 " GetChildPosByProperty("
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000985 " parsed.edges[edge_fields_count + edge_to_node_offset],"
986 " \"b\", property_type),\n"
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800987 " \"x\", property_type),"
988 " \"s\", property_type)");
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100989 CHECK(!string_obj_pos_val.IsEmpty());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000990 int string_obj_pos = static_cast<int>(
991 string_obj_pos_val->ToNumber(env.local()).ToLocalChecked()->Value());
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100992 v8::Local<v8::Object> nodes_array =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000993 parsed_snapshot->Get(env.local(), v8_str("nodes"))
994 .ToLocalChecked()
995 ->ToObject(env.local())
996 .ToLocalChecked();
997 int string_index =
998 static_cast<int>(nodes_array->Get(env.local(), string_obj_pos + 1)
999 .ToLocalChecked()
1000 ->ToNumber(env.local())
1001 .ToLocalChecked()
1002 ->Value());
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001003 CHECK_GT(string_index, 0);
1004 v8::Local<v8::Object> strings_array =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001005 parsed_snapshot->Get(env.local(), v8_str("strings"))
1006 .ToLocalChecked()
1007 ->ToObject(env.local())
1008 .ToLocalChecked();
1009 v8::Local<v8::String> string = strings_array->Get(env.local(), string_index)
1010 .ToLocalChecked()
1011 ->ToString(env.local())
1012 .ToLocalChecked();
1013 v8::Local<v8::String> ref_string = CompileRun(STRING_LITERAL_FOR_TEST)
1014 ->ToString(env.local())
1015 .ToLocalChecked();
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001016#undef STRING_LITERAL_FOR_TEST
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001017 CHECK_EQ(0, strcmp(*v8::String::Utf8Value(ref_string),
1018 *v8::String::Utf8Value(string)));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001019}
1020
1021
1022TEST(HeapSnapshotJSONSerializationAborting) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001023 LocalContext env;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001024 v8::HandleScope scope(env->GetIsolate());
1025 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001026 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001027 CHECK(ValidateSnapshot(snapshot));
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001028 TestJSONStream stream(5);
1029 snapshot->Serialize(&stream, v8::HeapSnapshot::kJSON);
1030 CHECK_GT(stream.size(), 0);
1031 CHECK_EQ(0, stream.eos_signaled());
1032}
1033
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001034namespace {
1035
1036class TestStatsStream : public v8::OutputStream {
1037 public:
1038 TestStatsStream()
1039 : eos_signaled_(0),
1040 updates_written_(0),
1041 entries_count_(0),
1042 entries_size_(0),
1043 intervals_count_(0),
1044 first_interval_index_(-1) { }
1045 TestStatsStream(const TestStatsStream& stream)
1046 : v8::OutputStream(stream),
1047 eos_signaled_(stream.eos_signaled_),
1048 updates_written_(stream.updates_written_),
1049 entries_count_(stream.entries_count_),
1050 entries_size_(stream.entries_size_),
1051 intervals_count_(stream.intervals_count_),
1052 first_interval_index_(stream.first_interval_index_) { }
1053 virtual ~TestStatsStream() {}
1054 virtual void EndOfStream() { ++eos_signaled_; }
1055 virtual WriteResult WriteAsciiChunk(char* buffer, int chars_written) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001056 CHECK(false);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001057 return kAbort;
1058 }
1059 virtual WriteResult WriteHeapStatsChunk(v8::HeapStatsUpdate* buffer,
1060 int updates_written) {
1061 ++intervals_count_;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001062 CHECK(updates_written);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001063 updates_written_ += updates_written;
1064 entries_count_ = 0;
1065 if (first_interval_index_ == -1 && updates_written != 0)
1066 first_interval_index_ = buffer[0].index;
1067 for (int i = 0; i < updates_written; ++i) {
1068 entries_count_ += buffer[i].count;
1069 entries_size_ += buffer[i].size;
1070 }
1071
1072 return kContinue;
1073 }
1074 int eos_signaled() { return eos_signaled_; }
1075 int updates_written() { return updates_written_; }
1076 uint32_t entries_count() const { return entries_count_; }
1077 uint32_t entries_size() const { return entries_size_; }
1078 int intervals_count() const { return intervals_count_; }
1079 int first_interval_index() const { return first_interval_index_; }
1080
1081 private:
1082 int eos_signaled_;
1083 int updates_written_;
1084 uint32_t entries_count_;
1085 uint32_t entries_size_;
1086 int intervals_count_;
1087 int first_interval_index_;
1088};
1089
1090} // namespace
1091
1092static TestStatsStream GetHeapStatsUpdate(
1093 v8::HeapProfiler* heap_profiler,
1094 v8::SnapshotObjectId* object_id = NULL) {
1095 TestStatsStream stream;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001096 int64_t timestamp = -1;
1097 v8::SnapshotObjectId last_seen_id =
1098 heap_profiler->GetHeapStats(&stream, &timestamp);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001099 if (object_id)
1100 *object_id = last_seen_id;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001101 CHECK_NE(-1, timestamp);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001102 CHECK_EQ(1, stream.eos_signaled());
1103 return stream;
1104}
1105
1106
1107TEST(HeapSnapshotObjectsStats) {
1108 LocalContext env;
1109 v8::HandleScope scope(env->GetIsolate());
1110 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
1111
1112 heap_profiler->StartTrackingHeapObjects();
1113 // We have to call GC 6 times. In other case the garbage will be
1114 // the reason of flakiness.
1115 for (int i = 0; i < 6; ++i) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001116 CcTest::heap()->CollectAllGarbage();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001117 }
1118
1119 v8::SnapshotObjectId initial_id;
1120 {
1121 // Single chunk of data expected in update. Initial data.
1122 TestStatsStream stats_update = GetHeapStatsUpdate(heap_profiler,
1123 &initial_id);
1124 CHECK_EQ(1, stats_update.intervals_count());
1125 CHECK_EQ(1, stats_update.updates_written());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001126 CHECK_LT(0u, stats_update.entries_size());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001127 CHECK_EQ(0, stats_update.first_interval_index());
1128 }
1129
1130 // No data expected in update because nothing has happened.
1131 v8::SnapshotObjectId same_id;
1132 CHECK_EQ(0, GetHeapStatsUpdate(heap_profiler, &same_id).updates_written());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001133 CHECK_EQ(initial_id, same_id);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001134
1135 {
1136 v8::SnapshotObjectId additional_string_id;
1137 v8::HandleScope inner_scope_1(env->GetIsolate());
1138 v8_str("string1");
1139 {
1140 // Single chunk of data with one new entry expected in update.
1141 TestStatsStream stats_update = GetHeapStatsUpdate(heap_profiler,
1142 &additional_string_id);
1143 CHECK_LT(same_id, additional_string_id);
1144 CHECK_EQ(1, stats_update.intervals_count());
1145 CHECK_EQ(1, stats_update.updates_written());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001146 CHECK_LT(0u, stats_update.entries_size());
1147 CHECK_EQ(1u, stats_update.entries_count());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001148 CHECK_EQ(2, stats_update.first_interval_index());
1149 }
1150
1151 // No data expected in update because nothing happened.
1152 v8::SnapshotObjectId last_id;
1153 CHECK_EQ(0, GetHeapStatsUpdate(heap_profiler, &last_id).updates_written());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001154 CHECK_EQ(additional_string_id, last_id);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001155
1156 {
1157 v8::HandleScope inner_scope_2(env->GetIsolate());
1158 v8_str("string2");
1159
1160 uint32_t entries_size;
1161 {
1162 v8::HandleScope inner_scope_3(env->GetIsolate());
1163 v8_str("string3");
1164 v8_str("string4");
1165
1166 {
1167 // Single chunk of data with three new entries expected in update.
1168 TestStatsStream stats_update = GetHeapStatsUpdate(heap_profiler);
1169 CHECK_EQ(1, stats_update.intervals_count());
1170 CHECK_EQ(1, stats_update.updates_written());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001171 CHECK_LT(0u, entries_size = stats_update.entries_size());
1172 CHECK_EQ(3u, stats_update.entries_count());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001173 CHECK_EQ(4, stats_update.first_interval_index());
1174 }
1175 }
1176
1177 {
1178 // Single chunk of data with two left entries expected in update.
1179 TestStatsStream stats_update = GetHeapStatsUpdate(heap_profiler);
1180 CHECK_EQ(1, stats_update.intervals_count());
1181 CHECK_EQ(1, stats_update.updates_written());
1182 CHECK_GT(entries_size, stats_update.entries_size());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001183 CHECK_EQ(1u, stats_update.entries_count());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001184 // Two strings from forth interval were released.
1185 CHECK_EQ(4, stats_update.first_interval_index());
1186 }
1187 }
1188
1189 {
1190 // Single chunk of data with 0 left entries expected in update.
1191 TestStatsStream stats_update = GetHeapStatsUpdate(heap_profiler);
1192 CHECK_EQ(1, stats_update.intervals_count());
1193 CHECK_EQ(1, stats_update.updates_written());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001194 CHECK_EQ(0u, stats_update.entries_size());
1195 CHECK_EQ(0u, stats_update.entries_count());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001196 // The last string from forth interval was released.
1197 CHECK_EQ(4, stats_update.first_interval_index());
1198 }
1199 }
1200 {
1201 // Single chunk of data with 0 left entries expected in update.
1202 TestStatsStream stats_update = GetHeapStatsUpdate(heap_profiler);
1203 CHECK_EQ(1, stats_update.intervals_count());
1204 CHECK_EQ(1, stats_update.updates_written());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001205 CHECK_EQ(0u, stats_update.entries_size());
1206 CHECK_EQ(0u, stats_update.entries_count());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001207 // The only string from the second interval was released.
1208 CHECK_EQ(2, stats_update.first_interval_index());
1209 }
1210
1211 v8::Local<v8::Array> array = v8::Array::New(env->GetIsolate());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001212 CHECK_EQ(0u, array->Length());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001213 // Force array's buffer allocation.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001214 array->Set(env.local(), 2, v8_num(7)).FromJust();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001215
1216 uint32_t entries_size;
1217 {
1218 // Single chunk of data with 2 entries expected in update.
1219 TestStatsStream stats_update = GetHeapStatsUpdate(heap_profiler);
1220 CHECK_EQ(1, stats_update.intervals_count());
1221 CHECK_EQ(1, stats_update.updates_written());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001222 CHECK_LT(0u, entries_size = stats_update.entries_size());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001223 // They are the array and its buffer.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001224 CHECK_EQ(2u, stats_update.entries_count());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001225 CHECK_EQ(8, stats_update.first_interval_index());
1226 }
1227
1228 for (int i = 0; i < 100; ++i)
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001229 array->Set(env.local(), i, v8_num(i)).FromJust();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001230
1231 {
1232 // Single chunk of data with 1 entry expected in update.
1233 TestStatsStream stats_update = GetHeapStatsUpdate(heap_profiler);
1234 CHECK_EQ(1, stats_update.intervals_count());
1235 // The first interval was changed because old buffer was collected.
1236 // The second interval was changed because new buffer was allocated.
1237 CHECK_EQ(2, stats_update.updates_written());
1238 CHECK_LT(entries_size, stats_update.entries_size());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001239 CHECK_EQ(2u, stats_update.entries_count());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001240 CHECK_EQ(8, stats_update.first_interval_index());
1241 }
1242
1243 heap_profiler->StopTrackingHeapObjects();
1244}
1245
1246
1247TEST(HeapObjectIds) {
1248 LocalContext env;
1249 v8::Isolate* isolate = env->GetIsolate();
1250 v8::HandleScope scope(isolate);
1251 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
1252
1253 const int kLength = 10;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001254 v8::Local<v8::Object> objects[kLength];
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001255 v8::SnapshotObjectId ids[kLength];
1256
1257 heap_profiler->StartTrackingHeapObjects(false);
1258
1259 for (int i = 0; i < kLength; i++) {
1260 objects[i] = v8::Object::New(isolate);
1261 }
1262 GetHeapStatsUpdate(heap_profiler);
1263
1264 for (int i = 0; i < kLength; i++) {
1265 v8::SnapshotObjectId id = heap_profiler->GetObjectId(objects[i]);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001266 CHECK_NE(v8::HeapProfiler::kUnknownObjectId, id);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001267 ids[i] = id;
1268 }
1269
1270 heap_profiler->StopTrackingHeapObjects();
1271 CcTest::heap()->CollectAllAvailableGarbage();
1272
1273 for (int i = 0; i < kLength; i++) {
1274 v8::SnapshotObjectId id = heap_profiler->GetObjectId(objects[i]);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001275 CHECK_EQ(ids[i], id);
1276 v8::Local<v8::Value> obj = heap_profiler->FindObjectById(ids[i]);
1277 CHECK(objects[i]->Equals(env.local(), obj).FromJust());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001278 }
1279
1280 heap_profiler->ClearObjectIds();
1281 for (int i = 0; i < kLength; i++) {
1282 v8::SnapshotObjectId id = heap_profiler->GetObjectId(objects[i]);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001283 CHECK_EQ(v8::HeapProfiler::kUnknownObjectId, id);
1284 v8::Local<v8::Value> obj = heap_profiler->FindObjectById(ids[i]);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001285 CHECK(obj.IsEmpty());
1286 }
1287}
1288
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -08001289
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001290static void CheckChildrenIds(const v8::HeapSnapshot* snapshot,
1291 const v8::HeapGraphNode* node,
1292 int level, int max_level) {
1293 if (level > max_level) return;
1294 CHECK_EQ(node, snapshot->GetNodeById(node->GetId()));
1295 for (int i = 0, count = node->GetChildrenCount(); i < count; ++i) {
1296 const v8::HeapGraphEdge* prop = node->GetChild(i);
1297 const v8::HeapGraphNode* child =
1298 snapshot->GetNodeById(prop->GetToNode()->GetId());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001299 CHECK_EQ(prop->GetToNode()->GetId(), child->GetId());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001300 CHECK_EQ(prop->GetToNode(), child);
1301 CheckChildrenIds(snapshot, child, level + 1, max_level);
1302 }
1303}
1304
1305
Ben Murdochb0fe1622011-05-05 13:52:32 +01001306TEST(HeapSnapshotGetNodeById) {
Ben Murdochb0fe1622011-05-05 13:52:32 +01001307 LocalContext env;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001308 v8::HandleScope scope(env->GetIsolate());
1309 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001310
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001311 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001312 CHECK(ValidateSnapshot(snapshot));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001313 const v8::HeapGraphNode* root = snapshot->GetRoot();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001314 CheckChildrenIds(snapshot, root, 0, 3);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001315 // Check a big id, which should not exist yet.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001316 CHECK(!snapshot->GetNodeById(0x1000000UL));
Ben Murdochb0fe1622011-05-05 13:52:32 +01001317}
1318
1319
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001320TEST(HeapSnapshotGetSnapshotObjectId) {
1321 LocalContext env;
1322 v8::HandleScope scope(env->GetIsolate());
1323 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
1324 CompileRun("globalObject = {};\n");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001325 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001326 CHECK(ValidateSnapshot(snapshot));
1327 const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
1328 const v8::HeapGraphNode* global_object =
1329 GetProperty(global, v8::HeapGraphEdge::kProperty, "globalObject");
1330 CHECK(global_object);
1331
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001332 v8::Local<v8::Value> globalObjectHandle =
1333 env->Global()->Get(env.local(), v8_str("globalObject")).ToLocalChecked();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001334 CHECK(!globalObjectHandle.IsEmpty());
1335 CHECK(globalObjectHandle->IsObject());
1336
1337 v8::SnapshotObjectId id = heap_profiler->GetObjectId(globalObjectHandle);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001338 CHECK_NE(v8::HeapProfiler::kUnknownObjectId, id);
1339 CHECK_EQ(id, global_object->GetId());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001340}
1341
1342
1343TEST(HeapSnapshotUnknownSnapshotObjectId) {
1344 LocalContext env;
1345 v8::HandleScope scope(env->GetIsolate());
1346 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
1347 CompileRun("globalObject = {};\n");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001348 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001349 CHECK(ValidateSnapshot(snapshot));
1350 const v8::HeapGraphNode* node =
1351 snapshot->GetNodeById(v8::HeapProfiler::kUnknownObjectId);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001352 CHECK(!node);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001353}
1354
1355
Ben Murdochb0fe1622011-05-05 13:52:32 +01001356namespace {
1357
1358class TestActivityControl : public v8::ActivityControl {
1359 public:
1360 explicit TestActivityControl(int abort_count)
1361 : done_(0), total_(0), abort_count_(abort_count) {}
1362 ControlOption ReportProgressValue(int done, int total) {
1363 done_ = done;
1364 total_ = total;
1365 return --abort_count_ != 0 ? kContinue : kAbort;
1366 }
1367 int done() { return done_; }
1368 int total() { return total_; }
1369
1370 private:
1371 int done_;
1372 int total_;
1373 int abort_count_;
1374};
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001375
1376} // namespace
Ben Murdochb0fe1622011-05-05 13:52:32 +01001377
Ben Murdochb0fe1622011-05-05 13:52:32 +01001378
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001379TEST(TakeHeapSnapshotAborting) {
1380 LocalContext env;
1381 v8::HandleScope scope(env->GetIsolate());
1382
1383 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
1384 const int snapshots_count = heap_profiler->GetSnapshotCount();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001385 TestActivityControl aborting_control(1);
Ben Murdochb0fe1622011-05-05 13:52:32 +01001386 const v8::HeapSnapshot* no_snapshot =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001387 heap_profiler->TakeHeapSnapshot(&aborting_control);
1388 CHECK(!no_snapshot);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001389 CHECK_EQ(snapshots_count, heap_profiler->GetSnapshotCount());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001390 CHECK_GT(aborting_control.total(), aborting_control.done());
1391
1392 TestActivityControl control(-1); // Don't abort.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001393 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot(&control);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001394 CHECK(ValidateSnapshot(snapshot));
1395
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001396 CHECK(snapshot);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001397 CHECK_EQ(snapshots_count + 1, heap_profiler->GetSnapshotCount());
Ben Murdochb0fe1622011-05-05 13:52:32 +01001398 CHECK_EQ(control.total(), control.done());
1399 CHECK_GT(control.total(), 0);
1400}
1401
Steve Block44f0eee2011-05-26 01:26:41 +01001402
1403namespace {
1404
1405class TestRetainedObjectInfo : public v8::RetainedObjectInfo {
1406 public:
1407 TestRetainedObjectInfo(int hash,
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001408 const char* group_label,
Steve Block44f0eee2011-05-26 01:26:41 +01001409 const char* label,
1410 intptr_t element_count = -1,
1411 intptr_t size = -1)
1412 : disposed_(false),
1413 hash_(hash),
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001414 group_label_(group_label),
Steve Block44f0eee2011-05-26 01:26:41 +01001415 label_(label),
1416 element_count_(element_count),
1417 size_(size) {
1418 instances.Add(this);
1419 }
1420 virtual ~TestRetainedObjectInfo() {}
1421 virtual void Dispose() {
1422 CHECK(!disposed_);
1423 disposed_ = true;
1424 }
1425 virtual bool IsEquivalent(RetainedObjectInfo* other) {
1426 return GetHash() == other->GetHash();
1427 }
1428 virtual intptr_t GetHash() { return hash_; }
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001429 virtual const char* GetGroupLabel() { return group_label_; }
Steve Block44f0eee2011-05-26 01:26:41 +01001430 virtual const char* GetLabel() { return label_; }
1431 virtual intptr_t GetElementCount() { return element_count_; }
1432 virtual intptr_t GetSizeInBytes() { return size_; }
1433 bool disposed() { return disposed_; }
1434
1435 static v8::RetainedObjectInfo* WrapperInfoCallback(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001436 uint16_t class_id, v8::Local<v8::Value> wrapper) {
Steve Block44f0eee2011-05-26 01:26:41 +01001437 if (class_id == 1) {
1438 if (wrapper->IsString()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001439 v8::String::Utf8Value utf8(wrapper);
1440 if (strcmp(*utf8, "AAA") == 0)
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001441 return new TestRetainedObjectInfo(1, "aaa-group", "aaa", 100);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001442 else if (strcmp(*utf8, "BBB") == 0)
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001443 return new TestRetainedObjectInfo(1, "aaa-group", "aaa", 100);
Steve Block44f0eee2011-05-26 01:26:41 +01001444 }
1445 } else if (class_id == 2) {
1446 if (wrapper->IsString()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001447 v8::String::Utf8Value utf8(wrapper);
1448 if (strcmp(*utf8, "CCC") == 0)
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001449 return new TestRetainedObjectInfo(2, "ccc-group", "ccc");
Steve Block44f0eee2011-05-26 01:26:41 +01001450 }
1451 }
1452 CHECK(false);
1453 return NULL;
1454 }
1455
1456 static i::List<TestRetainedObjectInfo*> instances;
1457
1458 private:
1459 bool disposed_;
Steve Block44f0eee2011-05-26 01:26:41 +01001460 int hash_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001461 const char* group_label_;
Steve Block44f0eee2011-05-26 01:26:41 +01001462 const char* label_;
1463 intptr_t element_count_;
1464 intptr_t size_;
1465};
1466
1467
1468i::List<TestRetainedObjectInfo*> TestRetainedObjectInfo::instances;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001469
1470} // namespace
Steve Block44f0eee2011-05-26 01:26:41 +01001471
1472
1473static const v8::HeapGraphNode* GetNode(const v8::HeapGraphNode* parent,
1474 v8::HeapGraphNode::Type type,
1475 const char* name) {
1476 for (int i = 0, count = parent->GetChildrenCount(); i < count; ++i) {
1477 const v8::HeapGraphNode* node = parent->GetChild(i)->GetToNode();
1478 if (node->GetType() == type && strcmp(name,
1479 const_cast<i::HeapEntry*>(
1480 reinterpret_cast<const i::HeapEntry*>(node))->name()) == 0) {
1481 return node;
1482 }
1483 }
1484 return NULL;
1485}
1486
1487
1488TEST(HeapSnapshotRetainedObjectInfo) {
Steve Block44f0eee2011-05-26 01:26:41 +01001489 LocalContext env;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001490 v8::Isolate* isolate = env->GetIsolate();
1491 v8::HandleScope scope(isolate);
1492 v8::HeapProfiler* heap_profiler = isolate->GetHeapProfiler();
Steve Block44f0eee2011-05-26 01:26:41 +01001493
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001494 heap_profiler->SetWrapperClassInfoProvider(
Steve Block44f0eee2011-05-26 01:26:41 +01001495 1, TestRetainedObjectInfo::WrapperInfoCallback);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001496 heap_profiler->SetWrapperClassInfoProvider(
Steve Block44f0eee2011-05-26 01:26:41 +01001497 2, TestRetainedObjectInfo::WrapperInfoCallback);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001498 v8::Persistent<v8::String> p_AAA(isolate, v8_str("AAA"));
Steve Block44f0eee2011-05-26 01:26:41 +01001499 p_AAA.SetWrapperClassId(1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001500 v8::Persistent<v8::String> p_BBB(isolate, v8_str("BBB"));
Steve Block44f0eee2011-05-26 01:26:41 +01001501 p_BBB.SetWrapperClassId(1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001502 v8::Persistent<v8::String> p_CCC(isolate, v8_str("CCC"));
Steve Block44f0eee2011-05-26 01:26:41 +01001503 p_CCC.SetWrapperClassId(2);
1504 CHECK_EQ(0, TestRetainedObjectInfo::instances.length());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001505 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001506 CHECK(ValidateSnapshot(snapshot));
Steve Block44f0eee2011-05-26 01:26:41 +01001507
1508 CHECK_EQ(3, TestRetainedObjectInfo::instances.length());
1509 for (int i = 0; i < TestRetainedObjectInfo::instances.length(); ++i) {
1510 CHECK(TestRetainedObjectInfo::instances[i]->disposed());
1511 delete TestRetainedObjectInfo::instances[i];
1512 }
1513
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001514 const v8::HeapGraphNode* native_group_aaa = GetNode(
1515 snapshot->GetRoot(), v8::HeapGraphNode::kSynthetic, "aaa-group");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001516 CHECK(native_group_aaa);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001517 CHECK_EQ(1, native_group_aaa->GetChildrenCount());
Steve Block44f0eee2011-05-26 01:26:41 +01001518 const v8::HeapGraphNode* aaa = GetNode(
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001519 native_group_aaa, v8::HeapGraphNode::kNative, "aaa / 100 entries");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001520 CHECK(aaa);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001521 CHECK_EQ(2, aaa->GetChildrenCount());
1522
1523 const v8::HeapGraphNode* native_group_ccc = GetNode(
1524 snapshot->GetRoot(), v8::HeapGraphNode::kSynthetic, "ccc-group");
Steve Block44f0eee2011-05-26 01:26:41 +01001525 const v8::HeapGraphNode* ccc = GetNode(
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001526 native_group_ccc, v8::HeapGraphNode::kNative, "ccc");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001527 CHECK(ccc);
Steve Block44f0eee2011-05-26 01:26:41 +01001528
Steve Block44f0eee2011-05-26 01:26:41 +01001529 const v8::HeapGraphNode* n_AAA = GetNode(
1530 aaa, v8::HeapGraphNode::kString, "AAA");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001531 CHECK(n_AAA);
Steve Block44f0eee2011-05-26 01:26:41 +01001532 const v8::HeapGraphNode* n_BBB = GetNode(
1533 aaa, v8::HeapGraphNode::kString, "BBB");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001534 CHECK(n_BBB);
Steve Block44f0eee2011-05-26 01:26:41 +01001535 CHECK_EQ(1, ccc->GetChildrenCount());
1536 const v8::HeapGraphNode* n_CCC = GetNode(
1537 ccc, v8::HeapGraphNode::kString, "CCC");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001538 CHECK(n_CCC);
Steve Block44f0eee2011-05-26 01:26:41 +01001539
Ben Murdoch8b112d22011-06-08 16:22:53 +01001540 CHECK_EQ(aaa, GetProperty(n_AAA, v8::HeapGraphEdge::kInternal, "native"));
1541 CHECK_EQ(aaa, GetProperty(n_BBB, v8::HeapGraphEdge::kInternal, "native"));
1542 CHECK_EQ(ccc, GetProperty(n_CCC, v8::HeapGraphEdge::kInternal, "native"));
Steve Block44f0eee2011-05-26 01:26:41 +01001543}
1544
1545
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001546class GraphWithImplicitRefs {
1547 public:
1548 static const int kObjectsCount = 4;
1549 explicit GraphWithImplicitRefs(LocalContext* env) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001550 CHECK(!instance_);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001551 instance_ = this;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001552 isolate_ = (*env)->GetIsolate();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001553 for (int i = 0; i < kObjectsCount; i++) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001554 objects_[i].Reset(isolate_, v8::Object::New(isolate_));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001555 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001556 (*env)
1557 ->Global()
1558 ->Set(isolate_->GetCurrentContext(), v8_str("root_object"),
1559 v8::Local<v8::Value>::New(isolate_, objects_[0]))
1560 .FromJust();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001561 }
1562 ~GraphWithImplicitRefs() {
1563 instance_ = NULL;
1564 }
1565
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001566 static void gcPrologue(v8::Isolate* isolate, v8::GCType type,
1567 v8::GCCallbackFlags flags) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001568 instance_->AddImplicitReferences();
1569 }
1570
1571 private:
1572 void AddImplicitReferences() {
1573 // 0 -> 1
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001574 isolate_->SetObjectGroupId(objects_[0],
1575 v8::UniqueId(1));
1576 isolate_->SetReferenceFromGroup(
1577 v8::UniqueId(1), objects_[1]);
1578 // Adding two more references: 1 -> 2, 1 -> 3
1579 isolate_->SetReference(objects_[1].As<v8::Object>(),
1580 objects_[2]);
1581 isolate_->SetReference(objects_[1].As<v8::Object>(),
1582 objects_[3]);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001583 }
1584
1585 v8::Persistent<v8::Value> objects_[kObjectsCount];
1586 static GraphWithImplicitRefs* instance_;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001587 v8::Isolate* isolate_;
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001588};
1589
1590GraphWithImplicitRefs* GraphWithImplicitRefs::instance_ = NULL;
1591
1592
1593TEST(HeapSnapshotImplicitReferences) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001594 LocalContext env;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001595 v8::HandleScope scope(env->GetIsolate());
1596 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001597
1598 GraphWithImplicitRefs graph(&env);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001599 env->GetIsolate()->AddGCPrologueCallback(&GraphWithImplicitRefs::gcPrologue);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001600
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001601 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001602 CHECK(ValidateSnapshot(snapshot));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001603
1604 const v8::HeapGraphNode* global_object = GetGlobalObject(snapshot);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001605 const v8::HeapGraphNode* obj0 = GetProperty(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001606 global_object, v8::HeapGraphEdge::kProperty, "root_object");
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001607 CHECK(obj0);
1608 CHECK_EQ(v8::HeapGraphNode::kObject, obj0->GetType());
1609 const v8::HeapGraphNode* obj1 = GetProperty(
1610 obj0, v8::HeapGraphEdge::kInternal, "native");
1611 CHECK(obj1);
1612 int implicit_targets_count = 0;
1613 for (int i = 0, count = obj1->GetChildrenCount(); i < count; ++i) {
1614 const v8::HeapGraphEdge* prop = obj1->GetChild(i);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001615 v8::String::Utf8Value prop_name(prop->GetName());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001616 if (prop->GetType() == v8::HeapGraphEdge::kInternal &&
1617 strcmp("native", *prop_name) == 0) {
1618 ++implicit_targets_count;
1619 }
1620 }
1621 CHECK_EQ(2, implicit_targets_count);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001622 env->GetIsolate()->RemoveGCPrologueCallback(
1623 &GraphWithImplicitRefs::gcPrologue);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001624}
1625
1626
Steve Block44f0eee2011-05-26 01:26:41 +01001627TEST(DeleteAllHeapSnapshots) {
Steve Block44f0eee2011-05-26 01:26:41 +01001628 LocalContext env;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001629 v8::HandleScope scope(env->GetIsolate());
1630 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
Steve Block44f0eee2011-05-26 01:26:41 +01001631
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001632 CHECK_EQ(0, heap_profiler->GetSnapshotCount());
1633 heap_profiler->DeleteAllHeapSnapshots();
1634 CHECK_EQ(0, heap_profiler->GetSnapshotCount());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001635 CHECK(heap_profiler->TakeHeapSnapshot());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001636 CHECK_EQ(1, heap_profiler->GetSnapshotCount());
1637 heap_profiler->DeleteAllHeapSnapshots();
1638 CHECK_EQ(0, heap_profiler->GetSnapshotCount());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001639 CHECK(heap_profiler->TakeHeapSnapshot());
1640 CHECK(heap_profiler->TakeHeapSnapshot());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001641 CHECK_EQ(2, heap_profiler->GetSnapshotCount());
1642 heap_profiler->DeleteAllHeapSnapshots();
1643 CHECK_EQ(0, heap_profiler->GetSnapshotCount());
1644}
1645
1646
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001647static bool FindHeapSnapshot(v8::HeapProfiler* profiler,
1648 const v8::HeapSnapshot* snapshot) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001649 int length = profiler->GetSnapshotCount();
1650 for (int i = 0; i < length; i++) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001651 if (snapshot == profiler->GetHeapSnapshot(i)) return true;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001652 }
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001653 return false;
Steve Block44f0eee2011-05-26 01:26:41 +01001654}
1655
1656
1657TEST(DeleteHeapSnapshot) {
Steve Block44f0eee2011-05-26 01:26:41 +01001658 LocalContext env;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001659 v8::HandleScope scope(env->GetIsolate());
1660 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
Steve Block44f0eee2011-05-26 01:26:41 +01001661
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001662 CHECK_EQ(0, heap_profiler->GetSnapshotCount());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001663 const v8::HeapSnapshot* s1 = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001664
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001665 CHECK(s1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001666 CHECK_EQ(1, heap_profiler->GetSnapshotCount());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001667 CHECK(FindHeapSnapshot(heap_profiler, s1));
Steve Block44f0eee2011-05-26 01:26:41 +01001668 const_cast<v8::HeapSnapshot*>(s1)->Delete();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001669 CHECK_EQ(0, heap_profiler->GetSnapshotCount());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001670 CHECK(!FindHeapSnapshot(heap_profiler, s1));
Steve Block44f0eee2011-05-26 01:26:41 +01001671
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001672 const v8::HeapSnapshot* s2 = heap_profiler->TakeHeapSnapshot();
1673 CHECK(s2);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001674 CHECK_EQ(1, heap_profiler->GetSnapshotCount());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001675 CHECK(FindHeapSnapshot(heap_profiler, s2));
1676 const v8::HeapSnapshot* s3 = heap_profiler->TakeHeapSnapshot();
1677 CHECK(s3);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001678 CHECK_EQ(2, heap_profiler->GetSnapshotCount());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001679 CHECK_NE(s2, s3);
1680 CHECK(FindHeapSnapshot(heap_profiler, s3));
Steve Block44f0eee2011-05-26 01:26:41 +01001681 const_cast<v8::HeapSnapshot*>(s2)->Delete();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001682 CHECK_EQ(1, heap_profiler->GetSnapshotCount());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001683 CHECK(!FindHeapSnapshot(heap_profiler, s2));
1684 CHECK(FindHeapSnapshot(heap_profiler, s3));
Steve Block44f0eee2011-05-26 01:26:41 +01001685 const_cast<v8::HeapSnapshot*>(s3)->Delete();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001686 CHECK_EQ(0, heap_profiler->GetSnapshotCount());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001687 CHECK(!FindHeapSnapshot(heap_profiler, s3));
Steve Block44f0eee2011-05-26 01:26:41 +01001688}
1689
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001690
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001691class NameResolver : public v8::HeapProfiler::ObjectNameResolver {
1692 public:
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001693 virtual const char* GetName(v8::Local<v8::Object> object) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001694 return "Global object name";
1695 }
1696};
1697
1698
1699TEST(GlobalObjectName) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001700 LocalContext env;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001701 v8::HandleScope scope(env->GetIsolate());
1702 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001703
1704 CompileRun("document = { URL:\"abcdefgh\" };");
1705
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001706 NameResolver name_resolver;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001707 const v8::HeapSnapshot* snapshot =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001708 heap_profiler->TakeHeapSnapshot(NULL, &name_resolver);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001709 CHECK(ValidateSnapshot(snapshot));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001710 const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001711 CHECK(global);
1712 CHECK_EQ(0,
1713 strcmp("Object / Global object name",
1714 const_cast<i::HeapEntry*>(
1715 reinterpret_cast<const i::HeapEntry*>(global))->name()));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001716}
1717
1718
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001719TEST(GlobalObjectFields) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001720 LocalContext env;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001721 v8::HandleScope scope(env->GetIsolate());
1722 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
1723 CompileRun("obj = {};");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001724 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001725 CHECK(ValidateSnapshot(snapshot));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001726 const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001727 const v8::HeapGraphNode* native_context =
1728 GetProperty(global, v8::HeapGraphEdge::kInternal, "native_context");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001729 CHECK(native_context);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001730 const v8::HeapGraphNode* global_proxy =
1731 GetProperty(global, v8::HeapGraphEdge::kInternal, "global_proxy");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001732 CHECK(global_proxy);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001733}
1734
1735
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001736TEST(NoHandleLeaks) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001737 LocalContext env;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001738 v8::HandleScope scope(env->GetIsolate());
1739 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001740
1741 CompileRun("document = { URL:\"abcdefgh\" };");
1742
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001743 i::Isolate* isolate = CcTest::i_isolate();
1744 int count_before = i::HandleScope::NumberOfHandles(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001745 heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001746 int count_after = i::HandleScope::NumberOfHandles(isolate);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001747 CHECK_EQ(count_before, count_after);
1748}
1749
1750
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001751TEST(NodesIteration) {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001752 LocalContext env;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001753 v8::HandleScope scope(env->GetIsolate());
1754 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001755 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001756 CHECK(ValidateSnapshot(snapshot));
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001757 const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001758 CHECK(global);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001759 // Verify that we can find this object by iteration.
1760 const int nodes_count = snapshot->GetNodesCount();
1761 int count = 0;
1762 for (int i = 0; i < nodes_count; ++i) {
1763 if (snapshot->GetNode(i) == global)
1764 ++count;
1765 }
1766 CHECK_EQ(1, count);
1767}
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001768
1769
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001770TEST(GetHeapValueForNode) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001771 LocalContext env;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001772 v8::HandleScope scope(env->GetIsolate());
1773 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001774
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001775 CompileRun("a = { s_prop: \'value\', n_prop: \'value2\' };");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001776 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001777 CHECK(ValidateSnapshot(snapshot));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001778 const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001779 CHECK(heap_profiler->FindObjectById(global->GetId())->IsObject());
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001780 v8::Local<v8::Object> js_global =
1781 env->Global()->GetPrototype().As<v8::Object>();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001782 CHECK(js_global == heap_profiler->FindObjectById(global->GetId()));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001783 const v8::HeapGraphNode* obj = GetProperty(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001784 global, v8::HeapGraphEdge::kProperty, "a");
1785 CHECK(heap_profiler->FindObjectById(obj->GetId())->IsObject());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001786 v8::Local<v8::Object> js_obj = js_global->Get(env.local(), v8_str("a"))
1787 .ToLocalChecked()
1788 .As<v8::Object>();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001789 CHECK(js_obj == heap_profiler->FindObjectById(obj->GetId()));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001790 const v8::HeapGraphNode* s_prop =
1791 GetProperty(obj, v8::HeapGraphEdge::kProperty, "s_prop");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001792 v8::Local<v8::String> js_s_prop = js_obj->Get(env.local(), v8_str("s_prop"))
1793 .ToLocalChecked()
1794 .As<v8::String>();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001795 CHECK(js_s_prop == heap_profiler->FindObjectById(s_prop->GetId()));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001796 const v8::HeapGraphNode* n_prop =
1797 GetProperty(obj, v8::HeapGraphEdge::kProperty, "n_prop");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001798 v8::Local<v8::String> js_n_prop = js_obj->Get(env.local(), v8_str("n_prop"))
1799 .ToLocalChecked()
1800 .As<v8::String>();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001801 CHECK(js_n_prop == heap_profiler->FindObjectById(n_prop->GetId()));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001802}
1803
1804
1805TEST(GetHeapValueForDeletedObject) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001806 LocalContext env;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001807 v8::HandleScope scope(env->GetIsolate());
1808 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001809
1810 // It is impossible to delete a global property, so we are about to delete a
1811 // property of the "a" object. Also, the "p" object can't be an empty one
1812 // because the empty object is static and isn't actually deleted.
1813 CompileRun("a = { p: { r: {} } };");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001814 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001815 CHECK(ValidateSnapshot(snapshot));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001816 const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
1817 const v8::HeapGraphNode* obj = GetProperty(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001818 global, v8::HeapGraphEdge::kProperty, "a");
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001819 const v8::HeapGraphNode* prop = GetProperty(
1820 obj, v8::HeapGraphEdge::kProperty, "p");
1821 {
1822 // Perform the check inside a nested local scope to avoid creating a
1823 // reference to the object we are deleting.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001824 v8::HandleScope scope(env->GetIsolate());
1825 CHECK(heap_profiler->FindObjectById(prop->GetId())->IsObject());
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001826 }
1827 CompileRun("delete a.p;");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001828 CHECK(heap_profiler->FindObjectById(prop->GetId()).IsEmpty());
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001829}
1830
1831
1832static int StringCmp(const char* ref, i::String* act) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001833 v8::base::SmartArrayPointer<char> s_act = act->ToCString();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001834 int result = strcmp(ref, s_act.get());
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001835 if (result != 0)
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001836 fprintf(stderr, "Expected: \"%s\", Actual: \"%s\"\n", ref, s_act.get());
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001837 return result;
1838}
1839
1840
1841TEST(GetConstructorName) {
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001842 LocalContext env;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001843 v8::HandleScope scope(env->GetIsolate());
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001844
1845 CompileRun(
1846 "function Constructor1() {};\n"
1847 "var obj1 = new Constructor1();\n"
1848 "var Constructor2 = function() {};\n"
1849 "var obj2 = new Constructor2();\n"
1850 "var obj3 = {};\n"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001851 "obj3.__proto__ = { constructor: function Constructor3() {} };\n"
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001852 "var obj4 = {};\n"
1853 "// Slow properties\n"
1854 "for (var i=0; i<2000; ++i) obj4[\"p\" + i] = i;\n"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001855 "obj4.__proto__ = { constructor: function Constructor4() {} };\n"
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001856 "var obj5 = {};\n"
1857 "var obj6 = {};\n"
1858 "obj6.constructor = 6;");
1859 v8::Local<v8::Object> js_global =
1860 env->Global()->GetPrototype().As<v8::Object>();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001861 v8::Local<v8::Object> obj1 = js_global->Get(env.local(), v8_str("obj1"))
1862 .ToLocalChecked()
1863 .As<v8::Object>();
1864 i::Handle<i::JSObject> js_obj1 =
1865 i::Handle<i::JSObject>::cast(v8::Utils::OpenHandle(*obj1));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001866 CHECK_EQ(0, StringCmp(
1867 "Constructor1", i::V8HeapExplorer::GetConstructorName(*js_obj1)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001868 v8::Local<v8::Object> obj2 = js_global->Get(env.local(), v8_str("obj2"))
1869 .ToLocalChecked()
1870 .As<v8::Object>();
1871 i::Handle<i::JSObject> js_obj2 =
1872 i::Handle<i::JSObject>::cast(v8::Utils::OpenHandle(*obj2));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001873 CHECK_EQ(0, StringCmp(
1874 "Constructor2", i::V8HeapExplorer::GetConstructorName(*js_obj2)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001875 v8::Local<v8::Object> obj3 = js_global->Get(env.local(), v8_str("obj3"))
1876 .ToLocalChecked()
1877 .As<v8::Object>();
1878 i::Handle<i::JSObject> js_obj3 =
1879 i::Handle<i::JSObject>::cast(v8::Utils::OpenHandle(*obj3));
1880 CHECK_EQ(0, StringCmp("Constructor3",
1881 i::V8HeapExplorer::GetConstructorName(*js_obj3)));
1882 v8::Local<v8::Object> obj4 = js_global->Get(env.local(), v8_str("obj4"))
1883 .ToLocalChecked()
1884 .As<v8::Object>();
1885 i::Handle<i::JSObject> js_obj4 =
1886 i::Handle<i::JSObject>::cast(v8::Utils::OpenHandle(*obj4));
1887 CHECK_EQ(0, StringCmp("Constructor4",
1888 i::V8HeapExplorer::GetConstructorName(*js_obj4)));
1889 v8::Local<v8::Object> obj5 = js_global->Get(env.local(), v8_str("obj5"))
1890 .ToLocalChecked()
1891 .As<v8::Object>();
1892 i::Handle<i::JSObject> js_obj5 =
1893 i::Handle<i::JSObject>::cast(v8::Utils::OpenHandle(*obj5));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001894 CHECK_EQ(0, StringCmp(
1895 "Object", i::V8HeapExplorer::GetConstructorName(*js_obj5)));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001896 v8::Local<v8::Object> obj6 = js_global->Get(env.local(), v8_str("obj6"))
1897 .ToLocalChecked()
1898 .As<v8::Object>();
1899 i::Handle<i::JSObject> js_obj6 =
1900 i::Handle<i::JSObject>::cast(v8::Utils::OpenHandle(*obj6));
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001901 CHECK_EQ(0, StringCmp(
1902 "Object", i::V8HeapExplorer::GetConstructorName(*js_obj6)));
1903}
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001904
1905
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001906TEST(FastCaseAccessors) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001907 LocalContext env;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001908 v8::HandleScope scope(env->GetIsolate());
1909 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001910
1911 CompileRun("var obj1 = {};\n"
1912 "obj1.__defineGetter__('propWithGetter', function Y() {\n"
1913 " return 42;\n"
1914 "});\n"
1915 "obj1.__defineSetter__('propWithSetter', function Z(value) {\n"
1916 " return this.value_ = value;\n"
1917 "});\n");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001918 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001919 CHECK(ValidateSnapshot(snapshot));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001920
1921 const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001922 CHECK(global);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001923 const v8::HeapGraphNode* obj1 =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001924 GetProperty(global, v8::HeapGraphEdge::kProperty, "obj1");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001925 CHECK(obj1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001926 const v8::HeapGraphNode* func;
1927 func = GetProperty(obj1, v8::HeapGraphEdge::kProperty, "get propWithGetter");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001928 CHECK(func);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001929 func = GetProperty(obj1, v8::HeapGraphEdge::kProperty, "set propWithGetter");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001930 CHECK(!func);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001931 func = GetProperty(obj1, v8::HeapGraphEdge::kProperty, "set propWithSetter");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001932 CHECK(func);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001933 func = GetProperty(obj1, v8::HeapGraphEdge::kProperty, "get propWithSetter");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001934 CHECK(!func);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001935}
1936
1937
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001938TEST(FastCaseRedefinedAccessors) {
1939 LocalContext env;
1940 v8::HandleScope scope(env->GetIsolate());
1941 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
1942
1943 CompileRun(
1944 "var obj1 = {};\n"
1945 "Object.defineProperty(obj1, 'prop', { "
1946 " get: function() { return 42; },\n"
1947 " set: function(value) { return this.prop_ = value; },\n"
1948 " configurable: true,\n"
1949 " enumerable: true,\n"
1950 "});\n"
1951 "Object.defineProperty(obj1, 'prop', { "
1952 " get: function() { return 153; },\n"
1953 " set: function(value) { return this.prop_ = value; },\n"
1954 " configurable: true,\n"
1955 " enumerable: true,\n"
1956 "});\n");
1957 v8::Local<v8::Object> js_global =
1958 env->Global()->GetPrototype().As<v8::Object>();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001959 i::Handle<i::JSReceiver> js_obj1 =
1960 v8::Utils::OpenHandle(*js_global->Get(env.local(), v8_str("obj1"))
1961 .ToLocalChecked()
1962 .As<v8::Object>());
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001963 USE(js_obj1);
1964
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001965 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot();
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001966 CHECK(ValidateSnapshot(snapshot));
1967 const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001968 CHECK(global);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001969 const v8::HeapGraphNode* obj1 =
1970 GetProperty(global, v8::HeapGraphEdge::kProperty, "obj1");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001971 CHECK(obj1);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001972 const v8::HeapGraphNode* func;
1973 func = GetProperty(obj1, v8::HeapGraphEdge::kProperty, "get prop");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001974 CHECK(func);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001975 func = GetProperty(obj1, v8::HeapGraphEdge::kProperty, "set prop");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001976 CHECK(func);
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001977}
1978
1979
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001980TEST(SlowCaseAccessors) {
1981 LocalContext env;
1982 v8::HandleScope scope(env->GetIsolate());
1983 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
1984
1985 CompileRun("var obj1 = {};\n"
1986 "for (var i = 0; i < 100; ++i) obj1['z' + i] = {};"
1987 "obj1.__defineGetter__('propWithGetter', function Y() {\n"
1988 " return 42;\n"
1989 "});\n"
1990 "obj1.__defineSetter__('propWithSetter', function Z(value) {\n"
1991 " return this.value_ = value;\n"
1992 "});\n");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001993 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001994 CHECK(ValidateSnapshot(snapshot));
1995
1996 const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00001997 CHECK(global);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00001998 const v8::HeapGraphNode* obj1 =
1999 GetProperty(global, v8::HeapGraphEdge::kProperty, "obj1");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002000 CHECK(obj1);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002001 const v8::HeapGraphNode* func;
2002 func = GetProperty(obj1, v8::HeapGraphEdge::kProperty, "get propWithGetter");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002003 CHECK(func);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002004 func = GetProperty(obj1, v8::HeapGraphEdge::kProperty, "set propWithGetter");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002005 CHECK(!func);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002006 func = GetProperty(obj1, v8::HeapGraphEdge::kProperty, "set propWithSetter");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002007 CHECK(func);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002008 func = GetProperty(obj1, v8::HeapGraphEdge::kProperty, "get propWithSetter");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002009 CHECK(!func);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002010}
2011
2012
2013TEST(HiddenPropertiesFastCase) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002014 v8::Isolate* isolate = CcTest::isolate();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002015 LocalContext env;
Emily Bernierd0a1eb72015-03-24 16:35:39 -04002016 v8::HandleScope scope(isolate);
2017 v8::HeapProfiler* heap_profiler = isolate->GetHeapProfiler();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002018
2019 CompileRun(
2020 "function C(x) { this.a = this; this.b = x; }\n"
2021 "c = new C(2012);\n");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002022 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002023 CHECK(ValidateSnapshot(snapshot));
2024 const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
2025 const v8::HeapGraphNode* c =
2026 GetProperty(global, v8::HeapGraphEdge::kProperty, "c");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002027 CHECK(c);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002028 const v8::HeapGraphNode* hidden_props =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002029 GetProperty(c, v8::HeapGraphEdge::kProperty, "<symbol>");
2030 CHECK(!hidden_props);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002031
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002032 v8::Local<v8::Value> cHandle =
2033 env->Global()->Get(env.local(), v8_str("c")).ToLocalChecked();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002034 CHECK(!cHandle.IsEmpty() && cHandle->IsObject());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002035 cHandle->ToObject(env.local())
2036 .ToLocalChecked()
2037 ->SetPrivate(env.local(),
2038 v8::Private::ForApi(env->GetIsolate(), v8_str("key")),
2039 v8_str("val"))
2040 .FromJust();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002041
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002042 snapshot = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002043 CHECK(ValidateSnapshot(snapshot));
2044 global = GetGlobalObject(snapshot);
2045 c = GetProperty(global, v8::HeapGraphEdge::kProperty, "c");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002046 CHECK(c);
2047 hidden_props = GetProperty(c, v8::HeapGraphEdge::kProperty, "<symbol>");
2048 CHECK(hidden_props);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002049}
2050
2051
2052TEST(AccessorInfo) {
2053 LocalContext env;
2054 v8::HandleScope scope(env->GetIsolate());
2055 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
2056
2057 CompileRun("function foo(x) { }\n");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002058 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002059 CHECK(ValidateSnapshot(snapshot));
2060 const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
2061 const v8::HeapGraphNode* foo =
2062 GetProperty(global, v8::HeapGraphEdge::kProperty, "foo");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002063 CHECK(foo);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002064 const v8::HeapGraphNode* map =
2065 GetProperty(foo, v8::HeapGraphEdge::kInternal, "map");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002066 CHECK(map);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002067 const v8::HeapGraphNode* descriptors =
2068 GetProperty(map, v8::HeapGraphEdge::kInternal, "descriptors");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002069 CHECK(descriptors);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002070 const v8::HeapGraphNode* length_name =
2071 GetProperty(descriptors, v8::HeapGraphEdge::kInternal, "2");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002072 CHECK(length_name);
2073 CHECK_EQ(0, strcmp("length", *v8::String::Utf8Value(length_name->GetName())));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002074 const v8::HeapGraphNode* length_accessor =
2075 GetProperty(descriptors, v8::HeapGraphEdge::kInternal, "4");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002076 CHECK(length_accessor);
Ben Murdoch097c5b22016-05-18 11:27:45 +01002077 CHECK_EQ(0, strcmp("system / AccessorInfo",
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002078 *v8::String::Utf8Value(length_accessor->GetName())));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002079 const v8::HeapGraphNode* name =
2080 GetProperty(length_accessor, v8::HeapGraphEdge::kInternal, "name");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002081 CHECK(name);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002082 const v8::HeapGraphNode* getter =
2083 GetProperty(length_accessor, v8::HeapGraphEdge::kInternal, "getter");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002084 CHECK(getter);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002085 const v8::HeapGraphNode* setter =
2086 GetProperty(length_accessor, v8::HeapGraphEdge::kInternal, "setter");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002087 CHECK(setter);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002088}
2089
2090
2091bool HasWeakEdge(const v8::HeapGraphNode* node) {
2092 for (int i = 0; i < node->GetChildrenCount(); ++i) {
2093 const v8::HeapGraphEdge* handle_edge = node->GetChild(i);
2094 if (handle_edge->GetType() == v8::HeapGraphEdge::kWeak) return true;
2095 }
2096 return false;
2097}
2098
2099
2100bool HasWeakGlobalHandle() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002101 v8::Isolate* isolate = CcTest::isolate();
2102 v8::HeapProfiler* heap_profiler = isolate->GetHeapProfiler();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002103 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002104 CHECK(ValidateSnapshot(snapshot));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002105 const v8::HeapGraphNode* gc_roots = GetNode(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002106 snapshot->GetRoot(), v8::HeapGraphNode::kSynthetic, "(GC roots)");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002107 CHECK(gc_roots);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002108 const v8::HeapGraphNode* global_handles = GetNode(
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002109 gc_roots, v8::HeapGraphNode::kSynthetic, "(Global handles)");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002110 CHECK(global_handles);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002111 return HasWeakEdge(global_handles);
2112}
2113
2114
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002115static void PersistentHandleCallback(
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002116 const v8::WeakCallbackInfo<v8::Persistent<v8::Object> >& data) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002117 data.GetParameter()->Reset();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002118}
2119
2120
2121TEST(WeakGlobalHandle) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002122 LocalContext env;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002123 v8::HandleScope scope(env->GetIsolate());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002124
2125 CHECK(!HasWeakGlobalHandle());
2126
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002127 v8::Persistent<v8::Object> handle(env->GetIsolate(),
2128 v8::Object::New(env->GetIsolate()));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002129 handle.SetWeak(&handle, PersistentHandleCallback,
2130 v8::WeakCallbackType::kParameter);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002131
2132 CHECK(HasWeakGlobalHandle());
2133}
2134
2135
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002136TEST(SfiAndJsFunctionWeakRefs) {
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002137 LocalContext env;
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002138 v8::HandleScope scope(env->GetIsolate());
2139 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002140
2141 CompileRun(
2142 "fun = (function (x) { return function () { return x + 1; } })(1);");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002143 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002144 CHECK(ValidateSnapshot(snapshot));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002145 const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002146 CHECK(global);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002147 const v8::HeapGraphNode* fun =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002148 GetProperty(global, v8::HeapGraphEdge::kProperty, "fun");
2149 CHECK(!HasWeakEdge(fun));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002150 const v8::HeapGraphNode* shared =
2151 GetProperty(fun, v8::HeapGraphEdge::kInternal, "shared");
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002152 CHECK(!HasWeakEdge(shared));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002153}
2154
2155
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002156TEST(NoDebugObjectInSnapshot) {
2157 LocalContext env;
2158 v8::HandleScope scope(env->GetIsolate());
2159 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
2160
2161 CHECK(CcTest::i_isolate()->debug()->Load());
2162 CompileRun("foo = {};");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002163 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002164 CHECK(ValidateSnapshot(snapshot));
2165 const v8::HeapGraphNode* root = snapshot->GetRoot();
2166 int globals_count = 0;
2167 for (int i = 0; i < root->GetChildrenCount(); ++i) {
2168 const v8::HeapGraphEdge* edge = root->GetChild(i);
2169 if (edge->GetType() == v8::HeapGraphEdge::kShortcut) {
2170 ++globals_count;
2171 const v8::HeapGraphNode* global = edge->GetToNode();
2172 const v8::HeapGraphNode* foo =
2173 GetProperty(global, v8::HeapGraphEdge::kProperty, "foo");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002174 CHECK(foo);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002175 }
2176 }
2177 CHECK_EQ(1, globals_count);
2178}
2179
2180
2181TEST(AllStrongGcRootsHaveNames) {
2182 LocalContext env;
2183 v8::HandleScope scope(env->GetIsolate());
2184 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
2185
2186 CompileRun("foo = {};");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002187 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002188 CHECK(ValidateSnapshot(snapshot));
2189 const v8::HeapGraphNode* gc_roots = GetNode(
2190 snapshot->GetRoot(), v8::HeapGraphNode::kSynthetic, "(GC roots)");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002191 CHECK(gc_roots);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002192 const v8::HeapGraphNode* strong_roots = GetNode(
2193 gc_roots, v8::HeapGraphNode::kSynthetic, "(Strong roots)");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002194 CHECK(strong_roots);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002195 for (int i = 0; i < strong_roots->GetChildrenCount(); ++i) {
2196 const v8::HeapGraphEdge* edge = strong_roots->GetChild(i);
2197 CHECK_EQ(v8::HeapGraphEdge::kInternal, edge->GetType());
2198 v8::String::Utf8Value name(edge->GetName());
2199 CHECK(isalpha(**name));
2200 }
2201}
2202
2203
2204TEST(NoRefsToNonEssentialEntries) {
2205 LocalContext env;
2206 v8::HandleScope scope(env->GetIsolate());
2207 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
2208 CompileRun("global_object = {};\n");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002209 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002210 CHECK(ValidateSnapshot(snapshot));
2211 const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
2212 const v8::HeapGraphNode* global_object =
2213 GetProperty(global, v8::HeapGraphEdge::kProperty, "global_object");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002214 CHECK(global_object);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002215 const v8::HeapGraphNode* properties =
2216 GetProperty(global_object, v8::HeapGraphEdge::kInternal, "properties");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002217 CHECK(!properties);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002218 const v8::HeapGraphNode* elements =
2219 GetProperty(global_object, v8::HeapGraphEdge::kInternal, "elements");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002220 CHECK(!elements);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002221}
2222
2223
2224TEST(MapHasDescriptorsAndTransitions) {
2225 LocalContext env;
2226 v8::HandleScope scope(env->GetIsolate());
2227 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
2228 CompileRun("obj = { a: 10 };\n");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002229 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002230 CHECK(ValidateSnapshot(snapshot));
2231 const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
2232 const v8::HeapGraphNode* global_object =
2233 GetProperty(global, v8::HeapGraphEdge::kProperty, "obj");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002234 CHECK(global_object);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002235
2236 const v8::HeapGraphNode* map =
2237 GetProperty(global_object, v8::HeapGraphEdge::kInternal, "map");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002238 CHECK(map);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002239 const v8::HeapGraphNode* own_descriptors = GetProperty(
2240 map, v8::HeapGraphEdge::kInternal, "descriptors");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002241 CHECK(own_descriptors);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002242 const v8::HeapGraphNode* own_transitions = GetProperty(
2243 map, v8::HeapGraphEdge::kInternal, "transitions");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002244 CHECK(!own_transitions);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002245}
2246
2247
2248TEST(ManyLocalsInSharedContext) {
2249 LocalContext env;
2250 v8::HandleScope scope(env->GetIsolate());
2251 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
2252 int num_objects = 6000;
2253 CompileRun(
2254 "var n = 6000;"
2255 "var result = [];"
2256 "result.push('(function outer() {');"
2257 "for (var i = 0; i < n; i++) {"
2258 " var f = 'function f_' + i + '() { ';"
2259 " if (i > 0)"
2260 " f += 'f_' + (i - 1) + '();';"
2261 " f += ' }';"
2262 " result.push(f);"
2263 "}"
2264 "result.push('return f_' + (n - 1) + ';');"
2265 "result.push('})()');"
2266 "var ok = eval(result.join('\\n'));");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002267 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002268 CHECK(ValidateSnapshot(snapshot));
2269
2270 const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002271 CHECK(global);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002272 const v8::HeapGraphNode* ok_object =
2273 GetProperty(global, v8::HeapGraphEdge::kProperty, "ok");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002274 CHECK(ok_object);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002275 const v8::HeapGraphNode* context_object =
2276 GetProperty(ok_object, v8::HeapGraphEdge::kInternal, "context");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002277 CHECK(context_object);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002278 // Check the objects are not duplicated in the context.
2279 CHECK_EQ(v8::internal::Context::MIN_CONTEXT_SLOTS + num_objects - 1,
2280 context_object->GetChildrenCount());
2281 // Check all the objects have got their names.
2282 // ... well check just every 15th because otherwise it's too slow in debug.
2283 for (int i = 0; i < num_objects - 1; i += 15) {
2284 i::EmbeddedVector<char, 100> var_name;
2285 i::SNPrintF(var_name, "f_%d", i);
2286 const v8::HeapGraphNode* f_object = GetProperty(
2287 context_object, v8::HeapGraphEdge::kContextVariable, var_name.start());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002288 CHECK(f_object);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002289 }
2290}
2291
2292
2293TEST(AllocationSitesAreVisible) {
2294 LocalContext env;
2295 v8::Isolate* isolate = env->GetIsolate();
2296 v8::HandleScope scope(isolate);
2297 v8::HeapProfiler* heap_profiler = isolate->GetHeapProfiler();
2298 CompileRun(
2299 "fun = function () { var a = [3, 2, 1]; return a; }\n"
2300 "fun();");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002301 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002302 CHECK(ValidateSnapshot(snapshot));
2303
2304 const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002305 CHECK(global);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002306 const v8::HeapGraphNode* fun_code =
2307 GetProperty(global, v8::HeapGraphEdge::kProperty, "fun");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002308 CHECK(fun_code);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002309 const v8::HeapGraphNode* literals =
2310 GetProperty(fun_code, v8::HeapGraphEdge::kInternal, "literals");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002311 CHECK(literals);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002312 CHECK_EQ(v8::HeapGraphNode::kArray, literals->GetType());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002313 CHECK_EQ(1, literals->GetChildrenCount());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002314
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002315 // The first value in the literals array should be the boilerplate,
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002316 // after an AllocationSite.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002317 const v8::HeapGraphEdge* prop = literals->GetChild(0);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002318 const v8::HeapGraphNode* allocation_site = prop->GetToNode();
2319 v8::String::Utf8Value name(allocation_site->GetName());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002320 CHECK_EQ(0, strcmp("system / AllocationSite", *name));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002321 const v8::HeapGraphNode* transition_info =
2322 GetProperty(allocation_site, v8::HeapGraphEdge::kInternal,
2323 "transition_info");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002324 CHECK(transition_info);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002325
2326 const v8::HeapGraphNode* elements =
2327 GetProperty(transition_info, v8::HeapGraphEdge::kInternal,
2328 "elements");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002329 CHECK(elements);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002330 CHECK_EQ(v8::HeapGraphNode::kArray, elements->GetType());
2331 CHECK_EQ(v8::internal::FixedArray::SizeFor(3),
2332 static_cast<int>(elements->GetShallowSize()));
2333
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002334 v8::Local<v8::Value> array_val =
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002335 heap_profiler->FindObjectById(transition_info->GetId());
2336 CHECK(array_val->IsArray());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002337 v8::Local<v8::Array> array = v8::Local<v8::Array>::Cast(array_val);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002338 // Verify the array is "a" in the code above.
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002339 CHECK_EQ(3u, array->Length());
2340 CHECK(v8::Integer::New(isolate, 3)
2341 ->Equals(env.local(),
2342 array->Get(env.local(), v8::Integer::New(isolate, 0))
2343 .ToLocalChecked())
2344 .FromJust());
2345 CHECK(v8::Integer::New(isolate, 2)
2346 ->Equals(env.local(),
2347 array->Get(env.local(), v8::Integer::New(isolate, 1))
2348 .ToLocalChecked())
2349 .FromJust());
2350 CHECK(v8::Integer::New(isolate, 1)
2351 ->Equals(env.local(),
2352 array->Get(env.local(), v8::Integer::New(isolate, 2))
2353 .ToLocalChecked())
2354 .FromJust());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002355}
2356
2357
2358TEST(JSFunctionHasCodeLink) {
2359 LocalContext env;
2360 v8::HandleScope scope(env->GetIsolate());
2361 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
2362 CompileRun("function foo(x, y) { return x + y; }\n");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002363 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002364 CHECK(ValidateSnapshot(snapshot));
2365 const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
2366 const v8::HeapGraphNode* foo_func =
2367 GetProperty(global, v8::HeapGraphEdge::kProperty, "foo");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002368 CHECK(foo_func);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002369 const v8::HeapGraphNode* code =
2370 GetProperty(foo_func, v8::HeapGraphEdge::kInternal, "code");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002371 CHECK(code);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002372}
2373
2374
2375static const v8::HeapGraphNode* GetNodeByPath(const v8::HeapSnapshot* snapshot,
2376 const char* path[],
2377 int depth) {
2378 const v8::HeapGraphNode* node = snapshot->GetRoot();
2379 for (int current_depth = 0; current_depth < depth; ++current_depth) {
2380 int i, count = node->GetChildrenCount();
2381 for (i = 0; i < count; ++i) {
2382 const v8::HeapGraphEdge* edge = node->GetChild(i);
2383 const v8::HeapGraphNode* to_node = edge->GetToNode();
2384 v8::String::Utf8Value edge_name(edge->GetName());
2385 v8::String::Utf8Value node_name(to_node->GetName());
2386 i::EmbeddedVector<char, 100> name;
2387 i::SNPrintF(name, "%s::%s", *edge_name, *node_name);
2388 if (strstr(name.start(), path[current_depth])) {
2389 node = to_node;
2390 break;
2391 }
2392 }
2393 if (i == count) return NULL;
2394 }
2395 return node;
2396}
2397
2398
2399TEST(CheckCodeNames) {
2400 LocalContext env;
2401 v8::HandleScope scope(env->GetIsolate());
2402 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
2403 CompileRun("var a = 1.1;");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002404 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002405 CHECK(ValidateSnapshot(snapshot));
2406
2407 const char* stub_path[] = {
2408 "::(GC roots)",
2409 "::(Strong roots)",
2410 "code_stubs::",
2411 "::(ArraySingleArgumentConstructorStub code)"
2412 };
2413 const v8::HeapGraphNode* node = GetNodeByPath(snapshot,
2414 stub_path, arraysize(stub_path));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002415 CHECK(node);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002416
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002417 const char* builtin_path1[] = {"::(GC roots)", "::(Builtins)",
2418 "::(KeyedLoadIC_Megamorphic builtin)"};
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002419 node = GetNodeByPath(snapshot, builtin_path1, arraysize(builtin_path1));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002420 CHECK(node);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002421
2422 const char* builtin_path2[] = {"::(GC roots)", "::(Builtins)",
2423 "::(CompileLazy builtin)"};
2424 node = GetNodeByPath(snapshot, builtin_path2, arraysize(builtin_path2));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002425 CHECK(node);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002426 v8::String::Utf8Value node_name(node->GetName());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002427 CHECK_EQ(0, strcmp("(CompileLazy builtin)", *node_name));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002428}
2429
2430
2431static const char* record_trace_tree_source =
2432"var topFunctions = [];\n"
2433"var global = this;\n"
2434"function generateFunctions(width, depth) {\n"
2435" var script = [];\n"
2436" for (var i = 0; i < width; i++) {\n"
2437" for (var j = 0; j < depth; j++) {\n"
2438" script.push('function f_' + i + '_' + j + '(x) {\\n');\n"
2439" script.push(' try {\\n');\n"
2440" if (j < depth-2) {\n"
2441" script.push(' return f_' + i + '_' + (j+1) + '(x+1);\\n');\n"
2442" } else if (j == depth - 2) {\n"
2443" script.push(' return new f_' + i + '_' + (depth - 1) + '();\\n');\n"
2444" } else if (j == depth - 1) {\n"
2445" script.push(' this.ts = Date.now();\\n');\n"
2446" }\n"
2447" script.push(' } catch (e) {}\\n');\n"
2448" script.push('}\\n');\n"
2449" \n"
2450" }\n"
2451" }\n"
2452" var script = script.join('');\n"
2453" // throw script;\n"
2454" global.eval(script);\n"
2455" for (var i = 0; i < width; i++) {\n"
2456" topFunctions.push(this['f_' + i + '_0']);\n"
2457" }\n"
2458"}\n"
2459"\n"
2460"var width = 3;\n"
2461"var depth = 3;\n"
2462"generateFunctions(width, depth);\n"
2463"var instances = [];\n"
2464"function start() {\n"
2465" for (var i = 0; i < width; i++) {\n"
2466" instances.push(topFunctions[i](0));\n"
2467" }\n"
2468"}\n"
2469"\n"
2470"for (var i = 0; i < 100; i++) start();\n";
2471
2472
2473static AllocationTraceNode* FindNode(
2474 AllocationTracker* tracker, const Vector<const char*>& names) {
2475 AllocationTraceNode* node = tracker->trace_tree()->root();
2476 for (int i = 0; node != NULL && i < names.length(); i++) {
2477 const char* name = names[i];
2478 Vector<AllocationTraceNode*> children = node->children();
2479 node = NULL;
2480 for (int j = 0; j < children.length(); j++) {
2481 unsigned index = children[j]->function_info_index();
2482 AllocationTracker::FunctionInfo* info =
2483 tracker->function_info_list()[index];
2484 if (info && strcmp(info->name, name) == 0) {
2485 node = children[j];
2486 break;
2487 }
2488 }
2489 }
2490 return node;
2491}
2492
2493
2494TEST(ArrayGrowLeftTrim) {
2495 LocalContext env;
2496 v8::HandleScope scope(env->GetIsolate());
2497 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
2498 heap_profiler->StartTrackingHeapObjects(true);
2499
2500 CompileRun(
2501 "var a = [];\n"
2502 "for (var i = 0; i < 5; ++i)\n"
2503 " a[i] = i;\n"
2504 "for (var i = 0; i < 3; ++i)\n"
2505 " a.shift();\n");
2506
2507 const char* names[] = {""};
2508 AllocationTracker* tracker =
2509 reinterpret_cast<i::HeapProfiler*>(heap_profiler)->allocation_tracker();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002510 CHECK(tracker);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002511 // Resolve all function locations.
2512 tracker->PrepareForSerialization();
2513 // Print for better diagnostics in case of failure.
2514 tracker->trace_tree()->Print(tracker);
2515
Ben Murdochc5610432016-08-08 18:44:38 +01002516 AllocationTraceNode* node = FindNode(tracker, ArrayVector(names));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002517 CHECK(node);
2518 CHECK_GE(node->allocation_count(), 2u);
2519 CHECK_GE(node->allocation_size(), 4u * 5u);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002520 heap_profiler->StopTrackingHeapObjects();
2521}
2522
2523
2524TEST(TrackHeapAllocations) {
2525 v8::HandleScope scope(v8::Isolate::GetCurrent());
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002526 LocalContext env;
2527
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002528 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
2529 heap_profiler->StartTrackingHeapObjects(true);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002530
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002531 CompileRun(record_trace_tree_source);
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002532
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002533 AllocationTracker* tracker =
2534 reinterpret_cast<i::HeapProfiler*>(heap_profiler)->allocation_tracker();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002535 CHECK(tracker);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002536 // Resolve all function locations.
2537 tracker->PrepareForSerialization();
2538 // Print for better diagnostics in case of failure.
2539 tracker->trace_tree()->Print(tracker);
2540
2541 const char* names[] = {"", "start", "f_0_0", "f_0_1", "f_0_2"};
Ben Murdochc5610432016-08-08 18:44:38 +01002542 AllocationTraceNode* node = FindNode(tracker, ArrayVector(names));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002543 CHECK(node);
2544 CHECK_GE(node->allocation_count(), 100u);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002545 CHECK_GE(node->allocation_size(), 4 * node->allocation_count());
2546 heap_profiler->StopTrackingHeapObjects();
2547}
2548
2549
2550static const char* inline_heap_allocation_source =
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002551 "function f_0(x) {\n"
2552 " return f_1(x+1);\n"
2553 "}\n"
2554 "%NeverOptimizeFunction(f_0);\n"
2555 "function f_1(x) {\n"
2556 " return new f_2(x+1);\n"
2557 "}\n"
2558 "%NeverOptimizeFunction(f_1);\n"
2559 "function f_2(x) {\n"
2560 " this.foo = x;\n"
2561 "}\n"
2562 "var instances = [];\n"
2563 "function start() {\n"
2564 " instances.push(f_0(0));\n"
2565 "}\n"
2566 "\n"
2567 "for (var i = 0; i < 100; i++) start();\n";
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002568
2569
2570TEST(TrackBumpPointerAllocations) {
2571 i::FLAG_allow_natives_syntax = true;
2572 v8::HandleScope scope(v8::Isolate::GetCurrent());
2573 LocalContext env;
2574
2575 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
2576 const char* names[] = {"", "start", "f_0", "f_1"};
2577 // First check that normally all allocations are recorded.
2578 {
2579 heap_profiler->StartTrackingHeapObjects(true);
2580
2581 CompileRun(inline_heap_allocation_source);
2582
2583 AllocationTracker* tracker =
2584 reinterpret_cast<i::HeapProfiler*>(heap_profiler)->allocation_tracker();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002585 CHECK(tracker);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002586 // Resolve all function locations.
2587 tracker->PrepareForSerialization();
2588 // Print for better diagnostics in case of failure.
2589 tracker->trace_tree()->Print(tracker);
2590
Ben Murdochc5610432016-08-08 18:44:38 +01002591 AllocationTraceNode* node = FindNode(tracker, ArrayVector(names));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002592 CHECK(node);
2593 CHECK_GE(node->allocation_count(), 100u);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002594 CHECK_GE(node->allocation_size(), 4 * node->allocation_count());
2595 heap_profiler->StopTrackingHeapObjects();
2596 }
2597
2598 {
2599 heap_profiler->StartTrackingHeapObjects(true);
2600
2601 // Now check that not all allocations are tracked if we manually reenable
2602 // inline allocations.
2603 CHECK(CcTest::heap()->inline_allocation_disabled());
2604 CcTest::heap()->EnableInlineAllocation();
2605
2606 CompileRun(inline_heap_allocation_source);
2607
2608 AllocationTracker* tracker =
2609 reinterpret_cast<i::HeapProfiler*>(heap_profiler)->allocation_tracker();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002610 CHECK(tracker);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002611 // Resolve all function locations.
2612 tracker->PrepareForSerialization();
2613 // Print for better diagnostics in case of failure.
2614 tracker->trace_tree()->Print(tracker);
2615
Ben Murdochc5610432016-08-08 18:44:38 +01002616 AllocationTraceNode* node = FindNode(tracker, ArrayVector(names));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002617 CHECK(node);
2618 CHECK_LT(node->allocation_count(), 100u);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002619
2620 CcTest::heap()->DisableInlineAllocation();
2621 heap_profiler->StopTrackingHeapObjects();
2622 }
2623}
2624
2625
2626TEST(TrackV8ApiAllocation) {
2627 v8::HandleScope scope(v8::Isolate::GetCurrent());
2628 LocalContext env;
2629
2630 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
2631 const char* names[] = { "(V8 API)" };
2632 heap_profiler->StartTrackingHeapObjects(true);
2633
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002634 v8::Local<v8::Object> o1 = v8::Object::New(env->GetIsolate());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002635 o1->Clone();
2636
2637 AllocationTracker* tracker =
2638 reinterpret_cast<i::HeapProfiler*>(heap_profiler)->allocation_tracker();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002639 CHECK(tracker);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002640 // Resolve all function locations.
2641 tracker->PrepareForSerialization();
2642 // Print for better diagnostics in case of failure.
2643 tracker->trace_tree()->Print(tracker);
2644
Ben Murdochc5610432016-08-08 18:44:38 +01002645 AllocationTraceNode* node = FindNode(tracker, ArrayVector(names));
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002646 CHECK(node);
2647 CHECK_GE(node->allocation_count(), 2u);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002648 CHECK_GE(node->allocation_size(), 4 * node->allocation_count());
2649 heap_profiler->StopTrackingHeapObjects();
2650}
2651
2652
2653TEST(ArrayBufferAndArrayBufferView) {
2654 LocalContext env;
2655 v8::HandleScope scope(env->GetIsolate());
2656 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
2657 CompileRun("arr1 = new Uint32Array(100);\n");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002658 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002659 CHECK(ValidateSnapshot(snapshot));
2660 const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
2661 const v8::HeapGraphNode* arr1_obj =
2662 GetProperty(global, v8::HeapGraphEdge::kProperty, "arr1");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002663 CHECK(arr1_obj);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002664 const v8::HeapGraphNode* arr1_buffer =
2665 GetProperty(arr1_obj, v8::HeapGraphEdge::kInternal, "buffer");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002666 CHECK(arr1_buffer);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002667 const v8::HeapGraphNode* backing_store =
2668 GetProperty(arr1_buffer, v8::HeapGraphEdge::kInternal, "backing_store");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002669 CHECK(backing_store);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002670 CHECK_EQ(400, static_cast<int>(backing_store->GetShallowSize()));
2671}
2672
2673
2674static int GetRetainersCount(const v8::HeapSnapshot* snapshot,
2675 const v8::HeapGraphNode* node) {
2676 int count = 0;
2677 for (int i = 0, l = snapshot->GetNodesCount(); i < l; ++i) {
2678 const v8::HeapGraphNode* parent = snapshot->GetNode(i);
2679 for (int j = 0, l2 = parent->GetChildrenCount(); j < l2; ++j) {
2680 if (parent->GetChild(j)->GetToNode() == node) {
2681 ++count;
2682 }
2683 }
2684 }
2685 return count;
2686}
2687
2688
2689TEST(ArrayBufferSharedBackingStore) {
2690 LocalContext env;
2691 v8::Isolate* isolate = env->GetIsolate();
2692 v8::HandleScope handle_scope(isolate);
2693 v8::HeapProfiler* heap_profiler = isolate->GetHeapProfiler();
2694
2695 v8::Local<v8::ArrayBuffer> ab = v8::ArrayBuffer::New(isolate, 1024);
2696 CHECK_EQ(1024, static_cast<int>(ab->ByteLength()));
2697 CHECK(!ab->IsExternal());
2698 v8::ArrayBuffer::Contents ab_contents = ab->Externalize();
2699 CHECK(ab->IsExternal());
2700
2701 CHECK_EQ(1024, static_cast<int>(ab_contents.ByteLength()));
2702 void* data = ab_contents.Data();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002703 CHECK(data != NULL);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002704 v8::Local<v8::ArrayBuffer> ab2 =
2705 v8::ArrayBuffer::New(isolate, data, ab_contents.ByteLength());
2706 CHECK(ab2->IsExternal());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002707 env->Global()->Set(env.local(), v8_str("ab1"), ab).FromJust();
2708 env->Global()->Set(env.local(), v8_str("ab2"), ab2).FromJust();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002709
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002710 v8::Local<v8::Value> result = CompileRun("ab2.byteLength");
2711 CHECK_EQ(1024, result->Int32Value(env.local()).FromJust());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002712
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002713 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002714 CHECK(ValidateSnapshot(snapshot));
2715 const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
2716 const v8::HeapGraphNode* ab1_node =
2717 GetProperty(global, v8::HeapGraphEdge::kProperty, "ab1");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002718 CHECK(ab1_node);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002719 const v8::HeapGraphNode* ab1_data =
2720 GetProperty(ab1_node, v8::HeapGraphEdge::kInternal, "backing_store");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002721 CHECK(ab1_data);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002722 const v8::HeapGraphNode* ab2_node =
2723 GetProperty(global, v8::HeapGraphEdge::kProperty, "ab2");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002724 CHECK(ab2_node);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002725 const v8::HeapGraphNode* ab2_data =
2726 GetProperty(ab2_node, v8::HeapGraphEdge::kInternal, "backing_store");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002727 CHECK(ab2_data);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002728 CHECK_EQ(ab1_data, ab2_data);
2729 CHECK_EQ(2, GetRetainersCount(snapshot, ab1_data));
2730 free(data);
2731}
2732
2733
2734TEST(BoxObject) {
2735 v8::Isolate* isolate = CcTest::isolate();
2736 v8::HandleScope scope(isolate);
2737 LocalContext env;
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002738 v8::Local<v8::Object> global_proxy = env->Global();
2739 v8::Local<v8::Object> global = global_proxy->GetPrototype().As<v8::Object>();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002740
2741 i::Factory* factory = CcTest::i_isolate()->factory();
2742 i::Handle<i::String> string = factory->NewStringFromStaticChars("string");
2743 i::Handle<i::Object> box = factory->NewBox(string);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002744 global->Set(env.local(), 0, v8::ToApiHandle<v8::Object>(box)).FromJust();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002745
2746 v8::HeapProfiler* heap_profiler = isolate->GetHeapProfiler();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002747 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002748 CHECK(ValidateSnapshot(snapshot));
2749 const v8::HeapGraphNode* global_node = GetGlobalObject(snapshot);
2750 const v8::HeapGraphNode* box_node =
2751 GetProperty(global_node, v8::HeapGraphEdge::kElement, "0");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002752 CHECK(box_node);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002753 v8::String::Utf8Value box_node_name(box_node->GetName());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002754 CHECK_EQ(0, strcmp("system / Box", *box_node_name));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002755 const v8::HeapGraphNode* box_value =
2756 GetProperty(box_node, v8::HeapGraphEdge::kInternal, "value");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002757 CHECK(box_value);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002758}
2759
2760
2761TEST(WeakContainers) {
2762 i::FLAG_allow_natives_syntax = true;
2763 LocalContext env;
2764 v8::HandleScope scope(env->GetIsolate());
2765 if (!CcTest::i_isolate()->use_crankshaft()) return;
2766 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
2767 CompileRun(
2768 "function foo(a) { return a.x; }\n"
2769 "obj = {x : 123};\n"
2770 "foo(obj);\n"
2771 "foo(obj);\n"
2772 "%OptimizeFunctionOnNextCall(foo);\n"
2773 "foo(obj);\n");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002774 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot();
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002775 CHECK(ValidateSnapshot(snapshot));
2776 const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
2777 const v8::HeapGraphNode* obj =
2778 GetProperty(global, v8::HeapGraphEdge::kProperty, "obj");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002779 CHECK(obj);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002780 const v8::HeapGraphNode* map =
2781 GetProperty(obj, v8::HeapGraphEdge::kInternal, "map");
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002782 CHECK(map);
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002783 const v8::HeapGraphNode* dependent_code =
2784 GetProperty(map, v8::HeapGraphEdge::kInternal, "dependent_code");
2785 if (!dependent_code) return;
2786 int count = dependent_code->GetChildrenCount();
2787 CHECK_NE(0, count);
2788 for (int i = 0; i < count; ++i) {
2789 const v8::HeapGraphEdge* prop = dependent_code->GetChild(i);
2790 CHECK_EQ(v8::HeapGraphEdge::kWeak, prop->GetType());
2791 }
2792}
2793
2794
2795static inline i::Address ToAddress(int n) {
2796 return reinterpret_cast<i::Address>(n);
2797}
2798
2799
2800TEST(AddressToTraceMap) {
2801 i::AddressToTraceMap map;
2802
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002803 CHECK_EQ(0u, map.GetTraceNodeId(ToAddress(150)));
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002804
2805 // [0x100, 0x200) -> 1
2806 map.AddRange(ToAddress(0x100), 0x100, 1U);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002807 CHECK_EQ(0u, map.GetTraceNodeId(ToAddress(0x50)));
2808 CHECK_EQ(1u, map.GetTraceNodeId(ToAddress(0x100)));
2809 CHECK_EQ(1u, map.GetTraceNodeId(ToAddress(0x150)));
2810 CHECK_EQ(0u, map.GetTraceNodeId(ToAddress(0x100 + 0x100)));
2811 CHECK_EQ(1u, map.size());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002812
2813 // [0x100, 0x200) -> 1, [0x200, 0x300) -> 2
2814 map.AddRange(ToAddress(0x200), 0x100, 2U);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002815 CHECK_EQ(2u, map.GetTraceNodeId(ToAddress(0x2a0)));
2816 CHECK_EQ(2u, map.size());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002817
2818 // [0x100, 0x180) -> 1, [0x180, 0x280) -> 3, [0x280, 0x300) -> 2
2819 map.AddRange(ToAddress(0x180), 0x100, 3U);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002820 CHECK_EQ(1u, map.GetTraceNodeId(ToAddress(0x17F)));
2821 CHECK_EQ(2u, map.GetTraceNodeId(ToAddress(0x280)));
2822 CHECK_EQ(3u, map.GetTraceNodeId(ToAddress(0x180)));
2823 CHECK_EQ(3u, map.size());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002824
2825 // [0x100, 0x180) -> 1, [0x180, 0x280) -> 3, [0x280, 0x300) -> 2,
2826 // [0x400, 0x500) -> 4
2827 map.AddRange(ToAddress(0x400), 0x100, 4U);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002828 CHECK_EQ(1u, map.GetTraceNodeId(ToAddress(0x17F)));
2829 CHECK_EQ(2u, map.GetTraceNodeId(ToAddress(0x280)));
2830 CHECK_EQ(3u, map.GetTraceNodeId(ToAddress(0x180)));
2831 CHECK_EQ(4u, map.GetTraceNodeId(ToAddress(0x450)));
2832 CHECK_EQ(0u, map.GetTraceNodeId(ToAddress(0x500)));
2833 CHECK_EQ(0u, map.GetTraceNodeId(ToAddress(0x350)));
2834 CHECK_EQ(4u, map.size());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002835
2836 // [0x100, 0x180) -> 1, [0x180, 0x200) -> 3, [0x200, 0x600) -> 5
2837 map.AddRange(ToAddress(0x200), 0x400, 5U);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002838 CHECK_EQ(5u, map.GetTraceNodeId(ToAddress(0x200)));
2839 CHECK_EQ(5u, map.GetTraceNodeId(ToAddress(0x400)));
2840 CHECK_EQ(3u, map.size());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002841
2842 // [0x100, 0x180) -> 1, [0x180, 0x200) -> 7, [0x200, 0x600) ->5
2843 map.AddRange(ToAddress(0x180), 0x80, 6U);
2844 map.AddRange(ToAddress(0x180), 0x80, 7U);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002845 CHECK_EQ(7u, map.GetTraceNodeId(ToAddress(0x180)));
2846 CHECK_EQ(5u, map.GetTraceNodeId(ToAddress(0x200)));
2847 CHECK_EQ(3u, map.size());
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002848
2849 map.Clear();
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00002850 CHECK_EQ(0u, map.size());
2851 CHECK_EQ(0u, map.GetTraceNodeId(ToAddress(0x400)));
Ben Murdoch3ef787d2012-04-12 10:51:47 +01002852}
Ben Murdoch097c5b22016-05-18 11:27:45 +01002853
2854
2855static const v8::AllocationProfile::Node* FindAllocationProfileNode(
2856 v8::AllocationProfile& profile, const Vector<const char*>& names) {
2857 v8::AllocationProfile::Node* node = profile.GetRootNode();
2858 for (int i = 0; node != nullptr && i < names.length(); ++i) {
2859 const char* name = names[i];
2860 auto children = node->children;
2861 node = nullptr;
2862 for (v8::AllocationProfile::Node* child : children) {
2863 v8::String::Utf8Value child_name(child->name);
2864 if (strcmp(*child_name, name) == 0) {
2865 node = child;
2866 break;
2867 }
2868 }
2869 }
2870 return node;
2871}
2872
Ben Murdochc5610432016-08-08 18:44:38 +01002873static void CheckNoZeroCountNodes(v8::AllocationProfile::Node* node) {
2874 for (auto alloc : node->allocations) {
2875 CHECK_GT(alloc.count, 0u);
2876 }
2877 for (auto child : node->children) {
2878 CheckNoZeroCountNodes(child);
2879 }
2880}
2881
Ben Murdoch097c5b22016-05-18 11:27:45 +01002882TEST(SamplingHeapProfiler) {
2883 v8::HandleScope scope(v8::Isolate::GetCurrent());
2884 LocalContext env;
2885 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
2886
2887 // Turn off always_opt. Inlining can cause stack traces to be shorter than
2888 // what we expect in this test.
2889 v8::internal::FLAG_always_opt = false;
2890
2891 // Suppress randomness to avoid flakiness in tests.
2892 v8::internal::FLAG_sampling_heap_profiler_suppress_randomness = true;
2893
2894 const char* script_source =
2895 "var A = [];\n"
2896 "function bar(size) { return new Array(size); }\n"
2897 "var foo = function() {\n"
2898 " for (var i = 0; i < 1024; ++i) {\n"
2899 " A[i] = bar(1024);\n"
2900 " }\n"
2901 "}\n"
2902 "foo();";
2903
2904 // Sample should be empty if requested before sampling has started.
2905 {
2906 v8::AllocationProfile* profile = heap_profiler->GetAllocationProfile();
2907 CHECK(profile == nullptr);
2908 }
2909
2910 int count_1024 = 0;
2911 {
2912 heap_profiler->StartSamplingHeapProfiler(1024);
2913 CompileRun(script_source);
2914
2915 v8::base::SmartPointer<v8::AllocationProfile> profile(
2916 heap_profiler->GetAllocationProfile());
2917 CHECK(!profile.is_empty());
2918
2919 const char* names[] = {"", "foo", "bar"};
Ben Murdochc5610432016-08-08 18:44:38 +01002920 auto node_bar = FindAllocationProfileNode(*profile, ArrayVector(names));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002921 CHECK(node_bar);
2922
2923 // Count the number of allocations we sampled from bar.
2924 for (auto allocation : node_bar->allocations) {
2925 count_1024 += allocation.count;
2926 }
2927
2928 heap_profiler->StopSamplingHeapProfiler();
2929 }
2930
2931 // Samples should get cleared once sampling is stopped.
2932 {
2933 v8::AllocationProfile* profile = heap_profiler->GetAllocationProfile();
2934 CHECK(profile == nullptr);
2935 }
2936
2937 // Sampling at a higher rate should give us similar numbers of objects.
2938 {
2939 heap_profiler->StartSamplingHeapProfiler(128);
2940 CompileRun(script_source);
2941
2942 v8::base::SmartPointer<v8::AllocationProfile> profile(
2943 heap_profiler->GetAllocationProfile());
2944 CHECK(!profile.is_empty());
2945
2946 const char* names[] = {"", "foo", "bar"};
Ben Murdochc5610432016-08-08 18:44:38 +01002947 auto node_bar = FindAllocationProfileNode(*profile, ArrayVector(names));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002948 CHECK(node_bar);
2949
2950 // Count the number of allocations we sampled from bar.
2951 int count_128 = 0;
2952 for (auto allocation : node_bar->allocations) {
2953 count_128 += allocation.count;
2954 }
2955
2956 // We should have similar unsampled counts of allocations. Though
2957 // we will sample different numbers of objects at different rates,
2958 // the unsampling process should produce similar final estimates
2959 // at the true number of allocations. However, the process to
2960 // determine these unsampled counts is probabilisitic so we need to
2961 // account for error.
2962 double max_count = std::max(count_128, count_1024);
2963 double min_count = std::min(count_128, count_1024);
2964 double percent_difference = (max_count - min_count) / min_count;
2965 CHECK_LT(percent_difference, 0.15);
2966
2967 heap_profiler->StopSamplingHeapProfiler();
2968 }
2969
2970 // A more complicated test cases with deeper call graph and dynamically
2971 // generated function names.
2972 {
2973 heap_profiler->StartSamplingHeapProfiler(64);
2974 CompileRun(record_trace_tree_source);
2975
2976 v8::base::SmartPointer<v8::AllocationProfile> profile(
2977 heap_profiler->GetAllocationProfile());
2978 CHECK(!profile.is_empty());
2979
2980 const char* names1[] = {"", "start", "f_0_0", "f_0_1", "f_0_2"};
Ben Murdochc5610432016-08-08 18:44:38 +01002981 auto node1 = FindAllocationProfileNode(*profile, ArrayVector(names1));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002982 CHECK(node1);
2983
2984 const char* names2[] = {"", "generateFunctions"};
Ben Murdochc5610432016-08-08 18:44:38 +01002985 auto node2 = FindAllocationProfileNode(*profile, ArrayVector(names2));
Ben Murdoch097c5b22016-05-18 11:27:45 +01002986 CHECK(node2);
2987
2988 heap_profiler->StopSamplingHeapProfiler();
2989 }
Ben Murdochda12d292016-06-02 14:46:10 +01002990
2991 // A test case with scripts unloaded before profile gathered
2992 {
2993 heap_profiler->StartSamplingHeapProfiler(64);
2994 CompileRun(
2995 "for (var i = 0; i < 1024; i++) {\n"
2996 " eval(\"new Array(100)\");\n"
2997 "}\n");
2998
2999 CcTest::heap()->CollectAllGarbage();
3000
3001 v8::base::SmartPointer<v8::AllocationProfile> profile(
3002 heap_profiler->GetAllocationProfile());
3003 CHECK(!profile.is_empty());
3004
Ben Murdochc5610432016-08-08 18:44:38 +01003005 CheckNoZeroCountNodes(profile->GetRootNode());
3006
Ben Murdochda12d292016-06-02 14:46:10 +01003007 heap_profiler->StopSamplingHeapProfiler();
3008 }
Ben Murdoch097c5b22016-05-18 11:27:45 +01003009}
3010
3011
3012TEST(SamplingHeapProfilerApiAllocation) {
3013 v8::HandleScope scope(v8::Isolate::GetCurrent());
3014 LocalContext env;
3015 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
3016
3017 // Suppress randomness to avoid flakiness in tests.
3018 v8::internal::FLAG_sampling_heap_profiler_suppress_randomness = true;
3019
3020 heap_profiler->StartSamplingHeapProfiler(256);
3021
3022 for (int i = 0; i < 8 * 1024; ++i) v8::Object::New(env->GetIsolate());
3023
3024 v8::base::SmartPointer<v8::AllocationProfile> profile(
3025 heap_profiler->GetAllocationProfile());
3026 CHECK(!profile.is_empty());
3027 const char* names[] = {"(V8 API)"};
Ben Murdochc5610432016-08-08 18:44:38 +01003028 auto node = FindAllocationProfileNode(*profile, ArrayVector(names));
Ben Murdoch097c5b22016-05-18 11:27:45 +01003029 CHECK(node);
3030
3031 heap_profiler->StopSamplingHeapProfiler();
3032}
3033
3034TEST(SamplingHeapProfilerLeftTrimming) {
3035 v8::HandleScope scope(v8::Isolate::GetCurrent());
3036 LocalContext env;
3037 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler();
3038
3039 // Suppress randomness to avoid flakiness in tests.
3040 v8::internal::FLAG_sampling_heap_profiler_suppress_randomness = true;
3041
3042 heap_profiler->StartSamplingHeapProfiler(64);
3043
3044 CompileRun(
3045 "for (var j = 0; j < 500; ++j) {\n"
3046 " var a = [];\n"
3047 " for (var i = 0; i < 5; ++i)\n"
3048 " a[i] = i;\n"
3049 " for (var i = 0; i < 3; ++i)\n"
3050 " a.shift();\n"
3051 "}\n");
3052
3053 CcTest::heap()->CollectGarbage(v8::internal::NEW_SPACE);
3054 // Should not crash.
3055
3056 heap_profiler->StopSamplingHeapProfiler();
3057}