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

Side by Side Diff: src/sampler.cc

Issue 422593003: Initial GetSample implementation. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Fixed variable naming nits. 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
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
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
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
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
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 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
679 ++js_and_external_sample_count_; 733 ++js_and_external_sample_count_;
680 } 734 }
681 } 735 }
682 Tick(sample); 736 Tick(sample);
683 if (sample != &sample_obj) { 737 if (sample != &sample_obj) {
684 isolate_->cpu_profiler()->FinishTickSample(); 738 isolate_->cpu_profiler()->FinishTickSample();
685 } 739 }
686 } 740 }
687 741
688 742
743 void Sampler::CopyTickSampleToSample(TickSample* tick_sample,
Sven Panne 2014/09/01 08:35:58 You don't need this, normal assignment will do the
gholap 2014/09/02 07:43:49 Done.
744 v8::Sample* sample) {
745 for (unsigned i = 0; i < tick_sample->frames_count; i++) {
746 sample->stack[i] = tick_sample->stack[i];
747 }
748 sample->frames_count = tick_sample->frames_count;
749 }
750
751
689 #if defined(USE_SIGNALS) 752 #if defined(USE_SIGNALS)
690 753
691 void Sampler::DoSample() { 754 void Sampler::DoSample() {
692 if (!SignalHandler::Installed()) return; 755 if (!SignalHandler::Installed()) return;
693 pthread_kill(platform_data()->vm_tid(), SIGPROF); 756 pthread_kill(platform_data()->vm_tid(), SIGPROF);
694 } 757 }
695 758
759
760 void Sampler::GetSample(v8::Sample* sample) {
761 sample->frames_count = 0;
762 SignalHandler::called_from_get_sample_ = true;
763 pthread_kill(platform_data()->vm_tid(), SIGPROF);
764 SignalHandler::sampling_semaphore_->Wait();
765 if (SignalHandler::sample_available_) {
766 CopyTickSampleToSample(&SignalHandler::sample_, sample);
767 }
768 SignalHandler::called_from_get_sample_ = false;
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 void 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 sample->frames_count = 0;
826 GetSampleHelper(&tick_sample, true);
827 CopyTickSampleToSample(&tick_sample, sample);
828 }
829
734 #endif // USE_SIGNALS 830 #endif // USE_SIGNALS
735 831
736 832
737 } } // namespace v8::internal 833 } } // namespace v8::internal
OLDNEW
« include/v8.h ('K') | « src/sampler.h ('k') | test/cctest/cctest.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698