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/profiler/cpu-profiler.h"
9 #include "src/profiler/profile-generator-inl.h"
10 #include "src/source-position-table.h"
11
12 namespace v8 {
13 namespace internal {
14
ProfilerListener(Isolate * isolate)15 ProfilerListener::ProfilerListener(Isolate* isolate)
16 : function_and_resource_names_(isolate->heap()) {}
17
~ProfilerListener()18 ProfilerListener::~ProfilerListener() {
19 for (auto code_entry : code_entries_) {
20 delete code_entry;
21 }
22 }
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->start = entry_point;
28 rec->entry = NewCodeEntry(CodeEventListener::CALLBACK_TAG, GetName(name));
29 rec->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->start = code->address();
38 rec->entry = NewCodeEntry(
39 tag, GetFunctionName(name), CodeEntry::kEmptyNamePrefix,
40 CodeEntry::kEmptyResourceName, CpuProfileNode::kNoLineNumberInfo,
41 CpuProfileNode::kNoColumnNumberInfo, NULL, code->instruction_start());
42 RecordInliningInfo(rec->entry, code);
43 rec->size = code->ExecutableSize();
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->start = code->address();
52 rec->entry = NewCodeEntry(
53 tag, GetFunctionName(name), CodeEntry::kEmptyNamePrefix,
54 CodeEntry::kEmptyResourceName, CpuProfileNode::kNoLineNumberInfo,
55 CpuProfileNode::kNoColumnNumberInfo, NULL, code->instruction_start());
56 RecordInliningInfo(rec->entry, code);
57 rec->size = code->ExecutableSize();
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->start = code->address();
68 rec->entry = NewCodeEntry(
69 tag, GetFunctionName(shared->DebugName()), CodeEntry::kEmptyNamePrefix,
70 GetName(InferScriptName(script_name, shared)),
71 CpuProfileNode::kNoLineNumberInfo, CpuProfileNode::kNoColumnNumberInfo,
72 NULL, code->instruction_start());
73 RecordInliningInfo(rec->entry, code);
74 rec->entry->FillFunctionInfo(shared);
75 rec->size = code->ExecutableSize();
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->start = abstract_code->address();
87 Script* script = Script::cast(shared->script());
88 JITLineInfoTable* line_table = NULL;
89 if (script) {
90 line_table = new JITLineInfoTable();
91 int offset = abstract_code->IsCode() ? Code::kHeaderSize
92 : BytecodeArray::kHeaderSize;
93 for (SourcePositionTableIterator it(abstract_code->source_position_table());
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;
99 int position = it.source_position().ScriptOffset();
100 int line_number = script->GetLineNumber(position) + 1;
101 int pc_offset = it.code_offset() + offset;
102 line_table->SetPosition(pc_offset, line_number);
103 }
104 }
105 rec->entry = NewCodeEntry(
106 tag, GetFunctionName(shared->DebugName()), CodeEntry::kEmptyNamePrefix,
107 GetName(InferScriptName(script_name, shared)), line, column, line_table,
108 abstract_code->instruction_start());
109 RecordInliningInfo(rec->entry, abstract_code);
110 RecordDeoptInlinedFrames(rec->entry, abstract_code);
111 rec->entry->FillFunctionInfo(shared);
112 rec->size = abstract_code->ExecutableSize();
113 DispatchCodeEvent(evt_rec);
114 }
115
CodeCreateEvent(CodeEventListener::LogEventsAndTags tag,AbstractCode * code,int args_count)116 void ProfilerListener::CodeCreateEvent(CodeEventListener::LogEventsAndTags tag,
117 AbstractCode* code, int args_count) {
118 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION);
119 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
120 rec->start = code->address();
121 rec->entry = NewCodeEntry(
122 tag, GetName(args_count), "args_count: ", CodeEntry::kEmptyResourceName,
123 CpuProfileNode::kNoLineNumberInfo, CpuProfileNode::kNoColumnNumberInfo,
124 NULL, code->instruction_start());
125 RecordInliningInfo(rec->entry, code);
126 rec->size = code->ExecutableSize();
127 DispatchCodeEvent(evt_rec);
128 }
129
CodeMoveEvent(AbstractCode * from,Address to)130 void ProfilerListener::CodeMoveEvent(AbstractCode* from, Address to) {
131 CodeEventsContainer evt_rec(CodeEventRecord::CODE_MOVE);
132 CodeMoveEventRecord* rec = &evt_rec.CodeMoveEventRecord_;
133 rec->from = from->address();
134 rec->to = to;
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->start = code->address();
143 rec->bailout_reason = GetBailoutReason(shared->disable_optimization_reason());
144 DispatchCodeEvent(evt_rec);
145 }
146
CodeDeoptEvent(Code * code,Address pc,int fp_to_sp_delta)147 void ProfilerListener::CodeDeoptEvent(Code* code, 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->start = code->address();
153 rec->deopt_reason = DeoptimizeReasonToString(info.deopt_reason);
154 rec->deopt_id = info.deopt_id;
155 rec->pc = reinterpret_cast<void*>(pc);
156 rec->fp_to_sp_delta = fp_to_sp_delta;
157 DispatchCodeEvent(evt_rec);
158 }
159
GetterCallbackEvent(Name * name,Address entry_point)160 void ProfilerListener::GetterCallbackEvent(Name* name, Address entry_point) {
161 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION);
162 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
163 rec->start = entry_point;
164 rec->entry =
165 NewCodeEntry(CodeEventListener::CALLBACK_TAG, GetName(name), "get ");
166 rec->size = 1;
167 DispatchCodeEvent(evt_rec);
168 }
169
RegExpCodeCreateEvent(AbstractCode * code,String * source)170 void ProfilerListener::RegExpCodeCreateEvent(AbstractCode* code,
171 String* source) {
172 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION);
173 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
174 rec->start = code->address();
175 rec->entry = NewCodeEntry(
176 CodeEventListener::REG_EXP_TAG, GetName(source), "RegExp: ",
177 CodeEntry::kEmptyResourceName, CpuProfileNode::kNoLineNumberInfo,
178 CpuProfileNode::kNoColumnNumberInfo, NULL, code->instruction_start());
179 rec->size = code->ExecutableSize();
180 DispatchCodeEvent(evt_rec);
181 }
182
SetterCallbackEvent(Name * name,Address entry_point)183 void ProfilerListener::SetterCallbackEvent(Name* name, Address entry_point) {
184 CodeEventsContainer evt_rec(CodeEventRecord::CODE_CREATION);
185 CodeCreateEventRecord* rec = &evt_rec.CodeCreateEventRecord_;
186 rec->start = entry_point;
187 rec->entry =
188 NewCodeEntry(CodeEventListener::CALLBACK_TAG, GetName(name), "set ");
189 rec->size = 1;
190 DispatchCodeEvent(evt_rec);
191 }
192
InferScriptName(Name * name,SharedFunctionInfo * info)193 Name* ProfilerListener::InferScriptName(Name* name, SharedFunctionInfo* info) {
194 if (name->IsString() && String::cast(name)->length()) return name;
195 if (!info->script()->IsScript()) return name;
196 Object* source_url = Script::cast(info->script())->source_url();
197 return source_url->IsName() ? Name::cast(source_url) : name;
198 }
199
RecordInliningInfo(CodeEntry * entry,AbstractCode * abstract_code)200 void ProfilerListener::RecordInliningInfo(CodeEntry* entry,
201 AbstractCode* abstract_code) {
202 if (!abstract_code->IsCode()) return;
203 Code* code = abstract_code->GetCode();
204 if (code->kind() != Code::OPTIMIZED_FUNCTION) return;
205 DeoptimizationInputData* deopt_input_data =
206 DeoptimizationInputData::cast(code->deoptimization_data());
207 int deopt_count = deopt_input_data->DeoptCount();
208 for (int i = 0; i < deopt_count; i++) {
209 int pc_offset = deopt_input_data->Pc(i)->value();
210 if (pc_offset == -1) continue;
211 int translation_index = deopt_input_data->TranslationIndex(i)->value();
212 TranslationIterator it(deopt_input_data->TranslationByteArray(),
213 translation_index);
214 Translation::Opcode opcode = static_cast<Translation::Opcode>(it.Next());
215 DCHECK_EQ(Translation::BEGIN, opcode);
216 it.Skip(Translation::NumberOfOperandsFor(opcode));
217 int depth = 0;
218 std::vector<CodeEntry*> inline_stack;
219 while (it.HasNext() &&
220 Translation::BEGIN !=
221 (opcode = static_cast<Translation::Opcode>(it.Next()))) {
222 if (opcode != Translation::JS_FRAME &&
223 opcode != Translation::INTERPRETED_FRAME) {
224 it.Skip(Translation::NumberOfOperandsFor(opcode));
225 continue;
226 }
227 it.Next(); // Skip ast_id
228 int shared_info_id = it.Next();
229 it.Next(); // Skip height
230 SharedFunctionInfo* shared_info = SharedFunctionInfo::cast(
231 deopt_input_data->LiteralArray()->get(shared_info_id));
232 if (!depth++) continue; // Skip the current function itself.
233 CodeEntry* inline_entry = new CodeEntry(
234 entry->tag(), GetFunctionName(shared_info->DebugName()),
235 CodeEntry::kEmptyNamePrefix, entry->resource_name(),
236 CpuProfileNode::kNoLineNumberInfo,
237 CpuProfileNode::kNoColumnNumberInfo, NULL, code->instruction_start());
238 inline_entry->FillFunctionInfo(shared_info);
239 inline_stack.push_back(inline_entry);
240 }
241 if (!inline_stack.empty()) {
242 entry->AddInlineStack(pc_offset, std::move(inline_stack));
243 }
244 }
245 }
246
RecordDeoptInlinedFrames(CodeEntry * entry,AbstractCode * abstract_code)247 void ProfilerListener::RecordDeoptInlinedFrames(CodeEntry* entry,
248 AbstractCode* abstract_code) {
249 if (abstract_code->kind() != AbstractCode::OPTIMIZED_FUNCTION) return;
250 Handle<Code> code(abstract_code->GetCode());
251
252 SourcePosition last_position = SourcePosition::Unknown();
253 int mask = RelocInfo::ModeMask(RelocInfo::DEOPT_ID) |
254 RelocInfo::ModeMask(RelocInfo::DEOPT_SCRIPT_OFFSET) |
255 RelocInfo::ModeMask(RelocInfo::DEOPT_INLINING_ID);
256 for (RelocIterator it(*code, mask); !it.done(); it.next()) {
257 RelocInfo* info = it.rinfo();
258 if (info->rmode() == RelocInfo::DEOPT_SCRIPT_OFFSET) {
259 int script_offset = static_cast<int>(info->data());
260 it.next();
261 DCHECK(it.rinfo()->rmode() == RelocInfo::DEOPT_INLINING_ID);
262 int inlining_id = static_cast<int>(it.rinfo()->data());
263 last_position = SourcePosition(script_offset, inlining_id);
264 continue;
265 }
266 if (info->rmode() == RelocInfo::DEOPT_ID) {
267 int deopt_id = static_cast<int>(info->data());
268 DCHECK(last_position.IsKnown());
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 }
281 }
282 }
283
NewCodeEntry(CodeEventListener::LogEventsAndTags tag,const char * name,const char * name_prefix,const char * resource_name,int line_number,int column_number,JITLineInfoTable * line_info,Address instruction_start)284 CodeEntry* ProfilerListener::NewCodeEntry(
285 CodeEventListener::LogEventsAndTags tag, const char* name,
286 const char* name_prefix, const char* resource_name, int line_number,
287 int column_number, JITLineInfoTable* line_info, Address instruction_start) {
288 CodeEntry* code_entry =
289 new CodeEntry(tag, name, name_prefix, resource_name, line_number,
290 column_number, line_info, instruction_start);
291 code_entries_.push_back(code_entry);
292 return code_entry;
293 }
294
AddObserver(CodeEventObserver * observer)295 void ProfilerListener::AddObserver(CodeEventObserver* observer) {
296 base::LockGuard<base::Mutex> guard(&mutex_);
297 if (std::find(observers_.begin(), observers_.end(), observer) !=
298 observers_.end())
299 return;
300 observers_.push_back(observer);
301 }
302
RemoveObserver(CodeEventObserver * observer)303 void ProfilerListener::RemoveObserver(CodeEventObserver* observer) {
304 base::LockGuard<base::Mutex> guard(&mutex_);
305 auto it = std::find(observers_.begin(), observers_.end(), observer);
306 if (it == observers_.end()) return;
307 observers_.erase(it);
308 }
309
310 } // namespace internal
311 } // namespace v8
312