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/frames-inl.h" | 9 #include "src/frames-inl.h" |
10 #include "src/isolate.h" | 10 #include "src/isolate.h" |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
56 int b_start = StartPosition(b); | 56 int b_start = StartPosition(b); |
57 if (a_start == b_start) return a->end_position() > b->end_position(); | 57 if (a_start == b_start) return a->end_position() > b->end_position(); |
58 return a_start < b_start; | 58 return a_start < b_start; |
59 } | 59 } |
60 } // anonymous namespace | 60 } // anonymous namespace |
61 | 61 |
62 Coverage* Coverage::Collect(Isolate* isolate, bool reset_count) { | 62 Coverage* Coverage::Collect(Isolate* isolate, bool reset_count) { |
63 SharedToCounterMap counter_map; | 63 SharedToCounterMap counter_map; |
64 | 64 |
65 // Feed invocation count into the counter map. | 65 // Feed invocation count into the counter map. |
66 if (isolate->IsCodeCoverageEnabled()) { | 66 switch (isolate->code_coverage_mode()) { |
67 // Feedback vectors are already listed to prevent losing them to GC. | 67 case debug::Coverage::kPreciseCount: { |
68 Handle<ArrayList> list = | 68 // Feedback vectors are already listed to prevent losing them to GC. |
69 Handle<ArrayList>::cast(isolate->factory()->code_coverage_list()); | 69 Handle<ArrayList> list = |
70 for (int i = 0; i < list->Length(); i++) { | 70 Handle<ArrayList>::cast(isolate->factory()->code_coverage_list()); |
71 FeedbackVector* vector = FeedbackVector::cast(list->Get(i)); | 71 for (int i = 0; i < list->Length(); i++) { |
72 SharedFunctionInfo* shared = vector->shared_function_info(); | 72 FeedbackVector* vector = FeedbackVector::cast(list->Get(i)); |
73 DCHECK(shared->IsSubjectToDebugging()); | 73 SharedFunctionInfo* shared = vector->shared_function_info(); |
74 uint32_t count = static_cast<uint32_t>(vector->invocation_count()); | 74 DCHECK(shared->IsSubjectToDebugging()); |
75 if (reset_count) vector->clear_invocation_count(); | 75 uint32_t count = static_cast<uint32_t>(vector->invocation_count()); |
76 counter_map.Add(shared, count); | 76 if (reset_count) vector->clear_invocation_count(); |
| 77 counter_map.Add(shared, count); |
| 78 } |
| 79 break; |
77 } | 80 } |
78 } else { | 81 case debug::Coverage::kBestEffort: { |
79 // Iterate the heap to find all feedback vectors and accumulate the | 82 // Iterate the heap to find all feedback vectors and accumulate the |
80 // invocation counts into the map for each shared function info. | 83 // invocation counts into the map for each shared function info. |
81 HeapIterator heap_iterator(isolate->heap()); | 84 HeapIterator heap_iterator(isolate->heap()); |
82 while (HeapObject* current_obj = heap_iterator.next()) { | 85 while (HeapObject* current_obj = heap_iterator.next()) { |
83 if (!current_obj->IsFeedbackVector()) continue; | 86 if (!current_obj->IsFeedbackVector()) continue; |
84 FeedbackVector* vector = FeedbackVector::cast(current_obj); | 87 FeedbackVector* vector = FeedbackVector::cast(current_obj); |
85 SharedFunctionInfo* shared = vector->shared_function_info(); | 88 SharedFunctionInfo* shared = vector->shared_function_info(); |
86 if (!shared->IsSubjectToDebugging()) continue; | 89 if (!shared->IsSubjectToDebugging()) continue; |
87 uint32_t count = static_cast<uint32_t>(vector->invocation_count()); | 90 uint32_t count = static_cast<uint32_t>(vector->invocation_count()); |
88 if (reset_count) vector->clear_invocation_count(); | 91 if (reset_count) vector->clear_invocation_count(); |
89 counter_map.Add(shared, count); | 92 counter_map.Add(shared, count); |
| 93 } |
| 94 break; |
90 } | 95 } |
91 } | 96 } |
92 | 97 |
93 // Iterate shared function infos of every script and build a mapping | 98 // Iterate shared function infos of every script and build a mapping |
94 // between source ranges and invocation counts. | 99 // between source ranges and invocation counts. |
95 Coverage* result = new Coverage(); | 100 Coverage* result = new Coverage(); |
96 Script::Iterator scripts(isolate); | 101 Script::Iterator scripts(isolate); |
97 while (Script* script = scripts.Next()) { | 102 while (Script* script = scripts.Next()) { |
98 if (!script->IsUserJavaScript()) continue; | 103 if (!script->IsUserJavaScript()) continue; |
99 | 104 |
(...skipping 18 matching lines...) Expand all Loading... |
118 int start = StartPosition(info); | 123 int start = StartPosition(info); |
119 int end = info->end_position(); | 124 int end = info->end_position(); |
120 uint32_t count = counter_map.Get(info); | 125 uint32_t count = counter_map.Get(info); |
121 Handle<String> name(info->DebugName(), isolate); | 126 Handle<String> name(info->DebugName(), isolate); |
122 functions->emplace_back(start, end, count, name); | 127 functions->emplace_back(start, end, count, name); |
123 } | 128 } |
124 } | 129 } |
125 return result; | 130 return result; |
126 } | 131 } |
127 | 132 |
128 void Coverage::TogglePrecise(Isolate* isolate, bool enable) { | 133 void Coverage::SelectMode(Isolate* isolate, debug::Coverage::Mode mode) { |
129 if (enable) { | 134 switch (mode) { |
130 HandleScope scope(isolate); | 135 case debug::Coverage::kBestEffort: |
131 // Remove all optimized function. Optimized and inlined functions do not | 136 isolate->SetCodeCoverageList(isolate->heap()->undefined_value()); |
132 // increment invocation count. | 137 break; |
133 Deoptimizer::DeoptimizeAll(isolate); | 138 case debug::Coverage::kPreciseCount: { |
134 // Collect existing feedback vectors. | 139 HandleScope scope(isolate); |
135 std::vector<Handle<FeedbackVector>> vectors; | 140 // Remove all optimized function. Optimized and inlined functions do not |
136 { | 141 // increment invocation count. |
137 HeapIterator heap_iterator(isolate->heap()); | 142 Deoptimizer::DeoptimizeAll(isolate); |
138 while (HeapObject* current_obj = heap_iterator.next()) { | 143 // Collect existing feedback vectors. |
139 if (!current_obj->IsFeedbackVector()) continue; | 144 std::vector<Handle<FeedbackVector>> vectors; |
140 FeedbackVector* vector = FeedbackVector::cast(current_obj); | 145 { |
141 SharedFunctionInfo* shared = vector->shared_function_info(); | 146 HeapIterator heap_iterator(isolate->heap()); |
142 if (!shared->IsSubjectToDebugging()) continue; | 147 while (HeapObject* current_obj = heap_iterator.next()) { |
143 vectors.emplace_back(vector, isolate); | 148 if (!current_obj->IsFeedbackVector()) continue; |
| 149 FeedbackVector* vector = FeedbackVector::cast(current_obj); |
| 150 SharedFunctionInfo* shared = vector->shared_function_info(); |
| 151 if (!shared->IsSubjectToDebugging()) continue; |
| 152 vectors.emplace_back(vector, isolate); |
| 153 } |
144 } | 154 } |
| 155 // Add collected feedback vectors to the root list lest we lose them to |
| 156 // GC. |
| 157 Handle<ArrayList> list = |
| 158 ArrayList::New(isolate, static_cast<int>(vectors.size())); |
| 159 for (const auto& vector : vectors) list = ArrayList::Add(list, vector); |
| 160 isolate->SetCodeCoverageList(*list); |
| 161 break; |
145 } | 162 } |
146 // Add collected feedback vectors to the root list lest we lose them to GC. | |
147 Handle<ArrayList> list = | |
148 ArrayList::New(isolate, static_cast<int>(vectors.size())); | |
149 for (const auto& vector : vectors) list = ArrayList::Add(list, vector); | |
150 isolate->SetCodeCoverageList(*list); | |
151 } else { | |
152 isolate->SetCodeCoverageList(isolate->heap()->undefined_value()); | |
153 } | 163 } |
| 164 isolate->set_code_coverage_mode(mode); |
154 } | 165 } |
155 | 166 |
156 } // namespace internal | 167 } // namespace internal |
157 } // namespace v8 | 168 } // namespace v8 |
OLD | NEW |