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