Commit b8372411 authored by alph's avatar alph Committed by Commit bot

Move SimulatorHelper into V8 out of profiler clients.

Clients should not know about the simulator.

BUG=v8:4789

Review-Url: https://codereview.chromium.org/2128613004
Cr-Commit-Position: refs/heads/master@{#37617}
parent f2ce4fe6
...@@ -7656,20 +7656,13 @@ bool Isolate::GetHeapCodeAndMetadataStatistics( ...@@ -7656,20 +7656,13 @@ bool Isolate::GetHeapCodeAndMetadataStatistics(
void Isolate::GetStackSample(const RegisterState& state, void** frames, void Isolate::GetStackSample(const RegisterState& state, void** frames,
size_t frames_limit, SampleInfo* sample_info) { size_t frames_limit, SampleInfo* sample_info) {
#if defined(USE_SIMULATOR) if (TickSample::GetStackSample(this, state, TickSample::kSkipCEntryFrame,
RegisterState regs; frames, frames_limit, sample_info)) {
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(this);
if (!i::SimulatorHelper::FillRegisters(isolate, &regs)) {
sample_info->frames_count = 0;
sample_info->vm_state = OTHER;
sample_info->external_callback_entry = nullptr;
return; return;
} }
#else sample_info->frames_count = 0;
const RegisterState& regs = state; sample_info->vm_state = OTHER;
#endif sample_info->external_callback_entry = nullptr;
TickSample::GetStackSample(this, regs, TickSample::kSkipCEntryFrame, frames,
frames_limit, sample_info);
} }
size_t Isolate::NumberOfPhantomHandleResetsSinceLastCall() { size_t Isolate::NumberOfPhantomHandleResetsSinceLastCall() {
......
...@@ -619,10 +619,10 @@ class Profiler: public base::Thread { ...@@ -619,10 +619,10 @@ class Profiler: public base::Thread {
// //
class Ticker: public sampler::Sampler { class Ticker: public sampler::Sampler {
public: public:
Ticker(Isolate* isolate, int interval): Ticker(Isolate* isolate, int interval)
sampler::Sampler(reinterpret_cast<v8::Isolate*>(isolate)), : sampler::Sampler(reinterpret_cast<v8::Isolate*>(isolate)),
profiler_(NULL), profiler_(nullptr),
sampling_thread_(new SamplingThread(this, interval)) {} sampling_thread_(new SamplingThread(this, interval)) {}
~Ticker() { ~Ticker() {
if (IsActive()) Stop(); if (IsActive()) Stop();
...@@ -630,7 +630,7 @@ class Ticker: public sampler::Sampler { ...@@ -630,7 +630,7 @@ class Ticker: public sampler::Sampler {
} }
void SetProfiler(Profiler* profiler) { void SetProfiler(Profiler* profiler) {
DCHECK(profiler_ == NULL); DCHECK(profiler_ == nullptr);
profiler_ = profiler; profiler_ = profiler;
IncreaseProfilingDepth(); IncreaseProfilingDepth();
if (!IsActive()) Start(); if (!IsActive()) Start();
...@@ -638,7 +638,7 @@ class Ticker: public sampler::Sampler { ...@@ -638,7 +638,7 @@ class Ticker: public sampler::Sampler {
} }
void ClearProfiler() { void ClearProfiler() {
profiler_ = NULL; profiler_ = nullptr;
if (IsActive()) Stop(); if (IsActive()) Stop();
DecreaseProfilingDepth(); DecreaseProfilingDepth();
sampling_thread_->Join(); sampling_thread_->Join();
...@@ -646,15 +646,9 @@ class Ticker: public sampler::Sampler { ...@@ -646,15 +646,9 @@ class Ticker: public sampler::Sampler {
void SampleStack(const v8::RegisterState& state) override { void SampleStack(const v8::RegisterState& state) override {
if (!profiler_) return; if (!profiler_) return;
v8::Isolate* v8_isolate = isolate(); Isolate* isolate = reinterpret_cast<Isolate*>(this->isolate());
Isolate* i_isolate = reinterpret_cast<Isolate*>(v8_isolate);
#if defined(USE_SIMULATOR)
if (!SimulatorHelper::FillRegisters(i_isolate,
const_cast<v8::RegisterState*>(&state)))
return;
#endif
TickSample sample; TickSample sample;
sample.Init(i_isolate, state, TickSample::kIncludeCEntryFrame, true); sample.Init(isolate, state, TickSample::kIncludeCEntryFrame, true);
profiler_->Insert(&sample); profiler_->Insert(&sample);
} }
......
...@@ -23,18 +23,11 @@ class CpuSampler : public sampler::Sampler { ...@@ -23,18 +23,11 @@ class CpuSampler : public sampler::Sampler {
: sampler::Sampler(reinterpret_cast<v8::Isolate*>(isolate)), : sampler::Sampler(reinterpret_cast<v8::Isolate*>(isolate)),
processor_(processor) {} processor_(processor) {}
void SampleStack(const v8::RegisterState& state) override { void SampleStack(const v8::RegisterState& regs) override {
v8::Isolate* v8_isolate = isolate();
Isolate* i_isolate = reinterpret_cast<Isolate*>(v8_isolate);
#if defined(USE_SIMULATOR)
v8::RegisterState regs;
if (!SimulatorHelper::FillRegisters(i_isolate, &regs)) return;
#else
const v8::RegisterState& regs = state;
#endif
TickSample* sample = processor_->StartTickSample(); TickSample* sample = processor_->StartTickSample();
if (sample == NULL) return; if (sample == nullptr) return;
sample->Init(i_isolate, regs, TickSample::kIncludeCEntryFrame, true); Isolate* isolate = reinterpret_cast<Isolate*>(this->isolate());
sample->Init(isolate, regs, TickSample::kIncludeCEntryFrame, true);
if (is_counting_samples_ && !sample->timestamp.IsNull()) { if (is_counting_samples_ && !sample->timestamp.IsNull()) {
if (sample->state == JS) ++js_sample_count_; if (sample->state == JS) ++js_sample_count_;
if (sample->state == EXTERNAL) ++external_sample_count_; if (sample->state == EXTERNAL) ++external_sample_count_;
......
...@@ -11,7 +11,6 @@ ...@@ -11,7 +11,6 @@
#include "src/vm-state-inl.h" #include "src/vm-state-inl.h"
namespace v8 { namespace v8 {
namespace { namespace {
bool IsSamePage(i::byte* ptr1, i::byte* ptr2) { bool IsSamePage(i::byte* ptr1, i::byte* ptr2) {
...@@ -77,6 +76,73 @@ bool IsNoFrameRegion(i::Address address) { ...@@ -77,6 +76,73 @@ bool IsNoFrameRegion(i::Address address) {
} // namespace } // namespace
namespace internal {
namespace {
#if defined(USE_SIMULATOR)
class SimulatorHelper {
public:
// Returns true if register values were successfully retrieved
// from the simulator, otherwise returns false.
static bool FillRegisters(Isolate* isolate, v8::RegisterState* state);
};
bool SimulatorHelper::FillRegisters(Isolate* isolate,
v8::RegisterState* state) {
Simulator* simulator = isolate->thread_local_top()->simulator_;
// Check if there is active simulator.
if (simulator == NULL) return false;
#if V8_TARGET_ARCH_ARM
if (!simulator->has_bad_pc()) {
state->pc = reinterpret_cast<Address>(simulator->get_pc());
}
state->sp = reinterpret_cast<Address>(simulator->get_register(Simulator::sp));
state->fp =
reinterpret_cast<Address>(simulator->get_register(Simulator::r11));
#elif V8_TARGET_ARCH_ARM64
state->pc = reinterpret_cast<Address>(simulator->pc());
state->sp = reinterpret_cast<Address>(simulator->sp());
state->fp = reinterpret_cast<Address>(simulator->fp());
#elif V8_TARGET_ARCH_MIPS || V8_TARGET_ARCH_MIPS64
if (!simulator->has_bad_pc()) {
state->pc = reinterpret_cast<Address>(simulator->get_pc());
}
state->sp = reinterpret_cast<Address>(simulator->get_register(Simulator::sp));
state->fp = reinterpret_cast<Address>(simulator->get_register(Simulator::fp));
#elif V8_TARGET_ARCH_PPC
if (!simulator->has_bad_pc()) {
state->pc = reinterpret_cast<Address>(simulator->get_pc());
}
state->sp = reinterpret_cast<Address>(simulator->get_register(Simulator::sp));
state->fp = reinterpret_cast<Address>(simulator->get_register(Simulator::fp));
#elif V8_TARGET_ARCH_S390
if (!simulator->has_bad_pc()) {
state->pc = reinterpret_cast<Address>(simulator->get_pc());
}
state->sp = reinterpret_cast<Address>(simulator->get_register(Simulator::sp));
state->fp = reinterpret_cast<Address>(simulator->get_register(Simulator::fp));
#endif
if (state->sp == 0 || state->fp == 0) {
// It possible that the simulator is interrupted while it is updating
// the sp or fp register. ARM64 simulator does this in two steps:
// first setting it to zero and then setting it to the new value.
// Bailout if sp/fp doesn't contain the new value.
//
// FIXME: The above doesn't really solve the issue.
// If a 64-bit target is executed on a 32-bit host even the final
// write is non-atomic, so it might obtain a half of the result.
// Moreover as long as the register set code uses memcpy (as of now),
// it is not guaranteed to be atomic even when both host and target
// are of same bitness.
return false;
}
return true;
}
#endif // USE_SIMULATOR
} // namespace
} // namespace internal
// //
// StackTracer implementation // StackTracer implementation
// //
...@@ -85,35 +151,33 @@ DISABLE_ASAN void TickSample::Init(Isolate* v8_isolate, ...@@ -85,35 +151,33 @@ DISABLE_ASAN void TickSample::Init(Isolate* v8_isolate,
RecordCEntryFrame record_c_entry_frame, RecordCEntryFrame record_c_entry_frame,
bool update_stats) { bool update_stats) {
this->update_stats = update_stats; this->update_stats = update_stats;
SampleInfo info; SampleInfo info;
if (GetStackSample(v8_isolate, const_cast<RegisterState&>(regs), if (!GetStackSample(v8_isolate, regs, record_c_entry_frame, stack,
record_c_entry_frame, reinterpret_cast<void**>(&stack[0]), kMaxFramesCount, &info)) {
kMaxFramesCount, &info)) {
state = info.vm_state;
pc = regs.pc;
frames_count = static_cast<unsigned>(info.frames_count);
has_external_callback = info.external_callback_entry != nullptr;
if (has_external_callback) {
external_callback_entry = info.external_callback_entry;
} else if (frames_count) {
// sp register may point at an arbitrary place in memory, make
// sure MSAN doesn't complain about it.
MSAN_MEMORY_IS_INITIALIZED(regs.sp, sizeof(void*));
// Sample potential return address value for frameless invocation of
// stubs (we'll figure out later, if this value makes sense).
tos = i::Memory::Address_at(reinterpret_cast<i::Address>(regs.sp));
} else {
tos = nullptr;
}
} else {
// It is executing JS but failed to collect a stack trace. // It is executing JS but failed to collect a stack trace.
// Mark the sample as spoiled. // Mark the sample as spoiled.
pc = nullptr; pc = nullptr;
return;
}
state = info.vm_state;
pc = regs.pc;
frames_count = static_cast<unsigned>(info.frames_count);
has_external_callback = info.external_callback_entry != nullptr;
if (has_external_callback) {
external_callback_entry = info.external_callback_entry;
} else if (frames_count) {
// sp register may point at an arbitrary place in memory, make
// sure MSAN doesn't complain about it.
MSAN_MEMORY_IS_INITIALIZED(regs.sp, sizeof(void*));
// Sample potential return address value for frameless invocation of
// stubs (we'll figure out later, if this value makes sense).
tos = i::Memory::Address_at(reinterpret_cast<i::Address>(regs.sp));
} else {
tos = nullptr;
} }
} }
bool TickSample::GetStackSample(Isolate* v8_isolate, const RegisterState& regs, bool TickSample::GetStackSample(Isolate* v8_isolate, const RegisterState& state,
RecordCEntryFrame record_c_entry_frame, RecordCEntryFrame record_c_entry_frame,
void** frames, size_t frames_limit, void** frames, size_t frames_limit,
v8::SampleInfo* sample_info) { v8::SampleInfo* sample_info) {
...@@ -125,10 +189,17 @@ bool TickSample::GetStackSample(Isolate* v8_isolate, const RegisterState& regs, ...@@ -125,10 +189,17 @@ bool TickSample::GetStackSample(Isolate* v8_isolate, const RegisterState& regs,
i::Address js_entry_sp = isolate->js_entry_sp(); i::Address js_entry_sp = isolate->js_entry_sp();
if (js_entry_sp == nullptr) return true; // Not executing JS now. if (js_entry_sp == nullptr) return true; // Not executing JS now.
#if defined(USE_SIMULATOR)
v8::RegisterState regs;
if (!i::SimulatorHelper::FillRegisters(isolate, &regs)) return false;
#else
const v8::RegisterState& regs = state;
#endif
DCHECK(regs.sp); DCHECK(regs.sp);
if (regs.pc && IsNoFrameRegion(static_cast<i::Address>(regs.pc))) { if (regs.pc && IsNoFrameRegion(static_cast<i::Address>(regs.pc))) {
// Can't collect stack. // The frame is not setup, so it'd be hard to iterate the stack. Bailout.
return false; return false;
} }
...@@ -183,59 +254,5 @@ void TickSample::Init(Isolate* isolate, const v8::RegisterState& state, ...@@ -183,59 +254,5 @@ void TickSample::Init(Isolate* isolate, const v8::RegisterState& state,
timestamp = base::TimeTicks::HighResolutionNow(); timestamp = base::TimeTicks::HighResolutionNow();
} }
#if defined(USE_SIMULATOR)
bool SimulatorHelper::FillRegisters(Isolate* isolate,
v8::RegisterState* state) {
Simulator* simulator = isolate->thread_local_top()->simulator_;
// Check if there is active simulator.
if (simulator == NULL) return false;
#if V8_TARGET_ARCH_ARM
if (!simulator->has_bad_pc()) {
state->pc = reinterpret_cast<Address>(simulator->get_pc());
}
state->sp = reinterpret_cast<Address>(simulator->get_register(Simulator::sp));
state->fp =
reinterpret_cast<Address>(simulator->get_register(Simulator::r11));
#elif V8_TARGET_ARCH_ARM64
state->pc = reinterpret_cast<Address>(simulator->pc());
state->sp = reinterpret_cast<Address>(simulator->sp());
state->fp = reinterpret_cast<Address>(simulator->fp());
#elif V8_TARGET_ARCH_MIPS || V8_TARGET_ARCH_MIPS64
if (!simulator->has_bad_pc()) {
state->pc = reinterpret_cast<Address>(simulator->get_pc());
}
state->sp = reinterpret_cast<Address>(simulator->get_register(Simulator::sp));
state->fp = reinterpret_cast<Address>(simulator->get_register(Simulator::fp));
#elif V8_TARGET_ARCH_PPC
if (!simulator->has_bad_pc()) {
state->pc = reinterpret_cast<Address>(simulator->get_pc());
}
state->sp = reinterpret_cast<Address>(simulator->get_register(Simulator::sp));
state->fp = reinterpret_cast<Address>(simulator->get_register(Simulator::fp));
#elif V8_TARGET_ARCH_S390
if (!simulator->has_bad_pc()) {
state->pc = reinterpret_cast<Address>(simulator->get_pc());
}
state->sp = reinterpret_cast<Address>(simulator->get_register(Simulator::sp));
state->fp = reinterpret_cast<Address>(simulator->get_register(Simulator::fp));
#endif
if (state->sp == 0 || state->fp == 0) {
// It possible that the simulator is interrupted while it is updating
// the sp or fp register. ARM64 simulator does this in two steps:
// first setting it to zero and then setting it to the new value.
// Bailout if sp/fp doesn't contain the new value.
//
// FIXME: The above doesn't really solve the issue.
// If a 64-bit target is executed on a 32-bit host even the final
// write is non-atomic, so it might obtain a half of the result.
// Moreover as long as the register set code uses memcpy (as of now),
// it is not guaranteed to be atomic even when both host and target
// are of same bitness.
return false;
}
return true;
}
#endif // USE_SIMULATOR
} // namespace internal } // namespace internal
} // namespace v8 } // namespace v8
...@@ -15,21 +15,11 @@ namespace internal { ...@@ -15,21 +15,11 @@ namespace internal {
class Isolate; class Isolate;
struct TickSample : public v8::TickSample { struct TickSample : public v8::TickSample {
TickSample() : v8::TickSample() {}
void Init(Isolate* isolate, const v8::RegisterState& state, void Init(Isolate* isolate, const v8::RegisterState& state,
RecordCEntryFrame record_c_entry_frame, bool update_stats); RecordCEntryFrame record_c_entry_frame, bool update_stats);
base::TimeTicks timestamp; base::TimeTicks timestamp;
}; };
#if defined(USE_SIMULATOR)
class SimulatorHelper {
public:
// Returns true if register values were successfully retrieved
// from the simulator, otherwise returns false.
static bool FillRegisters(Isolate* isolate, v8::RegisterState* state);
};
#endif // USE_SIMULATOR
} // namespace internal } // namespace internal
} // namespace v8 } // namespace v8
......
...@@ -41,10 +41,9 @@ class TestSampler : public Sampler { ...@@ -41,10 +41,9 @@ class TestSampler : public Sampler {
explicit TestSampler(Isolate* isolate) : Sampler(isolate) {} explicit TestSampler(Isolate* isolate) : Sampler(isolate) {}
void SampleStack(const v8::RegisterState& regs) override { void SampleStack(const v8::RegisterState& regs) override {
void* frames[Sampler::kMaxFramesCount]; void* frames[kMaxFramesCount];
SampleInfo sample_info; SampleInfo sample_info;
isolate()->GetStackSample(regs, reinterpret_cast<void**>(frames), isolate()->GetStackSample(regs, frames, kMaxFramesCount, &sample_info);
Sampler::kMaxFramesCount, &sample_info);
if (is_counting_samples_) { if (is_counting_samples_) {
if (sample_info.vm_state == JS) ++js_sample_count_; if (sample_info.vm_state == JS) ++js_sample_count_;
if (sample_info.vm_state == EXTERNAL) ++external_sample_count_; if (sample_info.vm_state == EXTERNAL) ++external_sample_count_;
......
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