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/global-handles.h"
12 #include "src/sampler.h"
13 #include "src/scopeinfo.h"
14 #include "src/unicode.h"
15 #include "src/zone-inl.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 const char* const CodeEntry::kEmptyNamePrefix = "";
136 const char* const CodeEntry::kEmptyResourceName = "";
137 const char* const CodeEntry::kEmptyBailoutReason = "";
140 CodeEntry::~CodeEntry() {
141 delete no_frame_ranges_;
146 uint32_t CodeEntry::GetCallUid() const {
147 uint32_t hash = ComputeIntegerHash(tag_, v8::internal::kZeroHashSeed);
148 if (shared_id_ != 0) {
149 hash ^= ComputeIntegerHash(static_cast<uint32_t>(shared_id_),
150 v8::internal::kZeroHashSeed);
152 hash ^= ComputeIntegerHash(
153 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(name_prefix_)),
154 v8::internal::kZeroHashSeed);
155 hash ^= ComputeIntegerHash(
156 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(name_)),
157 v8::internal::kZeroHashSeed);
158 hash ^= ComputeIntegerHash(
159 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(resource_name_)),
160 v8::internal::kZeroHashSeed);
161 hash ^= ComputeIntegerHash(line_number_, v8::internal::kZeroHashSeed);
167 bool CodeEntry::IsSameAs(CodeEntry* entry) const {
169 || (tag_ == entry->tag_
170 && shared_id_ == entry->shared_id_
172 || (name_prefix_ == entry->name_prefix_
173 && name_ == entry->name_
174 && resource_name_ == entry->resource_name_
175 && line_number_ == entry->line_number_)));
179 void CodeEntry::SetBuiltinId(Builtins::Name id) {
180 tag_ = Logger::BUILTIN_TAG;
185 int CodeEntry::GetSourceLine(int pc_offset) const {
186 if (line_info_ && !line_info_->Empty()) {
187 return line_info_->GetSourceLineNumber(pc_offset);
189 return v8::CpuProfileNode::kNoLineNumberInfo;
193 ProfileNode* ProfileNode::FindChild(CodeEntry* entry) {
194 HashMap::Entry* map_entry =
195 children_.Lookup(entry, CodeEntryHash(entry), false);
196 return map_entry != NULL ?
197 reinterpret_cast<ProfileNode*>(map_entry->value) : NULL;
201 ProfileNode* ProfileNode::FindOrAddChild(CodeEntry* entry) {
202 HashMap::Entry* map_entry =
203 children_.Lookup(entry, CodeEntryHash(entry), true);
204 if (map_entry->value == NULL) {
206 ProfileNode* new_node = new ProfileNode(tree_, entry);
207 map_entry->value = new_node;
208 children_list_.Add(new_node);
210 return reinterpret_cast<ProfileNode*>(map_entry->value);
214 void ProfileNode::IncrementLineTicks(int src_line) {
215 if (src_line == v8::CpuProfileNode::kNoLineNumberInfo) return;
216 // Increment a hit counter of a certain source line.
217 // Add a new source line if not found.
219 line_ticks_.Lookup(reinterpret_cast<void*>(src_line), src_line, true);
221 e->value = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(e->value) + 1);
225 bool ProfileNode::GetLineTicks(v8::CpuProfileNode::LineTick* entries,
226 unsigned int length) const {
227 if (entries == NULL || length == 0) return false;
229 unsigned line_count = line_ticks_.occupancy();
231 if (line_count == 0) return false;
232 if (length < line_count) return false;
234 v8::CpuProfileNode::LineTick* entry = entries;
236 for (HashMap::Entry* p = line_ticks_.Start();
238 p = line_ticks_.Next(p), entry++) {
240 static_cast<unsigned int>(reinterpret_cast<uintptr_t>(p->key));
242 static_cast<unsigned int>(reinterpret_cast<uintptr_t>(p->value));
249 void ProfileNode::Print(int indent) {
250 base::OS::Print("%5u %*s %s%s %d #%d %s", self_ticks_, indent, "",
251 entry_->name_prefix(), entry_->name(), entry_->script_id(),
252 id(), entry_->bailout_reason());
253 if (entry_->resource_name()[0] != '\0')
254 base::OS::Print(" %s:%d", entry_->resource_name(), entry_->line_number());
255 base::OS::Print("\n");
256 for (HashMap::Entry* p = children_.Start();
258 p = children_.Next(p)) {
259 reinterpret_cast<ProfileNode*>(p->value)->Print(indent + 2);
264 class DeleteNodesCallback {
266 void BeforeTraversingChild(ProfileNode*, ProfileNode*) { }
268 void AfterAllChildrenTraversed(ProfileNode* node) {
272 void AfterChildTraversed(ProfileNode*, ProfileNode*) { }
276 ProfileTree::ProfileTree()
277 : root_entry_(Logger::FUNCTION_TAG, "(root)"),
279 root_(new ProfileNode(this, &root_entry_)) {
283 ProfileTree::~ProfileTree() {
284 DeleteNodesCallback cb;
285 TraverseDepthFirst(&cb);
289 ProfileNode* ProfileTree::AddPathFromEnd(const Vector<CodeEntry*>& path,
291 ProfileNode* node = root_;
292 for (CodeEntry** entry = path.start() + path.length() - 1;
293 entry != path.start() - 1;
295 if (*entry != NULL) {
296 node = node->FindOrAddChild(*entry);
299 node->IncrementSelfTicks();
300 if (src_line != v8::CpuProfileNode::kNoLineNumberInfo) {
301 node->IncrementLineTicks(src_line);
307 void ProfileTree::AddPathFromStart(const Vector<CodeEntry*>& path,
309 ProfileNode* node = root_;
310 for (CodeEntry** entry = path.start();
311 entry != path.start() + path.length();
313 if (*entry != NULL) {
314 node = node->FindOrAddChild(*entry);
317 node->IncrementSelfTicks();
318 if (src_line != v8::CpuProfileNode::kNoLineNumberInfo) {
319 node->IncrementLineTicks(src_line);
325 NodesPair(ProfileNode* src, ProfileNode* dst)
326 : src(src), dst(dst) { }
334 explicit Position(ProfileNode* node)
335 : node(node), child_idx_(0) { }
336 INLINE(ProfileNode* current_child()) {
337 return node->children()->at(child_idx_);
339 INLINE(bool has_current_child()) {
340 return child_idx_ < node->children()->length();
342 INLINE(void next_child()) { ++child_idx_; }
350 // Non-recursive implementation of a depth-first post-order tree traversal.
351 template <typename Callback>
352 void ProfileTree::TraverseDepthFirst(Callback* callback) {
353 List<Position> stack(10);
354 stack.Add(Position(root_));
355 while (stack.length() > 0) {
356 Position& current = stack.last();
357 if (current.has_current_child()) {
358 callback->BeforeTraversingChild(current.node, current.current_child());
359 stack.Add(Position(current.current_child()));
361 callback->AfterAllChildrenTraversed(current.node);
362 if (stack.length() > 1) {
363 Position& parent = stack[stack.length() - 2];
364 callback->AfterChildTraversed(parent.node, current.node);
367 // Remove child from the stack.
374 CpuProfile::CpuProfile(const char* title, bool record_samples)
376 record_samples_(record_samples),
377 start_time_(base::TimeTicks::HighResolutionNow()) {
381 void CpuProfile::AddPath(base::TimeTicks timestamp,
382 const Vector<CodeEntry*>& path,
384 ProfileNode* top_frame_node = top_down_.AddPathFromEnd(path, src_line);
385 if (record_samples_) {
386 timestamps_.Add(timestamp);
387 samples_.Add(top_frame_node);
392 void CpuProfile::CalculateTotalTicksAndSamplingRate() {
393 end_time_ = base::TimeTicks::HighResolutionNow();
397 void CpuProfile::Print() {
398 base::OS::Print("[Top down]:\n");
403 CodeEntry* const CodeMap::kSharedFunctionCodeEntry = NULL;
404 const CodeMap::CodeTreeConfig::Key CodeMap::CodeTreeConfig::kNoKey = NULL;
407 void CodeMap::AddCode(Address addr, CodeEntry* entry, unsigned size) {
408 DeleteAllCoveredCode(addr, addr + size);
409 CodeTree::Locator locator;
410 tree_.Insert(addr, &locator);
411 locator.set_value(CodeEntryInfo(entry, size));
415 void CodeMap::DeleteAllCoveredCode(Address start, Address end) {
416 List<Address> to_delete;
417 Address addr = end - 1;
418 while (addr >= start) {
419 CodeTree::Locator locator;
420 if (!tree_.FindGreatestLessThan(addr, &locator)) break;
421 Address start2 = locator.key(), end2 = start2 + locator.value().size;
422 if (start2 < end && start < end2) to_delete.Add(start2);
425 for (int i = 0; i < to_delete.length(); ++i) tree_.Remove(to_delete[i]);
429 CodeEntry* CodeMap::FindEntry(Address addr, Address* start) {
430 CodeTree::Locator locator;
431 if (tree_.FindGreatestLessThan(addr, &locator)) {
432 // locator.key() <= addr. Need to check that addr is within entry.
433 const CodeEntryInfo& entry = locator.value();
434 if (addr < (locator.key() + entry.size)) {
436 *start = locator.key();
445 int CodeMap::GetSharedId(Address addr) {
446 CodeTree::Locator locator;
447 // For shared function entries, 'size' field is used to store their IDs.
448 if (tree_.Find(addr, &locator)) {
449 const CodeEntryInfo& entry = locator.value();
450 DCHECK(entry.entry == kSharedFunctionCodeEntry);
453 tree_.Insert(addr, &locator);
454 int id = next_shared_id_++;
455 locator.set_value(CodeEntryInfo(kSharedFunctionCodeEntry, id));
461 void CodeMap::MoveCode(Address from, Address to) {
462 if (from == to) return;
463 CodeTree::Locator locator;
464 if (!tree_.Find(from, &locator)) return;
465 CodeEntryInfo entry = locator.value();
467 AddCode(to, entry.entry, entry.size);
471 void CodeMap::CodeTreePrinter::Call(
472 const Address& key, const CodeMap::CodeEntryInfo& value) {
473 // For shared function entries, 'size' field is used to store their IDs.
474 if (value.entry == kSharedFunctionCodeEntry) {
475 base::OS::Print("%p SharedFunctionInfo %d\n", key, value.size);
477 base::OS::Print("%p %5d %s\n", key, value.size, value.entry->name());
482 void CodeMap::Print() {
483 CodeTreePrinter printer;
484 tree_.ForEach(&printer);
488 CpuProfilesCollection::CpuProfilesCollection(Heap* heap)
489 : function_and_resource_names_(heap),
490 current_profiles_semaphore_(1) {
494 static void DeleteCodeEntry(CodeEntry** entry_ptr) {
499 static void DeleteCpuProfile(CpuProfile** profile_ptr) {
504 CpuProfilesCollection::~CpuProfilesCollection() {
505 finished_profiles_.Iterate(DeleteCpuProfile);
506 current_profiles_.Iterate(DeleteCpuProfile);
507 code_entries_.Iterate(DeleteCodeEntry);
511 bool CpuProfilesCollection::StartProfiling(const char* title,
512 bool record_samples) {
513 current_profiles_semaphore_.Wait();
514 if (current_profiles_.length() >= kMaxSimultaneousProfiles) {
515 current_profiles_semaphore_.Signal();
518 for (int i = 0; i < current_profiles_.length(); ++i) {
519 if (strcmp(current_profiles_[i]->title(), title) == 0) {
520 // Ignore attempts to start profile with the same title...
521 current_profiles_semaphore_.Signal();
522 // ... though return true to force it collect a sample.
526 current_profiles_.Add(new CpuProfile(title, record_samples));
527 current_profiles_semaphore_.Signal();
532 CpuProfile* CpuProfilesCollection::StopProfiling(const char* title) {
533 const int title_len = StrLength(title);
534 CpuProfile* profile = NULL;
535 current_profiles_semaphore_.Wait();
536 for (int i = current_profiles_.length() - 1; i >= 0; --i) {
537 if (title_len == 0 || strcmp(current_profiles_[i]->title(), title) == 0) {
538 profile = current_profiles_.Remove(i);
542 current_profiles_semaphore_.Signal();
544 if (profile == NULL) return NULL;
545 profile->CalculateTotalTicksAndSamplingRate();
546 finished_profiles_.Add(profile);
551 bool CpuProfilesCollection::IsLastProfile(const char* title) {
552 // Called from VM thread, and only it can mutate the list,
553 // so no locking is needed here.
554 if (current_profiles_.length() != 1) return false;
555 return StrLength(title) == 0
556 || strcmp(current_profiles_[0]->title(), title) == 0;
560 void CpuProfilesCollection::RemoveProfile(CpuProfile* profile) {
561 // Called from VM thread for a completed profile.
562 for (int i = 0; i < finished_profiles_.length(); i++) {
563 if (profile == finished_profiles_[i]) {
564 finished_profiles_.Remove(i);
572 void CpuProfilesCollection::AddPathToCurrentProfiles(
573 base::TimeTicks timestamp,
574 const Vector<CodeEntry*>& path,
576 // As starting / stopping profiles is rare relatively to this
577 // method, we don't bother minimizing the duration of lock holding,
578 // e.g. copying contents of the list to a local vector.
579 current_profiles_semaphore_.Wait();
580 for (int i = 0; i < current_profiles_.length(); ++i) {
581 current_profiles_[i]->AddPath(timestamp, path, src_line);
583 current_profiles_semaphore_.Signal();
587 CodeEntry* CpuProfilesCollection::NewCodeEntry(
588 Logger::LogEventsAndTags tag,
590 const char* name_prefix,
591 const char* resource_name,
594 JITLineInfoTable* line_info) {
595 CodeEntry* code_entry = new CodeEntry(tag,
602 code_entries_.Add(code_entry);
607 const char* const ProfileGenerator::kProgramEntryName =
609 const char* const ProfileGenerator::kIdleEntryName =
611 const char* const ProfileGenerator::kGarbageCollectorEntryName =
612 "(garbage collector)";
613 const char* const ProfileGenerator::kUnresolvedFunctionName =
614 "(unresolved function)";
617 ProfileGenerator::ProfileGenerator(CpuProfilesCollection* profiles)
618 : profiles_(profiles),
620 profiles->NewCodeEntry(Logger::FUNCTION_TAG, kProgramEntryName)),
622 profiles->NewCodeEntry(Logger::FUNCTION_TAG, kIdleEntryName)),
624 profiles->NewCodeEntry(Logger::BUILTIN_TAG,
625 kGarbageCollectorEntryName)),
627 profiles->NewCodeEntry(Logger::FUNCTION_TAG,
628 kUnresolvedFunctionName)) {
632 void ProfileGenerator::RecordTickSample(const TickSample& sample) {
633 // Allocate space for stack frames + pc + function + vm-state.
634 ScopedVector<CodeEntry*> entries(sample.frames_count + 3);
635 // As actual number of decoded code entries may vary, initialize
636 // entries vector with NULL values.
637 CodeEntry** entry = entries.start();
638 memset(entry, 0, entries.length() * sizeof(*entry));
640 // The ProfileNode knows nothing about all versions of generated code for
641 // the same JS function. The line number information associated with
642 // the latest version of generated code is used to find a source line number
643 // for a JS function. Then, the detected source line is passed to
644 // ProfileNode to accumulate the samples.
645 int src_line = v8::CpuProfileNode::kNoLineNumberInfo;
647 if (sample.pc != NULL) {
648 if (sample.has_external_callback && sample.state == EXTERNAL &&
649 sample.top_frame_type == StackFrame::EXIT) {
650 // Don't use PC when in external callback code, as it can point
651 // inside callback's code, and we will erroneously report
652 // that a callback calls itself.
653 *entry++ = code_map_.FindEntry(sample.external_callback);
656 CodeEntry* pc_entry = code_map_.FindEntry(sample.pc, &start);
657 // If pc is in the function code before it set up stack frame or after the
658 // frame was destroyed SafeStackFrameIterator incorrectly thinks that
659 // ebp contains return address of the current function and skips caller's
660 // frame. Check for this case and just skip such samples.
662 List<OffsetRange>* ranges = pc_entry->no_frame_ranges();
663 Code* code = Code::cast(HeapObject::FromAddress(start));
664 int pc_offset = static_cast<int>(sample.pc - code->instruction_start());
665 src_line = pc_entry->GetSourceLine(pc_offset);
667 for (int i = 0; i < ranges->length(); i++) {
668 OffsetRange& range = ranges->at(i);
669 if (range.from <= pc_offset && pc_offset < range.to) {
676 if (pc_entry->builtin_id() == Builtins::kFunctionCall ||
677 pc_entry->builtin_id() == Builtins::kFunctionApply) {
678 // When current function is FunctionCall or FunctionApply builtin the
679 // top frame is either frame of the calling JS function or internal
680 // frame. In the latter case we know the caller for sure but in the
681 // former case we don't so we simply replace the frame with
682 // 'unresolved' entry.
683 if (sample.top_frame_type == StackFrame::JAVA_SCRIPT) {
684 *entry++ = unresolved_entry_;
690 bool src_line_not_found = src_line == v8::CpuProfileNode::kNoLineNumberInfo;
692 for (const Address* stack_pos = sample.stack,
693 *stack_end = stack_pos + sample.frames_count;
694 stack_pos != stack_end;
696 Address start = NULL;
697 *entry = code_map_.FindEntry(*stack_pos, &start);
699 // Skip unresolved frames (e.g. internal frame) and get source line of
701 if (src_line_not_found && *entry) {
702 Code* code = Code::cast(HeapObject::FromAddress(start));
704 static_cast<int>(*stack_pos - code->instruction_start());
705 src_line = (*entry)->GetSourceLine(pc_offset);
706 if (src_line == v8::CpuProfileNode::kNoLineNumberInfo) {
707 src_line = (*entry)->line_number();
709 src_line_not_found = false;
716 if (FLAG_prof_browser_mode) {
717 bool no_symbolized_entries = true;
718 for (CodeEntry** e = entries.start(); e != entry; ++e) {
720 no_symbolized_entries = false;
724 // If no frames were symbolized, put the VM state entry in.
725 if (no_symbolized_entries) {
726 *entry++ = EntryForVMState(sample.state);
730 profiles_->AddPathToCurrentProfiles(sample.timestamp, entries, src_line);
734 CodeEntry* ProfileGenerator::EntryForVMState(StateTag tag) {
740 // DOM events handlers are reported as OTHER / EXTERNAL entries.
741 // To avoid confusing people, let's put all these entries into
745 return program_entry_;
748 default: return NULL;
752 } } // namespace v8::internal