blob: 6017f12dc67ece8ac7e51383e2479b4f36721dcb [file] [log] [blame]
Ben Murdoch3ef787d2012-04-12 10:51:47 +01001// Copyright 2012 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Block6ded16b2010-05-10 14:33:55 +01004
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005#include "src/v8.h"
Ben Murdoch257744e2011-11-30 15:57:28 +00006
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007#include "src/profile-generator-inl.h"
Ben Murdoch257744e2011-11-30 15:57:28 +00008
Ben Murdochb8a8cc12014-11-26 15:28:44 +00009#include "src/compiler.h"
10#include "src/debug.h"
11#include "src/global-handles.h"
12#include "src/sampler.h"
13#include "src/scopeinfo.h"
14#include "src/unicode.h"
15#include "src/zone-inl.h"
Steve Block6ded16b2010-05-10 14:33:55 +010016
Steve Block6ded16b2010-05-10 14:33:55 +010017namespace v8 {
18namespace internal {
19
20
Ben Murdochb8a8cc12014-11-26 15:28:44 +000021bool StringsStorage::StringsMatch(void* key1, void* key2) {
22 return strcmp(reinterpret_cast<char*>(key1),
23 reinterpret_cast<char*>(key2)) == 0;
Leon Clarkef7060e22010-06-03 12:02:55 +010024}
25
26
Ben Murdochb8a8cc12014-11-26 15:28:44 +000027StringsStorage::StringsStorage(Heap* heap)
28 : hash_seed_(heap->HashSeed()), names_(StringsMatch) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010029}
30
31
32StringsStorage::~StringsStorage() {
33 for (HashMap::Entry* p = names_.Start();
34 p != NULL;
35 p = names_.Next(p)) {
36 DeleteArray(reinterpret_cast<const char*>(p->value));
37 }
Steve Block44f0eee2011-05-26 01:26:41 +010038}
39
40
41const char* StringsStorage::GetCopy(const char* src) {
42 int len = static_cast<int>(strlen(src));
Ben Murdochb8a8cc12014-11-26 15:28:44 +000043 HashMap::Entry* entry = GetEntry(src, len);
44 if (entry->value == NULL) {
45 Vector<char> dst = Vector<char>::New(len + 1);
46 StrNCpy(dst, src, len);
47 dst[len] = '\0';
48 entry->key = dst.start();
49 entry->value = entry->key;
50 }
51 return reinterpret_cast<const char*>(entry->value);
Steve Block44f0eee2011-05-26 01:26:41 +010052}
53
54
55const char* StringsStorage::GetFormatted(const char* format, ...) {
56 va_list args;
57 va_start(args, format);
58 const char* result = GetVFormatted(format, args);
59 va_end(args);
60 return result;
61}
62
63
Ben Murdochb8a8cc12014-11-26 15:28:44 +000064const char* StringsStorage::AddOrDisposeString(char* str, int len) {
65 HashMap::Entry* entry = GetEntry(str, len);
66 if (entry->value == NULL) {
Steve Block44f0eee2011-05-26 01:26:41 +010067 // New entry added.
Ben Murdochb8a8cc12014-11-26 15:28:44 +000068 entry->key = str;
69 entry->value = str;
Steve Block44f0eee2011-05-26 01:26:41 +010070 } else {
71 DeleteArray(str);
72 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000073 return reinterpret_cast<const char*>(entry->value);
Steve Block44f0eee2011-05-26 01:26:41 +010074}
75
76
77const char* StringsStorage::GetVFormatted(const char* format, va_list args) {
78 Vector<char> str = Vector<char>::New(1024);
Ben Murdochb8a8cc12014-11-26 15:28:44 +000079 int len = VSNPrintF(str, format, args);
Steve Block44f0eee2011-05-26 01:26:41 +010080 if (len == -1) {
81 DeleteArray(str.start());
Ben Murdochb8a8cc12014-11-26 15:28:44 +000082 return GetCopy(format);
Steve Block44f0eee2011-05-26 01:26:41 +010083 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000084 return AddOrDisposeString(str.start(), len);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010085}
86
87
Ben Murdochb8a8cc12014-11-26 15:28:44 +000088const char* StringsStorage::GetName(Name* name) {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010089 if (name->IsString()) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000090 String* str = String::cast(name);
91 int length = Min(kMaxNameSize, str->length());
92 int actual_length = 0;
Ben Murdoch3ef787d2012-04-12 10:51:47 +010093 SmartArrayPointer<char> data =
Ben Murdochb8a8cc12014-11-26 15:28:44 +000094 str->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL, 0, length,
95 &actual_length);
96 return AddOrDisposeString(data.Detach(), actual_length);
97 } else if (name->IsSymbol()) {
98 return "<symbol>";
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010099 }
100 return "";
101}
102
103
Ben Murdochf87a2032010-10-22 12:50:53 +0100104const char* StringsStorage::GetName(int index) {
Steve Block44f0eee2011-05-26 01:26:41 +0100105 return GetFormatted("%d", index);
Ben Murdochf87a2032010-10-22 12:50:53 +0100106}
107
108
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000109const char* StringsStorage::GetFunctionName(Name* name) {
110 return GetName(name);
111}
112
113
114const char* StringsStorage::GetFunctionName(const char* name) {
115 return GetCopy(name);
116}
117
118
119size_t StringsStorage::GetUsedMemorySize() const {
120 size_t size = sizeof(*this);
121 size += sizeof(HashMap::Entry) * names_.capacity();
122 for (HashMap::Entry* p = names_.Start(); p != NULL; p = names_.Next(p)) {
123 size += strlen(reinterpret_cast<const char*>(p->value)) + 1;
124 }
125 return size;
126}
127
128
129HashMap::Entry* StringsStorage::GetEntry(const char* str, int len) {
130 uint32_t hash = StringHasher::HashSequentialString(str, len, hash_seed_);
131 return names_.Lookup(const_cast<char*>(str), hash, true);
132}
133
134
Steve Block44f0eee2011-05-26 01:26:41 +0100135const char* const CodeEntry::kEmptyNamePrefix = "";
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000136const char* const CodeEntry::kEmptyResourceName = "";
137const char* const CodeEntry::kEmptyBailoutReason = "";
Steve Block6ded16b2010-05-10 14:33:55 +0100138
139
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000140CodeEntry::~CodeEntry() {
141 delete no_frame_ranges_;
Leon Clarkef7060e22010-06-03 12:02:55 +0100142}
143
144
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100145uint32_t CodeEntry::GetCallUid() const {
Ben Murdochc7cc0282012-03-05 14:35:55 +0000146 uint32_t hash = ComputeIntegerHash(tag_, v8::internal::kZeroHashSeed);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100147 if (shared_id_ != 0) {
Ben Murdochc7cc0282012-03-05 14:35:55 +0000148 hash ^= ComputeIntegerHash(static_cast<uint32_t>(shared_id_),
149 v8::internal::kZeroHashSeed);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100150 } else {
151 hash ^= ComputeIntegerHash(
Ben Murdochc7cc0282012-03-05 14:35:55 +0000152 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(name_prefix_)),
153 v8::internal::kZeroHashSeed);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100154 hash ^= ComputeIntegerHash(
Ben Murdochc7cc0282012-03-05 14:35:55 +0000155 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(name_)),
156 v8::internal::kZeroHashSeed);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100157 hash ^= ComputeIntegerHash(
Ben Murdochc7cc0282012-03-05 14:35:55 +0000158 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(resource_name_)),
159 v8::internal::kZeroHashSeed);
160 hash ^= ComputeIntegerHash(line_number_, v8::internal::kZeroHashSeed);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100161 }
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100162 return hash;
163}
164
165
166bool CodeEntry::IsSameAs(CodeEntry* entry) const {
167 return this == entry
168 || (tag_ == entry->tag_
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100169 && shared_id_ == entry->shared_id_
170 && (shared_id_ != 0
171 || (name_prefix_ == entry->name_prefix_
172 && name_ == entry->name_
173 && resource_name_ == entry->resource_name_
174 && line_number_ == entry->line_number_)));
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100175}
176
177
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000178void CodeEntry::SetBuiltinId(Builtins::Name id) {
179 tag_ = Logger::BUILTIN_TAG;
180 builtin_id_ = id;
181}
182
183
Steve Block6ded16b2010-05-10 14:33:55 +0100184ProfileNode* ProfileNode::FindChild(CodeEntry* entry) {
185 HashMap::Entry* map_entry =
186 children_.Lookup(entry, CodeEntryHash(entry), false);
187 return map_entry != NULL ?
188 reinterpret_cast<ProfileNode*>(map_entry->value) : NULL;
189}
190
191
192ProfileNode* ProfileNode::FindOrAddChild(CodeEntry* entry) {
193 HashMap::Entry* map_entry =
194 children_.Lookup(entry, CodeEntryHash(entry), true);
195 if (map_entry->value == NULL) {
196 // New node added.
197 ProfileNode* new_node = new ProfileNode(tree_, entry);
198 map_entry->value = new_node;
199 children_list_.Add(new_node);
200 }
201 return reinterpret_cast<ProfileNode*>(map_entry->value);
202}
203
204
Steve Block6ded16b2010-05-10 14:33:55 +0100205void ProfileNode::Print(int indent) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000206 base::OS::Print("%5u %*s %s%s %d #%d %s", self_ticks_, indent, "",
207 entry_->name_prefix(), entry_->name(), entry_->script_id(),
208 id(), entry_->bailout_reason());
Steve Block6ded16b2010-05-10 14:33:55 +0100209 if (entry_->resource_name()[0] != '\0')
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000210 base::OS::Print(" %s:%d", entry_->resource_name(), entry_->line_number());
211 base::OS::Print("\n");
Steve Block6ded16b2010-05-10 14:33:55 +0100212 for (HashMap::Entry* p = children_.Start();
213 p != NULL;
214 p = children_.Next(p)) {
215 reinterpret_cast<ProfileNode*>(p->value)->Print(indent + 2);
216 }
217}
218
219
Steve Block6ded16b2010-05-10 14:33:55 +0100220class DeleteNodesCallback {
221 public:
Leon Clarkef7060e22010-06-03 12:02:55 +0100222 void BeforeTraversingChild(ProfileNode*, ProfileNode*) { }
223
Steve Block6ded16b2010-05-10 14:33:55 +0100224 void AfterAllChildrenTraversed(ProfileNode* node) {
225 delete node;
226 }
227
228 void AfterChildTraversed(ProfileNode*, ProfileNode*) { }
229};
230
Steve Block6ded16b2010-05-10 14:33:55 +0100231
232ProfileTree::ProfileTree()
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000233 : root_entry_(Logger::FUNCTION_TAG, "(root)"),
234 next_node_id_(1),
Steve Block6ded16b2010-05-10 14:33:55 +0100235 root_(new ProfileNode(this, &root_entry_)) {
236}
237
238
239ProfileTree::~ProfileTree() {
240 DeleteNodesCallback cb;
Leon Clarkef7060e22010-06-03 12:02:55 +0100241 TraverseDepthFirst(&cb);
Steve Block6ded16b2010-05-10 14:33:55 +0100242}
243
244
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000245ProfileNode* ProfileTree::AddPathFromEnd(const Vector<CodeEntry*>& path) {
Steve Block6ded16b2010-05-10 14:33:55 +0100246 ProfileNode* node = root_;
247 for (CodeEntry** entry = path.start() + path.length() - 1;
248 entry != path.start() - 1;
249 --entry) {
250 if (*entry != NULL) {
251 node = node->FindOrAddChild(*entry);
252 }
253 }
254 node->IncrementSelfTicks();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000255 return node;
Steve Block6ded16b2010-05-10 14:33:55 +0100256}
257
258
259void ProfileTree::AddPathFromStart(const Vector<CodeEntry*>& path) {
260 ProfileNode* node = root_;
261 for (CodeEntry** entry = path.start();
262 entry != path.start() + path.length();
263 ++entry) {
264 if (*entry != NULL) {
265 node = node->FindOrAddChild(*entry);
266 }
267 }
268 node->IncrementSelfTicks();
269}
270
271
Leon Clarkef7060e22010-06-03 12:02:55 +0100272struct NodesPair {
273 NodesPair(ProfileNode* src, ProfileNode* dst)
274 : src(src), dst(dst) { }
275 ProfileNode* src;
276 ProfileNode* dst;
277};
278
279
Steve Block6ded16b2010-05-10 14:33:55 +0100280class Position {
281 public:
282 explicit Position(ProfileNode* node)
283 : node(node), child_idx_(0) { }
284 INLINE(ProfileNode* current_child()) {
285 return node->children()->at(child_idx_);
286 }
287 INLINE(bool has_current_child()) {
288 return child_idx_ < node->children()->length();
289 }
290 INLINE(void next_child()) { ++child_idx_; }
291
292 ProfileNode* node;
293 private:
294 int child_idx_;
295};
296
Steve Block6ded16b2010-05-10 14:33:55 +0100297
298// Non-recursive implementation of a depth-first post-order tree traversal.
299template <typename Callback>
Leon Clarkef7060e22010-06-03 12:02:55 +0100300void ProfileTree::TraverseDepthFirst(Callback* callback) {
Steve Block6ded16b2010-05-10 14:33:55 +0100301 List<Position> stack(10);
302 stack.Add(Position(root_));
Leon Clarkef7060e22010-06-03 12:02:55 +0100303 while (stack.length() > 0) {
Steve Block6ded16b2010-05-10 14:33:55 +0100304 Position& current = stack.last();
305 if (current.has_current_child()) {
Leon Clarkef7060e22010-06-03 12:02:55 +0100306 callback->BeforeTraversingChild(current.node, current.current_child());
Steve Block6ded16b2010-05-10 14:33:55 +0100307 stack.Add(Position(current.current_child()));
308 } else {
309 callback->AfterAllChildrenTraversed(current.node);
310 if (stack.length() > 1) {
311 Position& parent = stack[stack.length() - 2];
312 callback->AfterChildTraversed(parent.node, current.node);
313 parent.next_child();
Steve Block6ded16b2010-05-10 14:33:55 +0100314 }
Leon Clarkef7060e22010-06-03 12:02:55 +0100315 // Remove child from the stack.
316 stack.RemoveLast();
Steve Block6ded16b2010-05-10 14:33:55 +0100317 }
Leon Clarkef7060e22010-06-03 12:02:55 +0100318 }
Steve Block6ded16b2010-05-10 14:33:55 +0100319}
320
321
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000322CpuProfile::CpuProfile(const char* title, bool record_samples)
323 : title_(title),
324 record_samples_(record_samples),
325 start_time_(base::TimeTicks::HighResolutionNow()) {
326}
Leon Clarkef7060e22010-06-03 12:02:55 +0100327
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000328
329void CpuProfile::AddPath(base::TimeTicks timestamp,
330 const Vector<CodeEntry*>& path) {
331 ProfileNode* top_frame_node = top_down_.AddPathFromEnd(path);
332 if (record_samples_) {
333 timestamps_.Add(timestamp);
334 samples_.Add(top_frame_node);
Steve Block6ded16b2010-05-10 14:33:55 +0100335 }
Steve Block6ded16b2010-05-10 14:33:55 +0100336}
337
338
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000339void CpuProfile::CalculateTotalTicksAndSamplingRate() {
340 end_time_ = base::TimeTicks::HighResolutionNow();
Steve Block6ded16b2010-05-10 14:33:55 +0100341}
342
343
344void CpuProfile::Print() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000345 base::OS::Print("[Top down]:\n");
Steve Block6ded16b2010-05-10 14:33:55 +0100346 top_down_.Print();
Steve Block6ded16b2010-05-10 14:33:55 +0100347}
348
349
Steve Block44f0eee2011-05-26 01:26:41 +0100350CodeEntry* const CodeMap::kSharedFunctionCodeEntry = NULL;
Steve Block6ded16b2010-05-10 14:33:55 +0100351const CodeMap::CodeTreeConfig::Key CodeMap::CodeTreeConfig::kNoKey = NULL;
Steve Block6ded16b2010-05-10 14:33:55 +0100352
353
Ben Murdoch589d6972011-11-30 16:04:58 +0000354void CodeMap::AddCode(Address addr, CodeEntry* entry, unsigned size) {
355 DeleteAllCoveredCode(addr, addr + size);
356 CodeTree::Locator locator;
357 tree_.Insert(addr, &locator);
358 locator.set_value(CodeEntryInfo(entry, size));
359}
360
361
362void CodeMap::DeleteAllCoveredCode(Address start, Address end) {
363 List<Address> to_delete;
364 Address addr = end - 1;
365 while (addr >= start) {
366 CodeTree::Locator locator;
367 if (!tree_.FindGreatestLessThan(addr, &locator)) break;
368 Address start2 = locator.key(), end2 = start2 + locator.value().size;
369 if (start2 < end && start < end2) to_delete.Add(start2);
370 addr = start2 - 1;
371 }
372 for (int i = 0; i < to_delete.length(); ++i) tree_.Remove(to_delete[i]);
373}
374
375
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000376CodeEntry* CodeMap::FindEntry(Address addr, Address* start) {
Steve Block6ded16b2010-05-10 14:33:55 +0100377 CodeTree::Locator locator;
378 if (tree_.FindGreatestLessThan(addr, &locator)) {
379 // locator.key() <= addr. Need to check that addr is within entry.
380 const CodeEntryInfo& entry = locator.value();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000381 if (addr < (locator.key() + entry.size)) {
382 if (start) {
383 *start = locator.key();
384 }
Steve Block6ded16b2010-05-10 14:33:55 +0100385 return entry.entry;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000386 }
Steve Block6ded16b2010-05-10 14:33:55 +0100387 }
388 return NULL;
389}
390
391
Steve Block44f0eee2011-05-26 01:26:41 +0100392int CodeMap::GetSharedId(Address addr) {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100393 CodeTree::Locator locator;
Steve Block44f0eee2011-05-26 01:26:41 +0100394 // For shared function entries, 'size' field is used to store their IDs.
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100395 if (tree_.Find(addr, &locator)) {
396 const CodeEntryInfo& entry = locator.value();
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000397 DCHECK(entry.entry == kSharedFunctionCodeEntry);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100398 return entry.size;
399 } else {
400 tree_.Insert(addr, &locator);
Steve Block44f0eee2011-05-26 01:26:41 +0100401 int id = next_shared_id_++;
402 locator.set_value(CodeEntryInfo(kSharedFunctionCodeEntry, id));
403 return id;
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100404 }
405}
406
407
Ben Murdoch589d6972011-11-30 16:04:58 +0000408void CodeMap::MoveCode(Address from, Address to) {
409 if (from == to) return;
410 CodeTree::Locator locator;
411 if (!tree_.Find(from, &locator)) return;
412 CodeEntryInfo entry = locator.value();
413 tree_.Remove(from);
414 AddCode(to, entry.entry, entry.size);
415}
416
417
Steve Block6ded16b2010-05-10 14:33:55 +0100418void CodeMap::CodeTreePrinter::Call(
419 const Address& key, const CodeMap::CodeEntryInfo& value) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000420 // For shared function entries, 'size' field is used to store their IDs.
421 if (value.entry == kSharedFunctionCodeEntry) {
422 base::OS::Print("%p SharedFunctionInfo %d\n", key, value.size);
423 } else {
424 base::OS::Print("%p %5d %s\n", key, value.size, value.entry->name());
425 }
Steve Block6ded16b2010-05-10 14:33:55 +0100426}
427
428
429void CodeMap::Print() {
430 CodeTreePrinter printer;
431 tree_.ForEach(&printer);
432}
433
434
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000435CpuProfilesCollection::CpuProfilesCollection(Heap* heap)
436 : function_and_resource_names_(heap),
437 current_profiles_semaphore_(1) {
Steve Block6ded16b2010-05-10 14:33:55 +0100438}
439
440
Steve Block6ded16b2010-05-10 14:33:55 +0100441static void DeleteCodeEntry(CodeEntry** entry_ptr) {
442 delete *entry_ptr;
443}
444
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000445
Steve Block6ded16b2010-05-10 14:33:55 +0100446static void DeleteCpuProfile(CpuProfile** profile_ptr) {
447 delete *profile_ptr;
448}
449
450
451CpuProfilesCollection::~CpuProfilesCollection() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000452 finished_profiles_.Iterate(DeleteCpuProfile);
Steve Block6ded16b2010-05-10 14:33:55 +0100453 current_profiles_.Iterate(DeleteCpuProfile);
Steve Block6ded16b2010-05-10 14:33:55 +0100454 code_entries_.Iterate(DeleteCodeEntry);
Steve Block6ded16b2010-05-10 14:33:55 +0100455}
456
457
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000458bool CpuProfilesCollection::StartProfiling(const char* title,
459 bool record_samples) {
460 current_profiles_semaphore_.Wait();
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100461 if (current_profiles_.length() >= kMaxSimultaneousProfiles) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000462 current_profiles_semaphore_.Signal();
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100463 return false;
464 }
Steve Block6ded16b2010-05-10 14:33:55 +0100465 for (int i = 0; i < current_profiles_.length(); ++i) {
466 if (strcmp(current_profiles_[i]->title(), title) == 0) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000467 // Ignore attempts to start profile with the same title...
468 current_profiles_semaphore_.Signal();
469 // ... though return true to force it collect a sample.
470 return true;
Steve Block6ded16b2010-05-10 14:33:55 +0100471 }
472 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000473 current_profiles_.Add(new CpuProfile(title, record_samples));
474 current_profiles_semaphore_.Signal();
Steve Block6ded16b2010-05-10 14:33:55 +0100475 return true;
476}
477
478
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000479CpuProfile* CpuProfilesCollection::StopProfiling(const char* title) {
Steve Block6ded16b2010-05-10 14:33:55 +0100480 const int title_len = StrLength(title);
481 CpuProfile* profile = NULL;
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000482 current_profiles_semaphore_.Wait();
Steve Block6ded16b2010-05-10 14:33:55 +0100483 for (int i = current_profiles_.length() - 1; i >= 0; --i) {
484 if (title_len == 0 || strcmp(current_profiles_[i]->title(), title) == 0) {
485 profile = current_profiles_.Remove(i);
486 break;
487 }
488 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000489 current_profiles_semaphore_.Signal();
Steve Block6ded16b2010-05-10 14:33:55 +0100490
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000491 if (profile == NULL) return NULL;
492 profile->CalculateTotalTicksAndSamplingRate();
493 finished_profiles_.Add(profile);
494 return profile;
Steve Block44f0eee2011-05-26 01:26:41 +0100495}
496
497
Iain Merrick75681382010-08-19 15:07:18 +0100498bool CpuProfilesCollection::IsLastProfile(const char* title) {
499 // Called from VM thread, and only it can mutate the list,
500 // so no locking is needed here.
501 if (current_profiles_.length() != 1) return false;
502 return StrLength(title) == 0
503 || strcmp(current_profiles_[0]->title(), title) == 0;
504}
505
506
Steve Block44f0eee2011-05-26 01:26:41 +0100507void CpuProfilesCollection::RemoveProfile(CpuProfile* profile) {
508 // Called from VM thread for a completed profile.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000509 for (int i = 0; i < finished_profiles_.length(); i++) {
510 if (profile == finished_profiles_[i]) {
511 finished_profiles_.Remove(i);
512 return;
Steve Block44f0eee2011-05-26 01:26:41 +0100513 }
514 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000515 UNREACHABLE();
Leon Clarkef7060e22010-06-03 12:02:55 +0100516}
517
518
Steve Block6ded16b2010-05-10 14:33:55 +0100519void CpuProfilesCollection::AddPathToCurrentProfiles(
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000520 base::TimeTicks timestamp, const Vector<CodeEntry*>& path) {
Steve Block6ded16b2010-05-10 14:33:55 +0100521 // As starting / stopping profiles is rare relatively to this
522 // method, we don't bother minimizing the duration of lock holding,
523 // e.g. copying contents of the list to a local vector.
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000524 current_profiles_semaphore_.Wait();
Steve Block6ded16b2010-05-10 14:33:55 +0100525 for (int i = 0; i < current_profiles_.length(); ++i) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000526 current_profiles_[i]->AddPath(timestamp, path);
Steve Block6ded16b2010-05-10 14:33:55 +0100527 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000528 current_profiles_semaphore_.Signal();
Steve Block6ded16b2010-05-10 14:33:55 +0100529}
530
531
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000532CodeEntry* CpuProfilesCollection::NewCodeEntry(
533 Logger::LogEventsAndTags tag,
534 const char* name,
535 const char* name_prefix,
536 const char* resource_name,
537 int line_number,
538 int column_number) {
539 CodeEntry* code_entry = new CodeEntry(tag,
540 name,
541 name_prefix,
542 resource_name,
543 line_number,
544 column_number);
545 code_entries_.Add(code_entry);
546 return code_entry;
Steve Block6ded16b2010-05-10 14:33:55 +0100547}
548
549
Steve Block44f0eee2011-05-26 01:26:41 +0100550const char* const ProfileGenerator::kProgramEntryName =
551 "(program)";
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000552const char* const ProfileGenerator::kIdleEntryName =
553 "(idle)";
Steve Block44f0eee2011-05-26 01:26:41 +0100554const char* const ProfileGenerator::kGarbageCollectorEntryName =
555 "(garbage collector)";
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000556const char* const ProfileGenerator::kUnresolvedFunctionName =
557 "(unresolved function)";
Steve Block6ded16b2010-05-10 14:33:55 +0100558
559
560ProfileGenerator::ProfileGenerator(CpuProfilesCollection* profiles)
561 : profiles_(profiles),
562 program_entry_(
563 profiles->NewCodeEntry(Logger::FUNCTION_TAG, kProgramEntryName)),
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000564 idle_entry_(
565 profiles->NewCodeEntry(Logger::FUNCTION_TAG, kIdleEntryName)),
Steve Block6ded16b2010-05-10 14:33:55 +0100566 gc_entry_(
567 profiles->NewCodeEntry(Logger::BUILTIN_TAG,
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000568 kGarbageCollectorEntryName)),
569 unresolved_entry_(
570 profiles->NewCodeEntry(Logger::FUNCTION_TAG,
571 kUnresolvedFunctionName)) {
Steve Block6ded16b2010-05-10 14:33:55 +0100572}
573
574
575void ProfileGenerator::RecordTickSample(const TickSample& sample) {
576 // Allocate space for stack frames + pc + function + vm-state.
577 ScopedVector<CodeEntry*> entries(sample.frames_count + 3);
578 // As actual number of decoded code entries may vary, initialize
579 // entries vector with NULL values.
580 CodeEntry** entry = entries.start();
581 memset(entry, 0, entries.length() * sizeof(*entry));
582 if (sample.pc != NULL) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000583 if (sample.has_external_callback && sample.state == EXTERNAL &&
584 sample.top_frame_type == StackFrame::EXIT) {
Steve Block44f0eee2011-05-26 01:26:41 +0100585 // Don't use PC when in external callback code, as it can point
586 // inside callback's code, and we will erroneously report
587 // that a callback calls itself.
Steve Block44f0eee2011-05-26 01:26:41 +0100588 *entry++ = code_map_.FindEntry(sample.external_callback);
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000589 } else {
590 Address start;
591 CodeEntry* pc_entry = code_map_.FindEntry(sample.pc, &start);
592 // If pc is in the function code before it set up stack frame or after the
593 // frame was destroyed SafeStackFrameIterator incorrectly thinks that
594 // ebp contains return address of the current function and skips caller's
595 // frame. Check for this case and just skip such samples.
596 if (pc_entry) {
597 List<OffsetRange>* ranges = pc_entry->no_frame_ranges();
598 if (ranges) {
599 Code* code = Code::cast(HeapObject::FromAddress(start));
600 int pc_offset = static_cast<int>(
601 sample.pc - code->instruction_start());
602 for (int i = 0; i < ranges->length(); i++) {
603 OffsetRange& range = ranges->at(i);
604 if (range.from <= pc_offset && pc_offset < range.to) {
605 return;
606 }
607 }
608 }
609 *entry++ = pc_entry;
610
611 if (pc_entry->builtin_id() == Builtins::kFunctionCall ||
612 pc_entry->builtin_id() == Builtins::kFunctionApply) {
613 // When current function is FunctionCall or FunctionApply builtin the
614 // top frame is either frame of the calling JS function or internal
615 // frame. In the latter case we know the caller for sure but in the
616 // former case we don't so we simply replace the frame with
617 // 'unresolved' entry.
618 if (sample.top_frame_type == StackFrame::JAVA_SCRIPT) {
619 *entry++ = unresolved_entry_;
620 }
621 }
Steve Block6ded16b2010-05-10 14:33:55 +0100622 }
Steve Block6ded16b2010-05-10 14:33:55 +0100623 }
624
Ben Murdoch3ef787d2012-04-12 10:51:47 +0100625 for (const Address* stack_pos = sample.stack,
Steve Block6ded16b2010-05-10 14:33:55 +0100626 *stack_end = stack_pos + sample.frames_count;
627 stack_pos != stack_end;
628 ++stack_pos) {
629 *entry++ = code_map_.FindEntry(*stack_pos);
630 }
631 }
632
633 if (FLAG_prof_browser_mode) {
634 bool no_symbolized_entries = true;
635 for (CodeEntry** e = entries.start(); e != entry; ++e) {
636 if (*e != NULL) {
637 no_symbolized_entries = false;
638 break;
639 }
640 }
641 // If no frames were symbolized, put the VM state entry in.
642 if (no_symbolized_entries) {
643 *entry++ = EntryForVMState(sample.state);
644 }
645 }
646
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000647 profiles_->AddPathToCurrentProfiles(sample.timestamp, entries);
Steve Block6ded16b2010-05-10 14:33:55 +0100648}
649
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100650
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000651CodeEntry* ProfileGenerator::EntryForVMState(StateTag tag) {
652 switch (tag) {
653 case GC:
654 return gc_entry_;
655 case JS:
656 case COMPILER:
657 // DOM events handlers are reported as OTHER / EXTERNAL entries.
658 // To avoid confusing people, let's put all these entries into
659 // one bucket.
660 case OTHER:
661 case EXTERNAL:
662 return program_entry_;
663 case IDLE:
664 return idle_entry_;
665 default: return NULL;
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100666 }
667}
668
Steve Block6ded16b2010-05-10 14:33:55 +0100669} } // namespace v8::internal