Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 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 |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef V8_PROFILER_SAMPLER_H_ |
| 6 | #define V8_PROFILER_SAMPLER_H_ |
| 7 | |
| 8 | #include "include/v8.h" |
| 9 | |
| 10 | #include "src/base/atomicops.h" |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 11 | #include "src/base/macros.h" |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 12 | |
| 13 | namespace v8 { |
| 14 | namespace internal { |
| 15 | |
| 16 | class Isolate; |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 17 | struct TickSample; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 18 | |
| 19 | // ---------------------------------------------------------------------------- |
| 20 | // Sampler |
| 21 | // |
| 22 | // A sampler periodically samples the state of the VM and optionally |
| 23 | // (if used for profiling) the program counter and stack pointer for |
| 24 | // the thread that created it. |
| 25 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 26 | class Sampler { |
| 27 | public: |
| 28 | // Initializes the Sampler support. Called once at VM startup. |
| 29 | static void SetUp(); |
| 30 | static void TearDown(); |
| 31 | |
| 32 | // Initialize sampler. |
| 33 | Sampler(Isolate* isolate, int interval); |
| 34 | virtual ~Sampler(); |
| 35 | |
| 36 | Isolate* isolate() const { return isolate_; } |
| 37 | int interval() const { return interval_; } |
| 38 | |
| 39 | // Performs stack sampling. |
| 40 | void SampleStack(const v8::RegisterState& regs); |
| 41 | |
| 42 | // Start and stop sampler. |
| 43 | void Start(); |
| 44 | void Stop(); |
| 45 | |
| 46 | // Whether the sampling thread should use this Sampler for CPU profiling? |
| 47 | bool IsProfiling() const { |
| 48 | return base::NoBarrier_Load(&profiling_) > 0 && |
| 49 | !base::NoBarrier_Load(&has_processing_thread_); |
| 50 | } |
| 51 | void IncreaseProfilingDepth(); |
| 52 | void DecreaseProfilingDepth(); |
| 53 | |
| 54 | // Whether the sampler is running (that is, consumes resources). |
| 55 | bool IsActive() const { return base::NoBarrier_Load(&active_); } |
| 56 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 57 | // CpuProfiler collects samples by calling DoSample directly |
| 58 | // without calling Start. To keep it working, we register the sampler |
| 59 | // with the CpuProfiler. |
| 60 | bool IsRegistered() const { return base::NoBarrier_Load(®istered_); } |
| 61 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 62 | void DoSample(); |
| 63 | // If true next sample must be initiated on the profiler event processor |
| 64 | // thread right after latest sample is processed. |
| 65 | void SetHasProcessingThread(bool value) { |
| 66 | base::NoBarrier_Store(&has_processing_thread_, value); |
| 67 | } |
| 68 | |
| 69 | // Used in tests to make sure that stack sampling is performed. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 70 | unsigned js_sample_count() const { return js_sample_count_; } |
| 71 | unsigned external_sample_count() const { return external_sample_count_; } |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 72 | void StartCountingSamples() { |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 73 | js_sample_count_ = 0; |
| 74 | external_sample_count_ = 0; |
| 75 | is_counting_samples_ = true; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | class PlatformData; |
| 79 | PlatformData* platform_data() const { return data_; } |
| 80 | |
| 81 | protected: |
| 82 | // This method is called for each sampling period with the current |
| 83 | // program counter. |
| 84 | virtual void Tick(TickSample* sample) = 0; |
| 85 | |
| 86 | private: |
| 87 | void SetActive(bool value) { base::NoBarrier_Store(&active_, value); } |
| 88 | |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 89 | void SetRegistered(bool value) { base::NoBarrier_Store(®istered_, value); } |
| 90 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 91 | Isolate* isolate_; |
| 92 | const int interval_; |
| 93 | base::Atomic32 profiling_; |
| 94 | base::Atomic32 has_processing_thread_; |
| 95 | base::Atomic32 active_; |
Ben Murdoch | c561043 | 2016-08-08 18:44:38 +0100 | [diff] [blame^] | 96 | base::Atomic32 registered_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 97 | PlatformData* data_; // Platform specific data. |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 98 | // Counts stack samples taken in various VM states. |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 99 | bool is_counting_samples_; |
Ben Murdoch | 097c5b2 | 2016-05-18 11:27:45 +0100 | [diff] [blame] | 100 | unsigned js_sample_count_; |
| 101 | unsigned external_sample_count_; |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 102 | DISALLOW_IMPLICIT_CONSTRUCTORS(Sampler); |
| 103 | }; |
| 104 | |
Ben Murdoch | 4a90d5f | 2016-03-22 12:00:34 +0000 | [diff] [blame] | 105 | } // namespace internal |
| 106 | } // namespace v8 |
| 107 | |
| 108 | #endif // V8_PROFILER_SAMPLER_H_ |