2012-02-10 12:36:05 +00:00
|
|
|
// Copyright 2012 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2010-03-15 14:11:19 +00:00
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/v8.h"
|
2011-05-03 08:23:58 +00:00
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/profile-generator-inl.h"
|
2011-05-03 08:23:58 +00:00
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/compiler.h"
|
|
|
|
#include "src/debug.h"
|
|
|
|
#include "src/global-handles.h"
|
2014-06-20 08:40:11 +00:00
|
|
|
#include "src/sampler.h"
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/scopeinfo.h"
|
|
|
|
#include "src/unicode.h"
|
|
|
|
#include "src/zone-inl.h"
|
2010-03-15 14:11:19 +00:00
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
2010-04-06 10:36:38 +00:00
|
|
|
|
2013-10-18 08:56:14 +00:00
|
|
|
bool StringsStorage::StringsMatch(void* key1, void* key2) {
|
|
|
|
return strcmp(reinterpret_cast<char*>(key1),
|
|
|
|
reinterpret_cast<char*>(key2)) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-11 07:14:41 +00:00
|
|
|
StringsStorage::StringsStorage(Heap* heap)
|
|
|
|
: hash_seed_(heap->HashSeed()), names_(StringsMatch) {
|
2010-06-09 07:00:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
StringsStorage::~StringsStorage() {
|
|
|
|
for (HashMap::Entry* p = names_.Start();
|
|
|
|
p != NULL;
|
|
|
|
p = names_.Next(p)) {
|
|
|
|
DeleteArray(reinterpret_cast<const char*>(p->value));
|
|
|
|
}
|
2011-03-10 12:05:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char* StringsStorage::GetCopy(const char* src) {
|
2011-03-10 12:58:57 +00:00
|
|
|
int len = static_cast<int>(strlen(src));
|
2013-10-18 08:56:14 +00:00
|
|
|
HashMap::Entry* entry = GetEntry(src, len);
|
|
|
|
if (entry->value == NULL) {
|
|
|
|
Vector<char> dst = Vector<char>::New(len + 1);
|
2014-06-13 16:43:27 +00:00
|
|
|
StrNCpy(dst, src, len);
|
2013-10-18 08:56:14 +00:00
|
|
|
dst[len] = '\0';
|
|
|
|
entry->key = dst.start();
|
|
|
|
entry->value = entry->key;
|
|
|
|
}
|
|
|
|
return reinterpret_cast<const char*>(entry->value);
|
2011-03-10 12:05:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char* StringsStorage::GetFormatted(const char* format, ...) {
|
|
|
|
va_list args;
|
|
|
|
va_start(args, format);
|
|
|
|
const char* result = GetVFormatted(format, args);
|
|
|
|
va_end(args);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-18 08:56:14 +00:00
|
|
|
const char* StringsStorage::AddOrDisposeString(char* str, int len) {
|
|
|
|
HashMap::Entry* entry = GetEntry(str, len);
|
|
|
|
if (entry->value == NULL) {
|
2011-03-10 12:05:31 +00:00
|
|
|
// New entry added.
|
2013-10-18 08:56:14 +00:00
|
|
|
entry->key = str;
|
|
|
|
entry->value = str;
|
2011-03-10 12:05:31 +00:00
|
|
|
} else {
|
|
|
|
DeleteArray(str);
|
|
|
|
}
|
2013-10-18 08:56:14 +00:00
|
|
|
return reinterpret_cast<const char*>(entry->value);
|
2011-03-10 12:05:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char* StringsStorage::GetVFormatted(const char* format, va_list args) {
|
|
|
|
Vector<char> str = Vector<char>::New(1024);
|
2014-06-13 16:43:27 +00:00
|
|
|
int len = VSNPrintF(str, format, args);
|
2011-03-10 12:05:31 +00:00
|
|
|
if (len == -1) {
|
|
|
|
DeleteArray(str.start());
|
2013-10-18 08:56:14 +00:00
|
|
|
return GetCopy(format);
|
2011-03-10 12:05:31 +00:00
|
|
|
}
|
2013-10-18 08:56:14 +00:00
|
|
|
return AddOrDisposeString(str.start(), len);
|
2010-06-09 07:00:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-03-04 15:00:57 +00:00
|
|
|
const char* StringsStorage::GetName(Name* name) {
|
2010-06-09 07:00:33 +00:00
|
|
|
if (name->IsString()) {
|
2013-03-04 15:00:57 +00:00
|
|
|
String* str = String::cast(name);
|
|
|
|
int length = Min(kMaxNameSize, str->length());
|
2013-10-18 08:56:14 +00:00
|
|
|
int actual_length = 0;
|
2011-11-09 12:15:35 +00:00
|
|
|
SmartArrayPointer<char> data =
|
2013-10-18 08:56:14 +00:00
|
|
|
str->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL, 0, length,
|
|
|
|
&actual_length);
|
|
|
|
return AddOrDisposeString(data.Detach(), actual_length);
|
2013-03-04 15:00:57 +00:00
|
|
|
} else if (name->IsSymbol()) {
|
|
|
|
return "<symbol>";
|
2010-06-09 07:00:33 +00:00
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-10-18 09:15:38 +00:00
|
|
|
const char* StringsStorage::GetName(int index) {
|
2011-03-10 12:05:31 +00:00
|
|
|
return GetFormatted("%d", index);
|
2010-10-18 09:15:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-18 08:56:14 +00:00
|
|
|
const char* StringsStorage::GetFunctionName(Name* name) {
|
2014-08-05 07:08:39 +00:00
|
|
|
return GetName(name);
|
2013-10-18 08:56:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
const char* StringsStorage::GetFunctionName(const char* name) {
|
2014-08-05 07:08:39 +00:00
|
|
|
return GetCopy(name);
|
2013-10-18 08:56:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-06-13 11:02:24 +00:00
|
|
|
size_t StringsStorage::GetUsedMemorySize() const {
|
|
|
|
size_t size = sizeof(*this);
|
|
|
|
size += sizeof(HashMap::Entry) * names_.capacity();
|
|
|
|
for (HashMap::Entry* p = names_.Start(); p != NULL; p = names_.Next(p)) {
|
|
|
|
size += strlen(reinterpret_cast<const char*>(p->value)) + 1;
|
|
|
|
}
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2013-06-26 16:04:25 +00:00
|
|
|
|
2013-10-18 08:56:14 +00:00
|
|
|
HashMap::Entry* StringsStorage::GetEntry(const char* str, int len) {
|
|
|
|
uint32_t hash = StringHasher::HashSequentialString(str, len, hash_seed_);
|
|
|
|
return names_.Lookup(const_cast<char*>(str), hash, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
const char* const CodeEntry::kEmptyNamePrefix = "";
|
2013-06-26 16:04:25 +00:00
|
|
|
const char* const CodeEntry::kEmptyResourceName = "";
|
2013-09-05 13:20:51 +00:00
|
|
|
const char* const CodeEntry::kEmptyBailoutReason = "";
|
2010-04-06 10:36:38 +00:00
|
|
|
|
|
|
|
|
2013-05-14 22:51:33 +00:00
|
|
|
CodeEntry::~CodeEntry() {
|
|
|
|
delete no_frame_ranges_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-20 09:29:12 +00:00
|
|
|
uint32_t CodeEntry::GetCallUid() const {
|
2012-01-10 12:58:41 +00:00
|
|
|
uint32_t hash = ComputeIntegerHash(tag_, v8::internal::kZeroHashSeed);
|
2011-02-22 16:31:24 +00:00
|
|
|
if (shared_id_ != 0) {
|
2012-01-10 12:58:41 +00:00
|
|
|
hash ^= ComputeIntegerHash(static_cast<uint32_t>(shared_id_),
|
|
|
|
v8::internal::kZeroHashSeed);
|
2011-02-22 16:31:24 +00:00
|
|
|
} else {
|
|
|
|
hash ^= ComputeIntegerHash(
|
2012-01-10 12:58:41 +00:00
|
|
|
static_cast<uint32_t>(reinterpret_cast<uintptr_t>(name_prefix_)),
|
|
|
|
v8::internal::kZeroHashSeed);
|
2011-02-22 16:31:24 +00:00
|
|
|
hash ^= ComputeIntegerHash(
|
2012-01-10 12:58:41 +00:00
|
|
|
static_cast<uint32_t>(reinterpret_cast<uintptr_t>(name_)),
|
|
|
|
v8::internal::kZeroHashSeed);
|
2011-02-22 16:31:24 +00:00
|
|
|
hash ^= ComputeIntegerHash(
|
2012-01-10 12:58:41 +00:00
|
|
|
static_cast<uint32_t>(reinterpret_cast<uintptr_t>(resource_name_)),
|
|
|
|
v8::internal::kZeroHashSeed);
|
|
|
|
hash ^= ComputeIntegerHash(line_number_, v8::internal::kZeroHashSeed);
|
2011-02-22 16:31:24 +00:00
|
|
|
}
|
2010-09-20 09:29:12 +00:00
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool CodeEntry::IsSameAs(CodeEntry* entry) const {
|
|
|
|
return this == entry
|
|
|
|
|| (tag_ == entry->tag_
|
2011-02-22 16:31:24 +00:00
|
|
|
&& shared_id_ == entry->shared_id_
|
|
|
|
&& (shared_id_ != 0
|
|
|
|
|| (name_prefix_ == entry->name_prefix_
|
|
|
|
&& name_ == entry->name_
|
|
|
|
&& resource_name_ == entry->resource_name_
|
|
|
|
&& line_number_ == entry->line_number_)));
|
2010-09-20 09:29:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-02 07:51:09 +00:00
|
|
|
void CodeEntry::SetBuiltinId(Builtins::Name id) {
|
|
|
|
tag_ = Logger::BUILTIN_TAG;
|
|
|
|
builtin_id_ = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-15 14:11:19 +00:00
|
|
|
ProfileNode* ProfileNode::FindChild(CodeEntry* entry) {
|
|
|
|
HashMap::Entry* map_entry =
|
|
|
|
children_.Lookup(entry, CodeEntryHash(entry), false);
|
|
|
|
return map_entry != NULL ?
|
|
|
|
reinterpret_cast<ProfileNode*>(map_entry->value) : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ProfileNode* ProfileNode::FindOrAddChild(CodeEntry* entry) {
|
|
|
|
HashMap::Entry* map_entry =
|
|
|
|
children_.Lookup(entry, CodeEntryHash(entry), true);
|
|
|
|
if (map_entry->value == NULL) {
|
|
|
|
// New node added.
|
2010-04-15 11:37:29 +00:00
|
|
|
ProfileNode* new_node = new ProfileNode(tree_, entry);
|
2010-03-30 11:38:39 +00:00
|
|
|
map_entry->value = new_node;
|
|
|
|
children_list_.Add(new_node);
|
2010-03-15 14:11:19 +00:00
|
|
|
}
|
|
|
|
return reinterpret_cast<ProfileNode*>(map_entry->value);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ProfileNode::Print(int indent) {
|
2014-06-30 13:25:46 +00:00
|
|
|
base::OS::Print("%5u %*s %s%s %d #%d %s", self_ticks_, indent, "",
|
|
|
|
entry_->name_prefix(), entry_->name(), entry_->script_id(),
|
|
|
|
id(), entry_->bailout_reason());
|
2010-04-06 14:54:20 +00:00
|
|
|
if (entry_->resource_name()[0] != '\0')
|
2014-06-30 13:25:46 +00:00
|
|
|
base::OS::Print(" %s:%d", entry_->resource_name(), entry_->line_number());
|
|
|
|
base::OS::Print("\n");
|
2010-03-15 14:11:19 +00:00
|
|
|
for (HashMap::Entry* p = children_.Start();
|
|
|
|
p != NULL;
|
|
|
|
p = children_.Next(p)) {
|
|
|
|
reinterpret_cast<ProfileNode*>(p->value)->Print(indent + 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class DeleteNodesCallback {
|
|
|
|
public:
|
2010-05-18 14:19:33 +00:00
|
|
|
void BeforeTraversingChild(ProfileNode*, ProfileNode*) { }
|
|
|
|
|
2010-03-15 14:11:19 +00:00
|
|
|
void AfterAllChildrenTraversed(ProfileNode* node) {
|
|
|
|
delete node;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AfterChildTraversed(ProfileNode*, ProfileNode*) { }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-04-06 14:54:20 +00:00
|
|
|
ProfileTree::ProfileTree()
|
2013-07-01 10:12:03 +00:00
|
|
|
: root_entry_(Logger::FUNCTION_TAG, "(root)"),
|
2013-04-02 07:48:25 +00:00
|
|
|
next_node_id_(1),
|
2010-04-15 11:37:29 +00:00
|
|
|
root_(new ProfileNode(this, &root_entry_)) {
|
2010-04-06 14:54:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-15 14:11:19 +00:00
|
|
|
ProfileTree::~ProfileTree() {
|
|
|
|
DeleteNodesCallback cb;
|
2010-05-18 14:19:33 +00:00
|
|
|
TraverseDepthFirst(&cb);
|
2010-03-15 14:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-02 07:48:25 +00:00
|
|
|
ProfileNode* ProfileTree::AddPathFromEnd(const Vector<CodeEntry*>& path) {
|
2010-03-15 14:11:19 +00:00
|
|
|
ProfileNode* node = root_;
|
|
|
|
for (CodeEntry** entry = path.start() + path.length() - 1;
|
|
|
|
entry != path.start() - 1;
|
|
|
|
--entry) {
|
|
|
|
if (*entry != NULL) {
|
|
|
|
node = node->FindOrAddChild(*entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
node->IncrementSelfTicks();
|
2013-04-02 07:48:25 +00:00
|
|
|
return node;
|
2010-03-15 14:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void ProfileTree::AddPathFromStart(const Vector<CodeEntry*>& path) {
|
|
|
|
ProfileNode* node = root_;
|
|
|
|
for (CodeEntry** entry = path.start();
|
|
|
|
entry != path.start() + path.length();
|
|
|
|
++entry) {
|
|
|
|
if (*entry != NULL) {
|
|
|
|
node = node->FindOrAddChild(*entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
node->IncrementSelfTicks();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-18 14:19:33 +00:00
|
|
|
struct NodesPair {
|
|
|
|
NodesPair(ProfileNode* src, ProfileNode* dst)
|
|
|
|
: src(src), dst(dst) { }
|
|
|
|
ProfileNode* src;
|
|
|
|
ProfileNode* dst;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-03-30 11:38:39 +00:00
|
|
|
class Position {
|
|
|
|
public:
|
|
|
|
explicit Position(ProfileNode* node)
|
|
|
|
: node(node), child_idx_(0) { }
|
2010-03-15 14:11:19 +00:00
|
|
|
INLINE(ProfileNode* current_child()) {
|
2010-03-30 11:38:39 +00:00
|
|
|
return node->children()->at(child_idx_);
|
2010-03-15 14:11:19 +00:00
|
|
|
}
|
2010-03-30 11:38:39 +00:00
|
|
|
INLINE(bool has_current_child()) {
|
|
|
|
return child_idx_ < node->children()->length();
|
|
|
|
}
|
|
|
|
INLINE(void next_child()) { ++child_idx_; }
|
|
|
|
|
2010-03-15 14:11:19 +00:00
|
|
|
ProfileNode* node;
|
2010-03-30 11:38:39 +00:00
|
|
|
private:
|
|
|
|
int child_idx_;
|
2010-03-15 14:11:19 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2010-04-15 11:37:29 +00:00
|
|
|
// Non-recursive implementation of a depth-first post-order tree traversal.
|
2010-03-15 14:11:19 +00:00
|
|
|
template <typename Callback>
|
2010-05-18 14:19:33 +00:00
|
|
|
void ProfileTree::TraverseDepthFirst(Callback* callback) {
|
2010-03-15 14:11:19 +00:00
|
|
|
List<Position> stack(10);
|
2010-03-30 11:38:39 +00:00
|
|
|
stack.Add(Position(root_));
|
2010-05-18 14:19:33 +00:00
|
|
|
while (stack.length() > 0) {
|
2010-03-15 14:11:19 +00:00
|
|
|
Position& current = stack.last();
|
2010-03-30 11:38:39 +00:00
|
|
|
if (current.has_current_child()) {
|
2010-05-18 14:19:33 +00:00
|
|
|
callback->BeforeTraversingChild(current.node, current.current_child());
|
2010-03-30 11:38:39 +00:00
|
|
|
stack.Add(Position(current.current_child()));
|
2010-03-15 14:11:19 +00:00
|
|
|
} else {
|
|
|
|
callback->AfterAllChildrenTraversed(current.node);
|
|
|
|
if (stack.length() > 1) {
|
|
|
|
Position& parent = stack[stack.length() - 2];
|
|
|
|
callback->AfterChildTraversed(parent.node, current.node);
|
2010-03-30 11:38:39 +00:00
|
|
|
parent.next_child();
|
2010-03-15 14:11:19 +00:00
|
|
|
}
|
2010-05-18 14:19:33 +00:00
|
|
|
// Remove child from the stack.
|
|
|
|
stack.RemoveLast();
|
2010-03-15 14:11:19 +00:00
|
|
|
}
|
2010-05-18 14:19:33 +00:00
|
|
|
}
|
2010-03-15 14:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-18 08:59:09 +00:00
|
|
|
CpuProfile::CpuProfile(const char* title, bool record_samples)
|
2013-07-30 07:01:16 +00:00
|
|
|
: title_(title),
|
|
|
|
record_samples_(record_samples),
|
2014-06-30 13:25:46 +00:00
|
|
|
start_time_(base::TimeTicks::HighResolutionNow()) {
|
2013-07-30 07:01:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-06-30 13:25:46 +00:00
|
|
|
void CpuProfile::AddPath(base::TimeTicks timestamp,
|
|
|
|
const Vector<CodeEntry*>& path) {
|
2013-04-02 07:48:25 +00:00
|
|
|
ProfileNode* top_frame_node = top_down_.AddPathFromEnd(path);
|
2014-04-25 18:53:06 +00:00
|
|
|
if (record_samples_) {
|
|
|
|
timestamps_.Add(timestamp);
|
|
|
|
samples_.Add(top_frame_node);
|
|
|
|
}
|
2010-03-15 14:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-30 07:01:16 +00:00
|
|
|
void CpuProfile::CalculateTotalTicksAndSamplingRate() {
|
2014-06-30 13:25:46 +00:00
|
|
|
end_time_ = base::TimeTicks::HighResolutionNow();
|
2010-03-15 14:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CpuProfile::Print() {
|
2014-06-30 13:25:46 +00:00
|
|
|
base::OS::Print("[Top down]:\n");
|
2010-03-15 14:11:19 +00:00
|
|
|
top_down_.Print();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-10 12:00:27 +00:00
|
|
|
CodeEntry* const CodeMap::kSharedFunctionCodeEntry = NULL;
|
2010-03-15 14:11:19 +00:00
|
|
|
const CodeMap::CodeTreeConfig::Key CodeMap::CodeTreeConfig::kNoKey = NULL;
|
|
|
|
|
|
|
|
|
2011-09-14 11:47:03 +00:00
|
|
|
void CodeMap::AddCode(Address addr, CodeEntry* entry, unsigned size) {
|
|
|
|
DeleteAllCoveredCode(addr, addr + size);
|
|
|
|
CodeTree::Locator locator;
|
|
|
|
tree_.Insert(addr, &locator);
|
|
|
|
locator.set_value(CodeEntryInfo(entry, size));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CodeMap::DeleteAllCoveredCode(Address start, Address end) {
|
|
|
|
List<Address> to_delete;
|
|
|
|
Address addr = end - 1;
|
|
|
|
while (addr >= start) {
|
|
|
|
CodeTree::Locator locator;
|
|
|
|
if (!tree_.FindGreatestLessThan(addr, &locator)) break;
|
|
|
|
Address start2 = locator.key(), end2 = start2 + locator.value().size;
|
|
|
|
if (start2 < end && start < end2) to_delete.Add(start2);
|
|
|
|
addr = start2 - 1;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < to_delete.length(); ++i) tree_.Remove(to_delete[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-14 22:51:33 +00:00
|
|
|
CodeEntry* CodeMap::FindEntry(Address addr, Address* start) {
|
2010-03-15 14:11:19 +00:00
|
|
|
CodeTree::Locator locator;
|
|
|
|
if (tree_.FindGreatestLessThan(addr, &locator)) {
|
|
|
|
// locator.key() <= addr. Need to check that addr is within entry.
|
|
|
|
const CodeEntryInfo& entry = locator.value();
|
2013-05-14 22:51:33 +00:00
|
|
|
if (addr < (locator.key() + entry.size)) {
|
|
|
|
if (start) {
|
|
|
|
*start = locator.key();
|
|
|
|
}
|
2010-03-15 14:11:19 +00:00
|
|
|
return entry.entry;
|
2013-05-14 22:51:33 +00:00
|
|
|
}
|
2010-03-15 14:11:19 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-10 12:00:27 +00:00
|
|
|
int CodeMap::GetSharedId(Address addr) {
|
2011-02-22 16:31:24 +00:00
|
|
|
CodeTree::Locator locator;
|
2011-03-10 12:00:27 +00:00
|
|
|
// For shared function entries, 'size' field is used to store their IDs.
|
2011-02-22 16:31:24 +00:00
|
|
|
if (tree_.Find(addr, &locator)) {
|
|
|
|
const CodeEntryInfo& entry = locator.value();
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(entry.entry == kSharedFunctionCodeEntry);
|
2011-02-22 16:31:24 +00:00
|
|
|
return entry.size;
|
|
|
|
} else {
|
|
|
|
tree_.Insert(addr, &locator);
|
2011-03-10 12:00:27 +00:00
|
|
|
int id = next_shared_id_++;
|
|
|
|
locator.set_value(CodeEntryInfo(kSharedFunctionCodeEntry, id));
|
|
|
|
return id;
|
2011-02-22 16:31:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-09-14 11:47:03 +00:00
|
|
|
void CodeMap::MoveCode(Address from, Address to) {
|
|
|
|
if (from == to) return;
|
|
|
|
CodeTree::Locator locator;
|
|
|
|
if (!tree_.Find(from, &locator)) return;
|
|
|
|
CodeEntryInfo entry = locator.value();
|
|
|
|
tree_.Remove(from);
|
|
|
|
AddCode(to, entry.entry, entry.size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-30 11:38:39 +00:00
|
|
|
void CodeMap::CodeTreePrinter::Call(
|
|
|
|
const Address& key, const CodeMap::CodeEntryInfo& value) {
|
2013-04-24 12:09:04 +00:00
|
|
|
// For shared function entries, 'size' field is used to store their IDs.
|
|
|
|
if (value.entry == kSharedFunctionCodeEntry) {
|
2014-06-30 13:25:46 +00:00
|
|
|
base::OS::Print("%p SharedFunctionInfo %d\n", key, value.size);
|
2013-04-24 12:09:04 +00:00
|
|
|
} else {
|
2014-06-30 13:25:46 +00:00
|
|
|
base::OS::Print("%p %5d %s\n", key, value.size, value.entry->name());
|
2013-04-24 12:09:04 +00:00
|
|
|
}
|
2010-03-30 11:38:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CodeMap::Print() {
|
|
|
|
CodeTreePrinter printer;
|
|
|
|
tree_.ForEach(&printer);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-09-11 07:14:41 +00:00
|
|
|
CpuProfilesCollection::CpuProfilesCollection(Heap* heap)
|
|
|
|
: function_and_resource_names_(heap),
|
|
|
|
current_profiles_semaphore_(1) {
|
2010-03-15 14:11:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-19 09:46:53 +00:00
|
|
|
static void DeleteCodeEntry(CodeEntry** entry_ptr) {
|
2010-03-15 14:11:19 +00:00
|
|
|
delete *entry_ptr;
|
|
|
|
}
|
|
|
|
|
2013-07-05 09:52:11 +00:00
|
|
|
|
2010-03-19 09:46:53 +00:00
|
|
|
static void DeleteCpuProfile(CpuProfile** profile_ptr) {
|
|
|
|
delete *profile_ptr;
|
|
|
|
}
|
|
|
|
|
2013-07-05 09:52:11 +00:00
|
|
|
|
2010-03-19 09:46:53 +00:00
|
|
|
CpuProfilesCollection::~CpuProfilesCollection() {
|
2013-07-06 09:12:09 +00:00
|
|
|
finished_profiles_.Iterate(DeleteCpuProfile);
|
2010-03-30 11:38:39 +00:00
|
|
|
current_profiles_.Iterate(DeleteCpuProfile);
|
2010-03-19 09:46:53 +00:00
|
|
|
code_entries_.Iterate(DeleteCodeEntry);
|
|
|
|
}
|
2010-03-15 14:11:19 +00:00
|
|
|
|
2010-03-19 09:46:53 +00:00
|
|
|
|
2013-12-18 08:59:09 +00:00
|
|
|
bool CpuProfilesCollection::StartProfiling(const char* title,
|
2013-04-02 07:48:25 +00:00
|
|
|
bool record_samples) {
|
2013-09-02 12:26:06 +00:00
|
|
|
current_profiles_semaphore_.Wait();
|
2010-08-31 14:16:01 +00:00
|
|
|
if (current_profiles_.length() >= kMaxSimultaneousProfiles) {
|
2013-09-02 12:26:06 +00:00
|
|
|
current_profiles_semaphore_.Signal();
|
2010-08-31 14:16:01 +00:00
|
|
|
return false;
|
|
|
|
}
|
2010-03-30 11:38:39 +00:00
|
|
|
for (int i = 0; i < current_profiles_.length(); ++i) {
|
|
|
|
if (strcmp(current_profiles_[i]->title(), title) == 0) {
|
2014-07-11 09:06:12 +00:00
|
|
|
// Ignore attempts to start profile with the same title...
|
2013-09-02 12:26:06 +00:00
|
|
|
current_profiles_semaphore_.Signal();
|
2014-07-11 09:06:12 +00:00
|
|
|
// ... though return true to force it collect a sample.
|
|
|
|
return true;
|
2010-03-30 11:38:39 +00:00
|
|
|
}
|
|
|
|
}
|
2013-12-18 08:59:09 +00:00
|
|
|
current_profiles_.Add(new CpuProfile(title, record_samples));
|
2013-09-02 12:26:06 +00:00
|
|
|
current_profiles_semaphore_.Signal();
|
2010-03-30 11:38:39 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-30 07:01:16 +00:00
|
|
|
CpuProfile* CpuProfilesCollection::StopProfiling(const char* title) {
|
2010-04-13 11:59:37 +00:00
|
|
|
const int title_len = StrLength(title);
|
2010-03-30 11:38:39 +00:00
|
|
|
CpuProfile* profile = NULL;
|
2013-09-02 12:26:06 +00:00
|
|
|
current_profiles_semaphore_.Wait();
|
2010-03-30 11:38:39 +00:00
|
|
|
for (int i = current_profiles_.length() - 1; i >= 0; --i) {
|
|
|
|
if (title_len == 0 || strcmp(current_profiles_[i]->title(), title) == 0) {
|
|
|
|
profile = current_profiles_.Remove(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2013-09-02 12:26:06 +00:00
|
|
|
current_profiles_semaphore_.Signal();
|
2010-03-30 11:38:39 +00:00
|
|
|
|
2013-07-06 09:12:09 +00:00
|
|
|
if (profile == NULL) return NULL;
|
2013-07-30 07:01:16 +00:00
|
|
|
profile->CalculateTotalTicksAndSamplingRate();
|
2013-07-06 09:12:09 +00:00
|
|
|
finished_profiles_.Add(profile);
|
|
|
|
return profile;
|
2011-03-22 16:10:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-08-10 12:06:42 +00:00
|
|
|
bool CpuProfilesCollection::IsLastProfile(const char* title) {
|
|
|
|
// Called from VM thread, and only it can mutate the list,
|
|
|
|
// so no locking is needed here.
|
|
|
|
if (current_profiles_.length() != 1) return false;
|
|
|
|
return StrLength(title) == 0
|
|
|
|
|| strcmp(current_profiles_[0]->title(), title) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-22 16:10:01 +00:00
|
|
|
void CpuProfilesCollection::RemoveProfile(CpuProfile* profile) {
|
|
|
|
// Called from VM thread for a completed profile.
|
2013-07-06 09:12:09 +00:00
|
|
|
for (int i = 0; i < finished_profiles_.length(); i++) {
|
2013-12-18 08:59:09 +00:00
|
|
|
if (profile == finished_profiles_[i]) {
|
2013-07-06 09:12:09 +00:00
|
|
|
finished_profiles_.Remove(i);
|
|
|
|
return;
|
2010-05-18 14:19:33 +00:00
|
|
|
}
|
|
|
|
}
|
2013-07-06 09:12:09 +00:00
|
|
|
UNREACHABLE();
|
2010-03-19 09:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-03-30 11:38:39 +00:00
|
|
|
void CpuProfilesCollection::AddPathToCurrentProfiles(
|
2014-06-30 13:25:46 +00:00
|
|
|
base::TimeTicks timestamp, const Vector<CodeEntry*>& path) {
|
2010-03-30 11:38:39 +00:00
|
|
|
// As starting / stopping profiles is rare relatively to this
|
|
|
|
// method, we don't bother minimizing the duration of lock holding,
|
|
|
|
// e.g. copying contents of the list to a local vector.
|
2013-09-02 12:26:06 +00:00
|
|
|
current_profiles_semaphore_.Wait();
|
2010-03-30 11:38:39 +00:00
|
|
|
for (int i = 0; i < current_profiles_.length(); ++i) {
|
2014-04-25 18:53:06 +00:00
|
|
|
current_profiles_[i]->AddPath(timestamp, path);
|
2010-03-30 11:38:39 +00:00
|
|
|
}
|
2013-09-02 12:26:06 +00:00
|
|
|
current_profiles_semaphore_.Signal();
|
2010-03-30 11:38:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-07-01 10:12:03 +00:00
|
|
|
CodeEntry* CpuProfilesCollection::NewCodeEntry(
|
|
|
|
Logger::LogEventsAndTags tag,
|
|
|
|
const char* name,
|
|
|
|
const char* name_prefix,
|
|
|
|
const char* resource_name,
|
2013-10-10 13:15:47 +00:00
|
|
|
int line_number,
|
|
|
|
int column_number) {
|
2013-07-01 10:12:03 +00:00
|
|
|
CodeEntry* code_entry = new CodeEntry(tag,
|
|
|
|
name,
|
|
|
|
name_prefix,
|
|
|
|
resource_name,
|
2013-10-10 13:15:47 +00:00
|
|
|
line_number,
|
|
|
|
column_number);
|
2013-07-01 10:12:03 +00:00
|
|
|
code_entries_.Add(code_entry);
|
|
|
|
return code_entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-18 20:35:07 +00:00
|
|
|
const char* const ProfileGenerator::kProgramEntryName =
|
|
|
|
"(program)";
|
2013-08-07 17:04:27 +00:00
|
|
|
const char* const ProfileGenerator::kIdleEntryName =
|
|
|
|
"(idle)";
|
2011-03-18 20:35:07 +00:00
|
|
|
const char* const ProfileGenerator::kGarbageCollectorEntryName =
|
|
|
|
"(garbage collector)";
|
2013-07-02 07:51:09 +00:00
|
|
|
const char* const ProfileGenerator::kUnresolvedFunctionName =
|
|
|
|
"(unresolved function)";
|
2010-04-07 14:18:26 +00:00
|
|
|
|
2010-04-06 14:54:20 +00:00
|
|
|
|
2010-03-19 09:46:53 +00:00
|
|
|
ProfileGenerator::ProfileGenerator(CpuProfilesCollection* profiles)
|
2010-04-06 14:54:20 +00:00
|
|
|
: profiles_(profiles),
|
|
|
|
program_entry_(
|
2010-04-07 14:18:26 +00:00
|
|
|
profiles->NewCodeEntry(Logger::FUNCTION_TAG, kProgramEntryName)),
|
2013-08-07 17:04:27 +00:00
|
|
|
idle_entry_(
|
|
|
|
profiles->NewCodeEntry(Logger::FUNCTION_TAG, kIdleEntryName)),
|
2010-04-07 14:18:26 +00:00
|
|
|
gc_entry_(
|
|
|
|
profiles->NewCodeEntry(Logger::BUILTIN_TAG,
|
2013-07-02 07:51:09 +00:00
|
|
|
kGarbageCollectorEntryName)),
|
|
|
|
unresolved_entry_(
|
|
|
|
profiles->NewCodeEntry(Logger::FUNCTION_TAG,
|
|
|
|
kUnresolvedFunctionName)) {
|
2010-03-15 14:11:19 +00:00
|
|
|
}
|
|
|
|
|
2010-03-19 09:46:53 +00:00
|
|
|
|
|
|
|
void ProfileGenerator::RecordTickSample(const TickSample& sample) {
|
2010-04-07 14:18:26 +00:00
|
|
|
// Allocate space for stack frames + pc + function + vm-state.
|
2010-04-06 14:54:20 +00:00
|
|
|
ScopedVector<CodeEntry*> entries(sample.frames_count + 3);
|
2010-04-07 14:18:26 +00:00
|
|
|
// As actual number of decoded code entries may vary, initialize
|
|
|
|
// entries vector with NULL values.
|
2010-03-19 09:46:53 +00:00
|
|
|
CodeEntry** entry = entries.start();
|
2010-04-07 14:18:26 +00:00
|
|
|
memset(entry, 0, entries.length() * sizeof(*entry));
|
|
|
|
if (sample.pc != NULL) {
|
2013-07-23 15:01:38 +00:00
|
|
|
if (sample.has_external_callback && sample.state == EXTERNAL &&
|
|
|
|
sample.top_frame_type == StackFrame::EXIT) {
|
2011-03-21 18:13:27 +00:00
|
|
|
// Don't use PC when in external callback code, as it can point
|
|
|
|
// inside callback's code, and we will erroneously report
|
|
|
|
// that a callback calls itself.
|
|
|
|
*entry++ = code_map_.FindEntry(sample.external_callback);
|
2013-07-02 07:51:09 +00:00
|
|
|
} else {
|
|
|
|
Address start;
|
|
|
|
CodeEntry* pc_entry = code_map_.FindEntry(sample.pc, &start);
|
|
|
|
// If pc is in the function code before it set up stack frame or after the
|
|
|
|
// frame was destroyed SafeStackFrameIterator incorrectly thinks that
|
|
|
|
// ebp contains return address of the current function and skips caller's
|
|
|
|
// frame. Check for this case and just skip such samples.
|
|
|
|
if (pc_entry) {
|
|
|
|
List<OffsetRange>* ranges = pc_entry->no_frame_ranges();
|
|
|
|
if (ranges) {
|
|
|
|
Code* code = Code::cast(HeapObject::FromAddress(start));
|
|
|
|
int pc_offset = static_cast<int>(
|
|
|
|
sample.pc - code->instruction_start());
|
|
|
|
for (int i = 0; i < ranges->length(); i++) {
|
|
|
|
OffsetRange& range = ranges->at(i);
|
|
|
|
if (range.from <= pc_offset && pc_offset < range.to) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*entry++ = pc_entry;
|
|
|
|
|
2013-07-03 14:04:37 +00:00
|
|
|
if (pc_entry->builtin_id() == Builtins::kFunctionCall ||
|
|
|
|
pc_entry->builtin_id() == Builtins::kFunctionApply) {
|
|
|
|
// When current function is FunctionCall or FunctionApply builtin the
|
|
|
|
// top frame is either frame of the calling JS function or internal
|
|
|
|
// frame. In the latter case we know the caller for sure but in the
|
|
|
|
// former case we don't so we simply replace the frame with
|
|
|
|
// 'unresolved' entry.
|
|
|
|
if (sample.top_frame_type == StackFrame::JAVA_SCRIPT) {
|
|
|
|
*entry++ = unresolved_entry_;
|
|
|
|
}
|
2013-07-02 07:51:09 +00:00
|
|
|
}
|
|
|
|
}
|
2010-03-19 09:46:53 +00:00
|
|
|
}
|
|
|
|
|
2012-01-13 13:09:52 +00:00
|
|
|
for (const Address* stack_pos = sample.stack,
|
2010-04-07 14:18:26 +00:00
|
|
|
*stack_end = stack_pos + sample.frames_count;
|
|
|
|
stack_pos != stack_end;
|
|
|
|
++stack_pos) {
|
|
|
|
*entry++ = code_map_.FindEntry(*stack_pos);
|
|
|
|
}
|
2010-03-19 09:46:53 +00:00
|
|
|
}
|
|
|
|
|
2010-04-07 14:18:26 +00:00
|
|
|
if (FLAG_prof_browser_mode) {
|
2010-04-15 14:12:17 +00:00
|
|
|
bool no_symbolized_entries = true;
|
|
|
|
for (CodeEntry** e = entries.start(); e != entry; ++e) {
|
|
|
|
if (*e != NULL) {
|
|
|
|
no_symbolized_entries = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If no frames were symbolized, put the VM state entry in.
|
|
|
|
if (no_symbolized_entries) {
|
|
|
|
*entry++ = EntryForVMState(sample.state);
|
|
|
|
}
|
2010-04-07 14:18:26 +00:00
|
|
|
}
|
2010-04-06 14:54:20 +00:00
|
|
|
|
2014-04-25 18:53:06 +00:00
|
|
|
profiles_->AddPathToCurrentProfiles(sample.timestamp, entries);
|
2010-03-19 09:46:53 +00:00
|
|
|
}
|
|
|
|
|
2010-06-11 16:34:59 +00:00
|
|
|
|
2013-10-14 08:57:46 +00:00
|
|
|
CodeEntry* ProfileGenerator::EntryForVMState(StateTag tag) {
|
|
|
|
switch (tag) {
|
|
|
|
case GC:
|
|
|
|
return gc_entry_;
|
|
|
|
case JS:
|
|
|
|
case COMPILER:
|
|
|
|
// DOM events handlers are reported as OTHER / EXTERNAL entries.
|
|
|
|
// To avoid confusing people, let's put all these entries into
|
|
|
|
// one bucket.
|
|
|
|
case OTHER:
|
|
|
|
case EXTERNAL:
|
|
|
|
return program_entry_;
|
|
|
|
case IDLE:
|
|
|
|
return idle_entry_;
|
|
|
|
default: return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-15 14:11:19 +00:00
|
|
|
} } // namespace v8::internal
|