2017-02-09 19:00:49 +00:00
|
|
|
// Copyright 2017 the V8 project authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
#include "src/debug/debug-coverage.h"
|
|
|
|
|
|
|
|
#include "src/base/hashmap.h"
|
2017-02-10 08:21:03 +00:00
|
|
|
#include "src/deoptimizer.h"
|
|
|
|
#include "src/isolate.h"
|
2017-02-09 19:00:49 +00:00
|
|
|
#include "src/objects-inl.h"
|
|
|
|
#include "src/objects.h"
|
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
|
|
|
class SharedToCounterMap
|
|
|
|
: public base::TemplateHashMapImpl<SharedFunctionInfo*, uint32_t,
|
|
|
|
base::KeyEqualityMatcher<void*>,
|
|
|
|
base::DefaultAllocationPolicy> {
|
|
|
|
public:
|
|
|
|
typedef base::TemplateHashMapEntry<SharedFunctionInfo*, uint32_t> Entry;
|
|
|
|
inline void Add(SharedFunctionInfo* key, uint32_t count) {
|
|
|
|
Entry* entry = LookupOrInsert(key, Hash(key), []() { return 0; });
|
|
|
|
uint32_t old_count = entry->value;
|
|
|
|
if (UINT32_MAX - count < old_count) {
|
|
|
|
entry->value = UINT32_MAX;
|
|
|
|
} else {
|
|
|
|
entry->value = old_count + count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
inline uint32_t Get(SharedFunctionInfo* key) {
|
|
|
|
Entry* entry = Lookup(key, Hash(key));
|
|
|
|
if (entry == nullptr) return 0;
|
|
|
|
return entry->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static uint32_t Hash(SharedFunctionInfo* key) {
|
|
|
|
return static_cast<uint32_t>(reinterpret_cast<intptr_t>(key));
|
|
|
|
}
|
|
|
|
|
2017-02-14 06:47:58 +00:00
|
|
|
DisallowHeapAllocation no_gc;
|
2017-02-09 19:00:49 +00:00
|
|
|
};
|
|
|
|
|
2017-02-15 10:16:44 +00:00
|
|
|
namespace {
|
|
|
|
int StartPosition(SharedFunctionInfo* info) {
|
|
|
|
int start = info->function_token_position();
|
|
|
|
if (start == kNoSourcePosition) start = info->start_position();
|
|
|
|
return start;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CompareSharedFunctionInfo(SharedFunctionInfo* a, SharedFunctionInfo* b) {
|
|
|
|
int a_start = StartPosition(a);
|
|
|
|
int b_start = StartPosition(b);
|
|
|
|
if (a_start == b_start) return a->end_position() > b->end_position();
|
|
|
|
return a_start < b_start;
|
|
|
|
}
|
|
|
|
} // anonymous namespace
|
|
|
|
|
2017-02-22 10:21:57 +00:00
|
|
|
Coverage* Coverage::Collect(Isolate* isolate, bool reset_count) {
|
2017-02-09 19:00:49 +00:00
|
|
|
SharedToCounterMap counter_map;
|
2017-02-10 08:21:03 +00:00
|
|
|
|
2017-02-15 10:16:44 +00:00
|
|
|
// Feed invocation count into the counter map.
|
2017-02-10 08:21:03 +00:00
|
|
|
if (isolate->IsCodeCoverageEnabled()) {
|
|
|
|
// Feedback vectors are already listed to prevent losing them to GC.
|
|
|
|
Handle<ArrayList> list =
|
|
|
|
Handle<ArrayList>::cast(isolate->factory()->code_coverage_list());
|
|
|
|
for (int i = 0; i < list->Length(); i++) {
|
|
|
|
FeedbackVector* vector = FeedbackVector::cast(list->Get(i));
|
|
|
|
SharedFunctionInfo* shared = vector->shared_function_info();
|
|
|
|
DCHECK(shared->IsSubjectToDebugging());
|
|
|
|
uint32_t count = static_cast<uint32_t>(vector->invocation_count());
|
2017-02-22 10:21:57 +00:00
|
|
|
if (reset_count) vector->clear_invocation_count();
|
2017-02-10 08:21:03 +00:00
|
|
|
counter_map.Add(shared, count);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Iterate the heap to find all feedback vectors and accumulate the
|
|
|
|
// invocation counts into the map for each shared function info.
|
|
|
|
HeapIterator heap_iterator(isolate->heap());
|
|
|
|
while (HeapObject* current_obj = heap_iterator.next()) {
|
|
|
|
if (!current_obj->IsFeedbackVector()) continue;
|
|
|
|
FeedbackVector* vector = FeedbackVector::cast(current_obj);
|
|
|
|
SharedFunctionInfo* shared = vector->shared_function_info();
|
|
|
|
if (!shared->IsSubjectToDebugging()) continue;
|
|
|
|
uint32_t count = static_cast<uint32_t>(vector->invocation_count());
|
2017-02-22 10:21:57 +00:00
|
|
|
if (reset_count) vector->clear_invocation_count();
|
2017-02-10 08:21:03 +00:00
|
|
|
counter_map.Add(shared, count);
|
|
|
|
}
|
2017-02-09 19:00:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Iterate shared function infos of every script and build a mapping
|
|
|
|
// between source ranges and invocation counts.
|
2017-02-16 08:36:12 +00:00
|
|
|
Coverage* result = new Coverage();
|
2017-02-09 19:00:49 +00:00
|
|
|
Script::Iterator scripts(isolate);
|
|
|
|
while (Script* script = scripts.Next()) {
|
|
|
|
// Dismiss non-user scripts.
|
|
|
|
if (script->type() != Script::TYPE_NORMAL) continue;
|
2017-02-15 10:16:44 +00:00
|
|
|
|
|
|
|
// Create and add new script data.
|
2017-02-16 08:36:12 +00:00
|
|
|
Handle<Script> script_handle(script, isolate);
|
2017-02-22 10:21:57 +00:00
|
|
|
result->emplace_back(isolate, script_handle);
|
|
|
|
std::vector<CoverageFunction>* functions = &result->back().functions;
|
2017-02-15 10:16:44 +00:00
|
|
|
|
|
|
|
std::vector<SharedFunctionInfo*> sorted;
|
|
|
|
|
|
|
|
{
|
2017-02-22 10:21:57 +00:00
|
|
|
// Sort functions by start position, from outer to inner functions.
|
2017-02-16 08:36:12 +00:00
|
|
|
SharedFunctionInfo::ScriptIterator infos(script_handle);
|
2017-02-22 10:21:57 +00:00
|
|
|
while (SharedFunctionInfo* info = infos.Next()) {
|
|
|
|
sorted.push_back(info);
|
|
|
|
}
|
2017-02-15 10:16:44 +00:00
|
|
|
std::sort(sorted.begin(), sorted.end(), CompareSharedFunctionInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use sorted list to reconstruct function nesting.
|
|
|
|
for (SharedFunctionInfo* info : sorted) {
|
|
|
|
int start = StartPosition(info);
|
2017-02-09 19:00:49 +00:00
|
|
|
int end = info->end_position();
|
|
|
|
uint32_t count = counter_map.Get(info);
|
2017-02-22 10:21:57 +00:00
|
|
|
Handle<String> name(info->DebugName(), isolate);
|
|
|
|
functions->emplace_back(start, end, count, name);
|
2017-02-09 19:00:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-02-16 08:36:12 +00:00
|
|
|
void Coverage::TogglePrecise(Isolate* isolate, bool enable) {
|
|
|
|
if (enable) {
|
|
|
|
HandleScope scope(isolate);
|
|
|
|
// Remove all optimized function. Optimized and inlined functions do not
|
|
|
|
// increment invocation count.
|
|
|
|
Deoptimizer::DeoptimizeAll(isolate);
|
|
|
|
// Collect existing feedback vectors.
|
|
|
|
std::vector<Handle<FeedbackVector>> vectors;
|
|
|
|
{
|
|
|
|
HeapIterator heap_iterator(isolate->heap());
|
|
|
|
while (HeapObject* current_obj = heap_iterator.next()) {
|
|
|
|
if (!current_obj->IsFeedbackVector()) continue;
|
|
|
|
FeedbackVector* vector = FeedbackVector::cast(current_obj);
|
|
|
|
SharedFunctionInfo* shared = vector->shared_function_info();
|
|
|
|
if (!shared->IsSubjectToDebugging()) continue;
|
|
|
|
vector->clear_invocation_count();
|
|
|
|
vectors.emplace_back(vector, isolate);
|
|
|
|
}
|
2017-02-10 08:21:03 +00:00
|
|
|
}
|
2017-02-16 08:36:12 +00:00
|
|
|
// Add collected feedback vectors to the root list lest we lose them to GC.
|
|
|
|
Handle<ArrayList> list =
|
|
|
|
ArrayList::New(isolate, static_cast<int>(vectors.size()));
|
|
|
|
for (const auto& vector : vectors) list = ArrayList::Add(list, vector);
|
|
|
|
isolate->SetCodeCoverageList(*list);
|
|
|
|
} else {
|
|
|
|
isolate->SetCodeCoverageList(isolate->heap()->undefined_value());
|
2017-02-10 08:21:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-09 19:00:49 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|