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 2013 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2072 CompileRun("var a = 1.2; var b = 1.0; var c = 1.0;"); | 2065 CompileRun("var a = 1.2; var b = 1.0; var c = 1.0;"); |
2073 CompileRun( | 2066 CompileRun( |
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 | |
2083 | |
2084 static const char* record_trace_tree_source = | |
2085 "var topFunctions = [];\n" | |
2086 "var global = this;\n" | |
2087 "function generateFunctions(width, depth) {\n" | |
2088 " var script = [];\n" | |
2089 " for (var i = 0; i < width; i++) {\n" | |
2090 " for (var j = 0; j < depth; j++) {\n" | |
2091 " script.push('function f_' + i + '_' + j + '(x) {\\n');\n" | |
2092 " script.push(' try {\\n');\n" | |
2093 " if (j < depth-2) {\n" | |
2094 " script.push(' return f_' + i + '_' + (j+1) + '(x+1);\\n');\n" | |
2095 " } else if (j == depth - 2) {\n" | |
2096 " script.push(' return new f_' + i + '_' + (depth - 1) + '();\\n');\n" | |
2097 " } else if (j == depth - 1) {\n" | |
2098 " script.push(' this.ts = Date.now();\\n');\n" | |
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; | |
2145 } | |
2146 } | |
2147 } | |
2148 return node; | |
2149 } | |
2150 | |
2151 | |
2152 TEST(TrackHeapAllocations) { | |
2153 v8::HandleScope scope(v8::Isolate::GetCurrent()); | |
2154 LocalContext env; | |
2155 | |
2156 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); | |
2157 heap_profiler->StartRecordingHeapAllocations(); | |
2158 | |
2159 CompileRun(record_trace_tree_source); | |
2160 | |
2161 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot( | |
2162 v8::String::New("Test")); | |
2163 i::HeapSnapshotsCollection* collection = ToInternal(snapshot)->collection(); | |
2164 AllocationTracker* tracker = collection->allocation_tracker(); | |
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); | |
2176 CHECK_GE(node->allocation_count(), 100); | |
2177 CHECK_GE(node->allocation_size(), 4 * node->allocation_count()); | |
2178 heap_profiler->StopRecordingHeapAllocations(); | |
2179 } | |
OLD | NEW |