Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 | |
| 5 #include "src/sampler.h" | 4 #include "src/sampler.h" |
| 6 | 5 |
| 7 #if V8_OS_POSIX && !V8_OS_CYGWIN | 6 #if V8_OS_POSIX && !V8_OS_CYGWIN |
| 8 | 7 |
| 9 #define USE_SIGNALS | 8 #define USE_SIGNALS |
| 10 | 9 |
| 11 #include <errno.h> | 10 #include <errno.h> |
| 12 #include <pthread.h> | 11 #include <pthread.h> |
| 13 #include <signal.h> | 12 #include <signal.h> |
| 14 #include <sys/time.h> | 13 #include <sys/time.h> |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 35 !defined(__BIONIC_HAVE_STRUCT_SIGCONTEXT) | 34 !defined(__BIONIC_HAVE_STRUCT_SIGCONTEXT) |
| 36 #include <asm/sigcontext.h> // NOLINT | 35 #include <asm/sigcontext.h> // NOLINT |
| 37 #endif | 36 #endif |
| 38 | 37 |
| 39 #elif V8_OS_WIN || V8_OS_CYGWIN | 38 #elif V8_OS_WIN || V8_OS_CYGWIN |
| 40 | 39 |
| 41 #include "src/base/win32-headers.h" | 40 #include "src/base/win32-headers.h" |
| 42 | 41 |
| 43 #endif | 42 #endif |
| 44 | 43 |
| 44 #include "include/v8-sampler.h" | |
| 45 | |
| 45 #include "src/v8.h" | 46 #include "src/v8.h" |
| 46 | 47 |
| 47 #include "src/base/platform/platform.h" | 48 #include "src/base/platform/platform.h" |
| 48 #include "src/cpu-profiler-inl.h" | 49 #include "src/cpu-profiler-inl.h" |
| 49 #include "src/flags.h" | 50 #include "src/flags.h" |
| 50 #include "src/frames-inl.h" | 51 #include "src/frames-inl.h" |
| 51 #include "src/log.h" | 52 #include "src/log.h" |
| 52 #include "src/simulator.h" | 53 #include "src/simulator.h" |
| 53 #include "src/v8threads.h" | 54 #include "src/v8threads.h" |
| 54 #include "src/vm-state-inl.h" | 55 #include "src/vm-state-inl.h" |
| (...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 262 private: | 263 private: |
| 263 Simulator* simulator_; | 264 Simulator* simulator_; |
| 264 }; | 265 }; |
| 265 #endif // USE_SIMULATOR | 266 #endif // USE_SIMULATOR |
| 266 | 267 |
| 267 | 268 |
| 268 #if defined(USE_SIGNALS) | 269 #if defined(USE_SIGNALS) |
| 269 | 270 |
| 270 class SignalHandler : public AllStatic { | 271 class SignalHandler : public AllStatic { |
| 271 public: | 272 public: |
| 272 static void SetUp() { if (!mutex_) mutex_ = new base::Mutex(); } | 273 static void SetUp() { |
| 273 static void TearDown() { delete mutex_; } | 274 if (!mutex_) mutex_ = new base::Mutex(); |
|
Benedikt Meurer
2014/08/14 04:20:23
CHECK that both mutex_ and sampling_semaphore_ are
gholap
2014/08/15 17:54:13
Done.
Benedikt Meurer
2014/08/18 04:22:03
Acknowledged.
| |
| 275 if (!sampling_semaphore_) | |
| 276 sampling_semaphore_ = new base::Semaphore(0); | |
| 277 } | |
| 278 | |
| 279 static void TearDown() { | |
| 280 delete mutex_; | |
|
Benedikt Meurer
2014/08/14 04:20:23
Reset mutex_ and sampling_semaphore_ to NULL.
gholap
2014/08/15 17:54:13
Done.
Benedikt Meurer
2014/08/18 04:22:03
Acknowledged.
| |
| 281 delete sampling_semaphore_; | |
| 282 } | |
| 274 | 283 |
| 275 static void IncreaseSamplerCount() { | 284 static void IncreaseSamplerCount() { |
| 276 base::LockGuard<base::Mutex> lock_guard(mutex_); | 285 base::LockGuard<base::Mutex> lock_guard(mutex_); |
| 277 if (++client_count_ == 1) Install(); | 286 if (++client_count_ == 1) Install(); |
| 278 } | 287 } |
| 279 | 288 |
| 280 static void DecreaseSamplerCount() { | 289 static void DecreaseSamplerCount() { |
| 281 base::LockGuard<base::Mutex> lock_guard(mutex_); | 290 base::LockGuard<base::Mutex> lock_guard(mutex_); |
| 282 if (--client_count_ == 0) Restore(); | 291 if (--client_count_ == 0) Restore(); |
| 283 } | 292 } |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 301 } | 310 } |
| 302 | 311 |
| 303 static void Restore() { | 312 static void Restore() { |
| 304 if (signal_handler_installed_) { | 313 if (signal_handler_installed_) { |
| 305 sigaction(SIGPROF, &old_signal_handler_, 0); | 314 sigaction(SIGPROF, &old_signal_handler_, 0); |
| 306 signal_handler_installed_ = false; | 315 signal_handler_installed_ = false; |
| 307 } | 316 } |
| 308 } | 317 } |
| 309 | 318 |
| 310 static void HandleProfilerSignal(int signal, siginfo_t* info, void* context); | 319 static void HandleProfilerSignal(int signal, siginfo_t* info, void* context); |
| 320 | |
| 321 // The SIGPROF could have come from DoSample or from GetSample. | |
| 322 // GetSample is the internal endpoint for the public | |
| 323 // GetSample API, which aims to bypass all the internal buffers | |
| 324 // and return just one sample instead, rightaway. | |
| 325 // TODO(gholap): Eventually, get rid of DoSample. | |
| 326 // Only GetSample should remain. | |
| 327 | |
| 328 // This is the sample which will be filled by the call from GetSample. | |
| 329 static TickSample sample_; | |
|
Benedikt Meurer
2014/08/14 04:20:23
I'm not 100%, but I guess sample_ will also have t
| |
| 330 | |
| 331 // For the SIGPROF handler to know whether the call was from GetSample. | |
| 332 static volatile bool called_from_get_sample_; | |
| 333 | |
| 334 // GetSample waits synchronously till the SIGPROF handler returns. | |
| 335 // this semaphore is used to signal GetSample. | |
| 336 static base::Semaphore* sampling_semaphore_; | |
| 337 | |
| 338 // It is not that every time HandleProfilerSignal is invoked, | |
| 339 // it succeeds in obtaining a sample. | |
| 340 // This provides GetSample with the information whether | |
| 341 // the handler finished with or without getting the sample. | |
| 342 static bool sample_available_; | |
| 343 | |
| 311 // Protects the process wide state below. | 344 // Protects the process wide state below. |
| 312 static base::Mutex* mutex_; | 345 static base::Mutex* mutex_; |
| 313 static int client_count_; | 346 static int client_count_; |
| 314 static bool signal_handler_installed_; | 347 static bool signal_handler_installed_; |
| 315 static struct sigaction old_signal_handler_; | 348 static struct sigaction old_signal_handler_; |
| 349 | |
| 350 friend class Sampler; | |
| 316 }; | 351 }; |
| 317 | 352 |
| 318 | 353 |
| 354 TickSample SignalHandler::sample_; | |
| 355 volatile bool SignalHandler::called_from_get_sample_ = false; | |
| 356 base::Semaphore* SignalHandler::sampling_semaphore_ = NULL; | |
| 357 bool SignalHandler::sample_available_ = false; | |
| 358 | |
| 319 base::Mutex* SignalHandler::mutex_ = NULL; | 359 base::Mutex* SignalHandler::mutex_ = NULL; |
| 320 int SignalHandler::client_count_ = 0; | 360 int SignalHandler::client_count_ = 0; |
| 321 struct sigaction SignalHandler::old_signal_handler_; | 361 struct sigaction SignalHandler::old_signal_handler_; |
| 322 bool SignalHandler::signal_handler_installed_ = false; | 362 bool SignalHandler::signal_handler_installed_ = false; |
| 323 | 363 |
| 324 | 364 |
| 325 void SignalHandler::HandleProfilerSignal(int signal, siginfo_t* info, | 365 void SignalHandler::HandleProfilerSignal(int signal, siginfo_t* info, |
| 326 void* context) { | 366 void* context) { |
| 327 #if V8_OS_NACL | 367 #if V8_OS_NACL |
| 328 // As Native Client does not support signal handling, profiling | 368 // As Native Client does not support signal handling, profiling |
| 329 // is disabled. | 369 // is disabled. |
| 330 return; | 370 return; |
| 331 #else | 371 #else |
| 372 // Even if we return prematurely, | |
| 373 // we need to signal the sampling_semaphore_ | |
| 374 sample_available_ = false; | |
| 375 ScopedSemaphore scoped_semaphore(sampling_semaphore_); | |
|
Benedikt Meurer
2014/08/14 04:20:23
I'm not sure it's safe to call semaphore_signal()
gholap
2014/08/15 22:05:19
Yes, according to the xnu man pages, (http://web.m
Benedikt Meurer
2014/08/18 04:22:03
Acknowledged.
| |
| 376 | |
| 332 USE(info); | 377 USE(info); |
| 333 if (signal != SIGPROF) return; | 378 if (signal != SIGPROF) return; |
| 334 Isolate* isolate = Isolate::UncheckedReentrantCurrent(); | 379 Isolate* isolate = Isolate::UncheckedReentrantCurrent(); |
| 335 if (isolate == NULL || !isolate->IsInitialized() || !isolate->IsInUse()) { | 380 if (isolate == NULL || !isolate->IsInitialized() || !isolate->IsInUse()) { |
| 336 // We require a fully initialized and entered isolate. | 381 // We require a fully initialized and entered isolate. |
| 337 return; | 382 return; |
| 338 } | 383 } |
| 339 if (v8::Locker::IsActive() && | 384 if (v8::Locker::IsActive() && |
| 340 !isolate->thread_manager()->IsLockedByCurrentThread()) { | 385 !isolate->thread_manager()->IsLockedByCurrentThread()) { |
| 341 return; | 386 return; |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 459 state.pc = reinterpret_cast<Address>(mcontext.cpu.eip); | 504 state.pc = reinterpret_cast<Address>(mcontext.cpu.eip); |
| 460 state.sp = reinterpret_cast<Address>(mcontext.cpu.esp); | 505 state.sp = reinterpret_cast<Address>(mcontext.cpu.esp); |
| 461 state.fp = reinterpret_cast<Address>(mcontext.cpu.ebp); | 506 state.fp = reinterpret_cast<Address>(mcontext.cpu.ebp); |
| 462 #elif V8_HOST_ARCH_ARM | 507 #elif V8_HOST_ARCH_ARM |
| 463 state.pc = reinterpret_cast<Address>(mcontext.cpu.gpr[ARM_REG_PC]); | 508 state.pc = reinterpret_cast<Address>(mcontext.cpu.gpr[ARM_REG_PC]); |
| 464 state.sp = reinterpret_cast<Address>(mcontext.cpu.gpr[ARM_REG_SP]); | 509 state.sp = reinterpret_cast<Address>(mcontext.cpu.gpr[ARM_REG_SP]); |
| 465 state.fp = reinterpret_cast<Address>(mcontext.cpu.gpr[ARM_REG_FP]); | 510 state.fp = reinterpret_cast<Address>(mcontext.cpu.gpr[ARM_REG_FP]); |
| 466 #endif // V8_HOST_ARCH_* | 511 #endif // V8_HOST_ARCH_* |
| 467 #endif // V8_OS_QNX | 512 #endif // V8_OS_QNX |
| 468 #endif // USE_SIMULATOR | 513 #endif // USE_SIMULATOR |
| 469 sampler->SampleStack(state); | 514 if (called_from_get_sample_) { |
| 515 sample_.Init(sampler->isolate(), state); | |
| 516 } else { | |
| 517 sampler->SampleStack(state); | |
| 518 } | |
| 519 sample_available_ = true; | |
| 470 #endif // V8_OS_NACL | 520 #endif // V8_OS_NACL |
| 471 } | 521 } |
| 472 | 522 |
| 473 #endif | 523 #endif |
| 474 | 524 |
| 475 | 525 |
| 476 class SamplerThread : public base::Thread { | 526 class SamplerThread : public base::Thread { |
| 477 public: | 527 public: |
| 478 static const int kSamplerThreadStackSize = 64 * KB; | 528 static const int kSamplerThreadStackSize = 64 * KB; |
| 479 | 529 |
| (...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 679 ++js_and_external_sample_count_; | 729 ++js_and_external_sample_count_; |
| 680 } | 730 } |
| 681 } | 731 } |
| 682 Tick(sample); | 732 Tick(sample); |
| 683 if (sample != &sample_obj) { | 733 if (sample != &sample_obj) { |
| 684 isolate_->cpu_profiler()->FinishTickSample(); | 734 isolate_->cpu_profiler()->FinishTickSample(); |
| 685 } | 735 } |
| 686 } | 736 } |
| 687 | 737 |
| 688 | 738 |
| 739 void Sampler::CopyTickSampleToSample(TickSample* tick_sample, | |
| 740 v8::Sample* sample) { | |
| 741 sample->state = tick_sample->state; | |
| 742 for (int i = 0; i < tick_sample->frames_count; i++) { | |
| 743 sample->stack[i] = tick_sample->stack[i]; | |
| 744 } | |
| 745 sample->frames_count = tick_sample->frames_count; | |
| 746 } | |
| 747 | |
| 748 | |
| 689 #if defined(USE_SIGNALS) | 749 #if defined(USE_SIGNALS) |
| 690 | 750 |
| 691 void Sampler::DoSample() { | 751 void Sampler::DoSample() { |
| 692 if (!SignalHandler::Installed()) return; | 752 if (!SignalHandler::Installed()) return; |
| 693 pthread_kill(platform_data()->vm_tid(), SIGPROF); | 753 pthread_kill(platform_data()->vm_tid(), SIGPROF); |
| 694 } | 754 } |
| 695 | 755 |
| 756 | |
| 757 v8::Sample* Sampler::GetSample(v8::Sample* sample) { | |
| 758 if (!SignalHandler::Installed()) return NULL; | |
| 759 SignalHandler::called_from_get_sample_ = true; | |
| 760 pthread_kill(platform_data()->vm_tid(), SIGPROF); | |
| 761 SignalHandler::sampling_semaphore_->Wait(); | |
| 762 if (SignalHandler::sample_available_) { | |
| 763 CopyTickSampleToSample(&SignalHandler::sample_, sample); | |
| 764 } else { | |
| 765 sample = NULL; | |
| 766 } | |
| 767 SignalHandler::called_from_get_sample_ = false; | |
| 768 return sample; | |
| 769 } | |
| 770 | |
| 696 #elif V8_OS_WIN || V8_OS_CYGWIN | 771 #elif V8_OS_WIN || V8_OS_CYGWIN |
| 697 | 772 |
| 698 void Sampler::DoSample() { | 773 void Sampler::GetSampleHelper(TickSample * sample, |
| 774 bool called_from_get_sample) { | |
| 699 HANDLE profiled_thread = platform_data()->profiled_thread(); | 775 HANDLE profiled_thread = platform_data()->profiled_thread(); |
| 700 if (profiled_thread == NULL) return; | 776 if (profiled_thread == NULL) return NULL; |
| 701 | 777 |
| 702 #if defined(USE_SIMULATOR) | 778 #if defined(USE_SIMULATOR) |
| 703 SimulatorHelper helper; | 779 SimulatorHelper helper; |
| 704 if (!helper.Init(this, isolate())) return; | 780 if (!helper.Init(this, isolate())) return NULL; |
| 705 #endif | 781 #endif |
| 706 | 782 |
| 707 const DWORD kSuspendFailed = static_cast<DWORD>(-1); | 783 const DWORD kSuspendFailed = static_cast<DWORD>(-1); |
| 708 if (SuspendThread(profiled_thread) == kSuspendFailed) return; | 784 if (SuspendThread(profiled_thread) == kSuspendFailed) return NULL; |
| 709 | 785 |
| 710 // Context used for sampling the register state of the profiled thread. | 786 // Context used for sampling the register state of the profiled thread. |
| 711 CONTEXT context; | 787 CONTEXT context; |
| 712 memset(&context, 0, sizeof(context)); | 788 memset(&context, 0, sizeof(context)); |
| 713 context.ContextFlags = CONTEXT_FULL; | 789 context.ContextFlags = CONTEXT_FULL; |
| 714 if (GetThreadContext(profiled_thread, &context) != 0) { | 790 if (GetThreadContext(profiled_thread, &context) != 0) { |
| 715 RegisterState state; | 791 RegisterState state; |
| 716 #if defined(USE_SIMULATOR) | 792 #if defined(USE_SIMULATOR) |
| 717 helper.FillRegisters(&state); | 793 helper.FillRegisters(&state); |
| 718 #else | 794 #else |
| 719 #if V8_HOST_ARCH_X64 | 795 #if V8_HOST_ARCH_X64 |
| 720 state.pc = reinterpret_cast<Address>(context.Rip); | 796 state.pc = reinterpret_cast<Address>(context.Rip); |
| 721 state.sp = reinterpret_cast<Address>(context.Rsp); | 797 state.sp = reinterpret_cast<Address>(context.Rsp); |
| 722 state.fp = reinterpret_cast<Address>(context.Rbp); | 798 state.fp = reinterpret_cast<Address>(context.Rbp); |
| 723 #else | 799 #else |
| 724 state.pc = reinterpret_cast<Address>(context.Eip); | 800 state.pc = reinterpret_cast<Address>(context.Eip); |
| 725 state.sp = reinterpret_cast<Address>(context.Esp); | 801 state.sp = reinterpret_cast<Address>(context.Esp); |
| 726 state.fp = reinterpret_cast<Address>(context.Ebp); | 802 state.fp = reinterpret_cast<Address>(context.Ebp); |
| 727 #endif | 803 #endif |
| 728 #endif // USE_SIMULATOR | 804 #endif // USE_SIMULATOR |
| 729 SampleStack(state); | 805 if (called_from_get_sample) { |
| 806 sample->Init(sampler->isolate(), state); | |
| 807 } else { | |
| 808 SampleStack(state); | |
| 809 } | |
| 730 } | 810 } |
| 731 ResumeThread(profiled_thread); | 811 ResumeThread(profiled_thread); |
| 732 } | 812 } |
| 733 | 813 |
| 814 | |
| 815 void Sampler::DoSample() { | |
| 816 GetSampleHelper(NULL, false); | |
| 817 } | |
| 818 | |
| 819 | |
| 820 v8::Sample* Sampler::GetSample(v8::Sample* sample) { | |
| 821 // We need this tick_sample because, | |
| 822 // GetSampleHelper calls Init, which must be called on | |
| 823 // a TickSample and never on Sample. | |
| 824 TickSample tick_sample; | |
| 825 GetSampleHelper(&tick_sample, true); | |
| 826 CopyTickSampleToSample(&tick_sample, sample); | |
| 827 return sample; | |
| 828 } | |
| 829 | |
| 734 #endif // USE_SIGNALS | 830 #endif // USE_SIGNALS |
| 735 | 831 |
| 736 | 832 |
| 737 } } // namespace v8::internal | 833 } } // namespace v8::internal |
| OLD | NEW |