mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 1 | // Copyright 2013 the V8 project authors. All rights reserved. |
rossberg@chromium.org | 3484964 | 2014-04-29 16:30:47 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 4 | |
| 5 | #ifndef V8_SAMPLER_H_ |
| 6 | #define V8_SAMPLER_H_ |
| 7 | |
machenbach@chromium.org | 1e2d50c | 2014-06-06 00:04:56 +0000 | [diff] [blame] | 8 | #include "src/base/atomicops.h" |
machenbach@chromium.org | 196eb60 | 2014-06-04 00:06:13 +0000 | [diff] [blame] | 9 | #include "src/frames.h" |
| 10 | #include "src/globals.h" |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 11 | |
| 12 | namespace v8 { |
| 13 | namespace internal { |
| 14 | |
| 15 | class Isolate; |
| 16 | |
| 17 | // ---------------------------------------------------------------------------- |
| 18 | // Sampler |
| 19 | // |
| 20 | // A sampler periodically samples the state of the VM and optionally |
| 21 | // (if used for profiling) the program counter and stack pointer for |
| 22 | // the thread that created it. |
| 23 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 24 | struct RegisterState { |
| 25 | RegisterState() : pc(NULL), sp(NULL), fp(NULL) {} |
| 26 | Address pc; // Instruction pointer. |
| 27 | Address sp; // Stack pointer. |
| 28 | Address fp; // Frame pointer. |
| 29 | }; |
| 30 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 31 | // TickSample captures the information collected for each sample. |
| 32 | struct TickSample { |
| 33 | TickSample() |
| 34 | : state(OTHER), |
| 35 | pc(NULL), |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 36 | external_callback(NULL), |
ulan@chromium.org | 77ca49a | 2013-04-22 09:43:56 +0000 | [diff] [blame] | 37 | frames_count(0), |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 38 | has_external_callback(false), |
| 39 | top_frame_type(StackFrame::NONE) {} |
| 40 | void Init(Isolate* isolate, const RegisterState& state); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 41 | StateTag state; // The state of the VM. |
| 42 | Address pc; // Instruction pointer. |
ulan@chromium.org | 77ca49a | 2013-04-22 09:43:56 +0000 | [diff] [blame] | 43 | union { |
| 44 | Address tos; // Top stack value (*sp). |
| 45 | Address external_callback; |
| 46 | }; |
machenbach@chromium.org | 08e7569 | 2014-06-25 00:05:37 +0000 | [diff] [blame] | 47 | static const unsigned kMaxFramesCountLog2 = 8; |
| 48 | static const unsigned kMaxFramesCount = (1 << kMaxFramesCountLog2) - 1; |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 49 | Address stack[kMaxFramesCount]; // Call stack. |
yangguo@chromium.org | 5de0074 | 2014-07-01 11:58:10 +0000 | [diff] [blame] | 50 | base::TimeTicks timestamp; |
machenbach@chromium.org | 08e7569 | 2014-06-25 00:05:37 +0000 | [diff] [blame] | 51 | unsigned frames_count : kMaxFramesCountLog2; // Number of captured frames. |
ulan@chromium.org | 77ca49a | 2013-04-22 09:43:56 +0000 | [diff] [blame] | 52 | bool has_external_callback : 1; |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 53 | StackFrame::Type top_frame_type : 4; |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
| 56 | class Sampler { |
| 57 | public: |
| 58 | // Initializes the Sampler support. Called once at VM startup. |
| 59 | static void SetUp(); |
| 60 | static void TearDown(); |
| 61 | |
| 62 | // Initialize sampler. |
| 63 | Sampler(Isolate* isolate, int interval); |
| 64 | virtual ~Sampler(); |
| 65 | |
| 66 | Isolate* isolate() const { return isolate_; } |
| 67 | int interval() const { return interval_; } |
| 68 | |
| 69 | // Performs stack sampling. |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 70 | void SampleStack(const RegisterState& regs); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 71 | |
| 72 | // Start and stop sampler. |
| 73 | void Start(); |
| 74 | void Stop(); |
| 75 | |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 76 | // Whether the sampling thread should use this Sampler for CPU profiling? |
| 77 | bool IsProfiling() const { |
machenbach@chromium.org | 1e2d50c | 2014-06-06 00:04:56 +0000 | [diff] [blame] | 78 | return base::NoBarrier_Load(&profiling_) > 0 && |
| 79 | !base::NoBarrier_Load(&has_processing_thread_); |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 80 | } |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 81 | void IncreaseProfilingDepth(); |
| 82 | void DecreaseProfilingDepth(); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 83 | |
| 84 | // Whether the sampler is running (that is, consumes resources). |
machenbach@chromium.org | 1e2d50c | 2014-06-06 00:04:56 +0000 | [diff] [blame] | 85 | bool IsActive() const { return base::NoBarrier_Load(&active_); } |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 86 | |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 87 | void DoSample(); |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 88 | // If true next sample must be initiated on the profiler event processor |
| 89 | // thread right after latest sample is processed. |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 90 | void SetHasProcessingThread(bool value) { |
machenbach@chromium.org | 1e2d50c | 2014-06-06 00:04:56 +0000 | [diff] [blame] | 91 | base::NoBarrier_Store(&has_processing_thread_, value); |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 92 | } |
| 93 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 94 | // Used in tests to make sure that stack sampling is performed. |
danno@chromium.org | bee5199 | 2013-07-10 14:57:15 +0000 | [diff] [blame] | 95 | unsigned js_and_external_sample_count() const { |
| 96 | return js_and_external_sample_count_; |
| 97 | } |
| 98 | void StartCountingSamples() { |
| 99 | is_counting_samples_ = true; |
| 100 | js_and_external_sample_count_ = 0; |
| 101 | } |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 102 | |
| 103 | class PlatformData; |
| 104 | PlatformData* platform_data() const { return data_; } |
| 105 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 106 | protected: |
| 107 | // This method is called for each sampling period with the current |
| 108 | // program counter. |
| 109 | virtual void Tick(TickSample* sample) = 0; |
| 110 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 111 | private: |
machenbach@chromium.org | 1e2d50c | 2014-06-06 00:04:56 +0000 | [diff] [blame] | 112 | void SetActive(bool value) { base::NoBarrier_Store(&active_, value); } |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 113 | |
| 114 | Isolate* isolate_; |
| 115 | const int interval_; |
machenbach@chromium.org | 1e2d50c | 2014-06-06 00:04:56 +0000 | [diff] [blame] | 116 | base::Atomic32 profiling_; |
| 117 | base::Atomic32 has_processing_thread_; |
| 118 | base::Atomic32 active_; |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 119 | PlatformData* data_; // Platform specific data. |
danno@chromium.org | bee5199 | 2013-07-10 14:57:15 +0000 | [diff] [blame] | 120 | bool is_counting_samples_; |
| 121 | // Counts stack samples taken in JS VM state. |
| 122 | unsigned js_and_external_sample_count_; |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 123 | DISALLOW_IMPLICIT_CONSTRUCTORS(Sampler); |
| 124 | }; |
| 125 | |
| 126 | |
| 127 | } } // namespace v8::internal |
| 128 | |
| 129 | #endif // V8_SAMPLER_H_ |