Commit 9e158605 authored by Peter Marshall's avatar Peter Marshall Committed by Commit Bot

[cpu-profiler] De-duplicate CodeEntry objects for inline stacks

Within an inline stack we would have multiple copies of the exact same
CodeEntry object to represent an inline frame. We had one copy for every
time that the frame appeared in an inline stack. One CodeEntry can have
multiple inline stacks and each stack can have multiple inline frames.
In the common case, the stacks overlap and repeat frames.

This CL creates a single CodeEntry object to represent each inlined
function as an inline frame (for a given CodeEntry with inlinings). This
removes most of the duplication of inline CodeEntry objects. We still
have some duplication, e.g. if we inline bar() into foo() and foo2() but
they are not themselves inlined into anything, then we will have two
inline CodeEntry objects for bar(). Removing all duplication is harder
to achieve because the lifetime of the inlined frame CodeEntry is now no
longer tied to the inliner.

Get rid of the InlineEntry struct as it is now indentical to
CodeEntryAndLineNumber.

We store the list of canonical inline CodeEntry objects on the
CodeObject of the inlining function so that it can own the lifetimes of
inlined frames.

Also rename inline_locations_ to inline_stacks_ to be clearer.

Bug: v8:7719

Change-Id: Ied765b4cce7fd33f3290798331f1e6767cc42e8c
Reviewed-on: https://chromium-review.googlesource.com/c/1396086
Commit-Queue: Peter Marshall <petermarshall@chromium.org>
Reviewed-by: 's avatarTobias Tebbi <tebbi@chromium.org>
Reviewed-by: 's avatarAlexei Filippov <alph@chromium.org>
Cr-Commit-Position: refs/heads/master@{#58639}
parent 379095c1
......@@ -142,19 +142,24 @@ int CodeEntry::GetSourceLine(int pc_offset) const {
}
void CodeEntry::SetInlineStacks(
std::unordered_map<int, std::vector<InlineEntry>> inline_stacks) {
EnsureRareData()->inline_locations_ = std::move(inline_stacks);
std::unordered_set<std::unique_ptr<CodeEntry>, Hasher, Equals>
inline_entries,
std::unordered_map<int, std::vector<CodeEntryAndLineNumber>>
inline_stacks) {
EnsureRareData()->inline_entries_ = std::move(inline_entries);
rare_data_->inline_stacks_ = std::move(inline_stacks);
}
const std::vector<InlineEntry>* CodeEntry::GetInlineStack(int pc_offset) const {
const std::vector<CodeEntryAndLineNumber>* CodeEntry::GetInlineStack(
int pc_offset) const {
if (!line_info_) return nullptr;
int inlining_id = line_info_->GetInliningId(pc_offset);
if (inlining_id == SourcePosition::kNotInlined) return nullptr;
DCHECK(rare_data_);
auto it = rare_data_->inline_locations_.find(inlining_id);
return it != rare_data_->inline_locations_.end() ? &it->second : nullptr;
auto it = rare_data_->inline_stacks_.find(inlining_id);
return it != rare_data_->inline_stacks_.end() ? &it->second : nullptr;
}
void CodeEntry::set_deopt_info(
......@@ -220,14 +225,14 @@ void CodeEntry::print() const {
base::OS::Print(" - bailout_reason: %s\n", rare_data_->bailout_reason_);
base::OS::Print(" - deopt_id: %d\n", rare_data_->deopt_id_);
if (!rare_data_->inline_locations_.empty()) {
if (!rare_data_->inline_stacks_.empty()) {
base::OS::Print(" - inline stacks:\n");
for (auto it = rare_data_->inline_locations_.begin();
it != rare_data_->inline_locations_.end(); it++) {
for (auto it = rare_data_->inline_stacks_.begin();
it != rare_data_->inline_stacks_.end(); it++) {
base::OS::Print(" inlining_id: [%d]\n", it->first);
for (const auto& e : it->second) {
base::OS::Print(" %s --> %d\n", e.code_entry->name(),
e.call_line_number);
e.line_number);
}
}
} else {
......@@ -833,17 +838,12 @@ void ProfileGenerator::RecordTickSample(const TickSample& sample) {
int pc_offset =
static_cast<int>(stack_pos - entry->instruction_start());
// TODO(petermarshall): pc_offset can still be negative in some cases.
const std::vector<InlineEntry>* inline_stack =
const std::vector<CodeEntryAndLineNumber>* inline_stack =
entry->GetInlineStack(pc_offset);
if (inline_stack) {
int most_inlined_frame_line_number = entry->GetSourceLine(pc_offset);
std::transform(inline_stack->begin(), inline_stack->end(),
std::back_inserter(stack_trace),
[](const InlineEntry& inline_entry) {
return CodeEntryAndLineNumber{
inline_entry.code_entry.get(),
inline_entry.call_line_number};
});
stack_trace.insert(stack_trace.end(), inline_stack->begin(),
inline_stack->end());
// This is a bit of a messy hack. The line number for the most-inlined
// frame (the function at the end of the chain of function calls) has
// the wrong line number in inline_stack. The actual line number in
......
......@@ -53,7 +53,7 @@ class SourcePositionTable : public Malloced {
DISALLOW_COPY_AND_ASSIGN(SourcePositionTable);
};
struct InlineEntry;
struct CodeEntryAndLineNumber;
class CodeEntry {
public:
......@@ -109,9 +109,25 @@ class CodeEntry {
int GetSourceLine(int pc_offset) const;
struct Equals {
bool operator()(const std::unique_ptr<CodeEntry>& lhs,
const std::unique_ptr<CodeEntry>& rhs) const {
return lhs.get()->IsSameFunctionAs(rhs.get());
}
};
struct Hasher {
std::size_t operator()(const std::unique_ptr<CodeEntry>& e) const {
return e->GetHash();
}
};
void SetInlineStacks(
std::unordered_map<int, std::vector<InlineEntry>> inline_stacks);
const std::vector<InlineEntry>* GetInlineStack(int pc_offset) const;
std::unordered_set<std::unique_ptr<CodeEntry>, Hasher, Equals>
inline_entries,
std::unordered_map<int, std::vector<CodeEntryAndLineNumber>>
inline_stacks);
const std::vector<CodeEntryAndLineNumber>* GetInlineStack(
int pc_offset) const;
void set_instruction_start(Address start) { instruction_start_ = start; }
Address instruction_start() const { return instruction_start_; }
......@@ -148,7 +164,9 @@ class CodeEntry {
const char* deopt_reason_ = kNoDeoptReason;
const char* bailout_reason_ = kEmptyBailoutReason;
int deopt_id_ = kNoDeoptimizationId;
std::unordered_map<int, std::vector<InlineEntry>> inline_locations_;
std::unordered_map<int, std::vector<CodeEntryAndLineNumber>> inline_stacks_;
std::unordered_set<std::unique_ptr<CodeEntry>, Hasher, Equals>
inline_entries_;
std::vector<CpuProfileDeoptFrame> deopt_inlined_frames_;
};
......@@ -194,15 +212,6 @@ class CodeEntry {
DISALLOW_COPY_AND_ASSIGN(CodeEntry);
};
// Used to store information about inline call stacks - call_line_number is the
// line number within the function represented by code_entry. Inlining
// inherently happens at callsites, so this line number will always be the call
// to the next inline/noninline frame.
struct InlineEntry {
std::unique_ptr<CodeEntry> code_entry;
int call_line_number;
};
struct CodeEntryAndLineNumber {
CodeEntry* code_entry;
int line_number;
......
......@@ -75,6 +75,21 @@ void ProfilerListener::CodeCreateEvent(CodeEventListener::LogEventsAndTags tag,
DispatchCodeEvent(evt_rec);
}
namespace {
CodeEntry* GetOrInsertCachedEntry(
std::unordered_set<std::unique_ptr<CodeEntry>, CodeEntry::Hasher,
CodeEntry::Equals>* entries,
std::unique_ptr<CodeEntry> search_value) {
auto it = entries->find(search_value);
if (it != entries->end()) return it->get();
CodeEntry* ret = search_value.get();
entries->insert(std::move(search_value));
return ret;
}
} // namespace
void ProfilerListener::CodeCreateEvent(CodeEventListener::LogEventsAndTags tag,
AbstractCode abstract_code,
SharedFunctionInfo shared,
......@@ -83,7 +98,10 @@ void ProfilerListener::CodeCreateEvent(CodeEventListener::LogEventsAndTags tag,
CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
rec->instruction_start = abstract_code->InstructionStart();
std::unique_ptr<SourcePositionTable> line_table;
std::unordered_map<int, std::vector<InlineEntry>> inline_stacks;
std::unordered_map<int, std::vector<CodeEntryAndLineNumber>> inline_stacks;
std::unordered_set<std::unique_ptr<CodeEntry>, CodeEntry::Hasher,
CodeEntry::Equals>
cached_inline_entries;
if (shared->script()->IsScript()) {
Script script = Script::cast(shared->script());
line_table.reset(new SourcePositionTable());
......@@ -108,7 +126,7 @@ void ProfilerListener::CodeCreateEvent(CodeEventListener::LogEventsAndTags tag,
it.source_position().InliningStack(handle(code, isolate_));
DCHECK(!stack.empty());
std::vector<InlineEntry> inline_stack;
std::vector<CodeEntryAndLineNumber> inline_stack;
for (SourcePositionInfo& pos_info : stack) {
if (pos_info.position.ScriptOffset() == kNoSourcePosition) continue;
if (pos_info.script.is_null()) continue;
......@@ -135,8 +153,14 @@ void ProfilerListener::CodeCreateEvent(CodeEventListener::LogEventsAndTags tag,
start_pos_info.line + 1, start_pos_info.column + 1, nullptr,
code->InstructionStart());
inline_entry->FillFunctionInfo(*pos_info.shared);
// Create a canonical CodeEntry for each inlined frame and then re-use
// them for subsequent inline stacks to avoid a lot of duplication.
CodeEntry* cached_entry = GetOrInsertCachedEntry(
&cached_inline_entries, std::move(inline_entry));
inline_stack.push_back(
InlineEntry{std::move(inline_entry), line_number});
CodeEntryAndLineNumber{cached_entry, line_number});
}
DCHECK(!inline_stack.empty());
inline_stacks.emplace(inlining_id, std::move(inline_stack));
......@@ -148,7 +172,8 @@ void ProfilerListener::CodeCreateEvent(CodeEventListener::LogEventsAndTags tag,
GetName(InferScriptName(script_name, shared)), line, column,
std::move(line_table), abstract_code->InstructionStart());
if (!inline_stacks.empty()) {
rec->entry->SetInlineStacks(std::move(inline_stacks));
rec->entry->SetInlineStacks(std::move(cached_inline_entries),
std::move(inline_stacks));
}
rec->entry->FillFunctionInfo(shared);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment