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