Skip to content

Commit

Permalink
deps: V8: backport 14 CPU profiler commits from upstream
Browse files Browse the repository at this point in the history
[cpu-profiler] Fix bugs and add tests for JITLineInfoTable
https://chromium.googlesource.com/v8/v8/+/4feb5ce7fd5ef8c933f3f5dff2eca1173f85c1e9

[cpu-profiler] Fix incorrect line number calculation.
https://chromium.googlesource.com/v8/v8/+/ddb2856f39632f9e9f623d3cdb4600e636172031

[cpu-profiler] Use std::unordered_map for hashmaps.
https://chromium.googlesource.com/v8/v8/+/35985ce6abc80b85264fe3b87b246fed5f1806e6

[cpu-profiler] Do not store CodeEntries between profiling sessions.
https://chromium.googlesource.com/v8/v8.git/+/8ec48b2117b8092c4956f1ee11a0c85bec3ba1f8

[cpu-profiler] Remove name_prefix field from CodeEntry
https://chromium.googlesource.com/v8/v8.git/+/6f72af25fe43218b60c68129073ddcddb631566e

[cpu-profiler] Extract rare used fields of CodeEntry to an optional object.
https://chromium.googlesource.com/v8/v8.git/+/fcc1ebb55aab38013855834f556f6e874e0eb8b3

[profiler] Refactoring: decouple StringsStorage from Heap object.
https://chromium.googlesource.com/v8/v8/+/a31320f59c911a277566d6c2fa0b0f2ac83e0748

[cpu-profiler] Add a HandleScope to limit memory consumption.
https://chromium.googlesource.com/v8/v8.git/+/3e9f8a4f635e2d946651d6a4df81378266f32dc9

[cpu-profiler] Lazily create CPU profiler.
https://chromium.googlesource.com/v8/v8/+/1426ea1d6d45be0b4d9476bdb5bf3f27cfe578a0

[cpu-profiler] turn several std::map's into unordered_map's.
https://chromium.googlesource.com/v8/v8/+/3ed5dfb8a3cbc7aa0017bd01c2fdd6227485b8ad

[cpu-profiler] Eagerly delete not used CodeEntry'es
https://chromium.googlesource.com/v8/v8.git/+/c6c28f7a412a88df12055e953630a9e93cc64d49

[cpu-profiler] Move bailout reason into rare_info struct
https://chromium.googlesource.com/v8/v8.git/+/29ea4d1ef5360e71c61ecf8db6a5a0a0c3391fd1

[cpu-profiler] Save space in the SourcePositionTable by using a vector.
https://chromium.googlesource.com/v8/v8.git/+/1cb19f0e0a93adbac8c11bc906f951bd8098722d

[cpu-profiler] Only store deopt inline frames for functions that need it
https://chromium.googlesource.com/v8/v8.git/+/0bfcbdd4726920755e51dab28c18ab93e050819b

PR-URL: #21558
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Myles Borins <myles.borins@gmail.com>
  • Loading branch information
psmarshall authored and MylesBorins committed Jul 9, 2018
1 parent f1d9f16 commit b57ad02
Show file tree
Hide file tree
Showing 19 changed files with 446 additions and 421 deletions.
2 changes: 1 addition & 1 deletion deps/v8/include/v8-version.h
Expand Up @@ -11,7 +11,7 @@
#define V8_MAJOR_VERSION 6
#define V8_MINOR_VERSION 2
#define V8_BUILD_NUMBER 414
#define V8_PATCH_LEVEL 60
#define V8_PATCH_LEVEL 61

