ulan@chromium.org | 65a89c2 | 2012-02-14 11:46:07 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 2 | // 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.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 29 | |
| 30 | #include "profile-generator-inl.h" |
| 31 | |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 32 | #include "compiler.h" |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 33 | #include "debug.h" |
| 34 | #include "sampler.h" |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 35 | #include "global-handles.h" |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 36 | #include "scopeinfo.h" |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 37 | #include "unicode.h" |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 38 | #include "zone-inl.h" |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 39 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 40 | namespace v8 { |
| 41 | namespace internal { |
| 42 | |
| 43 | |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 44 | StringsStorage::StringsStorage(Heap* heap) |
| 45 | : hash_seed_(heap->HashSeed()), names_(StringsMatch) { |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | |
| 49 | StringsStorage::~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.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | |
| 58 | const 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.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 63 | uint32_t hash = |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 64 | StringHasher::HashSequentialString(dst.start(), len, hash_seed_); |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 65 | return AddOrDisposeString(dst.start(), hash); |
| 66 | } |
| 67 | |
| 68 | |
| 69 | const 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 | |
| 78 | const 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 | |
| 90 | const 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.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 97 | uint32_t hash = StringHasher::HashSequentialString( |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 98 | str.start(), len, hash_seed_); |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 99 | return AddOrDisposeString(str.start(), hash); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 103 | const char* StringsStorage::GetName(Name* name) { |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 104 | if (name->IsString()) { |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 105 | String* str = String::cast(name); |
| 106 | int length = Min(kMaxNameSize, str->length()); |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 107 | SmartArrayPointer<char> data = |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 108 | str->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL, 0, length); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 109 | uint32_t hash = StringHasher::HashSequentialString( |
| 110 | *data, length, name->GetHeap()->HashSeed()); |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 111 | return AddOrDisposeString(data.Detach(), hash); |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 112 | } else if (name->IsSymbol()) { |
| 113 | return "<symbol>"; |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 114 | } |
| 115 | return ""; |
| 116 | } |
| 117 | |
| 118 | |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 119 | const char* StringsStorage::GetName(int index) { |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 120 | return GetFormatted("%d", index); |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 124 | size_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.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 133 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 134 | const char* const CodeEntry::kEmptyNamePrefix = ""; |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 135 | const char* const CodeEntry::kEmptyResourceName = ""; |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 136 | const char* const CodeEntry::kEmptyBailoutReason = ""; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 137 | |
| 138 | |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 139 | CodeEntry::~CodeEntry() { |
| 140 | delete no_frame_ranges_; |
| 141 | } |
| 142 | |
| 143 | |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 144 | uint32_t CodeEntry::GetCallUid() const { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 145 | uint32_t hash = ComputeIntegerHash(tag_, v8::internal::kZeroHashSeed); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 146 | if (shared_id_ != 0) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 147 | hash ^= ComputeIntegerHash(static_cast<uint32_t>(shared_id_), |
| 148 | v8::internal::kZeroHashSeed); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 149 | } else { |
| 150 | hash ^= ComputeIntegerHash( |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 151 | static_cast<uint32_t>(reinterpret_cast<uintptr_t>(name_prefix_)), |
| 152 | v8::internal::kZeroHashSeed); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 153 | hash ^= ComputeIntegerHash( |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 154 | static_cast<uint32_t>(reinterpret_cast<uintptr_t>(name_)), |
| 155 | v8::internal::kZeroHashSeed); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 156 | hash ^= ComputeIntegerHash( |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 157 | static_cast<uint32_t>(reinterpret_cast<uintptr_t>(resource_name_)), |
| 158 | v8::internal::kZeroHashSeed); |
| 159 | hash ^= ComputeIntegerHash(line_number_, v8::internal::kZeroHashSeed); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 160 | } |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 161 | return hash; |
| 162 | } |
| 163 | |
| 164 | |
| 165 | bool CodeEntry::IsSameAs(CodeEntry* entry) const { |
| 166 | return this == entry |
| 167 | || (tag_ == entry->tag_ |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 168 | && 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.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 177 | void CodeEntry::SetBuiltinId(Builtins::Name id) { |
| 178 | tag_ = Logger::BUILTIN_TAG; |
| 179 | builtin_id_ = id; |
| 180 | } |
| 181 | |
| 182 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 183 | ProfileNode* 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 | |
| 191 | ProfileNode* 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.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 196 | ProfileNode* new_node = new ProfileNode(tree_, entry); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 197 | map_entry->value = new_node; |
| 198 | children_list_.Add(new_node); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 199 | } |
| 200 | return reinterpret_cast<ProfileNode*>(map_entry->value); |
| 201 | } |
| 202 | |
| 203 | |
| 204 | void ProfileNode::Print(int indent) { |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 205 | OS::Print("%5u %*c %s%s %d #%d %s", |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 206 | self_ticks_, |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 207 | indent, ' ', |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 208 | entry_->name_prefix(), |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 209 | entry_->name(), |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 210 | entry_->script_id(), |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 211 | id(), |
| 212 | entry_->bailout_reason()); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 213 | if (entry_->resource_name()[0] != '\0') |
| 214 | OS::Print(" %s:%d", entry_->resource_name(), entry_->line_number()); |
| 215 | OS::Print("\n"); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 216 | 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.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 224 | class DeleteNodesCallback { |
| 225 | public: |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 226 | void BeforeTraversingChild(ProfileNode*, ProfileNode*) { } |
| 227 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 228 | void AfterAllChildrenTraversed(ProfileNode* node) { |
| 229 | delete node; |
| 230 | } |
| 231 | |
| 232 | void AfterChildTraversed(ProfileNode*, ProfileNode*) { } |
| 233 | }; |
| 234 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 235 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 236 | ProfileTree::ProfileTree() |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 237 | : root_entry_(Logger::FUNCTION_TAG, "(root)"), |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 238 | next_node_id_(1), |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 239 | root_(new ProfileNode(this, &root_entry_)) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 243 | ProfileTree::~ProfileTree() { |
| 244 | DeleteNodesCallback cb; |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 245 | TraverseDepthFirst(&cb); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 249 | ProfileNode* ProfileTree::AddPathFromEnd(const Vector<CodeEntry*>& path) { |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 250 | 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.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 259 | return node; |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | |
| 263 | void 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.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 276 | struct NodesPair { |
| 277 | NodesPair(ProfileNode* src, ProfileNode* dst) |
| 278 | : src(src), dst(dst) { } |
| 279 | ProfileNode* src; |
| 280 | ProfileNode* dst; |
| 281 | }; |
| 282 | |
| 283 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 284 | class Position { |
| 285 | public: |
| 286 | explicit Position(ProfileNode* node) |
| 287 | : node(node), child_idx_(0) { } |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 288 | INLINE(ProfileNode* current_child()) { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 289 | return node->children()->at(child_idx_); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 290 | } |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 291 | INLINE(bool has_current_child()) { |
| 292 | return child_idx_ < node->children()->length(); |
| 293 | } |
| 294 | INLINE(void next_child()) { ++child_idx_; } |
| 295 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 296 | ProfileNode* node; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 297 | private: |
| 298 | int child_idx_; |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 299 | }; |
| 300 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 301 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 302 | // Non-recursive implementation of a depth-first post-order tree traversal. |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 303 | template <typename Callback> |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 304 | void ProfileTree::TraverseDepthFirst(Callback* callback) { |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 305 | List<Position> stack(10); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 306 | stack.Add(Position(root_)); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 307 | while (stack.length() > 0) { |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 308 | Position& current = stack.last(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 309 | if (current.has_current_child()) { |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 310 | callback->BeforeTraversingChild(current.node, current.current_child()); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 311 | stack.Add(Position(current.current_child())); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 312 | } 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.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 317 | parent.next_child(); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 318 | } |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 319 | // Remove child from the stack. |
| 320 | stack.RemoveLast(); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 321 | } |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 322 | } |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 326 | CpuProfile::CpuProfile(const char* title, unsigned uid, bool record_samples) |
| 327 | : title_(title), |
| 328 | uid_(uid), |
| 329 | record_samples_(record_samples), |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 330 | start_time_(Time::NowFromSystemTime()) { |
| 331 | timer_.Start(); |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 332 | } |
| 333 | |
| 334 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 335 | void CpuProfile::AddPath(const Vector<CodeEntry*>& path) { |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 336 | ProfileNode* top_frame_node = top_down_.AddPathFromEnd(path); |
| 337 | if (record_samples_) samples_.Add(top_frame_node); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 338 | } |
| 339 | |
| 340 | |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 341 | void CpuProfile::CalculateTotalTicksAndSamplingRate() { |
jkummerow@chromium.org | dc94e19 | 2013-08-30 11:35:42 +0000 | [diff] [blame] | 342 | end_time_ = start_time_ + timer_.Elapsed(); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | |
| 346 | void CpuProfile::Print() { |
| 347 | OS::Print("[Top down]:\n"); |
| 348 | top_down_.Print(); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 352 | CodeEntry* const CodeMap::kSharedFunctionCodeEntry = NULL; |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 353 | const CodeMap::CodeTreeConfig::Key CodeMap::CodeTreeConfig::kNoKey = NULL; |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 354 | |
| 355 | |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 356 | void 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 | |
| 364 | void 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.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 378 | CodeEntry* CodeMap::FindEntry(Address addr, Address* start) { |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 379 | 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.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 383 | if (addr < (locator.key() + entry.size)) { |
| 384 | if (start) { |
| 385 | *start = locator.key(); |
| 386 | } |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 387 | return entry.entry; |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 388 | } |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 389 | } |
| 390 | return NULL; |
| 391 | } |
| 392 | |
| 393 | |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 394 | int CodeMap::GetSharedId(Address addr) { |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 395 | CodeTree::Locator locator; |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 396 | // For shared function entries, 'size' field is used to store their IDs. |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 397 | if (tree_.Find(addr, &locator)) { |
| 398 | const CodeEntryInfo& entry = locator.value(); |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 399 | ASSERT(entry.entry == kSharedFunctionCodeEntry); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 400 | return entry.size; |
| 401 | } else { |
| 402 | tree_.Insert(addr, &locator); |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 403 | int id = next_shared_id_++; |
| 404 | locator.set_value(CodeEntryInfo(kSharedFunctionCodeEntry, id)); |
| 405 | return id; |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | |
| 409 | |
lrn@chromium.org | 34e6078 | 2011-09-15 07:25:40 +0000 | [diff] [blame] | 410 | void 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.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 420 | void CodeMap::CodeTreePrinter::Call( |
| 421 | const Address& key, const CodeMap::CodeEntryInfo& value) { |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 422 | // 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.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | |
| 431 | void CodeMap::Print() { |
| 432 | CodeTreePrinter printer; |
| 433 | tree_.ForEach(&printer); |
| 434 | } |
| 435 | |
| 436 | |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 437 | CpuProfilesCollection::CpuProfilesCollection(Heap* heap) |
| 438 | : function_and_resource_names_(heap), |
| 439 | current_profiles_semaphore_(1) { |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 443 | static void DeleteCodeEntry(CodeEntry** entry_ptr) { |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 444 | delete *entry_ptr; |
| 445 | } |
| 446 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 447 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 448 | static void DeleteCpuProfile(CpuProfile** profile_ptr) { |
| 449 | delete *profile_ptr; |
| 450 | } |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 451 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 452 | |
| 453 | CpuProfilesCollection::~CpuProfilesCollection() { |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 454 | finished_profiles_.Iterate(DeleteCpuProfile); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 455 | current_profiles_.Iterate(DeleteCpuProfile); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 456 | code_entries_.Iterate(DeleteCodeEntry); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 460 | bool CpuProfilesCollection::StartProfiling(const char* title, unsigned uid, |
| 461 | bool record_samples) { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 462 | ASSERT(uid > 0); |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 463 | current_profiles_semaphore_.Wait(); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 464 | if (current_profiles_.length() >= kMaxSimultaneousProfiles) { |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 465 | current_profiles_semaphore_.Signal(); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 466 | return false; |
| 467 | } |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 468 | 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.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 471 | current_profiles_semaphore_.Signal(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 472 | return false; |
| 473 | } |
| 474 | } |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 475 | current_profiles_.Add(new CpuProfile(title, uid, record_samples)); |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 476 | current_profiles_semaphore_.Signal(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 477 | return true; |
| 478 | } |
| 479 | |
| 480 | |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 481 | CpuProfile* CpuProfilesCollection::StopProfiling(const char* title) { |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 482 | const int title_len = StrLength(title); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 483 | CpuProfile* profile = NULL; |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 484 | current_profiles_semaphore_.Wait(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 485 | 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.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 491 | current_profiles_semaphore_.Signal(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 492 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 493 | if (profile == NULL) return NULL; |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 494 | profile->CalculateTotalTicksAndSamplingRate(); |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 495 | finished_profiles_.Add(profile); |
| 496 | return profile; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 500 | bool 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.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 509 | void CpuProfilesCollection::RemoveProfile(CpuProfile* profile) { |
| 510 | // Called from VM thread for a completed profile. |
| 511 | unsigned uid = profile->uid(); |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 512 | 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.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 516 | } |
| 517 | } |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 518 | UNREACHABLE(); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 522 | void 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.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 527 | current_profiles_semaphore_.Wait(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 528 | for (int i = 0; i < current_profiles_.length(); ++i) { |
| 529 | current_profiles_[i]->AddPath(path); |
| 530 | } |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 531 | current_profiles_semaphore_.Signal(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 535 | CodeEntry* CpuProfilesCollection::NewCodeEntry( |
| 536 | Logger::LogEventsAndTags tag, |
| 537 | const char* name, |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 538 | const char* name_prefix, |
| 539 | const char* resource_name, |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 540 | int line_number, |
| 541 | int column_number) { |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 542 | CodeEntry* code_entry = new CodeEntry(tag, |
| 543 | name, |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 544 | name_prefix, |
| 545 | resource_name, |
mvstanton@chromium.org | dd6d9ee | 2013-10-11 10:35:37 +0000 | [diff] [blame] | 546 | line_number, |
| 547 | column_number); |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 548 | code_entries_.Add(code_entry); |
| 549 | return code_entry; |
| 550 | } |
| 551 | |
| 552 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 553 | const char* const ProfileGenerator::kAnonymousFunctionName = |
| 554 | "(anonymous function)"; |
| 555 | const char* const ProfileGenerator::kProgramEntryName = |
| 556 | "(program)"; |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 557 | const char* const ProfileGenerator::kIdleEntryName = |
| 558 | "(idle)"; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 559 | const char* const ProfileGenerator::kGarbageCollectorEntryName = |
| 560 | "(garbage collector)"; |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 561 | const char* const ProfileGenerator::kUnresolvedFunctionName = |
| 562 | "(unresolved function)"; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 563 | |
| 564 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 565 | ProfileGenerator::ProfileGenerator(CpuProfilesCollection* profiles) |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 566 | : profiles_(profiles), |
| 567 | program_entry_( |
| 568 | profiles->NewCodeEntry(Logger::FUNCTION_TAG, kProgramEntryName)), |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 569 | idle_entry_( |
| 570 | profiles->NewCodeEntry(Logger::FUNCTION_TAG, kIdleEntryName)), |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 571 | gc_entry_( |
| 572 | profiles->NewCodeEntry(Logger::BUILTIN_TAG, |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 573 | kGarbageCollectorEntryName)), |
| 574 | unresolved_entry_( |
| 575 | profiles->NewCodeEntry(Logger::FUNCTION_TAG, |
| 576 | kUnresolvedFunctionName)) { |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | |
| 580 | void ProfileGenerator::RecordTickSample(const TickSample& sample) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 581 | // 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.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 585 | CodeEntry** entry = entries.start(); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 586 | memset(entry, 0, entries.length() * sizeof(*entry)); |
| 587 | if (sample.pc != NULL) { |
yangguo@chromium.org | c73d55b | 2013-07-24 08:18:28 +0000 | [diff] [blame] | 588 | if (sample.has_external_callback && sample.state == EXTERNAL && |
| 589 | sample.top_frame_type == StackFrame::EXIT) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 590 | // 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.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 593 | *entry++ = code_map_.FindEntry(sample.external_callback); |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 594 | } 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.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 616 | 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.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 626 | } |
| 627 | } |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 628 | } |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 629 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 630 | for (const Address* stack_pos = sample.stack, |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 631 | *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.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 636 | } |
| 637 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 638 | if (FLAG_prof_browser_mode) { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 639 | 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.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 650 | } |
| 651 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 652 | profiles_->AddPathToCurrentProfiles(entries); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 653 | } |
| 654 | |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 655 | |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 656 | CodeEntry* 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.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 674 | } } // namespace v8::internal |