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