Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // 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 Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 9 | #include "snapshot.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 10 | #include "string-stream.h" |
| 11 | #include "cctest.h" |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 12 | #include "zone-inl.h" |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 13 | #include "../include/v8-profiler.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 14 | |
| 15 | namespace i = v8::internal; |
| 16 | using i::ClustersCoarser; |
| 17 | using i::JSObjectsCluster; |
| 18 | using i::JSObjectsRetainerTree; |
| 19 | using i::JSObjectsClusterTree; |
| 20 | using i::RetainerHeapProfile; |
| 21 | |
| 22 | |
| 23 | static void CompileAndRunScript(const char *src) { |
| 24 | v8::Script::Compile(v8::String::New(src))->Run(); |
| 25 | } |
| 26 | |
| 27 | |
| 28 | namespace { |
| 29 | |
| 30 | class 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 | |
| 57 | TEST(ConstructorProfile) { |
| 58 | v8::HandleScope scope; |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame^] | 59 | LocalContext env; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 60 | |
| 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 Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 69 | for (i::HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 70 | cons_profile.CollectStats(obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 71 | CHECK_EQ(0, cons_profile.f_count()); |
| 72 | cons_profile.PrintStats(); |
| 73 | CHECK_EQ(2, cons_profile.f_count()); |
| 74 | } |
| 75 | |
| 76 | |
| 77 | static 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 | |
| 96 | static 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 | |
| 106 | static 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 | |
| 125 | static 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 | |
| 144 | TEST(ClustersCoarserSimple) { |
| 145 | v8::HandleScope scope; |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame^] | 146 | LocalContext env; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 147 | |
| 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 | |
| 182 | TEST(ClustersCoarserMultipleConstructors) { |
| 183 | v8::HandleScope scope; |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame^] | 184 | LocalContext env; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 185 | |
| 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 | |
| 212 | TEST(ClustersCoarserPathsTraversal) { |
| 213 | v8::HandleScope scope; |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame^] | 214 | LocalContext env; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 215 | |
| 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 | |
| 264 | TEST(ClustersCoarserSelf) { |
| 265 | v8::HandleScope scope; |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame^] | 266 | LocalContext env; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 267 | |
| 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 | |
| 311 | namespace { |
| 312 | |
| 313 | class 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 | |
| 358 | TEST(RetainerProfile) { |
| 359 | v8::HandleScope scope; |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame^] | 360 | LocalContext env; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 361 | |
| 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 Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 373 | for (i::HeapObject* obj = iterator.next(); obj != NULL; obj = iterator.next()) |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 374 | ret_profile.CollectStats(obj); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 375 | 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 Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 380 | CHECK_EQ(i::StrLength("(global property);1,B;2,C;2"), |
| 381 | i::StrLength(retainers_of_a)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 382 | 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 Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 389 | |
| 390 | namespace { |
| 391 | |
| 392 | class 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 | |
| 399 | void Apply(i::HeapEntry* entry) { |
| 400 | const char* node_name = entry->name(); |
| 401 | if (strcmp("A1", node_name) == 0 |
| 402 | && entry->GetRetainingPaths()->length() > 0) has_A1 = true; |
| 403 | if (strcmp("B1", node_name) == 0 |
| 404 | && entry->GetRetainingPaths()->length() > 0) has_B1 = true; |
| 405 | if (strcmp("C1", node_name) == 0 |
| 406 | && entry->GetRetainingPaths()->length() > 0) has_C1 = true; |
| 407 | if (strcmp("A2", node_name) == 0 |
| 408 | && entry->GetRetainingPaths()->length() > 0) has_A2 = true; |
| 409 | if (strcmp("B2", node_name) == 0 |
| 410 | && entry->GetRetainingPaths()->length() > 0) has_B2 = true; |
| 411 | if (strcmp("C2", node_name) == 0 |
| 412 | && entry->GetRetainingPaths()->length() > 0) has_C2 = true; |
| 413 | } |
| 414 | |
| 415 | bool has_A1; |
| 416 | bool has_B1; |
| 417 | bool has_C1; |
| 418 | bool has_A2; |
| 419 | bool has_B2; |
| 420 | bool has_C2; |
| 421 | }; |
| 422 | |
| 423 | } // namespace |
| 424 | |
| 425 | |
| 426 | static const v8::HeapGraphNode* GetGlobalObject( |
| 427 | const v8::HeapSnapshot* snapshot) { |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame^] | 428 | CHECK_EQ(1, snapshot->GetRoot()->GetChildrenCount()); |
| 429 | return snapshot->GetRoot()->GetChild(0)->GetToNode(); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | |
| 433 | static const v8::HeapGraphNode* GetProperty(const v8::HeapGraphNode* node, |
| 434 | v8::HeapGraphEdge::Type type, |
| 435 | const char* name) { |
| 436 | for (int i = 0, count = node->GetChildrenCount(); i < count; ++i) { |
| 437 | const v8::HeapGraphEdge* prop = node->GetChild(i); |
| 438 | v8::String::AsciiValue prop_name(prop->GetName()); |
| 439 | if (prop->GetType() == type && strcmp(name, *prop_name) == 0) |
| 440 | return prop->GetToNode(); |
| 441 | } |
| 442 | return NULL; |
| 443 | } |
| 444 | |
| 445 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame^] | 446 | static bool IsNodeRetainedAs(const v8::HeapGraphNode* node, |
| 447 | v8::HeapGraphEdge::Type type, |
| 448 | const char* name) { |
| 449 | for (int i = 0, count = node->GetRetainersCount(); i < count; ++i) { |
| 450 | const v8::HeapGraphEdge* prop = node->GetRetainer(i); |
| 451 | v8::String::AsciiValue prop_name(prop->GetName()); |
| 452 | if (prop->GetType() == type && strcmp(name, *prop_name) == 0) |
| 453 | return true; |
| 454 | } |
| 455 | return false; |
| 456 | } |
| 457 | |
| 458 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 459 | static bool HasString(const v8::HeapGraphNode* node, const char* contents) { |
| 460 | for (int i = 0, count = node->GetChildrenCount(); i < count; ++i) { |
| 461 | const v8::HeapGraphEdge* prop = node->GetChild(i); |
| 462 | const v8::HeapGraphNode* node = prop->GetToNode(); |
| 463 | if (node->GetType() == v8::HeapGraphNode::STRING) { |
| 464 | v8::String::AsciiValue node_name(node->GetName()); |
| 465 | if (strcmp(contents, *node_name) == 0) return true; |
| 466 | } |
| 467 | } |
| 468 | return false; |
| 469 | } |
| 470 | |
| 471 | |
| 472 | TEST(HeapSnapshot) { |
| 473 | v8::HandleScope scope; |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 474 | v8::Handle<v8::String> token1 = v8::String::New("token1"); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame^] | 475 | LocalContext env1; |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 476 | env1->SetSecurityToken(token1); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 477 | |
| 478 | CompileAndRunScript( |
| 479 | "function A1() {}\n" |
| 480 | "function B1(x) { this.x = x; }\n" |
| 481 | "function C1(x) { this.x1 = x; this.x2 = x; }\n" |
| 482 | "var a1 = new A1();\n" |
| 483 | "var b1_1 = new B1(a1), b1_2 = new B1(a1);\n" |
| 484 | "var c1 = new C1(a1);"); |
| 485 | |
| 486 | v8::Handle<v8::String> token2 = v8::String::New("token2"); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame^] | 487 | LocalContext env2; |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 488 | env2->SetSecurityToken(token2); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 489 | |
| 490 | CompileAndRunScript( |
| 491 | "function A2() {}\n" |
| 492 | "function B2(x) { return function() { return typeof x; }; }\n" |
| 493 | "function C2(x) { this.x1 = x; this.x2 = x; this[1] = x; }\n" |
| 494 | "var a2 = new A2();\n" |
| 495 | "var b2_1 = new B2(a2), b2_2 = new B2(a2);\n" |
| 496 | "var c2 = new C2(a2);"); |
| 497 | const v8::HeapSnapshot* snapshot_env2 = |
| 498 | v8::HeapProfiler::TakeSnapshot(v8::String::New("env2")); |
| 499 | const v8::HeapGraphNode* global_env2 = GetGlobalObject(snapshot_env2); |
| 500 | |
| 501 | // Verify, that JS global object of env2 doesn't have '..1' |
| 502 | // properties, but has '..2' properties. |
| 503 | CHECK_EQ(NULL, GetProperty(global_env2, v8::HeapGraphEdge::PROPERTY, "a1")); |
| 504 | CHECK_EQ(NULL, GetProperty(global_env2, v8::HeapGraphEdge::PROPERTY, "b1_1")); |
| 505 | CHECK_EQ(NULL, GetProperty(global_env2, v8::HeapGraphEdge::PROPERTY, "b1_2")); |
| 506 | CHECK_EQ(NULL, GetProperty(global_env2, v8::HeapGraphEdge::PROPERTY, "c1")); |
| 507 | const v8::HeapGraphNode* a2_node = |
| 508 | GetProperty(global_env2, v8::HeapGraphEdge::PROPERTY, "a2"); |
| 509 | CHECK_NE(NULL, a2_node); |
| 510 | CHECK_NE(NULL, GetProperty(global_env2, v8::HeapGraphEdge::PROPERTY, "b2_1")); |
| 511 | CHECK_NE(NULL, GetProperty(global_env2, v8::HeapGraphEdge::PROPERTY, "b2_2")); |
| 512 | CHECK_NE(NULL, GetProperty(global_env2, v8::HeapGraphEdge::PROPERTY, "c2")); |
| 513 | |
| 514 | // Verify that anything related to '[ABC]1' is not reachable. |
| 515 | NamedEntriesDetector det; |
| 516 | i::HeapSnapshot* i_snapshot_env2 = |
| 517 | const_cast<i::HeapSnapshot*>( |
| 518 | reinterpret_cast<const i::HeapSnapshot*>(snapshot_env2)); |
| 519 | i_snapshot_env2->IterateEntries(&det); |
| 520 | CHECK(!det.has_A1); |
| 521 | CHECK(!det.has_B1); |
| 522 | CHECK(!det.has_C1); |
| 523 | CHECK(det.has_A2); |
| 524 | CHECK(det.has_B2); |
| 525 | CHECK(det.has_C2); |
| 526 | |
| 527 | // Verify 'a2' object retainers. They are: |
| 528 | // - (global object).a2 |
| 529 | // - c2.x1, c2.x2, c2[1] |
| 530 | // - b2_1 and b2_2 closures: via 'x' variable |
| 531 | CHECK_EQ(6, a2_node->GetRetainingPathsCount()); |
| 532 | bool has_global_obj_a2_ref = false; |
| 533 | bool has_c2_x1_ref = false, has_c2_x2_ref = false, has_c2_1_ref = false; |
| 534 | bool has_b2_1_x_ref = false, has_b2_2_x_ref = false; |
| 535 | for (int i = 0; i < a2_node->GetRetainingPathsCount(); ++i) { |
| 536 | const v8::HeapGraphPath* path = a2_node->GetRetainingPath(i); |
| 537 | const int edges_count = path->GetEdgesCount(); |
| 538 | CHECK_GT(edges_count, 0); |
| 539 | const v8::HeapGraphEdge* last_edge = path->GetEdge(edges_count - 1); |
| 540 | v8::String::AsciiValue last_edge_name(last_edge->GetName()); |
| 541 | if (strcmp("a2", *last_edge_name) == 0 |
| 542 | && last_edge->GetType() == v8::HeapGraphEdge::PROPERTY) { |
| 543 | has_global_obj_a2_ref = true; |
| 544 | continue; |
| 545 | } |
| 546 | CHECK_GT(edges_count, 1); |
| 547 | const v8::HeapGraphEdge* prev_edge = path->GetEdge(edges_count - 2); |
| 548 | v8::String::AsciiValue prev_edge_name(prev_edge->GetName()); |
| 549 | if (strcmp("x1", *last_edge_name) == 0 |
| 550 | && last_edge->GetType() == v8::HeapGraphEdge::PROPERTY |
| 551 | && strcmp("c2", *prev_edge_name) == 0) has_c2_x1_ref = true; |
| 552 | if (strcmp("x2", *last_edge_name) == 0 |
| 553 | && last_edge->GetType() == v8::HeapGraphEdge::PROPERTY |
| 554 | && strcmp("c2", *prev_edge_name) == 0) has_c2_x2_ref = true; |
| 555 | if (strcmp("1", *last_edge_name) == 0 |
| 556 | && last_edge->GetType() == v8::HeapGraphEdge::ELEMENT |
| 557 | && strcmp("c2", *prev_edge_name) == 0) has_c2_1_ref = true; |
| 558 | if (strcmp("x", *last_edge_name) == 0 |
| 559 | && last_edge->GetType() == v8::HeapGraphEdge::CONTEXT_VARIABLE |
| 560 | && strcmp("b2_1", *prev_edge_name) == 0) has_b2_1_x_ref = true; |
| 561 | if (strcmp("x", *last_edge_name) == 0 |
| 562 | && last_edge->GetType() == v8::HeapGraphEdge::CONTEXT_VARIABLE |
| 563 | && strcmp("b2_2", *prev_edge_name) == 0) has_b2_2_x_ref = true; |
| 564 | } |
| 565 | CHECK(has_global_obj_a2_ref); |
| 566 | CHECK(has_c2_x1_ref); |
| 567 | CHECK(has_c2_x2_ref); |
| 568 | CHECK(has_c2_1_ref); |
| 569 | CHECK(has_b2_1_x_ref); |
| 570 | CHECK(has_b2_2_x_ref); |
| 571 | } |
| 572 | |
| 573 | |
| 574 | TEST(HeapSnapshotCodeObjects) { |
| 575 | v8::HandleScope scope; |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame^] | 576 | LocalContext env; |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 577 | |
| 578 | CompileAndRunScript( |
| 579 | "function lazy(x) { return x - 1; }\n" |
| 580 | "function compiled(x) { return x + 1; }\n" |
| 581 | "compiled(1)"); |
| 582 | const v8::HeapSnapshot* snapshot = |
| 583 | v8::HeapProfiler::TakeSnapshot(v8::String::New("code")); |
| 584 | |
| 585 | const v8::HeapGraphNode* global = GetGlobalObject(snapshot); |
| 586 | const v8::HeapGraphNode* compiled = |
| 587 | GetProperty(global, v8::HeapGraphEdge::PROPERTY, "compiled"); |
| 588 | CHECK_NE(NULL, compiled); |
| 589 | CHECK_EQ(v8::HeapGraphNode::CLOSURE, compiled->GetType()); |
| 590 | const v8::HeapGraphNode* lazy = |
| 591 | GetProperty(global, v8::HeapGraphEdge::PROPERTY, "lazy"); |
| 592 | CHECK_NE(NULL, lazy); |
| 593 | CHECK_EQ(v8::HeapGraphNode::CLOSURE, lazy->GetType()); |
| 594 | |
| 595 | // Find references to code. |
| 596 | const v8::HeapGraphNode* compiled_code = |
| 597 | GetProperty(compiled, v8::HeapGraphEdge::INTERNAL, "code"); |
| 598 | CHECK_NE(NULL, compiled_code); |
| 599 | const v8::HeapGraphNode* lazy_code = |
| 600 | GetProperty(lazy, v8::HeapGraphEdge::INTERNAL, "code"); |
| 601 | CHECK_NE(NULL, lazy_code); |
| 602 | |
| 603 | // Verify that non-compiled code doesn't contain references to "x" |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame^] | 604 | // literal, while compiled code does. The scope info is stored in FixedArray |
| 605 | // objects attached to the SharedFunctionInfo. |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 606 | bool compiled_references_x = false, lazy_references_x = false; |
| 607 | for (int i = 0, count = compiled_code->GetChildrenCount(); i < count; ++i) { |
| 608 | const v8::HeapGraphEdge* prop = compiled_code->GetChild(i); |
| 609 | const v8::HeapGraphNode* node = prop->GetToNode(); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame^] | 610 | if (node->GetType() == v8::HeapGraphNode::ARRAY) { |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 611 | if (HasString(node, "x")) { |
| 612 | compiled_references_x = true; |
| 613 | break; |
| 614 | } |
| 615 | } |
| 616 | } |
| 617 | for (int i = 0, count = lazy_code->GetChildrenCount(); i < count; ++i) { |
| 618 | const v8::HeapGraphEdge* prop = lazy_code->GetChild(i); |
| 619 | const v8::HeapGraphNode* node = prop->GetToNode(); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame^] | 620 | if (node->GetType() == v8::HeapGraphNode::ARRAY) { |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 621 | if (HasString(node, "x")) { |
| 622 | lazy_references_x = true; |
| 623 | break; |
| 624 | } |
| 625 | } |
| 626 | } |
| 627 | CHECK(compiled_references_x); |
| 628 | CHECK(!lazy_references_x); |
| 629 | } |
| 630 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame^] | 631 | |
| 632 | // Trying to introduce a check helper for uint64_t causes many |
| 633 | // overloading ambiguities, so it seems easier just to cast |
| 634 | // them to a signed type. |
| 635 | #define CHECK_EQ_UINT64_T(a, b) \ |
| 636 | CHECK_EQ(static_cast<int64_t>(a), static_cast<int64_t>(b)) |
| 637 | #define CHECK_NE_UINT64_T(a, b) do \ |
| 638 | { \ |
| 639 | bool ne = a != b; \ |
| 640 | CHECK(ne); \ |
| 641 | } while (false) |
| 642 | |
| 643 | TEST(HeapEntryIdsAndGC) { |
| 644 | v8::HandleScope scope; |
| 645 | LocalContext env; |
| 646 | |
| 647 | CompileAndRunScript( |
| 648 | "function A() {}\n" |
| 649 | "function B(x) { this.x = x; }\n" |
| 650 | "var a = new A();\n" |
| 651 | "var b = new B(a);"); |
| 652 | const v8::HeapSnapshot* snapshot1 = |
| 653 | v8::HeapProfiler::TakeSnapshot(v8::String::New("s1")); |
| 654 | |
| 655 | i::Heap::CollectAllGarbage(true); // Enforce compaction. |
| 656 | |
| 657 | const v8::HeapSnapshot* snapshot2 = |
| 658 | v8::HeapProfiler::TakeSnapshot(v8::String::New("s2")); |
| 659 | |
| 660 | const v8::HeapGraphNode* global1 = GetGlobalObject(snapshot1); |
| 661 | const v8::HeapGraphNode* global2 = GetGlobalObject(snapshot2); |
| 662 | CHECK_NE_UINT64_T(0, global1->GetId()); |
| 663 | CHECK_EQ_UINT64_T(global1->GetId(), global2->GetId()); |
| 664 | const v8::HeapGraphNode* A1 = |
| 665 | GetProperty(global1, v8::HeapGraphEdge::PROPERTY, "A"); |
| 666 | const v8::HeapGraphNode* A2 = |
| 667 | GetProperty(global2, v8::HeapGraphEdge::PROPERTY, "A"); |
| 668 | CHECK_NE_UINT64_T(0, A1->GetId()); |
| 669 | CHECK_EQ_UINT64_T(A1->GetId(), A2->GetId()); |
| 670 | const v8::HeapGraphNode* B1 = |
| 671 | GetProperty(global1, v8::HeapGraphEdge::PROPERTY, "B"); |
| 672 | const v8::HeapGraphNode* B2 = |
| 673 | GetProperty(global2, v8::HeapGraphEdge::PROPERTY, "B"); |
| 674 | CHECK_NE_UINT64_T(0, B1->GetId()); |
| 675 | CHECK_EQ_UINT64_T(B1->GetId(), B2->GetId()); |
| 676 | const v8::HeapGraphNode* a1 = |
| 677 | GetProperty(global1, v8::HeapGraphEdge::PROPERTY, "a"); |
| 678 | const v8::HeapGraphNode* a2 = |
| 679 | GetProperty(global2, v8::HeapGraphEdge::PROPERTY, "a"); |
| 680 | CHECK_NE_UINT64_T(0, a1->GetId()); |
| 681 | CHECK_EQ_UINT64_T(a1->GetId(), a2->GetId()); |
| 682 | const v8::HeapGraphNode* b1 = |
| 683 | GetProperty(global1, v8::HeapGraphEdge::PROPERTY, "b"); |
| 684 | const v8::HeapGraphNode* b2 = |
| 685 | GetProperty(global2, v8::HeapGraphEdge::PROPERTY, "b"); |
| 686 | CHECK_NE_UINT64_T(0, b1->GetId()); |
| 687 | CHECK_EQ_UINT64_T(b1->GetId(), b2->GetId()); |
| 688 | } |
| 689 | |
| 690 | |
| 691 | TEST(HeapSnapshotsDiff) { |
| 692 | v8::HandleScope scope; |
| 693 | LocalContext env; |
| 694 | |
| 695 | CompileAndRunScript( |
| 696 | "function A() {}\n" |
| 697 | "function B(x) { this.x = x; }\n" |
| 698 | "var a = new A();\n" |
| 699 | "var b = new B(a);"); |
| 700 | const v8::HeapSnapshot* snapshot1 = |
| 701 | v8::HeapProfiler::TakeSnapshot(v8::String::New("s1")); |
| 702 | |
| 703 | CompileAndRunScript( |
| 704 | "delete a;\n" |
| 705 | "b.x = null;\n" |
| 706 | "var a = new A();\n" |
| 707 | "var b2 = new B(a);"); |
| 708 | const v8::HeapSnapshot* snapshot2 = |
| 709 | v8::HeapProfiler::TakeSnapshot(v8::String::New("s2")); |
| 710 | |
| 711 | const v8::HeapSnapshotsDiff* diff = snapshot1->CompareWith(snapshot2); |
| 712 | |
| 713 | // Verify additions: ensure that addition of A and B was detected. |
| 714 | const v8::HeapGraphNode* additions_root = diff->GetAdditionsRoot(); |
| 715 | bool found_A = false, found_B = false; |
| 716 | uint64_t s1_A_id = 0; |
| 717 | for (int i = 0, count = additions_root->GetChildrenCount(); i < count; ++i) { |
| 718 | const v8::HeapGraphEdge* prop = additions_root->GetChild(i); |
| 719 | const v8::HeapGraphNode* node = prop->GetToNode(); |
| 720 | if (node->GetType() == v8::HeapGraphNode::OBJECT) { |
| 721 | v8::String::AsciiValue node_name(node->GetName()); |
| 722 | if (strcmp(*node_name, "A") == 0) { |
| 723 | CHECK(IsNodeRetainedAs(node, v8::HeapGraphEdge::PROPERTY, "a")); |
| 724 | CHECK(!found_A); |
| 725 | found_A = true; |
| 726 | s1_A_id = node->GetId(); |
| 727 | } else if (strcmp(*node_name, "B") == 0) { |
| 728 | CHECK(IsNodeRetainedAs(node, v8::HeapGraphEdge::PROPERTY, "b2")); |
| 729 | CHECK(!found_B); |
| 730 | found_B = true; |
| 731 | } |
| 732 | } |
| 733 | } |
| 734 | CHECK(found_A); |
| 735 | CHECK(found_B); |
| 736 | |
| 737 | // Verify deletions: ensure that deletion of A was detected. |
| 738 | const v8::HeapGraphNode* deletions_root = diff->GetDeletionsRoot(); |
| 739 | bool found_A_del = false; |
| 740 | uint64_t s2_A_id = 0; |
| 741 | for (int i = 0, count = deletions_root->GetChildrenCount(); i < count; ++i) { |
| 742 | const v8::HeapGraphEdge* prop = deletions_root->GetChild(i); |
| 743 | const v8::HeapGraphNode* node = prop->GetToNode(); |
| 744 | if (node->GetType() == v8::HeapGraphNode::OBJECT) { |
| 745 | v8::String::AsciiValue node_name(node->GetName()); |
| 746 | if (strcmp(*node_name, "A") == 0) { |
| 747 | CHECK(IsNodeRetainedAs(node, v8::HeapGraphEdge::PROPERTY, "a")); |
| 748 | CHECK(!found_A_del); |
| 749 | found_A_del = true; |
| 750 | s2_A_id = node->GetId(); |
| 751 | } |
| 752 | } |
| 753 | } |
| 754 | CHECK(found_A_del); |
| 755 | CHECK_NE_UINT64_T(0, s1_A_id); |
| 756 | CHECK(s1_A_id != s2_A_id); |
| 757 | } |
| 758 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 759 | #endif // ENABLE_LOGGING_AND_PROFILING |