| OLD | NEW |
| 1 // Copyright 2016 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" | 5 #include "src/profiler/profiler-listener.h" |
| 6 | 6 |
| 7 #include "src/deoptimizer.h" | 7 #include "src/deoptimizer.h" |
| 8 #include "src/profiler/cpu-profiler.h" | 8 #include "src/profiler/cpu-profiler.h" |
| 9 #include "src/profiler/profile-generator-inl.h" | 9 #include "src/profiler/profile-generator-inl.h" |
| 10 #include "src/source-position-table.h" | 10 #include "src/source-position-table.h" |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 83 int column) { | 83 int column) { |
| 84 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); | 84 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
| 85 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; | 85 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 86 rec->start = abstract_code->address(); | 86 rec->start = abstract_code->address(); |
| 87 Script* script = Script::cast(shared->script()); | 87 Script* script = Script::cast(shared->script()); |
| 88 JITLineInfoTable* line_table = NULL; | 88 JITLineInfoTable* line_table = NULL; |
| 89 if (script) { | 89 if (script) { |
| 90 line_table = new JITLineInfoTable(); | 90 line_table = new JITLineInfoTable(); |
| 91 int offset = abstract_code->IsCode() ? Code::kHeaderSize | 91 int offset = abstract_code->IsCode() ? Code::kHeaderSize |
| 92 : BytecodeArray::kHeaderSize; | 92 : BytecodeArray::kHeaderSize; |
| 93 int start_position = shared->start_position(); | |
| 94 int end_position = shared->end_position(); | |
| 95 for (SourcePositionTableIterator it(abstract_code->source_position_table()); | 93 for (SourcePositionTableIterator it(abstract_code->source_position_table()); |
| 96 !it.done(); it.Advance()) { | 94 !it.done(); it.Advance()) { |
| 95 // TODO(alph,tebbi) Skipping inlined positions for now, because they might |
| 96 // refer to a different script. |
| 97 if (it.source_position().InliningId() != SourcePosition::kNotInlined) |
| 98 continue; |
| 97 int position = it.source_position().ScriptOffset(); | 99 int position = it.source_position().ScriptOffset(); |
| 98 // TODO(alph): in case of inlining the position may correspond to an | |
| 99 // inlined function source code. Do not collect positions that fall | |
| 100 // beyond the function source code. There's however a chance the | |
| 101 // inlined function has similar positions but in another script. So | |
| 102 // the proper fix is to store script_id in some form along with the | |
| 103 // inlined function positions. | |
| 104 if (position < start_position || position >= end_position) continue; | |
| 105 int line_number = script->GetLineNumber(position) + 1; | 100 int line_number = script->GetLineNumber(position) + 1; |
| 106 int pc_offset = it.code_offset() + offset; | 101 int pc_offset = it.code_offset() + offset; |
| 107 line_table->SetPosition(pc_offset, line_number); | 102 line_table->SetPosition(pc_offset, line_number); |
| 108 } | 103 } |
| 109 } | 104 } |
| 110 rec->entry = NewCodeEntry( | 105 rec->entry = NewCodeEntry( |
| 111 tag, GetFunctionName(shared->DebugName()), CodeEntry::kEmptyNamePrefix, | 106 tag, GetFunctionName(shared->DebugName()), CodeEntry::kEmptyNamePrefix, |
| 112 GetName(InferScriptName(script_name, shared)), line, column, line_table, | 107 GetName(InferScriptName(script_name, shared)), line, column, line_table, |
| 113 abstract_code->instruction_start()); | 108 abstract_code->instruction_start()); |
| 114 RecordInliningInfo(rec->entry, abstract_code); | 109 RecordInliningInfo(rec->entry, abstract_code); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 DispatchCodeEvent(evt_rec); | 144 DispatchCodeEvent(evt_rec); |
| 150 } | 145 } |
| 151 | 146 |
| 152 void ProfilerListener::CodeDeoptEvent(Code* code, Address pc, | 147 void ProfilerListener::CodeDeoptEvent(Code* code, Address pc, |
| 153 int fp_to_sp_delta) { | 148 int fp_to_sp_delta) { |
| 154 CodeEventsContainer evt_rec(CodeEventRecord::CODE_DEOPT); | 149 CodeEventsContainer evt_rec(CodeEventRecord::CODE_DEOPT); |
| 155 CodeDeoptEventRecord* rec = &evt_rec.CodeDeoptEventRecord_; | 150 CodeDeoptEventRecord* rec = &evt_rec.CodeDeoptEventRecord_; |
| 156 Deoptimizer::DeoptInfo info = Deoptimizer::GetDeoptInfo(code, pc); | 151 Deoptimizer::DeoptInfo info = Deoptimizer::GetDeoptInfo(code, pc); |
| 157 rec->start = code->address(); | 152 rec->start = code->address(); |
| 158 rec->deopt_reason = DeoptimizeReasonToString(info.deopt_reason); | 153 rec->deopt_reason = DeoptimizeReasonToString(info.deopt_reason); |
| 159 rec->position = info.position; | |
| 160 rec->deopt_id = info.deopt_id; | 154 rec->deopt_id = info.deopt_id; |
| 161 rec->pc = reinterpret_cast<void*>(pc); | 155 rec->pc = reinterpret_cast<void*>(pc); |
| 162 rec->fp_to_sp_delta = fp_to_sp_delta; | 156 rec->fp_to_sp_delta = fp_to_sp_delta; |
| 163 DispatchCodeEvent(evt_rec); | 157 DispatchCodeEvent(evt_rec); |
| 164 } | 158 } |
| 165 | 159 |
| 166 void ProfilerListener::GetterCallbackEvent(Name* name, Address entry_point) { | 160 void ProfilerListener::GetterCallbackEvent(Name* name, Address entry_point) { |
| 167 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); | 161 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
| 168 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; | 162 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 169 rec->start = entry_point; | 163 rec->start = entry_point; |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 238 if (!depth++) continue; // Skip the current function itself. | 232 if (!depth++) continue; // Skip the current function itself. |
| 239 CodeEntry* inline_entry = new CodeEntry( | 233 CodeEntry* inline_entry = new CodeEntry( |
| 240 entry->tag(), GetFunctionName(shared_info->DebugName()), | 234 entry->tag(), GetFunctionName(shared_info->DebugName()), |
| 241 CodeEntry::kEmptyNamePrefix, entry->resource_name(), | 235 CodeEntry::kEmptyNamePrefix, entry->resource_name(), |
| 242 CpuProfileNode::kNoLineNumberInfo, | 236 CpuProfileNode::kNoLineNumberInfo, |
| 243 CpuProfileNode::kNoColumnNumberInfo, NULL, code->instruction_start()); | 237 CpuProfileNode::kNoColumnNumberInfo, NULL, code->instruction_start()); |
| 244 inline_entry->FillFunctionInfo(shared_info); | 238 inline_entry->FillFunctionInfo(shared_info); |
| 245 inline_stack.push_back(inline_entry); | 239 inline_stack.push_back(inline_entry); |
| 246 } | 240 } |
| 247 if (!inline_stack.empty()) { | 241 if (!inline_stack.empty()) { |
| 248 entry->AddInlineStack(pc_offset, inline_stack); | 242 entry->AddInlineStack(pc_offset, std::move(inline_stack)); |
| 249 DCHECK(inline_stack.empty()); | |
| 250 } | 243 } |
| 251 } | 244 } |
| 252 } | 245 } |
| 253 | 246 |
| 254 void ProfilerListener::RecordDeoptInlinedFrames(CodeEntry* entry, | 247 void ProfilerListener::RecordDeoptInlinedFrames(CodeEntry* entry, |
| 255 AbstractCode* abstract_code) { | 248 AbstractCode* abstract_code) { |
| 256 if (abstract_code->kind() != AbstractCode::OPTIMIZED_FUNCTION) return; | 249 if (abstract_code->kind() != AbstractCode::OPTIMIZED_FUNCTION) return; |
| 257 Code* code = abstract_code->GetCode(); | 250 Handle<Code> code(abstract_code->GetCode()); |
| 258 DeoptimizationInputData* deopt_input_data = | 251 |
| 259 DeoptimizationInputData::cast(code->deoptimization_data()); | 252 SourcePosition last_position = SourcePosition::Unknown(); |
| 260 int const mask = RelocInfo::ModeMask(RelocInfo::DEOPT_ID); | 253 int mask = RelocInfo::ModeMask(RelocInfo::DEOPT_ID) | |
| 261 for (RelocIterator rit(code, mask); !rit.done(); rit.next()) { | 254 RelocInfo::ModeMask(RelocInfo::DEOPT_SCRIPT_OFFSET) | |
| 262 RelocInfo* reloc_info = rit.rinfo(); | 255 RelocInfo::ModeMask(RelocInfo::DEOPT_INLINING_ID); |
| 263 DCHECK(RelocInfo::IsDeoptId(reloc_info->rmode())); | 256 for (RelocIterator it(*code, mask); !it.done(); it.next()) { |
| 264 int deopt_id = static_cast<int>(reloc_info->data()); | 257 RelocInfo* info = it.rinfo(); |
| 265 int translation_index = | 258 if (info->rmode() == RelocInfo::DEOPT_SCRIPT_OFFSET) { |
| 266 deopt_input_data->TranslationIndex(deopt_id)->value(); | 259 int script_offset = static_cast<int>(info->data()); |
| 267 TranslationIterator it(deopt_input_data->TranslationByteArray(), | 260 it.next(); |
| 268 translation_index); | 261 DCHECK(it.rinfo()->rmode() == RelocInfo::DEOPT_INLINING_ID); |
| 269 Translation::Opcode opcode = static_cast<Translation::Opcode>(it.Next()); | 262 int inlining_id = static_cast<int>(it.rinfo()->data()); |
| 270 DCHECK_EQ(Translation::BEGIN, opcode); | 263 last_position = SourcePosition(script_offset, inlining_id); |
| 271 it.Skip(Translation::NumberOfOperandsFor(opcode)); | 264 continue; |
| 272 std::vector<CodeEntry::DeoptInlinedFrame> inlined_frames; | 265 } |
| 273 while (it.HasNext() && | 266 if (info->rmode() == RelocInfo::DEOPT_ID) { |
| 274 Translation::BEGIN != | 267 int deopt_id = static_cast<int>(info->data()); |
| 275 (opcode = static_cast<Translation::Opcode>(it.Next()))) { | 268 DCHECK(last_position.IsKnown()); |
| 276 if (opcode != Translation::JS_FRAME && | 269 std::vector<CpuProfileDeoptFrame> inlined_frames; |
| 277 opcode != Translation::INTERPRETED_FRAME) { | 270 for (SourcePositionInfo& pos_info : last_position.InliningStack(code)) { |
| 278 it.Skip(Translation::NumberOfOperandsFor(opcode)); | 271 DCHECK(pos_info.position.ScriptOffset() != kNoSourcePosition); |
| 279 continue; | 272 size_t offset = static_cast<size_t>(pos_info.position.ScriptOffset()); |
| 273 int script_id = Script::cast(pos_info.function->script())->id(); |
| 274 inlined_frames.push_back(CpuProfileDeoptFrame({script_id, offset})); |
| 280 } | 275 } |
| 281 BailoutId ast_id = BailoutId(it.Next()); | 276 if (!inlined_frames.empty() && |
| 282 int shared_info_id = it.Next(); | 277 !entry->HasDeoptInlinedFramesFor(deopt_id)) { |
| 283 it.Next(); // Skip height | 278 entry->AddDeoptInlinedFrames(deopt_id, std::move(inlined_frames)); |
| 284 SharedFunctionInfo* shared = SharedFunctionInfo::cast( | |
| 285 deopt_input_data->LiteralArray()->get(shared_info_id)); | |
| 286 int source_position; | |
| 287 if (opcode == Translation::INTERPRETED_FRAME) { | |
| 288 source_position = | |
| 289 Deoptimizer::ComputeSourcePositionFromBytecodeArray(shared, ast_id); | |
| 290 } else { | |
| 291 DCHECK(opcode == Translation::JS_FRAME); | |
| 292 source_position = | |
| 293 Deoptimizer::ComputeSourcePositionFromBaselineCode(shared, ast_id); | |
| 294 } | 279 } |
| 295 int script_id = v8::UnboundScript::kNoScriptId; | |
| 296 if (shared->script()->IsScript()) { | |
| 297 Script* script = Script::cast(shared->script()); | |
| 298 script_id = script->id(); | |
| 299 } | |
| 300 CodeEntry::DeoptInlinedFrame frame = {source_position, script_id}; | |
| 301 inlined_frames.push_back(frame); | |
| 302 } | |
| 303 if (!inlined_frames.empty() && !entry->HasDeoptInlinedFramesFor(deopt_id)) { | |
| 304 entry->AddDeoptInlinedFrames(deopt_id, inlined_frames); | |
| 305 DCHECK(inlined_frames.empty()); | |
| 306 } | 280 } |
| 307 } | 281 } |
| 308 } | 282 } |
| 309 | 283 |
| 310 CodeEntry* ProfilerListener::NewCodeEntry( | 284 CodeEntry* ProfilerListener::NewCodeEntry( |
| 311 CodeEventListener::LogEventsAndTags tag, const char* name, | 285 CodeEventListener::LogEventsAndTags tag, const char* name, |
| 312 const char* name_prefix, const char* resource_name, int line_number, | 286 const char* name_prefix, const char* resource_name, int line_number, |
| 313 int column_number, JITLineInfoTable* line_info, Address instruction_start) { | 287 int column_number, JITLineInfoTable* line_info, Address instruction_start) { |
| 314 CodeEntry* code_entry = | 288 CodeEntry* code_entry = |
| 315 new CodeEntry(tag, name, name_prefix, resource_name, line_number, | 289 new CodeEntry(tag, name, name_prefix, resource_name, line_number, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 328 | 302 |
| 329 void ProfilerListener::RemoveObserver(CodeEventObserver* observer) { | 303 void ProfilerListener::RemoveObserver(CodeEventObserver* observer) { |
| 330 base::LockGuard<base::Mutex> guard(&mutex_); | 304 base::LockGuard<base::Mutex> guard(&mutex_); |
| 331 auto it = std::find(observers_.begin(), observers_.end(), observer); | 305 auto it = std::find(observers_.begin(), observers_.end(), observer); |
| 332 if (it == observers_.end()) return; | 306 if (it == observers_.end()) return; |
| 333 observers_.erase(it); | 307 observers_.erase(it); |
| 334 } | 308 } |
| 335 | 309 |
| 336 } // namespace internal | 310 } // namespace internal |
| 337 } // namespace v8 | 311 } // namespace v8 |
| OLD | NEW |