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" |
34 #include "cctest.h" | 35 #include "cctest.h" |
35 #include "hashmap.h" | 36 #include "hashmap.h" |
36 #include "heap-profiler.h" | 37 #include "heap-profiler.h" |
37 #include "snapshot.h" | 38 #include "snapshot.h" |
38 #include "debug.h" | 39 #include "debug.h" |
39 #include "utils-inl.h" | 40 #include "utils-inl.h" |
40 #include "../include/v8-profiler.h" | 41 #include "../include/v8-profiler.h" |
41 | 42 |
| 43 using i::AllocationTraceNode; |
| 44 using i::AllocationTraceTree; |
| 45 using i::AllocationTracker; |
| 46 using i::HashMap; |
| 47 using i::Vector; |
| 48 |
42 namespace { | 49 namespace { |
43 | 50 |
44 class NamedEntriesDetector { | 51 class NamedEntriesDetector { |
45 public: | 52 public: |
46 NamedEntriesDetector() | 53 NamedEntriesDetector() |
47 : has_A2(false), has_B2(false), has_C2(false) { | 54 : has_A2(false), has_B2(false), has_C2(false) { |
48 } | 55 } |
49 | 56 |
50 void CheckEntry(i::HeapEntry* entry) { | 57 void CheckEntry(i::HeapEntry* entry) { |
51 if (strcmp(entry->name(), "A2") == 0) has_A2 = true; | 58 if (strcmp(entry->name(), "A2") == 0) has_A2 = true; |
(...skipping 2066 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2118 CHECK_NE(NULL, node); | 2125 CHECK_NE(NULL, node); |
2119 | 2126 |
2120 const char* builtin_path[] = { | 2127 const char* builtin_path[] = { |
2121 "::(GC roots)", | 2128 "::(GC roots)", |
2122 "::(Builtins)", | 2129 "::(Builtins)", |
2123 "::(KeyedLoadIC_Generic code)" | 2130 "::(KeyedLoadIC_Generic code)" |
2124 }; | 2131 }; |
2125 node = GetNodeByPath(snapshot, builtin_path, ARRAY_SIZE(builtin_path)); | 2132 node = GetNodeByPath(snapshot, builtin_path, ARRAY_SIZE(builtin_path)); |
2126 CHECK_NE(NULL, node); | 2133 CHECK_NE(NULL, node); |
2127 } | 2134 } |
| 2135 |
| 2136 |
| 2137 static const char* record_trace_tree_source = |
| 2138 "var topFunctions = [];\n" |
| 2139 "var global = this;\n" |
| 2140 "function generateFunctions(width, depth) {\n" |
| 2141 " var script = [];\n" |
| 2142 " for (var i = 0; i < width; i++) {\n" |
| 2143 " for (var j = 0; j < depth; j++) {\n" |
| 2144 " script.push('function f_' + i + '_' + j + '(x) {\\n');\n" |
| 2145 " script.push(' try {\\n');\n" |
| 2146 " if (j < depth-2) {\n" |
| 2147 " script.push(' return f_' + i + '_' + (j+1) + '(x+1);\\n');\n" |
| 2148 " } else if (j == depth - 2) {\n" |
| 2149 " script.push(' return new f_' + i + '_' + (depth - 1) + '();\\n');\n" |
| 2150 " } else if (j == depth - 1) {\n" |
| 2151 " script.push(' this.ts = Date.now();\\n');\n" |
| 2152 " }\n" |
| 2153 " script.push(' } catch (e) {}\\n');\n" |
| 2154 " script.push('}\\n');\n" |
| 2155 " \n" |
| 2156 " }\n" |
| 2157 " }\n" |
| 2158 " var script = script.join('');\n" |
| 2159 " // throw script;\n" |
| 2160 " global.eval(script);\n" |
| 2161 " for (var i = 0; i < width; i++) {\n" |
| 2162 " topFunctions.push(this['f_' + i + '_0']);\n" |
| 2163 " }\n" |
| 2164 "}\n" |
| 2165 "\n" |
| 2166 "var width = 3;\n" |
| 2167 "var depth = 3;\n" |
| 2168 "generateFunctions(width, depth);\n" |
| 2169 "var instances = [];\n" |
| 2170 "function start() {\n" |
| 2171 " for (var i = 0; i < width; i++) {\n" |
| 2172 " instances.push(topFunctions[i](0));\n" |
| 2173 " }\n" |
| 2174 "}\n" |
| 2175 "\n" |
| 2176 "for (var i = 0; i < 100; i++) start();\n"; |
| 2177 |
| 2178 |
| 2179 static i::HeapSnapshot* ToInternal(const v8::HeapSnapshot* snapshot) { |
| 2180 return const_cast<i::HeapSnapshot*>( |
| 2181 reinterpret_cast<const i::HeapSnapshot*>(snapshot)); |
| 2182 } |
| 2183 |
| 2184 |
| 2185 static AllocationTraceNode* FindNode( |
| 2186 AllocationTracker* tracker, const Vector<const char*>& names) { |
| 2187 AllocationTraceNode* node = tracker->trace_tree()->root(); |
| 2188 for (int i = 0; node != NULL && i < names.length(); i++) { |
| 2189 const char* name = names[i]; |
| 2190 Vector<AllocationTraceNode*> children = node->children(); |
| 2191 node = NULL; |
| 2192 for (int j = 0; j < children.length(); j++) { |
| 2193 v8::SnapshotObjectId id = children[j]->function_id(); |
| 2194 AllocationTracker::FunctionInfo* info = tracker->GetFunctionInfo(id); |
| 2195 if (info && strcmp(info->name, name) == 0) { |
| 2196 node = children[j]; |
| 2197 break; |
| 2198 } |
| 2199 } |
| 2200 } |
| 2201 return node; |
| 2202 } |
| 2203 |
| 2204 |
| 2205 TEST(TrackHeapAllocations) { |
| 2206 v8::HandleScope scope(v8::Isolate::GetCurrent()); |
| 2207 LocalContext env; |
| 2208 |
| 2209 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
| 2210 heap_profiler->StartRecordingHeapAllocations(); |
| 2211 |
| 2212 CompileRun(record_trace_tree_source); |
| 2213 |
| 2214 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot( |
| 2215 v8::String::New("Test")); |
| 2216 i::HeapSnapshotsCollection* collection = ToInternal(snapshot)->collection(); |
| 2217 AllocationTracker* tracker = collection->allocation_tracker(); |
| 2218 CHECK_NE(NULL, tracker); |
| 2219 // Resolve all function locations. |
| 2220 tracker->PrepareForSerialization(); |
| 2221 // Print for better diagnostics in case of failure. |
| 2222 tracker->trace_tree()->Print(tracker); |
| 2223 |
| 2224 const char* names[] = |
| 2225 { "(anonymous function)", "start", "f_0_0", "f_0_1", "f_0_2" }; |
| 2226 AllocationTraceNode* node = |
| 2227 FindNode(tracker, Vector<const char*>(names, ARRAY_SIZE(names))); |
| 2228 CHECK_NE(NULL, node); |
| 2229 CHECK_GE(node->allocation_count(), 100); |
| 2230 CHECK_GE(node->allocation_size(), 4 * node->allocation_count()); |
| 2231 heap_profiler->StopRecordingHeapAllocations(); |
| 2232 } |
OLD | NEW |