blob: 07426f293992dab657bcdd02101b1fe265142d73 [file] [log] [blame]
fschneider@chromium.orgfb144a02011-05-04 12:43:48 +00001// Copyright 2011 the V8 project authors. All rights reserved.
fschneider@chromium.org086aac62010-03-17 13:18:24 +00002// 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.orgfb144a02011-05-04 12:43:48 +000029
30#include "profile-generator-inl.h"
31
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000032#include "global-handles.h"
whesse@chromium.orgb08986c2011-03-14 16:13:42 +000033#include "heap-profiler.h"
ager@chromium.org2cc82ae2010-06-14 07:35:38 +000034#include "scopeinfo.h"
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +000035#include "unicode.h"
ager@chromium.org2cc82ae2010-06-14 07:35:38 +000036#include "zone-inl.h"
fschneider@chromium.org086aac62010-03-17 13:18:24 +000037
fschneider@chromium.org086aac62010-03-17 13:18:24 +000038namespace v8 {
39namespace internal {
40
41
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000042TokenEnumerator::TokenEnumerator()
43 : token_locations_(4),
44 token_removed_(4) {
45}
46
47
48TokenEnumerator::~TokenEnumerator() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000049 Isolate* isolate = Isolate::Current();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000050 for (int i = 0; i < token_locations_.length(); ++i) {
51 if (!token_removed_[i]) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000052 isolate->global_handles()->ClearWeakness(token_locations_[i]);
53 isolate->global_handles()->Destroy(token_locations_[i]);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000054 }
55 }
56}
57
58
59int TokenEnumerator::GetTokenId(Object* token) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000060 Isolate* isolate = Isolate::Current();
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +000061 if (token == NULL) return TokenEnumerator::kNoSecurityToken;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000062 for (int i = 0; i < token_locations_.length(); ++i) {
63 if (*token_locations_[i] == token && !token_removed_[i]) return i;
64 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000065 Handle<Object> handle = isolate->global_handles()->Create(token);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000066 // handle.location() points to a memory cell holding a pointer
67 // to a token object in the V8's heap.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000068 isolate->global_handles()->MakeWeak(handle.location(), this,
69 TokenRemovedCallback);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000070 token_locations_.Add(handle.location());
71 token_removed_.Add(false);
72 return token_locations_.length() - 1;
73}
74
75
76void TokenEnumerator::TokenRemovedCallback(v8::Persistent<v8::Value> handle,
77 void* parameter) {
78 reinterpret_cast<TokenEnumerator*>(parameter)->TokenRemoved(
79 Utils::OpenHandle(*handle).location());
fschneider@chromium.orged78ffd2010-07-21 11:05:19 +000080 handle.Dispose();
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +000081}
82
83
84void TokenEnumerator::TokenRemoved(Object** token_location) {
85 for (int i = 0; i < token_locations_.length(); ++i) {
86 if (token_locations_[i] == token_location && !token_removed_[i]) {
87 token_removed_[i] = true;
88 return;
89 }
90 }
91}
92
93
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +000094StringsStorage::StringsStorage()
95 : names_(StringsMatch) {
96}
97
98
99StringsStorage::~StringsStorage() {
100 for (HashMap::Entry* p = names_.Start();
101 p != NULL;
102 p = names_.Next(p)) {
103 DeleteArray(reinterpret_cast<const char*>(p->value));
104 }
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000105}
106
107
108const char* StringsStorage::GetCopy(const char* src) {
109 int len = static_cast<int>(strlen(src));
110 Vector<char> dst = Vector<char>::New(len + 1);
111 OS::StrNCpy(dst, src, len);
112 dst[len] = '\0';
113 uint32_t hash = HashSequentialString(dst.start(), len);
114 return AddOrDisposeString(dst.start(), hash);
115}
116
117
118const char* StringsStorage::GetFormatted(const char* format, ...) {
119 va_list args;
120 va_start(args, format);
121 const char* result = GetVFormatted(format, args);
122 va_end(args);
123 return result;
124}
125
126
127const char* StringsStorage::AddOrDisposeString(char* str, uint32_t hash) {
128 HashMap::Entry* cache_entry = names_.Lookup(str, hash, true);
129 if (cache_entry->value == NULL) {
130 // New entry added.
131 cache_entry->value = str;
132 } else {
133 DeleteArray(str);
134 }
135 return reinterpret_cast<const char*>(cache_entry->value);
136}
137
138
139const char* StringsStorage::GetVFormatted(const char* format, va_list args) {
140 Vector<char> str = Vector<char>::New(1024);
141 int len = OS::VSNPrintF(str, format, args);
142 if (len == -1) {
143 DeleteArray(str.start());
144 return format;
145 }
146 uint32_t hash = HashSequentialString(str.start(), len);
147 return AddOrDisposeString(str.start(), hash);
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000148}
149
150
151const char* StringsStorage::GetName(String* name) {
152 if (name->IsString()) {
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000153 return AddOrDisposeString(
154 name->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL).Detach(),
155 name->Hash());
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000156 }
157 return "";
158}
159
160
vegorov@chromium.org42841962010-10-18 11:18:59 +0000161const char* StringsStorage::GetName(int index) {
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000162 return GetFormatted("%d", index);
vegorov@chromium.org42841962010-10-18 11:18:59 +0000163}
164
165
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000166const char* const CodeEntry::kEmptyNamePrefix = "";
lrn@chromium.org25156de2010-04-06 13:10:27 +0000167
168
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000169void CodeEntry::CopyData(const CodeEntry& source) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000170 tag_ = source.tag_;
171 name_prefix_ = source.name_prefix_;
172 name_ = source.name_;
173 resource_name_ = source.resource_name_;
174 line_number_ = source.line_number_;
175}
176
177
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000178uint32_t CodeEntry::GetCallUid() const {
179 uint32_t hash = ComputeIntegerHash(tag_);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000180 if (shared_id_ != 0) {
181 hash ^= ComputeIntegerHash(
182 static_cast<uint32_t>(shared_id_));
183 } else {
184 hash ^= ComputeIntegerHash(
185 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(name_prefix_)));
186 hash ^= ComputeIntegerHash(
187 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(name_)));
188 hash ^= ComputeIntegerHash(
189 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(resource_name_)));
190 hash ^= ComputeIntegerHash(line_number_);
191 }
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000192 return hash;
193}
194
195
196bool CodeEntry::IsSameAs(CodeEntry* entry) const {
197 return this == entry
198 || (tag_ == entry->tag_
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000199 && shared_id_ == entry->shared_id_
200 && (shared_id_ != 0
201 || (name_prefix_ == entry->name_prefix_
202 && name_ == entry->name_
203 && resource_name_ == entry->resource_name_
204 && line_number_ == entry->line_number_)));
fschneider@chromium.orgc20610a2010-09-22 09:44:58 +0000205}
206
207
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000208ProfileNode* ProfileNode::FindChild(CodeEntry* entry) {
209 HashMap::Entry* map_entry =
210 children_.Lookup(entry, CodeEntryHash(entry), false);
211 return map_entry != NULL ?
212 reinterpret_cast<ProfileNode*>(map_entry->value) : NULL;
213}
214
215
216ProfileNode* ProfileNode::FindOrAddChild(CodeEntry* entry) {
217 HashMap::Entry* map_entry =
218 children_.Lookup(entry, CodeEntryHash(entry), true);
219 if (map_entry->value == NULL) {
220 // New node added.
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000221 ProfileNode* new_node = new ProfileNode(tree_, entry);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000222 map_entry->value = new_node;
223 children_list_.Add(new_node);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000224 }
225 return reinterpret_cast<ProfileNode*>(map_entry->value);
226}
227
228
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000229double ProfileNode::GetSelfMillis() const {
230 return tree_->TicksToMillis(self_ticks_);
231}
232
233
234double ProfileNode::GetTotalMillis() const {
235 return tree_->TicksToMillis(total_ticks_);
236}
237
238
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000239void ProfileNode::Print(int indent) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000240 OS::Print("%5u %5u %*c %s%s [%d]",
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000241 total_ticks_, self_ticks_,
242 indent, ' ',
ager@chromium.org357bf652010-04-12 11:30:10 +0000243 entry_->name_prefix(),
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000244 entry_->name(),
245 entry_->security_token_id());
ager@chromium.org357bf652010-04-12 11:30:10 +0000246 if (entry_->resource_name()[0] != '\0')
247 OS::Print(" %s:%d", entry_->resource_name(), entry_->line_number());
248 OS::Print("\n");
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000249 for (HashMap::Entry* p = children_.Start();
250 p != NULL;
251 p = children_.Next(p)) {
252 reinterpret_cast<ProfileNode*>(p->value)->Print(indent + 2);
253 }
254}
255
256
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000257class DeleteNodesCallback {
258 public:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000259 void BeforeTraversingChild(ProfileNode*, ProfileNode*) { }
260
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000261 void AfterAllChildrenTraversed(ProfileNode* node) {
262 delete node;
263 }
264
265 void AfterChildTraversed(ProfileNode*, ProfileNode*) { }
266};
267
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000268
ager@chromium.org357bf652010-04-12 11:30:10 +0000269ProfileTree::ProfileTree()
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000270 : root_entry_(Logger::FUNCTION_TAG,
271 "",
272 "(root)",
273 "",
274 0,
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000275 TokenEnumerator::kNoSecurityToken),
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000276 root_(new ProfileNode(this, &root_entry_)) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000277}
278
279
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000280ProfileTree::~ProfileTree() {
281 DeleteNodesCallback cb;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000282 TraverseDepthFirst(&cb);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000283}
284
285
286void ProfileTree::AddPathFromEnd(const Vector<CodeEntry*>& path) {
287 ProfileNode* node = root_;
288 for (CodeEntry** entry = path.start() + path.length() - 1;
289 entry != path.start() - 1;
290 --entry) {
291 if (*entry != NULL) {
292 node = node->FindOrAddChild(*entry);
293 }
294 }
295 node->IncrementSelfTicks();
296}
297
298
299void ProfileTree::AddPathFromStart(const Vector<CodeEntry*>& path) {
300 ProfileNode* node = root_;
301 for (CodeEntry** entry = path.start();
302 entry != path.start() + path.length();
303 ++entry) {
304 if (*entry != NULL) {
305 node = node->FindOrAddChild(*entry);
306 }
307 }
308 node->IncrementSelfTicks();
309}
310
311
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000312struct NodesPair {
313 NodesPair(ProfileNode* src, ProfileNode* dst)
314 : src(src), dst(dst) { }
315 ProfileNode* src;
316 ProfileNode* dst;
317};
318
319
320class FilteredCloneCallback {
321 public:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000322 FilteredCloneCallback(ProfileNode* dst_root, int security_token_id)
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000323 : stack_(10),
324 security_token_id_(security_token_id) {
325 stack_.Add(NodesPair(NULL, dst_root));
326 }
327
328 void BeforeTraversingChild(ProfileNode* parent, ProfileNode* child) {
329 if (IsTokenAcceptable(child->entry()->security_token_id(),
330 parent->entry()->security_token_id())) {
331 ProfileNode* clone = stack_.last().dst->FindOrAddChild(child->entry());
332 clone->IncreaseSelfTicks(child->self_ticks());
333 stack_.Add(NodesPair(child, clone));
334 } else {
335 // Attribute ticks to parent node.
336 stack_.last().dst->IncreaseSelfTicks(child->self_ticks());
337 }
338 }
339
340 void AfterAllChildrenTraversed(ProfileNode* parent) { }
341
342 void AfterChildTraversed(ProfileNode*, ProfileNode* child) {
343 if (stack_.last().src == child) {
344 stack_.RemoveLast();
345 }
346 }
347
348 private:
349 bool IsTokenAcceptable(int token, int parent_token) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000350 if (token == TokenEnumerator::kNoSecurityToken
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000351 || token == security_token_id_) return true;
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000352 if (token == TokenEnumerator::kInheritsSecurityToken) {
353 ASSERT(parent_token != TokenEnumerator::kInheritsSecurityToken);
354 return parent_token == TokenEnumerator::kNoSecurityToken
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000355 || parent_token == security_token_id_;
356 }
357 return false;
358 }
359
360 List<NodesPair> stack_;
361 int security_token_id_;
362};
363
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000364void ProfileTree::FilteredClone(ProfileTree* src, int security_token_id) {
365 ms_to_ticks_scale_ = src->ms_to_ticks_scale_;
366 FilteredCloneCallback cb(root_, security_token_id);
367 src->TraverseDepthFirst(&cb);
368 CalculateTotalTicks();
369}
370
371
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000372void ProfileTree::SetTickRatePerMs(double ticks_per_ms) {
373 ms_to_ticks_scale_ = ticks_per_ms > 0 ? 1.0 / ticks_per_ms : 1.0;
374}
375
376
lrn@chromium.org25156de2010-04-06 13:10:27 +0000377class Position {
378 public:
379 explicit Position(ProfileNode* node)
380 : node(node), child_idx_(0) { }
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000381 INLINE(ProfileNode* current_child()) {
lrn@chromium.org25156de2010-04-06 13:10:27 +0000382 return node->children()->at(child_idx_);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000383 }
lrn@chromium.org25156de2010-04-06 13:10:27 +0000384 INLINE(bool has_current_child()) {
385 return child_idx_ < node->children()->length();
386 }
387 INLINE(void next_child()) { ++child_idx_; }
388
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000389 ProfileNode* node;
lrn@chromium.org25156de2010-04-06 13:10:27 +0000390 private:
391 int child_idx_;
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000392};
393
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000394
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000395// Non-recursive implementation of a depth-first post-order tree traversal.
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000396template <typename Callback>
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000397void ProfileTree::TraverseDepthFirst(Callback* callback) {
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000398 List<Position> stack(10);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000399 stack.Add(Position(root_));
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000400 while (stack.length() > 0) {
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000401 Position& current = stack.last();
lrn@chromium.org25156de2010-04-06 13:10:27 +0000402 if (current.has_current_child()) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000403 callback->BeforeTraversingChild(current.node, current.current_child());
lrn@chromium.org25156de2010-04-06 13:10:27 +0000404 stack.Add(Position(current.current_child()));
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000405 } else {
406 callback->AfterAllChildrenTraversed(current.node);
407 if (stack.length() > 1) {
408 Position& parent = stack[stack.length() - 2];
409 callback->AfterChildTraversed(parent.node, current.node);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000410 parent.next_child();
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000411 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000412 // Remove child from the stack.
413 stack.RemoveLast();
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000414 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000415 }
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000416}
417
418
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000419class CalculateTotalTicksCallback {
420 public:
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000421 void BeforeTraversingChild(ProfileNode*, ProfileNode*) { }
422
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000423 void AfterAllChildrenTraversed(ProfileNode* node) {
424 node->IncreaseTotalTicks(node->self_ticks());
425 }
426
427 void AfterChildTraversed(ProfileNode* parent, ProfileNode* child) {
428 parent->IncreaseTotalTicks(child->total_ticks());
429 }
430};
431
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000432
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000433void ProfileTree::CalculateTotalTicks() {
434 CalculateTotalTicksCallback cb;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000435 TraverseDepthFirst(&cb);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000436}
437
438
439void ProfileTree::ShortPrint() {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000440 OS::Print("root: %u %u %.2fms %.2fms\n",
441 root_->total_ticks(), root_->self_ticks(),
442 root_->GetTotalMillis(), root_->GetSelfMillis());
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000443}
444
445
446void CpuProfile::AddPath(const Vector<CodeEntry*>& path) {
447 top_down_.AddPathFromEnd(path);
448 bottom_up_.AddPathFromStart(path);
449}
450
451
452void CpuProfile::CalculateTotalTicks() {
453 top_down_.CalculateTotalTicks();
454 bottom_up_.CalculateTotalTicks();
455}
456
457
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000458void CpuProfile::SetActualSamplingRate(double actual_sampling_rate) {
459 top_down_.SetTickRatePerMs(actual_sampling_rate);
460 bottom_up_.SetTickRatePerMs(actual_sampling_rate);
461}
462
463
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000464CpuProfile* CpuProfile::FilteredClone(int security_token_id) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000465 ASSERT(security_token_id != TokenEnumerator::kNoSecurityToken);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000466 CpuProfile* clone = new CpuProfile(title_, uid_);
467 clone->top_down_.FilteredClone(&top_down_, security_token_id);
468 clone->bottom_up_.FilteredClone(&bottom_up_, security_token_id);
469 return clone;
470}
471
472
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000473void CpuProfile::ShortPrint() {
474 OS::Print("top down ");
475 top_down_.ShortPrint();
476 OS::Print("bottom up ");
477 bottom_up_.ShortPrint();
478}
479
480
481void CpuProfile::Print() {
482 OS::Print("[Top down]:\n");
483 top_down_.Print();
484 OS::Print("[Bottom up]:\n");
485 bottom_up_.Print();
486}
487
488
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000489CodeEntry* const CodeMap::kSharedFunctionCodeEntry = NULL;
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000490const CodeMap::CodeTreeConfig::Key CodeMap::CodeTreeConfig::kNoKey = NULL;
491const CodeMap::CodeTreeConfig::Value CodeMap::CodeTreeConfig::kNoValue =
492 CodeMap::CodeEntryInfo(NULL, 0);
493
494
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000495CodeEntry* CodeMap::FindEntry(Address addr) {
496 CodeTree::Locator locator;
497 if (tree_.FindGreatestLessThan(addr, &locator)) {
498 // locator.key() <= addr. Need to check that addr is within entry.
499 const CodeEntryInfo& entry = locator.value();
500 if (addr < (locator.key() + entry.size))
501 return entry.entry;
502 }
503 return NULL;
504}
505
506
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000507int CodeMap::GetSharedId(Address addr) {
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000508 CodeTree::Locator locator;
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000509 // For shared function entries, 'size' field is used to store their IDs.
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000510 if (tree_.Find(addr, &locator)) {
511 const CodeEntryInfo& entry = locator.value();
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000512 ASSERT(entry.entry == kSharedFunctionCodeEntry);
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000513 return entry.size;
514 } else {
515 tree_.Insert(addr, &locator);
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000516 int id = next_shared_id_++;
517 locator.set_value(CodeEntryInfo(kSharedFunctionCodeEntry, id));
518 return id;
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000519 }
520}
521
522
lrn@chromium.org25156de2010-04-06 13:10:27 +0000523void CodeMap::CodeTreePrinter::Call(
524 const Address& key, const CodeMap::CodeEntryInfo& value) {
525 OS::Print("%p %5d %s\n", key, value.size, value.entry->name());
526}
527
528
529void CodeMap::Print() {
530 CodeTreePrinter printer;
531 tree_.ForEach(&printer);
532}
533
534
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000535CpuProfilesCollection::CpuProfilesCollection()
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000536 : profiles_uids_(UidsMatch),
lrn@chromium.org25156de2010-04-06 13:10:27 +0000537 current_profiles_semaphore_(OS::CreateSemaphore(1)) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000538 // Create list of unabridged profiles.
539 profiles_by_token_.Add(new List<CpuProfile*>());
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000540}
541
542
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000543static void DeleteCodeEntry(CodeEntry** entry_ptr) {
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000544 delete *entry_ptr;
545}
546
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000547static void DeleteCpuProfile(CpuProfile** profile_ptr) {
548 delete *profile_ptr;
549}
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000550
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000551static void DeleteProfilesList(List<CpuProfile*>** list_ptr) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000552 if (*list_ptr != NULL) {
553 (*list_ptr)->Iterate(DeleteCpuProfile);
554 delete *list_ptr;
555 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000556}
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000557
558CpuProfilesCollection::~CpuProfilesCollection() {
lrn@chromium.org25156de2010-04-06 13:10:27 +0000559 delete current_profiles_semaphore_;
560 current_profiles_.Iterate(DeleteCpuProfile);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000561 detached_profiles_.Iterate(DeleteCpuProfile);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000562 profiles_by_token_.Iterate(DeleteProfilesList);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000563 code_entries_.Iterate(DeleteCodeEntry);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000564}
565
566
lrn@chromium.org25156de2010-04-06 13:10:27 +0000567bool CpuProfilesCollection::StartProfiling(const char* title, unsigned uid) {
568 ASSERT(uid > 0);
569 current_profiles_semaphore_->Wait();
ricow@chromium.orgd236f4d2010-09-01 06:52:08 +0000570 if (current_profiles_.length() >= kMaxSimultaneousProfiles) {
571 current_profiles_semaphore_->Signal();
572 return false;
573 }
lrn@chromium.org25156de2010-04-06 13:10:27 +0000574 for (int i = 0; i < current_profiles_.length(); ++i) {
575 if (strcmp(current_profiles_[i]->title(), title) == 0) {
576 // Ignore attempts to start profile with the same title.
577 current_profiles_semaphore_->Signal();
578 return false;
579 }
580 }
581 current_profiles_.Add(new CpuProfile(title, uid));
582 current_profiles_semaphore_->Signal();
583 return true;
584}
585
586
587bool CpuProfilesCollection::StartProfiling(String* title, unsigned uid) {
588 return StartProfiling(GetName(title), uid);
589}
590
591
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000592CpuProfile* CpuProfilesCollection::StopProfiling(int security_token_id,
593 const char* title,
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000594 double actual_sampling_rate) {
whesse@chromium.orgb6e43bb2010-04-14 09:36:28 +0000595 const int title_len = StrLength(title);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000596 CpuProfile* profile = NULL;
597 current_profiles_semaphore_->Wait();
598 for (int i = current_profiles_.length() - 1; i >= 0; --i) {
599 if (title_len == 0 || strcmp(current_profiles_[i]->title(), title) == 0) {
600 profile = current_profiles_.Remove(i);
601 break;
602 }
603 }
604 current_profiles_semaphore_->Signal();
605
606 if (profile != NULL) {
607 profile->CalculateTotalTicks();
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000608 profile->SetActualSamplingRate(actual_sampling_rate);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000609 List<CpuProfile*>* unabridged_list =
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000610 profiles_by_token_[TokenToIndex(TokenEnumerator::kNoSecurityToken)];
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000611 unabridged_list->Add(profile);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000612 HashMap::Entry* entry =
613 profiles_uids_.Lookup(reinterpret_cast<void*>(profile->uid()),
614 static_cast<uint32_t>(profile->uid()),
615 true);
616 ASSERT(entry->value == NULL);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000617 entry->value = reinterpret_cast<void*>(unabridged_list->length() - 1);
618 return GetProfile(security_token_id, profile->uid());
lrn@chromium.org25156de2010-04-06 13:10:27 +0000619 }
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000620 return NULL;
lrn@chromium.org25156de2010-04-06 13:10:27 +0000621}
622
623
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000624CpuProfile* CpuProfilesCollection::GetProfile(int security_token_id,
625 unsigned uid) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000626 int index = GetProfileIndex(uid);
627 if (index < 0) return NULL;
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000628 List<CpuProfile*>* unabridged_list =
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000629 profiles_by_token_[TokenToIndex(TokenEnumerator::kNoSecurityToken)];
630 if (security_token_id == TokenEnumerator::kNoSecurityToken) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000631 return unabridged_list->at(index);
632 }
633 List<CpuProfile*>* list = GetProfilesList(security_token_id);
634 if (list->at(index) == NULL) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000635 (*list)[index] =
636 unabridged_list->at(index)->FilteredClone(security_token_id);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000637 }
638 return list->at(index);
639}
640
641
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000642int CpuProfilesCollection::GetProfileIndex(unsigned uid) {
643 HashMap::Entry* entry = profiles_uids_.Lookup(reinterpret_cast<void*>(uid),
644 static_cast<uint32_t>(uid),
645 false);
646 return entry != NULL ?
647 static_cast<int>(reinterpret_cast<intptr_t>(entry->value)) : -1;
648}
649
650
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000651bool CpuProfilesCollection::IsLastProfile(const char* title) {
652 // Called from VM thread, and only it can mutate the list,
653 // so no locking is needed here.
654 if (current_profiles_.length() != 1) return false;
655 return StrLength(title) == 0
656 || strcmp(current_profiles_[0]->title(), title) == 0;
657}
658
659
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000660void CpuProfilesCollection::RemoveProfile(CpuProfile* profile) {
661 // Called from VM thread for a completed profile.
662 unsigned uid = profile->uid();
663 int index = GetProfileIndex(uid);
664 if (index < 0) {
665 detached_profiles_.RemoveElement(profile);
666 return;
667 }
668 profiles_uids_.Remove(reinterpret_cast<void*>(uid),
669 static_cast<uint32_t>(uid));
670 // Decrement all indexes above the deleted one.
671 for (HashMap::Entry* p = profiles_uids_.Start();
672 p != NULL;
673 p = profiles_uids_.Next(p)) {
674 intptr_t p_index = reinterpret_cast<intptr_t>(p->value);
675 if (p_index > index) {
676 p->value = reinterpret_cast<void*>(p_index - 1);
677 }
678 }
679 for (int i = 0; i < profiles_by_token_.length(); ++i) {
680 List<CpuProfile*>* list = profiles_by_token_[i];
681 if (list != NULL && index < list->length()) {
682 // Move all filtered clones into detached_profiles_,
683 // so we can know that they are still in use.
684 CpuProfile* cloned_profile = list->Remove(index);
685 if (cloned_profile != NULL && cloned_profile != profile) {
686 detached_profiles_.Add(cloned_profile);
687 }
688 }
689 }
690}
691
692
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000693int CpuProfilesCollection::TokenToIndex(int security_token_id) {
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000694 ASSERT(TokenEnumerator::kNoSecurityToken == -1);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000695 return security_token_id + 1; // kNoSecurityToken -> 0, 0 -> 1, ...
696}
697
698
699List<CpuProfile*>* CpuProfilesCollection::GetProfilesList(
700 int security_token_id) {
701 const int index = TokenToIndex(security_token_id);
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000702 const int lists_to_add = index - profiles_by_token_.length() + 1;
703 if (lists_to_add > 0) profiles_by_token_.AddBlock(NULL, lists_to_add);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000704 List<CpuProfile*>* unabridged_list =
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000705 profiles_by_token_[TokenToIndex(TokenEnumerator::kNoSecurityToken)];
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000706 const int current_count = unabridged_list->length();
707 if (profiles_by_token_[index] == NULL) {
708 profiles_by_token_[index] = new List<CpuProfile*>(current_count);
709 }
710 List<CpuProfile*>* list = profiles_by_token_[index];
ricow@chromium.org30ce4112010-05-31 10:38:25 +0000711 const int profiles_to_add = current_count - list->length();
712 if (profiles_to_add > 0) list->AddBlock(NULL, profiles_to_add);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000713 return list;
714}
715
716
717List<CpuProfile*>* CpuProfilesCollection::Profiles(int security_token_id) {
718 List<CpuProfile*>* unabridged_list =
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000719 profiles_by_token_[TokenToIndex(TokenEnumerator::kNoSecurityToken)];
720 if (security_token_id == TokenEnumerator::kNoSecurityToken) {
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000721 return unabridged_list;
722 }
723 List<CpuProfile*>* list = GetProfilesList(security_token_id);
724 const int current_count = unabridged_list->length();
725 for (int i = 0; i < current_count; ++i) {
726 if (list->at(i) == NULL) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +0000727 (*list)[i] = unabridged_list->at(i)->FilteredClone(security_token_id);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000728 }
729 }
730 return list;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000731}
732
733
734CodeEntry* CpuProfilesCollection::NewCodeEntry(Logger::LogEventsAndTags tag,
735 String* name,
736 String* resource_name,
737 int line_number) {
738 CodeEntry* entry = new CodeEntry(tag,
lrn@chromium.org25156de2010-04-06 13:10:27 +0000739 CodeEntry::kEmptyNamePrefix,
ager@chromium.org357bf652010-04-12 11:30:10 +0000740 GetFunctionName(name),
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000741 GetName(resource_name),
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000742 line_number,
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000743 TokenEnumerator::kNoSecurityToken);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000744 code_entries_.Add(entry);
745 return entry;
746}
747
748
749CodeEntry* CpuProfilesCollection::NewCodeEntry(Logger::LogEventsAndTags tag,
750 const char* name) {
lrn@chromium.org25156de2010-04-06 13:10:27 +0000751 CodeEntry* entry = new CodeEntry(tag,
752 CodeEntry::kEmptyNamePrefix,
ager@chromium.org357bf652010-04-12 11:30:10 +0000753 GetFunctionName(name),
lrn@chromium.org25156de2010-04-06 13:10:27 +0000754 "",
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000755 v8::CpuProfileNode::kNoLineNumberInfo,
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000756 TokenEnumerator::kNoSecurityToken);
lrn@chromium.org25156de2010-04-06 13:10:27 +0000757 code_entries_.Add(entry);
758 return entry;
759}
760
761
762CodeEntry* CpuProfilesCollection::NewCodeEntry(Logger::LogEventsAndTags tag,
763 const char* name_prefix,
764 String* name) {
765 CodeEntry* entry = new CodeEntry(tag,
766 name_prefix,
767 GetName(name),
768 "",
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000769 v8::CpuProfileNode::kNoLineNumberInfo,
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000770 TokenEnumerator::kInheritsSecurityToken);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000771 code_entries_.Add(entry);
772 return entry;
773}
774
775
776CodeEntry* CpuProfilesCollection::NewCodeEntry(Logger::LogEventsAndTags tag,
777 int args_count) {
lrn@chromium.org25156de2010-04-06 13:10:27 +0000778 CodeEntry* entry = new CodeEntry(tag,
779 "args_count: ",
780 GetName(args_count),
781 "",
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000782 v8::CpuProfileNode::kNoLineNumberInfo,
vegorov@chromium.org2356e6f2010-06-09 09:38:56 +0000783 TokenEnumerator::kInheritsSecurityToken);
erik.corry@gmail.com9dfbea42010-05-21 12:58:28 +0000784 code_entries_.Add(entry);
785 return entry;
786}
787
788
lrn@chromium.org25156de2010-04-06 13:10:27 +0000789void CpuProfilesCollection::AddPathToCurrentProfiles(
790 const Vector<CodeEntry*>& path) {
791 // As starting / stopping profiles is rare relatively to this
792 // method, we don't bother minimizing the duration of lock holding,
793 // e.g. copying contents of the list to a local vector.
794 current_profiles_semaphore_->Wait();
795 for (int i = 0; i < current_profiles_.length(); ++i) {
796 current_profiles_[i]->AddPath(path);
797 }
798 current_profiles_semaphore_->Signal();
799}
800
801
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000802void SampleRateCalculator::Tick() {
803 if (--wall_time_query_countdown_ == 0)
804 UpdateMeasurements(OS::TimeCurrentMillis());
805}
806
807
808void SampleRateCalculator::UpdateMeasurements(double current_time) {
809 if (measurements_count_++ != 0) {
810 const double measured_ticks_per_ms =
811 (kWallTimeQueryIntervalMs * ticks_per_ms_) /
812 (current_time - last_wall_time_);
813 // Update the average value.
814 ticks_per_ms_ +=
815 (measured_ticks_per_ms - ticks_per_ms_) / measurements_count_;
816 // Update the externally accessible result.
817 result_ = static_cast<AtomicWord>(ticks_per_ms_ * kResultScale);
818 }
819 last_wall_time_ = current_time;
820 wall_time_query_countdown_ =
821 static_cast<unsigned>(kWallTimeQueryIntervalMs * ticks_per_ms_);
822}
823
824
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000825const char* const ProfileGenerator::kAnonymousFunctionName =
826 "(anonymous function)";
827const char* const ProfileGenerator::kProgramEntryName =
828 "(program)";
829const char* const ProfileGenerator::kGarbageCollectorEntryName =
830 "(garbage collector)";
ager@chromium.org357bf652010-04-12 11:30:10 +0000831
832
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000833ProfileGenerator::ProfileGenerator(CpuProfilesCollection* profiles)
ager@chromium.org357bf652010-04-12 11:30:10 +0000834 : profiles_(profiles),
835 program_entry_(
836 profiles->NewCodeEntry(Logger::FUNCTION_TAG, kProgramEntryName)),
837 gc_entry_(
838 profiles->NewCodeEntry(Logger::BUILTIN_TAG,
839 kGarbageCollectorEntryName)) {
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000840}
841
842
843void ProfileGenerator::RecordTickSample(const TickSample& sample) {
ager@chromium.org357bf652010-04-12 11:30:10 +0000844 // Allocate space for stack frames + pc + function + vm-state.
845 ScopedVector<CodeEntry*> entries(sample.frames_count + 3);
846 // As actual number of decoded code entries may vary, initialize
847 // entries vector with NULL values.
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000848 CodeEntry** entry = entries.start();
ager@chromium.org357bf652010-04-12 11:30:10 +0000849 memset(entry, 0, entries.length() * sizeof(*entry));
850 if (sample.pc != NULL) {
851 *entry++ = code_map_.FindEntry(sample.pc);
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000852
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000853 if (sample.has_external_callback) {
854 // Don't use PC when in external callback code, as it can point
855 // inside callback's code, and we will erroneously report
856 // that a callback calls itself.
857 *(entries.start()) = NULL;
858 *entry++ = code_map_.FindEntry(sample.external_callback);
859 } else if (sample.tos != NULL) {
860 // Find out, if top of stack was pointing inside a JS function
861 // meaning that we have encountered a frameless invocation.
fschneider@chromium.org3a5fd782011-02-24 10:10:44 +0000862 *entry = code_map_.FindEntry(sample.tos);
ager@chromium.org357bf652010-04-12 11:30:10 +0000863 if (*entry != NULL && !(*entry)->is_js_function()) {
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000864 *entry = NULL;
ager@chromium.org357bf652010-04-12 11:30:10 +0000865 }
866 entry++;
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000867 }
ager@chromium.org357bf652010-04-12 11:30:10 +0000868
869 for (const Address *stack_pos = sample.stack,
870 *stack_end = stack_pos + sample.frames_count;
871 stack_pos != stack_end;
872 ++stack_pos) {
873 *entry++ = code_map_.FindEntry(*stack_pos);
874 }
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000875 }
876
ager@chromium.org357bf652010-04-12 11:30:10 +0000877 if (FLAG_prof_browser_mode) {
ricow@chromium.orgc9c80822010-04-21 08:22:37 +0000878 bool no_symbolized_entries = true;
879 for (CodeEntry** e = entries.start(); e != entry; ++e) {
880 if (*e != NULL) {
881 no_symbolized_entries = false;
882 break;
883 }
884 }
885 // If no frames were symbolized, put the VM state entry in.
886 if (no_symbolized_entries) {
887 *entry++ = EntryForVMState(sample.state);
888 }
whesse@chromium.orgcec079d2010-03-22 14:44:04 +0000889 }
890
lrn@chromium.org25156de2010-04-06 13:10:27 +0000891 profiles_->AddPathToCurrentProfiles(entries);
fschneider@chromium.org086aac62010-03-17 13:18:24 +0000892}
893
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000894
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000895void HeapGraphEdge::Init(
896 int child_index, Type type, const char* name, HeapEntry* to) {
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000897 ASSERT(type == kContextVariable
898 || type == kProperty
899 || type == kInternal
900 || type == kShortcut);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000901 child_index_ = child_index;
902 type_ = type;
903 name_ = name;
904 to_ = to;
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000905}
906
907
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000908void HeapGraphEdge::Init(int child_index, Type type, int index, HeapEntry* to) {
909 ASSERT(type == kElement || type == kHidden);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000910 child_index_ = child_index;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000911 type_ = type;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000912 index_ = index;
913 to_ = to;
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000914}
915
916
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000917void HeapGraphEdge::Init(int child_index, int index, HeapEntry* to) {
918 Init(child_index, kElement, index, to);
919}
920
921
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000922HeapEntry* HeapGraphEdge::From() {
923 return reinterpret_cast<HeapEntry*>(this - child_index_) - 1;
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000924}
925
926
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000927void HeapEntry::Init(HeapSnapshot* snapshot,
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000928 Type type,
929 const char* name,
930 uint64_t id,
931 int self_size,
932 int children_count,
933 int retainers_count) {
934 snapshot_ = snapshot;
935 type_ = type;
936 painted_ = kUnpainted;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000937 name_ = name;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000938 self_size_ = self_size;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000939 retained_size_ = 0;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000940 children_count_ = children_count;
941 retainers_count_ = retainers_count;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000942 dominator_ = NULL;
943
944 union {
945 uint64_t set_id;
946 Id stored_id;
947 } id_adaptor = {id};
948 id_ = id_adaptor.stored_id;
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000949}
950
951
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000952void HeapEntry::SetNamedReference(HeapGraphEdge::Type type,
953 int child_index,
954 const char* name,
955 HeapEntry* entry,
956 int retainer_index) {
957 children_arr()[child_index].Init(child_index, type, name, entry);
958 entry->retainers_arr()[retainer_index] = children_arr() + child_index;
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000959}
960
961
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000962void HeapEntry::SetIndexedReference(HeapGraphEdge::Type type,
963 int child_index,
964 int index,
965 HeapEntry* entry,
966 int retainer_index) {
967 children_arr()[child_index].Init(child_index, type, index, entry);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000968 entry->retainers_arr()[retainer_index] = children_arr() + child_index;
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000969}
970
971
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000972void HeapEntry::SetUnidirElementReference(
973 int child_index, int index, HeapEntry* entry) {
974 children_arr()[child_index].Init(child_index, index, entry);
ricow@chromium.org5ad5ace2010-06-23 09:06:43 +0000975}
976
977
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000978int HeapEntry::RetainedSize(bool exact) {
979 if (exact && (retained_size_ & kExactRetainedSizeTag) == 0) {
980 CalculateExactRetainedSize();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000981 }
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000982 return retained_size_ & (~kExactRetainedSizeTag);
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000983}
984
985
ricow@chromium.org4980dff2010-07-19 08:33:45 +0000986template<class Visitor>
987void HeapEntry::ApplyAndPaintAllReachable(Visitor* visitor) {
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000988 List<HeapEntry*> list(10);
989 list.Add(this);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000990 this->paint_reachable();
ricow@chromium.org4980dff2010-07-19 08:33:45 +0000991 visitor->Apply(this);
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000992 while (!list.is_empty()) {
993 HeapEntry* entry = list.RemoveLast();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000994 Vector<HeapGraphEdge> children = entry->children();
995 for (int i = 0; i < children.length(); ++i) {
vegorov@chromium.org21b5e952010-11-23 10:24:40 +0000996 if (children[i].type() == HeapGraphEdge::kShortcut) continue;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +0000997 HeapEntry* child = children[i].to();
ager@chromium.org2cc82ae2010-06-14 07:35:38 +0000998 if (!child->painted_reachable()) {
999 list.Add(child);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001000 child->paint_reachable();
ricow@chromium.org4980dff2010-07-19 08:33:45 +00001001 visitor->Apply(child);
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001002 }
1003 }
1004 }
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001005}
1006
1007
ricow@chromium.org4980dff2010-07-19 08:33:45 +00001008class NullClass {
1009 public:
1010 void Apply(HeapEntry* entry) { }
1011};
1012
1013void HeapEntry::PaintAllReachable() {
1014 NullClass null;
1015 ApplyAndPaintAllReachable(&null);
1016}
1017
1018
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001019void HeapEntry::Print(int max_depth, int indent) {
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001020 OS::Print("%6d %6d [%llu] ", self_size(), RetainedSize(false), id());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001021 if (type() != kString) {
1022 OS::Print("%s %.40s\n", TypeAsString(), name_);
1023 } else {
1024 OS::Print("\"");
1025 const char* c = name_;
1026 while (*c && (c - name_) <= 40) {
1027 if (*c != '\n')
1028 OS::Print("%c", *c);
1029 else
1030 OS::Print("\\n");
1031 ++c;
1032 }
1033 OS::Print("\"\n");
ricow@chromium.org4980dff2010-07-19 08:33:45 +00001034 }
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001035 if (--max_depth == 0) return;
1036 Vector<HeapGraphEdge> ch = children();
1037 for (int i = 0; i < ch.length(); ++i) {
1038 HeapGraphEdge& edge = ch[i];
1039 switch (edge.type()) {
1040 case HeapGraphEdge::kContextVariable:
1041 OS::Print(" %*c #%s: ", indent, ' ', edge.name());
1042 break;
1043 case HeapGraphEdge::kElement:
1044 OS::Print(" %*c %d: ", indent, ' ', edge.index());
1045 break;
1046 case HeapGraphEdge::kInternal:
1047 OS::Print(" %*c $%s: ", indent, ' ', edge.name());
1048 break;
1049 case HeapGraphEdge::kProperty:
1050 OS::Print(" %*c %s: ", indent, ' ', edge.name());
1051 break;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001052 case HeapGraphEdge::kHidden:
1053 OS::Print(" %*c $%d: ", indent, ' ', edge.index());
1054 break;
1055 case HeapGraphEdge::kShortcut:
1056 OS::Print(" %*c ^%s: ", indent, ' ', edge.name());
1057 break;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001058 default:
1059 OS::Print("!!! unknown edge type: %d ", edge.type());
1060 }
1061 edge.to()->Print(max_depth, indent + 2);
ricow@chromium.org4980dff2010-07-19 08:33:45 +00001062 }
ricow@chromium.org4980dff2010-07-19 08:33:45 +00001063}
1064
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001065
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001066const char* HeapEntry::TypeAsString() {
1067 switch (type()) {
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001068 case kHidden: return "/hidden/";
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001069 case kObject: return "/object/";
1070 case kClosure: return "/closure/";
1071 case kString: return "/string/";
1072 case kCode: return "/code/";
1073 case kArray: return "/array/";
vegorov@chromium.org42841962010-10-18 11:18:59 +00001074 case kRegExp: return "/regexp/";
1075 case kHeapNumber: return "/number/";
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00001076 case kNative: return "/native/";
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001077 default: return "???";
1078 }
1079}
1080
1081
1082int HeapEntry::EntriesSize(int entries_count,
1083 int children_count,
1084 int retainers_count) {
1085 return sizeof(HeapEntry) * entries_count // NOLINT
1086 + sizeof(HeapGraphEdge) * children_count // NOLINT
1087 + sizeof(HeapGraphEdge*) * retainers_count; // NOLINT
1088}
1089
1090
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001091class RetainedSizeCalculator {
1092 public:
1093 RetainedSizeCalculator()
1094 : retained_size_(0) {
1095 }
1096
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00001097 int retained_size() const { return retained_size_; }
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001098
1099 void Apply(HeapEntry** entry_ptr) {
1100 if ((*entry_ptr)->painted_reachable()) {
1101 retained_size_ += (*entry_ptr)->self_size();
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001102 }
1103 }
1104
1105 private:
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001106 int retained_size_;
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001107};
1108
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001109void HeapEntry::CalculateExactRetainedSize() {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001110 // To calculate retained size, first we paint all reachable nodes in
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001111 // one color, then we paint (or re-paint) all nodes reachable from
1112 // other nodes with a different color. Then we sum up self sizes of
1113 // nodes painted with the first color.
1114 snapshot()->ClearPaint();
1115 PaintAllReachable();
ricow@chromium.org4980dff2010-07-19 08:33:45 +00001116
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001117 List<HeapEntry*> list(10);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001118 HeapEntry* root = snapshot()->root();
1119 if (this != root) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001120 list.Add(root);
1121 root->paint_reachable_from_others();
ricow@chromium.org4980dff2010-07-19 08:33:45 +00001122 }
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001123 while (!list.is_empty()) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001124 HeapEntry* curr = list.RemoveLast();
1125 Vector<HeapGraphEdge> children = curr->children();
1126 for (int i = 0; i < children.length(); ++i) {
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001127 if (children[i].type() == HeapGraphEdge::kShortcut) continue;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001128 HeapEntry* child = children[i].to();
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001129 if (child != this && child->not_painted_reachable_from_others()) {
ricow@chromium.org4980dff2010-07-19 08:33:45 +00001130 list.Add(child);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001131 child->paint_reachable_from_others();
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001132 }
1133 }
1134 }
1135
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001136 RetainedSizeCalculator ret_size_calc;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001137 snapshot()->IterateEntries(&ret_size_calc);
jkummerow@chromium.orge297f592011-06-08 10:05:15 +00001138 retained_size_ = ret_size_calc.retained_size();
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001139 ASSERT((retained_size_ & kExactRetainedSizeTag) == 0);
1140 retained_size_ |= kExactRetainedSizeTag;
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001141}
1142
1143
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001144// It is very important to keep objects that form a heap snapshot
1145// as small as possible.
1146namespace { // Avoid littering the global namespace.
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001147
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001148template <size_t ptr_size> struct SnapshotSizeConstants;
1149
1150template <> struct SnapshotSizeConstants<4> {
1151 static const int kExpectedHeapGraphEdgeSize = 12;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001152 static const int kExpectedHeapEntrySize = 36;
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001153};
1154
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001155template <> struct SnapshotSizeConstants<8> {
1156 static const int kExpectedHeapGraphEdgeSize = 24;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001157 static const int kExpectedHeapEntrySize = 48;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001158};
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001159
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001160} // namespace
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001161
1162HeapSnapshot::HeapSnapshot(HeapSnapshotsCollection* collection,
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001163 HeapSnapshot::Type type,
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001164 const char* title,
1165 unsigned uid)
1166 : collection_(collection),
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001167 type_(type),
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001168 title_(title),
1169 uid_(uid),
ricow@chromium.orgeb7c1442010-10-04 08:54:21 +00001170 root_entry_(NULL),
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001171 gc_roots_entry_(NULL),
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00001172 natives_root_entry_(NULL),
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001173 raw_entries_(NULL),
lrn@chromium.org7516f052011-03-30 08:52:27 +00001174 entries_sorted_(false) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001175 STATIC_ASSERT(
1176 sizeof(HeapGraphEdge) ==
1177 SnapshotSizeConstants<sizeof(void*)>::kExpectedHeapGraphEdgeSize); // NOLINT
1178 STATIC_ASSERT(
1179 sizeof(HeapEntry) ==
1180 SnapshotSizeConstants<sizeof(void*)>::kExpectedHeapEntrySize); // NOLINT
ricow@chromium.org4980dff2010-07-19 08:33:45 +00001181}
1182
ricow@chromium.org4980dff2010-07-19 08:33:45 +00001183HeapSnapshot::~HeapSnapshot() {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001184 DeleteArray(raw_entries_);
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001185}
1186
1187
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001188void HeapSnapshot::Delete() {
1189 collection_->RemoveSnapshot(this);
1190 delete this;
1191}
1192
1193
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001194void HeapSnapshot::AllocateEntries(int entries_count,
1195 int children_count,
1196 int retainers_count) {
1197 ASSERT(raw_entries_ == NULL);
1198 raw_entries_ = NewArray<char>(
1199 HeapEntry::EntriesSize(entries_count, children_count, retainers_count));
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001200#ifdef DEBUG
1201 raw_entries_size_ =
1202 HeapEntry::EntriesSize(entries_count, children_count, retainers_count);
1203#endif
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001204}
1205
1206
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001207static void HeapEntryClearPaint(HeapEntry** entry_ptr) {
1208 (*entry_ptr)->clear_paint();
1209}
1210
1211void HeapSnapshot::ClearPaint() {
1212 entries_.Iterate(HeapEntryClearPaint);
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001213}
1214
1215
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001216HeapEntry* HeapSnapshot::AddRootEntry(int children_count) {
1217 ASSERT(root_entry_ == NULL);
1218 return (root_entry_ = AddEntry(HeapEntry::kObject,
1219 "",
1220 HeapObjectsMap::kInternalRootObjectId,
1221 0,
1222 children_count,
1223 0));
1224}
1225
1226
1227HeapEntry* HeapSnapshot::AddGcRootsEntry(int children_count,
1228 int retainers_count) {
1229 ASSERT(gc_roots_entry_ == NULL);
1230 return (gc_roots_entry_ = AddEntry(HeapEntry::kObject,
1231 "(GC roots)",
1232 HeapObjectsMap::kGcRootsObjectId,
1233 0,
1234 children_count,
1235 retainers_count));
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001236}
1237
1238
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00001239HeapEntry* HeapSnapshot::AddNativesRootEntry(int children_count,
1240 int retainers_count) {
1241 ASSERT(natives_root_entry_ == NULL);
1242 return (natives_root_entry_ = AddEntry(
1243 HeapEntry::kObject,
1244 "(Native objects)",
1245 HeapObjectsMap::kNativesRootObjectId,
1246 0,
1247 children_count,
1248 retainers_count));
1249}
1250
1251
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001252HeapEntry* HeapSnapshot::AddEntry(HeapEntry::Type type,
1253 const char* name,
1254 uint64_t id,
1255 int size,
1256 int children_count,
1257 int retainers_count) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001258 HeapEntry* entry = GetNextEntryToInit();
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001259 entry->Init(this, type, name, id, size, children_count, retainers_count);
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001260 return entry;
1261}
1262
1263
ager@chromium.orgbeb25712010-11-29 08:02:25 +00001264void HeapSnapshot::SetDominatorsToSelf() {
1265 for (int i = 0; i < entries_.length(); ++i) {
1266 HeapEntry* entry = entries_[i];
1267 if (entry->dominator() == NULL) entry->set_dominator(entry);
1268 }
1269}
1270
1271
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001272HeapEntry* HeapSnapshot::GetNextEntryToInit() {
1273 if (entries_.length() > 0) {
1274 HeapEntry* last_entry = entries_.last();
1275 entries_.Add(reinterpret_cast<HeapEntry*>(
1276 reinterpret_cast<char*>(last_entry) + last_entry->EntrySize()));
1277 } else {
1278 entries_.Add(reinterpret_cast<HeapEntry*>(raw_entries_));
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001279 }
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001280 ASSERT(reinterpret_cast<char*>(entries_.last()) <
1281 (raw_entries_ + raw_entries_size_));
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001282 return entries_.last();
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001283}
1284
1285
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001286HeapEntry* HeapSnapshot::GetEntryById(uint64_t id) {
kasperl@chromium.orga5551262010-12-07 12:49:48 +00001287 List<HeapEntry*>* entries_by_id = GetSortedEntriesList();
1288
1289 // Perform a binary search by id.
1290 int low = 0;
1291 int high = entries_by_id->length() - 1;
1292 while (low <= high) {
1293 int mid =
1294 (static_cast<unsigned int>(low) + static_cast<unsigned int>(high)) >> 1;
1295 uint64_t mid_id = entries_by_id->at(mid)->id();
1296 if (mid_id > id)
1297 high = mid - 1;
1298 else if (mid_id < id)
1299 low = mid + 1;
1300 else
1301 return entries_by_id->at(mid);
1302 }
1303 return NULL;
1304}
1305
1306
ricow@chromium.org4980dff2010-07-19 08:33:45 +00001307template<class T>
1308static int SortByIds(const T* entry1_ptr,
1309 const T* entry2_ptr) {
1310 if ((*entry1_ptr)->id() == (*entry2_ptr)->id()) return 0;
1311 return (*entry1_ptr)->id() < (*entry2_ptr)->id() ? -1 : 1;
1312}
1313
1314List<HeapEntry*>* HeapSnapshot::GetSortedEntriesList() {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001315 if (!entries_sorted_) {
1316 entries_.Sort(SortByIds);
1317 entries_sorted_ = true;
1318 }
1319 return &entries_;
ricow@chromium.org4980dff2010-07-19 08:33:45 +00001320}
1321
1322
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001323void HeapSnapshot::Print(int max_depth) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001324 root()->Print(max_depth, 0);
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001325}
1326
1327
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00001328// We split IDs on evens for embedder objects (see
1329// HeapObjectsMap::GenerateId) and odds for native objects.
1330const uint64_t HeapObjectsMap::kInternalRootObjectId = 1;
1331const uint64_t HeapObjectsMap::kGcRootsObjectId = 3;
1332const uint64_t HeapObjectsMap::kNativesRootObjectId = 5;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001333// Increase kFirstAvailableObjectId if new 'special' objects appear.
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00001334const uint64_t HeapObjectsMap::kFirstAvailableObjectId = 7;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001335
ricow@chromium.org4980dff2010-07-19 08:33:45 +00001336HeapObjectsMap::HeapObjectsMap()
1337 : initial_fill_mode_(true),
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001338 next_id_(kFirstAvailableObjectId),
ricow@chromium.org4980dff2010-07-19 08:33:45 +00001339 entries_map_(AddressesMatch),
1340 entries_(new List<EntryInfo>()) { }
1341
1342
1343HeapObjectsMap::~HeapObjectsMap() {
1344 delete entries_;
1345}
1346
1347
1348void HeapObjectsMap::SnapshotGenerationFinished() {
1349 initial_fill_mode_ = false;
1350 RemoveDeadEntries();
1351}
1352
1353
1354uint64_t HeapObjectsMap::FindObject(Address addr) {
1355 if (!initial_fill_mode_) {
1356 uint64_t existing = FindEntry(addr);
1357 if (existing != 0) return existing;
1358 }
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00001359 uint64_t id = next_id_;
1360 next_id_ += 2;
ricow@chromium.org4980dff2010-07-19 08:33:45 +00001361 AddEntry(addr, id);
1362 return id;
1363}
1364
1365
1366void HeapObjectsMap::MoveObject(Address from, Address to) {
1367 if (from == to) return;
1368 HashMap::Entry* entry = entries_map_.Lookup(from, AddressHash(from), false);
1369 if (entry != NULL) {
1370 void* value = entry->value;
1371 entries_map_.Remove(from, AddressHash(from));
1372 entry = entries_map_.Lookup(to, AddressHash(to), true);
1373 // We can have an entry at the new location, it is OK, as GC can overwrite
1374 // dead objects with alive objects being moved.
1375 entry->value = value;
1376 }
1377}
1378
1379
1380void HeapObjectsMap::AddEntry(Address addr, uint64_t id) {
1381 HashMap::Entry* entry = entries_map_.Lookup(addr, AddressHash(addr), true);
1382 ASSERT(entry->value == NULL);
1383 entry->value = reinterpret_cast<void*>(entries_->length());
1384 entries_->Add(EntryInfo(id));
1385}
1386
1387
1388uint64_t HeapObjectsMap::FindEntry(Address addr) {
1389 HashMap::Entry* entry = entries_map_.Lookup(addr, AddressHash(addr), false);
1390 if (entry != NULL) {
1391 int entry_index =
1392 static_cast<int>(reinterpret_cast<intptr_t>(entry->value));
1393 EntryInfo& entry_info = entries_->at(entry_index);
1394 entry_info.accessed = true;
1395 return entry_info.id;
1396 } else {
1397 return 0;
1398 }
1399}
1400
1401
1402void HeapObjectsMap::RemoveDeadEntries() {
1403 List<EntryInfo>* new_entries = new List<EntryInfo>();
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001404 List<void*> dead_entries;
ricow@chromium.org4980dff2010-07-19 08:33:45 +00001405 for (HashMap::Entry* entry = entries_map_.Start();
1406 entry != NULL;
1407 entry = entries_map_.Next(entry)) {
1408 int entry_index =
1409 static_cast<int>(reinterpret_cast<intptr_t>(entry->value));
1410 EntryInfo& entry_info = entries_->at(entry_index);
1411 if (entry_info.accessed) {
1412 entry->value = reinterpret_cast<void*>(new_entries->length());
1413 new_entries->Add(EntryInfo(entry_info.id, false));
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001414 } else {
1415 dead_entries.Add(entry->key);
ricow@chromium.org4980dff2010-07-19 08:33:45 +00001416 }
1417 }
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001418 for (int i = 0; i < dead_entries.length(); ++i) {
1419 void* raw_entry = dead_entries[i];
1420 entries_map_.Remove(
1421 raw_entry, AddressHash(reinterpret_cast<Address>(raw_entry)));
1422 }
ricow@chromium.org4980dff2010-07-19 08:33:45 +00001423 delete entries_;
1424 entries_ = new_entries;
1425}
1426
1427
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00001428uint64_t HeapObjectsMap::GenerateId(v8::RetainedObjectInfo* info) {
1429 uint64_t id = static_cast<uint64_t>(info->GetHash());
1430 const char* label = info->GetLabel();
1431 id ^= HashSequentialString(label, static_cast<int>(strlen(label)));
1432 intptr_t element_count = info->GetElementCount();
1433 if (element_count != -1)
1434 id ^= ComputeIntegerHash(static_cast<uint32_t>(element_count));
1435 return id << 1;
1436}
1437
1438
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001439HeapSnapshotsCollection::HeapSnapshotsCollection()
ricow@chromium.org4980dff2010-07-19 08:33:45 +00001440 : is_tracking_objects_(false),
1441 snapshots_uids_(HeapSnapshotsMatch),
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001442 token_enumerator_(new TokenEnumerator()) {
1443}
1444
1445
1446static void DeleteHeapSnapshot(HeapSnapshot** snapshot_ptr) {
1447 delete *snapshot_ptr;
1448}
1449
1450
1451HeapSnapshotsCollection::~HeapSnapshotsCollection() {
1452 delete token_enumerator_;
1453 snapshots_.Iterate(DeleteHeapSnapshot);
1454}
1455
1456
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001457HeapSnapshot* HeapSnapshotsCollection::NewSnapshot(HeapSnapshot::Type type,
1458 const char* name,
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001459 unsigned uid) {
ricow@chromium.org4980dff2010-07-19 08:33:45 +00001460 is_tracking_objects_ = true; // Start watching for heap objects moves.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001461 return new HeapSnapshot(this, type, name, uid);
1462}
1463
1464
1465void HeapSnapshotsCollection::SnapshotGenerationFinished(
1466 HeapSnapshot* snapshot) {
1467 ids_.SnapshotGenerationFinished();
1468 if (snapshot != NULL) {
1469 snapshots_.Add(snapshot);
1470 HashMap::Entry* entry =
1471 snapshots_uids_.Lookup(reinterpret_cast<void*>(snapshot->uid()),
1472 static_cast<uint32_t>(snapshot->uid()),
1473 true);
1474 ASSERT(entry->value == NULL);
1475 entry->value = snapshot;
1476 }
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001477}
1478
1479
1480HeapSnapshot* HeapSnapshotsCollection::GetSnapshot(unsigned uid) {
1481 HashMap::Entry* entry = snapshots_uids_.Lookup(reinterpret_cast<void*>(uid),
1482 static_cast<uint32_t>(uid),
1483 false);
1484 return entry != NULL ? reinterpret_cast<HeapSnapshot*>(entry->value) : NULL;
1485}
1486
1487
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001488void HeapSnapshotsCollection::RemoveSnapshot(HeapSnapshot* snapshot) {
1489 snapshots_.RemoveElement(snapshot);
1490 unsigned uid = snapshot->uid();
1491 snapshots_uids_.Remove(reinterpret_cast<void*>(uid),
1492 static_cast<uint32_t>(uid));
1493}
1494
1495
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001496HeapEntry *const HeapEntriesMap::kHeapEntryPlaceholder =
1497 reinterpret_cast<HeapEntry*>(1);
1498
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001499HeapEntriesMap::HeapEntriesMap()
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001500 : entries_(HeapThingsMatch),
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001501 entries_count_(0),
1502 total_children_count_(0),
1503 total_retainers_count_(0) {
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001504}
1505
1506
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001507HeapEntriesMap::~HeapEntriesMap() {
1508 for (HashMap::Entry* p = entries_.Start(); p != NULL; p = entries_.Next(p)) {
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001509 delete reinterpret_cast<EntryInfo*>(p->value);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001510 }
1511}
1512
1513
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001514void HeapEntriesMap::AllocateEntries() {
1515 for (HashMap::Entry* p = entries_.Start();
1516 p != NULL;
1517 p = entries_.Next(p)) {
1518 EntryInfo* entry_info = reinterpret_cast<EntryInfo*>(p->value);
1519 entry_info->entry = entry_info->allocator->AllocateEntry(
1520 p->key,
1521 entry_info->children_count,
1522 entry_info->retainers_count);
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00001523 ASSERT(entry_info->entry != NULL);
1524 ASSERT(entry_info->entry != kHeapEntryPlaceholder);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001525 entry_info->children_count = 0;
1526 entry_info->retainers_count = 0;
1527 }
1528}
1529
1530
1531HeapEntry* HeapEntriesMap::Map(HeapThing thing) {
1532 HashMap::Entry* cache_entry = entries_.Lookup(thing, Hash(thing), false);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001533 if (cache_entry != NULL) {
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001534 EntryInfo* entry_info = reinterpret_cast<EntryInfo*>(cache_entry->value);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001535 return entry_info->entry;
1536 } else {
1537 return NULL;
1538 }
1539}
1540
1541
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001542void HeapEntriesMap::Pair(
1543 HeapThing thing, HeapEntriesAllocator* allocator, HeapEntry* entry) {
1544 HashMap::Entry* cache_entry = entries_.Lookup(thing, Hash(thing), true);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001545 ASSERT(cache_entry->value == NULL);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001546 cache_entry->value = new EntryInfo(entry, allocator);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001547 ++entries_count_;
1548}
1549
1550
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001551void HeapEntriesMap::CountReference(HeapThing from, HeapThing to,
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001552 int* prev_children_count,
1553 int* prev_retainers_count) {
erik.corry@gmail.com145eff52010-08-23 11:36:18 +00001554 HashMap::Entry* from_cache_entry = entries_.Lookup(from, Hash(from), false);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001555 HashMap::Entry* to_cache_entry = entries_.Lookup(to, Hash(to), false);
1556 ASSERT(from_cache_entry != NULL);
1557 ASSERT(to_cache_entry != NULL);
1558 EntryInfo* from_entry_info =
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001559 reinterpret_cast<EntryInfo*>(from_cache_entry->value);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001560 EntryInfo* to_entry_info =
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001561 reinterpret_cast<EntryInfo*>(to_cache_entry->value);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001562 if (prev_children_count)
1563 *prev_children_count = from_entry_info->children_count;
1564 if (prev_retainers_count)
1565 *prev_retainers_count = to_entry_info->retainers_count;
1566 ++from_entry_info->children_count;
1567 ++to_entry_info->retainers_count;
1568 ++total_children_count_;
1569 ++total_retainers_count_;
1570}
1571
1572
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001573HeapObjectsSet::HeapObjectsSet()
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001574 : entries_(HeapEntriesMap::HeapThingsMatch) {
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001575}
1576
1577
1578void HeapObjectsSet::Clear() {
1579 entries_.Clear();
1580}
1581
1582
1583bool HeapObjectsSet::Contains(Object* obj) {
1584 if (!obj->IsHeapObject()) return false;
1585 HeapObject* object = HeapObject::cast(obj);
1586 HashMap::Entry* cache_entry =
1587 entries_.Lookup(object, HeapEntriesMap::Hash(object), false);
1588 return cache_entry != NULL;
1589}
1590
1591
1592void HeapObjectsSet::Insert(Object* obj) {
1593 if (!obj->IsHeapObject()) return;
1594 HeapObject* object = HeapObject::cast(obj);
1595 HashMap::Entry* cache_entry =
1596 entries_.Lookup(object, HeapEntriesMap::Hash(object), true);
1597 if (cache_entry->value == NULL) {
1598 cache_entry->value = HeapEntriesMap::kHeapEntryPlaceholder;
1599 }
1600}
1601
1602
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001603const char* HeapObjectsSet::GetTag(Object* obj) {
1604 HeapObject* object = HeapObject::cast(obj);
1605 HashMap::Entry* cache_entry =
1606 entries_.Lookup(object, HeapEntriesMap::Hash(object), false);
1607 if (cache_entry != NULL
1608 && cache_entry->value != HeapEntriesMap::kHeapEntryPlaceholder) {
1609 return reinterpret_cast<const char*>(cache_entry->value);
1610 } else {
1611 return NULL;
1612 }
1613}
1614
1615
1616void HeapObjectsSet::SetTag(Object* obj, const char* tag) {
1617 if (!obj->IsHeapObject()) return;
1618 HeapObject* object = HeapObject::cast(obj);
1619 HashMap::Entry* cache_entry =
1620 entries_.Lookup(object, HeapEntriesMap::Hash(object), true);
1621 cache_entry->value = const_cast<char*>(tag);
1622}
1623
1624
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001625HeapObject *const V8HeapExplorer::kInternalRootObject =
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00001626 reinterpret_cast<HeapObject*>(
1627 static_cast<intptr_t>(HeapObjectsMap::kInternalRootObjectId));
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001628HeapObject *const V8HeapExplorer::kGcRootsObject =
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00001629 reinterpret_cast<HeapObject*>(
1630 static_cast<intptr_t>(HeapObjectsMap::kGcRootsObjectId));
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001631
1632
1633V8HeapExplorer::V8HeapExplorer(
1634 HeapSnapshot* snapshot,
1635 SnapshottingProgressReportingInterface* progress)
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001636 : heap_(Isolate::Current()->heap()),
1637 snapshot_(snapshot),
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001638 collection_(snapshot_->collection()),
1639 progress_(progress),
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001640 filler_(NULL) {
1641}
1642
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001643
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001644V8HeapExplorer::~V8HeapExplorer() {
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001645}
1646
1647
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001648HeapEntry* V8HeapExplorer::AllocateEntry(
1649 HeapThing ptr, int children_count, int retainers_count) {
1650 return AddEntry(
1651 reinterpret_cast<HeapObject*>(ptr), children_count, retainers_count);
1652}
1653
1654
1655HeapEntry* V8HeapExplorer::AddEntry(HeapObject* object,
1656 int children_count,
1657 int retainers_count) {
1658 if (object == kInternalRootObject) {
1659 ASSERT(retainers_count == 0);
1660 return snapshot_->AddRootEntry(children_count);
1661 } else if (object == kGcRootsObject) {
1662 return snapshot_->AddGcRootsEntry(children_count, retainers_count);
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001663 } else if (object->IsJSGlobalObject()) {
1664 const char* tag = objects_tags_.GetTag(object);
1665 const char* name = collection_->names()->GetName(
1666 GetConstructorNameForHeapProfile(JSObject::cast(object)));
1667 if (tag != NULL) {
1668 name = collection_->names()->GetFormatted("%s / %s", name, tag);
1669 }
1670 return AddEntry(object,
1671 HeapEntry::kObject,
1672 name,
1673 children_count,
1674 retainers_count);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001675 } else if (object->IsJSFunction()) {
1676 JSFunction* func = JSFunction::cast(object);
1677 SharedFunctionInfo* shared = func->shared();
1678 return AddEntry(object,
1679 HeapEntry::kClosure,
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00001680 collection_->names()->GetName(String::cast(shared->name())),
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001681 children_count,
1682 retainers_count);
1683 } else if (object->IsJSRegExp()) {
1684 JSRegExp* re = JSRegExp::cast(object);
1685 return AddEntry(object,
1686 HeapEntry::kRegExp,
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00001687 collection_->names()->GetName(re->Pattern()),
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001688 children_count,
1689 retainers_count);
1690 } else if (object->IsJSObject()) {
1691 return AddEntry(object,
1692 HeapEntry::kObject,
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00001693 collection_->names()->GetName(
1694 GetConstructorNameForHeapProfile(
1695 JSObject::cast(object))),
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001696 children_count,
1697 retainers_count);
1698 } else if (object->IsString()) {
1699 return AddEntry(object,
1700 HeapEntry::kString,
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00001701 collection_->names()->GetName(String::cast(object)),
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001702 children_count,
1703 retainers_count);
1704 } else if (object->IsCode()) {
1705 return AddEntry(object,
1706 HeapEntry::kCode,
1707 "",
1708 children_count,
1709 retainers_count);
1710 } else if (object->IsSharedFunctionInfo()) {
1711 SharedFunctionInfo* shared = SharedFunctionInfo::cast(object);
1712 return AddEntry(object,
1713 HeapEntry::kCode,
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00001714 collection_->names()->GetName(String::cast(shared->name())),
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001715 children_count,
1716 retainers_count);
1717 } else if (object->IsScript()) {
1718 Script* script = Script::cast(object);
1719 return AddEntry(object,
1720 HeapEntry::kCode,
1721 script->name()->IsString() ?
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00001722 collection_->names()->GetName(
1723 String::cast(script->name()))
1724 : "",
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001725 children_count,
1726 retainers_count);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001727 } else if (object->IsFixedArray() ||
1728 object->IsFixedDoubleArray() ||
1729 object->IsByteArray() ||
1730 object->IsExternalArray()) {
1731 const char* tag = objects_tags_.GetTag(object);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001732 return AddEntry(object,
1733 HeapEntry::kArray,
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001734 tag != NULL ? tag : "",
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001735 children_count,
1736 retainers_count);
1737 } else if (object->IsHeapNumber()) {
1738 return AddEntry(object,
1739 HeapEntry::kHeapNumber,
1740 "number",
1741 children_count,
1742 retainers_count);
1743 }
1744 return AddEntry(object,
1745 HeapEntry::kHidden,
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001746 GetSystemEntryName(object),
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001747 children_count,
1748 retainers_count);
1749}
1750
1751
1752HeapEntry* V8HeapExplorer::AddEntry(HeapObject* object,
1753 HeapEntry::Type type,
1754 const char* name,
1755 int children_count,
1756 int retainers_count) {
1757 return snapshot_->AddEntry(type,
1758 name,
1759 collection_->GetObjectId(object->address()),
1760 object->Size(),
1761 children_count,
1762 retainers_count);
1763}
1764
1765
1766void V8HeapExplorer::AddRootEntries(SnapshotFillerInterface* filler) {
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00001767 filler->AddEntry(kInternalRootObject, this);
1768 filler->AddEntry(kGcRootsObject, this);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001769}
1770
1771
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001772const char* V8HeapExplorer::GetSystemEntryName(HeapObject* object) {
1773 switch (object->map()->instance_type()) {
1774 case MAP_TYPE: return "system / Map";
1775 case JS_GLOBAL_PROPERTY_CELL_TYPE: return "system / JSGlobalPropertyCell";
ager@chromium.orgea91cc52011-05-23 06:06:11 +00001776 case FOREIGN_TYPE: return "system / Foreign";
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001777 case ODDBALL_TYPE: return "system / Oddball";
1778#define MAKE_STRUCT_CASE(NAME, Name, name) \
1779 case NAME##_TYPE: return "system / "#Name;
1780 STRUCT_LIST(MAKE_STRUCT_CASE)
1781#undef MAKE_STRUCT_CASE
1782 default: return "system";
1783 }
1784}
1785
1786
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001787int V8HeapExplorer::EstimateObjectsCount() {
1788 HeapIterator iterator(HeapIterator::kFilterUnreachable);
1789 int objects_count = 0;
1790 for (HeapObject* obj = iterator.next();
1791 obj != NULL;
1792 obj = iterator.next(), ++objects_count) {}
1793 return objects_count;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001794}
1795
1796
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001797class IndexedReferencesExtractor : public ObjectVisitor {
1798 public:
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001799 IndexedReferencesExtractor(V8HeapExplorer* generator,
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001800 HeapObject* parent_obj,
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001801 HeapEntry* parent_entry)
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001802 : generator_(generator),
1803 parent_obj_(parent_obj),
1804 parent_(parent_entry),
1805 next_index_(1) {
1806 }
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001807 void VisitPointers(Object** start, Object** end) {
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001808 for (Object** p = start; p < end; p++) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001809 if (CheckVisitedAndUnmark(p)) continue;
1810 generator_->SetHiddenReference(parent_obj_, parent_, next_index_++, *p);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001811 }
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001812 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001813 static void MarkVisitedField(HeapObject* obj, int offset) {
1814 if (offset < 0) return;
1815 Address field = obj->address() + offset;
1816 ASSERT(!Memory::Object_at(field)->IsFailure());
1817 ASSERT(Memory::Object_at(field)->IsHeapObject());
1818 *field |= kFailureTag;
1819 }
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00001820
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001821 private:
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001822 bool CheckVisitedAndUnmark(Object** field) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001823 if ((*field)->IsFailure()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001824 intptr_t untagged = reinterpret_cast<intptr_t>(*field) & ~kFailureTagMask;
1825 *field = reinterpret_cast<Object*>(untagged | kHeapObjectTag);
1826 ASSERT((*field)->IsHeapObject());
1827 return true;
1828 }
1829 return false;
1830 }
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001831 V8HeapExplorer* generator_;
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001832 HeapObject* parent_obj_;
1833 HeapEntry* parent_;
1834 int next_index_;
1835};
1836
1837
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001838void V8HeapExplorer::ExtractReferences(HeapObject* obj) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001839 HeapEntry* entry = GetEntry(obj);
1840 if (entry == NULL) return; // No interest in this object.
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001841
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001842 bool extract_indexed_refs = true;
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001843 if (obj->IsJSGlobalProxy()) {
1844 // We need to reference JS global objects from snapshot's root.
1845 // We use JSGlobalProxy because this is what embedder (e.g. browser)
1846 // uses for the global object.
1847 JSGlobalProxy* proxy = JSGlobalProxy::cast(obj);
1848 SetRootShortcutReference(proxy->map()->prototype());
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001849 } else if (obj->IsJSObject()) {
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001850 JSObject* js_obj = JSObject::cast(obj);
1851 ExtractClosureReferences(js_obj, entry);
1852 ExtractPropertyReferences(js_obj, entry);
1853 ExtractElementReferences(js_obj, entry);
vegorov@chromium.org42841962010-10-18 11:18:59 +00001854 ExtractInternalReferences(js_obj, entry);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001855 SetPropertyReference(
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001856 obj, entry, heap_->Proto_symbol(), js_obj->GetPrototype());
vegorov@chromium.org42841962010-10-18 11:18:59 +00001857 if (obj->IsJSFunction()) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001858 JSFunction* js_fun = JSFunction::cast(js_obj);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001859 Object* proto_or_map = js_fun->prototype_or_initial_map();
1860 if (!proto_or_map->IsTheHole()) {
1861 if (!proto_or_map->IsMap()) {
1862 SetPropertyReference(
1863 obj, entry,
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001864 heap_->prototype_symbol(), proto_or_map,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001865 JSFunction::kPrototypeOrInitialMapOffset);
1866 } else {
1867 SetPropertyReference(
1868 obj, entry,
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001869 heap_->prototype_symbol(), js_fun->prototype());
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00001870 }
vegorov@chromium.org42841962010-10-18 11:18:59 +00001871 }
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001872 SetInternalReference(js_fun, entry,
1873 "shared", js_fun->shared(),
1874 JSFunction::kSharedFunctionInfoOffset);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001875 TagObject(js_fun->unchecked_context(), "(context)");
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001876 SetInternalReference(js_fun, entry,
1877 "context", js_fun->unchecked_context(),
1878 JSFunction::kContextOffset);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001879 TagObject(js_fun->literals(), "(function literals)");
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001880 SetInternalReference(js_fun, entry,
1881 "literals", js_fun->literals(),
1882 JSFunction::kLiteralsOffset);
vegorov@chromium.org42841962010-10-18 11:18:59 +00001883 }
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001884 TagObject(js_obj->properties(), "(object properties)");
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001885 SetInternalReference(obj, entry,
1886 "properties", js_obj->properties(),
1887 JSObject::kPropertiesOffset);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001888 TagObject(js_obj->elements(), "(object elements)");
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001889 SetInternalReference(obj, entry,
1890 "elements", js_obj->elements(),
1891 JSObject::kElementsOffset);
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001892 } else if (obj->IsString()) {
1893 if (obj->IsConsString()) {
1894 ConsString* cs = ConsString::cast(obj);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00001895 SetInternalReference(obj, entry, 1, cs->first());
1896 SetInternalReference(obj, entry, 2, cs->second());
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001897 }
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001898 extract_indexed_refs = false;
1899 } else if (obj->IsGlobalContext()) {
1900 Context* context = Context::cast(obj);
1901 TagObject(context->jsfunction_result_caches(),
1902 "(context func. result caches)");
1903 TagObject(context->normalized_map_cache(), "(context norm. map cache)");
1904 TagObject(context->runtime_context(), "(runtime context)");
1905 TagObject(context->map_cache(), "(context map cache)");
1906 TagObject(context->data(), "(context data)");
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001907 } else if (obj->IsMap()) {
1908 Map* map = Map::cast(obj);
1909 SetInternalReference(obj, entry,
1910 "prototype", map->prototype(), Map::kPrototypeOffset);
1911 SetInternalReference(obj, entry,
1912 "constructor", map->constructor(),
1913 Map::kConstructorOffset);
danno@chromium.org40cb8782011-05-25 07:58:50 +00001914 if (!map->instance_descriptors()->IsEmpty()) {
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001915 TagObject(map->instance_descriptors(), "(map descriptors)");
danno@chromium.org40cb8782011-05-25 07:58:50 +00001916 SetInternalReference(obj, entry,
1917 "descriptors", map->instance_descriptors(),
1918 Map::kInstanceDescriptorsOrBitField3Offset);
1919 }
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001920 SetInternalReference(obj, entry,
1921 "code_cache", map->code_cache(),
1922 Map::kCodeCacheOffset);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001923 } else if (obj->IsSharedFunctionInfo()) {
1924 SharedFunctionInfo* shared = SharedFunctionInfo::cast(obj);
1925 SetInternalReference(obj, entry,
1926 "name", shared->name(),
1927 SharedFunctionInfo::kNameOffset);
1928 SetInternalReference(obj, entry,
1929 "code", shared->unchecked_code(),
1930 SharedFunctionInfo::kCodeOffset);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001931 TagObject(shared->scope_info(), "(function scope info)");
1932 SetInternalReference(obj, entry,
1933 "scope_info", shared->scope_info(),
1934 SharedFunctionInfo::kScopeInfoOffset);
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001935 SetInternalReference(obj, entry,
1936 "instance_class_name", shared->instance_class_name(),
1937 SharedFunctionInfo::kInstanceClassNameOffset);
1938 SetInternalReference(obj, entry,
1939 "script", shared->script(),
1940 SharedFunctionInfo::kScriptOffset);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00001941 } else if (obj->IsScript()) {
1942 Script* script = Script::cast(obj);
1943 SetInternalReference(obj, entry,
1944 "source", script->source(),
1945 Script::kSourceOffset);
1946 SetInternalReference(obj, entry,
1947 "name", script->name(),
1948 Script::kNameOffset);
1949 SetInternalReference(obj, entry,
1950 "data", script->data(),
1951 Script::kDataOffset);
1952 SetInternalReference(obj, entry,
1953 "context_data", script->context_data(),
1954 Script::kContextOffset);
1955 TagObject(script->line_ends(), "(script line ends)");
1956 SetInternalReference(obj, entry,
1957 "line_ends", script->line_ends(),
1958 Script::kLineEndsOffset);
1959 } else if (obj->IsDescriptorArray()) {
1960 DescriptorArray* desc_array = DescriptorArray::cast(obj);
1961 if (desc_array->length() > DescriptorArray::kContentArrayIndex) {
1962 Object* content_array =
1963 desc_array->get(DescriptorArray::kContentArrayIndex);
1964 TagObject(content_array, "(map descriptor content)");
1965 SetInternalReference(obj, entry,
1966 "content", content_array,
1967 FixedArray::OffsetOfElementAt(
1968 DescriptorArray::kContentArrayIndex));
1969 }
1970 } else if (obj->IsCodeCache()) {
1971 CodeCache* code_cache = CodeCache::cast(obj);
1972 TagObject(code_cache->default_cache(), "(default code cache)");
1973 SetInternalReference(obj, entry,
1974 "default_cache", code_cache->default_cache(),
1975 CodeCache::kDefaultCacheOffset);
1976 TagObject(code_cache->normal_type_cache(), "(code type cache)");
1977 SetInternalReference(obj, entry,
1978 "type_cache", code_cache->normal_type_cache(),
1979 CodeCache::kNormalTypeCacheOffset);
1980 } else if (obj->IsCode()) {
1981 Code* code = Code::cast(obj);
1982 TagObject(code->unchecked_relocation_info(), "(code relocation info)");
1983 TagObject(code->unchecked_deoptimization_data(), "(code deopt data)");
1984 }
1985 if (extract_indexed_refs) {
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00001986 SetInternalReference(obj, entry, "map", obj->map(), HeapObject::kMapOffset);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00001987 IndexedReferencesExtractor refs_extractor(this, obj, entry);
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001988 obj->Iterate(&refs_extractor);
1989 }
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001990}
1991
1992
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00001993void V8HeapExplorer::ExtractClosureReferences(JSObject* js_obj,
1994 HeapEntry* entry) {
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00001995 if (js_obj->IsJSFunction()) {
1996 HandleScope hs;
1997 JSFunction* func = JSFunction::cast(js_obj);
1998 Context* context = func->context();
danno@chromium.org40cb8782011-05-25 07:58:50 +00001999 ZoneScope zscope(Isolate::Current(), DELETE_ON_EXIT);
ager@chromium.orgb5737492010-07-15 09:29:43 +00002000 SerializedScopeInfo* serialized_scope_info =
2001 context->closure()->shared()->scope_info();
2002 ScopeInfo<ZoneListAllocationPolicy> zone_scope_info(serialized_scope_info);
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002003 int locals_number = zone_scope_info.NumberOfLocals();
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002004 for (int i = 0; i < locals_number; ++i) {
ager@chromium.org6a2b0aa2010-07-13 20:58:03 +00002005 String* local_name = *zone_scope_info.LocalName(i);
ager@chromium.orgb5737492010-07-15 09:29:43 +00002006 int idx = serialized_scope_info->ContextSlotIndex(local_name, NULL);
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002007 if (idx >= 0 && idx < context->length()) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002008 SetClosureReference(js_obj, entry, local_name, context->get(idx));
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002009 }
2010 }
2011 }
2012}
2013
2014
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002015void V8HeapExplorer::ExtractPropertyReferences(JSObject* js_obj,
2016 HeapEntry* entry) {
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002017 if (js_obj->HasFastProperties()) {
2018 DescriptorArray* descs = js_obj->map()->instance_descriptors();
2019 for (int i = 0; i < descs->number_of_descriptors(); i++) {
2020 switch (descs->GetType(i)) {
2021 case FIELD: {
2022 int index = descs->GetFieldIndex(i);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002023 if (index < js_obj->map()->inobject_properties()) {
2024 SetPropertyReference(
2025 js_obj, entry,
2026 descs->GetKey(i), js_obj->InObjectPropertyAt(index),
2027 js_obj->GetInObjectPropertyOffset(index));
2028 } else {
2029 SetPropertyReference(
2030 js_obj, entry,
2031 descs->GetKey(i), js_obj->FastPropertyAt(index));
2032 }
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002033 break;
2034 }
2035 case CONSTANT_FUNCTION:
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002036 SetPropertyReference(
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002037 js_obj, entry,
2038 descs->GetKey(i), descs->GetConstantFunction(i));
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002039 break;
2040 default: ;
2041 }
2042 }
2043 } else {
2044 StringDictionary* dictionary = js_obj->property_dictionary();
2045 int length = dictionary->Capacity();
2046 for (int i = 0; i < length; ++i) {
2047 Object* k = dictionary->KeyAt(i);
2048 if (dictionary->IsKey(k)) {
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00002049 Object* target = dictionary->ValueAt(i);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002050 SetPropertyReference(
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00002051 js_obj, entry, String::cast(k), target);
2052 // We assume that global objects can only have slow properties.
2053 if (target->IsJSGlobalPropertyCell()) {
2054 SetPropertyShortcutReference(js_obj,
2055 entry,
2056 String::cast(k),
2057 JSGlobalPropertyCell::cast(
2058 target)->value());
2059 }
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002060 }
2061 }
2062 }
2063}
2064
2065
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002066void V8HeapExplorer::ExtractElementReferences(JSObject* js_obj,
2067 HeapEntry* entry) {
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002068 if (js_obj->HasFastElements()) {
2069 FixedArray* elements = FixedArray::cast(js_obj->elements());
2070 int length = js_obj->IsJSArray() ?
2071 Smi::cast(JSArray::cast(js_obj)->length())->value() :
2072 elements->length();
2073 for (int i = 0; i < length; ++i) {
2074 if (!elements->get(i)->IsTheHole()) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002075 SetElementReference(js_obj, entry, i, elements->get(i));
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002076 }
2077 }
2078 } else if (js_obj->HasDictionaryElements()) {
2079 NumberDictionary* dictionary = js_obj->element_dictionary();
2080 int length = dictionary->Capacity();
2081 for (int i = 0; i < length; ++i) {
2082 Object* k = dictionary->KeyAt(i);
2083 if (dictionary->IsKey(k)) {
2084 ASSERT(k->IsNumber());
2085 uint32_t index = static_cast<uint32_t>(k->Number());
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002086 SetElementReference(js_obj, entry, index, dictionary->ValueAt(i));
ager@chromium.org2cc82ae2010-06-14 07:35:38 +00002087 }
2088 }
2089 }
2090}
2091
ricow@chromium.org4980dff2010-07-19 08:33:45 +00002092
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002093void V8HeapExplorer::ExtractInternalReferences(JSObject* js_obj,
2094 HeapEntry* entry) {
vegorov@chromium.org42841962010-10-18 11:18:59 +00002095 int length = js_obj->GetInternalFieldCount();
2096 for (int i = 0; i < length; ++i) {
2097 Object* o = js_obj->GetInternalField(i);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002098 SetInternalReference(
2099 js_obj, entry, i, o, js_obj->GetInternalFieldOffset(i));
vegorov@chromium.org42841962010-10-18 11:18:59 +00002100 }
2101}
2102
2103
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002104HeapEntry* V8HeapExplorer::GetEntry(Object* obj) {
2105 if (!obj->IsHeapObject()) return NULL;
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002106 return filler_->FindOrAddEntry(obj, this);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002107}
2108
2109
2110class RootsReferencesExtractor : public ObjectVisitor {
2111 public:
2112 explicit RootsReferencesExtractor(V8HeapExplorer* explorer)
2113 : explorer_(explorer) {
2114 }
2115 void VisitPointers(Object** start, Object** end) {
2116 for (Object** p = start; p < end; p++) explorer_->SetGcRootsReference(*p);
2117 }
2118 private:
2119 V8HeapExplorer* explorer_;
2120};
2121
2122
2123bool V8HeapExplorer::IterateAndExtractReferences(
2124 SnapshotFillerInterface* filler) {
2125 filler_ = filler;
2126 HeapIterator iterator(HeapIterator::kFilterUnreachable);
2127 bool interrupted = false;
2128 // Heap iteration with filtering must be finished in any case.
2129 for (HeapObject* obj = iterator.next();
2130 obj != NULL;
2131 obj = iterator.next(), progress_->ProgressStep()) {
2132 if (!interrupted) {
2133 ExtractReferences(obj);
2134 if (!progress_->ProgressReport(false)) interrupted = true;
2135 }
2136 }
2137 if (interrupted) {
2138 filler_ = NULL;
2139 return false;
2140 }
2141 SetRootGcRootsReference();
2142 RootsReferencesExtractor extractor(this);
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002143 heap_->IterateRoots(&extractor, VISIT_ALL);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002144 filler_ = NULL;
2145 return progress_->ProgressReport(false);
2146}
2147
2148
2149void V8HeapExplorer::SetClosureReference(HeapObject* parent_obj,
2150 HeapEntry* parent_entry,
2151 String* reference_name,
2152 Object* child_obj) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002153 HeapEntry* child_entry = GetEntry(child_obj);
2154 if (child_entry != NULL) {
2155 filler_->SetNamedReference(HeapGraphEdge::kContextVariable,
2156 parent_obj,
2157 parent_entry,
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002158 collection_->names()->GetName(reference_name),
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002159 child_obj,
2160 child_entry);
2161 }
2162}
2163
2164
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002165void V8HeapExplorer::SetElementReference(HeapObject* parent_obj,
2166 HeapEntry* parent_entry,
2167 int index,
2168 Object* child_obj) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002169 HeapEntry* child_entry = GetEntry(child_obj);
2170 if (child_entry != NULL) {
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00002171 filler_->SetIndexedReference(HeapGraphEdge::kElement,
2172 parent_obj,
2173 parent_entry,
2174 index,
2175 child_obj,
2176 child_entry);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002177 }
2178}
2179
2180
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002181void V8HeapExplorer::SetInternalReference(HeapObject* parent_obj,
2182 HeapEntry* parent_entry,
2183 const char* reference_name,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002184 Object* child_obj,
2185 int field_offset) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002186 HeapEntry* child_entry = GetEntry(child_obj);
2187 if (child_entry != NULL) {
2188 filler_->SetNamedReference(HeapGraphEdge::kInternal,
2189 parent_obj,
2190 parent_entry,
2191 reference_name,
2192 child_obj,
2193 child_entry);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002194 IndexedReferencesExtractor::MarkVisitedField(parent_obj, field_offset);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002195 }
2196}
2197
2198
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002199void V8HeapExplorer::SetInternalReference(HeapObject* parent_obj,
2200 HeapEntry* parent_entry,
2201 int index,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002202 Object* child_obj,
2203 int field_offset) {
vegorov@chromium.org42841962010-10-18 11:18:59 +00002204 HeapEntry* child_entry = GetEntry(child_obj);
2205 if (child_entry != NULL) {
2206 filler_->SetNamedReference(HeapGraphEdge::kInternal,
2207 parent_obj,
2208 parent_entry,
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002209 collection_->names()->GetName(index),
vegorov@chromium.org42841962010-10-18 11:18:59 +00002210 child_obj,
2211 child_entry);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002212 IndexedReferencesExtractor::MarkVisitedField(parent_obj, field_offset);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00002213 }
2214}
2215
2216
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002217void V8HeapExplorer::SetHiddenReference(HeapObject* parent_obj,
2218 HeapEntry* parent_entry,
2219 int index,
2220 Object* child_obj) {
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00002221 HeapEntry* child_entry = GetEntry(child_obj);
2222 if (child_entry != NULL) {
2223 filler_->SetIndexedReference(HeapGraphEdge::kHidden,
2224 parent_obj,
2225 parent_entry,
2226 index,
2227 child_obj,
2228 child_entry);
vegorov@chromium.org42841962010-10-18 11:18:59 +00002229 }
2230}
2231
2232
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002233void V8HeapExplorer::SetPropertyReference(HeapObject* parent_obj,
2234 HeapEntry* parent_entry,
2235 String* reference_name,
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002236 Object* child_obj,
2237 int field_offset) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002238 HeapEntry* child_entry = GetEntry(child_obj);
2239 if (child_entry != NULL) {
vegorov@chromium.org42841962010-10-18 11:18:59 +00002240 HeapGraphEdge::Type type = reference_name->length() > 0 ?
2241 HeapGraphEdge::kProperty : HeapGraphEdge::kInternal;
2242 filler_->SetNamedReference(type,
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002243 parent_obj,
2244 parent_entry,
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002245 collection_->names()->GetName(reference_name),
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002246 child_obj,
2247 child_entry);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002248 IndexedReferencesExtractor::MarkVisitedField(parent_obj, field_offset);
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002249 }
2250}
2251
2252
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002253void V8HeapExplorer::SetPropertyShortcutReference(HeapObject* parent_obj,
2254 HeapEntry* parent_entry,
2255 String* reference_name,
2256 Object* child_obj) {
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00002257 HeapEntry* child_entry = GetEntry(child_obj);
2258 if (child_entry != NULL) {
2259 filler_->SetNamedReference(HeapGraphEdge::kShortcut,
2260 parent_obj,
2261 parent_entry,
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002262 collection_->names()->GetName(reference_name),
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00002263 child_obj,
2264 child_entry);
2265 }
2266}
2267
2268
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002269void V8HeapExplorer::SetRootGcRootsReference() {
2270 filler_->SetIndexedAutoIndexReference(
2271 HeapGraphEdge::kElement,
2272 kInternalRootObject, snapshot_->root(),
2273 kGcRootsObject, snapshot_->gc_roots());
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00002274}
2275
2276
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002277void V8HeapExplorer::SetRootShortcutReference(Object* child_obj) {
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002278 HeapEntry* child_entry = GetEntry(child_obj);
2279 ASSERT(child_entry != NULL);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002280 filler_->SetNamedAutoIndexReference(
2281 HeapGraphEdge::kShortcut,
2282 kInternalRootObject, snapshot_->root(),
2283 child_obj, child_entry);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00002284}
2285
2286
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002287void V8HeapExplorer::SetGcRootsReference(Object* child_obj) {
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00002288 HeapEntry* child_entry = GetEntry(child_obj);
2289 if (child_entry != NULL) {
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002290 filler_->SetIndexedAutoIndexReference(
2291 HeapGraphEdge::kElement,
2292 kGcRootsObject, snapshot_->gc_roots(),
2293 child_obj, child_entry);
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00002294 }
vegorov@chromium.org26c16f82010-08-11 13:41:03 +00002295}
2296
2297
ricow@chromium.org4f693d62011-07-04 14:01:31 +00002298void V8HeapExplorer::TagObject(Object* obj, const char* tag) {
2299 if (obj->IsHeapObject() &&
2300 !obj->IsOddball() &&
2301 obj != heap_->raw_unchecked_empty_byte_array() &&
2302 obj != heap_->raw_unchecked_empty_fixed_array() &&
2303 obj != heap_->raw_unchecked_empty_fixed_double_array() &&
2304 obj != heap_->raw_unchecked_empty_descriptor_array()) {
2305 objects_tags_.SetTag(obj, tag);
2306 }
2307}
2308
2309
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00002310class GlobalObjectsEnumerator : public ObjectVisitor {
2311 public:
2312 virtual void VisitPointers(Object** start, Object** end) {
2313 for (Object** p = start; p < end; p++) {
2314 if ((*p)->IsGlobalContext()) {
2315 Context* context = Context::cast(*p);
2316 JSObject* proxy = context->global_proxy();
2317 if (proxy->IsJSGlobalProxy()) {
2318 Object* global = proxy->map()->prototype();
2319 if (global->IsJSGlobalObject()) {
2320 objects_.Add(Handle<JSGlobalObject>(JSGlobalObject::cast(global)));
2321 }
2322 }
2323 }
2324 }
2325 }
2326 int count() { return objects_.length(); }
2327 Handle<JSGlobalObject>& at(int i) { return objects_[i]; }
2328
2329 private:
2330 List<Handle<JSGlobalObject> > objects_;
2331};
2332
2333
2334// Modifies heap. Must not be run during heap traversal.
2335void V8HeapExplorer::TagGlobalObjects() {
2336 Isolate* isolate = Isolate::Current();
2337 GlobalObjectsEnumerator enumerator;
2338 isolate->global_handles()->IterateAllRoots(&enumerator);
2339 Handle<String> document_string =
2340 isolate->factory()->NewStringFromAscii(CStrVector("document"));
2341 Handle<String> url_string =
2342 isolate->factory()->NewStringFromAscii(CStrVector("URL"));
2343 const char** urls = NewArray<const char*>(enumerator.count());
2344 for (int i = 0, l = enumerator.count(); i < l; ++i) {
2345 urls[i] = NULL;
2346 Handle<JSGlobalObject> global_obj = enumerator.at(i);
2347 Object* obj_document;
2348 if (global_obj->GetProperty(*document_string)->ToObject(&obj_document) &&
2349 obj_document->IsJSObject()) {
2350 JSObject* document = JSObject::cast(obj_document);
2351 Object* obj_url;
2352 if (document->GetProperty(*url_string)->ToObject(&obj_url) &&
2353 obj_url->IsString()) {
2354 urls[i] = collection_->names()->GetName(String::cast(obj_url));
2355 }
2356 }
2357 }
2358
2359 AssertNoAllocation no_allocation;
2360 for (int i = 0, l = enumerator.count(); i < l; ++i) {
2361 objects_tags_.SetTag(*enumerator.at(i), urls[i]);
2362 }
2363
2364 DeleteArray(urls);
2365}
2366
2367
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002368class GlobalHandlesExtractor : public ObjectVisitor {
2369 public:
2370 explicit GlobalHandlesExtractor(NativeObjectsExplorer* explorer)
2371 : explorer_(explorer) {}
2372 virtual ~GlobalHandlesExtractor() {}
2373 virtual void VisitPointers(Object** start, Object** end) {
2374 UNREACHABLE();
2375 }
2376 virtual void VisitEmbedderReference(Object** p, uint16_t class_id) {
2377 explorer_->VisitSubtreeWrapper(p, class_id);
2378 }
2379 private:
2380 NativeObjectsExplorer* explorer_;
2381};
2382
2383HeapThing const NativeObjectsExplorer::kNativesRootObject =
2384 reinterpret_cast<HeapThing>(
2385 static_cast<intptr_t>(HeapObjectsMap::kNativesRootObjectId));
2386
2387
2388NativeObjectsExplorer::NativeObjectsExplorer(
2389 HeapSnapshot* snapshot, SnapshottingProgressReportingInterface* progress)
2390 : snapshot_(snapshot),
2391 collection_(snapshot_->collection()),
2392 progress_(progress),
2393 embedder_queried_(false),
2394 objects_by_info_(RetainedInfosMatch),
2395 filler_(NULL) {
2396}
2397
2398
2399NativeObjectsExplorer::~NativeObjectsExplorer() {
2400 for (HashMap::Entry* p = objects_by_info_.Start();
2401 p != NULL;
2402 p = objects_by_info_.Next(p)) {
2403 v8::RetainedObjectInfo* info =
2404 reinterpret_cast<v8::RetainedObjectInfo*>(p->key);
2405 info->Dispose();
2406 List<HeapObject*>* objects =
2407 reinterpret_cast<List<HeapObject*>* >(p->value);
2408 delete objects;
2409 }
2410}
2411
2412
2413HeapEntry* NativeObjectsExplorer::AllocateEntry(
2414 HeapThing ptr, int children_count, int retainers_count) {
2415 if (ptr == kNativesRootObject) {
2416 return snapshot_->AddNativesRootEntry(children_count, retainers_count);
2417 } else {
2418 v8::RetainedObjectInfo* info =
2419 reinterpret_cast<v8::RetainedObjectInfo*>(ptr);
2420 intptr_t elements = info->GetElementCount();
2421 intptr_t size = info->GetSizeInBytes();
2422 return snapshot_->AddEntry(
2423 HeapEntry::kNative,
2424 elements != -1 ?
2425 collection_->names()->GetFormatted(
2426 "%s / %" V8_PTR_PREFIX "d entries",
2427 info->GetLabel(),
2428 info->GetElementCount()) :
2429 collection_->names()->GetCopy(info->GetLabel()),
2430 HeapObjectsMap::GenerateId(info),
2431 size != -1 ? static_cast<int>(size) : 0,
2432 children_count,
2433 retainers_count);
2434 }
2435}
2436
2437
2438void NativeObjectsExplorer::AddRootEntries(SnapshotFillerInterface* filler) {
2439 if (EstimateObjectsCount() <= 0) return;
2440 filler->AddEntry(kNativesRootObject, this);
2441}
2442
2443
2444int NativeObjectsExplorer::EstimateObjectsCount() {
2445 FillRetainedObjects();
2446 return objects_by_info_.occupancy();
2447}
2448
2449
2450void NativeObjectsExplorer::FillRetainedObjects() {
2451 if (embedder_queried_) return;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002452 Isolate* isolate = Isolate::Current();
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002453 // Record objects that are joined into ObjectGroups.
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002454 isolate->heap()->CallGlobalGCPrologueCallback();
2455 List<ObjectGroup*>* groups = isolate->global_handles()->object_groups();
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002456 for (int i = 0; i < groups->length(); ++i) {
2457 ObjectGroup* group = groups->at(i);
2458 if (group->info_ == NULL) continue;
2459 List<HeapObject*>* list = GetListMaybeDisposeInfo(group->info_);
karlklose@chromium.org44bc7082011-04-11 12:33:05 +00002460 for (size_t j = 0; j < group->length_; ++j) {
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002461 HeapObject* obj = HeapObject::cast(*group->objects_[j]);
2462 list->Add(obj);
2463 in_groups_.Insert(obj);
2464 }
2465 group->info_ = NULL; // Acquire info object ownership.
2466 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002467 isolate->global_handles()->RemoveObjectGroups();
2468 isolate->heap()->CallGlobalGCEpilogueCallback();
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002469 // Record objects that are not in ObjectGroups, but have class ID.
2470 GlobalHandlesExtractor extractor(this);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002471 isolate->global_handles()->IterateAllRootsWithClassIds(&extractor);
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002472 embedder_queried_ = true;
2473}
2474
2475
2476List<HeapObject*>* NativeObjectsExplorer::GetListMaybeDisposeInfo(
2477 v8::RetainedObjectInfo* info) {
2478 HashMap::Entry* entry =
2479 objects_by_info_.Lookup(info, InfoHash(info), true);
2480 if (entry->value != NULL) {
2481 info->Dispose();
2482 } else {
2483 entry->value = new List<HeapObject*>(4);
2484 }
2485 return reinterpret_cast<List<HeapObject*>* >(entry->value);
2486}
2487
2488
2489bool NativeObjectsExplorer::IterateAndExtractReferences(
2490 SnapshotFillerInterface* filler) {
2491 if (EstimateObjectsCount() <= 0) return true;
2492 filler_ = filler;
2493 FillRetainedObjects();
2494 for (HashMap::Entry* p = objects_by_info_.Start();
2495 p != NULL;
2496 p = objects_by_info_.Next(p)) {
2497 v8::RetainedObjectInfo* info =
2498 reinterpret_cast<v8::RetainedObjectInfo*>(p->key);
2499 SetNativeRootReference(info);
2500 List<HeapObject*>* objects =
2501 reinterpret_cast<List<HeapObject*>* >(p->value);
2502 for (int i = 0; i < objects->length(); ++i) {
2503 SetWrapperNativeReferences(objects->at(i), info);
2504 }
2505 }
2506 SetRootNativesRootReference();
2507 filler_ = NULL;
2508 return true;
2509}
2510
2511
2512void NativeObjectsExplorer::SetNativeRootReference(
2513 v8::RetainedObjectInfo* info) {
2514 HeapEntry* child_entry = filler_->FindOrAddEntry(info, this);
2515 ASSERT(child_entry != NULL);
2516 filler_->SetIndexedAutoIndexReference(
2517 HeapGraphEdge::kElement,
2518 kNativesRootObject, snapshot_->natives_root(),
2519 info, child_entry);
2520}
2521
2522
2523void NativeObjectsExplorer::SetWrapperNativeReferences(
2524 HeapObject* wrapper, v8::RetainedObjectInfo* info) {
2525 HeapEntry* wrapper_entry = filler_->FindEntry(wrapper);
2526 ASSERT(wrapper_entry != NULL);
2527 HeapEntry* info_entry = filler_->FindOrAddEntry(info, this);
2528 ASSERT(info_entry != NULL);
2529 filler_->SetNamedReference(HeapGraphEdge::kInternal,
2530 wrapper, wrapper_entry,
kmillikin@chromium.orgc36ce6e2011-04-04 08:25:31 +00002531 "native",
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002532 info, info_entry);
2533 filler_->SetIndexedAutoIndexReference(HeapGraphEdge::kElement,
2534 info, info_entry,
2535 wrapper, wrapper_entry);
2536}
2537
2538
2539void NativeObjectsExplorer::SetRootNativesRootReference() {
2540 filler_->SetIndexedAutoIndexReference(
2541 HeapGraphEdge::kElement,
2542 V8HeapExplorer::kInternalRootObject, snapshot_->root(),
2543 kNativesRootObject, snapshot_->natives_root());
2544}
2545
2546
2547void NativeObjectsExplorer::VisitSubtreeWrapper(Object** p, uint16_t class_id) {
2548 if (in_groups_.Contains(*p)) return;
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002549 Isolate* isolate = Isolate::Current();
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002550 v8::RetainedObjectInfo* info =
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00002551 isolate->heap_profiler()->ExecuteWrapperClassCallback(class_id, p);
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002552 if (info == NULL) return;
2553 GetListMaybeDisposeInfo(info)->Add(HeapObject::cast(*p));
2554}
2555
2556
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002557HeapSnapshotGenerator::HeapSnapshotGenerator(HeapSnapshot* snapshot,
2558 v8::ActivityControl* control)
2559 : snapshot_(snapshot),
2560 control_(control),
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002561 v8_heap_explorer_(snapshot_, this),
2562 dom_explorer_(snapshot_, this) {
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002563}
2564
2565
2566class SnapshotCounter : public SnapshotFillerInterface {
2567 public:
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002568 explicit SnapshotCounter(HeapEntriesMap* entries) : entries_(entries) { }
2569 HeapEntry* AddEntry(HeapThing ptr, HeapEntriesAllocator* allocator) {
2570 entries_->Pair(ptr, allocator, HeapEntriesMap::kHeapEntryPlaceholder);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002571 return HeapEntriesMap::kHeapEntryPlaceholder;
2572 }
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002573 HeapEntry* FindEntry(HeapThing ptr) {
2574 return entries_->Map(ptr);
2575 }
2576 HeapEntry* FindOrAddEntry(HeapThing ptr, HeapEntriesAllocator* allocator) {
2577 HeapEntry* entry = FindEntry(ptr);
2578 return entry != NULL ? entry : AddEntry(ptr, allocator);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002579 }
2580 void SetIndexedReference(HeapGraphEdge::Type,
2581 HeapThing parent_ptr,
2582 HeapEntry*,
2583 int,
2584 HeapThing child_ptr,
2585 HeapEntry*) {
2586 entries_->CountReference(parent_ptr, child_ptr);
2587 }
2588 void SetIndexedAutoIndexReference(HeapGraphEdge::Type,
2589 HeapThing parent_ptr,
2590 HeapEntry*,
2591 HeapThing child_ptr,
2592 HeapEntry*) {
2593 entries_->CountReference(parent_ptr, child_ptr);
2594 }
2595 void SetNamedReference(HeapGraphEdge::Type,
2596 HeapThing parent_ptr,
2597 HeapEntry*,
2598 const char*,
2599 HeapThing child_ptr,
2600 HeapEntry*) {
2601 entries_->CountReference(parent_ptr, child_ptr);
2602 }
2603 void SetNamedAutoIndexReference(HeapGraphEdge::Type,
2604 HeapThing parent_ptr,
2605 HeapEntry*,
2606 HeapThing child_ptr,
2607 HeapEntry*) {
2608 entries_->CountReference(parent_ptr, child_ptr);
2609 }
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00002610
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002611 private:
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002612 HeapEntriesMap* entries_;
2613};
2614
2615
2616class SnapshotFiller : public SnapshotFillerInterface {
2617 public:
2618 explicit SnapshotFiller(HeapSnapshot* snapshot, HeapEntriesMap* entries)
2619 : snapshot_(snapshot),
2620 collection_(snapshot->collection()),
2621 entries_(entries) { }
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002622 HeapEntry* AddEntry(HeapThing ptr, HeapEntriesAllocator* allocator) {
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002623 UNREACHABLE();
2624 return NULL;
2625 }
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002626 HeapEntry* FindEntry(HeapThing ptr) {
2627 return entries_->Map(ptr);
2628 }
2629 HeapEntry* FindOrAddEntry(HeapThing ptr, HeapEntriesAllocator* allocator) {
2630 HeapEntry* entry = FindEntry(ptr);
2631 return entry != NULL ? entry : AddEntry(ptr, allocator);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002632 }
2633 void SetIndexedReference(HeapGraphEdge::Type type,
2634 HeapThing parent_ptr,
2635 HeapEntry* parent_entry,
2636 int index,
2637 HeapThing child_ptr,
2638 HeapEntry* child_entry) {
2639 int child_index, retainer_index;
2640 entries_->CountReference(
2641 parent_ptr, child_ptr, &child_index, &retainer_index);
2642 parent_entry->SetIndexedReference(
2643 type, child_index, index, child_entry, retainer_index);
2644 }
2645 void SetIndexedAutoIndexReference(HeapGraphEdge::Type type,
2646 HeapThing parent_ptr,
2647 HeapEntry* parent_entry,
2648 HeapThing child_ptr,
2649 HeapEntry* child_entry) {
2650 int child_index, retainer_index;
2651 entries_->CountReference(
2652 parent_ptr, child_ptr, &child_index, &retainer_index);
2653 parent_entry->SetIndexedReference(
2654 type, child_index, child_index + 1, child_entry, retainer_index);
2655 }
2656 void SetNamedReference(HeapGraphEdge::Type type,
2657 HeapThing parent_ptr,
2658 HeapEntry* parent_entry,
2659 const char* reference_name,
2660 HeapThing child_ptr,
2661 HeapEntry* child_entry) {
2662 int child_index, retainer_index;
2663 entries_->CountReference(
2664 parent_ptr, child_ptr, &child_index, &retainer_index);
2665 parent_entry->SetNamedReference(
2666 type, child_index, reference_name, child_entry, retainer_index);
2667 }
2668 void SetNamedAutoIndexReference(HeapGraphEdge::Type type,
2669 HeapThing parent_ptr,
2670 HeapEntry* parent_entry,
2671 HeapThing child_ptr,
2672 HeapEntry* child_entry) {
2673 int child_index, retainer_index;
2674 entries_->CountReference(
2675 parent_ptr, child_ptr, &child_index, &retainer_index);
2676 parent_entry->SetNamedReference(type,
2677 child_index,
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002678 collection_->names()->GetName(child_index + 1),
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002679 child_entry,
2680 retainer_index);
2681 }
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00002682
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002683 private:
2684 HeapSnapshot* snapshot_;
2685 HeapSnapshotsCollection* collection_;
2686 HeapEntriesMap* entries_;
2687};
2688
2689
2690bool HeapSnapshotGenerator::GenerateSnapshot() {
ricow@chromium.orgd2be9012011-06-01 06:00:58 +00002691 v8_heap_explorer_.TagGlobalObjects();
2692
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002693 AssertNoAllocation no_alloc;
2694
2695 SetProgressTotal(4); // 2 passes + dominators + sizes.
2696
2697 // Pass 1. Iterate heap contents to count entries and references.
2698 if (!CountEntriesAndReferences()) return false;
2699
2700 // Allocate and fill entries in the snapshot, allocate references.
2701 snapshot_->AllocateEntries(entries_.entries_count(),
2702 entries_.total_children_count(),
2703 entries_.total_retainers_count());
2704 entries_.AllocateEntries();
2705
2706 // Pass 2. Fill references.
2707 if (!FillReferences()) return false;
2708
2709 if (!SetEntriesDominators()) return false;
2710 if (!ApproximateRetainedSizes()) return false;
2711
2712 progress_counter_ = progress_total_;
2713 if (!ProgressReport(true)) return false;
2714 return true;
2715}
2716
2717
2718void HeapSnapshotGenerator::ProgressStep() {
2719 ++progress_counter_;
2720}
2721
2722
2723bool HeapSnapshotGenerator::ProgressReport(bool force) {
2724 const int kProgressReportGranularity = 10000;
2725 if (control_ != NULL
2726 && (force || progress_counter_ % kProgressReportGranularity == 0)) {
2727 return
2728 control_->ReportProgressValue(progress_counter_, progress_total_) ==
2729 v8::ActivityControl::kContinue;
2730 }
2731 return true;
2732}
2733
2734
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002735void HeapSnapshotGenerator::SetProgressTotal(int iterations_count) {
2736 if (control_ == NULL) return;
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002737 progress_total_ = (
2738 v8_heap_explorer_.EstimateObjectsCount() +
2739 dom_explorer_.EstimateObjectsCount()) * iterations_count;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002740 progress_counter_ = 0;
2741}
2742
2743
2744bool HeapSnapshotGenerator::CountEntriesAndReferences() {
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002745 SnapshotCounter counter(&entries_);
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002746 v8_heap_explorer_.AddRootEntries(&counter);
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002747 dom_explorer_.AddRootEntries(&counter);
2748 return
2749 v8_heap_explorer_.IterateAndExtractReferences(&counter) &&
2750 dom_explorer_.IterateAndExtractReferences(&counter);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002751}
2752
2753
2754bool HeapSnapshotGenerator::FillReferences() {
2755 SnapshotFiller filler(snapshot_, &entries_);
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00002756 return
2757 v8_heap_explorer_.IterateAndExtractReferences(&filler) &&
2758 dom_explorer_.IterateAndExtractReferences(&filler);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002759}
2760
2761
2762void HeapSnapshotGenerator::FillReversePostorderIndexes(
2763 Vector<HeapEntry*>* entries) {
2764 snapshot_->ClearPaint();
2765 int current_entry = 0;
2766 List<HeapEntry*> nodes_to_visit;
2767 nodes_to_visit.Add(snapshot_->root());
2768 snapshot_->root()->paint_reachable();
2769 while (!nodes_to_visit.is_empty()) {
2770 HeapEntry* entry = nodes_to_visit.last();
2771 Vector<HeapGraphEdge> children = entry->children();
2772 bool has_new_edges = false;
2773 for (int i = 0; i < children.length(); ++i) {
2774 if (children[i].type() == HeapGraphEdge::kShortcut) continue;
2775 HeapEntry* child = children[i].to();
2776 if (!child->painted_reachable()) {
2777 nodes_to_visit.Add(child);
2778 child->paint_reachable();
2779 has_new_edges = true;
2780 }
2781 }
2782 if (!has_new_edges) {
2783 entry->set_ordered_index(current_entry);
2784 (*entries)[current_entry++] = entry;
2785 nodes_to_visit.RemoveLast();
2786 }
2787 }
2788 entries->Truncate(current_entry);
2789}
2790
2791
2792static int Intersect(int i1, int i2, const Vector<HeapEntry*>& dominators) {
2793 int finger1 = i1, finger2 = i2;
2794 while (finger1 != finger2) {
2795 while (finger1 < finger2) finger1 = dominators[finger1]->ordered_index();
2796 while (finger2 < finger1) finger2 = dominators[finger2]->ordered_index();
2797 }
2798 return finger1;
2799}
2800
2801// The algorithm is based on the article:
2802// K. Cooper, T. Harvey and K. Kennedy "A Simple, Fast Dominance Algorithm"
sgjesse@chromium.org496c03a2011-02-14 12:05:43 +00002803// Softw. Pract. Exper. 4 (2001), pp. 1-10.
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002804bool HeapSnapshotGenerator::BuildDominatorTree(
2805 const Vector<HeapEntry*>& entries,
2806 Vector<HeapEntry*>* dominators) {
2807 if (entries.length() == 0) return true;
2808 const int entries_length = entries.length(), root_index = entries_length - 1;
2809 for (int i = 0; i < root_index; ++i) (*dominators)[i] = NULL;
2810 (*dominators)[root_index] = entries[root_index];
2811 int changed = 1;
2812 const int base_progress_counter = progress_counter_;
2813 while (changed != 0) {
2814 changed = 0;
2815 for (int i = root_index - 1; i >= 0; --i) {
2816 HeapEntry* new_idom = NULL;
2817 Vector<HeapGraphEdge*> rets = entries[i]->retainers();
2818 int j = 0;
2819 for (; j < rets.length(); ++j) {
2820 if (rets[j]->type() == HeapGraphEdge::kShortcut) continue;
2821 HeapEntry* ret = rets[j]->From();
2822 if (dominators->at(ret->ordered_index()) != NULL) {
2823 new_idom = ret;
2824 break;
2825 }
2826 }
2827 for (++j; j < rets.length(); ++j) {
2828 if (rets[j]->type() == HeapGraphEdge::kShortcut) continue;
2829 HeapEntry* ret = rets[j]->From();
2830 if (dominators->at(ret->ordered_index()) != NULL) {
2831 new_idom = entries[Intersect(ret->ordered_index(),
2832 new_idom->ordered_index(),
2833 *dominators)];
2834 }
2835 }
2836 if (new_idom != NULL && dominators->at(i) != new_idom) {
2837 (*dominators)[i] = new_idom;
2838 ++changed;
2839 }
2840 }
2841 int remaining = entries_length - changed;
2842 if (remaining < 0) remaining = 0;
2843 progress_counter_ = base_progress_counter + remaining;
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002844 if (!ProgressReport(true)) return false;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002845 }
2846 return true;
2847}
2848
2849
2850bool HeapSnapshotGenerator::SetEntriesDominators() {
2851 // This array is used for maintaining reverse postorder of nodes.
2852 ScopedVector<HeapEntry*> ordered_entries(snapshot_->entries()->length());
2853 FillReversePostorderIndexes(&ordered_entries);
2854 ScopedVector<HeapEntry*> dominators(ordered_entries.length());
2855 if (!BuildDominatorTree(ordered_entries, &dominators)) return false;
2856 for (int i = 0; i < ordered_entries.length(); ++i) {
2857 ASSERT(dominators[i] != NULL);
2858 ordered_entries[i]->set_dominator(dominators[i]);
2859 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002860 return true;
2861}
2862
2863
2864bool HeapSnapshotGenerator::ApproximateRetainedSizes() {
2865 // As for the dominators tree we only know parent nodes, not
2866 // children, to sum up total sizes we "bubble" node's self size
2867 // adding it to all of its parents.
2868 for (int i = 0; i < snapshot_->entries()->length(); ++i) {
2869 HeapEntry* entry = snapshot_->entries()->at(i);
2870 entry->set_retained_size(entry->self_size());
2871 }
2872 for (int i = 0;
2873 i < snapshot_->entries()->length();
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002874 ++i, ProgressStep()) {
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002875 HeapEntry* entry = snapshot_->entries()->at(i);
2876 int entry_size = entry->self_size();
2877 for (HeapEntry* dominator = entry->dominator();
2878 dominator != entry;
2879 entry = dominator, dominator = entry->dominator()) {
2880 dominator->add_retained_size(entry_size);
2881 }
ager@chromium.org9ee27ae2011-03-02 13:43:26 +00002882 if (!ProgressReport()) return false;
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00002883 }
2884 return true;
2885}
2886
2887
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00002888class OutputStreamWriter {
2889 public:
2890 explicit OutputStreamWriter(v8::OutputStream* stream)
2891 : stream_(stream),
2892 chunk_size_(stream->GetChunkSize()),
2893 chunk_(chunk_size_),
2894 chunk_pos_(0),
2895 aborted_(false) {
2896 ASSERT(chunk_size_ > 0);
2897 }
2898 bool aborted() { return aborted_; }
2899 void AddCharacter(char c) {
2900 ASSERT(c != '\0');
2901 ASSERT(chunk_pos_ < chunk_size_);
2902 chunk_[chunk_pos_++] = c;
2903 MaybeWriteChunk();
2904 }
2905 void AddString(const char* s) {
2906 AddSubstring(s, StrLength(s));
2907 }
2908 void AddSubstring(const char* s, int n) {
2909 if (n <= 0) return;
2910 ASSERT(static_cast<size_t>(n) <= strlen(s));
2911 const char* s_end = s + n;
2912 while (s < s_end) {
2913 int s_chunk_size = Min(
2914 chunk_size_ - chunk_pos_, static_cast<int>(s_end - s));
2915 ASSERT(s_chunk_size > 0);
2916 memcpy(chunk_.start() + chunk_pos_, s, s_chunk_size);
2917 s += s_chunk_size;
2918 chunk_pos_ += s_chunk_size;
2919 MaybeWriteChunk();
2920 }
2921 }
2922 void AddNumber(int n) { AddNumberImpl<int>(n, "%d"); }
2923 void AddNumber(unsigned n) { AddNumberImpl<unsigned>(n, "%u"); }
2924 void AddNumber(uint64_t n) { AddNumberImpl<uint64_t>(n, "%llu"); }
2925 void Finalize() {
2926 if (aborted_) return;
2927 ASSERT(chunk_pos_ < chunk_size_);
2928 if (chunk_pos_ != 0) {
2929 WriteChunk();
2930 }
2931 stream_->EndOfStream();
2932 }
2933
2934 private:
2935 template<typename T>
2936 void AddNumberImpl(T n, const char* format) {
2937 ScopedVector<char> buffer(32);
2938 int result = OS::SNPrintF(buffer, format, n);
2939 USE(result);
2940 ASSERT(result != -1);
2941 AddString(buffer.start());
2942 }
2943 void MaybeWriteChunk() {
2944 ASSERT(chunk_pos_ <= chunk_size_);
2945 if (chunk_pos_ == chunk_size_) {
2946 WriteChunk();
2947 chunk_pos_ = 0;
2948 }
2949 }
2950 void WriteChunk() {
2951 if (aborted_) return;
2952 if (stream_->WriteAsciiChunk(chunk_.start(), chunk_pos_) ==
2953 v8::OutputStream::kAbort) aborted_ = true;
2954 }
2955
2956 v8::OutputStream* stream_;
2957 int chunk_size_;
2958 ScopedVector<char> chunk_;
2959 int chunk_pos_;
2960 bool aborted_;
2961};
2962
2963void HeapSnapshotJSONSerializer::Serialize(v8::OutputStream* stream) {
2964 ASSERT(writer_ == NULL);
2965 writer_ = new OutputStreamWriter(stream);
2966
2967 // Since nodes graph is cyclic, we need the first pass to enumerate
2968 // them. Strings can be serialized in one pass.
2969 EnumerateNodes();
2970 SerializeImpl();
2971
2972 delete writer_;
2973 writer_ = NULL;
2974}
2975
2976
2977void HeapSnapshotJSONSerializer::SerializeImpl() {
2978 writer_->AddCharacter('{');
2979 writer_->AddString("\"snapshot\":{");
2980 SerializeSnapshot();
2981 if (writer_->aborted()) return;
2982 writer_->AddString("},\n");
2983 writer_->AddString("\"nodes\":[");
2984 SerializeNodes();
2985 if (writer_->aborted()) return;
2986 writer_->AddString("],\n");
2987 writer_->AddString("\"strings\":[");
2988 SerializeStrings();
2989 if (writer_->aborted()) return;
2990 writer_->AddCharacter(']');
2991 writer_->AddCharacter('}');
2992 writer_->Finalize();
2993}
2994
2995
2996class HeapSnapshotJSONSerializerEnumerator {
2997 public:
2998 explicit HeapSnapshotJSONSerializerEnumerator(HeapSnapshotJSONSerializer* s)
2999 : s_(s) {
3000 }
3001 void Apply(HeapEntry** entry) {
3002 s_->GetNodeId(*entry);
3003 }
3004 private:
3005 HeapSnapshotJSONSerializer* s_;
3006};
3007
3008void HeapSnapshotJSONSerializer::EnumerateNodes() {
3009 GetNodeId(snapshot_->root()); // Make sure root gets the first id.
3010 HeapSnapshotJSONSerializerEnumerator iter(this);
3011 snapshot_->IterateEntries(&iter);
3012}
3013
3014
3015int HeapSnapshotJSONSerializer::GetNodeId(HeapEntry* entry) {
3016 HashMap::Entry* cache_entry = nodes_.Lookup(entry, ObjectHash(entry), true);
3017 if (cache_entry->value == NULL) {
3018 cache_entry->value = reinterpret_cast<void*>(next_node_id_++);
3019 }
3020 return static_cast<int>(reinterpret_cast<intptr_t>(cache_entry->value));
3021}
3022
3023
3024int HeapSnapshotJSONSerializer::GetStringId(const char* s) {
3025 HashMap::Entry* cache_entry = strings_.Lookup(
3026 const_cast<char*>(s), ObjectHash(s), true);
3027 if (cache_entry->value == NULL) {
3028 cache_entry->value = reinterpret_cast<void*>(next_string_id_++);
3029 }
3030 return static_cast<int>(reinterpret_cast<intptr_t>(cache_entry->value));
3031}
3032
3033
3034void HeapSnapshotJSONSerializer::SerializeEdge(HeapGraphEdge* edge) {
3035 writer_->AddCharacter(',');
3036 writer_->AddNumber(edge->type());
3037 writer_->AddCharacter(',');
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003038 if (edge->type() == HeapGraphEdge::kElement
3039 || edge->type() == HeapGraphEdge::kHidden) {
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003040 writer_->AddNumber(edge->index());
3041 } else {
3042 writer_->AddNumber(GetStringId(edge->name()));
3043 }
3044 writer_->AddCharacter(',');
3045 writer_->AddNumber(GetNodeId(edge->to()));
3046}
3047
3048
3049void HeapSnapshotJSONSerializer::SerializeNode(HeapEntry* entry) {
3050 writer_->AddCharacter('\n');
3051 writer_->AddCharacter(',');
3052 writer_->AddNumber(entry->type());
3053 writer_->AddCharacter(',');
3054 writer_->AddNumber(GetStringId(entry->name()));
3055 writer_->AddCharacter(',');
3056 writer_->AddNumber(entry->id());
3057 writer_->AddCharacter(',');
3058 writer_->AddNumber(entry->self_size());
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003059 writer_->AddCharacter(',');
3060 writer_->AddNumber(entry->RetainedSize(false));
3061 writer_->AddCharacter(',');
3062 writer_->AddNumber(GetNodeId(entry->dominator()));
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003063 Vector<HeapGraphEdge> children = entry->children();
3064 writer_->AddCharacter(',');
3065 writer_->AddNumber(children.length());
3066 for (int i = 0; i < children.length(); ++i) {
3067 SerializeEdge(&children[i]);
3068 if (writer_->aborted()) return;
3069 }
3070}
3071
3072
3073void HeapSnapshotJSONSerializer::SerializeNodes() {
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003074 // The first (zero) item of nodes array is an object describing node
3075 // serialization layout. We use a set of macros to improve
3076 // readability.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003077#define JSON_A(s) "["s"]"
3078#define JSON_O(s) "{"s"}"
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003079#define JSON_S(s) "\""s"\""
3080 writer_->AddString(JSON_O(
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003081 JSON_S("fields") ":" JSON_A(
3082 JSON_S("type")
3083 "," JSON_S("name")
3084 "," JSON_S("id")
3085 "," JSON_S("self_size")
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003086 "," JSON_S("retained_size")
3087 "," JSON_S("dominator")
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003088 "," JSON_S("children_count")
3089 "," JSON_S("children"))
3090 "," JSON_S("types") ":" JSON_A(
3091 JSON_A(
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003092 JSON_S("hidden")
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003093 "," JSON_S("array")
3094 "," JSON_S("string")
3095 "," JSON_S("object")
3096 "," JSON_S("code")
vegorov@chromium.org42841962010-10-18 11:18:59 +00003097 "," JSON_S("closure")
3098 "," JSON_S("regexp")
whesse@chromium.orgb08986c2011-03-14 16:13:42 +00003099 "," JSON_S("number")
3100 "," JSON_S("native"))
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003101 "," JSON_S("string")
3102 "," JSON_S("number")
3103 "," JSON_S("number")
3104 "," JSON_S("number")
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003105 "," JSON_S("number")
3106 "," JSON_S("number")
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003107 "," JSON_O(
3108 JSON_S("fields") ":" JSON_A(
3109 JSON_S("type")
3110 "," JSON_S("name_or_index")
3111 "," JSON_S("to_node"))
3112 "," JSON_S("types") ":" JSON_A(
3113 JSON_A(
3114 JSON_S("context")
3115 "," JSON_S("element")
3116 "," JSON_S("property")
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003117 "," JSON_S("internal")
3118 "," JSON_S("hidden")
3119 "," JSON_S("shortcut"))
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003120 "," JSON_S("string_or_number")
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003121 "," JSON_S("node"))))));
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003122#undef JSON_S
3123#undef JSON_O
3124#undef JSON_A
3125
vegorov@chromium.org21b5e952010-11-23 10:24:40 +00003126 const int node_fields_count = 7;
3127 // type,name,id,self_size,retained_size,dominator,children_count.
erik.corry@gmail.comd88afa22010-09-15 12:33:05 +00003128 const int edge_fields_count = 3; // type,name|index,to_node.
3129 List<HashMap::Entry*> sorted_nodes;
3130 SortHashMap(&nodes_, &sorted_nodes);
3131 // Rewrite node ids, so they refer to actual array positions.
3132 if (sorted_nodes.length() > 1) {
3133 // Nodes start from array index 1.
3134 int prev_value = 1;
3135 sorted_nodes[0]->value = reinterpret_cast<void*>(prev_value);
3136 for (int i = 1; i < sorted_nodes.length(); ++i) {
3137 HeapEntry* prev_heap_entry =
3138 reinterpret_cast<HeapEntry*>(sorted_nodes[i-1]->key);
3139 prev_value += node_fields_count +
3140 prev_heap_entry->children().length() * edge_fields_count;
3141 sorted_nodes[i]->value = reinterpret_cast<void*>(prev_value);
3142 }
3143 }
3144 for (int i = 0; i < sorted_nodes.length(); ++i) {
3145 SerializeNode(reinterpret_cast<HeapEntry*>(sorted_nodes[i]->key));
3146 if (writer_->aborted()) return;
3147 }
3148}
3149
3150
3151void HeapSnapshotJSONSerializer::SerializeSnapshot() {
3152 writer_->AddString("\"title\":\"");
3153 writer_->AddString(snapshot_->title());
3154 writer_->AddString("\"");
3155 writer_->AddString(",\"uid\":");
3156 writer_->AddNumber(snapshot_->uid());
3157}
3158
3159
3160static void WriteUChar(OutputStreamWriter* w, unibrow::uchar u) {
3161 static const char hex_chars[] = "0123456789ABCDEF";
3162 w->AddString("\\u");
3163 w->AddCharacter(hex_chars[(u >> 12) & 0xf]);
3164 w->AddCharacter(hex_chars[(u >> 8) & 0xf]);
3165 w->AddCharacter(hex_chars[(u >> 4) & 0xf]);
3166 w->AddCharacter(hex_chars[u & 0xf]);
3167}
3168
3169void HeapSnapshotJSONSerializer::SerializeString(const unsigned char* s) {
3170 writer_->AddCharacter('\n');
3171 writer_->AddCharacter('\"');
3172 for ( ; *s != '\0'; ++s) {
3173 switch (*s) {
3174 case '\b':
3175 writer_->AddString("\\b");
3176 continue;
3177 case '\f':
3178 writer_->AddString("\\f");
3179 continue;
3180 case '\n':
3181 writer_->AddString("\\n");
3182 continue;
3183 case '\r':
3184 writer_->AddString("\\r");
3185 continue;
3186 case '\t':
3187 writer_->AddString("\\t");
3188 continue;
3189 case '\"':
3190 case '\\':
3191 writer_->AddCharacter('\\');
3192 writer_->AddCharacter(*s);
3193 continue;
3194 default:
3195 if (*s > 31 && *s < 128) {
3196 writer_->AddCharacter(*s);
3197 } else if (*s <= 31) {
3198 // Special character with no dedicated literal.
3199 WriteUChar(writer_, *s);
3200 } else {
3201 // Convert UTF-8 into \u UTF-16 literal.
3202 unsigned length = 1, cursor = 0;
3203 for ( ; length <= 4 && *(s + length) != '\0'; ++length) { }
3204 unibrow::uchar c = unibrow::Utf8::CalculateValue(s, length, &cursor);
3205 if (c != unibrow::Utf8::kBadChar) {
3206 WriteUChar(writer_, c);
3207 ASSERT(cursor != 0);
3208 s += cursor - 1;
3209 } else {
3210 writer_->AddCharacter('?');
3211 }
3212 }
3213 }
3214 }
3215 writer_->AddCharacter('\"');
3216}
3217
3218
3219void HeapSnapshotJSONSerializer::SerializeStrings() {
3220 List<HashMap::Entry*> sorted_strings;
3221 SortHashMap(&strings_, &sorted_strings);
3222 writer_->AddString("\"<dummy>\"");
3223 for (int i = 0; i < sorted_strings.length(); ++i) {
3224 writer_->AddCharacter(',');
3225 SerializeString(
3226 reinterpret_cast<const unsigned char*>(sorted_strings[i]->key));
3227 if (writer_->aborted()) return;
3228 }
3229}
3230
3231
3232template<typename T>
3233inline static int SortUsingEntryValue(const T* x, const T* y) {
3234 uintptr_t x_uint = reinterpret_cast<uintptr_t>((*x)->value);
3235 uintptr_t y_uint = reinterpret_cast<uintptr_t>((*y)->value);
3236 if (x_uint > y_uint) {
3237 return 1;
3238 } else if (x_uint == y_uint) {
3239 return 0;
3240 } else {
3241 return -1;
3242 }
3243}
3244
3245
3246void HeapSnapshotJSONSerializer::SortHashMap(
3247 HashMap* map, List<HashMap::Entry*>* sorted_entries) {
3248 for (HashMap::Entry* p = map->Start(); p != NULL; p = map->Next(p))
3249 sorted_entries->Add(p);
3250 sorted_entries->Sort(SortUsingEntryValue);
3251}
3252
ager@chromium.orgbeb25712010-11-29 08:02:25 +00003253
3254String* GetConstructorNameForHeapProfile(JSObject* object) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +00003255 if (object->IsJSFunction()) return HEAP->closure_symbol();
ager@chromium.orgbeb25712010-11-29 08:02:25 +00003256 return object->constructor_name();
3257}
3258
fschneider@chromium.org086aac62010-03-17 13:18:24 +00003259} } // namespace v8::internal