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.
7 #include "src/profile-generator-inl.h"
9 #include "src/compiler.h"
10 #include "src/debug.h"
11 #include "src/deoptimizer.h"
12 #include "src/global-handles.h"
13 #include "src/sampler.h"
14 #include "src/scopeinfo.h"
15 #include "src/unicode.h"
21 bool StringsStorage::StringsMatch(void* key1, void* key2) {
22 return strcmp(reinterpret_cast<char*>(key1),
23 reinterpret_cast<char*>(key2)) == 0;
27 StringsStorage::StringsStorage(Heap* heap)
28 : hash_seed_(heap->HashSeed()), names_(StringsMatch) {
32 StringsStorage::~StringsStorage() {
33 for (HashMap::Entry* p = names_.Start();
36 DeleteArray(reinterpret_cast<const char*>(p->value));
41 const char* StringsStorage::GetCopy(const char* src) {
42 int len = static_cast<int>(strlen(src));
43 HashMap::Entry* entry = GetEntry(src, len);
44 if (entry->value == NULL) {
45 Vector<char> dst = Vector<char>::New(len + 1);
46 StrNCpy(dst, src, len);
48 entry->key = dst.start();
49 entry->value = entry->key;
51 return reinterpret_cast<const char*>(entry->value);
55 const char* StringsStorage::GetFormatted(const char* format, ...) {
57 va_start(args, format);
58 const char* result = GetVFormatted(format, args);
64 const char* StringsStorage::AddOrDisposeString(char* str, int len) {
65 HashMap::Entry* entry = GetEntry(str, len);
66 if (entry->value == NULL) {
73 return reinterpret_cast<const char*>(entry->value);
77 const char* StringsStorage::GetVFormatted(const char* format, va_list args) {
78 Vector<char> str = Vector<char>::New(1024);
79 int len = VSNPrintF(str, format, args);
81 DeleteArray(str.start());
82 return GetCopy(format);
84 return AddOrDisposeString(str.start(), len);
88 const char* StringsStorage::GetName(Name* name) {
89 if (name->IsString()) {
90 String* str = String::cast(name);
91 int length = Min(kMaxNameSize, str->length());
92 int actual_length = 0;
93 SmartArrayPointer<char> data =
94 str->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL, 0, length,
96 return AddOrDisposeString(data.Detach(), actual_length);
97 } else if (name->IsSymbol()) {
104 const char* StringsStorage::GetName(int index) {
105 return GetFormatted("%d", index);
109 const char* StringsStorage::GetFunctionName(Name* name) {
110 return GetName(name);
114 const char* StringsStorage::GetFunctionName(const char* name) {
115 return GetCopy(name);
119 size_t StringsStorage::GetUsedMemorySize() const {
120 size_t size = sizeof(*this);
121 size += sizeof(HashMap::Entry) * names_.capacity();
122 for (HashMap::Entry* p = names_.Start(); p != NULL; p = names_.Next(p)) {
123 size += strlen(reinterpret_cast<const char*>(p->value)) + 1;
129 HashMap::Entry* StringsStorage::GetEntry(const char* str, int len) {
130 uint32_t hash = StringHasher::HashSequentialString(str, len, hash_seed_);
131 return names_.Lookup(const_cast<char*>(str), hash, true);
135 JITLineInfoTable::JITLineInfoTable() {}
138 JITLineInfoTable::~JITLineInfoTable() {}
141 void JITLineInfoTable::SetPosition(int pc_offset, int line) {
142 DCHECK(pc_offset >= 0);
143 DCHECK(line > 0); // The 1-based number of the source line.
144 if (GetSourceLineNumber(pc_offset) != line) {
145 pc_offset_map_.insert(std::make_pair(pc_offset, line));
150 int JITLineInfoTable::GetSourceLineNumber(int pc_offset) const {
151 PcOffsetMap::const_iterator it = pc_offset_map_.lower_bound(pc_offset);
152 if (it == pc_offset_map_.end()) {
153 if (pc_offset_map_.empty()) return v8::CpuProfileNode::kNoLineNumberInfo;
154 return (--pc_offset_map_.end())->second;
160 const char* const CodeEntry::kEmptyNamePrefix = "";
161 const char* const CodeEntry::kEmptyResourceName = "";
162 const char* const CodeEntry::kEmptyBailoutReason = "";
163 const char* const CodeEntry::kNoDeoptReason = "";
166 CodeEntry::~CodeEntry() {
167 delete no_frame_ranges_;
172 uint32_t CodeEntry::GetCallUid() const {
173 uint32_t hash = ComputeIntegerHash(tag(), v8::internal::kZeroHashSeed);
174 if (shared_id_ != 0) {
175 hash ^= ComputeIntegerHash(static_cast<uint32_t>(shared_id_),
176 v8::internal::kZeroHashSeed);
178 hash ^= ComputeIntegerHash(
179 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(name_prefix_)),
180 v8::internal::kZeroHashSeed);
181 hash ^= ComputeIntegerHash(
182 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(name_)),
183 v8::internal::kZeroHashSeed);
184 hash ^= ComputeIntegerHash(
185 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(resource_name_)),
186 v8::internal::kZeroHashSeed);
187 hash ^= ComputeIntegerHash(line_number_, v8::internal::kZeroHashSeed);
193 bool CodeEntry::IsSameAs(CodeEntry* entry) const {
194 return this == entry ||
195 (tag() == entry->tag() && shared_id_ == entry->shared_id_ &&
197 (name_prefix_ == entry->name_prefix_ && name_ == entry->name_ &&
198 resource_name_ == entry->resource_name_ &&
199 line_number_ == entry->line_number_)));
203 void CodeEntry::SetBuiltinId(Builtins::Name id) {
204 bit_field_ = TagField::update(bit_field_, Logger::BUILTIN_TAG);
205 bit_field_ = BuiltinIdField::update(bit_field_, id);
209 int CodeEntry::GetSourceLine(int pc_offset) const {
210 if (line_info_ && !line_info_->empty()) {
211 return line_info_->GetSourceLineNumber(pc_offset);
213 return v8::CpuProfileNode::kNoLineNumberInfo;
217 void ProfileNode::CollectDeoptInfo(CodeEntry* entry) {
218 deopt_infos_.Add(DeoptInfo(entry->deopt_reason(), entry->deopt_location()));
219 entry->clear_deopt_info();
223 ProfileNode* ProfileNode::FindChild(CodeEntry* entry) {
224 HashMap::Entry* map_entry =
225 children_.Lookup(entry, CodeEntryHash(entry), false);
226 return map_entry != NULL ?
227 reinterpret_cast<ProfileNode*>(map_entry->value) : NULL;
231 ProfileNode* ProfileNode::FindOrAddChild(CodeEntry* entry) {
232 HashMap::Entry* map_entry =
233 children_.Lookup(entry, CodeEntryHash(entry), true);
234 ProfileNode* node = reinterpret_cast<ProfileNode*>(map_entry->value);
237 node = new ProfileNode(tree_, entry);
238 map_entry->value = node;
239 children_list_.Add(node);
245 void ProfileNode::IncrementLineTicks(int src_line) {
246 if (src_line == v8::CpuProfileNode::kNoLineNumberInfo) return;
247 // Increment a hit counter of a certain source line.
248 // Add a new source line if not found.
250 line_ticks_.Lookup(reinterpret_cast<void*>(src_line), src_line, true);
252 e->value = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(e->value) + 1);
256 bool ProfileNode::GetLineTicks(v8::CpuProfileNode::LineTick* entries,
257 unsigned int length) const {
258 if (entries == NULL || length == 0) return false;
260 unsigned line_count = line_ticks_.occupancy();
262 if (line_count == 0) return true;
263 if (length < line_count) return false;
265 v8::CpuProfileNode::LineTick* entry = entries;
267 for (HashMap::Entry* p = line_ticks_.Start(); p != NULL;
268 p = line_ticks_.Next(p), entry++) {
270 static_cast<unsigned int>(reinterpret_cast<uintptr_t>(p->key));
272 static_cast<unsigned int>(reinterpret_cast<uintptr_t>(p->value));
279 void ProfileNode::Print(int indent) {
280 base::OS::Print("%5u %*s %s%s %d #%d", self_ticks_, indent, "",
281 entry_->name_prefix(), entry_->name(), entry_->script_id(),
283 if (entry_->resource_name()[0] != '\0')
284 base::OS::Print(" %s:%d", entry_->resource_name(), entry_->line_number());
285 base::OS::Print("\n");
286 for (auto info : deopt_infos_) {
287 base::OS::Print("%*s deopted at %d with reason '%s'\n", indent + 10, "",
288 info.deopt_location, info.deopt_reason);
290 const char* bailout_reason = entry_->bailout_reason();
291 if (bailout_reason != GetBailoutReason(BailoutReason::kNoReason) &&
292 bailout_reason != CodeEntry::kEmptyBailoutReason) {
293 base::OS::Print("%*s bailed out due to '%s'\n", indent + 10, "",
296 for (HashMap::Entry* p = children_.Start();
298 p = children_.Next(p)) {
299 reinterpret_cast<ProfileNode*>(p->value)->Print(indent + 2);
304 class DeleteNodesCallback {
306 void BeforeTraversingChild(ProfileNode*, ProfileNode*) { }
308 void AfterAllChildrenTraversed(ProfileNode* node) {
312 void AfterChildTraversed(ProfileNode*, ProfileNode*) { }
316 ProfileTree::ProfileTree()
317 : root_entry_(Logger::FUNCTION_TAG, "(root)"),
319 root_(new ProfileNode(this, &root_entry_)) {
323 ProfileTree::~ProfileTree() {
324 DeleteNodesCallback cb;
325 TraverseDepthFirst(&cb);
329 ProfileNode* ProfileTree::AddPathFromEnd(const Vector<CodeEntry*>& path,
331 ProfileNode* node = root_;
332 CodeEntry* last_entry = NULL;
333 for (CodeEntry** entry = path.start() + path.length() - 1;
334 entry != path.start() - 1;
336 if (*entry != NULL) {
337 node = node->FindOrAddChild(*entry);
341 if (last_entry && last_entry->has_deopt_info()) {
342 node->CollectDeoptInfo(last_entry);
344 node->IncrementSelfTicks();
345 if (src_line != v8::CpuProfileNode::kNoLineNumberInfo) {
346 node->IncrementLineTicks(src_line);
353 NodesPair(ProfileNode* src, ProfileNode* dst)
354 : src(src), dst(dst) { }
362 explicit Position(ProfileNode* node)
363 : node(node), child_idx_(0) { }
364 INLINE(ProfileNode* current_child()) {
365 return node->children()->at(child_idx_);
367 INLINE(bool has_current_child()) {
368 return child_idx_ < node->children()->length();
370 INLINE(void next_child()) { ++child_idx_; }
378 // Non-recursive implementation of a depth-first post-order tree traversal.
379 template <typename Callback>
380 void ProfileTree::TraverseDepthFirst(Callback* callback) {
381 List<Position> stack(10);
382 stack.Add(Position(root_));
383 while (stack.length() > 0) {
384 Position& current = stack.last();
385 if (current.has_current_child()) {
386 callback->BeforeTraversingChild(current.node, current.current_child());
387 stack.Add(Position(current.current_child()));
389 callback->AfterAllChildrenTraversed(current.node);
390 if (stack.length() > 1) {
391 Position& parent = stack[stack.length() - 2];
392 callback->AfterChildTraversed(parent.node, current.node);
395 // Remove child from the stack.
402 CpuProfile::CpuProfile(const char* title, bool record_samples)
404 record_samples_(record_samples),
405 start_time_(base::TimeTicks::HighResolutionNow()) {
409 void CpuProfile::AddPath(base::TimeTicks timestamp,
410 const Vector<CodeEntry*>& path, int src_line) {
411 ProfileNode* top_frame_node = top_down_.AddPathFromEnd(path, src_line);
412 if (record_samples_) {
413 timestamps_.Add(timestamp);
414 samples_.Add(top_frame_node);
419 void CpuProfile::CalculateTotalTicksAndSamplingRate() {
420 end_time_ = base::TimeTicks::HighResolutionNow();
424 void CpuProfile::Print() {
425 base::OS::Print("[Top down]:\n");
430 CodeEntry* const CodeMap::kSharedFunctionCodeEntry = NULL;
431 const CodeMap::CodeTreeConfig::Key CodeMap::CodeTreeConfig::kNoKey = NULL;
434 void CodeMap::AddCode(Address addr, CodeEntry* entry, unsigned size) {
435 DeleteAllCoveredCode(addr, addr + size);
436 CodeTree::Locator locator;
437 tree_.Insert(addr, &locator);
438 locator.set_value(CodeEntryInfo(entry, size));
442 void CodeMap::DeleteAllCoveredCode(Address start, Address end) {
443 List<Address> to_delete;
444 Address addr = end - 1;
445 while (addr >= start) {
446 CodeTree::Locator locator;
447 if (!tree_.FindGreatestLessThan(addr, &locator)) break;
448 Address start2 = locator.key(), end2 = start2 + locator.value().size;
449 if (start2 < end && start < end2) to_delete.Add(start2);
452 for (int i = 0; i < to_delete.length(); ++i) tree_.Remove(to_delete[i]);
456 CodeEntry* CodeMap::FindEntry(Address addr, Address* start) {
457 CodeTree::Locator locator;
458 if (tree_.FindGreatestLessThan(addr, &locator)) {
459 // locator.key() <= addr. Need to check that addr is within entry.
460 const CodeEntryInfo& entry = locator.value();
461 if (addr < (locator.key() + entry.size)) {
463 *start = locator.key();
472 int CodeMap::GetSharedId(Address addr) {
473 CodeTree::Locator locator;
474 // For shared function entries, 'size' field is used to store their IDs.
475 if (tree_.Find(addr, &locator)) {
476 const CodeEntryInfo& entry = locator.value();
477 DCHECK(entry.entry == kSharedFunctionCodeEntry);
480 tree_.Insert(addr, &locator);
481 int id = next_shared_id_++;
482 locator.set_value(CodeEntryInfo(kSharedFunctionCodeEntry, id));
488 void CodeMap::MoveCode(Address from, Address to) {
489 if (from == to) return;
490 CodeTree::Locator locator;
491 if (!tree_.Find(from, &locator)) return;
492 CodeEntryInfo entry = locator.value();
494 AddCode(to, entry.entry, entry.size);
498 void CodeMap::CodeTreePrinter::Call(
499 const Address& key, const CodeMap::CodeEntryInfo& value) {
500 // For shared function entries, 'size' field is used to store their IDs.
501 if (value.entry == kSharedFunctionCodeEntry) {
502 base::OS::Print("%p SharedFunctionInfo %d\n", key, value.size);
504 base::OS::Print("%p %5d %s\n", key, value.size, value.entry->name());
509 void CodeMap::Print() {
510 CodeTreePrinter printer;
511 tree_.ForEach(&printer);
515 CpuProfilesCollection::CpuProfilesCollection(Heap* heap)
516 : function_and_resource_names_(heap),
517 current_profiles_semaphore_(1) {
521 static void DeleteCodeEntry(CodeEntry** entry_ptr) {
526 static void DeleteCpuProfile(CpuProfile** profile_ptr) {
531 CpuProfilesCollection::~CpuProfilesCollection() {
532 finished_profiles_.Iterate(DeleteCpuProfile);
533 current_profiles_.Iterate(DeleteCpuProfile);
534 code_entries_.Iterate(DeleteCodeEntry);
538 bool CpuProfilesCollection::StartProfiling(const char* title,
539 bool record_samples) {
540 current_profiles_semaphore_.Wait();
541 if (current_profiles_.length() >= kMaxSimultaneousProfiles) {
542 current_profiles_semaphore_.Signal();
545 for (int i = 0; i < current_profiles_.length(); ++i) {
546 if (strcmp(current_profiles_[i]->title(), title) == 0) {
547 // Ignore attempts to start profile with the same title...
548 current_profiles_semaphore_.Signal();
549 // ... though return true to force it collect a sample.
553 current_profiles_.Add(new CpuProfile(title, record_samples));
554 current_profiles_semaphore_.Signal();
559 CpuProfile* CpuProfilesCollection::StopProfiling(const char* title) {
560 const int title_len = StrLength(title);
561 CpuProfile* profile = NULL;
562 current_profiles_semaphore_.Wait();
563 for (int i = current_profiles_.length() - 1; i >= 0; --i) {
564 if (title_len == 0 || strcmp(current_profiles_[i]->title(), title) == 0) {
565 profile = current_profiles_.Remove(i);
569 current_profiles_semaphore_.Signal();
571 if (profile == NULL) return NULL;
572 profile->CalculateTotalTicksAndSamplingRate();
573 finished_profiles_.Add(profile);
578 bool CpuProfilesCollection::IsLastProfile(const char* title) {
579 // Called from VM thread, and only it can mutate the list,
580 // so no locking is needed here.
581 if (current_profiles_.length() != 1) return false;
582 return StrLength(title) == 0
583 || strcmp(current_profiles_[0]->title(), title) == 0;
587 void CpuProfilesCollection::RemoveProfile(CpuProfile* profile) {
588 // Called from VM thread for a completed profile.
589 for (int i = 0; i < finished_profiles_.length(); i++) {
590 if (profile == finished_profiles_[i]) {
591 finished_profiles_.Remove(i);
599 void CpuProfilesCollection::AddPathToCurrentProfiles(
600 base::TimeTicks timestamp, const Vector<CodeEntry*>& path, int src_line) {
601 // As starting / stopping profiles is rare relatively to this
602 // method, we don't bother minimizing the duration of lock holding,
603 // e.g. copying contents of the list to a local vector.
604 current_profiles_semaphore_.Wait();
605 for (int i = 0; i < current_profiles_.length(); ++i) {
606 current_profiles_[i]->AddPath(timestamp, path, src_line);
608 current_profiles_semaphore_.Signal();
612 CodeEntry* CpuProfilesCollection::NewCodeEntry(
613 Logger::LogEventsAndTags tag, const char* name, const char* name_prefix,
614 const char* resource_name, int line_number, int column_number,
615 JITLineInfoTable* line_info, Address instruction_start) {
616 CodeEntry* code_entry =
617 new CodeEntry(tag, name, name_prefix, resource_name, line_number,
618 column_number, line_info, instruction_start);
619 code_entries_.Add(code_entry);
624 const char* const ProfileGenerator::kProgramEntryName =
626 const char* const ProfileGenerator::kIdleEntryName =
628 const char* const ProfileGenerator::kGarbageCollectorEntryName =
629 "(garbage collector)";
630 const char* const ProfileGenerator::kUnresolvedFunctionName =
631 "(unresolved function)";
634 ProfileGenerator::ProfileGenerator(CpuProfilesCollection* profiles)
635 : profiles_(profiles),
637 profiles->NewCodeEntry(Logger::FUNCTION_TAG, kProgramEntryName)),
639 profiles->NewCodeEntry(Logger::FUNCTION_TAG, kIdleEntryName)),
641 profiles->NewCodeEntry(Logger::BUILTIN_TAG,
642 kGarbageCollectorEntryName)),
644 profiles->NewCodeEntry(Logger::FUNCTION_TAG,
645 kUnresolvedFunctionName)) {
649 void ProfileGenerator::RecordTickSample(const TickSample& sample) {
650 // Allocate space for stack frames + pc + function + vm-state.
651 ScopedVector<CodeEntry*> entries(sample.frames_count + 3);
652 // As actual number of decoded code entries may vary, initialize
653 // entries vector with NULL values.
654 CodeEntry** entry = entries.start();
655 memset(entry, 0, entries.length() * sizeof(*entry));
657 // The ProfileNode knows nothing about all versions of generated code for
658 // the same JS function. The line number information associated with
659 // the latest version of generated code is used to find a source line number
660 // for a JS function. Then, the detected source line is passed to
661 // ProfileNode to increase the tick count for this source line.
662 int src_line = v8::CpuProfileNode::kNoLineNumberInfo;
663 bool src_line_not_found = true;
665 if (sample.pc != NULL) {
666 if (sample.has_external_callback && sample.state == EXTERNAL &&
667 sample.top_frame_type == StackFrame::EXIT) {
668 // Don't use PC when in external callback code, as it can point
669 // inside callback's code, and we will erroneously report
670 // that a callback calls itself.
671 *entry++ = code_map_.FindEntry(sample.external_callback);
674 CodeEntry* pc_entry = code_map_.FindEntry(sample.pc, &start);
675 // If pc is in the function code before it set up stack frame or after the
676 // frame was destroyed SafeStackFrameIterator incorrectly thinks that
677 // ebp contains return address of the current function and skips caller's
678 // frame. Check for this case and just skip such samples.
680 List<OffsetRange>* ranges = pc_entry->no_frame_ranges();
682 static_cast<int>(sample.pc - pc_entry->instruction_start());
684 for (int i = 0; i < ranges->length(); i++) {
685 OffsetRange& range = ranges->at(i);
686 if (range.from <= pc_offset && pc_offset < range.to) {
691 src_line = pc_entry->GetSourceLine(pc_offset);
692 if (src_line == v8::CpuProfileNode::kNoLineNumberInfo) {
693 src_line = pc_entry->line_number();
695 src_line_not_found = false;
698 if (pc_entry->builtin_id() == Builtins::kFunctionCall ||
699 pc_entry->builtin_id() == Builtins::kFunctionApply) {
700 // When current function is FunctionCall or FunctionApply builtin the
701 // top frame is either frame of the calling JS function or internal
702 // frame. In the latter case we know the caller for sure but in the
703 // former case we don't so we simply replace the frame with
704 // 'unresolved' entry.
705 if (sample.top_frame_type == StackFrame::JAVA_SCRIPT) {
706 *entry++ = unresolved_entry_;
712 for (const Address* stack_pos = sample.stack,
713 *stack_end = stack_pos + sample.frames_count;
714 stack_pos != stack_end;
716 Address start = NULL;
717 *entry = code_map_.FindEntry(*stack_pos, &start);
719 // Skip unresolved frames (e.g. internal frame) and get source line of
720 // the first JS caller.
721 if (src_line_not_found && *entry) {
723 static_cast<int>(*stack_pos - (*entry)->instruction_start());
724 src_line = (*entry)->GetSourceLine(pc_offset);
725 if (src_line == v8::CpuProfileNode::kNoLineNumberInfo) {
726 src_line = (*entry)->line_number();
728 src_line_not_found = false;
735 if (FLAG_prof_browser_mode) {
736 bool no_symbolized_entries = true;
737 for (CodeEntry** e = entries.start(); e != entry; ++e) {
739 no_symbolized_entries = false;
743 // If no frames were symbolized, put the VM state entry in.
744 if (no_symbolized_entries) {
745 *entry++ = EntryForVMState(sample.state);
749 profiles_->AddPathToCurrentProfiles(sample.timestamp, entries, src_line);
753 CodeEntry* ProfileGenerator::EntryForVMState(StateTag tag) {
759 // DOM events handlers are reported as OTHER / EXTERNAL entries.
760 // To avoid confusing people, let's put all these entries into
764 return program_entry_;
767 default: return NULL;
771 } } // namespace v8::internal