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
3 // found in the LICENSE file.
4
5 #include "src/profiler/profiler-listener.h"
6
7 #include "src/deoptimizer.h"
8 #include "src/instruction-stream.h"
9 #include "src/objects-inl.h"
10 #include "src/profiler/cpu-profiler.h"
11 #include "src/profiler/profile-generator-inl.h"
12 #include "src/source-position-table.h"
13 #include "src/wasm/wasm-code-manager.h"
14
15 namespace v8 {
16 namespace internal {
17
ProfilerListener(Isolate * isolate,CodeEventObserver * observer)18 ProfilerListener::ProfilerListener(Isolate* isolate,
19 CodeEventObserver* observer)
20 : isolate_(isolate), observer_(observer) {}
21
22 ProfilerListener::~ProfilerListener() = default;
23
CallbackEvent(Name * name,Address entry_point)24 void ProfilerListener::CallbackEvent(Name* name, Address entry_point) {
25 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION);
26 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
27 rec->instruction_start = entry_point;
28 rec->entry = NewCodeEntry(CodeEventListener::CALLBACK_TAG, GetName(name));
29 rec->instruction_size = 1;
30 DispatchCodeEvent(evt_rec);
31 }
32
CodeCreateEvent(CodeEventListener::LogEventsAndTags tag,AbstractCode * code,const char * name)33 void ProfilerListener::CodeCreateEvent(CodeEventListener::LogEventsAndTags tag,
34 AbstractCode* code, const char* name) {
35 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION);
36 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
37 rec->instruction_start = code->InstructionStart();
38 rec->entry = NewCodeEntry(
39 tag, GetFunctionName(name), CodeEntry::kEmptyResourceName,
40 CpuProfileNode::kNoLineNumberInfo, CpuProfileNode::kNoColumnNumberInfo,
41 nullptr, code->InstructionStart());
42 RecordInliningInfo(rec->entry, code);
43 rec->instruction_size = code->InstructionSize();
44 DispatchCodeEvent(evt_rec);
45 }
46
CodeCreateEvent(CodeEventListener::LogEventsAndTags tag,AbstractCode * code,Name * name)47 void ProfilerListener::CodeCreateEvent(CodeEventListener::LogEventsAndTags tag,
48 AbstractCode* code, Name* name) {
49 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION);
50 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
51 rec->instruction_start = code->InstructionStart();
52 rec->entry = NewCodeEntry(
53 tag, GetFunctionName(name), CodeEntry::kEmptyResourceName,
54 CpuProfileNode::kNoLineNumberInfo, CpuProfileNode::kNoColumnNumberInfo,
55 nullptr, code->InstructionStart());
56 RecordInliningInfo(rec->entry, code);
57 rec->instruction_size = code->InstructionSize();
58 DispatchCodeEvent(evt_rec);
59 }
60
CodeCreateEvent(CodeEventListener::LogEventsAndTags tag,AbstractCode * code,SharedFunctionInfo * shared,Name * script_name)61 void ProfilerListener::CodeCreateEvent(CodeEventListener::LogEventsAndTags tag,
62 AbstractCode* code,
63 SharedFunctionInfo* shared,
64 Name* script_name) {
65 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION);
66 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
67 rec->instruction_start = code->InstructionStart();
68 rec->entry = NewCodeEntry(tag, GetFunctionName(shared->DebugName()),
69 GetName(InferScriptName(script_name, shared)),
70 CpuProfileNode::kNoLineNumberInfo,
71 CpuProfileNode::kNoColumnNumberInfo, nullptr,
72 code->InstructionStart());
73 RecordInliningInfo(rec->entry, code);
74 rec->entry->FillFunctionInfo(shared);
75 rec->instruction_size = code->InstructionSize();
76 DispatchCodeEvent(evt_rec);
77 }
78
CodeCreateEvent(CodeEventListener::LogEventsAndTags tag,AbstractCode * abstract_code,SharedFunctionInfo * shared,Name * script_name,int line,int column)79 void ProfilerListener::CodeCreateEvent(CodeEventListener::LogEventsAndTags tag,
80 AbstractCode* abstract_code,
81 SharedFunctionInfo* shared,
82 Name* script_name, int line,
83 int column) {
84 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION);
85 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
86 rec->instruction_start = abstract_code->InstructionStart();
87 std::unique_ptr<SourcePositionTable> line_table;
88 if (shared->script()->IsScript()) {
89 Script* script = Script::cast(shared->script());
90 line_table.reset(new SourcePositionTable());
91 for (SourcePositionTableIterator it(abstract_code->source_position_table());
92 !it.done(); it.Advance()) {
93 // TODO(alph,tebbi) Skipping inlined positions for now, because they might
94 // refer to a different script.
95 if (it.source_position().InliningId() != SourcePosition::kNotInlined)
96 continue;
97 int position = it.source_position().ScriptOffset();
98 int line_number = script->GetLineNumber(position) + 1;
99 line_table->SetPosition(it.code_offset(), line_number);
100 }
101 }
102 rec->entry =
103 NewCodeEntry(tag, GetFunctionName(shared->DebugName()),
104 GetName(InferScriptName(script_name, shared)), line, column,
105 std::move(line_table), abstract_code->InstructionStart());
106 RecordInliningInfo(rec->entry, abstract_code);
107 rec->entry->FillFunctionInfo(shared);
108 rec->instruction_size = abstract_code->InstructionSize();
109 DispatchCodeEvent(evt_rec);
110 }
111
CodeCreateEvent(CodeEventListener::LogEventsAndTags tag,const wasm::WasmCode * code,wasm::WasmName name)112 void ProfilerListener::CodeCreateEvent(CodeEventListener::LogEventsAndTags tag,
113 const wasm::WasmCode* code,
114 wasm::WasmName name) {
115 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION);
116 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
117 rec->instruction_start = code->instruction_start();
118 // TODO(herhut): Instead of sanitizing here, make sure all wasm functions
119 // have names.
120 const char* name_ptr =
121 name.start() == nullptr ? "<anonymous>" : GetFunctionName(name.start());
122 rec->entry = NewCodeEntry(tag, name_ptr, CodeEntry::kEmptyResourceName,
123 CpuProfileNode::kNoLineNumberInfo,
124 CpuProfileNode::kNoColumnNumberInfo, nullptr,
125 code->instruction_start());
126 rec->instruction_size = code->instructions().length();
127 DispatchCodeEvent(evt_rec);
128 }
129
CodeMoveEvent(AbstractCode * from,AbstractCode * to)130 void ProfilerListener::CodeMoveEvent(AbstractCode* from, AbstractCode* to) {
131 CodeEventsContainer evt_rec(CodeEventRecord::CODE_MOVE);
132 CodeMoveEventRecord* rec = &evt_rec.CodeMoveEventRecord_;
133 rec->from_instruction_start = from->InstructionStart();
134 rec->to_instruction_start = to->InstructionStart();
135 DispatchCodeEvent(evt_rec);
136 }
137
CodeDisableOptEvent(AbstractCode * code,SharedFunctionInfo * shared)138 void ProfilerListener::CodeDisableOptEvent(AbstractCode* code,
139 SharedFunctionInfo* shared) {
140 CodeEventsContainer evt_rec(CodeEventRecord::CODE_DISABLE_OPT);
141 CodeDisableOptEventRecord* rec = &evt_rec.CodeDisableOptEventRecord_;
142 rec->instruction_start = code->InstructionStart();
143 rec->bailout_reason = GetBailoutReason(shared->disable_optimization_reason());
144 DispatchCodeEvent(evt_rec);
145 }
146
CodeDeoptEvent(Code * code,DeoptKind kind,Address pc,int fp_to_sp_delta)147 void ProfilerListener::CodeDeoptEvent(Code* code, DeoptKind kind, Address pc,
148 int fp_to_sp_delta) {
149 CodeEventsContainer evt_rec(CodeEventRecord::CODE_DEOPT);
150 CodeDeoptEventRecord* rec = &evt_rec.CodeDeoptEventRecord_;
151 Deoptimizer::DeoptInfo info = Deoptimizer::GetDeoptInfo(code, pc);
152 rec->instruction_start = code->InstructionStart();
153 rec->deopt_reason = DeoptimizeReasonToString(info.deopt_reason);
154 rec->deopt_id = info.deopt_id;
155 rec->pc = pc;
156 rec->fp_to_sp_delta = fp_to_sp_delta;
157
158 // When a function is deoptimized, we store the deoptimized frame information
159 // for the use of GetDeoptInfos().
160 AttachDeoptInlinedFrames(code, rec);
161 DispatchCodeEvent(evt_rec);
162 }
163
GetterCallbackEvent(Name * name,Address entry_point)164 void ProfilerListener::GetterCallbackEvent(Name* name, Address entry_point) {
165 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION);
166 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
167 rec->instruction_start = entry_point;
168 rec->entry =
169 NewCodeEntry(CodeEventListener::CALLBACK_TAG, GetConsName("get ", name));
170 rec->instruction_size = 1;
171 DispatchCodeEvent(evt_rec);
172 }
173
RegExpCodeCreateEvent(AbstractCode * code,String * source)174 void ProfilerListener::RegExpCodeCreateEvent(AbstractCode* code,
175 String* source) {
176 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION);
177 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
178 rec->instruction_start = code->InstructionStart();
179 rec->entry = NewCodeEntry(
180 CodeEventListener::REG_EXP_TAG, GetConsName("RegExp: ", source),
181 CodeEntry::kEmptyResourceName, CpuProfileNode::kNoLineNumberInfo,
182 CpuProfileNode::kNoColumnNumberInfo, nullptr, code->InstructionStart());
183 rec->instruction_size = code->InstructionSize();
184 DispatchCodeEvent(evt_rec);
185 }
186
SetterCallbackEvent(Name * name,Address entry_point)187 void ProfilerListener::SetterCallbackEvent(Name* name, Address entry_point) {
188 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION);
189 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
190 rec->instruction_start = entry_point;
191 rec->entry =
192 NewCodeEntry(CodeEventListener::CALLBACK_TAG, GetConsName("set ", name));
193 rec->instruction_size = 1;
194 DispatchCodeEvent(evt_rec);
195 }
196
InferScriptName(Name * name,SharedFunctionInfo * info)197 Name* ProfilerListener::InferScriptName(Name* name, SharedFunctionInfo* info) {
198 if (name->IsString() && String::cast(name)->length()) return name;
199 if (!info->script()->IsScript()) return name;
200 Object* source_url = Script::cast(info->script())->source_url();
201 return source_url->IsName() ? Name::cast(source_url) : name;
202 }
203
RecordInliningInfo(CodeEntry * entry,AbstractCode * abstract_code)204 void ProfilerListener::RecordInliningInfo(CodeEntry* entry,
205 AbstractCode* abstract_code) {
206 if (!abstract_code->IsCode()) return;
207 Code* code = abstract_code->GetCode();
208 if (code->kind() != Code::OPTIMIZED_FUNCTION) return;
209 DeoptimizationData* deopt_input_data =
210 DeoptimizationData::cast(code->deoptimization_data());
211 int deopt_count = deopt_input_data->DeoptCount();
212 for (int i = 0; i < deopt_count; i++) {
213 int pc_offset = deopt_input_data->Pc(i)->value();
214 if (pc_offset == -1) continue;
215 int translation_index = deopt_input_data->TranslationIndex(i)->value();
216 TranslationIterator it(deopt_input_data->TranslationByteArray(),
217 translation_index);
218 Translation::Opcode opcode = static_cast<Translation::Opcode>(it.Next());
219 DCHECK_EQ(Translation::BEGIN, opcode);
220 it.Skip(Translation::NumberOfOperandsFor(opcode));
221 int depth = 0;
222 std::vector<std::unique_ptr<CodeEntry>> inline_stack;
223 while (it.HasNext() &&
224 Translation::BEGIN !=
225 (opcode = static_cast<Translation::Opcode>(it.Next()))) {
226 if (opcode != Translation::INTERPRETED_FRAME) {
227 it.Skip(Translation::NumberOfOperandsFor(opcode));
228 continue;
229 }
230 it.Next(); // Skip ast_id
231 int shared_info_id = it.Next();
232 it.Next(); // Skip height
233 SharedFunctionInfo* shared_info = SharedFunctionInfo::cast(
234 deopt_input_data->LiteralArray()->get(shared_info_id));
235 if (!depth++) continue; // Skip the current function itself.
236
237 const char* resource_name =
238 (shared_info->script()->IsScript() &&
239 Script::cast(shared_info->script())->name()->IsName())
240 ? GetName(Name::cast(Script::cast(shared_info->script())->name()))
241 : CodeEntry::kEmptyResourceName;
242
243 CodeEntry* inline_entry =
244 new CodeEntry(entry->tag(), GetFunctionName(shared_info->DebugName()),
245 resource_name, CpuProfileNode::kNoLineNumberInfo,
246 CpuProfileNode::kNoColumnNumberInfo, nullptr,
247 code->InstructionStart());
248 inline_entry->FillFunctionInfo(shared_info);
249 inline_stack.emplace_back(inline_entry);
250 }
251 if (!inline_stack.empty()) {
252 entry->AddInlineStack(pc_offset, std::move(inline_stack));
253 }
254 }
255 }
256
AttachDeoptInlinedFrames(Code * code,CodeDeoptEventRecord * rec)257 void ProfilerListener::AttachDeoptInlinedFrames(Code* code,
258 CodeDeoptEventRecord* rec) {
259 int deopt_id = rec->deopt_id;
260 SourcePosition last_position = SourcePosition::Unknown();
261 int mask = RelocInfo::ModeMask(RelocInfo::DEOPT_ID) |
262 RelocInfo::ModeMask(RelocInfo::DEOPT_SCRIPT_OFFSET) |
263 RelocInfo::ModeMask(RelocInfo::DEOPT_INLINING_ID);
264
265 rec->deopt_frames = nullptr;
266 rec->deopt_frame_count = 0;
267
268 for (RelocIterator it(code, mask); !it.done(); it.next()) {
269 RelocInfo* info = it.rinfo();
270 if (info->rmode() == RelocInfo::DEOPT_SCRIPT_OFFSET) {
271 int script_offset = static_cast<int>(info->data());
272 it.next();
273 DCHECK(it.rinfo()->rmode() == RelocInfo::DEOPT_INLINING_ID);
274 int inlining_id = static_cast<int>(it.rinfo()->data());
275 last_position = SourcePosition(script_offset, inlining_id);
276 continue;
277 }
278 if (info->rmode() == RelocInfo::DEOPT_ID) {
279 if (deopt_id != static_cast<int>(info->data())) continue;
280 DCHECK(last_position.IsKnown());
281
282 // SourcePosition::InliningStack allocates a handle for the SFI of each
283 // frame. These don't escape this function, but quickly add up. This
284 // scope limits their lifetime.
285 HandleScope scope(isolate_);
286 std::vector<SourcePositionInfo> stack =
287 last_position.InliningStack(handle(code));
288 CpuProfileDeoptFrame* deopt_frames =
289 new CpuProfileDeoptFrame[stack.size()];
290
291 int deopt_frame_count = 0;
292 for (SourcePositionInfo& pos_info : stack) {
293 if (pos_info.position.ScriptOffset() == kNoSourcePosition) continue;
294 if (pos_info.script.is_null()) continue;
295 int script_id = pos_info.script->id();
296 size_t offset = static_cast<size_t>(pos_info.position.ScriptOffset());
297 deopt_frames[deopt_frame_count++] = {script_id, offset};
298 }
299 rec->deopt_frames = deopt_frames;
300 rec->deopt_frame_count = deopt_frame_count;
301 break;
302 }
303 }
304 }
305
NewCodeEntry(CodeEventListener::LogEventsAndTags tag,const char * name,const char * resource_name,int line_number,int column_number,std::unique_ptr<SourcePositionTable> line_info,Address instruction_start)306 CodeEntry* ProfilerListener::NewCodeEntry(
307 CodeEventListener::LogEventsAndTags tag, const char* name,
308 const char* resource_name, int line_number, int column_number,
309 std::unique_ptr<SourcePositionTable> line_info, Address instruction_start) {
310 return new CodeEntry(tag, name, resource_name, line_number, column_number,
311 std::move(line_info), instruction_start);
312 }
313
314 } // namespace internal
315 } // namespace v8
316