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

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: Move CpuProfilerManager to .cc 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 namespace {
21 16
17 #define CALL_CODE_EVENT_HANDLER(evt_rec) \
18 do { \
19 if (code_event_handler_) code_event_handler_(evt_rec); \
20 } while (false);
22 21
23 ProfilerEventsProcessor::ProfilerEventsProcessor(ProfileGenerator* generator, 22 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 23
24 } // namespace
34 25
35 ProfilerEventsProcessor::~ProfilerEventsProcessor() {} 26 ProfilerListener::~ProfilerListener() {
36 27 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 } 28 }
42 29
43 30 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); 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( 34 rec->entry = NewCodeEntry(Logger::CALLBACK_TAG, GetName(name));
208 Logger::CALLBACK_TAG,
209 profiles_->GetName(name));
210 rec->size = 1; 35 rec->size = 1;
211 processor_->Enqueue(evt_rec); 36 CALL_CODE_EVENT_HANDLER(evt_rec);
212 } 37 }
213 38
214 void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, 39 void ProfilerListener::CodeCreateEvent(Logger::LogEventsAndTags tag,
215 AbstractCode* code, const char* name) { 40 AbstractCode* code, const char* name) {
216 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); 41 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION);
217 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; 42 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
218 rec->start = code->address(); 43 rec->start = code->address();
219 rec->entry = profiles_->NewCodeEntry( 44 rec->entry = NewCodeEntry(
220 tag, profiles_->GetFunctionName(name), CodeEntry::kEmptyNamePrefix, 45 tag, GetFunctionName(name), CodeEntry::kEmptyNamePrefix,
221 CodeEntry::kEmptyResourceName, CpuProfileNode::kNoLineNumberInfo, 46 CodeEntry::kEmptyResourceName, CpuProfileNode::kNoLineNumberInfo,
222 CpuProfileNode::kNoColumnNumberInfo, NULL, code->instruction_start()); 47 CpuProfileNode::kNoColumnNumberInfo, NULL, code->instruction_start());
223 RecordInliningInfo(rec->entry, code); 48 RecordInliningInfo(rec->entry, code);
224 rec->size = code->ExecutableSize(); 49 rec->size = code->ExecutableSize();
225 processor_->Enqueue(evt_rec); 50 CALL_CODE_EVENT_HANDLER(evt_rec);
226 } 51 }
227 52
228 void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, 53 void ProfilerListener::CodeCreateEvent(Logger::LogEventsAndTags tag,
229 AbstractCode* code, Name* name) { 54 AbstractCode* code, Name* name) {
230 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); 55 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION);
231 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; 56 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
232 rec->start = code->address(); 57 rec->start = code->address();
233 rec->entry = profiles_->NewCodeEntry( 58 rec->entry = NewCodeEntry(
234 tag, profiles_->GetFunctionName(name), CodeEntry::kEmptyNamePrefix, 59 tag, GetFunctionName(name), CodeEntry::kEmptyNamePrefix,
235 CodeEntry::kEmptyResourceName, CpuProfileNode::kNoLineNumberInfo, 60 CodeEntry::kEmptyResourceName, CpuProfileNode::kNoLineNumberInfo,
236 CpuProfileNode::kNoColumnNumberInfo, NULL, code->instruction_start()); 61 CpuProfileNode::kNoColumnNumberInfo, NULL, code->instruction_start());
237 RecordInliningInfo(rec->entry, code); 62 RecordInliningInfo(rec->entry, code);
238 rec->size = code->ExecutableSize(); 63 rec->size = code->ExecutableSize();
239 processor_->Enqueue(evt_rec); 64 CALL_CODE_EVENT_HANDLER(evt_rec);
240 } 65 }
241 66
242 void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, 67 void ProfilerListener::CodeCreateEvent(Logger::LogEventsAndTags tag,
243 AbstractCode* code, 68 AbstractCode* code,
244 SharedFunctionInfo* shared, 69 SharedFunctionInfo* shared,
245 Name* script_name) { 70 Name* script_name) {
246 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); 71 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION);
247 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; 72 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
248 rec->start = code->address(); 73 rec->start = code->address();
249 rec->entry = profiles_->NewCodeEntry( 74 rec->entry = NewCodeEntry(
250 tag, profiles_->GetFunctionName(shared->DebugName()), 75 tag, GetFunctionName(shared->DebugName()), CodeEntry::kEmptyNamePrefix,
251 CodeEntry::kEmptyNamePrefix, 76 GetName(InferScriptName(script_name, shared)),
252 profiles_->GetName(InferScriptName(script_name, shared)),
253 CpuProfileNode::kNoLineNumberInfo, CpuProfileNode::kNoColumnNumberInfo, 77 CpuProfileNode::kNoLineNumberInfo, CpuProfileNode::kNoColumnNumberInfo,
254 NULL, code->instruction_start()); 78 NULL, code->instruction_start());
255 RecordInliningInfo(rec->entry, code); 79 RecordInliningInfo(rec->entry, code);
256 rec->entry->FillFunctionInfo(shared); 80 rec->entry->FillFunctionInfo(shared);
257 rec->size = code->ExecutableSize(); 81 rec->size = code->ExecutableSize();
258 processor_->Enqueue(evt_rec); 82 CALL_CODE_EVENT_HANDLER(evt_rec);
259 } 83 }
260 84
261 void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, 85 void ProfilerListener::CodeCreateEvent(Logger::LogEventsAndTags tag,
262 AbstractCode* abstract_code, 86 AbstractCode* abstract_code,
263 SharedFunctionInfo* shared, Name* script_name, 87 SharedFunctionInfo* shared,
264 int line, int column) { 88 Name* script_name, int line,
89 int column) {
265 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); 90 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION);
266 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; 91 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
267 rec->start = abstract_code->address(); 92 rec->start = abstract_code->address();
268 Script* script = Script::cast(shared->script()); 93 Script* script = Script::cast(shared->script());
269 JITLineInfoTable* line_table = NULL; 94 JITLineInfoTable* line_table = NULL;
270 if (script) { 95 if (script) {
271 if (abstract_code->IsCode()) { 96 if (abstract_code->IsCode()) {
272 Code* code = abstract_code->GetCode(); 97 Code* code = abstract_code->GetCode();
273 int start_position = shared->start_position(); 98 int start_position = shared->start_position();
274 int end_position = shared->end_position(); 99 int end_position = shared->end_position();
(...skipping 18 matching lines...) Expand all
293 line_table = new JITLineInfoTable(); 118 line_table = new JITLineInfoTable();
294 interpreter::SourcePositionTableIterator it( 119 interpreter::SourcePositionTableIterator it(
295 bytecode->source_position_table()); 120 bytecode->source_position_table());
296 for (; !it.done(); it.Advance()) { 121 for (; !it.done(); it.Advance()) {
297 int line_number = script->GetLineNumber(it.source_position()) + 1; 122 int line_number = script->GetLineNumber(it.source_position()) + 1;
298 int pc_offset = it.bytecode_offset() + BytecodeArray::kHeaderSize; 123 int pc_offset = it.bytecode_offset() + BytecodeArray::kHeaderSize;
299 line_table->SetPosition(pc_offset, line_number); 124 line_table->SetPosition(pc_offset, line_number);
300 } 125 }
301 } 126 }
302 } 127 }
303 rec->entry = profiles_->NewCodeEntry( 128 rec->entry = NewCodeEntry(
304 tag, profiles_->GetFunctionName(shared->DebugName()), 129 tag, GetFunctionName(shared->DebugName()), CodeEntry::kEmptyNamePrefix,
305 CodeEntry::kEmptyNamePrefix, 130 GetName(InferScriptName(script_name, shared)), line, column, line_table,
306 profiles_->GetName(InferScriptName(script_name, shared)), line, column, 131 abstract_code->instruction_start());
307 line_table, abstract_code->instruction_start());
308 RecordInliningInfo(rec->entry, abstract_code); 132 RecordInliningInfo(rec->entry, abstract_code);
309 RecordDeoptInlinedFrames(rec->entry, abstract_code); 133 RecordDeoptInlinedFrames(rec->entry, abstract_code);
310 rec->entry->FillFunctionInfo(shared); 134 rec->entry->FillFunctionInfo(shared);
311 rec->size = abstract_code->ExecutableSize(); 135 rec->size = abstract_code->ExecutableSize();
312 processor_->Enqueue(evt_rec); 136 CALL_CODE_EVENT_HANDLER(evt_rec);
313 } 137 }
314 138
315 void CpuProfiler::CodeCreateEvent(Logger::LogEventsAndTags tag, 139 void ProfilerListener::CodeCreateEvent(Logger::LogEventsAndTags tag,
316 AbstractCode* code, int args_count) { 140 AbstractCode* code, int args_count) {
317 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); 141 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION);
318 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; 142 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
319 rec->start = code->address(); 143 rec->start = code->address();
320 rec->entry = profiles_->NewCodeEntry( 144 rec->entry = NewCodeEntry(
321 tag, profiles_->GetName(args_count), "args_count: ", 145 tag, GetName(args_count), "args_count: ", CodeEntry::kEmptyResourceName,
322 CodeEntry::kEmptyResourceName, CpuProfileNode::kNoLineNumberInfo, 146 CpuProfileNode::kNoLineNumberInfo, CpuProfileNode::kNoColumnNumberInfo,
323 CpuProfileNode::kNoColumnNumberInfo, NULL, code->instruction_start()); 147 NULL, code->instruction_start());
324 RecordInliningInfo(rec->entry, code); 148 RecordInliningInfo(rec->entry, code);
325 rec->size = code->ExecutableSize(); 149 rec->size = code->ExecutableSize();
326 processor_->Enqueue(evt_rec); 150 CALL_CODE_EVENT_HANDLER(evt_rec);
327 } 151 }
328 152
329 void CpuProfiler::CodeMoveEvent(AbstractCode* from, Address to) { 153 void ProfilerListener::CodeMoveEvent(AbstractCode* from, Address to) {
330 CodeEventsContainer evt_rec(CodeEventRecord::CODE_MOVE); 154 CodeEventsContainer evt_rec(CodeEventRecord::CODE_MOVE);
331 CodeMoveEventRecord* rec = &evt_rec.CodeMoveEventRecord_; 155 CodeMoveEventRecord* rec = &evt_rec.CodeMoveEventRecord_;
332 rec->from = from->address(); 156 rec->from = from->address();
333 rec->to = to; 157 rec->to = to;
334 processor_->Enqueue(evt_rec); 158 CALL_CODE_EVENT_HANDLER(evt_rec);
335 } 159 }
336 160
337 void CpuProfiler::CodeDisableOptEvent(AbstractCode* code, 161 void ProfilerListener::CodeDisableOptEvent(AbstractCode* code,
338 SharedFunctionInfo* shared) { 162 SharedFunctionInfo* shared) {
339 CodeEventsContainer evt_rec(CodeEventRecord::CODE_DISABLE_OPT); 163 CodeEventsContainer evt_rec(CodeEventRecord::CODE_DISABLE_OPT);
340 CodeDisableOptEventRecord* rec = &evt_rec.CodeDisableOptEventRecord_; 164 CodeDisableOptEventRecord* rec = &evt_rec.CodeDisableOptEventRecord_;
341 rec->start = code->address(); 165 rec->start = code->address();
342 rec->bailout_reason = GetBailoutReason(shared->disable_optimization_reason()); 166 rec->bailout_reason = GetBailoutReason(shared->disable_optimization_reason());
343 processor_->Enqueue(evt_rec); 167 CALL_CODE_EVENT_HANDLER(evt_rec);
344 } 168 }
345 169
346 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) {
347 CodeEventsContainer evt_rec(CodeEventRecord::CODE_DEOPT); 172 CodeEventsContainer evt_rec(CodeEventRecord::CODE_DEOPT);
348 CodeDeoptEventRecord* rec = &evt_rec.CodeDeoptEventRecord_; 173 CodeDeoptEventRecord* rec = &evt_rec.CodeDeoptEventRecord_;
349 Deoptimizer::DeoptInfo info = Deoptimizer::GetDeoptInfo(code, pc); 174 Deoptimizer::DeoptInfo info = Deoptimizer::GetDeoptInfo(code, pc);
350 rec->start = code->address(); 175 rec->start = code->address();
351 rec->deopt_reason = Deoptimizer::GetDeoptReason(info.deopt_reason); 176 rec->deopt_reason = Deoptimizer::GetDeoptReason(info.deopt_reason);
352 rec->position = info.position; 177 rec->position = info.position;
353 rec->deopt_id = info.deopt_id; 178 rec->deopt_id = info.deopt_id;
354 processor_->Enqueue(evt_rec); 179 rec->pc = reinterpret_cast<void*>(pc);
355 processor_->AddDeoptStack(isolate_, pc, fp_to_sp_delta); 180 rec->fp_to_sp_delta = fp_to_sp_delta;
181 CALL_CODE_EVENT_HANDLER(evt_rec);
356 } 182 }
357 183
358 void CpuProfiler::GetterCallbackEvent(Name* name, Address entry_point) { 184 void ProfilerListener::GetterCallbackEvent(Name* name, Address entry_point) {
359 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); 185 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION);
360 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; 186 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
361 rec->start = entry_point; 187 rec->start = entry_point;
362 rec->entry = profiles_->NewCodeEntry( 188 rec->entry = NewCodeEntry(Logger::CALLBACK_TAG, GetName(name), "get ");
363 Logger::CALLBACK_TAG,
364 profiles_->GetName(name),
365 "get ");
366 rec->size = 1; 189 rec->size = 1;
367 processor_->Enqueue(evt_rec); 190 CALL_CODE_EVENT_HANDLER(evt_rec);
368 } 191 }
369 192
370 void CpuProfiler::RegExpCodeCreateEvent(AbstractCode* code, String* source) { 193 void ProfilerListener::RegExpCodeCreateEvent(AbstractCode* code,
194 String* source) {
371 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); 195 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION);
372 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; 196 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
373 rec->start = code->address(); 197 rec->start = code->address();
374 rec->entry = profiles_->NewCodeEntry( 198 rec->entry = NewCodeEntry(
375 Logger::REG_EXP_TAG, profiles_->GetName(source), "RegExp: ", 199 Logger::REG_EXP_TAG, GetName(source), "RegExp: ",
376 CodeEntry::kEmptyResourceName, CpuProfileNode::kNoLineNumberInfo, 200 CodeEntry::kEmptyResourceName, CpuProfileNode::kNoLineNumberInfo,
377 CpuProfileNode::kNoColumnNumberInfo, NULL, code->instruction_start()); 201 CpuProfileNode::kNoColumnNumberInfo, NULL, code->instruction_start());
378 rec->size = code->ExecutableSize(); 202 rec->size = code->ExecutableSize();
379 processor_->Enqueue(evt_rec); 203 CALL_CODE_EVENT_HANDLER(evt_rec);
380 } 204 }
381 205
382 206 void ProfilerListener::SetterCallbackEvent(Name* name, Address entry_point) {
383 void CpuProfiler::SetterCallbackEvent(Name* name, Address entry_point) {
384 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); 207 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION);
385 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; 208 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
386 rec->start = entry_point; 209 rec->start = entry_point;
387 rec->entry = profiles_->NewCodeEntry( 210 rec->entry = NewCodeEntry(Logger::CALLBACK_TAG, GetName(name), "set ");
388 Logger::CALLBACK_TAG,
389 profiles_->GetName(name),
390 "set ");
391 rec->size = 1; 211 rec->size = 1;
392 processor_->Enqueue(evt_rec); 212 CALL_CODE_EVENT_HANDLER(evt_rec);
393 } 213 }
394 214
395 Name* CpuProfiler::InferScriptName(Name* name, SharedFunctionInfo* info) { 215 Name* ProfilerListener::InferScriptName(Name* name, SharedFunctionInfo* info) {
396 if (name->IsString() && String::cast(name)->length()) return name; 216 if (name->IsString() && String::cast(name)->length()) return name;
397 if (!info->script()->IsScript()) return name; 217 if (!info->script()->IsScript()) return name;
398 Object* source_url = Script::cast(info->script())->source_url(); 218 Object* source_url = Script::cast(info->script())->source_url();
399 return source_url->IsName() ? Name::cast(source_url) : name; 219 return source_url->IsName() ? Name::cast(source_url) : name;
400 } 220 }
401 221
402 void CpuProfiler::RecordInliningInfo(CodeEntry* entry, 222 void ProfilerListener::RecordInliningInfo(CodeEntry* entry,
403 AbstractCode* abstract_code) { 223 AbstractCode* abstract_code) {
404 if (!abstract_code->IsCode()) return; 224 if (!abstract_code->IsCode()) return;
405 Code* code = abstract_code->GetCode(); 225 Code* code = abstract_code->GetCode();
406 if (code->kind() != Code::OPTIMIZED_FUNCTION) return; 226 if (code->kind() != Code::OPTIMIZED_FUNCTION) return;
407 DeoptimizationInputData* deopt_input_data = 227 DeoptimizationInputData* deopt_input_data =
408 DeoptimizationInputData::cast(code->deoptimization_data()); 228 DeoptimizationInputData::cast(code->deoptimization_data());
409 int deopt_count = deopt_input_data->DeoptCount(); 229 int deopt_count = deopt_input_data->DeoptCount();
410 for (int i = 0; i < deopt_count; i++) { 230 for (int i = 0; i < deopt_count; i++) {
411 int pc_offset = deopt_input_data->Pc(i)->value(); 231 int pc_offset = deopt_input_data->Pc(i)->value();
412 if (pc_offset == -1) continue; 232 if (pc_offset == -1) continue;
413 int translation_index = deopt_input_data->TranslationIndex(i)->value(); 233 int translation_index = deopt_input_data->TranslationIndex(i)->value();
(...skipping 12 matching lines...) Expand all
426 it.Skip(Translation::NumberOfOperandsFor(opcode)); 246 it.Skip(Translation::NumberOfOperandsFor(opcode));
427 continue; 247 continue;
428 } 248 }
429 it.Next(); // Skip ast_id 249 it.Next(); // Skip ast_id
430 int shared_info_id = it.Next(); 250 int shared_info_id = it.Next();
431 it.Next(); // Skip height 251 it.Next(); // Skip height
432 SharedFunctionInfo* shared_info = SharedFunctionInfo::cast( 252 SharedFunctionInfo* shared_info = SharedFunctionInfo::cast(
433 deopt_input_data->LiteralArray()->get(shared_info_id)); 253 deopt_input_data->LiteralArray()->get(shared_info_id));
434 if (!depth++) continue; // Skip the current function itself. 254 if (!depth++) continue; // Skip the current function itself.
435 CodeEntry* inline_entry = new CodeEntry( 255 CodeEntry* inline_entry = new CodeEntry(
436 entry->tag(), profiles_->GetFunctionName(shared_info->DebugName()), 256 entry->tag(), GetFunctionName(shared_info->DebugName()),
437 CodeEntry::kEmptyNamePrefix, entry->resource_name(), 257 CodeEntry::kEmptyNamePrefix, entry->resource_name(),
438 CpuProfileNode::kNoLineNumberInfo, 258 CpuProfileNode::kNoLineNumberInfo,
439 CpuProfileNode::kNoColumnNumberInfo, NULL, code->instruction_start()); 259 CpuProfileNode::kNoColumnNumberInfo, NULL, code->instruction_start());
440 inline_entry->FillFunctionInfo(shared_info); 260 inline_entry->FillFunctionInfo(shared_info);
441 inline_stack.push_back(inline_entry); 261 inline_stack.push_back(inline_entry);
442 } 262 }
443 if (!inline_stack.empty()) { 263 if (!inline_stack.empty()) {
444 entry->AddInlineStack(pc_offset, inline_stack); 264 entry->AddInlineStack(pc_offset, inline_stack);
445 DCHECK(inline_stack.empty()); 265 DCHECK(inline_stack.empty());
446 } 266 }
447 } 267 }
448 } 268 }
449 269
450 void CpuProfiler::RecordDeoptInlinedFrames(CodeEntry* entry, 270 void ProfilerListener::RecordDeoptInlinedFrames(CodeEntry* entry,
451 AbstractCode* abstract_code) { 271 AbstractCode* abstract_code) {
452 if (abstract_code->kind() != AbstractCode::OPTIMIZED_FUNCTION) return; 272 if (abstract_code->kind() != AbstractCode::OPTIMIZED_FUNCTION) return;
453 Code* code = abstract_code->GetCode(); 273 Code* code = abstract_code->GetCode();
454 DeoptimizationInputData* deopt_input_data = 274 DeoptimizationInputData* deopt_input_data =
455 DeoptimizationInputData::cast(code->deoptimization_data()); 275 DeoptimizationInputData::cast(code->deoptimization_data());
456 int const mask = RelocInfo::ModeMask(RelocInfo::DEOPT_ID); 276 int const mask = RelocInfo::ModeMask(RelocInfo::DEOPT_ID);
457 for (RelocIterator rit(code, mask); !rit.done(); rit.next()) { 277 for (RelocIterator rit(code, mask); !rit.done(); rit.next()) {
458 RelocInfo* reloc_info = rit.rinfo(); 278 RelocInfo* reloc_info = rit.rinfo();
459 DCHECK(RelocInfo::IsDeoptId(reloc_info->rmode())); 279 DCHECK(RelocInfo::IsDeoptId(reloc_info->rmode()));
460 int deopt_id = static_cast<int>(reloc_info->data()); 280 int deopt_id = static_cast<int>(reloc_info->data());
461 int translation_index = 281 int translation_index =
(...skipping 26 matching lines...) Expand all
488 CodeEntry::DeoptInlinedFrame frame = {source_position, script_id}; 308 CodeEntry::DeoptInlinedFrame frame = {source_position, script_id};
489 inlined_frames.push_back(frame); 309 inlined_frames.push_back(frame);
490 } 310 }
491 if (!inlined_frames.empty() && !entry->HasDeoptInlinedFramesFor(deopt_id)) { 311 if (!inlined_frames.empty() && !entry->HasDeoptInlinedFramesFor(deopt_id)) {
492 entry->AddDeoptInlinedFrames(deopt_id, inlined_frames); 312 entry->AddDeoptInlinedFrames(deopt_id, inlined_frames);
493 DCHECK(inlined_frames.empty()); 313 DCHECK(inlined_frames.empty());
494 } 314 }
495 } 315 }
496 } 316 }
497 317
498 CpuProfiler::CpuProfiler(Isolate* isolate) 318 void ProfilerListener::ResolveCodeEvent() { is_resolving_code_entry_ = true; }
499 : isolate_(isolate), 319
500 sampling_interval_(base::TimeDelta::FromMicroseconds( 320 CodeEntry* ProfilerListener::NewCodeEntry(
501 FLAG_cpu_profiler_sampling_interval)), 321 Logger::LogEventsAndTags tag, const char* name, const char* name_prefix,
502 profiles_(new CpuProfilesCollection(isolate->heap())), 322 const char* resource_name, int line_number, int column_number,
503 generator_(NULL), 323 JITLineInfoTable* line_info, Address instruction_start) {
504 processor_(NULL), 324 CodeEntry* code_entry =
505 is_profiling_(false) { 325 new CodeEntry(tag, name, name_prefix, resource_name, line_number,
326 column_number, line_info, instruction_start);
327 code_entries_.Add(code_entry);
328 return code_entry;
506 } 329 }
507 330
508 331 void ProfilerListener::RegisterCodeEventHandler(
509 CpuProfiler::CpuProfiler(Isolate* isolate, 332 ProfilerCodeEventHandler code_event_handler) {
510 CpuProfilesCollection* test_profiles, 333 code_event_handler_ = code_event_handler;
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 } 334 }
521 335
522
523 CpuProfiler::~CpuProfiler() {
524 DCHECK(!is_profiling_);
525 delete profiles_;
526 }
527
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 336 } // namespace internal
646 } // namespace v8 337 } // namespace v8
OLDNEW
« src/profiler/cpu-profiler.cc ('K') | « src/profiler/profiler-listener.h ('k') | src/v8.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698