blob: cf268afc9b41e18f0b763816df783a1f0008798d [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
hpayer@chromium.orgc5d49712013-09-11 08:25:48 +000044StringsStorage::StringsStorage(Heap* heap)
45 : hash_seed_(heap->HashSeed()), names_(StringsMatch) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +000046}
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 =
hpayer@chromium.orgc5d49712013-09-11 08:25:48 +000064 StringHasher::HashSequentialString(dst.start(), len, hash_seed_);
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(
hpayer@chromium.orgc5d49712013-09-11 08:25:48 +000098 str.start(), len, hash_seed_);
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 = "";
jkummerow@chromium.org2c9426b2013-09-05 16:31:13 +0000136const char* const CodeEntry::kEmptyBailoutReason = "";
lrn@chromium.org25156de2010-04-06 13:10:27 +0000137
138
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000139CodeEntry::~CodeEntry() {
140 delete no_frame_ranges_;
141}
142
143
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000144uint32_t CodeEntry::GetCallUid() const {
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000145 uint32_t hash = ComputeIntegerHash(tag_, v8::internal::kZeroHashSeed);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000146 if (shared_id_ != 0) {
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000147 hash ^= ComputeIntegerHash(static_cast<uint32_t>(shared_id_),
148 v8::internal::kZeroHashSeed);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000149 } else {
150 hash ^= ComputeIntegerHash(
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000151 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(name_prefix_)),
152 v8::internal::kZeroHashSeed);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000153 hash ^= ComputeIntegerHash(
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000154 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(name_)),
155 v8::internal::kZeroHashSeed);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000156 hash ^= ComputeIntegerHash(
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000157 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(resource_name_)),
158 v8::internal::kZeroHashSeed);
159 hash ^= ComputeIntegerHash(line_number_, v8::internal::kZeroHashSeed);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000160 }
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000161 return hash;
162}
163
164
165bool CodeEntry::IsSameAs(CodeEntry* entry) const {
166 return this == entry
167 || (tag_ == entry->tag_
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000168 && shared_id_ == entry->shared_id_
169 && (shared_id_ != 0
170 || (name_prefix_ == entry->name_prefix_
171 && name_ == entry->name_
172 && resource_name_ == entry->resource_name_
173 && line_number_ == entry->line_number_)));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000174}
175
176
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000177void CodeEntry::SetBuiltinId(Builtins::Name id) {
178 tag_ = Logger::BUILTIN_TAG;
179 builtin_id_ = id;
180}
181
182
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000183ProfileNode* ProfileNode::FindChild(CodeEntry* entry) {
184 HashMap::Entry* map_entry =
185 children_.Lookup(entry, CodeEntryHash(entry), false);
186 return map_entry != NULL ?
187 reinterpret_cast<ProfileNode*>(map_entry->value) : NULL;
188}
189
190
191ProfileNode* ProfileNode::FindOrAddChild(CodeEntry* entry) {
192 HashMap::Entry* map_entry =
193 children_.Lookup(entry, CodeEntryHash(entry), true);
194 if (map_entry->value == NULL) {
195 // New node added.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000196 ProfileNode* new_node = new ProfileNode(tree_, entry);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000197 map_entry->value = new_node;
198 children_list_.Add(new_node);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000199 }
200 return reinterpret_cast<ProfileNode*>(map_entry->value);
201}
202
203
204void ProfileNode::Print(int indent) {
jkummerow@chromium.org2c9426b2013-09-05 16:31:13 +0000205 OS::Print("%5u %*c %s%s %d #%d %s",
verwaest@chromium.org662436e2013-08-28 08:41:27 +0000206 self_ticks_,
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000207 indent, ' ',
ager@chromium.org357bf652010-04-12 11:30:10 +0000208 entry_->name_prefix(),
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000209 entry_->name(),
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000210 entry_->script_id(),
jkummerow@chromium.org2c9426b2013-09-05 16:31:13 +0000211 id(),
212 entry_->bailout_reason());
ager@chromium.org357bf652010-04-12 11:30:10 +0000213 if (entry_->resource_name()[0] != '\0')
214 OS::Print(" %s:%d", entry_->resource_name(), entry_->line_number());
215 OS::Print("\n");
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000216 for (HashMap::Entry* p = children_.Start();
217 p != NULL;
218 p = children_.Next(p)) {
219 reinterpret_cast<ProfileNode*>(p->value)->Print(indent + 2);
220 }
221}
222
223
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000224class DeleteNodesCallback {
225 public:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000226 void BeforeTraversingChild(ProfileNode*, ProfileNode*) { }
227
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000228 void AfterAllChildrenTraversed(ProfileNode* node) {
229 delete node;
230 }
231
232 void AfterChildTraversed(ProfileNode*, ProfileNode*) { }
233};
234
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000235
ager@chromium.org357bf652010-04-12 11:30:10 +0000236ProfileTree::ProfileTree()
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000237 : root_entry_(Logger::FUNCTION_TAG, "(root)"),
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000238 next_node_id_(1),
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000239 root_(new ProfileNode(this, &root_entry_)) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000240}
241
242
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000243ProfileTree::~ProfileTree() {
244 DeleteNodesCallback cb;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000245 TraverseDepthFirst(&cb);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000246}
247
248
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000249ProfileNode* ProfileTree::AddPathFromEnd(const Vector<CodeEntry*>& path) {
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000250 ProfileNode* node = root_;
251 for (CodeEntry** entry = path.start() + path.length() - 1;
252 entry != path.start() - 1;
253 --entry) {
254 if (*entry != NULL) {
255 node = node->FindOrAddChild(*entry);
256 }
257 }
258 node->IncrementSelfTicks();
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000259 return node;
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000260}
261
262
263void ProfileTree::AddPathFromStart(const Vector<CodeEntry*>& path) {
264 ProfileNode* node = root_;
265 for (CodeEntry** entry = path.start();
266 entry != path.start() + path.length();
267 ++entry) {
268 if (*entry != NULL) {
269 node = node->FindOrAddChild(*entry);
270 }
271 }
272 node->IncrementSelfTicks();
273}
274
275
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000276struct NodesPair {
277 NodesPair(ProfileNode* src, ProfileNode* dst)
278 : src(src), dst(dst) { }
279 ProfileNode* src;
280 ProfileNode* dst;
281};
282
283
lrn@chromium.org25156de2010-04-06 13:10:27 +0000284class Position {
285 public:
286 explicit Position(ProfileNode* node)
287 : node(node), child_idx_(0) { }
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000288 INLINE(ProfileNode* current_child()) {
lrn@chromium.org25156de2010-04-06 13:10:27 +0000289 return node->children()->at(child_idx_);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000290 }
lrn@chromium.org25156de2010-04-06 13:10:27 +0000291 INLINE(bool has_current_child()) {
292 return child_idx_ < node->children()->length();
293 }
294 INLINE(void next_child()) { ++child_idx_; }
295
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000296 ProfileNode* node;
lrn@chromium.org25156de2010-04-06 13:10:27 +0000297 private:
298 int child_idx_;
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000299};
300
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000301
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000302// Non-recursive implementation of a depth-first post-order tree traversal.
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000303template <typename Callback>
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000304void ProfileTree::TraverseDepthFirst(Callback* callback) {
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000305 List<Position> stack(10);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000306 stack.Add(Position(root_));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000307 while (stack.length() > 0) {
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000308 Position& current = stack.last();
lrn@chromium.org25156de2010-04-06 13:10:27 +0000309 if (current.has_current_child()) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000310 callback->BeforeTraversingChild(current.node, current.current_child());
lrn@chromium.org25156de2010-04-06 13:10:27 +0000311 stack.Add(Position(current.current_child()));
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000312 } else {
313 callback->AfterAllChildrenTraversed(current.node);
314 if (stack.length() > 1) {
315 Position& parent = stack[stack.length() - 2];
316 callback->AfterChildTraversed(parent.node, current.node);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000317 parent.next_child();
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000318 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000319 // Remove child from the stack.
320 stack.RemoveLast();
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000321 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000322 }
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000323}
324
325
danno@chromium.orgd3c42102013-08-01 16:58:23 +0000326CpuProfile::CpuProfile(const char* title, unsigned uid, bool record_samples)
327 : title_(title),
328 uid_(uid),
329 record_samples_(record_samples),
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +0000330 start_time_(Time::NowFromSystemTime()) {
331 timer_.Start();
danno@chromium.orgd3c42102013-08-01 16:58:23 +0000332}
333
334
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000335void CpuProfile::AddPath(const Vector<CodeEntry*>& path) {
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000336 ProfileNode* top_frame_node = top_down_.AddPathFromEnd(path);
337 if (record_samples_) samples_.Add(top_frame_node);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000338}
339
340
danno@chromium.orgd3c42102013-08-01 16:58:23 +0000341void CpuProfile::CalculateTotalTicksAndSamplingRate() {
jkummerow@chromium.orgdc94e192013-08-30 11:35:42 +0000342 end_time_ = start_time_ + timer_.Elapsed();
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000343}
344
345
346void CpuProfile::Print() {
347 OS::Print("[Top down]:\n");
348 top_down_.Print();
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000349}
350
351
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000352CodeEntry* const CodeMap::kSharedFunctionCodeEntry = NULL;
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000353const CodeMap::CodeTreeConfig::Key CodeMap::CodeTreeConfig::kNoKey = NULL;
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000354
355
lrn@chromium.org34e60782011-09-15 07:25:40 +0000356void CodeMap::AddCode(Address addr, CodeEntry* entry, unsigned size) {
357 DeleteAllCoveredCode(addr, addr + size);
358 CodeTree::Locator locator;
359 tree_.Insert(addr, &locator);
360 locator.set_value(CodeEntryInfo(entry, size));
361}
362
363
364void CodeMap::DeleteAllCoveredCode(Address start, Address end) {
365 List<Address> to_delete;
366 Address addr = end - 1;
367 while (addr >= start) {
368 CodeTree::Locator locator;
369 if (!tree_.FindGreatestLessThan(addr, &locator)) break;
370 Address start2 = locator.key(), end2 = start2 + locator.value().size;
371 if (start2 < end && start < end2) to_delete.Add(start2);
372 addr = start2 - 1;
373 }
374 for (int i = 0; i < to_delete.length(); ++i) tree_.Remove(to_delete[i]);
375}
376
377
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000378CodeEntry* CodeMap::FindEntry(Address addr, Address* start) {
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000379 CodeTree::Locator locator;
380 if (tree_.FindGreatestLessThan(addr, &locator)) {
381 // locator.key() <= addr. Need to check that addr is within entry.
382 const CodeEntryInfo& entry = locator.value();
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000383 if (addr < (locator.key() + entry.size)) {
384 if (start) {
385 *start = locator.key();
386 }
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000387 return entry.entry;
jkummerow@chromium.org4e308cf2013-05-17 13:39:16 +0000388 }
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000389 }
390 return NULL;
391}
392
393
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000394int CodeMap::GetSharedId(Address addr) {
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000395 CodeTree::Locator locator;
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000396 // For shared function entries, 'size' field is used to store their IDs.
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000397 if (tree_.Find(addr, &locator)) {
398 const CodeEntryInfo& entry = locator.value();
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000399 ASSERT(entry.entry == kSharedFunctionCodeEntry);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000400 return entry.size;
401 } else {
402 tree_.Insert(addr, &locator);
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000403 int id = next_shared_id_++;
404 locator.set_value(CodeEntryInfo(kSharedFunctionCodeEntry, id));
405 return id;
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000406 }
407}
408
409
lrn@chromium.org34e60782011-09-15 07:25:40 +0000410void CodeMap::MoveCode(Address from, Address to) {
411 if (from == to) return;
412 CodeTree::Locator locator;
413 if (!tree_.Find(from, &locator)) return;
414 CodeEntryInfo entry = locator.value();
415 tree_.Remove(from);
416 AddCode(to, entry.entry, entry.size);
417}
418
419
lrn@chromium.org25156de2010-04-06 13:10:27 +0000420void CodeMap::CodeTreePrinter::Call(
421 const Address& key, const CodeMap::CodeEntryInfo& value) {
danno@chromium.orgca29dd82013-04-26 11:59:48 +0000422 // For shared function entries, 'size' field is used to store their IDs.
423 if (value.entry == kSharedFunctionCodeEntry) {
424 OS::Print("%p SharedFunctionInfo %d\n", key, value.size);
425 } else {
426 OS::Print("%p %5d %s\n", key, value.size, value.entry->name());
427 }
lrn@chromium.org25156de2010-04-06 13:10:27 +0000428}
429
430
431void CodeMap::Print() {
432 CodeTreePrinter printer;
433 tree_.ForEach(&printer);
434}
435
436
hpayer@chromium.orgc5d49712013-09-11 08:25:48 +0000437CpuProfilesCollection::CpuProfilesCollection(Heap* heap)
438 : function_and_resource_names_(heap),
439 current_profiles_semaphore_(1) {
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000440}
441
442
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000443static void DeleteCodeEntry(CodeEntry** entry_ptr) {
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000444 delete *entry_ptr;
445}
446
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +0000447
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000448static void DeleteCpuProfile(CpuProfile** profile_ptr) {
449 delete *profile_ptr;
450}
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000451
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000452
453CpuProfilesCollection::~CpuProfilesCollection() {
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +0000454 finished_profiles_.Iterate(DeleteCpuProfile);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000455 current_profiles_.Iterate(DeleteCpuProfile);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000456 code_entries_.Iterate(DeleteCodeEntry);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000457}
458
459
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000460bool CpuProfilesCollection::StartProfiling(const char* title, unsigned uid,
461 bool record_samples) {
lrn@chromium.org25156de2010-04-06 13:10:27 +0000462 ASSERT(uid > 0);
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000463 current_profiles_semaphore_.Wait();
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000464 if (current_profiles_.length() >= kMaxSimultaneousProfiles) {
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000465 current_profiles_semaphore_.Signal();
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000466 return false;
467 }
lrn@chromium.org25156de2010-04-06 13:10:27 +0000468 for (int i = 0; i < current_profiles_.length(); ++i) {
469 if (strcmp(current_profiles_[i]->title(), title) == 0) {
470 // Ignore attempts to start profile with the same title.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000471 current_profiles_semaphore_.Signal();
lrn@chromium.org25156de2010-04-06 13:10:27 +0000472 return false;
473 }
474 }
mstarzinger@chromium.orgf705b502013-04-04 11:38:09 +0000475 current_profiles_.Add(new CpuProfile(title, uid, record_samples));
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000476 current_profiles_semaphore_.Signal();
lrn@chromium.org25156de2010-04-06 13:10:27 +0000477 return true;
478}
479
480
danno@chromium.orgd3c42102013-08-01 16:58:23 +0000481CpuProfile* CpuProfilesCollection::StopProfiling(const char* title) {
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +0000482 const int title_len = StrLength(title);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000483 CpuProfile* profile = NULL;
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000484 current_profiles_semaphore_.Wait();
lrn@chromium.org25156de2010-04-06 13:10:27 +0000485 for (int i = current_profiles_.length() - 1; i >= 0; --i) {
486 if (title_len == 0 || strcmp(current_profiles_[i]->title(), title) == 0) {
487 profile = current_profiles_.Remove(i);
488 break;
489 }
490 }
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000491 current_profiles_semaphore_.Signal();
lrn@chromium.org25156de2010-04-06 13:10:27 +0000492
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +0000493 if (profile == NULL) return NULL;
danno@chromium.orgd3c42102013-08-01 16:58:23 +0000494 profile->CalculateTotalTicksAndSamplingRate();
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +0000495 finished_profiles_.Add(profile);
496 return profile;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000497}
498
499
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000500bool CpuProfilesCollection::IsLastProfile(const char* title) {
501 // Called from VM thread, and only it can mutate the list,
502 // so no locking is needed here.
503 if (current_profiles_.length() != 1) return false;
504 return StrLength(title) == 0
505 || strcmp(current_profiles_[0]->title(), title) == 0;
506}
507
508
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000509void CpuProfilesCollection::RemoveProfile(CpuProfile* profile) {
510 // Called from VM thread for a completed profile.
511 unsigned uid = profile->uid();
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +0000512 for (int i = 0; i < finished_profiles_.length(); i++) {
513 if (uid == finished_profiles_[i]->uid()) {
514 finished_profiles_.Remove(i);
515 return;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000516 }
517 }
mstarzinger@chromium.orge0e1b0d2013-07-08 08:38:06 +0000518 UNREACHABLE();
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000519}
520
521
lrn@chromium.org25156de2010-04-06 13:10:27 +0000522void CpuProfilesCollection::AddPathToCurrentProfiles(
523 const Vector<CodeEntry*>& path) {
524 // As starting / stopping profiles is rare relatively to this
525 // method, we don't bother minimizing the duration of lock holding,
526 // e.g. copying contents of the list to a local vector.
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000527 current_profiles_semaphore_.Wait();
lrn@chromium.org25156de2010-04-06 13:10:27 +0000528 for (int i = 0; i < current_profiles_.length(); ++i) {
529 current_profiles_[i]->AddPath(path);
530 }
mstarzinger@chromium.orge9000182013-09-03 11:25:39 +0000531 current_profiles_semaphore_.Signal();
lrn@chromium.org25156de2010-04-06 13:10:27 +0000532}
533
534
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000535CodeEntry* CpuProfilesCollection::NewCodeEntry(
536 Logger::LogEventsAndTags tag,
537 const char* name,
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000538 const char* name_prefix,
539 const char* resource_name,
mvstanton@chromium.orgdd6d9ee2013-10-11 10:35:37 +0000540 int line_number,
541 int column_number) {
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000542 CodeEntry* code_entry = new CodeEntry(tag,
543 name,
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000544 name_prefix,
545 resource_name,
mvstanton@chromium.orgdd6d9ee2013-10-11 10:35:37 +0000546 line_number,
547 column_number);
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000548 code_entries_.Add(code_entry);
549 return code_entry;
550}
551
552
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000553const char* const ProfileGenerator::kAnonymousFunctionName =
554 "(anonymous function)";
555const char* const ProfileGenerator::kProgramEntryName =
556 "(program)";
danno@chromium.org59400602013-08-13 17:09:37 +0000557const char* const ProfileGenerator::kIdleEntryName =
558 "(idle)";
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000559const char* const ProfileGenerator::kGarbageCollectorEntryName =
560 "(garbage collector)";
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000561const char* const ProfileGenerator::kUnresolvedFunctionName =
562 "(unresolved function)";
ager@chromium.org357bf652010-04-12 11:30:10 +0000563
564
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000565ProfileGenerator::ProfileGenerator(CpuProfilesCollection* profiles)
ager@chromium.org357bf652010-04-12 11:30:10 +0000566 : profiles_(profiles),
567 program_entry_(
568 profiles->NewCodeEntry(Logger::FUNCTION_TAG, kProgramEntryName)),
danno@chromium.org59400602013-08-13 17:09:37 +0000569 idle_entry_(
570 profiles->NewCodeEntry(Logger::FUNCTION_TAG, kIdleEntryName)),
ager@chromium.org357bf652010-04-12 11:30:10 +0000571 gc_entry_(
572 profiles->NewCodeEntry(Logger::BUILTIN_TAG,
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000573 kGarbageCollectorEntryName)),
574 unresolved_entry_(
575 profiles->NewCodeEntry(Logger::FUNCTION_TAG,
576 kUnresolvedFunctionName)) {
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000577}
578
579
580void ProfileGenerator::RecordTickSample(const TickSample& sample) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000581 // Allocate space for stack frames + pc + function + vm-state.
582 ScopedVector<CodeEntry*> entries(sample.frames_count + 3);
583 // As actual number of decoded code entries may vary, initialize
584 // entries vector with NULL values.
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000585 CodeEntry** entry = entries.start();
ager@chromium.org357bf652010-04-12 11:30:10 +0000586 memset(entry, 0, entries.length() * sizeof(*entry));
587 if (sample.pc != NULL) {
yangguo@chromium.orgc73d55b2013-07-24 08:18:28 +0000588 if (sample.has_external_callback && sample.state == EXTERNAL &&
589 sample.top_frame_type == StackFrame::EXIT) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000590 // Don't use PC when in external callback code, as it can point
591 // inside callback's code, and we will erroneously report
592 // that a callback calls itself.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000593 *entry++ = code_map_.FindEntry(sample.external_callback);
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000594 } else {
595 Address start;
596 CodeEntry* pc_entry = code_map_.FindEntry(sample.pc, &start);
597 // If pc is in the function code before it set up stack frame or after the
598 // frame was destroyed SafeStackFrameIterator incorrectly thinks that
599 // ebp contains return address of the current function and skips caller's
600 // frame. Check for this case and just skip such samples.
601 if (pc_entry) {
602 List<OffsetRange>* ranges = pc_entry->no_frame_ranges();
603 if (ranges) {
604 Code* code = Code::cast(HeapObject::FromAddress(start));
605 int pc_offset = static_cast<int>(
606 sample.pc - code->instruction_start());
607 for (int i = 0; i < ranges->length(); i++) {
608 OffsetRange& range = ranges->at(i);
609 if (range.from <= pc_offset && pc_offset < range.to) {
610 return;
611 }
612 }
613 }
614 *entry++ = pc_entry;
615
machenbach@chromium.orgc1789ee2013-07-05 07:09:57 +0000616 if (pc_entry->builtin_id() == Builtins::kFunctionCall ||
617 pc_entry->builtin_id() == Builtins::kFunctionApply) {
618 // When current function is FunctionCall or FunctionApply builtin the
619 // top frame is either frame of the calling JS function or internal
620 // frame. In the latter case we know the caller for sure but in the
621 // former case we don't so we simply replace the frame with
622 // 'unresolved' entry.
623 if (sample.top_frame_type == StackFrame::JAVA_SCRIPT) {
624 *entry++ = unresolved_entry_;
625 }
jkummerow@chromium.org93a47f42013-07-02 14:43:41 +0000626 }
627 }
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000628 }
ager@chromium.org357bf652010-04-12 11:30:10 +0000629
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +0000630 for (const Address* stack_pos = sample.stack,
ager@chromium.org357bf652010-04-12 11:30:10 +0000631 *stack_end = stack_pos + sample.frames_count;
632 stack_pos != stack_end;
633 ++stack_pos) {
634 *entry++ = code_map_.FindEntry(*stack_pos);
635 }
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000636 }
637
ager@chromium.org357bf652010-04-12 11:30:10 +0000638 if (FLAG_prof_browser_mode) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000639 bool no_symbolized_entries = true;
640 for (CodeEntry** e = entries.start(); e != entry; ++e) {
641 if (*e != NULL) {
642 no_symbolized_entries = false;
643 break;
644 }
645 }
646 // If no frames were symbolized, put the VM state entry in.
647 if (no_symbolized_entries) {
648 *entry++ = EntryForVMState(sample.state);
649 }
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000650 }
651
lrn@chromium.org25156de2010-04-06 13:10:27 +0000652 profiles_->AddPathToCurrentProfiles(entries);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000653}
654
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000655
mstarzinger@chromium.orga2e1a402013-10-15 08:25:05 +0000656CodeEntry* ProfileGenerator::EntryForVMState(StateTag tag) {
657 switch (tag) {
658 case GC:
659 return gc_entry_;
660 case JS:
661 case COMPILER:
662 // DOM events handlers are reported as OTHER / EXTERNAL entries.
663 // To avoid confusing people, let's put all these entries into
664 // one bucket.
665 case OTHER:
666 case EXTERNAL:
667 return program_entry_;
668 case IDLE:
669 return idle_entry_;
670 default: return NULL;
671 }
672}
673
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000674} } // namespace v8::internal