| 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(); |
| 93 for (SourcePositionTableIterator it(abstract_code->source_position_table()); | 95 for (SourcePositionTableIterator it(abstract_code->source_position_table()); |
| 94 !it.done(); it.Advance()) { | 96 !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; | |
| 99 int position = it.source_position().ScriptOffset(); | 97 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; |
| 100 int line_number = script->GetLineNumber(position) + 1; | 105 int line_number = script->GetLineNumber(position) + 1; |
| 101 int pc_offset = it.code_offset() + offset; | 106 int pc_offset = it.code_offset() + offset; |
| 102 line_table->SetPosition(pc_offset, line_number); | 107 line_table->SetPosition(pc_offset, line_number); |
| 103 } | 108 } |
| 104 } | 109 } |
| 105 rec->entry = NewCodeEntry( | 110 rec->entry = NewCodeEntry( |
| 106 tag, GetFunctionName(shared->DebugName()), CodeEntry::kEmptyNamePrefix, | 111 tag, GetFunctionName(shared->DebugName()), CodeEntry::kEmptyNamePrefix, |
| 107 GetName(InferScriptName(script_name, shared)), line, column, line_table, | 112 GetName(InferScriptName(script_name, shared)), line, column, line_table, |
| 108 abstract_code->instruction_start()); | 113 abstract_code->instruction_start()); |
| 109 RecordInliningInfo(rec->entry, abstract_code); | 114 RecordInliningInfo(rec->entry, abstract_code); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 DispatchCodeEvent(evt_rec); | 149 DispatchCodeEvent(evt_rec); |
| 145 } | 150 } |
| 146 | 151 |
| 147 void ProfilerListener::CodeDeoptEvent(Code* code, Address pc, | 152 void ProfilerListener::CodeDeoptEvent(Code* code, Address pc, |
| 148 int fp_to_sp_delta) { | 153 int fp_to_sp_delta) { |
| 149 CodeEventsContainer evt_rec(CodeEventRecord::CODE_DEOPT); | 154 CodeEventsContainer evt_rec(CodeEventRecord::CODE_DEOPT); |
| 150 CodeDeoptEventRecord* rec = &evt_rec.CodeDeoptEventRecord_; | 155 CodeDeoptEventRecord* rec = &evt_rec.CodeDeoptEventRecord_; |
| 151 Deoptimizer::DeoptInfo info = Deoptimizer::GetDeoptInfo(code, pc); | 156 Deoptimizer::DeoptInfo info = Deoptimizer::GetDeoptInfo(code, pc); |
| 152 rec->start = code->address(); | 157 rec->start = code->address(); |
| 153 rec->deopt_reason = DeoptimizeReasonToString(info.deopt_reason); | 158 rec->deopt_reason = DeoptimizeReasonToString(info.deopt_reason); |
| 159 rec->position = info.position; |
| 154 rec->deopt_id = info.deopt_id; | 160 rec->deopt_id = info.deopt_id; |
| 155 rec->pc = reinterpret_cast<void*>(pc); | 161 rec->pc = reinterpret_cast<void*>(pc); |
| 156 rec->fp_to_sp_delta = fp_to_sp_delta; | 162 rec->fp_to_sp_delta = fp_to_sp_delta; |
| 157 DispatchCodeEvent(evt_rec); | 163 DispatchCodeEvent(evt_rec); |
| 158 } | 164 } |
| 159 | 165 |
| 160 void ProfilerListener::GetterCallbackEvent(Name* name, Address entry_point) { | 166 void ProfilerListener::GetterCallbackEvent(Name* name, Address entry_point) { |
| 161 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); | 167 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION); |
| 162 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; | 168 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_; |
| 163 rec->start = entry_point; | 169 rec->start = entry_point; |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 if (!depth++) continue; // Skip the current function itself. | 238 if (!depth++) continue; // Skip the current function itself. |
| 233 CodeEntry* inline_entry = new CodeEntry( | 239 CodeEntry* inline_entry = new CodeEntry( |
| 234 entry->tag(), GetFunctionName(shared_info->DebugName()), | 240 entry->tag(), GetFunctionName(shared_info->DebugName()), |
| 235 CodeEntry::kEmptyNamePrefix, entry->resource_name(), | 241 CodeEntry::kEmptyNamePrefix, entry->resource_name(), |
| 236 CpuProfileNode::kNoLineNumberInfo, | 242 CpuProfileNode::kNoLineNumberInfo, |
| 237 CpuProfileNode::kNoColumnNumberInfo, NULL, code->instruction_start()); | 243 CpuProfileNode::kNoColumnNumberInfo, NULL, code->instruction_start()); |
| 238 inline_entry->FillFunctionInfo(shared_info); | 244 inline_entry->FillFunctionInfo(shared_info); |
| 239 inline_stack.push_back(inline_entry); | 245 inline_stack.push_back(inline_entry); |
| 240 } | 246 } |
| 241 if (!inline_stack.empty()) { | 247 if (!inline_stack.empty()) { |
| 242 entry->AddInlineStack(pc_offset, std::move(inline_stack)); | 248 entry->AddInlineStack(pc_offset, inline_stack); |
| 249 DCHECK(inline_stack.empty()); |
| 243 } | 250 } |
| 244 } | 251 } |
| 245 } | 252 } |
| 246 | 253 |
| 247 void ProfilerListener::RecordDeoptInlinedFrames(CodeEntry* entry, | 254 void ProfilerListener::RecordDeoptInlinedFrames(CodeEntry* entry, |
| 248 AbstractCode* abstract_code) { | 255 AbstractCode* abstract_code) { |
| 249 if (abstract_code->kind() != AbstractCode::OPTIMIZED_FUNCTION) return; | 256 if (abstract_code->kind() != AbstractCode::OPTIMIZED_FUNCTION) return; |
| 250 Handle<Code> code(abstract_code->GetCode()); | 257 Code* code = abstract_code->GetCode(); |
| 251 | 258 DeoptimizationInputData* deopt_input_data = |
| 252 SourcePosition last_position = SourcePosition::Unknown(); | 259 DeoptimizationInputData::cast(code->deoptimization_data()); |
| 253 int mask = RelocInfo::ModeMask(RelocInfo::DEOPT_ID) | | 260 int const mask = RelocInfo::ModeMask(RelocInfo::DEOPT_ID); |
| 254 RelocInfo::ModeMask(RelocInfo::DEOPT_SCRIPT_OFFSET) | | 261 for (RelocIterator rit(code, mask); !rit.done(); rit.next()) { |
| 255 RelocInfo::ModeMask(RelocInfo::DEOPT_INLINING_ID); | 262 RelocInfo* reloc_info = rit.rinfo(); |
| 256 for (RelocIterator it(*code, mask); !it.done(); it.next()) { | 263 DCHECK(RelocInfo::IsDeoptId(reloc_info->rmode())); |
| 257 RelocInfo* info = it.rinfo(); | 264 int deopt_id = static_cast<int>(reloc_info->data()); |
| 258 if (info->rmode() == RelocInfo::DEOPT_SCRIPT_OFFSET) { | 265 int translation_index = |
| 259 int script_offset = static_cast<int>(info->data()); | 266 deopt_input_data->TranslationIndex(deopt_id)->value(); |
| 260 it.next(); | 267 TranslationIterator it(deopt_input_data->TranslationByteArray(), |
| 261 DCHECK(it.rinfo()->rmode() == RelocInfo::DEOPT_INLINING_ID); | 268 translation_index); |
| 262 int inlining_id = static_cast<int>(it.rinfo()->data()); | 269 Translation::Opcode opcode = static_cast<Translation::Opcode>(it.Next()); |
| 263 last_position = SourcePosition(script_offset, inlining_id); | 270 DCHECK_EQ(Translation::BEGIN, opcode); |
| 264 continue; | 271 it.Skip(Translation::NumberOfOperandsFor(opcode)); |
| 272 std::vector<CodeEntry::DeoptInlinedFrame> inlined_frames; |
| 273 while (it.HasNext() && |
| 274 Translation::BEGIN != |
| 275 (opcode = static_cast<Translation::Opcode>(it.Next()))) { |
| 276 if (opcode != Translation::JS_FRAME && |
| 277 opcode != Translation::INTERPRETED_FRAME) { |
| 278 it.Skip(Translation::NumberOfOperandsFor(opcode)); |
| 279 continue; |
| 280 } |
| 281 BailoutId ast_id = BailoutId(it.Next()); |
| 282 int shared_info_id = it.Next(); |
| 283 it.Next(); // Skip height |
| 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 } |
| 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); |
| 265 } | 302 } |
| 266 if (info->rmode() == RelocInfo::DEOPT_ID) { | 303 if (!inlined_frames.empty() && !entry->HasDeoptInlinedFramesFor(deopt_id)) { |
| 267 int deopt_id = static_cast<int>(info->data()); | 304 entry->AddDeoptInlinedFrames(deopt_id, inlined_frames); |
| 268 DCHECK(last_position.IsKnown()); | 305 DCHECK(inlined_frames.empty()); |
| 269 std::vector<CpuProfileDeoptFrame> inlined_frames; | |
| 270 for (SourcePositionInfo& pos_info : last_position.InliningStack(code)) { | |
| 271 DCHECK(pos_info.position.ScriptOffset() != kNoSourcePosition); | |
| 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})); | |
| 275 } | |
| 276 if (!inlined_frames.empty() && | |
| 277 !entry->HasDeoptInlinedFramesFor(deopt_id)) { | |
| 278 entry->AddDeoptInlinedFrames(deopt_id, std::move(inlined_frames)); | |
| 279 } | |
| 280 } | 306 } |
| 281 } | 307 } |
| 282 } | 308 } |
| 283 | 309 |
| 284 CodeEntry* ProfilerListener::NewCodeEntry( | 310 CodeEntry* ProfilerListener::NewCodeEntry( |
| 285 CodeEventListener::LogEventsAndTags tag, const char* name, | 311 CodeEventListener::LogEventsAndTags tag, const char* name, |
| 286 const char* name_prefix, const char* resource_name, int line_number, | 312 const char* name_prefix, const char* resource_name, int line_number, |
| 287 int column_number, JITLineInfoTable* line_info, Address instruction_start) { | 313 int column_number, JITLineInfoTable* line_info, Address instruction_start) { |
| 288 CodeEntry* code_entry = | 314 CodeEntry* code_entry = |
| 289 new CodeEntry(tag, name, name_prefix, resource_name, line_number, | 315 new CodeEntry(tag, name, name_prefix, resource_name, line_number, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 302 | 328 |
| 303 void ProfilerListener::RemoveObserver(CodeEventObserver* observer) { | 329 void ProfilerListener::RemoveObserver(CodeEventObserver* observer) { |
| 304 base::LockGuard<base::Mutex> guard(&mutex_); | 330 base::LockGuard<base::Mutex> guard(&mutex_); |
| 305 auto it = std::find(observers_.begin(), observers_.end(), observer); | 331 auto it = std::find(observers_.begin(), observers_.end(), observer); |
| 306 if (it == observers_.end()) return; | 332 if (it == observers_.end()) return; |
| 307 observers_.erase(it); | 333 observers_.erase(it); |
| 308 } | 334 } |
| 309 | 335 |
| 310 } // namespace internal | 336 } // namespace internal |
| 311 } // namespace v8 | 337 } // namespace v8 |
| OLD | NEW |