heap-profiler.h 3.96 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 13 14 15
#include "include/v8-profiler.h"
#include "src/base/platform/mutex.h"
#include "src/debug/debug-interface.h"
#include "src/globals.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
  HeapSnapshot* TakeSnapshot(
33 34
      v8::ActivityControl* control,
      v8::HeapProfiler::ObjectNameResolver* resolver);
35

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

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

50 51
  SnapshotObjectId PushHeapObjectsStats(OutputStream* stream,
                                        int64_t* timestamp_us);
52 53 54 55
  int GetSnapshotsCount();
  HeapSnapshot* GetSnapshot(int index);
  SnapshotObjectId GetSnapshotObjectId(Handle<Object> obj);
  void DeleteAllSnapshots();
56
  void RemoveSnapshot(HeapSnapshot* snapshot);
57

58 59
  void ObjectMoveEvent(Address from, Address to, int size);

60
  void AllocationEvent(Address addr, int size) override;
61

62
  void UpdateObjectSizeEvent(Address addr, int size) override;
63

64
  void DefineWrapperClass(
65 66
      uint16_t class_id, v8::HeapProfiler::WrapperInfoCallback callback);

67
  v8::RetainedObjectInfo* ExecuteWrapperClassCallback(uint16_t class_id,
68
                                                      Handle<Object> wrapper);
69

70 71 72 73
  void SetGetRetainerInfosCallback(
      v8::HeapProfiler::GetRetainerInfosCallback callback);
  v8::HeapProfiler::RetainerInfos GetRetainerInfos(Isolate* isolate);

74 75 76 77
  void AddBuildEmbedderGraphCallback(
      v8::HeapProfiler::BuildEmbedderGraphCallback callback, void* data);
  void RemoveBuildEmbedderGraphCallback(
      v8::HeapProfiler::BuildEmbedderGraphCallback callback, void* data);
78 79
  void BuildEmbedderGraph(Isolate* isolate, v8::EmbedderGraph* graph);
  bool HasBuildEmbedderGraphCallback() {
80
    return !build_embedder_graph_callbacks_.empty();
81 82
  }

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 91 92 93
  void QueryObjects(Handle<Context> context,
                    debug::QueryObjectPredicate* predicate,
                    v8::PersistentValueVector<v8::Object>* objects);

94
 private:
95 96
  void MaybeClearStringsStorage();

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::vector<v8::HeapProfiler::WrapperInfoCallback> wrapper_callbacks_;
104
  std::unique_ptr<AllocationTracker> allocation_tracker_;
105
  bool is_tracking_object_moves_;
106
  base::Mutex profiler_mutex_;
107
  std::unique_ptr<SamplingHeapProfiler> sampling_heap_profiler_;
108 109
  v8::HeapProfiler::GetRetainerInfosCallback get_retainer_infos_callback_ =
      nullptr;
110 111
  std::vector<std::pair<v8::HeapProfiler::BuildEmbedderGraphCallback, void*>>
      build_embedder_graph_callbacks_;
112 113

  DISALLOW_COPY_AND_ASSIGN(HeapProfiler);
114 115
};

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

119
#endif  // V8_PROFILER_HEAP_PROFILER_H_