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
17namespace v8 {
18namespace internal {
19
20// Forward declarations.
21class AllocationTracker;
22class HeapObjectsMap;
23class HeapSnapshot;
24class SamplingHeapProfiler;
25class StringsStorage;
26
27class HeapProfiler : public HeapObjectAllocationTracker {
28 public:
29  explicit HeapProfiler(Heap* heap);
30  ~HeapProfiler() override;
31  HeapProfiler(const HeapProfiler&) = delete;
32  HeapProfiler& operator=(const HeapProfiler&) = delete;
33
34  HeapSnapshot* TakeSnapshot(v8::ActivityControl* control,
35                             v8::HeapProfiler::ObjectNameResolver* resolver,
36                             bool treat_global_objects_as_roots,
37                             bool capture_numeric_value);
38
39  bool StartSamplingHeapProfiler(uint64_t sample_interval, int stack_depth,
40                                 v8::HeapProfiler::SamplingFlags);
41  void StopSamplingHeapProfiler();
42  bool is_sampling_allocations() { return !!sampling_heap_profiler_; }
43  AllocationProfile* GetAllocationProfile();
44
45  void StartHeapObjectsTracking(bool track_allocations);
46  void StopHeapObjectsTracking();
47  AllocationTracker* allocation_tracker() const {
48    return allocation_tracker_.get();
49  }
50  HeapObjectsMap* heap_object_map() const { return ids_.get(); }
51  StringsStorage* names() const { return names_.get(); }
52
53  SnapshotObjectId PushHeapObjectsStats(OutputStream* stream,
54                                        int64_t* timestamp_us);
55  int GetSnapshotsCount() const;
56  bool IsTakingSnapshot() const;
57  HeapSnapshot* GetSnapshot(int index);
58  SnapshotObjectId GetSnapshotObjectId(Handle<Object> obj);
59  SnapshotObjectId GetSnapshotObjectId(NativeObject obj);
60  void DeleteAllSnapshots();
61  void RemoveSnapshot(HeapSnapshot* snapshot);
62
63  void ObjectMoveEvent(Address from, Address to, int size);
64
65  void AllocationEvent(Address addr, int size) override;
66
67  void UpdateObjectSizeEvent(Address addr, int size) override;
68
69  void AddBuildEmbedderGraphCallback(
70      v8::HeapProfiler::BuildEmbedderGraphCallback callback, void* data);
71  void RemoveBuildEmbedderGraphCallback(
72      v8::HeapProfiler::BuildEmbedderGraphCallback callback, void* data);
73  void BuildEmbedderGraph(Isolate* isolate, v8::EmbedderGraph* graph);
74  bool HasBuildEmbedderGraphCallback() {
75    return !build_embedder_graph_callbacks_.empty();
76  }
77
78  void SetGetDetachednessCallback(
79      v8::HeapProfiler::GetDetachednessCallback callback, void* data);
80  bool HasGetDetachednessCallback() const {
81    return get_detachedness_callback_.first != nullptr;
82  }
83  v8::EmbedderGraph::Node::Detachedness GetDetachedness(
84      const v8::Local<v8::Value> v8_value, uint16_t class_id);
85
86  bool is_tracking_object_moves() const { return is_tracking_object_moves_; }
87
88  Handle<HeapObject> FindHeapObjectById(SnapshotObjectId id);
89  void ClearHeapObjectMap();
90
91  Isolate* isolate() const;
92
93  void QueryObjects(Handle<Context> context,
94                    debug::QueryObjectPredicate* predicate,
95                    v8::PersistentValueVector<v8::Object>* objects);
96
97 private:
98  void MaybeClearStringsStorage();
99
100  Heap* heap() const;
101
102  // Mapping from HeapObject addresses to objects' uids.
103  std::unique_ptr<HeapObjectsMap> ids_;
104  std::vector<std::unique_ptr<HeapSnapshot>> snapshots_;
105  std::unique_ptr<StringsStorage> names_;
106  std::unique_ptr<AllocationTracker> allocation_tracker_;
107  bool is_tracking_object_moves_;
108  bool is_taking_snapshot_;
109  base::Mutex profiler_mutex_;
110  std::unique_ptr<SamplingHeapProfiler> sampling_heap_profiler_;
111  std::vector<std::pair<v8::HeapProfiler::BuildEmbedderGraphCallback, void*>>
112      build_embedder_graph_callbacks_;
113  std::pair<v8::HeapProfiler::GetDetachednessCallback, void*>
114      get_detachedness_callback_;
115};
116
117}  // namespace internal
118}  // namespace v8
119
120#endif  // V8_PROFILER_HEAP_PROFILER_H_
121