// Use 1 for candidates and 0 otherwise.
// (Boolean macro values are not supported by all preprocessors.)
Expand Down
12 changes: 2 additions & 10 deletions deps/v8/src/api.cc
Expand Up @@ -8419,7 +8419,7 @@ HeapProfiler* Isolate::GetHeapProfiler() {

CpuProfiler* Isolate::GetCpuProfiler() {
i::CpuProfiler* cpu_profiler =
reinterpret_cast<i::Isolate*>(this)->cpu_profiler();
reinterpret_cast<i::Isolate*>(this)->EnsureCpuProfiler();
return reinterpret_cast<CpuProfiler*>(cpu_profiler);
}

Expand Down Expand Up @@ -10139,15 +10139,7 @@ Local<String> CpuProfileNode::GetFunctionName() const {
const i::CodeEntry* entry = node->entry();
i::Handle<i::String> name =
isolate->factory()->InternalizeUtf8String(entry->name());
if (!entry->has_name_prefix()) {
return ToApiHandle<String>(name);
} else {
// We do not expect this to fail. Change this if it does.
i::Handle<i::String> cons = isolate->factory()->NewConsString(
isolate->factory()->InternalizeUtf8String(entry->name_prefix()),
name).ToHandleChecked();
return ToApiHandle<String>(cons);
}
return ToApiHandle<String>(name);
}

int debug::Coverage::BlockData::StartOffset() const { return block_->start; }
Expand Down
8 changes: 7 additions & 1 deletion deps/v8/src/isolate.cc
Expand Up @@ -2689,7 +2689,6 @@ bool Isolate::Init(StartupDeserializer* des) {
call_descriptor_data_ =
new CallInterfaceDescriptorData[CallDescriptors::NUMBER_OF_DESCRIPTORS];
access_compiler_data_ = new AccessCompilerData();
cpu_profiler_ = new CpuProfiler(this);
heap_profiler_ = new HeapProfiler(heap());
interpreter_ = new interpreter::Interpreter(this);
compiler_dispatcher_ =
Expand Down Expand Up @@ -3686,6 +3685,13 @@ void Isolate::PrintWithTimestamp(const char* format, ...) {
va_end(arguments);
}

CpuProfiler* Isolate::EnsureCpuProfiler() {
if (!cpu_profiler_) {
cpu_profiler_ = new CpuProfiler(this);
}
return cpu_profiler_;
}

bool StackLimitCheck::JsHasOverflowed(uintptr_t gap) const {
StackGuard* stack_guard = isolate_->stack_guard();
#ifdef USE_SIMULATOR
Expand Down
2 changes: 1 addition & 1 deletion deps/v8/src/isolate.h
Expand Up @@ -1449,7 +1449,7 @@ class Isolate {

// TODO(alph): Remove along with the deprecated GetCpuProfiler().
friend v8::CpuProfiler* v8::Isolate::GetCpuProfiler();
CpuProfiler* cpu_profiler() const { return cpu_profiler_; }
CpuProfiler* EnsureCpuProfiler();

base::Atomic32 id_;
EntryStackItem* entry_stack_;
Expand Down
21 changes: 0 additions & 21 deletions deps/v8/src/log.cc
Expand Up @@ -22,7 +22,6 @@
#include "src/log-utils.h"
#include "src/macro-assembler.h"
#include "src/perf-jit.h"
#include "src/profiler/profiler-listener.h"
#include "src/profiler/tick-sample.h"
#include "src/runtime-profiler.h"
#include "src/source-position-table.h"
Expand Down Expand Up @@ -739,7 +738,6 @@ Logger::Logger(Isolate* isolate)
perf_jit_logger_(NULL),
ll_logger_(NULL),
jit_logger_(NULL),
listeners_(5),
is_initialized_(false) {}

Logger::~Logger() {
Expand Down Expand Up @@ -1876,8 +1874,6 @@ bool Logger::SetUp(Isolate* isolate) {
profiler_->Engage();
}

profiler_listener_.reset();

if (is_logging_) {
addCodeEventListener(this);
}
Expand Down Expand Up @@ -1905,19 +1901,6 @@ void Logger::SetCodeEventHandler(uint32_t options,
}
}

void Logger::SetUpProfilerListener() {
if (!is_initialized_) return;
if (profiler_listener_.get() == nullptr) {
profiler_listener_.reset(new ProfilerListener(isolate_));
}
addCodeEventListener(profiler_listener_.get());
}

void Logger::TearDownProfilerListener() {
if (profiler_listener_->HasObservers()) return;
removeCodeEventListener(profiler_listener_.get());
}

sampler::Sampler* Logger::sampler() {
return ticker_;
}
Expand Down Expand Up @@ -1961,10 +1944,6 @@ FILE* Logger::TearDown() {
jit_logger_ = NULL;
}

if (profiler_listener_.get() != nullptr) {
removeCodeEventListener(profiler_listener_.get());
}

return log_->Close();
}

Expand Down
11 changes: 0 additions & 11 deletions deps/v8/src/log.h
Expand Up @@ -74,7 +74,6 @@ class LowLevelLogger;
class PerfBasicLogger;
class PerfJitLogger;
class Profiler;
class ProfilerListener;
class RuntimeCallTimer;
class Ticker;

Expand Down Expand Up @@ -102,16 +101,8 @@ class Logger : public CodeEventListener {
void SetCodeEventHandler(uint32_t options,
JitCodeEventHandler event_handler);

// Sets up ProfilerListener.
void SetUpProfilerListener();

// Tear down ProfilerListener if it has no observers.
void TearDownProfilerListener();

sampler::Sampler* sampler();

ProfilerListener* profiler_listener() { return profiler_listener_.get(); }

// Frees resources acquired in SetUp.
// When a temporary file is used for the log, returns its stream descriptor,
// leaving the file open.
Expand Down Expand Up @@ -316,8 +307,6 @@ class Logger : public CodeEventListener {
PerfJitLogger* perf_jit_logger_;
LowLevelLogger* ll_logger_;
JitLogger* jit_logger_;
std::unique_ptr<ProfilerListener> profiler_listener_;
List<CodeEventListener*> listeners_;
std::set<int> logged_source_code_;
uint32_t next_source_info_id_ = 0;

Expand Down
6 changes: 5 additions & 1 deletion deps/v8/src/profiler/cpu-profiler-inl.h
Expand Up @@ -35,7 +35,11 @@ void CodeDisableOptEventRecord::UpdateCodeMap(CodeMap* code_map) {

void CodeDeoptEventRecord::UpdateCodeMap(CodeMap* code_map) {
CodeEntry* entry = code_map->FindEntry(start);
if (entry != NULL) entry->set_deopt_info(deopt_reason, deopt_id);
if (entry == nullptr) return;
std::vector<CpuProfileDeoptFrame> frames_vector(
deopt_frames, deopt_frames + deopt_frame_count);
entry->set_deopt_info(deopt_reason, deopt_id, std::move(frames_vector));
delete[] deopt_frames;
}


Expand Down
36 changes: 21 additions & 15 deletions deps/v8/src/profiler/cpu-profiler.cc
Expand Up @@ -4,6 +4,12 @@

#include "src/profiler/cpu-profiler.h"

#include <unordered_map>
#include <utility>

#include "src/base/lazy-instance.h"
#include "src/base/platform/mutex.h"
#include "src/base/template-utils.h"
#include "src/debug/debug.h"
#include "src/deoptimizer.h"
#include "src/frames-inl.h"
Expand Down Expand Up @@ -275,20 +281,19 @@ void CpuProfiler::set_sampling_interval(base::TimeDelta value) {
void CpuProfiler::ResetProfiles() {
profiles_.reset(new CpuProfilesCollection(isolate_));
profiles_->set_cpu_profiler(this);
profiler_listener_.reset();
generator_.reset();
}

void CpuProfiler::CreateEntriesForRuntimeCallStats() {
static_entries_.clear();
RuntimeCallStats* rcs = isolate_->counters()->runtime_call_stats();
CodeMap* code_map = generator_->code_map();
for (int i = 0; i < RuntimeCallStats::counters_count; ++i) {
RuntimeCallCounter* counter = &(rcs->*(RuntimeCallStats::counters[i]));
DCHECK(counter->name());
std::unique_ptr<CodeEntry> entry(
new CodeEntry(CodeEventListener::FUNCTION_TAG, counter->name(),
CodeEntry::kEmptyNamePrefix, "native V8Runtime"));
code_map->AddCode(reinterpret_cast<Address>(counter), entry.get(), 1);
static_entries_.push_back(std::move(entry));
auto entry = new CodeEntry(CodeEventListener::FUNCTION_TAG, counter->name(),
"native V8Runtime");
code_map->AddCode(reinterpret_cast<Address>(counter), entry, 1);
}
}

Expand Down Expand Up @@ -321,13 +326,17 @@ void CpuProfiler::StartProcessorIfNotStarted() {
// Disable logging when using the new implementation.
saved_is_logging_ = logger->is_logging_;
logger->is_logging_ = false;
generator_.reset(new ProfileGenerator(profiles_.get()));
if (!generator_) {
generator_.reset(new ProfileGenerator(profiles_.get()));
CreateEntriesForRuntimeCallStats();
}
processor_.reset(new ProfilerEventsProcessor(isolate_, generator_.get(),
sampling_interval_));
CreateEntriesForRuntimeCallStats();
logger->SetUpProfilerListener();
ProfilerListener* profiler_listener = logger->profiler_listener();
profiler_listener->AddObserver(this);
if (!profiler_listener_) {
profiler_listener_.reset(new ProfilerListener(isolate_, this));
}
logger->addCodeEventListener(profiler_listener_.get());

is_profiling_ = true;
isolate_->set_is_profiling(true);
// Enumerate stuff we already have in the heap.
Expand Down Expand Up @@ -362,12 +371,9 @@ void CpuProfiler::StopProcessor() {
Logger* logger = isolate_->logger();
is_profiling_ = false;
isolate_->set_is_profiling(false);
ProfilerListener* profiler_listener = logger->profiler_listener();
profiler_listener->RemoveObserver(this);
logger->removeCodeEventListener(profiler_listener_.get());
processor_->StopSynchronously();
logger->TearDownProfilerListener();
processor_.reset();
generator_.reset();
logger->is_logging_ = saved_is_logging_;
}

Expand Down
9 changes: 7 additions & 2 deletions deps/v8/src/profiler/cpu-profiler.h
Expand Up @@ -86,6 +86,8 @@ class CodeDeoptEventRecord : public CodeEventRecord {
int deopt_id;
void* pc;
int fp_to_sp_delta;
CpuProfileDeoptFrame* deopt_frames;
int deopt_frame_count;

INLINE(void UpdateCodeMap(CodeMap* code_map));
};
Expand Down Expand Up @@ -214,6 +216,10 @@ class CpuProfiler : public CodeEventObserver {
ProfilerEventsProcessor* processor() const { return processor_.get(); }
Isolate* isolate() const { return isolate_; }

ProfilerListener* profiler_listener_for_test() {
return profiler_listener_.get();
}

private:
void StartProcessorIfNotStarted();
void StopProcessorIfLastProfile(const char* title);
Expand All @@ -227,7 +233,7 @@ class CpuProfiler : public CodeEventObserver {
std::unique_ptr<CpuProfilesCollection> profiles_;
std::unique_ptr<ProfileGenerator> generator_;
std::unique_ptr<ProfilerEventsProcessor> processor_;
std::vector<std::unique_ptr<CodeEntry>> static_entries_;
std::unique_ptr<ProfilerListener> profiler_listener_;
bool saved_is_logging_;
bool is_profiling_;

Expand All @@ -237,5 +243,4 @@ class CpuProfiler : public CodeEventObserver {
} // namespace internal
} // namespace v8


#endif // V8_PROFILER_CPU_PROFILER_H_
4 changes: 2 additions & 2 deletions deps/v8/src/profiler/heap-profiler.cc
Expand Up @@ -16,7 +16,7 @@ namespace internal {

HeapProfiler::HeapProfiler(Heap* heap)
: ids_(new HeapObjectsMap(heap)),
names_(new StringsStorage(heap)),
names_(new StringsStorage(heap->HashSeed())),
is_tracking_object_moves_(false),
get_retainer_infos_callback_(nullptr) {}

Expand All @@ -34,7 +34,7 @@ HeapProfiler::~HeapProfiler() {
void HeapProfiler::DeleteAllSnapshots() {
snapshots_.Iterate(DeleteHeapSnapshot);
snapshots_.Clear();
names_.reset(new StringsStorage(heap()));
names_.reset(new StringsStorage(heap()->HashSeed()));
}


Expand Down
23 changes: 12 additions & 11 deletions deps/v8/src/profiler/profile-generator-inl.h
Expand Up @@ -11,33 +11,34 @@ namespace v8 {
namespace internal {

CodeEntry::CodeEntry(CodeEventListener::LogEventsAndTags tag, const char* name,
const char* name_prefix, const char* resource_name,
int line_number, int column_number,
JITLineInfoTable* line_info, Address instruction_start)
const char* resource_name, int line_number,
int column_number,
std::unique_ptr<SourcePositionTable> line_info,
Address instruction_start)
: bit_field_(TagField::encode(tag) |
BuiltinIdField::encode(Builtins::builtin_count)),
name_prefix_(name_prefix),
name_(name),
resource_name_(resource_name),
line_number_(line_number),
column_number_(column_number),
script_id_(v8::UnboundScript::kNoScriptId),
position_(0),
bailout_reason_(kEmptyBailoutReason),
deopt_reason_(kNoDeoptReason),
deopt_id_(kNoDeoptimizationId),
line_info_(line_info),
line_info_(std::move(line_info)),
instruction_start_(instruction_start) {}

inline CodeEntry* ProfileGenerator::FindEntry(Address address) {
CodeEntry* entry = code_map_.FindEntry(address);
if (entry) entry->mark_used();
return entry;
}

ProfileNode::ProfileNode(ProfileTree* tree, CodeEntry* entry,
ProfileNode* parent)
: tree_(tree),
entry_(entry),
self_ticks_(0),
children_(CodeEntriesMatch),
parent_(parent),
id_(tree->next_node_id()),
line_ticks_(LineTickMatch) {
id_(tree->next_node_id()) {
tree_->EnqueueNode(this);
}

Expand Down

0 comments on commit b57ad02

Please sign in to comment.