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
10#include "src/isolate.h"
11#include "src/list.h"
12
13namespace v8 {
14namespace internal {
15
16// Forward declarations.
17class AllocationTracker;
18class HeapObjectsMap;
19class HeapSnapshot;
20class SamplingHeapProfiler;
21class StringsStorage;
22
23class HeapProfiler {
24 public:
25  explicit HeapProfiler(Heap* heap);
26  ~HeapProfiler();
27
28  size_t GetMemorySizeUsedByProfiler();
29
30  HeapSnapshot* TakeSnapshot(
31      v8::ActivityControl* control,
32      v8::HeapProfiler::ObjectNameResolver* resolver);
33
34  bool StartSamplingHeapProfiler(uint64_t sample_interval, int stack_depth,
35                                 v8::HeapProfiler::SamplingFlags);
36  void StopSamplingHeapProfiler();
37  bool is_sampling_allocations() { return !!sampling_heap_profiler_; }
38  AllocationProfile* GetAllocationProfile();
39
40  void StartHeapObjectsTracking(bool track_allocations);
41  void StopHeapObjectsTracking();
42  AllocationTracker* allocation_tracker() const {
43    return allocation_tracker_.get();
44  }
45  HeapObjectsMap* heap_object_map() const { return ids_.get(); }
46  StringsStorage* names() const { return names_.get(); }
47
48  SnapshotObjectId PushHeapObjectsStats(OutputStream* stream,
49                                        int64_t* timestamp_us);
50  int GetSnapshotsCount();
51  HeapSnapshot* GetSnapshot(int index);
52  SnapshotObjectId GetSnapshotObjectId(Handle<Object> obj);
53  void DeleteAllSnapshots();
54  void RemoveSnapshot(HeapSnapshot* snapshot);
55
56  void ObjectMoveEvent(Address from, Address to, int size);
57
58  void AllocationEvent(Address addr, int size);
59
60  void UpdateObjectSizeEvent(Address addr, int size);
61
62  void DefineWrapperClass(
63      uint16_t class_id, v8::HeapProfiler::WrapperInfoCallback callback);
64
65  v8::RetainedObjectInfo* ExecuteWrapperClassCallback(uint16_t class_id,
66                                                      Object** wrapper);
67  void SetRetainedObjectInfo(UniqueId id, RetainedObjectInfo* info);
68
69  void SetGetRetainerInfosCallback(
70      v8::HeapProfiler::GetRetainerInfosCallback callback);
71
72  v8::HeapProfiler::RetainerInfos GetRetainerInfos(Isolate* isolate);
73
74  bool is_tracking_object_moves() const { return is_tracking_object_moves_; }
75  bool is_tracking_allocations() const { return !!allocation_tracker_; }
76
77  Handle<HeapObject> FindHeapObjectById(SnapshotObjectId id);
78  void ClearHeapObjectMap();
79
80  Isolate* isolate() const { return heap()->isolate(); }
81
82 private:
83  Heap* heap() const;
84
85  // Mapping from HeapObject addresses to objects' uids.
86  std::unique_ptr<HeapObjectsMap> ids_;
87  List<HeapSnapshot*> snapshots_;
88  std::unique_ptr<StringsStorage> names_;
89  List<v8::HeapProfiler::WrapperInfoCallback> wrapper_callbacks_;
90  std::unique_ptr<AllocationTracker> allocation_tracker_;
91  bool is_tracking_object_moves_;
92  base::Mutex profiler_mutex_;
93  std::unique_ptr<SamplingHeapProfiler> sampling_heap_profiler_;
94  v8::HeapProfiler::GetRetainerInfosCallback get_retainer_infos_callback_;
95
96  DISALLOW_COPY_AND_ASSIGN(HeapProfiler);
97};
98
99}  // namespace internal
100}  // namespace v8
101
102#endif  // V8_PROFILER_HEAP_PROFILER_H_
103