| OLD | NEW |
| 1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 // | 27 // |
| 28 // Tests for heap profiler | 28 // Tests for heap profiler |
| 29 | 29 |
| 30 #include <ctype.h> | 30 #include <ctype.h> |
| 31 | 31 |
| 32 #include "v8.h" | 32 #include "v8.h" |
| 33 | 33 |
| 34 #include "allocation-tracker.h" | |
| 35 #include "cctest.h" | 34 #include "cctest.h" |
| 36 #include "hashmap.h" | 35 #include "hashmap.h" |
| 37 #include "heap-profiler.h" | 36 #include "heap-profiler.h" |
| 38 #include "snapshot.h" | 37 #include "snapshot.h" |
| 39 #include "debug.h" | 38 #include "debug.h" |
| 40 #include "utils-inl.h" | 39 #include "utils-inl.h" |
| 41 #include "../include/v8-profiler.h" | 40 #include "../include/v8-profiler.h" |
| 42 | 41 |
| 43 using i::AllocationTraceNode; | |
| 44 using i::AllocationTraceTree; | |
| 45 using i::AllocationTracker; | |
| 46 using i::HashMap; | |
| 47 using i::Vector; | |
| 48 | |
| 49 namespace { | 42 namespace { |
| 50 | 43 |
| 51 class NamedEntriesDetector { | 44 class NamedEntriesDetector { |
| 52 public: | 45 public: |
| 53 NamedEntriesDetector() | 46 NamedEntriesDetector() |
| 54 : has_A2(false), has_B2(false), has_C2(false) { | 47 : has_A2(false), has_B2(false), has_C2(false) { |
| 55 } | 48 } |
| 56 | 49 |
| 57 void CheckEntry(i::HeapEntry* entry) { | 50 void CheckEntry(i::HeapEntry* entry) { |
| 58 if (strcmp(entry->name(), "A2") == 0) has_A2 = true; | 51 if (strcmp(entry->name(), "A2") == 0) has_A2 = true; |
| (...skipping 2015 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 2074 "var a = [];\n" | 2067 "var a = [];\n" |
| 2075 "for (var i = 0; i < 5; ++i)\n" | 2068 "for (var i = 0; i < 5; ++i)\n" |
| 2076 " a[i] = i;\n" | 2069 " a[i] = i;\n" |
| 2077 "findUntrackedObjects();\n" | 2070 "findUntrackedObjects();\n" |
| 2078 "for (var i = 0; i < 3; ++i)\n" | 2071 "for (var i = 0; i < 3; ++i)\n" |
| 2079 " a.shift();\n" | 2072 " a.shift();\n" |
| 2080 "findUntrackedObjects();\n"); | 2073 "findUntrackedObjects();\n"); |
| 2081 } | 2074 } |
| 2082 | 2075 |
| 2083 | 2076 |
| 2084 static const char* record_trace_tree_source = | 2077 static const v8::HeapGraphNode* GetNodeByPath(const v8::HeapSnapshot* snapshot, |
| 2085 "var topFunctions = [];\n" | 2078 const char* path[], |
| 2086 "var global = this;\n" | 2079 int depth) { |
| 2087 "function generateFunctions(width, depth) {\n" | 2080 const v8::HeapGraphNode* node = snapshot->GetRoot(); |
| 2088 " var script = [];\n" | 2081 for (int current_depth = 0; current_depth < depth; ++current_depth) { |
| 2089 " for (var i = 0; i < width; i++) {\n" | 2082 int i, count = node->GetChildrenCount(); |
| 2090 " for (var j = 0; j < depth; j++) {\n" | 2083 for (i = 0; i < count; ++i) { |
| 2091 " script.push('function f_' + i + '_' + j + '(x) {\\n');\n" | 2084 const v8::HeapGraphEdge* edge = node->GetChild(i); |
| 2092 " script.push(' try {\\n');\n" | 2085 const v8::HeapGraphNode* to_node = edge->GetToNode(); |
| 2093 " if (j < depth-2) {\n" | 2086 v8::String::Utf8Value edge_name(edge->GetName()); |
| 2094 " script.push(' return f_' + i + '_' + (j+1) + '(x+1);\\n');\n" | 2087 v8::String::Utf8Value node_name(to_node->GetName()); |
| 2095 " } else if (j == depth - 2) {\n" | 2088 i::EmbeddedVector<char, 100> name; |
| 2096 " script.push(' return new f_' + i + '_' + (depth - 1) + '();\\n');\n" | 2089 i::OS::SNPrintF(name, "%s::%s", *edge_name, *node_name); |
| 2097 " } else if (j == depth - 1) {\n" | 2090 if (strstr(name.start(), path[current_depth])) { |
| 2098 " script.push(' this.ts = Date.now();\\n');\n" | 2091 node = to_node; |
| 2099 " }\n" | |
| 2100 " script.push(' } catch (e) {}\\n');\n" | |
| 2101 " script.push('}\\n');\n" | |
| 2102 " \n" | |
| 2103 " }\n" | |
| 2104 " }\n" | |
| 2105 " var script = script.join('');\n" | |
| 2106 " // throw script;\n" | |
| 2107 " global.eval(script);\n" | |
| 2108 " for (var i = 0; i < width; i++) {\n" | |
| 2109 " topFunctions.push(this['f_' + i + '_0']);\n" | |
| 2110 " }\n" | |
| 2111 "}\n" | |
| 2112 "\n" | |
| 2113 "var width = 3;\n" | |
| 2114 "var depth = 3;\n" | |
| 2115 "generateFunctions(width, depth);\n" | |
| 2116 "var instances = [];\n" | |
| 2117 "function start() {\n" | |
| 2118 " for (var i = 0; i < width; i++) {\n" | |
| 2119 " instances.push(topFunctions[i](0));\n" | |
| 2120 " }\n" | |
| 2121 "}\n" | |
| 2122 "\n" | |
| 2123 "for (var i = 0; i < 100; i++) start();\n"; | |
| 2124 | |
| 2125 | |
| 2126 static i::HeapSnapshot* ToInternal(const v8::HeapSnapshot* snapshot) { | |
| 2127 return const_cast<i::HeapSnapshot*>( | |
| 2128 reinterpret_cast<const i::HeapSnapshot*>(snapshot)); | |
| 2129 } | |
| 2130 | |
| 2131 | |
| 2132 static AllocationTraceNode* FindNode( | |
| 2133 AllocationTracker* tracker, const Vector<const char*>& names) { | |
| 2134 AllocationTraceNode* node = tracker->trace_tree()->root(); | |
| 2135 for (int i = 0; node != NULL && i < names.length(); i++) { | |
| 2136 const char* name = names[i]; | |
| 2137 Vector<AllocationTraceNode*> children = node->children(); | |
| 2138 node = NULL; | |
| 2139 for (int j = 0; j < children.length(); j++) { | |
| 2140 v8::SnapshotObjectId id = children[j]->function_id(); | |
| 2141 AllocationTracker::FunctionInfo* info = tracker->GetFunctionInfo(id); | |
| 2142 if (info && strcmp(info->name, name) == 0) { | |
| 2143 node = children[j]; | |
| 2144 break; | 2092 break; |
| 2145 } | 2093 } |
| 2146 } | 2094 } |
| 2095 if (i == count) return NULL; |
| 2147 } | 2096 } |
| 2148 return node; | 2097 return node; |
| 2149 } | 2098 } |
| 2150 | 2099 |
| 2151 | 2100 |
| 2152 TEST(TrackHeapAllocations) { | 2101 TEST(CheckCodeNames) { |
| 2153 v8::HandleScope scope(v8::Isolate::GetCurrent()); | |
| 2154 LocalContext env; | 2102 LocalContext env; |
| 2103 v8::HandleScope scope(env->GetIsolate()); |
| 2104 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
| 2105 CompileRun("var a = 1.1;"); |
| 2106 const v8::HeapSnapshot* snapshot = |
| 2107 heap_profiler->TakeHeapSnapshot(v8_str("CheckCodeNames")); |
| 2108 CHECK(ValidateSnapshot(snapshot)); |
| 2155 | 2109 |
| 2156 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); | 2110 const char* stub_path[] = { |
| 2157 heap_profiler->StartRecordingHeapAllocations(); | 2111 "::(GC roots)", |
| 2112 "::(Strong roots)", |
| 2113 "code_stubs::", |
| 2114 "::(ArraySingleArgumentConstructorStub code)" |
| 2115 }; |
| 2116 const v8::HeapGraphNode* node = GetNodeByPath(snapshot, |
| 2117 stub_path, ARRAY_SIZE(stub_path)); |
| 2118 CHECK_NE(NULL, node); |
| 2158 | 2119 |
| 2159 CompileRun(record_trace_tree_source); | 2120 const char* builtin_path[] = { |
| 2160 | 2121 "::(GC roots)", |
| 2161 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot( | 2122 "::(Builtins)", |
| 2162 v8::String::New("Test")); | 2123 "::(KeyedLoadIC_Generic code)" |
| 2163 i::HeapSnapshotsCollection* collection = ToInternal(snapshot)->collection(); | 2124 }; |
| 2164 AllocationTracker* tracker = collection->allocation_tracker(); | 2125 node = GetNodeByPath(snapshot, builtin_path, ARRAY_SIZE(builtin_path)); |
| 2165 CHECK_NE(NULL, tracker); | |
| 2166 // Resolve all function locations. | |
| 2167 tracker->PrepareForSerialization(); | |
| 2168 // Print for better diagnostics in case of failure. | |
| 2169 tracker->trace_tree()->Print(tracker); | |
| 2170 | |
| 2171 const char* names[] = | |
| 2172 { "(anonymous function)", "start", "f_0_0", "f_0_1", "f_0_2" }; | |
| 2173 AllocationTraceNode* node = | |
| 2174 FindNode(tracker, Vector<const char*>(names, ARRAY_SIZE(names))); | |
| 2175 CHECK_NE(NULL, node); | 2126 CHECK_NE(NULL, node); |
| 2176 CHECK_GE(node->allocation_count(), 100); | |
| 2177 CHECK_GE(node->allocation_size(), 4 * node->allocation_count()); | |
| 2178 heap_profiler->StopRecordingHeapAllocations(); | |
| 2179 } | 2127 } |
| OLD | NEW |