V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
heap-profiler.cc
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 #include "src/profiler/heap-profiler.h"
6 
7 #include "src/api-inl.h"
8 #include "src/debug/debug.h"
9 #include "src/heap/heap-inl.h"
10 #include "src/profiler/allocation-tracker.h"
11 #include "src/profiler/heap-snapshot-generator-inl.h"
12 #include "src/profiler/sampling-heap-profiler.h"
13 
14 namespace v8 {
15 namespace internal {
16 
17 HeapProfiler::HeapProfiler(Heap* heap)
18  : ids_(new HeapObjectsMap(heap)),
19  names_(new StringsStorage()),
20  is_tracking_object_moves_(false) {}
21 
22 HeapProfiler::~HeapProfiler() = default;
23 
24 void HeapProfiler::DeleteAllSnapshots() {
25  snapshots_.clear();
26  MaybeClearStringsStorage();
27 }
28 
29 void HeapProfiler::MaybeClearStringsStorage() {
30  if (snapshots_.empty() && !sampling_heap_profiler_ && !allocation_tracker_) {
31  names_.reset(new StringsStorage());
32  }
33 }
34 
35 void HeapProfiler::RemoveSnapshot(HeapSnapshot* snapshot) {
36  snapshots_.erase(
37  std::find_if(snapshots_.begin(), snapshots_.end(),
38  [&](const std::unique_ptr<HeapSnapshot>& entry) {
39  return entry.get() == snapshot;
40  }));
41 }
42 
43 
44 void HeapProfiler::DefineWrapperClass(
45  uint16_t class_id, v8::HeapProfiler::WrapperInfoCallback callback) {
47  if (wrapper_callbacks_.size() <= class_id) {
48  wrapper_callbacks_.insert(wrapper_callbacks_.end(),
49  class_id - wrapper_callbacks_.size() + 1,
50  nullptr);
51  }
52  wrapper_callbacks_[class_id] = callback;
53 }
54 
55 
56 v8::RetainedObjectInfo* HeapProfiler::ExecuteWrapperClassCallback(
57  uint16_t class_id, Object** wrapper) {
58  if (wrapper_callbacks_.size() <= class_id) return nullptr;
59  return wrapper_callbacks_[class_id](
60  class_id, Utils::ToLocal(Handle<Object>(wrapper)));
61 }
62 
63 void HeapProfiler::SetGetRetainerInfosCallback(
65  get_retainer_infos_callback_ = callback;
66 }
67 
68 v8::HeapProfiler::RetainerInfos HeapProfiler::GetRetainerInfos(
69  Isolate* isolate) {
71  if (get_retainer_infos_callback_ != nullptr)
72  infos =
73  get_retainer_infos_callback_(reinterpret_cast<v8::Isolate*>(isolate));
74  return infos;
75 }
76 
77 void HeapProfiler::AddBuildEmbedderGraphCallback(
78  v8::HeapProfiler::BuildEmbedderGraphCallback callback, void* data) {
79  build_embedder_graph_callbacks_.push_back({callback, data});
80 }
81 
82 void HeapProfiler::RemoveBuildEmbedderGraphCallback(
83  v8::HeapProfiler::BuildEmbedderGraphCallback callback, void* data) {
84  auto it = std::find(build_embedder_graph_callbacks_.begin(),
85  build_embedder_graph_callbacks_.end(),
86  std::make_pair(callback, data));
87  if (it != build_embedder_graph_callbacks_.end())
88  build_embedder_graph_callbacks_.erase(it);
89 }
90 
91 void HeapProfiler::BuildEmbedderGraph(Isolate* isolate,
92  v8::EmbedderGraph* graph) {
93  for (const auto& cb : build_embedder_graph_callbacks_) {
94  cb.first(reinterpret_cast<v8::Isolate*>(isolate), graph, cb.second);
95  }
96 }
97 
98 HeapSnapshot* HeapProfiler::TakeSnapshot(
99  v8::ActivityControl* control,
101  HeapSnapshot* result = new HeapSnapshot(this);
102  {
103  HeapSnapshotGenerator generator(result, control, resolver, heap());
104  if (!generator.GenerateSnapshot()) {
105  delete result;
106  result = nullptr;
107  } else {
108  snapshots_.emplace_back(result);
109  }
110  }
111  ids_->RemoveDeadEntries();
112  is_tracking_object_moves_ = true;
113 
114  heap()->isolate()->debug()->feature_tracker()->Track(
115  DebugFeatureTracker::kHeapSnapshot);
116 
117  return result;
118 }
119 
120 bool HeapProfiler::StartSamplingHeapProfiler(
121  uint64_t sample_interval, int stack_depth,
122  v8::HeapProfiler::SamplingFlags flags) {
123  if (sampling_heap_profiler_.get()) {
124  return false;
125  }
126  sampling_heap_profiler_.reset(new SamplingHeapProfiler(
127  heap(), names_.get(), sample_interval, stack_depth, flags));
128  return true;
129 }
130 
131 
132 void HeapProfiler::StopSamplingHeapProfiler() {
133  sampling_heap_profiler_.reset();
134  MaybeClearStringsStorage();
135 }
136 
137 
138 v8::AllocationProfile* HeapProfiler::GetAllocationProfile() {
139  if (sampling_heap_profiler_.get()) {
140  return sampling_heap_profiler_->GetAllocationProfile();
141  } else {
142  return nullptr;
143  }
144 }
145 
146 
147 void HeapProfiler::StartHeapObjectsTracking(bool track_allocations) {
148  ids_->UpdateHeapObjectsMap();
149  is_tracking_object_moves_ = true;
150  DCHECK(!allocation_tracker_);
151  if (track_allocations) {
152  allocation_tracker_.reset(new AllocationTracker(ids_.get(), names_.get()));
153  heap()->AddHeapObjectAllocationTracker(this);
154  heap()->isolate()->debug()->feature_tracker()->Track(
155  DebugFeatureTracker::kAllocationTracking);
156  }
157 }
158 
159 SnapshotObjectId HeapProfiler::PushHeapObjectsStats(OutputStream* stream,
160  int64_t* timestamp_us) {
161  return ids_->PushHeapObjectsStats(stream, timestamp_us);
162 }
163 
164 void HeapProfiler::StopHeapObjectsTracking() {
165  ids_->StopHeapObjectsTracking();
166  if (allocation_tracker_) {
167  allocation_tracker_.reset();
168  MaybeClearStringsStorage();
169  heap()->RemoveHeapObjectAllocationTracker(this);
170  }
171 }
172 
173 int HeapProfiler::GetSnapshotsCount() {
174  return static_cast<int>(snapshots_.size());
175 }
176 
177 HeapSnapshot* HeapProfiler::GetSnapshot(int index) {
178  return snapshots_.at(index).get();
179 }
180 
181 SnapshotObjectId HeapProfiler::GetSnapshotObjectId(Handle<Object> obj) {
182  if (!obj->IsHeapObject())
184  return ids_->FindEntry(HeapObject::cast(*obj)->address());
185 }
186 
187 void HeapProfiler::ObjectMoveEvent(Address from, Address to, int size) {
188  base::MutexGuard guard(&profiler_mutex_);
189  bool known_object = ids_->MoveObject(from, to, size);
190  if (!known_object && allocation_tracker_) {
191  allocation_tracker_->address_to_trace()->MoveObject(from, to, size);
192  }
193 }
194 
195 void HeapProfiler::AllocationEvent(Address addr, int size) {
196  DisallowHeapAllocation no_allocation;
197  if (allocation_tracker_) {
198  allocation_tracker_->AllocationEvent(addr, size);
199  }
200 }
201 
202 
203 void HeapProfiler::UpdateObjectSizeEvent(Address addr, int size) {
204  ids_->UpdateObjectSize(addr, size);
205 }
206 
207 Handle<HeapObject> HeapProfiler::FindHeapObjectById(SnapshotObjectId id) {
208  HeapObject* object = nullptr;
209  HeapIterator iterator(heap(), HeapIterator::kFilterUnreachable);
210  // Make sure that object with the given id is still reachable.
211  for (HeapObject* obj = iterator.next(); obj != nullptr;
212  obj = iterator.next()) {
213  if (ids_->FindEntry(obj->address()) == id) {
214  DCHECK_NULL(object);
215  object = obj;
216  // Can't break -- kFilterUnreachable requires full heap traversal.
217  }
218  }
219  return object != nullptr ? Handle<HeapObject>(object, isolate())
220  : Handle<HeapObject>();
221 }
222 
223 
224 void HeapProfiler::ClearHeapObjectMap() {
225  ids_.reset(new HeapObjectsMap(heap()));
226  if (!allocation_tracker_) is_tracking_object_moves_ = false;
227 }
228 
229 
230 Heap* HeapProfiler::heap() const { return ids_->heap(); }
231 
232 Isolate* HeapProfiler::isolate() const { return heap()->isolate(); }
233 
234 void HeapProfiler::QueryObjects(Handle<Context> context,
235  debug::QueryObjectPredicate* predicate,
236  PersistentValueVector<v8::Object>* objects) {
237  // We should return accurate information about live objects, so we need to
238  // collect all garbage first.
239  heap()->CollectAllAvailableGarbage(GarbageCollectionReason::kHeapProfiler);
240  HeapIterator heap_iterator(heap());
241  HeapObject* heap_obj;
242  while ((heap_obj = heap_iterator.next()) != nullptr) {
243  if (!heap_obj->IsJSObject() || heap_obj->IsExternal(isolate())) continue;
244  v8::Local<v8::Object> v8_obj(
245  Utils::ToLocal(handle(JSObject::cast(heap_obj), isolate())));
246  if (!predicate->Filter(v8_obj)) continue;
247  objects->Append(v8_obj);
248  }
249 }
250 
251 } // namespace internal
252 } // namespace v8
RetainerInfos(* GetRetainerInfosCallback)(v8::Isolate *isolate)
Definition: v8-profiler.h:765
Definition: libplatform.h:13
static const uint16_t kPersistentHandleNoClassId
Definition: v8-profiler.h:952
void(* BuildEmbedderGraphCallback)(v8::Isolate *isolate, v8::EmbedderGraph *graph, void *data)
Definition: v8-profiler.h:782
RetainedObjectInfo *(* WrapperInfoCallback)(uint16_t class_id, Local< Value > wrapper)
Definition: v8-profiler.h:773
static const SnapshotObjectId kUnknownObjectId
Definition: v8-profiler.h:820