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