Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 27 | // |
| 28 | // Tests for heap profiler |
| 29 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 30 | #include <ctype.h> |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 31 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 32 | #include "src/v8.h" |
| 33 | |
| 34 | #include "include/v8-profiler.h" |
| 35 | #include "src/allocation-tracker.h" |
| 36 | #include "src/debug.h" |
| 37 | #include "src/hashmap.h" |
| 38 | #include "src/heap-profiler.h" |
| 39 | #include "src/snapshot.h" |
| 40 | #include "src/utils-inl.h" |
| 41 | #include "test/cctest/cctest.h" |
| 42 | |
| 43 | using i::AllocationTraceNode; |
| 44 | using i::AllocationTraceTree; |
| 45 | using i::AllocationTracker; |
| 46 | using i::HashMap; |
| 47 | using i::Vector; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 48 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 49 | namespace { |
| 50 | |
| 51 | class NamedEntriesDetector { |
| 52 | public: |
| 53 | NamedEntriesDetector() |
Russell Brenner | 90bac25 | 2010-11-18 13:33:46 -0800 | [diff] [blame] | 54 | : has_A2(false), has_B2(false), has_C2(false) { |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 55 | } |
| 56 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 57 | void CheckEntry(i::HeapEntry* entry) { |
| 58 | if (strcmp(entry->name(), "A2") == 0) has_A2 = true; |
| 59 | if (strcmp(entry->name(), "B2") == 0) has_B2 = true; |
| 60 | if (strcmp(entry->name(), "C2") == 0) has_C2 = true; |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 61 | } |
| 62 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 63 | static bool AddressesMatch(void* key1, void* key2) { |
| 64 | return key1 == key2; |
| 65 | } |
| 66 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 67 | void CheckAllReachables(i::HeapEntry* root) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 68 | i::HashMap visited(AddressesMatch); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 69 | i::List<i::HeapEntry*> list(10); |
| 70 | list.Add(root); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 71 | CheckEntry(root); |
| 72 | while (!list.is_empty()) { |
| 73 | i::HeapEntry* entry = list.RemoveLast(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 74 | i::Vector<i::HeapGraphEdge*> children = entry->children(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 75 | for (int i = 0; i < children.length(); ++i) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 76 | if (children[i]->type() == i::HeapGraphEdge::kShortcut) continue; |
| 77 | i::HeapEntry* child = children[i]->to(); |
| 78 | i::HashMap::Entry* entry = visited.Lookup( |
| 79 | reinterpret_cast<void*>(child), |
| 80 | static_cast<uint32_t>(reinterpret_cast<uintptr_t>(child)), |
| 81 | true); |
| 82 | if (entry->value) |
| 83 | continue; |
| 84 | entry->value = reinterpret_cast<void*>(1); |
| 85 | list.Add(child); |
| 86 | CheckEntry(child); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 87 | } |
| 88 | } |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 89 | } |
| 90 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 91 | bool has_A2; |
| 92 | bool has_B2; |
| 93 | bool has_C2; |
| 94 | }; |
| 95 | |
| 96 | } // namespace |
| 97 | |
| 98 | |
| 99 | static const v8::HeapGraphNode* GetGlobalObject( |
| 100 | const v8::HeapSnapshot* snapshot) { |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 101 | CHECK_EQ(2, snapshot->GetRoot()->GetChildrenCount()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 102 | // The 0th-child is (GC Roots), 1st is the user root. |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 103 | const v8::HeapGraphNode* global_obj = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 104 | snapshot->GetRoot()->GetChild(1)->GetToNode(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 105 | CHECK_EQ(0, strncmp("Object", const_cast<i::HeapEntry*>( |
| 106 | reinterpret_cast<const i::HeapEntry*>(global_obj))->name(), 6)); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 107 | return global_obj; |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | |
| 111 | static const v8::HeapGraphNode* GetProperty(const v8::HeapGraphNode* node, |
| 112 | v8::HeapGraphEdge::Type type, |
| 113 | const char* name) { |
| 114 | for (int i = 0, count = node->GetChildrenCount(); i < count; ++i) { |
| 115 | const v8::HeapGraphEdge* prop = node->GetChild(i); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 116 | v8::String::Utf8Value prop_name(prop->GetName()); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 117 | if (prop->GetType() == type && strcmp(name, *prop_name) == 0) |
| 118 | return prop->GetToNode(); |
| 119 | } |
| 120 | return NULL; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | static bool HasString(const v8::HeapGraphNode* node, const char* contents) { |
| 125 | for (int i = 0, count = node->GetChildrenCount(); i < count; ++i) { |
| 126 | const v8::HeapGraphEdge* prop = node->GetChild(i); |
| 127 | const v8::HeapGraphNode* node = prop->GetToNode(); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 128 | if (node->GetType() == v8::HeapGraphNode::kString) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 129 | v8::String::Utf8Value node_name(node->GetName()); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 130 | if (strcmp(contents, *node_name) == 0) return true; |
| 131 | } |
| 132 | } |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 137 | static bool AddressesMatch(void* key1, void* key2) { |
| 138 | return key1 == key2; |
| 139 | } |
| 140 | |
| 141 | |
| 142 | // Check that snapshot has no unretained entries except root. |
| 143 | static bool ValidateSnapshot(const v8::HeapSnapshot* snapshot, int depth = 3) { |
| 144 | i::HeapSnapshot* heap_snapshot = const_cast<i::HeapSnapshot*>( |
| 145 | reinterpret_cast<const i::HeapSnapshot*>(snapshot)); |
| 146 | |
| 147 | i::HashMap visited(AddressesMatch); |
| 148 | i::List<i::HeapGraphEdge>& edges = heap_snapshot->edges(); |
| 149 | for (int i = 0; i < edges.length(); ++i) { |
| 150 | i::HashMap::Entry* entry = visited.Lookup( |
| 151 | reinterpret_cast<void*>(edges[i].to()), |
| 152 | static_cast<uint32_t>(reinterpret_cast<uintptr_t>(edges[i].to())), |
| 153 | true); |
| 154 | uint32_t ref_count = static_cast<uint32_t>( |
| 155 | reinterpret_cast<uintptr_t>(entry->value)); |
| 156 | entry->value = reinterpret_cast<void*>(ref_count + 1); |
| 157 | } |
| 158 | uint32_t unretained_entries_count = 0; |
| 159 | i::List<i::HeapEntry>& entries = heap_snapshot->entries(); |
| 160 | for (int i = 0; i < entries.length(); ++i) { |
| 161 | i::HashMap::Entry* entry = visited.Lookup( |
| 162 | reinterpret_cast<void*>(&entries[i]), |
| 163 | static_cast<uint32_t>(reinterpret_cast<uintptr_t>(&entries[i])), |
| 164 | false); |
| 165 | if (!entry && entries[i].id() != 1) { |
| 166 | entries[i].Print("entry with no retainer", "", depth, 0); |
| 167 | ++unretained_entries_count; |
| 168 | } |
| 169 | } |
| 170 | return unretained_entries_count == 0; |
| 171 | } |
| 172 | |
| 173 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 174 | TEST(HeapSnapshot) { |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 175 | LocalContext env2; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 176 | v8::HandleScope scope(env2->GetIsolate()); |
| 177 | v8::HeapProfiler* heap_profiler = env2->GetIsolate()->GetHeapProfiler(); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 178 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 179 | CompileRun( |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 180 | "function A2() {}\n" |
| 181 | "function B2(x) { return function() { return typeof x; }; }\n" |
| 182 | "function C2(x) { this.x1 = x; this.x2 = x; this[1] = x; }\n" |
| 183 | "var a2 = new A2();\n" |
| 184 | "var b2_1 = new B2(a2), b2_2 = new B2(a2);\n" |
| 185 | "var c2 = new C2(a2);"); |
| 186 | const v8::HeapSnapshot* snapshot_env2 = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 187 | heap_profiler->TakeHeapSnapshot(v8_str("env2")); |
| 188 | CHECK(ValidateSnapshot(snapshot_env2)); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 189 | const v8::HeapGraphNode* global_env2 = GetGlobalObject(snapshot_env2); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 190 | |
Russell Brenner | 90bac25 | 2010-11-18 13:33:46 -0800 | [diff] [blame] | 191 | // Verify, that JS global object of env2 has '..2' properties. |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 192 | const v8::HeapGraphNode* a2_node = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 193 | GetProperty(global_env2, v8::HeapGraphEdge::kProperty, "a2"); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 194 | CHECK_NE(NULL, a2_node); |
| 195 | CHECK_NE( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 196 | NULL, GetProperty(global_env2, v8::HeapGraphEdge::kProperty, "b2_1")); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 197 | CHECK_NE( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 198 | NULL, GetProperty(global_env2, v8::HeapGraphEdge::kProperty, "b2_2")); |
| 199 | CHECK_NE(NULL, GetProperty(global_env2, v8::HeapGraphEdge::kProperty, "c2")); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 200 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 201 | NamedEntriesDetector det; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 202 | det.CheckAllReachables(const_cast<i::HeapEntry*>( |
| 203 | reinterpret_cast<const i::HeapEntry*>(global_env2))); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 204 | CHECK(det.has_A2); |
| 205 | CHECK(det.has_B2); |
| 206 | CHECK(det.has_C2); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 210 | TEST(HeapSnapshotObjectSizes) { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 211 | LocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 212 | v8::HandleScope scope(env->GetIsolate()); |
| 213 | v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 214 | |
| 215 | // -a-> X1 --a |
| 216 | // x -b-> X2 <-| |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 217 | CompileRun( |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 218 | "function X(a, b) { this.a = a; this.b = b; }\n" |
| 219 | "x = new X(new X(), new X());\n" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 220 | "dummy = new X();\n" |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 221 | "(function() { x.a.a = x.b; })();"); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 222 | const v8::HeapSnapshot* snapshot = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 223 | heap_profiler->TakeHeapSnapshot(v8_str("sizes")); |
| 224 | CHECK(ValidateSnapshot(snapshot)); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 225 | const v8::HeapGraphNode* global = GetGlobalObject(snapshot); |
| 226 | const v8::HeapGraphNode* x = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 227 | GetProperty(global, v8::HeapGraphEdge::kProperty, "x"); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 228 | CHECK_NE(NULL, x); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 229 | const v8::HeapGraphNode* x1 = |
| 230 | GetProperty(x, v8::HeapGraphEdge::kProperty, "a"); |
| 231 | CHECK_NE(NULL, x1); |
| 232 | const v8::HeapGraphNode* x2 = |
| 233 | GetProperty(x, v8::HeapGraphEdge::kProperty, "b"); |
| 234 | CHECK_NE(NULL, x2); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 235 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 236 | // Test sizes. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 237 | CHECK_NE(0, static_cast<int>(x->GetShallowSize())); |
| 238 | CHECK_NE(0, static_cast<int>(x1->GetShallowSize())); |
| 239 | CHECK_NE(0, static_cast<int>(x2->GetShallowSize())); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | |
| 243 | TEST(BoundFunctionInSnapshot) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 244 | LocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 245 | v8::HandleScope scope(env->GetIsolate()); |
| 246 | v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 247 | CompileRun( |
| 248 | "function myFunction(a, b) { this.a = a; this.b = b; }\n" |
| 249 | "function AAAAA() {}\n" |
| 250 | "boundFunction = myFunction.bind(new AAAAA(), 20, new Number(12)); \n"); |
| 251 | const v8::HeapSnapshot* snapshot = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 252 | heap_profiler->TakeHeapSnapshot(v8_str("sizes")); |
| 253 | CHECK(ValidateSnapshot(snapshot)); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 254 | const v8::HeapGraphNode* global = GetGlobalObject(snapshot); |
| 255 | const v8::HeapGraphNode* f = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 256 | GetProperty(global, v8::HeapGraphEdge::kProperty, "boundFunction"); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 257 | CHECK(f); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 258 | CHECK_EQ(v8::String::NewFromUtf8(env->GetIsolate(), "native_bind"), |
| 259 | f->GetName()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 260 | const v8::HeapGraphNode* bindings = |
| 261 | GetProperty(f, v8::HeapGraphEdge::kInternal, "bindings"); |
| 262 | CHECK_NE(NULL, bindings); |
| 263 | CHECK_EQ(v8::HeapGraphNode::kArray, bindings->GetType()); |
| 264 | CHECK_EQ(4, bindings->GetChildrenCount()); |
| 265 | |
| 266 | const v8::HeapGraphNode* bound_this = GetProperty( |
| 267 | f, v8::HeapGraphEdge::kShortcut, "bound_this"); |
| 268 | CHECK(bound_this); |
| 269 | CHECK_EQ(v8::HeapGraphNode::kObject, bound_this->GetType()); |
| 270 | |
| 271 | const v8::HeapGraphNode* bound_function = GetProperty( |
| 272 | f, v8::HeapGraphEdge::kShortcut, "bound_function"); |
| 273 | CHECK(bound_function); |
| 274 | CHECK_EQ(v8::HeapGraphNode::kClosure, bound_function->GetType()); |
| 275 | |
| 276 | const v8::HeapGraphNode* bound_argument = GetProperty( |
| 277 | f, v8::HeapGraphEdge::kShortcut, "bound_argument_1"); |
| 278 | CHECK(bound_argument); |
| 279 | CHECK_EQ(v8::HeapGraphNode::kObject, bound_argument->GetType()); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | |
| 283 | TEST(HeapSnapshotEntryChildren) { |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 284 | LocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 285 | v8::HandleScope scope(env->GetIsolate()); |
| 286 | v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 287 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 288 | CompileRun( |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 289 | "function A() { }\n" |
| 290 | "a = new A;"); |
| 291 | const v8::HeapSnapshot* snapshot = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 292 | heap_profiler->TakeHeapSnapshot(v8_str("children")); |
| 293 | CHECK(ValidateSnapshot(snapshot)); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 294 | const v8::HeapGraphNode* global = GetGlobalObject(snapshot); |
| 295 | for (int i = 0, count = global->GetChildrenCount(); i < count; ++i) { |
| 296 | const v8::HeapGraphEdge* prop = global->GetChild(i); |
| 297 | CHECK_EQ(global, prop->GetFromNode()); |
| 298 | } |
| 299 | const v8::HeapGraphNode* a = |
| 300 | GetProperty(global, v8::HeapGraphEdge::kProperty, "a"); |
| 301 | CHECK_NE(NULL, a); |
| 302 | for (int i = 0, count = a->GetChildrenCount(); i < count; ++i) { |
| 303 | const v8::HeapGraphEdge* prop = a->GetChild(i); |
| 304 | CHECK_EQ(a, prop->GetFromNode()); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 309 | TEST(HeapSnapshotCodeObjects) { |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 310 | LocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 311 | v8::HandleScope scope(env->GetIsolate()); |
| 312 | v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 313 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 314 | CompileRun( |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 315 | "function lazy(x) { return x - 1; }\n" |
| 316 | "function compiled(x) { return x + 1; }\n" |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 317 | "var anonymous = (function() { return function() { return 0; } })();\n" |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 318 | "compiled(1)"); |
| 319 | const v8::HeapSnapshot* snapshot = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 320 | heap_profiler->TakeHeapSnapshot(v8_str("code")); |
| 321 | CHECK(ValidateSnapshot(snapshot)); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 322 | |
| 323 | const v8::HeapGraphNode* global = GetGlobalObject(snapshot); |
| 324 | const v8::HeapGraphNode* compiled = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 325 | GetProperty(global, v8::HeapGraphEdge::kProperty, "compiled"); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 326 | CHECK_NE(NULL, compiled); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 327 | CHECK_EQ(v8::HeapGraphNode::kClosure, compiled->GetType()); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 328 | const v8::HeapGraphNode* lazy = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 329 | GetProperty(global, v8::HeapGraphEdge::kProperty, "lazy"); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 330 | CHECK_NE(NULL, lazy); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 331 | CHECK_EQ(v8::HeapGraphNode::kClosure, lazy->GetType()); |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 332 | const v8::HeapGraphNode* anonymous = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 333 | GetProperty(global, v8::HeapGraphEdge::kProperty, "anonymous"); |
Steve Block | 791712a | 2010-08-27 10:21:07 +0100 | [diff] [blame] | 334 | CHECK_NE(NULL, anonymous); |
| 335 | CHECK_EQ(v8::HeapGraphNode::kClosure, anonymous->GetType()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 336 | v8::String::Utf8Value anonymous_name(anonymous->GetName()); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 337 | CHECK_EQ("", *anonymous_name); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 338 | |
| 339 | // Find references to code. |
| 340 | const v8::HeapGraphNode* compiled_code = |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 341 | GetProperty(compiled, v8::HeapGraphEdge::kInternal, "shared"); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 342 | CHECK_NE(NULL, compiled_code); |
| 343 | const v8::HeapGraphNode* lazy_code = |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 344 | GetProperty(lazy, v8::HeapGraphEdge::kInternal, "shared"); |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 345 | CHECK_NE(NULL, lazy_code); |
| 346 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 347 | // Check that there's no strong next_code_link. There might be a weak one |
| 348 | // but might be not, so we can't check that fact. |
| 349 | const v8::HeapGraphNode* code = |
| 350 | GetProperty(compiled_code, v8::HeapGraphEdge::kInternal, "code"); |
| 351 | CHECK_NE(NULL, code); |
| 352 | const v8::HeapGraphNode* next_code_link = |
| 353 | GetProperty(code, v8::HeapGraphEdge::kInternal, "code"); |
| 354 | CHECK_EQ(NULL, next_code_link); |
| 355 | |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 356 | // Verify that non-compiled code doesn't contain references to "x" |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 357 | // literal, while compiled code does. The scope info is stored in FixedArray |
| 358 | // objects attached to the SharedFunctionInfo. |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 359 | bool compiled_references_x = false, lazy_references_x = false; |
| 360 | for (int i = 0, count = compiled_code->GetChildrenCount(); i < count; ++i) { |
| 361 | const v8::HeapGraphEdge* prop = compiled_code->GetChild(i); |
| 362 | const v8::HeapGraphNode* node = prop->GetToNode(); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 363 | if (node->GetType() == v8::HeapGraphNode::kArray) { |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 364 | if (HasString(node, "x")) { |
| 365 | compiled_references_x = true; |
| 366 | break; |
| 367 | } |
| 368 | } |
| 369 | } |
| 370 | for (int i = 0, count = lazy_code->GetChildrenCount(); i < count; ++i) { |
| 371 | const v8::HeapGraphEdge* prop = lazy_code->GetChild(i); |
| 372 | const v8::HeapGraphNode* node = prop->GetToNode(); |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 373 | if (node->GetType() == v8::HeapGraphNode::kArray) { |
Kristian Monsen | 9dcf7e2 | 2010-06-28 14:14:28 +0100 | [diff] [blame] | 374 | if (HasString(node, "x")) { |
| 375 | lazy_references_x = true; |
| 376 | break; |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | CHECK(compiled_references_x); |
| 381 | CHECK(!lazy_references_x); |
| 382 | } |
| 383 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 384 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 385 | TEST(HeapSnapshotHeapNumbers) { |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 386 | LocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 387 | v8::HandleScope scope(env->GetIsolate()); |
| 388 | v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 389 | CompileRun( |
| 390 | "a = 1; // a is Smi\n" |
| 391 | "b = 2.5; // b is HeapNumber"); |
| 392 | const v8::HeapSnapshot* snapshot = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 393 | heap_profiler->TakeHeapSnapshot(v8_str("numbers")); |
| 394 | CHECK(ValidateSnapshot(snapshot)); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 395 | const v8::HeapGraphNode* global = GetGlobalObject(snapshot); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 396 | CHECK_EQ(NULL, GetProperty(global, v8::HeapGraphEdge::kProperty, "a")); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 397 | const v8::HeapGraphNode* b = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 398 | GetProperty(global, v8::HeapGraphEdge::kProperty, "b"); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 399 | CHECK_NE(NULL, b); |
| 400 | CHECK_EQ(v8::HeapGraphNode::kHeapNumber, b->GetType()); |
| 401 | } |
| 402 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 403 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 404 | TEST(HeapSnapshotSlicedString) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 405 | LocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 406 | v8::HandleScope scope(env->GetIsolate()); |
| 407 | v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 408 | CompileRun( |
| 409 | "parent_string = \"123456789.123456789.123456789.123456789.123456789." |
| 410 | "123456789.123456789.123456789.123456789.123456789." |
| 411 | "123456789.123456789.123456789.123456789.123456789." |
| 412 | "123456789.123456789.123456789.123456789.123456789.\";" |
| 413 | "child_string = parent_string.slice(100);"); |
| 414 | const v8::HeapSnapshot* snapshot = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 415 | heap_profiler->TakeHeapSnapshot(v8_str("strings")); |
| 416 | CHECK(ValidateSnapshot(snapshot)); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 417 | const v8::HeapGraphNode* global = GetGlobalObject(snapshot); |
| 418 | const v8::HeapGraphNode* parent_string = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 419 | GetProperty(global, v8::HeapGraphEdge::kProperty, "parent_string"); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 420 | CHECK_NE(NULL, parent_string); |
| 421 | const v8::HeapGraphNode* child_string = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 422 | GetProperty(global, v8::HeapGraphEdge::kProperty, "child_string"); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 423 | CHECK_NE(NULL, child_string); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 424 | CHECK_EQ(v8::HeapGraphNode::kSlicedString, child_string->GetType()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 425 | const v8::HeapGraphNode* parent = |
| 426 | GetProperty(child_string, v8::HeapGraphEdge::kInternal, "parent"); |
| 427 | CHECK_EQ(parent_string, parent); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 428 | heap_profiler->DeleteAllHeapSnapshots(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 429 | } |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 430 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 431 | |
| 432 | TEST(HeapSnapshotConsString) { |
| 433 | v8::Isolate* isolate = CcTest::isolate(); |
| 434 | v8::HandleScope scope(isolate); |
| 435 | v8::Local<v8::ObjectTemplate> global_template = |
| 436 | v8::ObjectTemplate::New(isolate); |
| 437 | global_template->SetInternalFieldCount(1); |
| 438 | LocalContext env(NULL, global_template); |
| 439 | v8::Handle<v8::Object> global_proxy = env->Global(); |
| 440 | v8::Handle<v8::Object> global = global_proxy->GetPrototype().As<v8::Object>(); |
| 441 | CHECK_EQ(1, global->InternalFieldCount()); |
| 442 | |
| 443 | i::Factory* factory = CcTest::i_isolate()->factory(); |
| 444 | i::Handle<i::String> first = factory->NewStringFromStaticChars("0123456789"); |
| 445 | i::Handle<i::String> second = factory->NewStringFromStaticChars("0123456789"); |
| 446 | i::Handle<i::String> cons_string = |
| 447 | factory->NewConsString(first, second).ToHandleChecked(); |
| 448 | |
| 449 | global->SetInternalField(0, v8::ToApiHandle<v8::String>(cons_string)); |
| 450 | |
| 451 | v8::HeapProfiler* heap_profiler = isolate->GetHeapProfiler(); |
| 452 | const v8::HeapSnapshot* snapshot = |
| 453 | heap_profiler->TakeHeapSnapshot(v8_str("cons_strings")); |
| 454 | CHECK(ValidateSnapshot(snapshot)); |
| 455 | const v8::HeapGraphNode* global_node = GetGlobalObject(snapshot); |
| 456 | |
| 457 | const v8::HeapGraphNode* string_node = |
| 458 | GetProperty(global_node, v8::HeapGraphEdge::kInternal, "0"); |
| 459 | CHECK_NE(NULL, string_node); |
| 460 | CHECK_EQ(v8::HeapGraphNode::kConsString, string_node->GetType()); |
| 461 | |
| 462 | const v8::HeapGraphNode* first_node = |
| 463 | GetProperty(string_node, v8::HeapGraphEdge::kInternal, "first"); |
| 464 | CHECK_EQ(v8::HeapGraphNode::kString, first_node->GetType()); |
| 465 | |
| 466 | const v8::HeapGraphNode* second_node = |
| 467 | GetProperty(string_node, v8::HeapGraphEdge::kInternal, "second"); |
| 468 | CHECK_EQ(v8::HeapGraphNode::kString, second_node->GetType()); |
| 469 | |
| 470 | heap_profiler->DeleteAllHeapSnapshots(); |
| 471 | } |
| 472 | |
| 473 | |
| 474 | TEST(HeapSnapshotSymbol) { |
| 475 | LocalContext env; |
| 476 | v8::HandleScope scope(env->GetIsolate()); |
| 477 | v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
| 478 | |
| 479 | CompileRun("a = Symbol('mySymbol');\n"); |
| 480 | const v8::HeapSnapshot* snapshot = |
| 481 | heap_profiler->TakeHeapSnapshot(v8_str("Symbol")); |
| 482 | CHECK(ValidateSnapshot(snapshot)); |
| 483 | const v8::HeapGraphNode* global = GetGlobalObject(snapshot); |
| 484 | const v8::HeapGraphNode* a = |
| 485 | GetProperty(global, v8::HeapGraphEdge::kProperty, "a"); |
| 486 | CHECK_NE(NULL, a); |
| 487 | CHECK_EQ(a->GetType(), v8::HeapGraphNode::kSymbol); |
| 488 | CHECK_EQ(v8_str("symbol"), a->GetName()); |
| 489 | const v8::HeapGraphNode* name = |
| 490 | GetProperty(a, v8::HeapGraphEdge::kInternal, "name"); |
| 491 | CHECK_NE(NULL, name); |
| 492 | CHECK_EQ(v8_str("mySymbol"), name->GetName()); |
| 493 | } |
| 494 | |
| 495 | |
| 496 | TEST(HeapSnapshotWeakCollection) { |
| 497 | LocalContext env; |
| 498 | v8::HandleScope scope(env->GetIsolate()); |
| 499 | v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
| 500 | |
| 501 | CompileRun( |
| 502 | "k = {}; v = {}; s = 'str';\n" |
| 503 | "ws = new WeakSet(); ws.add(k); ws.add(v); ws[s] = s;\n" |
| 504 | "wm = new WeakMap(); wm.set(k, v); wm[s] = s;\n"); |
| 505 | const v8::HeapSnapshot* snapshot = |
| 506 | heap_profiler->TakeHeapSnapshot(v8_str("WeakCollections")); |
| 507 | CHECK(ValidateSnapshot(snapshot)); |
| 508 | const v8::HeapGraphNode* global = GetGlobalObject(snapshot); |
| 509 | const v8::HeapGraphNode* k = |
| 510 | GetProperty(global, v8::HeapGraphEdge::kProperty, "k"); |
| 511 | CHECK_NE(NULL, k); |
| 512 | const v8::HeapGraphNode* v = |
| 513 | GetProperty(global, v8::HeapGraphEdge::kProperty, "v"); |
| 514 | CHECK_NE(NULL, v); |
| 515 | const v8::HeapGraphNode* s = |
| 516 | GetProperty(global, v8::HeapGraphEdge::kProperty, "s"); |
| 517 | CHECK_NE(NULL, s); |
| 518 | |
| 519 | const v8::HeapGraphNode* ws = |
| 520 | GetProperty(global, v8::HeapGraphEdge::kProperty, "ws"); |
| 521 | CHECK_NE(NULL, ws); |
| 522 | CHECK_EQ(v8::HeapGraphNode::kObject, ws->GetType()); |
| 523 | CHECK_EQ(v8_str("WeakSet"), ws->GetName()); |
| 524 | |
| 525 | const v8::HeapGraphNode* ws_table = |
| 526 | GetProperty(ws, v8::HeapGraphEdge::kInternal, "table"); |
| 527 | CHECK_EQ(v8::HeapGraphNode::kArray, ws_table->GetType()); |
| 528 | CHECK_GT(ws_table->GetChildrenCount(), 0); |
| 529 | int weak_entries = 0; |
| 530 | for (int i = 0, count = ws_table->GetChildrenCount(); i < count; ++i) { |
| 531 | const v8::HeapGraphEdge* prop = ws_table->GetChild(i); |
| 532 | if (prop->GetType() != v8::HeapGraphEdge::kWeak) continue; |
| 533 | if (k->GetId() == prop->GetToNode()->GetId()) { |
| 534 | ++weak_entries; |
| 535 | } |
| 536 | } |
| 537 | CHECK_EQ(1, weak_entries); |
| 538 | const v8::HeapGraphNode* ws_s = |
| 539 | GetProperty(ws, v8::HeapGraphEdge::kProperty, "str"); |
| 540 | CHECK_NE(NULL, ws_s); |
| 541 | CHECK_EQ(static_cast<int>(s->GetId()), static_cast<int>(ws_s->GetId())); |
| 542 | |
| 543 | const v8::HeapGraphNode* wm = |
| 544 | GetProperty(global, v8::HeapGraphEdge::kProperty, "wm"); |
| 545 | CHECK_NE(NULL, wm); |
| 546 | CHECK_EQ(v8::HeapGraphNode::kObject, wm->GetType()); |
| 547 | CHECK_EQ(v8_str("WeakMap"), wm->GetName()); |
| 548 | |
| 549 | const v8::HeapGraphNode* wm_table = |
| 550 | GetProperty(wm, v8::HeapGraphEdge::kInternal, "table"); |
| 551 | CHECK_EQ(v8::HeapGraphNode::kArray, wm_table->GetType()); |
| 552 | CHECK_GT(wm_table->GetChildrenCount(), 0); |
| 553 | weak_entries = 0; |
| 554 | for (int i = 0, count = wm_table->GetChildrenCount(); i < count; ++i) { |
| 555 | const v8::HeapGraphEdge* prop = wm_table->GetChild(i); |
| 556 | if (prop->GetType() != v8::HeapGraphEdge::kWeak) continue; |
| 557 | const v8::SnapshotObjectId to_node_id = prop->GetToNode()->GetId(); |
| 558 | if (to_node_id == k->GetId() || to_node_id == v->GetId()) { |
| 559 | ++weak_entries; |
| 560 | } |
| 561 | } |
| 562 | CHECK_EQ(2, weak_entries); |
| 563 | const v8::HeapGraphNode* wm_s = |
| 564 | GetProperty(wm, v8::HeapGraphEdge::kProperty, "str"); |
| 565 | CHECK_NE(NULL, wm_s); |
| 566 | CHECK_EQ(static_cast<int>(s->GetId()), static_cast<int>(wm_s->GetId())); |
| 567 | } |
| 568 | |
| 569 | |
| 570 | TEST(HeapSnapshotCollection) { |
| 571 | LocalContext env; |
| 572 | v8::HandleScope scope(env->GetIsolate()); |
| 573 | v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
| 574 | |
| 575 | CompileRun( |
| 576 | "k = {}; v = {}; s = 'str';\n" |
| 577 | "set = new Set(); set.add(k); set.add(v); set[s] = s;\n" |
| 578 | "map = new Map(); map.set(k, v); map[s] = s;\n"); |
| 579 | const v8::HeapSnapshot* snapshot = |
| 580 | heap_profiler->TakeHeapSnapshot(v8_str("Collections")); |
| 581 | CHECK(ValidateSnapshot(snapshot)); |
| 582 | const v8::HeapGraphNode* global = GetGlobalObject(snapshot); |
| 583 | const v8::HeapGraphNode* k = |
| 584 | GetProperty(global, v8::HeapGraphEdge::kProperty, "k"); |
| 585 | CHECK_NE(NULL, k); |
| 586 | const v8::HeapGraphNode* v = |
| 587 | GetProperty(global, v8::HeapGraphEdge::kProperty, "v"); |
| 588 | CHECK_NE(NULL, v); |
| 589 | const v8::HeapGraphNode* s = |
| 590 | GetProperty(global, v8::HeapGraphEdge::kProperty, "s"); |
| 591 | CHECK_NE(NULL, s); |
| 592 | |
| 593 | const v8::HeapGraphNode* set = |
| 594 | GetProperty(global, v8::HeapGraphEdge::kProperty, "set"); |
| 595 | CHECK_NE(NULL, set); |
| 596 | CHECK_EQ(v8::HeapGraphNode::kObject, set->GetType()); |
| 597 | CHECK_EQ(v8_str("Set"), set->GetName()); |
| 598 | |
| 599 | const v8::HeapGraphNode* set_table = |
| 600 | GetProperty(set, v8::HeapGraphEdge::kInternal, "table"); |
| 601 | CHECK_EQ(v8::HeapGraphNode::kArray, set_table->GetType()); |
| 602 | CHECK_GT(set_table->GetChildrenCount(), 0); |
| 603 | int entries = 0; |
| 604 | for (int i = 0, count = set_table->GetChildrenCount(); i < count; ++i) { |
| 605 | const v8::HeapGraphEdge* prop = set_table->GetChild(i); |
| 606 | const v8::SnapshotObjectId to_node_id = prop->GetToNode()->GetId(); |
| 607 | if (to_node_id == k->GetId() || to_node_id == v->GetId()) { |
| 608 | ++entries; |
| 609 | } |
| 610 | } |
| 611 | CHECK_EQ(2, entries); |
| 612 | const v8::HeapGraphNode* set_s = |
| 613 | GetProperty(set, v8::HeapGraphEdge::kProperty, "str"); |
| 614 | CHECK_NE(NULL, set_s); |
| 615 | CHECK_EQ(static_cast<int>(s->GetId()), static_cast<int>(set_s->GetId())); |
| 616 | |
| 617 | const v8::HeapGraphNode* map = |
| 618 | GetProperty(global, v8::HeapGraphEdge::kProperty, "map"); |
| 619 | CHECK_NE(NULL, map); |
| 620 | CHECK_EQ(v8::HeapGraphNode::kObject, map->GetType()); |
| 621 | CHECK_EQ(v8_str("Map"), map->GetName()); |
| 622 | |
| 623 | const v8::HeapGraphNode* map_table = |
| 624 | GetProperty(map, v8::HeapGraphEdge::kInternal, "table"); |
| 625 | CHECK_EQ(v8::HeapGraphNode::kArray, map_table->GetType()); |
| 626 | CHECK_GT(map_table->GetChildrenCount(), 0); |
| 627 | entries = 0; |
| 628 | for (int i = 0, count = map_table->GetChildrenCount(); i < count; ++i) { |
| 629 | const v8::HeapGraphEdge* prop = map_table->GetChild(i); |
| 630 | const v8::SnapshotObjectId to_node_id = prop->GetToNode()->GetId(); |
| 631 | if (to_node_id == k->GetId() || to_node_id == v->GetId()) { |
| 632 | ++entries; |
| 633 | } |
| 634 | } |
| 635 | CHECK_EQ(2, entries); |
| 636 | const v8::HeapGraphNode* map_s = |
| 637 | GetProperty(map, v8::HeapGraphEdge::kProperty, "str"); |
| 638 | CHECK_NE(NULL, map_s); |
| 639 | CHECK_EQ(static_cast<int>(s->GetId()), static_cast<int>(map_s->GetId())); |
| 640 | } |
| 641 | |
| 642 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 643 | TEST(HeapSnapshotInternalReferences) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 644 | v8::Isolate* isolate = CcTest::isolate(); |
| 645 | v8::HandleScope scope(isolate); |
| 646 | v8::Local<v8::ObjectTemplate> global_template = |
| 647 | v8::ObjectTemplate::New(isolate); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 648 | global_template->SetInternalFieldCount(2); |
| 649 | LocalContext env(NULL, global_template); |
| 650 | v8::Handle<v8::Object> global_proxy = env->Global(); |
| 651 | v8::Handle<v8::Object> global = global_proxy->GetPrototype().As<v8::Object>(); |
| 652 | CHECK_EQ(2, global->InternalFieldCount()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 653 | v8::Local<v8::Object> obj = v8::Object::New(isolate); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 654 | global->SetInternalField(0, v8_num(17)); |
| 655 | global->SetInternalField(1, obj); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 656 | v8::HeapProfiler* heap_profiler = isolate->GetHeapProfiler(); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 657 | const v8::HeapSnapshot* snapshot = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 658 | heap_profiler->TakeHeapSnapshot(v8_str("internals")); |
| 659 | CHECK(ValidateSnapshot(snapshot)); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 660 | const v8::HeapGraphNode* global_node = GetGlobalObject(snapshot); |
| 661 | // The first reference will not present, because it's a Smi. |
| 662 | CHECK_EQ(NULL, GetProperty(global_node, v8::HeapGraphEdge::kInternal, "0")); |
| 663 | // The second reference is to an object. |
| 664 | CHECK_NE(NULL, GetProperty(global_node, v8::HeapGraphEdge::kInternal, "1")); |
| 665 | } |
| 666 | |
| 667 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 668 | // Trying to introduce a check helper for uint32_t causes many |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 669 | // overloading ambiguities, so it seems easier just to cast |
| 670 | // them to a signed type. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 671 | #define CHECK_EQ_SNAPSHOT_OBJECT_ID(a, b) \ |
| 672 | CHECK_EQ(static_cast<int32_t>(a), static_cast<int32_t>(b)) |
| 673 | #define CHECK_NE_SNAPSHOT_OBJECT_ID(a, b) \ |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 674 | CHECK((a) != (b)) // NOLINT |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 675 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 676 | TEST(HeapSnapshotAddressReuse) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 677 | LocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 678 | v8::HandleScope scope(env->GetIsolate()); |
| 679 | v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
| 680 | |
| 681 | CompileRun( |
| 682 | "function A() {}\n" |
| 683 | "var a = [];\n" |
| 684 | "for (var i = 0; i < 10000; ++i)\n" |
| 685 | " a[i] = new A();\n"); |
| 686 | const v8::HeapSnapshot* snapshot1 = |
| 687 | heap_profiler->TakeHeapSnapshot(v8_str("snapshot1")); |
| 688 | CHECK(ValidateSnapshot(snapshot1)); |
| 689 | v8::SnapshotObjectId maxId1 = snapshot1->GetMaxSnapshotJSObjectId(); |
| 690 | |
| 691 | CompileRun( |
| 692 | "for (var i = 0; i < 10000; ++i)\n" |
| 693 | " a[i] = new A();\n"); |
| 694 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
| 695 | |
| 696 | const v8::HeapSnapshot* snapshot2 = |
| 697 | heap_profiler->TakeHeapSnapshot(v8_str("snapshot2")); |
| 698 | CHECK(ValidateSnapshot(snapshot2)); |
| 699 | const v8::HeapGraphNode* global2 = GetGlobalObject(snapshot2); |
| 700 | |
| 701 | const v8::HeapGraphNode* array_node = |
| 702 | GetProperty(global2, v8::HeapGraphEdge::kProperty, "a"); |
| 703 | CHECK_NE(NULL, array_node); |
| 704 | int wrong_count = 0; |
| 705 | for (int i = 0, count = array_node->GetChildrenCount(); i < count; ++i) { |
| 706 | const v8::HeapGraphEdge* prop = array_node->GetChild(i); |
| 707 | if (prop->GetType() != v8::HeapGraphEdge::kElement) |
| 708 | continue; |
| 709 | v8::SnapshotObjectId id = prop->GetToNode()->GetId(); |
| 710 | if (id < maxId1) |
| 711 | ++wrong_count; |
| 712 | } |
| 713 | CHECK_EQ(0, wrong_count); |
| 714 | } |
| 715 | |
| 716 | |
| 717 | TEST(HeapEntryIdsAndArrayShift) { |
| 718 | LocalContext env; |
| 719 | v8::HandleScope scope(env->GetIsolate()); |
| 720 | v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 721 | |
| 722 | CompileRun( |
| 723 | "function AnObject() {\n" |
| 724 | " this.first = 'first';\n" |
| 725 | " this.second = 'second';\n" |
| 726 | "}\n" |
| 727 | "var a = new Array();\n" |
| 728 | "for (var i = 0; i < 10; ++i)\n" |
| 729 | " a.push(new AnObject());\n"); |
| 730 | const v8::HeapSnapshot* snapshot1 = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 731 | heap_profiler->TakeHeapSnapshot(v8_str("s1")); |
| 732 | CHECK(ValidateSnapshot(snapshot1)); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 733 | |
| 734 | CompileRun( |
| 735 | "for (var i = 0; i < 1; ++i)\n" |
| 736 | " a.shift();\n"); |
| 737 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 738 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 739 | |
| 740 | const v8::HeapSnapshot* snapshot2 = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 741 | heap_profiler->TakeHeapSnapshot(v8_str("s2")); |
| 742 | CHECK(ValidateSnapshot(snapshot2)); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 743 | |
| 744 | const v8::HeapGraphNode* global1 = GetGlobalObject(snapshot1); |
| 745 | const v8::HeapGraphNode* global2 = GetGlobalObject(snapshot2); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 746 | CHECK_NE_SNAPSHOT_OBJECT_ID(0, global1->GetId()); |
| 747 | CHECK_EQ_SNAPSHOT_OBJECT_ID(global1->GetId(), global2->GetId()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 748 | |
| 749 | const v8::HeapGraphNode* a1 = |
| 750 | GetProperty(global1, v8::HeapGraphEdge::kProperty, "a"); |
| 751 | CHECK_NE(NULL, a1); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 752 | const v8::HeapGraphNode* k1 = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 753 | GetProperty(a1, v8::HeapGraphEdge::kInternal, "elements"); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 754 | CHECK_NE(NULL, k1); |
| 755 | const v8::HeapGraphNode* a2 = |
| 756 | GetProperty(global2, v8::HeapGraphEdge::kProperty, "a"); |
| 757 | CHECK_NE(NULL, a2); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 758 | const v8::HeapGraphNode* k2 = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 759 | GetProperty(a2, v8::HeapGraphEdge::kInternal, "elements"); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 760 | CHECK_NE(NULL, k2); |
| 761 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 762 | CHECK_EQ_SNAPSHOT_OBJECT_ID(a1->GetId(), a2->GetId()); |
| 763 | CHECK_EQ_SNAPSHOT_OBJECT_ID(k1->GetId(), k2->GetId()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 764 | } |
| 765 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 766 | |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 767 | TEST(HeapEntryIdsAndGC) { |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 768 | LocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 769 | v8::HandleScope scope(env->GetIsolate()); |
| 770 | v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 771 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 772 | CompileRun( |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 773 | "function A() {}\n" |
| 774 | "function B(x) { this.x = x; }\n" |
| 775 | "var a = new A();\n" |
| 776 | "var b = new B(a);"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 777 | v8::Local<v8::String> s1_str = v8_str("s1"); |
| 778 | v8::Local<v8::String> s2_str = v8_str("s2"); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 779 | const v8::HeapSnapshot* snapshot1 = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 780 | heap_profiler->TakeHeapSnapshot(s1_str); |
| 781 | CHECK(ValidateSnapshot(snapshot1)); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 782 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 783 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 784 | |
| 785 | const v8::HeapSnapshot* snapshot2 = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 786 | heap_profiler->TakeHeapSnapshot(s2_str); |
| 787 | CHECK(ValidateSnapshot(snapshot2)); |
| 788 | |
| 789 | CHECK_GT(snapshot1->GetMaxSnapshotJSObjectId(), 7000); |
| 790 | CHECK(snapshot1->GetMaxSnapshotJSObjectId() <= |
| 791 | snapshot2->GetMaxSnapshotJSObjectId()); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 792 | |
| 793 | const v8::HeapGraphNode* global1 = GetGlobalObject(snapshot1); |
| 794 | const v8::HeapGraphNode* global2 = GetGlobalObject(snapshot2); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 795 | CHECK_NE_SNAPSHOT_OBJECT_ID(0, global1->GetId()); |
| 796 | CHECK_EQ_SNAPSHOT_OBJECT_ID(global1->GetId(), global2->GetId()); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 797 | const v8::HeapGraphNode* A1 = |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 798 | GetProperty(global1, v8::HeapGraphEdge::kProperty, "A"); |
| 799 | CHECK_NE(NULL, A1); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 800 | const v8::HeapGraphNode* A2 = |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 801 | GetProperty(global2, v8::HeapGraphEdge::kProperty, "A"); |
| 802 | CHECK_NE(NULL, A2); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 803 | CHECK_NE_SNAPSHOT_OBJECT_ID(0, A1->GetId()); |
| 804 | CHECK_EQ_SNAPSHOT_OBJECT_ID(A1->GetId(), A2->GetId()); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 805 | const v8::HeapGraphNode* B1 = |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 806 | GetProperty(global1, v8::HeapGraphEdge::kProperty, "B"); |
| 807 | CHECK_NE(NULL, B1); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 808 | const v8::HeapGraphNode* B2 = |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 809 | GetProperty(global2, v8::HeapGraphEdge::kProperty, "B"); |
| 810 | CHECK_NE(NULL, B2); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 811 | CHECK_NE_SNAPSHOT_OBJECT_ID(0, B1->GetId()); |
| 812 | CHECK_EQ_SNAPSHOT_OBJECT_ID(B1->GetId(), B2->GetId()); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 813 | const v8::HeapGraphNode* a1 = |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 814 | GetProperty(global1, v8::HeapGraphEdge::kProperty, "a"); |
| 815 | CHECK_NE(NULL, a1); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 816 | const v8::HeapGraphNode* a2 = |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 817 | GetProperty(global2, v8::HeapGraphEdge::kProperty, "a"); |
| 818 | CHECK_NE(NULL, a2); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 819 | CHECK_NE_SNAPSHOT_OBJECT_ID(0, a1->GetId()); |
| 820 | CHECK_EQ_SNAPSHOT_OBJECT_ID(a1->GetId(), a2->GetId()); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 821 | const v8::HeapGraphNode* b1 = |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 822 | GetProperty(global1, v8::HeapGraphEdge::kProperty, "b"); |
| 823 | CHECK_NE(NULL, b1); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 824 | const v8::HeapGraphNode* b2 = |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame] | 825 | GetProperty(global2, v8::HeapGraphEdge::kProperty, "b"); |
| 826 | CHECK_NE(NULL, b2); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 827 | CHECK_NE_SNAPSHOT_OBJECT_ID(0, b1->GetId()); |
| 828 | CHECK_EQ_SNAPSHOT_OBJECT_ID(b1->GetId(), b2->GetId()); |
Ben Murdoch | 3bec4d2 | 2010-07-22 14:51:16 +0100 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 832 | TEST(HeapSnapshotRootPreservedAfterSorting) { |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 833 | LocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 834 | v8::HandleScope scope(env->GetIsolate()); |
| 835 | v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 836 | const v8::HeapSnapshot* snapshot = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 837 | heap_profiler->TakeHeapSnapshot(v8_str("s")); |
| 838 | CHECK(ValidateSnapshot(snapshot)); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 839 | const v8::HeapGraphNode* root1 = snapshot->GetRoot(); |
| 840 | const_cast<i::HeapSnapshot*>(reinterpret_cast<const i::HeapSnapshot*>( |
| 841 | snapshot))->GetSortedEntriesList(); |
| 842 | const v8::HeapGraphNode* root2 = snapshot->GetRoot(); |
| 843 | CHECK_EQ(root1, root2); |
| 844 | } |
| 845 | |
| 846 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 847 | namespace { |
| 848 | |
| 849 | class TestJSONStream : public v8::OutputStream { |
| 850 | public: |
| 851 | TestJSONStream() : eos_signaled_(0), abort_countdown_(-1) {} |
| 852 | explicit TestJSONStream(int abort_countdown) |
| 853 | : eos_signaled_(0), abort_countdown_(abort_countdown) {} |
| 854 | virtual ~TestJSONStream() {} |
| 855 | virtual void EndOfStream() { ++eos_signaled_; } |
| 856 | virtual WriteResult WriteAsciiChunk(char* buffer, int chars_written) { |
| 857 | if (abort_countdown_ > 0) --abort_countdown_; |
| 858 | if (abort_countdown_ == 0) return kAbort; |
| 859 | CHECK_GT(chars_written, 0); |
| 860 | i::Vector<char> chunk = buffer_.AddBlock(chars_written, '\0'); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 861 | i::MemCopy(chunk.start(), buffer, chars_written); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 862 | return kContinue; |
| 863 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 864 | virtual WriteResult WriteUint32Chunk(uint32_t* buffer, int chars_written) { |
| 865 | DCHECK(false); |
| 866 | return kAbort; |
| 867 | } |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 868 | void WriteTo(i::Vector<char> dest) { buffer_.WriteTo(dest); } |
| 869 | int eos_signaled() { return eos_signaled_; } |
| 870 | int size() { return buffer_.size(); } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 871 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 872 | private: |
| 873 | i::Collector<char> buffer_; |
| 874 | int eos_signaled_; |
| 875 | int abort_countdown_; |
| 876 | }; |
| 877 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 878 | class OneByteResource : public v8::String::ExternalOneByteStringResource { |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 879 | public: |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 880 | explicit OneByteResource(i::Vector<char> string) : data_(string.start()) { |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 881 | length_ = string.length(); |
| 882 | } |
| 883 | virtual const char* data() const { return data_; } |
| 884 | virtual size_t length() const { return length_; } |
| 885 | private: |
| 886 | const char* data_; |
| 887 | size_t length_; |
| 888 | }; |
| 889 | |
| 890 | } // namespace |
| 891 | |
| 892 | TEST(HeapSnapshotJSONSerialization) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 893 | v8::Isolate* isolate = CcTest::isolate(); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 894 | LocalContext env; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 895 | v8::HandleScope scope(isolate); |
| 896 | v8::HeapProfiler* heap_profiler = isolate->GetHeapProfiler(); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 897 | |
| 898 | #define STRING_LITERAL_FOR_TEST \ |
| 899 | "\"String \\n\\r\\u0008\\u0081\\u0101\\u0801\\u8001\"" |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 900 | CompileRun( |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 901 | "function A(s) { this.s = s; }\n" |
| 902 | "function B(x) { this.x = x; }\n" |
| 903 | "var a = new A(" STRING_LITERAL_FOR_TEST ");\n" |
| 904 | "var b = new B(a);"); |
| 905 | const v8::HeapSnapshot* snapshot = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 906 | heap_profiler->TakeHeapSnapshot(v8_str("json")); |
| 907 | CHECK(ValidateSnapshot(snapshot)); |
| 908 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 909 | TestJSONStream stream; |
| 910 | snapshot->Serialize(&stream, v8::HeapSnapshot::kJSON); |
| 911 | CHECK_GT(stream.size(), 0); |
| 912 | CHECK_EQ(1, stream.eos_signaled()); |
| 913 | i::ScopedVector<char> json(stream.size()); |
| 914 | stream.WriteTo(json); |
| 915 | |
| 916 | // Verify that snapshot string is valid JSON. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 917 | OneByteResource* json_res = new OneByteResource(json); |
| 918 | v8::Local<v8::String> json_string = |
| 919 | v8::String::NewExternal(env->GetIsolate(), json_res); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 920 | env->Global()->Set(v8_str("json_snapshot"), json_string); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 921 | v8::Local<v8::Value> snapshot_parse_result = CompileRun( |
| 922 | "var parsed = JSON.parse(json_snapshot); true;"); |
| 923 | CHECK(!snapshot_parse_result.IsEmpty()); |
| 924 | |
| 925 | // Verify that snapshot object has required fields. |
| 926 | v8::Local<v8::Object> parsed_snapshot = |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 927 | env->Global()->Get(v8_str("parsed"))->ToObject(isolate); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 928 | CHECK(parsed_snapshot->Has(v8_str("snapshot"))); |
| 929 | CHECK(parsed_snapshot->Has(v8_str("nodes"))); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 930 | CHECK(parsed_snapshot->Has(v8_str("edges"))); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 931 | CHECK(parsed_snapshot->Has(v8_str("strings"))); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 932 | |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 933 | // Get node and edge "member" offsets. |
| 934 | v8::Local<v8::Value> meta_analysis_result = CompileRun( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 935 | "var meta = parsed.snapshot.meta;\n" |
| 936 | "var edge_count_offset = meta.node_fields.indexOf('edge_count');\n" |
| 937 | "var node_fields_count = meta.node_fields.length;\n" |
| 938 | "var edge_fields_count = meta.edge_fields.length;\n" |
| 939 | "var edge_type_offset = meta.edge_fields.indexOf('type');\n" |
| 940 | "var edge_name_offset = meta.edge_fields.indexOf('name_or_index');\n" |
| 941 | "var edge_to_node_offset = meta.edge_fields.indexOf('to_node');\n" |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 942 | "var property_type =" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 943 | " meta.edge_types[edge_type_offset].indexOf('property');\n" |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 944 | "var shortcut_type =" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 945 | " meta.edge_types[edge_type_offset].indexOf('shortcut');\n" |
| 946 | "var node_count = parsed.nodes.length / node_fields_count;\n" |
| 947 | "var first_edge_indexes = parsed.first_edge_indexes = [];\n" |
| 948 | "for (var i = 0, first_edge_index = 0; i < node_count; ++i) {\n" |
| 949 | " first_edge_indexes[i] = first_edge_index;\n" |
| 950 | " first_edge_index += edge_fields_count *\n" |
| 951 | " parsed.nodes[i * node_fields_count + edge_count_offset];\n" |
| 952 | "}\n" |
| 953 | "first_edge_indexes[node_count] = first_edge_index;\n"); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 954 | CHECK(!meta_analysis_result.IsEmpty()); |
| 955 | |
| 956 | // A helper function for processing encoded nodes. |
| 957 | CompileRun( |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 958 | "function GetChildPosByProperty(pos, prop_name, prop_type) {\n" |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 959 | " var nodes = parsed.nodes;\n" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 960 | " var edges = parsed.edges;\n" |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 961 | " var strings = parsed.strings;\n" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 962 | " var node_ordinal = pos / node_fields_count;\n" |
| 963 | " for (var i = parsed.first_edge_indexes[node_ordinal],\n" |
| 964 | " count = parsed.first_edge_indexes[node_ordinal + 1];\n" |
| 965 | " i < count; i += edge_fields_count) {\n" |
| 966 | " if (edges[i + edge_type_offset] === prop_type\n" |
| 967 | " && strings[edges[i + edge_name_offset]] === prop_name)\n" |
| 968 | " return edges[i + edge_to_node_offset];\n" |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 969 | " }\n" |
| 970 | " return null;\n" |
| 971 | "}\n"); |
| 972 | // Get the string index using the path: <root> -> <global>.b.x.s |
| 973 | v8::Local<v8::Value> string_obj_pos_val = CompileRun( |
| 974 | "GetChildPosByProperty(\n" |
| 975 | " GetChildPosByProperty(\n" |
| 976 | " GetChildPosByProperty(" |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 977 | " parsed.edges[edge_fields_count + edge_to_node_offset]," |
| 978 | " \"b\", property_type),\n" |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 979 | " \"x\", property_type)," |
| 980 | " \"s\", property_type)"); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 981 | CHECK(!string_obj_pos_val.IsEmpty()); |
| 982 | int string_obj_pos = |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 983 | static_cast<int>(string_obj_pos_val->ToNumber(isolate)->Value()); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 984 | v8::Local<v8::Object> nodes_array = |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 985 | parsed_snapshot->Get(v8_str("nodes"))->ToObject(isolate); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 986 | int string_index = static_cast<int>( |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 987 | nodes_array->Get(string_obj_pos + 1)->ToNumber(isolate)->Value()); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 988 | CHECK_GT(string_index, 0); |
| 989 | v8::Local<v8::Object> strings_array = |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 990 | parsed_snapshot->Get(v8_str("strings"))->ToObject(isolate); |
| 991 | v8::Local<v8::String> string = |
| 992 | strings_array->Get(string_index)->ToString(isolate); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 993 | v8::Local<v8::String> ref_string = |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 994 | CompileRun(STRING_LITERAL_FOR_TEST)->ToString(isolate); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 995 | #undef STRING_LITERAL_FOR_TEST |
| 996 | CHECK_EQ(*v8::String::Utf8Value(ref_string), |
| 997 | *v8::String::Utf8Value(string)); |
| 998 | } |
| 999 | |
| 1000 | |
| 1001 | TEST(HeapSnapshotJSONSerializationAborting) { |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1002 | LocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1003 | v8::HandleScope scope(env->GetIsolate()); |
| 1004 | v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1005 | const v8::HeapSnapshot* snapshot = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1006 | heap_profiler->TakeHeapSnapshot(v8_str("abort")); |
| 1007 | CHECK(ValidateSnapshot(snapshot)); |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 1008 | TestJSONStream stream(5); |
| 1009 | snapshot->Serialize(&stream, v8::HeapSnapshot::kJSON); |
| 1010 | CHECK_GT(stream.size(), 0); |
| 1011 | CHECK_EQ(0, stream.eos_signaled()); |
| 1012 | } |
| 1013 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1014 | namespace { |
| 1015 | |
| 1016 | class TestStatsStream : public v8::OutputStream { |
| 1017 | public: |
| 1018 | TestStatsStream() |
| 1019 | : eos_signaled_(0), |
| 1020 | updates_written_(0), |
| 1021 | entries_count_(0), |
| 1022 | entries_size_(0), |
| 1023 | intervals_count_(0), |
| 1024 | first_interval_index_(-1) { } |
| 1025 | TestStatsStream(const TestStatsStream& stream) |
| 1026 | : v8::OutputStream(stream), |
| 1027 | eos_signaled_(stream.eos_signaled_), |
| 1028 | updates_written_(stream.updates_written_), |
| 1029 | entries_count_(stream.entries_count_), |
| 1030 | entries_size_(stream.entries_size_), |
| 1031 | intervals_count_(stream.intervals_count_), |
| 1032 | first_interval_index_(stream.first_interval_index_) { } |
| 1033 | virtual ~TestStatsStream() {} |
| 1034 | virtual void EndOfStream() { ++eos_signaled_; } |
| 1035 | virtual WriteResult WriteAsciiChunk(char* buffer, int chars_written) { |
| 1036 | DCHECK(false); |
| 1037 | return kAbort; |
| 1038 | } |
| 1039 | virtual WriteResult WriteHeapStatsChunk(v8::HeapStatsUpdate* buffer, |
| 1040 | int updates_written) { |
| 1041 | ++intervals_count_; |
| 1042 | DCHECK(updates_written); |
| 1043 | updates_written_ += updates_written; |
| 1044 | entries_count_ = 0; |
| 1045 | if (first_interval_index_ == -1 && updates_written != 0) |
| 1046 | first_interval_index_ = buffer[0].index; |
| 1047 | for (int i = 0; i < updates_written; ++i) { |
| 1048 | entries_count_ += buffer[i].count; |
| 1049 | entries_size_ += buffer[i].size; |
| 1050 | } |
| 1051 | |
| 1052 | return kContinue; |
| 1053 | } |
| 1054 | int eos_signaled() { return eos_signaled_; } |
| 1055 | int updates_written() { return updates_written_; } |
| 1056 | uint32_t entries_count() const { return entries_count_; } |
| 1057 | uint32_t entries_size() const { return entries_size_; } |
| 1058 | int intervals_count() const { return intervals_count_; } |
| 1059 | int first_interval_index() const { return first_interval_index_; } |
| 1060 | |
| 1061 | private: |
| 1062 | int eos_signaled_; |
| 1063 | int updates_written_; |
| 1064 | uint32_t entries_count_; |
| 1065 | uint32_t entries_size_; |
| 1066 | int intervals_count_; |
| 1067 | int first_interval_index_; |
| 1068 | }; |
| 1069 | |
| 1070 | } // namespace |
| 1071 | |
| 1072 | static TestStatsStream GetHeapStatsUpdate( |
| 1073 | v8::HeapProfiler* heap_profiler, |
| 1074 | v8::SnapshotObjectId* object_id = NULL) { |
| 1075 | TestStatsStream stream; |
| 1076 | v8::SnapshotObjectId last_seen_id = heap_profiler->GetHeapStats(&stream); |
| 1077 | if (object_id) |
| 1078 | *object_id = last_seen_id; |
| 1079 | CHECK_EQ(1, stream.eos_signaled()); |
| 1080 | return stream; |
| 1081 | } |
| 1082 | |
| 1083 | |
| 1084 | TEST(HeapSnapshotObjectsStats) { |
| 1085 | LocalContext env; |
| 1086 | v8::HandleScope scope(env->GetIsolate()); |
| 1087 | v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
| 1088 | |
| 1089 | heap_profiler->StartTrackingHeapObjects(); |
| 1090 | // We have to call GC 6 times. In other case the garbage will be |
| 1091 | // the reason of flakiness. |
| 1092 | for (int i = 0; i < 6; ++i) { |
| 1093 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
| 1094 | } |
| 1095 | |
| 1096 | v8::SnapshotObjectId initial_id; |
| 1097 | { |
| 1098 | // Single chunk of data expected in update. Initial data. |
| 1099 | TestStatsStream stats_update = GetHeapStatsUpdate(heap_profiler, |
| 1100 | &initial_id); |
| 1101 | CHECK_EQ(1, stats_update.intervals_count()); |
| 1102 | CHECK_EQ(1, stats_update.updates_written()); |
| 1103 | CHECK_LT(0, stats_update.entries_size()); |
| 1104 | CHECK_EQ(0, stats_update.first_interval_index()); |
| 1105 | } |
| 1106 | |
| 1107 | // No data expected in update because nothing has happened. |
| 1108 | v8::SnapshotObjectId same_id; |
| 1109 | CHECK_EQ(0, GetHeapStatsUpdate(heap_profiler, &same_id).updates_written()); |
| 1110 | CHECK_EQ_SNAPSHOT_OBJECT_ID(initial_id, same_id); |
| 1111 | |
| 1112 | { |
| 1113 | v8::SnapshotObjectId additional_string_id; |
| 1114 | v8::HandleScope inner_scope_1(env->GetIsolate()); |
| 1115 | v8_str("string1"); |
| 1116 | { |
| 1117 | // Single chunk of data with one new entry expected in update. |
| 1118 | TestStatsStream stats_update = GetHeapStatsUpdate(heap_profiler, |
| 1119 | &additional_string_id); |
| 1120 | CHECK_LT(same_id, additional_string_id); |
| 1121 | CHECK_EQ(1, stats_update.intervals_count()); |
| 1122 | CHECK_EQ(1, stats_update.updates_written()); |
| 1123 | CHECK_LT(0, stats_update.entries_size()); |
| 1124 | CHECK_EQ(1, stats_update.entries_count()); |
| 1125 | CHECK_EQ(2, stats_update.first_interval_index()); |
| 1126 | } |
| 1127 | |
| 1128 | // No data expected in update because nothing happened. |
| 1129 | v8::SnapshotObjectId last_id; |
| 1130 | CHECK_EQ(0, GetHeapStatsUpdate(heap_profiler, &last_id).updates_written()); |
| 1131 | CHECK_EQ_SNAPSHOT_OBJECT_ID(additional_string_id, last_id); |
| 1132 | |
| 1133 | { |
| 1134 | v8::HandleScope inner_scope_2(env->GetIsolate()); |
| 1135 | v8_str("string2"); |
| 1136 | |
| 1137 | uint32_t entries_size; |
| 1138 | { |
| 1139 | v8::HandleScope inner_scope_3(env->GetIsolate()); |
| 1140 | v8_str("string3"); |
| 1141 | v8_str("string4"); |
| 1142 | |
| 1143 | { |
| 1144 | // Single chunk of data with three new entries expected in update. |
| 1145 | TestStatsStream stats_update = GetHeapStatsUpdate(heap_profiler); |
| 1146 | CHECK_EQ(1, stats_update.intervals_count()); |
| 1147 | CHECK_EQ(1, stats_update.updates_written()); |
| 1148 | CHECK_LT(0, entries_size = stats_update.entries_size()); |
| 1149 | CHECK_EQ(3, stats_update.entries_count()); |
| 1150 | CHECK_EQ(4, stats_update.first_interval_index()); |
| 1151 | } |
| 1152 | } |
| 1153 | |
| 1154 | { |
| 1155 | // Single chunk of data with two left entries expected in update. |
| 1156 | TestStatsStream stats_update = GetHeapStatsUpdate(heap_profiler); |
| 1157 | CHECK_EQ(1, stats_update.intervals_count()); |
| 1158 | CHECK_EQ(1, stats_update.updates_written()); |
| 1159 | CHECK_GT(entries_size, stats_update.entries_size()); |
| 1160 | CHECK_EQ(1, stats_update.entries_count()); |
| 1161 | // Two strings from forth interval were released. |
| 1162 | CHECK_EQ(4, stats_update.first_interval_index()); |
| 1163 | } |
| 1164 | } |
| 1165 | |
| 1166 | { |
| 1167 | // Single chunk of data with 0 left entries expected in update. |
| 1168 | TestStatsStream stats_update = GetHeapStatsUpdate(heap_profiler); |
| 1169 | CHECK_EQ(1, stats_update.intervals_count()); |
| 1170 | CHECK_EQ(1, stats_update.updates_written()); |
| 1171 | CHECK_EQ(0, stats_update.entries_size()); |
| 1172 | CHECK_EQ(0, stats_update.entries_count()); |
| 1173 | // The last string from forth interval was released. |
| 1174 | CHECK_EQ(4, stats_update.first_interval_index()); |
| 1175 | } |
| 1176 | } |
| 1177 | { |
| 1178 | // Single chunk of data with 0 left entries expected in update. |
| 1179 | TestStatsStream stats_update = GetHeapStatsUpdate(heap_profiler); |
| 1180 | CHECK_EQ(1, stats_update.intervals_count()); |
| 1181 | CHECK_EQ(1, stats_update.updates_written()); |
| 1182 | CHECK_EQ(0, stats_update.entries_size()); |
| 1183 | CHECK_EQ(0, stats_update.entries_count()); |
| 1184 | // The only string from the second interval was released. |
| 1185 | CHECK_EQ(2, stats_update.first_interval_index()); |
| 1186 | } |
| 1187 | |
| 1188 | v8::Local<v8::Array> array = v8::Array::New(env->GetIsolate()); |
| 1189 | CHECK_EQ(0, array->Length()); |
| 1190 | // Force array's buffer allocation. |
| 1191 | array->Set(2, v8_num(7)); |
| 1192 | |
| 1193 | uint32_t entries_size; |
| 1194 | { |
| 1195 | // Single chunk of data with 2 entries expected in update. |
| 1196 | TestStatsStream stats_update = GetHeapStatsUpdate(heap_profiler); |
| 1197 | CHECK_EQ(1, stats_update.intervals_count()); |
| 1198 | CHECK_EQ(1, stats_update.updates_written()); |
| 1199 | CHECK_LT(0, entries_size = stats_update.entries_size()); |
| 1200 | // They are the array and its buffer. |
| 1201 | CHECK_EQ(2, stats_update.entries_count()); |
| 1202 | CHECK_EQ(8, stats_update.first_interval_index()); |
| 1203 | } |
| 1204 | |
| 1205 | for (int i = 0; i < 100; ++i) |
| 1206 | array->Set(i, v8_num(i)); |
| 1207 | |
| 1208 | { |
| 1209 | // Single chunk of data with 1 entry expected in update. |
| 1210 | TestStatsStream stats_update = GetHeapStatsUpdate(heap_profiler); |
| 1211 | CHECK_EQ(1, stats_update.intervals_count()); |
| 1212 | // The first interval was changed because old buffer was collected. |
| 1213 | // The second interval was changed because new buffer was allocated. |
| 1214 | CHECK_EQ(2, stats_update.updates_written()); |
| 1215 | CHECK_LT(entries_size, stats_update.entries_size()); |
| 1216 | CHECK_EQ(2, stats_update.entries_count()); |
| 1217 | CHECK_EQ(8, stats_update.first_interval_index()); |
| 1218 | } |
| 1219 | |
| 1220 | heap_profiler->StopTrackingHeapObjects(); |
| 1221 | } |
| 1222 | |
| 1223 | |
| 1224 | TEST(HeapObjectIds) { |
| 1225 | LocalContext env; |
| 1226 | v8::Isolate* isolate = env->GetIsolate(); |
| 1227 | v8::HandleScope scope(isolate); |
| 1228 | v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
| 1229 | |
| 1230 | const int kLength = 10; |
| 1231 | v8::Handle<v8::Object> objects[kLength]; |
| 1232 | v8::SnapshotObjectId ids[kLength]; |
| 1233 | |
| 1234 | heap_profiler->StartTrackingHeapObjects(false); |
| 1235 | |
| 1236 | for (int i = 0; i < kLength; i++) { |
| 1237 | objects[i] = v8::Object::New(isolate); |
| 1238 | } |
| 1239 | GetHeapStatsUpdate(heap_profiler); |
| 1240 | |
| 1241 | for (int i = 0; i < kLength; i++) { |
| 1242 | v8::SnapshotObjectId id = heap_profiler->GetObjectId(objects[i]); |
| 1243 | CHECK_NE(v8::HeapProfiler::kUnknownObjectId, static_cast<int>(id)); |
| 1244 | ids[i] = id; |
| 1245 | } |
| 1246 | |
| 1247 | heap_profiler->StopTrackingHeapObjects(); |
| 1248 | CcTest::heap()->CollectAllAvailableGarbage(); |
| 1249 | |
| 1250 | for (int i = 0; i < kLength; i++) { |
| 1251 | v8::SnapshotObjectId id = heap_profiler->GetObjectId(objects[i]); |
| 1252 | CHECK_EQ(static_cast<int>(ids[i]), static_cast<int>(id)); |
| 1253 | v8::Handle<v8::Value> obj = heap_profiler->FindObjectById(ids[i]); |
| 1254 | CHECK_EQ(objects[i], obj); |
| 1255 | } |
| 1256 | |
| 1257 | heap_profiler->ClearObjectIds(); |
| 1258 | for (int i = 0; i < kLength; i++) { |
| 1259 | v8::SnapshotObjectId id = heap_profiler->GetObjectId(objects[i]); |
| 1260 | CHECK_EQ(v8::HeapProfiler::kUnknownObjectId, static_cast<int>(id)); |
| 1261 | v8::Handle<v8::Value> obj = heap_profiler->FindObjectById(ids[i]); |
| 1262 | CHECK(obj.IsEmpty()); |
| 1263 | } |
| 1264 | } |
| 1265 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 1266 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1267 | static void CheckChildrenIds(const v8::HeapSnapshot* snapshot, |
| 1268 | const v8::HeapGraphNode* node, |
| 1269 | int level, int max_level) { |
| 1270 | if (level > max_level) return; |
| 1271 | CHECK_EQ(node, snapshot->GetNodeById(node->GetId())); |
| 1272 | for (int i = 0, count = node->GetChildrenCount(); i < count; ++i) { |
| 1273 | const v8::HeapGraphEdge* prop = node->GetChild(i); |
| 1274 | const v8::HeapGraphNode* child = |
| 1275 | snapshot->GetNodeById(prop->GetToNode()->GetId()); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1276 | CHECK_EQ_SNAPSHOT_OBJECT_ID(prop->GetToNode()->GetId(), child->GetId()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1277 | CHECK_EQ(prop->GetToNode(), child); |
| 1278 | CheckChildrenIds(snapshot, child, level + 1, max_level); |
| 1279 | } |
| 1280 | } |
| 1281 | |
| 1282 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1283 | TEST(HeapSnapshotGetNodeById) { |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1284 | LocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1285 | v8::HandleScope scope(env->GetIsolate()); |
| 1286 | v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1287 | |
| 1288 | const v8::HeapSnapshot* snapshot = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1289 | heap_profiler->TakeHeapSnapshot(v8_str("id")); |
| 1290 | CHECK(ValidateSnapshot(snapshot)); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1291 | const v8::HeapGraphNode* root = snapshot->GetRoot(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1292 | CheckChildrenIds(snapshot, root, 0, 3); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1293 | // Check a big id, which should not exist yet. |
| 1294 | CHECK_EQ(NULL, snapshot->GetNodeById(0x1000000UL)); |
| 1295 | } |
| 1296 | |
| 1297 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1298 | TEST(HeapSnapshotGetSnapshotObjectId) { |
| 1299 | LocalContext env; |
| 1300 | v8::HandleScope scope(env->GetIsolate()); |
| 1301 | v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
| 1302 | CompileRun("globalObject = {};\n"); |
| 1303 | const v8::HeapSnapshot* snapshot = |
| 1304 | heap_profiler->TakeHeapSnapshot(v8_str("get_snapshot_object_id")); |
| 1305 | CHECK(ValidateSnapshot(snapshot)); |
| 1306 | const v8::HeapGraphNode* global = GetGlobalObject(snapshot); |
| 1307 | const v8::HeapGraphNode* global_object = |
| 1308 | GetProperty(global, v8::HeapGraphEdge::kProperty, "globalObject"); |
| 1309 | CHECK(global_object); |
| 1310 | |
| 1311 | v8::Local<v8::Value> globalObjectHandle = env->Global()->Get( |
| 1312 | v8::String::NewFromUtf8(env->GetIsolate(), "globalObject")); |
| 1313 | CHECK(!globalObjectHandle.IsEmpty()); |
| 1314 | CHECK(globalObjectHandle->IsObject()); |
| 1315 | |
| 1316 | v8::SnapshotObjectId id = heap_profiler->GetObjectId(globalObjectHandle); |
| 1317 | CHECK_NE(static_cast<int>(v8::HeapProfiler::kUnknownObjectId), |
| 1318 | id); |
| 1319 | CHECK_EQ(static_cast<int>(id), global_object->GetId()); |
| 1320 | } |
| 1321 | |
| 1322 | |
| 1323 | TEST(HeapSnapshotUnknownSnapshotObjectId) { |
| 1324 | LocalContext env; |
| 1325 | v8::HandleScope scope(env->GetIsolate()); |
| 1326 | v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
| 1327 | CompileRun("globalObject = {};\n"); |
| 1328 | const v8::HeapSnapshot* snapshot = |
| 1329 | heap_profiler->TakeHeapSnapshot(v8_str("unknown_object_id")); |
| 1330 | CHECK(ValidateSnapshot(snapshot)); |
| 1331 | const v8::HeapGraphNode* node = |
| 1332 | snapshot->GetNodeById(v8::HeapProfiler::kUnknownObjectId); |
| 1333 | CHECK_EQ(NULL, node); |
| 1334 | } |
| 1335 | |
| 1336 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1337 | namespace { |
| 1338 | |
| 1339 | class TestActivityControl : public v8::ActivityControl { |
| 1340 | public: |
| 1341 | explicit TestActivityControl(int abort_count) |
| 1342 | : done_(0), total_(0), abort_count_(abort_count) {} |
| 1343 | ControlOption ReportProgressValue(int done, int total) { |
| 1344 | done_ = done; |
| 1345 | total_ = total; |
| 1346 | return --abort_count_ != 0 ? kContinue : kAbort; |
| 1347 | } |
| 1348 | int done() { return done_; } |
| 1349 | int total() { return total_; } |
| 1350 | |
| 1351 | private: |
| 1352 | int done_; |
| 1353 | int total_; |
| 1354 | int abort_count_; |
| 1355 | }; |
| 1356 | } |
| 1357 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1358 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1359 | TEST(TakeHeapSnapshotAborting) { |
| 1360 | LocalContext env; |
| 1361 | v8::HandleScope scope(env->GetIsolate()); |
| 1362 | |
| 1363 | v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
| 1364 | const int snapshots_count = heap_profiler->GetSnapshotCount(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1365 | TestActivityControl aborting_control(1); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1366 | const v8::HeapSnapshot* no_snapshot = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1367 | heap_profiler->TakeHeapSnapshot(v8_str("abort"), |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1368 | &aborting_control); |
| 1369 | CHECK_EQ(NULL, no_snapshot); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1370 | CHECK_EQ(snapshots_count, heap_profiler->GetSnapshotCount()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1371 | CHECK_GT(aborting_control.total(), aborting_control.done()); |
| 1372 | |
| 1373 | TestActivityControl control(-1); // Don't abort. |
| 1374 | const v8::HeapSnapshot* snapshot = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1375 | heap_profiler->TakeHeapSnapshot(v8_str("full"), |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1376 | &control); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1377 | CHECK(ValidateSnapshot(snapshot)); |
| 1378 | |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1379 | CHECK_NE(NULL, snapshot); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1380 | CHECK_EQ(snapshots_count + 1, heap_profiler->GetSnapshotCount()); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 1381 | CHECK_EQ(control.total(), control.done()); |
| 1382 | CHECK_GT(control.total(), 0); |
| 1383 | } |
| 1384 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1385 | |
| 1386 | namespace { |
| 1387 | |
| 1388 | class TestRetainedObjectInfo : public v8::RetainedObjectInfo { |
| 1389 | public: |
| 1390 | TestRetainedObjectInfo(int hash, |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1391 | const char* group_label, |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1392 | const char* label, |
| 1393 | intptr_t element_count = -1, |
| 1394 | intptr_t size = -1) |
| 1395 | : disposed_(false), |
| 1396 | hash_(hash), |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1397 | group_label_(group_label), |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1398 | label_(label), |
| 1399 | element_count_(element_count), |
| 1400 | size_(size) { |
| 1401 | instances.Add(this); |
| 1402 | } |
| 1403 | virtual ~TestRetainedObjectInfo() {} |
| 1404 | virtual void Dispose() { |
| 1405 | CHECK(!disposed_); |
| 1406 | disposed_ = true; |
| 1407 | } |
| 1408 | virtual bool IsEquivalent(RetainedObjectInfo* other) { |
| 1409 | return GetHash() == other->GetHash(); |
| 1410 | } |
| 1411 | virtual intptr_t GetHash() { return hash_; } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1412 | virtual const char* GetGroupLabel() { return group_label_; } |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1413 | virtual const char* GetLabel() { return label_; } |
| 1414 | virtual intptr_t GetElementCount() { return element_count_; } |
| 1415 | virtual intptr_t GetSizeInBytes() { return size_; } |
| 1416 | bool disposed() { return disposed_; } |
| 1417 | |
| 1418 | static v8::RetainedObjectInfo* WrapperInfoCallback( |
| 1419 | uint16_t class_id, v8::Handle<v8::Value> wrapper) { |
| 1420 | if (class_id == 1) { |
| 1421 | if (wrapper->IsString()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1422 | v8::String::Utf8Value utf8(wrapper); |
| 1423 | if (strcmp(*utf8, "AAA") == 0) |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1424 | return new TestRetainedObjectInfo(1, "aaa-group", "aaa", 100); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1425 | else if (strcmp(*utf8, "BBB") == 0) |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1426 | return new TestRetainedObjectInfo(1, "aaa-group", "aaa", 100); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1427 | } |
| 1428 | } else if (class_id == 2) { |
| 1429 | if (wrapper->IsString()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1430 | v8::String::Utf8Value utf8(wrapper); |
| 1431 | if (strcmp(*utf8, "CCC") == 0) |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1432 | return new TestRetainedObjectInfo(2, "ccc-group", "ccc"); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1433 | } |
| 1434 | } |
| 1435 | CHECK(false); |
| 1436 | return NULL; |
| 1437 | } |
| 1438 | |
| 1439 | static i::List<TestRetainedObjectInfo*> instances; |
| 1440 | |
| 1441 | private: |
| 1442 | bool disposed_; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1443 | int hash_; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1444 | const char* group_label_; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1445 | const char* label_; |
| 1446 | intptr_t element_count_; |
| 1447 | intptr_t size_; |
| 1448 | }; |
| 1449 | |
| 1450 | |
| 1451 | i::List<TestRetainedObjectInfo*> TestRetainedObjectInfo::instances; |
| 1452 | } |
| 1453 | |
| 1454 | |
| 1455 | static const v8::HeapGraphNode* GetNode(const v8::HeapGraphNode* parent, |
| 1456 | v8::HeapGraphNode::Type type, |
| 1457 | const char* name) { |
| 1458 | for (int i = 0, count = parent->GetChildrenCount(); i < count; ++i) { |
| 1459 | const v8::HeapGraphNode* node = parent->GetChild(i)->GetToNode(); |
| 1460 | if (node->GetType() == type && strcmp(name, |
| 1461 | const_cast<i::HeapEntry*>( |
| 1462 | reinterpret_cast<const i::HeapEntry*>(node))->name()) == 0) { |
| 1463 | return node; |
| 1464 | } |
| 1465 | } |
| 1466 | return NULL; |
| 1467 | } |
| 1468 | |
| 1469 | |
| 1470 | TEST(HeapSnapshotRetainedObjectInfo) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1471 | LocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1472 | v8::Isolate* isolate = env->GetIsolate(); |
| 1473 | v8::HandleScope scope(isolate); |
| 1474 | v8::HeapProfiler* heap_profiler = isolate->GetHeapProfiler(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1475 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1476 | heap_profiler->SetWrapperClassInfoProvider( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1477 | 1, TestRetainedObjectInfo::WrapperInfoCallback); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1478 | heap_profiler->SetWrapperClassInfoProvider( |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1479 | 2, TestRetainedObjectInfo::WrapperInfoCallback); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1480 | v8::Persistent<v8::String> p_AAA(isolate, v8_str("AAA")); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1481 | p_AAA.SetWrapperClassId(1); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1482 | v8::Persistent<v8::String> p_BBB(isolate, v8_str("BBB")); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1483 | p_BBB.SetWrapperClassId(1); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1484 | v8::Persistent<v8::String> p_CCC(isolate, v8_str("CCC")); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1485 | p_CCC.SetWrapperClassId(2); |
| 1486 | CHECK_EQ(0, TestRetainedObjectInfo::instances.length()); |
| 1487 | const v8::HeapSnapshot* snapshot = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1488 | heap_profiler->TakeHeapSnapshot(v8_str("retained")); |
| 1489 | CHECK(ValidateSnapshot(snapshot)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1490 | |
| 1491 | CHECK_EQ(3, TestRetainedObjectInfo::instances.length()); |
| 1492 | for (int i = 0; i < TestRetainedObjectInfo::instances.length(); ++i) { |
| 1493 | CHECK(TestRetainedObjectInfo::instances[i]->disposed()); |
| 1494 | delete TestRetainedObjectInfo::instances[i]; |
| 1495 | } |
| 1496 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1497 | const v8::HeapGraphNode* native_group_aaa = GetNode( |
| 1498 | snapshot->GetRoot(), v8::HeapGraphNode::kSynthetic, "aaa-group"); |
| 1499 | CHECK_NE(NULL, native_group_aaa); |
| 1500 | CHECK_EQ(1, native_group_aaa->GetChildrenCount()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1501 | const v8::HeapGraphNode* aaa = GetNode( |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1502 | native_group_aaa, v8::HeapGraphNode::kNative, "aaa / 100 entries"); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1503 | CHECK_NE(NULL, aaa); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1504 | CHECK_EQ(2, aaa->GetChildrenCount()); |
| 1505 | |
| 1506 | const v8::HeapGraphNode* native_group_ccc = GetNode( |
| 1507 | snapshot->GetRoot(), v8::HeapGraphNode::kSynthetic, "ccc-group"); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1508 | const v8::HeapGraphNode* ccc = GetNode( |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1509 | native_group_ccc, v8::HeapGraphNode::kNative, "ccc"); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1510 | CHECK_NE(NULL, ccc); |
| 1511 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1512 | const v8::HeapGraphNode* n_AAA = GetNode( |
| 1513 | aaa, v8::HeapGraphNode::kString, "AAA"); |
| 1514 | CHECK_NE(NULL, n_AAA); |
| 1515 | const v8::HeapGraphNode* n_BBB = GetNode( |
| 1516 | aaa, v8::HeapGraphNode::kString, "BBB"); |
| 1517 | CHECK_NE(NULL, n_BBB); |
| 1518 | CHECK_EQ(1, ccc->GetChildrenCount()); |
| 1519 | const v8::HeapGraphNode* n_CCC = GetNode( |
| 1520 | ccc, v8::HeapGraphNode::kString, "CCC"); |
| 1521 | CHECK_NE(NULL, n_CCC); |
| 1522 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 1523 | CHECK_EQ(aaa, GetProperty(n_AAA, v8::HeapGraphEdge::kInternal, "native")); |
| 1524 | CHECK_EQ(aaa, GetProperty(n_BBB, v8::HeapGraphEdge::kInternal, "native")); |
| 1525 | CHECK_EQ(ccc, GetProperty(n_CCC, v8::HeapGraphEdge::kInternal, "native")); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1526 | } |
| 1527 | |
| 1528 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1529 | class GraphWithImplicitRefs { |
| 1530 | public: |
| 1531 | static const int kObjectsCount = 4; |
| 1532 | explicit GraphWithImplicitRefs(LocalContext* env) { |
| 1533 | CHECK_EQ(NULL, instance_); |
| 1534 | instance_ = this; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1535 | isolate_ = (*env)->GetIsolate(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1536 | for (int i = 0; i < kObjectsCount; i++) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1537 | objects_[i].Reset(isolate_, v8::Object::New(isolate_)); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1538 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1539 | (*env)->Global()->Set(v8_str("root_object"), |
| 1540 | v8::Local<v8::Value>::New(isolate_, objects_[0])); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1541 | } |
| 1542 | ~GraphWithImplicitRefs() { |
| 1543 | instance_ = NULL; |
| 1544 | } |
| 1545 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1546 | static void gcPrologue(v8::GCType type, v8::GCCallbackFlags flags) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1547 | instance_->AddImplicitReferences(); |
| 1548 | } |
| 1549 | |
| 1550 | private: |
| 1551 | void AddImplicitReferences() { |
| 1552 | // 0 -> 1 |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1553 | isolate_->SetObjectGroupId(objects_[0], |
| 1554 | v8::UniqueId(1)); |
| 1555 | isolate_->SetReferenceFromGroup( |
| 1556 | v8::UniqueId(1), objects_[1]); |
| 1557 | // Adding two more references: 1 -> 2, 1 -> 3 |
| 1558 | isolate_->SetReference(objects_[1].As<v8::Object>(), |
| 1559 | objects_[2]); |
| 1560 | isolate_->SetReference(objects_[1].As<v8::Object>(), |
| 1561 | objects_[3]); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1562 | } |
| 1563 | |
| 1564 | v8::Persistent<v8::Value> objects_[kObjectsCount]; |
| 1565 | static GraphWithImplicitRefs* instance_; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1566 | v8::Isolate* isolate_; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1567 | }; |
| 1568 | |
| 1569 | GraphWithImplicitRefs* GraphWithImplicitRefs::instance_ = NULL; |
| 1570 | |
| 1571 | |
| 1572 | TEST(HeapSnapshotImplicitReferences) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1573 | LocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1574 | v8::HandleScope scope(env->GetIsolate()); |
| 1575 | v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1576 | |
| 1577 | GraphWithImplicitRefs graph(&env); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1578 | v8::V8::AddGCPrologueCallback(&GraphWithImplicitRefs::gcPrologue); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1579 | |
| 1580 | const v8::HeapSnapshot* snapshot = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1581 | heap_profiler->TakeHeapSnapshot(v8_str("implicit_refs")); |
| 1582 | CHECK(ValidateSnapshot(snapshot)); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1583 | |
| 1584 | const v8::HeapGraphNode* global_object = GetGlobalObject(snapshot); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1585 | const v8::HeapGraphNode* obj0 = GetProperty( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1586 | global_object, v8::HeapGraphEdge::kProperty, "root_object"); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1587 | CHECK(obj0); |
| 1588 | CHECK_EQ(v8::HeapGraphNode::kObject, obj0->GetType()); |
| 1589 | const v8::HeapGraphNode* obj1 = GetProperty( |
| 1590 | obj0, v8::HeapGraphEdge::kInternal, "native"); |
| 1591 | CHECK(obj1); |
| 1592 | int implicit_targets_count = 0; |
| 1593 | for (int i = 0, count = obj1->GetChildrenCount(); i < count; ++i) { |
| 1594 | const v8::HeapGraphEdge* prop = obj1->GetChild(i); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1595 | v8::String::Utf8Value prop_name(prop->GetName()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1596 | if (prop->GetType() == v8::HeapGraphEdge::kInternal && |
| 1597 | strcmp("native", *prop_name) == 0) { |
| 1598 | ++implicit_targets_count; |
| 1599 | } |
| 1600 | } |
| 1601 | CHECK_EQ(2, implicit_targets_count); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1602 | v8::V8::RemoveGCPrologueCallback(&GraphWithImplicitRefs::gcPrologue); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1603 | } |
| 1604 | |
| 1605 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1606 | TEST(DeleteAllHeapSnapshots) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1607 | LocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1608 | v8::HandleScope scope(env->GetIsolate()); |
| 1609 | v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1610 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1611 | CHECK_EQ(0, heap_profiler->GetSnapshotCount()); |
| 1612 | heap_profiler->DeleteAllHeapSnapshots(); |
| 1613 | CHECK_EQ(0, heap_profiler->GetSnapshotCount()); |
| 1614 | CHECK_NE(NULL, heap_profiler->TakeHeapSnapshot(v8_str("1"))); |
| 1615 | CHECK_EQ(1, heap_profiler->GetSnapshotCount()); |
| 1616 | heap_profiler->DeleteAllHeapSnapshots(); |
| 1617 | CHECK_EQ(0, heap_profiler->GetSnapshotCount()); |
| 1618 | CHECK_NE(NULL, heap_profiler->TakeHeapSnapshot(v8_str("1"))); |
| 1619 | CHECK_NE(NULL, heap_profiler->TakeHeapSnapshot(v8_str("2"))); |
| 1620 | CHECK_EQ(2, heap_profiler->GetSnapshotCount()); |
| 1621 | heap_profiler->DeleteAllHeapSnapshots(); |
| 1622 | CHECK_EQ(0, heap_profiler->GetSnapshotCount()); |
| 1623 | } |
| 1624 | |
| 1625 | |
| 1626 | static const v8::HeapSnapshot* FindHeapSnapshot(v8::HeapProfiler* profiler, |
| 1627 | unsigned uid) { |
| 1628 | int length = profiler->GetSnapshotCount(); |
| 1629 | for (int i = 0; i < length; i++) { |
| 1630 | const v8::HeapSnapshot* snapshot = profiler->GetHeapSnapshot(i); |
| 1631 | if (snapshot->GetUid() == uid) { |
| 1632 | return snapshot; |
| 1633 | } |
| 1634 | } |
| 1635 | return NULL; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1636 | } |
| 1637 | |
| 1638 | |
| 1639 | TEST(DeleteHeapSnapshot) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1640 | LocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1641 | v8::HandleScope scope(env->GetIsolate()); |
| 1642 | v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1643 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1644 | CHECK_EQ(0, heap_profiler->GetSnapshotCount()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1645 | const v8::HeapSnapshot* s1 = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1646 | heap_profiler->TakeHeapSnapshot(v8_str("1")); |
| 1647 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1648 | CHECK_NE(NULL, s1); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1649 | CHECK_EQ(1, heap_profiler->GetSnapshotCount()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1650 | unsigned uid1 = s1->GetUid(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1651 | CHECK_EQ(s1, FindHeapSnapshot(heap_profiler, uid1)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1652 | const_cast<v8::HeapSnapshot*>(s1)->Delete(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1653 | CHECK_EQ(0, heap_profiler->GetSnapshotCount()); |
| 1654 | CHECK_EQ(NULL, FindHeapSnapshot(heap_profiler, uid1)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1655 | |
| 1656 | const v8::HeapSnapshot* s2 = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1657 | heap_profiler->TakeHeapSnapshot(v8_str("2")); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1658 | CHECK_NE(NULL, s2); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1659 | CHECK_EQ(1, heap_profiler->GetSnapshotCount()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1660 | unsigned uid2 = s2->GetUid(); |
| 1661 | CHECK_NE(static_cast<int>(uid1), static_cast<int>(uid2)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1662 | CHECK_EQ(s2, FindHeapSnapshot(heap_profiler, uid2)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1663 | const v8::HeapSnapshot* s3 = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1664 | heap_profiler->TakeHeapSnapshot(v8_str("3")); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1665 | CHECK_NE(NULL, s3); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1666 | CHECK_EQ(2, heap_profiler->GetSnapshotCount()); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1667 | unsigned uid3 = s3->GetUid(); |
| 1668 | CHECK_NE(static_cast<int>(uid1), static_cast<int>(uid3)); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1669 | CHECK_EQ(s3, FindHeapSnapshot(heap_profiler, uid3)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1670 | const_cast<v8::HeapSnapshot*>(s2)->Delete(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1671 | CHECK_EQ(1, heap_profiler->GetSnapshotCount()); |
| 1672 | CHECK_EQ(NULL, FindHeapSnapshot(heap_profiler, uid2)); |
| 1673 | CHECK_EQ(s3, FindHeapSnapshot(heap_profiler, uid3)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1674 | const_cast<v8::HeapSnapshot*>(s3)->Delete(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1675 | CHECK_EQ(0, heap_profiler->GetSnapshotCount()); |
| 1676 | CHECK_EQ(NULL, FindHeapSnapshot(heap_profiler, uid3)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 1677 | } |
| 1678 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1679 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1680 | class NameResolver : public v8::HeapProfiler::ObjectNameResolver { |
| 1681 | public: |
| 1682 | virtual const char* GetName(v8::Handle<v8::Object> object) { |
| 1683 | return "Global object name"; |
| 1684 | } |
| 1685 | }; |
| 1686 | |
| 1687 | |
| 1688 | TEST(GlobalObjectName) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1689 | LocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1690 | v8::HandleScope scope(env->GetIsolate()); |
| 1691 | v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1692 | |
| 1693 | CompileRun("document = { URL:\"abcdefgh\" };"); |
| 1694 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1695 | NameResolver name_resolver; |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1696 | const v8::HeapSnapshot* snapshot = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1697 | heap_profiler->TakeHeapSnapshot(v8_str("document"), |
| 1698 | NULL, |
| 1699 | &name_resolver); |
| 1700 | CHECK(ValidateSnapshot(snapshot)); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1701 | const v8::HeapGraphNode* global = GetGlobalObject(snapshot); |
| 1702 | CHECK_NE(NULL, global); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1703 | CHECK_EQ("Object / Global object name" , |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1704 | const_cast<i::HeapEntry*>( |
| 1705 | reinterpret_cast<const i::HeapEntry*>(global))->name()); |
| 1706 | } |
| 1707 | |
| 1708 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1709 | TEST(GlobalObjectFields) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1710 | LocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1711 | v8::HandleScope scope(env->GetIsolate()); |
| 1712 | v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
| 1713 | CompileRun("obj = {};"); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1714 | const v8::HeapSnapshot* snapshot = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1715 | heap_profiler->TakeHeapSnapshot(v8_str("snapshot")); |
| 1716 | CHECK(ValidateSnapshot(snapshot)); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1717 | const v8::HeapGraphNode* global = GetGlobalObject(snapshot); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1718 | const v8::HeapGraphNode* builtins = |
| 1719 | GetProperty(global, v8::HeapGraphEdge::kInternal, "builtins"); |
| 1720 | CHECK_NE(NULL, builtins); |
| 1721 | const v8::HeapGraphNode* native_context = |
| 1722 | GetProperty(global, v8::HeapGraphEdge::kInternal, "native_context"); |
| 1723 | CHECK_NE(NULL, native_context); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1724 | const v8::HeapGraphNode* global_proxy = |
| 1725 | GetProperty(global, v8::HeapGraphEdge::kInternal, "global_proxy"); |
| 1726 | CHECK_NE(NULL, global_proxy); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1727 | } |
| 1728 | |
| 1729 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1730 | TEST(NoHandleLeaks) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1731 | LocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1732 | v8::HandleScope scope(env->GetIsolate()); |
| 1733 | v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1734 | |
| 1735 | CompileRun("document = { URL:\"abcdefgh\" };"); |
| 1736 | |
| 1737 | v8::Handle<v8::String> name(v8_str("leakz")); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1738 | i::Isolate* isolate = CcTest::i_isolate(); |
| 1739 | int count_before = i::HandleScope::NumberOfHandles(isolate); |
| 1740 | heap_profiler->TakeHeapSnapshot(name); |
| 1741 | int count_after = i::HandleScope::NumberOfHandles(isolate); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1742 | CHECK_EQ(count_before, count_after); |
| 1743 | } |
| 1744 | |
| 1745 | |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1746 | TEST(NodesIteration) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1747 | LocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1748 | v8::HandleScope scope(env->GetIsolate()); |
| 1749 | v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1750 | const v8::HeapSnapshot* snapshot = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1751 | heap_profiler->TakeHeapSnapshot(v8_str("iteration")); |
| 1752 | CHECK(ValidateSnapshot(snapshot)); |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 1753 | const v8::HeapGraphNode* global = GetGlobalObject(snapshot); |
| 1754 | CHECK_NE(NULL, global); |
| 1755 | // Verify that we can find this object by iteration. |
| 1756 | const int nodes_count = snapshot->GetNodesCount(); |
| 1757 | int count = 0; |
| 1758 | for (int i = 0; i < nodes_count; ++i) { |
| 1759 | if (snapshot->GetNode(i) == global) |
| 1760 | ++count; |
| 1761 | } |
| 1762 | CHECK_EQ(1, count); |
| 1763 | } |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1764 | |
| 1765 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1766 | TEST(GetHeapValueForNode) { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1767 | LocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1768 | v8::HandleScope scope(env->GetIsolate()); |
| 1769 | v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1770 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1771 | CompileRun("a = { s_prop: \'value\', n_prop: \'value2\' };"); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1772 | const v8::HeapSnapshot* snapshot = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1773 | heap_profiler->TakeHeapSnapshot(v8_str("value")); |
| 1774 | CHECK(ValidateSnapshot(snapshot)); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1775 | const v8::HeapGraphNode* global = GetGlobalObject(snapshot); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1776 | CHECK(heap_profiler->FindObjectById(global->GetId())->IsObject()); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1777 | v8::Local<v8::Object> js_global = |
| 1778 | env->Global()->GetPrototype().As<v8::Object>(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1779 | CHECK(js_global == heap_profiler->FindObjectById(global->GetId())); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1780 | const v8::HeapGraphNode* obj = GetProperty( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1781 | global, v8::HeapGraphEdge::kProperty, "a"); |
| 1782 | CHECK(heap_profiler->FindObjectById(obj->GetId())->IsObject()); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1783 | v8::Local<v8::Object> js_obj = js_global->Get(v8_str("a")).As<v8::Object>(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1784 | CHECK(js_obj == heap_profiler->FindObjectById(obj->GetId())); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1785 | const v8::HeapGraphNode* s_prop = |
| 1786 | GetProperty(obj, v8::HeapGraphEdge::kProperty, "s_prop"); |
| 1787 | v8::Local<v8::String> js_s_prop = |
| 1788 | js_obj->Get(v8_str("s_prop")).As<v8::String>(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1789 | CHECK(js_s_prop == heap_profiler->FindObjectById(s_prop->GetId())); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1790 | const v8::HeapGraphNode* n_prop = |
| 1791 | GetProperty(obj, v8::HeapGraphEdge::kProperty, "n_prop"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1792 | v8::Local<v8::String> js_n_prop = |
| 1793 | js_obj->Get(v8_str("n_prop")).As<v8::String>(); |
| 1794 | CHECK(js_n_prop == heap_profiler->FindObjectById(n_prop->GetId())); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1795 | } |
| 1796 | |
| 1797 | |
| 1798 | TEST(GetHeapValueForDeletedObject) { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1799 | LocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1800 | v8::HandleScope scope(env->GetIsolate()); |
| 1801 | v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1802 | |
| 1803 | // It is impossible to delete a global property, so we are about to delete a |
| 1804 | // property of the "a" object. Also, the "p" object can't be an empty one |
| 1805 | // because the empty object is static and isn't actually deleted. |
| 1806 | CompileRun("a = { p: { r: {} } };"); |
| 1807 | const v8::HeapSnapshot* snapshot = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1808 | heap_profiler->TakeHeapSnapshot(v8_str("snapshot")); |
| 1809 | CHECK(ValidateSnapshot(snapshot)); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1810 | const v8::HeapGraphNode* global = GetGlobalObject(snapshot); |
| 1811 | const v8::HeapGraphNode* obj = GetProperty( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1812 | global, v8::HeapGraphEdge::kProperty, "a"); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1813 | const v8::HeapGraphNode* prop = GetProperty( |
| 1814 | obj, v8::HeapGraphEdge::kProperty, "p"); |
| 1815 | { |
| 1816 | // Perform the check inside a nested local scope to avoid creating a |
| 1817 | // reference to the object we are deleting. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1818 | v8::HandleScope scope(env->GetIsolate()); |
| 1819 | CHECK(heap_profiler->FindObjectById(prop->GetId())->IsObject()); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1820 | } |
| 1821 | CompileRun("delete a.p;"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1822 | CHECK(heap_profiler->FindObjectById(prop->GetId()).IsEmpty()); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1823 | } |
| 1824 | |
| 1825 | |
| 1826 | static int StringCmp(const char* ref, i::String* act) { |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 1827 | i::SmartArrayPointer<char> s_act = act->ToCString(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1828 | int result = strcmp(ref, s_act.get()); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1829 | if (result != 0) |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1830 | fprintf(stderr, "Expected: \"%s\", Actual: \"%s\"\n", ref, s_act.get()); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1831 | return result; |
| 1832 | } |
| 1833 | |
| 1834 | |
| 1835 | TEST(GetConstructorName) { |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1836 | LocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1837 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1838 | |
| 1839 | CompileRun( |
| 1840 | "function Constructor1() {};\n" |
| 1841 | "var obj1 = new Constructor1();\n" |
| 1842 | "var Constructor2 = function() {};\n" |
| 1843 | "var obj2 = new Constructor2();\n" |
| 1844 | "var obj3 = {};\n" |
| 1845 | "obj3.constructor = function Constructor3() {};\n" |
| 1846 | "var obj4 = {};\n" |
| 1847 | "// Slow properties\n" |
| 1848 | "for (var i=0; i<2000; ++i) obj4[\"p\" + i] = i;\n" |
| 1849 | "obj4.constructor = function Constructor4() {};\n" |
| 1850 | "var obj5 = {};\n" |
| 1851 | "var obj6 = {};\n" |
| 1852 | "obj6.constructor = 6;"); |
| 1853 | v8::Local<v8::Object> js_global = |
| 1854 | env->Global()->GetPrototype().As<v8::Object>(); |
| 1855 | v8::Local<v8::Object> obj1 = js_global->Get(v8_str("obj1")).As<v8::Object>(); |
| 1856 | i::Handle<i::JSObject> js_obj1 = v8::Utils::OpenHandle(*obj1); |
| 1857 | CHECK_EQ(0, StringCmp( |
| 1858 | "Constructor1", i::V8HeapExplorer::GetConstructorName(*js_obj1))); |
| 1859 | v8::Local<v8::Object> obj2 = js_global->Get(v8_str("obj2")).As<v8::Object>(); |
| 1860 | i::Handle<i::JSObject> js_obj2 = v8::Utils::OpenHandle(*obj2); |
| 1861 | CHECK_EQ(0, StringCmp( |
| 1862 | "Constructor2", i::V8HeapExplorer::GetConstructorName(*js_obj2))); |
| 1863 | v8::Local<v8::Object> obj3 = js_global->Get(v8_str("obj3")).As<v8::Object>(); |
| 1864 | i::Handle<i::JSObject> js_obj3 = v8::Utils::OpenHandle(*obj3); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1865 | // TODO(verwaest): Restore to Constructor3 once supported by the |
| 1866 | // heap-snapshot-generator. |
| 1867 | CHECK_EQ( |
| 1868 | 0, StringCmp("Object", i::V8HeapExplorer::GetConstructorName(*js_obj3))); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1869 | v8::Local<v8::Object> obj4 = js_global->Get(v8_str("obj4")).As<v8::Object>(); |
| 1870 | i::Handle<i::JSObject> js_obj4 = v8::Utils::OpenHandle(*obj4); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1871 | // TODO(verwaest): Restore to Constructor4 once supported by the |
| 1872 | // heap-snapshot-generator. |
| 1873 | CHECK_EQ( |
| 1874 | 0, StringCmp("Object", i::V8HeapExplorer::GetConstructorName(*js_obj4))); |
Ben Murdoch | 69a99ed | 2011-11-30 16:03:39 +0000 | [diff] [blame] | 1875 | v8::Local<v8::Object> obj5 = js_global->Get(v8_str("obj5")).As<v8::Object>(); |
| 1876 | i::Handle<i::JSObject> js_obj5 = v8::Utils::OpenHandle(*obj5); |
| 1877 | CHECK_EQ(0, StringCmp( |
| 1878 | "Object", i::V8HeapExplorer::GetConstructorName(*js_obj5))); |
| 1879 | v8::Local<v8::Object> obj6 = js_global->Get(v8_str("obj6")).As<v8::Object>(); |
| 1880 | i::Handle<i::JSObject> js_obj6 = v8::Utils::OpenHandle(*obj6); |
| 1881 | CHECK_EQ(0, StringCmp( |
| 1882 | "Object", i::V8HeapExplorer::GetConstructorName(*js_obj6))); |
| 1883 | } |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1884 | |
| 1885 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1886 | TEST(FastCaseAccessors) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1887 | LocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1888 | v8::HandleScope scope(env->GetIsolate()); |
| 1889 | v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1890 | |
| 1891 | CompileRun("var obj1 = {};\n" |
| 1892 | "obj1.__defineGetter__('propWithGetter', function Y() {\n" |
| 1893 | " return 42;\n" |
| 1894 | "});\n" |
| 1895 | "obj1.__defineSetter__('propWithSetter', function Z(value) {\n" |
| 1896 | " return this.value_ = value;\n" |
| 1897 | "});\n"); |
| 1898 | const v8::HeapSnapshot* snapshot = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1899 | heap_profiler->TakeHeapSnapshot(v8_str("fastCaseAccessors")); |
| 1900 | CHECK(ValidateSnapshot(snapshot)); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1901 | |
| 1902 | const v8::HeapGraphNode* global = GetGlobalObject(snapshot); |
| 1903 | CHECK_NE(NULL, global); |
| 1904 | const v8::HeapGraphNode* obj1 = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1905 | GetProperty(global, v8::HeapGraphEdge::kProperty, "obj1"); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 1906 | CHECK_NE(NULL, obj1); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1907 | const v8::HeapGraphNode* func; |
| 1908 | func = GetProperty(obj1, v8::HeapGraphEdge::kProperty, "get propWithGetter"); |
| 1909 | CHECK_NE(NULL, func); |
| 1910 | func = GetProperty(obj1, v8::HeapGraphEdge::kProperty, "set propWithGetter"); |
| 1911 | CHECK_EQ(NULL, func); |
| 1912 | func = GetProperty(obj1, v8::HeapGraphEdge::kProperty, "set propWithSetter"); |
| 1913 | CHECK_NE(NULL, func); |
| 1914 | func = GetProperty(obj1, v8::HeapGraphEdge::kProperty, "get propWithSetter"); |
| 1915 | CHECK_EQ(NULL, func); |
| 1916 | } |
| 1917 | |
| 1918 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1919 | TEST(FastCaseRedefinedAccessors) { |
| 1920 | LocalContext env; |
| 1921 | v8::HandleScope scope(env->GetIsolate()); |
| 1922 | v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
| 1923 | |
| 1924 | CompileRun( |
| 1925 | "var obj1 = {};\n" |
| 1926 | "Object.defineProperty(obj1, 'prop', { " |
| 1927 | " get: function() { return 42; },\n" |
| 1928 | " set: function(value) { return this.prop_ = value; },\n" |
| 1929 | " configurable: true,\n" |
| 1930 | " enumerable: true,\n" |
| 1931 | "});\n" |
| 1932 | "Object.defineProperty(obj1, 'prop', { " |
| 1933 | " get: function() { return 153; },\n" |
| 1934 | " set: function(value) { return this.prop_ = value; },\n" |
| 1935 | " configurable: true,\n" |
| 1936 | " enumerable: true,\n" |
| 1937 | "});\n"); |
| 1938 | v8::Local<v8::Object> js_global = |
| 1939 | env->Global()->GetPrototype().As<v8::Object>(); |
| 1940 | i::Handle<i::JSObject> js_obj1 = |
| 1941 | v8::Utils::OpenHandle(*js_global->Get(v8_str("obj1")).As<v8::Object>()); |
| 1942 | USE(js_obj1); |
| 1943 | |
| 1944 | const v8::HeapSnapshot* snapshot = |
| 1945 | heap_profiler->TakeHeapSnapshot(v8_str("fastCaseAccessors")); |
| 1946 | CHECK(ValidateSnapshot(snapshot)); |
| 1947 | const v8::HeapGraphNode* global = GetGlobalObject(snapshot); |
| 1948 | CHECK_NE(NULL, global); |
| 1949 | const v8::HeapGraphNode* obj1 = |
| 1950 | GetProperty(global, v8::HeapGraphEdge::kProperty, "obj1"); |
| 1951 | CHECK_NE(NULL, obj1); |
| 1952 | const v8::HeapGraphNode* func; |
| 1953 | func = GetProperty(obj1, v8::HeapGraphEdge::kProperty, "get prop"); |
| 1954 | CHECK_NE(NULL, func); |
| 1955 | func = GetProperty(obj1, v8::HeapGraphEdge::kProperty, "set prop"); |
| 1956 | CHECK_NE(NULL, func); |
| 1957 | } |
| 1958 | |
| 1959 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1960 | TEST(SlowCaseAccessors) { |
| 1961 | LocalContext env; |
| 1962 | v8::HandleScope scope(env->GetIsolate()); |
| 1963 | v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
| 1964 | |
| 1965 | CompileRun("var obj1 = {};\n" |
| 1966 | "for (var i = 0; i < 100; ++i) obj1['z' + i] = {};" |
| 1967 | "obj1.__defineGetter__('propWithGetter', function Y() {\n" |
| 1968 | " return 42;\n" |
| 1969 | "});\n" |
| 1970 | "obj1.__defineSetter__('propWithSetter', function Z(value) {\n" |
| 1971 | " return this.value_ = value;\n" |
| 1972 | "});\n"); |
| 1973 | const v8::HeapSnapshot* snapshot = |
| 1974 | heap_profiler->TakeHeapSnapshot(v8_str("slowCaseAccessors")); |
| 1975 | CHECK(ValidateSnapshot(snapshot)); |
| 1976 | |
| 1977 | const v8::HeapGraphNode* global = GetGlobalObject(snapshot); |
| 1978 | CHECK_NE(NULL, global); |
| 1979 | const v8::HeapGraphNode* obj1 = |
| 1980 | GetProperty(global, v8::HeapGraphEdge::kProperty, "obj1"); |
| 1981 | CHECK_NE(NULL, obj1); |
| 1982 | const v8::HeapGraphNode* func; |
| 1983 | func = GetProperty(obj1, v8::HeapGraphEdge::kProperty, "get propWithGetter"); |
| 1984 | CHECK_NE(NULL, func); |
| 1985 | func = GetProperty(obj1, v8::HeapGraphEdge::kProperty, "set propWithGetter"); |
| 1986 | CHECK_EQ(NULL, func); |
| 1987 | func = GetProperty(obj1, v8::HeapGraphEdge::kProperty, "set propWithSetter"); |
| 1988 | CHECK_NE(NULL, func); |
| 1989 | func = GetProperty(obj1, v8::HeapGraphEdge::kProperty, "get propWithSetter"); |
| 1990 | CHECK_EQ(NULL, func); |
| 1991 | } |
| 1992 | |
| 1993 | |
| 1994 | TEST(HiddenPropertiesFastCase) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1995 | v8::Isolate* isolate = CcTest::isolate(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1996 | LocalContext env; |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 1997 | v8::HandleScope scope(isolate); |
| 1998 | v8::HeapProfiler* heap_profiler = isolate->GetHeapProfiler(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1999 | |
| 2000 | CompileRun( |
| 2001 | "function C(x) { this.a = this; this.b = x; }\n" |
| 2002 | "c = new C(2012);\n"); |
| 2003 | const v8::HeapSnapshot* snapshot = |
| 2004 | heap_profiler->TakeHeapSnapshot(v8_str("HiddenPropertiesFastCase1")); |
| 2005 | CHECK(ValidateSnapshot(snapshot)); |
| 2006 | const v8::HeapGraphNode* global = GetGlobalObject(snapshot); |
| 2007 | const v8::HeapGraphNode* c = |
| 2008 | GetProperty(global, v8::HeapGraphEdge::kProperty, "c"); |
| 2009 | CHECK_NE(NULL, c); |
| 2010 | const v8::HeapGraphNode* hidden_props = |
| 2011 | GetProperty(c, v8::HeapGraphEdge::kInternal, "hidden_properties"); |
| 2012 | CHECK_EQ(NULL, hidden_props); |
| 2013 | |
| 2014 | v8::Handle<v8::Value> cHandle = |
| 2015 | env->Global()->Get(v8::String::NewFromUtf8(env->GetIsolate(), "c")); |
| 2016 | CHECK(!cHandle.IsEmpty() && cHandle->IsObject()); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame^] | 2017 | cHandle->ToObject(isolate)->SetHiddenValue(v8_str("key"), v8_str("val")); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2018 | |
| 2019 | snapshot = heap_profiler->TakeHeapSnapshot( |
| 2020 | v8_str("HiddenPropertiesFastCase2")); |
| 2021 | CHECK(ValidateSnapshot(snapshot)); |
| 2022 | global = GetGlobalObject(snapshot); |
| 2023 | c = GetProperty(global, v8::HeapGraphEdge::kProperty, "c"); |
| 2024 | CHECK_NE(NULL, c); |
| 2025 | hidden_props = GetProperty(c, v8::HeapGraphEdge::kInternal, |
| 2026 | "hidden_properties"); |
| 2027 | CHECK_NE(NULL, hidden_props); |
| 2028 | } |
| 2029 | |
| 2030 | |
| 2031 | TEST(AccessorInfo) { |
| 2032 | LocalContext env; |
| 2033 | v8::HandleScope scope(env->GetIsolate()); |
| 2034 | v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
| 2035 | |
| 2036 | CompileRun("function foo(x) { }\n"); |
| 2037 | const v8::HeapSnapshot* snapshot = |
| 2038 | heap_profiler->TakeHeapSnapshot(v8_str("AccessorInfoTest")); |
| 2039 | CHECK(ValidateSnapshot(snapshot)); |
| 2040 | const v8::HeapGraphNode* global = GetGlobalObject(snapshot); |
| 2041 | const v8::HeapGraphNode* foo = |
| 2042 | GetProperty(global, v8::HeapGraphEdge::kProperty, "foo"); |
| 2043 | CHECK_NE(NULL, foo); |
| 2044 | const v8::HeapGraphNode* map = |
| 2045 | GetProperty(foo, v8::HeapGraphEdge::kInternal, "map"); |
| 2046 | CHECK_NE(NULL, map); |
| 2047 | const v8::HeapGraphNode* descriptors = |
| 2048 | GetProperty(map, v8::HeapGraphEdge::kInternal, "descriptors"); |
| 2049 | CHECK_NE(NULL, descriptors); |
| 2050 | const v8::HeapGraphNode* length_name = |
| 2051 | GetProperty(descriptors, v8::HeapGraphEdge::kInternal, "2"); |
| 2052 | CHECK_NE(NULL, length_name); |
| 2053 | CHECK_EQ("length", *v8::String::Utf8Value(length_name->GetName())); |
| 2054 | const v8::HeapGraphNode* length_accessor = |
| 2055 | GetProperty(descriptors, v8::HeapGraphEdge::kInternal, "4"); |
| 2056 | CHECK_NE(NULL, length_accessor); |
| 2057 | CHECK_EQ("system / ExecutableAccessorInfo", |
| 2058 | *v8::String::Utf8Value(length_accessor->GetName())); |
| 2059 | const v8::HeapGraphNode* name = |
| 2060 | GetProperty(length_accessor, v8::HeapGraphEdge::kInternal, "name"); |
| 2061 | CHECK_NE(NULL, name); |
| 2062 | const v8::HeapGraphNode* getter = |
| 2063 | GetProperty(length_accessor, v8::HeapGraphEdge::kInternal, "getter"); |
| 2064 | CHECK_NE(NULL, getter); |
| 2065 | const v8::HeapGraphNode* setter = |
| 2066 | GetProperty(length_accessor, v8::HeapGraphEdge::kInternal, "setter"); |
| 2067 | CHECK_NE(NULL, setter); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2068 | } |
| 2069 | |
| 2070 | |
| 2071 | bool HasWeakEdge(const v8::HeapGraphNode* node) { |
| 2072 | for (int i = 0; i < node->GetChildrenCount(); ++i) { |
| 2073 | const v8::HeapGraphEdge* handle_edge = node->GetChild(i); |
| 2074 | if (handle_edge->GetType() == v8::HeapGraphEdge::kWeak) return true; |
| 2075 | } |
| 2076 | return false; |
| 2077 | } |
| 2078 | |
| 2079 | |
| 2080 | bool HasWeakGlobalHandle() { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2081 | v8::Isolate* isolate = CcTest::isolate(); |
| 2082 | v8::HeapProfiler* heap_profiler = isolate->GetHeapProfiler(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2083 | const v8::HeapSnapshot* snapshot = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2084 | heap_profiler->TakeHeapSnapshot(v8_str("weaks")); |
| 2085 | CHECK(ValidateSnapshot(snapshot)); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2086 | const v8::HeapGraphNode* gc_roots = GetNode( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2087 | snapshot->GetRoot(), v8::HeapGraphNode::kSynthetic, "(GC roots)"); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2088 | CHECK_NE(NULL, gc_roots); |
| 2089 | const v8::HeapGraphNode* global_handles = GetNode( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2090 | gc_roots, v8::HeapGraphNode::kSynthetic, "(Global handles)"); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2091 | CHECK_NE(NULL, global_handles); |
| 2092 | return HasWeakEdge(global_handles); |
| 2093 | } |
| 2094 | |
| 2095 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2096 | static void PersistentHandleCallback( |
| 2097 | const v8::WeakCallbackData<v8::Object, v8::Persistent<v8::Object> >& data) { |
| 2098 | data.GetParameter()->Reset(); |
| 2099 | delete data.GetParameter(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2100 | } |
| 2101 | |
| 2102 | |
| 2103 | TEST(WeakGlobalHandle) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2104 | LocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2105 | v8::HandleScope scope(env->GetIsolate()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2106 | |
| 2107 | CHECK(!HasWeakGlobalHandle()); |
| 2108 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2109 | v8::Persistent<v8::Object> handle(env->GetIsolate(), |
| 2110 | v8::Object::New(env->GetIsolate())); |
| 2111 | handle.SetWeak(&handle, PersistentHandleCallback); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2112 | |
| 2113 | CHECK(HasWeakGlobalHandle()); |
| 2114 | } |
| 2115 | |
| 2116 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2117 | TEST(SfiAndJsFunctionWeakRefs) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2118 | LocalContext env; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2119 | v8::HandleScope scope(env->GetIsolate()); |
| 2120 | v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2121 | |
| 2122 | CompileRun( |
| 2123 | "fun = (function (x) { return function () { return x + 1; } })(1);"); |
| 2124 | const v8::HeapSnapshot* snapshot = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2125 | heap_profiler->TakeHeapSnapshot(v8_str("fun")); |
| 2126 | CHECK(ValidateSnapshot(snapshot)); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2127 | const v8::HeapGraphNode* global = GetGlobalObject(snapshot); |
| 2128 | CHECK_NE(NULL, global); |
| 2129 | const v8::HeapGraphNode* fun = |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2130 | GetProperty(global, v8::HeapGraphEdge::kProperty, "fun"); |
| 2131 | CHECK(!HasWeakEdge(fun)); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2132 | const v8::HeapGraphNode* shared = |
| 2133 | GetProperty(fun, v8::HeapGraphEdge::kInternal, "shared"); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2134 | CHECK(!HasWeakEdge(shared)); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2135 | } |
| 2136 | |
| 2137 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2138 | TEST(NoDebugObjectInSnapshot) { |
| 2139 | LocalContext env; |
| 2140 | v8::HandleScope scope(env->GetIsolate()); |
| 2141 | v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
| 2142 | |
| 2143 | CHECK(CcTest::i_isolate()->debug()->Load()); |
| 2144 | CompileRun("foo = {};"); |
| 2145 | const v8::HeapSnapshot* snapshot = |
| 2146 | heap_profiler->TakeHeapSnapshot(v8_str("snapshot")); |
| 2147 | CHECK(ValidateSnapshot(snapshot)); |
| 2148 | const v8::HeapGraphNode* root = snapshot->GetRoot(); |
| 2149 | int globals_count = 0; |
| 2150 | for (int i = 0; i < root->GetChildrenCount(); ++i) { |
| 2151 | const v8::HeapGraphEdge* edge = root->GetChild(i); |
| 2152 | if (edge->GetType() == v8::HeapGraphEdge::kShortcut) { |
| 2153 | ++globals_count; |
| 2154 | const v8::HeapGraphNode* global = edge->GetToNode(); |
| 2155 | const v8::HeapGraphNode* foo = |
| 2156 | GetProperty(global, v8::HeapGraphEdge::kProperty, "foo"); |
| 2157 | CHECK_NE(NULL, foo); |
| 2158 | } |
| 2159 | } |
| 2160 | CHECK_EQ(1, globals_count); |
| 2161 | } |
| 2162 | |
| 2163 | |
| 2164 | TEST(AllStrongGcRootsHaveNames) { |
| 2165 | LocalContext env; |
| 2166 | v8::HandleScope scope(env->GetIsolate()); |
| 2167 | v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
| 2168 | |
| 2169 | CompileRun("foo = {};"); |
| 2170 | const v8::HeapSnapshot* snapshot = |
| 2171 | heap_profiler->TakeHeapSnapshot(v8_str("snapshot")); |
| 2172 | CHECK(ValidateSnapshot(snapshot)); |
| 2173 | const v8::HeapGraphNode* gc_roots = GetNode( |
| 2174 | snapshot->GetRoot(), v8::HeapGraphNode::kSynthetic, "(GC roots)"); |
| 2175 | CHECK_NE(NULL, gc_roots); |
| 2176 | const v8::HeapGraphNode* strong_roots = GetNode( |
| 2177 | gc_roots, v8::HeapGraphNode::kSynthetic, "(Strong roots)"); |
| 2178 | CHECK_NE(NULL, strong_roots); |
| 2179 | for (int i = 0; i < strong_roots->GetChildrenCount(); ++i) { |
| 2180 | const v8::HeapGraphEdge* edge = strong_roots->GetChild(i); |
| 2181 | CHECK_EQ(v8::HeapGraphEdge::kInternal, edge->GetType()); |
| 2182 | v8::String::Utf8Value name(edge->GetName()); |
| 2183 | CHECK(isalpha(**name)); |
| 2184 | } |
| 2185 | } |
| 2186 | |
| 2187 | |
| 2188 | TEST(NoRefsToNonEssentialEntries) { |
| 2189 | LocalContext env; |
| 2190 | v8::HandleScope scope(env->GetIsolate()); |
| 2191 | v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
| 2192 | CompileRun("global_object = {};\n"); |
| 2193 | const v8::HeapSnapshot* snapshot = |
| 2194 | heap_profiler->TakeHeapSnapshot(v8_str("snapshot")); |
| 2195 | CHECK(ValidateSnapshot(snapshot)); |
| 2196 | const v8::HeapGraphNode* global = GetGlobalObject(snapshot); |
| 2197 | const v8::HeapGraphNode* global_object = |
| 2198 | GetProperty(global, v8::HeapGraphEdge::kProperty, "global_object"); |
| 2199 | CHECK_NE(NULL, global_object); |
| 2200 | const v8::HeapGraphNode* properties = |
| 2201 | GetProperty(global_object, v8::HeapGraphEdge::kInternal, "properties"); |
| 2202 | CHECK_EQ(NULL, properties); |
| 2203 | const v8::HeapGraphNode* elements = |
| 2204 | GetProperty(global_object, v8::HeapGraphEdge::kInternal, "elements"); |
| 2205 | CHECK_EQ(NULL, elements); |
| 2206 | } |
| 2207 | |
| 2208 | |
| 2209 | TEST(MapHasDescriptorsAndTransitions) { |
| 2210 | LocalContext env; |
| 2211 | v8::HandleScope scope(env->GetIsolate()); |
| 2212 | v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
| 2213 | CompileRun("obj = { a: 10 };\n"); |
| 2214 | const v8::HeapSnapshot* snapshot = |
| 2215 | heap_profiler->TakeHeapSnapshot(v8_str("snapshot")); |
| 2216 | CHECK(ValidateSnapshot(snapshot)); |
| 2217 | const v8::HeapGraphNode* global = GetGlobalObject(snapshot); |
| 2218 | const v8::HeapGraphNode* global_object = |
| 2219 | GetProperty(global, v8::HeapGraphEdge::kProperty, "obj"); |
| 2220 | CHECK_NE(NULL, global_object); |
| 2221 | |
| 2222 | const v8::HeapGraphNode* map = |
| 2223 | GetProperty(global_object, v8::HeapGraphEdge::kInternal, "map"); |
| 2224 | CHECK_NE(NULL, map); |
| 2225 | const v8::HeapGraphNode* own_descriptors = GetProperty( |
| 2226 | map, v8::HeapGraphEdge::kInternal, "descriptors"); |
| 2227 | CHECK_NE(NULL, own_descriptors); |
| 2228 | const v8::HeapGraphNode* own_transitions = GetProperty( |
| 2229 | map, v8::HeapGraphEdge::kInternal, "transitions"); |
| 2230 | CHECK_EQ(NULL, own_transitions); |
| 2231 | } |
| 2232 | |
| 2233 | |
| 2234 | TEST(ManyLocalsInSharedContext) { |
| 2235 | LocalContext env; |
| 2236 | v8::HandleScope scope(env->GetIsolate()); |
| 2237 | v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
| 2238 | int num_objects = 6000; |
| 2239 | CompileRun( |
| 2240 | "var n = 6000;" |
| 2241 | "var result = [];" |
| 2242 | "result.push('(function outer() {');" |
| 2243 | "for (var i = 0; i < n; i++) {" |
| 2244 | " var f = 'function f_' + i + '() { ';" |
| 2245 | " if (i > 0)" |
| 2246 | " f += 'f_' + (i - 1) + '();';" |
| 2247 | " f += ' }';" |
| 2248 | " result.push(f);" |
| 2249 | "}" |
| 2250 | "result.push('return f_' + (n - 1) + ';');" |
| 2251 | "result.push('})()');" |
| 2252 | "var ok = eval(result.join('\\n'));"); |
| 2253 | const v8::HeapSnapshot* snapshot = |
| 2254 | heap_profiler->TakeHeapSnapshot(v8_str("snapshot")); |
| 2255 | CHECK(ValidateSnapshot(snapshot)); |
| 2256 | |
| 2257 | const v8::HeapGraphNode* global = GetGlobalObject(snapshot); |
| 2258 | CHECK_NE(NULL, global); |
| 2259 | const v8::HeapGraphNode* ok_object = |
| 2260 | GetProperty(global, v8::HeapGraphEdge::kProperty, "ok"); |
| 2261 | CHECK_NE(NULL, ok_object); |
| 2262 | const v8::HeapGraphNode* context_object = |
| 2263 | GetProperty(ok_object, v8::HeapGraphEdge::kInternal, "context"); |
| 2264 | CHECK_NE(NULL, context_object); |
| 2265 | // Check the objects are not duplicated in the context. |
| 2266 | CHECK_EQ(v8::internal::Context::MIN_CONTEXT_SLOTS + num_objects - 1, |
| 2267 | context_object->GetChildrenCount()); |
| 2268 | // Check all the objects have got their names. |
| 2269 | // ... well check just every 15th because otherwise it's too slow in debug. |
| 2270 | for (int i = 0; i < num_objects - 1; i += 15) { |
| 2271 | i::EmbeddedVector<char, 100> var_name; |
| 2272 | i::SNPrintF(var_name, "f_%d", i); |
| 2273 | const v8::HeapGraphNode* f_object = GetProperty( |
| 2274 | context_object, v8::HeapGraphEdge::kContextVariable, var_name.start()); |
| 2275 | CHECK_NE(NULL, f_object); |
| 2276 | } |
| 2277 | } |
| 2278 | |
| 2279 | |
| 2280 | TEST(AllocationSitesAreVisible) { |
| 2281 | LocalContext env; |
| 2282 | v8::Isolate* isolate = env->GetIsolate(); |
| 2283 | v8::HandleScope scope(isolate); |
| 2284 | v8::HeapProfiler* heap_profiler = isolate->GetHeapProfiler(); |
| 2285 | CompileRun( |
| 2286 | "fun = function () { var a = [3, 2, 1]; return a; }\n" |
| 2287 | "fun();"); |
| 2288 | const v8::HeapSnapshot* snapshot = |
| 2289 | heap_profiler->TakeHeapSnapshot(v8_str("snapshot")); |
| 2290 | CHECK(ValidateSnapshot(snapshot)); |
| 2291 | |
| 2292 | const v8::HeapGraphNode* global = GetGlobalObject(snapshot); |
| 2293 | CHECK_NE(NULL, global); |
| 2294 | const v8::HeapGraphNode* fun_code = |
| 2295 | GetProperty(global, v8::HeapGraphEdge::kProperty, "fun"); |
| 2296 | CHECK_NE(NULL, fun_code); |
| 2297 | const v8::HeapGraphNode* literals = |
| 2298 | GetProperty(fun_code, v8::HeapGraphEdge::kInternal, "literals"); |
| 2299 | CHECK_NE(NULL, literals); |
| 2300 | CHECK_EQ(v8::HeapGraphNode::kArray, literals->GetType()); |
| 2301 | CHECK_EQ(2, literals->GetChildrenCount()); |
| 2302 | |
| 2303 | // The second value in the literals array should be the boilerplate, |
| 2304 | // after an AllocationSite. |
| 2305 | const v8::HeapGraphEdge* prop = literals->GetChild(1); |
| 2306 | const v8::HeapGraphNode* allocation_site = prop->GetToNode(); |
| 2307 | v8::String::Utf8Value name(allocation_site->GetName()); |
| 2308 | CHECK_EQ("system / AllocationSite", *name); |
| 2309 | const v8::HeapGraphNode* transition_info = |
| 2310 | GetProperty(allocation_site, v8::HeapGraphEdge::kInternal, |
| 2311 | "transition_info"); |
| 2312 | CHECK_NE(NULL, transition_info); |
| 2313 | |
| 2314 | const v8::HeapGraphNode* elements = |
| 2315 | GetProperty(transition_info, v8::HeapGraphEdge::kInternal, |
| 2316 | "elements"); |
| 2317 | CHECK_NE(NULL, elements); |
| 2318 | CHECK_EQ(v8::HeapGraphNode::kArray, elements->GetType()); |
| 2319 | CHECK_EQ(v8::internal::FixedArray::SizeFor(3), |
| 2320 | static_cast<int>(elements->GetShallowSize())); |
| 2321 | |
| 2322 | v8::Handle<v8::Value> array_val = |
| 2323 | heap_profiler->FindObjectById(transition_info->GetId()); |
| 2324 | CHECK(array_val->IsArray()); |
| 2325 | v8::Handle<v8::Array> array = v8::Handle<v8::Array>::Cast(array_val); |
| 2326 | // Verify the array is "a" in the code above. |
| 2327 | CHECK_EQ(3, array->Length()); |
| 2328 | CHECK_EQ(v8::Integer::New(isolate, 3), |
| 2329 | array->Get(v8::Integer::New(isolate, 0))); |
| 2330 | CHECK_EQ(v8::Integer::New(isolate, 2), |
| 2331 | array->Get(v8::Integer::New(isolate, 1))); |
| 2332 | CHECK_EQ(v8::Integer::New(isolate, 1), |
| 2333 | array->Get(v8::Integer::New(isolate, 2))); |
| 2334 | } |
| 2335 | |
| 2336 | |
| 2337 | TEST(JSFunctionHasCodeLink) { |
| 2338 | LocalContext env; |
| 2339 | v8::HandleScope scope(env->GetIsolate()); |
| 2340 | v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
| 2341 | CompileRun("function foo(x, y) { return x + y; }\n"); |
| 2342 | const v8::HeapSnapshot* snapshot = |
| 2343 | heap_profiler->TakeHeapSnapshot(v8_str("snapshot")); |
| 2344 | CHECK(ValidateSnapshot(snapshot)); |
| 2345 | const v8::HeapGraphNode* global = GetGlobalObject(snapshot); |
| 2346 | const v8::HeapGraphNode* foo_func = |
| 2347 | GetProperty(global, v8::HeapGraphEdge::kProperty, "foo"); |
| 2348 | CHECK_NE(NULL, foo_func); |
| 2349 | const v8::HeapGraphNode* code = |
| 2350 | GetProperty(foo_func, v8::HeapGraphEdge::kInternal, "code"); |
| 2351 | CHECK_NE(NULL, code); |
| 2352 | } |
| 2353 | |
| 2354 | |
| 2355 | static const v8::HeapGraphNode* GetNodeByPath(const v8::HeapSnapshot* snapshot, |
| 2356 | const char* path[], |
| 2357 | int depth) { |
| 2358 | const v8::HeapGraphNode* node = snapshot->GetRoot(); |
| 2359 | for (int current_depth = 0; current_depth < depth; ++current_depth) { |
| 2360 | int i, count = node->GetChildrenCount(); |
| 2361 | for (i = 0; i < count; ++i) { |
| 2362 | const v8::HeapGraphEdge* edge = node->GetChild(i); |
| 2363 | const v8::HeapGraphNode* to_node = edge->GetToNode(); |
| 2364 | v8::String::Utf8Value edge_name(edge->GetName()); |
| 2365 | v8::String::Utf8Value node_name(to_node->GetName()); |
| 2366 | i::EmbeddedVector<char, 100> name; |
| 2367 | i::SNPrintF(name, "%s::%s", *edge_name, *node_name); |
| 2368 | if (strstr(name.start(), path[current_depth])) { |
| 2369 | node = to_node; |
| 2370 | break; |
| 2371 | } |
| 2372 | } |
| 2373 | if (i == count) return NULL; |
| 2374 | } |
| 2375 | return node; |
| 2376 | } |
| 2377 | |
| 2378 | |
| 2379 | TEST(CheckCodeNames) { |
| 2380 | LocalContext env; |
| 2381 | v8::HandleScope scope(env->GetIsolate()); |
| 2382 | v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
| 2383 | CompileRun("var a = 1.1;"); |
| 2384 | const v8::HeapSnapshot* snapshot = |
| 2385 | heap_profiler->TakeHeapSnapshot(v8_str("CheckCodeNames")); |
| 2386 | CHECK(ValidateSnapshot(snapshot)); |
| 2387 | |
| 2388 | const char* stub_path[] = { |
| 2389 | "::(GC roots)", |
| 2390 | "::(Strong roots)", |
| 2391 | "code_stubs::", |
| 2392 | "::(ArraySingleArgumentConstructorStub code)" |
| 2393 | }; |
| 2394 | const v8::HeapGraphNode* node = GetNodeByPath(snapshot, |
| 2395 | stub_path, arraysize(stub_path)); |
| 2396 | CHECK_NE(NULL, node); |
| 2397 | |
| 2398 | const char* builtin_path1[] = { |
| 2399 | "::(GC roots)", |
| 2400 | "::(Builtins)", |
| 2401 | "::(KeyedLoadIC_Generic builtin)" |
| 2402 | }; |
| 2403 | node = GetNodeByPath(snapshot, builtin_path1, arraysize(builtin_path1)); |
| 2404 | CHECK_NE(NULL, node); |
| 2405 | |
| 2406 | const char* builtin_path2[] = {"::(GC roots)", "::(Builtins)", |
| 2407 | "::(CompileLazy builtin)"}; |
| 2408 | node = GetNodeByPath(snapshot, builtin_path2, arraysize(builtin_path2)); |
| 2409 | CHECK_NE(NULL, node); |
| 2410 | v8::String::Utf8Value node_name(node->GetName()); |
| 2411 | CHECK_EQ("(CompileLazy builtin)", *node_name); |
| 2412 | } |
| 2413 | |
| 2414 | |
| 2415 | static const char* record_trace_tree_source = |
| 2416 | "var topFunctions = [];\n" |
| 2417 | "var global = this;\n" |
| 2418 | "function generateFunctions(width, depth) {\n" |
| 2419 | " var script = [];\n" |
| 2420 | " for (var i = 0; i < width; i++) {\n" |
| 2421 | " for (var j = 0; j < depth; j++) {\n" |
| 2422 | " script.push('function f_' + i + '_' + j + '(x) {\\n');\n" |
| 2423 | " script.push(' try {\\n');\n" |
| 2424 | " if (j < depth-2) {\n" |
| 2425 | " script.push(' return f_' + i + '_' + (j+1) + '(x+1);\\n');\n" |
| 2426 | " } else if (j == depth - 2) {\n" |
| 2427 | " script.push(' return new f_' + i + '_' + (depth - 1) + '();\\n');\n" |
| 2428 | " } else if (j == depth - 1) {\n" |
| 2429 | " script.push(' this.ts = Date.now();\\n');\n" |
| 2430 | " }\n" |
| 2431 | " script.push(' } catch (e) {}\\n');\n" |
| 2432 | " script.push('}\\n');\n" |
| 2433 | " \n" |
| 2434 | " }\n" |
| 2435 | " }\n" |
| 2436 | " var script = script.join('');\n" |
| 2437 | " // throw script;\n" |
| 2438 | " global.eval(script);\n" |
| 2439 | " for (var i = 0; i < width; i++) {\n" |
| 2440 | " topFunctions.push(this['f_' + i + '_0']);\n" |
| 2441 | " }\n" |
| 2442 | "}\n" |
| 2443 | "\n" |
| 2444 | "var width = 3;\n" |
| 2445 | "var depth = 3;\n" |
| 2446 | "generateFunctions(width, depth);\n" |
| 2447 | "var instances = [];\n" |
| 2448 | "function start() {\n" |
| 2449 | " for (var i = 0; i < width; i++) {\n" |
| 2450 | " instances.push(topFunctions[i](0));\n" |
| 2451 | " }\n" |
| 2452 | "}\n" |
| 2453 | "\n" |
| 2454 | "for (var i = 0; i < 100; i++) start();\n"; |
| 2455 | |
| 2456 | |
| 2457 | static AllocationTraceNode* FindNode( |
| 2458 | AllocationTracker* tracker, const Vector<const char*>& names) { |
| 2459 | AllocationTraceNode* node = tracker->trace_tree()->root(); |
| 2460 | for (int i = 0; node != NULL && i < names.length(); i++) { |
| 2461 | const char* name = names[i]; |
| 2462 | Vector<AllocationTraceNode*> children = node->children(); |
| 2463 | node = NULL; |
| 2464 | for (int j = 0; j < children.length(); j++) { |
| 2465 | unsigned index = children[j]->function_info_index(); |
| 2466 | AllocationTracker::FunctionInfo* info = |
| 2467 | tracker->function_info_list()[index]; |
| 2468 | if (info && strcmp(info->name, name) == 0) { |
| 2469 | node = children[j]; |
| 2470 | break; |
| 2471 | } |
| 2472 | } |
| 2473 | } |
| 2474 | return node; |
| 2475 | } |
| 2476 | |
| 2477 | |
| 2478 | TEST(ArrayGrowLeftTrim) { |
| 2479 | LocalContext env; |
| 2480 | v8::HandleScope scope(env->GetIsolate()); |
| 2481 | v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
| 2482 | heap_profiler->StartTrackingHeapObjects(true); |
| 2483 | |
| 2484 | CompileRun( |
| 2485 | "var a = [];\n" |
| 2486 | "for (var i = 0; i < 5; ++i)\n" |
| 2487 | " a[i] = i;\n" |
| 2488 | "for (var i = 0; i < 3; ++i)\n" |
| 2489 | " a.shift();\n"); |
| 2490 | |
| 2491 | const char* names[] = {""}; |
| 2492 | AllocationTracker* tracker = |
| 2493 | reinterpret_cast<i::HeapProfiler*>(heap_profiler)->allocation_tracker(); |
| 2494 | CHECK_NE(NULL, tracker); |
| 2495 | // Resolve all function locations. |
| 2496 | tracker->PrepareForSerialization(); |
| 2497 | // Print for better diagnostics in case of failure. |
| 2498 | tracker->trace_tree()->Print(tracker); |
| 2499 | |
| 2500 | AllocationTraceNode* node = |
| 2501 | FindNode(tracker, Vector<const char*>(names, arraysize(names))); |
| 2502 | CHECK_NE(NULL, node); |
| 2503 | CHECK_GE(node->allocation_count(), 2); |
| 2504 | CHECK_GE(node->allocation_size(), 4 * 5); |
| 2505 | heap_profiler->StopTrackingHeapObjects(); |
| 2506 | } |
| 2507 | |
| 2508 | |
| 2509 | TEST(TrackHeapAllocations) { |
| 2510 | v8::HandleScope scope(v8::Isolate::GetCurrent()); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2511 | LocalContext env; |
| 2512 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2513 | v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
| 2514 | heap_profiler->StartTrackingHeapObjects(true); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2515 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2516 | CompileRun(record_trace_tree_source); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2517 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 2518 | AllocationTracker* tracker = |
| 2519 | reinterpret_cast<i::HeapProfiler*>(heap_profiler)->allocation_tracker(); |
| 2520 | CHECK_NE(NULL, tracker); |
| 2521 | // Resolve all function locations. |
| 2522 | tracker->PrepareForSerialization(); |
| 2523 | // Print for better diagnostics in case of failure. |
| 2524 | tracker->trace_tree()->Print(tracker); |
| 2525 | |
| 2526 | const char* names[] = {"", "start", "f_0_0", "f_0_1", "f_0_2"}; |
| 2527 | AllocationTraceNode* node = |
| 2528 | FindNode(tracker, Vector<const char*>(names, arraysize(names))); |
| 2529 | CHECK_NE(NULL, node); |
| 2530 | CHECK_GE(node->allocation_count(), 100); |
| 2531 | CHECK_GE(node->allocation_size(), 4 * node->allocation_count()); |
| 2532 | heap_profiler->StopTrackingHeapObjects(); |
| 2533 | } |
| 2534 | |
| 2535 | |
| 2536 | static const char* inline_heap_allocation_source = |
| 2537 | "function f_0(x) {\n" |
| 2538 | " return f_1(x+1);\n" |
| 2539 | "}\n" |
| 2540 | "%NeverOptimizeFunction(f_0);\n" |
| 2541 | "function f_1(x) {\n" |
| 2542 | " return new f_2(x+1);\n" |
| 2543 | "}\n" |
| 2544 | "function f_2(x) {\n" |
| 2545 | " this.foo = x;\n" |
| 2546 | "}\n" |
| 2547 | "var instances = [];\n" |
| 2548 | "function start() {\n" |
| 2549 | " instances.push(f_0(0));\n" |
| 2550 | "}\n" |
| 2551 | "\n" |
| 2552 | "for (var i = 0; i < 100; i++) start();\n"; |
| 2553 | |
| 2554 | |
| 2555 | TEST(TrackBumpPointerAllocations) { |
| 2556 | i::FLAG_allow_natives_syntax = true; |
| 2557 | v8::HandleScope scope(v8::Isolate::GetCurrent()); |
| 2558 | LocalContext env; |
| 2559 | |
| 2560 | v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
| 2561 | const char* names[] = {"", "start", "f_0", "f_1"}; |
| 2562 | // First check that normally all allocations are recorded. |
| 2563 | { |
| 2564 | heap_profiler->StartTrackingHeapObjects(true); |
| 2565 | |
| 2566 | CompileRun(inline_heap_allocation_source); |
| 2567 | |
| 2568 | AllocationTracker* tracker = |
| 2569 | reinterpret_cast<i::HeapProfiler*>(heap_profiler)->allocation_tracker(); |
| 2570 | CHECK_NE(NULL, tracker); |
| 2571 | // Resolve all function locations. |
| 2572 | tracker->PrepareForSerialization(); |
| 2573 | // Print for better diagnostics in case of failure. |
| 2574 | tracker->trace_tree()->Print(tracker); |
| 2575 | |
| 2576 | AllocationTraceNode* node = |
| 2577 | FindNode(tracker, Vector<const char*>(names, arraysize(names))); |
| 2578 | CHECK_NE(NULL, node); |
| 2579 | CHECK_GE(node->allocation_count(), 100); |
| 2580 | CHECK_GE(node->allocation_size(), 4 * node->allocation_count()); |
| 2581 | heap_profiler->StopTrackingHeapObjects(); |
| 2582 | } |
| 2583 | |
| 2584 | { |
| 2585 | heap_profiler->StartTrackingHeapObjects(true); |
| 2586 | |
| 2587 | // Now check that not all allocations are tracked if we manually reenable |
| 2588 | // inline allocations. |
| 2589 | CHECK(CcTest::heap()->inline_allocation_disabled()); |
| 2590 | CcTest::heap()->EnableInlineAllocation(); |
| 2591 | |
| 2592 | CompileRun(inline_heap_allocation_source); |
| 2593 | |
| 2594 | AllocationTracker* tracker = |
| 2595 | reinterpret_cast<i::HeapProfiler*>(heap_profiler)->allocation_tracker(); |
| 2596 | CHECK_NE(NULL, tracker); |
| 2597 | // Resolve all function locations. |
| 2598 | tracker->PrepareForSerialization(); |
| 2599 | // Print for better diagnostics in case of failure. |
| 2600 | tracker->trace_tree()->Print(tracker); |
| 2601 | |
| 2602 | AllocationTraceNode* node = |
| 2603 | FindNode(tracker, Vector<const char*>(names, arraysize(names))); |
| 2604 | CHECK_NE(NULL, node); |
| 2605 | CHECK_LT(node->allocation_count(), 100); |
| 2606 | |
| 2607 | CcTest::heap()->DisableInlineAllocation(); |
| 2608 | heap_profiler->StopTrackingHeapObjects(); |
| 2609 | } |
| 2610 | } |
| 2611 | |
| 2612 | |
| 2613 | TEST(TrackV8ApiAllocation) { |
| 2614 | v8::HandleScope scope(v8::Isolate::GetCurrent()); |
| 2615 | LocalContext env; |
| 2616 | |
| 2617 | v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
| 2618 | const char* names[] = { "(V8 API)" }; |
| 2619 | heap_profiler->StartTrackingHeapObjects(true); |
| 2620 | |
| 2621 | v8::Handle<v8::Object> o1 = v8::Object::New(env->GetIsolate()); |
| 2622 | o1->Clone(); |
| 2623 | |
| 2624 | AllocationTracker* tracker = |
| 2625 | reinterpret_cast<i::HeapProfiler*>(heap_profiler)->allocation_tracker(); |
| 2626 | CHECK_NE(NULL, tracker); |
| 2627 | // Resolve all function locations. |
| 2628 | tracker->PrepareForSerialization(); |
| 2629 | // Print for better diagnostics in case of failure. |
| 2630 | tracker->trace_tree()->Print(tracker); |
| 2631 | |
| 2632 | AllocationTraceNode* node = |
| 2633 | FindNode(tracker, Vector<const char*>(names, arraysize(names))); |
| 2634 | CHECK_NE(NULL, node); |
| 2635 | CHECK_GE(node->allocation_count(), 2); |
| 2636 | CHECK_GE(node->allocation_size(), 4 * node->allocation_count()); |
| 2637 | heap_profiler->StopTrackingHeapObjects(); |
| 2638 | } |
| 2639 | |
| 2640 | |
| 2641 | TEST(ArrayBufferAndArrayBufferView) { |
| 2642 | LocalContext env; |
| 2643 | v8::HandleScope scope(env->GetIsolate()); |
| 2644 | v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
| 2645 | CompileRun("arr1 = new Uint32Array(100);\n"); |
| 2646 | const v8::HeapSnapshot* snapshot = |
| 2647 | heap_profiler->TakeHeapSnapshot(v8_str("snapshot")); |
| 2648 | CHECK(ValidateSnapshot(snapshot)); |
| 2649 | const v8::HeapGraphNode* global = GetGlobalObject(snapshot); |
| 2650 | const v8::HeapGraphNode* arr1_obj = |
| 2651 | GetProperty(global, v8::HeapGraphEdge::kProperty, "arr1"); |
| 2652 | CHECK_NE(NULL, arr1_obj); |
| 2653 | const v8::HeapGraphNode* arr1_buffer = |
| 2654 | GetProperty(arr1_obj, v8::HeapGraphEdge::kInternal, "buffer"); |
| 2655 | CHECK_NE(NULL, arr1_buffer); |
| 2656 | const v8::HeapGraphNode* first_view = |
| 2657 | GetProperty(arr1_buffer, v8::HeapGraphEdge::kWeak, "weak_first_view"); |
| 2658 | CHECK_NE(NULL, first_view); |
| 2659 | const v8::HeapGraphNode* backing_store = |
| 2660 | GetProperty(arr1_buffer, v8::HeapGraphEdge::kInternal, "backing_store"); |
| 2661 | CHECK_NE(NULL, backing_store); |
| 2662 | CHECK_EQ(400, static_cast<int>(backing_store->GetShallowSize())); |
| 2663 | } |
| 2664 | |
| 2665 | |
| 2666 | static int GetRetainersCount(const v8::HeapSnapshot* snapshot, |
| 2667 | const v8::HeapGraphNode* node) { |
| 2668 | int count = 0; |
| 2669 | for (int i = 0, l = snapshot->GetNodesCount(); i < l; ++i) { |
| 2670 | const v8::HeapGraphNode* parent = snapshot->GetNode(i); |
| 2671 | for (int j = 0, l2 = parent->GetChildrenCount(); j < l2; ++j) { |
| 2672 | if (parent->GetChild(j)->GetToNode() == node) { |
| 2673 | ++count; |
| 2674 | } |
| 2675 | } |
| 2676 | } |
| 2677 | return count; |
| 2678 | } |
| 2679 | |
| 2680 | |
| 2681 | TEST(ArrayBufferSharedBackingStore) { |
| 2682 | LocalContext env; |
| 2683 | v8::Isolate* isolate = env->GetIsolate(); |
| 2684 | v8::HandleScope handle_scope(isolate); |
| 2685 | v8::HeapProfiler* heap_profiler = isolate->GetHeapProfiler(); |
| 2686 | |
| 2687 | v8::Local<v8::ArrayBuffer> ab = v8::ArrayBuffer::New(isolate, 1024); |
| 2688 | CHECK_EQ(1024, static_cast<int>(ab->ByteLength())); |
| 2689 | CHECK(!ab->IsExternal()); |
| 2690 | v8::ArrayBuffer::Contents ab_contents = ab->Externalize(); |
| 2691 | CHECK(ab->IsExternal()); |
| 2692 | |
| 2693 | CHECK_EQ(1024, static_cast<int>(ab_contents.ByteLength())); |
| 2694 | void* data = ab_contents.Data(); |
| 2695 | DCHECK(data != NULL); |
| 2696 | v8::Local<v8::ArrayBuffer> ab2 = |
| 2697 | v8::ArrayBuffer::New(isolate, data, ab_contents.ByteLength()); |
| 2698 | CHECK(ab2->IsExternal()); |
| 2699 | env->Global()->Set(v8_str("ab1"), ab); |
| 2700 | env->Global()->Set(v8_str("ab2"), ab2); |
| 2701 | |
| 2702 | v8::Handle<v8::Value> result = CompileRun("ab2.byteLength"); |
| 2703 | CHECK_EQ(1024, result->Int32Value()); |
| 2704 | |
| 2705 | const v8::HeapSnapshot* snapshot = |
| 2706 | heap_profiler->TakeHeapSnapshot(v8_str("snapshot")); |
| 2707 | CHECK(ValidateSnapshot(snapshot)); |
| 2708 | const v8::HeapGraphNode* global = GetGlobalObject(snapshot); |
| 2709 | const v8::HeapGraphNode* ab1_node = |
| 2710 | GetProperty(global, v8::HeapGraphEdge::kProperty, "ab1"); |
| 2711 | CHECK_NE(NULL, ab1_node); |
| 2712 | const v8::HeapGraphNode* ab1_data = |
| 2713 | GetProperty(ab1_node, v8::HeapGraphEdge::kInternal, "backing_store"); |
| 2714 | CHECK_NE(NULL, ab1_data); |
| 2715 | const v8::HeapGraphNode* ab2_node = |
| 2716 | GetProperty(global, v8::HeapGraphEdge::kProperty, "ab2"); |
| 2717 | CHECK_NE(NULL, ab2_node); |
| 2718 | const v8::HeapGraphNode* ab2_data = |
| 2719 | GetProperty(ab2_node, v8::HeapGraphEdge::kInternal, "backing_store"); |
| 2720 | CHECK_NE(NULL, ab2_data); |
| 2721 | CHECK_EQ(ab1_data, ab2_data); |
| 2722 | CHECK_EQ(2, GetRetainersCount(snapshot, ab1_data)); |
| 2723 | free(data); |
| 2724 | } |
| 2725 | |
| 2726 | |
| 2727 | TEST(BoxObject) { |
| 2728 | v8::Isolate* isolate = CcTest::isolate(); |
| 2729 | v8::HandleScope scope(isolate); |
| 2730 | LocalContext env; |
| 2731 | v8::Handle<v8::Object> global_proxy = env->Global(); |
| 2732 | v8::Handle<v8::Object> global = global_proxy->GetPrototype().As<v8::Object>(); |
| 2733 | |
| 2734 | i::Factory* factory = CcTest::i_isolate()->factory(); |
| 2735 | i::Handle<i::String> string = factory->NewStringFromStaticChars("string"); |
| 2736 | i::Handle<i::Object> box = factory->NewBox(string); |
| 2737 | global->Set(0, v8::ToApiHandle<v8::Object>(box)); |
| 2738 | |
| 2739 | v8::HeapProfiler* heap_profiler = isolate->GetHeapProfiler(); |
| 2740 | const v8::HeapSnapshot* snapshot = |
| 2741 | heap_profiler->TakeHeapSnapshot(v8_str("snapshot")); |
| 2742 | CHECK(ValidateSnapshot(snapshot)); |
| 2743 | const v8::HeapGraphNode* global_node = GetGlobalObject(snapshot); |
| 2744 | const v8::HeapGraphNode* box_node = |
| 2745 | GetProperty(global_node, v8::HeapGraphEdge::kElement, "0"); |
| 2746 | CHECK_NE(NULL, box_node); |
| 2747 | v8::String::Utf8Value box_node_name(box_node->GetName()); |
| 2748 | CHECK_EQ("system / Box", *box_node_name); |
| 2749 | const v8::HeapGraphNode* box_value = |
| 2750 | GetProperty(box_node, v8::HeapGraphEdge::kInternal, "value"); |
| 2751 | CHECK_NE(NULL, box_value); |
| 2752 | } |
| 2753 | |
| 2754 | |
| 2755 | TEST(WeakContainers) { |
| 2756 | i::FLAG_allow_natives_syntax = true; |
| 2757 | LocalContext env; |
| 2758 | v8::HandleScope scope(env->GetIsolate()); |
| 2759 | if (!CcTest::i_isolate()->use_crankshaft()) return; |
| 2760 | v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
| 2761 | CompileRun( |
| 2762 | "function foo(a) { return a.x; }\n" |
| 2763 | "obj = {x : 123};\n" |
| 2764 | "foo(obj);\n" |
| 2765 | "foo(obj);\n" |
| 2766 | "%OptimizeFunctionOnNextCall(foo);\n" |
| 2767 | "foo(obj);\n"); |
| 2768 | const v8::HeapSnapshot* snapshot = |
| 2769 | heap_profiler->TakeHeapSnapshot(v8_str("snapshot")); |
| 2770 | CHECK(ValidateSnapshot(snapshot)); |
| 2771 | const v8::HeapGraphNode* global = GetGlobalObject(snapshot); |
| 2772 | const v8::HeapGraphNode* obj = |
| 2773 | GetProperty(global, v8::HeapGraphEdge::kProperty, "obj"); |
| 2774 | CHECK_NE(NULL, obj); |
| 2775 | const v8::HeapGraphNode* map = |
| 2776 | GetProperty(obj, v8::HeapGraphEdge::kInternal, "map"); |
| 2777 | CHECK_NE(NULL, map); |
| 2778 | const v8::HeapGraphNode* dependent_code = |
| 2779 | GetProperty(map, v8::HeapGraphEdge::kInternal, "dependent_code"); |
| 2780 | if (!dependent_code) return; |
| 2781 | int count = dependent_code->GetChildrenCount(); |
| 2782 | CHECK_NE(0, count); |
| 2783 | for (int i = 0; i < count; ++i) { |
| 2784 | const v8::HeapGraphEdge* prop = dependent_code->GetChild(i); |
| 2785 | CHECK_EQ(v8::HeapGraphEdge::kWeak, prop->GetType()); |
| 2786 | } |
| 2787 | } |
| 2788 | |
| 2789 | |
| 2790 | static inline i::Address ToAddress(int n) { |
| 2791 | return reinterpret_cast<i::Address>(n); |
| 2792 | } |
| 2793 | |
| 2794 | |
| 2795 | TEST(AddressToTraceMap) { |
| 2796 | i::AddressToTraceMap map; |
| 2797 | |
| 2798 | CHECK_EQ(0, map.GetTraceNodeId(ToAddress(150))); |
| 2799 | |
| 2800 | // [0x100, 0x200) -> 1 |
| 2801 | map.AddRange(ToAddress(0x100), 0x100, 1U); |
| 2802 | CHECK_EQ(0, map.GetTraceNodeId(ToAddress(0x50))); |
| 2803 | CHECK_EQ(1, map.GetTraceNodeId(ToAddress(0x100))); |
| 2804 | CHECK_EQ(1, map.GetTraceNodeId(ToAddress(0x150))); |
| 2805 | CHECK_EQ(0, map.GetTraceNodeId(ToAddress(0x100 + 0x100))); |
| 2806 | CHECK_EQ(1, static_cast<int>(map.size())); |
| 2807 | |
| 2808 | // [0x100, 0x200) -> 1, [0x200, 0x300) -> 2 |
| 2809 | map.AddRange(ToAddress(0x200), 0x100, 2U); |
| 2810 | CHECK_EQ(2, map.GetTraceNodeId(ToAddress(0x2a0))); |
| 2811 | CHECK_EQ(2, static_cast<int>(map.size())); |
| 2812 | |
| 2813 | // [0x100, 0x180) -> 1, [0x180, 0x280) -> 3, [0x280, 0x300) -> 2 |
| 2814 | map.AddRange(ToAddress(0x180), 0x100, 3U); |
| 2815 | CHECK_EQ(1, map.GetTraceNodeId(ToAddress(0x17F))); |
| 2816 | CHECK_EQ(2, map.GetTraceNodeId(ToAddress(0x280))); |
| 2817 | CHECK_EQ(3, map.GetTraceNodeId(ToAddress(0x180))); |
| 2818 | CHECK_EQ(3, static_cast<int>(map.size())); |
| 2819 | |
| 2820 | // [0x100, 0x180) -> 1, [0x180, 0x280) -> 3, [0x280, 0x300) -> 2, |
| 2821 | // [0x400, 0x500) -> 4 |
| 2822 | map.AddRange(ToAddress(0x400), 0x100, 4U); |
| 2823 | CHECK_EQ(1, map.GetTraceNodeId(ToAddress(0x17F))); |
| 2824 | CHECK_EQ(2, map.GetTraceNodeId(ToAddress(0x280))); |
| 2825 | CHECK_EQ(3, map.GetTraceNodeId(ToAddress(0x180))); |
| 2826 | CHECK_EQ(4, map.GetTraceNodeId(ToAddress(0x450))); |
| 2827 | CHECK_EQ(0, map.GetTraceNodeId(ToAddress(0x500))); |
| 2828 | CHECK_EQ(0, map.GetTraceNodeId(ToAddress(0x350))); |
| 2829 | CHECK_EQ(4, static_cast<int>(map.size())); |
| 2830 | |
| 2831 | // [0x100, 0x180) -> 1, [0x180, 0x200) -> 3, [0x200, 0x600) -> 5 |
| 2832 | map.AddRange(ToAddress(0x200), 0x400, 5U); |
| 2833 | CHECK_EQ(5, map.GetTraceNodeId(ToAddress(0x200))); |
| 2834 | CHECK_EQ(5, map.GetTraceNodeId(ToAddress(0x400))); |
| 2835 | CHECK_EQ(3, static_cast<int>(map.size())); |
| 2836 | |
| 2837 | // [0x100, 0x180) -> 1, [0x180, 0x200) -> 7, [0x200, 0x600) ->5 |
| 2838 | map.AddRange(ToAddress(0x180), 0x80, 6U); |
| 2839 | map.AddRange(ToAddress(0x180), 0x80, 7U); |
| 2840 | CHECK_EQ(7, map.GetTraceNodeId(ToAddress(0x180))); |
| 2841 | CHECK_EQ(5, map.GetTraceNodeId(ToAddress(0x200))); |
| 2842 | CHECK_EQ(3, static_cast<int>(map.size())); |
| 2843 | |
| 2844 | map.Clear(); |
| 2845 | CHECK_EQ(0, static_cast<int>(map.size())); |
| 2846 | CHECK_EQ(0, map.GetTraceNodeId(ToAddress(0x400))); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 2847 | } |