fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 1 | // Copyright 2010 the V8 project authors. All rights reserved. |
| 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 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 28 | #ifdef ENABLE_LOGGING_AND_PROFILING |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 29 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 30 | #include "v8.h" |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 31 | #include "global-handles.h" |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 32 | #include "scopeinfo.h" |
| 33 | #include "top.h" |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 34 | #include "unicode.h" |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 35 | #include "zone-inl.h" |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 36 | |
| 37 | #include "profile-generator-inl.h" |
| 38 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 39 | namespace v8 { |
| 40 | namespace internal { |
| 41 | |
| 42 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 43 | TokenEnumerator::TokenEnumerator() |
| 44 | : token_locations_(4), |
| 45 | token_removed_(4) { |
| 46 | } |
| 47 | |
| 48 | |
| 49 | TokenEnumerator::~TokenEnumerator() { |
| 50 | for (int i = 0; i < token_locations_.length(); ++i) { |
| 51 | if (!token_removed_[i]) { |
| 52 | GlobalHandles::ClearWeakness(token_locations_[i]); |
| 53 | GlobalHandles::Destroy(token_locations_[i]); |
| 54 | } |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | |
| 59 | int TokenEnumerator::GetTokenId(Object* token) { |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 60 | if (token == NULL) return TokenEnumerator::kNoSecurityToken; |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 61 | for (int i = 0; i < token_locations_.length(); ++i) { |
| 62 | if (*token_locations_[i] == token && !token_removed_[i]) return i; |
| 63 | } |
| 64 | Handle<Object> handle = GlobalHandles::Create(token); |
| 65 | // handle.location() points to a memory cell holding a pointer |
| 66 | // to a token object in the V8's heap. |
| 67 | GlobalHandles::MakeWeak(handle.location(), this, TokenRemovedCallback); |
| 68 | token_locations_.Add(handle.location()); |
| 69 | token_removed_.Add(false); |
| 70 | return token_locations_.length() - 1; |
| 71 | } |
| 72 | |
| 73 | |
| 74 | void TokenEnumerator::TokenRemovedCallback(v8::Persistent<v8::Value> handle, |
| 75 | void* parameter) { |
| 76 | reinterpret_cast<TokenEnumerator*>(parameter)->TokenRemoved( |
| 77 | Utils::OpenHandle(*handle).location()); |
fschneider@chromium.org | ed78ffd | 2010-07-21 11:05:19 +0000 | [diff] [blame] | 78 | handle.Dispose(); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | |
| 82 | void TokenEnumerator::TokenRemoved(Object** token_location) { |
| 83 | for (int i = 0; i < token_locations_.length(); ++i) { |
| 84 | if (token_locations_[i] == token_location && !token_removed_[i]) { |
| 85 | token_removed_[i] = true; |
| 86 | return; |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 92 | StringsStorage::StringsStorage() |
| 93 | : names_(StringsMatch) { |
| 94 | } |
| 95 | |
| 96 | |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 97 | static void DeleteIndexName(char** name_ptr) { |
| 98 | DeleteArray(*name_ptr); |
| 99 | } |
| 100 | |
| 101 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 102 | StringsStorage::~StringsStorage() { |
| 103 | for (HashMap::Entry* p = names_.Start(); |
| 104 | p != NULL; |
| 105 | p = names_.Next(p)) { |
| 106 | DeleteArray(reinterpret_cast<const char*>(p->value)); |
| 107 | } |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 108 | index_names_.Iterate(DeleteIndexName); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | |
| 112 | const char* StringsStorage::GetName(String* name) { |
| 113 | if (name->IsString()) { |
| 114 | char* c_name = |
| 115 | name->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL).Detach(); |
| 116 | HashMap::Entry* cache_entry = names_.Lookup(c_name, name->Hash(), true); |
| 117 | if (cache_entry->value == NULL) { |
| 118 | // New entry added. |
| 119 | cache_entry->value = c_name; |
| 120 | } else { |
| 121 | DeleteArray(c_name); |
| 122 | } |
| 123 | return reinterpret_cast<const char*>(cache_entry->value); |
| 124 | } |
| 125 | return ""; |
| 126 | } |
| 127 | |
| 128 | |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 129 | const char* StringsStorage::GetName(int index) { |
| 130 | ASSERT(index >= 0); |
| 131 | if (index_names_.length() <= index) { |
| 132 | index_names_.AddBlock( |
| 133 | NULL, index - index_names_.length() + 1); |
| 134 | } |
| 135 | if (index_names_[index] == NULL) { |
| 136 | const int kMaximumNameLength = 32; |
| 137 | char* name = NewArray<char>(kMaximumNameLength); |
| 138 | OS::SNPrintF(Vector<char>(name, kMaximumNameLength), "%d", index); |
| 139 | index_names_[index] = name; |
| 140 | } |
| 141 | return index_names_[index]; |
| 142 | } |
| 143 | |
| 144 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 145 | const char* CodeEntry::kEmptyNamePrefix = ""; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 146 | |
| 147 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 148 | void CodeEntry::CopyData(const CodeEntry& source) { |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 149 | tag_ = source.tag_; |
| 150 | name_prefix_ = source.name_prefix_; |
| 151 | name_ = source.name_; |
| 152 | resource_name_ = source.resource_name_; |
| 153 | line_number_ = source.line_number_; |
| 154 | } |
| 155 | |
| 156 | |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 157 | uint32_t CodeEntry::GetCallUid() const { |
| 158 | uint32_t hash = ComputeIntegerHash(tag_); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 159 | hash ^= ComputeIntegerHash( |
| 160 | static_cast<uint32_t>(reinterpret_cast<uintptr_t>(name_prefix_))); |
| 161 | hash ^= ComputeIntegerHash( |
| 162 | static_cast<uint32_t>(reinterpret_cast<uintptr_t>(name_))); |
| 163 | hash ^= ComputeIntegerHash( |
| 164 | static_cast<uint32_t>(reinterpret_cast<uintptr_t>(resource_name_))); |
| 165 | hash ^= ComputeIntegerHash(line_number_); |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 166 | return hash; |
| 167 | } |
| 168 | |
| 169 | |
| 170 | bool CodeEntry::IsSameAs(CodeEntry* entry) const { |
| 171 | return this == entry |
| 172 | || (tag_ == entry->tag_ |
| 173 | && name_prefix_ == entry->name_prefix_ |
| 174 | && name_ == entry->name_ |
| 175 | && resource_name_ == entry->resource_name_ |
| 176 | && line_number_ == entry->line_number_); |
| 177 | } |
| 178 | |
| 179 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 180 | ProfileNode* ProfileNode::FindChild(CodeEntry* entry) { |
| 181 | HashMap::Entry* map_entry = |
| 182 | children_.Lookup(entry, CodeEntryHash(entry), false); |
| 183 | return map_entry != NULL ? |
| 184 | reinterpret_cast<ProfileNode*>(map_entry->value) : NULL; |
| 185 | } |
| 186 | |
| 187 | |
| 188 | ProfileNode* ProfileNode::FindOrAddChild(CodeEntry* entry) { |
| 189 | HashMap::Entry* map_entry = |
| 190 | children_.Lookup(entry, CodeEntryHash(entry), true); |
| 191 | if (map_entry->value == NULL) { |
| 192 | // New node added. |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 193 | ProfileNode* new_node = new ProfileNode(tree_, entry); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 194 | map_entry->value = new_node; |
| 195 | children_list_.Add(new_node); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 196 | } |
| 197 | return reinterpret_cast<ProfileNode*>(map_entry->value); |
| 198 | } |
| 199 | |
| 200 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 201 | double ProfileNode::GetSelfMillis() const { |
| 202 | return tree_->TicksToMillis(self_ticks_); |
| 203 | } |
| 204 | |
| 205 | |
| 206 | double ProfileNode::GetTotalMillis() const { |
| 207 | return tree_->TicksToMillis(total_ticks_); |
| 208 | } |
| 209 | |
| 210 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 211 | void ProfileNode::Print(int indent) { |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 212 | OS::Print("%5u %5u %*c %s%s [%d]", |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 213 | total_ticks_, self_ticks_, |
| 214 | indent, ' ', |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 215 | entry_->name_prefix(), |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 216 | entry_->name(), |
| 217 | entry_->security_token_id()); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 218 | if (entry_->resource_name()[0] != '\0') |
| 219 | OS::Print(" %s:%d", entry_->resource_name(), entry_->line_number()); |
| 220 | OS::Print("\n"); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 221 | for (HashMap::Entry* p = children_.Start(); |
| 222 | p != NULL; |
| 223 | p = children_.Next(p)) { |
| 224 | reinterpret_cast<ProfileNode*>(p->value)->Print(indent + 2); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 229 | class DeleteNodesCallback { |
| 230 | public: |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 231 | void BeforeTraversingChild(ProfileNode*, ProfileNode*) { } |
| 232 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 233 | void AfterAllChildrenTraversed(ProfileNode* node) { |
| 234 | delete node; |
| 235 | } |
| 236 | |
| 237 | void AfterChildTraversed(ProfileNode*, ProfileNode*) { } |
| 238 | }; |
| 239 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 240 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 241 | ProfileTree::ProfileTree() |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 242 | : root_entry_(Logger::FUNCTION_TAG, |
| 243 | "", |
| 244 | "(root)", |
| 245 | "", |
| 246 | 0, |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 247 | TokenEnumerator::kNoSecurityToken), |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 248 | root_(new ProfileNode(this, &root_entry_)) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 249 | } |
| 250 | |
| 251 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 252 | ProfileTree::~ProfileTree() { |
| 253 | DeleteNodesCallback cb; |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 254 | TraverseDepthFirst(&cb); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | |
| 258 | void ProfileTree::AddPathFromEnd(const Vector<CodeEntry*>& path) { |
| 259 | ProfileNode* node = root_; |
| 260 | for (CodeEntry** entry = path.start() + path.length() - 1; |
| 261 | entry != path.start() - 1; |
| 262 | --entry) { |
| 263 | if (*entry != NULL) { |
| 264 | node = node->FindOrAddChild(*entry); |
| 265 | } |
| 266 | } |
| 267 | node->IncrementSelfTicks(); |
| 268 | } |
| 269 | |
| 270 | |
| 271 | void ProfileTree::AddPathFromStart(const Vector<CodeEntry*>& path) { |
| 272 | ProfileNode* node = root_; |
| 273 | for (CodeEntry** entry = path.start(); |
| 274 | entry != path.start() + path.length(); |
| 275 | ++entry) { |
| 276 | if (*entry != NULL) { |
| 277 | node = node->FindOrAddChild(*entry); |
| 278 | } |
| 279 | } |
| 280 | node->IncrementSelfTicks(); |
| 281 | } |
| 282 | |
| 283 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 284 | struct NodesPair { |
| 285 | NodesPair(ProfileNode* src, ProfileNode* dst) |
| 286 | : src(src), dst(dst) { } |
| 287 | ProfileNode* src; |
| 288 | ProfileNode* dst; |
| 289 | }; |
| 290 | |
| 291 | |
| 292 | class FilteredCloneCallback { |
| 293 | public: |
| 294 | explicit FilteredCloneCallback(ProfileNode* dst_root, int security_token_id) |
| 295 | : stack_(10), |
| 296 | security_token_id_(security_token_id) { |
| 297 | stack_.Add(NodesPair(NULL, dst_root)); |
| 298 | } |
| 299 | |
| 300 | void BeforeTraversingChild(ProfileNode* parent, ProfileNode* child) { |
| 301 | if (IsTokenAcceptable(child->entry()->security_token_id(), |
| 302 | parent->entry()->security_token_id())) { |
| 303 | ProfileNode* clone = stack_.last().dst->FindOrAddChild(child->entry()); |
| 304 | clone->IncreaseSelfTicks(child->self_ticks()); |
| 305 | stack_.Add(NodesPair(child, clone)); |
| 306 | } else { |
| 307 | // Attribute ticks to parent node. |
| 308 | stack_.last().dst->IncreaseSelfTicks(child->self_ticks()); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | void AfterAllChildrenTraversed(ProfileNode* parent) { } |
| 313 | |
| 314 | void AfterChildTraversed(ProfileNode*, ProfileNode* child) { |
| 315 | if (stack_.last().src == child) { |
| 316 | stack_.RemoveLast(); |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | private: |
| 321 | bool IsTokenAcceptable(int token, int parent_token) { |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 322 | if (token == TokenEnumerator::kNoSecurityToken |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 323 | || token == security_token_id_) return true; |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 324 | if (token == TokenEnumerator::kInheritsSecurityToken) { |
| 325 | ASSERT(parent_token != TokenEnumerator::kInheritsSecurityToken); |
| 326 | return parent_token == TokenEnumerator::kNoSecurityToken |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 327 | || parent_token == security_token_id_; |
| 328 | } |
| 329 | return false; |
| 330 | } |
| 331 | |
| 332 | List<NodesPair> stack_; |
| 333 | int security_token_id_; |
| 334 | }; |
| 335 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 336 | void ProfileTree::FilteredClone(ProfileTree* src, int security_token_id) { |
| 337 | ms_to_ticks_scale_ = src->ms_to_ticks_scale_; |
| 338 | FilteredCloneCallback cb(root_, security_token_id); |
| 339 | src->TraverseDepthFirst(&cb); |
| 340 | CalculateTotalTicks(); |
| 341 | } |
| 342 | |
| 343 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 344 | void ProfileTree::SetTickRatePerMs(double ticks_per_ms) { |
| 345 | ms_to_ticks_scale_ = ticks_per_ms > 0 ? 1.0 / ticks_per_ms : 1.0; |
| 346 | } |
| 347 | |
| 348 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 349 | class Position { |
| 350 | public: |
| 351 | explicit Position(ProfileNode* node) |
| 352 | : node(node), child_idx_(0) { } |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 353 | INLINE(ProfileNode* current_child()) { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 354 | return node->children()->at(child_idx_); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 355 | } |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 356 | INLINE(bool has_current_child()) { |
| 357 | return child_idx_ < node->children()->length(); |
| 358 | } |
| 359 | INLINE(void next_child()) { ++child_idx_; } |
| 360 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 361 | ProfileNode* node; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 362 | private: |
| 363 | int child_idx_; |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 364 | }; |
| 365 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 366 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 367 | // Non-recursive implementation of a depth-first post-order tree traversal. |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 368 | template <typename Callback> |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 369 | void ProfileTree::TraverseDepthFirst(Callback* callback) { |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 370 | List<Position> stack(10); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 371 | stack.Add(Position(root_)); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 372 | while (stack.length() > 0) { |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 373 | Position& current = stack.last(); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 374 | if (current.has_current_child()) { |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 375 | callback->BeforeTraversingChild(current.node, current.current_child()); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 376 | stack.Add(Position(current.current_child())); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 377 | } else { |
| 378 | callback->AfterAllChildrenTraversed(current.node); |
| 379 | if (stack.length() > 1) { |
| 380 | Position& parent = stack[stack.length() - 2]; |
| 381 | callback->AfterChildTraversed(parent.node, current.node); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 382 | parent.next_child(); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 383 | } |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 384 | // Remove child from the stack. |
| 385 | stack.RemoveLast(); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 386 | } |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 387 | } |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 391 | class CalculateTotalTicksCallback { |
| 392 | public: |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 393 | void BeforeTraversingChild(ProfileNode*, ProfileNode*) { } |
| 394 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 395 | void AfterAllChildrenTraversed(ProfileNode* node) { |
| 396 | node->IncreaseTotalTicks(node->self_ticks()); |
| 397 | } |
| 398 | |
| 399 | void AfterChildTraversed(ProfileNode* parent, ProfileNode* child) { |
| 400 | parent->IncreaseTotalTicks(child->total_ticks()); |
| 401 | } |
| 402 | }; |
| 403 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 404 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 405 | void ProfileTree::CalculateTotalTicks() { |
| 406 | CalculateTotalTicksCallback cb; |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 407 | TraverseDepthFirst(&cb); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | |
| 411 | void ProfileTree::ShortPrint() { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 412 | OS::Print("root: %u %u %.2fms %.2fms\n", |
| 413 | root_->total_ticks(), root_->self_ticks(), |
| 414 | root_->GetTotalMillis(), root_->GetSelfMillis()); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | |
| 418 | void CpuProfile::AddPath(const Vector<CodeEntry*>& path) { |
| 419 | top_down_.AddPathFromEnd(path); |
| 420 | bottom_up_.AddPathFromStart(path); |
| 421 | } |
| 422 | |
| 423 | |
| 424 | void CpuProfile::CalculateTotalTicks() { |
| 425 | top_down_.CalculateTotalTicks(); |
| 426 | bottom_up_.CalculateTotalTicks(); |
| 427 | } |
| 428 | |
| 429 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 430 | void CpuProfile::SetActualSamplingRate(double actual_sampling_rate) { |
| 431 | top_down_.SetTickRatePerMs(actual_sampling_rate); |
| 432 | bottom_up_.SetTickRatePerMs(actual_sampling_rate); |
| 433 | } |
| 434 | |
| 435 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 436 | CpuProfile* CpuProfile::FilteredClone(int security_token_id) { |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 437 | ASSERT(security_token_id != TokenEnumerator::kNoSecurityToken); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 438 | CpuProfile* clone = new CpuProfile(title_, uid_); |
| 439 | clone->top_down_.FilteredClone(&top_down_, security_token_id); |
| 440 | clone->bottom_up_.FilteredClone(&bottom_up_, security_token_id); |
| 441 | return clone; |
| 442 | } |
| 443 | |
| 444 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 445 | void CpuProfile::ShortPrint() { |
| 446 | OS::Print("top down "); |
| 447 | top_down_.ShortPrint(); |
| 448 | OS::Print("bottom up "); |
| 449 | bottom_up_.ShortPrint(); |
| 450 | } |
| 451 | |
| 452 | |
| 453 | void CpuProfile::Print() { |
| 454 | OS::Print("[Top down]:\n"); |
| 455 | top_down_.Print(); |
| 456 | OS::Print("[Bottom up]:\n"); |
| 457 | bottom_up_.Print(); |
| 458 | } |
| 459 | |
| 460 | |
| 461 | const CodeMap::CodeTreeConfig::Key CodeMap::CodeTreeConfig::kNoKey = NULL; |
| 462 | const CodeMap::CodeTreeConfig::Value CodeMap::CodeTreeConfig::kNoValue = |
| 463 | CodeMap::CodeEntryInfo(NULL, 0); |
| 464 | |
| 465 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 466 | void CodeMap::AddAlias(Address start, CodeEntry* entry, Address code_start) { |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 467 | CodeTree::Locator locator; |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 468 | if (tree_.Find(code_start, &locator)) { |
| 469 | const CodeEntryInfo& code_info = locator.value(); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 470 | if (tree_.Insert(start, &locator)) { |
| 471 | entry->CopyData(*code_info.entry); |
| 472 | locator.set_value(CodeEntryInfo(entry, code_info.size)); |
| 473 | } |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 474 | } |
| 475 | } |
| 476 | |
| 477 | |
| 478 | CodeEntry* CodeMap::FindEntry(Address addr) { |
| 479 | CodeTree::Locator locator; |
| 480 | if (tree_.FindGreatestLessThan(addr, &locator)) { |
| 481 | // locator.key() <= addr. Need to check that addr is within entry. |
| 482 | const CodeEntryInfo& entry = locator.value(); |
| 483 | if (addr < (locator.key() + entry.size)) |
| 484 | return entry.entry; |
| 485 | } |
| 486 | return NULL; |
| 487 | } |
| 488 | |
| 489 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 490 | void CodeMap::CodeTreePrinter::Call( |
| 491 | const Address& key, const CodeMap::CodeEntryInfo& value) { |
| 492 | OS::Print("%p %5d %s\n", key, value.size, value.entry->name()); |
| 493 | } |
| 494 | |
| 495 | |
| 496 | void CodeMap::Print() { |
| 497 | CodeTreePrinter printer; |
| 498 | tree_.ForEach(&printer); |
| 499 | } |
| 500 | |
| 501 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 502 | CpuProfilesCollection::CpuProfilesCollection() |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 503 | : profiles_uids_(UidsMatch), |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 504 | current_profiles_semaphore_(OS::CreateSemaphore(1)) { |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 505 | // Create list of unabridged profiles. |
| 506 | profiles_by_token_.Add(new List<CpuProfile*>()); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 510 | static void DeleteCodeEntry(CodeEntry** entry_ptr) { |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 511 | delete *entry_ptr; |
| 512 | } |
| 513 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 514 | static void DeleteCpuProfile(CpuProfile** profile_ptr) { |
| 515 | delete *profile_ptr; |
| 516 | } |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 517 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 518 | static void DeleteProfilesList(List<CpuProfile*>** list_ptr) { |
| 519 | (*list_ptr)->Iterate(DeleteCpuProfile); |
| 520 | delete *list_ptr; |
| 521 | } |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 522 | |
| 523 | CpuProfilesCollection::~CpuProfilesCollection() { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 524 | delete current_profiles_semaphore_; |
| 525 | current_profiles_.Iterate(DeleteCpuProfile); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 526 | profiles_by_token_.Iterate(DeleteProfilesList); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 527 | code_entries_.Iterate(DeleteCodeEntry); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 531 | bool CpuProfilesCollection::StartProfiling(const char* title, unsigned uid) { |
| 532 | ASSERT(uid > 0); |
| 533 | current_profiles_semaphore_->Wait(); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 534 | if (current_profiles_.length() >= kMaxSimultaneousProfiles) { |
| 535 | current_profiles_semaphore_->Signal(); |
| 536 | return false; |
| 537 | } |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 538 | for (int i = 0; i < current_profiles_.length(); ++i) { |
| 539 | if (strcmp(current_profiles_[i]->title(), title) == 0) { |
| 540 | // Ignore attempts to start profile with the same title. |
| 541 | current_profiles_semaphore_->Signal(); |
| 542 | return false; |
| 543 | } |
| 544 | } |
| 545 | current_profiles_.Add(new CpuProfile(title, uid)); |
| 546 | current_profiles_semaphore_->Signal(); |
| 547 | return true; |
| 548 | } |
| 549 | |
| 550 | |
| 551 | bool CpuProfilesCollection::StartProfiling(String* title, unsigned uid) { |
| 552 | return StartProfiling(GetName(title), uid); |
| 553 | } |
| 554 | |
| 555 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 556 | CpuProfile* CpuProfilesCollection::StopProfiling(int security_token_id, |
| 557 | const char* title, |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 558 | double actual_sampling_rate) { |
whesse@chromium.org | b6e43bb | 2010-04-14 09:36:28 +0000 | [diff] [blame] | 559 | const int title_len = StrLength(title); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 560 | CpuProfile* profile = NULL; |
| 561 | current_profiles_semaphore_->Wait(); |
| 562 | for (int i = current_profiles_.length() - 1; i >= 0; --i) { |
| 563 | if (title_len == 0 || strcmp(current_profiles_[i]->title(), title) == 0) { |
| 564 | profile = current_profiles_.Remove(i); |
| 565 | break; |
| 566 | } |
| 567 | } |
| 568 | current_profiles_semaphore_->Signal(); |
| 569 | |
| 570 | if (profile != NULL) { |
| 571 | profile->CalculateTotalTicks(); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 572 | profile->SetActualSamplingRate(actual_sampling_rate); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 573 | List<CpuProfile*>* unabridged_list = |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 574 | profiles_by_token_[TokenToIndex(TokenEnumerator::kNoSecurityToken)]; |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 575 | unabridged_list->Add(profile); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 576 | HashMap::Entry* entry = |
| 577 | profiles_uids_.Lookup(reinterpret_cast<void*>(profile->uid()), |
| 578 | static_cast<uint32_t>(profile->uid()), |
| 579 | true); |
| 580 | ASSERT(entry->value == NULL); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 581 | entry->value = reinterpret_cast<void*>(unabridged_list->length() - 1); |
| 582 | return GetProfile(security_token_id, profile->uid()); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 583 | } |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 584 | return NULL; |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 588 | CpuProfile* CpuProfilesCollection::GetProfile(int security_token_id, |
| 589 | unsigned uid) { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 590 | HashMap::Entry* entry = profiles_uids_.Lookup(reinterpret_cast<void*>(uid), |
| 591 | static_cast<uint32_t>(uid), |
| 592 | false); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 593 | int index; |
| 594 | if (entry != NULL) { |
| 595 | index = static_cast<int>(reinterpret_cast<intptr_t>(entry->value)); |
| 596 | } else { |
| 597 | return NULL; |
| 598 | } |
| 599 | List<CpuProfile*>* unabridged_list = |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 600 | profiles_by_token_[TokenToIndex(TokenEnumerator::kNoSecurityToken)]; |
| 601 | if (security_token_id == TokenEnumerator::kNoSecurityToken) { |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 602 | return unabridged_list->at(index); |
| 603 | } |
| 604 | List<CpuProfile*>* list = GetProfilesList(security_token_id); |
| 605 | if (list->at(index) == NULL) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 606 | (*list)[index] = |
| 607 | unabridged_list->at(index)->FilteredClone(security_token_id); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 608 | } |
| 609 | return list->at(index); |
| 610 | } |
| 611 | |
| 612 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 613 | bool CpuProfilesCollection::IsLastProfile(const char* title) { |
| 614 | // Called from VM thread, and only it can mutate the list, |
| 615 | // so no locking is needed here. |
| 616 | if (current_profiles_.length() != 1) return false; |
| 617 | return StrLength(title) == 0 |
| 618 | || strcmp(current_profiles_[0]->title(), title) == 0; |
| 619 | } |
| 620 | |
| 621 | |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 622 | int CpuProfilesCollection::TokenToIndex(int security_token_id) { |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 623 | ASSERT(TokenEnumerator::kNoSecurityToken == -1); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 624 | return security_token_id + 1; // kNoSecurityToken -> 0, 0 -> 1, ... |
| 625 | } |
| 626 | |
| 627 | |
| 628 | List<CpuProfile*>* CpuProfilesCollection::GetProfilesList( |
| 629 | int security_token_id) { |
| 630 | const int index = TokenToIndex(security_token_id); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 631 | const int lists_to_add = index - profiles_by_token_.length() + 1; |
| 632 | if (lists_to_add > 0) profiles_by_token_.AddBlock(NULL, lists_to_add); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 633 | List<CpuProfile*>* unabridged_list = |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 634 | profiles_by_token_[TokenToIndex(TokenEnumerator::kNoSecurityToken)]; |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 635 | const int current_count = unabridged_list->length(); |
| 636 | if (profiles_by_token_[index] == NULL) { |
| 637 | profiles_by_token_[index] = new List<CpuProfile*>(current_count); |
| 638 | } |
| 639 | List<CpuProfile*>* list = profiles_by_token_[index]; |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 640 | const int profiles_to_add = current_count - list->length(); |
| 641 | if (profiles_to_add > 0) list->AddBlock(NULL, profiles_to_add); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 642 | return list; |
| 643 | } |
| 644 | |
| 645 | |
| 646 | List<CpuProfile*>* CpuProfilesCollection::Profiles(int security_token_id) { |
| 647 | List<CpuProfile*>* unabridged_list = |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 648 | profiles_by_token_[TokenToIndex(TokenEnumerator::kNoSecurityToken)]; |
| 649 | if (security_token_id == TokenEnumerator::kNoSecurityToken) { |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 650 | return unabridged_list; |
| 651 | } |
| 652 | List<CpuProfile*>* list = GetProfilesList(security_token_id); |
| 653 | const int current_count = unabridged_list->length(); |
| 654 | for (int i = 0; i < current_count; ++i) { |
| 655 | if (list->at(i) == NULL) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 656 | (*list)[i] = unabridged_list->at(i)->FilteredClone(security_token_id); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 657 | } |
| 658 | } |
| 659 | return list; |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 660 | } |
| 661 | |
| 662 | |
| 663 | CodeEntry* CpuProfilesCollection::NewCodeEntry(Logger::LogEventsAndTags tag, |
| 664 | String* name, |
| 665 | String* resource_name, |
| 666 | int line_number) { |
| 667 | CodeEntry* entry = new CodeEntry(tag, |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 668 | CodeEntry::kEmptyNamePrefix, |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 669 | GetFunctionName(name), |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 670 | GetName(resource_name), |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 671 | line_number, |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 672 | TokenEnumerator::kNoSecurityToken); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 673 | code_entries_.Add(entry); |
| 674 | return entry; |
| 675 | } |
| 676 | |
| 677 | |
| 678 | CodeEntry* CpuProfilesCollection::NewCodeEntry(Logger::LogEventsAndTags tag, |
| 679 | const char* name) { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 680 | CodeEntry* entry = new CodeEntry(tag, |
| 681 | CodeEntry::kEmptyNamePrefix, |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 682 | GetFunctionName(name), |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 683 | "", |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 684 | v8::CpuProfileNode::kNoLineNumberInfo, |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 685 | TokenEnumerator::kNoSecurityToken); |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 686 | code_entries_.Add(entry); |
| 687 | return entry; |
| 688 | } |
| 689 | |
| 690 | |
| 691 | CodeEntry* CpuProfilesCollection::NewCodeEntry(Logger::LogEventsAndTags tag, |
| 692 | const char* name_prefix, |
| 693 | String* name) { |
| 694 | CodeEntry* entry = new CodeEntry(tag, |
| 695 | name_prefix, |
| 696 | GetName(name), |
| 697 | "", |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 698 | v8::CpuProfileNode::kNoLineNumberInfo, |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 699 | TokenEnumerator::kInheritsSecurityToken); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 700 | code_entries_.Add(entry); |
| 701 | return entry; |
| 702 | } |
| 703 | |
| 704 | |
| 705 | CodeEntry* CpuProfilesCollection::NewCodeEntry(Logger::LogEventsAndTags tag, |
| 706 | int args_count) { |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 707 | CodeEntry* entry = new CodeEntry(tag, |
| 708 | "args_count: ", |
| 709 | GetName(args_count), |
| 710 | "", |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 711 | v8::CpuProfileNode::kNoLineNumberInfo, |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 712 | TokenEnumerator::kInheritsSecurityToken); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 713 | code_entries_.Add(entry); |
| 714 | return entry; |
| 715 | } |
| 716 | |
| 717 | |
| 718 | CodeEntry* CpuProfilesCollection::NewCodeEntry(int security_token_id) { |
| 719 | CodeEntry* entry = new CodeEntry(security_token_id); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 720 | code_entries_.Add(entry); |
| 721 | return entry; |
| 722 | } |
| 723 | |
| 724 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 725 | void CpuProfilesCollection::AddPathToCurrentProfiles( |
| 726 | const Vector<CodeEntry*>& path) { |
| 727 | // As starting / stopping profiles is rare relatively to this |
| 728 | // method, we don't bother minimizing the duration of lock holding, |
| 729 | // e.g. copying contents of the list to a local vector. |
| 730 | current_profiles_semaphore_->Wait(); |
| 731 | for (int i = 0; i < current_profiles_.length(); ++i) { |
| 732 | current_profiles_[i]->AddPath(path); |
| 733 | } |
| 734 | current_profiles_semaphore_->Signal(); |
| 735 | } |
| 736 | |
| 737 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 738 | void SampleRateCalculator::Tick() { |
| 739 | if (--wall_time_query_countdown_ == 0) |
| 740 | UpdateMeasurements(OS::TimeCurrentMillis()); |
| 741 | } |
| 742 | |
| 743 | |
| 744 | void SampleRateCalculator::UpdateMeasurements(double current_time) { |
| 745 | if (measurements_count_++ != 0) { |
| 746 | const double measured_ticks_per_ms = |
| 747 | (kWallTimeQueryIntervalMs * ticks_per_ms_) / |
| 748 | (current_time - last_wall_time_); |
| 749 | // Update the average value. |
| 750 | ticks_per_ms_ += |
| 751 | (measured_ticks_per_ms - ticks_per_ms_) / measurements_count_; |
| 752 | // Update the externally accessible result. |
| 753 | result_ = static_cast<AtomicWord>(ticks_per_ms_ * kResultScale); |
| 754 | } |
| 755 | last_wall_time_ = current_time; |
| 756 | wall_time_query_countdown_ = |
| 757 | static_cast<unsigned>(kWallTimeQueryIntervalMs * ticks_per_ms_); |
| 758 | } |
| 759 | |
| 760 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 761 | const char* ProfileGenerator::kAnonymousFunctionName = "(anonymous function)"; |
| 762 | const char* ProfileGenerator::kProgramEntryName = "(program)"; |
| 763 | const char* ProfileGenerator::kGarbageCollectorEntryName = |
| 764 | "(garbage collector)"; |
| 765 | |
| 766 | |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 767 | ProfileGenerator::ProfileGenerator(CpuProfilesCollection* profiles) |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 768 | : profiles_(profiles), |
| 769 | program_entry_( |
| 770 | profiles->NewCodeEntry(Logger::FUNCTION_TAG, kProgramEntryName)), |
| 771 | gc_entry_( |
| 772 | profiles->NewCodeEntry(Logger::BUILTIN_TAG, |
| 773 | kGarbageCollectorEntryName)) { |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | |
| 777 | void ProfileGenerator::RecordTickSample(const TickSample& sample) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 778 | // Allocate space for stack frames + pc + function + vm-state. |
| 779 | ScopedVector<CodeEntry*> entries(sample.frames_count + 3); |
| 780 | // As actual number of decoded code entries may vary, initialize |
| 781 | // entries vector with NULL values. |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 782 | CodeEntry** entry = entries.start(); |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 783 | memset(entry, 0, entries.length() * sizeof(*entry)); |
| 784 | if (sample.pc != NULL) { |
| 785 | *entry++ = code_map_.FindEntry(sample.pc); |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 786 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 787 | if (sample.function != NULL) { |
| 788 | *entry = code_map_.FindEntry(sample.function); |
| 789 | if (*entry != NULL && !(*entry)->is_js_function()) { |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 790 | *entry = NULL; |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 791 | } else { |
| 792 | CodeEntry* pc_entry = *entries.start(); |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 793 | if (pc_entry == NULL) { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 794 | *entry = NULL; |
erik.corry@gmail.com | 9dfbea4 | 2010-05-21 12:58:28 +0000 | [diff] [blame] | 795 | } else if (pc_entry->is_js_function()) { |
| 796 | // Use function entry in favor of pc entry, as function |
| 797 | // entry has security token. |
| 798 | *entries.start() = NULL; |
| 799 | } |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 800 | } |
| 801 | entry++; |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 802 | } |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 803 | |
| 804 | for (const Address *stack_pos = sample.stack, |
| 805 | *stack_end = stack_pos + sample.frames_count; |
| 806 | stack_pos != stack_end; |
| 807 | ++stack_pos) { |
| 808 | *entry++ = code_map_.FindEntry(*stack_pos); |
| 809 | } |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 810 | } |
| 811 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 812 | if (FLAG_prof_browser_mode) { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 813 | bool no_symbolized_entries = true; |
| 814 | for (CodeEntry** e = entries.start(); e != entry; ++e) { |
| 815 | if (*e != NULL) { |
| 816 | no_symbolized_entries = false; |
| 817 | break; |
| 818 | } |
| 819 | } |
| 820 | // If no frames were symbolized, put the VM state entry in. |
| 821 | if (no_symbolized_entries) { |
| 822 | *entry++ = EntryForVMState(sample.state); |
| 823 | } |
whesse@chromium.org | cec079d | 2010-03-22 14:44:04 +0000 | [diff] [blame] | 824 | } |
| 825 | |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 826 | profiles_->AddPathToCurrentProfiles(entries); |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 827 | } |
| 828 | |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 829 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 830 | void HeapGraphEdge::Init( |
| 831 | int child_index, Type type, const char* name, HeapEntry* to) { |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 832 | ASSERT(type == kContextVariable |
| 833 | || type == kProperty |
| 834 | || type == kInternal |
| 835 | || type == kShortcut); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 836 | child_index_ = child_index; |
| 837 | type_ = type; |
| 838 | name_ = name; |
| 839 | to_ = to; |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 843 | void HeapGraphEdge::Init(int child_index, Type type, int index, HeapEntry* to) { |
| 844 | ASSERT(type == kElement || type == kHidden); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 845 | child_index_ = child_index; |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 846 | type_ = type; |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 847 | index_ = index; |
| 848 | to_ = to; |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 852 | void HeapGraphEdge::Init(int child_index, int index, HeapEntry* to) { |
| 853 | Init(child_index, kElement, index, to); |
| 854 | } |
| 855 | |
| 856 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 857 | HeapEntry* HeapGraphEdge::From() { |
| 858 | return reinterpret_cast<HeapEntry*>(this - child_index_) - 1; |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 859 | } |
| 860 | |
| 861 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 862 | void HeapEntry::Init(HeapSnapshot* snapshot, |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 863 | Type type, |
| 864 | const char* name, |
| 865 | uint64_t id, |
| 866 | int self_size, |
| 867 | int children_count, |
| 868 | int retainers_count) { |
| 869 | snapshot_ = snapshot; |
| 870 | type_ = type; |
| 871 | painted_ = kUnpainted; |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 872 | name_ = name; |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 873 | self_size_ = self_size; |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 874 | retained_size_ = 0; |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 875 | children_count_ = children_count; |
| 876 | retainers_count_ = retainers_count; |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 877 | dominator_ = NULL; |
| 878 | |
| 879 | union { |
| 880 | uint64_t set_id; |
| 881 | Id stored_id; |
| 882 | } id_adaptor = {id}; |
| 883 | id_ = id_adaptor.stored_id; |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 887 | void HeapEntry::SetNamedReference(HeapGraphEdge::Type type, |
| 888 | int child_index, |
| 889 | const char* name, |
| 890 | HeapEntry* entry, |
| 891 | int retainer_index) { |
| 892 | children_arr()[child_index].Init(child_index, type, name, entry); |
| 893 | entry->retainers_arr()[retainer_index] = children_arr() + child_index; |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 897 | void HeapEntry::SetIndexedReference(HeapGraphEdge::Type type, |
| 898 | int child_index, |
| 899 | int index, |
| 900 | HeapEntry* entry, |
| 901 | int retainer_index) { |
| 902 | children_arr()[child_index].Init(child_index, type, index, entry); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 903 | entry->retainers_arr()[retainer_index] = children_arr() + child_index; |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 907 | void HeapEntry::SetUnidirElementReference( |
| 908 | int child_index, int index, HeapEntry* entry) { |
| 909 | children_arr()[child_index].Init(child_index, index, entry); |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 910 | } |
| 911 | |
| 912 | |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 913 | int HeapEntry::RetainedSize(bool exact) { |
| 914 | if (exact && (retained_size_ & kExactRetainedSizeTag) == 0) { |
| 915 | CalculateExactRetainedSize(); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 916 | } |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 917 | return retained_size_ & (~kExactRetainedSizeTag); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 918 | } |
| 919 | |
| 920 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 921 | List<HeapGraphPath*>* HeapEntry::GetRetainingPaths() { |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 922 | return snapshot_->GetRetainingPaths(this); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 926 | template<class Visitor> |
| 927 | void HeapEntry::ApplyAndPaintAllReachable(Visitor* visitor) { |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 928 | List<HeapEntry*> list(10); |
| 929 | list.Add(this); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 930 | this->paint_reachable(); |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 931 | visitor->Apply(this); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 932 | while (!list.is_empty()) { |
| 933 | HeapEntry* entry = list.RemoveLast(); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 934 | Vector<HeapGraphEdge> children = entry->children(); |
| 935 | for (int i = 0; i < children.length(); ++i) { |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 936 | if (children[i].type() == HeapGraphEdge::kShortcut) continue; |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 937 | HeapEntry* child = children[i].to(); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 938 | if (!child->painted_reachable()) { |
| 939 | list.Add(child); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 940 | child->paint_reachable(); |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 941 | visitor->Apply(child); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 942 | } |
| 943 | } |
| 944 | } |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 948 | class NullClass { |
| 949 | public: |
| 950 | void Apply(HeapEntry* entry) { } |
| 951 | }; |
| 952 | |
| 953 | void HeapEntry::PaintAllReachable() { |
| 954 | NullClass null; |
| 955 | ApplyAndPaintAllReachable(&null); |
| 956 | } |
| 957 | |
| 958 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 959 | void HeapEntry::Print(int max_depth, int indent) { |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 960 | OS::Print("%6d %6d [%llu] ", self_size(), RetainedSize(false), id()); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 961 | if (type() != kString) { |
| 962 | OS::Print("%s %.40s\n", TypeAsString(), name_); |
| 963 | } else { |
| 964 | OS::Print("\""); |
| 965 | const char* c = name_; |
| 966 | while (*c && (c - name_) <= 40) { |
| 967 | if (*c != '\n') |
| 968 | OS::Print("%c", *c); |
| 969 | else |
| 970 | OS::Print("\\n"); |
| 971 | ++c; |
| 972 | } |
| 973 | OS::Print("\"\n"); |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 974 | } |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 975 | if (--max_depth == 0) return; |
| 976 | Vector<HeapGraphEdge> ch = children(); |
| 977 | for (int i = 0; i < ch.length(); ++i) { |
| 978 | HeapGraphEdge& edge = ch[i]; |
| 979 | switch (edge.type()) { |
| 980 | case HeapGraphEdge::kContextVariable: |
| 981 | OS::Print(" %*c #%s: ", indent, ' ', edge.name()); |
| 982 | break; |
| 983 | case HeapGraphEdge::kElement: |
| 984 | OS::Print(" %*c %d: ", indent, ' ', edge.index()); |
| 985 | break; |
| 986 | case HeapGraphEdge::kInternal: |
| 987 | OS::Print(" %*c $%s: ", indent, ' ', edge.name()); |
| 988 | break; |
| 989 | case HeapGraphEdge::kProperty: |
| 990 | OS::Print(" %*c %s: ", indent, ' ', edge.name()); |
| 991 | break; |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 992 | case HeapGraphEdge::kHidden: |
| 993 | OS::Print(" %*c $%d: ", indent, ' ', edge.index()); |
| 994 | break; |
| 995 | case HeapGraphEdge::kShortcut: |
| 996 | OS::Print(" %*c ^%s: ", indent, ' ', edge.name()); |
| 997 | break; |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 998 | default: |
| 999 | OS::Print("!!! unknown edge type: %d ", edge.type()); |
| 1000 | } |
| 1001 | edge.to()->Print(max_depth, indent + 2); |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 1002 | } |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1005 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1006 | const char* HeapEntry::TypeAsString() { |
| 1007 | switch (type()) { |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1008 | case kHidden: return "/hidden/"; |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1009 | case kObject: return "/object/"; |
| 1010 | case kClosure: return "/closure/"; |
| 1011 | case kString: return "/string/"; |
| 1012 | case kCode: return "/code/"; |
| 1013 | case kArray: return "/array/"; |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 1014 | case kRegExp: return "/regexp/"; |
| 1015 | case kHeapNumber: return "/number/"; |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1016 | default: return "???"; |
| 1017 | } |
| 1018 | } |
| 1019 | |
| 1020 | |
| 1021 | int HeapEntry::EntriesSize(int entries_count, |
| 1022 | int children_count, |
| 1023 | int retainers_count) { |
| 1024 | return sizeof(HeapEntry) * entries_count // NOLINT |
| 1025 | + sizeof(HeapGraphEdge) * children_count // NOLINT |
| 1026 | + sizeof(HeapGraphEdge*) * retainers_count; // NOLINT |
| 1027 | } |
| 1028 | |
| 1029 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1030 | class RetainedSizeCalculator { |
| 1031 | public: |
| 1032 | RetainedSizeCalculator() |
| 1033 | : retained_size_(0) { |
| 1034 | } |
| 1035 | |
| 1036 | int reained_size() const { return retained_size_; } |
| 1037 | |
| 1038 | void Apply(HeapEntry** entry_ptr) { |
| 1039 | if ((*entry_ptr)->painted_reachable()) { |
| 1040 | retained_size_ += (*entry_ptr)->self_size(); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1041 | } |
| 1042 | } |
| 1043 | |
| 1044 | private: |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1045 | int retained_size_; |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1046 | }; |
| 1047 | |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1048 | void HeapEntry::CalculateExactRetainedSize() { |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1049 | // To calculate retained size, first we paint all reachable nodes in |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1050 | // one color, then we paint (or re-paint) all nodes reachable from |
| 1051 | // other nodes with a different color. Then we sum up self sizes of |
| 1052 | // nodes painted with the first color. |
| 1053 | snapshot()->ClearPaint(); |
| 1054 | PaintAllReachable(); |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 1055 | |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1056 | List<HeapEntry*> list(10); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1057 | HeapEntry* root = snapshot()->root(); |
| 1058 | if (this != root) { |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1059 | list.Add(root); |
| 1060 | root->paint_reachable_from_others(); |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 1061 | } |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1062 | while (!list.is_empty()) { |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1063 | HeapEntry* curr = list.RemoveLast(); |
| 1064 | Vector<HeapGraphEdge> children = curr->children(); |
| 1065 | for (int i = 0; i < children.length(); ++i) { |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1066 | if (children[i].type() == HeapGraphEdge::kShortcut) continue; |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1067 | HeapEntry* child = children[i].to(); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1068 | if (child != this && child->not_painted_reachable_from_others()) { |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 1069 | list.Add(child); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1070 | child->paint_reachable_from_others(); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1071 | } |
| 1072 | } |
| 1073 | } |
| 1074 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1075 | RetainedSizeCalculator ret_size_calc; |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1076 | snapshot()->IterateEntries(&ret_size_calc); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1077 | retained_size_ = ret_size_calc.reained_size(); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1078 | ASSERT((retained_size_ & kExactRetainedSizeTag) == 0); |
| 1079 | retained_size_ |= kExactRetainedSizeTag; |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | |
| 1083 | class CachedHeapGraphPath { |
| 1084 | public: |
| 1085 | CachedHeapGraphPath() |
| 1086 | : nodes_(NodesMatch) { } |
| 1087 | CachedHeapGraphPath(const CachedHeapGraphPath& src) |
| 1088 | : nodes_(NodesMatch, &HashMap::DefaultAllocator, src.nodes_.capacity()), |
| 1089 | path_(src.path_.length() + 1) { |
| 1090 | for (HashMap::Entry* p = src.nodes_.Start(); |
| 1091 | p != NULL; |
| 1092 | p = src.nodes_.Next(p)) { |
| 1093 | nodes_.Lookup(p->key, p->hash, true); |
| 1094 | } |
| 1095 | path_.AddAll(src.path_); |
| 1096 | } |
| 1097 | void Add(HeapGraphEdge* edge) { |
| 1098 | nodes_.Lookup(edge->to(), Hash(edge->to()), true); |
| 1099 | path_.Add(edge); |
| 1100 | } |
| 1101 | bool ContainsNode(HeapEntry* node) { |
| 1102 | return nodes_.Lookup(node, Hash(node), false) != NULL; |
| 1103 | } |
| 1104 | const List<HeapGraphEdge*>* path() const { return &path_; } |
| 1105 | |
| 1106 | private: |
| 1107 | static uint32_t Hash(HeapEntry* entry) { |
| 1108 | return static_cast<uint32_t>(reinterpret_cast<intptr_t>(entry)); |
| 1109 | } |
| 1110 | static bool NodesMatch(void* key1, void* key2) { return key1 == key2; } |
| 1111 | |
| 1112 | HashMap nodes_; |
| 1113 | List<HeapGraphEdge*> path_; |
| 1114 | }; |
| 1115 | |
| 1116 | |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1117 | List<HeapGraphPath*>* HeapEntry::CalculateRetainingPaths() { |
| 1118 | List<HeapGraphPath*>* retaining_paths = new List<HeapGraphPath*>(4); |
| 1119 | CachedHeapGraphPath path; |
| 1120 | FindRetainingPaths(&path, retaining_paths); |
| 1121 | return retaining_paths; |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1122 | } |
| 1123 | |
| 1124 | |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1125 | void HeapEntry::FindRetainingPaths(CachedHeapGraphPath* prev_path, |
| 1126 | List<HeapGraphPath*>* retaining_paths) { |
| 1127 | Vector<HeapGraphEdge*> rets = retainers(); |
| 1128 | for (int i = 0; i < rets.length(); ++i) { |
| 1129 | HeapGraphEdge* ret_edge = rets[i]; |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1130 | if (prev_path->ContainsNode(ret_edge->From())) continue; |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1131 | if (ret_edge->From() != snapshot()->root()) { |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1132 | CachedHeapGraphPath path(*prev_path); |
| 1133 | path.Add(ret_edge); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1134 | ret_edge->From()->FindRetainingPaths(&path, retaining_paths); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1135 | } else { |
| 1136 | HeapGraphPath* ret_path = new HeapGraphPath(*prev_path->path()); |
| 1137 | ret_path->Set(0, ret_edge); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1138 | retaining_paths->Add(ret_path); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1139 | } |
| 1140 | } |
| 1141 | } |
| 1142 | |
| 1143 | |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1144 | HeapGraphPath::HeapGraphPath(const List<HeapGraphEdge*>& path) |
| 1145 | : path_(path.length() + 1) { |
| 1146 | Add(NULL); |
| 1147 | for (int i = path.length() - 1; i >= 0; --i) { |
| 1148 | Add(path[i]); |
| 1149 | } |
| 1150 | } |
| 1151 | |
| 1152 | |
| 1153 | void HeapGraphPath::Print() { |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1154 | path_[0]->From()->Print(1, 0); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1155 | for (int i = 0; i < path_.length(); ++i) { |
| 1156 | OS::Print(" -> "); |
| 1157 | HeapGraphEdge* edge = path_[i]; |
| 1158 | switch (edge->type()) { |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1159 | case HeapGraphEdge::kContextVariable: |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1160 | OS::Print("[#%s] ", edge->name()); |
| 1161 | break; |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1162 | case HeapGraphEdge::kElement: |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1163 | case HeapGraphEdge::kHidden: |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1164 | OS::Print("[%d] ", edge->index()); |
| 1165 | break; |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1166 | case HeapGraphEdge::kInternal: |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 1167 | OS::Print("[$%s] ", edge->name()); |
| 1168 | break; |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1169 | case HeapGraphEdge::kProperty: |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1170 | OS::Print("[%s] ", edge->name()); |
| 1171 | break; |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1172 | case HeapGraphEdge::kShortcut: |
| 1173 | OS::Print("[^%s] ", edge->name()); |
| 1174 | break; |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1175 | default: |
| 1176 | OS::Print("!!! unknown edge type: %d ", edge->type()); |
| 1177 | } |
| 1178 | edge->to()->Print(1, 0); |
| 1179 | } |
| 1180 | OS::Print("\n"); |
| 1181 | } |
| 1182 | |
| 1183 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1184 | HeapObject *const HeapSnapshot::kInternalRootObject = |
| 1185 | reinterpret_cast<HeapObject*>(1); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1186 | HeapObject *const HeapSnapshot::kGcRootsObject = |
| 1187 | reinterpret_cast<HeapObject*>(2); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1188 | |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1189 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1190 | // It is very important to keep objects that form a heap snapshot |
| 1191 | // as small as possible. |
| 1192 | namespace { // Avoid littering the global namespace. |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1193 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1194 | template <size_t ptr_size> struct SnapshotSizeConstants; |
| 1195 | |
| 1196 | template <> struct SnapshotSizeConstants<4> { |
| 1197 | static const int kExpectedHeapGraphEdgeSize = 12; |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1198 | static const int kExpectedHeapEntrySize = 36; |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1199 | }; |
| 1200 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1201 | template <> struct SnapshotSizeConstants<8> { |
| 1202 | static const int kExpectedHeapGraphEdgeSize = 24; |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1203 | static const int kExpectedHeapEntrySize = 48; |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1204 | }; |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1205 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1206 | } // namespace |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1207 | |
| 1208 | HeapSnapshot::HeapSnapshot(HeapSnapshotsCollection* collection, |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 1209 | HeapSnapshot::Type type, |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1210 | const char* title, |
| 1211 | unsigned uid) |
| 1212 | : collection_(collection), |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 1213 | type_(type), |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1214 | title_(title), |
| 1215 | uid_(uid), |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 1216 | root_entry_(NULL), |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1217 | gc_roots_entry_(NULL), |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1218 | raw_entries_(NULL), |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1219 | entries_sorted_(false), |
| 1220 | retaining_paths_(HeapEntry::Match) { |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1221 | STATIC_ASSERT( |
| 1222 | sizeof(HeapGraphEdge) == |
| 1223 | SnapshotSizeConstants<sizeof(void*)>::kExpectedHeapGraphEdgeSize); // NOLINT |
| 1224 | STATIC_ASSERT( |
| 1225 | sizeof(HeapEntry) == |
| 1226 | SnapshotSizeConstants<sizeof(void*)>::kExpectedHeapEntrySize); // NOLINT |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 1227 | } |
| 1228 | |
| 1229 | |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1230 | static void DeleteHeapGraphPath(HeapGraphPath** path_ptr) { |
| 1231 | delete *path_ptr; |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 1234 | HeapSnapshot::~HeapSnapshot() { |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1235 | DeleteArray(raw_entries_); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1236 | for (HashMap::Entry* p = retaining_paths_.Start(); |
| 1237 | p != NULL; |
| 1238 | p = retaining_paths_.Next(p)) { |
| 1239 | List<HeapGraphPath*>* list = |
| 1240 | reinterpret_cast<List<HeapGraphPath*>*>(p->value); |
| 1241 | list->Iterate(DeleteHeapGraphPath); |
| 1242 | delete list; |
| 1243 | } |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1244 | } |
| 1245 | |
| 1246 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1247 | void HeapSnapshot::AllocateEntries(int entries_count, |
| 1248 | int children_count, |
| 1249 | int retainers_count) { |
| 1250 | ASSERT(raw_entries_ == NULL); |
| 1251 | raw_entries_ = NewArray<char>( |
| 1252 | HeapEntry::EntriesSize(entries_count, children_count, retainers_count)); |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 1253 | #ifdef DEBUG |
| 1254 | raw_entries_size_ = |
| 1255 | HeapEntry::EntriesSize(entries_count, children_count, retainers_count); |
| 1256 | #endif |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1257 | } |
| 1258 | |
| 1259 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1260 | HeapEntry* HeapSnapshot::AddEntry(HeapObject* object, |
| 1261 | int children_count, |
| 1262 | int retainers_count) { |
| 1263 | if (object == kInternalRootObject) { |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 1264 | ASSERT(root_entry_ == NULL); |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 1265 | ASSERT(retainers_count == 0); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1266 | return (root_entry_ = AddEntry(HeapEntry::kObject, |
| 1267 | "", |
| 1268 | HeapObjectsMap::kInternalRootObjectId, |
| 1269 | 0, |
| 1270 | children_count, |
| 1271 | retainers_count)); |
| 1272 | } else if (object == kGcRootsObject) { |
| 1273 | ASSERT(gc_roots_entry_ == NULL); |
| 1274 | return (gc_roots_entry_ = AddEntry(HeapEntry::kObject, |
| 1275 | "(GC roots)", |
| 1276 | HeapObjectsMap::kGcRootsObjectId, |
| 1277 | 0, |
| 1278 | children_count, |
| 1279 | retainers_count)); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1280 | } else if (object->IsJSFunction()) { |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1281 | JSFunction* func = JSFunction::cast(object); |
| 1282 | SharedFunctionInfo* shared = func->shared(); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1283 | return AddEntry(object, |
| 1284 | HeapEntry::kClosure, |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 1285 | collection_->GetName(String::cast(shared->name())), |
| 1286 | children_count, |
| 1287 | retainers_count); |
| 1288 | } else if (object->IsJSRegExp()) { |
| 1289 | JSRegExp* re = JSRegExp::cast(object); |
| 1290 | return AddEntry(object, |
| 1291 | HeapEntry::kRegExp, |
| 1292 | collection_->GetName(re->Pattern()), |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1293 | children_count, |
| 1294 | retainers_count); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1295 | } else if (object->IsJSObject()) { |
| 1296 | return AddEntry(object, |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1297 | HeapEntry::kObject, |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1298 | collection_->GetName(GetConstructorNameForHeapProfile( |
| 1299 | JSObject::cast(object))), |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1300 | children_count, |
| 1301 | retainers_count); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1302 | } else if (object->IsString()) { |
| 1303 | return AddEntry(object, |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1304 | HeapEntry::kString, |
| 1305 | collection_->GetName(String::cast(object)), |
| 1306 | children_count, |
| 1307 | retainers_count); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 1308 | } else if (object->IsCode()) { |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1309 | return AddEntry(object, |
| 1310 | HeapEntry::kCode, |
| 1311 | "", |
| 1312 | children_count, |
| 1313 | retainers_count); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 1314 | } else if (object->IsSharedFunctionInfo()) { |
| 1315 | SharedFunctionInfo* shared = SharedFunctionInfo::cast(object); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1316 | return AddEntry(object, |
| 1317 | HeapEntry::kCode, |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 1318 | collection_->GetName(String::cast(shared->name())), |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1319 | children_count, |
| 1320 | retainers_count); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 1321 | } else if (object->IsScript()) { |
| 1322 | Script* script = Script::cast(object); |
| 1323 | return AddEntry(object, |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1324 | HeapEntry::kCode, |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 1325 | script->name()->IsString() ? |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1326 | collection_->GetName(String::cast(script->name())) : "", |
| 1327 | children_count, |
| 1328 | retainers_count); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1329 | } else if (object->IsFixedArray()) { |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1330 | return AddEntry(object, |
| 1331 | HeapEntry::kArray, |
| 1332 | "", |
| 1333 | children_count, |
| 1334 | retainers_count); |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 1335 | } else if (object->IsHeapNumber()) { |
| 1336 | return AddEntry(object, |
| 1337 | HeapEntry::kHeapNumber, |
| 1338 | "number", |
| 1339 | children_count, |
| 1340 | retainers_count); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1341 | } |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1342 | return AddEntry(object, |
| 1343 | HeapEntry::kHidden, |
| 1344 | "system", |
| 1345 | children_count, |
| 1346 | retainers_count); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1347 | } |
| 1348 | |
| 1349 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1350 | static void HeapEntryClearPaint(HeapEntry** entry_ptr) { |
| 1351 | (*entry_ptr)->clear_paint(); |
| 1352 | } |
| 1353 | |
| 1354 | void HeapSnapshot::ClearPaint() { |
| 1355 | entries_.Iterate(HeapEntryClearPaint); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1359 | HeapEntry* HeapSnapshot::AddEntry(HeapObject* object, |
| 1360 | HeapEntry::Type type, |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1361 | const char* name, |
| 1362 | int children_count, |
| 1363 | int retainers_count) { |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 1364 | return AddEntry(type, |
| 1365 | name, |
| 1366 | collection_->GetObjectId(object->address()), |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1367 | object->Size(), |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 1368 | children_count, |
| 1369 | retainers_count); |
| 1370 | } |
| 1371 | |
| 1372 | |
| 1373 | HeapEntry* HeapSnapshot::AddEntry(HeapEntry::Type type, |
| 1374 | const char* name, |
| 1375 | uint64_t id, |
| 1376 | int size, |
| 1377 | int children_count, |
| 1378 | int retainers_count) { |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1379 | HeapEntry* entry = GetNextEntryToInit(); |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 1380 | entry->Init(this, type, name, id, size, children_count, retainers_count); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1381 | return entry; |
| 1382 | } |
| 1383 | |
| 1384 | |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1385 | void HeapSnapshot::SetDominatorsToSelf() { |
| 1386 | for (int i = 0; i < entries_.length(); ++i) { |
| 1387 | HeapEntry* entry = entries_[i]; |
| 1388 | if (entry->dominator() == NULL) entry->set_dominator(entry); |
| 1389 | } |
| 1390 | } |
| 1391 | |
| 1392 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1393 | HeapEntry* HeapSnapshot::GetNextEntryToInit() { |
| 1394 | if (entries_.length() > 0) { |
| 1395 | HeapEntry* last_entry = entries_.last(); |
| 1396 | entries_.Add(reinterpret_cast<HeapEntry*>( |
| 1397 | reinterpret_cast<char*>(last_entry) + last_entry->EntrySize())); |
| 1398 | } else { |
| 1399 | entries_.Add(reinterpret_cast<HeapEntry*>(raw_entries_)); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1400 | } |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 1401 | ASSERT(reinterpret_cast<char*>(entries_.last()) < |
| 1402 | (raw_entries_ + raw_entries_size_)); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1403 | return entries_.last(); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1404 | } |
| 1405 | |
| 1406 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1407 | HeapSnapshotsDiff* HeapSnapshot::CompareWith(HeapSnapshot* snapshot) { |
| 1408 | return collection_->CompareSnapshots(this, snapshot); |
| 1409 | } |
| 1410 | |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 1411 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1412 | HeapEntry* HeapSnapshot::GetEntryById(uint64_t id) { |
| 1413 | // GetSortedEntriesList is used in diff algorithm and sorts |
| 1414 | // entries by their id. |
| 1415 | List<HeapEntry*>* entries_by_id = GetSortedEntriesList(); |
| 1416 | |
| 1417 | // Perform a binary search by id. |
| 1418 | int low = 0; |
| 1419 | int high = entries_by_id->length() - 1; |
| 1420 | while (low <= high) { |
| 1421 | int mid = |
| 1422 | (static_cast<unsigned int>(low) + static_cast<unsigned int>(high)) >> 1; |
| 1423 | uint64_t mid_id = entries_by_id->at(mid)->id(); |
| 1424 | if (mid_id > id) |
| 1425 | high = mid - 1; |
| 1426 | else if (mid_id < id) |
| 1427 | low = mid + 1; |
| 1428 | else |
| 1429 | return entries_by_id->at(mid); |
| 1430 | } |
| 1431 | return NULL; |
| 1432 | } |
| 1433 | |
| 1434 | |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1435 | List<HeapGraphPath*>* HeapSnapshot::GetRetainingPaths(HeapEntry* entry) { |
| 1436 | HashMap::Entry* p = |
| 1437 | retaining_paths_.Lookup(entry, HeapEntry::Hash(entry), true); |
| 1438 | if (p->value == NULL) { |
| 1439 | p->value = entry->CalculateRetainingPaths(); |
| 1440 | } |
| 1441 | return reinterpret_cast<List<HeapGraphPath*>*>(p->value); |
| 1442 | } |
| 1443 | |
| 1444 | |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 1445 | template<class T> |
| 1446 | static int SortByIds(const T* entry1_ptr, |
| 1447 | const T* entry2_ptr) { |
| 1448 | if ((*entry1_ptr)->id() == (*entry2_ptr)->id()) return 0; |
| 1449 | return (*entry1_ptr)->id() < (*entry2_ptr)->id() ? -1 : 1; |
| 1450 | } |
| 1451 | |
| 1452 | List<HeapEntry*>* HeapSnapshot::GetSortedEntriesList() { |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1453 | if (!entries_sorted_) { |
| 1454 | entries_.Sort(SortByIds); |
| 1455 | entries_sorted_ = true; |
| 1456 | } |
| 1457 | return &entries_; |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 1458 | } |
| 1459 | |
| 1460 | |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1461 | void HeapSnapshot::Print(int max_depth) { |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1462 | root()->Print(max_depth, 0); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1463 | } |
| 1464 | |
| 1465 | |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1466 | const uint64_t HeapObjectsMap::kInternalRootObjectId = 0; |
| 1467 | const uint64_t HeapObjectsMap::kGcRootsObjectId = 1; |
| 1468 | // Increase kFirstAvailableObjectId if new 'special' objects appear. |
| 1469 | const uint64_t HeapObjectsMap::kFirstAvailableObjectId = 2; |
| 1470 | |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 1471 | HeapObjectsMap::HeapObjectsMap() |
| 1472 | : initial_fill_mode_(true), |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1473 | next_id_(kFirstAvailableObjectId), |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 1474 | entries_map_(AddressesMatch), |
| 1475 | entries_(new List<EntryInfo>()) { } |
| 1476 | |
| 1477 | |
| 1478 | HeapObjectsMap::~HeapObjectsMap() { |
| 1479 | delete entries_; |
| 1480 | } |
| 1481 | |
| 1482 | |
| 1483 | void HeapObjectsMap::SnapshotGenerationFinished() { |
| 1484 | initial_fill_mode_ = false; |
| 1485 | RemoveDeadEntries(); |
| 1486 | } |
| 1487 | |
| 1488 | |
| 1489 | uint64_t HeapObjectsMap::FindObject(Address addr) { |
| 1490 | if (!initial_fill_mode_) { |
| 1491 | uint64_t existing = FindEntry(addr); |
| 1492 | if (existing != 0) return existing; |
| 1493 | } |
| 1494 | uint64_t id = next_id_++; |
| 1495 | AddEntry(addr, id); |
| 1496 | return id; |
| 1497 | } |
| 1498 | |
| 1499 | |
| 1500 | void HeapObjectsMap::MoveObject(Address from, Address to) { |
| 1501 | if (from == to) return; |
| 1502 | HashMap::Entry* entry = entries_map_.Lookup(from, AddressHash(from), false); |
| 1503 | if (entry != NULL) { |
| 1504 | void* value = entry->value; |
| 1505 | entries_map_.Remove(from, AddressHash(from)); |
| 1506 | entry = entries_map_.Lookup(to, AddressHash(to), true); |
| 1507 | // We can have an entry at the new location, it is OK, as GC can overwrite |
| 1508 | // dead objects with alive objects being moved. |
| 1509 | entry->value = value; |
| 1510 | } |
| 1511 | } |
| 1512 | |
| 1513 | |
| 1514 | void HeapObjectsMap::AddEntry(Address addr, uint64_t id) { |
| 1515 | HashMap::Entry* entry = entries_map_.Lookup(addr, AddressHash(addr), true); |
| 1516 | ASSERT(entry->value == NULL); |
| 1517 | entry->value = reinterpret_cast<void*>(entries_->length()); |
| 1518 | entries_->Add(EntryInfo(id)); |
| 1519 | } |
| 1520 | |
| 1521 | |
| 1522 | uint64_t HeapObjectsMap::FindEntry(Address addr) { |
| 1523 | HashMap::Entry* entry = entries_map_.Lookup(addr, AddressHash(addr), false); |
| 1524 | if (entry != NULL) { |
| 1525 | int entry_index = |
| 1526 | static_cast<int>(reinterpret_cast<intptr_t>(entry->value)); |
| 1527 | EntryInfo& entry_info = entries_->at(entry_index); |
| 1528 | entry_info.accessed = true; |
| 1529 | return entry_info.id; |
| 1530 | } else { |
| 1531 | return 0; |
| 1532 | } |
| 1533 | } |
| 1534 | |
| 1535 | |
| 1536 | void HeapObjectsMap::RemoveDeadEntries() { |
| 1537 | List<EntryInfo>* new_entries = new List<EntryInfo>(); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1538 | List<void*> dead_entries; |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 1539 | for (HashMap::Entry* entry = entries_map_.Start(); |
| 1540 | entry != NULL; |
| 1541 | entry = entries_map_.Next(entry)) { |
| 1542 | int entry_index = |
| 1543 | static_cast<int>(reinterpret_cast<intptr_t>(entry->value)); |
| 1544 | EntryInfo& entry_info = entries_->at(entry_index); |
| 1545 | if (entry_info.accessed) { |
| 1546 | entry->value = reinterpret_cast<void*>(new_entries->length()); |
| 1547 | new_entries->Add(EntryInfo(entry_info.id, false)); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1548 | } else { |
| 1549 | dead_entries.Add(entry->key); |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 1550 | } |
| 1551 | } |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1552 | for (int i = 0; i < dead_entries.length(); ++i) { |
| 1553 | void* raw_entry = dead_entries[i]; |
| 1554 | entries_map_.Remove( |
| 1555 | raw_entry, AddressHash(reinterpret_cast<Address>(raw_entry))); |
| 1556 | } |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 1557 | delete entries_; |
| 1558 | entries_ = new_entries; |
| 1559 | } |
| 1560 | |
| 1561 | |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1562 | HeapSnapshotsCollection::HeapSnapshotsCollection() |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 1563 | : is_tracking_objects_(false), |
| 1564 | snapshots_uids_(HeapSnapshotsMatch), |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1565 | token_enumerator_(new TokenEnumerator()) { |
| 1566 | } |
| 1567 | |
| 1568 | |
| 1569 | static void DeleteHeapSnapshot(HeapSnapshot** snapshot_ptr) { |
| 1570 | delete *snapshot_ptr; |
| 1571 | } |
| 1572 | |
| 1573 | |
| 1574 | HeapSnapshotsCollection::~HeapSnapshotsCollection() { |
| 1575 | delete token_enumerator_; |
| 1576 | snapshots_.Iterate(DeleteHeapSnapshot); |
| 1577 | } |
| 1578 | |
| 1579 | |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 1580 | HeapSnapshot* HeapSnapshotsCollection::NewSnapshot(HeapSnapshot::Type type, |
| 1581 | const char* name, |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1582 | unsigned uid) { |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 1583 | is_tracking_objects_ = true; // Start watching for heap objects moves. |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1584 | return new HeapSnapshot(this, type, name, uid); |
| 1585 | } |
| 1586 | |
| 1587 | |
| 1588 | void HeapSnapshotsCollection::SnapshotGenerationFinished( |
| 1589 | HeapSnapshot* snapshot) { |
| 1590 | ids_.SnapshotGenerationFinished(); |
| 1591 | if (snapshot != NULL) { |
| 1592 | snapshots_.Add(snapshot); |
| 1593 | HashMap::Entry* entry = |
| 1594 | snapshots_uids_.Lookup(reinterpret_cast<void*>(snapshot->uid()), |
| 1595 | static_cast<uint32_t>(snapshot->uid()), |
| 1596 | true); |
| 1597 | ASSERT(entry->value == NULL); |
| 1598 | entry->value = snapshot; |
| 1599 | } |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1600 | } |
| 1601 | |
| 1602 | |
| 1603 | HeapSnapshot* HeapSnapshotsCollection::GetSnapshot(unsigned uid) { |
| 1604 | HashMap::Entry* entry = snapshots_uids_.Lookup(reinterpret_cast<void*>(uid), |
| 1605 | static_cast<uint32_t>(uid), |
| 1606 | false); |
| 1607 | return entry != NULL ? reinterpret_cast<HeapSnapshot*>(entry->value) : NULL; |
| 1608 | } |
| 1609 | |
| 1610 | |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 1611 | HeapSnapshotsDiff* HeapSnapshotsCollection::CompareSnapshots( |
| 1612 | HeapSnapshot* snapshot1, |
| 1613 | HeapSnapshot* snapshot2) { |
| 1614 | return comparator_.Compare(snapshot1, snapshot2); |
| 1615 | } |
| 1616 | |
| 1617 | |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 1618 | HeapEntry *const HeapEntriesMap::kHeapEntryPlaceholder = |
| 1619 | reinterpret_cast<HeapEntry*>(1); |
| 1620 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1621 | HeapEntriesMap::HeapEntriesMap() |
| 1622 | : entries_(HeapObjectsMatch), |
| 1623 | entries_count_(0), |
| 1624 | total_children_count_(0), |
| 1625 | total_retainers_count_(0) { |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1626 | } |
| 1627 | |
| 1628 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1629 | HeapEntriesMap::~HeapEntriesMap() { |
| 1630 | for (HashMap::Entry* p = entries_.Start(); p != NULL; p = entries_.Next(p)) { |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1631 | delete reinterpret_cast<EntryInfo*>(p->value); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1632 | } |
| 1633 | } |
| 1634 | |
| 1635 | |
| 1636 | HeapEntry* HeapEntriesMap::Map(HeapObject* object) { |
| 1637 | HashMap::Entry* cache_entry = entries_.Lookup(object, Hash(object), false); |
| 1638 | if (cache_entry != NULL) { |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1639 | EntryInfo* entry_info = reinterpret_cast<EntryInfo*>(cache_entry->value); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1640 | return entry_info->entry; |
| 1641 | } else { |
| 1642 | return NULL; |
| 1643 | } |
| 1644 | } |
| 1645 | |
| 1646 | |
| 1647 | void HeapEntriesMap::Pair(HeapObject* object, HeapEntry* entry) { |
| 1648 | HashMap::Entry* cache_entry = entries_.Lookup(object, Hash(object), true); |
| 1649 | ASSERT(cache_entry->value == NULL); |
| 1650 | cache_entry->value = new EntryInfo(entry); |
| 1651 | ++entries_count_; |
| 1652 | } |
| 1653 | |
| 1654 | |
| 1655 | void HeapEntriesMap::CountReference(HeapObject* from, HeapObject* to, |
| 1656 | int* prev_children_count, |
| 1657 | int* prev_retainers_count) { |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 1658 | HashMap::Entry* from_cache_entry = entries_.Lookup(from, Hash(from), false); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1659 | HashMap::Entry* to_cache_entry = entries_.Lookup(to, Hash(to), false); |
| 1660 | ASSERT(from_cache_entry != NULL); |
| 1661 | ASSERT(to_cache_entry != NULL); |
| 1662 | EntryInfo* from_entry_info = |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1663 | reinterpret_cast<EntryInfo*>(from_cache_entry->value); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1664 | EntryInfo* to_entry_info = |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1665 | reinterpret_cast<EntryInfo*>(to_cache_entry->value); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1666 | if (prev_children_count) |
| 1667 | *prev_children_count = from_entry_info->children_count; |
| 1668 | if (prev_retainers_count) |
| 1669 | *prev_retainers_count = to_entry_info->retainers_count; |
| 1670 | ++from_entry_info->children_count; |
| 1671 | ++to_entry_info->retainers_count; |
| 1672 | ++total_children_count_; |
| 1673 | ++total_retainers_count_; |
| 1674 | } |
| 1675 | |
| 1676 | |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1677 | HeapObjectsSet::HeapObjectsSet() |
| 1678 | : entries_(HeapEntriesMap::HeapObjectsMatch) { |
| 1679 | } |
| 1680 | |
| 1681 | |
| 1682 | void HeapObjectsSet::Clear() { |
| 1683 | entries_.Clear(); |
| 1684 | } |
| 1685 | |
| 1686 | |
| 1687 | bool HeapObjectsSet::Contains(Object* obj) { |
| 1688 | if (!obj->IsHeapObject()) return false; |
| 1689 | HeapObject* object = HeapObject::cast(obj); |
| 1690 | HashMap::Entry* cache_entry = |
| 1691 | entries_.Lookup(object, HeapEntriesMap::Hash(object), false); |
| 1692 | return cache_entry != NULL; |
| 1693 | } |
| 1694 | |
| 1695 | |
| 1696 | void HeapObjectsSet::Insert(Object* obj) { |
| 1697 | if (!obj->IsHeapObject()) return; |
| 1698 | HeapObject* object = HeapObject::cast(obj); |
| 1699 | HashMap::Entry* cache_entry = |
| 1700 | entries_.Lookup(object, HeapEntriesMap::Hash(object), true); |
| 1701 | if (cache_entry->value == NULL) { |
| 1702 | cache_entry->value = HeapEntriesMap::kHeapEntryPlaceholder; |
| 1703 | } |
| 1704 | } |
| 1705 | |
| 1706 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1707 | HeapSnapshotGenerator::HeapSnapshotGenerator(HeapSnapshot* snapshot, |
| 1708 | v8::ActivityControl* control) |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1709 | : snapshot_(snapshot), |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1710 | control_(control), |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1711 | collection_(snapshot->collection()), |
| 1712 | filler_(NULL) { |
| 1713 | } |
| 1714 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1715 | class SnapshotCounter : public HeapSnapshotGenerator::SnapshotFillerInterface { |
| 1716 | public: |
| 1717 | explicit SnapshotCounter(HeapEntriesMap* entries) |
| 1718 | : entries_(entries) { } |
| 1719 | HeapEntry* AddEntry(HeapObject* obj) { |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 1720 | entries_->Pair(obj, HeapEntriesMap::kHeapEntryPlaceholder); |
| 1721 | return HeapEntriesMap::kHeapEntryPlaceholder; |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1722 | } |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1723 | void SetIndexedReference(HeapGraphEdge::Type, |
| 1724 | HeapObject* parent_obj, |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1725 | HeapEntry*, |
| 1726 | int, |
| 1727 | Object* child_obj, |
| 1728 | HeapEntry*) { |
| 1729 | entries_->CountReference(parent_obj, HeapObject::cast(child_obj)); |
| 1730 | } |
| 1731 | void SetNamedReference(HeapGraphEdge::Type, |
| 1732 | HeapObject* parent_obj, |
| 1733 | HeapEntry*, |
| 1734 | const char*, |
| 1735 | Object* child_obj, |
| 1736 | HeapEntry*) { |
| 1737 | entries_->CountReference(parent_obj, HeapObject::cast(child_obj)); |
| 1738 | } |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1739 | void SetRootShortcutReference(Object* child_obj, HeapEntry*) { |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1740 | entries_->CountReference( |
| 1741 | HeapSnapshot::kInternalRootObject, HeapObject::cast(child_obj)); |
| 1742 | } |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1743 | void SetRootGcRootsReference() { |
| 1744 | entries_->CountReference( |
| 1745 | HeapSnapshot::kInternalRootObject, HeapSnapshot::kGcRootsObject); |
| 1746 | } |
| 1747 | void SetStrongRootReference(Object* child_obj, HeapEntry*) { |
| 1748 | entries_->CountReference( |
| 1749 | HeapSnapshot::kGcRootsObject, HeapObject::cast(child_obj)); |
| 1750 | } |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1751 | private: |
| 1752 | HeapEntriesMap* entries_; |
| 1753 | }; |
| 1754 | |
| 1755 | |
| 1756 | class SnapshotFiller : public HeapSnapshotGenerator::SnapshotFillerInterface { |
| 1757 | public: |
| 1758 | explicit SnapshotFiller(HeapSnapshot* snapshot, HeapEntriesMap* entries) |
| 1759 | : snapshot_(snapshot), |
| 1760 | collection_(snapshot->collection()), |
| 1761 | entries_(entries) { } |
| 1762 | HeapEntry* AddEntry(HeapObject* obj) { |
| 1763 | UNREACHABLE(); |
| 1764 | return NULL; |
| 1765 | } |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1766 | void SetIndexedReference(HeapGraphEdge::Type type, |
| 1767 | HeapObject* parent_obj, |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1768 | HeapEntry* parent_entry, |
| 1769 | int index, |
| 1770 | Object* child_obj, |
| 1771 | HeapEntry* child_entry) { |
| 1772 | int child_index, retainer_index; |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1773 | entries_->CountReference(parent_obj, |
| 1774 | HeapObject::cast(child_obj), |
| 1775 | &child_index, |
| 1776 | &retainer_index); |
| 1777 | parent_entry->SetIndexedReference( |
| 1778 | type, child_index, index, child_entry, retainer_index); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1779 | } |
| 1780 | void SetNamedReference(HeapGraphEdge::Type type, |
| 1781 | HeapObject* parent_obj, |
| 1782 | HeapEntry* parent_entry, |
| 1783 | const char* reference_name, |
| 1784 | Object* child_obj, |
| 1785 | HeapEntry* child_entry) { |
| 1786 | int child_index, retainer_index; |
| 1787 | entries_->CountReference(parent_obj, HeapObject::cast(child_obj), |
| 1788 | &child_index, &retainer_index); |
| 1789 | parent_entry->SetNamedReference(type, |
| 1790 | child_index, |
| 1791 | reference_name, |
| 1792 | child_entry, |
| 1793 | retainer_index); |
| 1794 | } |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1795 | void SetRootGcRootsReference() { |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1796 | int child_index, retainer_index; |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1797 | entries_->CountReference(HeapSnapshot::kInternalRootObject, |
| 1798 | HeapSnapshot::kGcRootsObject, |
| 1799 | &child_index, |
| 1800 | &retainer_index); |
| 1801 | snapshot_->root()->SetIndexedReference(HeapGraphEdge::kElement, |
| 1802 | child_index, |
| 1803 | child_index + 1, |
| 1804 | snapshot_->gc_roots(), |
| 1805 | retainer_index); |
| 1806 | } |
| 1807 | void SetRootShortcutReference(Object* child_obj, |
| 1808 | HeapEntry* child_entry) { |
| 1809 | int child_index, retainer_index; |
| 1810 | entries_->CountReference(HeapSnapshot::kInternalRootObject, |
| 1811 | HeapObject::cast(child_obj), |
| 1812 | &child_index, |
| 1813 | &retainer_index); |
| 1814 | snapshot_->root()->SetNamedReference(HeapGraphEdge::kShortcut, |
| 1815 | child_index, |
| 1816 | collection_->GetName(child_index + 1), |
| 1817 | child_entry, |
| 1818 | retainer_index); |
| 1819 | } |
| 1820 | void SetStrongRootReference(Object* child_obj, |
| 1821 | HeapEntry* child_entry) { |
| 1822 | int child_index, retainer_index; |
| 1823 | entries_->CountReference(HeapSnapshot::kGcRootsObject, |
| 1824 | HeapObject::cast(child_obj), |
| 1825 | &child_index, |
| 1826 | &retainer_index); |
| 1827 | snapshot_->gc_roots()->SetIndexedReference(HeapGraphEdge::kElement, |
| 1828 | child_index, |
| 1829 | child_index + 1, |
| 1830 | child_entry, |
| 1831 | retainer_index); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1832 | } |
| 1833 | private: |
| 1834 | HeapSnapshot* snapshot_; |
| 1835 | HeapSnapshotsCollection* collection_; |
| 1836 | HeapEntriesMap* entries_; |
| 1837 | }; |
| 1838 | |
| 1839 | class SnapshotAllocator { |
| 1840 | public: |
| 1841 | explicit SnapshotAllocator(HeapSnapshot* snapshot) |
| 1842 | : snapshot_(snapshot) { } |
| 1843 | HeapEntry* GetEntry( |
| 1844 | HeapObject* obj, int children_count, int retainers_count) { |
| 1845 | HeapEntry* entry = |
| 1846 | snapshot_->AddEntry(obj, children_count, retainers_count); |
| 1847 | ASSERT(entry != NULL); |
| 1848 | return entry; |
| 1849 | } |
| 1850 | private: |
| 1851 | HeapSnapshot* snapshot_; |
| 1852 | }; |
| 1853 | |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1854 | class RootsReferencesExtractor : public ObjectVisitor { |
| 1855 | public: |
| 1856 | explicit RootsReferencesExtractor(HeapSnapshotGenerator* generator) |
| 1857 | : generator_(generator) { |
| 1858 | } |
| 1859 | void VisitPointers(Object** start, Object** end) { |
| 1860 | for (Object** p = start; p < end; p++) generator_->SetGcRootsReference(*p); |
| 1861 | } |
| 1862 | private: |
| 1863 | HeapSnapshotGenerator* generator_; |
| 1864 | }; |
| 1865 | |
| 1866 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1867 | bool HeapSnapshotGenerator::GenerateSnapshot() { |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1868 | AssertNoAllocation no_alloc; |
| 1869 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1870 | SetProgressTotal(4); // 2 passes + dominators + sizes. |
| 1871 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1872 | // Pass 1. Iterate heap contents to count entries and references. |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1873 | if (!CountEntriesAndReferences()) return false; |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1874 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1875 | // Allocate and fill entries in the snapshot, allocate references. |
| 1876 | snapshot_->AllocateEntries(entries_.entries_count(), |
| 1877 | entries_.total_children_count(), |
| 1878 | entries_.total_retainers_count()); |
| 1879 | SnapshotAllocator allocator(snapshot_); |
| 1880 | entries_.UpdateEntries(&allocator); |
| 1881 | |
| 1882 | // Pass 2. Fill references. |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1883 | if (!FillReferences()) return false; |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1884 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1885 | if (!SetEntriesDominators()) return false; |
| 1886 | if (!ApproximateRetainedSizes()) return false; |
| 1887 | |
| 1888 | progress_counter_ = progress_total_; |
| 1889 | if (!ReportProgress(true)) return false; |
| 1890 | return true; |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1891 | } |
| 1892 | |
| 1893 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1894 | HeapEntry* HeapSnapshotGenerator::GetEntry(Object* obj) { |
| 1895 | if (!obj->IsHeapObject()) return NULL; |
| 1896 | HeapObject* object = HeapObject::cast(obj); |
| 1897 | HeapEntry* entry = entries_.Map(object); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1898 | // A new entry. |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1899 | if (entry == NULL) entry = filler_->AddEntry(object); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1900 | return entry; |
| 1901 | } |
| 1902 | |
| 1903 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1904 | class IndexedReferencesExtractor : public ObjectVisitor { |
| 1905 | public: |
| 1906 | IndexedReferencesExtractor(HeapSnapshotGenerator* generator, |
| 1907 | HeapObject* parent_obj, |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1908 | HeapEntry* parent_entry, |
| 1909 | HeapObjectsSet* known_references = NULL) |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1910 | : generator_(generator), |
| 1911 | parent_obj_(parent_obj), |
| 1912 | parent_(parent_entry), |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1913 | known_references_(known_references), |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1914 | next_index_(1) { |
| 1915 | } |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1916 | void VisitPointers(Object** start, Object** end) { |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1917 | for (Object** p = start; p < end; p++) { |
| 1918 | if (!known_references_ || !known_references_->Contains(*p)) { |
| 1919 | generator_->SetHiddenReference(parent_obj_, parent_, next_index_++, *p); |
| 1920 | } |
| 1921 | } |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1922 | } |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1923 | private: |
| 1924 | HeapSnapshotGenerator* generator_; |
| 1925 | HeapObject* parent_obj_; |
| 1926 | HeapEntry* parent_; |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1927 | HeapObjectsSet* known_references_; |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1928 | int next_index_; |
| 1929 | }; |
| 1930 | |
| 1931 | |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1932 | void HeapSnapshotGenerator::ExtractReferences(HeapObject* obj) { |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1933 | HeapEntry* entry = GetEntry(obj); |
| 1934 | if (entry == NULL) return; // No interest in this object. |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1935 | |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1936 | known_references_.Clear(); |
| 1937 | if (obj->IsJSGlobalProxy()) { |
| 1938 | // We need to reference JS global objects from snapshot's root. |
| 1939 | // We use JSGlobalProxy because this is what embedder (e.g. browser) |
| 1940 | // uses for the global object. |
| 1941 | JSGlobalProxy* proxy = JSGlobalProxy::cast(obj); |
| 1942 | SetRootShortcutReference(proxy->map()->prototype()); |
| 1943 | IndexedReferencesExtractor refs_extractor(this, obj, entry); |
| 1944 | obj->Iterate(&refs_extractor); |
| 1945 | } else if (obj->IsJSObject()) { |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1946 | JSObject* js_obj = JSObject::cast(obj); |
| 1947 | ExtractClosureReferences(js_obj, entry); |
| 1948 | ExtractPropertyReferences(js_obj, entry); |
| 1949 | ExtractElementReferences(js_obj, entry); |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 1950 | ExtractInternalReferences(js_obj, entry); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1951 | SetPropertyReference( |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 1952 | obj, entry, Heap::Proto_symbol(), js_obj->GetPrototype()); |
| 1953 | if (obj->IsJSFunction()) { |
| 1954 | JSFunction* js_fun = JSFunction::cast(obj); |
| 1955 | if (js_fun->has_prototype()) { |
| 1956 | SetPropertyReference( |
| 1957 | obj, entry, Heap::prototype_symbol(), js_fun->prototype()); |
| 1958 | } |
| 1959 | } |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1960 | IndexedReferencesExtractor refs_extractor( |
| 1961 | this, obj, entry, &known_references_); |
| 1962 | obj->Iterate(&refs_extractor); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1963 | } else if (obj->IsString()) { |
| 1964 | if (obj->IsConsString()) { |
| 1965 | ConsString* cs = ConsString::cast(obj); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1966 | SetInternalReference(obj, entry, 1, cs->first()); |
| 1967 | SetInternalReference(obj, entry, 2, cs->second()); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1968 | } |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1969 | } else { |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1970 | IndexedReferencesExtractor refs_extractor(this, obj, entry); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1971 | obj->Iterate(&refs_extractor); |
| 1972 | } |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1973 | } |
| 1974 | |
| 1975 | |
| 1976 | void HeapSnapshotGenerator::ExtractClosureReferences(JSObject* js_obj, |
| 1977 | HeapEntry* entry) { |
| 1978 | if (js_obj->IsJSFunction()) { |
| 1979 | HandleScope hs; |
| 1980 | JSFunction* func = JSFunction::cast(js_obj); |
| 1981 | Context* context = func->context(); |
| 1982 | ZoneScope zscope(DELETE_ON_EXIT); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 1983 | SerializedScopeInfo* serialized_scope_info = |
| 1984 | context->closure()->shared()->scope_info(); |
| 1985 | ScopeInfo<ZoneListAllocationPolicy> zone_scope_info(serialized_scope_info); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 1986 | int locals_number = zone_scope_info.NumberOfLocals(); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1987 | for (int i = 0; i < locals_number; ++i) { |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 1988 | String* local_name = *zone_scope_info.LocalName(i); |
ager@chromium.org | b573749 | 2010-07-15 09:29:43 +0000 | [diff] [blame] | 1989 | int idx = serialized_scope_info->ContextSlotIndex(local_name, NULL); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1990 | if (idx >= 0 && idx < context->length()) { |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1991 | SetClosureReference(js_obj, entry, local_name, context->get(idx)); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1992 | } |
| 1993 | } |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1994 | SetInternalReference(js_obj, entry, "code", func->shared()); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1995 | } |
| 1996 | } |
| 1997 | |
| 1998 | |
| 1999 | void HeapSnapshotGenerator::ExtractPropertyReferences(JSObject* js_obj, |
| 2000 | HeapEntry* entry) { |
| 2001 | if (js_obj->HasFastProperties()) { |
| 2002 | DescriptorArray* descs = js_obj->map()->instance_descriptors(); |
| 2003 | for (int i = 0; i < descs->number_of_descriptors(); i++) { |
| 2004 | switch (descs->GetType(i)) { |
| 2005 | case FIELD: { |
| 2006 | int index = descs->GetFieldIndex(i); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 2007 | SetPropertyReference( |
| 2008 | js_obj, entry, descs->GetKey(i), js_obj->FastPropertyAt(index)); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 2009 | break; |
| 2010 | } |
| 2011 | case CONSTANT_FUNCTION: |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 2012 | SetPropertyReference( |
| 2013 | js_obj, entry, descs->GetKey(i), descs->GetConstantFunction(i)); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 2014 | break; |
| 2015 | default: ; |
| 2016 | } |
| 2017 | } |
| 2018 | } else { |
| 2019 | StringDictionary* dictionary = js_obj->property_dictionary(); |
| 2020 | int length = dictionary->Capacity(); |
| 2021 | for (int i = 0; i < length; ++i) { |
| 2022 | Object* k = dictionary->KeyAt(i); |
| 2023 | if (dictionary->IsKey(k)) { |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 2024 | Object* target = dictionary->ValueAt(i); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 2025 | SetPropertyReference( |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 2026 | js_obj, entry, String::cast(k), target); |
| 2027 | // We assume that global objects can only have slow properties. |
| 2028 | if (target->IsJSGlobalPropertyCell()) { |
| 2029 | SetPropertyShortcutReference(js_obj, |
| 2030 | entry, |
| 2031 | String::cast(k), |
| 2032 | JSGlobalPropertyCell::cast( |
| 2033 | target)->value()); |
| 2034 | } |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 2035 | } |
| 2036 | } |
| 2037 | } |
| 2038 | } |
| 2039 | |
| 2040 | |
| 2041 | void HeapSnapshotGenerator::ExtractElementReferences(JSObject* js_obj, |
| 2042 | HeapEntry* entry) { |
| 2043 | if (js_obj->HasFastElements()) { |
| 2044 | FixedArray* elements = FixedArray::cast(js_obj->elements()); |
| 2045 | int length = js_obj->IsJSArray() ? |
| 2046 | Smi::cast(JSArray::cast(js_obj)->length())->value() : |
| 2047 | elements->length(); |
| 2048 | for (int i = 0; i < length; ++i) { |
| 2049 | if (!elements->get(i)->IsTheHole()) { |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 2050 | SetElementReference(js_obj, entry, i, elements->get(i)); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 2051 | } |
| 2052 | } |
| 2053 | } else if (js_obj->HasDictionaryElements()) { |
| 2054 | NumberDictionary* dictionary = js_obj->element_dictionary(); |
| 2055 | int length = dictionary->Capacity(); |
| 2056 | for (int i = 0; i < length; ++i) { |
| 2057 | Object* k = dictionary->KeyAt(i); |
| 2058 | if (dictionary->IsKey(k)) { |
| 2059 | ASSERT(k->IsNumber()); |
| 2060 | uint32_t index = static_cast<uint32_t>(k->Number()); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 2061 | SetElementReference(js_obj, entry, index, dictionary->ValueAt(i)); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 2062 | } |
| 2063 | } |
| 2064 | } |
| 2065 | } |
| 2066 | |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 2067 | |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 2068 | void HeapSnapshotGenerator::ExtractInternalReferences(JSObject* js_obj, |
| 2069 | HeapEntry* entry) { |
| 2070 | int length = js_obj->GetInternalFieldCount(); |
| 2071 | for (int i = 0; i < length; ++i) { |
| 2072 | Object* o = js_obj->GetInternalField(i); |
| 2073 | SetInternalReference(js_obj, entry, i, o); |
| 2074 | } |
| 2075 | } |
| 2076 | |
| 2077 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 2078 | void HeapSnapshotGenerator::SetClosureReference(HeapObject* parent_obj, |
| 2079 | HeapEntry* parent_entry, |
| 2080 | String* reference_name, |
| 2081 | Object* child_obj) { |
| 2082 | HeapEntry* child_entry = GetEntry(child_obj); |
| 2083 | if (child_entry != NULL) { |
| 2084 | filler_->SetNamedReference(HeapGraphEdge::kContextVariable, |
| 2085 | parent_obj, |
| 2086 | parent_entry, |
| 2087 | collection_->GetName(reference_name), |
| 2088 | child_obj, |
| 2089 | child_entry); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 2090 | known_references_.Insert(child_obj); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 2091 | } |
| 2092 | } |
| 2093 | |
| 2094 | |
| 2095 | void HeapSnapshotGenerator::SetElementReference(HeapObject* parent_obj, |
| 2096 | HeapEntry* parent_entry, |
| 2097 | int index, |
| 2098 | Object* child_obj) { |
| 2099 | HeapEntry* child_entry = GetEntry(child_obj); |
| 2100 | if (child_entry != NULL) { |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 2101 | filler_->SetIndexedReference(HeapGraphEdge::kElement, |
| 2102 | parent_obj, |
| 2103 | parent_entry, |
| 2104 | index, |
| 2105 | child_obj, |
| 2106 | child_entry); |
| 2107 | known_references_.Insert(child_obj); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 2108 | } |
| 2109 | } |
| 2110 | |
| 2111 | |
| 2112 | void HeapSnapshotGenerator::SetInternalReference(HeapObject* parent_obj, |
| 2113 | HeapEntry* parent_entry, |
| 2114 | const char* reference_name, |
| 2115 | Object* child_obj) { |
| 2116 | HeapEntry* child_entry = GetEntry(child_obj); |
| 2117 | if (child_entry != NULL) { |
| 2118 | filler_->SetNamedReference(HeapGraphEdge::kInternal, |
| 2119 | parent_obj, |
| 2120 | parent_entry, |
| 2121 | reference_name, |
| 2122 | child_obj, |
| 2123 | child_entry); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 2124 | known_references_.Insert(child_obj); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 2125 | } |
| 2126 | } |
| 2127 | |
| 2128 | |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 2129 | void HeapSnapshotGenerator::SetInternalReference(HeapObject* parent_obj, |
| 2130 | HeapEntry* parent_entry, |
| 2131 | int index, |
| 2132 | Object* child_obj) { |
| 2133 | HeapEntry* child_entry = GetEntry(child_obj); |
| 2134 | if (child_entry != NULL) { |
| 2135 | filler_->SetNamedReference(HeapGraphEdge::kInternal, |
| 2136 | parent_obj, |
| 2137 | parent_entry, |
| 2138 | collection_->GetName(index), |
| 2139 | child_obj, |
| 2140 | child_entry); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 2141 | known_references_.Insert(child_obj); |
| 2142 | } |
| 2143 | } |
| 2144 | |
| 2145 | |
| 2146 | void HeapSnapshotGenerator::SetHiddenReference(HeapObject* parent_obj, |
| 2147 | HeapEntry* parent_entry, |
| 2148 | int index, |
| 2149 | Object* child_obj) { |
| 2150 | HeapEntry* child_entry = GetEntry(child_obj); |
| 2151 | if (child_entry != NULL) { |
| 2152 | filler_->SetIndexedReference(HeapGraphEdge::kHidden, |
| 2153 | parent_obj, |
| 2154 | parent_entry, |
| 2155 | index, |
| 2156 | child_obj, |
| 2157 | child_entry); |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 2158 | } |
| 2159 | } |
| 2160 | |
| 2161 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 2162 | void HeapSnapshotGenerator::SetPropertyReference(HeapObject* parent_obj, |
| 2163 | HeapEntry* parent_entry, |
| 2164 | String* reference_name, |
| 2165 | Object* child_obj) { |
| 2166 | HeapEntry* child_entry = GetEntry(child_obj); |
| 2167 | if (child_entry != NULL) { |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 2168 | HeapGraphEdge::Type type = reference_name->length() > 0 ? |
| 2169 | HeapGraphEdge::kProperty : HeapGraphEdge::kInternal; |
| 2170 | filler_->SetNamedReference(type, |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 2171 | parent_obj, |
| 2172 | parent_entry, |
| 2173 | collection_->GetName(reference_name), |
| 2174 | child_obj, |
| 2175 | child_entry); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 2176 | known_references_.Insert(child_obj); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 2177 | } |
| 2178 | } |
| 2179 | |
| 2180 | |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 2181 | void HeapSnapshotGenerator::SetPropertyShortcutReference( |
| 2182 | HeapObject* parent_obj, |
| 2183 | HeapEntry* parent_entry, |
| 2184 | String* reference_name, |
| 2185 | Object* child_obj) { |
| 2186 | HeapEntry* child_entry = GetEntry(child_obj); |
| 2187 | if (child_entry != NULL) { |
| 2188 | filler_->SetNamedReference(HeapGraphEdge::kShortcut, |
| 2189 | parent_obj, |
| 2190 | parent_entry, |
| 2191 | collection_->GetName(reference_name), |
| 2192 | child_obj, |
| 2193 | child_entry); |
| 2194 | } |
| 2195 | } |
| 2196 | |
| 2197 | |
| 2198 | void HeapSnapshotGenerator::SetRootGcRootsReference() { |
| 2199 | filler_->SetRootGcRootsReference(); |
| 2200 | } |
| 2201 | |
| 2202 | |
| 2203 | void HeapSnapshotGenerator::SetRootShortcutReference(Object* child_obj) { |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 2204 | HeapEntry* child_entry = GetEntry(child_obj); |
| 2205 | ASSERT(child_entry != NULL); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 2206 | filler_->SetRootShortcutReference(child_obj, child_entry); |
| 2207 | } |
| 2208 | |
| 2209 | |
| 2210 | void HeapSnapshotGenerator::SetGcRootsReference(Object* child_obj) { |
| 2211 | HeapEntry* child_entry = GetEntry(child_obj); |
| 2212 | if (child_entry != NULL) { |
| 2213 | filler_->SetStrongRootReference(child_obj, child_entry); |
| 2214 | } |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 2215 | } |
| 2216 | |
| 2217 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2218 | void HeapSnapshotGenerator::SetProgressTotal(int iterations_count) { |
| 2219 | if (control_ == NULL) return; |
| 2220 | |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 2221 | HeapIterator iterator(HeapIterator::kFilterUnreachable); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2222 | int objects_count = 0; |
| 2223 | for (HeapObject* obj = iterator.next(); |
| 2224 | obj != NULL; |
| 2225 | obj = iterator.next(), ++objects_count) {} |
| 2226 | progress_total_ = objects_count * iterations_count; |
| 2227 | progress_counter_ = 0; |
| 2228 | } |
| 2229 | |
| 2230 | |
| 2231 | bool HeapSnapshotGenerator::CountEntriesAndReferences() { |
| 2232 | SnapshotCounter counter(&entries_); |
| 2233 | filler_ = &counter; |
| 2234 | filler_->AddEntry(HeapSnapshot::kInternalRootObject); |
| 2235 | filler_->AddEntry(HeapSnapshot::kGcRootsObject); |
| 2236 | return IterateAndExtractReferences(); |
| 2237 | } |
| 2238 | |
| 2239 | |
| 2240 | bool HeapSnapshotGenerator::FillReferences() { |
| 2241 | SnapshotFiller filler(snapshot_, &entries_); |
| 2242 | filler_ = &filler; |
| 2243 | return IterateAndExtractReferences(); |
| 2244 | } |
| 2245 | |
| 2246 | |
| 2247 | void HeapSnapshotGenerator::FillReversePostorderIndexes( |
| 2248 | Vector<HeapEntry*>* entries) { |
| 2249 | snapshot_->ClearPaint(); |
| 2250 | int current_entry = 0; |
| 2251 | List<HeapEntry*> nodes_to_visit; |
| 2252 | nodes_to_visit.Add(snapshot_->root()); |
| 2253 | snapshot_->root()->paint_reachable(); |
| 2254 | while (!nodes_to_visit.is_empty()) { |
| 2255 | HeapEntry* entry = nodes_to_visit.last(); |
| 2256 | Vector<HeapGraphEdge> children = entry->children(); |
| 2257 | bool has_new_edges = false; |
| 2258 | for (int i = 0; i < children.length(); ++i) { |
| 2259 | if (children[i].type() == HeapGraphEdge::kShortcut) continue; |
| 2260 | HeapEntry* child = children[i].to(); |
| 2261 | if (!child->painted_reachable()) { |
| 2262 | nodes_to_visit.Add(child); |
| 2263 | child->paint_reachable(); |
| 2264 | has_new_edges = true; |
| 2265 | } |
| 2266 | } |
| 2267 | if (!has_new_edges) { |
| 2268 | entry->set_ordered_index(current_entry); |
| 2269 | (*entries)[current_entry++] = entry; |
| 2270 | nodes_to_visit.RemoveLast(); |
| 2271 | } |
| 2272 | } |
| 2273 | entries->Truncate(current_entry); |
| 2274 | } |
| 2275 | |
| 2276 | |
| 2277 | static int Intersect(int i1, int i2, const Vector<HeapEntry*>& dominators) { |
| 2278 | int finger1 = i1, finger2 = i2; |
| 2279 | while (finger1 != finger2) { |
| 2280 | while (finger1 < finger2) finger1 = dominators[finger1]->ordered_index(); |
| 2281 | while (finger2 < finger1) finger2 = dominators[finger2]->ordered_index(); |
| 2282 | } |
| 2283 | return finger1; |
| 2284 | } |
| 2285 | |
| 2286 | // The algorithm is based on the article: |
| 2287 | // K. Cooper, T. Harvey and K. Kennedy "A Simple, Fast Dominance Algorithm" |
| 2288 | // Softw. Pract. Exper. 4 (2001), pp. 1–10. |
| 2289 | bool HeapSnapshotGenerator::BuildDominatorTree( |
| 2290 | const Vector<HeapEntry*>& entries, |
| 2291 | Vector<HeapEntry*>* dominators) { |
| 2292 | if (entries.length() == 0) return true; |
| 2293 | const int entries_length = entries.length(), root_index = entries_length - 1; |
| 2294 | for (int i = 0; i < root_index; ++i) (*dominators)[i] = NULL; |
| 2295 | (*dominators)[root_index] = entries[root_index]; |
| 2296 | int changed = 1; |
| 2297 | const int base_progress_counter = progress_counter_; |
| 2298 | while (changed != 0) { |
| 2299 | changed = 0; |
| 2300 | for (int i = root_index - 1; i >= 0; --i) { |
| 2301 | HeapEntry* new_idom = NULL; |
| 2302 | Vector<HeapGraphEdge*> rets = entries[i]->retainers(); |
| 2303 | int j = 0; |
| 2304 | for (; j < rets.length(); ++j) { |
| 2305 | if (rets[j]->type() == HeapGraphEdge::kShortcut) continue; |
| 2306 | HeapEntry* ret = rets[j]->From(); |
| 2307 | if (dominators->at(ret->ordered_index()) != NULL) { |
| 2308 | new_idom = ret; |
| 2309 | break; |
| 2310 | } |
| 2311 | } |
| 2312 | for (++j; j < rets.length(); ++j) { |
| 2313 | if (rets[j]->type() == HeapGraphEdge::kShortcut) continue; |
| 2314 | HeapEntry* ret = rets[j]->From(); |
| 2315 | if (dominators->at(ret->ordered_index()) != NULL) { |
| 2316 | new_idom = entries[Intersect(ret->ordered_index(), |
| 2317 | new_idom->ordered_index(), |
| 2318 | *dominators)]; |
| 2319 | } |
| 2320 | } |
| 2321 | if (new_idom != NULL && dominators->at(i) != new_idom) { |
| 2322 | (*dominators)[i] = new_idom; |
| 2323 | ++changed; |
| 2324 | } |
| 2325 | } |
| 2326 | int remaining = entries_length - changed; |
| 2327 | if (remaining < 0) remaining = 0; |
| 2328 | progress_counter_ = base_progress_counter + remaining; |
| 2329 | if (!ReportProgress(true)) return false; |
| 2330 | } |
| 2331 | return true; |
| 2332 | } |
| 2333 | |
| 2334 | |
| 2335 | bool HeapSnapshotGenerator::SetEntriesDominators() { |
| 2336 | // This array is used for maintaining reverse postorder of nodes. |
| 2337 | ScopedVector<HeapEntry*> ordered_entries(snapshot_->entries()->length()); |
| 2338 | FillReversePostorderIndexes(&ordered_entries); |
| 2339 | ScopedVector<HeapEntry*> dominators(ordered_entries.length()); |
| 2340 | if (!BuildDominatorTree(ordered_entries, &dominators)) return false; |
| 2341 | for (int i = 0; i < ordered_entries.length(); ++i) { |
| 2342 | ASSERT(dominators[i] != NULL); |
| 2343 | ordered_entries[i]->set_dominator(dominators[i]); |
| 2344 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2345 | return true; |
| 2346 | } |
| 2347 | |
| 2348 | |
| 2349 | bool HeapSnapshotGenerator::ApproximateRetainedSizes() { |
| 2350 | // As for the dominators tree we only know parent nodes, not |
| 2351 | // children, to sum up total sizes we "bubble" node's self size |
| 2352 | // adding it to all of its parents. |
| 2353 | for (int i = 0; i < snapshot_->entries()->length(); ++i) { |
| 2354 | HeapEntry* entry = snapshot_->entries()->at(i); |
| 2355 | entry->set_retained_size(entry->self_size()); |
| 2356 | } |
| 2357 | for (int i = 0; |
| 2358 | i < snapshot_->entries()->length(); |
| 2359 | ++i, IncProgressCounter()) { |
| 2360 | HeapEntry* entry = snapshot_->entries()->at(i); |
| 2361 | int entry_size = entry->self_size(); |
| 2362 | for (HeapEntry* dominator = entry->dominator(); |
| 2363 | dominator != entry; |
| 2364 | entry = dominator, dominator = entry->dominator()) { |
| 2365 | dominator->add_retained_size(entry_size); |
| 2366 | } |
| 2367 | if (!ReportProgress()) return false; |
| 2368 | } |
| 2369 | return true; |
| 2370 | } |
| 2371 | |
| 2372 | |
| 2373 | bool HeapSnapshotGenerator::IterateAndExtractReferences() { |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 2374 | HeapIterator iterator(HeapIterator::kFilterUnreachable); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2375 | bool interrupted = false; |
whesse@chromium.org | 023421e | 2010-12-21 12:19:12 +0000 | [diff] [blame] | 2376 | // Heap iteration with filtering must be finished in any case. |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2377 | for (HeapObject* obj = iterator.next(); |
| 2378 | obj != NULL; |
| 2379 | obj = iterator.next(), IncProgressCounter()) { |
| 2380 | if (!interrupted) { |
| 2381 | ExtractReferences(obj); |
| 2382 | if (!ReportProgress()) interrupted = true; |
| 2383 | } |
| 2384 | } |
| 2385 | if (interrupted) return false; |
| 2386 | SetRootGcRootsReference(); |
| 2387 | RootsReferencesExtractor extractor(this); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 2388 | Heap::IterateRoots(&extractor, VISIT_ALL); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 2389 | return ReportProgress(); |
| 2390 | } |
| 2391 | |
| 2392 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 2393 | void HeapSnapshotsDiff::CreateRoots(int additions_count, int deletions_count) { |
| 2394 | raw_additions_root_ = |
| 2395 | NewArray<char>(HeapEntry::EntriesSize(1, additions_count, 0)); |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 2396 | additions_root()->Init( |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 2397 | snapshot2_, HeapEntry::kHidden, "", 0, 0, additions_count, 0); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 2398 | raw_deletions_root_ = |
| 2399 | NewArray<char>(HeapEntry::EntriesSize(1, deletions_count, 0)); |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 2400 | deletions_root()->Init( |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 2401 | snapshot1_, HeapEntry::kHidden, "", 0, 0, deletions_count, 0); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 2402 | } |
| 2403 | |
| 2404 | |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 2405 | static void DeleteHeapSnapshotsDiff(HeapSnapshotsDiff** diff_ptr) { |
| 2406 | delete *diff_ptr; |
| 2407 | } |
| 2408 | |
| 2409 | HeapSnapshotsComparator::~HeapSnapshotsComparator() { |
| 2410 | diffs_.Iterate(DeleteHeapSnapshotsDiff); |
| 2411 | } |
| 2412 | |
| 2413 | |
| 2414 | HeapSnapshotsDiff* HeapSnapshotsComparator::Compare(HeapSnapshot* snapshot1, |
| 2415 | HeapSnapshot* snapshot2) { |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 2416 | snapshot1->ClearPaint(); |
| 2417 | snapshot1->root()->PaintAllReachable(); |
| 2418 | snapshot2->ClearPaint(); |
| 2419 | snapshot2->root()->PaintAllReachable(); |
| 2420 | |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 2421 | List<HeapEntry*>* entries1 = snapshot1->GetSortedEntriesList(); |
| 2422 | List<HeapEntry*>* entries2 = snapshot2->GetSortedEntriesList(); |
| 2423 | int i = 0, j = 0; |
| 2424 | List<HeapEntry*> added_entries, deleted_entries; |
| 2425 | while (i < entries1->length() && j < entries2->length()) { |
| 2426 | uint64_t id1 = entries1->at(i)->id(); |
| 2427 | uint64_t id2 = entries2->at(j)->id(); |
| 2428 | if (id1 == id2) { |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 2429 | HeapEntry* entry1 = entries1->at(i++); |
| 2430 | HeapEntry* entry2 = entries2->at(j++); |
| 2431 | if (entry1->painted_reachable() != entry2->painted_reachable()) { |
| 2432 | if (entry1->painted_reachable()) |
| 2433 | deleted_entries.Add(entry1); |
| 2434 | else |
| 2435 | added_entries.Add(entry2); |
| 2436 | } |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 2437 | } else if (id1 < id2) { |
| 2438 | HeapEntry* entry = entries1->at(i++); |
| 2439 | deleted_entries.Add(entry); |
| 2440 | } else { |
| 2441 | HeapEntry* entry = entries2->at(j++); |
| 2442 | added_entries.Add(entry); |
| 2443 | } |
| 2444 | } |
| 2445 | while (i < entries1->length()) { |
| 2446 | HeapEntry* entry = entries1->at(i++); |
| 2447 | deleted_entries.Add(entry); |
| 2448 | } |
| 2449 | while (j < entries2->length()) { |
| 2450 | HeapEntry* entry = entries2->at(j++); |
| 2451 | added_entries.Add(entry); |
| 2452 | } |
| 2453 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 2454 | HeapSnapshotsDiff* diff = new HeapSnapshotsDiff(snapshot1, snapshot2); |
| 2455 | diffs_.Add(diff); |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 2456 | diff->CreateRoots(added_entries.length(), deleted_entries.length()); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 2457 | |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 2458 | for (int i = 0; i < deleted_entries.length(); ++i) { |
| 2459 | HeapEntry* entry = deleted_entries[i]; |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 2460 | diff->AddDeletedEntry(i, i + 1, entry); |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 2461 | } |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 2462 | for (int i = 0; i < added_entries.length(); ++i) { |
| 2463 | HeapEntry* entry = added_entries[i]; |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 2464 | diff->AddAddedEntry(i, i + 1, entry); |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 2465 | } |
| 2466 | return diff; |
| 2467 | } |
| 2468 | |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 2469 | |
| 2470 | class OutputStreamWriter { |
| 2471 | public: |
| 2472 | explicit OutputStreamWriter(v8::OutputStream* stream) |
| 2473 | : stream_(stream), |
| 2474 | chunk_size_(stream->GetChunkSize()), |
| 2475 | chunk_(chunk_size_), |
| 2476 | chunk_pos_(0), |
| 2477 | aborted_(false) { |
| 2478 | ASSERT(chunk_size_ > 0); |
| 2479 | } |
| 2480 | bool aborted() { return aborted_; } |
| 2481 | void AddCharacter(char c) { |
| 2482 | ASSERT(c != '\0'); |
| 2483 | ASSERT(chunk_pos_ < chunk_size_); |
| 2484 | chunk_[chunk_pos_++] = c; |
| 2485 | MaybeWriteChunk(); |
| 2486 | } |
| 2487 | void AddString(const char* s) { |
| 2488 | AddSubstring(s, StrLength(s)); |
| 2489 | } |
| 2490 | void AddSubstring(const char* s, int n) { |
| 2491 | if (n <= 0) return; |
| 2492 | ASSERT(static_cast<size_t>(n) <= strlen(s)); |
| 2493 | const char* s_end = s + n; |
| 2494 | while (s < s_end) { |
| 2495 | int s_chunk_size = Min( |
| 2496 | chunk_size_ - chunk_pos_, static_cast<int>(s_end - s)); |
| 2497 | ASSERT(s_chunk_size > 0); |
| 2498 | memcpy(chunk_.start() + chunk_pos_, s, s_chunk_size); |
| 2499 | s += s_chunk_size; |
| 2500 | chunk_pos_ += s_chunk_size; |
| 2501 | MaybeWriteChunk(); |
| 2502 | } |
| 2503 | } |
| 2504 | void AddNumber(int n) { AddNumberImpl<int>(n, "%d"); } |
| 2505 | void AddNumber(unsigned n) { AddNumberImpl<unsigned>(n, "%u"); } |
| 2506 | void AddNumber(uint64_t n) { AddNumberImpl<uint64_t>(n, "%llu"); } |
| 2507 | void Finalize() { |
| 2508 | if (aborted_) return; |
| 2509 | ASSERT(chunk_pos_ < chunk_size_); |
| 2510 | if (chunk_pos_ != 0) { |
| 2511 | WriteChunk(); |
| 2512 | } |
| 2513 | stream_->EndOfStream(); |
| 2514 | } |
| 2515 | |
| 2516 | private: |
| 2517 | template<typename T> |
| 2518 | void AddNumberImpl(T n, const char* format) { |
| 2519 | ScopedVector<char> buffer(32); |
| 2520 | int result = OS::SNPrintF(buffer, format, n); |
| 2521 | USE(result); |
| 2522 | ASSERT(result != -1); |
| 2523 | AddString(buffer.start()); |
| 2524 | } |
| 2525 | void MaybeWriteChunk() { |
| 2526 | ASSERT(chunk_pos_ <= chunk_size_); |
| 2527 | if (chunk_pos_ == chunk_size_) { |
| 2528 | WriteChunk(); |
| 2529 | chunk_pos_ = 0; |
| 2530 | } |
| 2531 | } |
| 2532 | void WriteChunk() { |
| 2533 | if (aborted_) return; |
| 2534 | if (stream_->WriteAsciiChunk(chunk_.start(), chunk_pos_) == |
| 2535 | v8::OutputStream::kAbort) aborted_ = true; |
| 2536 | } |
| 2537 | |
| 2538 | v8::OutputStream* stream_; |
| 2539 | int chunk_size_; |
| 2540 | ScopedVector<char> chunk_; |
| 2541 | int chunk_pos_; |
| 2542 | bool aborted_; |
| 2543 | }; |
| 2544 | |
| 2545 | void HeapSnapshotJSONSerializer::Serialize(v8::OutputStream* stream) { |
| 2546 | ASSERT(writer_ == NULL); |
| 2547 | writer_ = new OutputStreamWriter(stream); |
| 2548 | |
| 2549 | // Since nodes graph is cyclic, we need the first pass to enumerate |
| 2550 | // them. Strings can be serialized in one pass. |
| 2551 | EnumerateNodes(); |
| 2552 | SerializeImpl(); |
| 2553 | |
| 2554 | delete writer_; |
| 2555 | writer_ = NULL; |
| 2556 | } |
| 2557 | |
| 2558 | |
| 2559 | void HeapSnapshotJSONSerializer::SerializeImpl() { |
| 2560 | writer_->AddCharacter('{'); |
| 2561 | writer_->AddString("\"snapshot\":{"); |
| 2562 | SerializeSnapshot(); |
| 2563 | if (writer_->aborted()) return; |
| 2564 | writer_->AddString("},\n"); |
| 2565 | writer_->AddString("\"nodes\":["); |
| 2566 | SerializeNodes(); |
| 2567 | if (writer_->aborted()) return; |
| 2568 | writer_->AddString("],\n"); |
| 2569 | writer_->AddString("\"strings\":["); |
| 2570 | SerializeStrings(); |
| 2571 | if (writer_->aborted()) return; |
| 2572 | writer_->AddCharacter(']'); |
| 2573 | writer_->AddCharacter('}'); |
| 2574 | writer_->Finalize(); |
| 2575 | } |
| 2576 | |
| 2577 | |
| 2578 | class HeapSnapshotJSONSerializerEnumerator { |
| 2579 | public: |
| 2580 | explicit HeapSnapshotJSONSerializerEnumerator(HeapSnapshotJSONSerializer* s) |
| 2581 | : s_(s) { |
| 2582 | } |
| 2583 | void Apply(HeapEntry** entry) { |
| 2584 | s_->GetNodeId(*entry); |
| 2585 | } |
| 2586 | private: |
| 2587 | HeapSnapshotJSONSerializer* s_; |
| 2588 | }; |
| 2589 | |
| 2590 | void HeapSnapshotJSONSerializer::EnumerateNodes() { |
| 2591 | GetNodeId(snapshot_->root()); // Make sure root gets the first id. |
| 2592 | HeapSnapshotJSONSerializerEnumerator iter(this); |
| 2593 | snapshot_->IterateEntries(&iter); |
| 2594 | } |
| 2595 | |
| 2596 | |
| 2597 | int HeapSnapshotJSONSerializer::GetNodeId(HeapEntry* entry) { |
| 2598 | HashMap::Entry* cache_entry = nodes_.Lookup(entry, ObjectHash(entry), true); |
| 2599 | if (cache_entry->value == NULL) { |
| 2600 | cache_entry->value = reinterpret_cast<void*>(next_node_id_++); |
| 2601 | } |
| 2602 | return static_cast<int>(reinterpret_cast<intptr_t>(cache_entry->value)); |
| 2603 | } |
| 2604 | |
| 2605 | |
| 2606 | int HeapSnapshotJSONSerializer::GetStringId(const char* s) { |
| 2607 | HashMap::Entry* cache_entry = strings_.Lookup( |
| 2608 | const_cast<char*>(s), ObjectHash(s), true); |
| 2609 | if (cache_entry->value == NULL) { |
| 2610 | cache_entry->value = reinterpret_cast<void*>(next_string_id_++); |
| 2611 | } |
| 2612 | return static_cast<int>(reinterpret_cast<intptr_t>(cache_entry->value)); |
| 2613 | } |
| 2614 | |
| 2615 | |
| 2616 | void HeapSnapshotJSONSerializer::SerializeEdge(HeapGraphEdge* edge) { |
| 2617 | writer_->AddCharacter(','); |
| 2618 | writer_->AddNumber(edge->type()); |
| 2619 | writer_->AddCharacter(','); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 2620 | if (edge->type() == HeapGraphEdge::kElement |
| 2621 | || edge->type() == HeapGraphEdge::kHidden) { |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 2622 | writer_->AddNumber(edge->index()); |
| 2623 | } else { |
| 2624 | writer_->AddNumber(GetStringId(edge->name())); |
| 2625 | } |
| 2626 | writer_->AddCharacter(','); |
| 2627 | writer_->AddNumber(GetNodeId(edge->to())); |
| 2628 | } |
| 2629 | |
| 2630 | |
| 2631 | void HeapSnapshotJSONSerializer::SerializeNode(HeapEntry* entry) { |
| 2632 | writer_->AddCharacter('\n'); |
| 2633 | writer_->AddCharacter(','); |
| 2634 | writer_->AddNumber(entry->type()); |
| 2635 | writer_->AddCharacter(','); |
| 2636 | writer_->AddNumber(GetStringId(entry->name())); |
| 2637 | writer_->AddCharacter(','); |
| 2638 | writer_->AddNumber(entry->id()); |
| 2639 | writer_->AddCharacter(','); |
| 2640 | writer_->AddNumber(entry->self_size()); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 2641 | writer_->AddCharacter(','); |
| 2642 | writer_->AddNumber(entry->RetainedSize(false)); |
| 2643 | writer_->AddCharacter(','); |
| 2644 | writer_->AddNumber(GetNodeId(entry->dominator())); |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 2645 | Vector<HeapGraphEdge> children = entry->children(); |
| 2646 | writer_->AddCharacter(','); |
| 2647 | writer_->AddNumber(children.length()); |
| 2648 | for (int i = 0; i < children.length(); ++i) { |
| 2649 | SerializeEdge(&children[i]); |
| 2650 | if (writer_->aborted()) return; |
| 2651 | } |
| 2652 | } |
| 2653 | |
| 2654 | |
| 2655 | void HeapSnapshotJSONSerializer::SerializeNodes() { |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 2656 | // The first (zero) item of nodes array is an object describing node |
| 2657 | // serialization layout. We use a set of macros to improve |
| 2658 | // readability. |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 2659 | #define JSON_A(s) "["s"]" |
| 2660 | #define JSON_O(s) "{"s"}" |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 2661 | #define JSON_S(s) "\""s"\"" |
| 2662 | writer_->AddString(JSON_O( |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 2663 | JSON_S("fields") ":" JSON_A( |
| 2664 | JSON_S("type") |
| 2665 | "," JSON_S("name") |
| 2666 | "," JSON_S("id") |
| 2667 | "," JSON_S("self_size") |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 2668 | "," JSON_S("retained_size") |
| 2669 | "," JSON_S("dominator") |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 2670 | "," JSON_S("children_count") |
| 2671 | "," JSON_S("children")) |
| 2672 | "," JSON_S("types") ":" JSON_A( |
| 2673 | JSON_A( |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 2674 | JSON_S("hidden") |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 2675 | "," JSON_S("array") |
| 2676 | "," JSON_S("string") |
| 2677 | "," JSON_S("object") |
| 2678 | "," JSON_S("code") |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 2679 | "," JSON_S("closure") |
| 2680 | "," JSON_S("regexp") |
| 2681 | "," JSON_S("number")) |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 2682 | "," JSON_S("string") |
| 2683 | "," JSON_S("number") |
| 2684 | "," JSON_S("number") |
| 2685 | "," JSON_S("number") |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 2686 | "," JSON_S("number") |
| 2687 | "," JSON_S("number") |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 2688 | "," JSON_O( |
| 2689 | JSON_S("fields") ":" JSON_A( |
| 2690 | JSON_S("type") |
| 2691 | "," JSON_S("name_or_index") |
| 2692 | "," JSON_S("to_node")) |
| 2693 | "," JSON_S("types") ":" JSON_A( |
| 2694 | JSON_A( |
| 2695 | JSON_S("context") |
| 2696 | "," JSON_S("element") |
| 2697 | "," JSON_S("property") |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 2698 | "," JSON_S("internal") |
| 2699 | "," JSON_S("hidden") |
| 2700 | "," JSON_S("shortcut")) |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 2701 | "," JSON_S("string_or_number") |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 2702 | "," JSON_S("node")))))); |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 2703 | #undef JSON_S |
| 2704 | #undef JSON_O |
| 2705 | #undef JSON_A |
| 2706 | |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 2707 | const int node_fields_count = 7; |
| 2708 | // type,name,id,self_size,retained_size,dominator,children_count. |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 2709 | const int edge_fields_count = 3; // type,name|index,to_node. |
| 2710 | List<HashMap::Entry*> sorted_nodes; |
| 2711 | SortHashMap(&nodes_, &sorted_nodes); |
| 2712 | // Rewrite node ids, so they refer to actual array positions. |
| 2713 | if (sorted_nodes.length() > 1) { |
| 2714 | // Nodes start from array index 1. |
| 2715 | int prev_value = 1; |
| 2716 | sorted_nodes[0]->value = reinterpret_cast<void*>(prev_value); |
| 2717 | for (int i = 1; i < sorted_nodes.length(); ++i) { |
| 2718 | HeapEntry* prev_heap_entry = |
| 2719 | reinterpret_cast<HeapEntry*>(sorted_nodes[i-1]->key); |
| 2720 | prev_value += node_fields_count + |
| 2721 | prev_heap_entry->children().length() * edge_fields_count; |
| 2722 | sorted_nodes[i]->value = reinterpret_cast<void*>(prev_value); |
| 2723 | } |
| 2724 | } |
| 2725 | for (int i = 0; i < sorted_nodes.length(); ++i) { |
| 2726 | SerializeNode(reinterpret_cast<HeapEntry*>(sorted_nodes[i]->key)); |
| 2727 | if (writer_->aborted()) return; |
| 2728 | } |
| 2729 | } |
| 2730 | |
| 2731 | |
| 2732 | void HeapSnapshotJSONSerializer::SerializeSnapshot() { |
| 2733 | writer_->AddString("\"title\":\""); |
| 2734 | writer_->AddString(snapshot_->title()); |
| 2735 | writer_->AddString("\""); |
| 2736 | writer_->AddString(",\"uid\":"); |
| 2737 | writer_->AddNumber(snapshot_->uid()); |
| 2738 | } |
| 2739 | |
| 2740 | |
| 2741 | static void WriteUChar(OutputStreamWriter* w, unibrow::uchar u) { |
| 2742 | static const char hex_chars[] = "0123456789ABCDEF"; |
| 2743 | w->AddString("\\u"); |
| 2744 | w->AddCharacter(hex_chars[(u >> 12) & 0xf]); |
| 2745 | w->AddCharacter(hex_chars[(u >> 8) & 0xf]); |
| 2746 | w->AddCharacter(hex_chars[(u >> 4) & 0xf]); |
| 2747 | w->AddCharacter(hex_chars[u & 0xf]); |
| 2748 | } |
| 2749 | |
| 2750 | void HeapSnapshotJSONSerializer::SerializeString(const unsigned char* s) { |
| 2751 | writer_->AddCharacter('\n'); |
| 2752 | writer_->AddCharacter('\"'); |
| 2753 | for ( ; *s != '\0'; ++s) { |
| 2754 | switch (*s) { |
| 2755 | case '\b': |
| 2756 | writer_->AddString("\\b"); |
| 2757 | continue; |
| 2758 | case '\f': |
| 2759 | writer_->AddString("\\f"); |
| 2760 | continue; |
| 2761 | case '\n': |
| 2762 | writer_->AddString("\\n"); |
| 2763 | continue; |
| 2764 | case '\r': |
| 2765 | writer_->AddString("\\r"); |
| 2766 | continue; |
| 2767 | case '\t': |
| 2768 | writer_->AddString("\\t"); |
| 2769 | continue; |
| 2770 | case '\"': |
| 2771 | case '\\': |
| 2772 | writer_->AddCharacter('\\'); |
| 2773 | writer_->AddCharacter(*s); |
| 2774 | continue; |
| 2775 | default: |
| 2776 | if (*s > 31 && *s < 128) { |
| 2777 | writer_->AddCharacter(*s); |
| 2778 | } else if (*s <= 31) { |
| 2779 | // Special character with no dedicated literal. |
| 2780 | WriteUChar(writer_, *s); |
| 2781 | } else { |
| 2782 | // Convert UTF-8 into \u UTF-16 literal. |
| 2783 | unsigned length = 1, cursor = 0; |
| 2784 | for ( ; length <= 4 && *(s + length) != '\0'; ++length) { } |
| 2785 | unibrow::uchar c = unibrow::Utf8::CalculateValue(s, length, &cursor); |
| 2786 | if (c != unibrow::Utf8::kBadChar) { |
| 2787 | WriteUChar(writer_, c); |
| 2788 | ASSERT(cursor != 0); |
| 2789 | s += cursor - 1; |
| 2790 | } else { |
| 2791 | writer_->AddCharacter('?'); |
| 2792 | } |
| 2793 | } |
| 2794 | } |
| 2795 | } |
| 2796 | writer_->AddCharacter('\"'); |
| 2797 | } |
| 2798 | |
| 2799 | |
| 2800 | void HeapSnapshotJSONSerializer::SerializeStrings() { |
| 2801 | List<HashMap::Entry*> sorted_strings; |
| 2802 | SortHashMap(&strings_, &sorted_strings); |
| 2803 | writer_->AddString("\"<dummy>\""); |
| 2804 | for (int i = 0; i < sorted_strings.length(); ++i) { |
| 2805 | writer_->AddCharacter(','); |
| 2806 | SerializeString( |
| 2807 | reinterpret_cast<const unsigned char*>(sorted_strings[i]->key)); |
| 2808 | if (writer_->aborted()) return; |
| 2809 | } |
| 2810 | } |
| 2811 | |
| 2812 | |
| 2813 | template<typename T> |
| 2814 | inline static int SortUsingEntryValue(const T* x, const T* y) { |
| 2815 | uintptr_t x_uint = reinterpret_cast<uintptr_t>((*x)->value); |
| 2816 | uintptr_t y_uint = reinterpret_cast<uintptr_t>((*y)->value); |
| 2817 | if (x_uint > y_uint) { |
| 2818 | return 1; |
| 2819 | } else if (x_uint == y_uint) { |
| 2820 | return 0; |
| 2821 | } else { |
| 2822 | return -1; |
| 2823 | } |
| 2824 | } |
| 2825 | |
| 2826 | |
| 2827 | void HeapSnapshotJSONSerializer::SortHashMap( |
| 2828 | HashMap* map, List<HashMap::Entry*>* sorted_entries) { |
| 2829 | for (HashMap::Entry* p = map->Start(); p != NULL; p = map->Next(p)) |
| 2830 | sorted_entries->Add(p); |
| 2831 | sorted_entries->Sort(SortUsingEntryValue); |
| 2832 | } |
| 2833 | |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 2834 | |
| 2835 | String* GetConstructorNameForHeapProfile(JSObject* object) { |
| 2836 | if (object->IsJSFunction()) return Heap::closure_symbol(); |
| 2837 | return object->constructor_name(); |
| 2838 | } |
| 2839 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 2840 | } } // namespace v8::internal |
lrn@chromium.org | 25156de | 2010-04-06 13:10:27 +0000 | [diff] [blame] | 2841 | |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 2842 | #endif // ENABLE_LOGGING_AND_PROFILING |