OLD | NEW |
1 // Copyright 2017 the V8 project authors. All rights reserved. | 1 // Copyright 2017 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/debug/debug-coverage.h" | 5 #include "src/debug/debug-coverage.h" |
6 | 6 |
7 #include "src/base/hashmap.h" | 7 #include "src/base/hashmap.h" |
8 #include "src/deoptimizer.h" | 8 #include "src/deoptimizer.h" |
9 #include "src/isolate.h" | 9 #include "src/isolate.h" |
10 #include "src/objects-inl.h" | 10 #include "src/objects-inl.h" |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
51 } | 51 } |
52 | 52 |
53 bool CompareSharedFunctionInfo(SharedFunctionInfo* a, SharedFunctionInfo* b) { | 53 bool CompareSharedFunctionInfo(SharedFunctionInfo* a, SharedFunctionInfo* b) { |
54 int a_start = StartPosition(a); | 54 int a_start = StartPosition(a); |
55 int b_start = StartPosition(b); | 55 int b_start = StartPosition(b); |
56 if (a_start == b_start) return a->end_position() > b->end_position(); | 56 if (a_start == b_start) return a->end_position() > b->end_position(); |
57 return a_start < b_start; | 57 return a_start < b_start; |
58 } | 58 } |
59 } // anonymous namespace | 59 } // anonymous namespace |
60 | 60 |
61 CoverageScript::CoverageScript(Isolate* isolate, Handle<Script> s, | |
62 int source_length) | |
63 : script(s), | |
64 toplevel(0, source_length, 1, isolate->factory()->empty_string()) {} | |
65 | |
66 Coverage* Coverage::Collect(Isolate* isolate) { | 61 Coverage* Coverage::Collect(Isolate* isolate) { |
67 SharedToCounterMap counter_map; | 62 SharedToCounterMap counter_map; |
68 | 63 |
69 // Feed invocation count into the counter map. | 64 // Feed invocation count into the counter map. |
70 if (isolate->IsCodeCoverageEnabled()) { | 65 if (isolate->IsCodeCoverageEnabled()) { |
71 // Feedback vectors are already listed to prevent losing them to GC. | 66 // Feedback vectors are already listed to prevent losing them to GC. |
72 Handle<ArrayList> list = | 67 Handle<ArrayList> list = |
73 Handle<ArrayList>::cast(isolate->factory()->code_coverage_list()); | 68 Handle<ArrayList>::cast(isolate->factory()->code_coverage_list()); |
74 for (int i = 0; i < list->Length(); i++) { | 69 for (int i = 0; i < list->Length(); i++) { |
75 FeedbackVector* vector = FeedbackVector::cast(list->Get(i)); | 70 FeedbackVector* vector = FeedbackVector::cast(list->Get(i)); |
(...skipping 18 matching lines...) Expand all Loading... |
94 | 89 |
95 // Iterate shared function infos of every script and build a mapping | 90 // Iterate shared function infos of every script and build a mapping |
96 // between source ranges and invocation counts. | 91 // between source ranges and invocation counts. |
97 Coverage* result = new Coverage(); | 92 Coverage* result = new Coverage(); |
98 Script::Iterator scripts(isolate); | 93 Script::Iterator scripts(isolate); |
99 while (Script* script = scripts.Next()) { | 94 while (Script* script = scripts.Next()) { |
100 // Dismiss non-user scripts. | 95 // Dismiss non-user scripts. |
101 if (script->type() != Script::TYPE_NORMAL) continue; | 96 if (script->type() != Script::TYPE_NORMAL) continue; |
102 | 97 |
103 // Create and add new script data. | 98 // Create and add new script data. |
104 int source_end = String::cast(script->source())->length(); | |
105 Handle<Script> script_handle(script, isolate); | 99 Handle<Script> script_handle(script, isolate); |
106 result->emplace_back(isolate, script_handle, source_end); | 100 result->emplace_back(isolate, script_handle); |
| 101 std::vector<CoverageFunction>* functions = &result->back().functions; |
107 | 102 |
108 std::vector<SharedFunctionInfo*> sorted; | 103 std::vector<SharedFunctionInfo*> sorted; |
| 104 bool has_toplevel = false; |
109 | 105 |
110 { | 106 { |
111 // Collect a list of shared function infos sorted by start position. | 107 // Sort functions by start position, from outer to inner functions. |
112 // Shared function infos are usually already sorted. Except for classes. | |
113 // If the start position is the same, sort from outer to inner function. | |
114 SharedFunctionInfo::ScriptIterator infos(script_handle); | 108 SharedFunctionInfo::ScriptIterator infos(script_handle); |
115 while (SharedFunctionInfo* info = infos.Next()) sorted.push_back(info); | 109 while (SharedFunctionInfo* info = infos.Next()) { |
| 110 has_toplevel |= info->is_toplevel(); |
| 111 sorted.push_back(info); |
| 112 } |
116 std::sort(sorted.begin(), sorted.end(), CompareSharedFunctionInfo); | 113 std::sort(sorted.begin(), sorted.end(), CompareSharedFunctionInfo); |
117 } | 114 } |
118 | 115 |
119 std::vector<CoverageRange*> stack; | 116 functions->reserve(sorted.size() + (has_toplevel ? 0 : 1)); |
120 stack.push_back(&result->back().toplevel); | 117 |
| 118 if (!has_toplevel) { |
| 119 // Add a replacement toplevel function if it does not exist. |
| 120 int source_end = String::cast(script->source())->length(); |
| 121 functions->emplace_back(0, source_end, 1u, |
| 122 isolate->factory()->empty_string()); |
| 123 } |
121 | 124 |
122 // Use sorted list to reconstruct function nesting. | 125 // Use sorted list to reconstruct function nesting. |
123 for (SharedFunctionInfo* info : sorted) { | 126 for (SharedFunctionInfo* info : sorted) { |
124 int start = StartPosition(info); | 127 int start = StartPosition(info); |
125 int end = info->end_position(); | 128 int end = info->end_position(); |
126 uint32_t count = counter_map.Get(info); | 129 uint32_t count = counter_map.Get(info); |
127 if (info->is_toplevel()) { | 130 Handle<String> name(info->DebugName(), isolate); |
128 // Top-level function is available. | 131 functions->emplace_back(start, end, count, name); |
129 DCHECK_EQ(1, stack.size()); | |
130 result->back().toplevel.start = start; | |
131 result->back().toplevel.end = end; | |
132 result->back().toplevel.count = count; | |
133 } else { | |
134 // The shared function infos are sorted by start. | |
135 DCHECK_LE(stack.back()->start, start); | |
136 // Drop the stack to the outer function. | |
137 while (start >= stack.back()->end) stack.pop_back(); | |
138 CoverageRange* outer = stack.back(); | |
139 // New nested function. | |
140 DCHECK_LE(end, outer->end); | |
141 Handle<String> name(info->DebugName(), isolate); | |
142 outer->inner.emplace_back(start, end, count, name); | |
143 stack.push_back(&outer->inner.back()); | |
144 } | |
145 } | 132 } |
146 } | 133 } |
147 return result; | 134 return result; |
148 } | 135 } |
149 | 136 |
150 void Coverage::TogglePrecise(Isolate* isolate, bool enable) { | 137 void Coverage::TogglePrecise(Isolate* isolate, bool enable) { |
151 if (enable) { | 138 if (enable) { |
152 HandleScope scope(isolate); | 139 HandleScope scope(isolate); |
153 // Remove all optimized function. Optimized and inlined functions do not | 140 // Remove all optimized function. Optimized and inlined functions do not |
154 // increment invocation count. | 141 // increment invocation count. |
(...skipping 16 matching lines...) Expand all Loading... |
171 ArrayList::New(isolate, static_cast<int>(vectors.size())); | 158 ArrayList::New(isolate, static_cast<int>(vectors.size())); |
172 for (const auto& vector : vectors) list = ArrayList::Add(list, vector); | 159 for (const auto& vector : vectors) list = ArrayList::Add(list, vector); |
173 isolate->SetCodeCoverageList(*list); | 160 isolate->SetCodeCoverageList(*list); |
174 } else { | 161 } else { |
175 isolate->SetCodeCoverageList(isolate->heap()->undefined_value()); | 162 isolate->SetCodeCoverageList(isolate->heap()->undefined_value()); |
176 } | 163 } |
177 } | 164 } |
178 | 165 |
179 } // namespace internal | 166 } // namespace internal |
180 } // namespace v8 | 167 } // namespace v8 |
OLD | NEW |