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 #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 namespace { |
21 | 16 |
17 #define CALL_CODE_EVENT_HANDLER(evt_rec) \ | |
18 do { \ | |
19 for (int i = 0, length = observers_.length(); i < length; ++i) { \ | |
20 observers_[i]->CodeEventHandler(evt_rec); \ | |
21 } \ | |
22 } while (false); | |
22 | 23 |
23 ProfilerEventsProcessor::ProfilerEventsProcessor(ProfileGenerator* generator, | 24 static void DeleteCodeEntry(CodeEntry** entry_ptr) { delete *entry_ptr; } |
24 sampler::Sampler* sampler, | |
25 base::TimeDelta period) | |
26 : Thread(Thread::Options("v8:ProfEvntProc", kProfilerStackSize)), | |
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 | 25 |
26 } // namespace | |
34 | 27 |
35 ProfilerEventsProcessor::~ProfilerEventsProcessor() {} | 28 ProfilerListener::~ProfilerListener() { |
36 | 29 code_entries_.Iterate(DeleteCodeEntry); |
37 | |
38 void ProfilerEventsProcessor::Enqueue(const CodeEventsContainer& event) { | |
39 event.generic.order = last_code_event_id_.Increment(1); | |
40 events_buffer_.Enqueue(event); | |
41 } | 30 } |
42 | 31 |
43 | 32 void ProfilerListener::CallbackEvent(Name* name, Address entry_point) { |
44 void ProfilerEventsProcessor::AddDeoptStack(Isolate* isolate, Address from, | |
45 int fp_to_sp_delta) { | |
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 } | |
200 } | |
201 | |
202 | |
203 void CpuProfiler::CallbackEvent(Name* name, Address entry_point) { | |
204 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); | 33 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
205 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; | 34 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
206 rec->start = entry_point; | 35 rec->start = entry_point; |
207 rec->entry = profiles_->NewCodeEntry( | 36 rec->entry = NewCodeEntry(Logger::CALLBACK_TAG, GetName(name)); |
208 Logger::CALLBACK_TAG, | |
209 profiles_->GetName(name)); | |
210 rec->size = 1; | 37 rec->size = 1; |
211 processor_->Enqueue(evt_rec); | 38 CALL_CODE_EVENT_HANDLER(evt_rec); |
alph
2016/06/14 03:48:45
Why macros? Seems to fit just a regular function.
lpy
2016/06/14 09:53:47
Done.
| |
212 } | 39 } |
213 | 40 |
214 void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, | 41 void ProfilerListener::CodeCreateEvent(Logger::LogEventsAndTags tag, |
215 AbstractCode* code, const char* name) { | 42 AbstractCode* code, const char* name) { |
216 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); | 43 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
217 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; | 44 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
218 rec->start = code->address(); | 45 rec->start = code->address(); |
219 rec->entry = profiles_->NewCodeEntry( | 46 rec->entry = NewCodeEntry( |
220 tag, profiles_->GetFunctionName(name), CodeEntry::kEmptyNamePrefix, | 47 tag, GetFunctionName(name), CodeEntry::kEmptyNamePrefix, |
221 CodeEntry::kEmptyResourceName, CpuProfileNode::kNoLineNumberInfo, | 48 CodeEntry::kEmptyResourceName, CpuProfileNode::kNoLineNumberInfo, |
222 CpuProfileNode::kNoColumnNumberInfo, NULL, code->instruction_start()); | 49 CpuProfileNode::kNoColumnNumberInfo, NULL, code->instruction_start()); |
223 RecordInliningInfo(rec->entry, code); | 50 RecordInliningInfo(rec->entry, code); |
224 rec->size = code->ExecutableSize(); | 51 rec->size = code->ExecutableSize(); |
225 processor_->Enqueue(evt_rec); | 52 CALL_CODE_EVENT_HANDLER(evt_rec); |
226 } | 53 } |
227 | 54 |
228 void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, | 55 void ProfilerListener::CodeCreateEvent(Logger::LogEventsAndTags tag, |
229 AbstractCode* code, Name* name) { | 56 AbstractCode* code, Name* name) { |
230 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); | 57 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
231 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; | 58 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
232 rec->start = code->address(); | 59 rec->start = code->address(); |
233 rec->entry = profiles_->NewCodeEntry( | 60 rec->entry = NewCodeEntry( |
234 tag, profiles_->GetFunctionName(name), CodeEntry::kEmptyNamePrefix, | 61 tag, GetFunctionName(name), CodeEntry::kEmptyNamePrefix, |
235 CodeEntry::kEmptyResourceName, CpuProfileNode::kNoLineNumberInfo, | 62 CodeEntry::kEmptyResourceName, CpuProfileNode::kNoLineNumberInfo, |
236 CpuProfileNode::kNoColumnNumberInfo, NULL, code->instruction_start()); | 63 CpuProfileNode::kNoColumnNumberInfo, NULL, code->instruction_start()); |
237 RecordInliningInfo(rec->entry, code); | 64 RecordInliningInfo(rec->entry, code); |
238 rec->size = code->ExecutableSize(); | 65 rec->size = code->ExecutableSize(); |
239 processor_->Enqueue(evt_rec); | 66 CALL_CODE_EVENT_HANDLER(evt_rec); |
240 } | 67 } |
241 | 68 |
242 void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, | 69 void ProfilerListener::CodeCreateEvent(Logger::LogEventsAndTags tag, |
243 AbstractCode* code, | 70 AbstractCode* code, |
244 SharedFunctionInfo* shared, | 71 SharedFunctionInfo* shared, |
245 Name* script_name) { | 72 Name* script_name) { |
246 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); | 73 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
247 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; | 74 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
248 rec->start = code->address(); | 75 rec->start = code->address(); |
249 rec->entry = profiles_->NewCodeEntry( | 76 rec->entry = NewCodeEntry( |
250 tag, profiles_->GetFunctionName(shared->DebugName()), | 77 tag, GetFunctionName(shared->DebugName()), CodeEntry::kEmptyNamePrefix, |
251 CodeEntry::kEmptyNamePrefix, | 78 GetName(InferScriptName(script_name, shared)), |
252 profiles_->GetName(InferScriptName(script_name, shared)), | |
253 CpuProfileNode::kNoLineNumberInfo, CpuProfileNode::kNoColumnNumberInfo, | 79 CpuProfileNode::kNoLineNumberInfo, CpuProfileNode::kNoColumnNumberInfo, |
254 NULL, code->instruction_start()); | 80 NULL, code->instruction_start()); |
255 RecordInliningInfo(rec->entry, code); | 81 RecordInliningInfo(rec->entry, code); |
256 rec->entry->FillFunctionInfo(shared); | 82 rec->entry->FillFunctionInfo(shared); |
257 rec->size = code->ExecutableSize(); | 83 rec->size = code->ExecutableSize(); |
258 processor_->Enqueue(evt_rec); | 84 CALL_CODE_EVENT_HANDLER(evt_rec); |
259 } | 85 } |
260 | 86 |
261 void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, | 87 void ProfilerListener::CodeCreateEvent(Logger::LogEventsAndTags tag, |
262 AbstractCode* abstract_code, | 88 AbstractCode* abstract_code, |
263 SharedFunctionInfo* shared, Name* script_name, | 89 SharedFunctionInfo* shared, |
264 int line, int column) { | 90 Name* script_name, int line, |
91 int column) { | |
265 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); | 92 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
266 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; | 93 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
267 rec->start = abstract_code->address(); | 94 rec->start = abstract_code->address(); |
268 Script* script = Script::cast(shared->script()); | 95 Script* script = Script::cast(shared->script()); |
269 JITLineInfoTable* line_table = NULL; | 96 JITLineInfoTable* line_table = NULL; |
270 if (script) { | 97 if (script) { |
271 if (abstract_code->IsCode()) { | 98 if (abstract_code->IsCode()) { |
272 Code* code = abstract_code->GetCode(); | 99 Code* code = abstract_code->GetCode(); |
273 int start_position = shared->start_position(); | 100 int start_position = shared->start_position(); |
274 int end_position = shared->end_position(); | 101 int end_position = shared->end_position(); |
(...skipping 18 matching lines...) Expand all Loading... | |
293 line_table = new JITLineInfoTable(); | 120 line_table = new JITLineInfoTable(); |
294 interpreter::SourcePositionTableIterator it( | 121 interpreter::SourcePositionTableIterator it( |
295 bytecode->source_position_table()); | 122 bytecode->source_position_table()); |
296 for (; !it.done(); it.Advance()) { | 123 for (; !it.done(); it.Advance()) { |
297 int line_number = script->GetLineNumber(it.source_position()) + 1; | 124 int line_number = script->GetLineNumber(it.source_position()) + 1; |
298 int pc_offset = it.bytecode_offset() + BytecodeArray::kHeaderSize; | 125 int pc_offset = it.bytecode_offset() + BytecodeArray::kHeaderSize; |
299 line_table->SetPosition(pc_offset, line_number); | 126 line_table->SetPosition(pc_offset, line_number); |
300 } | 127 } |
301 } | 128 } |
302 } | 129 } |
303 rec->entry = profiles_->NewCodeEntry( | 130 rec->entry = NewCodeEntry( |
304 tag, profiles_->GetFunctionName(shared->DebugName()), | 131 tag, GetFunctionName(shared->DebugName()), CodeEntry::kEmptyNamePrefix, |
305 CodeEntry::kEmptyNamePrefix, | 132 GetName(InferScriptName(script_name, shared)), line, column, line_table, |
306 profiles_->GetName(InferScriptName(script_name, shared)), line, column, | 133 abstract_code->instruction_start()); |
307 line_table, abstract_code->instruction_start()); | |
308 RecordInliningInfo(rec->entry, abstract_code); | 134 RecordInliningInfo(rec->entry, abstract_code); |
309 RecordDeoptInlinedFrames(rec->entry, abstract_code); | 135 RecordDeoptInlinedFrames(rec->entry, abstract_code); |
310 rec->entry->FillFunctionInfo(shared); | 136 rec->entry->FillFunctionInfo(shared); |
311 rec->size = abstract_code->ExecutableSize(); | 137 rec->size = abstract_code->ExecutableSize(); |
312 processor_->Enqueue(evt_rec); | 138 CALL_CODE_EVENT_HANDLER(evt_rec); |
313 } | 139 } |
314 | 140 |
315 void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, | 141 void ProfilerListener::CodeCreateEvent(Logger::LogEventsAndTags tag, |
316 AbstractCode* code, int args_count) { | 142 AbstractCode* code, int args_count) { |
317 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); | 143 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
318 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; | 144 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
319 rec->start = code->address(); | 145 rec->start = code->address(); |
320 rec->entry = profiles_->NewCodeEntry( | 146 rec->entry = NewCodeEntry( |
321 tag, profiles_->GetName(args_count), "args_count: ", | 147 tag, GetName(args_count), "args_count: ", CodeEntry::kEmptyResourceName, |
322 CodeEntry::kEmptyResourceName, CpuProfileNode::kNoLineNumberInfo, | 148 CpuProfileNode::kNoLineNumberInfo, CpuProfileNode::kNoColumnNumberInfo, |
323 CpuProfileNode::kNoColumnNumberInfo, NULL, code->instruction_start()); | 149 NULL, code->instruction_start()); |
324 RecordInliningInfo(rec->entry, code); | 150 RecordInliningInfo(rec->entry, code); |
325 rec->size = code->ExecutableSize(); | 151 rec->size = code->ExecutableSize(); |
326 processor_->Enqueue(evt_rec); | 152 CALL_CODE_EVENT_HANDLER(evt_rec); |
327 } | 153 } |
328 | 154 |
329 void CpuProfiler::CodeMoveEvent(AbstractCode* from, Address to) { | 155 void ProfilerListener::CodeMoveEvent(AbstractCode* from, Address to) { |
330 CodeEventsContainer evt_rec(CodeEventRecord::CODE_MOVE); | 156 CodeEventsContainer evt_rec(CodeEventRecord::CODE_MOVE); |
331 CodeMoveEventRecord* rec = &evt_rec.CodeMoveEventRecord_; | 157 CodeMoveEventRecord* rec = &evt_rec.CodeMoveEventRecord_; |
332 rec->from = from->address(); | 158 rec->from = from->address(); |
333 rec->to = to; | 159 rec->to = to; |
334 processor_->Enqueue(evt_rec); | 160 CALL_CODE_EVENT_HANDLER(evt_rec); |
335 } | 161 } |
336 | 162 |
337 void CpuProfiler::CodeDisableOptEvent(AbstractCode* code, | 163 void ProfilerListener::CodeDisableOptEvent(AbstractCode* code, |
338 SharedFunctionInfo* shared) { | 164 SharedFunctionInfo* shared) { |
339 CodeEventsContainer evt_rec(CodeEventRecord::CODE_DISABLE_OPT); | 165 CodeEventsContainer evt_rec(CodeEventRecord::CODE_DISABLE_OPT); |
340 CodeDisableOptEventRecord* rec = &evt_rec.CodeDisableOptEventRecord_; | 166 CodeDisableOptEventRecord* rec = &evt_rec.CodeDisableOptEventRecord_; |
341 rec->start = code->address(); | 167 rec->start = code->address(); |
342 rec->bailout_reason = GetBailoutReason(shared->disable_optimization_reason()); | 168 rec->bailout_reason = GetBailoutReason(shared->disable_optimization_reason()); |
343 processor_->Enqueue(evt_rec); | 169 CALL_CODE_EVENT_HANDLER(evt_rec); |
344 } | 170 } |
345 | 171 |
346 void CpuProfiler::CodeDeoptEvent(Code* code, Address pc, int fp_to_sp_delta) { | 172 void ProfilerListener::CodeDeoptEvent(Code* code, Address pc, |
173 int fp_to_sp_delta) { | |
347 CodeEventsContainer evt_rec(CodeEventRecord::CODE_DEOPT); | 174 CodeEventsContainer evt_rec(CodeEventRecord::CODE_DEOPT); |
348 CodeDeoptEventRecord* rec = &evt_rec.CodeDeoptEventRecord_; | 175 CodeDeoptEventRecord* rec = &evt_rec.CodeDeoptEventRecord_; |
349 Deoptimizer::DeoptInfo info = Deoptimizer::GetDeoptInfo(code, pc); | 176 Deoptimizer::DeoptInfo info = Deoptimizer::GetDeoptInfo(code, pc); |
350 rec->start = code->address(); | 177 rec->start = code->address(); |
351 rec->deopt_reason = Deoptimizer::GetDeoptReason(info.deopt_reason); | 178 rec->deopt_reason = Deoptimizer::GetDeoptReason(info.deopt_reason); |
352 rec->position = info.position; | 179 rec->position = info.position; |
353 rec->deopt_id = info.deopt_id; | 180 rec->deopt_id = info.deopt_id; |
354 processor_->Enqueue(evt_rec); | 181 rec->pc = reinterpret_cast<void*>(pc); |
355 processor_->AddDeoptStack(isolate_, pc, fp_to_sp_delta); | 182 rec->fp_to_sp_delta = fp_to_sp_delta; |
183 CALL_CODE_EVENT_HANDLER(evt_rec); | |
356 } | 184 } |
357 | 185 |
358 void CpuProfiler::GetterCallbackEvent(Name* name, Address entry_point) { | 186 void ProfilerListener::GetterCallbackEvent(Name* name, Address entry_point) { |
359 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); | 187 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
360 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; | 188 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
361 rec->start = entry_point; | 189 rec->start = entry_point; |
362 rec->entry = profiles_->NewCodeEntry( | 190 rec->entry = NewCodeEntry(Logger::CALLBACK_TAG, GetName(name), "get "); |
363 Logger::CALLBACK_TAG, | |
364 profiles_->GetName(name), | |
365 "get "); | |
366 rec->size = 1; | 191 rec->size = 1; |
367 processor_->Enqueue(evt_rec); | 192 CALL_CODE_EVENT_HANDLER(evt_rec); |
368 } | 193 } |
369 | 194 |
370 void CpuProfiler::RegExpCodeCreateEvent(AbstractCode* code, String* source) { | 195 void ProfilerListener::RegExpCodeCreateEvent(AbstractCode* code, |
196 String* source) { | |
371 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); | 197 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
372 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; | 198 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
373 rec->start = code->address(); | 199 rec->start = code->address(); |
374 rec->entry = profiles_->NewCodeEntry( | 200 rec->entry = NewCodeEntry( |
375 Logger::REG_EXP_TAG, profiles_->GetName(source), "RegExp: ", | 201 Logger::REG_EXP_TAG, GetName(source), "RegExp: ", |
376 CodeEntry::kEmptyResourceName, CpuProfileNode::kNoLineNumberInfo, | 202 CodeEntry::kEmptyResourceName, CpuProfileNode::kNoLineNumberInfo, |
377 CpuProfileNode::kNoColumnNumberInfo, NULL, code->instruction_start()); | 203 CpuProfileNode::kNoColumnNumberInfo, NULL, code->instruction_start()); |
378 rec->size = code->ExecutableSize(); | 204 rec->size = code->ExecutableSize(); |
379 processor_->Enqueue(evt_rec); | 205 CALL_CODE_EVENT_HANDLER(evt_rec); |
380 } | 206 } |
381 | 207 |
382 | 208 void ProfilerListener::SetterCallbackEvent(Name* name, Address entry_point) { |
383 void CpuProfiler::SetterCallbackEvent(Name* name, Address entry_point) { | |
384 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); | 209 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
385 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; | 210 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
386 rec->start = entry_point; | 211 rec->start = entry_point; |
387 rec->entry = profiles_->NewCodeEntry( | 212 rec->entry = NewCodeEntry(Logger::CALLBACK_TAG, GetName(name), "set "); |
388 Logger::CALLBACK_TAG, | |
389 profiles_->GetName(name), | |
390 "set "); | |
391 rec->size = 1; | 213 rec->size = 1; |
392 processor_->Enqueue(evt_rec); | 214 CALL_CODE_EVENT_HANDLER(evt_rec); |
393 } | 215 } |
394 | 216 |
395 Name* CpuProfiler::InferScriptName(Name* name, SharedFunctionInfo* info) { | 217 Name* ProfilerListener::InferScriptName(Name* name, SharedFunctionInfo* info) { |
396 if (name->IsString() && String::cast(name)->length()) return name; | 218 if (name->IsString() && String::cast(name)->length()) return name; |
397 if (!info->script()->IsScript()) return name; | 219 if (!info->script()->IsScript()) return name; |
398 Object* source_url = Script::cast(info->script())->source_url(); | 220 Object* source_url = Script::cast(info->script())->source_url(); |
399 return source_url->IsName() ? Name::cast(source_url) : name; | 221 return source_url->IsName() ? Name::cast(source_url) : name; |
400 } | 222 } |
401 | 223 |
402 void CpuProfiler::RecordInliningInfo(CodeEntry* entry, | 224 void ProfilerListener::RecordInliningInfo(CodeEntry* entry, |
403 AbstractCode* abstract_code) { | 225 AbstractCode* abstract_code) { |
404 if (!abstract_code->IsCode()) return; | 226 if (!abstract_code->IsCode()) return; |
405 Code* code = abstract_code->GetCode(); | 227 Code* code = abstract_code->GetCode(); |
406 if (code->kind() != Code::OPTIMIZED_FUNCTION) return; | 228 if (code->kind() != Code::OPTIMIZED_FUNCTION) return; |
407 DeoptimizationInputData* deopt_input_data = | 229 DeoptimizationInputData* deopt_input_data = |
408 DeoptimizationInputData::cast(code->deoptimization_data()); | 230 DeoptimizationInputData::cast(code->deoptimization_data()); |
409 int deopt_count = deopt_input_data->DeoptCount(); | 231 int deopt_count = deopt_input_data->DeoptCount(); |
410 for (int i = 0; i < deopt_count; i++) { | 232 for (int i = 0; i < deopt_count; i++) { |
411 int pc_offset = deopt_input_data->Pc(i)->value(); | 233 int pc_offset = deopt_input_data->Pc(i)->value(); |
412 if (pc_offset == -1) continue; | 234 if (pc_offset == -1) continue; |
413 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 Loading... | |
426 it.Skip(Translation::NumberOfOperandsFor(opcode)); | 248 it.Skip(Translation::NumberOfOperandsFor(opcode)); |
427 continue; | 249 continue; |
428 } | 250 } |
429 it.Next(); // Skip ast_id | 251 it.Next(); // Skip ast_id |
430 int shared_info_id = it.Next(); | 252 int shared_info_id = it.Next(); |
431 it.Next(); // Skip height | 253 it.Next(); // Skip height |
432 SharedFunctionInfo* shared_info = SharedFunctionInfo::cast( | 254 SharedFunctionInfo* shared_info = SharedFunctionInfo::cast( |
433 deopt_input_data->LiteralArray()->get(shared_info_id)); | 255 deopt_input_data->LiteralArray()->get(shared_info_id)); |
434 if (!depth++) continue; // Skip the current function itself. | 256 if (!depth++) continue; // Skip the current function itself. |
435 CodeEntry* inline_entry = new CodeEntry( | 257 CodeEntry* inline_entry = new CodeEntry( |
436 entry->tag(), profiles_->GetFunctionName(shared_info->DebugName()), | 258 entry->tag(), GetFunctionName(shared_info->DebugName()), |
437 CodeEntry::kEmptyNamePrefix, entry->resource_name(), | 259 CodeEntry::kEmptyNamePrefix, entry->resource_name(), |
438 CpuProfileNode::kNoLineNumberInfo, | 260 CpuProfileNode::kNoLineNumberInfo, |
439 CpuProfileNode::kNoColumnNumberInfo, NULL, code->instruction_start()); | 261 CpuProfileNode::kNoColumnNumberInfo, NULL, code->instruction_start()); |
440 inline_entry->FillFunctionInfo(shared_info); | 262 inline_entry->FillFunctionInfo(shared_info); |
441 inline_stack.push_back(inline_entry); | 263 inline_stack.push_back(inline_entry); |
442 } | 264 } |
443 if (!inline_stack.empty()) { | 265 if (!inline_stack.empty()) { |
444 entry->AddInlineStack(pc_offset, inline_stack); | 266 entry->AddInlineStack(pc_offset, inline_stack); |
445 DCHECK(inline_stack.empty()); | 267 DCHECK(inline_stack.empty()); |
446 } | 268 } |
447 } | 269 } |
448 } | 270 } |
449 | 271 |
450 void CpuProfiler::RecordDeoptInlinedFrames(CodeEntry* entry, | 272 void ProfilerListener::RecordDeoptInlinedFrames(CodeEntry* entry, |
451 AbstractCode* abstract_code) { | 273 AbstractCode* abstract_code) { |
452 if (abstract_code->kind() != AbstractCode::OPTIMIZED_FUNCTION) return; | 274 if (abstract_code->kind() != AbstractCode::OPTIMIZED_FUNCTION) return; |
453 Code* code = abstract_code->GetCode(); | 275 Code* code = abstract_code->GetCode(); |
454 DeoptimizationInputData* deopt_input_data = | 276 DeoptimizationInputData* deopt_input_data = |
455 DeoptimizationInputData::cast(code->deoptimization_data()); | 277 DeoptimizationInputData::cast(code->deoptimization_data()); |
456 int const mask = RelocInfo::ModeMask(RelocInfo::DEOPT_ID); | 278 int const mask = RelocInfo::ModeMask(RelocInfo::DEOPT_ID); |
457 for (RelocIterator rit(code, mask); !rit.done(); rit.next()) { | 279 for (RelocIterator rit(code, mask); !rit.done(); rit.next()) { |
458 RelocInfo* reloc_info = rit.rinfo(); | 280 RelocInfo* reloc_info = rit.rinfo(); |
459 DCHECK(RelocInfo::IsDeoptId(reloc_info->rmode())); | 281 DCHECK(RelocInfo::IsDeoptId(reloc_info->rmode())); |
460 int deopt_id = static_cast<int>(reloc_info->data()); | 282 int deopt_id = static_cast<int>(reloc_info->data()); |
461 int translation_index = | 283 int translation_index = |
(...skipping 26 matching lines...) Expand all Loading... | |
488 CodeEntry::DeoptInlinedFrame frame = {source_position, script_id}; | 310 CodeEntry::DeoptInlinedFrame frame = {source_position, script_id}; |
489 inlined_frames.push_back(frame); | 311 inlined_frames.push_back(frame); |
490 } | 312 } |
491 if (!inlined_frames.empty() && !entry->HasDeoptInlinedFramesFor(deopt_id)) { | 313 if (!inlined_frames.empty() && !entry->HasDeoptInlinedFramesFor(deopt_id)) { |
492 entry->AddDeoptInlinedFrames(deopt_id, inlined_frames); | 314 entry->AddDeoptInlinedFrames(deopt_id, inlined_frames); |
493 DCHECK(inlined_frames.empty()); | 315 DCHECK(inlined_frames.empty()); |
494 } | 316 } |
495 } | 317 } |
496 } | 318 } |
497 | 319 |
498 CpuProfiler::CpuProfiler(Isolate* isolate) | 320 void ProfilerListener::ResolveCodeEvent() { is_resolving_code_entry_ = true; } |
499 : isolate_(isolate), | 321 |
500 sampling_interval_(base::TimeDelta::FromMicroseconds( | 322 CodeEntry* ProfilerListener::NewCodeEntry( |
501 FLAG_cpu_profiler_sampling_interval)), | 323 Logger::LogEventsAndTags tag, const char* name, const char* name_prefix, |
502 profiles_(new CpuProfilesCollection(isolate->heap())), | 324 const char* resource_name, int line_number, int column_number, |
503 generator_(NULL), | 325 JITLineInfoTable* line_info, Address instruction_start) { |
504 processor_(NULL), | 326 CodeEntry* code_entry = |
505 is_profiling_(false) { | 327 new CodeEntry(tag, name, name_prefix, resource_name, line_number, |
328 column_number, line_info, instruction_start); | |
329 code_entries_.Add(code_entry); | |
330 return code_entry; | |
506 } | 331 } |
507 | 332 |
508 | 333 void ProfilerListener::RegisterObserver(CodeEventObserver* observer) { |
509 CpuProfiler::CpuProfiler(Isolate* isolate, | 334 if (observers_.Contains(observer)) return; |
510 CpuProfilesCollection* test_profiles, | 335 observers_.Add(observer); |
511 ProfileGenerator* test_generator, | |
512 ProfilerEventsProcessor* test_processor) | |
513 : isolate_(isolate), | |
514 sampling_interval_(base::TimeDelta::FromMicroseconds( | |
515 FLAG_cpu_profiler_sampling_interval)), | |
516 profiles_(test_profiles), | |
517 generator_(test_generator), | |
518 processor_(test_processor), | |
519 is_profiling_(false) { | |
520 } | 336 } |
521 | 337 |
522 | 338 void ProfilerListener::RemoveObserver(CodeEventObserver* observer) { |
523 CpuProfiler::~CpuProfiler() { | 339 observers_.RemoveElement(observer); |
524 DCHECK(!is_profiling_); | |
525 delete profiles_; | |
526 } | 340 } |
527 | 341 |
528 | |
529 void CpuProfiler::set_sampling_interval(base::TimeDelta value) { | |
530 DCHECK(!is_profiling_); | |
531 sampling_interval_ = value; | |
532 } | |
533 | |
534 | |
535 void CpuProfiler::ResetProfiles() { | |
536 delete profiles_; | |
537 profiles_ = new CpuProfilesCollection(isolate()->heap()); | |
538 } | |
539 | |
540 void CpuProfiler::CollectSample() { | |
541 if (processor_ != NULL) { | |
542 processor_->AddCurrentStack(isolate_); | |
543 } | |
544 } | |
545 | |
546 void CpuProfiler::StartProfiling(const char* title, bool record_samples) { | |
547 if (profiles_->StartProfiling(title, record_samples)) { | |
548 StartProcessorIfNotStarted(); | |
549 } | |
550 } | |
551 | |
552 | |
553 void CpuProfiler::StartProfiling(String* title, bool record_samples) { | |
554 StartProfiling(profiles_->GetName(title), record_samples); | |
555 isolate_->debug()->feature_tracker()->Track(DebugFeatureTracker::kProfiler); | |
556 } | |
557 | |
558 | |
559 void CpuProfiler::StartProcessorIfNotStarted() { | |
560 if (processor_ != NULL) { | |
561 processor_->AddCurrentStack(isolate_); | |
562 return; | |
563 } | |
564 Logger* logger = isolate_->logger(); | |
565 // Disable logging when using the new implementation. | |
566 saved_is_logging_ = logger->is_logging_; | |
567 logger->is_logging_ = false; | |
568 generator_ = new ProfileGenerator(profiles_); | |
569 sampler::Sampler* sampler = logger->sampler(); | |
570 processor_ = new ProfilerEventsProcessor( | |
571 generator_, sampler, sampling_interval_); | |
572 is_profiling_ = true; | |
573 isolate_->set_is_profiling(true); | |
574 // Enumerate stuff we already have in the heap. | |
575 DCHECK(isolate_->heap()->HasBeenSetUp()); | |
576 if (!FLAG_prof_browser_mode) { | |
577 logger->LogCodeObjects(); | |
578 } | |
579 logger->LogCompiledFunctions(); | |
580 logger->LogAccessorCallbacks(); | |
581 LogBuiltins(); | |
582 // Enable stack sampling. | |
583 sampler->SetHasProcessingThread(true); | |
584 sampler->IncreaseProfilingDepth(); | |
585 processor_->AddCurrentStack(isolate_); | |
586 processor_->StartSynchronously(); | |
587 } | |
588 | |
589 | |
590 CpuProfile* CpuProfiler::StopProfiling(const char* title) { | |
591 if (!is_profiling_) return NULL; | |
592 StopProcessorIfLastProfile(title); | |
593 CpuProfile* result = profiles_->StopProfiling(title); | |
594 if (result != NULL) { | |
595 result->Print(); | |
596 } | |
597 return result; | |
598 } | |
599 | |
600 | |
601 CpuProfile* CpuProfiler::StopProfiling(String* title) { | |
602 if (!is_profiling_) return NULL; | |
603 const char* profile_title = profiles_->GetName(title); | |
604 StopProcessorIfLastProfile(profile_title); | |
605 return profiles_->StopProfiling(profile_title); | |
606 } | |
607 | |
608 | |
609 void CpuProfiler::StopProcessorIfLastProfile(const char* title) { | |
610 if (profiles_->IsLastProfile(title)) StopProcessor(); | |
611 } | |
612 | |
613 | |
614 void CpuProfiler::StopProcessor() { | |
615 Logger* logger = isolate_->logger(); | |
616 sampler::Sampler* sampler = | |
617 reinterpret_cast<sampler::Sampler*>(logger->ticker_); | |
618 is_profiling_ = false; | |
619 isolate_->set_is_profiling(false); | |
620 processor_->StopSynchronously(); | |
621 delete processor_; | |
622 delete generator_; | |
623 processor_ = NULL; | |
624 generator_ = NULL; | |
625 sampler->SetHasProcessingThread(false); | |
626 sampler->DecreaseProfilingDepth(); | |
627 logger->is_logging_ = saved_is_logging_; | |
628 } | |
629 | |
630 | |
631 void CpuProfiler::LogBuiltins() { | |
632 Builtins* builtins = isolate_->builtins(); | |
633 DCHECK(builtins->is_initialized()); | |
634 for (int i = 0; i < Builtins::builtin_count; i++) { | |
635 CodeEventsContainer evt_rec(CodeEventRecord::REPORT_BUILTIN); | |
636 ReportBuiltinEventRecord* rec = &evt_rec.ReportBuiltinEventRecord_; | |
637 Builtins::Name id = static_cast<Builtins::Name>(i); | |
638 rec->start = builtins->builtin(id)->address(); | |
639 rec->builtin_id = id; | |
640 processor_->Enqueue(evt_rec); | |
641 } | |
642 } | |
643 | |
644 | |
645 } // namespace internal | 342 } // namespace internal |
646 } // namespace v8 | 343 } // namespace v8 |
OLD | NEW |