Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(271)

Side by Side Diff: src/sampler.cc

Issue 596533002: Initial implementation of GetStackSample sampling profiler API. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright 2013 the V8 project authors. All rights reserved. 1 // Copyright 2013 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "src/sampler.h" 5 #include "src/sampler.h"
6 6
7 #if V8_OS_POSIX && !V8_OS_CYGWIN 7 #if V8_OS_POSIX && !V8_OS_CYGWIN
8 8
9 #define USE_SIGNALS 9 #define USE_SIGNALS
10 10
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after
219 219
220 private: 220 private:
221 HANDLE profiled_thread_; 221 HANDLE profiled_thread_;
222 }; 222 };
223 #endif 223 #endif
224 224
225 225
226 #if defined(USE_SIMULATOR) 226 #if defined(USE_SIMULATOR)
227 class SimulatorHelper { 227 class SimulatorHelper {
228 public: 228 public:
229 inline bool Init(Sampler* sampler, Isolate* isolate) { 229 inline bool Init(Isolate* isolate) {
230 simulator_ = isolate->thread_local_top()->simulator_; 230 simulator_ = isolate->thread_local_top()->simulator_;
231 // Check if there is active simulator. 231 // Check if there is active simulator.
232 return simulator_ != NULL; 232 return simulator_ != NULL;
233 } 233 }
234 234
235 inline void FillRegisters(RegisterState* state) { 235 inline void FillRegisters(v8::RegisterState* state) {
236 #if V8_TARGET_ARCH_ARM 236 #if V8_TARGET_ARCH_ARM
237 state->pc = reinterpret_cast<Address>(simulator_->get_pc()); 237 state->pc = reinterpret_cast<Address>(simulator_->get_pc());
238 state->sp = reinterpret_cast<Address>(simulator_->get_register( 238 state->sp = reinterpret_cast<Address>(simulator_->get_register(
239 Simulator::sp)); 239 Simulator::sp));
240 state->fp = reinterpret_cast<Address>(simulator_->get_register( 240 state->fp = reinterpret_cast<Address>(simulator_->get_register(
241 Simulator::r11)); 241 Simulator::r11));
242 #elif V8_TARGET_ARCH_ARM64 242 #elif V8_TARGET_ARCH_ARM64
243 if (simulator_->sp() == 0 || simulator_->fp() == 0) { 243 if (simulator_->sp() == 0 || simulator_->fp() == 0) {
244 // It possible that the simulator is interrupted while it is updating 244 // It's possible that the simulator is interrupted while it is updating
245 // the sp or fp register. ARM64 simulator does this in two steps: 245 // the sp or fp register. ARM64 simulator does this in two steps:
246 // first setting it to zero and then setting it to the new value. 246 // first setting it to zero and then setting it to a new value.
247 // Bailout if sp/fp doesn't contain the new value. 247 // Bailout if sp/fp doesn't contain the new value.
248 return; 248 return;
249 } 249 }
250 state->pc = reinterpret_cast<Address>(simulator_->pc()); 250 state->pc = reinterpret_cast<Address>(simulator_->pc());
251 state->sp = reinterpret_cast<Address>(simulator_->sp()); 251 state->sp = reinterpret_cast<Address>(simulator_->sp());
252 state->fp = reinterpret_cast<Address>(simulator_->fp()); 252 state->fp = reinterpret_cast<Address>(simulator_->fp());
253 #elif V8_TARGET_ARCH_MIPS 253 #elif V8_TARGET_ARCH_MIPS
254 state->pc = reinterpret_cast<Address>(simulator_->get_pc()); 254 state->pc = reinterpret_cast<Address>(simulator_->get_pc());
255 state->sp = reinterpret_cast<Address>(simulator_->get_register( 255 state->sp = reinterpret_cast<Address>(simulator_->get_register(
256 Simulator::sp)); 256 Simulator::sp));
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
346 return; 346 return;
347 } 347 }
348 if (v8::Locker::IsActive() && 348 if (v8::Locker::IsActive() &&
349 !isolate->thread_manager()->IsLockedByCurrentThread()) { 349 !isolate->thread_manager()->IsLockedByCurrentThread()) {
350 return; 350 return;
351 } 351 }
352 352
353 Sampler* sampler = isolate->logger()->sampler(); 353 Sampler* sampler = isolate->logger()->sampler();
354 if (sampler == NULL) return; 354 if (sampler == NULL) return;
355 355
356 RegisterState state; 356 v8::RegisterState state;
357 357
358 #if defined(USE_SIMULATOR) 358 #if defined(USE_SIMULATOR)
359 SimulatorHelper helper; 359 SimulatorHelper helper;
360 if (!helper.Init(sampler, isolate)) return; 360 if (!helper.Init(isolate)) return;
361 helper.FillRegisters(&state); 361 helper.FillRegisters(&state);
362 // It possible that the simulator is interrupted while it is updating 362 // It possible that the simulator is interrupted while it is updating
363 // the sp or fp register. ARM64 simulator does this in two steps: 363 // the sp or fp register. ARM64 simulator does this in two steps:
364 // first setting it to zero and then setting it to the new value. 364 // first setting it to zero and then setting it to the new value.
365 // Bailout if sp/fp doesn't contain the new value. 365 // Bailout if sp/fp doesn't contain the new value.
366 if (state.sp == 0 || state.fp == 0) return; 366 if (state.sp == 0 || state.fp == 0) return;
367 #else 367 #else
368 // Extracting the sample from the context is extremely machine dependent. 368 // Extracting the sample from the context is extremely machine dependent.
369 ucontext_t* ucontext = reinterpret_cast<ucontext_t*>(context); 369 ucontext_t* ucontext = reinterpret_cast<ucontext_t*>(context);
370 #if !V8_OS_OPENBSD 370 #if !V8_OS_OPENBSD
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
570 570
571 571
572 base::Mutex* SamplerThread::mutex_ = NULL; 572 base::Mutex* SamplerThread::mutex_ = NULL;
573 SamplerThread* SamplerThread::instance_ = NULL; 573 SamplerThread* SamplerThread::instance_ = NULL;
574 574
575 575
576 // 576 //
577 // StackTracer implementation 577 // StackTracer implementation
578 // 578 //
579 DISABLE_ASAN void TickSample::Init(Isolate* isolate, 579 DISABLE_ASAN void TickSample::Init(Isolate* isolate,
580 const RegisterState& regs) { 580 const v8::RegisterState& regs) {
581 DCHECK(isolate->IsInitialized()); 581 DCHECK(isolate->IsInitialized());
582 timestamp = base::TimeTicks::HighResolutionNow(); 582 timestamp = base::TimeTicks::HighResolutionNow();
583 pc = regs.pc; 583 pc = reinterpret_cast<Address>(regs.pc);
584 state = isolate->current_vm_state(); 584 state = isolate->current_vm_state();
585 585
586 // Avoid collecting traces while doing GC. 586 // Avoid collecting traces while doing GC.
587 if (state == GC) return; 587 if (state == GC) return;
588 588
589 Address js_entry_sp = isolate->js_entry_sp(); 589 Address js_entry_sp = isolate->js_entry_sp();
590 if (js_entry_sp == 0) { 590 if (js_entry_sp == 0) {
591 // Not executing JS now. 591 // Not executing JS now.
592 return; 592 return;
593 } 593 }
594 594
595 ExternalCallbackScope* scope = isolate->external_callback_scope(); 595 ExternalCallbackScope* scope = isolate->external_callback_scope();
596 Address handler = Isolate::handler(isolate->thread_local_top()); 596 Address handler = Isolate::handler(isolate->thread_local_top());
597 // If there is a handler on top of the external callback scope then 597 // If there is a handler on top of the external callback scope then
598 // we have already entrered JavaScript again and the external callback 598 // we have already entrered JavaScript again and the external callback
599 // is not the top function. 599 // is not the top function.
600 if (scope && scope->scope_address() < handler) { 600 if (scope && scope->scope_address() < handler) {
601 external_callback = scope->callback(); 601 external_callback = scope->callback();
602 has_external_callback = true; 602 has_external_callback = true;
603 } else { 603 } else {
604 // Sample potential return address value for frameless invocation of 604 // Sample potential return address value for frameless invocation of
605 // stubs (we'll figure out later, if this value makes sense). 605 // stubs (we'll figure out later, if this value makes sense).
606 tos = Memory::Address_at(regs.sp); 606 tos = Memory::Address_at(reinterpret_cast<Address>(regs.sp));
607 has_external_callback = false; 607 has_external_callback = false;
608 } 608 }
609 609
610 SafeStackFrameIterator it(isolate, regs.fp, regs.sp, js_entry_sp); 610 SafeStackFrameIterator it(isolate, reinterpret_cast<Address>(regs.fp),
611 reinterpret_cast<Address>(regs.sp), js_entry_sp);
611 top_frame_type = it.top_frame_type(); 612 top_frame_type = it.top_frame_type();
612 unsigned i = 0; 613
613 while (!it.done() && i < TickSample::kMaxFramesCount) { 614 frames_count = GetStackSample(
614 stack[i++] = it.frame()->pc(); 615 isolate, regs, reinterpret_cast<void**>(&stack[0]), kMaxFramesCount);
615 it.Advance();
616 }
617 frames_count = i;
618 } 616 }
619 617
620 618
619 int TickSample::GetStackSample(Isolate* isolate, const v8::RegisterState& regs,
Benedikt Meurer 2014/09/23 05:47:48 Nit: int -> size_t
alph 2014/09/23 07:30:46 Done.
620 void** frames, int frames_limit) {
Benedikt Meurer 2014/09/23 05:47:48 Nit: int -> size_t
alph 2014/09/23 07:30:46 Done.
621 DCHECK(isolate->IsInitialized());
622 Address js_entry_sp = isolate->js_entry_sp();
623 if (js_entry_sp == 0) {
624 // Not executing JS now.
625 return 0;
626 }
627
628 SafeStackFrameIterator it(isolate, reinterpret_cast<Address>(regs.fp),
629 reinterpret_cast<Address>(regs.sp), js_entry_sp);
630 int i = 0;
Benedikt Meurer 2014/09/23 05:47:48 Nit: int -> size_t
alph 2014/09/23 07:30:46 Done.
631 while (!it.done() && i < frames_limit) {
632 frames[i++] = it.frame()->pc();
633 it.Advance();
634 }
635 return i;
636 }
637
638
621 void Sampler::SetUp() { 639 void Sampler::SetUp() {
622 #if defined(USE_SIGNALS) 640 #if defined(USE_SIGNALS)
623 SignalHandler::SetUp(); 641 SignalHandler::SetUp();
624 #endif 642 #endif
625 SamplerThread::SetUp(); 643 SamplerThread::SetUp();
626 } 644 }
627 645
628 646
629 void Sampler::TearDown() { 647 void Sampler::TearDown() {
630 SamplerThread::TearDown(); 648 SamplerThread::TearDown();
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
675 693
676 694
677 void Sampler::DecreaseProfilingDepth() { 695 void Sampler::DecreaseProfilingDepth() {
678 #if defined(USE_SIGNALS) 696 #if defined(USE_SIGNALS)
679 SignalHandler::DecreaseSamplerCount(); 697 SignalHandler::DecreaseSamplerCount();
680 #endif 698 #endif
681 base::NoBarrier_AtomicIncrement(&profiling_, -1); 699 base::NoBarrier_AtomicIncrement(&profiling_, -1);
682 } 700 }
683 701
684 702
685 void Sampler::SampleStack(const RegisterState& state) { 703 void Sampler::SampleStack(const v8::RegisterState& state) {
686 TickSample* sample = isolate_->cpu_profiler()->StartTickSample(); 704 TickSample* sample = isolate_->cpu_profiler()->StartTickSample();
687 TickSample sample_obj; 705 TickSample sample_obj;
688 if (sample == NULL) sample = &sample_obj; 706 if (sample == NULL) sample = &sample_obj;
689 sample->Init(isolate_, state); 707 sample->Init(isolate_, state);
690 if (is_counting_samples_) { 708 if (is_counting_samples_) {
691 if (sample->state == JS || sample->state == EXTERNAL) { 709 if (sample->state == JS || sample->state == EXTERNAL) {
692 ++js_and_external_sample_count_; 710 ++js_and_external_sample_count_;
693 } 711 }
694 } 712 }
695 Tick(sample); 713 Tick(sample);
(...skipping 11 matching lines...) Expand all
707 } 725 }
708 726
709 #elif V8_OS_WIN || V8_OS_CYGWIN 727 #elif V8_OS_WIN || V8_OS_CYGWIN
710 728
711 void Sampler::DoSample() { 729 void Sampler::DoSample() {
712 HANDLE profiled_thread = platform_data()->profiled_thread(); 730 HANDLE profiled_thread = platform_data()->profiled_thread();
713 if (profiled_thread == NULL) return; 731 if (profiled_thread == NULL) return;
714 732
715 #if defined(USE_SIMULATOR) 733 #if defined(USE_SIMULATOR)
716 SimulatorHelper helper; 734 SimulatorHelper helper;
717 if (!helper.Init(this, isolate())) return; 735 if (!helper.Init(isolate())) return;
718 #endif 736 #endif
719 737
720 const DWORD kSuspendFailed = static_cast<DWORD>(-1); 738 const DWORD kSuspendFailed = static_cast<DWORD>(-1);
721 if (SuspendThread(profiled_thread) == kSuspendFailed) return; 739 if (SuspendThread(profiled_thread) == kSuspendFailed) return;
722 740
723 // Context used for sampling the register state of the profiled thread. 741 // Context used for sampling the register state of the profiled thread.
724 CONTEXT context; 742 CONTEXT context;
725 memset(&context, 0, sizeof(context)); 743 memset(&context, 0, sizeof(context));
726 context.ContextFlags = CONTEXT_FULL; 744 context.ContextFlags = CONTEXT_FULL;
727 if (GetThreadContext(profiled_thread, &context) != 0) { 745 if (GetThreadContext(profiled_thread, &context) != 0) {
728 RegisterState state; 746 v8::RegisterState state;
729 #if defined(USE_SIMULATOR) 747 #if defined(USE_SIMULATOR)
730 helper.FillRegisters(&state); 748 helper.FillRegisters(&state);
731 #else 749 #else
732 #if V8_HOST_ARCH_X64 750 #if V8_HOST_ARCH_X64
733 state.pc = reinterpret_cast<Address>(context.Rip); 751 state.pc = reinterpret_cast<Address>(context.Rip);
734 state.sp = reinterpret_cast<Address>(context.Rsp); 752 state.sp = reinterpret_cast<Address>(context.Rsp);
735 state.fp = reinterpret_cast<Address>(context.Rbp); 753 state.fp = reinterpret_cast<Address>(context.Rbp);
736 #else 754 #else
737 state.pc = reinterpret_cast<Address>(context.Eip); 755 state.pc = reinterpret_cast<Address>(context.Eip);
738 state.sp = reinterpret_cast<Address>(context.Esp); 756 state.sp = reinterpret_cast<Address>(context.Esp);
739 state.fp = reinterpret_cast<Address>(context.Ebp); 757 state.fp = reinterpret_cast<Address>(context.Ebp);
740 #endif 758 #endif
741 #endif // USE_SIMULATOR 759 #endif // USE_SIMULATOR
742 SampleStack(state); 760 SampleStack(state);
743 } 761 }
744 ResumeThread(profiled_thread); 762 ResumeThread(profiled_thread);
745 } 763 }
746 764
747 #endif // USE_SIGNALS 765 #endif // USE_SIGNALS
748 766
749 767
750 } } // namespace v8::internal 768 } } // namespace v8::internal
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698