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 1962 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2014 HeapObjectsTracker tracker; | 2021 HeapObjectsTracker tracker; |
2015 CompileRun("var a = 1.2"); | 2022 CompileRun("var a = 1.2"); |
2016 CompileRun("var a = 1.2; var b = 1.0; var c = 1.0;"); | 2023 CompileRun("var a = 1.2; var b = 1.0; var c = 1.0;"); |
2017 CompileRun( | 2024 CompileRun( |
2018 "var a = [];" | 2025 "var a = [];" |
2019 "for (var i = 0; i < 5; ++i)" | 2026 "for (var i = 0; i < 5; ++i)" |
2020 " a[i] = i;\n" | 2027 " a[i] = i;\n" |
2021 "for (var i = 0; i < 3; ++i)" | 2028 "for (var i = 0; i < 3; ++i)" |
2022 " a.shift();\n"); | 2029 " a.shift();\n"); |
2023 } | 2030 } |
| 2031 |
| 2032 |
| 2033 static const char* record_allocation_traces_source = |
| 2034 "var topFunctions = [];\n" |
| 2035 "var global = this;\n" |
| 2036 "function generateFunctions(width, depth) {\n" |
| 2037 " var script = [];\n" |
| 2038 " for (var i = 0; i < width; i++) {\n" |
| 2039 " for (var j = 0; j < depth; j++) {\n" |
| 2040 " script.push('function f_' + i + '_' + j + '(x) {\\n');\n" |
| 2041 " script.push(' try {\\n');\n" |
| 2042 " if (j < depth-2) {\n" |
| 2043 " script.push(' return f_' + i + '_' + (j+1) + '(x+1);\\n');\n" |
| 2044 " } else if (j == depth - 2) {\n" |
| 2045 " script.push(' return new f_' + i + '_' + (depth - 1) + '();\\n');\n" |
| 2046 " } else if (j == depth - 1) {\n" |
| 2047 " script.push(' this.ts = Date.now();\\n');\n" |
| 2048 " }\n" |
| 2049 " script.push(' } catch (e) {}\\n');\n" |
| 2050 " script.push('}\\n');\n" |
| 2051 " \n" |
| 2052 " }\n" |
| 2053 " }\n" |
| 2054 " var script = script.join('');\n" |
| 2055 " // throw script;\n" |
| 2056 " global.eval(script);\n" |
| 2057 " for (var i = 0; i < width; i++) {\n" |
| 2058 " topFunctions.push(this['f_' + i + '_0']);\n" |
| 2059 " }\n" |
| 2060 "}\n" |
| 2061 "\n" |
| 2062 "var width = 3;\n" |
| 2063 "var depth = 4;\n" |
| 2064 "generateFunctions(width, depth);\n" |
| 2065 "var instances = [];\n" |
| 2066 "function start() {\n" |
| 2067 " for (var i = 0; i < width; i++) {\n" |
| 2068 " instances.push(topFunctions[i](0));\n" |
| 2069 " }\n" |
| 2070 "}\n" |
| 2071 "\n" |
| 2072 "for (var i = 0; i < 100; i++) start();\n"; |
| 2073 |
| 2074 |
| 2075 static i::HeapSnapshot* ToInternal(const v8::HeapSnapshot* snapshot) { |
| 2076 return const_cast<i::HeapSnapshot*>( |
| 2077 reinterpret_cast<const i::HeapSnapshot*>(snapshot)); |
| 2078 } |
| 2079 |
| 2080 |
| 2081 static AllocationTraceNode* FindNode( |
| 2082 AllocationTracker* tracker, const Vector<const char*>& names) { |
| 2083 AllocationTraceNode* node = tracker->allocation_traces()->root(); |
| 2084 for (int i = 0; node != NULL && i < names.length(); i++) { |
| 2085 const char* name = names[i]; |
| 2086 Vector<AllocationTraceNode*> children = node->children(); |
| 2087 node = NULL; |
| 2088 for (int j = 0; j < children.length(); j++) { |
| 2089 v8::SnapshotObjectId id = children[j]->function_id(); |
| 2090 AllocationTracker::FunctionInfo* info = tracker->GetFunctionInfo(id); |
| 2091 if (info && strcmp(info->name, name) == 0) { |
| 2092 node = children[j]; |
| 2093 break; |
| 2094 } |
| 2095 } |
| 2096 } |
| 2097 return node; |
| 2098 } |
| 2099 |
| 2100 |
| 2101 TEST(TrackHeapAllocations) { |
| 2102 v8::HandleScope scope(v8::Isolate::GetCurrent()); |
| 2103 LocalContext env; |
| 2104 |
| 2105 v8::HeapProfiler* heap_profiler = env->GetIsolate()->GetHeapProfiler(); |
| 2106 heap_profiler->StartRecordingHeapAllocations(); |
| 2107 |
| 2108 CompileRun(record_allocation_traces_source); |
| 2109 |
| 2110 const v8::HeapSnapshot* snapshot = heap_profiler->TakeHeapSnapshot( |
| 2111 v8::String::New("Test")); |
| 2112 i::HeapSnapshotsCollection* collection = ToInternal(snapshot)->collection(); |
| 2113 AllocationTracker* tracker = collection->allocation_tracker(); |
| 2114 CHECK_NE(NULL, tracker); |
| 2115 // Resolve all function locations. |
| 2116 tracker->PrepareForSerialization(); |
| 2117 // Print for better diagnostics in case of failure. |
| 2118 tracker->allocation_traces()->Print(tracker); |
| 2119 |
| 2120 const char* names[] = |
| 2121 { "(anonymous function)", "start", "f_0_0", "f_0_1", "f_0_2" }; |
| 2122 AllocationTraceNode* node = |
| 2123 FindNode(tracker, Vector<const char*>(names, ARRAY_SIZE(names))); |
| 2124 CHECK_NE(NULL, node); |
| 2125 CHECK_GE(node->allocation_count(), 100); |
| 2126 CHECK_GE(node->allocation_size(), 4 * node->allocation_count()); |
| 2127 heap_profiler->StopRecordingHeapAllocations(); |
| 2128 } |
OLD | NEW |