Line data Source code
1 : // Copyright 2012 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/profile-generator.h"
6 :
7 : #include "src/base/adapters.h"
8 : #include "src/debug/debug.h"
9 : #include "src/deoptimizer.h"
10 : #include "src/global-handles.h"
11 : #include "src/objects-inl.h"
12 : #include "src/profiler/cpu-profiler.h"
13 : #include "src/profiler/profile-generator-inl.h"
14 : #include "src/tracing/trace-event.h"
15 : #include "src/tracing/traced-value.h"
16 : #include "src/unicode.h"
17 :
18 : namespace v8 {
19 : namespace internal {
20 :
21 2713312 : void SourcePositionTable::SetPosition(int pc_offset, int line,
22 : int inlining_id) {
23 : DCHECK_GE(pc_offset, 0);
24 : DCHECK_GT(line, 0); // The 1-based number of the source line.
25 : // Check that we are inserting in ascending order, so that the vector remains
26 : // sorted.
27 : DCHECK(pc_offsets_to_lines_.empty() ||
28 : pc_offsets_to_lines_.back().pc_offset < pc_offset);
29 4823965 : if (pc_offsets_to_lines_.empty() ||
30 4222395 : pc_offsets_to_lines_.back().line_number != line ||
31 1509083 : pc_offsets_to_lines_.back().inlining_id != inlining_id) {
32 2408464 : pc_offsets_to_lines_.push_back({pc_offset, line, inlining_id});
33 : }
34 2713312 : }
35 :
36 77816 : int SourcePositionTable::GetSourceLineNumber(int pc_offset) const {
37 77816 : if (pc_offsets_to_lines_.empty()) {
38 : return v8::CpuProfileNode::kNoLineNumberInfo;
39 : }
40 : auto it = std::lower_bound(
41 : pc_offsets_to_lines_.begin(), pc_offsets_to_lines_.end(),
42 : SourcePositionTuple{pc_offset, 0, SourcePosition::kNotInlined});
43 77761 : if (it != pc_offsets_to_lines_.begin()) --it;
44 77761 : return it->line_number;
45 : }
46 :
47 49791 : int SourcePositionTable::GetInliningId(int pc_offset) const {
48 49791 : if (pc_offsets_to_lines_.empty()) {
49 : return SourcePosition::kNotInlined;
50 : }
51 : auto it = std::lower_bound(
52 : pc_offsets_to_lines_.begin(), pc_offsets_to_lines_.end(),
53 : SourcePositionTuple{pc_offset, 0, SourcePosition::kNotInlined});
54 49781 : if (it != pc_offsets_to_lines_.begin()) --it;
55 49781 : return it->inlining_id;
56 : }
57 :
58 0 : void SourcePositionTable::print() const {
59 0 : base::OS::Print(" - source position table at %p\n", this);
60 0 : for (const SourcePositionTuple& pos_info : pc_offsets_to_lines_) {
61 : base::OS::Print(" %d --> line_number: %d inlining_id: %d\n",
62 : pos_info.pc_offset, pos_info.line_number,
63 0 : pos_info.inlining_id);
64 : }
65 0 : }
66 :
67 : const char* const CodeEntry::kWasmResourceNamePrefix = "wasm ";
68 : const char* const CodeEntry::kEmptyResourceName = "";
69 : const char* const CodeEntry::kEmptyBailoutReason = "";
70 : const char* const CodeEntry::kNoDeoptReason = "";
71 :
72 : const char* const CodeEntry::kProgramEntryName = "(program)";
73 : const char* const CodeEntry::kIdleEntryName = "(idle)";
74 : const char* const CodeEntry::kGarbageCollectorEntryName = "(garbage collector)";
75 : const char* const CodeEntry::kUnresolvedFunctionName = "(unresolved function)";
76 :
77 : base::LazyDynamicInstance<CodeEntry, CodeEntry::ProgramEntryCreateTrait>::type
78 : CodeEntry::kProgramEntry = LAZY_DYNAMIC_INSTANCE_INITIALIZER;
79 :
80 : base::LazyDynamicInstance<CodeEntry, CodeEntry::IdleEntryCreateTrait>::type
81 : CodeEntry::kIdleEntry = LAZY_DYNAMIC_INSTANCE_INITIALIZER;
82 :
83 : base::LazyDynamicInstance<CodeEntry, CodeEntry::GCEntryCreateTrait>::type
84 : CodeEntry::kGCEntry = LAZY_DYNAMIC_INSTANCE_INITIALIZER;
85 :
86 : base::LazyDynamicInstance<CodeEntry,
87 : CodeEntry::UnresolvedEntryCreateTrait>::type
88 : CodeEntry::kUnresolvedEntry = LAZY_DYNAMIC_INSTANCE_INITIALIZER;
89 :
90 163 : CodeEntry* CodeEntry::ProgramEntryCreateTrait::Create() {
91 326 : return new CodeEntry(Logger::FUNCTION_TAG, CodeEntry::kProgramEntryName);
92 : }
93 :
94 5 : CodeEntry* CodeEntry::IdleEntryCreateTrait::Create() {
95 10 : return new CodeEntry(Logger::FUNCTION_TAG, CodeEntry::kIdleEntryName);
96 : }
97 :
98 48 : CodeEntry* CodeEntry::GCEntryCreateTrait::Create() {
99 : return new CodeEntry(Logger::BUILTIN_TAG,
100 96 : CodeEntry::kGarbageCollectorEntryName);
101 : }
102 :
103 0 : CodeEntry* CodeEntry::UnresolvedEntryCreateTrait::Create() {
104 : return new CodeEntry(Logger::FUNCTION_TAG,
105 0 : CodeEntry::kUnresolvedFunctionName);
106 : }
107 :
108 77811 : uint32_t CodeEntry::GetHash() const {
109 : uint32_t hash = ComputeUnseededHash(tag());
110 77811 : if (script_id_ != v8::UnboundScript::kNoScriptId) {
111 117260 : hash ^= ComputeUnseededHash(static_cast<uint32_t>(script_id_));
112 117260 : hash ^= ComputeUnseededHash(static_cast<uint32_t>(position_));
113 : } else {
114 : hash ^= ComputeUnseededHash(
115 38362 : static_cast<uint32_t>(reinterpret_cast<uintptr_t>(name_)));
116 : hash ^= ComputeUnseededHash(
117 38362 : static_cast<uint32_t>(reinterpret_cast<uintptr_t>(resource_name_)));
118 38362 : hash ^= ComputeUnseededHash(line_number_);
119 : }
120 77811 : return hash;
121 : }
122 :
123 72491 : bool CodeEntry::IsSameFunctionAs(const CodeEntry* entry) const {
124 72491 : if (this == entry) return true;
125 1521 : if (script_id_ != v8::UnboundScript::kNoScriptId) {
126 1511 : return script_id_ == entry->script_id_ && position_ == entry->position_;
127 : }
128 20 : return name_ == entry->name_ && resource_name_ == entry->resource_name_ &&
129 20 : line_number_ == entry->line_number_;
130 : }
131 :
132 :
133 26593 : void CodeEntry::SetBuiltinId(Builtins::Name id) {
134 53186 : bit_field_ = TagField::update(bit_field_, CodeEventListener::BUILTIN_TAG);
135 26593 : bit_field_ = BuiltinIdField::update(bit_field_, id);
136 26593 : }
137 :
138 :
139 0 : int CodeEntry::GetSourceLine(int pc_offset) const {
140 79078 : if (line_info_) return line_info_->GetSourceLineNumber(pc_offset);
141 : return v8::CpuProfileNode::kNoLineNumberInfo;
142 : }
143 :
144 10 : void CodeEntry::SetInlineStacks(
145 : std::unordered_set<std::unique_ptr<CodeEntry>, Hasher, Equals>
146 : inline_entries,
147 : std::unordered_map<int, std::vector<CodeEntryAndLineNumber>>
148 : inline_stacks) {
149 10 : EnsureRareData()->inline_entries_ = std::move(inline_entries);
150 : rare_data_->inline_stacks_ = std::move(inline_stacks);
151 10 : }
152 :
153 51102 : const std::vector<CodeEntryAndLineNumber>* CodeEntry::GetInlineStack(
154 : int pc_offset) const {
155 51102 : if (!line_info_) return nullptr;
156 :
157 49761 : int inlining_id = line_info_->GetInliningId(pc_offset);
158 49761 : if (inlining_id == SourcePosition::kNotInlined) return nullptr;
159 : DCHECK(rare_data_);
160 :
161 : auto it = rare_data_->inline_stacks_.find(inlining_id);
162 2743 : return it != rare_data_->inline_stacks_.end() ? &it->second : nullptr;
163 : }
164 :
165 0 : void CodeEntry::set_deopt_info(
166 : const char* deopt_reason, int deopt_id,
167 : std::vector<CpuProfileDeoptFrame> inlined_frames) {
168 : DCHECK(!has_deopt_info());
169 0 : RareData* rare_data = EnsureRareData();
170 0 : rare_data->deopt_reason_ = deopt_reason;
171 0 : rare_data->deopt_id_ = deopt_id;
172 0 : rare_data->deopt_inlined_frames_ = std::move(inlined_frames);
173 0 : }
174 :
175 603536 : void CodeEntry::FillFunctionInfo(SharedFunctionInfo shared) {
176 1810608 : if (!shared->script()->IsScript()) return;
177 603536 : Script script = Script::cast(shared->script());
178 : set_script_id(script->id());
179 603536 : set_position(shared->StartPosition());
180 603536 : if (shared->optimization_disabled()) {
181 61 : set_bailout_reason(GetBailoutReason(shared->disable_optimization_reason()));
182 : }
183 : }
184 :
185 0 : CpuProfileDeoptInfo CodeEntry::GetDeoptInfo() {
186 : DCHECK(has_deopt_info());
187 :
188 : CpuProfileDeoptInfo info;
189 0 : info.deopt_reason = rare_data_->deopt_reason_;
190 : DCHECK_NE(kNoDeoptimizationId, rare_data_->deopt_id_);
191 0 : if (rare_data_->deopt_inlined_frames_.empty()) {
192 : info.stack.push_back(CpuProfileDeoptFrame(
193 0 : {script_id_, static_cast<size_t>(std::max(0, position()))}));
194 : } else {
195 0 : info.stack = rare_data_->deopt_inlined_frames_;
196 : }
197 0 : return info;
198 : }
199 :
200 71 : CodeEntry::RareData* CodeEntry::EnsureRareData() {
201 71 : if (!rare_data_) {
202 71 : rare_data_.reset(new RareData());
203 : }
204 71 : return rare_data_.get();
205 : }
206 :
207 0 : void CodeEntry::print() const {
208 0 : base::OS::Print("CodeEntry: at %p\n", this);
209 :
210 0 : base::OS::Print(" - name: %s\n", name_);
211 0 : base::OS::Print(" - resource_name: %s\n", resource_name_);
212 0 : base::OS::Print(" - line_number: %d\n", line_number_);
213 0 : base::OS::Print(" - column_number: %d\n", column_number_);
214 0 : base::OS::Print(" - script_id: %d\n", script_id_);
215 0 : base::OS::Print(" - position: %d\n", position_);
216 : base::OS::Print(" - instruction_start: %p\n",
217 0 : reinterpret_cast<void*>(instruction_start_));
218 :
219 0 : if (line_info_) {
220 0 : line_info_->print();
221 : }
222 :
223 0 : if (rare_data_) {
224 0 : base::OS::Print(" - deopt_reason: %s\n", rare_data_->deopt_reason_);
225 0 : base::OS::Print(" - bailout_reason: %s\n", rare_data_->bailout_reason_);
226 0 : base::OS::Print(" - deopt_id: %d\n", rare_data_->deopt_id_);
227 :
228 0 : if (!rare_data_->inline_stacks_.empty()) {
229 0 : base::OS::Print(" - inline stacks:\n");
230 0 : for (auto it = rare_data_->inline_stacks_.begin();
231 : it != rare_data_->inline_stacks_.end(); it++) {
232 0 : base::OS::Print(" inlining_id: [%d]\n", it->first);
233 0 : for (const auto& e : it->second) {
234 : base::OS::Print(" %s --> %d\n", e.code_entry->name(),
235 0 : e.line_number);
236 : }
237 : }
238 : } else {
239 0 : base::OS::Print(" - inline stacks: (empty)\n");
240 : }
241 :
242 0 : if (!rare_data_->deopt_inlined_frames_.empty()) {
243 0 : base::OS::Print(" - deopt inlined frames:\n");
244 0 : for (const CpuProfileDeoptFrame& frame :
245 : rare_data_->deopt_inlined_frames_) {
246 : base::OS::Print("script_id: %d position: %zu\n", frame.script_id,
247 0 : frame.position);
248 : }
249 : } else {
250 0 : base::OS::Print(" - deopt inlined frames: (empty)\n");
251 : }
252 : }
253 0 : base::OS::Print("\n");
254 0 : }
255 :
256 0 : void ProfileNode::CollectDeoptInfo(CodeEntry* entry) {
257 0 : deopt_infos_.push_back(entry->GetDeoptInfo());
258 : entry->clear_deopt_info();
259 0 : }
260 :
261 350 : ProfileNode* ProfileNode::FindChild(CodeEntry* entry, int line_number) {
262 700 : auto map_entry = children_.find({entry, line_number});
263 350 : return map_entry != children_.end() ? map_entry->second : nullptr;
264 : }
265 :
266 74707 : ProfileNode* ProfileNode::FindOrAddChild(CodeEntry* entry, int line_number) {
267 149414 : auto map_entry = children_.find({entry, line_number});
268 74707 : if (map_entry == children_.end()) {
269 2590 : ProfileNode* node = new ProfileNode(tree_, entry, this, line_number);
270 5180 : children_[{entry, line_number}] = node;
271 2590 : children_list_.push_back(node);
272 2590 : return node;
273 : } else {
274 72117 : return map_entry->second;
275 : }
276 : }
277 :
278 :
279 25204 : void ProfileNode::IncrementLineTicks(int src_line) {
280 50408 : if (src_line == v8::CpuProfileNode::kNoLineNumberInfo) return;
281 : // Increment a hit counter of a certain source line.
282 : // Add a new source line if not found.
283 : auto map_entry = line_ticks_.find(src_line);
284 25204 : if (map_entry == line_ticks_.end()) {
285 254 : line_ticks_[src_line] = 1;
286 : } else {
287 24950 : line_ticks_[src_line]++;
288 : }
289 : }
290 :
291 :
292 12 : bool ProfileNode::GetLineTicks(v8::CpuProfileNode::LineTick* entries,
293 : unsigned int length) const {
294 12 : if (entries == nullptr || length == 0) return false;
295 :
296 12 : unsigned line_count = static_cast<unsigned>(line_ticks_.size());
297 :
298 12 : if (line_count == 0) return true;
299 12 : if (length < line_count) return false;
300 :
301 : v8::CpuProfileNode::LineTick* entry = entries;
302 :
303 34 : for (auto p = line_ticks_.begin(); p != line_ticks_.end(); p++, entry++) {
304 22 : entry->line = p->first;
305 22 : entry->hit_count = p->second;
306 : }
307 :
308 : return true;
309 : }
310 :
311 :
312 2748 : void ProfileNode::Print(int indent) {
313 4147 : int line_number = line_number_ != 0 ? line_number_ : entry_->line_number();
314 : base::OS::Print("%5u %*s %s:%d %d #%d", self_ticks_, indent, "",
315 2748 : entry_->name(), line_number, entry_->script_id(), id());
316 2748 : if (entry_->resource_name()[0] != '\0')
317 25 : base::OS::Print(" %s:%d", entry_->resource_name(), entry_->line_number());
318 1374 : base::OS::Print("\n");
319 2748 : for (size_t i = 0; i < deopt_infos_.size(); ++i) {
320 1374 : CpuProfileDeoptInfo& info = deopt_infos_[i];
321 : base::OS::Print("%*s;;; deopted at script_id: %d position: %" PRIuS
322 : " with reason '%s'.\n",
323 : indent + 10, "", info.stack[0].script_id,
324 0 : info.stack[0].position, info.deopt_reason);
325 0 : for (size_t index = 1; index < info.stack.size(); ++index) {
326 : base::OS::Print("%*s;;; Inline point: script_id %d position: %" PRIuS
327 : ".\n",
328 : indent + 10, "", info.stack[index].script_id,
329 0 : info.stack[index].position);
330 : }
331 : }
332 1374 : const char* bailout_reason = entry_->bailout_reason();
333 1374 : if (bailout_reason != GetBailoutReason(BailoutReason::kNoReason) &&
334 : bailout_reason != CodeEntry::kEmptyBailoutReason) {
335 : base::OS::Print("%*s bailed out due to '%s'\n", indent + 10, "",
336 88 : bailout_reason);
337 : }
338 3554 : for (auto child : children_) {
339 806 : child.second->Print(indent + 2);
340 : }
341 1374 : }
342 :
343 :
344 : class DeleteNodesCallback {
345 : public:
346 : void BeforeTraversingChild(ProfileNode*, ProfileNode*) { }
347 :
348 3953 : void AfterAllChildrenTraversed(ProfileNode* node) {
349 3953 : delete node;
350 3953 : }
351 :
352 : void AfterChildTraversed(ProfileNode*, ProfileNode*) { }
353 : };
354 :
355 1363 : ProfileTree::ProfileTree(Isolate* isolate)
356 : : root_entry_(CodeEventListener::FUNCTION_TAG, "(root)"),
357 : next_node_id_(1),
358 1363 : root_(new ProfileNode(this, &root_entry_, nullptr)),
359 : isolate_(isolate),
360 4089 : next_function_id_(1) {}
361 :
362 2726 : ProfileTree::~ProfileTree() {
363 : DeleteNodesCallback cb;
364 1363 : TraverseDepthFirst(&cb);
365 1363 : }
366 :
367 :
368 0 : unsigned ProfileTree::GetFunctionId(const ProfileNode* node) {
369 0 : CodeEntry* code_entry = node->entry();
370 : auto map_entry = function_ids_.find(code_entry);
371 0 : if (map_entry == function_ids_.end()) {
372 0 : return function_ids_[code_entry] = next_function_id_++;
373 : }
374 0 : return function_ids_[code_entry];
375 : }
376 :
377 95 : ProfileNode* ProfileTree::AddPathFromEnd(const std::vector<CodeEntry*>& path,
378 : int src_line, bool update_stats) {
379 95 : ProfileNode* node = root_;
380 : CodeEntry* last_entry = nullptr;
381 290 : for (auto it = path.rbegin(); it != path.rend(); ++it) {
382 195 : if (*it == nullptr) continue;
383 : last_entry = *it;
384 145 : node = node->FindOrAddChild(*it, v8::CpuProfileNode::kNoLineNumberInfo);
385 : }
386 190 : if (last_entry && last_entry->has_deopt_info()) {
387 0 : node->CollectDeoptInfo(last_entry);
388 : }
389 95 : if (update_stats) {
390 : node->IncrementSelfTicks();
391 95 : if (src_line != v8::CpuProfileNode::kNoLineNumberInfo) {
392 0 : node->IncrementLineTicks(src_line);
393 : }
394 : }
395 95 : return node;
396 : }
397 :
398 27707 : ProfileNode* ProfileTree::AddPathFromEnd(const ProfileStackTrace& path,
399 : int src_line, bool update_stats,
400 : ProfilingMode mode) {
401 27707 : ProfileNode* node = root_;
402 : CodeEntry* last_entry = nullptr;
403 : int parent_line_number = v8::CpuProfileNode::kNoLineNumberInfo;
404 115131 : for (auto it = path.rbegin(); it != path.rend(); ++it) {
405 87424 : if ((*it).code_entry == nullptr) continue;
406 : last_entry = (*it).code_entry;
407 74477 : node = node->FindOrAddChild((*it).code_entry, parent_line_number);
408 : parent_line_number = mode == ProfilingMode::kCallerLineNumbers
409 : ? (*it).line_number
410 74477 : : v8::CpuProfileNode::kNoLineNumberInfo;
411 : }
412 55343 : if (last_entry && last_entry->has_deopt_info()) {
413 0 : node->CollectDeoptInfo(last_entry);
414 : }
415 27707 : if (update_stats) {
416 : node->IncrementSelfTicks();
417 26894 : if (src_line != v8::CpuProfileNode::kNoLineNumberInfo) {
418 25104 : node->IncrementLineTicks(src_line);
419 : }
420 : }
421 27707 : return node;
422 : }
423 :
424 :
425 : class Position {
426 : public:
427 : explicit Position(ProfileNode* node)
428 3953 : : node(node), child_idx_(0) { }
429 : V8_INLINE ProfileNode* current_child() {
430 5180 : return node->children()->at(child_idx_);
431 : }
432 : V8_INLINE bool has_current_child() {
433 6543 : return child_idx_ < static_cast<int>(node->children()->size());
434 : }
435 2590 : V8_INLINE void next_child() { ++child_idx_; }
436 :
437 : ProfileNode* node;
438 : private:
439 : int child_idx_;
440 : };
441 :
442 :
443 : // Non-recursive implementation of a depth-first post-order tree traversal.
444 : template <typename Callback>
445 1363 : void ProfileTree::TraverseDepthFirst(Callback* callback) {
446 : std::vector<Position> stack;
447 1363 : stack.emplace_back(root_);
448 17175 : while (stack.size() > 0) {
449 11723 : Position& current = stack.back();
450 6543 : if (current.has_current_child()) {
451 : callback->BeforeTraversingChild(current.node, current.current_child());
452 2590 : stack.emplace_back(current.current_child());
453 : } else {
454 3953 : callback->AfterAllChildrenTraversed(current.node);
455 7906 : if (stack.size() > 1) {
456 2590 : Position& parent = stack[stack.size() - 2];
457 : callback->AfterChildTraversed(parent.node, current.node);
458 : parent.next_child();
459 : }
460 : // Remove child from the stack.
461 : stack.pop_back();
462 : }
463 : }
464 1363 : }
465 :
466 : using v8::tracing::TracedValue;
467 :
468 : std::atomic<uint32_t> CpuProfile::last_id_;
469 :
470 2636 : CpuProfile::CpuProfile(CpuProfiler* profiler, const char* title,
471 : bool record_samples, ProfilingMode mode)
472 : : title_(title),
473 : record_samples_(record_samples),
474 : mode_(mode),
475 : start_time_(base::TimeTicks::HighResolutionNow()),
476 : top_down_(profiler->isolate()),
477 : profiler_(profiler),
478 : streaming_next_sample_(0),
479 3954 : id_(++last_id_) {
480 1318 : auto value = TracedValue::Create();
481 : value->SetDouble("startTime",
482 2636 : (start_time_ - base::TimeTicks()).InMicroseconds());
483 2636 : TRACE_EVENT_SAMPLE_WITH_ID1(TRACE_DISABLED_BY_DEFAULT("v8.cpu_profiler"),
484 : "Profile", id_, "data", std::move(value));
485 1318 : }
486 :
487 27702 : void CpuProfile::AddPath(base::TimeTicks timestamp,
488 : const ProfileStackTrace& path, int src_line,
489 : bool update_stats) {
490 : ProfileNode* top_frame_node =
491 27702 : top_down_.AddPathFromEnd(path, src_line, update_stats, mode_);
492 :
493 27786 : if (record_samples_ && !timestamp.IsNull()) {
494 68 : timestamps_.push_back(timestamp);
495 68 : samples_.push_back(top_frame_node);
496 : }
497 :
498 : const int kSamplesFlushCount = 100;
499 : const int kNodesFlushCount = 10;
500 55404 : if (samples_.size() - streaming_next_sample_ >= kSamplesFlushCount ||
501 : top_down_.pending_nodes_count() >= kNodesFlushCount) {
502 10 : StreamPendingTraceEvents();
503 : }
504 27702 : }
505 :
506 : namespace {
507 :
508 9123 : void BuildNodeValue(const ProfileNode* node, TracedValue* value) {
509 15205 : const CodeEntry* entry = node->entry();
510 3041 : value->BeginDictionary("callFrame");
511 3041 : value->SetString("functionName", entry->name());
512 3041 : if (*entry->resource_name()) {
513 35 : value->SetString("url", entry->resource_name());
514 : }
515 3041 : value->SetInteger("scriptId", entry->script_id());
516 3041 : if (entry->line_number()) {
517 254 : value->SetInteger("lineNumber", entry->line_number() - 1);
518 : }
519 3041 : if (entry->column_number()) {
520 254 : value->SetInteger("columnNumber", entry->column_number() - 1);
521 : }
522 3041 : value->EndDictionary();
523 3041 : value->SetInteger("id", node->id());
524 3041 : if (node->parent()) {
525 2308 : value->SetInteger("parent", node->parent()->id());
526 : }
527 : const char* deopt_reason = entry->bailout_reason();
528 3041 : if (deopt_reason && deopt_reason[0] && strcmp(deopt_reason, "no reason")) {
529 89 : value->SetString("deoptReason", deopt_reason);
530 : }
531 3041 : }
532 :
533 : } // namespace
534 :
535 743 : void CpuProfile::StreamPendingTraceEvents() {
536 : std::vector<const ProfileNode*> pending_nodes = top_down_.TakePendingNodes();
537 3117 : if (pending_nodes.empty() && samples_.empty()) return;
538 738 : auto value = TracedValue::Create();
539 :
540 738 : if (!pending_nodes.empty() || streaming_next_sample_ != samples_.size()) {
541 738 : value->BeginDictionary("cpuProfile");
542 738 : if (!pending_nodes.empty()) {
543 738 : value->BeginArray("nodes");
544 4517 : for (auto node : pending_nodes) {
545 3041 : value->BeginDictionary();
546 3041 : BuildNodeValue(node, value.get());
547 3041 : value->EndDictionary();
548 : }
549 738 : value->EndArray();
550 : }
551 1476 : if (streaming_next_sample_ != samples_.size()) {
552 41 : value->BeginArray("samples");
553 218 : for (size_t i = streaming_next_sample_; i < samples_.size(); ++i) {
554 136 : value->AppendInteger(samples_[i]->id());
555 : }
556 41 : value->EndArray();
557 : }
558 738 : value->EndDictionary();
559 : }
560 1476 : if (streaming_next_sample_ != samples_.size()) {
561 41 : value->BeginArray("timeDeltas");
562 : base::TimeTicks lastTimestamp =
563 177 : streaming_next_sample_ ? timestamps_[streaming_next_sample_ - 1]
564 41 : : start_time();
565 218 : for (size_t i = streaming_next_sample_; i < timestamps_.size(); ++i) {
566 : value->AppendInteger(
567 136 : static_cast<int>((timestamps_[i] - lastTimestamp).InMicroseconds()));
568 68 : lastTimestamp = timestamps_[i];
569 : }
570 41 : value->EndArray();
571 : DCHECK_EQ(samples_.size(), timestamps_.size());
572 41 : streaming_next_sample_ = samples_.size();
573 : }
574 :
575 1476 : TRACE_EVENT_SAMPLE_WITH_ID1(TRACE_DISABLED_BY_DEFAULT("v8.cpu_profiler"),
576 : "ProfileChunk", id_, "data", std::move(value));
577 : }
578 :
579 733 : void CpuProfile::FinishProfile() {
580 733 : end_time_ = base::TimeTicks::HighResolutionNow();
581 733 : StreamPendingTraceEvents();
582 733 : auto value = TracedValue::Create();
583 1466 : value->SetDouble("endTime", (end_time_ - base::TimeTicks()).InMicroseconds());
584 1466 : TRACE_EVENT_SAMPLE_WITH_ID1(TRACE_DISABLED_BY_DEFAULT("v8.cpu_profiler"),
585 : "ProfileChunk", id_, "data", std::move(value));
586 733 : }
587 :
588 545 : void CpuProfile::Print() {
589 545 : base::OS::Print("[Top down]:\n");
590 545 : top_down_.Print();
591 545 : }
592 :
593 : CodeMap::CodeMap() = default;
594 :
595 793 : CodeMap::~CodeMap() {
596 : // First clean the free list as it's otherwise impossible to tell
597 : // the slot type.
598 793 : unsigned free_slot = free_list_head_;
599 1591 : while (free_slot != kNoFreeSlot) {
600 5 : unsigned next_slot = code_entries_[free_slot].next_free_slot;
601 5 : code_entries_[free_slot].entry = nullptr;
602 : free_slot = next_slot;
603 : }
604 3008863 : for (auto slot : code_entries_) delete slot.entry;
605 793 : }
606 :
607 1510821 : void CodeMap::AddCode(Address addr, CodeEntry* entry, unsigned size) {
608 1510821 : ClearCodesInRange(addr, addr + size);
609 1510819 : unsigned index = AddCodeEntry(addr, entry);
610 3021628 : code_map_.emplace(addr, CodeEntryMapInfo{index, size});
611 : DCHECK(entry->instruction_start() == kNullAddress ||
612 : addr == entry->instruction_start());
613 1510816 : }
614 :
615 1510831 : void CodeMap::ClearCodesInRange(Address start, Address end) {
616 : auto left = code_map_.upper_bound(start);
617 1510831 : if (left != code_map_.begin()) {
618 : --left;
619 1506265 : if (left->first + left->second.size <= start) ++left;
620 : }
621 : auto right = left;
622 3028445 : for (; right != code_map_.end() && right->first < end; ++right) {
623 20358 : if (!entry(right->second.index)->used()) {
624 6777 : DeleteCodeEntry(right->second.index);
625 : }
626 : }
627 : code_map_.erase(left, right);
628 1510830 : }
629 :
630 1228106 : CodeEntry* CodeMap::FindEntry(Address addr) {
631 : auto it = code_map_.upper_bound(addr);
632 1228106 : if (it == code_map_.begin()) return nullptr;
633 : --it;
634 1223399 : Address start_address = it->first;
635 1223399 : Address end_address = start_address + it->second.size;
636 1223399 : CodeEntry* ret = addr < end_address ? entry(it->second.index) : nullptr;
637 : if (ret && ret->instruction_start() != kNullAddress) {
638 : DCHECK_EQ(start_address, ret->instruction_start());
639 : DCHECK(addr >= start_address && addr < end_address);
640 : }
641 1223399 : return ret;
642 : }
643 :
644 10 : void CodeMap::MoveCode(Address from, Address to) {
645 10 : if (from == to) return;
646 : auto it = code_map_.find(from);
647 10 : if (it == code_map_.end()) return;
648 10 : CodeEntryMapInfo info = it->second;
649 : code_map_.erase(it);
650 : DCHECK(from + info.size <= to || to + info.size <= from);
651 10 : ClearCodesInRange(to, to + info.size);
652 : code_map_.emplace(to, info);
653 :
654 20 : CodeEntry* entry = code_entries_[info.index].entry;
655 10 : entry->set_instruction_start(to);
656 : }
657 :
658 1510818 : unsigned CodeMap::AddCodeEntry(Address start, CodeEntry* entry) {
659 1510818 : if (free_list_head_ == kNoFreeSlot) {
660 3008090 : code_entries_.push_back(CodeEntrySlotInfo{entry});
661 1504044 : return static_cast<unsigned>(code_entries_.size()) - 1;
662 : }
663 : unsigned index = free_list_head_;
664 6772 : free_list_head_ = code_entries_[index].next_free_slot;
665 6772 : code_entries_[index].entry = entry;
666 6772 : return index;
667 : }
668 :
669 6777 : void CodeMap::DeleteCodeEntry(unsigned index) {
670 6777 : delete code_entries_[index].entry;
671 6777 : code_entries_[index].next_free_slot = free_list_head_;
672 6777 : free_list_head_ = index;
673 6777 : }
674 :
675 0 : void CodeMap::Print() {
676 0 : for (const auto& pair : code_map_) {
677 : base::OS::Print("%p %5d %s\n", reinterpret_cast<void*>(pair.first),
678 0 : pair.second.size, entry(pair.second.index)->name());
679 : }
680 0 : }
681 :
682 998 : CpuProfilesCollection::CpuProfilesCollection(Isolate* isolate)
683 2994 : : profiler_(nullptr), current_profiles_semaphore_(1) {}
684 :
685 1333 : bool CpuProfilesCollection::StartProfiling(const char* title,
686 : bool record_samples,
687 : ProfilingMode mode) {
688 1333 : current_profiles_semaphore_.Wait();
689 2666 : if (static_cast<int>(current_profiles_.size()) >= kMaxSimultaneousProfiles) {
690 5 : current_profiles_semaphore_.Signal();
691 5 : return false;
692 : }
693 26113 : for (const std::unique_ptr<CpuProfile>& profile : current_profiles_) {
694 24795 : if (strcmp(profile->title(), title) == 0) {
695 : // Ignore attempts to start profile with the same title...
696 10 : current_profiles_semaphore_.Signal();
697 : // ... though return true to force it collect a sample.
698 : return true;
699 : }
700 : }
701 : current_profiles_.emplace_back(
702 1318 : new CpuProfile(profiler_, title, record_samples, mode));
703 1318 : current_profiles_semaphore_.Signal();
704 1318 : return true;
705 : }
706 :
707 :
708 738 : CpuProfile* CpuProfilesCollection::StopProfiling(const char* title) {
709 1476 : const int title_len = StrLength(title);
710 : CpuProfile* profile = nullptr;
711 738 : current_profiles_semaphore_.Wait();
712 :
713 : auto it =
714 : std::find_if(current_profiles_.rbegin(), current_profiles_.rend(),
715 743 : [&](const std::unique_ptr<CpuProfile>& p) {
716 1428 : return title_len == 0 || strcmp(p->title(), title) == 0;
717 743 : });
718 :
719 738 : if (it != current_profiles_.rend()) {
720 733 : (*it)->FinishProfile();
721 : profile = it->get();
722 733 : finished_profiles_.push_back(std::move(*it));
723 : // Convert reverse iterator to matching forward iterator.
724 733 : current_profiles_.erase(--(it.base()));
725 : }
726 :
727 738 : current_profiles_semaphore_.Signal();
728 738 : return profile;
729 : }
730 :
731 :
732 703 : bool CpuProfilesCollection::IsLastProfile(const char* title) {
733 : // Called from VM thread, and only it can mutate the list,
734 : // so no locking is needed here.
735 1406 : if (current_profiles_.size() != 1) return false;
736 : return StrLength(title) == 0
737 1323 : || strcmp(current_profiles_[0]->title(), title) == 0;
738 : }
739 :
740 :
741 630 : void CpuProfilesCollection::RemoveProfile(CpuProfile* profile) {
742 : // Called from VM thread for a completed profile.
743 : auto pos =
744 : std::find_if(finished_profiles_.begin(), finished_profiles_.end(),
745 : [&](const std::unique_ptr<CpuProfile>& finished_profile) {
746 630 : return finished_profile.get() == profile;
747 630 : });
748 : DCHECK(pos != finished_profiles_.end());
749 630 : finished_profiles_.erase(pos);
750 630 : }
751 :
752 27689 : void CpuProfilesCollection::AddPathToCurrentProfiles(
753 : base::TimeTicks timestamp, const ProfileStackTrace& path, int src_line,
754 : bool update_stats) {
755 : // As starting / stopping profiles is rare relatively to this
756 : // method, we don't bother minimizing the duration of lock holding,
757 : // e.g. copying contents of the list to a local vector.
758 27689 : current_profiles_semaphore_.Wait();
759 83080 : for (const std::unique_ptr<CpuProfile>& profile : current_profiles_) {
760 55404 : profile->AddPath(timestamp, path, src_line, update_stats);
761 : }
762 27689 : current_profiles_semaphore_.Signal();
763 27689 : }
764 :
765 788 : ProfileGenerator::ProfileGenerator(CpuProfilesCollection* profiles)
766 1576 : : profiles_(profiles) {}
767 :
768 27689 : void ProfileGenerator::RecordTickSample(const TickSample& sample) {
769 : ProfileStackTrace stack_trace;
770 : // Conservatively reserve space for stack frames + pc + function + vm-state.
771 : // There could in fact be more of them because of inlined entries.
772 27689 : stack_trace.reserve(sample.frames_count + 3);
773 :
774 : // The ProfileNode knows nothing about all versions of generated code for
775 : // the same JS function. The line number information associated with
776 : // the latest version of generated code is used to find a source line number
777 : // for a JS function. Then, the detected source line is passed to
778 : // ProfileNode to increase the tick count for this source line.
779 : const int no_line_info = v8::CpuProfileNode::kNoLineNumberInfo;
780 : int src_line = no_line_info;
781 : bool src_line_not_found = true;
782 :
783 27689 : if (sample.pc != nullptr) {
784 26939 : if (sample.has_external_callback && sample.state == EXTERNAL) {
785 : // Don't use PC when in external callback code, as it can point
786 : // inside a callback's code, and we will erroneously report
787 : // that a callback calls itself.
788 : stack_trace.push_back(
789 12536 : {FindEntry(reinterpret_cast<Address>(sample.external_callback_entry)),
790 25072 : no_line_info});
791 : } else {
792 14403 : Address attributed_pc = reinterpret_cast<Address>(sample.pc);
793 14877 : CodeEntry* pc_entry = FindEntry(attributed_pc);
794 : // If there is no pc_entry, we're likely in native code. Find out if the
795 : // top of the stack (the return address) was pointing inside a JS
796 : // function, meaning that we have encountered a frameless invocation.
797 14403 : if (!pc_entry && !sample.has_external_callback) {
798 14315 : attributed_pc = reinterpret_cast<Address>(sample.tos);
799 14315 : pc_entry = FindEntry(attributed_pc);
800 : }
801 : // If pc is in the function code before it set up stack frame or after the
802 : // frame was destroyed, SafeStackFrameIterator incorrectly thinks that
803 : // ebp contains the return address of the current function and skips the
804 : // caller's frame. Check for this case and just skip such samples.
805 14403 : if (pc_entry) {
806 : int pc_offset =
807 418 : static_cast<int>(attributed_pc - pc_entry->instruction_start());
808 : // TODO(petermarshall): pc_offset can still be negative in some cases.
809 : src_line = pc_entry->GetSourceLine(pc_offset);
810 209 : if (src_line == v8::CpuProfileNode::kNoLineNumberInfo) {
811 : src_line = pc_entry->line_number();
812 : }
813 : src_line_not_found = false;
814 418 : stack_trace.push_back({pc_entry, src_line});
815 :
816 209 : if (pc_entry->builtin_id() == Builtins::kFunctionPrototypeApply ||
817 : pc_entry->builtin_id() == Builtins::kFunctionPrototypeCall) {
818 : // When current function is either the Function.prototype.apply or the
819 : // Function.prototype.call builtin the top frame is either frame of
820 : // the calling JS function or internal frame.
821 : // In the latter case we know the caller for sure but in the
822 : // former case we don't so we simply replace the frame with
823 : // 'unresolved' entry.
824 0 : if (!sample.has_external_callback) {
825 : stack_trace.push_back(
826 0 : {CodeEntry::unresolved_entry(), no_line_info});
827 : }
828 : }
829 : }
830 : }
831 :
832 64044 : for (unsigned i = 0; i < sample.frames_count; ++i) {
833 64044 : Address stack_pos = reinterpret_cast<Address>(sample.stack[i]);
834 115146 : CodeEntry* entry = FindEntry(stack_pos);
835 : int line_number = no_line_info;
836 64044 : if (entry) {
837 : // Find out if the entry has an inlining stack associated.
838 : int pc_offset =
839 102204 : static_cast<int>(stack_pos - entry->instruction_start());
840 : // TODO(petermarshall): pc_offset can still be negative in some cases.
841 5486 : const std::vector<CodeEntryAndLineNumber>* inline_stack =
842 51102 : entry->GetInlineStack(pc_offset);
843 51102 : if (inline_stack) {
844 : int most_inlined_frame_line_number = entry->GetSourceLine(pc_offset);
845 : stack_trace.insert(stack_trace.end(), inline_stack->begin(),
846 2743 : inline_stack->end());
847 : // This is a bit of a messy hack. The line number for the most-inlined
848 : // frame (the function at the end of the chain of function calls) has
849 : // the wrong line number in inline_stack. The actual line number in
850 : // this function is stored in the SourcePositionTable in entry. We fix
851 : // up the line number for the most-inlined frame here.
852 : // TODO(petermarshall): Remove this and use a tree with a node per
853 : // inlining_id.
854 : DCHECK(!inline_stack->empty());
855 5486 : size_t index = stack_trace.size() - inline_stack->size();
856 2743 : stack_trace[index].line_number = most_inlined_frame_line_number;
857 : }
858 : // Skip unresolved frames (e.g. internal frame) and get source line of
859 : // the first JS caller.
860 51102 : if (src_line_not_found) {
861 : src_line = entry->GetSourceLine(pc_offset);
862 25024 : if (src_line == v8::CpuProfileNode::kNoLineNumberInfo) {
863 : src_line = entry->line_number();
864 : }
865 : src_line_not_found = false;
866 : }
867 : line_number = entry->GetSourceLine(pc_offset);
868 :
869 : // The inline stack contains the top-level function i.e. the same
870 : // function as entry. We don't want to add it twice. The one from the
871 : // inline stack has the correct line number for this particular inlining
872 : // so we use it instead of pushing entry to stack_trace.
873 51102 : if (inline_stack) continue;
874 : }
875 122602 : stack_trace.push_back({entry, line_number});
876 : }
877 : }
878 :
879 27689 : if (FLAG_prof_browser_mode) {
880 : bool no_symbolized_entries = true;
881 55517 : for (auto e : stack_trace) {
882 25547 : if (e.code_entry != nullptr) {
883 : no_symbolized_entries = false;
884 : break;
885 : }
886 : }
887 : // If no frames were symbolized, put the VM state entry in.
888 27573 : if (no_symbolized_entries) {
889 4794 : stack_trace.push_back({EntryForVMState(sample.state), no_line_info});
890 : }
891 : }
892 :
893 : profiles_->AddPathToCurrentProfiles(sample.timestamp, stack_trace, src_line,
894 27689 : sample.update_stats);
895 27689 : }
896 :
897 2397 : CodeEntry* ProfileGenerator::EntryForVMState(StateTag tag) {
898 2397 : switch (tag) {
899 : case GC:
900 1018 : return CodeEntry::gc_entry();
901 : case JS:
902 : case PARSER:
903 : case COMPILER:
904 : case BYTECODE_COMPILER:
905 : // DOM events handlers are reported as OTHER / EXTERNAL entries.
906 : // To avoid confusing people, let's put all these entries into
907 : // one bucket.
908 : case OTHER:
909 : case EXTERNAL:
910 1364 : return CodeEntry::program_entry();
911 : case IDLE:
912 15 : return CodeEntry::idle_entry();
913 : }
914 0 : UNREACHABLE();
915 : }
916 :
917 : } // namespace internal
918 183867 : } // namespace v8
|