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