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_PROFILER_SAMPLER_H_ | 5 #ifndef V8_PROFILER_SAMPLER_H_ |
6 #define V8_PROFILER_SAMPLER_H_ | 6 #define V8_PROFILER_SAMPLER_H_ |
7 | 7 |
8 #include "include/v8.h" | 8 #include "include/v8.h" |
9 | 9 |
10 #include "src/base/atomicops.h" | |
11 #include "src/base/platform/time.h" | 10 #include "src/base/platform/time.h" |
12 #include "src/frames.h" | 11 #include "src/frames.h" |
13 #include "src/globals.h" | |
14 | 12 |
15 namespace v8 { | 13 namespace v8 { |
16 namespace internal { | 14 namespace internal { |
17 | 15 |
18 class Isolate; | 16 class Isolate; |
19 | 17 |
20 // ---------------------------------------------------------------------------- | |
21 // Sampler | |
22 // | |
23 // A sampler periodically samples the state of the VM and optionally | |
24 // (if used for profiling) the program counter and stack pointer for | |
25 // the thread that created it. | |
26 | |
27 // TickSample captures the information collected for each sample. | 18 // TickSample captures the information collected for each sample. |
28 struct TickSample { | 19 struct TickSample { |
29 // Internal profiling (with --prof + tools/$OS-tick-processor) wants to | 20 // Internal profiling (with --prof + tools/$OS-tick-processor) wants to |
30 // include the runtime function we're calling. Externally exposed tick | 21 // include the runtime function we're calling. Externally exposed tick |
31 // samples don't care. | 22 // samples don't care. |
32 enum RecordCEntryFrame { kIncludeCEntryFrame, kSkipCEntryFrame }; | 23 enum RecordCEntryFrame { kIncludeCEntryFrame, kSkipCEntryFrame }; |
33 | 24 |
34 TickSample() | 25 TickSample() |
35 : state(OTHER), | 26 : state(OTHER), |
36 pc(NULL), | 27 pc(NULL), |
(...skipping 17 matching lines...) Expand all Loading... |
54 static const unsigned kMaxFramesCountLog2 = 8; | 45 static const unsigned kMaxFramesCountLog2 = 8; |
55 static const unsigned kMaxFramesCount = (1 << kMaxFramesCountLog2) - 1; | 46 static const unsigned kMaxFramesCount = (1 << kMaxFramesCountLog2) - 1; |
56 Address stack[kMaxFramesCount]; // Call stack. | 47 Address stack[kMaxFramesCount]; // Call stack. |
57 base::TimeTicks timestamp; | 48 base::TimeTicks timestamp; |
58 unsigned frames_count : kMaxFramesCountLog2; // Number of captured frames. | 49 unsigned frames_count : kMaxFramesCountLog2; // Number of captured frames. |
59 bool has_external_callback : 1; | 50 bool has_external_callback : 1; |
60 bool update_stats : 1; // Whether the sample should update aggregated stats. | 51 bool update_stats : 1; // Whether the sample should update aggregated stats. |
61 StackFrame::Type top_frame_type : 4; | 52 StackFrame::Type top_frame_type : 4; |
62 }; | 53 }; |
63 | 54 |
64 class Sampler { | |
65 public: | |
66 // Initializes the Sampler support. Called once at VM startup. | |
67 static void SetUp(); | |
68 static void TearDown(); | |
69 | |
70 // Initialize sampler. | |
71 Sampler(Isolate* isolate, int interval); | |
72 virtual ~Sampler(); | |
73 | |
74 Isolate* isolate() const { return isolate_; } | |
75 int interval() const { return interval_; } | |
76 | |
77 // Performs stack sampling. | |
78 void SampleStack(const v8::RegisterState& regs); | |
79 | |
80 // Start and stop sampler. | |
81 void Start(); | |
82 void Stop(); | |
83 | |
84 // Whether the sampling thread should use this Sampler for CPU profiling? | |
85 bool IsProfiling() const { | |
86 return base::NoBarrier_Load(&profiling_) > 0 && | |
87 !base::NoBarrier_Load(&has_processing_thread_); | |
88 } | |
89 void IncreaseProfilingDepth(); | |
90 void DecreaseProfilingDepth(); | |
91 | |
92 // Whether the sampler is running (that is, consumes resources). | |
93 bool IsActive() const { return base::NoBarrier_Load(&active_); } | |
94 | |
95 void DoSample(); | |
96 // If true next sample must be initiated on the profiler event processor | |
97 // thread right after latest sample is processed. | |
98 void SetHasProcessingThread(bool value) { | |
99 base::NoBarrier_Store(&has_processing_thread_, value); | |
100 } | |
101 | |
102 // Used in tests to make sure that stack sampling is performed. | |
103 unsigned js_sample_count() const { return js_sample_count_; } | |
104 unsigned external_sample_count() const { return external_sample_count_; } | |
105 void StartCountingSamples() { | |
106 js_sample_count_ = 0; | |
107 external_sample_count_ = 0; | |
108 is_counting_samples_ = true; | |
109 } | |
110 | |
111 class PlatformData; | |
112 PlatformData* platform_data() const { return data_; } | |
113 | |
114 protected: | |
115 // This method is called for each sampling period with the current | |
116 // program counter. | |
117 virtual void Tick(TickSample* sample) = 0; | |
118 | |
119 private: | |
120 void SetActive(bool value) { base::NoBarrier_Store(&active_, value); } | |
121 | |
122 Isolate* isolate_; | |
123 const int interval_; | |
124 base::Atomic32 profiling_; | |
125 base::Atomic32 has_processing_thread_; | |
126 base::Atomic32 active_; | |
127 PlatformData* data_; // Platform specific data. | |
128 // Counts stack samples taken in various VM states. | |
129 bool is_counting_samples_; | |
130 unsigned js_sample_count_; | |
131 unsigned external_sample_count_; | |
132 DISALLOW_IMPLICIT_CONSTRUCTORS(Sampler); | |
133 }; | |
134 | |
135 | |
136 } // namespace internal | 55 } // namespace internal |
137 } // namespace v8 | 56 } // namespace v8 |
138 | 57 |
139 #endif // V8_PROFILER_SAMPLER_H_ | 58 #endif // V8_PROFILER_SAMPLER_H_ |
OLD | NEW |