heap-profiler.h 4.07 KB
Newer Older
1
// Copyright 2009-2010 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5 6
#ifndef V8_PROFILER_HEAP_PROFILER_H_
#define V8_PROFILER_HEAP_PROFILER_H_
7

8
#include <memory>
9
#include <vector>
10

11 12
#include "include/v8-profiler.h"
#include "src/base/platform/mutex.h"
13
#include "src/common/globals.h"
14 15
#include "src/debug/debug-interface.h"
#include "src/heap/heap.h"
16

17 18 19
namespace v8 {
namespace internal {

20 21 22
// Forward declarations.
class AllocationTracker;
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
  HeapProfiler(const HeapProfiler&) = delete;
  HeapProfiler& operator=(const HeapProfiler&) = delete;
33

34 35 36
  HeapSnapshot* TakeSnapshot(v8::ActivityControl* control,
                             v8::HeapProfiler::ObjectNameResolver* resolver,
                             bool treat_global_objects_as_roots);
37

38 39
  bool StartSamplingHeapProfiler(uint64_t sample_interval, int stack_depth,
                                 v8::HeapProfiler::SamplingFlags);
40
  void StopSamplingHeapProfiler();
41
  bool is_sampling_allocations() { return !!sampling_heap_profiler_; }
42 43
  AllocationProfile* GetAllocationProfile();

44
  void StartHeapObjectsTracking(bool track_allocations);
45
  void StopHeapObjectsTracking();
46 47 48 49 50
  AllocationTracker* allocation_tracker() const {
    return allocation_tracker_.get();
  }
  HeapObjectsMap* heap_object_map() const { return ids_.get(); }
  StringsStorage* names() const { return names_.get(); }
51

52 53
  SnapshotObjectId PushHeapObjectsStats(OutputStream* stream,
                                        int64_t* timestamp_us);
54 55
  int GetSnapshotsCount() const;
  bool IsTakingSnapshot() const;
56 57
  HeapSnapshot* GetSnapshot(int index);
  SnapshotObjectId GetSnapshotObjectId(Handle<Object> obj);
58
  SnapshotObjectId GetSnapshotObjectId(NativeObject obj);
59
  void DeleteAllSnapshots();
60
  void RemoveSnapshot(HeapSnapshot* snapshot);
61

62 63
  void ObjectMoveEvent(Address from, Address to, int size);

64
  void AllocationEvent(Address addr, int size) override;
65

66
  void UpdateObjectSizeEvent(Address addr, int size) override;
67

68 69 70 71
  void AddBuildEmbedderGraphCallback(
      v8::HeapProfiler::BuildEmbedderGraphCallback callback, void* data);
  void RemoveBuildEmbedderGraphCallback(
      v8::HeapProfiler::BuildEmbedderGraphCallback callback, void* data);
72 73
  void BuildEmbedderGraph(Isolate* isolate, v8::EmbedderGraph* graph);
  bool HasBuildEmbedderGraphCallback() {
74
    return !build_embedder_graph_callbacks_.empty();
75 76
  }

77 78 79 80 81 82 83 84
  void SetGetDetachednessCallback(
      v8::HeapProfiler::GetDetachednessCallback callback, void* data);
  bool HasGetDetachednessCallback() const {
    return get_detachedness_callback_.first != nullptr;
  }
  v8::EmbedderGraph::Node::Detachedness GetDetachedness(
      const v8::Local<v8::Value> v8_value, uint16_t class_id);

85
  bool is_tracking_object_moves() const { return is_tracking_object_moves_; }
86

87
  Handle<HeapObject> FindHeapObjectById(SnapshotObjectId id);
88
  void ClearHeapObjectMap();
89

90
  Isolate* isolate() const;
91

92 93 94 95
  void QueryObjects(Handle<Context> context,
                    debug::QueryObjectPredicate* predicate,
                    v8::PersistentValueVector<v8::Object>* objects);

96
 private:
97 98
  void MaybeClearStringsStorage();

99
  Heap* heap() const;
100

101
  // Mapping from HeapObject addresses to objects' uids.
102
  std::unique_ptr<HeapObjectsMap> ids_;
103
  std::vector<std::unique_ptr<HeapSnapshot>> snapshots_;
104 105
  std::unique_ptr<StringsStorage> names_;
  std::unique_ptr<AllocationTracker> allocation_tracker_;
106
  bool is_tracking_object_moves_;
107
  bool is_taking_snapshot_;
108
  base::Mutex profiler_mutex_;
109
  std::unique_ptr<SamplingHeapProfiler> sampling_heap_profiler_;
110 111
  std::vector<std::pair<v8::HeapProfiler::BuildEmbedderGraphCallback, void*>>
      build_embedder_graph_callbacks_;
112 113
  std::pair<v8::HeapProfiler::GetDetachednessCallback, void*>
      get_detachedness_callback_;
114 115
};

116 117
}  // namespace internal
}  // namespace v8
118

119
#endif  // V8_PROFILER_HEAP_PROFILER_H_