~Ticker() { if (IsActive()) Stop(); }
- void SampleStack(TickSample* sample) {
+ virtual void SampleStack(TickSample* sample) {
+ ASSERT(IsSynchronous());
StackTracer::Trace(sample);
}
- void Tick(TickSample* sample) {
+ virtual void Tick(TickSample* sample) {
if (profiler_) profiler_->Insert(sample);
if (window_) window_->AddState(sample->state);
}
USE(info);
if (signal != SIGPROF) return;
if (active_sampler_ == NULL) return;
+ if (!IsVmThread()) return;
TickSample sample_obj;
TickSample* sample = CpuProfiler::TickSampleEvent();
// We always sample the VM state.
sample->state = VMState::current_state();
+
// If profiling, we extract the current pc and sp.
if (active_sampler_->IsProfiling()) {
// Extracting the sample from the context is extremely machine dependent.
// Implement this on MIPS.
UNIMPLEMENTED();
#endif
- if (IsVmThread()) {
- active_sampler_->SampleStack(sample);
- }
+ active_sampler_->SampleStack(sample);
}
active_sampler_->Tick(sample);
Sampler::Sampler(int interval, bool profiling)
- : interval_(interval), profiling_(profiling), active_(false) {
+ : interval_(interval),
+ profiling_(profiling),
+ synchronous_(profiling),
+ active_(false) {
data_ = new PlatformData();
}
// Sampler thread handler.
void Runner() {
- // Loop until the sampler is disengaged, keeping the specified samling freq.
+ // Loop until the sampler is disengaged, keeping the specified
+ // sampling frequency.
for ( ; sampler_->IsActive(); OS::Sleep(sampler_->interval_)) {
TickSample sample_obj;
TickSample* sample = CpuProfiler::TickSampleEvent();
if (sample == NULL) sample = &sample_obj;
+ // If the sampler runs in sync with the JS thread, we try to
+ // suspend it. If we fail, we skip the current sample.
+ if (sampler_->IsSynchronous()) {
+ if (KERN_SUCCESS != thread_suspend(profiled_thread_)) continue;
+ }
+
// We always sample the VM state.
sample->state = VMState::current_state();
+
// If profiling, we record the pc and sp of the profiled thread.
- if (sampler_->IsProfiling()
- && KERN_SUCCESS == thread_suspend(profiled_thread_)) {
+ if (sampler_->IsProfiling()) {
#if V8_HOST_ARCH_X64
thread_state_flavor_t flavor = x86_THREAD_STATE64;
x86_thread_state64_t state;
sample->fp = reinterpret_cast<Address>(state.REGISTER_FIELD(bp));
sampler_->SampleStack(sample);
}
- thread_resume(profiled_thread_);
}
// Invoke tick handler with program counter and stack pointer.
sampler_->Tick(sample);
+
+ // If the sampler runs in sync with the JS thread, we have to
+ // remember to resume it.
+ if (sampler_->IsSynchronous()) thread_resume(profiled_thread_);
}
}
};
Sampler::Sampler(int interval, bool profiling)
- : interval_(interval), profiling_(profiling), active_(false) {
+ : interval_(interval),
+ profiling_(profiling),
+ synchronous_(profiling),
+ active_(false) {
data_ = new PlatformData(this);
}
void Sampler::Start() {
- // If we are profiling, we need to be able to access the calling
- // thread.
- if (IsProfiling()) {
+ // If we are starting a synchronous sampler, we need to be able to
+ // access the calling thread.
+ if (IsSynchronous()) {
data_->profiled_thread_ = mach_thread_self();
}
pthread_join(data_->sampler_thread_, NULL);
// Deallocate Mach port for thread.
- if (IsProfiling()) {
+ if (IsSynchronous()) {
mach_port_deallocate(data_->task_self_, data_->profiled_thread_);
}
}
// Context used for sampling the register state of the profiled thread.
CONTEXT context;
memset(&context, 0, sizeof(context));
- // Loop until the sampler is disengaged, keeping the specified samling freq.
+ // Loop until the sampler is disengaged, keeping the specified
+ // sampling frequency.
for ( ; sampler_->IsActive(); Sleep(sampler_->interval_)) {
TickSample sample_obj;
TickSample* sample = CpuProfiler::TickSampleEvent();
if (sample == NULL) sample = &sample_obj;
+ // If the sampler runs in sync with the JS thread, we try to
+ // suspend it. If we fail, we skip the current sample.
+ if (sampler_->IsSynchronous()) {
+ static const DWORD kSuspendFailed = static_cast<DWORD>(-1);
+ if (SuspendThread(profiled_thread_) == kSuspendFailed) continue;
+ }
+
// We always sample the VM state.
sample->state = VMState::current_state();
+
// If profiling, we record the pc and sp of the profiled thread.
- if (sampler_->IsProfiling()
- && SuspendThread(profiled_thread_) != (DWORD)-1) {
+ if (sampler_->IsProfiling()) {
context.ContextFlags = CONTEXT_FULL;
if (GetThreadContext(profiled_thread_, &context) != 0) {
#if V8_HOST_ARCH_X64
#endif
sampler_->SampleStack(sample);
}
- ResumeThread(profiled_thread_);
}
// Invoke tick handler with program counter and stack pointer.
sampler_->Tick(sample);
+
+ // If the sampler runs in sync with the JS thread, we have to
+ // remember to resume it.
+ if (sampler_->IsSynchronous()) ResumeThread(profiled_thread_);
}
}
};
// Initialize a profile sampler.
Sampler::Sampler(int interval, bool profiling)
- : interval_(interval), profiling_(profiling), active_(false) {
+ : interval_(interval),
+ profiling_(profiling),
+ synchronous_(profiling),
+ active_(false) {
data_ = new PlatformData(this);
}
// Start profiling.
void Sampler::Start() {
- // If we are profiling, we need to be able to access the calling
- // thread.
- if (IsProfiling()) {
+ // If we are starting a synchronous sampler, we need to be able to
+ // access the calling thread.
+ if (IsSynchronous()) {
// Get a handle to the calling thread. This is the thread that we are
// going to profile. We need to make a copy of the handle because we are
// going to use it in the sampler thread. Using GetThreadHandle() will
void Start();
void Stop();
- // Is the sampler used for profiling.
- inline bool IsProfiling() { return profiling_; }
+ // Is the sampler used for profiling?
+ bool IsProfiling() const { return profiling_; }
+
+ // Is the sampler running in sync with the JS thread? On platforms
+ // where the sampler is implemented with a thread that wakes up
+ // every now and then, having a synchronous sampler implies
+ // suspending/resuming the JS thread.
+ bool IsSynchronous() const { return synchronous_; }
// Whether the sampler is running (that is, consumes resources).
- inline bool IsActive() { return active_; }
+ bool IsActive() const { return active_; }
class PlatformData;
private:
const int interval_;
const bool profiling_;
+ const bool synchronous_;
bool active_;
PlatformData* data_; // Platform specific data.
DISALLOW_IMPLICIT_CONSTRUCTORS(Sampler);
CHECK(!sampler.WasSampleStackCalled());
nonJsThread.WaitForRunning();
nonJsThread.SendSigProf();
- CHECK(sampler.WaitForTick());
+ CHECK(!sampler.WaitForTick());
CHECK(!sampler.WasSampleStackCalled());
sampler.Stop();