ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 1 | // Copyright 2013 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #ifndef V8_HEAP_SNAPSHOT_GENERATOR_H_ |
| 29 | #define V8_HEAP_SNAPSHOT_GENERATOR_H_ |
| 30 | |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 31 | #include "profile-generator-inl.h" |
| 32 | |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 33 | namespace v8 { |
| 34 | namespace internal { |
| 35 | |
| 36 | class HeapEntry; |
| 37 | class HeapSnapshot; |
| 38 | |
| 39 | class HeapGraphEdge BASE_EMBEDDED { |
| 40 | public: |
| 41 | enum Type { |
| 42 | kContextVariable = v8::HeapGraphEdge::kContextVariable, |
| 43 | kElement = v8::HeapGraphEdge::kElement, |
| 44 | kProperty = v8::HeapGraphEdge::kProperty, |
| 45 | kInternal = v8::HeapGraphEdge::kInternal, |
| 46 | kHidden = v8::HeapGraphEdge::kHidden, |
| 47 | kShortcut = v8::HeapGraphEdge::kShortcut, |
| 48 | kWeak = v8::HeapGraphEdge::kWeak |
| 49 | }; |
| 50 | |
| 51 | HeapGraphEdge() { } |
| 52 | HeapGraphEdge(Type type, const char* name, int from, int to); |
| 53 | HeapGraphEdge(Type type, int index, int from, int to); |
| 54 | void ReplaceToIndexWithEntry(HeapSnapshot* snapshot); |
| 55 | |
| 56 | Type type() const { return static_cast<Type>(type_); } |
| 57 | int index() const { |
| 58 | ASSERT(type_ == kElement || type_ == kHidden || type_ == kWeak); |
| 59 | return index_; |
| 60 | } |
| 61 | const char* name() const { |
| 62 | ASSERT(type_ == kContextVariable |
| 63 | || type_ == kProperty |
| 64 | || type_ == kInternal |
| 65 | || type_ == kShortcut); |
| 66 | return name_; |
| 67 | } |
| 68 | INLINE(HeapEntry* from() const); |
| 69 | HeapEntry* to() const { return to_entry_; } |
| 70 | |
| 71 | private: |
| 72 | INLINE(HeapSnapshot* snapshot() const); |
| 73 | |
| 74 | unsigned type_ : 3; |
| 75 | int from_index_ : 29; |
| 76 | union { |
| 77 | // During entries population |to_index_| is used for storing the index, |
| 78 | // afterwards it is replaced with a pointer to the entry. |
| 79 | int to_index_; |
| 80 | HeapEntry* to_entry_; |
| 81 | }; |
| 82 | union { |
| 83 | int index_; |
| 84 | const char* name_; |
| 85 | }; |
| 86 | }; |
| 87 | |
| 88 | |
| 89 | // HeapEntry instances represent an entity from the heap (or a special |
| 90 | // virtual node, e.g. root). |
| 91 | class HeapEntry BASE_EMBEDDED { |
| 92 | public: |
| 93 | enum Type { |
| 94 | kHidden = v8::HeapGraphNode::kHidden, |
| 95 | kArray = v8::HeapGraphNode::kArray, |
| 96 | kString = v8::HeapGraphNode::kString, |
| 97 | kObject = v8::HeapGraphNode::kObject, |
| 98 | kCode = v8::HeapGraphNode::kCode, |
| 99 | kClosure = v8::HeapGraphNode::kClosure, |
| 100 | kRegExp = v8::HeapGraphNode::kRegExp, |
| 101 | kHeapNumber = v8::HeapGraphNode::kHeapNumber, |
| 102 | kNative = v8::HeapGraphNode::kNative, |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 103 | kSynthetic = v8::HeapGraphNode::kSynthetic, |
| 104 | kConsString = v8::HeapGraphNode::kConsString, |
| 105 | kSlicedString = v8::HeapGraphNode::kSlicedString |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 106 | }; |
| 107 | static const int kNoEntry; |
| 108 | |
| 109 | HeapEntry() { } |
| 110 | HeapEntry(HeapSnapshot* snapshot, |
| 111 | Type type, |
| 112 | const char* name, |
| 113 | SnapshotObjectId id, |
| 114 | int self_size); |
| 115 | |
| 116 | HeapSnapshot* snapshot() { return snapshot_; } |
| 117 | Type type() { return static_cast<Type>(type_); } |
| 118 | const char* name() { return name_; } |
| 119 | void set_name(const char* name) { name_ = name; } |
| 120 | inline SnapshotObjectId id() { return id_; } |
| 121 | int self_size() { return self_size_; } |
| 122 | INLINE(int index() const); |
| 123 | int children_count() const { return children_count_; } |
| 124 | INLINE(int set_children_index(int index)); |
| 125 | void add_child(HeapGraphEdge* edge) { |
| 126 | children_arr()[children_count_++] = edge; |
| 127 | } |
| 128 | Vector<HeapGraphEdge*> children() { |
| 129 | return Vector<HeapGraphEdge*>(children_arr(), children_count_); } |
| 130 | |
| 131 | void SetIndexedReference( |
| 132 | HeapGraphEdge::Type type, int index, HeapEntry* entry); |
| 133 | void SetNamedReference( |
| 134 | HeapGraphEdge::Type type, const char* name, HeapEntry* entry); |
| 135 | |
| 136 | void Print( |
| 137 | const char* prefix, const char* edge_name, int max_depth, int indent); |
| 138 | |
| 139 | Handle<HeapObject> GetHeapObject(); |
| 140 | |
| 141 | private: |
| 142 | INLINE(HeapGraphEdge** children_arr()); |
| 143 | const char* TypeAsString(); |
| 144 | |
| 145 | unsigned type_: 4; |
| 146 | int children_count_: 28; |
| 147 | int children_index_; |
| 148 | int self_size_; |
| 149 | SnapshotObjectId id_; |
| 150 | HeapSnapshot* snapshot_; |
| 151 | const char* name_; |
| 152 | }; |
| 153 | |
| 154 | |
| 155 | class HeapSnapshotsCollection; |
| 156 | |
| 157 | // HeapSnapshot represents a single heap snapshot. It is stored in |
| 158 | // HeapSnapshotsCollection, which is also a factory for |
| 159 | // HeapSnapshots. All HeapSnapshots share strings copied from JS heap |
| 160 | // to be able to return them even if they were collected. |
| 161 | // HeapSnapshotGenerator fills in a HeapSnapshot. |
| 162 | class HeapSnapshot { |
| 163 | public: |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 164 | HeapSnapshot(HeapSnapshotsCollection* collection, |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 165 | const char* title, |
| 166 | unsigned uid); |
| 167 | void Delete(); |
| 168 | |
| 169 | HeapSnapshotsCollection* collection() { return collection_; } |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 170 | const char* title() { return title_; } |
| 171 | unsigned uid() { return uid_; } |
| 172 | size_t RawSnapshotSize() const; |
| 173 | HeapEntry* root() { return &entries_[root_index_]; } |
| 174 | HeapEntry* gc_roots() { return &entries_[gc_roots_index_]; } |
| 175 | HeapEntry* natives_root() { return &entries_[natives_root_index_]; } |
| 176 | HeapEntry* gc_subroot(int index) { |
| 177 | return &entries_[gc_subroot_indexes_[index]]; |
| 178 | } |
| 179 | List<HeapEntry>& entries() { return entries_; } |
| 180 | List<HeapGraphEdge>& edges() { return edges_; } |
| 181 | List<HeapGraphEdge*>& children() { return children_; } |
| 182 | void RememberLastJSObjectId(); |
| 183 | SnapshotObjectId max_snapshot_js_object_id() const { |
| 184 | return max_snapshot_js_object_id_; |
| 185 | } |
| 186 | |
| 187 | HeapEntry* AddEntry(HeapEntry::Type type, |
| 188 | const char* name, |
| 189 | SnapshotObjectId id, |
| 190 | int size); |
| 191 | HeapEntry* AddRootEntry(); |
| 192 | HeapEntry* AddGcRootsEntry(); |
| 193 | HeapEntry* AddGcSubrootEntry(int tag); |
| 194 | HeapEntry* AddNativesRootEntry(); |
| 195 | HeapEntry* GetEntryById(SnapshotObjectId id); |
| 196 | List<HeapEntry*>* GetSortedEntriesList(); |
| 197 | void FillChildren(); |
| 198 | |
| 199 | void Print(int max_depth); |
| 200 | void PrintEntriesSize(); |
| 201 | |
| 202 | private: |
| 203 | HeapSnapshotsCollection* collection_; |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 204 | const char* title_; |
| 205 | unsigned uid_; |
| 206 | int root_index_; |
| 207 | int gc_roots_index_; |
| 208 | int natives_root_index_; |
| 209 | int gc_subroot_indexes_[VisitorSynchronization::kNumberOfSyncTags]; |
| 210 | List<HeapEntry> entries_; |
| 211 | List<HeapGraphEdge> edges_; |
| 212 | List<HeapGraphEdge*> children_; |
| 213 | List<HeapEntry*> sorted_entries_; |
| 214 | SnapshotObjectId max_snapshot_js_object_id_; |
| 215 | |
| 216 | friend class HeapSnapshotTester; |
| 217 | |
| 218 | DISALLOW_COPY_AND_ASSIGN(HeapSnapshot); |
| 219 | }; |
| 220 | |
| 221 | |
| 222 | class HeapObjectsMap { |
| 223 | public: |
| 224 | explicit HeapObjectsMap(Heap* heap); |
| 225 | |
| 226 | Heap* heap() const { return heap_; } |
| 227 | |
| 228 | void SnapshotGenerationFinished(); |
| 229 | SnapshotObjectId FindEntry(Address addr); |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 230 | SnapshotObjectId FindOrAddEntry(Address addr, |
| 231 | unsigned int size, |
| 232 | bool accessed = true); |
| 233 | void MoveObject(Address from, Address to, int size); |
| 234 | void NewObject(Address addr, int size); |
| 235 | void UpdateObjectSize(Address addr, int size); |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 236 | SnapshotObjectId last_assigned_id() const { |
| 237 | return next_id_ - kObjectIdStep; |
| 238 | } |
| 239 | |
| 240 | void StopHeapObjectsTracking(); |
| 241 | SnapshotObjectId PushHeapObjectsStats(OutputStream* stream); |
| 242 | size_t GetUsedMemorySize() const; |
| 243 | |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 244 | static SnapshotObjectId GenerateId(Heap* heap, v8::RetainedObjectInfo* info); |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 245 | static inline SnapshotObjectId GetNthGcSubrootId(int delta); |
| 246 | |
| 247 | static const int kObjectIdStep = 2; |
| 248 | static const SnapshotObjectId kInternalRootObjectId; |
| 249 | static const SnapshotObjectId kGcRootsObjectId; |
| 250 | static const SnapshotObjectId kNativesRootObjectId; |
| 251 | static const SnapshotObjectId kGcRootsFirstSubrootId; |
| 252 | static const SnapshotObjectId kFirstAvailableObjectId; |
| 253 | |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 254 | int FindUntrackedObjects(); |
| 255 | |
| 256 | void UpdateHeapObjectsMap(); |
| 257 | |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 258 | private: |
| 259 | struct EntryInfo { |
| 260 | EntryInfo(SnapshotObjectId id, Address addr, unsigned int size) |
| 261 | : id(id), addr(addr), size(size), accessed(true) { } |
| 262 | EntryInfo(SnapshotObjectId id, Address addr, unsigned int size, bool accessed) |
| 263 | : id(id), addr(addr), size(size), accessed(accessed) { } |
| 264 | SnapshotObjectId id; |
| 265 | Address addr; |
| 266 | unsigned int size; |
| 267 | bool accessed; |
| 268 | }; |
| 269 | struct TimeInterval { |
| 270 | explicit TimeInterval(SnapshotObjectId id) : id(id), size(0), count(0) { } |
| 271 | SnapshotObjectId id; |
| 272 | uint32_t size; |
| 273 | uint32_t count; |
| 274 | }; |
| 275 | |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 276 | void RemoveDeadEntries(); |
| 277 | |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 278 | SnapshotObjectId next_id_; |
| 279 | HashMap entries_map_; |
| 280 | List<EntryInfo> entries_; |
| 281 | List<TimeInterval> time_intervals_; |
| 282 | Heap* heap_; |
| 283 | |
| 284 | DISALLOW_COPY_AND_ASSIGN(HeapObjectsMap); |
| 285 | }; |
| 286 | |
| 287 | |
| 288 | class HeapSnapshotsCollection { |
| 289 | public: |
| 290 | explicit HeapSnapshotsCollection(Heap* heap); |
| 291 | ~HeapSnapshotsCollection(); |
| 292 | |
| 293 | Heap* heap() const { return ids_.heap(); } |
| 294 | |
| 295 | bool is_tracking_objects() { return is_tracking_objects_; } |
| 296 | SnapshotObjectId PushHeapObjectsStats(OutputStream* stream) { |
| 297 | return ids_.PushHeapObjectsStats(stream); |
| 298 | } |
| 299 | void StartHeapObjectsTracking() { is_tracking_objects_ = true; } |
| 300 | void StopHeapObjectsTracking() { ids_.StopHeapObjectsTracking(); } |
| 301 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 302 | HeapSnapshot* NewSnapshot(const char* name, unsigned uid); |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 303 | void SnapshotGenerationFinished(HeapSnapshot* snapshot); |
| 304 | List<HeapSnapshot*>* snapshots() { return &snapshots_; } |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 305 | void RemoveSnapshot(HeapSnapshot* snapshot); |
| 306 | |
| 307 | StringsStorage* names() { return &names_; } |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 308 | |
| 309 | SnapshotObjectId FindObjectId(Address object_addr) { |
| 310 | return ids_.FindEntry(object_addr); |
| 311 | } |
| 312 | SnapshotObjectId GetObjectId(Address object_addr, int object_size) { |
| 313 | return ids_.FindOrAddEntry(object_addr, object_size); |
| 314 | } |
| 315 | Handle<HeapObject> FindHeapObjectById(SnapshotObjectId id); |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 316 | void ObjectMoveEvent(Address from, Address to, int size) { |
| 317 | ids_.MoveObject(from, to, size); |
| 318 | } |
| 319 | void NewObjectEvent(Address addr, int size) { ids_.NewObject(addr, size); } |
| 320 | void UpdateObjectSizeEvent(Address addr, int size) { |
| 321 | ids_.UpdateObjectSize(addr, size); |
| 322 | } |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 323 | SnapshotObjectId last_assigned_id() const { |
| 324 | return ids_.last_assigned_id(); |
| 325 | } |
| 326 | size_t GetUsedMemorySize() const; |
| 327 | |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 328 | int FindUntrackedObjects() { return ids_.FindUntrackedObjects(); } |
| 329 | |
| 330 | void UpdateHeapObjectsMap() { ids_.UpdateHeapObjectsMap(); } |
| 331 | |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 332 | private: |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 333 | bool is_tracking_objects_; // Whether tracking object moves is needed. |
| 334 | List<HeapSnapshot*> snapshots_; |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 335 | StringsStorage names_; |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 336 | // Mapping from HeapObject addresses to objects' uids. |
| 337 | HeapObjectsMap ids_; |
| 338 | |
| 339 | DISALLOW_COPY_AND_ASSIGN(HeapSnapshotsCollection); |
| 340 | }; |
| 341 | |
| 342 | |
| 343 | // A typedef for referencing anything that can be snapshotted living |
| 344 | // in any kind of heap memory. |
| 345 | typedef void* HeapThing; |
| 346 | |
| 347 | |
| 348 | // An interface that creates HeapEntries by HeapThings. |
| 349 | class HeapEntriesAllocator { |
| 350 | public: |
| 351 | virtual ~HeapEntriesAllocator() { } |
| 352 | virtual HeapEntry* AllocateEntry(HeapThing ptr) = 0; |
| 353 | }; |
| 354 | |
| 355 | |
| 356 | // The HeapEntriesMap instance is used to track a mapping between |
| 357 | // real heap objects and their representations in heap snapshots. |
| 358 | class HeapEntriesMap { |
| 359 | public: |
| 360 | HeapEntriesMap(); |
| 361 | |
| 362 | int Map(HeapThing thing); |
| 363 | void Pair(HeapThing thing, int entry); |
| 364 | |
| 365 | private: |
| 366 | static uint32_t Hash(HeapThing thing) { |
| 367 | return ComputeIntegerHash( |
| 368 | static_cast<uint32_t>(reinterpret_cast<uintptr_t>(thing)), |
| 369 | v8::internal::kZeroHashSeed); |
| 370 | } |
| 371 | static bool HeapThingsMatch(HeapThing key1, HeapThing key2) { |
| 372 | return key1 == key2; |
| 373 | } |
| 374 | |
| 375 | HashMap entries_; |
| 376 | |
| 377 | friend class HeapObjectsSet; |
| 378 | |
| 379 | DISALLOW_COPY_AND_ASSIGN(HeapEntriesMap); |
| 380 | }; |
| 381 | |
| 382 | |
| 383 | class HeapObjectsSet { |
| 384 | public: |
| 385 | HeapObjectsSet(); |
| 386 | void Clear(); |
| 387 | bool Contains(Object* object); |
| 388 | void Insert(Object* obj); |
| 389 | const char* GetTag(Object* obj); |
| 390 | void SetTag(Object* obj, const char* tag); |
| 391 | bool is_empty() const { return entries_.occupancy() == 0; } |
| 392 | |
| 393 | private: |
| 394 | HashMap entries_; |
| 395 | |
| 396 | DISALLOW_COPY_AND_ASSIGN(HeapObjectsSet); |
| 397 | }; |
| 398 | |
| 399 | |
| 400 | // An interface used to populate a snapshot with nodes and edges. |
| 401 | class SnapshotFillerInterface { |
| 402 | public: |
| 403 | virtual ~SnapshotFillerInterface() { } |
| 404 | virtual HeapEntry* AddEntry(HeapThing ptr, |
| 405 | HeapEntriesAllocator* allocator) = 0; |
| 406 | virtual HeapEntry* FindEntry(HeapThing ptr) = 0; |
| 407 | virtual HeapEntry* FindOrAddEntry(HeapThing ptr, |
| 408 | HeapEntriesAllocator* allocator) = 0; |
| 409 | virtual void SetIndexedReference(HeapGraphEdge::Type type, |
| 410 | int parent_entry, |
| 411 | int index, |
| 412 | HeapEntry* child_entry) = 0; |
| 413 | virtual void SetIndexedAutoIndexReference(HeapGraphEdge::Type type, |
| 414 | int parent_entry, |
| 415 | HeapEntry* child_entry) = 0; |
| 416 | virtual void SetNamedReference(HeapGraphEdge::Type type, |
| 417 | int parent_entry, |
| 418 | const char* reference_name, |
| 419 | HeapEntry* child_entry) = 0; |
| 420 | virtual void SetNamedAutoIndexReference(HeapGraphEdge::Type type, |
| 421 | int parent_entry, |
| 422 | HeapEntry* child_entry) = 0; |
| 423 | }; |
| 424 | |
| 425 | |
| 426 | class SnapshottingProgressReportingInterface { |
| 427 | public: |
| 428 | virtual ~SnapshottingProgressReportingInterface() { } |
| 429 | virtual void ProgressStep() = 0; |
| 430 | virtual bool ProgressReport(bool force) = 0; |
| 431 | }; |
| 432 | |
| 433 | |
| 434 | // An implementation of V8 heap graph extractor. |
| 435 | class V8HeapExplorer : public HeapEntriesAllocator { |
| 436 | public: |
| 437 | V8HeapExplorer(HeapSnapshot* snapshot, |
| 438 | SnapshottingProgressReportingInterface* progress, |
| 439 | v8::HeapProfiler::ObjectNameResolver* resolver); |
| 440 | virtual ~V8HeapExplorer(); |
| 441 | virtual HeapEntry* AllocateEntry(HeapThing ptr); |
| 442 | void AddRootEntries(SnapshotFillerInterface* filler); |
| 443 | int EstimateObjectsCount(HeapIterator* iterator); |
| 444 | bool IterateAndExtractReferences(SnapshotFillerInterface* filler); |
| 445 | void TagGlobalObjects(); |
| 446 | |
| 447 | static String* GetConstructorName(JSObject* object); |
| 448 | |
| 449 | static HeapObject* const kInternalRootObject; |
| 450 | |
| 451 | private: |
| 452 | HeapEntry* AddEntry(HeapObject* object); |
| 453 | HeapEntry* AddEntry(HeapObject* object, |
| 454 | HeapEntry::Type type, |
| 455 | const char* name); |
| 456 | const char* GetSystemEntryName(HeapObject* object); |
| 457 | |
| 458 | void ExtractReferences(HeapObject* obj); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 459 | void ExtractJSGlobalProxyReferences(int entry, JSGlobalProxy* proxy); |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 460 | void ExtractJSObjectReferences(int entry, JSObject* js_obj); |
| 461 | void ExtractStringReferences(int entry, String* obj); |
| 462 | void ExtractContextReferences(int entry, Context* context); |
| 463 | void ExtractMapReferences(int entry, Map* map); |
| 464 | void ExtractSharedFunctionInfoReferences(int entry, |
| 465 | SharedFunctionInfo* shared); |
| 466 | void ExtractScriptReferences(int entry, Script* script); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 467 | void ExtractAccessorPairReferences(int entry, AccessorPair* accessors); |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 468 | void ExtractCodeCacheReferences(int entry, CodeCache* code_cache); |
| 469 | void ExtractCodeReferences(int entry, Code* code); |
danno@chromium.org | 4172848 | 2013-06-12 22:31:22 +0000 | [diff] [blame] | 470 | void ExtractCellReferences(int entry, Cell* cell); |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 471 | void ExtractPropertyCellReferences(int entry, PropertyCell* cell); |
danno@chromium.org | bee5199 | 2013-07-10 14:57:15 +0000 | [diff] [blame] | 472 | void ExtractAllocationSiteReferences(int entry, AllocationSite* site); |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 473 | void ExtractClosureReferences(JSObject* js_obj, int entry); |
| 474 | void ExtractPropertyReferences(JSObject* js_obj, int entry); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 475 | bool ExtractAccessorPairProperty(JSObject* js_obj, int entry, |
| 476 | Object* key, Object* callback_obj); |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 477 | void ExtractElementReferences(JSObject* js_obj, int entry); |
| 478 | void ExtractInternalReferences(JSObject* js_obj, int entry); |
| 479 | bool IsEssentialObject(Object* object); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 480 | void SetContextReference(HeapObject* parent_obj, |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 481 | int parent, |
| 482 | String* reference_name, |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 483 | Object* child, |
| 484 | int field_offset); |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 485 | void SetNativeBindReference(HeapObject* parent_obj, |
| 486 | int parent, |
| 487 | const char* reference_name, |
| 488 | Object* child); |
| 489 | void SetElementReference(HeapObject* parent_obj, |
| 490 | int parent, |
| 491 | int index, |
| 492 | Object* child); |
| 493 | void SetInternalReference(HeapObject* parent_obj, |
| 494 | int parent, |
| 495 | const char* reference_name, |
| 496 | Object* child, |
| 497 | int field_offset = -1); |
| 498 | void SetInternalReference(HeapObject* parent_obj, |
| 499 | int parent, |
| 500 | int index, |
| 501 | Object* child, |
| 502 | int field_offset = -1); |
| 503 | void SetHiddenReference(HeapObject* parent_obj, |
| 504 | int parent, |
| 505 | int index, |
| 506 | Object* child); |
| 507 | void SetWeakReference(HeapObject* parent_obj, |
| 508 | int parent, |
| 509 | int index, |
| 510 | Object* child_obj, |
| 511 | int field_offset); |
| 512 | void SetPropertyReference(HeapObject* parent_obj, |
| 513 | int parent, |
ulan@chromium.org | 750145a | 2013-03-07 15:14:13 +0000 | [diff] [blame] | 514 | Name* reference_name, |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 515 | Object* child, |
| 516 | const char* name_format_string = NULL, |
| 517 | int field_offset = -1); |
| 518 | void SetUserGlobalReference(Object* user_global); |
| 519 | void SetRootGcRootsReference(); |
| 520 | void SetGcRootsReference(VisitorSynchronization::SyncTag tag); |
| 521 | void SetGcSubrootReference( |
| 522 | VisitorSynchronization::SyncTag tag, bool is_weak, Object* child); |
| 523 | const char* GetStrongGcSubrootName(Object* object); |
| 524 | void TagObject(Object* obj, const char* tag); |
| 525 | |
| 526 | HeapEntry* GetEntry(Object* obj); |
| 527 | |
| 528 | static inline HeapObject* GetNthGcSubrootObject(int delta); |
| 529 | static inline int GetGcSubrootOrder(HeapObject* subroot); |
| 530 | |
| 531 | Heap* heap_; |
| 532 | HeapSnapshot* snapshot_; |
| 533 | HeapSnapshotsCollection* collection_; |
| 534 | SnapshottingProgressReportingInterface* progress_; |
| 535 | SnapshotFillerInterface* filler_; |
| 536 | HeapObjectsSet objects_tags_; |
| 537 | HeapObjectsSet strong_gc_subroot_names_; |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 538 | HeapObjectsSet user_roots_; |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 539 | v8::HeapProfiler::ObjectNameResolver* global_object_name_resolver_; |
| 540 | |
| 541 | static HeapObject* const kGcRootsObject; |
| 542 | static HeapObject* const kFirstGcSubrootObject; |
| 543 | static HeapObject* const kLastGcSubrootObject; |
| 544 | |
| 545 | friend class IndexedReferencesExtractor; |
| 546 | friend class GcSubrootsEnumerator; |
| 547 | friend class RootsReferencesExtractor; |
| 548 | |
| 549 | DISALLOW_COPY_AND_ASSIGN(V8HeapExplorer); |
| 550 | }; |
| 551 | |
| 552 | |
| 553 | class NativeGroupRetainedObjectInfo; |
| 554 | |
| 555 | |
| 556 | // An implementation of retained native objects extractor. |
| 557 | class NativeObjectsExplorer { |
| 558 | public: |
| 559 | NativeObjectsExplorer(HeapSnapshot* snapshot, |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 560 | SnapshottingProgressReportingInterface* progress); |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 561 | virtual ~NativeObjectsExplorer(); |
| 562 | void AddRootEntries(SnapshotFillerInterface* filler); |
| 563 | int EstimateObjectsCount(); |
| 564 | bool IterateAndExtractReferences(SnapshotFillerInterface* filler); |
| 565 | |
| 566 | private: |
| 567 | void FillRetainedObjects(); |
| 568 | void FillImplicitReferences(); |
| 569 | List<HeapObject*>* GetListMaybeDisposeInfo(v8::RetainedObjectInfo* info); |
| 570 | void SetNativeRootReference(v8::RetainedObjectInfo* info); |
| 571 | void SetRootNativeRootsReference(); |
| 572 | void SetWrapperNativeReferences(HeapObject* wrapper, |
| 573 | v8::RetainedObjectInfo* info); |
| 574 | void VisitSubtreeWrapper(Object** p, uint16_t class_id); |
| 575 | |
| 576 | static uint32_t InfoHash(v8::RetainedObjectInfo* info) { |
| 577 | return ComputeIntegerHash(static_cast<uint32_t>(info->GetHash()), |
| 578 | v8::internal::kZeroHashSeed); |
| 579 | } |
| 580 | static bool RetainedInfosMatch(void* key1, void* key2) { |
| 581 | return key1 == key2 || |
| 582 | (reinterpret_cast<v8::RetainedObjectInfo*>(key1))->IsEquivalent( |
| 583 | reinterpret_cast<v8::RetainedObjectInfo*>(key2)); |
| 584 | } |
| 585 | INLINE(static bool StringsMatch(void* key1, void* key2)) { |
| 586 | return strcmp(reinterpret_cast<char*>(key1), |
| 587 | reinterpret_cast<char*>(key2)) == 0; |
| 588 | } |
| 589 | |
| 590 | NativeGroupRetainedObjectInfo* FindOrAddGroupInfo(const char* label); |
| 591 | |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 592 | Isolate* isolate_; |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 593 | HeapSnapshot* snapshot_; |
| 594 | HeapSnapshotsCollection* collection_; |
| 595 | SnapshottingProgressReportingInterface* progress_; |
| 596 | bool embedder_queried_; |
| 597 | HeapObjectsSet in_groups_; |
| 598 | // RetainedObjectInfo* -> List<HeapObject*>* |
| 599 | HashMap objects_by_info_; |
| 600 | HashMap native_groups_; |
| 601 | HeapEntriesAllocator* synthetic_entries_allocator_; |
| 602 | HeapEntriesAllocator* native_entries_allocator_; |
| 603 | // Used during references extraction. |
| 604 | SnapshotFillerInterface* filler_; |
| 605 | |
| 606 | static HeapThing const kNativesRootObject; |
| 607 | |
| 608 | friend class GlobalHandlesExtractor; |
| 609 | |
| 610 | DISALLOW_COPY_AND_ASSIGN(NativeObjectsExplorer); |
| 611 | }; |
| 612 | |
| 613 | |
| 614 | class HeapSnapshotGenerator : public SnapshottingProgressReportingInterface { |
| 615 | public: |
| 616 | HeapSnapshotGenerator(HeapSnapshot* snapshot, |
| 617 | v8::ActivityControl* control, |
| 618 | v8::HeapProfiler::ObjectNameResolver* resolver, |
| 619 | Heap* heap); |
| 620 | bool GenerateSnapshot(); |
| 621 | |
| 622 | private: |
| 623 | bool FillReferences(); |
| 624 | void ProgressStep(); |
| 625 | bool ProgressReport(bool force = false); |
| 626 | void SetProgressTotal(int iterations_count); |
| 627 | |
| 628 | HeapSnapshot* snapshot_; |
| 629 | v8::ActivityControl* control_; |
| 630 | V8HeapExplorer v8_heap_explorer_; |
| 631 | NativeObjectsExplorer dom_explorer_; |
| 632 | // Mapping from HeapThing pointers to HeapEntry* pointers. |
| 633 | HeapEntriesMap entries_; |
| 634 | // Used during snapshot generation. |
| 635 | int progress_counter_; |
| 636 | int progress_total_; |
| 637 | Heap* heap_; |
| 638 | |
| 639 | DISALLOW_COPY_AND_ASSIGN(HeapSnapshotGenerator); |
| 640 | }; |
| 641 | |
| 642 | class OutputStreamWriter; |
| 643 | |
| 644 | class HeapSnapshotJSONSerializer { |
| 645 | public: |
| 646 | explicit HeapSnapshotJSONSerializer(HeapSnapshot* snapshot) |
| 647 | : snapshot_(snapshot), |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 648 | strings_(StringsMatch), |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 649 | next_node_id_(1), |
| 650 | next_string_id_(1), |
| 651 | writer_(NULL) { |
| 652 | } |
| 653 | void Serialize(v8::OutputStream* stream); |
| 654 | |
| 655 | private: |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 656 | INLINE(static bool StringsMatch(void* key1, void* key2)) { |
| 657 | return strcmp(reinterpret_cast<char*>(key1), |
| 658 | reinterpret_cast<char*>(key2)) == 0; |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 659 | } |
| 660 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 661 | INLINE(static uint32_t StringHash(const void* string)) { |
| 662 | const char* s = reinterpret_cast<const char*>(string); |
| 663 | int len = static_cast<int>(strlen(s)); |
| 664 | return StringHasher::HashSequentialString( |
| 665 | s, len, v8::internal::kZeroHashSeed); |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 666 | } |
| 667 | |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 668 | int GetStringId(const char* s); |
| 669 | int entry_index(HeapEntry* e) { return e->index() * kNodeFieldsCount; } |
| 670 | void SerializeEdge(HeapGraphEdge* edge, bool first_edge); |
| 671 | void SerializeEdges(); |
| 672 | void SerializeImpl(); |
| 673 | void SerializeNode(HeapEntry* entry); |
| 674 | void SerializeNodes(); |
| 675 | void SerializeSnapshot(); |
| 676 | void SerializeString(const unsigned char* s); |
| 677 | void SerializeStrings(); |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 678 | |
| 679 | static const int kEdgeFieldsCount; |
| 680 | static const int kNodeFieldsCount; |
| 681 | |
| 682 | HeapSnapshot* snapshot_; |
| 683 | HashMap strings_; |
| 684 | int next_node_id_; |
| 685 | int next_string_id_; |
| 686 | OutputStreamWriter* writer_; |
| 687 | |
| 688 | friend class HeapSnapshotJSONSerializerEnumerator; |
| 689 | friend class HeapSnapshotJSONSerializerIterator; |
| 690 | |
| 691 | DISALLOW_COPY_AND_ASSIGN(HeapSnapshotJSONSerializer); |
| 692 | }; |
| 693 | |
| 694 | |
| 695 | } } // namespace v8::internal |
| 696 | |
| 697 | #endif // V8_HEAP_SNAPSHOT_GENERATOR_H_ |
| 698 | |