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/debug/debug-interface.h" 14 #include "src/globals.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(); 31 32 HeapSnapshot* TakeSnapshot( 33 v8::ActivityControl* control, 34 v8::HeapProfiler::ObjectNameResolver* resolver); 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(); 53 HeapSnapshot* GetSnapshot(int index); 54 SnapshotObjectId GetSnapshotObjectId(Handle<Object> obj); 55 void DeleteAllSnapshots(); 56 void RemoveSnapshot(HeapSnapshot* snapshot); 57 58 void ObjectMoveEvent(Address from, Address to, int size); 59 60 void AllocationEvent(Address addr, int size) override; 61 62 void UpdateObjectSizeEvent(Address addr, int size) override; 63 64 void DefineWrapperClass( 65 uint16_t class_id, v8::HeapProfiler::WrapperInfoCallback callback); 66 67 v8::RetainedObjectInfo* ExecuteWrapperClassCallback(uint16_t class_id, 68 Object** wrapper); 69 70 void SetGetRetainerInfosCallback( 71 v8::HeapProfiler::GetRetainerInfosCallback callback); 72 v8::HeapProfiler::RetainerInfos GetRetainerInfos(Isolate* isolate); 73 74 void AddBuildEmbedderGraphCallback( 75 v8::HeapProfiler::BuildEmbedderGraphCallback callback, void* data); 76 void RemoveBuildEmbedderGraphCallback( 77 v8::HeapProfiler::BuildEmbedderGraphCallback callback, void* data); 78 void BuildEmbedderGraph(Isolate* isolate, v8::EmbedderGraph* graph); HasBuildEmbedderGraphCallback()79 bool HasBuildEmbedderGraphCallback() { 80 return !build_embedder_graph_callbacks_.empty(); 81 } 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 Heap* heap() const; 96 97 // Mapping from HeapObject addresses to objects' uids. 98 std::unique_ptr<HeapObjectsMap> ids_; 99 std::vector<std::unique_ptr<HeapSnapshot>> snapshots_; 100 std::unique_ptr<StringsStorage> names_; 101 std::vector<v8::HeapProfiler::WrapperInfoCallback> wrapper_callbacks_; 102 std::unique_ptr<AllocationTracker> allocation_tracker_; 103 bool is_tracking_object_moves_; 104 base::Mutex profiler_mutex_; 105 std::unique_ptr<SamplingHeapProfiler> sampling_heap_profiler_; 106 v8::HeapProfiler::GetRetainerInfosCallback get_retainer_infos_callback_ = 107 nullptr; 108 std::vector<std::pair<v8::HeapProfiler::BuildEmbedderGraphCallback, void*>> 109 build_embedder_graph_callbacks_; 110 111 DISALLOW_COPY_AND_ASSIGN(HeapProfiler); 112 }; 113 114 } // namespace internal 115 } // namespace v8 116 117 #endif // V8_PROFILER_HEAP_PROFILER_H_ 118