blob: aefe1a0f607df3ceb75321ee5daf745c13f789d9 [file] [log] [blame]
Ben Murdoch592a9fc2012-03-05 11:04:45 +00001// Copyright 2011 the V8 project authors. All rights reserved.
Steve Block6ded16b2010-05-10 14:33:55 +01002// 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#ifndef V8_PROFILE_GENERATOR_H_
29#define V8_PROFILE_GENERATOR_H_
30
Ben Murdoch257744e2011-11-30 15:57:28 +000031#include "allocation.h"
Steve Block6ded16b2010-05-10 14:33:55 +010032#include "hashmap.h"
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010033#include "../include/v8-profiler.h"
Steve Block6ded16b2010-05-10 14:33:55 +010034
35namespace v8 {
36namespace internal {
37
Leon Clarkef7060e22010-06-03 12:02:55 +010038class TokenEnumerator {
39 public:
40 TokenEnumerator();
41 ~TokenEnumerator();
42 int GetTokenId(Object* token);
43
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010044 static const int kNoSecurityToken = -1;
45 static const int kInheritsSecurityToken = -2;
46
Leon Clarkef7060e22010-06-03 12:02:55 +010047 private:
48 static void TokenRemovedCallback(v8::Persistent<v8::Value> handle,
49 void* parameter);
50 void TokenRemoved(Object** token_location);
51
52 List<Object**> token_locations_;
53 List<bool> token_removed_;
54
55 friend class TokenEnumeratorTester;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010056
57 DISALLOW_COPY_AND_ASSIGN(TokenEnumerator);
58};
59
60
61// Provides a storage of strings allocated in C++ heap, to hold them
62// forever, even if they disappear from JS heap or external storage.
63class StringsStorage {
64 public:
65 StringsStorage();
66 ~StringsStorage();
67
Steve Block44f0eee2011-05-26 01:26:41 +010068 const char* GetCopy(const char* src);
69 const char* GetFormatted(const char* format, ...);
70 const char* GetVFormatted(const char* format, va_list args);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010071 const char* GetName(String* name);
Ben Murdochf87a2032010-10-22 12:50:53 +010072 const char* GetName(int index);
Steve Block791712a2010-08-27 10:21:07 +010073 inline const char* GetFunctionName(String* name);
74 inline const char* GetFunctionName(const char* name);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010075
76 private:
Ben Murdoch592a9fc2012-03-05 11:04:45 +000077 static const int kMaxNameSize = 1024;
78
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010079 INLINE(static bool StringsMatch(void* key1, void* key2)) {
80 return strcmp(reinterpret_cast<char*>(key1),
81 reinterpret_cast<char*>(key2)) == 0;
82 }
Steve Block44f0eee2011-05-26 01:26:41 +010083 const char* AddOrDisposeString(char* str, uint32_t hash);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010084
Ben Murdoch3bec4d22010-07-22 14:51:16 +010085 // Mapping of strings by String::Hash to const char* strings.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +010086 HashMap names_;
87
88 DISALLOW_COPY_AND_ASSIGN(StringsStorage);
Leon Clarkef7060e22010-06-03 12:02:55 +010089};
90
91
Steve Block6ded16b2010-05-10 14:33:55 +010092class CodeEntry {
93 public:
94 // CodeEntry doesn't own name strings, just references them.
95 INLINE(CodeEntry(Logger::LogEventsAndTags tag,
96 const char* name_prefix,
97 const char* name,
98 const char* resource_name,
Leon Clarkef7060e22010-06-03 12:02:55 +010099 int line_number,
100 int security_token_id));
Steve Block6ded16b2010-05-10 14:33:55 +0100101
102 INLINE(bool is_js_function() const) { return is_js_function_tag(tag_); }
103 INLINE(const char* name_prefix() const) { return name_prefix_; }
104 INLINE(bool has_name_prefix() const) { return name_prefix_[0] != '\0'; }
105 INLINE(const char* name() const) { return name_; }
106 INLINE(const char* resource_name() const) { return resource_name_; }
107 INLINE(int line_number() const) { return line_number_; }
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100108 INLINE(int shared_id() const) { return shared_id_; }
109 INLINE(void set_shared_id(int shared_id)) { shared_id_ = shared_id; }
Leon Clarkef7060e22010-06-03 12:02:55 +0100110 INLINE(int security_token_id() const) { return security_token_id_; }
Steve Block6ded16b2010-05-10 14:33:55 +0100111
112 INLINE(static bool is_js_function_tag(Logger::LogEventsAndTags tag));
113
Leon Clarkef7060e22010-06-03 12:02:55 +0100114 void CopyData(const CodeEntry& source);
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100115 uint32_t GetCallUid() const;
116 bool IsSameAs(CodeEntry* entry) const;
Leon Clarkef7060e22010-06-03 12:02:55 +0100117
Steve Block44f0eee2011-05-26 01:26:41 +0100118 static const char* const kEmptyNamePrefix;
Steve Block6ded16b2010-05-10 14:33:55 +0100119
120 private:
Steve Block6ded16b2010-05-10 14:33:55 +0100121 Logger::LogEventsAndTags tag_;
122 const char* name_prefix_;
123 const char* name_;
124 const char* resource_name_;
125 int line_number_;
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100126 int shared_id_;
Leon Clarkef7060e22010-06-03 12:02:55 +0100127 int security_token_id_;
Steve Block6ded16b2010-05-10 14:33:55 +0100128
Steve Block6ded16b2010-05-10 14:33:55 +0100129 DISALLOW_COPY_AND_ASSIGN(CodeEntry);
130};
131
132
133class ProfileTree;
134
135class ProfileNode {
136 public:
137 INLINE(ProfileNode(ProfileTree* tree, CodeEntry* entry));
138
139 ProfileNode* FindChild(CodeEntry* entry);
140 ProfileNode* FindOrAddChild(CodeEntry* entry);
141 INLINE(void IncrementSelfTicks()) { ++self_ticks_; }
Leon Clarkef7060e22010-06-03 12:02:55 +0100142 INLINE(void IncreaseSelfTicks(unsigned amount)) { self_ticks_ += amount; }
Steve Block6ded16b2010-05-10 14:33:55 +0100143 INLINE(void IncreaseTotalTicks(unsigned amount)) { total_ticks_ += amount; }
144
145 INLINE(CodeEntry* entry() const) { return entry_; }
146 INLINE(unsigned self_ticks() const) { return self_ticks_; }
147 INLINE(unsigned total_ticks() const) { return total_ticks_; }
148 INLINE(const List<ProfileNode*>* children() const) { return &children_list_; }
149 double GetSelfMillis() const;
150 double GetTotalMillis() const;
151
152 void Print(int indent);
153
154 private:
155 INLINE(static bool CodeEntriesMatch(void* entry1, void* entry2)) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100156 return reinterpret_cast<CodeEntry*>(entry1)->IsSameAs(
157 reinterpret_cast<CodeEntry*>(entry2));
Steve Block6ded16b2010-05-10 14:33:55 +0100158 }
159
160 INLINE(static uint32_t CodeEntryHash(CodeEntry* entry)) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100161 return entry->GetCallUid();
Steve Block6ded16b2010-05-10 14:33:55 +0100162 }
163
164 ProfileTree* tree_;
165 CodeEntry* entry_;
166 unsigned total_ticks_;
167 unsigned self_ticks_;
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100168 // Mapping from CodeEntry* to ProfileNode*
Steve Block6ded16b2010-05-10 14:33:55 +0100169 HashMap children_;
170 List<ProfileNode*> children_list_;
171
172 DISALLOW_COPY_AND_ASSIGN(ProfileNode);
173};
174
175
176class ProfileTree {
177 public:
178 ProfileTree();
179 ~ProfileTree();
180
181 void AddPathFromEnd(const Vector<CodeEntry*>& path);
182 void AddPathFromStart(const Vector<CodeEntry*>& path);
183 void CalculateTotalTicks();
Leon Clarkef7060e22010-06-03 12:02:55 +0100184 void FilteredClone(ProfileTree* src, int security_token_id);
Steve Block6ded16b2010-05-10 14:33:55 +0100185
186 double TicksToMillis(unsigned ticks) const {
187 return ticks * ms_to_ticks_scale_;
188 }
189 ProfileNode* root() const { return root_; }
190 void SetTickRatePerMs(double ticks_per_ms);
191
192 void ShortPrint();
193 void Print() {
194 root_->Print(0);
195 }
196
197 private:
198 template <typename Callback>
Leon Clarkef7060e22010-06-03 12:02:55 +0100199 void TraverseDepthFirst(Callback* callback);
Steve Block6ded16b2010-05-10 14:33:55 +0100200
201 CodeEntry root_entry_;
202 ProfileNode* root_;
203 double ms_to_ticks_scale_;
204
205 DISALLOW_COPY_AND_ASSIGN(ProfileTree);
206};
207
208
209class CpuProfile {
210 public:
211 CpuProfile(const char* title, unsigned uid)
212 : title_(title), uid_(uid) { }
213
214 // Add pc -> ... -> main() call path to the profile.
215 void AddPath(const Vector<CodeEntry*>& path);
216 void CalculateTotalTicks();
217 void SetActualSamplingRate(double actual_sampling_rate);
Leon Clarkef7060e22010-06-03 12:02:55 +0100218 CpuProfile* FilteredClone(int security_token_id);
Steve Block6ded16b2010-05-10 14:33:55 +0100219
220 INLINE(const char* title() const) { return title_; }
221 INLINE(unsigned uid() const) { return uid_; }
222 INLINE(const ProfileTree* top_down() const) { return &top_down_; }
223 INLINE(const ProfileTree* bottom_up() const) { return &bottom_up_; }
224
225 void UpdateTicksScale();
226
227 void ShortPrint();
228 void Print();
229
230 private:
231 const char* title_;
232 unsigned uid_;
233 ProfileTree top_down_;
234 ProfileTree bottom_up_;
235
236 DISALLOW_COPY_AND_ASSIGN(CpuProfile);
237};
238
239
240class CodeMap {
241 public:
Steve Block44f0eee2011-05-26 01:26:41 +0100242 CodeMap() : next_shared_id_(1) { }
Ben Murdoch589d6972011-11-30 16:04:58 +0000243 void AddCode(Address addr, CodeEntry* entry, unsigned size);
244 void MoveCode(Address from, Address to);
Steve Block6ded16b2010-05-10 14:33:55 +0100245 CodeEntry* FindEntry(Address addr);
Steve Block44f0eee2011-05-26 01:26:41 +0100246 int GetSharedId(Address addr);
Steve Block6ded16b2010-05-10 14:33:55 +0100247
248 void Print();
249
250 private:
251 struct CodeEntryInfo {
252 CodeEntryInfo(CodeEntry* an_entry, unsigned a_size)
253 : entry(an_entry), size(a_size) { }
254 CodeEntry* entry;
255 unsigned size;
256 };
257
258 struct CodeTreeConfig {
259 typedef Address Key;
260 typedef CodeEntryInfo Value;
261 static const Key kNoKey;
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000262 static const Value NoValue() { return CodeEntryInfo(NULL, 0); }
Steve Block6ded16b2010-05-10 14:33:55 +0100263 static int Compare(const Key& a, const Key& b) {
264 return a < b ? -1 : (a > b ? 1 : 0);
265 }
266 };
267 typedef SplayTree<CodeTreeConfig> CodeTree;
268
269 class CodeTreePrinter {
270 public:
271 void Call(const Address& key, const CodeEntryInfo& value);
272 };
273
Ben Murdoch589d6972011-11-30 16:04:58 +0000274 void DeleteAllCoveredCode(Address start, Address end);
275
Steve Block44f0eee2011-05-26 01:26:41 +0100276 // Fake CodeEntry pointer to distinguish shared function entries.
277 static CodeEntry* const kSharedFunctionCodeEntry;
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100278
Steve Block6ded16b2010-05-10 14:33:55 +0100279 CodeTree tree_;
Steve Block44f0eee2011-05-26 01:26:41 +0100280 int next_shared_id_;
Steve Block6ded16b2010-05-10 14:33:55 +0100281
282 DISALLOW_COPY_AND_ASSIGN(CodeMap);
283};
284
285
286class CpuProfilesCollection {
287 public:
288 CpuProfilesCollection();
289 ~CpuProfilesCollection();
290
291 bool StartProfiling(const char* title, unsigned uid);
292 bool StartProfiling(String* title, unsigned uid);
Leon Clarkef7060e22010-06-03 12:02:55 +0100293 CpuProfile* StopProfiling(int security_token_id,
294 const char* title,
295 double actual_sampling_rate);
Leon Clarkef7060e22010-06-03 12:02:55 +0100296 List<CpuProfile*>* Profiles(int security_token_id);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100297 const char* GetName(String* name) {
298 return function_and_resource_names_.GetName(name);
299 }
Ben Murdochf87a2032010-10-22 12:50:53 +0100300 const char* GetName(int args_count) {
301 return function_and_resource_names_.GetName(args_count);
302 }
Leon Clarkef7060e22010-06-03 12:02:55 +0100303 CpuProfile* GetProfile(int security_token_id, unsigned uid);
Iain Merrick75681382010-08-19 15:07:18 +0100304 bool IsLastProfile(const char* title);
Steve Block44f0eee2011-05-26 01:26:41 +0100305 void RemoveProfile(CpuProfile* profile);
306 bool HasDetachedProfiles() { return detached_profiles_.length() > 0; }
Steve Block6ded16b2010-05-10 14:33:55 +0100307
308 CodeEntry* NewCodeEntry(Logger::LogEventsAndTags tag,
309 String* name, String* resource_name, int line_number);
310 CodeEntry* NewCodeEntry(Logger::LogEventsAndTags tag, const char* name);
311 CodeEntry* NewCodeEntry(Logger::LogEventsAndTags tag,
312 const char* name_prefix, String* name);
313 CodeEntry* NewCodeEntry(Logger::LogEventsAndTags tag, int args_count);
Leon Clarkef7060e22010-06-03 12:02:55 +0100314 CodeEntry* NewCodeEntry(int security_token_id);
Steve Block6ded16b2010-05-10 14:33:55 +0100315
316 // Called from profile generator thread.
317 void AddPathToCurrentProfiles(const Vector<CodeEntry*>& path);
318
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100319 // Limits the number of profiles that can be simultaneously collected.
320 static const int kMaxSimultaneousProfiles = 100;
321
Steve Block6ded16b2010-05-10 14:33:55 +0100322 private:
Steve Block791712a2010-08-27 10:21:07 +0100323 const char* GetFunctionName(String* name) {
324 return function_and_resource_names_.GetFunctionName(name);
325 }
326 const char* GetFunctionName(const char* name) {
327 return function_and_resource_names_.GetFunctionName(name);
328 }
Steve Block44f0eee2011-05-26 01:26:41 +0100329 int GetProfileIndex(unsigned uid);
Leon Clarkef7060e22010-06-03 12:02:55 +0100330 List<CpuProfile*>* GetProfilesList(int security_token_id);
331 int TokenToIndex(int security_token_id);
Steve Block6ded16b2010-05-10 14:33:55 +0100332
Leon Clarkef7060e22010-06-03 12:02:55 +0100333 INLINE(static bool UidsMatch(void* key1, void* key2)) {
Steve Block6ded16b2010-05-10 14:33:55 +0100334 return key1 == key2;
335 }
336
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100337 StringsStorage function_and_resource_names_;
Steve Block6ded16b2010-05-10 14:33:55 +0100338 List<CodeEntry*> code_entries_;
Leon Clarkef7060e22010-06-03 12:02:55 +0100339 List<List<CpuProfile*>* > profiles_by_token_;
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100340 // Mapping from profiles' uids to indexes in the second nested list
341 // of profiles_by_token_.
Steve Block6ded16b2010-05-10 14:33:55 +0100342 HashMap profiles_uids_;
Steve Block44f0eee2011-05-26 01:26:41 +0100343 List<CpuProfile*> detached_profiles_;
Steve Block6ded16b2010-05-10 14:33:55 +0100344
345 // Accessed by VM thread and profile generator thread.
346 List<CpuProfile*> current_profiles_;
347 Semaphore* current_profiles_semaphore_;
348
349 DISALLOW_COPY_AND_ASSIGN(CpuProfilesCollection);
350};
351
352
353class SampleRateCalculator {
354 public:
355 SampleRateCalculator()
356 : result_(Logger::kSamplingIntervalMs * kResultScale),
357 ticks_per_ms_(Logger::kSamplingIntervalMs),
358 measurements_count_(0),
359 wall_time_query_countdown_(1) {
360 }
361
362 double ticks_per_ms() {
363 return result_ / static_cast<double>(kResultScale);
364 }
365 void Tick();
366 void UpdateMeasurements(double current_time);
367
368 // Instead of querying current wall time each tick,
369 // we use this constant to control query intervals.
370 static const unsigned kWallTimeQueryIntervalMs = 100;
371
372 private:
373 // As the result needs to be accessed from a different thread, we
374 // use type that guarantees atomic writes to memory. There should
375 // be <= 1000 ticks per second, thus storing a value of a 10 ** 5
376 // order should provide enough precision while keeping away from a
377 // potential overflow.
378 static const int kResultScale = 100000;
379
380 AtomicWord result_;
381 // All other fields are accessed only from the sampler thread.
382 double ticks_per_ms_;
383 unsigned measurements_count_;
384 unsigned wall_time_query_countdown_;
385 double last_wall_time_;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100386
387 DISALLOW_COPY_AND_ASSIGN(SampleRateCalculator);
Steve Block6ded16b2010-05-10 14:33:55 +0100388};
389
390
391class ProfileGenerator {
392 public:
393 explicit ProfileGenerator(CpuProfilesCollection* profiles);
394
395 INLINE(CodeEntry* NewCodeEntry(Logger::LogEventsAndTags tag,
396 String* name,
397 String* resource_name,
398 int line_number)) {
399 return profiles_->NewCodeEntry(tag, name, resource_name, line_number);
400 }
401
402 INLINE(CodeEntry* NewCodeEntry(Logger::LogEventsAndTags tag,
403 const char* name)) {
404 return profiles_->NewCodeEntry(tag, name);
405 }
406
407 INLINE(CodeEntry* NewCodeEntry(Logger::LogEventsAndTags tag,
408 const char* name_prefix,
409 String* name)) {
410 return profiles_->NewCodeEntry(tag, name_prefix, name);
411 }
412
413 INLINE(CodeEntry* NewCodeEntry(Logger::LogEventsAndTags tag,
414 int args_count)) {
415 return profiles_->NewCodeEntry(tag, args_count);
416 }
417
Leon Clarkef7060e22010-06-03 12:02:55 +0100418 INLINE(CodeEntry* NewCodeEntry(int security_token_id)) {
419 return profiles_->NewCodeEntry(security_token_id);
420 }
421
Steve Block6ded16b2010-05-10 14:33:55 +0100422 void RecordTickSample(const TickSample& sample);
423
424 INLINE(CodeMap* code_map()) { return &code_map_; }
425
426 INLINE(void Tick()) { sample_rate_calc_.Tick(); }
427 INLINE(double actual_sampling_rate()) {
428 return sample_rate_calc_.ticks_per_ms();
429 }
430
Steve Block44f0eee2011-05-26 01:26:41 +0100431 static const char* const kAnonymousFunctionName;
432 static const char* const kProgramEntryName;
433 static const char* const kGarbageCollectorEntryName;
Steve Block6ded16b2010-05-10 14:33:55 +0100434
435 private:
436 INLINE(CodeEntry* EntryForVMState(StateTag tag));
437
438 CpuProfilesCollection* profiles_;
439 CodeMap code_map_;
440 CodeEntry* program_entry_;
441 CodeEntry* gc_entry_;
442 SampleRateCalculator sample_rate_calc_;
443
444 DISALLOW_COPY_AND_ASSIGN(ProfileGenerator);
445};
446
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100447
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100448class HeapEntry;
449
Iain Merrick75681382010-08-19 15:07:18 +0100450class HeapGraphEdge BASE_EMBEDDED {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100451 public:
452 enum Type {
Iain Merrick75681382010-08-19 15:07:18 +0100453 kContextVariable = v8::HeapGraphEdge::kContextVariable,
454 kElement = v8::HeapGraphEdge::kElement,
455 kProperty = v8::HeapGraphEdge::kProperty,
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800456 kInternal = v8::HeapGraphEdge::kInternal,
457 kHidden = v8::HeapGraphEdge::kHidden,
Ben Murdochc7cc0282012-03-05 14:35:55 +0000458 kShortcut = v8::HeapGraphEdge::kShortcut,
459 kWeak = v8::HeapGraphEdge::kWeak
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100460 };
461
Iain Merrick75681382010-08-19 15:07:18 +0100462 HeapGraphEdge() { }
463 void Init(int child_index, Type type, const char* name, HeapEntry* to);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800464 void Init(int child_index, Type type, int index, HeapEntry* to);
Iain Merrick75681382010-08-19 15:07:18 +0100465 void Init(int child_index, int index, HeapEntry* to);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100466
Iain Merrick75681382010-08-19 15:07:18 +0100467 Type type() { return static_cast<Type>(type_); }
468 int index() {
Ben Murdochc7cc0282012-03-05 14:35:55 +0000469 ASSERT(type_ == kElement || type_ == kHidden || type_ == kWeak);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100470 return index_;
471 }
Iain Merrick75681382010-08-19 15:07:18 +0100472 const char* name() {
473 ASSERT(type_ == kContextVariable
474 || type_ == kProperty
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800475 || type_ == kInternal
476 || type_ == kShortcut);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100477 return name_;
478 }
Iain Merrick75681382010-08-19 15:07:18 +0100479 HeapEntry* to() { return to_; }
480
481 HeapEntry* From();
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100482
483 private:
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800484 int child_index_ : 29;
485 unsigned type_ : 3;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100486 union {
487 int index_;
488 const char* name_;
489 };
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100490 HeapEntry* to_;
491
492 DISALLOW_COPY_AND_ASSIGN(HeapGraphEdge);
493};
494
495
Iain Merrick75681382010-08-19 15:07:18 +0100496class HeapSnapshot;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100497
Iain Merrick75681382010-08-19 15:07:18 +0100498// HeapEntry instances represent an entity from the heap (or a special
499// virtual node, e.g. root). To make heap snapshots more compact,
500// HeapEntries has a special memory layout (no Vectors or Lists used):
501//
502// +-----------------+
503// HeapEntry
504// +-----------------+
505// HeapGraphEdge |
506// ... } children_count
507// HeapGraphEdge |
508// +-----------------+
509// HeapGraphEdge* |
510// ... } retainers_count
511// HeapGraphEdge* |
512// +-----------------+
513//
514// In a HeapSnapshot, all entries are hand-allocated in a continuous array
515// of raw bytes.
516//
517class HeapEntry BASE_EMBEDDED {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100518 public:
519 enum Type {
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800520 kHidden = v8::HeapGraphNode::kHidden,
Iain Merrick75681382010-08-19 15:07:18 +0100521 kArray = v8::HeapGraphNode::kArray,
522 kString = v8::HeapGraphNode::kString,
523 kObject = v8::HeapGraphNode::kObject,
524 kCode = v8::HeapGraphNode::kCode,
Ben Murdochf87a2032010-10-22 12:50:53 +0100525 kClosure = v8::HeapGraphNode::kClosure,
526 kRegExp = v8::HeapGraphNode::kRegExp,
Steve Block44f0eee2011-05-26 01:26:41 +0100527 kHeapNumber = v8::HeapGraphNode::kHeapNumber,
528 kNative = v8::HeapGraphNode::kNative
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100529 };
530
Iain Merrick75681382010-08-19 15:07:18 +0100531 HeapEntry() { }
Iain Merrick75681382010-08-19 15:07:18 +0100532 void Init(HeapSnapshot* snapshot,
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100533 Type type,
534 const char* name,
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100535 uint64_t id,
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100536 int self_size,
Iain Merrick75681382010-08-19 15:07:18 +0100537 int children_count,
538 int retainers_count);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100539
Iain Merrick75681382010-08-19 15:07:18 +0100540 HeapSnapshot* snapshot() { return snapshot_; }
541 Type type() { return static_cast<Type>(type_); }
542 const char* name() { return name_; }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100543 inline uint64_t id();
Iain Merrick75681382010-08-19 15:07:18 +0100544 int self_size() { return self_size_; }
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800545 int retained_size() { return retained_size_; }
546 void add_retained_size(int size) { retained_size_ += size; }
547 void set_retained_size(int value) { retained_size_ = value; }
548 int ordered_index() { return ordered_index_; }
549 void set_ordered_index(int value) { ordered_index_ = value; }
Iain Merrick75681382010-08-19 15:07:18 +0100550
551 Vector<HeapGraphEdge> children() {
552 return Vector<HeapGraphEdge>(children_arr(), children_count_); }
553 Vector<HeapGraphEdge*> retainers() {
554 return Vector<HeapGraphEdge*>(retainers_arr(), retainers_count_); }
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800555 HeapEntry* dominator() { return dominator_; }
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000556 void set_dominator(HeapEntry* entry) {
557 ASSERT(entry != NULL);
558 dominator_ = entry;
559 }
Iain Merrick75681382010-08-19 15:07:18 +0100560
561 void clear_paint() { painted_ = kUnpainted; }
562 bool painted_reachable() { return painted_ == kPainted; }
563 void paint_reachable() {
564 ASSERT(painted_ == kUnpainted);
565 painted_ = kPainted;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100566 }
Iain Merrick75681382010-08-19 15:07:18 +0100567 bool not_painted_reachable_from_others() {
568 return painted_ != kPaintedReachableFromOthers;
569 }
570 void paint_reachable_from_others() {
571 painted_ = kPaintedReachableFromOthers;
572 }
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100573 template<class Visitor>
574 void ApplyAndPaintAllReachable(Visitor* visitor);
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100575 void PaintAllReachable();
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100576
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800577 void SetIndexedReference(HeapGraphEdge::Type type,
578 int child_index,
579 int index,
580 HeapEntry* entry,
581 int retainer_index);
Iain Merrick75681382010-08-19 15:07:18 +0100582 void SetNamedReference(HeapGraphEdge::Type type,
583 int child_index,
584 const char* name,
585 HeapEntry* entry,
586 int retainer_index);
587 void SetUnidirElementReference(int child_index, int index, HeapEntry* entry);
588
589 int EntrySize() { return EntriesSize(1, children_count_, retainers_count_); }
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800590 int RetainedSize(bool exact);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100591
Ben Murdochc7cc0282012-03-05 14:35:55 +0000592 void Print(
593 const char* prefix, const char* edge_name, int max_depth, int indent);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100594
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000595 Handle<HeapObject> GetHeapObject();
596
Iain Merrick75681382010-08-19 15:07:18 +0100597 static int EntriesSize(int entries_count,
598 int children_count,
599 int retainers_count);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100600
Iain Merrick75681382010-08-19 15:07:18 +0100601 private:
602 HeapGraphEdge* children_arr() {
603 return reinterpret_cast<HeapGraphEdge*>(this + 1);
604 }
605 HeapGraphEdge** retainers_arr() {
606 return reinterpret_cast<HeapGraphEdge**>(children_arr() + children_count_);
607 }
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800608 void CalculateExactRetainedSize();
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100609 const char* TypeAsString();
610
Iain Merrick75681382010-08-19 15:07:18 +0100611 unsigned painted_: 2;
Steve Block44f0eee2011-05-26 01:26:41 +0100612 unsigned type_: 4;
613 int children_count_: 26;
Iain Merrick75681382010-08-19 15:07:18 +0100614 int retainers_count_;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800615 int self_size_;
616 union {
617 int ordered_index_; // Used during dominator tree building.
618 int retained_size_; // At that moment, there is no retained size yet.
619 };
620 HeapEntry* dominator_;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100621 HeapSnapshot* snapshot_;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800622 struct Id {
623 uint32_t id1_;
624 uint32_t id2_;
625 } id_; // This is to avoid extra padding of 64-bit value.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100626 const char* name_;
Iain Merrick75681382010-08-19 15:07:18 +0100627
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800628 // Paints used for exact retained sizes calculation.
Iain Merrick75681382010-08-19 15:07:18 +0100629 static const unsigned kUnpainted = 0;
630 static const unsigned kPainted = 1;
631 static const unsigned kPaintedReachableFromOthers = 2;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800632
633 static const int kExactRetainedSizeTag = 1;
Iain Merrick75681382010-08-19 15:07:18 +0100634
635 DISALLOW_COPY_AND_ASSIGN(HeapEntry);
636};
637
638
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100639class HeapSnapshotsCollection;
640
641// HeapSnapshot represents a single heap snapshot. It is stored in
642// HeapSnapshotsCollection, which is also a factory for
643// HeapSnapshots. All HeapSnapshots share strings copied from JS heap
644// to be able to return them even if they were collected.
645// HeapSnapshotGenerator fills in a HeapSnapshot.
646class HeapSnapshot {
647 public:
Steve Block791712a2010-08-27 10:21:07 +0100648 enum Type {
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000649 kFull = v8::HeapSnapshot::kFull
Steve Block791712a2010-08-27 10:21:07 +0100650 };
651
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100652 HeapSnapshot(HeapSnapshotsCollection* collection,
Steve Block791712a2010-08-27 10:21:07 +0100653 Type type,
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100654 const char* title,
655 unsigned uid);
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100656 ~HeapSnapshot();
Steve Block44f0eee2011-05-26 01:26:41 +0100657 void Delete();
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100658
Iain Merrick75681382010-08-19 15:07:18 +0100659 HeapSnapshotsCollection* collection() { return collection_; }
Steve Block791712a2010-08-27 10:21:07 +0100660 Type type() { return type_; }
Iain Merrick75681382010-08-19 15:07:18 +0100661 const char* title() { return title_; }
662 unsigned uid() { return uid_; }
Ben Murdochf87a2032010-10-22 12:50:53 +0100663 HeapEntry* root() { return root_entry_; }
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800664 HeapEntry* gc_roots() { return gc_roots_entry_; }
Steve Block44f0eee2011-05-26 01:26:41 +0100665 HeapEntry* natives_root() { return natives_root_entry_; }
Ben Murdochc7cc0282012-03-05 14:35:55 +0000666 HeapEntry* gc_subroot(int index) { return gc_subroot_entries_[index]; }
Ben Murdochb0fe1622011-05-05 13:52:32 +0100667 List<HeapEntry*>* entries() { return &entries_; }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000668 int raw_entries_size() { return raw_entries_size_; }
Iain Merrick75681382010-08-19 15:07:18 +0100669
670 void AllocateEntries(
671 int entries_count, int children_count, int retainers_count);
Steve Block791712a2010-08-27 10:21:07 +0100672 HeapEntry* AddEntry(HeapEntry::Type type,
673 const char* name,
674 uint64_t id,
675 int size,
676 int children_count,
677 int retainers_count);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100678 HeapEntry* AddRootEntry(int children_count);
679 HeapEntry* AddGcRootsEntry(int children_count, int retainers_count);
Ben Murdochc7cc0282012-03-05 14:35:55 +0000680 HeapEntry* AddGcSubrootEntry(int tag,
681 int children_count,
682 int retainers_count);
Steve Block44f0eee2011-05-26 01:26:41 +0100683 HeapEntry* AddNativesRootEntry(int children_count, int retainers_count);
Iain Merrick75681382010-08-19 15:07:18 +0100684 void ClearPaint();
Ben Murdochb0fe1622011-05-05 13:52:32 +0100685 HeapEntry* GetEntryById(uint64_t id);
Iain Merrick75681382010-08-19 15:07:18 +0100686 List<HeapEntry*>* GetSortedEntriesList();
687 template<class Visitor>
688 void IterateEntries(Visitor* visitor) { entries_.Iterate(visitor); }
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800689 void SetDominatorsToSelf();
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100690
691 void Print(int max_depth);
Iain Merrick75681382010-08-19 15:07:18 +0100692 void PrintEntriesSize();
693
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100694 private:
Iain Merrick75681382010-08-19 15:07:18 +0100695 HeapEntry* GetNextEntryToInit();
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100696
697 HeapSnapshotsCollection* collection_;
Steve Block791712a2010-08-27 10:21:07 +0100698 Type type_;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100699 const char* title_;
700 unsigned uid_;
Ben Murdochf87a2032010-10-22 12:50:53 +0100701 HeapEntry* root_entry_;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800702 HeapEntry* gc_roots_entry_;
Steve Block44f0eee2011-05-26 01:26:41 +0100703 HeapEntry* natives_root_entry_;
Ben Murdochc7cc0282012-03-05 14:35:55 +0000704 HeapEntry* gc_subroot_entries_[VisitorSynchronization::kNumberOfSyncTags];
Iain Merrick75681382010-08-19 15:07:18 +0100705 char* raw_entries_;
706 List<HeapEntry*> entries_;
707 bool entries_sorted_;
Steve Block791712a2010-08-27 10:21:07 +0100708 int raw_entries_size_;
Iain Merrick75681382010-08-19 15:07:18 +0100709
710 friend class HeapSnapshotTester;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100711
712 DISALLOW_COPY_AND_ASSIGN(HeapSnapshot);
713};
714
715
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100716class HeapObjectsMap {
717 public:
718 HeapObjectsMap();
719 ~HeapObjectsMap();
720
721 void SnapshotGenerationFinished();
722 uint64_t FindObject(Address addr);
723 void MoveObject(Address from, Address to);
724
Steve Block44f0eee2011-05-26 01:26:41 +0100725 static uint64_t GenerateId(v8::RetainedObjectInfo* info);
Ben Murdochc7cc0282012-03-05 14:35:55 +0000726 static inline uint64_t GetNthGcSubrootId(int delta);
Steve Block44f0eee2011-05-26 01:26:41 +0100727
Ben Murdochc7cc0282012-03-05 14:35:55 +0000728 static const int kObjectIdStep = 2;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800729 static const uint64_t kInternalRootObjectId;
730 static const uint64_t kGcRootsObjectId;
Steve Block44f0eee2011-05-26 01:26:41 +0100731 static const uint64_t kNativesRootObjectId;
Ben Murdochc7cc0282012-03-05 14:35:55 +0000732 static const uint64_t kGcRootsFirstSubrootId;
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800733 static const uint64_t kFirstAvailableObjectId;
734
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100735 private:
736 struct EntryInfo {
737 explicit EntryInfo(uint64_t id) : id(id), accessed(true) { }
738 EntryInfo(uint64_t id, bool accessed) : id(id), accessed(accessed) { }
739 uint64_t id;
740 bool accessed;
741 };
742
743 void AddEntry(Address addr, uint64_t id);
744 uint64_t FindEntry(Address addr);
745 void RemoveDeadEntries();
746
747 static bool AddressesMatch(void* key1, void* key2) {
748 return key1 == key2;
749 }
750
751 static uint32_t AddressHash(Address addr) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100752 return ComputeIntegerHash(
Ben Murdochc7cc0282012-03-05 14:35:55 +0000753 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(addr)),
754 v8::internal::kZeroHashSeed);
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100755 }
756
757 bool initial_fill_mode_;
758 uint64_t next_id_;
759 HashMap entries_map_;
760 List<EntryInfo>* entries_;
761
762 DISALLOW_COPY_AND_ASSIGN(HeapObjectsMap);
763};
764
765
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100766class HeapSnapshotsCollection {
767 public:
768 HeapSnapshotsCollection();
769 ~HeapSnapshotsCollection();
770
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100771 bool is_tracking_objects() { return is_tracking_objects_; }
772
Steve Block791712a2010-08-27 10:21:07 +0100773 HeapSnapshot* NewSnapshot(
774 HeapSnapshot::Type type, const char* name, unsigned uid);
Ben Murdochb0fe1622011-05-05 13:52:32 +0100775 void SnapshotGenerationFinished(HeapSnapshot* snapshot);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100776 List<HeapSnapshot*>* snapshots() { return &snapshots_; }
777 HeapSnapshot* GetSnapshot(unsigned uid);
Steve Block44f0eee2011-05-26 01:26:41 +0100778 void RemoveSnapshot(HeapSnapshot* snapshot);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100779
Steve Block44f0eee2011-05-26 01:26:41 +0100780 StringsStorage* names() { return &names_; }
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100781 TokenEnumerator* token_enumerator() { return token_enumerator_; }
782
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100783 uint64_t GetObjectId(Address addr) { return ids_.FindObject(addr); }
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000784 Handle<HeapObject> FindHeapObjectById(uint64_t id);
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100785 void ObjectMoveEvent(Address from, Address to) { ids_.MoveObject(from, to); }
786
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100787 private:
788 INLINE(static bool HeapSnapshotsMatch(void* key1, void* key2)) {
789 return key1 == key2;
790 }
791
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100792 bool is_tracking_objects_; // Whether tracking object moves is needed.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100793 List<HeapSnapshot*> snapshots_;
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100794 // Mapping from snapshots' uids to HeapSnapshot* pointers.
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100795 HashMap snapshots_uids_;
796 StringsStorage names_;
797 TokenEnumerator* token_enumerator_;
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100798 // Mapping from HeapObject addresses to objects' uids.
799 HeapObjectsMap ids_;
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100800
801 DISALLOW_COPY_AND_ASSIGN(HeapSnapshotsCollection);
802};
803
804
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100805// A typedef for referencing anything that can be snapshotted living
806// in any kind of heap memory.
807typedef void* HeapThing;
808
809
810// An interface that creates HeapEntries by HeapThings.
811class HeapEntriesAllocator {
812 public:
813 virtual ~HeapEntriesAllocator() { }
814 virtual HeapEntry* AllocateEntry(
815 HeapThing ptr, int children_count, int retainers_count) = 0;
816};
817
818
Iain Merrick75681382010-08-19 15:07:18 +0100819// The HeapEntriesMap instance is used to track a mapping between
820// real heap objects and their representations in heap snapshots.
821class HeapEntriesMap {
822 public:
823 HeapEntriesMap();
824 ~HeapEntriesMap();
825
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100826 void AllocateEntries();
827 HeapEntry* Map(HeapThing thing);
828 void Pair(HeapThing thing, HeapEntriesAllocator* allocator, HeapEntry* entry);
829 void CountReference(HeapThing from, HeapThing to,
Iain Merrick75681382010-08-19 15:07:18 +0100830 int* prev_children_count = NULL,
831 int* prev_retainers_count = NULL);
Iain Merrick75681382010-08-19 15:07:18 +0100832
833 int entries_count() { return entries_count_; }
834 int total_children_count() { return total_children_count_; }
835 int total_retainers_count() { return total_retainers_count_; }
836
Ben Murdochc7cc0282012-03-05 14:35:55 +0000837 static HeapEntry* const kHeapEntryPlaceholder;
Steve Block791712a2010-08-27 10:21:07 +0100838
Iain Merrick75681382010-08-19 15:07:18 +0100839 private:
840 struct EntryInfo {
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100841 EntryInfo(HeapEntry* entry, HeapEntriesAllocator* allocator)
842 : entry(entry),
843 allocator(allocator),
844 children_count(0),
845 retainers_count(0) {
846 }
Iain Merrick75681382010-08-19 15:07:18 +0100847 HeapEntry* entry;
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100848 HeapEntriesAllocator* allocator;
Iain Merrick75681382010-08-19 15:07:18 +0100849 int children_count;
850 int retainers_count;
851 };
852
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100853 static uint32_t Hash(HeapThing thing) {
Kristian Monsen0d5e1162010-09-30 15:31:59 +0100854 return ComputeIntegerHash(
Ben Murdochc7cc0282012-03-05 14:35:55 +0000855 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(thing)),
856 v8::internal::kZeroHashSeed);
Iain Merrick75681382010-08-19 15:07:18 +0100857 }
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100858 static bool HeapThingsMatch(HeapThing key1, HeapThing key2) {
859 return key1 == key2;
860 }
Iain Merrick75681382010-08-19 15:07:18 +0100861
Iain Merrick75681382010-08-19 15:07:18 +0100862 HashMap entries_;
863 int entries_count_;
864 int total_children_count_;
865 int total_retainers_count_;
866
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800867 friend class HeapObjectsSet;
Iain Merrick75681382010-08-19 15:07:18 +0100868
869 DISALLOW_COPY_AND_ASSIGN(HeapEntriesMap);
870};
871
872
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800873class HeapObjectsSet {
874 public:
875 HeapObjectsSet();
876 void Clear();
877 bool Contains(Object* object);
878 void Insert(Object* obj);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000879 const char* GetTag(Object* obj);
880 void SetTag(Object* obj, const char* tag);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800881
882 private:
883 HashMap entries_;
884
885 DISALLOW_COPY_AND_ASSIGN(HeapObjectsSet);
886};
887
888
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100889// An interface used to populate a snapshot with nodes and edges.
890class SnapshotFillerInterface {
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100891 public:
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100892 virtual ~SnapshotFillerInterface() { }
Steve Block44f0eee2011-05-26 01:26:41 +0100893 virtual HeapEntry* AddEntry(HeapThing ptr,
894 HeapEntriesAllocator* allocator) = 0;
895 virtual HeapEntry* FindEntry(HeapThing ptr) = 0;
896 virtual HeapEntry* FindOrAddEntry(HeapThing ptr,
897 HeapEntriesAllocator* allocator) = 0;
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100898 virtual void SetIndexedReference(HeapGraphEdge::Type type,
899 HeapThing parent_ptr,
Iain Merrick75681382010-08-19 15:07:18 +0100900 HeapEntry* parent_entry,
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100901 int index,
902 HeapThing child_ptr,
Iain Merrick75681382010-08-19 15:07:18 +0100903 HeapEntry* child_entry) = 0;
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100904 virtual void SetIndexedAutoIndexReference(HeapGraphEdge::Type type,
905 HeapThing parent_ptr,
906 HeapEntry* parent_entry,
907 HeapThing child_ptr,
908 HeapEntry* child_entry) = 0;
909 virtual void SetNamedReference(HeapGraphEdge::Type type,
910 HeapThing parent_ptr,
911 HeapEntry* parent_entry,
912 const char* reference_name,
913 HeapThing child_ptr,
914 HeapEntry* child_entry) = 0;
915 virtual void SetNamedAutoIndexReference(HeapGraphEdge::Type type,
916 HeapThing parent_ptr,
917 HeapEntry* parent_entry,
918 HeapThing child_ptr,
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800919 HeapEntry* child_entry) = 0;
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100920};
Iain Merrick75681382010-08-19 15:07:18 +0100921
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100922
923class SnapshottingProgressReportingInterface {
924 public:
925 virtual ~SnapshottingProgressReportingInterface() { }
926 virtual void ProgressStep() = 0;
927 virtual bool ProgressReport(bool force) = 0;
928};
929
930
931// An implementation of V8 heap graph extractor.
932class V8HeapExplorer : public HeapEntriesAllocator {
933 public:
934 V8HeapExplorer(HeapSnapshot* snapshot,
935 SnapshottingProgressReportingInterface* progress);
Steve Block44f0eee2011-05-26 01:26:41 +0100936 virtual ~V8HeapExplorer();
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100937 virtual HeapEntry* AllocateEntry(
938 HeapThing ptr, int children_count, int retainers_count);
939 void AddRootEntries(SnapshotFillerInterface* filler);
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000940 int EstimateObjectsCount(HeapIterator* iterator);
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100941 bool IterateAndExtractReferences(SnapshotFillerInterface* filler);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +0000942 void TagGlobalObjects();
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100943
Ben Murdoch69a99ed2011-11-30 16:03:39 +0000944 static String* GetConstructorName(JSObject* object);
945
Steve Block44f0eee2011-05-26 01:26:41 +0100946 static HeapObject* const kInternalRootObject;
947
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100948 private:
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100949 HeapEntry* AddEntry(
950 HeapObject* object, int children_count, int retainers_count);
951 HeapEntry* AddEntry(HeapObject* object,
952 HeapEntry::Type type,
953 const char* name,
954 int children_count,
955 int retainers_count);
Ben Murdoch8b112d22011-06-08 16:22:53 +0100956 const char* GetSystemEntryName(HeapObject* object);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +0100957 void ExtractReferences(HeapObject* obj);
958 void ExtractClosureReferences(JSObject* js_obj, HeapEntry* entry);
959 void ExtractPropertyReferences(JSObject* js_obj, HeapEntry* entry);
960 void ExtractElementReferences(JSObject* js_obj, HeapEntry* entry);
Ben Murdochf87a2032010-10-22 12:50:53 +0100961 void ExtractInternalReferences(JSObject* js_obj, HeapEntry* entry);
Iain Merrick75681382010-08-19 15:07:18 +0100962 void SetClosureReference(HeapObject* parent_obj,
963 HeapEntry* parent,
964 String* reference_name,
965 Object* child);
966 void SetElementReference(HeapObject* parent_obj,
967 HeapEntry* parent,
968 int index,
969 Object* child);
970 void SetInternalReference(HeapObject* parent_obj,
971 HeapEntry* parent,
972 const char* reference_name,
Steve Block44f0eee2011-05-26 01:26:41 +0100973 Object* child,
974 int field_offset = -1);
Ben Murdochf87a2032010-10-22 12:50:53 +0100975 void SetInternalReference(HeapObject* parent_obj,
976 HeapEntry* parent,
977 int index,
Steve Block44f0eee2011-05-26 01:26:41 +0100978 Object* child,
979 int field_offset = -1);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800980 void SetHiddenReference(HeapObject* parent_obj,
981 HeapEntry* parent,
982 int index,
983 Object* child);
Ben Murdochc7cc0282012-03-05 14:35:55 +0000984 void SetWeakReference(HeapObject* parent_obj,
985 HeapEntry* parent_entry,
986 int index,
987 Object* child_obj,
988 int field_offset);
Iain Merrick75681382010-08-19 15:07:18 +0100989 void SetPropertyReference(HeapObject* parent_obj,
990 HeapEntry* parent,
991 String* reference_name,
Steve Block44f0eee2011-05-26 01:26:41 +0100992 Object* child,
Ben Murdoch592a9fc2012-03-05 11:04:45 +0000993 const char* name_format_string = NULL,
Steve Block44f0eee2011-05-26 01:26:41 +0100994 int field_offset = -1);
Shimeng (Simon) Wang8a31eba2010-12-06 19:01:33 -0800995 void SetPropertyShortcutReference(HeapObject* parent_obj,
996 HeapEntry* parent,
997 String* reference_name,
998 Object* child);
999 void SetRootShortcutReference(Object* child);
1000 void SetRootGcRootsReference();
Ben Murdochc7cc0282012-03-05 14:35:55 +00001001 void SetGcRootsReference(VisitorSynchronization::SyncTag tag);
1002 void SetGcSubrootReference(
1003 VisitorSynchronization::SyncTag tag, bool is_weak, Object* child);
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001004 void TagObject(Object* obj, const char* tag);
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001005
1006 HeapEntry* GetEntry(Object* obj);
1007
Ben Murdochc7cc0282012-03-05 14:35:55 +00001008 static inline HeapObject* GetNthGcSubrootObject(int delta);
1009 static inline int GetGcSubrootOrder(HeapObject* subroot);
1010
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001011 Heap* heap_;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001012 HeapSnapshot* snapshot_;
1013 HeapSnapshotsCollection* collection_;
1014 SnapshottingProgressReportingInterface* progress_;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001015 SnapshotFillerInterface* filler_;
Ben Murdoch3fb3ca82011-12-02 17:19:32 +00001016 HeapObjectsSet objects_tags_;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001017
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001018 static HeapObject* const kGcRootsObject;
Ben Murdochc7cc0282012-03-05 14:35:55 +00001019 static HeapObject* const kFirstGcSubrootObject;
1020 static HeapObject* const kLastGcSubrootObject;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001021
1022 friend class IndexedReferencesExtractor;
Ben Murdochc7cc0282012-03-05 14:35:55 +00001023 friend class GcSubrootsEnumerator;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001024 friend class RootsReferencesExtractor;
1025
1026 DISALLOW_COPY_AND_ASSIGN(V8HeapExplorer);
1027};
1028
1029
Steve Block44f0eee2011-05-26 01:26:41 +01001030// An implementation of retained native objects extractor.
1031class NativeObjectsExplorer : public HeapEntriesAllocator {
1032 public:
1033 NativeObjectsExplorer(HeapSnapshot* snapshot,
1034 SnapshottingProgressReportingInterface* progress);
1035 virtual ~NativeObjectsExplorer();
1036 virtual HeapEntry* AllocateEntry(
1037 HeapThing ptr, int children_count, int retainers_count);
1038 void AddRootEntries(SnapshotFillerInterface* filler);
1039 int EstimateObjectsCount();
1040 bool IterateAndExtractReferences(SnapshotFillerInterface* filler);
1041
1042 private:
1043 void FillRetainedObjects();
1044 List<HeapObject*>* GetListMaybeDisposeInfo(v8::RetainedObjectInfo* info);
1045 void SetNativeRootReference(v8::RetainedObjectInfo* info);
1046 void SetRootNativesRootReference();
1047 void SetWrapperNativeReferences(HeapObject* wrapper,
1048 v8::RetainedObjectInfo* info);
1049 void VisitSubtreeWrapper(Object** p, uint16_t class_id);
1050
1051 static uint32_t InfoHash(v8::RetainedObjectInfo* info) {
Ben Murdochc7cc0282012-03-05 14:35:55 +00001052 return ComputeIntegerHash(static_cast<uint32_t>(info->GetHash()),
1053 v8::internal::kZeroHashSeed);
Steve Block44f0eee2011-05-26 01:26:41 +01001054 }
1055 static bool RetainedInfosMatch(void* key1, void* key2) {
1056 return key1 == key2 ||
1057 (reinterpret_cast<v8::RetainedObjectInfo*>(key1))->IsEquivalent(
1058 reinterpret_cast<v8::RetainedObjectInfo*>(key2));
1059 }
1060
1061 HeapSnapshot* snapshot_;
1062 HeapSnapshotsCollection* collection_;
1063 SnapshottingProgressReportingInterface* progress_;
1064 bool embedder_queried_;
1065 HeapObjectsSet in_groups_;
1066 // RetainedObjectInfo* -> List<HeapObject*>*
1067 HashMap objects_by_info_;
1068 // Used during references extraction.
1069 SnapshotFillerInterface* filler_;
1070
1071 static HeapThing const kNativesRootObject;
1072
1073 friend class GlobalHandlesExtractor;
1074
1075 DISALLOW_COPY_AND_ASSIGN(NativeObjectsExplorer);
1076};
1077
1078
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001079class HeapSnapshotGenerator : public SnapshottingProgressReportingInterface {
1080 public:
1081 HeapSnapshotGenerator(HeapSnapshot* snapshot,
1082 v8::ActivityControl* control);
1083 bool GenerateSnapshot();
1084
1085 private:
1086 bool ApproximateRetainedSizes();
1087 bool BuildDominatorTree(const Vector<HeapEntry*>& entries,
1088 Vector<HeapEntry*>* dominators);
1089 bool CountEntriesAndReferences();
1090 bool FillReferences();
1091 void FillReversePostorderIndexes(Vector<HeapEntry*>* entries);
1092 void ProgressStep();
1093 bool ProgressReport(bool force = false);
1094 bool SetEntriesDominators();
Ben Murdochb0fe1622011-05-05 13:52:32 +01001095 void SetProgressTotal(int iterations_count);
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001096
1097 HeapSnapshot* snapshot_;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001098 v8::ActivityControl* control_;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001099 V8HeapExplorer v8_heap_explorer_;
Steve Block44f0eee2011-05-26 01:26:41 +01001100 NativeObjectsExplorer dom_explorer_;
Ben Murdoche0cee9b2011-05-25 10:26:03 +01001101 // Mapping from HeapThing pointers to HeapEntry* pointers.
Iain Merrick75681382010-08-19 15:07:18 +01001102 HeapEntriesMap entries_;
Ben Murdochb0fe1622011-05-05 13:52:32 +01001103 // Used during snapshot generation.
1104 int progress_counter_;
1105 int progress_total_;
Iain Merrick75681382010-08-19 15:07:18 +01001106
Ben Murdoch7f4d5bd2010-06-15 11:15:29 +01001107 DISALLOW_COPY_AND_ASSIGN(HeapSnapshotGenerator);
1108};
1109
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001110class OutputStreamWriter;
1111
1112class HeapSnapshotJSONSerializer {
1113 public:
1114 explicit HeapSnapshotJSONSerializer(HeapSnapshot* snapshot)
1115 : snapshot_(snapshot),
1116 nodes_(ObjectsMatch),
1117 strings_(ObjectsMatch),
1118 next_node_id_(1),
1119 next_string_id_(1),
1120 writer_(NULL) {
1121 }
1122 void Serialize(v8::OutputStream* stream);
1123
1124 private:
1125 INLINE(static bool ObjectsMatch(void* key1, void* key2)) {
1126 return key1 == key2;
1127 }
1128
1129 INLINE(static uint32_t ObjectHash(const void* key)) {
1130 return ComputeIntegerHash(
Ben Murdochc7cc0282012-03-05 14:35:55 +00001131 static_cast<uint32_t>(reinterpret_cast<uintptr_t>(key)),
1132 v8::internal::kZeroHashSeed);
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001133 }
1134
1135 void EnumerateNodes();
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001136 HeapSnapshot* CreateFakeSnapshot();
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001137 int GetNodeId(HeapEntry* entry);
1138 int GetStringId(const char* s);
1139 void SerializeEdge(HeapGraphEdge* edge);
1140 void SerializeImpl();
1141 void SerializeNode(HeapEntry* entry);
1142 void SerializeNodes();
1143 void SerializeSnapshot();
1144 void SerializeString(const unsigned char* s);
1145 void SerializeStrings();
1146 void SortHashMap(HashMap* map, List<HashMap::Entry*>* sorted_entries);
1147
Ben Murdoch69a99ed2011-11-30 16:03:39 +00001148 static const int kMaxSerializableSnapshotRawSize;
1149
Kristian Monsen0d5e1162010-09-30 15:31:59 +01001150 HeapSnapshot* snapshot_;
1151 HashMap nodes_;
1152 HashMap strings_;
1153 int next_node_id_;
1154 int next_string_id_;
1155 OutputStreamWriter* writer_;
1156
1157 friend class HeapSnapshotJSONSerializerEnumerator;
1158 friend class HeapSnapshotJSONSerializerIterator;
1159
1160 DISALLOW_COPY_AND_ASSIGN(HeapSnapshotJSONSerializer);
1161};
1162
Steve Block6ded16b2010-05-10 14:33:55 +01001163} } // namespace v8::internal
1164
Steve Block6ded16b2010-05-10 14:33:55 +01001165#endif // V8_PROFILE_GENERATOR_H_