blob: 92ad0a4002a1fbd967a3bf03a4324ec8be8c290d [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2009 the V8 project authors. All rights reserved.
2//
3// Tests for heap profiler
4
5#ifdef ENABLE_LOGGING_AND_PROFILING
6
7#include "v8.h"
8#include "heap-profiler.h"
Kristian Monsen9dcf7e22010-06-28 14:14:28 +01009#include "snapshot.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000010#include "string-stream.h"
11#include "cctest.h"
Steve Block6ded16b2010-05-10 14:33:55 +010012#include "zone-inl.h"
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010013#include "../include/v8-profiler.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000014
15namespace i = v8::internal;
16using i::ClustersCoarser;
17using i::JSObjectsCluster;
18using i::JSObjectsRetainerTree;
19using i::JSObjectsClusterTree;
20using i::RetainerHeapProfile;
21
22
23static void CompileAndRunScript(const char *src) {
24 v8::Script::Compile(v8::String::New(src))->Run();
25}
26
27
28namespace {
29
30class ConstructorHeapProfileTestHelper : public i::ConstructorHeapProfile {
31 public:
32 ConstructorHeapProfileTestHelper()
33 : i::ConstructorHeapProfile(),
34 f_name_(i::Factory::NewStringFromAscii(i::CStrVector("F"))),
35 f_count_(0) {
36 }
37
38 void Call(const JSObjectsCluster& cluster,
39 const i::NumberAndSizeInfo& number_and_size) {
40 if (f_name_->Equals(cluster.constructor())) {
41 CHECK_EQ(f_count_, 0);
42 f_count_ = number_and_size.number();
43 CHECK_GT(f_count_, 0);
44 }
45 }
46
47 int f_count() { return f_count_; }
48
49 private:
50 i::Handle<i::String> f_name_;
51 int f_count_;
52};
53
54} // namespace
55
56
57TEST(ConstructorProfile) {
58 v8::HandleScope scope;
Ben Murdoch3bec4d22010-07-22 14:51:16 +010059 LocalContext env;
Steve Blocka7e24c12009-10-30 11:49:00 +000060
61 CompileAndRunScript(
62 "function F() {} // A constructor\n"
63 "var f1 = new F();\n"
64 "var f2 = new F();\n");
65
66 ConstructorHeapProfileTestHelper cons_profile;
67 i::AssertNoAllocation no_alloc;
68 i::HeapIterator iterator;
Leon Clarked91b9f72010-01-27 17:25:45 +000069 for (i::HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next())
Steve Blocka7e24c12009-10-30 11:49:00 +000070 cons_profile.CollectStats(obj);
Steve Blocka7e24c12009-10-30 11:49:00 +000071 CHECK_EQ(0, cons_profile.f_count());
72 cons_profile.PrintStats();
73 CHECK_EQ(2, cons_profile.f_count());
74}
75
76
77static JSObjectsCluster AddHeapObjectToTree(JSObjectsRetainerTree* tree,
78 i::String* constructor,
79 int instance,
80 JSObjectsCluster* ref1 = NULL,
81 JSObjectsCluster* ref2 = NULL,
82 JSObjectsCluster* ref3 = NULL) {
83 JSObjectsCluster o(constructor, reinterpret_cast<i::Object*>(instance));
84 JSObjectsClusterTree* o_tree = new JSObjectsClusterTree();
85 JSObjectsClusterTree::Locator o_loc;
86 if (ref1 != NULL) o_tree->Insert(*ref1, &o_loc);
87 if (ref2 != NULL) o_tree->Insert(*ref2, &o_loc);
88 if (ref3 != NULL) o_tree->Insert(*ref3, &o_loc);
89 JSObjectsRetainerTree::Locator loc;
90 tree->Insert(o, &loc);
91 loc.set_value(o_tree);
92 return o;
93}
94
95
96static void AddSelfReferenceToTree(JSObjectsRetainerTree* tree,
97 JSObjectsCluster* self_ref) {
98 JSObjectsRetainerTree::Locator loc;
99 CHECK(tree->Find(*self_ref, &loc));
100 JSObjectsClusterTree::Locator o_loc;
101 CHECK_NE(NULL, loc.value());
102 loc.value()->Insert(*self_ref, &o_loc);
103}
104
105
106static inline void CheckEqualsHelper(const char* file, int line,
107 const char* expected_source,
108 const JSObjectsCluster& expected,
109 const char* value_source,
110 const JSObjectsCluster& value) {
111 if (JSObjectsCluster::Compare(expected, value) != 0) {
112 i::HeapStringAllocator allocator;
113 i::StringStream stream(&allocator);
114 stream.Add("# Expected: ");
115 expected.DebugPrint(&stream);
116 stream.Add("\n# Found: ");
117 value.DebugPrint(&stream);
118 V8_Fatal(file, line, "CHECK_EQ(%s, %s) failed\n%s",
119 expected_source, value_source,
120 *stream.ToCString());
121 }
122}
123
124
125static inline void CheckNonEqualsHelper(const char* file, int line,
126 const char* expected_source,
127 const JSObjectsCluster& expected,
128 const char* value_source,
129 const JSObjectsCluster& value) {
130 if (JSObjectsCluster::Compare(expected, value) == 0) {
131 i::HeapStringAllocator allocator;
132 i::StringStream stream(&allocator);
133 stream.Add("# !Expected: ");
134 expected.DebugPrint(&stream);
135 stream.Add("\n# Found: ");
136 value.DebugPrint(&stream);
137 V8_Fatal(file, line, "CHECK_NE(%s, %s) failed\n%s",
138 expected_source, value_source,
139 *stream.ToCString());
140 }
141}
142
143
144TEST(ClustersCoarserSimple) {
145 v8::HandleScope scope;
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100146 LocalContext env;
Steve Blocka7e24c12009-10-30 11:49:00 +0000147
148 i::ZoneScope zn_scope(i::DELETE_ON_EXIT);
149
150 JSObjectsRetainerTree tree;
151 JSObjectsCluster function(i::Heap::function_class_symbol());
152 JSObjectsCluster a(*i::Factory::NewStringFromAscii(i::CStrVector("A")));
153 JSObjectsCluster b(*i::Factory::NewStringFromAscii(i::CStrVector("B")));
154
155 // o1 <- Function
156 JSObjectsCluster o1 =
157 AddHeapObjectToTree(&tree, i::Heap::Object_symbol(), 0x100, &function);
158 // o2 <- Function
159 JSObjectsCluster o2 =
160 AddHeapObjectToTree(&tree, i::Heap::Object_symbol(), 0x200, &function);
161 // o3 <- A, B
162 JSObjectsCluster o3 =
163 AddHeapObjectToTree(&tree, i::Heap::Object_symbol(), 0x300, &a, &b);
164 // o4 <- B, A
165 JSObjectsCluster o4 =
166 AddHeapObjectToTree(&tree, i::Heap::Object_symbol(), 0x400, &b, &a);
167 // o5 <- A, B, Function
168 JSObjectsCluster o5 =
169 AddHeapObjectToTree(&tree, i::Heap::Object_symbol(), 0x500,
170 &a, &b, &function);
171
172 ClustersCoarser coarser;
173 coarser.Process(&tree);
174
175 CHECK_EQ(coarser.GetCoarseEquivalent(o1), coarser.GetCoarseEquivalent(o2));
176 CHECK_EQ(coarser.GetCoarseEquivalent(o3), coarser.GetCoarseEquivalent(o4));
177 CHECK_NE(coarser.GetCoarseEquivalent(o1), coarser.GetCoarseEquivalent(o3));
178 CHECK_EQ(JSObjectsCluster(), coarser.GetCoarseEquivalent(o5));
179}
180
181
182TEST(ClustersCoarserMultipleConstructors) {
183 v8::HandleScope scope;
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100184 LocalContext env;
Steve Blocka7e24c12009-10-30 11:49:00 +0000185
186 i::ZoneScope zn_scope(i::DELETE_ON_EXIT);
187
188 JSObjectsRetainerTree tree;
189 JSObjectsCluster function(i::Heap::function_class_symbol());
190
191 // o1 <- Function
192 JSObjectsCluster o1 =
193 AddHeapObjectToTree(&tree, i::Heap::Object_symbol(), 0x100, &function);
194 // a1 <- Function
195 JSObjectsCluster a1 =
196 AddHeapObjectToTree(&tree, i::Heap::Array_symbol(), 0x1000, &function);
197 // o2 <- Function
198 JSObjectsCluster o2 =
199 AddHeapObjectToTree(&tree, i::Heap::Object_symbol(), 0x200, &function);
200 // a2 <- Function
201 JSObjectsCluster a2 =
202 AddHeapObjectToTree(&tree, i::Heap::Array_symbol(), 0x2000, &function);
203
204 ClustersCoarser coarser;
205 coarser.Process(&tree);
206
207 CHECK_EQ(coarser.GetCoarseEquivalent(o1), coarser.GetCoarseEquivalent(o2));
208 CHECK_EQ(coarser.GetCoarseEquivalent(a1), coarser.GetCoarseEquivalent(a2));
209}
210
211
212TEST(ClustersCoarserPathsTraversal) {
213 v8::HandleScope scope;
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100214 LocalContext env;
Steve Blocka7e24c12009-10-30 11:49:00 +0000215
216 i::ZoneScope zn_scope(i::DELETE_ON_EXIT);
217
218 JSObjectsRetainerTree tree;
219
220 // On the following graph:
221 //
222 // p
223 // <- o21 <- o11 <-
224 // q o
225 // <- o22 <- o12 <-
226 // r
227 //
228 // we expect that coarser will deduce equivalences: p ~ q ~ r,
229 // o21 ~ o22, and o11 ~ o12.
230
231 JSObjectsCluster o =
232 AddHeapObjectToTree(&tree, i::Heap::Object_symbol(), 0x100);
233 JSObjectsCluster o11 =
234 AddHeapObjectToTree(&tree, i::Heap::Object_symbol(), 0x110, &o);
235 JSObjectsCluster o12 =
236 AddHeapObjectToTree(&tree, i::Heap::Object_symbol(), 0x120, &o);
237 JSObjectsCluster o21 =
238 AddHeapObjectToTree(&tree, i::Heap::Object_symbol(), 0x210, &o11);
239 JSObjectsCluster o22 =
240 AddHeapObjectToTree(&tree, i::Heap::Object_symbol(), 0x220, &o12);
241 JSObjectsCluster p =
242 AddHeapObjectToTree(&tree, i::Heap::Object_symbol(), 0x300, &o21);
243 JSObjectsCluster q =
244 AddHeapObjectToTree(&tree, i::Heap::Object_symbol(), 0x310, &o21, &o22);
245 JSObjectsCluster r =
246 AddHeapObjectToTree(&tree, i::Heap::Object_symbol(), 0x320, &o22);
247
248 ClustersCoarser coarser;
249 coarser.Process(&tree);
250
251 CHECK_EQ(JSObjectsCluster(), coarser.GetCoarseEquivalent(o));
252 CHECK_NE(JSObjectsCluster(), coarser.GetCoarseEquivalent(o11));
253 CHECK_EQ(coarser.GetCoarseEquivalent(o11), coarser.GetCoarseEquivalent(o12));
254 CHECK_EQ(coarser.GetCoarseEquivalent(o21), coarser.GetCoarseEquivalent(o22));
255 CHECK_NE(coarser.GetCoarseEquivalent(o11), coarser.GetCoarseEquivalent(o21));
256 CHECK_NE(JSObjectsCluster(), coarser.GetCoarseEquivalent(p));
257 CHECK_EQ(coarser.GetCoarseEquivalent(p), coarser.GetCoarseEquivalent(q));
258 CHECK_EQ(coarser.GetCoarseEquivalent(q), coarser.GetCoarseEquivalent(r));
259 CHECK_NE(coarser.GetCoarseEquivalent(o11), coarser.GetCoarseEquivalent(p));
260 CHECK_NE(coarser.GetCoarseEquivalent(o21), coarser.GetCoarseEquivalent(p));
261}
262
263
264TEST(ClustersCoarserSelf) {
265 v8::HandleScope scope;
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100266 LocalContext env;
Steve Blocka7e24c12009-10-30 11:49:00 +0000267
268 i::ZoneScope zn_scope(i::DELETE_ON_EXIT);
269
270 JSObjectsRetainerTree tree;
271
272 // On the following graph:
273 //
274 // p (self-referencing)
275 // <- o1 <-
276 // q (self-referencing) o
277 // <- o2 <-
278 // r (self-referencing)
279 //
280 // we expect that coarser will deduce equivalences: p ~ q ~ r, o1 ~ o2;
281
282 JSObjectsCluster o =
283 AddHeapObjectToTree(&tree, i::Heap::Object_symbol(), 0x100);
284 JSObjectsCluster o1 =
285 AddHeapObjectToTree(&tree, i::Heap::Object_symbol(), 0x110, &o);
286 JSObjectsCluster o2 =
287 AddHeapObjectToTree(&tree, i::Heap::Object_symbol(), 0x120, &o);
288 JSObjectsCluster p =
289 AddHeapObjectToTree(&tree, i::Heap::Object_symbol(), 0x300, &o1);
290 AddSelfReferenceToTree(&tree, &p);
291 JSObjectsCluster q =
292 AddHeapObjectToTree(&tree, i::Heap::Object_symbol(), 0x310, &o1, &o2);
293 AddSelfReferenceToTree(&tree, &q);
294 JSObjectsCluster r =
295 AddHeapObjectToTree(&tree, i::Heap::Object_symbol(), 0x320, &o2);
296 AddSelfReferenceToTree(&tree, &r);
297
298 ClustersCoarser coarser;
299 coarser.Process(&tree);
300
301 CHECK_EQ(JSObjectsCluster(), coarser.GetCoarseEquivalent(o));
302 CHECK_NE(JSObjectsCluster(), coarser.GetCoarseEquivalent(o1));
303 CHECK_EQ(coarser.GetCoarseEquivalent(o1), coarser.GetCoarseEquivalent(o2));
304 CHECK_NE(JSObjectsCluster(), coarser.GetCoarseEquivalent(p));
305 CHECK_EQ(coarser.GetCoarseEquivalent(p), coarser.GetCoarseEquivalent(q));
306 CHECK_EQ(coarser.GetCoarseEquivalent(q), coarser.GetCoarseEquivalent(r));
307 CHECK_NE(coarser.GetCoarseEquivalent(o1), coarser.GetCoarseEquivalent(p));
308}
309
310
311namespace {
312
313class RetainerProfilePrinter : public RetainerHeapProfile::Printer {
314 public:
315 RetainerProfilePrinter() : stream_(&allocator_), lines_(100) {}
316
317 void PrintRetainers(const JSObjectsCluster& cluster,
318 const i::StringStream& retainers) {
319 cluster.Print(&stream_);
320 stream_.Add("%s", *(retainers.ToCString()));
321 stream_.Put('\0');
322 }
323
324 const char* GetRetainers(const char* constructor) {
325 FillLines();
326 const size_t cons_len = strlen(constructor);
327 for (int i = 0; i < lines_.length(); ++i) {
328 if (strncmp(constructor, lines_[i], cons_len) == 0 &&
329 lines_[i][cons_len] == ',') {
330 return lines_[i] + cons_len + 1;
331 }
332 }
333 return NULL;
334 }
335
336 private:
337 void FillLines() {
338 if (lines_.length() > 0) return;
339 stream_.Put('\0');
340 stream_str_ = stream_.ToCString();
341 const char* pos = *stream_str_;
342 while (pos != NULL && *pos != '\0') {
343 lines_.Add(pos);
344 pos = strchr(pos, '\0');
345 if (pos != NULL) ++pos;
346 }
347 }
348
349 i::HeapStringAllocator allocator_;
350 i::StringStream stream_;
351 i::SmartPointer<const char> stream_str_;
352 i::List<const char*> lines_;
353};
354
355} // namespace
356
357
358TEST(RetainerProfile) {
359 v8::HandleScope scope;
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100360 LocalContext env;
Steve Blocka7e24c12009-10-30 11:49:00 +0000361
362 CompileAndRunScript(
363 "function A() {}\n"
364 "function B(x) { this.x = x; }\n"
365 "function C(x) { this.x1 = x; this.x2 = x; }\n"
366 "var a = new A();\n"
367 "var b1 = new B(a), b2 = new B(a);\n"
368 "var c = new C(a);");
369
370 RetainerHeapProfile ret_profile;
371 i::AssertNoAllocation no_alloc;
372 i::HeapIterator iterator;
Leon Clarked91b9f72010-01-27 17:25:45 +0000373 for (i::HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next())
Steve Blocka7e24c12009-10-30 11:49:00 +0000374 ret_profile.CollectStats(obj);
Steve Blocka7e24c12009-10-30 11:49:00 +0000375 RetainerProfilePrinter printer;
376 ret_profile.DebugPrintStats(&printer);
377 const char* retainers_of_a = printer.GetRetainers("A");
378 // The order of retainers is unspecified, so we check string length, and
379 // verify each retainer separately.
Steve Blockd0582a62009-12-15 09:54:21 +0000380 CHECK_EQ(i::StrLength("(global property);1,B;2,C;2"),
381 i::StrLength(retainers_of_a));
Steve Blocka7e24c12009-10-30 11:49:00 +0000382 CHECK(strstr(retainers_of_a, "(global property);1") != NULL);
383 CHECK(strstr(retainers_of_a, "B;2") != NULL);
384 CHECK(strstr(retainers_of_a, "C;2") != NULL);
385 CHECK_EQ("(global property);2", printer.GetRetainers("B"));
386 CHECK_EQ("(global property);1", printer.GetRetainers("C"));
387}
388
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100389
390namespace {
391
392class NamedEntriesDetector {
393 public:
394 NamedEntriesDetector()
395 : has_A1(false), has_B1(false), has_C1(false),
396 has_A2(false), has_B2(false), has_C2(false) {
397 }
398
Iain Merrick75681382010-08-19 15:07:18 +0100399 void Apply(i::HeapEntry** entry_ptr) {
400 if (IsReachableNodeWithName(*entry_ptr, "A1")) has_A1 = true;
401 if (IsReachableNodeWithName(*entry_ptr, "B1")) has_B1 = true;
402 if (IsReachableNodeWithName(*entry_ptr, "C1")) has_C1 = true;
403 if (IsReachableNodeWithName(*entry_ptr, "A2")) has_A2 = true;
404 if (IsReachableNodeWithName(*entry_ptr, "B2")) has_B2 = true;
405 if (IsReachableNodeWithName(*entry_ptr, "C2")) has_C2 = true;
406 }
407
408 static bool IsReachableNodeWithName(i::HeapEntry* entry, const char* name) {
409 return strcmp(name, entry->name()) == 0 && entry->painted_reachable();
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100410 }
411
412 bool has_A1;
413 bool has_B1;
414 bool has_C1;
415 bool has_A2;
416 bool has_B2;
417 bool has_C2;
418};
419
420} // namespace
421
422
423static const v8::HeapGraphNode* GetGlobalObject(
424 const v8::HeapSnapshot* snapshot) {
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100425 CHECK_EQ(1, snapshot->GetRoot()->GetChildrenCount());
426 return snapshot->GetRoot()->GetChild(0)->GetToNode();
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100427}
428
429
430static const v8::HeapGraphNode* GetProperty(const v8::HeapGraphNode* node,
431 v8::HeapGraphEdge::Type type,
432 const char* name) {
433 for (int i = 0, count = node->GetChildrenCount(); i < count; ++i) {
434 const v8::HeapGraphEdge* prop = node->GetChild(i);
435 v8::String::AsciiValue prop_name(prop->GetName());
436 if (prop->GetType() == type && strcmp(name, *prop_name) == 0)
437 return prop->GetToNode();
438 }
439 return NULL;
440}
441
442
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100443static bool IsNodeRetainedAs(const v8::HeapGraphNode* node,
444 v8::HeapGraphEdge::Type type,
445 const char* name) {
446 for (int i = 0, count = node->GetRetainersCount(); i < count; ++i) {
447 const v8::HeapGraphEdge* prop = node->GetRetainer(i);
448 v8::String::AsciiValue prop_name(prop->GetName());
449 if (prop->GetType() == type && strcmp(name, *prop_name) == 0)
450 return true;
451 }
452 return false;
453}
454
455
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100456static bool HasString(const v8::HeapGraphNode* node, const char* contents) {
457 for (int i = 0, count = node->GetChildrenCount(); i < count; ++i) {
458 const v8::HeapGraphEdge* prop = node->GetChild(i);
459 const v8::HeapGraphNode* node = prop->GetToNode();
Iain Merrick75681382010-08-19 15:07:18 +0100460 if (node->GetType() == v8::HeapGraphNode::kString) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100461 v8::String::AsciiValue node_name(node->GetName());
462 if (strcmp(contents, *node_name) == 0) return true;
463 }
464 }
465 return false;
466}
467
468
469TEST(HeapSnapshot) {
470 v8::HandleScope scope;
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100471 v8::Handle<v8::String> token1 = v8::String::New("token1");
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100472 LocalContext env1;
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100473 env1->SetSecurityToken(token1);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100474
475 CompileAndRunScript(
476 "function A1() {}\n"
477 "function B1(x) { this.x = x; }\n"
478 "function C1(x) { this.x1 = x; this.x2 = x; }\n"
479 "var a1 = new A1();\n"
480 "var b1_1 = new B1(a1), b1_2 = new B1(a1);\n"
481 "var c1 = new C1(a1);");
482
483 v8::Handle<v8::String> token2 = v8::String::New("token2");
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100484 LocalContext env2;
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100485 env2->SetSecurityToken(token2);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100486
487 CompileAndRunScript(
488 "function A2() {}\n"
489 "function B2(x) { return function() { return typeof x; }; }\n"
490 "function C2(x) { this.x1 = x; this.x2 = x; this[1] = x; }\n"
491 "var a2 = new A2();\n"
492 "var b2_1 = new B2(a2), b2_2 = new B2(a2);\n"
493 "var c2 = new C2(a2);");
494 const v8::HeapSnapshot* snapshot_env2 =
495 v8::HeapProfiler::TakeSnapshot(v8::String::New("env2"));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100496 i::HeapSnapshot* i_snapshot_env2 =
497 const_cast<i::HeapSnapshot*>(
498 reinterpret_cast<const i::HeapSnapshot*>(snapshot_env2));
Iain Merrick75681382010-08-19 15:07:18 +0100499 const v8::HeapGraphNode* global_env2 = GetGlobalObject(snapshot_env2);
500 // Paint all nodes reachable from global object.
501 i_snapshot_env2->ClearPaint();
502 const_cast<i::HeapEntry*>(
503 reinterpret_cast<const i::HeapEntry*>(global_env2))->PaintAllReachable();
504
505 // Verify, that JS global object of env2 doesn't have '..1'
506 // properties, but has '..2' properties.
507 CHECK_EQ(NULL, GetProperty(global_env2, v8::HeapGraphEdge::kProperty, "a1"));
508 CHECK_EQ(
509 NULL, GetProperty(global_env2, v8::HeapGraphEdge::kProperty, "b1_1"));
510 CHECK_EQ(
511 NULL, GetProperty(global_env2, v8::HeapGraphEdge::kProperty, "b1_2"));
512 CHECK_EQ(NULL, GetProperty(global_env2, v8::HeapGraphEdge::kProperty, "c1"));
513 const v8::HeapGraphNode* a2_node =
514 GetProperty(global_env2, v8::HeapGraphEdge::kProperty, "a2");
515 CHECK_NE(NULL, a2_node);
516 CHECK_NE(
517 NULL, GetProperty(global_env2, v8::HeapGraphEdge::kProperty, "b2_1"));
518 CHECK_NE(
519 NULL, GetProperty(global_env2, v8::HeapGraphEdge::kProperty, "b2_2"));
520 CHECK_NE(NULL, GetProperty(global_env2, v8::HeapGraphEdge::kProperty, "c2"));
521
522 // Verify that anything related to '[ABC]1' is not reachable.
523 NamedEntriesDetector det;
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100524 i_snapshot_env2->IterateEntries(&det);
525 CHECK(!det.has_A1);
526 CHECK(!det.has_B1);
527 CHECK(!det.has_C1);
528 CHECK(det.has_A2);
529 CHECK(det.has_B2);
530 CHECK(det.has_C2);
531
532 // Verify 'a2' object retainers. They are:
533 // - (global object).a2
534 // - c2.x1, c2.x2, c2[1]
535 // - b2_1 and b2_2 closures: via 'x' variable
536 CHECK_EQ(6, a2_node->GetRetainingPathsCount());
537 bool has_global_obj_a2_ref = false;
538 bool has_c2_x1_ref = false, has_c2_x2_ref = false, has_c2_1_ref = false;
539 bool has_b2_1_x_ref = false, has_b2_2_x_ref = false;
540 for (int i = 0; i < a2_node->GetRetainingPathsCount(); ++i) {
541 const v8::HeapGraphPath* path = a2_node->GetRetainingPath(i);
542 const int edges_count = path->GetEdgesCount();
543 CHECK_GT(edges_count, 0);
544 const v8::HeapGraphEdge* last_edge = path->GetEdge(edges_count - 1);
545 v8::String::AsciiValue last_edge_name(last_edge->GetName());
546 if (strcmp("a2", *last_edge_name) == 0
Iain Merrick75681382010-08-19 15:07:18 +0100547 && last_edge->GetType() == v8::HeapGraphEdge::kProperty) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100548 has_global_obj_a2_ref = true;
549 continue;
550 }
551 CHECK_GT(edges_count, 1);
552 const v8::HeapGraphEdge* prev_edge = path->GetEdge(edges_count - 2);
553 v8::String::AsciiValue prev_edge_name(prev_edge->GetName());
554 if (strcmp("x1", *last_edge_name) == 0
Iain Merrick75681382010-08-19 15:07:18 +0100555 && last_edge->GetType() == v8::HeapGraphEdge::kProperty
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100556 && strcmp("c2", *prev_edge_name) == 0) has_c2_x1_ref = true;
557 if (strcmp("x2", *last_edge_name) == 0
Iain Merrick75681382010-08-19 15:07:18 +0100558 && last_edge->GetType() == v8::HeapGraphEdge::kProperty
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100559 && strcmp("c2", *prev_edge_name) == 0) has_c2_x2_ref = true;
560 if (strcmp("1", *last_edge_name) == 0
Iain Merrick75681382010-08-19 15:07:18 +0100561 && last_edge->GetType() == v8::HeapGraphEdge::kElement
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100562 && strcmp("c2", *prev_edge_name) == 0) has_c2_1_ref = true;
563 if (strcmp("x", *last_edge_name) == 0
Iain Merrick75681382010-08-19 15:07:18 +0100564 && last_edge->GetType() == v8::HeapGraphEdge::kContextVariable
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100565 && strcmp("b2_1", *prev_edge_name) == 0) has_b2_1_x_ref = true;
566 if (strcmp("x", *last_edge_name) == 0
Iain Merrick75681382010-08-19 15:07:18 +0100567 && last_edge->GetType() == v8::HeapGraphEdge::kContextVariable
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100568 && strcmp("b2_2", *prev_edge_name) == 0) has_b2_2_x_ref = true;
569 }
570 CHECK(has_global_obj_a2_ref);
571 CHECK(has_c2_x1_ref);
572 CHECK(has_c2_x2_ref);
573 CHECK(has_c2_1_ref);
574 CHECK(has_b2_1_x_ref);
575 CHECK(has_b2_2_x_ref);
576}
577
578
Iain Merrick75681382010-08-19 15:07:18 +0100579TEST(HeapSnapshotObjectSizes) {
580 v8::HandleScope scope;
581 LocalContext env;
582
583 // -a-> X1 --a
584 // x -b-> X2 <-|
585 CompileAndRunScript(
586 "function X(a, b) { this.a = a; this.b = b; }\n"
587 "x = new X(new X(), new X());\n"
588 "x.a.a = x.b;");
589 const v8::HeapSnapshot* snapshot =
590 v8::HeapProfiler::TakeSnapshot(v8::String::New("sizes"));
591 const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
592 const v8::HeapGraphNode* x =
593 GetProperty(global, v8::HeapGraphEdge::kProperty, "x");
594 CHECK_NE(NULL, x);
595 const v8::HeapGraphNode* x_prototype =
596 GetProperty(x, v8::HeapGraphEdge::kProperty, "prototype");
597 CHECK_NE(NULL, x_prototype);
598 const v8::HeapGraphNode* x1 =
599 GetProperty(x, v8::HeapGraphEdge::kProperty, "a");
600 CHECK_NE(NULL, x1);
601 const v8::HeapGraphNode* x2 =
602 GetProperty(x, v8::HeapGraphEdge::kProperty, "b");
603 CHECK_NE(NULL, x2);
604 CHECK_EQ(
605 x->GetSelfSize() * 3,
606 x->GetReachableSize() - x_prototype->GetReachableSize());
607 CHECK_EQ(
608 x->GetSelfSize() * 3 + x_prototype->GetSelfSize(), x->GetRetainedSize());
609 CHECK_EQ(
610 x1->GetSelfSize() * 2,
611 x1->GetReachableSize() - x_prototype->GetReachableSize());
612 CHECK_EQ(
613 x1->GetSelfSize(), x1->GetRetainedSize());
614 CHECK_EQ(
615 x2->GetSelfSize(),
616 x2->GetReachableSize() - x_prototype->GetReachableSize());
617 CHECK_EQ(
618 x2->GetSelfSize(), x2->GetRetainedSize());
619}
620
621
622TEST(HeapSnapshotEntryChildren) {
623 v8::HandleScope scope;
624 LocalContext env;
625
626 CompileAndRunScript(
627 "function A() { }\n"
628 "a = new A;");
629 const v8::HeapSnapshot* snapshot =
630 v8::HeapProfiler::TakeSnapshot(v8::String::New("children"));
631 const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
632 for (int i = 0, count = global->GetChildrenCount(); i < count; ++i) {
633 const v8::HeapGraphEdge* prop = global->GetChild(i);
634 CHECK_EQ(global, prop->GetFromNode());
635 }
636 const v8::HeapGraphNode* a =
637 GetProperty(global, v8::HeapGraphEdge::kProperty, "a");
638 CHECK_NE(NULL, a);
639 for (int i = 0, count = a->GetChildrenCount(); i < count; ++i) {
640 const v8::HeapGraphEdge* prop = a->GetChild(i);
641 CHECK_EQ(a, prop->GetFromNode());
642 }
643}
644
645
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100646TEST(HeapSnapshotCodeObjects) {
647 v8::HandleScope scope;
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100648 LocalContext env;
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100649
650 CompileAndRunScript(
651 "function lazy(x) { return x - 1; }\n"
652 "function compiled(x) { return x + 1; }\n"
653 "compiled(1)");
654 const v8::HeapSnapshot* snapshot =
655 v8::HeapProfiler::TakeSnapshot(v8::String::New("code"));
656
657 const v8::HeapGraphNode* global = GetGlobalObject(snapshot);
658 const v8::HeapGraphNode* compiled =
Iain Merrick75681382010-08-19 15:07:18 +0100659 GetProperty(global, v8::HeapGraphEdge::kProperty, "compiled");
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100660 CHECK_NE(NULL, compiled);
Iain Merrick75681382010-08-19 15:07:18 +0100661 CHECK_EQ(v8::HeapGraphNode::kClosure, compiled->GetType());
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100662 const v8::HeapGraphNode* lazy =
Iain Merrick75681382010-08-19 15:07:18 +0100663 GetProperty(global, v8::HeapGraphEdge::kProperty, "lazy");
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100664 CHECK_NE(NULL, lazy);
Iain Merrick75681382010-08-19 15:07:18 +0100665 CHECK_EQ(v8::HeapGraphNode::kClosure, lazy->GetType());
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100666
667 // Find references to code.
668 const v8::HeapGraphNode* compiled_code =
Iain Merrick75681382010-08-19 15:07:18 +0100669 GetProperty(compiled, v8::HeapGraphEdge::kInternal, "code");
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100670 CHECK_NE(NULL, compiled_code);
671 const v8::HeapGraphNode* lazy_code =
Iain Merrick75681382010-08-19 15:07:18 +0100672 GetProperty(lazy, v8::HeapGraphEdge::kInternal, "code");
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100673 CHECK_NE(NULL, lazy_code);
674
675 // Verify that non-compiled code doesn't contain references to "x"
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100676 // literal, while compiled code does. The scope info is stored in FixedArray
677 // objects attached to the SharedFunctionInfo.
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100678 bool compiled_references_x = false, lazy_references_x = false;
679 for (int i = 0, count = compiled_code->GetChildrenCount(); i < count; ++i) {
680 const v8::HeapGraphEdge* prop = compiled_code->GetChild(i);
681 const v8::HeapGraphNode* node = prop->GetToNode();
Iain Merrick75681382010-08-19 15:07:18 +0100682 if (node->GetType() == v8::HeapGraphNode::kArray) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100683 if (HasString(node, "x")) {
684 compiled_references_x = true;
685 break;
686 }
687 }
688 }
689 for (int i = 0, count = lazy_code->GetChildrenCount(); i < count; ++i) {
690 const v8::HeapGraphEdge* prop = lazy_code->GetChild(i);
691 const v8::HeapGraphNode* node = prop->GetToNode();
Iain Merrick75681382010-08-19 15:07:18 +0100692 if (node->GetType() == v8::HeapGraphNode::kArray) {
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100693 if (HasString(node, "x")) {
694 lazy_references_x = true;
695 break;
696 }
697 }
698 }
699 CHECK(compiled_references_x);
700 CHECK(!lazy_references_x);
701}
702
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100703
704// Trying to introduce a check helper for uint64_t causes many
705// overloading ambiguities, so it seems easier just to cast
706// them to a signed type.
707#define CHECK_EQ_UINT64_T(a, b) \
708 CHECK_EQ(static_cast<int64_t>(a), static_cast<int64_t>(b))
Iain Merrick75681382010-08-19 15:07:18 +0100709#define CHECK_NE_UINT64_T(a, b) \
710 CHECK((a) != (b)) // NOLINT
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100711
712TEST(HeapEntryIdsAndGC) {
713 v8::HandleScope scope;
714 LocalContext env;
715
716 CompileAndRunScript(
717 "function A() {}\n"
718 "function B(x) { this.x = x; }\n"
719 "var a = new A();\n"
720 "var b = new B(a);");
721 const v8::HeapSnapshot* snapshot1 =
722 v8::HeapProfiler::TakeSnapshot(v8::String::New("s1"));
723
724 i::Heap::CollectAllGarbage(true); // Enforce compaction.
725
726 const v8::HeapSnapshot* snapshot2 =
727 v8::HeapProfiler::TakeSnapshot(v8::String::New("s2"));
728
729 const v8::HeapGraphNode* global1 = GetGlobalObject(snapshot1);
730 const v8::HeapGraphNode* global2 = GetGlobalObject(snapshot2);
731 CHECK_NE_UINT64_T(0, global1->GetId());
732 CHECK_EQ_UINT64_T(global1->GetId(), global2->GetId());
733 const v8::HeapGraphNode* A1 =
Iain Merrick75681382010-08-19 15:07:18 +0100734 GetProperty(global1, v8::HeapGraphEdge::kProperty, "A");
735 CHECK_NE(NULL, A1);
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100736 const v8::HeapGraphNode* A2 =
Iain Merrick75681382010-08-19 15:07:18 +0100737 GetProperty(global2, v8::HeapGraphEdge::kProperty, "A");
738 CHECK_NE(NULL, A2);
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100739 CHECK_NE_UINT64_T(0, A1->GetId());
740 CHECK_EQ_UINT64_T(A1->GetId(), A2->GetId());
741 const v8::HeapGraphNode* B1 =
Iain Merrick75681382010-08-19 15:07:18 +0100742 GetProperty(global1, v8::HeapGraphEdge::kProperty, "B");
743 CHECK_NE(NULL, B1);
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100744 const v8::HeapGraphNode* B2 =
Iain Merrick75681382010-08-19 15:07:18 +0100745 GetProperty(global2, v8::HeapGraphEdge::kProperty, "B");
746 CHECK_NE(NULL, B2);
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100747 CHECK_NE_UINT64_T(0, B1->GetId());
748 CHECK_EQ_UINT64_T(B1->GetId(), B2->GetId());
749 const v8::HeapGraphNode* a1 =
Iain Merrick75681382010-08-19 15:07:18 +0100750 GetProperty(global1, v8::HeapGraphEdge::kProperty, "a");
751 CHECK_NE(NULL, a1);
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100752 const v8::HeapGraphNode* a2 =
Iain Merrick75681382010-08-19 15:07:18 +0100753 GetProperty(global2, v8::HeapGraphEdge::kProperty, "a");
754 CHECK_NE(NULL, a2);
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100755 CHECK_NE_UINT64_T(0, a1->GetId());
756 CHECK_EQ_UINT64_T(a1->GetId(), a2->GetId());
757 const v8::HeapGraphNode* b1 =
Iain Merrick75681382010-08-19 15:07:18 +0100758 GetProperty(global1, v8::HeapGraphEdge::kProperty, "b");
759 CHECK_NE(NULL, b1);
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100760 const v8::HeapGraphNode* b2 =
Iain Merrick75681382010-08-19 15:07:18 +0100761 GetProperty(global2, v8::HeapGraphEdge::kProperty, "b");
762 CHECK_NE(NULL, b2);
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100763 CHECK_NE_UINT64_T(0, b1->GetId());
764 CHECK_EQ_UINT64_T(b1->GetId(), b2->GetId());
765}
766
767
768TEST(HeapSnapshotsDiff) {
769 v8::HandleScope scope;
770 LocalContext env;
771
772 CompileAndRunScript(
773 "function A() {}\n"
774 "function B(x) { this.x = x; }\n"
775 "var a = new A();\n"
776 "var b = new B(a);");
777 const v8::HeapSnapshot* snapshot1 =
778 v8::HeapProfiler::TakeSnapshot(v8::String::New("s1"));
779
780 CompileAndRunScript(
781 "delete a;\n"
782 "b.x = null;\n"
783 "var a = new A();\n"
784 "var b2 = new B(a);");
785 const v8::HeapSnapshot* snapshot2 =
786 v8::HeapProfiler::TakeSnapshot(v8::String::New("s2"));
787
788 const v8::HeapSnapshotsDiff* diff = snapshot1->CompareWith(snapshot2);
789
790 // Verify additions: ensure that addition of A and B was detected.
791 const v8::HeapGraphNode* additions_root = diff->GetAdditionsRoot();
792 bool found_A = false, found_B = false;
793 uint64_t s1_A_id = 0;
794 for (int i = 0, count = additions_root->GetChildrenCount(); i < count; ++i) {
795 const v8::HeapGraphEdge* prop = additions_root->GetChild(i);
796 const v8::HeapGraphNode* node = prop->GetToNode();
Iain Merrick75681382010-08-19 15:07:18 +0100797 if (node->GetType() == v8::HeapGraphNode::kObject) {
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100798 v8::String::AsciiValue node_name(node->GetName());
799 if (strcmp(*node_name, "A") == 0) {
Iain Merrick75681382010-08-19 15:07:18 +0100800 CHECK(IsNodeRetainedAs(node, v8::HeapGraphEdge::kProperty, "a"));
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100801 CHECK(!found_A);
802 found_A = true;
803 s1_A_id = node->GetId();
804 } else if (strcmp(*node_name, "B") == 0) {
Iain Merrick75681382010-08-19 15:07:18 +0100805 CHECK(IsNodeRetainedAs(node, v8::HeapGraphEdge::kProperty, "b2"));
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100806 CHECK(!found_B);
807 found_B = true;
808 }
809 }
810 }
811 CHECK(found_A);
812 CHECK(found_B);
813
814 // Verify deletions: ensure that deletion of A was detected.
815 const v8::HeapGraphNode* deletions_root = diff->GetDeletionsRoot();
816 bool found_A_del = false;
817 uint64_t s2_A_id = 0;
818 for (int i = 0, count = deletions_root->GetChildrenCount(); i < count; ++i) {
819 const v8::HeapGraphEdge* prop = deletions_root->GetChild(i);
820 const v8::HeapGraphNode* node = prop->GetToNode();
Iain Merrick75681382010-08-19 15:07:18 +0100821 if (node->GetType() == v8::HeapGraphNode::kObject) {
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100822 v8::String::AsciiValue node_name(node->GetName());
823 if (strcmp(*node_name, "A") == 0) {
Iain Merrick75681382010-08-19 15:07:18 +0100824 CHECK(IsNodeRetainedAs(node, v8::HeapGraphEdge::kProperty, "a"));
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100825 CHECK(!found_A_del);
826 found_A_del = true;
827 s2_A_id = node->GetId();
828 }
829 }
830 }
831 CHECK(found_A_del);
832 CHECK_NE_UINT64_T(0, s1_A_id);
833 CHECK(s1_A_id != s2_A_id);
834}
835
Iain Merrick75681382010-08-19 15:07:18 +0100836
837namespace v8 {
838namespace internal {
839
840class HeapSnapshotTester {
841 public:
842 static int CalculateNetworkSize(JSObject* obj) {
843 return HeapSnapshot::CalculateNetworkSize(obj);
844 }
845};
846
847} } // namespace v8::internal
848
849// http://code.google.com/p/v8/issues/detail?id=822
850// Trying to call CalculateNetworkSize on an object with elements set
851// to non-FixedArray may cause an assertion error in debug builds.
852TEST(Issue822) {
853 v8::HandleScope scope;
854 LocalContext context;
855 const int kElementCount = 260;
856 uint8_t* pixel_data = reinterpret_cast<uint8_t*>(malloc(kElementCount));
857 i::Handle<i::PixelArray> pixels = i::Factory::NewPixelArray(kElementCount,
858 pixel_data);
859 v8::Handle<v8::Object> obj = v8::Object::New();
860 // Set the elements to be the pixels.
861 obj->SetIndexedPropertiesToPixelData(pixel_data, kElementCount);
862 i::Handle<i::JSObject> jsobj = v8::Utils::OpenHandle(*obj);
863 // This call must not cause an assertion error in debug builds.
864 i::HeapSnapshotTester::CalculateNetworkSize(*jsobj);
865}
866
Steve Blocka7e24c12009-10-30 11:49:00 +0000867#endif // ENABLE_LOGGING_AND_PROFILING