Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(6)

Side by Side Diff: src/profiler/profiler-listener.cc

Issue 2053523003: Refactor CpuProfiler. (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: address alph's comments Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 #include "src/profiler/profiler-listener.h"
6
7 #include "src/deoptimizer.h"
8 #include "src/interpreter/source-position-table.h"
5 #include "src/profiler/cpu-profiler.h" 9 #include "src/profiler/cpu-profiler.h"
6 10 #include "src/profiler/profile-generator-inl.h"
7 #include "src/debug/debug.h"
8 #include "src/deoptimizer.h"
9 #include "src/frames-inl.h"
10 #include "src/locked-queue-inl.h"
11 #include "src/log-inl.h"
12 #include "src/profiler/cpu-profiler-inl.h"
13 #include "src/vm-state-inl.h"
14
15 #include "include/v8-profiler.h"
16 11
17 namespace v8 { 12 namespace v8 {
18 namespace internal { 13 namespace internal {
19 14
20 static const int kProfilerStackSize = 64 * KB; 15 ProfilerListener::ProfilerListener(Isolate* isolate)
21 16 : isolate_(isolate),
22 17 function_and_resource_names_(isolate->heap()),
23 ProfilerEventsProcessor::ProfilerEventsProcessor(ProfileGenerator* generator, 18 is_building_code_entry_(false) {
24 sampler::Sampler* sampler, 19 bool result = isolate_->code_event_dispatcher()->AddListener(this);
alph 2016/06/17 10:38:44 I can't find where you remove the listener.
lpy 2016/06/17 13:55:00 Done.
25 base::TimeDelta period) 20 USE(result);
26 : Thread(Thread::Options("v8:ProfEvntProc", kProfilerStackSize)), 21 DCHECK(result);
27 generator_(generator),
28 sampler_(sampler),
29 running_(1),
30 period_(period),
31 last_code_event_id_(0),
32 last_processed_code_event_id_(0) {}
33
34
35 ProfilerEventsProcessor::~ProfilerEventsProcessor() {}
36
37
38 void ProfilerEventsProcessor::Enqueue(const CodeEventsContainer& event) {
39 event.generic.order = last_code_event_id_.Increment(1);
40 events_buffer_.Enqueue(event);
41 } 22 }
42 23
43 24 ProfilerListener::~ProfilerListener() {
44 void ProfilerEventsProcessor::AddDeoptStack(Isolate* isolate, Address from, 25 for (auto it = code_entries_.begin(); it != code_entries_.end(); ++it) {
alph 2016/06/17 10:38:44 nit: for (auto entry : code_entries_)
lpy 2016/06/17 13:55:00 Done.
45 int fp_to_sp_delta) { 26 delete *it;
46 TickSampleEventRecord record(last_code_event_id_.Value());
47 RegisterState regs;
48 Address fp = isolate->c_entry_fp(isolate->thread_local_top());
49 regs.sp = fp - fp_to_sp_delta;
50 regs.fp = fp;
51 regs.pc = from;
52 record.sample.Init(isolate, regs, TickSample::kSkipCEntryFrame, false);
53 ticks_from_vm_buffer_.Enqueue(record);
54 }
55
56 void ProfilerEventsProcessor::AddCurrentStack(Isolate* isolate,
57 bool update_stats) {
58 TickSampleEventRecord record(last_code_event_id_.Value());
59 RegisterState regs;
60 StackFrameIterator it(isolate);
61 if (!it.done()) {
62 StackFrame* frame = it.frame();
63 regs.sp = frame->sp();
64 regs.fp = frame->fp();
65 regs.pc = frame->pc();
66 }
67 record.sample.Init(isolate, regs, TickSample::kSkipCEntryFrame, update_stats);
68 ticks_from_vm_buffer_.Enqueue(record);
69 }
70
71
72 void ProfilerEventsProcessor::StopSynchronously() {
73 if (!base::NoBarrier_AtomicExchange(&running_, 0)) return;
74 Join();
75 }
76
77
78 bool ProfilerEventsProcessor::ProcessCodeEvent() {
79 CodeEventsContainer record;
80 if (events_buffer_.Dequeue(&record)) {
81 switch (record.generic.type) {
82 #define PROFILER_TYPE_CASE(type, clss) \
83 case CodeEventRecord::type: \
84 record.clss##_.UpdateCodeMap(generator_->code_map()); \
85 break;
86
87 CODE_EVENTS_TYPE_LIST(PROFILER_TYPE_CASE)
88
89 #undef PROFILER_TYPE_CASE
90 default: return true; // Skip record.
91 }
92 last_processed_code_event_id_ = record.generic.order;
93 return true;
94 }
95 return false;
96 }
97
98 ProfilerEventsProcessor::SampleProcessingResult
99 ProfilerEventsProcessor::ProcessOneSample() {
100 TickSampleEventRecord record1;
101 if (ticks_from_vm_buffer_.Peek(&record1) &&
102 (record1.order == last_processed_code_event_id_)) {
103 TickSampleEventRecord record;
104 ticks_from_vm_buffer_.Dequeue(&record);
105 generator_->RecordTickSample(record.sample);
106 return OneSampleProcessed;
107 }
108
109 const TickSampleEventRecord* record = ticks_buffer_.Peek();
110 if (record == NULL) {
111 if (ticks_from_vm_buffer_.IsEmpty()) return NoSamplesInQueue;
112 return FoundSampleForNextCodeEvent;
113 }
114 if (record->order != last_processed_code_event_id_) {
115 return FoundSampleForNextCodeEvent;
116 }
117 generator_->RecordTickSample(record->sample);
118 ticks_buffer_.Remove();
119 return OneSampleProcessed;
120 }
121
122
123 void ProfilerEventsProcessor::Run() {
124 while (!!base::NoBarrier_Load(&running_)) {
125 base::TimeTicks nextSampleTime =
126 base::TimeTicks::HighResolutionNow() + period_;
127 base::TimeTicks now;
128 SampleProcessingResult result;
129 // Keep processing existing events until we need to do next sample
130 // or the ticks buffer is empty.
131 do {
132 result = ProcessOneSample();
133 if (result == FoundSampleForNextCodeEvent) {
134 // All ticks of the current last_processed_code_event_id_ are
135 // processed, proceed to the next code event.
136 ProcessCodeEvent();
137 }
138 now = base::TimeTicks::HighResolutionNow();
139 } while (result != NoSamplesInQueue && now < nextSampleTime);
140
141 if (nextSampleTime > now) {
142 #if V8_OS_WIN
143 // Do not use Sleep on Windows as it is very imprecise.
144 // Could be up to 16ms jitter, which is unacceptable for the purpose.
145 while (base::TimeTicks::HighResolutionNow() < nextSampleTime) {
146 }
147 #else
148 base::OS::Sleep(nextSampleTime - now);
149 #endif
150 }
151
152 // Schedule next sample. sampler_ is NULL in tests.
153 if (sampler_) sampler_->DoSample();
154 }
155
156 // Process remaining tick events.
157 do {
158 SampleProcessingResult result;
159 do {
160 result = ProcessOneSample();
161 } while (result == OneSampleProcessed);
162 } while (ProcessCodeEvent());
163 }
164
165
166 void* ProfilerEventsProcessor::operator new(size_t size) {
167 return AlignedAlloc(size, V8_ALIGNOF(ProfilerEventsProcessor));
168 }
169
170
171 void ProfilerEventsProcessor::operator delete(void* ptr) {
172 AlignedFree(ptr);
173 }
174
175
176 int CpuProfiler::GetProfilesCount() {
177 // The count of profiles doesn't depend on a security token.
178 return profiles_->profiles()->length();
179 }
180
181
182 CpuProfile* CpuProfiler::GetProfile(int index) {
183 return profiles_->profiles()->at(index);
184 }
185
186
187 void CpuProfiler::DeleteAllProfiles() {
188 if (is_profiling_) StopProcessor();
189 ResetProfiles();
190 }
191
192
193 void CpuProfiler::DeleteProfile(CpuProfile* profile) {
194 profiles_->RemoveProfile(profile);
195 delete profile;
196 if (profiles_->profiles()->is_empty() && !is_profiling_) {
197 // If this was the last profile, clean up all accessory data as well.
198 ResetProfiles();
199 } 27 }
200 } 28 }
201 29
202 30 void ProfilerListener::CallbackEvent(Name* name, Address entry_point) {
203 void CpuProfiler::CallbackEvent(Name* name, Address entry_point) {
204 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); 31 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION);
205 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; 32 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
206 rec->start = entry_point; 33 rec->start = entry_point;
207 rec->entry = profiles_->NewCodeEntry(CodeEventListener::CALLBACK_TAG, 34 rec->entry = NewCodeEntry(CodeEventListener::CALLBACK_TAG, GetName(name));
208 profiles_->GetName(name));
209 rec->size = 1; 35 rec->size = 1;
210 processor_->Enqueue(evt_rec); 36 DispatchCodeEvent(evt_rec);
211 } 37 }
212 38
213 void CpuProfiler::CodeCreateEvent(CodeEventListener::LogEventsAndTags tag, 39 void ProfilerListener::CodeCreateEvent(CodeEventListener::LogEventsAndTags tag,
214 AbstractCode* code, const char* name) { 40 AbstractCode* code, const char* name) {
215 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); 41 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION);
216 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; 42 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
217 rec->start = code->address(); 43 rec->start = code->address();
218 rec->entry = profiles_->NewCodeEntry( 44 rec->entry = NewCodeEntry(
219 tag, profiles_->GetFunctionName(name), CodeEntry::kEmptyNamePrefix, 45 tag, GetFunctionName(name), CodeEntry::kEmptyNamePrefix,
220 CodeEntry::kEmptyResourceName, CpuProfileNode::kNoLineNumberInfo, 46 CodeEntry::kEmptyResourceName, CpuProfileNode::kNoLineNumberInfo,
221 CpuProfileNode::kNoColumnNumberInfo, NULL, code->instruction_start()); 47 CpuProfileNode::kNoColumnNumberInfo, NULL, code->instruction_start());
222 RecordInliningInfo(rec->entry, code); 48 RecordInliningInfo(rec->entry, code);
223 rec->size = code->ExecutableSize(); 49 rec->size = code->ExecutableSize();
224 processor_->Enqueue(evt_rec); 50 DispatchCodeEvent(evt_rec);
225 } 51 }
226 52
227 void CpuProfiler::CodeCreateEvent(CodeEventListener::LogEventsAndTags tag, 53 void ProfilerListener::CodeCreateEvent(CodeEventListener::LogEventsAndTags tag,
228 AbstractCode* code, Name* name) { 54 AbstractCode* code, Name* name) {
229 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); 55 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION);
230 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; 56 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
231 rec->start = code->address(); 57 rec->start = code->address();
232 rec->entry = profiles_->NewCodeEntry( 58 rec->entry = NewCodeEntry(
233 tag, profiles_->GetFunctionName(name), CodeEntry::kEmptyNamePrefix, 59 tag, GetFunctionName(name), CodeEntry::kEmptyNamePrefix,
234 CodeEntry::kEmptyResourceName, CpuProfileNode::kNoLineNumberInfo, 60 CodeEntry::kEmptyResourceName, CpuProfileNode::kNoLineNumberInfo,
235 CpuProfileNode::kNoColumnNumberInfo, NULL, code->instruction_start()); 61 CpuProfileNode::kNoColumnNumberInfo, NULL, code->instruction_start());
236 RecordInliningInfo(rec->entry, code); 62 RecordInliningInfo(rec->entry, code);
237 rec->size = code->ExecutableSize(); 63 rec->size = code->ExecutableSize();
238 processor_->Enqueue(evt_rec); 64 DispatchCodeEvent(evt_rec);
239 } 65 }
240 66
241 void CpuProfiler::CodeCreateEvent(CodeEventListener::LogEventsAndTags tag, 67 void ProfilerListener::CodeCreateEvent(CodeEventListener::LogEventsAndTags tag,
242 AbstractCode* code, 68 AbstractCode* code,
243 SharedFunctionInfo* shared, 69 SharedFunctionInfo* shared,
244 Name* script_name) { 70 Name* script_name) {
245 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); 71 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION);
246 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; 72 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
247 rec->start = code->address(); 73 rec->start = code->address();
248 rec->entry = profiles_->NewCodeEntry( 74 rec->entry = NewCodeEntry(
249 tag, profiles_->GetFunctionName(shared->DebugName()), 75 tag, GetFunctionName(shared->DebugName()), CodeEntry::kEmptyNamePrefix,
250 CodeEntry::kEmptyNamePrefix, 76 GetName(InferScriptName(script_name, shared)),
251 profiles_->GetName(InferScriptName(script_name, shared)),
252 CpuProfileNode::kNoLineNumberInfo, CpuProfileNode::kNoColumnNumberInfo, 77 CpuProfileNode::kNoLineNumberInfo, CpuProfileNode::kNoColumnNumberInfo,
253 NULL, code->instruction_start()); 78 NULL, code->instruction_start());
254 RecordInliningInfo(rec->entry, code); 79 RecordInliningInfo(rec->entry, code);
255 rec->entry->FillFunctionInfo(shared); 80 rec->entry->FillFunctionInfo(shared);
256 rec->size = code->ExecutableSize(); 81 rec->size = code->ExecutableSize();
257 processor_->Enqueue(evt_rec); 82 DispatchCodeEvent(evt_rec);
258 } 83 }
259 84
260 void CpuProfiler::CodeCreateEvent(CodeEventListener::LogEventsAndTags tag, 85 void ProfilerListener::CodeCreateEvent(CodeEventListener::LogEventsAndTags tag,
261 AbstractCode* abstract_code, 86 AbstractCode* abstract_code,
262 SharedFunctionInfo* shared, Name* script_name, 87 SharedFunctionInfo* shared,
263 int line, int column) { 88 Name* script_name, int line,
89 int column) {
264 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); 90 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION);
265 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; 91 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
266 rec->start = abstract_code->address(); 92 rec->start = abstract_code->address();
267 Script* script = Script::cast(shared->script()); 93 Script* script = Script::cast(shared->script());
268 JITLineInfoTable* line_table = NULL; 94 JITLineInfoTable* line_table = NULL;
269 if (script) { 95 if (script) {
270 if (abstract_code->IsCode()) { 96 if (abstract_code->IsCode()) {
271 Code* code = abstract_code->GetCode(); 97 Code* code = abstract_code->GetCode();
272 int start_position = shared->start_position(); 98 int start_position = shared->start_position();
273 int end_position = shared->end_position(); 99 int end_position = shared->end_position();
(...skipping 18 matching lines...) Expand all
292 line_table = new JITLineInfoTable(); 118 line_table = new JITLineInfoTable();
293 interpreter::SourcePositionTableIterator it( 119 interpreter::SourcePositionTableIterator it(
294 bytecode->source_position_table()); 120 bytecode->source_position_table());
295 for (; !it.done(); it.Advance()) { 121 for (; !it.done(); it.Advance()) {
296 int line_number = script->GetLineNumber(it.source_position()) + 1; 122 int line_number = script->GetLineNumber(it.source_position()) + 1;
297 int pc_offset = it.bytecode_offset() + BytecodeArray::kHeaderSize; 123 int pc_offset = it.bytecode_offset() + BytecodeArray::kHeaderSize;
298 line_table->SetPosition(pc_offset, line_number); 124 line_table->SetPosition(pc_offset, line_number);
299 } 125 }
300 } 126 }
301 } 127 }
302 rec->entry = profiles_->NewCodeEntry( 128 rec->entry = NewCodeEntry(
303 tag, profiles_->GetFunctionName(shared->DebugName()), 129 tag, GetFunctionName(shared->DebugName()), CodeEntry::kEmptyNamePrefix,
304 CodeEntry::kEmptyNamePrefix, 130 GetName(InferScriptName(script_name, shared)), line, column, line_table,
305 profiles_->GetName(InferScriptName(script_name, shared)), line, column, 131 abstract_code->instruction_start());
306 line_table, abstract_code->instruction_start());
307 RecordInliningInfo(rec->entry, abstract_code); 132 RecordInliningInfo(rec->entry, abstract_code);
308 RecordDeoptInlinedFrames(rec->entry, abstract_code); 133 RecordDeoptInlinedFrames(rec->entry, abstract_code);
309 rec->entry->FillFunctionInfo(shared); 134 rec->entry->FillFunctionInfo(shared);
310 rec->size = abstract_code->ExecutableSize(); 135 rec->size = abstract_code->ExecutableSize();
311 processor_->Enqueue(evt_rec); 136 DispatchCodeEvent(evt_rec);
312 } 137 }
313 138
314 void CpuProfiler::CodeCreateEvent(CodeEventListener::LogEventsAndTags tag, 139 void ProfilerListener::CodeCreateEvent(CodeEventListener::LogEventsAndTags tag,
315 AbstractCode* code, int args_count) { 140 AbstractCode* code, int args_count) {
316 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); 141 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION);
317 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; 142 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
318 rec->start = code->address(); 143 rec->start = code->address();
319 rec->entry = profiles_->NewCodeEntry( 144 rec->entry = NewCodeEntry(
320 tag, profiles_->GetName(args_count), "args_count: ", 145 tag, GetName(args_count), "args_count: ", CodeEntry::kEmptyResourceName,
321 CodeEntry::kEmptyResourceName, CpuProfileNode::kNoLineNumberInfo, 146 CpuProfileNode::kNoLineNumberInfo, CpuProfileNode::kNoColumnNumberInfo,
322 CpuProfileNode::kNoColumnNumberInfo, NULL, code->instruction_start()); 147 NULL, code->instruction_start());
323 RecordInliningInfo(rec->entry, code); 148 RecordInliningInfo(rec->entry, code);
324 rec->size = code->ExecutableSize(); 149 rec->size = code->ExecutableSize();
325 processor_->Enqueue(evt_rec); 150 DispatchCodeEvent(evt_rec);
326 } 151 }
327 152
328 void CpuProfiler::CodeMoveEvent(AbstractCode* from, Address to) { 153 void ProfilerListener::CodeMoveEvent(AbstractCode* from, Address to) {
329 CodeEventsContainer evt_rec(CodeEventRecord::CODE_MOVE); 154 CodeEventsContainer evt_rec(CodeEventRecord::CODE_MOVE);
330 CodeMoveEventRecord* rec = &evt_rec.CodeMoveEventRecord_; 155 CodeMoveEventRecord* rec = &evt_rec.CodeMoveEventRecord_;
331 rec->from = from->address(); 156 rec->from = from->address();
332 rec->to = to; 157 rec->to = to;
333 processor_->Enqueue(evt_rec); 158 DispatchCodeEvent(evt_rec);
334 } 159 }
335 160
336 void CpuProfiler::CodeDisableOptEvent(AbstractCode* code, 161 void ProfilerListener::CodeDisableOptEvent(AbstractCode* code,
337 SharedFunctionInfo* shared) { 162 SharedFunctionInfo* shared) {
338 CodeEventsContainer evt_rec(CodeEventRecord::CODE_DISABLE_OPT); 163 CodeEventsContainer evt_rec(CodeEventRecord::CODE_DISABLE_OPT);
339 CodeDisableOptEventRecord* rec = &evt_rec.CodeDisableOptEventRecord_; 164 CodeDisableOptEventRecord* rec = &evt_rec.CodeDisableOptEventRecord_;
340 rec->start = code->address(); 165 rec->start = code->address();
341 rec->bailout_reason = GetBailoutReason(shared->disable_optimization_reason()); 166 rec->bailout_reason = GetBailoutReason(shared->disable_optimization_reason());
342 processor_->Enqueue(evt_rec); 167 DispatchCodeEvent(evt_rec);
343 } 168 }
344 169
345 void CpuProfiler::CodeDeoptEvent(Code* code, Address pc, int fp_to_sp_delta) { 170 void ProfilerListener::CodeDeoptEvent(Code* code, Address pc,
171 int fp_to_sp_delta) {
346 CodeEventsContainer evt_rec(CodeEventRecord::CODE_DEOPT); 172 CodeEventsContainer evt_rec(CodeEventRecord::CODE_DEOPT);
347 CodeDeoptEventRecord* rec = &evt_rec.CodeDeoptEventRecord_; 173 CodeDeoptEventRecord* rec = &evt_rec.CodeDeoptEventRecord_;
348 Deoptimizer::DeoptInfo info = Deoptimizer::GetDeoptInfo(code, pc); 174 Deoptimizer::DeoptInfo info = Deoptimizer::GetDeoptInfo(code, pc);
349 rec->start = code->address(); 175 rec->start = code->address();
350 rec->deopt_reason = Deoptimizer::GetDeoptReason(info.deopt_reason); 176 rec->deopt_reason = Deoptimizer::GetDeoptReason(info.deopt_reason);
351 rec->position = info.position; 177 rec->position = info.position;
352 rec->deopt_id = info.deopt_id; 178 rec->deopt_id = info.deopt_id;
353 processor_->Enqueue(evt_rec); 179 rec->pc = reinterpret_cast<void*>(pc);
354 processor_->AddDeoptStack(isolate_, pc, fp_to_sp_delta); 180 rec->fp_to_sp_delta = fp_to_sp_delta;
181 DispatchCodeEvent(evt_rec);
355 } 182 }
356 183
357 void CpuProfiler::GetterCallbackEvent(Name* name, Address entry_point) { 184 void ProfilerListener::GetterCallbackEvent(Name* name, Address entry_point) {
358 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); 185 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION);
359 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; 186 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
360 rec->start = entry_point; 187 rec->start = entry_point;
361 rec->entry = profiles_->NewCodeEntry(CodeEventListener::CALLBACK_TAG, 188 rec->entry =
362 profiles_->GetName(name), "get "); 189 NewCodeEntry(CodeEventListener::CALLBACK_TAG, GetName(name), "get ");
363 rec->size = 1; 190 rec->size = 1;
364 processor_->Enqueue(evt_rec); 191 DispatchCodeEvent(evt_rec);
365 } 192 }
366 193
367 void CpuProfiler::RegExpCodeCreateEvent(AbstractCode* code, String* source) { 194 void ProfilerListener::RegExpCodeCreateEvent(AbstractCode* code,
195 String* source) {
368 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); 196 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION);
369 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; 197 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
370 rec->start = code->address(); 198 rec->start = code->address();
371 rec->entry = profiles_->NewCodeEntry( 199 rec->entry = NewCodeEntry(
372 CodeEventListener::REG_EXP_TAG, profiles_->GetName(source), "RegExp: ", 200 CodeEventListener::REG_EXP_TAG, GetName(source), "RegExp: ",
373 CodeEntry::kEmptyResourceName, CpuProfileNode::kNoLineNumberInfo, 201 CodeEntry::kEmptyResourceName, CpuProfileNode::kNoLineNumberInfo,
374 CpuProfileNode::kNoColumnNumberInfo, NULL, code->instruction_start()); 202 CpuProfileNode::kNoColumnNumberInfo, NULL, code->instruction_start());
375 rec->size = code->ExecutableSize(); 203 rec->size = code->ExecutableSize();
376 processor_->Enqueue(evt_rec); 204 DispatchCodeEvent(evt_rec);
377 } 205 }
378 206
379 207 void ProfilerListener::SetterCallbackEvent(Name* name, Address entry_point) {
380 void CpuProfiler::SetterCallbackEvent(Name* name, Address entry_point) {
381 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); 208 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION);
382 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; 209 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
383 rec->start = entry_point; 210 rec->start = entry_point;
384 rec->entry = profiles_->NewCodeEntry(CodeEventListener::CALLBACK_TAG, 211 rec->entry =
385 profiles_->GetName(name), "set "); 212 NewCodeEntry(CodeEventListener::CALLBACK_TAG, GetName(name), "set ");
386 rec->size = 1; 213 rec->size = 1;
387 processor_->Enqueue(evt_rec); 214 DispatchCodeEvent(evt_rec);
388 } 215 }
389 216
390 Name* CpuProfiler::InferScriptName(Name* name, SharedFunctionInfo* info) { 217 Name* ProfilerListener::InferScriptName(Name* name, SharedFunctionInfo* info) {
391 if (name->IsString() && String::cast(name)->length()) return name; 218 if (name->IsString() && String::cast(name)->length()) return name;
392 if (!info->script()->IsScript()) return name; 219 if (!info->script()->IsScript()) return name;
393 Object* source_url = Script::cast(info->script())->source_url(); 220 Object* source_url = Script::cast(info->script())->source_url();
394 return source_url->IsName() ? Name::cast(source_url) : name; 221 return source_url->IsName() ? Name::cast(source_url) : name;
395 } 222 }
396 223
397 void CpuProfiler::RecordInliningInfo(CodeEntry* entry, 224 void ProfilerListener::RecordInliningInfo(CodeEntry* entry,
398 AbstractCode* abstract_code) { 225 AbstractCode* abstract_code) {
399 if (!abstract_code->IsCode()) return; 226 if (!abstract_code->IsCode()) return;
400 Code* code = abstract_code->GetCode(); 227 Code* code = abstract_code->GetCode();
401 if (code->kind() != Code::OPTIMIZED_FUNCTION) return; 228 if (code->kind() != Code::OPTIMIZED_FUNCTION) return;
402 DeoptimizationInputData* deopt_input_data = 229 DeoptimizationInputData* deopt_input_data =
403 DeoptimizationInputData::cast(code->deoptimization_data()); 230 DeoptimizationInputData::cast(code->deoptimization_data());
404 int deopt_count = deopt_input_data->DeoptCount(); 231 int deopt_count = deopt_input_data->DeoptCount();
405 for (int i = 0; i < deopt_count; i++) { 232 for (int i = 0; i < deopt_count; i++) {
406 int pc_offset = deopt_input_data->Pc(i)->value(); 233 int pc_offset = deopt_input_data->Pc(i)->value();
407 if (pc_offset == -1) continue; 234 if (pc_offset == -1) continue;
408 int translation_index = deopt_input_data->TranslationIndex(i)->value(); 235 int translation_index = deopt_input_data->TranslationIndex(i)->value();
(...skipping 12 matching lines...) Expand all
421 it.Skip(Translation::NumberOfOperandsFor(opcode)); 248 it.Skip(Translation::NumberOfOperandsFor(opcode));
422 continue; 249 continue;
423 } 250 }
424 it.Next(); // Skip ast_id 251 it.Next(); // Skip ast_id
425 int shared_info_id = it.Next(); 252 int shared_info_id = it.Next();
426 it.Next(); // Skip height 253 it.Next(); // Skip height
427 SharedFunctionInfo* shared_info = SharedFunctionInfo::cast( 254 SharedFunctionInfo* shared_info = SharedFunctionInfo::cast(
428 deopt_input_data->LiteralArray()->get(shared_info_id)); 255 deopt_input_data->LiteralArray()->get(shared_info_id));
429 if (!depth++) continue; // Skip the current function itself. 256 if (!depth++) continue; // Skip the current function itself.
430 CodeEntry* inline_entry = new CodeEntry( 257 CodeEntry* inline_entry = new CodeEntry(
431 entry->tag(), profiles_->GetFunctionName(shared_info->DebugName()), 258 entry->tag(), GetFunctionName(shared_info->DebugName()),
432 CodeEntry::kEmptyNamePrefix, entry->resource_name(), 259 CodeEntry::kEmptyNamePrefix, entry->resource_name(),
433 CpuProfileNode::kNoLineNumberInfo, 260 CpuProfileNode::kNoLineNumberInfo,
434 CpuProfileNode::kNoColumnNumberInfo, NULL, code->instruction_start()); 261 CpuProfileNode::kNoColumnNumberInfo, NULL, code->instruction_start());
435 inline_entry->FillFunctionInfo(shared_info); 262 inline_entry->FillFunctionInfo(shared_info);
436 inline_stack.push_back(inline_entry); 263 inline_stack.push_back(inline_entry);
437 } 264 }
438 if (!inline_stack.empty()) { 265 if (!inline_stack.empty()) {
439 entry->AddInlineStack(pc_offset, inline_stack); 266 entry->AddInlineStack(pc_offset, inline_stack);
440 DCHECK(inline_stack.empty()); 267 DCHECK(inline_stack.empty());
441 } 268 }
442 } 269 }
443 } 270 }
444 271
445 void CpuProfiler::RecordDeoptInlinedFrames(CodeEntry* entry, 272 void ProfilerListener::RecordDeoptInlinedFrames(CodeEntry* entry,
446 AbstractCode* abstract_code) { 273 AbstractCode* abstract_code) {
447 if (abstract_code->kind() != AbstractCode::OPTIMIZED_FUNCTION) return; 274 if (abstract_code->kind() != AbstractCode::OPTIMIZED_FUNCTION) return;
448 Code* code = abstract_code->GetCode(); 275 Code* code = abstract_code->GetCode();
449 DeoptimizationInputData* deopt_input_data = 276 DeoptimizationInputData* deopt_input_data =
450 DeoptimizationInputData::cast(code->deoptimization_data()); 277 DeoptimizationInputData::cast(code->deoptimization_data());
451 int const mask = RelocInfo::ModeMask(RelocInfo::DEOPT_ID); 278 int const mask = RelocInfo::ModeMask(RelocInfo::DEOPT_ID);
452 for (RelocIterator rit(code, mask); !rit.done(); rit.next()) { 279 for (RelocIterator rit(code, mask); !rit.done(); rit.next()) {
453 RelocInfo* reloc_info = rit.rinfo(); 280 RelocInfo* reloc_info = rit.rinfo();
454 DCHECK(RelocInfo::IsDeoptId(reloc_info->rmode())); 281 DCHECK(RelocInfo::IsDeoptId(reloc_info->rmode()));
455 int deopt_id = static_cast<int>(reloc_info->data()); 282 int deopt_id = static_cast<int>(reloc_info->data());
456 int translation_index = 283 int translation_index =
(...skipping 26 matching lines...) Expand all
483 CodeEntry::DeoptInlinedFrame frame = {source_position, script_id}; 310 CodeEntry::DeoptInlinedFrame frame = {source_position, script_id};
484 inlined_frames.push_back(frame); 311 inlined_frames.push_back(frame);
485 } 312 }
486 if (!inlined_frames.empty() && !entry->HasDeoptInlinedFramesFor(deopt_id)) { 313 if (!inlined_frames.empty() && !entry->HasDeoptInlinedFramesFor(deopt_id)) {
487 entry->AddDeoptInlinedFrames(deopt_id, inlined_frames); 314 entry->AddDeoptInlinedFrames(deopt_id, inlined_frames);
488 DCHECK(inlined_frames.empty()); 315 DCHECK(inlined_frames.empty());
489 } 316 }
490 } 317 }
491 } 318 }
492 319
493 CpuProfiler::CpuProfiler(Isolate* isolate) 320 void ProfilerListener::StartBuildingCodeEntry() {
494 : isolate_(isolate), 321 is_building_code_entry_ = true;
495 sampling_interval_(base::TimeDelta::FromMicroseconds(
496 FLAG_cpu_profiler_sampling_interval)),
497 profiles_(new CpuProfilesCollection(isolate)),
498 is_profiling_(false) {
499 profiles_->set_cpu_profiler(this);
500 } 322 }
501 323
502 CpuProfiler::CpuProfiler(Isolate* isolate, CpuProfilesCollection* test_profiles, 324 CodeEntry* ProfilerListener::NewCodeEntry(
503 ProfileGenerator* test_generator, 325 CodeEventListener::LogEventsAndTags tag, const char* name,
504 ProfilerEventsProcessor* test_processor) 326 const char* name_prefix, const char* resource_name, int line_number,
505 : isolate_(isolate), 327 int column_number, JITLineInfoTable* line_info, Address instruction_start) {
506 sampling_interval_(base::TimeDelta::FromMicroseconds( 328 CodeEntry* code_entry =
507 FLAG_cpu_profiler_sampling_interval)), 329 new CodeEntry(tag, name, name_prefix, resource_name, line_number,
508 profiles_(test_profiles), 330 column_number, line_info, instruction_start);
509 generator_(test_generator), 331 code_entries_.push_back(code_entry);
510 processor_(test_processor), 332 return code_entry;
511 is_profiling_(false) {
512 profiles_->set_cpu_profiler(this);
513 } 333 }
514 334
515 CpuProfiler::~CpuProfiler() { 335 void ProfilerListener::AddObserver(CodeEventObserver* observer) {
516 DCHECK(!is_profiling_); 336 for (auto it = observers_.begin(); it != observers_.end(); ++it) {
alph 2016/06/17 10:38:44 nit: std::find
lpy 2016/06/17 13:55:00 Done.
337 if (*it == observer) return;
338 }
339 observers_.push_back(observer);
517 } 340 }
518 341
519 void CpuProfiler::set_sampling_interval(base::TimeDelta value) { 342 void ProfilerListener::RemoveObserver(CodeEventObserver* observer) {
520 DCHECK(!is_profiling_); 343 for (auto it = observers_.begin(); it != observers_.end(); ++it) {
alph 2016/06/17 10:38:44 nit: std::find
lpy 2016/06/17 13:55:00 Done.
521 sampling_interval_ = value; 344 if (*it == observer) {
522 } 345 observers_.erase(it);
523 346 return;
524 void CpuProfiler::ResetProfiles() { 347 }
525 profiles_.reset(new CpuProfilesCollection(isolate_));
526 profiles_->set_cpu_profiler(this);
527 }
528
529 void CpuProfiler::CollectSample() {
530 if (processor_) {
531 processor_->AddCurrentStack(isolate_);
532 } 348 }
533 } 349 }
534 350
535 void CpuProfiler::StartProfiling(const char* title, bool record_samples) {
536 if (profiles_->StartProfiling(title, record_samples)) {
537 StartProcessorIfNotStarted();
538 }
539 }
540
541
542 void CpuProfiler::StartProfiling(String* title, bool record_samples) {
543 StartProfiling(profiles_->GetName(title), record_samples);
544 isolate_->debug()->feature_tracker()->Track(DebugFeatureTracker::kProfiler);
545 }
546
547
548 void CpuProfiler::StartProcessorIfNotStarted() {
549 if (processor_) {
550 processor_->AddCurrentStack(isolate_);
551 return;
552 }
553 Logger* logger = isolate_->logger();
554 // Disable logging when using the new implementation.
555 saved_is_logging_ = logger->is_logging_;
556 logger->is_logging_ = false;
557 sampler::Sampler* sampler = logger->sampler();
558 generator_.reset(new ProfileGenerator(profiles_.get()));
559 processor_.reset(new ProfilerEventsProcessor(generator_.get(), sampler,
560 sampling_interval_));
561 is_profiling_ = true;
562 isolate_->set_is_profiling(true);
563 // Enumerate stuff we already have in the heap.
564 DCHECK(isolate_->heap()->HasBeenSetUp());
565 isolate_->code_event_dispatcher()->AddListener(this);
566 if (!FLAG_prof_browser_mode) {
567 logger->LogCodeObjects();
568 }
569 logger->LogCompiledFunctions();
570 logger->LogAccessorCallbacks();
571 LogBuiltins();
572 // Enable stack sampling.
573 sampler->SetHasProcessingThread(true);
574 sampler->IncreaseProfilingDepth();
575 processor_->AddCurrentStack(isolate_);
576 processor_->StartSynchronously();
577 }
578
579
580 CpuProfile* CpuProfiler::StopProfiling(const char* title) {
581 if (!is_profiling_) return nullptr;
582 StopProcessorIfLastProfile(title);
583 CpuProfile* result = profiles_->StopProfiling(title);
584 if (result) {
585 result->Print();
586 }
587 return result;
588 }
589
590
591 CpuProfile* CpuProfiler::StopProfiling(String* title) {
592 if (!is_profiling_) return nullptr;
593 const char* profile_title = profiles_->GetName(title);
594 StopProcessorIfLastProfile(profile_title);
595 return profiles_->StopProfiling(profile_title);
596 }
597
598
599 void CpuProfiler::StopProcessorIfLastProfile(const char* title) {
600 if (profiles_->IsLastProfile(title)) {
601 StopProcessor();
602 }
603 }
604
605
606 void CpuProfiler::StopProcessor() {
607 Logger* logger = isolate_->logger();
608 sampler::Sampler* sampler =
609 reinterpret_cast<sampler::Sampler*>(logger->ticker_);
610 is_profiling_ = false;
611 isolate_->set_is_profiling(false);
612 isolate_->code_event_dispatcher()->RemoveListener(this);
613 processor_->StopSynchronously();
614 processor_.reset();
615 generator_.reset();
616 sampler->SetHasProcessingThread(false);
617 sampler->DecreaseProfilingDepth();
618 logger->is_logging_ = saved_is_logging_;
619 }
620
621
622 void CpuProfiler::LogBuiltins() {
623 Builtins* builtins = isolate_->builtins();
624 DCHECK(builtins->is_initialized());
625 for (int i = 0; i < Builtins::builtin_count; i++) {
626 CodeEventsContainer evt_rec(CodeEventRecord::REPORT_BUILTIN);
627 ReportBuiltinEventRecord* rec = &evt_rec.ReportBuiltinEventRecord_;
628 Builtins::Name id = static_cast<Builtins::Name>(i);
629 rec->start = builtins->builtin(id)->address();
630 rec->builtin_id = id;
631 processor_->Enqueue(evt_rec);
632 }
633 }
634
635
636 } // namespace internal 351 } // namespace internal
637 } // namespace v8 352 } // namespace v8
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698