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/platform/time.h" | 11 #include "src/base/macros.h" |
12 #include "src/frames.h" | |
13 #include "src/globals.h" | |
14 | 12 |
15 namespace v8 { | 13 namespace v8 { |
16 namespace internal { | 14 namespace sampler { |
17 | |
18 class Isolate; | |
19 | 15 |
20 // ---------------------------------------------------------------------------- | 16 // ---------------------------------------------------------------------------- |
21 // Sampler | 17 // Sampler |
22 // | 18 // |
23 // A sampler periodically samples the state of the VM and optionally | 19 // A sampler periodically samples the state of the VM and optionally |
24 // (if used for profiling) the program counter and stack pointer for | 20 // (if used for profiling) the program counter and stack pointer for |
25 // the thread that created it. | 21 // the thread that created it. |
26 | 22 |
27 // TickSample captures the information collected for each sample. | |
28 struct TickSample { | |
29 // Internal profiling (with --prof + tools/$OS-tick-processor) wants to | |
30 // include the runtime function we're calling. Externally exposed tick | |
31 // samples don't care. | |
32 enum RecordCEntryFrame { kIncludeCEntryFrame, kSkipCEntryFrame }; | |
33 | |
34 TickSample() | |
35 : state(OTHER), | |
36 pc(NULL), | |
37 external_callback_entry(NULL), | |
38 frames_count(0), | |
39 has_external_callback(false), | |
40 update_stats(true), | |
41 top_frame_type(StackFrame::NONE) {} | |
42 void Init(Isolate* isolate, const v8::RegisterState& state, | |
43 RecordCEntryFrame record_c_entry_frame, bool update_stats); | |
44 static void GetStackSample(Isolate* isolate, const v8::RegisterState& state, | |
45 RecordCEntryFrame record_c_entry_frame, | |
46 void** frames, size_t frames_limit, | |
47 v8::SampleInfo* sample_info); | |
48 StateTag state; // The state of the VM. | |
49 Address pc; // Instruction pointer. | |
50 union { | |
51 Address tos; // Top stack value (*sp). | |
52 Address external_callback_entry; | |
53 }; | |
54 static const unsigned kMaxFramesCountLog2 = 8; | |
55 static const unsigned kMaxFramesCount = (1 << kMaxFramesCountLog2) - 1; | |
56 Address stack[kMaxFramesCount]; // Call stack. | |
57 base::TimeTicks timestamp; | |
58 unsigned frames_count : kMaxFramesCountLog2; // Number of captured frames. | |
59 bool has_external_callback : 1; | |
60 bool update_stats : 1; // Whether the sample should update aggregated stats. | |
61 StackFrame::Type top_frame_type : 5; | |
62 }; | |
63 | |
64 class Sampler { | 23 class Sampler { |
65 public: | 24 public: |
| 25 static const int kMaxFramesCountLog2 = 8; |
| 26 static const unsigned kMaxFramesCount = (1u << kMaxFramesCountLog2) - 1; |
| 27 |
66 // Initializes the Sampler support. Called once at VM startup. | 28 // Initializes the Sampler support. Called once at VM startup. |
67 static void SetUp(); | 29 static void SetUp(); |
68 static void TearDown(); | 30 static void TearDown(); |
69 | 31 |
70 // Initialize sampler. | 32 // Initialize sampler. |
71 Sampler(Isolate* isolate, int interval); | 33 explicit Sampler(Isolate* isolate); |
72 virtual ~Sampler(); | 34 virtual ~Sampler(); |
73 | 35 |
74 Isolate* isolate() const { return isolate_; } | 36 Isolate* isolate() const { return isolate_; } |
75 int interval() const { return interval_; } | |
76 | 37 |
77 // Performs stack sampling. | 38 // Performs stack sampling. |
78 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; |
79 | 42 |
80 // Start and stop sampler. | 43 // Start and stop sampler. |
81 void Start(); | 44 void Start(); |
82 void Stop(); | 45 void Stop(); |
83 | 46 |
84 // Whether the sampling thread should use this Sampler for CPU profiling? | 47 // Whether the sampling thread should use this Sampler for CPU profiling? |
85 bool IsProfiling() const { | 48 bool IsProfiling() const { |
86 return base::NoBarrier_Load(&profiling_) > 0 && | 49 return base::NoBarrier_Load(&profiling_) > 0; |
87 !base::NoBarrier_Load(&has_processing_thread_); | |
88 } | 50 } |
89 void IncreaseProfilingDepth(); | 51 void IncreaseProfilingDepth(); |
90 void DecreaseProfilingDepth(); | 52 void DecreaseProfilingDepth(); |
91 | 53 |
92 // Whether the sampler is running (that is, consumes resources). | 54 // Whether the sampler is running (that is, consumes resources). |
93 bool IsActive() const { return base::NoBarrier_Load(&active_); } | 55 bool IsActive() const { return base::NoBarrier_Load(&active_); } |
94 | 56 |
95 // CpuProfiler collects samples by calling DoSample directly | |
96 // without calling Start. To keep it working, we register the sampler | |
97 // with the CpuProfiler. | |
98 bool IsRegistered() const { return base::NoBarrier_Load(®istered_); } | |
99 | |
100 void DoSample(); | 57 void DoSample(); |
101 // If true next sample must be initiated on the profiler event processor | |
102 // thread right after latest sample is processed. | |
103 void SetHasProcessingThread(bool value) { | |
104 base::NoBarrier_Store(&has_processing_thread_, value); | |
105 } | |
106 | 58 |
107 // Used in tests to make sure that stack sampling is performed. | 59 // Used in tests to make sure that stack sampling is performed. |
108 unsigned js_sample_count() const { return js_sample_count_; } | 60 unsigned js_sample_count() const { return js_sample_count_; } |
109 unsigned external_sample_count() const { return external_sample_count_; } | 61 unsigned external_sample_count() const { return external_sample_count_; } |
110 void StartCountingSamples() { | 62 void StartCountingSamples() { |
111 js_sample_count_ = 0; | 63 js_sample_count_ = 0; |
112 external_sample_count_ = 0; | 64 external_sample_count_ = 0; |
113 is_counting_samples_ = true; | 65 is_counting_samples_ = true; |
114 } | 66 } |
115 | 67 |
116 class PlatformData; | 68 class PlatformData; |
117 PlatformData* platform_data() const { return data_; } | 69 PlatformData* platform_data() const { return data_; } |
118 | 70 |
119 protected: | 71 protected: |
120 // This method is called for each sampling period with the current | 72 // Counts stack samples taken in various VM states. |
121 // program counter. | 73 bool is_counting_samples_; |
122 virtual void Tick(TickSample* sample) = 0; | 74 unsigned js_sample_count_; |
| 75 unsigned external_sample_count_; |
123 | 76 |
124 private: | 77 private: |
125 void SetActive(bool value) { base::NoBarrier_Store(&active_, value); } | 78 void SetActive(bool value) { base::NoBarrier_Store(&active_, value); } |
126 | 79 |
127 void SetRegistered(bool value) { base::NoBarrier_Store(®istered_, value); } | |
128 | |
129 Isolate* isolate_; | 80 Isolate* isolate_; |
130 const int interval_; | |
131 base::Atomic32 profiling_; | 81 base::Atomic32 profiling_; |
132 base::Atomic32 has_processing_thread_; | |
133 base::Atomic32 active_; | 82 base::Atomic32 active_; |
134 base::Atomic32 registered_; | |
135 PlatformData* data_; // Platform specific data. | 83 PlatformData* data_; // Platform specific data. |
136 // Counts stack samples taken in various VM states. | |
137 bool is_counting_samples_; | |
138 unsigned js_sample_count_; | |
139 unsigned external_sample_count_; | |
140 DISALLOW_IMPLICIT_CONSTRUCTORS(Sampler); | 84 DISALLOW_IMPLICIT_CONSTRUCTORS(Sampler); |
141 }; | 85 }; |
142 | 86 |
143 | 87 } // namespace sampler |
144 #if defined(USE_SIMULATOR) | |
145 class SimulatorHelper : AllStatic { | |
146 public: | |
147 // Returns true if register values were successfully retrieved | |
148 // from the simulator, otherwise returns false. | |
149 static bool FillRegisters(Isolate* isolate, v8::RegisterState* state); | |
150 }; | |
151 #endif // USE_SIMULATOR | |
152 | |
153 | |
154 } // namespace internal | |
155 } // namespace v8 | 88 } // namespace v8 |
156 | 89 |
157 #endif // V8_PROFILER_SAMPLER_H_ | 90 #endif // V8_LIBSAMPLER_SAMPLER_H_ |
OLD | NEW |