OLD | NEW |
---|---|
1 // Copyright 2012 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_CPU_PROFILER_H_ | 5 #ifndef V8_PROFILER_PROFILER_LISTENER_H_ |
6 #define V8_PROFILER_CPU_PROFILER_H_ | 6 #define V8_PROFILER_PROFILER_LISTENER_H_ |
7 | 7 |
8 #include <memory> | 8 #include "src/code-events.h" |
9 | 9 #include "src/profiler/profile-generator.h" |
10 #include "src/allocation.h" | |
11 #include "src/base/atomic-utils.h" | |
12 #include "src/base/atomicops.h" | |
13 #include "src/base/platform/time.h" | |
14 #include "src/compiler.h" | |
15 #include "src/isolate.h" | |
16 #include "src/libsampler/v8-sampler.h" | |
17 #include "src/locked-queue.h" | |
18 #include "src/profiler/circular-queue.h" | |
19 #include "src/profiler/tick-sample.h" | |
20 | 10 |
21 namespace v8 { | 11 namespace v8 { |
22 namespace internal { | 12 namespace internal { |
23 | 13 |
24 // Forward declarations. | 14 class CodeEventsContainer; |
25 class CodeEntry; | |
26 class CodeMap; | |
27 class CpuProfile; | |
28 class CpuProfilesCollection; | |
29 class ProfileGenerator; | |
30 | 15 |
31 #define CODE_EVENTS_TYPE_LIST(V) \ | 16 class CodeEventObserver { |
32 V(CODE_CREATION, CodeCreateEventRecord) \ | |
33 V(CODE_MOVE, CodeMoveEventRecord) \ | |
34 V(CODE_DISABLE_OPT, CodeDisableOptEventRecord) \ | |
35 V(CODE_DEOPT, CodeDeoptEventRecord) \ | |
36 V(REPORT_BUILTIN, ReportBuiltinEventRecord) | |
37 | |
38 | |
39 class CodeEventRecord { | |
40 public: | 17 public: |
41 #define DECLARE_TYPE(type, ignore) type, | 18 virtual void CodeEventHandler(const CodeEventsContainer& evt_rec) = 0; |
42 enum Type { | 19 virtual ~CodeEventObserver() {} |
43 NONE = 0, | |
44 CODE_EVENTS_TYPE_LIST(DECLARE_TYPE) | |
45 NUMBER_OF_TYPES | |
46 }; | |
47 #undef DECLARE_TYPE | |
48 | |
49 Type type; | |
50 mutable unsigned order; | |
51 }; | 20 }; |
52 | 21 |
22 class ProfilerListener : public CodeEventListener { | |
23 public: | |
24 explicit ProfilerListener(Isolate* isolate); | |
25 ~ProfilerListener() override; | |
53 | 26 |
54 class CodeCreateEventRecord : public CodeEventRecord { | |
55 public: | |
56 Address start; | |
57 CodeEntry* entry; | |
58 unsigned size; | |
59 | |
60 INLINE(void UpdateCodeMap(CodeMap* code_map)); | |
61 }; | |
62 | |
63 | |
64 class CodeMoveEventRecord : public CodeEventRecord { | |
65 public: | |
66 Address from; | |
67 Address to; | |
68 | |
69 INLINE(void UpdateCodeMap(CodeMap* code_map)); | |
70 }; | |
71 | |
72 | |
73 class CodeDisableOptEventRecord : public CodeEventRecord { | |
74 public: | |
75 Address start; | |
76 const char* bailout_reason; | |
77 | |
78 INLINE(void UpdateCodeMap(CodeMap* code_map)); | |
79 }; | |
80 | |
81 | |
82 class CodeDeoptEventRecord : public CodeEventRecord { | |
83 public: | |
84 Address start; | |
85 const char* deopt_reason; | |
86 SourcePosition position; | |
87 int deopt_id; | |
88 | |
89 INLINE(void UpdateCodeMap(CodeMap* code_map)); | |
90 }; | |
91 | |
92 | |
93 class ReportBuiltinEventRecord : public CodeEventRecord { | |
94 public: | |
95 Address start; | |
96 Builtins::Name builtin_id; | |
97 | |
98 INLINE(void UpdateCodeMap(CodeMap* code_map)); | |
99 }; | |
100 | |
101 | |
102 class TickSampleEventRecord { | |
103 public: | |
104 // The parameterless constructor is used when we dequeue data from | |
105 // the ticks buffer. | |
106 TickSampleEventRecord() { } | |
107 explicit TickSampleEventRecord(unsigned order) : order(order) { } | |
108 | |
109 unsigned order; | |
110 TickSample sample; | |
111 }; | |
112 | |
113 | |
114 class CodeEventsContainer { | |
115 public: | |
116 explicit CodeEventsContainer( | |
117 CodeEventRecord::Type type = CodeEventRecord::NONE) { | |
118 generic.type = type; | |
119 } | |
120 union { | |
121 CodeEventRecord generic; | |
122 #define DECLARE_CLASS(ignore, type) type type##_; | |
123 CODE_EVENTS_TYPE_LIST(DECLARE_CLASS) | |
124 #undef DECLARE_TYPE | |
125 }; | |
126 }; | |
127 | |
128 | |
129 // This class implements both the profile events processor thread and | |
130 // methods called by event producers: VM and stack sampler threads. | |
131 class ProfilerEventsProcessor : public base::Thread { | |
132 public: | |
133 ProfilerEventsProcessor(ProfileGenerator* generator, | |
134 sampler::Sampler* sampler, | |
135 base::TimeDelta period); | |
136 virtual ~ProfilerEventsProcessor(); | |
137 | |
138 // Thread control. | |
139 virtual void Run(); | |
140 void StopSynchronously(); | |
141 INLINE(bool running()) { return !!base::NoBarrier_Load(&running_); } | |
142 void Enqueue(const CodeEventsContainer& event); | |
143 | |
144 // Puts current stack into tick sample events buffer. | |
145 void AddCurrentStack(Isolate* isolate, bool update_stats = false); | |
146 void AddDeoptStack(Isolate* isolate, Address from, int fp_to_sp_delta); | |
147 | |
148 // Tick sample events are filled directly in the buffer of the circular | |
149 // queue (because the structure is of fixed width, but usually not all | |
150 // stack frame entries are filled.) This method returns a pointer to the | |
151 // next record of the buffer. | |
152 inline TickSample* StartTickSample(); | |
153 inline void FinishTickSample(); | |
154 | |
155 // SamplingCircularQueue has stricter alignment requirements than a normal new | |
156 // can fulfil, so we need to provide our own new/delete here. | |
157 void* operator new(size_t size); | |
158 void operator delete(void* ptr); | |
159 | |
160 private: | |
161 // Called from events processing thread (Run() method.) | |
162 bool ProcessCodeEvent(); | |
163 | |
164 enum SampleProcessingResult { | |
165 OneSampleProcessed, | |
166 FoundSampleForNextCodeEvent, | |
167 NoSamplesInQueue | |
168 }; | |
169 SampleProcessingResult ProcessOneSample(); | |
170 | |
171 ProfileGenerator* generator_; | |
172 sampler::Sampler* sampler_; | |
173 base::Atomic32 running_; | |
174 const base::TimeDelta period_; // Samples & code events processing period. | |
175 LockedQueue<CodeEventsContainer> events_buffer_; | |
176 static const size_t kTickSampleBufferSize = 1 * MB; | |
177 static const size_t kTickSampleQueueLength = | |
178 kTickSampleBufferSize / sizeof(TickSampleEventRecord); | |
179 SamplingCircularQueue<TickSampleEventRecord, | |
180 kTickSampleQueueLength> ticks_buffer_; | |
181 LockedQueue<TickSampleEventRecord> ticks_from_vm_buffer_; | |
182 base::AtomicNumber<unsigned> last_code_event_id_; | |
183 unsigned last_processed_code_event_id_; | |
184 }; | |
185 | |
186 class CpuProfiler : public CodeEventListener { | |
187 public: | |
188 explicit CpuProfiler(Isolate* isolate); | |
189 | |
190 CpuProfiler(Isolate* isolate, CpuProfilesCollection* profiles, | |
191 ProfileGenerator* test_generator, | |
192 ProfilerEventsProcessor* test_processor); | |
193 | |
194 ~CpuProfiler() override; | |
195 | |
196 void set_sampling_interval(base::TimeDelta value); | |
197 void CollectSample(); | |
198 void StartProfiling(const char* title, bool record_samples = false); | |
199 void StartProfiling(String* title, bool record_samples); | |
200 CpuProfile* StopProfiling(const char* title); | |
201 CpuProfile* StopProfiling(String* title); | |
202 int GetProfilesCount(); | |
203 CpuProfile* GetProfile(int index); | |
204 void DeleteAllProfiles(); | |
205 void DeleteProfile(CpuProfile* profile); | |
206 | |
207 // Invoked from stack sampler (thread or signal handler.) | |
208 inline TickSample* StartTickSample(); | |
209 inline void FinishTickSample(); | |
210 | |
211 // Must be called via PROFILE macro, otherwise will crash when | |
212 // profiling is not enabled. | |
213 void CallbackEvent(Name* name, Address entry_point) override; | 27 void CallbackEvent(Name* name, Address entry_point) override; |
214 void CodeCreateEvent(LogEventsAndTags tag, AbstractCode* code, | 28 void CodeCreateEvent(CodeEventListener::LogEventsAndTags tag, |
215 const char* comment) override; | 29 AbstractCode* code, const char* comment) override; |
216 void CodeCreateEvent(LogEventsAndTags tag, AbstractCode* code, | 30 void CodeCreateEvent(CodeEventListener::LogEventsAndTags tag, |
217 Name* name) override; | 31 AbstractCode* code, Name* name) override; |
218 void CodeCreateEvent(LogEventsAndTags tag, AbstractCode* code, | 32 void CodeCreateEvent(CodeEventListener::LogEventsAndTags tag, |
219 SharedFunctionInfo* shared, Name* script_name) override; | 33 AbstractCode* code, SharedFunctionInfo* shared, |
220 void CodeCreateEvent(LogEventsAndTags tag, AbstractCode* code, | 34 Name* script_name) override; |
221 SharedFunctionInfo* shared, Name* script_name, int line, | 35 void CodeCreateEvent(CodeEventListener::LogEventsAndTags tag, |
222 int column) override; | 36 AbstractCode* code, SharedFunctionInfo* shared, |
223 void CodeCreateEvent(LogEventsAndTags tag, AbstractCode* code, | 37 Name* script_name, int line, int column) override; |
224 int args_count) override; | 38 void CodeCreateEvent(CodeEventListener::LogEventsAndTags tag, |
39 AbstractCode* code, int args_count) override; | |
225 void CodeMovingGCEvent() override {} | 40 void CodeMovingGCEvent() override {} |
226 void CodeMoveEvent(AbstractCode* from, Address to) override; | 41 void CodeMoveEvent(AbstractCode* from, Address to) override; |
227 void CodeDisableOptEvent(AbstractCode* code, | 42 void CodeDisableOptEvent(AbstractCode* code, |
228 SharedFunctionInfo* shared) override; | 43 SharedFunctionInfo* shared) override; |
229 void CodeDeoptEvent(Code* code, Address pc, int fp_to_sp_delta) override; | 44 void CodeDeoptEvent(Code* code, Address pc, int fp_to_sp_delta) override; |
230 void GetterCallbackEvent(Name* name, Address entry_point) override; | 45 void GetterCallbackEvent(Name* name, Address entry_point) override; |
231 void RegExpCodeCreateEvent(AbstractCode* code, String* source) override; | 46 void RegExpCodeCreateEvent(AbstractCode* code, String* source) override; |
232 void SetterCallbackEvent(Name* name, Address entry_point) override; | 47 void SetterCallbackEvent(Name* name, Address entry_point) override; |
233 void SharedFunctionInfoMoveEvent(Address from, Address to) override {} | 48 void SharedFunctionInfoMoveEvent(Address from, Address to) override {} |
234 | 49 |
235 bool is_profiling() const { return is_profiling_; } | 50 void StartBuildingCodeEntry(); |
51 CodeEntry* NewCodeEntry( | |
52 CodeEventListener::LogEventsAndTags tag, const char* name, | |
53 const char* name_prefix = CodeEntry::kEmptyNamePrefix, | |
54 const char* resource_name = CodeEntry::kEmptyResourceName, | |
55 int line_number = v8::CpuProfileNode::kNoLineNumberInfo, | |
56 int column_number = v8::CpuProfileNode::kNoColumnNumberInfo, | |
57 JITLineInfoTable* line_info = NULL, Address instruction_start = NULL); | |
236 | 58 |
237 ProfileGenerator* generator() const { return generator_.get(); } | 59 void RegisterObserver(CodeEventObserver* observer); |
alph
2016/06/16 09:31:21
AddObserver
lpy
2016/06/16 12:40:52
Done.
| |
238 ProfilerEventsProcessor* processor() const { return processor_.get(); } | 60 void RemoveObserver(CodeEventObserver* observer); |
239 Isolate* isolate() const { return isolate_; } | 61 |
62 const char* GetName(Name* name) { | |
63 if (!is_building_code_entry_) return ""; | |
64 return function_and_resource_names_.GetName(name); | |
65 } | |
66 const char* GetName(int args_count) { | |
67 if (!is_building_code_entry_) return ""; | |
68 return function_and_resource_names_.GetName(args_count); | |
69 } | |
70 const char* GetFunctionName(Name* name) { | |
71 if (!is_building_code_entry_) return ""; | |
72 return function_and_resource_names_.GetFunctionName(name); | |
73 } | |
74 const char* GetFunctionName(const char* name) { | |
75 if (!is_building_code_entry_) return ""; | |
76 return function_and_resource_names_.GetFunctionName(name); | |
77 } | |
240 | 78 |
241 private: | 79 private: |
242 void StartProcessorIfNotStarted(); | |
243 void StopProcessorIfLastProfile(const char* title); | |
244 void StopProcessor(); | |
245 void ResetProfiles(); | |
246 void LogBuiltins(); | |
247 void RecordInliningInfo(CodeEntry* entry, AbstractCode* abstract_code); | 80 void RecordInliningInfo(CodeEntry* entry, AbstractCode* abstract_code); |
248 void RecordDeoptInlinedFrames(CodeEntry* entry, AbstractCode* abstract_code); | 81 void RecordDeoptInlinedFrames(CodeEntry* entry, AbstractCode* abstract_code); |
249 Name* InferScriptName(Name* name, SharedFunctionInfo* info); | 82 Name* InferScriptName(Name* name, SharedFunctionInfo* info); |
83 V8_INLINE void DispatchCodeEvent(const CodeEventsContainer& evt_rec) { | |
84 for (int i = 0, length = observers_.length(); i < length; ++i) { | |
85 observers_[i]->CodeEventHandler(evt_rec); | |
86 } | |
87 } | |
250 | 88 |
251 Isolate* const isolate_; | 89 Isolate* isolate_; |
252 base::TimeDelta sampling_interval_; | 90 StringsStorage function_and_resource_names_; |
253 std::unique_ptr<CpuProfilesCollection> profiles_; | 91 bool is_building_code_entry_; |
254 std::unique_ptr<ProfileGenerator> generator_; | 92 List<CodeEntry*> code_entries_; |
alph
2016/06/16 09:31:21
Let's use std::vector for new code.
lpy
2016/06/16 12:40:52
Done.
| |
255 std::unique_ptr<ProfilerEventsProcessor> processor_; | 93 List<CodeEventObserver*> observers_; |
alph
2016/06/16 09:31:21
ditto.
lpy
2016/06/16 12:40:52
Done.
| |
256 bool saved_is_logging_; | |
257 bool is_profiling_; | |
258 | 94 |
259 DISALLOW_COPY_AND_ASSIGN(CpuProfiler); | 95 DISALLOW_COPY_AND_ASSIGN(ProfilerListener); |
260 }; | 96 }; |
261 | 97 |
262 } // namespace internal | 98 } // namespace internal |
263 } // namespace v8 | 99 } // namespace v8 |
264 | 100 |
265 | 101 #endif // V8_PROFILER_PROFILER_LISTENER_H_ |
266 #endif // V8_PROFILER_CPU_PROFILER_H_ | |
OLD | NEW |