1 // Copyright 2009-2010 the V8 project authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef V8_PROFILER_HEAP_PROFILER_H_ 6 #define V8_PROFILER_HEAP_PROFILER_H_ 7 8 #include <memory> 9 #include <vector> 10 11 #include "include/v8-profiler.h" 12 #include "src/base/platform/mutex.h" 13 #include "src/common/globals.h" 14 #include "src/debug/debug-interface.h" 15 #include "src/heap/heap.h" 16 17 namespace v8 { 18 namespace internal { 19 20 // Forward declarations. 21 class AllocationTracker; 22 class HeapObjectsMap; 23 class HeapSnapshot; 24 class SamplingHeapProfiler; 25 class StringsStorage; 26 27 class HeapProfiler : public HeapObjectAllocationTracker { 28 public: 29 explicit HeapProfiler(Heap* heap); 30 ~HeapProfiler() override; 31 32 HeapSnapshot* TakeSnapshot(v8::ActivityControl* control, 33 v8::HeapProfiler::ObjectNameResolver* resolver, 34 bool treat_global_objects_as_roots); 35 36 bool StartSamplingHeapProfiler(uint64_t sample_interval, int stack_depth, 37 v8::HeapProfiler::SamplingFlags); 38 void StopSamplingHeapProfiler(); is_sampling_allocations()39 bool is_sampling_allocations() { return !!sampling_heap_profiler_; } 40 AllocationProfile* GetAllocationProfile(); 41 42 void StartHeapObjectsTracking(bool track_allocations); 43 void StopHeapObjectsTracking(); allocation_tracker()44 AllocationTracker* allocation_tracker() const { 45 return allocation_tracker_.get(); 46 } heap_object_map()47 HeapObjectsMap* heap_object_map() const { return ids_.get(); } names()48 StringsStorage* names() const { return names_.get(); } 49 50 SnapshotObjectId PushHeapObjectsStats(OutputStream* stream, 51 int64_t* timestamp_us); 52 int GetSnapshotsCount() const; 53 bool IsTakingSnapshot() const; 54 HeapSnapshot* GetSnapshot(int index); 55 SnapshotObjectId GetSnapshotObjectId(Handle<Object> obj); 56 SnapshotObjectId GetSnapshotObjectId(NativeObject obj); 57 void DeleteAllSnapshots(); 58 void RemoveSnapshot(HeapSnapshot* snapshot); 59 60 void ObjectMoveEvent(Address from, Address to, int size); 61 62 void AllocationEvent(Address addr, int size) override; 63 64 void UpdateObjectSizeEvent(Address addr, int size) override; 65 66 void AddBuildEmbedderGraphCallback( 67 v8::HeapProfiler::BuildEmbedderGraphCallback callback, void* data); 68 void RemoveBuildEmbedderGraphCallback( 69 v8::HeapProfiler::BuildEmbedderGraphCallback callback, void* data); 70 void BuildEmbedderGraph(Isolate* isolate, v8::EmbedderGraph* graph); HasBuildEmbedderGraphCallback()71 bool HasBuildEmbedderGraphCallback() { 72 return !build_embedder_graph_callbacks_.empty(); 73 } 74 75 void SetGetDetachednessCallback( 76 v8::HeapProfiler::GetDetachednessCallback callback, void* data); HasGetDetachednessCallback()77 bool HasGetDetachednessCallback() const { 78 return get_detachedness_callback_.first != nullptr; 79 } 80 v8::EmbedderGraph::Node::Detachedness GetDetachedness( 81 const v8::Local<v8::Value> v8_value, uint16_t class_id); 82 is_tracking_object_moves()83 bool is_tracking_object_moves() const { return is_tracking_object_moves_; } 84 85 Handle<HeapObject> FindHeapObjectById(SnapshotObjectId id); 86 void ClearHeapObjectMap(); 87 88 Isolate* isolate() const; 89 90 void QueryObjects(Handle<Context> context, 91 debug::QueryObjectPredicate* predicate, 92 v8::PersistentValueVector<v8::Object>* objects); 93 94 private: 95 void MaybeClearStringsStorage(); 96 97 Heap* heap() const; 98 99 // Mapping from HeapObject addresses to objects' uids. 100 std::unique_ptr<HeapObjectsMap> ids_; 101 std::vector<std::unique_ptr<HeapSnapshot>> snapshots_; 102 std::unique_ptr<StringsStorage> names_; 103 std::unique_ptr<AllocationTracker> allocation_tracker_; 104 bool is_tracking_object_moves_; 105 bool is_taking_snapshot_; 106 base::Mutex profiler_mutex_; 107 std::unique_ptr<SamplingHeapProfiler> sampling_heap_profiler_; 108 std::vector<std::pair<v8::HeapProfiler::BuildEmbedderGraphCallback, void*>> 109 build_embedder_graph_callbacks_; 110 std::pair<v8::HeapProfiler::GetDetachednessCallback, void*> 111 get_detachedness_callback_; 112 113 DISALLOW_COPY_AND_ASSIGN(HeapProfiler); 114 }; 115 116 } // namespace internal 117 } // namespace v8 118 119 #endif // V8_PROFILER_HEAP_PROFILER_H_ 120