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 206 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
686 } | 740 } |
687 | 741 |
688 | 742 |
689 #if defined(USE_SIGNALS) | 743 #if defined(USE_SIGNALS) |
690 | 744 |
691 void Sampler::DoSample() { | 745 void Sampler::DoSample() { |
692 if (!SignalHandler::Installed()) return; | 746 if (!SignalHandler::Installed()) return; |
693 pthread_kill(platform_data()->vm_tid(), SIGPROF); | 747 pthread_kill(platform_data()->vm_tid(), SIGPROF); |
694 } | 748 } |
695 | 749 |
| 750 |
| 751 void Sampler::GetSample(v8::Sample* sample) { |
| 752 sample->frames_count = 0; |
| 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 = SignalHandler::sample_; |
| 758 } |
| 759 SignalHandler::called_from_get_sample_ = false; |
| 760 } |
| 761 |
696 #elif V8_OS_WIN || V8_OS_CYGWIN | 762 #elif V8_OS_WIN || V8_OS_CYGWIN |
697 | 763 |
698 void Sampler::DoSample() { | 764 void Sampler::GetSampleHelper(TickSample * sample, |
| 765 bool called_from_get_sample) { |
699 HANDLE profiled_thread = platform_data()->profiled_thread(); | 766 HANDLE profiled_thread = platform_data()->profiled_thread(); |
700 if (profiled_thread == NULL) return; | 767 if (profiled_thread == NULL) return NULL; |
701 | 768 |
702 #if defined(USE_SIMULATOR) | 769 #if defined(USE_SIMULATOR) |
703 SimulatorHelper helper; | 770 SimulatorHelper helper; |
704 if (!helper.Init(this, isolate())) return; | 771 if (!helper.Init(this, isolate())) return NULL; |
705 #endif | 772 #endif |
706 | 773 |
707 const DWORD kSuspendFailed = static_cast<DWORD>(-1); | 774 const DWORD kSuspendFailed = static_cast<DWORD>(-1); |
708 if (SuspendThread(profiled_thread) == kSuspendFailed) return; | 775 if (SuspendThread(profiled_thread) == kSuspendFailed) return NULL; |
709 | 776 |
710 // Context used for sampling the register state of the profiled thread. | 777 // Context used for sampling the register state of the profiled thread. |
711 CONTEXT context; | 778 CONTEXT context; |
712 memset(&context, 0, sizeof(context)); | 779 memset(&context, 0, sizeof(context)); |
713 context.ContextFlags = CONTEXT_FULL; | 780 context.ContextFlags = CONTEXT_FULL; |
714 if (GetThreadContext(profiled_thread, &context) != 0) { | 781 if (GetThreadContext(profiled_thread, &context) != 0) { |
715 RegisterState state; | 782 RegisterState state; |
716 #if defined(USE_SIMULATOR) | 783 #if defined(USE_SIMULATOR) |
717 helper.FillRegisters(&state); | 784 helper.FillRegisters(&state); |
718 #else | 785 #else |
719 #if V8_HOST_ARCH_X64 | 786 #if V8_HOST_ARCH_X64 |
720 state.pc = reinterpret_cast<Address>(context.Rip); | 787 state.pc = reinterpret_cast<Address>(context.Rip); |
721 state.sp = reinterpret_cast<Address>(context.Rsp); | 788 state.sp = reinterpret_cast<Address>(context.Rsp); |
722 state.fp = reinterpret_cast<Address>(context.Rbp); | 789 state.fp = reinterpret_cast<Address>(context.Rbp); |
723 #else | 790 #else |
724 state.pc = reinterpret_cast<Address>(context.Eip); | 791 state.pc = reinterpret_cast<Address>(context.Eip); |
725 state.sp = reinterpret_cast<Address>(context.Esp); | 792 state.sp = reinterpret_cast<Address>(context.Esp); |
726 state.fp = reinterpret_cast<Address>(context.Ebp); | 793 state.fp = reinterpret_cast<Address>(context.Ebp); |
727 #endif | 794 #endif |
728 #endif // USE_SIMULATOR | 795 #endif // USE_SIMULATOR |
729 SampleStack(state); | 796 if (called_from_get_sample) { |
| 797 sample->Init(sampler->isolate(), state); |
| 798 } else { |
| 799 SampleStack(state); |
| 800 } |
730 } | 801 } |
731 ResumeThread(profiled_thread); | 802 ResumeThread(profiled_thread); |
732 } | 803 } |
733 | 804 |
| 805 |
| 806 void Sampler::DoSample() { |
| 807 GetSampleHelper(NULL, false); |
| 808 } |
| 809 |
| 810 |
| 811 void Sampler::GetSample(v8::Sample* sample) { |
| 812 // We need this tick_sample because, |
| 813 // GetSampleHelper calls Init, which must be called on |
| 814 // a TickSample and never on Sample. |
| 815 TickSample tick_sample; |
| 816 sample->frames_count = 0; |
| 817 GetSampleHelper(&tick_sample, true); |
| 818 *sample = tick_sample; |
| 819 } |
| 820 |
734 #endif // USE_SIGNALS | 821 #endif // USE_SIGNALS |
735 | 822 |
736 | 823 |
737 } } // namespace v8::internal | 824 } } // namespace v8::internal |
OLD | NEW |