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 | |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 934 | HeapGraphEdge::HeapGraphEdge(Type type, const char* name, int from, int to) |
| 935 | : type_(type), |
| 936 | from_index_(from), |
| 937 | to_index_(to), |
| 938 | name_(name) { |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 939 | ASSERT(type == kContextVariable |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 940 | || type == kProperty |
| 941 | || type == kInternal |
| 942 | || type == kShortcut); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 946 | HeapGraphEdge::HeapGraphEdge(Type type, int index, int from, int to) |
| 947 | : type_(type), |
| 948 | from_index_(from), |
| 949 | to_index_(to), |
| 950 | index_(index) { |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 951 | ASSERT(type == kElement || type == kHidden || type == kWeak); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 955 | void HeapGraphEdge::ReplaceToIndexWithEntry(HeapSnapshot* snapshot) { |
| 956 | to_entry_ = &snapshot->entries()[to_index_]; |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 957 | } |
| 958 | |
| 959 | |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 960 | const int HeapEntry::kNoEntry = -1; |
| 961 | |
| 962 | HeapEntry::HeapEntry(HeapSnapshot* snapshot, |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 963 | Type type, |
| 964 | const char* name, |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 965 | SnapshotObjectId id, |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 966 | int self_size) |
mstarzinger@chromium.org | 15613d0 | 2012-05-23 12:04:37 +0000 | [diff] [blame^] | 967 | : type_(type), |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 968 | children_count_(0), |
| 969 | children_index_(-1), |
| 970 | self_size_(self_size), |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 971 | id_(id), |
| 972 | snapshot_(snapshot), |
| 973 | name_(name) { } |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 974 | |
| 975 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 976 | void HeapEntry::SetNamedReference(HeapGraphEdge::Type type, |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 977 | const char* name, |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 978 | HeapEntry* entry) { |
| 979 | HeapGraphEdge edge(type, name, this->index(), entry->index()); |
| 980 | snapshot_->edges().Add(edge); |
| 981 | ++children_count_; |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 982 | } |
| 983 | |
| 984 | |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 985 | void HeapEntry::SetIndexedReference(HeapGraphEdge::Type type, |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 986 | int index, |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 987 | HeapEntry* entry) { |
| 988 | HeapGraphEdge edge(type, index, this->index(), entry->index()); |
| 989 | snapshot_->edges().Add(edge); |
| 990 | ++children_count_; |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 991 | } |
| 992 | |
| 993 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 994 | Handle<HeapObject> HeapEntry::GetHeapObject() { |
| 995 | return snapshot_->collection()->FindHeapObjectById(id()); |
| 996 | } |
| 997 | |
| 998 | |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 999 | void HeapEntry::Print( |
| 1000 | const char* prefix, const char* edge_name, int max_depth, int indent) { |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1001 | STATIC_CHECK(sizeof(unsigned) == sizeof(id())); |
mstarzinger@chromium.org | 15613d0 | 2012-05-23 12:04:37 +0000 | [diff] [blame^] | 1002 | OS::Print("%6d @%6u %*c %s%s: ", |
| 1003 | self_size(), id(), indent, ' ', prefix, edge_name); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1004 | if (type() != kString) { |
| 1005 | OS::Print("%s %.40s\n", TypeAsString(), name_); |
| 1006 | } else { |
| 1007 | OS::Print("\""); |
| 1008 | const char* c = name_; |
| 1009 | while (*c && (c - name_) <= 40) { |
| 1010 | if (*c != '\n') |
| 1011 | OS::Print("%c", *c); |
| 1012 | else |
| 1013 | OS::Print("\\n"); |
| 1014 | ++c; |
| 1015 | } |
| 1016 | OS::Print("\"\n"); |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 1017 | } |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1018 | if (--max_depth == 0) return; |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1019 | Vector<HeapGraphEdge*> ch = children(); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1020 | for (int i = 0; i < ch.length(); ++i) { |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1021 | HeapGraphEdge& edge = *ch[i]; |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1022 | const char* edge_prefix = ""; |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 1023 | EmbeddedVector<char, 64> index; |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1024 | const char* edge_name = index.start(); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1025 | switch (edge.type()) { |
| 1026 | case HeapGraphEdge::kContextVariable: |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1027 | edge_prefix = "#"; |
| 1028 | edge_name = edge.name(); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1029 | break; |
| 1030 | case HeapGraphEdge::kElement: |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1031 | OS::SNPrintF(index, "%d", edge.index()); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1032 | break; |
| 1033 | case HeapGraphEdge::kInternal: |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1034 | edge_prefix = "$"; |
| 1035 | edge_name = edge.name(); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1036 | break; |
| 1037 | case HeapGraphEdge::kProperty: |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1038 | edge_name = edge.name(); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1039 | break; |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1040 | case HeapGraphEdge::kHidden: |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1041 | edge_prefix = "$"; |
| 1042 | OS::SNPrintF(index, "%d", edge.index()); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1043 | break; |
| 1044 | case HeapGraphEdge::kShortcut: |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1045 | edge_prefix = "^"; |
| 1046 | edge_name = edge.name(); |
| 1047 | break; |
| 1048 | case HeapGraphEdge::kWeak: |
| 1049 | edge_prefix = "w"; |
| 1050 | OS::SNPrintF(index, "%d", edge.index()); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1051 | break; |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1052 | default: |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1053 | OS::SNPrintF(index, "!!! unknown edge type: %d ", edge.type()); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1054 | } |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1055 | edge.to()->Print(edge_prefix, edge_name, max_depth, indent + 2); |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 1056 | } |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1059 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1060 | const char* HeapEntry::TypeAsString() { |
| 1061 | switch (type()) { |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1062 | case kHidden: return "/hidden/"; |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1063 | case kObject: return "/object/"; |
| 1064 | case kClosure: return "/closure/"; |
| 1065 | case kString: return "/string/"; |
| 1066 | case kCode: return "/code/"; |
| 1067 | case kArray: return "/array/"; |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 1068 | case kRegExp: return "/regexp/"; |
| 1069 | case kHeapNumber: return "/number/"; |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 1070 | case kNative: return "/native/"; |
jkummerow@chromium.org | ab7dad4 | 2012-02-07 12:07:34 +0000 | [diff] [blame] | 1071 | case kSynthetic: return "/synthetic/"; |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1072 | default: return "???"; |
| 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1077 | // It is very important to keep objects that form a heap snapshot |
| 1078 | // as small as possible. |
| 1079 | namespace { // Avoid littering the global namespace. |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1080 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1081 | template <size_t ptr_size> struct SnapshotSizeConstants; |
| 1082 | |
| 1083 | template <> struct SnapshotSizeConstants<4> { |
| 1084 | static const int kExpectedHeapGraphEdgeSize = 12; |
mstarzinger@chromium.org | 15613d0 | 2012-05-23 12:04:37 +0000 | [diff] [blame^] | 1085 | static const int kExpectedHeapEntrySize = 24; |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 1086 | static const size_t kMaxSerializableSnapshotRawSize = 256 * MB; |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1087 | }; |
| 1088 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1089 | template <> struct SnapshotSizeConstants<8> { |
| 1090 | static const int kExpectedHeapGraphEdgeSize = 24; |
mstarzinger@chromium.org | 15613d0 | 2012-05-23 12:04:37 +0000 | [diff] [blame^] | 1091 | static const int kExpectedHeapEntrySize = 32; |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 1092 | static const uint64_t kMaxSerializableSnapshotRawSize = |
| 1093 | static_cast<uint64_t>(6000) * MB; |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1094 | }; |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1095 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1096 | } // namespace |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1097 | |
| 1098 | HeapSnapshot::HeapSnapshot(HeapSnapshotsCollection* collection, |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 1099 | HeapSnapshot::Type type, |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1100 | const char* title, |
| 1101 | unsigned uid) |
| 1102 | : collection_(collection), |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 1103 | type_(type), |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1104 | title_(title), |
| 1105 | uid_(uid), |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1106 | root_index_(HeapEntry::kNoEntry), |
| 1107 | gc_roots_index_(HeapEntry::kNoEntry), |
| 1108 | natives_root_index_(HeapEntry::kNoEntry), |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 1109 | max_snapshot_js_object_id_(0) { |
mstarzinger@chromium.org | 3233d2f | 2012-03-14 11:16:03 +0000 | [diff] [blame] | 1110 | STATIC_CHECK( |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1111 | sizeof(HeapGraphEdge) == |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1112 | SnapshotSizeConstants<kPointerSize>::kExpectedHeapGraphEdgeSize); |
mstarzinger@chromium.org | 3233d2f | 2012-03-14 11:16:03 +0000 | [diff] [blame] | 1113 | STATIC_CHECK( |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1114 | sizeof(HeapEntry) == |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1115 | SnapshotSizeConstants<kPointerSize>::kExpectedHeapEntrySize); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1116 | for (int i = 0; i < VisitorSynchronization::kNumberOfSyncTags; ++i) { |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1117 | gc_subroot_indexes_[i] = HeapEntry::kNoEntry; |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1118 | } |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 1119 | } |
| 1120 | |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 1121 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1122 | void HeapSnapshot::Delete() { |
| 1123 | collection_->RemoveSnapshot(this); |
| 1124 | delete this; |
| 1125 | } |
| 1126 | |
| 1127 | |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 1128 | void HeapSnapshot::RememberLastJSObjectId() { |
| 1129 | max_snapshot_js_object_id_ = collection_->last_assigned_id(); |
| 1130 | } |
| 1131 | |
| 1132 | |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1133 | HeapEntry* HeapSnapshot::AddRootEntry() { |
| 1134 | ASSERT(root_index_ == HeapEntry::kNoEntry); |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 1135 | ASSERT(entries_.is_empty()); // Root entry must be the first one. |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1136 | HeapEntry* entry = AddEntry(HeapEntry::kObject, |
| 1137 | "", |
| 1138 | HeapObjectsMap::kInternalRootObjectId, |
| 1139 | 0); |
| 1140 | root_index_ = entry->index(); |
| 1141 | ASSERT(root_index_ == 0); |
| 1142 | return entry; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1143 | } |
| 1144 | |
| 1145 | |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1146 | HeapEntry* HeapSnapshot::AddGcRootsEntry() { |
| 1147 | ASSERT(gc_roots_index_ == HeapEntry::kNoEntry); |
| 1148 | HeapEntry* entry = AddEntry(HeapEntry::kObject, |
| 1149 | "(GC roots)", |
| 1150 | HeapObjectsMap::kGcRootsObjectId, |
| 1151 | 0); |
| 1152 | gc_roots_index_ = entry->index(); |
| 1153 | return entry; |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
| 1156 | |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1157 | HeapEntry* HeapSnapshot::AddGcSubrootEntry(int tag) { |
| 1158 | ASSERT(gc_subroot_indexes_[tag] == HeapEntry::kNoEntry); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1159 | ASSERT(0 <= tag && tag < VisitorSynchronization::kNumberOfSyncTags); |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1160 | HeapEntry* entry = AddEntry( |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1161 | HeapEntry::kObject, |
| 1162 | VisitorSynchronization::kTagNames[tag], |
| 1163 | HeapObjectsMap::GetNthGcSubrootId(tag), |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1164 | 0); |
| 1165 | gc_subroot_indexes_[tag] = entry->index(); |
| 1166 | return entry; |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 1170 | HeapEntry* HeapSnapshot::AddEntry(HeapEntry::Type type, |
| 1171 | const char* name, |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 1172 | SnapshotObjectId id, |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1173 | int size) { |
| 1174 | HeapEntry entry(this, type, name, id, size); |
| 1175 | entries_.Add(entry); |
| 1176 | return &entries_.last(); |
| 1177 | } |
| 1178 | |
| 1179 | |
mstarzinger@chromium.org | 15613d0 | 2012-05-23 12:04:37 +0000 | [diff] [blame^] | 1180 | void HeapSnapshot::FillChildren() { |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1181 | ASSERT(children().is_empty()); |
| 1182 | children().Allocate(edges().length()); |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1183 | int children_index = 0; |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1184 | for (int i = 0; i < entries().length(); ++i) { |
| 1185 | HeapEntry* entry = &entries()[i]; |
| 1186 | children_index = entry->set_children_index(children_index); |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1187 | } |
| 1188 | ASSERT(edges().length() == children_index); |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1189 | for (int i = 0; i < edges().length(); ++i) { |
| 1190 | HeapGraphEdge* edge = &edges()[i]; |
| 1191 | edge->ReplaceToIndexWithEntry(this); |
| 1192 | edge->from()->add_child(edge); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 1193 | } |
| 1194 | } |
| 1195 | |
| 1196 | |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 1197 | class FindEntryById { |
| 1198 | public: |
| 1199 | explicit FindEntryById(SnapshotObjectId id) : id_(id) { } |
| 1200 | int operator()(HeapEntry* const* entry) { |
| 1201 | if ((*entry)->id() == id_) return 0; |
| 1202 | return (*entry)->id() < id_ ? -1 : 1; |
| 1203 | } |
| 1204 | private: |
| 1205 | SnapshotObjectId id_; |
| 1206 | }; |
| 1207 | |
| 1208 | |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 1209 | HeapEntry* HeapSnapshot::GetEntryById(SnapshotObjectId id) { |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1210 | List<HeapEntry*>* entries_by_id = GetSortedEntriesList(); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1211 | // Perform a binary search by id. |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 1212 | int index = SortedListBSearch(*entries_by_id, FindEntryById(id)); |
| 1213 | if (index == -1) |
| 1214 | return NULL; |
| 1215 | return entries_by_id->at(index); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
| 1218 | |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 1219 | template<class T> |
| 1220 | static int SortByIds(const T* entry1_ptr, |
| 1221 | const T* entry2_ptr) { |
| 1222 | if ((*entry1_ptr)->id() == (*entry2_ptr)->id()) return 0; |
| 1223 | return (*entry1_ptr)->id() < (*entry2_ptr)->id() ? -1 : 1; |
| 1224 | } |
| 1225 | |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 1226 | |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 1227 | List<HeapEntry*>* HeapSnapshot::GetSortedEntriesList() { |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 1228 | if (sorted_entries_.is_empty()) { |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1229 | sorted_entries_.Allocate(entries_.length()); |
| 1230 | for (int i = 0; i < entries_.length(); ++i) { |
| 1231 | sorted_entries_[i] = &entries_[i]; |
| 1232 | } |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 1233 | sorted_entries_.Sort(SortByIds); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1234 | } |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 1235 | return &sorted_entries_; |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
| 1238 | |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1239 | void HeapSnapshot::Print(int max_depth) { |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1240 | root()->Print("", "", max_depth, 0); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1244 | template<typename T, class P> |
| 1245 | static size_t GetMemoryUsedByList(const List<T, P>& list) { |
| 1246 | return list.capacity() * sizeof(T); |
| 1247 | } |
| 1248 | |
| 1249 | |
| 1250 | size_t HeapSnapshot::RawSnapshotSize() const { |
| 1251 | return |
| 1252 | GetMemoryUsedByList(entries_) + |
| 1253 | GetMemoryUsedByList(edges_) + |
| 1254 | GetMemoryUsedByList(children_) + |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1255 | GetMemoryUsedByList(sorted_entries_); |
| 1256 | } |
| 1257 | |
| 1258 | |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 1259 | // We split IDs on evens for embedder objects (see |
| 1260 | // HeapObjectsMap::GenerateId) and odds for native objects. |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 1261 | const SnapshotObjectId HeapObjectsMap::kInternalRootObjectId = 1; |
| 1262 | const SnapshotObjectId HeapObjectsMap::kGcRootsObjectId = |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1263 | HeapObjectsMap::kInternalRootObjectId + HeapObjectsMap::kObjectIdStep; |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 1264 | const SnapshotObjectId HeapObjectsMap::kGcRootsFirstSubrootId = |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 1265 | HeapObjectsMap::kGcRootsObjectId + HeapObjectsMap::kObjectIdStep; |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 1266 | const SnapshotObjectId HeapObjectsMap::kFirstAvailableObjectId = |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1267 | HeapObjectsMap::kGcRootsFirstSubrootId + |
| 1268 | VisitorSynchronization::kNumberOfSyncTags * HeapObjectsMap::kObjectIdStep; |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1269 | |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 1270 | HeapObjectsMap::HeapObjectsMap() |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1271 | : next_id_(kFirstAvailableObjectId), |
| 1272 | entries_map_(AddressesMatch) { |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 1273 | // This dummy element solves a problem with entries_map_. |
| 1274 | // When we do lookup in HashMap we see no difference between two cases: |
| 1275 | // it has an entry with NULL as the value or it has created |
| 1276 | // a new entry on the fly with NULL as the default value. |
| 1277 | // With such dummy element we have a guaranty that all entries_map_ entries |
| 1278 | // will have the value field grater than 0. |
| 1279 | // This fact is using in MoveObject method. |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1280 | entries_.Add(EntryInfo(0, NULL, 0)); |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 1281 | } |
| 1282 | |
| 1283 | |
| 1284 | void HeapObjectsMap::SnapshotGenerationFinished() { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1285 | RemoveDeadEntries(); |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 1286 | } |
| 1287 | |
| 1288 | |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 1289 | void HeapObjectsMap::MoveObject(Address from, Address to) { |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 1290 | ASSERT(to != NULL); |
| 1291 | ASSERT(from != NULL); |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 1292 | if (from == to) return; |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 1293 | void* from_value = entries_map_.Remove(from, AddressHash(from)); |
| 1294 | if (from_value == NULL) return; |
| 1295 | int from_entry_info_index = |
| 1296 | static_cast<int>(reinterpret_cast<intptr_t>(from_value)); |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1297 | entries_.at(from_entry_info_index).addr = to; |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 1298 | HashMap::Entry* to_entry = entries_map_.Lookup(to, AddressHash(to), true); |
| 1299 | if (to_entry->value != NULL) { |
| 1300 | int to_entry_info_index = |
| 1301 | static_cast<int>(reinterpret_cast<intptr_t>(to_entry->value)); |
| 1302 | // Without this operation we will have two EntryInfo's with the same |
| 1303 | // value in addr field. It is bad because later at RemoveDeadEntries |
| 1304 | // one of this entry will be removed with the corresponding entries_map_ |
| 1305 | // entry. |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1306 | entries_.at(to_entry_info_index).addr = NULL; |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 1307 | } |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 1308 | to_entry->value = reinterpret_cast<void*>(from_entry_info_index); |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 1309 | } |
| 1310 | |
| 1311 | |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 1312 | SnapshotObjectId HeapObjectsMap::FindEntry(Address addr) { |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 1313 | HashMap::Entry* entry = entries_map_.Lookup(addr, AddressHash(addr), false); |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1314 | if (entry == NULL) return 0; |
| 1315 | int entry_index = static_cast<int>(reinterpret_cast<intptr_t>(entry->value)); |
| 1316 | EntryInfo& entry_info = entries_.at(entry_index); |
| 1317 | ASSERT(static_cast<uint32_t>(entries_.length()) > entries_map_.occupancy()); |
| 1318 | return entry_info.id; |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 1319 | } |
| 1320 | |
| 1321 | |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1322 | SnapshotObjectId HeapObjectsMap::FindOrAddEntry(Address addr, |
| 1323 | unsigned int size) { |
| 1324 | ASSERT(static_cast<uint32_t>(entries_.length()) > entries_map_.occupancy()); |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 1325 | HashMap::Entry* entry = entries_map_.Lookup(addr, AddressHash(addr), true); |
| 1326 | if (entry->value != NULL) { |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 1327 | int entry_index = |
| 1328 | static_cast<int>(reinterpret_cast<intptr_t>(entry->value)); |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1329 | EntryInfo& entry_info = entries_.at(entry_index); |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 1330 | entry_info.accessed = true; |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1331 | entry_info.size = size; |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 1332 | return entry_info.id; |
| 1333 | } |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1334 | entry->value = reinterpret_cast<void*>(entries_.length()); |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 1335 | SnapshotObjectId id = next_id_; |
| 1336 | next_id_ += kObjectIdStep; |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1337 | entries_.Add(EntryInfo(id, addr, size)); |
| 1338 | ASSERT(static_cast<uint32_t>(entries_.length()) > entries_map_.occupancy()); |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 1339 | return id; |
| 1340 | } |
| 1341 | |
| 1342 | |
| 1343 | void HeapObjectsMap::StopHeapObjectsTracking() { |
| 1344 | time_intervals_.Clear(); |
| 1345 | } |
| 1346 | |
| 1347 | void HeapObjectsMap::UpdateHeapObjectsMap() { |
| 1348 | HEAP->CollectAllGarbage(Heap::kMakeHeapIterableMask, |
| 1349 | "HeapSnapshotsCollection::UpdateHeapObjectsMap"); |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1350 | HeapIterator iterator; |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 1351 | for (HeapObject* obj = iterator.next(); |
| 1352 | obj != NULL; |
| 1353 | obj = iterator.next()) { |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1354 | FindOrAddEntry(obj->address(), obj->Size()); |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 1355 | } |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 1356 | RemoveDeadEntries(); |
| 1357 | } |
| 1358 | |
| 1359 | |
| 1360 | void HeapObjectsMap::PushHeapObjectsStats(OutputStream* stream) { |
| 1361 | UpdateHeapObjectsMap(); |
| 1362 | time_intervals_.Add(TimeInterval(next_id_)); |
| 1363 | int prefered_chunk_size = stream->GetChunkSize(); |
svenpanne@chromium.org | fb04633 | 2012-04-19 12:02:44 +0000 | [diff] [blame] | 1364 | List<v8::HeapStatsUpdate> stats_buffer; |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1365 | ASSERT(!entries_.is_empty()); |
| 1366 | EntryInfo* entry_info = &entries_.first(); |
| 1367 | EntryInfo* end_entry_info = &entries_.last() + 1; |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 1368 | for (int time_interval_index = 0; |
| 1369 | time_interval_index < time_intervals_.length(); |
| 1370 | ++time_interval_index) { |
| 1371 | TimeInterval& time_interval = time_intervals_[time_interval_index]; |
| 1372 | SnapshotObjectId time_interval_id = time_interval.id; |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1373 | uint32_t entries_size = 0; |
| 1374 | EntryInfo* start_entry_info = entry_info; |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 1375 | 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] | 1376 | entries_size += entry_info->size; |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 1377 | ++entry_info; |
| 1378 | } |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1379 | uint32_t entries_count = |
| 1380 | static_cast<uint32_t>(entry_info - start_entry_info); |
| 1381 | if (time_interval.count != entries_count || |
| 1382 | time_interval.size != entries_size) { |
svenpanne@chromium.org | fb04633 | 2012-04-19 12:02:44 +0000 | [diff] [blame] | 1383 | stats_buffer.Add(v8::HeapStatsUpdate( |
| 1384 | time_interval_index, |
| 1385 | time_interval.count = entries_count, |
| 1386 | time_interval.size = entries_size)); |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 1387 | if (stats_buffer.length() >= prefered_chunk_size) { |
svenpanne@chromium.org | fb04633 | 2012-04-19 12:02:44 +0000 | [diff] [blame] | 1388 | OutputStream::WriteResult result = stream->WriteHeapStatsChunk( |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 1389 | &stats_buffer.first(), stats_buffer.length()); |
| 1390 | if (result == OutputStream::kAbort) return; |
| 1391 | stats_buffer.Clear(); |
| 1392 | } |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 1393 | } |
| 1394 | } |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 1395 | ASSERT(entry_info == end_entry_info); |
| 1396 | if (!stats_buffer.is_empty()) { |
svenpanne@chromium.org | fb04633 | 2012-04-19 12:02:44 +0000 | [diff] [blame] | 1397 | OutputStream::WriteResult result = stream->WriteHeapStatsChunk( |
| 1398 | &stats_buffer.first(), stats_buffer.length()); |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 1399 | if (result == OutputStream::kAbort) return; |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1400 | } |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 1401 | stream->EndOfStream(); |
| 1402 | } |
| 1403 | |
| 1404 | |
| 1405 | void HeapObjectsMap::RemoveDeadEntries() { |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1406 | ASSERT(entries_.length() > 0 && |
| 1407 | entries_.at(0).id == 0 && |
| 1408 | entries_.at(0).addr == NULL); |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 1409 | int first_free_entry = 1; |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1410 | for (int i = 1; i < entries_.length(); ++i) { |
| 1411 | EntryInfo& entry_info = entries_.at(i); |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 1412 | if (entry_info.accessed) { |
| 1413 | if (first_free_entry != i) { |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1414 | entries_.at(first_free_entry) = entry_info; |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 1415 | } |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1416 | entries_.at(first_free_entry).accessed = false; |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 1417 | HashMap::Entry* entry = entries_map_.Lookup( |
| 1418 | entry_info.addr, AddressHash(entry_info.addr), false); |
| 1419 | ASSERT(entry); |
| 1420 | entry->value = reinterpret_cast<void*>(first_free_entry); |
| 1421 | ++first_free_entry; |
| 1422 | } else { |
| 1423 | if (entry_info.addr) { |
| 1424 | entries_map_.Remove(entry_info.addr, AddressHash(entry_info.addr)); |
| 1425 | } |
| 1426 | } |
| 1427 | } |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1428 | entries_.Rewind(first_free_entry); |
| 1429 | ASSERT(static_cast<uint32_t>(entries_.length()) - 1 == |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 1430 | entries_map_.occupancy()); |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 1431 | } |
| 1432 | |
| 1433 | |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 1434 | SnapshotObjectId HeapObjectsMap::GenerateId(v8::RetainedObjectInfo* info) { |
| 1435 | SnapshotObjectId id = static_cast<SnapshotObjectId>(info->GetHash()); |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 1436 | const char* label = info->GetLabel(); |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 1437 | id ^= HashSequentialString(label, |
| 1438 | static_cast<int>(strlen(label)), |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1439 | HEAP->HashSeed()); |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 1440 | intptr_t element_count = info->GetElementCount(); |
| 1441 | if (element_count != -1) |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1442 | id ^= ComputeIntegerHash(static_cast<uint32_t>(element_count), |
| 1443 | v8::internal::kZeroHashSeed); |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 1444 | return id << 1; |
| 1445 | } |
| 1446 | |
| 1447 | |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1448 | HeapSnapshotsCollection::HeapSnapshotsCollection() |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 1449 | : is_tracking_objects_(false), |
| 1450 | snapshots_uids_(HeapSnapshotsMatch), |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1451 | token_enumerator_(new TokenEnumerator()) { |
| 1452 | } |
| 1453 | |
| 1454 | |
| 1455 | static void DeleteHeapSnapshot(HeapSnapshot** snapshot_ptr) { |
| 1456 | delete *snapshot_ptr; |
| 1457 | } |
| 1458 | |
| 1459 | |
| 1460 | HeapSnapshotsCollection::~HeapSnapshotsCollection() { |
| 1461 | delete token_enumerator_; |
| 1462 | snapshots_.Iterate(DeleteHeapSnapshot); |
| 1463 | } |
| 1464 | |
| 1465 | |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 1466 | HeapSnapshot* HeapSnapshotsCollection::NewSnapshot(HeapSnapshot::Type type, |
| 1467 | const char* name, |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1468 | unsigned uid) { |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 1469 | is_tracking_objects_ = true; // Start watching for heap objects moves. |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1470 | return new HeapSnapshot(this, type, name, uid); |
| 1471 | } |
| 1472 | |
| 1473 | |
| 1474 | void HeapSnapshotsCollection::SnapshotGenerationFinished( |
| 1475 | HeapSnapshot* snapshot) { |
| 1476 | ids_.SnapshotGenerationFinished(); |
| 1477 | if (snapshot != NULL) { |
| 1478 | snapshots_.Add(snapshot); |
| 1479 | HashMap::Entry* entry = |
| 1480 | snapshots_uids_.Lookup(reinterpret_cast<void*>(snapshot->uid()), |
| 1481 | static_cast<uint32_t>(snapshot->uid()), |
| 1482 | true); |
| 1483 | ASSERT(entry->value == NULL); |
| 1484 | entry->value = snapshot; |
| 1485 | } |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1486 | } |
| 1487 | |
| 1488 | |
| 1489 | HeapSnapshot* HeapSnapshotsCollection::GetSnapshot(unsigned uid) { |
| 1490 | HashMap::Entry* entry = snapshots_uids_.Lookup(reinterpret_cast<void*>(uid), |
| 1491 | static_cast<uint32_t>(uid), |
| 1492 | false); |
| 1493 | return entry != NULL ? reinterpret_cast<HeapSnapshot*>(entry->value) : NULL; |
| 1494 | } |
| 1495 | |
| 1496 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1497 | void HeapSnapshotsCollection::RemoveSnapshot(HeapSnapshot* snapshot) { |
| 1498 | snapshots_.RemoveElement(snapshot); |
| 1499 | unsigned uid = snapshot->uid(); |
| 1500 | snapshots_uids_.Remove(reinterpret_cast<void*>(uid), |
| 1501 | static_cast<uint32_t>(uid)); |
| 1502 | } |
| 1503 | |
| 1504 | |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 1505 | Handle<HeapObject> HeapSnapshotsCollection::FindHeapObjectById( |
| 1506 | SnapshotObjectId id) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1507 | // First perform a full GC in order to avoid dead objects. |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 1508 | HEAP->CollectAllGarbage(Heap::kMakeHeapIterableMask, |
| 1509 | "HeapSnapshotsCollection::FindHeapObjectById"); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1510 | AssertNoAllocation no_allocation; |
| 1511 | HeapObject* object = NULL; |
| 1512 | HeapIterator iterator(HeapIterator::kFilterUnreachable); |
| 1513 | // Make sure that object with the given id is still reachable. |
| 1514 | for (HeapObject* obj = iterator.next(); |
| 1515 | obj != NULL; |
| 1516 | obj = iterator.next()) { |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1517 | if (ids_.FindEntry(obj->address()) == id) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1518 | ASSERT(object == NULL); |
| 1519 | object = obj; |
| 1520 | // Can't break -- kFilterUnreachable requires full heap traversal. |
| 1521 | } |
| 1522 | } |
| 1523 | return object != NULL ? Handle<HeapObject>(object) : Handle<HeapObject>(); |
| 1524 | } |
| 1525 | |
| 1526 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1527 | HeapEntriesMap::HeapEntriesMap() |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1528 | : entries_(HeapThingsMatch) { |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1529 | } |
| 1530 | |
| 1531 | |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1532 | int HeapEntriesMap::Map(HeapThing thing) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1533 | HashMap::Entry* cache_entry = entries_.Lookup(thing, Hash(thing), false); |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1534 | if (cache_entry == NULL) return HeapEntry::kNoEntry; |
| 1535 | return static_cast<int>(reinterpret_cast<intptr_t>(cache_entry->value)); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1536 | } |
| 1537 | |
| 1538 | |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1539 | void HeapEntriesMap::Pair(HeapThing thing, int entry) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1540 | HashMap::Entry* cache_entry = entries_.Lookup(thing, Hash(thing), true); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1541 | ASSERT(cache_entry->value == NULL); |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1542 | cache_entry->value = reinterpret_cast<void*>(static_cast<intptr_t>(entry)); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1543 | } |
| 1544 | |
| 1545 | |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1546 | HeapObjectsSet::HeapObjectsSet() |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1547 | : entries_(HeapEntriesMap::HeapThingsMatch) { |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1548 | } |
| 1549 | |
| 1550 | |
| 1551 | void HeapObjectsSet::Clear() { |
| 1552 | entries_.Clear(); |
| 1553 | } |
| 1554 | |
| 1555 | |
| 1556 | bool HeapObjectsSet::Contains(Object* obj) { |
| 1557 | if (!obj->IsHeapObject()) return false; |
| 1558 | HeapObject* object = HeapObject::cast(obj); |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1559 | return entries_.Lookup(object, HeapEntriesMap::Hash(object), false) != NULL; |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1560 | } |
| 1561 | |
| 1562 | |
| 1563 | void HeapObjectsSet::Insert(Object* obj) { |
| 1564 | if (!obj->IsHeapObject()) return; |
| 1565 | HeapObject* object = HeapObject::cast(obj); |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1566 | entries_.Lookup(object, HeapEntriesMap::Hash(object), true); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1567 | } |
| 1568 | |
| 1569 | |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 1570 | const char* HeapObjectsSet::GetTag(Object* obj) { |
| 1571 | HeapObject* object = HeapObject::cast(obj); |
| 1572 | HashMap::Entry* cache_entry = |
| 1573 | entries_.Lookup(object, HeapEntriesMap::Hash(object), false); |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1574 | return cache_entry != NULL |
| 1575 | ? reinterpret_cast<const char*>(cache_entry->value) |
| 1576 | : NULL; |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 1577 | } |
| 1578 | |
| 1579 | |
| 1580 | void HeapObjectsSet::SetTag(Object* obj, const char* tag) { |
| 1581 | if (!obj->IsHeapObject()) return; |
| 1582 | HeapObject* object = HeapObject::cast(obj); |
| 1583 | HashMap::Entry* cache_entry = |
| 1584 | entries_.Lookup(object, HeapEntriesMap::Hash(object), true); |
| 1585 | cache_entry->value = const_cast<char*>(tag); |
| 1586 | } |
| 1587 | |
| 1588 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1589 | HeapObject* const V8HeapExplorer::kInternalRootObject = |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 1590 | reinterpret_cast<HeapObject*>( |
| 1591 | static_cast<intptr_t>(HeapObjectsMap::kInternalRootObjectId)); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1592 | HeapObject* const V8HeapExplorer::kGcRootsObject = |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 1593 | reinterpret_cast<HeapObject*>( |
| 1594 | static_cast<intptr_t>(HeapObjectsMap::kGcRootsObjectId)); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1595 | HeapObject* const V8HeapExplorer::kFirstGcSubrootObject = |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1596 | reinterpret_cast<HeapObject*>( |
| 1597 | static_cast<intptr_t>(HeapObjectsMap::kGcRootsFirstSubrootId)); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1598 | HeapObject* const V8HeapExplorer::kLastGcSubrootObject = |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1599 | reinterpret_cast<HeapObject*>( |
| 1600 | static_cast<intptr_t>(HeapObjectsMap::kFirstAvailableObjectId)); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1601 | |
| 1602 | |
| 1603 | V8HeapExplorer::V8HeapExplorer( |
| 1604 | HeapSnapshot* snapshot, |
| 1605 | SnapshottingProgressReportingInterface* progress) |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 1606 | : heap_(Isolate::Current()->heap()), |
| 1607 | snapshot_(snapshot), |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1608 | collection_(snapshot_->collection()), |
| 1609 | progress_(progress), |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1610 | filler_(NULL) { |
| 1611 | } |
| 1612 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1613 | |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1614 | V8HeapExplorer::~V8HeapExplorer() { |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1615 | } |
| 1616 | |
| 1617 | |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1618 | HeapEntry* V8HeapExplorer::AllocateEntry(HeapThing ptr) { |
| 1619 | return AddEntry(reinterpret_cast<HeapObject*>(ptr)); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1620 | } |
| 1621 | |
| 1622 | |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1623 | HeapEntry* V8HeapExplorer::AddEntry(HeapObject* object) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1624 | if (object == kInternalRootObject) { |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1625 | snapshot_->AddRootEntry(); |
| 1626 | return snapshot_->root(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1627 | } else if (object == kGcRootsObject) { |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1628 | HeapEntry* entry = snapshot_->AddGcRootsEntry(); |
| 1629 | return entry; |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1630 | } else if (object >= kFirstGcSubrootObject && object < kLastGcSubrootObject) { |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1631 | HeapEntry* entry = snapshot_->AddGcSubrootEntry(GetGcSubrootOrder(object)); |
| 1632 | return entry; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1633 | } else if (object->IsJSFunction()) { |
| 1634 | JSFunction* func = JSFunction::cast(object); |
| 1635 | SharedFunctionInfo* shared = func->shared(); |
ulan@chromium.org | 65a89c2 | 2012-02-14 11:46:07 +0000 | [diff] [blame] | 1636 | const char* name = shared->bound() ? "native_bind" : |
| 1637 | collection_->names()->GetName(String::cast(shared->name())); |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1638 | return AddEntry(object, HeapEntry::kClosure, name); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1639 | } else if (object->IsJSRegExp()) { |
| 1640 | JSRegExp* re = JSRegExp::cast(object); |
| 1641 | return AddEntry(object, |
| 1642 | HeapEntry::kRegExp, |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1643 | collection_->names()->GetName(re->Pattern())); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1644 | } else if (object->IsJSObject()) { |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1645 | const char* name = collection_->names()->GetName( |
| 1646 | GetConstructorName(JSObject::cast(object))); |
| 1647 | if (object->IsJSGlobalObject()) { |
| 1648 | const char* tag = objects_tags_.GetTag(object); |
| 1649 | if (tag != NULL) { |
| 1650 | name = collection_->names()->GetFormatted("%s / %s", name, tag); |
| 1651 | } |
| 1652 | } |
| 1653 | return AddEntry(object, HeapEntry::kObject, name); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1654 | } else if (object->IsString()) { |
| 1655 | return AddEntry(object, |
| 1656 | HeapEntry::kString, |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1657 | collection_->names()->GetName(String::cast(object))); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1658 | } else if (object->IsCode()) { |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1659 | return AddEntry(object, HeapEntry::kCode, ""); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1660 | } else if (object->IsSharedFunctionInfo()) { |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1661 | String* name = String::cast(SharedFunctionInfo::cast(object)->name()); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1662 | return AddEntry(object, |
| 1663 | HeapEntry::kCode, |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1664 | collection_->names()->GetName(name)); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1665 | } else if (object->IsScript()) { |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1666 | Object* name = Script::cast(object)->name(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1667 | return AddEntry(object, |
| 1668 | HeapEntry::kCode, |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1669 | name->IsString() |
| 1670 | ? collection_->names()->GetName(String::cast(name)) |
| 1671 | : ""); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1672 | } else if (object->IsGlobalContext()) { |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1673 | return AddEntry(object, HeapEntry::kHidden, "system / GlobalContext"); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1674 | } else if (object->IsContext()) { |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1675 | return AddEntry(object, HeapEntry::kHidden, "system / Context"); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 1676 | } else if (object->IsFixedArray() || |
| 1677 | object->IsFixedDoubleArray() || |
| 1678 | object->IsByteArray() || |
| 1679 | object->IsExternalArray()) { |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1680 | return AddEntry(object, HeapEntry::kArray, ""); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1681 | } else if (object->IsHeapNumber()) { |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1682 | return AddEntry(object, HeapEntry::kHeapNumber, "number"); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1683 | } |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1684 | return AddEntry(object, HeapEntry::kHidden, GetSystemEntryName(object)); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1685 | } |
| 1686 | |
| 1687 | |
| 1688 | HeapEntry* V8HeapExplorer::AddEntry(HeapObject* object, |
| 1689 | HeapEntry::Type type, |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1690 | const char* name) { |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 1691 | int object_size = object->Size(); |
| 1692 | SnapshotObjectId object_id = |
| 1693 | collection_->GetObjectId(object->address(), object_size); |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1694 | return snapshot_->AddEntry(type, name, object_id, object_size); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1695 | } |
| 1696 | |
| 1697 | |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1698 | class GcSubrootsEnumerator : public ObjectVisitor { |
| 1699 | public: |
| 1700 | GcSubrootsEnumerator( |
| 1701 | SnapshotFillerInterface* filler, V8HeapExplorer* explorer) |
| 1702 | : filler_(filler), |
| 1703 | explorer_(explorer), |
| 1704 | previous_object_count_(0), |
| 1705 | object_count_(0) { |
| 1706 | } |
| 1707 | void VisitPointers(Object** start, Object** end) { |
| 1708 | object_count_ += end - start; |
| 1709 | } |
| 1710 | void Synchronize(VisitorSynchronization::SyncTag tag) { |
| 1711 | // Skip empty subroots. |
| 1712 | if (previous_object_count_ != object_count_) { |
| 1713 | previous_object_count_ = object_count_; |
| 1714 | filler_->AddEntry(V8HeapExplorer::GetNthGcSubrootObject(tag), explorer_); |
| 1715 | } |
| 1716 | } |
| 1717 | private: |
| 1718 | SnapshotFillerInterface* filler_; |
| 1719 | V8HeapExplorer* explorer_; |
| 1720 | intptr_t previous_object_count_; |
| 1721 | intptr_t object_count_; |
| 1722 | }; |
| 1723 | |
| 1724 | |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1725 | void V8HeapExplorer::AddRootEntries(SnapshotFillerInterface* filler) { |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 1726 | filler->AddEntry(kInternalRootObject, this); |
| 1727 | filler->AddEntry(kGcRootsObject, this); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 1728 | GcSubrootsEnumerator enumerator(filler, this); |
| 1729 | heap_->IterateRoots(&enumerator, VISIT_ALL); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1730 | } |
| 1731 | |
| 1732 | |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1733 | const char* V8HeapExplorer::GetSystemEntryName(HeapObject* object) { |
| 1734 | switch (object->map()->instance_type()) { |
| 1735 | case MAP_TYPE: return "system / Map"; |
| 1736 | case JS_GLOBAL_PROPERTY_CELL_TYPE: return "system / JSGlobalPropertyCell"; |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 1737 | case FOREIGN_TYPE: return "system / Foreign"; |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1738 | case ODDBALL_TYPE: return "system / Oddball"; |
| 1739 | #define MAKE_STRUCT_CASE(NAME, Name, name) \ |
| 1740 | case NAME##_TYPE: return "system / "#Name; |
| 1741 | STRUCT_LIST(MAKE_STRUCT_CASE) |
| 1742 | #undef MAKE_STRUCT_CASE |
| 1743 | default: return "system"; |
| 1744 | } |
| 1745 | } |
| 1746 | |
| 1747 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1748 | int V8HeapExplorer::EstimateObjectsCount(HeapIterator* iterator) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1749 | int objects_count = 0; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1750 | for (HeapObject* obj = iterator->next(); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1751 | obj != NULL; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1752 | obj = iterator->next()) { |
| 1753 | objects_count++; |
| 1754 | } |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1755 | return objects_count; |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1756 | } |
| 1757 | |
| 1758 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1759 | class IndexedReferencesExtractor : public ObjectVisitor { |
| 1760 | public: |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1761 | IndexedReferencesExtractor(V8HeapExplorer* generator, |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1762 | HeapObject* parent_obj, |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1763 | int parent) |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1764 | : generator_(generator), |
| 1765 | parent_obj_(parent_obj), |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1766 | parent_(parent), |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1767 | next_index_(1) { |
| 1768 | } |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1769 | void VisitPointers(Object** start, Object** end) { |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1770 | for (Object** p = start; p < end; p++) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1771 | if (CheckVisitedAndUnmark(p)) continue; |
| 1772 | generator_->SetHiddenReference(parent_obj_, parent_, next_index_++, *p); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1773 | } |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1774 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1775 | static void MarkVisitedField(HeapObject* obj, int offset) { |
| 1776 | if (offset < 0) return; |
| 1777 | Address field = obj->address() + offset; |
| 1778 | ASSERT(!Memory::Object_at(field)->IsFailure()); |
| 1779 | ASSERT(Memory::Object_at(field)->IsHeapObject()); |
| 1780 | *field |= kFailureTag; |
| 1781 | } |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 1782 | |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1783 | private: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1784 | bool CheckVisitedAndUnmark(Object** field) { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1785 | if ((*field)->IsFailure()) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 1786 | intptr_t untagged = reinterpret_cast<intptr_t>(*field) & ~kFailureTagMask; |
| 1787 | *field = reinterpret_cast<Object*>(untagged | kHeapObjectTag); |
| 1788 | ASSERT((*field)->IsHeapObject()); |
| 1789 | return true; |
| 1790 | } |
| 1791 | return false; |
| 1792 | } |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1793 | V8HeapExplorer* generator_; |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1794 | HeapObject* parent_obj_; |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1795 | int parent_; |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1796 | int next_index_; |
| 1797 | }; |
| 1798 | |
| 1799 | |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 1800 | void V8HeapExplorer::ExtractReferences(HeapObject* obj) { |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1801 | HeapEntry* heap_entry = GetEntry(obj); |
| 1802 | if (heap_entry == NULL) return; // No interest in this object. |
| 1803 | int entry = heap_entry->index(); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1804 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 1805 | bool extract_indexed_refs = true; |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1806 | if (obj->IsJSGlobalProxy()) { |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 1807 | ExtractJSGlobalProxyReferences(JSGlobalProxy::cast(obj)); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 1808 | } else if (obj->IsJSObject()) { |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 1809 | ExtractJSObjectReferences(entry, JSObject::cast(obj)); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1810 | } else if (obj->IsString()) { |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 1811 | ExtractStringReferences(entry, String::cast(obj)); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 1812 | extract_indexed_refs = false; |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 1813 | } else if (obj->IsContext()) { |
| 1814 | ExtractContextReferences(entry, Context::cast(obj)); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1815 | } else if (obj->IsMap()) { |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 1816 | ExtractMapReferences(entry, Map::cast(obj)); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1817 | } else if (obj->IsSharedFunctionInfo()) { |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 1818 | ExtractSharedFunctionInfoReferences(entry, SharedFunctionInfo::cast(obj)); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 1819 | } else if (obj->IsScript()) { |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 1820 | ExtractScriptReferences(entry, Script::cast(obj)); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 1821 | } else if (obj->IsCodeCache()) { |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 1822 | ExtractCodeCacheReferences(entry, CodeCache::cast(obj)); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 1823 | } else if (obj->IsCode()) { |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 1824 | ExtractCodeReferences(entry, Code::cast(obj)); |
| 1825 | } else if (obj->IsJSGlobalPropertyCell()) { |
| 1826 | ExtractJSGlobalPropertyCellReferences( |
| 1827 | entry, JSGlobalPropertyCell::cast(obj)); |
| 1828 | extract_indexed_refs = false; |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 1829 | } |
| 1830 | if (extract_indexed_refs) { |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 1831 | SetInternalReference(obj, entry, "map", obj->map(), HeapObject::kMapOffset); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 1832 | IndexedReferencesExtractor refs_extractor(this, obj, entry); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1833 | obj->Iterate(&refs_extractor); |
| 1834 | } |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 1835 | } |
| 1836 | |
| 1837 | |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 1838 | void V8HeapExplorer::ExtractJSGlobalProxyReferences(JSGlobalProxy* proxy) { |
| 1839 | // We need to reference JS global objects from snapshot's root. |
| 1840 | // We use JSGlobalProxy because this is what embedder (e.g. browser) |
| 1841 | // uses for the global object. |
| 1842 | Object* object = proxy->map()->prototype(); |
| 1843 | bool is_debug_object = false; |
| 1844 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 1845 | is_debug_object = object->IsGlobalObject() && |
| 1846 | Isolate::Current()->debug()->IsDebugGlobal(GlobalObject::cast(object)); |
| 1847 | #endif |
| 1848 | if (!is_debug_object) { |
| 1849 | SetUserGlobalReference(object); |
| 1850 | } |
| 1851 | } |
| 1852 | |
| 1853 | |
| 1854 | void V8HeapExplorer::ExtractJSObjectReferences( |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1855 | int entry, JSObject* js_obj) { |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 1856 | HeapObject* obj = js_obj; |
| 1857 | ExtractClosureReferences(js_obj, entry); |
| 1858 | ExtractPropertyReferences(js_obj, entry); |
| 1859 | ExtractElementReferences(js_obj, entry); |
| 1860 | ExtractInternalReferences(js_obj, entry); |
| 1861 | SetPropertyReference( |
| 1862 | obj, entry, heap_->Proto_symbol(), js_obj->GetPrototype()); |
| 1863 | if (obj->IsJSFunction()) { |
| 1864 | JSFunction* js_fun = JSFunction::cast(js_obj); |
| 1865 | Object* proto_or_map = js_fun->prototype_or_initial_map(); |
| 1866 | if (!proto_or_map->IsTheHole()) { |
| 1867 | if (!proto_or_map->IsMap()) { |
| 1868 | SetPropertyReference( |
| 1869 | obj, entry, |
| 1870 | heap_->prototype_symbol(), proto_or_map, |
| 1871 | NULL, |
| 1872 | JSFunction::kPrototypeOrInitialMapOffset); |
| 1873 | } else { |
| 1874 | SetPropertyReference( |
| 1875 | obj, entry, |
| 1876 | heap_->prototype_symbol(), js_fun->prototype()); |
| 1877 | } |
| 1878 | } |
| 1879 | SharedFunctionInfo* shared_info = js_fun->shared(); |
| 1880 | // JSFunction has either bindings or literals and never both. |
| 1881 | bool bound = shared_info->bound(); |
| 1882 | TagObject(js_fun->literals_or_bindings(), |
| 1883 | bound ? "(function bindings)" : "(function literals)"); |
| 1884 | SetInternalReference(js_fun, entry, |
| 1885 | bound ? "bindings" : "literals", |
| 1886 | js_fun->literals_or_bindings(), |
| 1887 | JSFunction::kLiteralsOffset); |
| 1888 | TagObject(shared_info, "(shared function info)"); |
| 1889 | SetInternalReference(js_fun, entry, |
| 1890 | "shared", shared_info, |
| 1891 | JSFunction::kSharedFunctionInfoOffset); |
| 1892 | TagObject(js_fun->unchecked_context(), "(context)"); |
| 1893 | SetInternalReference(js_fun, entry, |
| 1894 | "context", js_fun->unchecked_context(), |
| 1895 | JSFunction::kContextOffset); |
| 1896 | for (int i = JSFunction::kNonWeakFieldsEndOffset; |
| 1897 | i < JSFunction::kSize; |
| 1898 | i += kPointerSize) { |
| 1899 | SetWeakReference(js_fun, entry, i, *HeapObject::RawField(js_fun, i), i); |
| 1900 | } |
| 1901 | } else if (obj->IsGlobalObject()) { |
| 1902 | GlobalObject* global_obj = GlobalObject::cast(obj); |
| 1903 | SetInternalReference(global_obj, entry, |
| 1904 | "builtins", global_obj->builtins(), |
| 1905 | GlobalObject::kBuiltinsOffset); |
| 1906 | SetInternalReference(global_obj, entry, |
| 1907 | "global_context", global_obj->global_context(), |
| 1908 | GlobalObject::kGlobalContextOffset); |
| 1909 | SetInternalReference(global_obj, entry, |
| 1910 | "global_receiver", global_obj->global_receiver(), |
| 1911 | GlobalObject::kGlobalReceiverOffset); |
| 1912 | } |
| 1913 | TagObject(js_obj->properties(), "(object properties)"); |
| 1914 | SetInternalReference(obj, entry, |
| 1915 | "properties", js_obj->properties(), |
| 1916 | JSObject::kPropertiesOffset); |
| 1917 | TagObject(js_obj->elements(), "(object elements)"); |
| 1918 | SetInternalReference(obj, entry, |
| 1919 | "elements", js_obj->elements(), |
| 1920 | JSObject::kElementsOffset); |
| 1921 | } |
| 1922 | |
| 1923 | |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1924 | void V8HeapExplorer::ExtractStringReferences(int entry, String* string) { |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 1925 | if (string->IsConsString()) { |
| 1926 | ConsString* cs = ConsString::cast(string); |
| 1927 | SetInternalReference(cs, entry, "first", cs->first()); |
| 1928 | SetInternalReference(cs, entry, "second", cs->second()); |
| 1929 | } else if (string->IsSlicedString()) { |
| 1930 | SlicedString* ss = SlicedString::cast(string); |
| 1931 | SetInternalReference(ss, entry, "parent", ss->parent()); |
| 1932 | } |
| 1933 | } |
| 1934 | |
| 1935 | |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1936 | void V8HeapExplorer::ExtractContextReferences(int entry, Context* context) { |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 1937 | #define EXTRACT_CONTEXT_FIELD(index, type, name) \ |
| 1938 | SetInternalReference(context, entry, #name, context->get(Context::index), \ |
| 1939 | FixedArray::OffsetOfElementAt(Context::index)); |
| 1940 | EXTRACT_CONTEXT_FIELD(CLOSURE_INDEX, JSFunction, closure); |
| 1941 | EXTRACT_CONTEXT_FIELD(PREVIOUS_INDEX, Context, previous); |
| 1942 | EXTRACT_CONTEXT_FIELD(EXTENSION_INDEX, Object, extension); |
| 1943 | EXTRACT_CONTEXT_FIELD(GLOBAL_INDEX, GlobalObject, global); |
| 1944 | if (context->IsGlobalContext()) { |
| 1945 | TagObject(context->jsfunction_result_caches(), |
| 1946 | "(context func. result caches)"); |
| 1947 | TagObject(context->normalized_map_cache(), "(context norm. map cache)"); |
| 1948 | TagObject(context->runtime_context(), "(runtime context)"); |
| 1949 | TagObject(context->data(), "(context data)"); |
| 1950 | GLOBAL_CONTEXT_FIELDS(EXTRACT_CONTEXT_FIELD); |
| 1951 | #undef EXTRACT_CONTEXT_FIELD |
| 1952 | for (int i = Context::FIRST_WEAK_SLOT; |
| 1953 | i < Context::GLOBAL_CONTEXT_SLOTS; |
| 1954 | ++i) { |
| 1955 | SetWeakReference(context, entry, i, context->get(i), |
| 1956 | FixedArray::OffsetOfElementAt(i)); |
| 1957 | } |
| 1958 | } |
| 1959 | } |
| 1960 | |
| 1961 | |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1962 | void V8HeapExplorer::ExtractMapReferences(int entry, Map* map) { |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 1963 | SetInternalReference(map, entry, |
| 1964 | "prototype", map->prototype(), Map::kPrototypeOffset); |
| 1965 | SetInternalReference(map, entry, |
| 1966 | "constructor", map->constructor(), |
| 1967 | Map::kConstructorOffset); |
| 1968 | if (!map->instance_descriptors()->IsEmpty()) { |
| 1969 | TagObject(map->instance_descriptors(), "(map descriptors)"); |
| 1970 | SetInternalReference(map, entry, |
| 1971 | "descriptors", map->instance_descriptors(), |
| 1972 | Map::kInstanceDescriptorsOrBitField3Offset); |
| 1973 | } |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1974 | if (map->unchecked_prototype_transitions()->IsFixedArray()) { |
| 1975 | TagObject(map->prototype_transitions(), "(prototype transitions)"); |
| 1976 | SetInternalReference(map, entry, |
| 1977 | "prototype_transitions", map->prototype_transitions(), |
| 1978 | Map::kPrototypeTransitionsOrBackPointerOffset); |
| 1979 | } else { |
| 1980 | SetInternalReference(map, entry, |
| 1981 | "back_pointer", map->GetBackPointer(), |
| 1982 | Map::kPrototypeTransitionsOrBackPointerOffset); |
| 1983 | } |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 1984 | SetInternalReference(map, entry, |
| 1985 | "code_cache", map->code_cache(), |
| 1986 | Map::kCodeCacheOffset); |
| 1987 | } |
| 1988 | |
| 1989 | |
| 1990 | void V8HeapExplorer::ExtractSharedFunctionInfoReferences( |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 1991 | int entry, SharedFunctionInfo* shared) { |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 1992 | HeapObject* obj = shared; |
| 1993 | SetInternalReference(obj, entry, |
| 1994 | "name", shared->name(), |
| 1995 | SharedFunctionInfo::kNameOffset); |
| 1996 | TagObject(shared->code(), "(code)"); |
| 1997 | SetInternalReference(obj, entry, |
| 1998 | "code", shared->code(), |
| 1999 | SharedFunctionInfo::kCodeOffset); |
| 2000 | TagObject(shared->scope_info(), "(function scope info)"); |
| 2001 | SetInternalReference(obj, entry, |
| 2002 | "scope_info", shared->scope_info(), |
| 2003 | SharedFunctionInfo::kScopeInfoOffset); |
| 2004 | SetInternalReference(obj, entry, |
| 2005 | "instance_class_name", shared->instance_class_name(), |
| 2006 | SharedFunctionInfo::kInstanceClassNameOffset); |
| 2007 | SetInternalReference(obj, entry, |
| 2008 | "script", shared->script(), |
| 2009 | SharedFunctionInfo::kScriptOffset); |
| 2010 | TagObject(shared->construct_stub(), "(code)"); |
| 2011 | SetInternalReference(obj, entry, |
| 2012 | "construct_stub", shared->construct_stub(), |
| 2013 | SharedFunctionInfo::kConstructStubOffset); |
| 2014 | SetInternalReference(obj, entry, |
| 2015 | "function_data", shared->function_data(), |
| 2016 | SharedFunctionInfo::kFunctionDataOffset); |
| 2017 | SetInternalReference(obj, entry, |
| 2018 | "debug_info", shared->debug_info(), |
| 2019 | SharedFunctionInfo::kDebugInfoOffset); |
| 2020 | SetInternalReference(obj, entry, |
| 2021 | "inferred_name", shared->inferred_name(), |
| 2022 | SharedFunctionInfo::kInferredNameOffset); |
| 2023 | SetInternalReference(obj, entry, |
| 2024 | "this_property_assignments", |
| 2025 | shared->this_property_assignments(), |
| 2026 | SharedFunctionInfo::kThisPropertyAssignmentsOffset); |
| 2027 | SetWeakReference(obj, entry, |
| 2028 | 1, shared->initial_map(), |
| 2029 | SharedFunctionInfo::kInitialMapOffset); |
| 2030 | } |
| 2031 | |
| 2032 | |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2033 | void V8HeapExplorer::ExtractScriptReferences(int entry, Script* script) { |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 2034 | HeapObject* obj = script; |
| 2035 | SetInternalReference(obj, entry, |
| 2036 | "source", script->source(), |
| 2037 | Script::kSourceOffset); |
| 2038 | SetInternalReference(obj, entry, |
| 2039 | "name", script->name(), |
| 2040 | Script::kNameOffset); |
| 2041 | SetInternalReference(obj, entry, |
| 2042 | "data", script->data(), |
| 2043 | Script::kDataOffset); |
| 2044 | SetInternalReference(obj, entry, |
| 2045 | "context_data", script->context_data(), |
| 2046 | Script::kContextOffset); |
| 2047 | TagObject(script->line_ends(), "(script line ends)"); |
| 2048 | SetInternalReference(obj, entry, |
| 2049 | "line_ends", script->line_ends(), |
| 2050 | Script::kLineEndsOffset); |
| 2051 | } |
| 2052 | |
| 2053 | |
| 2054 | void V8HeapExplorer::ExtractCodeCacheReferences( |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2055 | int entry, CodeCache* code_cache) { |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 2056 | TagObject(code_cache->default_cache(), "(default code cache)"); |
| 2057 | SetInternalReference(code_cache, entry, |
| 2058 | "default_cache", code_cache->default_cache(), |
| 2059 | CodeCache::kDefaultCacheOffset); |
| 2060 | TagObject(code_cache->normal_type_cache(), "(code type cache)"); |
| 2061 | SetInternalReference(code_cache, entry, |
| 2062 | "type_cache", code_cache->normal_type_cache(), |
| 2063 | CodeCache::kNormalTypeCacheOffset); |
| 2064 | } |
| 2065 | |
| 2066 | |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2067 | void V8HeapExplorer::ExtractCodeReferences(int entry, Code* code) { |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 2068 | TagObject(code->relocation_info(), "(code relocation info)"); |
| 2069 | SetInternalReference(code, entry, |
| 2070 | "relocation_info", code->relocation_info(), |
| 2071 | Code::kRelocationInfoOffset); |
| 2072 | SetInternalReference(code, entry, |
| 2073 | "handler_table", code->handler_table(), |
| 2074 | Code::kHandlerTableOffset); |
| 2075 | TagObject(code->deoptimization_data(), "(code deopt data)"); |
| 2076 | SetInternalReference(code, entry, |
| 2077 | "deoptimization_data", code->deoptimization_data(), |
| 2078 | Code::kDeoptimizationDataOffset); |
| 2079 | SetInternalReference(code, entry, |
| 2080 | "type_feedback_info", code->type_feedback_info(), |
| 2081 | Code::kTypeFeedbackInfoOffset); |
| 2082 | SetInternalReference(code, entry, |
| 2083 | "gc_metadata", code->gc_metadata(), |
| 2084 | Code::kGCMetadataOffset); |
| 2085 | } |
| 2086 | |
| 2087 | |
| 2088 | void V8HeapExplorer::ExtractJSGlobalPropertyCellReferences( |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2089 | int entry, JSGlobalPropertyCell* cell) { |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 2090 | SetInternalReference(cell, entry, "value", cell->value()); |
| 2091 | } |
| 2092 | |
| 2093 | |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2094 | void V8HeapExplorer::ExtractClosureReferences(JSObject* js_obj, int entry) { |
ulan@chromium.org | 65a89c2 | 2012-02-14 11:46:07 +0000 | [diff] [blame] | 2095 | if (!js_obj->IsJSFunction()) return; |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 2096 | |
ulan@chromium.org | 65a89c2 | 2012-02-14 11:46:07 +0000 | [diff] [blame] | 2097 | JSFunction* func = JSFunction::cast(js_obj); |
ulan@chromium.org | 65a89c2 | 2012-02-14 11:46:07 +0000 | [diff] [blame] | 2098 | if (func->shared()->bound()) { |
| 2099 | FixedArray* bindings = func->function_bindings(); |
| 2100 | SetNativeBindReference(js_obj, entry, "bound_this", |
| 2101 | bindings->get(JSFunction::kBoundThisIndex)); |
| 2102 | SetNativeBindReference(js_obj, entry, "bound_function", |
| 2103 | bindings->get(JSFunction::kBoundFunctionIndex)); |
| 2104 | for (int i = JSFunction::kBoundArgumentsStartIndex; |
| 2105 | i < bindings->length(); i++) { |
| 2106 | const char* reference_name = collection_->names()->GetFormatted( |
| 2107 | "bound_argument_%d", |
| 2108 | i - JSFunction::kBoundArgumentsStartIndex); |
| 2109 | SetNativeBindReference(js_obj, entry, reference_name, |
| 2110 | bindings->get(i)); |
| 2111 | } |
| 2112 | } else { |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 2113 | Context* context = func->context()->declaration_context(); |
| 2114 | ScopeInfo* scope_info = context->closure()->shared()->scope_info(); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 2115 | // Add context allocated locals. |
| 2116 | int context_locals = scope_info->ContextLocalCount(); |
| 2117 | for (int i = 0; i < context_locals; ++i) { |
| 2118 | String* local_name = scope_info->ContextLocalName(i); |
| 2119 | int idx = Context::MIN_CONTEXT_SLOTS + i; |
| 2120 | SetClosureReference(js_obj, entry, local_name, context->get(idx)); |
| 2121 | } |
| 2122 | |
| 2123 | // Add function variable. |
| 2124 | if (scope_info->HasFunctionName()) { |
| 2125 | String* name = scope_info->FunctionName(); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 2126 | VariableMode mode; |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 2127 | int idx = scope_info->FunctionContextSlotIndex(name, &mode); |
| 2128 | if (idx >= 0) { |
| 2129 | SetClosureReference(js_obj, entry, name, context->get(idx)); |
| 2130 | } |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 2131 | } |
| 2132 | } |
| 2133 | } |
| 2134 | |
| 2135 | |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2136 | void V8HeapExplorer::ExtractPropertyReferences(JSObject* js_obj, int entry) { |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 2137 | if (js_obj->HasFastProperties()) { |
| 2138 | DescriptorArray* descs = js_obj->map()->instance_descriptors(); |
| 2139 | for (int i = 0; i < descs->number_of_descriptors(); i++) { |
| 2140 | switch (descs->GetType(i)) { |
| 2141 | case FIELD: { |
| 2142 | int index = descs->GetFieldIndex(i); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2143 | if (index < js_obj->map()->inobject_properties()) { |
| 2144 | SetPropertyReference( |
| 2145 | js_obj, entry, |
| 2146 | descs->GetKey(i), js_obj->InObjectPropertyAt(index), |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 2147 | NULL, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2148 | js_obj->GetInObjectPropertyOffset(index)); |
| 2149 | } else { |
| 2150 | SetPropertyReference( |
| 2151 | js_obj, entry, |
| 2152 | descs->GetKey(i), js_obj->FastPropertyAt(index)); |
| 2153 | } |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 2154 | break; |
| 2155 | } |
| 2156 | case CONSTANT_FUNCTION: |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 2157 | SetPropertyReference( |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2158 | js_obj, entry, |
| 2159 | descs->GetKey(i), descs->GetConstantFunction(i)); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 2160 | break; |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 2161 | case CALLBACKS: { |
| 2162 | Object* callback_obj = descs->GetValue(i); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 2163 | if (callback_obj->IsAccessorPair()) { |
| 2164 | AccessorPair* accessors = AccessorPair::cast(callback_obj); |
| 2165 | if (Object* getter = accessors->getter()) { |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 2166 | SetPropertyReference(js_obj, entry, descs->GetKey(i), |
| 2167 | getter, "get-%s"); |
| 2168 | } |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 2169 | if (Object* setter = accessors->setter()) { |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 2170 | SetPropertyReference(js_obj, entry, descs->GetKey(i), |
| 2171 | setter, "set-%s"); |
| 2172 | } |
| 2173 | } |
| 2174 | break; |
| 2175 | } |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 2176 | case NORMAL: // only in slow mode |
| 2177 | case HANDLER: // only in lookup results, not in descriptors |
| 2178 | case INTERCEPTOR: // only in lookup results, not in descriptors |
| 2179 | case MAP_TRANSITION: // we do not care about transitions here... |
| 2180 | case ELEMENTS_TRANSITION: |
| 2181 | case CONSTANT_TRANSITION: |
| 2182 | case NULL_DESCRIPTOR: // ... and not about "holes" |
| 2183 | break; |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 2184 | } |
| 2185 | } |
| 2186 | } else { |
| 2187 | StringDictionary* dictionary = js_obj->property_dictionary(); |
| 2188 | int length = dictionary->Capacity(); |
| 2189 | for (int i = 0; i < length; ++i) { |
| 2190 | Object* k = dictionary->KeyAt(i); |
| 2191 | if (dictionary->IsKey(k)) { |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 2192 | Object* target = dictionary->ValueAt(i); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 2193 | // We assume that global objects can only have slow properties. |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 2194 | Object* value = target->IsJSGlobalPropertyCell() |
| 2195 | ? JSGlobalPropertyCell::cast(target)->value() |
| 2196 | : target; |
| 2197 | if (String::cast(k)->length() > 0) { |
| 2198 | SetPropertyReference(js_obj, entry, String::cast(k), value); |
| 2199 | } else { |
| 2200 | TagObject(value, "(hidden properties)"); |
| 2201 | SetInternalReference(js_obj, entry, "hidden_properties", value); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 2202 | } |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 2203 | } |
| 2204 | } |
| 2205 | } |
| 2206 | } |
| 2207 | |
| 2208 | |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2209 | void V8HeapExplorer::ExtractElementReferences(JSObject* js_obj, int entry) { |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 2210 | if (js_obj->HasFastElements()) { |
| 2211 | FixedArray* elements = FixedArray::cast(js_obj->elements()); |
| 2212 | int length = js_obj->IsJSArray() ? |
| 2213 | Smi::cast(JSArray::cast(js_obj)->length())->value() : |
| 2214 | elements->length(); |
| 2215 | for (int i = 0; i < length; ++i) { |
| 2216 | if (!elements->get(i)->IsTheHole()) { |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 2217 | SetElementReference(js_obj, entry, i, elements->get(i)); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 2218 | } |
| 2219 | } |
| 2220 | } else if (js_obj->HasDictionaryElements()) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 2221 | SeededNumberDictionary* dictionary = js_obj->element_dictionary(); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 2222 | int length = dictionary->Capacity(); |
| 2223 | for (int i = 0; i < length; ++i) { |
| 2224 | Object* k = dictionary->KeyAt(i); |
| 2225 | if (dictionary->IsKey(k)) { |
| 2226 | ASSERT(k->IsNumber()); |
| 2227 | uint32_t index = static_cast<uint32_t>(k->Number()); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 2228 | SetElementReference(js_obj, entry, index, dictionary->ValueAt(i)); |
ager@chromium.org | 2cc82ae | 2010-06-14 07:35:38 +0000 | [diff] [blame] | 2229 | } |
| 2230 | } |
| 2231 | } |
| 2232 | } |
| 2233 | |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 2234 | |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2235 | void V8HeapExplorer::ExtractInternalReferences(JSObject* js_obj, int entry) { |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 2236 | int length = js_obj->GetInternalFieldCount(); |
| 2237 | for (int i = 0; i < length; ++i) { |
| 2238 | Object* o = js_obj->GetInternalField(i); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2239 | SetInternalReference( |
| 2240 | js_obj, entry, i, o, js_obj->GetInternalFieldOffset(i)); |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 2241 | } |
| 2242 | } |
| 2243 | |
| 2244 | |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 2245 | String* V8HeapExplorer::GetConstructorName(JSObject* object) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2246 | Heap* heap = object->GetHeap(); |
| 2247 | if (object->IsJSFunction()) return heap->closure_symbol(); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 2248 | String* constructor_name = object->constructor_name(); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2249 | if (constructor_name == heap->Object_symbol()) { |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 2250 | // Look up an immediate "constructor" property, if it is a function, |
| 2251 | // return its name. This is for instances of binding objects, which |
| 2252 | // have prototype constructor type "Object". |
| 2253 | Object* constructor_prop = NULL; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 2254 | LookupResult result(heap->isolate()); |
| 2255 | object->LocalLookupRealNamedProperty(heap->constructor_symbol(), &result); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 2256 | if (result.IsProperty()) { |
| 2257 | constructor_prop = result.GetLazyValue(); |
| 2258 | } |
| 2259 | if (constructor_prop->IsJSFunction()) { |
| 2260 | Object* maybe_name = JSFunction::cast(constructor_prop)->shared()->name(); |
| 2261 | if (maybe_name->IsString()) { |
| 2262 | String* name = String::cast(maybe_name); |
| 2263 | if (name->length() > 0) return name; |
| 2264 | } |
| 2265 | } |
| 2266 | } |
| 2267 | return object->constructor_name(); |
| 2268 | } |
| 2269 | |
| 2270 | |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2271 | HeapEntry* V8HeapExplorer::GetEntry(Object* obj) { |
| 2272 | if (!obj->IsHeapObject()) return NULL; |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 2273 | return filler_->FindOrAddEntry(obj, this); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2274 | } |
| 2275 | |
| 2276 | |
| 2277 | class RootsReferencesExtractor : public ObjectVisitor { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2278 | private: |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 2279 | struct IndexTag { |
| 2280 | IndexTag(int index, VisitorSynchronization::SyncTag tag) |
| 2281 | : index(index), tag(tag) { } |
| 2282 | int index; |
| 2283 | VisitorSynchronization::SyncTag tag; |
| 2284 | }; |
| 2285 | |
| 2286 | public: |
| 2287 | RootsReferencesExtractor() |
| 2288 | : collecting_all_references_(false), |
| 2289 | previous_reference_count_(0) { |
| 2290 | } |
| 2291 | |
| 2292 | void VisitPointers(Object** start, Object** end) { |
| 2293 | if (collecting_all_references_) { |
| 2294 | for (Object** p = start; p < end; p++) all_references_.Add(*p); |
| 2295 | } else { |
| 2296 | for (Object** p = start; p < end; p++) strong_references_.Add(*p); |
| 2297 | } |
| 2298 | } |
| 2299 | |
| 2300 | void SetCollectingAllReferences() { collecting_all_references_ = true; } |
| 2301 | |
| 2302 | void FillReferences(V8HeapExplorer* explorer) { |
| 2303 | ASSERT(strong_references_.length() <= all_references_.length()); |
| 2304 | for (int i = 0; i < reference_tags_.length(); ++i) { |
| 2305 | explorer->SetGcRootsReference(reference_tags_[i].tag); |
| 2306 | } |
| 2307 | int strong_index = 0, all_index = 0, tags_index = 0; |
| 2308 | while (all_index < all_references_.length()) { |
| 2309 | if (strong_index < strong_references_.length() && |
| 2310 | strong_references_[strong_index] == all_references_[all_index]) { |
| 2311 | explorer->SetGcSubrootReference(reference_tags_[tags_index].tag, |
| 2312 | false, |
| 2313 | all_references_[all_index++]); |
| 2314 | ++strong_index; |
| 2315 | } else { |
| 2316 | explorer->SetGcSubrootReference(reference_tags_[tags_index].tag, |
| 2317 | true, |
| 2318 | all_references_[all_index++]); |
| 2319 | } |
| 2320 | if (reference_tags_[tags_index].index == all_index) ++tags_index; |
| 2321 | } |
| 2322 | } |
| 2323 | |
| 2324 | void Synchronize(VisitorSynchronization::SyncTag tag) { |
| 2325 | if (collecting_all_references_ && |
| 2326 | previous_reference_count_ != all_references_.length()) { |
| 2327 | previous_reference_count_ = all_references_.length(); |
| 2328 | reference_tags_.Add(IndexTag(previous_reference_count_, tag)); |
| 2329 | } |
| 2330 | } |
| 2331 | |
| 2332 | private: |
| 2333 | bool collecting_all_references_; |
| 2334 | List<Object*> strong_references_; |
| 2335 | List<Object*> all_references_; |
| 2336 | int previous_reference_count_; |
| 2337 | List<IndexTag> reference_tags_; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2338 | }; |
| 2339 | |
| 2340 | |
| 2341 | bool V8HeapExplorer::IterateAndExtractReferences( |
| 2342 | SnapshotFillerInterface* filler) { |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2343 | HeapIterator iterator(HeapIterator::kFilterUnreachable); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2344 | |
| 2345 | filler_ = filler; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2346 | bool interrupted = false; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2347 | |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2348 | // Heap iteration with filtering must be finished in any case. |
| 2349 | for (HeapObject* obj = iterator.next(); |
| 2350 | obj != NULL; |
| 2351 | obj = iterator.next(), progress_->ProgressStep()) { |
| 2352 | if (!interrupted) { |
| 2353 | ExtractReferences(obj); |
| 2354 | if (!progress_->ProgressReport(false)) interrupted = true; |
| 2355 | } |
| 2356 | } |
| 2357 | if (interrupted) { |
| 2358 | filler_ = NULL; |
| 2359 | return false; |
| 2360 | } |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2361 | |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2362 | SetRootGcRootsReference(); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 2363 | RootsReferencesExtractor extractor; |
| 2364 | heap_->IterateRoots(&extractor, VISIT_ONLY_STRONG); |
| 2365 | extractor.SetCollectingAllReferences(); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2366 | heap_->IterateRoots(&extractor, VISIT_ALL); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 2367 | extractor.FillReferences(this); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2368 | filler_ = NULL; |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2369 | return progress_->ProgressReport(true); |
ulan@chromium.org | 9a21ec4 | 2012-03-06 08:42:24 +0000 | [diff] [blame] | 2370 | } |
| 2371 | |
| 2372 | |
mstarzinger@chromium.org | 88d326b | 2012-04-23 12:57:22 +0000 | [diff] [blame] | 2373 | bool V8HeapExplorer::IsEssentialObject(Object* object) { |
| 2374 | // We have to use raw_unchecked_* versions because checked versions |
| 2375 | // would fail during iteration over object properties. |
| 2376 | return object->IsHeapObject() |
| 2377 | && !object->IsOddball() |
| 2378 | && object != heap_->raw_unchecked_empty_byte_array() |
| 2379 | && object != heap_->raw_unchecked_empty_fixed_array() |
| 2380 | && object != heap_->raw_unchecked_empty_descriptor_array() |
| 2381 | && object != heap_->raw_unchecked_fixed_array_map() |
| 2382 | && object != heap_->raw_unchecked_global_property_cell_map() |
| 2383 | && object != heap_->raw_unchecked_shared_function_info_map() |
| 2384 | && object != heap_->raw_unchecked_free_space_map() |
| 2385 | && object != heap_->raw_unchecked_one_pointer_filler_map() |
| 2386 | && object != heap_->raw_unchecked_two_pointer_filler_map(); |
| 2387 | } |
| 2388 | |
| 2389 | |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2390 | void V8HeapExplorer::SetClosureReference(HeapObject* parent_obj, |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2391 | int parent_entry, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2392 | String* reference_name, |
| 2393 | Object* child_obj) { |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 2394 | HeapEntry* child_entry = GetEntry(child_obj); |
| 2395 | if (child_entry != NULL) { |
| 2396 | filler_->SetNamedReference(HeapGraphEdge::kContextVariable, |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 2397 | parent_entry, |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 2398 | collection_->names()->GetName(reference_name), |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 2399 | child_entry); |
| 2400 | } |
| 2401 | } |
| 2402 | |
| 2403 | |
ulan@chromium.org | 65a89c2 | 2012-02-14 11:46:07 +0000 | [diff] [blame] | 2404 | void V8HeapExplorer::SetNativeBindReference(HeapObject* parent_obj, |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2405 | int parent_entry, |
ulan@chromium.org | 65a89c2 | 2012-02-14 11:46:07 +0000 | [diff] [blame] | 2406 | const char* reference_name, |
| 2407 | Object* child_obj) { |
| 2408 | HeapEntry* child_entry = GetEntry(child_obj); |
| 2409 | if (child_entry != NULL) { |
| 2410 | filler_->SetNamedReference(HeapGraphEdge::kShortcut, |
ulan@chromium.org | 65a89c2 | 2012-02-14 11:46:07 +0000 | [diff] [blame] | 2411 | parent_entry, |
| 2412 | reference_name, |
ulan@chromium.org | 65a89c2 | 2012-02-14 11:46:07 +0000 | [diff] [blame] | 2413 | child_entry); |
| 2414 | } |
| 2415 | } |
| 2416 | |
| 2417 | |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2418 | void V8HeapExplorer::SetElementReference(HeapObject* parent_obj, |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2419 | int parent_entry, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2420 | int index, |
| 2421 | Object* child_obj) { |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 2422 | HeapEntry* child_entry = GetEntry(child_obj); |
| 2423 | if (child_entry != NULL) { |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 2424 | filler_->SetIndexedReference(HeapGraphEdge::kElement, |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 2425 | parent_entry, |
| 2426 | index, |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 2427 | child_entry); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 2428 | } |
| 2429 | } |
| 2430 | |
| 2431 | |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2432 | void V8HeapExplorer::SetInternalReference(HeapObject* parent_obj, |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2433 | int parent_entry, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2434 | const char* reference_name, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2435 | Object* child_obj, |
| 2436 | int field_offset) { |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 2437 | HeapEntry* child_entry = GetEntry(child_obj); |
mstarzinger@chromium.org | 88d326b | 2012-04-23 12:57:22 +0000 | [diff] [blame] | 2438 | if (child_entry == NULL) return; |
| 2439 | if (IsEssentialObject(child_obj)) { |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 2440 | filler_->SetNamedReference(HeapGraphEdge::kInternal, |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2441 | parent_entry, |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 2442 | reference_name, |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2443 | child_entry); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 2444 | } |
mstarzinger@chromium.org | 88d326b | 2012-04-23 12:57:22 +0000 | [diff] [blame] | 2445 | IndexedReferencesExtractor::MarkVisitedField(parent_obj, field_offset); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 2446 | } |
| 2447 | |
| 2448 | |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2449 | void V8HeapExplorer::SetInternalReference(HeapObject* parent_obj, |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2450 | int parent_entry, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2451 | int index, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2452 | Object* child_obj, |
| 2453 | int field_offset) { |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 2454 | HeapEntry* child_entry = GetEntry(child_obj); |
mstarzinger@chromium.org | 88d326b | 2012-04-23 12:57:22 +0000 | [diff] [blame] | 2455 | if (child_entry == NULL) return; |
| 2456 | if (IsEssentialObject(child_obj)) { |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 2457 | filler_->SetNamedReference(HeapGraphEdge::kInternal, |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2458 | parent_entry, |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 2459 | collection_->names()->GetName(index), |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2460 | child_entry); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 2461 | } |
mstarzinger@chromium.org | 88d326b | 2012-04-23 12:57:22 +0000 | [diff] [blame] | 2462 | IndexedReferencesExtractor::MarkVisitedField(parent_obj, field_offset); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 2463 | } |
| 2464 | |
| 2465 | |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2466 | void V8HeapExplorer::SetHiddenReference(HeapObject* parent_obj, |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2467 | int parent_entry, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2468 | int index, |
| 2469 | Object* child_obj) { |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 2470 | HeapEntry* child_entry = GetEntry(child_obj); |
mstarzinger@chromium.org | 88d326b | 2012-04-23 12:57:22 +0000 | [diff] [blame] | 2471 | if (child_entry != NULL && IsEssentialObject(child_obj)) { |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 2472 | filler_->SetIndexedReference(HeapGraphEdge::kHidden, |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 2473 | parent_entry, |
| 2474 | index, |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 2475 | child_entry); |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 2476 | } |
| 2477 | } |
| 2478 | |
| 2479 | |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 2480 | void V8HeapExplorer::SetWeakReference(HeapObject* parent_obj, |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2481 | int parent_entry, |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 2482 | int index, |
| 2483 | Object* child_obj, |
| 2484 | int field_offset) { |
| 2485 | HeapEntry* child_entry = GetEntry(child_obj); |
| 2486 | if (child_entry != NULL) { |
| 2487 | filler_->SetIndexedReference(HeapGraphEdge::kWeak, |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 2488 | parent_entry, |
| 2489 | index, |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 2490 | child_entry); |
| 2491 | IndexedReferencesExtractor::MarkVisitedField(parent_obj, field_offset); |
| 2492 | } |
| 2493 | } |
| 2494 | |
| 2495 | |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2496 | void V8HeapExplorer::SetPropertyReference(HeapObject* parent_obj, |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2497 | int parent_entry, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2498 | String* reference_name, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2499 | Object* child_obj, |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 2500 | const char* name_format_string, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2501 | int field_offset) { |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 2502 | HeapEntry* child_entry = GetEntry(child_obj); |
| 2503 | if (child_entry != NULL) { |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 2504 | HeapGraphEdge::Type type = reference_name->length() > 0 ? |
| 2505 | HeapGraphEdge::kProperty : HeapGraphEdge::kInternal; |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 2506 | const char* name = name_format_string != NULL ? |
| 2507 | collection_->names()->GetFormatted( |
| 2508 | name_format_string, |
| 2509 | *reference_name->ToCString(DISALLOW_NULLS, |
| 2510 | ROBUST_STRING_TRAVERSAL)) : |
| 2511 | collection_->names()->GetName(reference_name); |
| 2512 | |
vegorov@chromium.org | 4284196 | 2010-10-18 11:18:59 +0000 | [diff] [blame] | 2513 | filler_->SetNamedReference(type, |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 2514 | parent_entry, |
jkummerow@chromium.org | 04e4f1e | 2011-11-14 13:36:17 +0000 | [diff] [blame] | 2515 | name, |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 2516 | child_entry); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2517 | IndexedReferencesExtractor::MarkVisitedField(parent_obj, field_offset); |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 2518 | } |
| 2519 | } |
| 2520 | |
| 2521 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2522 | void V8HeapExplorer::SetPropertyShortcutReference(HeapObject* parent_obj, |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2523 | int parent_entry, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2524 | String* reference_name, |
| 2525 | Object* child_obj) { |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 2526 | HeapEntry* child_entry = GetEntry(child_obj); |
| 2527 | if (child_entry != NULL) { |
| 2528 | filler_->SetNamedReference(HeapGraphEdge::kShortcut, |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 2529 | parent_entry, |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 2530 | collection_->names()->GetName(reference_name), |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 2531 | child_entry); |
| 2532 | } |
| 2533 | } |
| 2534 | |
| 2535 | |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2536 | void V8HeapExplorer::SetRootGcRootsReference() { |
| 2537 | filler_->SetIndexedAutoIndexReference( |
| 2538 | HeapGraphEdge::kElement, |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2539 | snapshot_->root()->index(), |
| 2540 | snapshot_->gc_roots()); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 2541 | } |
| 2542 | |
| 2543 | |
svenpanne@chromium.org | fb04633 | 2012-04-19 12:02:44 +0000 | [diff] [blame] | 2544 | void V8HeapExplorer::SetUserGlobalReference(Object* child_obj) { |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 2545 | HeapEntry* child_entry = GetEntry(child_obj); |
| 2546 | ASSERT(child_entry != NULL); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2547 | filler_->SetNamedAutoIndexReference( |
| 2548 | HeapGraphEdge::kShortcut, |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2549 | snapshot_->root()->index(), |
| 2550 | child_entry); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 2551 | } |
| 2552 | |
| 2553 | |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 2554 | void V8HeapExplorer::SetGcRootsReference(VisitorSynchronization::SyncTag tag) { |
| 2555 | filler_->SetIndexedAutoIndexReference( |
| 2556 | HeapGraphEdge::kElement, |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2557 | snapshot_->gc_roots()->index(), |
| 2558 | snapshot_->gc_subroot(tag)); |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 2559 | } |
| 2560 | |
| 2561 | |
| 2562 | void V8HeapExplorer::SetGcSubrootReference( |
| 2563 | VisitorSynchronization::SyncTag tag, bool is_weak, Object* child_obj) { |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 2564 | HeapEntry* child_entry = GetEntry(child_obj); |
| 2565 | if (child_entry != NULL) { |
mstarzinger@chromium.org | 88d326b | 2012-04-23 12:57:22 +0000 | [diff] [blame] | 2566 | const char* name = GetStrongGcSubrootName(child_obj); |
| 2567 | if (name != NULL) { |
| 2568 | filler_->SetNamedReference( |
| 2569 | HeapGraphEdge::kInternal, |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2570 | snapshot_->gc_subroot(tag)->index(), |
mstarzinger@chromium.org | 88d326b | 2012-04-23 12:57:22 +0000 | [diff] [blame] | 2571 | name, |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2572 | child_entry); |
mstarzinger@chromium.org | 88d326b | 2012-04-23 12:57:22 +0000 | [diff] [blame] | 2573 | } else { |
| 2574 | filler_->SetIndexedAutoIndexReference( |
| 2575 | is_weak ? HeapGraphEdge::kWeak : HeapGraphEdge::kElement, |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2576 | snapshot_->gc_subroot(tag)->index(), |
| 2577 | child_entry); |
mstarzinger@chromium.org | 88d326b | 2012-04-23 12:57:22 +0000 | [diff] [blame] | 2578 | } |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 2579 | } |
vegorov@chromium.org | 26c16f8 | 2010-08-11 13:41:03 +0000 | [diff] [blame] | 2580 | } |
| 2581 | |
| 2582 | |
mstarzinger@chromium.org | 88d326b | 2012-04-23 12:57:22 +0000 | [diff] [blame] | 2583 | const char* V8HeapExplorer::GetStrongGcSubrootName(Object* object) { |
| 2584 | if (strong_gc_subroot_names_.is_empty()) { |
| 2585 | #define NAME_ENTRY(name) strong_gc_subroot_names_.SetTag(heap_->name(), #name); |
| 2586 | #define ROOT_NAME(type, name, camel_name) NAME_ENTRY(name) |
| 2587 | STRONG_ROOT_LIST(ROOT_NAME) |
| 2588 | #undef ROOT_NAME |
| 2589 | #define STRUCT_MAP_NAME(NAME, Name, name) NAME_ENTRY(name##_map) |
| 2590 | STRUCT_LIST(STRUCT_MAP_NAME) |
| 2591 | #undef STRUCT_MAP_NAME |
| 2592 | #define SYMBOL_NAME(name, str) NAME_ENTRY(name) |
| 2593 | SYMBOL_LIST(SYMBOL_NAME) |
| 2594 | #undef SYMBOL_NAME |
| 2595 | #undef NAME_ENTRY |
| 2596 | CHECK(!strong_gc_subroot_names_.is_empty()); |
| 2597 | } |
| 2598 | return strong_gc_subroot_names_.GetTag(object); |
| 2599 | } |
| 2600 | |
| 2601 | |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2602 | void V8HeapExplorer::TagObject(Object* obj, const char* tag) { |
mstarzinger@chromium.org | 88d326b | 2012-04-23 12:57:22 +0000 | [diff] [blame] | 2603 | if (IsEssentialObject(obj)) { |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2604 | HeapEntry* entry = GetEntry(obj); |
| 2605 | if (entry->name()[0] == '\0') { |
| 2606 | entry->set_name(tag); |
| 2607 | } |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 2608 | } |
| 2609 | } |
| 2610 | |
| 2611 | |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 2612 | class GlobalObjectsEnumerator : public ObjectVisitor { |
| 2613 | public: |
| 2614 | virtual void VisitPointers(Object** start, Object** end) { |
| 2615 | for (Object** p = start; p < end; p++) { |
| 2616 | if ((*p)->IsGlobalContext()) { |
| 2617 | Context* context = Context::cast(*p); |
| 2618 | JSObject* proxy = context->global_proxy(); |
| 2619 | if (proxy->IsJSGlobalProxy()) { |
| 2620 | Object* global = proxy->map()->prototype(); |
| 2621 | if (global->IsJSGlobalObject()) { |
| 2622 | objects_.Add(Handle<JSGlobalObject>(JSGlobalObject::cast(global))); |
| 2623 | } |
| 2624 | } |
| 2625 | } |
| 2626 | } |
| 2627 | } |
| 2628 | int count() { return objects_.length(); } |
| 2629 | Handle<JSGlobalObject>& at(int i) { return objects_[i]; } |
| 2630 | |
| 2631 | private: |
| 2632 | List<Handle<JSGlobalObject> > objects_; |
| 2633 | }; |
| 2634 | |
| 2635 | |
| 2636 | // Modifies heap. Must not be run during heap traversal. |
| 2637 | void V8HeapExplorer::TagGlobalObjects() { |
erikcorry | 0ad885c | 2011-11-21 13:51:57 +0000 | [diff] [blame] | 2638 | HandleScope scope; |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 2639 | Isolate* isolate = Isolate::Current(); |
| 2640 | GlobalObjectsEnumerator enumerator; |
| 2641 | isolate->global_handles()->IterateAllRoots(&enumerator); |
| 2642 | Handle<String> document_string = |
| 2643 | isolate->factory()->NewStringFromAscii(CStrVector("document")); |
| 2644 | Handle<String> url_string = |
| 2645 | isolate->factory()->NewStringFromAscii(CStrVector("URL")); |
| 2646 | const char** urls = NewArray<const char*>(enumerator.count()); |
| 2647 | for (int i = 0, l = enumerator.count(); i < l; ++i) { |
| 2648 | urls[i] = NULL; |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 2649 | HandleScope scope; |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 2650 | Handle<JSGlobalObject> global_obj = enumerator.at(i); |
| 2651 | Object* obj_document; |
| 2652 | if (global_obj->GetProperty(*document_string)->ToObject(&obj_document) && |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 2653 | obj_document->IsJSObject()) { |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 2654 | JSObject* document = JSObject::cast(obj_document); |
| 2655 | Object* obj_url; |
| 2656 | if (document->GetProperty(*url_string)->ToObject(&obj_url) && |
| 2657 | obj_url->IsString()) { |
| 2658 | urls[i] = collection_->names()->GetName(String::cast(obj_url)); |
| 2659 | } |
| 2660 | } |
| 2661 | } |
| 2662 | |
| 2663 | AssertNoAllocation no_allocation; |
| 2664 | for (int i = 0, l = enumerator.count(); i < l; ++i) { |
| 2665 | objects_tags_.SetTag(*enumerator.at(i), urls[i]); |
| 2666 | } |
| 2667 | |
| 2668 | DeleteArray(urls); |
| 2669 | } |
| 2670 | |
| 2671 | |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 2672 | class GlobalHandlesExtractor : public ObjectVisitor { |
| 2673 | public: |
| 2674 | explicit GlobalHandlesExtractor(NativeObjectsExplorer* explorer) |
| 2675 | : explorer_(explorer) {} |
| 2676 | virtual ~GlobalHandlesExtractor() {} |
| 2677 | virtual void VisitPointers(Object** start, Object** end) { |
| 2678 | UNREACHABLE(); |
| 2679 | } |
| 2680 | virtual void VisitEmbedderReference(Object** p, uint16_t class_id) { |
| 2681 | explorer_->VisitSubtreeWrapper(p, class_id); |
| 2682 | } |
| 2683 | private: |
| 2684 | NativeObjectsExplorer* explorer_; |
| 2685 | }; |
| 2686 | |
jkummerow@chromium.org | ab7dad4 | 2012-02-07 12:07:34 +0000 | [diff] [blame] | 2687 | |
| 2688 | class BasicHeapEntriesAllocator : public HeapEntriesAllocator { |
| 2689 | public: |
| 2690 | BasicHeapEntriesAllocator( |
| 2691 | HeapSnapshot* snapshot, |
| 2692 | HeapEntry::Type entries_type) |
| 2693 | : snapshot_(snapshot), |
| 2694 | collection_(snapshot_->collection()), |
| 2695 | entries_type_(entries_type) { |
| 2696 | } |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2697 | virtual HeapEntry* AllocateEntry(HeapThing ptr); |
jkummerow@chromium.org | ab7dad4 | 2012-02-07 12:07:34 +0000 | [diff] [blame] | 2698 | private: |
| 2699 | HeapSnapshot* snapshot_; |
| 2700 | HeapSnapshotsCollection* collection_; |
| 2701 | HeapEntry::Type entries_type_; |
| 2702 | }; |
| 2703 | |
| 2704 | |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2705 | HeapEntry* BasicHeapEntriesAllocator::AllocateEntry(HeapThing ptr) { |
jkummerow@chromium.org | ab7dad4 | 2012-02-07 12:07:34 +0000 | [diff] [blame] | 2706 | v8::RetainedObjectInfo* info = reinterpret_cast<v8::RetainedObjectInfo*>(ptr); |
| 2707 | intptr_t elements = info->GetElementCount(); |
| 2708 | intptr_t size = info->GetSizeInBytes(); |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2709 | const char* name = elements != -1 |
| 2710 | ? collection_->names()->GetFormatted( |
| 2711 | "%s / %" V8_PTR_PREFIX "d entries", info->GetLabel(), elements) |
| 2712 | : collection_->names()->GetCopy(info->GetLabel()); |
jkummerow@chromium.org | ab7dad4 | 2012-02-07 12:07:34 +0000 | [diff] [blame] | 2713 | return snapshot_->AddEntry( |
| 2714 | entries_type_, |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2715 | name, |
jkummerow@chromium.org | ab7dad4 | 2012-02-07 12:07:34 +0000 | [diff] [blame] | 2716 | HeapObjectsMap::GenerateId(info), |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2717 | size != -1 ? static_cast<int>(size) : 0); |
jkummerow@chromium.org | ab7dad4 | 2012-02-07 12:07:34 +0000 | [diff] [blame] | 2718 | } |
| 2719 | |
| 2720 | |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 2721 | NativeObjectsExplorer::NativeObjectsExplorer( |
| 2722 | HeapSnapshot* snapshot, SnapshottingProgressReportingInterface* progress) |
| 2723 | : snapshot_(snapshot), |
| 2724 | collection_(snapshot_->collection()), |
| 2725 | progress_(progress), |
| 2726 | embedder_queried_(false), |
| 2727 | objects_by_info_(RetainedInfosMatch), |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 2728 | native_groups_(StringsMatch), |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 2729 | filler_(NULL) { |
jkummerow@chromium.org | ab7dad4 | 2012-02-07 12:07:34 +0000 | [diff] [blame] | 2730 | synthetic_entries_allocator_ = |
| 2731 | new BasicHeapEntriesAllocator(snapshot, HeapEntry::kSynthetic); |
| 2732 | native_entries_allocator_ = |
| 2733 | new BasicHeapEntriesAllocator(snapshot, HeapEntry::kNative); |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 2734 | } |
| 2735 | |
| 2736 | |
| 2737 | NativeObjectsExplorer::~NativeObjectsExplorer() { |
| 2738 | for (HashMap::Entry* p = objects_by_info_.Start(); |
| 2739 | p != NULL; |
| 2740 | p = objects_by_info_.Next(p)) { |
| 2741 | v8::RetainedObjectInfo* info = |
| 2742 | reinterpret_cast<v8::RetainedObjectInfo*>(p->key); |
| 2743 | info->Dispose(); |
| 2744 | List<HeapObject*>* objects = |
| 2745 | reinterpret_cast<List<HeapObject*>* >(p->value); |
| 2746 | delete objects; |
| 2747 | } |
jkummerow@chromium.org | 1145ef8 | 2012-02-02 16:21:15 +0000 | [diff] [blame] | 2748 | for (HashMap::Entry* p = native_groups_.Start(); |
| 2749 | p != NULL; |
| 2750 | p = native_groups_.Next(p)) { |
| 2751 | v8::RetainedObjectInfo* info = |
| 2752 | reinterpret_cast<v8::RetainedObjectInfo*>(p->value); |
| 2753 | info->Dispose(); |
| 2754 | } |
jkummerow@chromium.org | ab7dad4 | 2012-02-07 12:07:34 +0000 | [diff] [blame] | 2755 | delete synthetic_entries_allocator_; |
| 2756 | delete native_entries_allocator_; |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 2757 | } |
| 2758 | |
| 2759 | |
| 2760 | int NativeObjectsExplorer::EstimateObjectsCount() { |
| 2761 | FillRetainedObjects(); |
| 2762 | return objects_by_info_.occupancy(); |
| 2763 | } |
| 2764 | |
| 2765 | |
| 2766 | void NativeObjectsExplorer::FillRetainedObjects() { |
| 2767 | if (embedder_queried_) return; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2768 | Isolate* isolate = Isolate::Current(); |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 2769 | // Record objects that are joined into ObjectGroups. |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2770 | isolate->heap()->CallGlobalGCPrologueCallback(); |
| 2771 | List<ObjectGroup*>* groups = isolate->global_handles()->object_groups(); |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 2772 | for (int i = 0; i < groups->length(); ++i) { |
| 2773 | ObjectGroup* group = groups->at(i); |
| 2774 | if (group->info_ == NULL) continue; |
| 2775 | List<HeapObject*>* list = GetListMaybeDisposeInfo(group->info_); |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 2776 | for (size_t j = 0; j < group->length_; ++j) { |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 2777 | HeapObject* obj = HeapObject::cast(*group->objects_[j]); |
| 2778 | list->Add(obj); |
| 2779 | in_groups_.Insert(obj); |
| 2780 | } |
| 2781 | group->info_ = NULL; // Acquire info object ownership. |
| 2782 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2783 | isolate->global_handles()->RemoveObjectGroups(); |
| 2784 | isolate->heap()->CallGlobalGCEpilogueCallback(); |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 2785 | // Record objects that are not in ObjectGroups, but have class ID. |
| 2786 | GlobalHandlesExtractor extractor(this); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2787 | isolate->global_handles()->IterateAllRootsWithClassIds(&extractor); |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 2788 | embedder_queried_ = true; |
| 2789 | } |
| 2790 | |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 2791 | void NativeObjectsExplorer::FillImplicitReferences() { |
| 2792 | Isolate* isolate = Isolate::Current(); |
| 2793 | List<ImplicitRefGroup*>* groups = |
| 2794 | isolate->global_handles()->implicit_ref_groups(); |
| 2795 | for (int i = 0; i < groups->length(); ++i) { |
| 2796 | ImplicitRefGroup* group = groups->at(i); |
| 2797 | HeapObject* parent = *group->parent_; |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2798 | int parent_entry = |
| 2799 | filler_->FindOrAddEntry(parent, native_entries_allocator_)->index(); |
| 2800 | ASSERT(parent_entry != HeapEntry::kNoEntry); |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 2801 | Object*** children = group->children_; |
| 2802 | for (size_t j = 0; j < group->length_; ++j) { |
| 2803 | Object* child = *children[j]; |
jkummerow@chromium.org | ab7dad4 | 2012-02-07 12:07:34 +0000 | [diff] [blame] | 2804 | HeapEntry* child_entry = |
| 2805 | filler_->FindOrAddEntry(child, native_entries_allocator_); |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 2806 | filler_->SetNamedReference( |
| 2807 | HeapGraphEdge::kInternal, |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2808 | parent_entry, |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 2809 | "native", |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2810 | child_entry); |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 2811 | } |
| 2812 | } |
| 2813 | } |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 2814 | |
| 2815 | List<HeapObject*>* NativeObjectsExplorer::GetListMaybeDisposeInfo( |
| 2816 | v8::RetainedObjectInfo* info) { |
| 2817 | HashMap::Entry* entry = |
| 2818 | objects_by_info_.Lookup(info, InfoHash(info), true); |
| 2819 | if (entry->value != NULL) { |
| 2820 | info->Dispose(); |
| 2821 | } else { |
| 2822 | entry->value = new List<HeapObject*>(4); |
| 2823 | } |
| 2824 | return reinterpret_cast<List<HeapObject*>* >(entry->value); |
| 2825 | } |
| 2826 | |
| 2827 | |
| 2828 | bool NativeObjectsExplorer::IterateAndExtractReferences( |
| 2829 | SnapshotFillerInterface* filler) { |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 2830 | filler_ = filler; |
| 2831 | FillRetainedObjects(); |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 2832 | FillImplicitReferences(); |
| 2833 | if (EstimateObjectsCount() > 0) { |
| 2834 | for (HashMap::Entry* p = objects_by_info_.Start(); |
| 2835 | p != NULL; |
| 2836 | p = objects_by_info_.Next(p)) { |
| 2837 | v8::RetainedObjectInfo* info = |
| 2838 | reinterpret_cast<v8::RetainedObjectInfo*>(p->key); |
| 2839 | SetNativeRootReference(info); |
| 2840 | List<HeapObject*>* objects = |
| 2841 | reinterpret_cast<List<HeapObject*>* >(p->value); |
| 2842 | for (int i = 0; i < objects->length(); ++i) { |
| 2843 | SetWrapperNativeReferences(objects->at(i), info); |
| 2844 | } |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 2845 | } |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 2846 | SetRootNativeRootsReference(); |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 2847 | } |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 2848 | filler_ = NULL; |
| 2849 | return true; |
| 2850 | } |
| 2851 | |
| 2852 | |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 2853 | class NativeGroupRetainedObjectInfo : public v8::RetainedObjectInfo { |
| 2854 | public: |
| 2855 | explicit NativeGroupRetainedObjectInfo(const char* label) |
| 2856 | : disposed_(false), |
| 2857 | hash_(reinterpret_cast<intptr_t>(label)), |
| 2858 | label_(label) { |
| 2859 | } |
| 2860 | |
| 2861 | virtual ~NativeGroupRetainedObjectInfo() {} |
| 2862 | virtual void Dispose() { |
| 2863 | CHECK(!disposed_); |
| 2864 | disposed_ = true; |
jkummerow@chromium.org | 1145ef8 | 2012-02-02 16:21:15 +0000 | [diff] [blame] | 2865 | delete this; |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 2866 | } |
| 2867 | virtual bool IsEquivalent(RetainedObjectInfo* other) { |
| 2868 | return hash_ == other->GetHash() && !strcmp(label_, other->GetLabel()); |
| 2869 | } |
| 2870 | virtual intptr_t GetHash() { return hash_; } |
| 2871 | virtual const char* GetLabel() { return label_; } |
| 2872 | |
| 2873 | private: |
| 2874 | bool disposed_; |
| 2875 | intptr_t hash_; |
| 2876 | const char* label_; |
| 2877 | }; |
| 2878 | |
| 2879 | |
| 2880 | NativeGroupRetainedObjectInfo* NativeObjectsExplorer::FindOrAddGroupInfo( |
| 2881 | const char* label) { |
| 2882 | const char* label_copy = collection_->names()->GetCopy(label); |
| 2883 | uint32_t hash = HashSequentialString(label_copy, |
| 2884 | static_cast<int>(strlen(label_copy)), |
| 2885 | HEAP->HashSeed()); |
| 2886 | HashMap::Entry* entry = native_groups_.Lookup(const_cast<char*>(label_copy), |
| 2887 | hash, true); |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2888 | if (entry->value == NULL) { |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 2889 | entry->value = new NativeGroupRetainedObjectInfo(label); |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2890 | } |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 2891 | return static_cast<NativeGroupRetainedObjectInfo*>(entry->value); |
| 2892 | } |
| 2893 | |
| 2894 | |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 2895 | void NativeObjectsExplorer::SetNativeRootReference( |
| 2896 | v8::RetainedObjectInfo* info) { |
jkummerow@chromium.org | ab7dad4 | 2012-02-07 12:07:34 +0000 | [diff] [blame] | 2897 | HeapEntry* child_entry = |
| 2898 | filler_->FindOrAddEntry(info, native_entries_allocator_); |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 2899 | ASSERT(child_entry != NULL); |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 2900 | NativeGroupRetainedObjectInfo* group_info = |
| 2901 | FindOrAddGroupInfo(info->GetGroupLabel()); |
jkummerow@chromium.org | ab7dad4 | 2012-02-07 12:07:34 +0000 | [diff] [blame] | 2902 | HeapEntry* group_entry = |
| 2903 | filler_->FindOrAddEntry(group_info, synthetic_entries_allocator_); |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 2904 | filler_->SetNamedAutoIndexReference( |
| 2905 | HeapGraphEdge::kInternal, |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2906 | group_entry->index(), |
| 2907 | child_entry); |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 2908 | } |
| 2909 | |
| 2910 | |
| 2911 | void NativeObjectsExplorer::SetWrapperNativeReferences( |
| 2912 | HeapObject* wrapper, v8::RetainedObjectInfo* info) { |
| 2913 | HeapEntry* wrapper_entry = filler_->FindEntry(wrapper); |
| 2914 | ASSERT(wrapper_entry != NULL); |
jkummerow@chromium.org | ab7dad4 | 2012-02-07 12:07:34 +0000 | [diff] [blame] | 2915 | HeapEntry* info_entry = |
| 2916 | filler_->FindOrAddEntry(info, native_entries_allocator_); |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 2917 | ASSERT(info_entry != NULL); |
| 2918 | filler_->SetNamedReference(HeapGraphEdge::kInternal, |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2919 | wrapper_entry->index(), |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 2920 | "native", |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2921 | info_entry); |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 2922 | filler_->SetIndexedAutoIndexReference(HeapGraphEdge::kElement, |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2923 | info_entry->index(), |
| 2924 | wrapper_entry); |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 2925 | } |
| 2926 | |
| 2927 | |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 2928 | void NativeObjectsExplorer::SetRootNativeRootsReference() { |
| 2929 | for (HashMap::Entry* entry = native_groups_.Start(); |
| 2930 | entry; |
| 2931 | entry = native_groups_.Next(entry)) { |
| 2932 | NativeGroupRetainedObjectInfo* group_info = |
| 2933 | static_cast<NativeGroupRetainedObjectInfo*>(entry->value); |
jkummerow@chromium.org | ab7dad4 | 2012-02-07 12:07:34 +0000 | [diff] [blame] | 2934 | HeapEntry* group_entry = |
| 2935 | filler_->FindOrAddEntry(group_info, native_entries_allocator_); |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 2936 | ASSERT(group_entry != NULL); |
| 2937 | filler_->SetIndexedAutoIndexReference( |
| 2938 | HeapGraphEdge::kElement, |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2939 | snapshot_->root()->index(), |
| 2940 | group_entry); |
danno@chromium.org | fa458e4 | 2012-02-01 10:48:36 +0000 | [diff] [blame] | 2941 | } |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 2942 | } |
| 2943 | |
| 2944 | |
| 2945 | void NativeObjectsExplorer::VisitSubtreeWrapper(Object** p, uint16_t class_id) { |
| 2946 | if (in_groups_.Contains(*p)) return; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2947 | Isolate* isolate = Isolate::Current(); |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 2948 | v8::RetainedObjectInfo* info = |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 2949 | isolate->heap_profiler()->ExecuteWrapperClassCallback(class_id, p); |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 2950 | if (info == NULL) return; |
| 2951 | GetListMaybeDisposeInfo(info)->Add(HeapObject::cast(*p)); |
| 2952 | } |
| 2953 | |
| 2954 | |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2955 | class SnapshotFiller : public SnapshotFillerInterface { |
| 2956 | public: |
| 2957 | explicit SnapshotFiller(HeapSnapshot* snapshot, HeapEntriesMap* entries) |
| 2958 | : snapshot_(snapshot), |
| 2959 | collection_(snapshot->collection()), |
| 2960 | entries_(entries) { } |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 2961 | HeapEntry* AddEntry(HeapThing ptr, HeapEntriesAllocator* allocator) { |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2962 | HeapEntry* entry = allocator->AllocateEntry(ptr); |
| 2963 | entries_->Pair(ptr, entry->index()); |
| 2964 | return entry; |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2965 | } |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 2966 | HeapEntry* FindEntry(HeapThing ptr) { |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2967 | int index = entries_->Map(ptr); |
| 2968 | return index != HeapEntry::kNoEntry ? &snapshot_->entries()[index] : NULL; |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 2969 | } |
| 2970 | HeapEntry* FindOrAddEntry(HeapThing ptr, HeapEntriesAllocator* allocator) { |
| 2971 | HeapEntry* entry = FindEntry(ptr); |
| 2972 | return entry != NULL ? entry : AddEntry(ptr, allocator); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2973 | } |
| 2974 | void SetIndexedReference(HeapGraphEdge::Type type, |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2975 | int parent, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2976 | int index, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2977 | HeapEntry* child_entry) { |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2978 | HeapEntry* parent_entry = &snapshot_->entries()[parent]; |
| 2979 | parent_entry->SetIndexedReference(type, index, child_entry); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2980 | } |
| 2981 | void SetIndexedAutoIndexReference(HeapGraphEdge::Type type, |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2982 | int parent, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2983 | HeapEntry* child_entry) { |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2984 | HeapEntry* parent_entry = &snapshot_->entries()[parent]; |
| 2985 | int index = parent_entry->children_count() + 1; |
| 2986 | parent_entry->SetIndexedReference(type, index, child_entry); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2987 | } |
| 2988 | void SetNamedReference(HeapGraphEdge::Type type, |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2989 | int parent, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2990 | const char* reference_name, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2991 | HeapEntry* child_entry) { |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2992 | HeapEntry* parent_entry = &snapshot_->entries()[parent]; |
| 2993 | parent_entry->SetNamedReference(type, reference_name, child_entry); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2994 | } |
| 2995 | void SetNamedAutoIndexReference(HeapGraphEdge::Type type, |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2996 | int parent, |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 2997 | HeapEntry* child_entry) { |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 2998 | HeapEntry* parent_entry = &snapshot_->entries()[parent]; |
| 2999 | int index = parent_entry->children_count() + 1; |
| 3000 | parent_entry->SetNamedReference( |
| 3001 | type, |
| 3002 | collection_->names()->GetName(index), |
| 3003 | child_entry); |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 3004 | } |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 3005 | |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 3006 | private: |
| 3007 | HeapSnapshot* snapshot_; |
| 3008 | HeapSnapshotsCollection* collection_; |
| 3009 | HeapEntriesMap* entries_; |
| 3010 | }; |
| 3011 | |
| 3012 | |
ulan@chromium.org | 9a21ec4 | 2012-03-06 08:42:24 +0000 | [diff] [blame] | 3013 | HeapSnapshotGenerator::HeapSnapshotGenerator(HeapSnapshot* snapshot, |
| 3014 | v8::ActivityControl* control) |
| 3015 | : snapshot_(snapshot), |
| 3016 | control_(control), |
| 3017 | v8_heap_explorer_(snapshot_, this), |
| 3018 | dom_explorer_(snapshot_, this) { |
| 3019 | } |
| 3020 | |
| 3021 | |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 3022 | bool HeapSnapshotGenerator::GenerateSnapshot() { |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 3023 | v8_heap_explorer_.TagGlobalObjects(); |
| 3024 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3025 | // TODO(1562) Profiler assumes that any object that is in the heap after |
| 3026 | // full GC is reachable from the root when computing dominators. |
| 3027 | // This is not true for weakly reachable objects. |
| 3028 | // As a temporary solution we call GC twice. |
rossberg@chromium.org | 994edf6 | 2012-02-06 10:12:55 +0000 | [diff] [blame] | 3029 | Isolate::Current()->heap()->CollectAllGarbage( |
| 3030 | Heap::kMakeHeapIterableMask, |
| 3031 | "HeapSnapshotGenerator::GenerateSnapshot"); |
| 3032 | Isolate::Current()->heap()->CollectAllGarbage( |
| 3033 | Heap::kMakeHeapIterableMask, |
| 3034 | "HeapSnapshotGenerator::GenerateSnapshot"); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3035 | |
| 3036 | #ifdef DEBUG |
| 3037 | Heap* debug_heap = Isolate::Current()->heap(); |
| 3038 | ASSERT(!debug_heap->old_data_space()->was_swept_conservatively()); |
| 3039 | ASSERT(!debug_heap->old_pointer_space()->was_swept_conservatively()); |
| 3040 | ASSERT(!debug_heap->code_space()->was_swept_conservatively()); |
| 3041 | ASSERT(!debug_heap->cell_space()->was_swept_conservatively()); |
| 3042 | ASSERT(!debug_heap->map_space()->was_swept_conservatively()); |
| 3043 | #endif |
| 3044 | |
| 3045 | // The following code uses heap iterators, so we want the heap to be |
| 3046 | // stable. It should follow TagGlobalObjects as that can allocate. |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 3047 | AssertNoAllocation no_alloc; |
| 3048 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3049 | #ifdef DEBUG |
| 3050 | debug_heap->Verify(); |
| 3051 | #endif |
| 3052 | |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 3053 | SetProgressTotal(1); // 1 pass. |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 3054 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3055 | #ifdef DEBUG |
| 3056 | debug_heap->Verify(); |
| 3057 | #endif |
| 3058 | |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 3059 | if (!FillReferences()) return false; |
| 3060 | |
mstarzinger@chromium.org | 15613d0 | 2012-05-23 12:04:37 +0000 | [diff] [blame^] | 3061 | snapshot_->FillChildren(); |
jkummerow@chromium.org | 1456e70 | 2012-03-30 08:38:13 +0000 | [diff] [blame] | 3062 | snapshot_->RememberLastJSObjectId(); |
| 3063 | |
ager@chromium.org | 9ee27ae | 2011-03-02 13:43:26 +0000 | [diff] [blame] | 3064 | progress_counter_ = progress_total_; |
| 3065 | if (!ProgressReport(true)) return false; |
| 3066 | return true; |
| 3067 | } |
| 3068 | |
| 3069 | |
| 3070 | void HeapSnapshotGenerator::ProgressStep() { |
| 3071 | ++progress_counter_; |
| 3072 | } |
| 3073 | |
| 3074 | |
| 3075 | bool HeapSnapshotGenerator::ProgressReport(bool force) { |
| 3076 | const int kProgressReportGranularity = 10000; |
| 3077 | if (control_ != NULL |
| 3078 | && (force || progress_counter_ % kProgressReportGranularity == 0)) { |
| 3079 | return |
| 3080 | control_->ReportProgressValue(progress_counter_, progress_total_) == |
| 3081 | v8::ActivityControl::kContinue; |
| 3082 | } |
| 3083 | return true; |
| 3084 | } |
| 3085 | |
| 3086 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 3087 | void HeapSnapshotGenerator::SetProgressTotal(int iterations_count) { |
| 3088 | if (control_ == NULL) return; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3089 | HeapIterator iterator(HeapIterator::kFilterUnreachable); |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 3090 | progress_total_ = iterations_count * ( |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3091 | v8_heap_explorer_.EstimateObjectsCount(&iterator) + |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 3092 | dom_explorer_.EstimateObjectsCount()); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 3093 | progress_counter_ = 0; |
| 3094 | } |
| 3095 | |
| 3096 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 3097 | bool HeapSnapshotGenerator::FillReferences() { |
| 3098 | SnapshotFiller filler(snapshot_, &entries_); |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 3099 | v8_heap_explorer_.AddRootEntries(&filler); |
ulan@chromium.org | 9a21ec4 | 2012-03-06 08:42:24 +0000 | [diff] [blame] | 3100 | return v8_heap_explorer_.IterateAndExtractReferences(&filler) |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 3101 | && dom_explorer_.IterateAndExtractReferences(&filler); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 3102 | } |
| 3103 | |
| 3104 | |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 3105 | template<int bytes> struct MaxDecimalDigitsIn; |
| 3106 | template<> struct MaxDecimalDigitsIn<4> { |
| 3107 | static const int kSigned = 11; |
| 3108 | static const int kUnsigned = 10; |
| 3109 | }; |
| 3110 | template<> struct MaxDecimalDigitsIn<8> { |
| 3111 | static const int kSigned = 20; |
| 3112 | static const int kUnsigned = 20; |
| 3113 | }; |
| 3114 | |
| 3115 | |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 3116 | class OutputStreamWriter { |
| 3117 | public: |
| 3118 | explicit OutputStreamWriter(v8::OutputStream* stream) |
| 3119 | : stream_(stream), |
| 3120 | chunk_size_(stream->GetChunkSize()), |
| 3121 | chunk_(chunk_size_), |
| 3122 | chunk_pos_(0), |
| 3123 | aborted_(false) { |
| 3124 | ASSERT(chunk_size_ > 0); |
| 3125 | } |
| 3126 | bool aborted() { return aborted_; } |
| 3127 | void AddCharacter(char c) { |
| 3128 | ASSERT(c != '\0'); |
| 3129 | ASSERT(chunk_pos_ < chunk_size_); |
| 3130 | chunk_[chunk_pos_++] = c; |
| 3131 | MaybeWriteChunk(); |
| 3132 | } |
| 3133 | void AddString(const char* s) { |
| 3134 | AddSubstring(s, StrLength(s)); |
| 3135 | } |
| 3136 | void AddSubstring(const char* s, int n) { |
| 3137 | if (n <= 0) return; |
| 3138 | ASSERT(static_cast<size_t>(n) <= strlen(s)); |
| 3139 | const char* s_end = s + n; |
| 3140 | while (s < s_end) { |
| 3141 | int s_chunk_size = Min( |
| 3142 | chunk_size_ - chunk_pos_, static_cast<int>(s_end - s)); |
| 3143 | ASSERT(s_chunk_size > 0); |
| 3144 | memcpy(chunk_.start() + chunk_pos_, s, s_chunk_size); |
| 3145 | s += s_chunk_size; |
| 3146 | chunk_pos_ += s_chunk_size; |
| 3147 | MaybeWriteChunk(); |
| 3148 | } |
| 3149 | } |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 3150 | void AddNumber(unsigned n) { AddNumberImpl<unsigned>(n, "%u"); } |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 3151 | void Finalize() { |
| 3152 | if (aborted_) return; |
| 3153 | ASSERT(chunk_pos_ < chunk_size_); |
| 3154 | if (chunk_pos_ != 0) { |
| 3155 | WriteChunk(); |
| 3156 | } |
| 3157 | stream_->EndOfStream(); |
| 3158 | } |
| 3159 | |
| 3160 | private: |
| 3161 | template<typename T> |
| 3162 | void AddNumberImpl(T n, const char* format) { |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 3163 | // Buffer for the longest value plus trailing \0 |
| 3164 | static const int kMaxNumberSize = |
| 3165 | MaxDecimalDigitsIn<sizeof(T)>::kUnsigned + 1; |
| 3166 | if (chunk_size_ - chunk_pos_ >= kMaxNumberSize) { |
| 3167 | int result = OS::SNPrintF( |
| 3168 | chunk_.SubVector(chunk_pos_, chunk_size_), format, n); |
| 3169 | ASSERT(result != -1); |
| 3170 | chunk_pos_ += result; |
| 3171 | MaybeWriteChunk(); |
| 3172 | } else { |
| 3173 | EmbeddedVector<char, kMaxNumberSize> buffer; |
| 3174 | int result = OS::SNPrintF(buffer, format, n); |
| 3175 | USE(result); |
| 3176 | ASSERT(result != -1); |
| 3177 | AddString(buffer.start()); |
| 3178 | } |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 3179 | } |
| 3180 | void MaybeWriteChunk() { |
| 3181 | ASSERT(chunk_pos_ <= chunk_size_); |
| 3182 | if (chunk_pos_ == chunk_size_) { |
| 3183 | WriteChunk(); |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 3184 | } |
| 3185 | } |
| 3186 | void WriteChunk() { |
| 3187 | if (aborted_) return; |
| 3188 | if (stream_->WriteAsciiChunk(chunk_.start(), chunk_pos_) == |
| 3189 | v8::OutputStream::kAbort) aborted_ = true; |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 3190 | chunk_pos_ = 0; |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 3191 | } |
| 3192 | |
| 3193 | v8::OutputStream* stream_; |
| 3194 | int chunk_size_; |
| 3195 | ScopedVector<char> chunk_; |
| 3196 | int chunk_pos_; |
| 3197 | bool aborted_; |
| 3198 | }; |
| 3199 | |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 3200 | |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 3201 | // type, name|index, to_node. |
| 3202 | const int HeapSnapshotJSONSerializer::kEdgeFieldsCount = 3; |
mstarzinger@chromium.org | 15613d0 | 2012-05-23 12:04:37 +0000 | [diff] [blame^] | 3203 | // type, name, id, self_size, children_index. |
| 3204 | const int HeapSnapshotJSONSerializer::kNodeFieldsCount = 5; |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 3205 | |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 3206 | void HeapSnapshotJSONSerializer::Serialize(v8::OutputStream* stream) { |
| 3207 | ASSERT(writer_ == NULL); |
| 3208 | writer_ = new OutputStreamWriter(stream); |
| 3209 | |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 3210 | HeapSnapshot* original_snapshot = NULL; |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 3211 | if (snapshot_->RawSnapshotSize() >= |
ulan@chromium.org | 65a89c2 | 2012-02-14 11:46:07 +0000 | [diff] [blame] | 3212 | SnapshotSizeConstants<kPointerSize>::kMaxSerializableSnapshotRawSize) { |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 3213 | // The snapshot is too big. Serialize a fake snapshot. |
| 3214 | original_snapshot = snapshot_; |
| 3215 | snapshot_ = CreateFakeSnapshot(); |
| 3216 | } |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 3217 | |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 3218 | SerializeImpl(); |
| 3219 | |
| 3220 | delete writer_; |
| 3221 | writer_ = NULL; |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 3222 | |
| 3223 | if (original_snapshot != NULL) { |
| 3224 | delete snapshot_; |
| 3225 | snapshot_ = original_snapshot; |
| 3226 | } |
| 3227 | } |
| 3228 | |
| 3229 | |
| 3230 | HeapSnapshot* HeapSnapshotJSONSerializer::CreateFakeSnapshot() { |
| 3231 | HeapSnapshot* result = new HeapSnapshot(snapshot_->collection(), |
| 3232 | HeapSnapshot::kFull, |
| 3233 | snapshot_->title(), |
| 3234 | snapshot_->uid()); |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 3235 | result->AddRootEntry(); |
ulan@chromium.org | 65a89c2 | 2012-02-14 11:46:07 +0000 | [diff] [blame] | 3236 | const char* text = snapshot_->collection()->names()->GetFormatted( |
| 3237 | "The snapshot is too big. " |
svenpanne@chromium.org | 4efbdb1 | 2012-03-12 08:18:42 +0000 | [diff] [blame] | 3238 | "Maximum snapshot size is %" V8_PTR_PREFIX "u MB. " |
| 3239 | "Actual snapshot size is %" V8_PTR_PREFIX "u MB.", |
ulan@chromium.org | 65a89c2 | 2012-02-14 11:46:07 +0000 | [diff] [blame] | 3240 | SnapshotSizeConstants<kPointerSize>::kMaxSerializableSnapshotRawSize / MB, |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 3241 | (snapshot_->RawSnapshotSize() + MB - 1) / MB); |
| 3242 | HeapEntry* message = result->AddEntry(HeapEntry::kString, text, 0, 4); |
| 3243 | result->root()->SetIndexedReference(HeapGraphEdge::kElement, 1, message); |
mstarzinger@chromium.org | 15613d0 | 2012-05-23 12:04:37 +0000 | [diff] [blame^] | 3244 | result->FillChildren(); |
fschneider@chromium.org | 1805e21 | 2011-09-05 10:49:12 +0000 | [diff] [blame] | 3245 | return result; |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 3246 | } |
| 3247 | |
| 3248 | |
| 3249 | void HeapSnapshotJSONSerializer::SerializeImpl() { |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 3250 | List<HeapEntry>& nodes = snapshot_->entries(); |
| 3251 | ASSERT(0 == snapshot_->root()->index()); |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 3252 | writer_->AddCharacter('{'); |
| 3253 | writer_->AddString("\"snapshot\":{"); |
| 3254 | SerializeSnapshot(); |
| 3255 | if (writer_->aborted()) return; |
| 3256 | writer_->AddString("},\n"); |
| 3257 | writer_->AddString("\"nodes\":["); |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 3258 | SerializeNodes(nodes); |
| 3259 | if (writer_->aborted()) return; |
| 3260 | writer_->AddString("],\n"); |
| 3261 | writer_->AddString("\"edges\":["); |
| 3262 | SerializeEdges(nodes); |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 3263 | if (writer_->aborted()) return; |
| 3264 | writer_->AddString("],\n"); |
| 3265 | writer_->AddString("\"strings\":["); |
| 3266 | SerializeStrings(); |
| 3267 | if (writer_->aborted()) return; |
| 3268 | writer_->AddCharacter(']'); |
| 3269 | writer_->AddCharacter('}'); |
| 3270 | writer_->Finalize(); |
| 3271 | } |
| 3272 | |
| 3273 | |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 3274 | int HeapSnapshotJSONSerializer::GetStringId(const char* s) { |
| 3275 | HashMap::Entry* cache_entry = strings_.Lookup( |
| 3276 | const_cast<char*>(s), ObjectHash(s), true); |
| 3277 | if (cache_entry->value == NULL) { |
| 3278 | cache_entry->value = reinterpret_cast<void*>(next_string_id_++); |
| 3279 | } |
| 3280 | return static_cast<int>(reinterpret_cast<intptr_t>(cache_entry->value)); |
| 3281 | } |
| 3282 | |
| 3283 | |
yangguo@chromium.org | cb9affa | 2012-05-15 12:16:38 +0000 | [diff] [blame] | 3284 | static int utoa(unsigned value, const Vector<char>& buffer, int buffer_pos) { |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 3285 | int number_of_digits = 0; |
yangguo@chromium.org | cb9affa | 2012-05-15 12:16:38 +0000 | [diff] [blame] | 3286 | unsigned t = value; |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 3287 | do { |
| 3288 | ++number_of_digits; |
| 3289 | } while (t /= 10); |
| 3290 | |
| 3291 | buffer_pos += number_of_digits; |
| 3292 | int result = buffer_pos; |
| 3293 | do { |
| 3294 | int last_digit = value % 10; |
| 3295 | buffer[--buffer_pos] = '0' + last_digit; |
| 3296 | value /= 10; |
| 3297 | } while (value); |
| 3298 | return result; |
| 3299 | } |
| 3300 | |
| 3301 | |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 3302 | void HeapSnapshotJSONSerializer::SerializeEdge(HeapGraphEdge* edge, |
| 3303 | bool first_edge) { |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 3304 | // The buffer needs space for 3 ints, 3 commas and \0 |
| 3305 | static const int kBufferSize = |
| 3306 | MaxDecimalDigitsIn<sizeof(int)>::kSigned * 3 + 3 + 1; // NOLINT |
| 3307 | EmbeddedVector<char, kBufferSize> buffer; |
| 3308 | int edge_name_or_index = edge->type() == HeapGraphEdge::kElement |
ricow@chromium.org | 64e3a4b | 2011-12-13 08:07:27 +0000 | [diff] [blame] | 3309 | || edge->type() == HeapGraphEdge::kHidden |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 3310 | || edge->type() == HeapGraphEdge::kWeak |
| 3311 | ? edge->index() : GetStringId(edge->name()); |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 3312 | int buffer_pos = 0; |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 3313 | if (!first_edge) { |
| 3314 | buffer[buffer_pos++] = ','; |
| 3315 | } |
yangguo@chromium.org | cb9affa | 2012-05-15 12:16:38 +0000 | [diff] [blame] | 3316 | buffer_pos = utoa(edge->type(), buffer, buffer_pos); |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 3317 | buffer[buffer_pos++] = ','; |
yangguo@chromium.org | cb9affa | 2012-05-15 12:16:38 +0000 | [diff] [blame] | 3318 | buffer_pos = utoa(edge_name_or_index, buffer, buffer_pos); |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 3319 | buffer[buffer_pos++] = ','; |
yangguo@chromium.org | cb9affa | 2012-05-15 12:16:38 +0000 | [diff] [blame] | 3320 | buffer_pos = utoa(entry_index(edge->to()), buffer, buffer_pos); |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 3321 | buffer[buffer_pos++] = '\0'; |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 3322 | writer_->AddString(buffer.start()); |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 3323 | } |
| 3324 | |
| 3325 | |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 3326 | void HeapSnapshotJSONSerializer::SerializeEdges(const List<HeapEntry>& nodes) { |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 3327 | bool first_edge = true; |
| 3328 | for (int i = 0; i < nodes.length(); ++i) { |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 3329 | HeapEntry* entry = &nodes[i]; |
| 3330 | Vector<HeapGraphEdge*> children = entry->children(); |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 3331 | for (int j = 0; j < children.length(); ++j) { |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 3332 | SerializeEdge(children[j], first_edge); |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 3333 | first_edge = false; |
| 3334 | if (writer_->aborted()) return; |
| 3335 | } |
| 3336 | } |
| 3337 | } |
| 3338 | |
| 3339 | |
| 3340 | void HeapSnapshotJSONSerializer::SerializeNode(HeapEntry* entry, |
| 3341 | int edges_index) { |
mstarzinger@chromium.org | 15613d0 | 2012-05-23 12:04:37 +0000 | [diff] [blame^] | 3342 | // The buffer needs space for 5 uint32_t, 5 commas, \n and \0 |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 3343 | static const int kBufferSize = |
mstarzinger@chromium.org | 15613d0 | 2012-05-23 12:04:37 +0000 | [diff] [blame^] | 3344 | 5 * MaxDecimalDigitsIn<sizeof(uint32_t)>::kUnsigned // NOLINT |
| 3345 | + 5 + 1 + 1; |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 3346 | EmbeddedVector<char, kBufferSize> buffer; |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 3347 | int buffer_pos = 0; |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 3348 | if (entry_index(entry) != 0) { |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 3349 | buffer[buffer_pos++] = ','; |
| 3350 | } |
yangguo@chromium.org | cb9affa | 2012-05-15 12:16:38 +0000 | [diff] [blame] | 3351 | buffer_pos = utoa(entry->type(), buffer, buffer_pos); |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 3352 | buffer[buffer_pos++] = ','; |
yangguo@chromium.org | cb9affa | 2012-05-15 12:16:38 +0000 | [diff] [blame] | 3353 | buffer_pos = utoa(GetStringId(entry->name()), buffer, buffer_pos); |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 3354 | buffer[buffer_pos++] = ','; |
yangguo@chromium.org | cb9affa | 2012-05-15 12:16:38 +0000 | [diff] [blame] | 3355 | buffer_pos = utoa(entry->id(), buffer, buffer_pos); |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 3356 | buffer[buffer_pos++] = ','; |
yangguo@chromium.org | cb9affa | 2012-05-15 12:16:38 +0000 | [diff] [blame] | 3357 | buffer_pos = utoa(entry->self_size(), buffer, buffer_pos); |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 3358 | buffer[buffer_pos++] = ','; |
yangguo@chromium.org | cb9affa | 2012-05-15 12:16:38 +0000 | [diff] [blame] | 3359 | buffer_pos = utoa(edges_index, buffer, buffer_pos); |
| 3360 | buffer[buffer_pos++] = '\n'; |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 3361 | buffer[buffer_pos++] = '\0'; |
kmillikin@chromium.org | be6bd10 | 2012-02-23 08:45:21 +0000 | [diff] [blame] | 3362 | writer_->AddString(buffer.start()); |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 3363 | } |
| 3364 | |
| 3365 | |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 3366 | void HeapSnapshotJSONSerializer::SerializeNodes(const List<HeapEntry>& nodes) { |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 3367 | int edges_index = 0; |
fschneider@chromium.org | 7d10be5 | 2012-04-10 12:30:14 +0000 | [diff] [blame] | 3368 | for (int i = 0; i < nodes.length(); ++i) { |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 3369 | HeapEntry* entry = &nodes[i]; |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 3370 | SerializeNode(entry, edges_index); |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 3371 | edges_index += entry->children().length() * kEdgeFieldsCount; |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 3372 | if (writer_->aborted()) return; |
| 3373 | } |
| 3374 | } |
| 3375 | |
| 3376 | |
| 3377 | void HeapSnapshotJSONSerializer::SerializeSnapshot() { |
| 3378 | writer_->AddString("\"title\":\""); |
| 3379 | writer_->AddString(snapshot_->title()); |
| 3380 | writer_->AddString("\""); |
| 3381 | writer_->AddString(",\"uid\":"); |
| 3382 | writer_->AddNumber(snapshot_->uid()); |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 3383 | writer_->AddString(",\"meta\":"); |
| 3384 | // The object describing node serialization layout. |
| 3385 | // We use a set of macros to improve readability. |
ulan@chromium.org | 0e3f88b | 2012-05-22 09:16:05 +0000 | [diff] [blame] | 3386 | #define JSON_A(s) "[" s "]" |
| 3387 | #define JSON_O(s) "{" s "}" |
| 3388 | #define JSON_S(s) "\"" s "\"" |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 3389 | writer_->AddString(JSON_O( |
| 3390 | JSON_S("node_fields") ":" JSON_A( |
| 3391 | JSON_S("type") "," |
| 3392 | JSON_S("name") "," |
| 3393 | JSON_S("id") "," |
| 3394 | JSON_S("self_size") "," |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 3395 | JSON_S("edges_index")) "," |
| 3396 | JSON_S("node_types") ":" JSON_A( |
| 3397 | JSON_A( |
| 3398 | JSON_S("hidden") "," |
| 3399 | JSON_S("array") "," |
| 3400 | JSON_S("string") "," |
| 3401 | JSON_S("object") "," |
| 3402 | JSON_S("code") "," |
| 3403 | JSON_S("closure") "," |
| 3404 | JSON_S("regexp") "," |
| 3405 | JSON_S("number") "," |
| 3406 | JSON_S("native") "," |
| 3407 | JSON_S("synthetic")) "," |
| 3408 | JSON_S("string") "," |
| 3409 | JSON_S("number") "," |
| 3410 | JSON_S("number") "," |
| 3411 | JSON_S("number") "," |
| 3412 | JSON_S("number") "," |
| 3413 | JSON_S("number")) "," |
| 3414 | JSON_S("edge_fields") ":" JSON_A( |
| 3415 | JSON_S("type") "," |
| 3416 | JSON_S("name_or_index") "," |
| 3417 | JSON_S("to_node")) "," |
| 3418 | JSON_S("edge_types") ":" JSON_A( |
| 3419 | JSON_A( |
| 3420 | JSON_S("context") "," |
| 3421 | JSON_S("element") "," |
| 3422 | JSON_S("property") "," |
| 3423 | JSON_S("internal") "," |
| 3424 | JSON_S("hidden") "," |
| 3425 | JSON_S("shortcut") "," |
| 3426 | JSON_S("weak")) "," |
| 3427 | JSON_S("string_or_number") "," |
| 3428 | JSON_S("node")))); |
| 3429 | #undef JSON_S |
| 3430 | #undef JSON_O |
| 3431 | #undef JSON_A |
| 3432 | writer_->AddString(",\"node_count\":"); |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 3433 | writer_->AddNumber(snapshot_->entries().length()); |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 3434 | writer_->AddString(",\"edge_count\":"); |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 3435 | writer_->AddNumber(snapshot_->edges().length()); |
erik.corry@gmail.com | d88afa2 | 2010-09-15 12:33:05 +0000 | [diff] [blame] | 3436 | } |
| 3437 | |
| 3438 | |
| 3439 | static void WriteUChar(OutputStreamWriter* w, unibrow::uchar u) { |
| 3440 | static const char hex_chars[] = "0123456789ABCDEF"; |
| 3441 | w->AddString("\\u"); |
| 3442 | w->AddCharacter(hex_chars[(u >> 12) & 0xf]); |
| 3443 | w->AddCharacter(hex_chars[(u >> 8) & 0xf]); |
| 3444 | w->AddCharacter(hex_chars[(u >> 4) & 0xf]); |
| 3445 | w->AddCharacter(hex_chars[u & 0xf]); |
| 3446 | } |
| 3447 | |
| 3448 | void HeapSnapshotJSONSerializer::SerializeString(const unsigned char* s) { |
| 3449 | writer_->AddCharacter('\n'); |
| 3450 | writer_->AddCharacter('\"'); |
| 3451 | for ( ; *s != '\0'; ++s) { |
| 3452 | switch (*s) { |
| 3453 | case '\b': |
| 3454 | writer_->AddString("\\b"); |
| 3455 | continue; |
| 3456 | case '\f': |
| 3457 | writer_->AddString("\\f"); |
| 3458 | continue; |
| 3459 | case '\n': |
| 3460 | writer_->AddString("\\n"); |
| 3461 | continue; |
| 3462 | case '\r': |
| 3463 | writer_->AddString("\\r"); |
| 3464 | continue; |
| 3465 | case '\t': |
| 3466 | writer_->AddString("\\t"); |
| 3467 | continue; |
| 3468 | case '\"': |
| 3469 | case '\\': |
| 3470 | writer_->AddCharacter('\\'); |
| 3471 | writer_->AddCharacter(*s); |
| 3472 | continue; |
| 3473 | default: |
| 3474 | if (*s > 31 && *s < 128) { |
| 3475 | writer_->AddCharacter(*s); |
| 3476 | } else if (*s <= 31) { |
| 3477 | // Special character with no dedicated literal. |
| 3478 | WriteUChar(writer_, *s); |
| 3479 | } else { |
| 3480 | // Convert UTF-8 into \u UTF-16 literal. |
| 3481 | unsigned length = 1, cursor = 0; |
| 3482 | for ( ; length <= 4 && *(s + length) != '\0'; ++length) { } |
| 3483 | unibrow::uchar c = unibrow::Utf8::CalculateValue(s, length, &cursor); |
| 3484 | if (c != unibrow::Utf8::kBadChar) { |
| 3485 | WriteUChar(writer_, c); |
| 3486 | ASSERT(cursor != 0); |
| 3487 | s += cursor - 1; |
| 3488 | } else { |
| 3489 | writer_->AddCharacter('?'); |
| 3490 | } |
| 3491 | } |
| 3492 | } |
| 3493 | } |
| 3494 | writer_->AddCharacter('\"'); |
| 3495 | } |
| 3496 | |
| 3497 | |
| 3498 | void HeapSnapshotJSONSerializer::SerializeStrings() { |
| 3499 | List<HashMap::Entry*> sorted_strings; |
| 3500 | SortHashMap(&strings_, &sorted_strings); |
| 3501 | writer_->AddString("\"<dummy>\""); |
| 3502 | for (int i = 0; i < sorted_strings.length(); ++i) { |
| 3503 | writer_->AddCharacter(','); |
| 3504 | SerializeString( |
| 3505 | reinterpret_cast<const unsigned char*>(sorted_strings[i]->key)); |
| 3506 | if (writer_->aborted()) return; |
| 3507 | } |
| 3508 | } |
| 3509 | |
| 3510 | |
| 3511 | template<typename T> |
| 3512 | inline static int SortUsingEntryValue(const T* x, const T* y) { |
| 3513 | uintptr_t x_uint = reinterpret_cast<uintptr_t>((*x)->value); |
| 3514 | uintptr_t y_uint = reinterpret_cast<uintptr_t>((*y)->value); |
| 3515 | if (x_uint > y_uint) { |
| 3516 | return 1; |
| 3517 | } else if (x_uint == y_uint) { |
| 3518 | return 0; |
| 3519 | } else { |
| 3520 | return -1; |
| 3521 | } |
| 3522 | } |
| 3523 | |
| 3524 | |
| 3525 | void HeapSnapshotJSONSerializer::SortHashMap( |
| 3526 | HashMap* map, List<HashMap::Entry*>* sorted_entries) { |
| 3527 | for (HashMap::Entry* p = map->Start(); p != NULL; p = map->Next(p)) |
| 3528 | sorted_entries->Add(p); |
| 3529 | sorted_entries->Sort(SortUsingEntryValue); |
| 3530 | } |
| 3531 | |
fschneider@chromium.org | 086aac6 | 2010-03-17 13:18:24 +0000 | [diff] [blame] | 3532 | } } // namespace v8::internal |