blob: 19090a0deafa2d7416aa18762484c6875982c0b7 [file] [log] [blame]
ulan@chromium.org65a89c22012-02-14 11:46:07 +00001// Copyright 2012 the V8 project authors. All rights reserved.
fschneider@chromium.org086aac62010-03-17 13:18:24 +00002// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28#include "v8.h"
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +000029
30#include "profile-generator-inl.h"
31
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +000032#include "compiler.h"
machenbach@chromium.orgc1789ee2013-07-05 07:09:57 +000033#include "debug.h"
34#include "sampler.h"
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000035#include "global-handles.h"
ager@chromium.org2cc82ae2010-06-14 07:35:38 +000036#include "scopeinfo.h"
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +000037#include "unicode.h"
ager@chromium.org2cc82ae2010-06-14 07:35:38 +000038#include "zone-inl.h"
fschneider@chromium.org086aac62010-03-17 13:18:24 +000039
fschneider@chromium.org086aac62010-03-17 13:18:24 +000040namespace v8 {
41namespace internal {
42
43
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +000044StringsStorage::StringsStorage()
45 : names_(StringsMatch) {
46}
47
48
49StringsStorage::~StringsStorage() {
50 for (HashMap::Entry* p = names_.Start();
51 p != NULL;
52 p = names_.Next(p)) {
53 DeleteArray(reinterpret_cast<const char*>(p->value));
54 }
whesse@chromium.orgb08986c2011-03-14 16:13:42 +000055}
56
57
58const char* StringsStorage::GetCopy(const char* src) {
59 int len = static_cast<int>(strlen(src));
60 Vector<char> dst = Vector<char>::New(len + 1);
61 OS::StrNCpy(dst, src, len);
62 dst[len] = '\0';
rossberg@chromium.orgfab14982012-01-05 15:02:15 +000063 uint32_t hash =
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +000064 StringHasher::HashSequentialString(dst.start(), len, HEAP->HashSeed());
whesse@chromium.orgb08986c2011-03-14 16:13:42 +000065 return AddOrDisposeString(dst.start(), hash);
66}
67
68
69const char* StringsStorage::GetFormatted(const char* format, ...) {
70 va_list args;
71 va_start(args, format);
72 const char* result = GetVFormatted(format, args);
73 va_end(args);
74 return result;
75}
76
77
78const char* StringsStorage::AddOrDisposeString(char* str, uint32_t hash) {
79 HashMap::Entry* cache_entry = names_.Lookup(str, hash, true);
80 if (cache_entry->value == NULL) {
81 // New entry added.
82 cache_entry->value = str;
83 } else {
84 DeleteArray(str);
85 }
86 return reinterpret_cast<const char*>(cache_entry->value);
87}
88
89
90const char* StringsStorage::GetVFormatted(const char* format, va_list args) {
91 Vector<char> str = Vector<char>::New(1024);
92 int len = OS::VSNPrintF(str, format, args);
93 if (len == -1) {
94 DeleteArray(str.start());
95 return format;
96 }
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +000097 uint32_t hash = StringHasher::HashSequentialString(
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +000098 str.start(), len, HEAP->HashSeed());
whesse@chromium.orgb08986c2011-03-14 16:13:42 +000099 return AddOrDisposeString(str.start(), hash);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000100}
101
102
ulan@chromium.org750145a2013-03-07 15:14:13 +0000103const char* StringsStorage::GetName(Name* name) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000104 if (name->IsString()) {
ulan@chromium.org750145a2013-03-07 15:14:13 +0000105 String* str = String::cast(name);
106 int length = Min(kMaxNameSize, str->length());
danno@chromium.orgc612e022011-11-10 11:38:15 +0000107 SmartArrayPointer<char> data =
ulan@chromium.org750145a2013-03-07 15:14:13 +0000108 str->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL, 0, length);
yangguo@chromium.orga6bbcc82012-12-21 12:35:02 +0000109 uint32_t hash = StringHasher::HashSequentialString(
110 *data, length, name->GetHeap()->HashSeed());
danno@chromium.orgc612e022011-11-10 11:38:15 +0000111 return AddOrDisposeString(data.Detach(), hash);
ulan@chromium.org750145a2013-03-07 15:14:13 +0000112 } else if (name->IsSymbol()) {
113 return "<symbol>";
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000114 }
115 return "";
116}
117
118
vegorov@chromium.org42841962010-10-18 11:18:59 +0000119const char* StringsStorage::GetName(int index) {
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000120 return GetFormatted("%d", index);
vegorov@chromium.org42841962010-10-18 11:18:59 +0000121}
122
123
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000124size_t StringsStorage::GetUsedMemorySize() const {
125 size_t size = sizeof(*this);
126 size += sizeof(HashMap::Entry) * names_.capacity();
127 for (HashMap::Entry* p = names_.Start(); p != NULL; p = names_.Next(p)) {
128 size += strlen(reinterpret_cast<const char*>(p->value)) + 1;
129 }
130 return size;
131}
132
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000133
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000134const char* const CodeEntry::kEmptyNamePrefix = "";
mstarzinger@chromium.org1510d582013-06-28 14:00:48 +0000135const char* const CodeEntry::kEmptyResourceName = "";
lrn@chromium.org25156de2010-04-06 13:10:27 +0000136
137
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000138CodeEntry::~CodeEntry() {
139 delete no_frame_ranges_;
140}
141
142
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000143void CodeEntry::CopyData(const CodeEntry& source) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000144 tag_ = source.tag_;
145 name_prefix_ = source.name_prefix_;
146 name_ = source.name_;
147 resource_name_ = source.resource_name_;
148 line_number_ = source.line_number_;
149}
150
151
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000152uint32_t CodeEntry::GetCallUid() const {
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000153 uint32_t hash = ComputeIntegerHash(tag_, v8::internal::kZeroHashSeed);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000154 if (shared_id_ != 0) {
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000155 hash ^= ComputeIntegerHash(static_cast<uint32_t>(shared_id_),
156 v8::internal::kZeroHashSeed);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000157 } else {
158 hash ^= ComputeIntegerHash(
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000159 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(name_prefix_)),
160 v8::internal::kZeroHashSeed);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000161 hash ^= ComputeIntegerHash(
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000162 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(name_)),
163 v8::internal::kZeroHashSeed);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000164 hash ^= ComputeIntegerHash(
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000165 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(resource_name_)),
166 v8::internal::kZeroHashSeed);
167 hash ^= ComputeIntegerHash(line_number_, v8::internal::kZeroHashSeed);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000168 }
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000169 return hash;
170}
171
172
173bool CodeEntry::IsSameAs(CodeEntry* entry) const {
174 return this == entry
175 || (tag_ == entry->tag_
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000176 && shared_id_ == entry->shared_id_
177 && (shared_id_ != 0
178 || (name_prefix_ == entry->name_prefix_
179 && name_ == entry->name_
180 && resource_name_ == entry->resource_name_
181 && line_number_ == entry->line_number_)));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000182}
183
184
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000185void CodeEntry::SetBuiltinId(Builtins::Name id) {
186 tag_ = Logger::BUILTIN_TAG;
187 builtin_id_ = id;
188}
189
190
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000191ProfileNode* ProfileNode::FindChild(CodeEntry* entry) {
192 HashMap::Entry* map_entry =
193 children_.Lookup(entry, CodeEntryHash(entry), false);
194 return map_entry != NULL ?
195 reinterpret_cast<ProfileNode*>(map_entry->value) : NULL;
196}
197
198
199ProfileNode* ProfileNode::FindOrAddChild(CodeEntry* entry) {
200 HashMap::Entry* map_entry =
201 children_.Lookup(entry, CodeEntryHash(entry), true);
202 if (map_entry->value == NULL) {
203 // New node added.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000204 ProfileNode* new_node = new ProfileNode(tree_, entry);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000205 map_entry->value = new_node;
206 children_list_.Add(new_node);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000207 }
208 return reinterpret_cast<ProfileNode*>(map_entry->value);
209}
210
211
212void ProfileNode::Print(int indent) {
verwaest@chromium.org662436e2013-08-28 08:41:27 +0000213 OS::Print("%5u %*c %s%s %d #%d",
214 self_ticks_,
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000215 indent, ' ',
ager@chromium.org357bf652010-04-12 11:30:10 +0000216 entry_->name_prefix(),
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000217 entry_->name(),
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000218 entry_->script_id(),
ulan@chromium.org32d7dba2013-04-24 10:59:06 +0000219 id());
ager@chromium.org357bf652010-04-12 11:30:10 +0000220 if (entry_->resource_name()[0] != '\0')
221 OS::Print(" %s:%d", entry_->resource_name(), entry_->line_number());
222 OS::Print("\n");
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000223 for (HashMap::Entry* p = children_.Start();
224 p != NULL;
225 p = children_.Next(p)) {
226 reinterpret_cast<ProfileNode*>(p->value)->Print(indent + 2);
227 }
228}
229
230
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000231class DeleteNodesCallback {
232 public:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000233 void BeforeTraversingChild(ProfileNode*, ProfileNode*) { }
234
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000235 void AfterAllChildrenTraversed(ProfileNode* node) {
236 delete node;
237 }
238
239 void AfterChildTraversed(ProfileNode*, ProfileNode*) { }
240};
241
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000242
ager@chromium.org357bf652010-04-12 11:30:10 +0000243ProfileTree::ProfileTree()
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000244 : root_entry_(Logger::FUNCTION_TAG, "(root)"),
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000245 next_node_id_(1),
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000246 root_(new ProfileNode(this, &root_entry_)) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000247}
248
249
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000250ProfileTree::~ProfileTree() {
251 DeleteNodesCallback cb;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000252 TraverseDepthFirst(&cb);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000253}
254
255
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000256ProfileNode* ProfileTree::AddPathFromEnd(const Vector<CodeEntry*>& path) {
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000257 ProfileNode* node = root_;
258 for (CodeEntry** entry = path.start() + path.length() - 1;
259 entry != path.start() - 1;
260 --entry) {
261 if (*entry != NULL) {
262 node = node->FindOrAddChild(*entry);
263 }
264 }
265 node->IncrementSelfTicks();
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000266 return node;
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000267}
268
269
270void ProfileTree::AddPathFromStart(const Vector<CodeEntry*>& path) {
271 ProfileNode* node = root_;
272 for (CodeEntry** entry = path.start();
273 entry != path.start() + path.length();
274 ++entry) {
275 if (*entry != NULL) {
276 node = node->FindOrAddChild(*entry);
277 }
278 }
279 node->IncrementSelfTicks();
280}
281
282
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000283struct NodesPair {
284 NodesPair(ProfileNode* src, ProfileNode* dst)
285 : src(src), dst(dst) { }
286 ProfileNode* src;
287 ProfileNode* dst;
288};
289
290
lrn@chromium.org25156de2010-04-06 13:10:27 +0000291class Position {
292 public:
293 explicit Position(ProfileNode* node)
294 : node(node), child_idx_(0) { }
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000295 INLINE(ProfileNode* current_child()) {
lrn@chromium.org25156de2010-04-06 13:10:27 +0000296 return node->children()->at(child_idx_);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000297 }
lrn@chromium.org25156de2010-04-06 13:10:27 +0000298 INLINE(bool has_current_child()) {
299 return child_idx_ < node->children()->length();
300 }
301 INLINE(void next_child()) { ++child_idx_; }
302
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000303 ProfileNode* node;
lrn@chromium.org25156de2010-04-06 13:10:27 +0000304 private:
305 int child_idx_;
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000306};
307
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000308
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000309// Non-recursive implementation of a depth-first post-order tree traversal.
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000310template <typename Callback>
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000311void ProfileTree::TraverseDepthFirst(Callback* callback) {
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000312 List<Position> stack(10);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000313 stack.Add(Position(root_));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000314 while (stack.length() > 0) {
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000315 Position& current = stack.last();
lrn@chromium.org25156de2010-04-06 13:10:27 +0000316 if (current.has_current_child()) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000317 callback->BeforeTraversingChild(current.node, current.current_child());
lrn@chromium.org25156de2010-04-06 13:10:27 +0000318 stack.Add(Position(current.current_child()));
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000319 } else {
320 callback->AfterAllChildrenTraversed(current.node);
321 if (stack.length() > 1) {
322 Position& parent = stack[stack.length() - 2];
323 callback->AfterChildTraversed(parent.node, current.node);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000324 parent.next_child();
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000325 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000326 // Remove child from the stack.
327 stack.RemoveLast();
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000328 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000329 }
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000330}
331
332
danno@chromium.orgd3c42102013-08-01 16:58:23 +0000333CpuProfile::CpuProfile(const char* title, unsigned uid, bool record_samples)
334 : title_(title),
335 uid_(uid),
336 record_samples_(record_samples),
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +0000337 start_time_(Time::NowFromSystemTime()) {
338 timer_.Start();
danno@chromium.orgd3c42102013-08-01 16:58:23 +0000339}
340
341
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000342void CpuProfile::AddPath(const Vector<CodeEntry*>& path) {
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000343 ProfileNode* top_frame_node = top_down_.AddPathFromEnd(path);
344 if (record_samples_) samples_.Add(top_frame_node);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000345}
346
347
danno@chromium.orgd3c42102013-08-01 16:58:23 +0000348void CpuProfile::CalculateTotalTicksAndSamplingRate() {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +0000349 end_time_ = start_time_ + timer_.Elapsed();
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000350}
351
352
353void CpuProfile::Print() {
354 OS::Print("[Top down]:\n");
355 top_down_.Print();
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000356}
357
358
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000359CodeEntry* const CodeMap::kSharedFunctionCodeEntry = NULL;
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000360const CodeMap::CodeTreeConfig::Key CodeMap::CodeTreeConfig::kNoKey = NULL;
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000361
362
lrn@chromium.org34e60782011-09-15 07:25:40 +0000363void CodeMap::AddCode(Address addr, CodeEntry* entry, unsigned size) {
364 DeleteAllCoveredCode(addr, addr + size);
365 CodeTree::Locator locator;
366 tree_.Insert(addr, &locator);
367 locator.set_value(CodeEntryInfo(entry, size));
368}
369
370
371void CodeMap::DeleteAllCoveredCode(Address start, Address end) {
372 List<Address> to_delete;
373 Address addr = end - 1;
374 while (addr >= start) {
375 CodeTree::Locator locator;
376 if (!tree_.FindGreatestLessThan(addr, &locator)) break;
377 Address start2 = locator.key(), end2 = start2 + locator.value().size;
378 if (start2 < end && start < end2) to_delete.Add(start2);
379 addr = start2 - 1;
380 }
381 for (int i = 0; i < to_delete.length(); ++i) tree_.Remove(to_delete[i]);
382}
383
384
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000385CodeEntry* CodeMap::FindEntry(Address addr, Address* start) {
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000386 CodeTree::Locator locator;
387 if (tree_.FindGreatestLessThan(addr, &locator)) {
388 // locator.key() <= addr. Need to check that addr is within entry.
389 const CodeEntryInfo& entry = locator.value();
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000390 if (addr < (locator.key() + entry.size)) {
391 if (start) {
392 *start = locator.key();
393 }
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000394 return entry.entry;
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000395 }
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000396 }
397 return NULL;
398}
399
400
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000401int CodeMap::GetSharedId(Address addr) {
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000402 CodeTree::Locator locator;
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000403 // For shared function entries, 'size' field is used to store their IDs.
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000404 if (tree_.Find(addr, &locator)) {
405 const CodeEntryInfo& entry = locator.value();
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000406 ASSERT(entry.entry == kSharedFunctionCodeEntry);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000407 return entry.size;
408 } else {
409 tree_.Insert(addr, &locator);
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000410 int id = next_shared_id_++;
411 locator.set_value(CodeEntryInfo(kSharedFunctionCodeEntry, id));
412 return id;
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000413 }
414}
415
416
lrn@chromium.org34e60782011-09-15 07:25:40 +0000417void CodeMap::MoveCode(Address from, Address to) {
418 if (from == to) return;
419 CodeTree::Locator locator;
420 if (!tree_.Find(from, &locator)) return;
421 CodeEntryInfo entry = locator.value();
422 tree_.Remove(from);
423 AddCode(to, entry.entry, entry.size);
424}
425
426
lrn@chromium.org25156de2010-04-06 13:10:27 +0000427void CodeMap::CodeTreePrinter::Call(
428 const Address& key, const CodeMap::CodeEntryInfo& value) {
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000429 // For shared function entries, 'size' field is used to store their IDs.
430 if (value.entry == kSharedFunctionCodeEntry) {
431 OS::Print("%p SharedFunctionInfo %d\n", key, value.size);
432 } else {
433 OS::Print("%p %5d %s\n", key, value.size, value.entry->name());
434 }
lrn@chromium.org25156de2010-04-06 13:10:27 +0000435}
436
437
438void CodeMap::Print() {
439 CodeTreePrinter printer;
440 tree_.ForEach(&printer);
441}
442
443
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000444CpuProfilesCollection::CpuProfilesCollection()
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000445 : current_profiles_semaphore_(1) {
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000446}
447
448
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000449static void DeleteCodeEntry(CodeEntry** entry_ptr) {
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000450 delete *entry_ptr;
451}
452
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +0000453
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000454static void DeleteCpuProfile(CpuProfile** profile_ptr) {
455 delete *profile_ptr;
456}
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000457
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000458
459CpuProfilesCollection::~CpuProfilesCollection() {
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +0000460 finished_profiles_.Iterate(DeleteCpuProfile);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000461 current_profiles_.Iterate(DeleteCpuProfile);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000462 code_entries_.Iterate(DeleteCodeEntry);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000463}
464
465
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000466bool CpuProfilesCollection::StartProfiling(const char* title, unsigned uid,
467 bool record_samples) {
lrn@chromium.org25156de2010-04-06 13:10:27 +0000468 ASSERT(uid > 0);
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000469 current_profiles_semaphore_.Wait();
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000470 if (current_profiles_.length() >= kMaxSimultaneousProfiles) {
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000471 current_profiles_semaphore_.Signal();
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000472 return false;
473 }
lrn@chromium.org25156de2010-04-06 13:10:27 +0000474 for (int i = 0; i < current_profiles_.length(); ++i) {
475 if (strcmp(current_profiles_[i]->title(), title) == 0) {
476 // Ignore attempts to start profile with the same title.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000477 current_profiles_semaphore_.Signal();
lrn@chromium.org25156de2010-04-06 13:10:27 +0000478 return false;
479 }
480 }
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000481 current_profiles_.Add(new CpuProfile(title, uid, record_samples));
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000482 current_profiles_semaphore_.Signal();
lrn@chromium.org25156de2010-04-06 13:10:27 +0000483 return true;
484}
485
486
danno@chromium.orgd3c42102013-08-01 16:58:23 +0000487CpuProfile* CpuProfilesCollection::StopProfiling(const char* title) {
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +0000488 const int title_len = StrLength(title);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000489 CpuProfile* profile = NULL;
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000490 current_profiles_semaphore_.Wait();
lrn@chromium.org25156de2010-04-06 13:10:27 +0000491 for (int i = current_profiles_.length() - 1; i >= 0; --i) {
492 if (title_len == 0 || strcmp(current_profiles_[i]->title(), title) == 0) {
493 profile = current_profiles_.Remove(i);
494 break;
495 }
496 }
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000497 current_profiles_semaphore_.Signal();
lrn@chromium.org25156de2010-04-06 13:10:27 +0000498
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +0000499 if (profile == NULL) return NULL;
danno@chromium.orgd3c42102013-08-01 16:58:23 +0000500 profile->CalculateTotalTicksAndSamplingRate();
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +0000501 finished_profiles_.Add(profile);
502 return profile;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000503}
504
505
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000506bool CpuProfilesCollection::IsLastProfile(const char* title) {
507 // Called from VM thread, and only it can mutate the list,
508 // so no locking is needed here.
509 if (current_profiles_.length() != 1) return false;
510 return StrLength(title) == 0
511 || strcmp(current_profiles_[0]->title(), title) == 0;
512}
513
514
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000515void CpuProfilesCollection::RemoveProfile(CpuProfile* profile) {
516 // Called from VM thread for a completed profile.
517 unsigned uid = profile->uid();
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +0000518 for (int i = 0; i < finished_profiles_.length(); i++) {
519 if (uid == finished_profiles_[i]->uid()) {
520 finished_profiles_.Remove(i);
521 return;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000522 }
523 }
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +0000524 UNREACHABLE();
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000525}
526
527
lrn@chromium.org25156de2010-04-06 13:10:27 +0000528void CpuProfilesCollection::AddPathToCurrentProfiles(
529 const Vector<CodeEntry*>& path) {
530 // As starting / stopping profiles is rare relatively to this
531 // method, we don't bother minimizing the duration of lock holding,
532 // e.g. copying contents of the list to a local vector.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000533 current_profiles_semaphore_.Wait();
lrn@chromium.org25156de2010-04-06 13:10:27 +0000534 for (int i = 0; i < current_profiles_.length(); ++i) {
535 current_profiles_[i]->AddPath(path);
536 }
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000537 current_profiles_semaphore_.Signal();
lrn@chromium.org25156de2010-04-06 13:10:27 +0000538}
539
540
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000541CodeEntry* CpuProfilesCollection::NewCodeEntry(
542 Logger::LogEventsAndTags tag,
543 const char* name,
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000544 const char* name_prefix,
545 const char* resource_name,
546 int line_number) {
547 CodeEntry* code_entry = new CodeEntry(tag,
548 name,
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000549 name_prefix,
550 resource_name,
551 line_number);
552 code_entries_.Add(code_entry);
553 return code_entry;
554}
555
556
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000557const char* const ProfileGenerator::kAnonymousFunctionName =
558 "(anonymous function)";
559const char* const ProfileGenerator::kProgramEntryName =
560 "(program)";
danno@chromium.org59400602013-08-13 17:09:37 +0000561const char* const ProfileGenerator::kIdleEntryName =
562 "(idle)";
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000563const char* const ProfileGenerator::kGarbageCollectorEntryName =
564 "(garbage collector)";
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000565const char* const ProfileGenerator::kUnresolvedFunctionName =
566 "(unresolved function)";
ager@chromium.org357bf652010-04-12 11:30:10 +0000567
568
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000569ProfileGenerator::ProfileGenerator(CpuProfilesCollection* profiles)
ager@chromium.org357bf652010-04-12 11:30:10 +0000570 : profiles_(profiles),
571 program_entry_(
572 profiles->NewCodeEntry(Logger::FUNCTION_TAG, kProgramEntryName)),
danno@chromium.org59400602013-08-13 17:09:37 +0000573 idle_entry_(
574 profiles->NewCodeEntry(Logger::FUNCTION_TAG, kIdleEntryName)),
ager@chromium.org357bf652010-04-12 11:30:10 +0000575 gc_entry_(
576 profiles->NewCodeEntry(Logger::BUILTIN_TAG,
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000577 kGarbageCollectorEntryName)),
578 unresolved_entry_(
579 profiles->NewCodeEntry(Logger::FUNCTION_TAG,
580 kUnresolvedFunctionName)) {
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000581}
582
583
584void ProfileGenerator::RecordTickSample(const TickSample& sample) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000585 // Allocate space for stack frames + pc + function + vm-state.
586 ScopedVector<CodeEntry*> entries(sample.frames_count + 3);
587 // As actual number of decoded code entries may vary, initialize
588 // entries vector with NULL values.
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000589 CodeEntry** entry = entries.start();
ager@chromium.org357bf652010-04-12 11:30:10 +0000590 memset(entry, 0, entries.length() * sizeof(*entry));
591 if (sample.pc != NULL) {
yangguo@chromium.orgc73d55b2013-07-24 08:18:28 +0000592 if (sample.has_external_callback && sample.state == EXTERNAL &&
593 sample.top_frame_type == StackFrame::EXIT) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000594 // Don't use PC when in external callback code, as it can point
595 // inside callback's code, and we will erroneously report
596 // that a callback calls itself.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000597 *entry++ = code_map_.FindEntry(sample.external_callback);
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000598 } else {
599 Address start;
600 CodeEntry* pc_entry = code_map_.FindEntry(sample.pc, &start);
601 // If pc is in the function code before it set up stack frame or after the
602 // frame was destroyed SafeStackFrameIterator incorrectly thinks that
603 // ebp contains return address of the current function and skips caller's
604 // frame. Check for this case and just skip such samples.
605 if (pc_entry) {
606 List<OffsetRange>* ranges = pc_entry->no_frame_ranges();
607 if (ranges) {
608 Code* code = Code::cast(HeapObject::FromAddress(start));
609 int pc_offset = static_cast<int>(
610 sample.pc - code->instruction_start());
611 for (int i = 0; i < ranges->length(); i++) {
612 OffsetRange& range = ranges->at(i);
613 if (range.from <= pc_offset && pc_offset < range.to) {
614 return;
615 }
616 }
617 }
618 *entry++ = pc_entry;
619
machenbach@chromium.orgc1789ee2013-07-05 07:09:57 +0000620 if (pc_entry->builtin_id() == Builtins::kFunctionCall ||
621 pc_entry->builtin_id() == Builtins::kFunctionApply) {
622 // When current function is FunctionCall or FunctionApply builtin the
623 // top frame is either frame of the calling JS function or internal
624 // frame. In the latter case we know the caller for sure but in the
625 // former case we don't so we simply replace the frame with
626 // 'unresolved' entry.
627 if (sample.top_frame_type == StackFrame::JAVA_SCRIPT) {
628 *entry++ = unresolved_entry_;
629 }
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000630 }
631 }
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000632 }
ager@chromium.org357bf652010-04-12 11:30:10 +0000633
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000634 for (const Address* stack_pos = sample.stack,
ager@chromium.org357bf652010-04-12 11:30:10 +0000635 *stack_end = stack_pos + sample.frames_count;
636 stack_pos != stack_end;
637 ++stack_pos) {
638 *entry++ = code_map_.FindEntry(*stack_pos);
639 }
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000640 }
641
ager@chromium.org357bf652010-04-12 11:30:10 +0000642 if (FLAG_prof_browser_mode) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000643 bool no_symbolized_entries = true;
644 for (CodeEntry** e = entries.start(); e != entry; ++e) {
645 if (*e != NULL) {
646 no_symbolized_entries = false;
647 break;
648 }
649 }
650 // If no frames were symbolized, put the VM state entry in.
651 if (no_symbolized_entries) {
652 *entry++ = EntryForVMState(sample.state);
653 }
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000654 }
655
lrn@chromium.org25156de2010-04-06 13:10:27 +0000656 profiles_->AddPathToCurrentProfiles(entries);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000657}
658
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000659
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000660} } // namespace v8::internal