Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 2 | // |
| 3 | // Tests for heap profiler |
| 4 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 5 | #include "v8.h" |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 6 | |
| 7 | #include "cctest.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 8 | #include "heap-profiler.h" |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 9 | #include "snapshot.h" |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 10 | #include "utils-inl.h" |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 11 | #include "../include/v8-profiler.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12 | |
| 13 | namespace i = v8::internal; |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 14 | |
| 15 | namespace { |
| 16 | |
| 17 | class NamedEntriesDetector { |
| 18 | public: |
| 19 | NamedEntriesDetector() |
Russell Brenner | 90bac25 | 2010-11-18 13:33:46 -0800 | [diff] [blame] | 20 | : has_A2(false), has_B2(false), has_C2(false) { |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 21 | } |
| 22 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 23 | void Apply(i::HeapEntry** entry_ptr) { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 24 | if (IsReachableNodeWithName(*entry_ptr, "A2")) has_A2 = true; |
| 25 | if (IsReachableNodeWithName(*entry_ptr, "B2")) has_B2 = true; |
| 26 | if (IsReachableNodeWithName(*entry_ptr, "C2")) has_C2 = true; |
| 27 | } |
| 28 | |
| 29 | static bool IsReachableNodeWithName(i::HeapEntry* entry, const char* name) { |
| 30 | return strcmp(name, entry->name()) == 0 && entry->painted_reachable(); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 31 | } |
| 32 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 33 | bool has_A2; |
| 34 | bool has_B2; |
| 35 | bool has_C2; |
| 36 | }; |
| 37 | |
| 38 | } // namespace |
| 39 | |
| 40 | |
| 41 | static const v8::HeapGraphNode* GetGlobalObject( |
| 42 | const v8::HeapSnapshot* snapshot) { |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 43 | CHECK_EQ(2, snapshot->GetRoot()->GetChildrenCount()); |
| 44 | const v8::HeapGraphNode* global_obj = |
| 45 | snapshot->GetRoot()->GetChild(0)->GetToNode(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 46 | CHECK_EQ(0, strncmp("Object", const_cast<i::HeapEntry*>( |
| 47 | reinterpret_cast<const i::HeapEntry*>(global_obj))->name(), 6)); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 48 | return global_obj; |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | |
| 52 | static const v8::HeapGraphNode* GetProperty(const v8::HeapGraphNode* node, |
| 53 | v8::HeapGraphEdge::Type type, |
| 54 | const char* name) { |
| 55 | for (int i = 0, count = node->GetChildrenCount(); i < count; ++i) { |
| 56 | const v8::HeapGraphEdge* prop = node->GetChild(i); |
| 57 | v8::String::AsciiValue prop_name(prop->GetName()); |
| 58 | if (prop->GetType() == type && strcmp(name, *prop_name) == 0) |
| 59 | return prop->GetToNode(); |
| 60 | } |
| 61 | return NULL; |
| 62 | } |
| 63 | |
| 64 | |
| 65 | static bool HasString(const v8::HeapGraphNode* node, const char* contents) { |
| 66 | for (int i = 0, count = node->GetChildrenCount(); i < count; ++i) { |
| 67 | const v8::HeapGraphEdge* prop = node->GetChild(i); |
| 68 | const v8::HeapGraphNode* node = prop->GetToNode(); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 69 | if (node->GetType() == v8::HeapGraphNode::kString) { |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 70 | v8::String::AsciiValue node_name(node->GetName()); |
| 71 | if (strcmp(contents, *node_name) == 0) return true; |
| 72 | } |
| 73 | } |
| 74 | return false; |
| 75 | } |
| 76 | |
| 77 | |
| 78 | TEST(HeapSnapshot) { |
| 79 | v8::HandleScope scope; |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 80 | LocalContext env2; |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 81 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 82 | CompileRun( |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 83 | "function A2() {}\n" |
| 84 | "function B2(x) { return function() { return typeof x; }; }\n" |
| 85 | "function C2(x) { this.x1 = x; this.x2 = x; this[1] = x; }\n" |
| 86 | "var a2 = new A2();\n" |
| 87 | "var b2_1 = new B2(a2), b2_2 = new B2(a2);\n" |
| 88 | "var c2 = new C2(a2);"); |
| 89 | const v8::HeapSnapshot* snapshot_env2 = |
| 90 | v8::HeapProfiler::TakeSnapshot(v8::String::New("env2")); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 91 | i::HeapSnapshot* i_snapshot_env2 = |
| 92 | const_cast<i::HeapSnapshot*>( |
| 93 | reinterpret_cast<const i::HeapSnapshot*>(snapshot_env2)); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 94 | const v8::HeapGraphNode* global_env2 = GetGlobalObject(snapshot_env2); |
| 95 | // Paint all nodes reachable from global object. |
| 96 | i_snapshot_env2->ClearPaint(); |
| 97 | const_cast<i::HeapEntry*>( |
| 98 | reinterpret_cast<const i::HeapEntry*>(global_env2))->PaintAllReachable(); |
| 99 | |
Russell Brenner | 90bac25 | 2010-11-18 13:33:46 -0800 | [diff] [blame] | 100 | // Verify, that JS global object of env2 has '..2' properties. |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 101 | const v8::HeapGraphNode* a2_node = |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 102 | GetProperty(global_env2, v8::HeapGraphEdge::kShortcut, "a2"); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 103 | CHECK_NE(NULL, a2_node); |
| 104 | CHECK_NE( |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 105 | NULL, GetProperty(global_env2, v8::HeapGraphEdge::kShortcut, "b2_1")); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 106 | CHECK_NE( |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 107 | NULL, GetProperty(global_env2, v8::HeapGraphEdge::kShortcut, "b2_2")); |
| 108 | CHECK_NE(NULL, GetProperty(global_env2, v8::HeapGraphEdge::kShortcut, "c2")); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 109 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 110 | NamedEntriesDetector det; |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 111 | i_snapshot_env2->IterateEntries(&det); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 112 | CHECK(det.has_A2); |
| 113 | CHECK(det.has_B2); |
| 114 | CHECK(det.has_C2); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 118 | TEST(HeapSnapshotObjectSizes) { |
| 119 | v8::HandleScope scope; |
| 120 | LocalContext env; |
| 121 | |
| 122 | // -a-> X1 --a |
| 123 | // x -b-> X2 <-| |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 124 | CompileRun( |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 125 | "function X(a, b) { this.a = a; this.b = b; }\n" |
| 126 | "x = new X(new X(), new X());\n" |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 127 | "(function() { x.a.a = x.b; })();"); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 128 | const v8::HeapSnapshot* snapshot = |
| 129 | v8::HeapProfiler::TakeSnapshot(v8::String::New("sizes")); |
| 130 | const v8::HeapGraphNode* global = GetGlobalObject(snapshot); |
| 131 | const v8::HeapGraphNode* x = |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 132 | GetProperty(global, v8::HeapGraphEdge::kShortcut, "x"); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 133 | CHECK_NE(NULL, x); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 134 | const v8::HeapGraphNode* x1 = |
| 135 | GetProperty(x, v8::HeapGraphEdge::kProperty, "a"); |
| 136 | CHECK_NE(NULL, x1); |
| 137 | const v8::HeapGraphNode* x2 = |
| 138 | GetProperty(x, v8::HeapGraphEdge::kProperty, "b"); |
| 139 | CHECK_NE(NULL, x2); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 140 | |
| 141 | // Test approximate sizes. |
| 142 | CHECK_EQ(x->GetSelfSize() * 3, x->GetRetainedSize(false)); |
| 143 | CHECK_EQ(x1->GetSelfSize(), x1->GetRetainedSize(false)); |
| 144 | CHECK_EQ(x2->GetSelfSize(), x2->GetRetainedSize(false)); |
| 145 | // Test exact sizes. |
| 146 | CHECK_EQ(x->GetSelfSize() * 3, x->GetRetainedSize(true)); |
| 147 | CHECK_EQ(x1->GetSelfSize(), x1->GetRetainedSize(true)); |
| 148 | CHECK_EQ(x2->GetSelfSize(), x2->GetRetainedSize(true)); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | |
| 152 | TEST(HeapSnapshotEntryChildren) { |
| 153 | v8::HandleScope scope; |
| 154 | LocalContext env; |
| 155 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 156 | CompileRun( |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 157 | "function A() { }\n" |
| 158 | "a = new A;"); |
| 159 | const v8::HeapSnapshot* snapshot = |
| 160 | v8::HeapProfiler::TakeSnapshot(v8::String::New("children")); |
| 161 | const v8::HeapGraphNode* global = GetGlobalObject(snapshot); |
| 162 | for (int i = 0, count = global->GetChildrenCount(); i < count; ++i) { |
| 163 | const v8::HeapGraphEdge* prop = global->GetChild(i); |
| 164 | CHECK_EQ(global, prop->GetFromNode()); |
| 165 | } |
| 166 | const v8::HeapGraphNode* a = |
| 167 | GetProperty(global, v8::HeapGraphEdge::kProperty, "a"); |
| 168 | CHECK_NE(NULL, a); |
| 169 | for (int i = 0, count = a->GetChildrenCount(); i < count; ++i) { |
| 170 | const v8::HeapGraphEdge* prop = a->GetChild(i); |
| 171 | CHECK_EQ(a, prop->GetFromNode()); |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 176 | TEST(HeapSnapshotCodeObjects) { |
| 177 | v8::HandleScope scope; |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 178 | LocalContext env; |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 179 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 180 | CompileRun( |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 181 | "function lazy(x) { return x - 1; }\n" |
| 182 | "function compiled(x) { return x + 1; }\n" |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 183 | "var anonymous = (function() { return function() { return 0; } })();\n" |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 184 | "compiled(1)"); |
| 185 | const v8::HeapSnapshot* snapshot = |
| 186 | v8::HeapProfiler::TakeSnapshot(v8::String::New("code")); |
| 187 | |
| 188 | const v8::HeapGraphNode* global = GetGlobalObject(snapshot); |
| 189 | const v8::HeapGraphNode* compiled = |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 190 | GetProperty(global, v8::HeapGraphEdge::kShortcut, "compiled"); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 191 | CHECK_NE(NULL, compiled); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 192 | CHECK_EQ(v8::HeapGraphNode::kClosure, compiled->GetType()); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 193 | const v8::HeapGraphNode* lazy = |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 194 | GetProperty(global, v8::HeapGraphEdge::kShortcut, "lazy"); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 195 | CHECK_NE(NULL, lazy); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 196 | CHECK_EQ(v8::HeapGraphNode::kClosure, lazy->GetType()); |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 197 | const v8::HeapGraphNode* anonymous = |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 198 | GetProperty(global, v8::HeapGraphEdge::kShortcut, "anonymous"); |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 199 | CHECK_NE(NULL, anonymous); |
| 200 | CHECK_EQ(v8::HeapGraphNode::kClosure, anonymous->GetType()); |
| 201 | v8::String::AsciiValue anonymous_name(anonymous->GetName()); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 202 | CHECK_EQ("", *anonymous_name); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 203 | |
| 204 | // Find references to code. |
| 205 | const v8::HeapGraphNode* compiled_code = |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 206 | GetProperty(compiled, v8::HeapGraphEdge::kInternal, "shared"); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 207 | CHECK_NE(NULL, compiled_code); |
| 208 | const v8::HeapGraphNode* lazy_code = |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 209 | GetProperty(lazy, v8::HeapGraphEdge::kInternal, "shared"); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 210 | CHECK_NE(NULL, lazy_code); |
| 211 | |
| 212 | // Verify that non-compiled code doesn't contain references to "x" |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 213 | // literal, while compiled code does. The scope info is stored in FixedArray |
| 214 | // objects attached to the SharedFunctionInfo. |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 215 | bool compiled_references_x = false, lazy_references_x = false; |
| 216 | for (int i = 0, count = compiled_code->GetChildrenCount(); i < count; ++i) { |
| 217 | const v8::HeapGraphEdge* prop = compiled_code->GetChild(i); |
| 218 | const v8::HeapGraphNode* node = prop->GetToNode(); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 219 | if (node->GetType() == v8::HeapGraphNode::kArray) { |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 220 | if (HasString(node, "x")) { |
| 221 | compiled_references_x = true; |
| 222 | break; |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 | for (int i = 0, count = lazy_code->GetChildrenCount(); i < count; ++i) { |
| 227 | const v8::HeapGraphEdge* prop = lazy_code->GetChild(i); |
| 228 | const v8::HeapGraphNode* node = prop->GetToNode(); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 229 | if (node->GetType() == v8::HeapGraphNode::kArray) { |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 230 | if (HasString(node, "x")) { |
| 231 | lazy_references_x = true; |
| 232 | break; |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | CHECK(compiled_references_x); |
| 237 | CHECK(!lazy_references_x); |
| 238 | } |
| 239 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 240 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 241 | TEST(HeapSnapshotHeapNumbers) { |
| 242 | v8::HandleScope scope; |
| 243 | LocalContext env; |
| 244 | CompileRun( |
| 245 | "a = 1; // a is Smi\n" |
| 246 | "b = 2.5; // b is HeapNumber"); |
| 247 | const v8::HeapSnapshot* snapshot = |
| 248 | v8::HeapProfiler::TakeSnapshot(v8::String::New("numbers")); |
| 249 | const v8::HeapGraphNode* global = GetGlobalObject(snapshot); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 250 | CHECK_EQ(NULL, GetProperty(global, v8::HeapGraphEdge::kShortcut, "a")); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 251 | const v8::HeapGraphNode* b = |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 252 | GetProperty(global, v8::HeapGraphEdge::kShortcut, "b"); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 253 | CHECK_NE(NULL, b); |
| 254 | CHECK_EQ(v8::HeapGraphNode::kHeapNumber, b->GetType()); |
| 255 | } |
| 256 | |
| 257 | |
| 258 | TEST(HeapSnapshotInternalReferences) { |
| 259 | v8::HandleScope scope; |
| 260 | v8::Local<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New(); |
| 261 | global_template->SetInternalFieldCount(2); |
| 262 | LocalContext env(NULL, global_template); |
| 263 | v8::Handle<v8::Object> global_proxy = env->Global(); |
| 264 | v8::Handle<v8::Object> global = global_proxy->GetPrototype().As<v8::Object>(); |
| 265 | CHECK_EQ(2, global->InternalFieldCount()); |
| 266 | v8::Local<v8::Object> obj = v8::Object::New(); |
| 267 | global->SetInternalField(0, v8_num(17)); |
| 268 | global->SetInternalField(1, obj); |
| 269 | const v8::HeapSnapshot* snapshot = |
| 270 | v8::HeapProfiler::TakeSnapshot(v8::String::New("internals")); |
| 271 | const v8::HeapGraphNode* global_node = GetGlobalObject(snapshot); |
| 272 | // The first reference will not present, because it's a Smi. |
| 273 | CHECK_EQ(NULL, GetProperty(global_node, v8::HeapGraphEdge::kInternal, "0")); |
| 274 | // The second reference is to an object. |
| 275 | CHECK_NE(NULL, GetProperty(global_node, v8::HeapGraphEdge::kInternal, "1")); |
| 276 | } |
| 277 | |
| 278 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 279 | // Trying to introduce a check helper for uint64_t causes many |
| 280 | // overloading ambiguities, so it seems easier just to cast |
| 281 | // them to a signed type. |
| 282 | #define CHECK_EQ_UINT64_T(a, b) \ |
| 283 | CHECK_EQ(static_cast<int64_t>(a), static_cast<int64_t>(b)) |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 284 | #define CHECK_NE_UINT64_T(a, b) \ |
| 285 | CHECK((a) != (b)) // NOLINT |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 286 | |
| 287 | TEST(HeapEntryIdsAndGC) { |
| 288 | v8::HandleScope scope; |
| 289 | LocalContext env; |
| 290 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 291 | CompileRun( |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 292 | "function A() {}\n" |
| 293 | "function B(x) { this.x = x; }\n" |
| 294 | "var a = new A();\n" |
| 295 | "var b = new B(a);"); |
| 296 | const v8::HeapSnapshot* snapshot1 = |
| 297 | v8::HeapProfiler::TakeSnapshot(v8::String::New("s1")); |
| 298 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 299 | HEAP->CollectAllGarbage(true); // Enforce compaction. |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 300 | |
| 301 | const v8::HeapSnapshot* snapshot2 = |
| 302 | v8::HeapProfiler::TakeSnapshot(v8::String::New("s2")); |
| 303 | |
| 304 | const v8::HeapGraphNode* global1 = GetGlobalObject(snapshot1); |
| 305 | const v8::HeapGraphNode* global2 = GetGlobalObject(snapshot2); |
| 306 | CHECK_NE_UINT64_T(0, global1->GetId()); |
| 307 | CHECK_EQ_UINT64_T(global1->GetId(), global2->GetId()); |
| 308 | const v8::HeapGraphNode* A1 = |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 309 | GetProperty(global1, v8::HeapGraphEdge::kProperty, "A"); |
| 310 | CHECK_NE(NULL, A1); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 311 | const v8::HeapGraphNode* A2 = |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 312 | GetProperty(global2, v8::HeapGraphEdge::kProperty, "A"); |
| 313 | CHECK_NE(NULL, A2); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 314 | CHECK_NE_UINT64_T(0, A1->GetId()); |
| 315 | CHECK_EQ_UINT64_T(A1->GetId(), A2->GetId()); |
| 316 | const v8::HeapGraphNode* B1 = |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 317 | GetProperty(global1, v8::HeapGraphEdge::kProperty, "B"); |
| 318 | CHECK_NE(NULL, B1); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 319 | const v8::HeapGraphNode* B2 = |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 320 | GetProperty(global2, v8::HeapGraphEdge::kProperty, "B"); |
| 321 | CHECK_NE(NULL, B2); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 322 | CHECK_NE_UINT64_T(0, B1->GetId()); |
| 323 | CHECK_EQ_UINT64_T(B1->GetId(), B2->GetId()); |
| 324 | const v8::HeapGraphNode* a1 = |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 325 | GetProperty(global1, v8::HeapGraphEdge::kProperty, "a"); |
| 326 | CHECK_NE(NULL, a1); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 327 | const v8::HeapGraphNode* a2 = |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 328 | GetProperty(global2, v8::HeapGraphEdge::kProperty, "a"); |
| 329 | CHECK_NE(NULL, a2); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 330 | CHECK_NE_UINT64_T(0, a1->GetId()); |
| 331 | CHECK_EQ_UINT64_T(a1->GetId(), a2->GetId()); |
| 332 | const v8::HeapGraphNode* b1 = |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 333 | GetProperty(global1, v8::HeapGraphEdge::kProperty, "b"); |
| 334 | CHECK_NE(NULL, b1); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 335 | const v8::HeapGraphNode* b2 = |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 336 | GetProperty(global2, v8::HeapGraphEdge::kProperty, "b"); |
| 337 | CHECK_NE(NULL, b2); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 338 | CHECK_NE_UINT64_T(0, b1->GetId()); |
| 339 | CHECK_EQ_UINT64_T(b1->GetId(), b2->GetId()); |
| 340 | } |
| 341 | |
| 342 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 343 | TEST(HeapSnapshotRootPreservedAfterSorting) { |
| 344 | v8::HandleScope scope; |
| 345 | LocalContext env; |
| 346 | const v8::HeapSnapshot* snapshot = |
| 347 | v8::HeapProfiler::TakeSnapshot(v8::String::New("s")); |
| 348 | const v8::HeapGraphNode* root1 = snapshot->GetRoot(); |
| 349 | const_cast<i::HeapSnapshot*>(reinterpret_cast<const i::HeapSnapshot*>( |
| 350 | snapshot))->GetSortedEntriesList(); |
| 351 | const v8::HeapGraphNode* root2 = snapshot->GetRoot(); |
| 352 | CHECK_EQ(root1, root2); |
| 353 | } |
| 354 | |
| 355 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 356 | TEST(HeapEntryDominator) { |
| 357 | // The graph looks like this: |
| 358 | // |
| 359 | // -> node1 |
| 360 | // a |^ |
| 361 | // -> node5 ba |
| 362 | // a v| |
| 363 | // node6 -> node2 |
| 364 | // b a |^ |
| 365 | // -> node4 ba |
| 366 | // b v| |
| 367 | // -> node3 |
| 368 | // |
| 369 | // The dominator for all nodes is node6. |
| 370 | |
| 371 | v8::HandleScope scope; |
| 372 | LocalContext env; |
| 373 | |
| 374 | CompileRun( |
| 375 | "function X(a, b) { this.a = a; this.b = b; }\n" |
| 376 | "node6 = new X(new X(new X()), new X(new X(),new X()));\n" |
| 377 | "(function(){\n" |
| 378 | "node6.a.a.b = node6.b.a; // node1 -> node2\n" |
| 379 | "node6.b.a.a = node6.a.a; // node2 -> node1\n" |
| 380 | "node6.b.a.b = node6.b.b; // node2 -> node3\n" |
| 381 | "node6.b.b.a = node6.b.a; // node3 -> node2\n" |
| 382 | "})();"); |
| 383 | |
| 384 | const v8::HeapSnapshot* snapshot = |
| 385 | v8::HeapProfiler::TakeSnapshot(v8::String::New("dominators")); |
| 386 | |
| 387 | const v8::HeapGraphNode* global = GetGlobalObject(snapshot); |
| 388 | CHECK_NE(NULL, global); |
| 389 | const v8::HeapGraphNode* node6 = |
| 390 | GetProperty(global, v8::HeapGraphEdge::kShortcut, "node6"); |
| 391 | CHECK_NE(NULL, node6); |
| 392 | const v8::HeapGraphNode* node5 = |
| 393 | GetProperty(node6, v8::HeapGraphEdge::kProperty, "a"); |
| 394 | CHECK_NE(NULL, node5); |
| 395 | const v8::HeapGraphNode* node4 = |
| 396 | GetProperty(node6, v8::HeapGraphEdge::kProperty, "b"); |
| 397 | CHECK_NE(NULL, node4); |
| 398 | const v8::HeapGraphNode* node3 = |
| 399 | GetProperty(node4, v8::HeapGraphEdge::kProperty, "b"); |
| 400 | CHECK_NE(NULL, node3); |
| 401 | const v8::HeapGraphNode* node2 = |
| 402 | GetProperty(node4, v8::HeapGraphEdge::kProperty, "a"); |
| 403 | CHECK_NE(NULL, node2); |
| 404 | const v8::HeapGraphNode* node1 = |
| 405 | GetProperty(node5, v8::HeapGraphEdge::kProperty, "a"); |
| 406 | CHECK_NE(NULL, node1); |
| 407 | |
| 408 | CHECK_EQ(node6, node1->GetDominatorNode()); |
| 409 | CHECK_EQ(node6, node2->GetDominatorNode()); |
| 410 | CHECK_EQ(node6, node3->GetDominatorNode()); |
| 411 | CHECK_EQ(node6, node4->GetDominatorNode()); |
| 412 | CHECK_EQ(node6, node5->GetDominatorNode()); |
| 413 | } |
| 414 | |
| 415 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 416 | namespace { |
| 417 | |
| 418 | class TestJSONStream : public v8::OutputStream { |
| 419 | public: |
| 420 | TestJSONStream() : eos_signaled_(0), abort_countdown_(-1) {} |
| 421 | explicit TestJSONStream(int abort_countdown) |
| 422 | : eos_signaled_(0), abort_countdown_(abort_countdown) {} |
| 423 | virtual ~TestJSONStream() {} |
| 424 | virtual void EndOfStream() { ++eos_signaled_; } |
| 425 | virtual WriteResult WriteAsciiChunk(char* buffer, int chars_written) { |
| 426 | if (abort_countdown_ > 0) --abort_countdown_; |
| 427 | if (abort_countdown_ == 0) return kAbort; |
| 428 | CHECK_GT(chars_written, 0); |
| 429 | i::Vector<char> chunk = buffer_.AddBlock(chars_written, '\0'); |
| 430 | memcpy(chunk.start(), buffer, chars_written); |
| 431 | return kContinue; |
| 432 | } |
| 433 | void WriteTo(i::Vector<char> dest) { buffer_.WriteTo(dest); } |
| 434 | int eos_signaled() { return eos_signaled_; } |
| 435 | int size() { return buffer_.size(); } |
| 436 | private: |
| 437 | i::Collector<char> buffer_; |
| 438 | int eos_signaled_; |
| 439 | int abort_countdown_; |
| 440 | }; |
| 441 | |
| 442 | class AsciiResource: public v8::String::ExternalAsciiStringResource { |
| 443 | public: |
| 444 | explicit AsciiResource(i::Vector<char> string): data_(string.start()) { |
| 445 | length_ = string.length(); |
| 446 | } |
| 447 | virtual const char* data() const { return data_; } |
| 448 | virtual size_t length() const { return length_; } |
| 449 | private: |
| 450 | const char* data_; |
| 451 | size_t length_; |
| 452 | }; |
| 453 | |
| 454 | } // namespace |
| 455 | |
| 456 | TEST(HeapSnapshotJSONSerialization) { |
| 457 | v8::HandleScope scope; |
| 458 | LocalContext env; |
| 459 | |
| 460 | #define STRING_LITERAL_FOR_TEST \ |
| 461 | "\"String \\n\\r\\u0008\\u0081\\u0101\\u0801\\u8001\"" |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 462 | CompileRun( |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 463 | "function A(s) { this.s = s; }\n" |
| 464 | "function B(x) { this.x = x; }\n" |
| 465 | "var a = new A(" STRING_LITERAL_FOR_TEST ");\n" |
| 466 | "var b = new B(a);"); |
| 467 | const v8::HeapSnapshot* snapshot = |
| 468 | v8::HeapProfiler::TakeSnapshot(v8::String::New("json")); |
| 469 | TestJSONStream stream; |
| 470 | snapshot->Serialize(&stream, v8::HeapSnapshot::kJSON); |
| 471 | CHECK_GT(stream.size(), 0); |
| 472 | CHECK_EQ(1, stream.eos_signaled()); |
| 473 | i::ScopedVector<char> json(stream.size()); |
| 474 | stream.WriteTo(json); |
| 475 | |
| 476 | // Verify that snapshot string is valid JSON. |
| 477 | AsciiResource json_res(json); |
| 478 | v8::Local<v8::String> json_string = v8::String::NewExternal(&json_res); |
| 479 | env->Global()->Set(v8::String::New("json_snapshot"), json_string); |
| 480 | v8::Local<v8::Value> snapshot_parse_result = CompileRun( |
| 481 | "var parsed = JSON.parse(json_snapshot); true;"); |
| 482 | CHECK(!snapshot_parse_result.IsEmpty()); |
| 483 | |
| 484 | // Verify that snapshot object has required fields. |
| 485 | v8::Local<v8::Object> parsed_snapshot = |
| 486 | env->Global()->Get(v8::String::New("parsed"))->ToObject(); |
| 487 | CHECK(parsed_snapshot->Has(v8::String::New("snapshot"))); |
| 488 | CHECK(parsed_snapshot->Has(v8::String::New("nodes"))); |
| 489 | CHECK(parsed_snapshot->Has(v8::String::New("strings"))); |
| 490 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 491 | // Get node and edge "member" offsets. |
| 492 | v8::Local<v8::Value> meta_analysis_result = CompileRun( |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 493 | "var parsed_meta = parsed.nodes[0];\n" |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 494 | "var children_count_offset =" |
| 495 | " parsed_meta.fields.indexOf('children_count');\n" |
| 496 | "var children_offset =" |
| 497 | " parsed_meta.fields.indexOf('children');\n" |
| 498 | "var children_meta =" |
| 499 | " parsed_meta.types[children_offset];\n" |
| 500 | "var child_fields_count = children_meta.fields.length;\n" |
| 501 | "var child_type_offset =" |
| 502 | " children_meta.fields.indexOf('type');\n" |
| 503 | "var child_name_offset =" |
| 504 | " children_meta.fields.indexOf('name_or_index');\n" |
| 505 | "var child_to_node_offset =" |
| 506 | " children_meta.fields.indexOf('to_node');\n" |
| 507 | "var property_type =" |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 508 | " children_meta.types[child_type_offset].indexOf('property');\n" |
| 509 | "var shortcut_type =" |
| 510 | " children_meta.types[child_type_offset].indexOf('shortcut');"); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 511 | CHECK(!meta_analysis_result.IsEmpty()); |
| 512 | |
| 513 | // A helper function for processing encoded nodes. |
| 514 | CompileRun( |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 515 | "function GetChildPosByProperty(pos, prop_name, prop_type) {\n" |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 516 | " var nodes = parsed.nodes;\n" |
| 517 | " var strings = parsed.strings;\n" |
| 518 | " for (var i = 0,\n" |
| 519 | " count = nodes[pos + children_count_offset] * child_fields_count;\n" |
| 520 | " i < count; i += child_fields_count) {\n" |
| 521 | " var child_pos = pos + children_offset + i;\n" |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 522 | " if (nodes[child_pos + child_type_offset] === prop_type\n" |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 523 | " && strings[nodes[child_pos + child_name_offset]] === prop_name)\n" |
| 524 | " return nodes[child_pos + child_to_node_offset];\n" |
| 525 | " }\n" |
| 526 | " return null;\n" |
| 527 | "}\n"); |
| 528 | // Get the string index using the path: <root> -> <global>.b.x.s |
| 529 | v8::Local<v8::Value> string_obj_pos_val = CompileRun( |
| 530 | "GetChildPosByProperty(\n" |
| 531 | " GetChildPosByProperty(\n" |
| 532 | " GetChildPosByProperty(" |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 533 | " parsed.nodes[1 + children_offset + child_to_node_offset]," |
| 534 | " \"b\",shortcut_type),\n" |
| 535 | " \"x\", property_type)," |
| 536 | " \"s\", property_type)"); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 537 | CHECK(!string_obj_pos_val.IsEmpty()); |
| 538 | int string_obj_pos = |
| 539 | static_cast<int>(string_obj_pos_val->ToNumber()->Value()); |
| 540 | v8::Local<v8::Object> nodes_array = |
| 541 | parsed_snapshot->Get(v8::String::New("nodes"))->ToObject(); |
| 542 | int string_index = static_cast<int>( |
| 543 | nodes_array->Get(string_obj_pos + 1)->ToNumber()->Value()); |
| 544 | CHECK_GT(string_index, 0); |
| 545 | v8::Local<v8::Object> strings_array = |
| 546 | parsed_snapshot->Get(v8::String::New("strings"))->ToObject(); |
| 547 | v8::Local<v8::String> string = strings_array->Get(string_index)->ToString(); |
| 548 | v8::Local<v8::String> ref_string = |
| 549 | CompileRun(STRING_LITERAL_FOR_TEST)->ToString(); |
| 550 | #undef STRING_LITERAL_FOR_TEST |
| 551 | CHECK_EQ(*v8::String::Utf8Value(ref_string), |
| 552 | *v8::String::Utf8Value(string)); |
| 553 | } |
| 554 | |
| 555 | |
| 556 | TEST(HeapSnapshotJSONSerializationAborting) { |
| 557 | v8::HandleScope scope; |
| 558 | LocalContext env; |
| 559 | const v8::HeapSnapshot* snapshot = |
| 560 | v8::HeapProfiler::TakeSnapshot(v8::String::New("abort")); |
| 561 | TestJSONStream stream(5); |
| 562 | snapshot->Serialize(&stream, v8::HeapSnapshot::kJSON); |
| 563 | CHECK_GT(stream.size(), 0); |
| 564 | CHECK_EQ(0, stream.eos_signaled()); |
| 565 | } |
| 566 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 567 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 568 | TEST(HeapSnapshotGetNodeById) { |
| 569 | v8::HandleScope scope; |
| 570 | LocalContext env; |
| 571 | |
| 572 | const v8::HeapSnapshot* snapshot = |
| 573 | v8::HeapProfiler::TakeSnapshot(v8::String::New("id")); |
| 574 | const v8::HeapGraphNode* root = snapshot->GetRoot(); |
| 575 | CHECK_EQ(root, snapshot->GetNodeById(root->GetId())); |
| 576 | for (int i = 0, count = root->GetChildrenCount(); i < count; ++i) { |
| 577 | const v8::HeapGraphEdge* prop = root->GetChild(i); |
| 578 | CHECK_EQ( |
| 579 | prop->GetToNode(), snapshot->GetNodeById(prop->GetToNode()->GetId())); |
| 580 | } |
| 581 | // Check a big id, which should not exist yet. |
| 582 | CHECK_EQ(NULL, snapshot->GetNodeById(0x1000000UL)); |
| 583 | } |
| 584 | |
| 585 | |
| 586 | namespace { |
| 587 | |
| 588 | class TestActivityControl : public v8::ActivityControl { |
| 589 | public: |
| 590 | explicit TestActivityControl(int abort_count) |
| 591 | : done_(0), total_(0), abort_count_(abort_count) {} |
| 592 | ControlOption ReportProgressValue(int done, int total) { |
| 593 | done_ = done; |
| 594 | total_ = total; |
| 595 | return --abort_count_ != 0 ? kContinue : kAbort; |
| 596 | } |
| 597 | int done() { return done_; } |
| 598 | int total() { return total_; } |
| 599 | |
| 600 | private: |
| 601 | int done_; |
| 602 | int total_; |
| 603 | int abort_count_; |
| 604 | }; |
| 605 | } |
| 606 | |
| 607 | TEST(TakeHeapSnapshotAborting) { |
| 608 | v8::HandleScope scope; |
| 609 | LocalContext env; |
| 610 | |
| 611 | const int snapshots_count = v8::HeapProfiler::GetSnapshotsCount(); |
| 612 | TestActivityControl aborting_control(3); |
| 613 | const v8::HeapSnapshot* no_snapshot = |
| 614 | v8::HeapProfiler::TakeSnapshot(v8::String::New("abort"), |
| 615 | v8::HeapSnapshot::kFull, |
| 616 | &aborting_control); |
| 617 | CHECK_EQ(NULL, no_snapshot); |
| 618 | CHECK_EQ(snapshots_count, v8::HeapProfiler::GetSnapshotsCount()); |
| 619 | CHECK_GT(aborting_control.total(), aborting_control.done()); |
| 620 | |
| 621 | TestActivityControl control(-1); // Don't abort. |
| 622 | const v8::HeapSnapshot* snapshot = |
| 623 | v8::HeapProfiler::TakeSnapshot(v8::String::New("full"), |
| 624 | v8::HeapSnapshot::kFull, |
| 625 | &control); |
| 626 | CHECK_NE(NULL, snapshot); |
| 627 | CHECK_EQ(snapshots_count + 1, v8::HeapProfiler::GetSnapshotsCount()); |
| 628 | CHECK_EQ(control.total(), control.done()); |
| 629 | CHECK_GT(control.total(), 0); |
| 630 | } |
| 631 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 632 | |
| 633 | namespace { |
| 634 | |
| 635 | class TestRetainedObjectInfo : public v8::RetainedObjectInfo { |
| 636 | public: |
| 637 | TestRetainedObjectInfo(int hash, |
| 638 | const char* label, |
| 639 | intptr_t element_count = -1, |
| 640 | intptr_t size = -1) |
| 641 | : disposed_(false), |
| 642 | hash_(hash), |
| 643 | label_(label), |
| 644 | element_count_(element_count), |
| 645 | size_(size) { |
| 646 | instances.Add(this); |
| 647 | } |
| 648 | virtual ~TestRetainedObjectInfo() {} |
| 649 | virtual void Dispose() { |
| 650 | CHECK(!disposed_); |
| 651 | disposed_ = true; |
| 652 | } |
| 653 | virtual bool IsEquivalent(RetainedObjectInfo* other) { |
| 654 | return GetHash() == other->GetHash(); |
| 655 | } |
| 656 | virtual intptr_t GetHash() { return hash_; } |
| 657 | virtual const char* GetLabel() { return label_; } |
| 658 | virtual intptr_t GetElementCount() { return element_count_; } |
| 659 | virtual intptr_t GetSizeInBytes() { return size_; } |
| 660 | bool disposed() { return disposed_; } |
| 661 | |
| 662 | static v8::RetainedObjectInfo* WrapperInfoCallback( |
| 663 | uint16_t class_id, v8::Handle<v8::Value> wrapper) { |
| 664 | if (class_id == 1) { |
| 665 | if (wrapper->IsString()) { |
| 666 | v8::String::AsciiValue ascii(wrapper); |
| 667 | if (strcmp(*ascii, "AAA") == 0) |
| 668 | return new TestRetainedObjectInfo(1, "aaa", 100); |
| 669 | else if (strcmp(*ascii, "BBB") == 0) |
| 670 | return new TestRetainedObjectInfo(1, "aaa", 100); |
| 671 | } |
| 672 | } else if (class_id == 2) { |
| 673 | if (wrapper->IsString()) { |
| 674 | v8::String::AsciiValue ascii(wrapper); |
| 675 | if (strcmp(*ascii, "CCC") == 0) |
| 676 | return new TestRetainedObjectInfo(2, "ccc"); |
| 677 | } |
| 678 | } |
| 679 | CHECK(false); |
| 680 | return NULL; |
| 681 | } |
| 682 | |
| 683 | static i::List<TestRetainedObjectInfo*> instances; |
| 684 | |
| 685 | private: |
| 686 | bool disposed_; |
| 687 | int category_; |
| 688 | int hash_; |
| 689 | const char* label_; |
| 690 | intptr_t element_count_; |
| 691 | intptr_t size_; |
| 692 | }; |
| 693 | |
| 694 | |
| 695 | i::List<TestRetainedObjectInfo*> TestRetainedObjectInfo::instances; |
| 696 | } |
| 697 | |
| 698 | |
| 699 | static const v8::HeapGraphNode* GetNode(const v8::HeapGraphNode* parent, |
| 700 | v8::HeapGraphNode::Type type, |
| 701 | const char* name) { |
| 702 | for (int i = 0, count = parent->GetChildrenCount(); i < count; ++i) { |
| 703 | const v8::HeapGraphNode* node = parent->GetChild(i)->GetToNode(); |
| 704 | if (node->GetType() == type && strcmp(name, |
| 705 | const_cast<i::HeapEntry*>( |
| 706 | reinterpret_cast<const i::HeapEntry*>(node))->name()) == 0) { |
| 707 | return node; |
| 708 | } |
| 709 | } |
| 710 | return NULL; |
| 711 | } |
| 712 | |
| 713 | |
| 714 | TEST(HeapSnapshotRetainedObjectInfo) { |
| 715 | v8::HandleScope scope; |
| 716 | LocalContext env; |
| 717 | |
| 718 | v8::HeapProfiler::DefineWrapperClass( |
| 719 | 1, TestRetainedObjectInfo::WrapperInfoCallback); |
| 720 | v8::HeapProfiler::DefineWrapperClass( |
| 721 | 2, TestRetainedObjectInfo::WrapperInfoCallback); |
| 722 | v8::Persistent<v8::String> p_AAA = |
| 723 | v8::Persistent<v8::String>::New(v8_str("AAA")); |
| 724 | p_AAA.SetWrapperClassId(1); |
| 725 | v8::Persistent<v8::String> p_BBB = |
| 726 | v8::Persistent<v8::String>::New(v8_str("BBB")); |
| 727 | p_BBB.SetWrapperClassId(1); |
| 728 | v8::Persistent<v8::String> p_CCC = |
| 729 | v8::Persistent<v8::String>::New(v8_str("CCC")); |
| 730 | p_CCC.SetWrapperClassId(2); |
| 731 | CHECK_EQ(0, TestRetainedObjectInfo::instances.length()); |
| 732 | const v8::HeapSnapshot* snapshot = |
| 733 | v8::HeapProfiler::TakeSnapshot(v8::String::New("retained")); |
| 734 | |
| 735 | CHECK_EQ(3, TestRetainedObjectInfo::instances.length()); |
| 736 | for (int i = 0; i < TestRetainedObjectInfo::instances.length(); ++i) { |
| 737 | CHECK(TestRetainedObjectInfo::instances[i]->disposed()); |
| 738 | delete TestRetainedObjectInfo::instances[i]; |
| 739 | } |
| 740 | |
| 741 | const v8::HeapGraphNode* natives = GetNode( |
| 742 | snapshot->GetRoot(), v8::HeapGraphNode::kObject, "(Native objects)"); |
| 743 | CHECK_NE(NULL, natives); |
| 744 | CHECK_EQ(2, natives->GetChildrenCount()); |
| 745 | const v8::HeapGraphNode* aaa = GetNode( |
| 746 | natives, v8::HeapGraphNode::kNative, "aaa / 100 entries"); |
| 747 | CHECK_NE(NULL, aaa); |
| 748 | const v8::HeapGraphNode* ccc = GetNode( |
| 749 | natives, v8::HeapGraphNode::kNative, "ccc"); |
| 750 | CHECK_NE(NULL, ccc); |
| 751 | |
| 752 | CHECK_EQ(2, aaa->GetChildrenCount()); |
| 753 | const v8::HeapGraphNode* n_AAA = GetNode( |
| 754 | aaa, v8::HeapGraphNode::kString, "AAA"); |
| 755 | CHECK_NE(NULL, n_AAA); |
| 756 | const v8::HeapGraphNode* n_BBB = GetNode( |
| 757 | aaa, v8::HeapGraphNode::kString, "BBB"); |
| 758 | CHECK_NE(NULL, n_BBB); |
| 759 | CHECK_EQ(1, ccc->GetChildrenCount()); |
| 760 | const v8::HeapGraphNode* n_CCC = GetNode( |
| 761 | ccc, v8::HeapGraphNode::kString, "CCC"); |
| 762 | CHECK_NE(NULL, n_CCC); |
| 763 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 764 | CHECK_EQ(aaa, GetProperty(n_AAA, v8::HeapGraphEdge::kInternal, "native")); |
| 765 | CHECK_EQ(aaa, GetProperty(n_BBB, v8::HeapGraphEdge::kInternal, "native")); |
| 766 | CHECK_EQ(ccc, GetProperty(n_CCC, v8::HeapGraphEdge::kInternal, "native")); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | |
| 770 | TEST(DeleteAllHeapSnapshots) { |
| 771 | v8::HandleScope scope; |
| 772 | LocalContext env; |
| 773 | |
| 774 | CHECK_EQ(0, v8::HeapProfiler::GetSnapshotsCount()); |
| 775 | v8::HeapProfiler::DeleteAllSnapshots(); |
| 776 | CHECK_EQ(0, v8::HeapProfiler::GetSnapshotsCount()); |
| 777 | CHECK_NE(NULL, v8::HeapProfiler::TakeSnapshot(v8::String::New("1"))); |
| 778 | CHECK_EQ(1, v8::HeapProfiler::GetSnapshotsCount()); |
| 779 | v8::HeapProfiler::DeleteAllSnapshots(); |
| 780 | CHECK_EQ(0, v8::HeapProfiler::GetSnapshotsCount()); |
| 781 | CHECK_NE(NULL, v8::HeapProfiler::TakeSnapshot(v8::String::New("1"))); |
| 782 | CHECK_NE(NULL, v8::HeapProfiler::TakeSnapshot(v8::String::New("2"))); |
| 783 | CHECK_EQ(2, v8::HeapProfiler::GetSnapshotsCount()); |
| 784 | v8::HeapProfiler::DeleteAllSnapshots(); |
| 785 | CHECK_EQ(0, v8::HeapProfiler::GetSnapshotsCount()); |
| 786 | } |
| 787 | |
| 788 | |
| 789 | TEST(DeleteHeapSnapshot) { |
| 790 | v8::HandleScope scope; |
| 791 | LocalContext env; |
| 792 | |
| 793 | CHECK_EQ(0, v8::HeapProfiler::GetSnapshotsCount()); |
| 794 | const v8::HeapSnapshot* s1 = |
| 795 | v8::HeapProfiler::TakeSnapshot(v8::String::New("1")); |
| 796 | CHECK_NE(NULL, s1); |
| 797 | CHECK_EQ(1, v8::HeapProfiler::GetSnapshotsCount()); |
| 798 | unsigned uid1 = s1->GetUid(); |
| 799 | CHECK_EQ(s1, v8::HeapProfiler::FindSnapshot(uid1)); |
| 800 | const_cast<v8::HeapSnapshot*>(s1)->Delete(); |
| 801 | CHECK_EQ(0, v8::HeapProfiler::GetSnapshotsCount()); |
| 802 | CHECK_EQ(NULL, v8::HeapProfiler::FindSnapshot(uid1)); |
| 803 | |
| 804 | const v8::HeapSnapshot* s2 = |
| 805 | v8::HeapProfiler::TakeSnapshot(v8::String::New("2")); |
| 806 | CHECK_NE(NULL, s2); |
| 807 | CHECK_EQ(1, v8::HeapProfiler::GetSnapshotsCount()); |
| 808 | unsigned uid2 = s2->GetUid(); |
| 809 | CHECK_NE(static_cast<int>(uid1), static_cast<int>(uid2)); |
| 810 | CHECK_EQ(s2, v8::HeapProfiler::FindSnapshot(uid2)); |
| 811 | const v8::HeapSnapshot* s3 = |
| 812 | v8::HeapProfiler::TakeSnapshot(v8::String::New("3")); |
| 813 | CHECK_NE(NULL, s3); |
| 814 | CHECK_EQ(2, v8::HeapProfiler::GetSnapshotsCount()); |
| 815 | unsigned uid3 = s3->GetUid(); |
| 816 | CHECK_NE(static_cast<int>(uid1), static_cast<int>(uid3)); |
| 817 | CHECK_EQ(s3, v8::HeapProfiler::FindSnapshot(uid3)); |
| 818 | const_cast<v8::HeapSnapshot*>(s2)->Delete(); |
| 819 | CHECK_EQ(1, v8::HeapProfiler::GetSnapshotsCount()); |
| 820 | CHECK_EQ(NULL, v8::HeapProfiler::FindSnapshot(uid2)); |
| 821 | CHECK_EQ(s3, v8::HeapProfiler::FindSnapshot(uid3)); |
| 822 | const_cast<v8::HeapSnapshot*>(s3)->Delete(); |
| 823 | CHECK_EQ(0, v8::HeapProfiler::GetSnapshotsCount()); |
| 824 | CHECK_EQ(NULL, v8::HeapProfiler::FindSnapshot(uid3)); |
| 825 | } |
| 826 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame^] | 827 | |
| 828 | TEST(DocumentURL) { |
| 829 | v8::HandleScope scope; |
| 830 | LocalContext env; |
| 831 | |
| 832 | CompileRun("document = { URL:\"abcdefgh\" };"); |
| 833 | |
| 834 | const v8::HeapSnapshot* snapshot = |
| 835 | v8::HeapProfiler::TakeSnapshot(v8::String::New("document")); |
| 836 | const v8::HeapGraphNode* global = GetGlobalObject(snapshot); |
| 837 | CHECK_NE(NULL, global); |
| 838 | CHECK_EQ("Object / abcdefgh", |
| 839 | const_cast<i::HeapEntry*>( |
| 840 | reinterpret_cast<const i::HeapEntry*>(global))->name()); |
| 841 | } |
| 842 | |
| 843 | |
| 844 | TEST(DocumentWithException) { |
| 845 | v8::HandleScope scope; |
| 846 | LocalContext env; |
| 847 | |
| 848 | CompileRun( |
| 849 | "this.__defineGetter__(\"document\", function() { throw new Error(); })"); |
| 850 | const v8::HeapSnapshot* snapshot = |
| 851 | v8::HeapProfiler::TakeSnapshot(v8::String::New("document")); |
| 852 | const v8::HeapGraphNode* global = GetGlobalObject(snapshot); |
| 853 | CHECK_NE(NULL, global); |
| 854 | CHECK_EQ("Object", |
| 855 | const_cast<i::HeapEntry*>( |
| 856 | reinterpret_cast<const i::HeapEntry*>(global))->name()); |
| 857 | } |
| 858 | |
| 859 | |
| 860 | TEST(DocumentURLWithException) { |
| 861 | v8::HandleScope scope; |
| 862 | LocalContext env; |
| 863 | |
| 864 | CompileRun( |
| 865 | "function URLWithException() {}\n" |
| 866 | "URLWithException.prototype = { get URL() { throw new Error(); } };\n" |
| 867 | "document = { URL: new URLWithException() };"); |
| 868 | const v8::HeapSnapshot* snapshot = |
| 869 | v8::HeapProfiler::TakeSnapshot(v8::String::New("document")); |
| 870 | const v8::HeapGraphNode* global = GetGlobalObject(snapshot); |
| 871 | CHECK_NE(NULL, global); |
| 872 | CHECK_EQ("Object", |
| 873 | const_cast<i::HeapEntry*>( |
| 874 | reinterpret_cast<const i::HeapEntry*>(global))->name()); |
| 875 | } |
| 876 | |
| 877 | |
| 878 | TEST(NodesIteration) { |
| 879 | v8::HandleScope scope; |
| 880 | LocalContext env; |
| 881 | const v8::HeapSnapshot* snapshot = |
| 882 | v8::HeapProfiler::TakeSnapshot(v8::String::New("iteration")); |
| 883 | const v8::HeapGraphNode* global = GetGlobalObject(snapshot); |
| 884 | CHECK_NE(NULL, global); |
| 885 | // Verify that we can find this object by iteration. |
| 886 | const int nodes_count = snapshot->GetNodesCount(); |
| 887 | int count = 0; |
| 888 | for (int i = 0; i < nodes_count; ++i) { |
| 889 | if (snapshot->GetNode(i) == global) |
| 890 | ++count; |
| 891 | } |
| 892 | CHECK_EQ(1, count); |
| 893 | } |