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 | 4 |
| 5 #ifndef V8_SAMPLER_H_ | 5 #ifndef V8_SAMPLER_H_ |
| 6 #define V8_SAMPLER_H_ | 6 #define V8_SAMPLER_H_ |
| 7 | 7 |
| 8 #include "include/v8.h" | |
| 9 | |
| 8 #include "src/base/atomicops.h" | 10 #include "src/base/atomicops.h" |
| 9 #include "src/frames.h" | 11 #include "src/frames.h" |
| 10 #include "src/globals.h" | 12 #include "src/globals.h" |
| 11 | 13 |
| 14 #if V8_OS_WIN || V8_OS_CYGWIN | |
| 15 #include "src/base/win32-headers.h" | |
| 16 #endif | |
| 17 | |
| 12 namespace v8 { | 18 namespace v8 { |
| 13 namespace internal { | 19 namespace internal { |
| 14 | 20 |
| 15 class Isolate; | 21 class Isolate; |
| 22 class SignalHandler; | |
|
alph
2014/09/17 11:55:43
seems to be unused in the header.
| |
| 23 | |
| 24 class ScopedSemaphore { | |
| 25 public: | |
| 26 explicit ScopedSemaphore(base::Semaphore* sem) {semaphore_ = sem;} | |
|
alph
2014/09/17 11:55:43
nit: should be an initializer.
| |
| 27 ~ScopedSemaphore() {semaphore_->Signal();} | |
| 28 private: | |
| 29 base::Semaphore* semaphore_; | |
| 30 }; | |
| 31 | |
| 16 | 32 |
| 17 // ---------------------------------------------------------------------------- | 33 // ---------------------------------------------------------------------------- |
| 18 // Sampler | 34 // Sampler |
| 19 // | 35 // |
| 20 // A sampler periodically samples the state of the VM and optionally | 36 // A sampler periodically samples the state of the VM and optionally |
| 21 // (if used for profiling) the program counter and stack pointer for | 37 // (if used for profiling) the program counter and stack pointer for |
| 22 // the thread that created it. | 38 // the thread that created it. |
| 23 | 39 |
| 24 struct RegisterState { | 40 struct RegisterState { |
| 25 RegisterState() : pc(NULL), sp(NULL), fp(NULL) {} | 41 RegisterState() : pc(NULL), sp(NULL), fp(NULL) {} |
| 26 Address pc; // Instruction pointer. | 42 Address pc; // Instruction pointer. |
| 27 Address sp; // Stack pointer. | 43 Address sp; // Stack pointer. |
| 28 Address fp; // Frame pointer. | 44 Address fp; // Frame pointer. |
| 29 }; | 45 }; |
| 30 | 46 |
| 31 // TickSample captures the information collected for each sample. | 47 // TickSample captures the information collected for each sample. |
| 32 struct TickSample { | 48 struct TickSample { |
| 33 TickSample() | 49 TickSample() |
| 34 : state(OTHER), | 50 : state(OTHER), |
| 35 pc(NULL), | 51 pc(NULL), |
| 36 external_callback(NULL), | 52 external_callback(NULL), |
| 37 frames_count(0), | |
| 38 has_external_callback(false), | 53 has_external_callback(false), |
| 39 top_frame_type(StackFrame::NONE) {} | 54 top_frame_type(StackFrame::NONE) {} |
| 40 void Init(Isolate* isolate, const RegisterState& state); | 55 void Init(Isolate* isolate, const RegisterState& state); |
| 41 StateTag state; // The state of the VM. | 56 enum { kMaxFramesCount = 255u }; |
| 42 Address pc; // Instruction pointer. | 57 |
| 58 void* stack[kMaxFramesCount]; // Call stack. | |
| 59 size_t frames_count; // Number of captured frames. | |
| 60 StateTag state; // The state of the VM. | |
| 61 Address pc; // Instruction pointer. | |
| 43 union { | 62 union { |
| 44 Address tos; // Top stack value (*sp). | 63 Address tos; // Top stack value (*sp). |
| 45 Address external_callback; | 64 Address external_callback; |
| 46 }; | 65 }; |
| 47 static const unsigned kMaxFramesCountLog2 = 8; | |
| 48 static const unsigned kMaxFramesCount = (1 << kMaxFramesCountLog2) - 1; | |
| 49 Address stack[kMaxFramesCount]; // Call stack. | |
| 50 base::TimeTicks timestamp; | 66 base::TimeTicks timestamp; |
| 51 unsigned frames_count : kMaxFramesCountLog2; // Number of captured frames. | |
| 52 bool has_external_callback : 1; | 67 bool has_external_callback : 1; |
| 53 StackFrame::Type top_frame_type : 4; | 68 StackFrame::Type top_frame_type : 4; |
| 54 }; | 69 }; |
| 55 | 70 |
| 56 class Sampler { | 71 class Sampler { |
| 57 public: | 72 public: |
| 58 // Initializes the Sampler support. Called once at VM startup. | 73 // Initializes the Sampler support. Called once at VM startup. |
| 59 static void SetUp(); | 74 static void SetUp(); |
| 60 static void TearDown(); | 75 static void TearDown(); |
| 61 | 76 |
| 77 // For new sampling API, initialize and install just the SignalHandler | |
| 78 static void SetUpForNewSamplingAPI(); | |
|
alph
2014/09/17 11:55:43
Avoid using words like 'old' and 'new' in the func
| |
| 79 | |
| 62 // Initialize sampler. | 80 // Initialize sampler. |
| 63 Sampler(Isolate* isolate, int interval); | 81 Sampler(Isolate* isolate, int interval); |
| 64 virtual ~Sampler(); | 82 virtual ~Sampler(); |
| 65 | 83 |
| 66 Isolate* isolate() const { return isolate_; } | 84 Isolate* isolate() const { return isolate_; } |
| 67 int interval() const { return interval_; } | 85 int interval() const { return interval_; } |
| 68 | 86 |
| 69 // Performs stack sampling. | 87 // Performs stack sampling. |
| 70 void SampleStack(const RegisterState& regs); | 88 void SampleStack(const RegisterState& regs); |
| 71 | 89 |
| 72 // Start and stop sampler. | 90 // Start and stop sampler. |
| 73 void Start(); | 91 void Start(); |
| 74 void Stop(); | 92 void Stop(); |
| 75 | 93 |
| 76 // Whether the sampling thread should use this Sampler for CPU profiling? | 94 // Whether the sampling thread should use this Sampler for CPU profiling? |
| 77 bool IsProfiling() const { | 95 bool IsProfiling() const { |
| 78 return base::NoBarrier_Load(&profiling_) > 0 && | 96 return base::NoBarrier_Load(&profiling_) > 0 && |
| 79 !base::NoBarrier_Load(&has_processing_thread_); | 97 !base::NoBarrier_Load(&has_processing_thread_); |
| 80 } | 98 } |
| 81 void IncreaseProfilingDepth(); | 99 void IncreaseProfilingDepth(); |
| 82 void DecreaseProfilingDepth(); | 100 void DecreaseProfilingDepth(); |
| 83 | 101 |
| 84 // Whether the sampler is running (that is, consumes resources). | 102 // Whether the sampler is running (that is, consumes resources). |
| 85 bool IsActive() const { return base::NoBarrier_Load(&active_); } | 103 bool IsActive() const { return base::NoBarrier_Load(&active_); } |
| 86 | 104 |
| 87 void DoSample(); | 105 void DoSample(); |
| 106 #if V8_OS_POSIX && !V8_OS_CYGWIN | |
| 107 static void GetSample(v8::Sample* sample); | |
| 108 #elif V8_OS_WIN || V8_OS_CYGWIN | |
| 109 static void GetSample(Isolate* isolate, | |
| 110 const CONTEXT& context, | |
| 111 v8::Sample* sample); | |
| 112 #endif | |
| 88 // If true next sample must be initiated on the profiler event processor | 113 // If true next sample must be initiated on the profiler event processor |
| 89 // thread right after latest sample is processed. | 114 // thread right after latest sample is processed. |
| 90 void SetHasProcessingThread(bool value) { | 115 void SetHasProcessingThread(bool value) { |
| 91 base::NoBarrier_Store(&has_processing_thread_, value); | 116 base::NoBarrier_Store(&has_processing_thread_, value); |
| 92 } | 117 } |
| 93 | 118 |
| 94 // Used in tests to make sure that stack sampling is performed. | 119 // Used in tests to make sure that stack sampling is performed. |
| 95 unsigned js_and_external_sample_count() const { | 120 unsigned js_and_external_sample_count() const { |
| 96 return js_and_external_sample_count_; | 121 return js_and_external_sample_count_; |
| 97 } | 122 } |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 120 bool is_counting_samples_; | 145 bool is_counting_samples_; |
| 121 // Counts stack samples taken in JS VM state. | 146 // Counts stack samples taken in JS VM state. |
| 122 unsigned js_and_external_sample_count_; | 147 unsigned js_and_external_sample_count_; |
| 123 DISALLOW_IMPLICIT_CONSTRUCTORS(Sampler); | 148 DISALLOW_IMPLICIT_CONSTRUCTORS(Sampler); |
| 124 }; | 149 }; |
| 125 | 150 |
| 126 | 151 |
| 127 } } // namespace v8::internal | 152 } } // namespace v8::internal |
| 128 | 153 |
| 129 #endif // V8_SAMPLER_H_ | 154 #endif // V8_SAMPLER_H_ |
| OLD | NEW |