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 22 matching lines...) Expand all Loading... | |
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 |
45 #include "src/v8.h" | 44 #include "src/v8.h" |
46 | 45 |
46 #include "src/base/logging.h" | |
47 #include "src/base/platform/platform.h" | 47 #include "src/base/platform/platform.h" |
48 #include "src/cpu-profiler-inl.h" | 48 #include "src/cpu-profiler-inl.h" |
49 #include "src/flags.h" | 49 #include "src/flags.h" |
50 #include "src/frames-inl.h" | 50 #include "src/frames-inl.h" |
51 #include "src/log.h" | 51 #include "src/log.h" |
52 #include "src/simulator.h" | 52 #include "src/simulator.h" |
53 #include "src/v8threads.h" | 53 #include "src/v8threads.h" |
54 #include "src/vm-state-inl.h" | 54 #include "src/vm-state-inl.h" |
55 | 55 |
56 | 56 |
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
262 private: | 262 private: |
263 Simulator* simulator_; | 263 Simulator* simulator_; |
264 }; | 264 }; |
265 #endif // USE_SIMULATOR | 265 #endif // USE_SIMULATOR |
266 | 266 |
267 | 267 |
268 #if defined(USE_SIGNALS) | 268 #if defined(USE_SIGNALS) |
269 | 269 |
270 class SignalHandler : public AllStatic { | 270 class SignalHandler : public AllStatic { |
271 public: | 271 public: |
272 static void SetUp() { if (!mutex_) mutex_ = new base::Mutex(); } | 272 static void SetUp() { |
273 static void TearDown() { delete mutex_; } | 273 ASSERT_EQ(NULL, mutex_); |
274 ASSERT_EQ(NULL, sampling_semaphore_); | |
275 | |
276 mutex_ = new base::Mutex(); | |
277 sampling_semaphore_ = new base::Semaphore(0); | |
278 } | |
279 | |
280 static void TearDown() { | |
281 delete mutex_; | |
282 delete sampling_semaphore_; | |
283 | |
284 mutex_ = NULL; | |
285 sampling_semaphore_ = NULL; | |
286 } | |
274 | 287 |
275 static void IncreaseSamplerCount() { | 288 static void IncreaseSamplerCount() { |
276 base::LockGuard<base::Mutex> lock_guard(mutex_); | 289 base::LockGuard<base::Mutex> lock_guard(mutex_); |
277 if (++client_count_ == 1) Install(); | 290 if (++client_count_ == 1) Install(); |
278 } | 291 } |
279 | 292 |
280 static void DecreaseSamplerCount() { | 293 static void DecreaseSamplerCount() { |
281 base::LockGuard<base::Mutex> lock_guard(mutex_); | 294 base::LockGuard<base::Mutex> lock_guard(mutex_); |
282 if (--client_count_ == 0) Restore(); | 295 if (--client_count_ == 0) Restore(); |
283 } | 296 } |
(...skipping 17 matching lines...) Expand all Loading... | |
301 } | 314 } |
302 | 315 |
303 static void Restore() { | 316 static void Restore() { |
304 if (signal_handler_installed_) { | 317 if (signal_handler_installed_) { |
305 sigaction(SIGPROF, &old_signal_handler_, 0); | 318 sigaction(SIGPROF, &old_signal_handler_, 0); |
306 signal_handler_installed_ = false; | 319 signal_handler_installed_ = false; |
307 } | 320 } |
308 } | 321 } |
309 | 322 |
310 static void HandleProfilerSignal(int signal, siginfo_t* info, void* context); | 323 static void HandleProfilerSignal(int signal, siginfo_t* info, void* context); |
324 | |
325 // The SIGPROF could have come from DoSample or from GetSample. | |
326 // GetSample is the internal endpoint for the public | |
327 // GetSample API, which aims to bypass all the internal buffers | |
328 // and return just one sample instead, rightaway. | |
329 // TODO(gholap): Eventually, get rid of DoSample. | |
330 // Only GetSample should remain. | |
331 | |
332 // This is the sample which will be filled by the call from GetSample. | |
333 static TickSample sample_; | |
334 | |
335 // For the SIGPROF handler to know whether the call was from GetSample. | |
336 static volatile bool called_from_get_sample_; | |
337 | |
338 // GetSample waits synchronously till the SIGPROF handler returns. | |
339 // this semaphore is used to signal GetSample. | |
340 static base::Semaphore* sampling_semaphore_; | |
341 | |
342 // It is not that every time HandleProfilerSignal is invoked, | |
343 // it succeeds in obtaining a sample. | |
344 // This provides GetSample with the information whether | |
345 // the handler finished with or without getting the sample. | |
346 static bool sample_available_; | |
347 | |
311 // Protects the process wide state below. | 348 // Protects the process wide state below. |
312 static base::Mutex* mutex_; | 349 static base::Mutex* mutex_; |
313 static int client_count_; | 350 static int client_count_; |
314 static bool signal_handler_installed_; | 351 static bool signal_handler_installed_; |
315 static struct sigaction old_signal_handler_; | 352 static struct sigaction old_signal_handler_; |
353 | |
354 friend class Sampler; | |
316 }; | 355 }; |
317 | 356 |
318 | 357 |
358 TickSample SignalHandler::sample_; | |
359 volatile bool SignalHandler::called_from_get_sample_ = false; | |
360 base::Semaphore* SignalHandler::sampling_semaphore_ = NULL; | |
361 bool SignalHandler::sample_available_ = false; | |
362 | |
319 base::Mutex* SignalHandler::mutex_ = NULL; | 363 base::Mutex* SignalHandler::mutex_ = NULL; |
320 int SignalHandler::client_count_ = 0; | 364 int SignalHandler::client_count_ = 0; |
321 struct sigaction SignalHandler::old_signal_handler_; | 365 struct sigaction SignalHandler::old_signal_handler_; |
322 bool SignalHandler::signal_handler_installed_ = false; | 366 bool SignalHandler::signal_handler_installed_ = false; |
323 | 367 |
324 | 368 |
325 void SignalHandler::HandleProfilerSignal(int signal, siginfo_t* info, | 369 void SignalHandler::HandleProfilerSignal(int signal, siginfo_t* info, |
326 void* context) { | 370 void* context) { |
327 #if V8_OS_NACL | 371 #if V8_OS_NACL |
328 // As Native Client does not support signal handling, profiling | 372 // As Native Client does not support signal handling, profiling |
329 // is disabled. | 373 // is disabled. |
330 return; | 374 return; |
331 #else | 375 #else |
376 // Even if we return prematurely, | |
377 // we need to signal the sampling_semaphore_ | |
378 sample_available_ = false; | |
379 ScopedSemaphore scoped_semaphore(sampling_semaphore_); | |
380 | |
332 USE(info); | 381 USE(info); |
333 if (signal != SIGPROF) return; | 382 if (signal != SIGPROF) return; |
334 Isolate* isolate = Isolate::UncheckedReentrantCurrent(); | 383 Isolate* isolate = Isolate::UncheckedReentrantCurrent(); |
335 if (isolate == NULL || !isolate->IsInitialized() || !isolate->IsInUse()) { | 384 if (isolate == NULL || !isolate->IsInitialized() || !isolate->IsInUse()) { |
336 // We require a fully initialized and entered isolate. | 385 // We require a fully initialized and entered isolate. |
337 return; | 386 return; |
338 } | 387 } |
339 if (v8::Locker::IsActive() && | 388 if (v8::Locker::IsActive() && |
340 !isolate->thread_manager()->IsLockedByCurrentThread()) { | 389 !isolate->thread_manager()->IsLockedByCurrentThread()) { |
341 return; | 390 return; |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
459 state.pc = reinterpret_cast<Address>(mcontext.cpu.eip); | 508 state.pc = reinterpret_cast<Address>(mcontext.cpu.eip); |
460 state.sp = reinterpret_cast<Address>(mcontext.cpu.esp); | 509 state.sp = reinterpret_cast<Address>(mcontext.cpu.esp); |
461 state.fp = reinterpret_cast<Address>(mcontext.cpu.ebp); | 510 state.fp = reinterpret_cast<Address>(mcontext.cpu.ebp); |
462 #elif V8_HOST_ARCH_ARM | 511 #elif V8_HOST_ARCH_ARM |
463 state.pc = reinterpret_cast<Address>(mcontext.cpu.gpr[ARM_REG_PC]); | 512 state.pc = reinterpret_cast<Address>(mcontext.cpu.gpr[ARM_REG_PC]); |
464 state.sp = reinterpret_cast<Address>(mcontext.cpu.gpr[ARM_REG_SP]); | 513 state.sp = reinterpret_cast<Address>(mcontext.cpu.gpr[ARM_REG_SP]); |
465 state.fp = reinterpret_cast<Address>(mcontext.cpu.gpr[ARM_REG_FP]); | 514 state.fp = reinterpret_cast<Address>(mcontext.cpu.gpr[ARM_REG_FP]); |
466 #endif // V8_HOST_ARCH_* | 515 #endif // V8_HOST_ARCH_* |
467 #endif // V8_OS_QNX | 516 #endif // V8_OS_QNX |
468 #endif // USE_SIMULATOR | 517 #endif // USE_SIMULATOR |
469 sampler->SampleStack(state); | 518 if (called_from_get_sample_) { |
519 sample_.Init(sampler->isolate(), state); | |
520 } else { | |
521 sampler->SampleStack(state); | |
522 } | |
523 sample_available_ = true; | |
470 #endif // V8_OS_NACL | 524 #endif // V8_OS_NACL |
471 } | 525 } |
472 | 526 |
473 #endif | 527 #endif |
474 | 528 |
475 | 529 |
476 class SamplerThread : public base::Thread { | 530 class SamplerThread : public base::Thread { |
477 public: | 531 public: |
478 static const int kSamplerThreadStackSize = 64 * KB; | 532 static const int kSamplerThreadStackSize = 64 * KB; |
479 | 533 |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
562 | 616 |
563 // | 617 // |
564 // StackTracer implementation | 618 // StackTracer implementation |
565 // | 619 // |
566 DISABLE_ASAN void TickSample::Init(Isolate* isolate, | 620 DISABLE_ASAN void TickSample::Init(Isolate* isolate, |
567 const RegisterState& regs) { | 621 const RegisterState& regs) { |
568 ASSERT(isolate->IsInitialized()); | 622 ASSERT(isolate->IsInitialized()); |
569 timestamp = base::TimeTicks::HighResolutionNow(); | 623 timestamp = base::TimeTicks::HighResolutionNow(); |
570 pc = regs.pc; | 624 pc = regs.pc; |
571 state = isolate->current_vm_state(); | 625 state = isolate->current_vm_state(); |
626 frames_count = 0; | |
572 | 627 |
573 // Avoid collecting traces while doing GC. | 628 // Avoid collecting traces while doing GC. |
574 if (state == GC) return; | 629 if (state == GC) return; |
575 | 630 |
576 Address js_entry_sp = isolate->js_entry_sp(); | 631 Address js_entry_sp = isolate->js_entry_sp(); |
577 if (js_entry_sp == 0) { | 632 if (js_entry_sp == 0) { |
578 // Not executing JS now. | 633 // Not executing JS now. |
579 return; | 634 return; |
580 } | 635 } |
581 | 636 |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
686 } | 741 } |
687 | 742 |
688 | 743 |
689 #if defined(USE_SIGNALS) | 744 #if defined(USE_SIGNALS) |
690 | 745 |
691 void Sampler::DoSample() { | 746 void Sampler::DoSample() { |
692 if (!SignalHandler::Installed()) return; | 747 if (!SignalHandler::Installed()) return; |
693 pthread_kill(platform_data()->vm_tid(), SIGPROF); | 748 pthread_kill(platform_data()->vm_tid(), SIGPROF); |
694 } | 749 } |
695 | 750 |
751 | |
752 void Sampler::GetSample(v8::Sample* sample) { | |
753 SignalHandler::called_from_get_sample_ = true; | |
754 pthread_kill(platform_data()->vm_tid(), SIGPROF); | |
755 SignalHandler::sampling_semaphore_->Wait(); | |
756 if (SignalHandler::sample_available_) { | |
757 *sample = v8::Sample(SignalHandler::sample_.stack, | |
Benedikt Meurer
2014/09/05 03:50:00
Nit: Use
v8::Sample(&SignalHandler::sample_.stac
alph
2014/09/08 12:47:40
Note it will always copy all the 255 frames.
The p
gholap
2014/09/08 23:57:38
Done.
gholap
2014/09/08 23:57:38
Done.
| |
758 SignalHandler::sample_.stack | |
759 + SignalHandler::sample_.frames_count); | |
760 } | |
761 SignalHandler::called_from_get_sample_ = false; | |
762 } | |
763 | |
696 #elif V8_OS_WIN || V8_OS_CYGWIN | 764 #elif V8_OS_WIN || V8_OS_CYGWIN |
697 | 765 |
698 void Sampler::DoSample() { | 766 void Sampler::GetSampleHelper(TickSample * sample, |
alph
2014/09/08 12:47:40
nit: it doesn't match the declaration in .h
gholap
2014/09/08 23:57:38
Done.
| |
767 bool called_from_get_sample) { | |
699 HANDLE profiled_thread = platform_data()->profiled_thread(); | 768 HANDLE profiled_thread = platform_data()->profiled_thread(); |
700 if (profiled_thread == NULL) return; | 769 if (profiled_thread == NULL) return NULL; |
701 | 770 |
702 #if defined(USE_SIMULATOR) | 771 #if defined(USE_SIMULATOR) |
703 SimulatorHelper helper; | 772 SimulatorHelper helper; |
704 if (!helper.Init(this, isolate())) return; | 773 if (!helper.Init(this, isolate())) return NULL; |
705 #endif | 774 #endif |
706 | 775 |
707 const DWORD kSuspendFailed = static_cast<DWORD>(-1); | 776 const DWORD kSuspendFailed = static_cast<DWORD>(-1); |
708 if (SuspendThread(profiled_thread) == kSuspendFailed) return; | 777 if (SuspendThread(profiled_thread) == kSuspendFailed) return NULL; |
709 | 778 |
710 // Context used for sampling the register state of the profiled thread. | 779 // Context used for sampling the register state of the profiled thread. |
711 CONTEXT context; | 780 CONTEXT context; |
712 memset(&context, 0, sizeof(context)); | 781 memset(&context, 0, sizeof(context)); |
713 context.ContextFlags = CONTEXT_FULL; | 782 context.ContextFlags = CONTEXT_FULL; |
714 if (GetThreadContext(profiled_thread, &context) != 0) { | 783 if (GetThreadContext(profiled_thread, &context) != 0) { |
715 RegisterState state; | 784 RegisterState state; |
716 #if defined(USE_SIMULATOR) | 785 #if defined(USE_SIMULATOR) |
717 helper.FillRegisters(&state); | 786 helper.FillRegisters(&state); |
718 #else | 787 #else |
719 #if V8_HOST_ARCH_X64 | 788 #if V8_HOST_ARCH_X64 |
720 state.pc = reinterpret_cast<Address>(context.Rip); | 789 state.pc = reinterpret_cast<Address>(context.Rip); |
721 state.sp = reinterpret_cast<Address>(context.Rsp); | 790 state.sp = reinterpret_cast<Address>(context.Rsp); |
722 state.fp = reinterpret_cast<Address>(context.Rbp); | 791 state.fp = reinterpret_cast<Address>(context.Rbp); |
723 #else | 792 #else |
724 state.pc = reinterpret_cast<Address>(context.Eip); | 793 state.pc = reinterpret_cast<Address>(context.Eip); |
725 state.sp = reinterpret_cast<Address>(context.Esp); | 794 state.sp = reinterpret_cast<Address>(context.Esp); |
726 state.fp = reinterpret_cast<Address>(context.Ebp); | 795 state.fp = reinterpret_cast<Address>(context.Ebp); |
727 #endif | 796 #endif |
728 #endif // USE_SIMULATOR | 797 #endif // USE_SIMULATOR |
729 SampleStack(state); | 798 if (called_from_get_sample) { |
799 sample->Init(sampler->isolate(), state); | |
alph
2014/09/08 12:47:40
hmm... what is sampler?
Does it compile on Win?
gholap
2014/09/08 23:57:38
Corrected.
| |
800 } else { | |
801 SampleStack(state); | |
802 } | |
730 } | 803 } |
731 ResumeThread(profiled_thread); | 804 ResumeThread(profiled_thread); |
732 } | 805 } |
733 | 806 |
807 | |
808 void Sampler::DoSample() { | |
809 GetSampleHelper(NULL, false); | |
810 } | |
811 | |
812 | |
813 void Sampler::GetSample(v8::Sample* sample) { | |
814 // We need this tick_sample because, | |
815 // GetSampleHelper calls Init, which must be called on | |
816 // a TickSample and never on Sample. | |
817 TickSample tick_sample; | |
818 sample->frames_count = 0; | |
819 GetSampleHelper(&tick_sample, true); | |
820 *sample = v8::Sample(tick_sample.stack, | |
Benedikt Meurer
2014/09/05 03:50:00
Nit: Use
v8::Sample(&tick_sample.stack[0], &tick
gholap
2014/09/08 23:57:38
Done.
| |
821 tick_sample.stack + tick_sample.frames_count); | |
822 } | |
823 | |
734 #endif // USE_SIGNALS | 824 #endif // USE_SIGNALS |
735 | 825 |
736 | 826 |
737 } } // namespace v8::internal | 827 } } // namespace v8::internal |
OLD | NEW |