| OLD | NEW |
| 1 // Copyright 2009 the V8 project authors. All rights reserved. | 1 // Copyright 2009 the V8 project authors. All rights reserved. |
| 2 // | 2 // |
| 3 // Tests for heap profiler | 3 // Tests for heap profiler |
| 4 | 4 |
| 5 #ifdef ENABLE_LOGGING_AND_PROFILING | 5 #ifdef ENABLE_LOGGING_AND_PROFILING |
| 6 | 6 |
| 7 #include "v8.h" | 7 #include "v8.h" |
| 8 #include "heap-profiler.h" | 8 #include "heap-profiler.h" |
| 9 #include "string-stream.h" | 9 #include "string-stream.h" |
| 10 #include "cctest.h" | 10 #include "cctest.h" |
| 11 | 11 |
| 12 namespace i = v8::internal; | 12 namespace i = v8::internal; |
| 13 using i::ClustersCoarser; | 13 using i::ClustersCoarser; |
| 14 using i::JSObjectsCluster; | 14 using i::JSObjectsCluster; |
| 15 using i::JSObjectsRetainerTree; |
| 15 using i::JSObjectsClusterTree; | 16 using i::JSObjectsClusterTree; |
| 16 using i::RetainerHeapProfile; | 17 using i::RetainerHeapProfile; |
| 17 | 18 |
| 18 | 19 |
| 19 static void CompileAndRunScript(const char *src) { | 20 static void CompileAndRunScript(const char *src) { |
| 20 v8::Script::Compile(v8::String::New(src))->Run(); | 21 v8::Script::Compile(v8::String::New(src))->Run(); |
| 21 } | 22 } |
| 22 | 23 |
| 23 | 24 |
| 24 namespace { | 25 namespace { |
| 25 | 26 |
| 26 class ConstructorHeapProfileTestHelper : public i::ConstructorHeapProfile { | 27 class ConstructorHeapProfileTestHelper : public i::ConstructorHeapProfile { |
| 27 public: | 28 public: |
| 28 ConstructorHeapProfileTestHelper() | 29 ConstructorHeapProfileTestHelper() |
| 29 : i::ConstructorHeapProfile(), | 30 : i::ConstructorHeapProfile(), |
| 30 f_name_(i::Factory::NewStringFromAscii(i::CStrVector("F"))), | 31 f_name_(i::Factory::NewStringFromAscii(i::CStrVector("F"))), |
| 31 f_count_(0) { | 32 f_count_(0) { |
| 32 } | 33 } |
| 33 | 34 |
| 34 void Call(i::String* name, const i::NumberAndSizeInfo& number_and_size) { | 35 void Call(const JSObjectsCluster& cluster, |
| 35 CHECK(name != NULL); | 36 const i::NumberAndSizeInfo& number_and_size) { |
| 36 if (f_name_->Equals(name)) { | 37 if (f_name_->Equals(cluster.constructor())) { |
| 37 CHECK_EQ(f_count_, 0); | 38 CHECK_EQ(f_count_, 0); |
| 38 f_count_ = number_and_size.number(); | 39 f_count_ = number_and_size.number(); |
| 39 CHECK_GT(f_count_, 0); | 40 CHECK_GT(f_count_, 0); |
| 40 } | 41 } |
| 41 } | 42 } |
| 42 | 43 |
| 43 int f_count() { return f_count_; } | 44 int f_count() { return f_count_; } |
| 44 | 45 |
| 45 private: | 46 private: |
| 46 i::Handle<i::String> f_name_; | 47 i::Handle<i::String> f_name_; |
| (...skipping 20 matching lines...) Expand all Loading... |
| 67 i::HeapObject* obj = iterator.next(); | 68 i::HeapObject* obj = iterator.next(); |
| 68 cons_profile.CollectStats(obj); | 69 cons_profile.CollectStats(obj); |
| 69 } | 70 } |
| 70 CHECK_EQ(0, cons_profile.f_count()); | 71 CHECK_EQ(0, cons_profile.f_count()); |
| 71 cons_profile.PrintStats(); | 72 cons_profile.PrintStats(); |
| 72 CHECK_EQ(2, cons_profile.f_count()); | 73 CHECK_EQ(2, cons_profile.f_count()); |
| 73 } | 74 } |
| 74 | 75 |
| 75 | 76 |
| 76 static JSObjectsCluster AddHeapObjectToTree( | 77 static JSObjectsCluster AddHeapObjectToTree( |
| 77 JSObjectsClusterTree* tree, | 78 JSObjectsRetainerTree* tree, |
| 78 i::String* constructor, | 79 i::String* constructor, |
| 79 int instance, | 80 int instance, |
| 80 JSObjectsCluster* ref1 = NULL, | 81 JSObjectsCluster* ref1 = NULL, |
| 81 JSObjectsCluster* ref2 = NULL, | 82 JSObjectsCluster* ref2 = NULL, |
| 82 JSObjectsCluster* ref3 = NULL) { | 83 JSObjectsCluster* ref3 = NULL) { |
| 83 JSObjectsCluster o(constructor, reinterpret_cast<i::Object*>(instance)); | 84 JSObjectsCluster o(constructor, reinterpret_cast<i::Object*>(instance)); |
| 84 JSObjectsClusterTree* o_tree = new JSObjectsClusterTree(); | 85 JSObjectsClusterTree* o_tree = new JSObjectsClusterTree(); |
| 85 JSObjectsClusterTree::Locator loc; | 86 JSObjectsClusterTree::Locator o_loc; |
| 86 if (ref1 != NULL) o_tree->Insert(*ref1, &loc); | 87 if (ref1 != NULL) o_tree->Insert(*ref1, &o_loc); |
| 87 if (ref2 != NULL) o_tree->Insert(*ref2, &loc); | 88 if (ref2 != NULL) o_tree->Insert(*ref2, &o_loc); |
| 88 if (ref3 != NULL) o_tree->Insert(*ref3, &loc); | 89 if (ref3 != NULL) o_tree->Insert(*ref3, &o_loc); |
| 90 JSObjectsRetainerTree::Locator loc; |
| 89 tree->Insert(o, &loc); | 91 tree->Insert(o, &loc); |
| 90 loc.set_value(o_tree); | 92 loc.set_value(o_tree); |
| 91 return o; | 93 return o; |
| 92 } | 94 } |
| 93 | 95 |
| 94 | 96 |
| 95 static inline void CheckEqualsHelper(const char* file, int line, | 97 static inline void CheckEqualsHelper(const char* file, int line, |
| 96 const char* expected_source, | 98 const char* expected_source, |
| 97 const JSObjectsCluster& expected, | 99 const JSObjectsCluster& expected, |
| 98 const char* value_source, | 100 const char* value_source, |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 130 } | 132 } |
| 131 | 133 |
| 132 | 134 |
| 133 TEST(ClustersCoarserSimple) { | 135 TEST(ClustersCoarserSimple) { |
| 134 v8::HandleScope scope; | 136 v8::HandleScope scope; |
| 135 v8::Handle<v8::Context> env = v8::Context::New(); | 137 v8::Handle<v8::Context> env = v8::Context::New(); |
| 136 env->Enter(); | 138 env->Enter(); |
| 137 | 139 |
| 138 i::ZoneScope zn_scope(i::DELETE_ON_EXIT); | 140 i::ZoneScope zn_scope(i::DELETE_ON_EXIT); |
| 139 | 141 |
| 140 JSObjectsClusterTree tree; | 142 JSObjectsRetainerTree tree; |
| 141 JSObjectsCluster function(i::Heap::function_class_symbol()); | 143 JSObjectsCluster function(i::Heap::function_class_symbol()); |
| 142 JSObjectsCluster a(*i::Factory::NewStringFromAscii(i::CStrVector("A"))); | 144 JSObjectsCluster a(*i::Factory::NewStringFromAscii(i::CStrVector("A"))); |
| 143 JSObjectsCluster b(*i::Factory::NewStringFromAscii(i::CStrVector("B"))); | 145 JSObjectsCluster b(*i::Factory::NewStringFromAscii(i::CStrVector("B"))); |
| 144 | 146 |
| 145 // o1 <- Function | 147 // o1 <- Function |
| 146 JSObjectsCluster o1 = | 148 JSObjectsCluster o1 = |
| 147 AddHeapObjectToTree(&tree, i::Heap::Object_symbol(), 0x100, &function); | 149 AddHeapObjectToTree(&tree, i::Heap::Object_symbol(), 0x100, &function); |
| 148 // o2 <- Function | 150 // o2 <- Function |
| 149 JSObjectsCluster o2 = | 151 JSObjectsCluster o2 = |
| 150 AddHeapObjectToTree(&tree, i::Heap::Object_symbol(), 0x200, &function); | 152 AddHeapObjectToTree(&tree, i::Heap::Object_symbol(), 0x200, &function); |
| (...skipping 18 matching lines...) Expand all Loading... |
| 169 } | 171 } |
| 170 | 172 |
| 171 | 173 |
| 172 TEST(ClustersCoarserMultipleConstructors) { | 174 TEST(ClustersCoarserMultipleConstructors) { |
| 173 v8::HandleScope scope; | 175 v8::HandleScope scope; |
| 174 v8::Handle<v8::Context> env = v8::Context::New(); | 176 v8::Handle<v8::Context> env = v8::Context::New(); |
| 175 env->Enter(); | 177 env->Enter(); |
| 176 | 178 |
| 177 i::ZoneScope zn_scope(i::DELETE_ON_EXIT); | 179 i::ZoneScope zn_scope(i::DELETE_ON_EXIT); |
| 178 | 180 |
| 179 JSObjectsClusterTree tree; | 181 JSObjectsRetainerTree tree; |
| 180 JSObjectsCluster function(i::Heap::function_class_symbol()); | 182 JSObjectsCluster function(i::Heap::function_class_symbol()); |
| 181 | 183 |
| 182 // o1 <- Function | 184 // o1 <- Function |
| 183 JSObjectsCluster o1 = | 185 JSObjectsCluster o1 = |
| 184 AddHeapObjectToTree(&tree, i::Heap::Object_symbol(), 0x100, &function); | 186 AddHeapObjectToTree(&tree, i::Heap::Object_symbol(), 0x100, &function); |
| 185 // a1 <- Function | 187 // a1 <- Function |
| 186 JSObjectsCluster a1 = | 188 JSObjectsCluster a1 = |
| 187 AddHeapObjectToTree(&tree, i::Heap::Array_symbol(), 0x1000, &function); | 189 AddHeapObjectToTree(&tree, i::Heap::Array_symbol(), 0x1000, &function); |
| 188 // o2 <- Function | 190 // o2 <- Function |
| 189 JSObjectsCluster o2 = | 191 JSObjectsCluster o2 = |
| (...skipping 10 matching lines...) Expand all Loading... |
| 200 } | 202 } |
| 201 | 203 |
| 202 | 204 |
| 203 TEST(ClustersCoarserPathsTraversal) { | 205 TEST(ClustersCoarserPathsTraversal) { |
| 204 v8::HandleScope scope; | 206 v8::HandleScope scope; |
| 205 v8::Handle<v8::Context> env = v8::Context::New(); | 207 v8::Handle<v8::Context> env = v8::Context::New(); |
| 206 env->Enter(); | 208 env->Enter(); |
| 207 | 209 |
| 208 i::ZoneScope zn_scope(i::DELETE_ON_EXIT); | 210 i::ZoneScope zn_scope(i::DELETE_ON_EXIT); |
| 209 | 211 |
| 210 JSObjectsClusterTree tree; | 212 JSObjectsRetainerTree tree; |
| 211 | 213 |
| 212 // On the following graph: | 214 // On the following graph: |
| 213 // | 215 // |
| 214 // p | 216 // p |
| 215 // <- o21 <- o11 <- | 217 // <- o21 <- o11 <- |
| 216 // q o | 218 // q o |
| 217 // <- o22 <- o12 <- | 219 // <- o22 <- o12 <- |
| 218 // r | 220 // r |
| 219 // | 221 // |
| 220 // we expect that coarser will deduce equivalences: p ~ q ~ r, | 222 // we expect that coarser will deduce equivalences: p ~ q ~ r, |
| (...skipping 29 matching lines...) Expand all Loading... |
| 250 CHECK_NE(coarser.GetCoarseEquivalent(o21), coarser.GetCoarseEquivalent(p)); | 252 CHECK_NE(coarser.GetCoarseEquivalent(o21), coarser.GetCoarseEquivalent(p)); |
| 251 } | 253 } |
| 252 | 254 |
| 253 | 255 |
| 254 namespace { | 256 namespace { |
| 255 | 257 |
| 256 class RetainerProfilePrinter : public RetainerHeapProfile::Printer { | 258 class RetainerProfilePrinter : public RetainerHeapProfile::Printer { |
| 257 public: | 259 public: |
| 258 RetainerProfilePrinter() : stream_(&allocator_), lines_(100) {} | 260 RetainerProfilePrinter() : stream_(&allocator_), lines_(100) {} |
| 259 | 261 |
| 260 void PrintRetainers(const i::StringStream& retainers) { | 262 void PrintRetainers(const JSObjectsCluster& cluster, |
| 263 const i::StringStream& retainers) { |
| 264 cluster.Print(&stream_); |
| 261 stream_.Add("%s", *(retainers.ToCString())); | 265 stream_.Add("%s", *(retainers.ToCString())); |
| 262 stream_.Put('\0'); | 266 stream_.Put('\0'); |
| 263 } | 267 } |
| 264 | 268 |
| 265 const char* GetRetainers(const char* constructor) { | 269 const char* GetRetainers(const char* constructor) { |
| 266 FillLines(); | 270 FillLines(); |
| 267 const size_t cons_len = strlen(constructor); | 271 const size_t cons_len = strlen(constructor); |
| 268 for (int i = 0; i < lines_.length(); ++i) { | 272 for (int i = 0; i < lines_.length(); ++i) { |
| 269 if (strncmp(constructor, lines_[i], cons_len) == 0 && | 273 if (strncmp(constructor, lines_[i], cons_len) == 0 && |
| 270 lines_[i][cons_len] == ',') { | 274 lines_[i][cons_len] == ',') { |
| (...skipping 26 matching lines...) Expand all Loading... |
| 297 | 301 |
| 298 | 302 |
| 299 TEST(RetainerProfile) { | 303 TEST(RetainerProfile) { |
| 300 v8::HandleScope scope; | 304 v8::HandleScope scope; |
| 301 v8::Handle<v8::Context> env = v8::Context::New(); | 305 v8::Handle<v8::Context> env = v8::Context::New(); |
| 302 env->Enter(); | 306 env->Enter(); |
| 303 | 307 |
| 304 CompileAndRunScript( | 308 CompileAndRunScript( |
| 305 "function A() {}\n" | 309 "function A() {}\n" |
| 306 "function B(x) { this.x = x; }\n" | 310 "function B(x) { this.x = x; }\n" |
| 311 "function C(x) { this.x1 = x; this.x2 = x; }\n" |
| 307 "var a = new A();\n" | 312 "var a = new A();\n" |
| 308 "var b = new B(a);\n"); | 313 "var b1 = new B(a), b2 = new B(a);\n" |
| 314 "var c = new C(a);"); |
| 309 | 315 |
| 310 RetainerHeapProfile ret_profile; | 316 RetainerHeapProfile ret_profile; |
| 311 i::AssertNoAllocation no_alloc; | 317 i::AssertNoAllocation no_alloc; |
| 312 i::HeapIterator iterator; | 318 i::HeapIterator iterator; |
| 313 while (iterator.has_next()) { | 319 while (iterator.has_next()) { |
| 314 i::HeapObject* obj = iterator.next(); | 320 i::HeapObject* obj = iterator.next(); |
| 315 ret_profile.CollectStats(obj); | 321 ret_profile.CollectStats(obj); |
| 316 } | 322 } |
| 317 RetainerProfilePrinter printer; | 323 RetainerProfilePrinter printer; |
| 318 ret_profile.DebugPrintStats(&printer); | 324 ret_profile.DebugPrintStats(&printer); |
| 319 CHECK_EQ("(global property),B", printer.GetRetainers("A")); | 325 CHECK_EQ("(global property);1,B;2,C;2", printer.GetRetainers("A")); |
| 320 CHECK_EQ("(global property)", printer.GetRetainers("B")); | 326 CHECK_EQ("(global property);2", printer.GetRetainers("B")); |
| 327 CHECK_EQ("(global property);1", printer.GetRetainers("C")); |
| 321 } | 328 } |
| 322 | 329 |
| 323 #endif // ENABLE_LOGGING_AND_PROFILING | 330 #endif // ENABLE_LOGGING_AND_PROFILING |
| OLD | NEW |