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.
7 #include "src/heap-profiler.h"
9 #include "src/allocation-tracker.h"
10 #include "src/heap-snapshot-generator-inl.h"
15 HeapProfiler::HeapProfiler(Heap* heap)
16 : ids_(new HeapObjectsMap(heap)),
17 names_(new StringsStorage(heap)),
18 is_tracking_object_moves_(false) {
22 static void DeleteHeapSnapshot(HeapSnapshot** snapshot_ptr) {
27 HeapProfiler::~HeapProfiler() {
28 snapshots_.Iterate(DeleteHeapSnapshot);
33 void HeapProfiler::DeleteAllSnapshots() {
34 snapshots_.Iterate(DeleteHeapSnapshot);
36 names_.Reset(new StringsStorage(heap()));
40 void HeapProfiler::RemoveSnapshot(HeapSnapshot* snapshot) {
41 snapshots_.RemoveElement(snapshot);
45 void HeapProfiler::DefineWrapperClass(
46 uint16_t class_id, v8::HeapProfiler::WrapperInfoCallback callback) {
47 DCHECK(class_id != v8::HeapProfiler::kPersistentHandleNoClassId);
48 if (wrapper_callbacks_.length() <= class_id) {
49 wrapper_callbacks_.AddBlock(
50 NULL, class_id - wrapper_callbacks_.length() + 1);
52 wrapper_callbacks_[class_id] = callback;
56 v8::RetainedObjectInfo* HeapProfiler::ExecuteWrapperClassCallback(
57 uint16_t class_id, Object** wrapper) {
58 if (wrapper_callbacks_.length() <= class_id) return NULL;
59 return wrapper_callbacks_[class_id](
60 class_id, Utils::ToLocal(Handle<Object>(wrapper)));
64 HeapSnapshot* HeapProfiler::TakeSnapshot(
65 v8::ActivityControl* control,
66 v8::HeapProfiler::ObjectNameResolver* resolver) {
67 HeapSnapshot* result = new HeapSnapshot(this);
69 HeapSnapshotGenerator generator(result, control, resolver, heap());
70 if (!generator.GenerateSnapshot()) {
74 snapshots_.Add(result);
77 ids_->RemoveDeadEntries();
78 is_tracking_object_moves_ = true;
83 void HeapProfiler::StartHeapObjectsTracking(bool track_allocations) {
84 ids_->UpdateHeapObjectsMap();
85 is_tracking_object_moves_ = true;
86 DCHECK(!is_tracking_allocations());
87 if (track_allocations) {
88 allocation_tracker_.Reset(new AllocationTracker(ids_.get(), names_.get()));
89 heap()->DisableInlineAllocation();
94 SnapshotObjectId HeapProfiler::PushHeapObjectsStats(OutputStream* stream,
95 int64_t* timestamp_us) {
96 return ids_->PushHeapObjectsStats(stream, timestamp_us);
100 void HeapProfiler::StopHeapObjectsTracking() {
101 ids_->StopHeapObjectsTracking();
102 if (is_tracking_allocations()) {
103 allocation_tracker_.Reset(NULL);
104 heap()->EnableInlineAllocation();
109 size_t HeapProfiler::GetMemorySizeUsedByProfiler() {
110 size_t size = sizeof(*this);
111 size += names_->GetUsedMemorySize();
112 size += ids_->GetUsedMemorySize();
113 size += GetMemoryUsedByList(snapshots_);
114 for (int i = 0; i < snapshots_.length(); ++i) {
115 size += snapshots_[i]->RawSnapshotSize();
121 int HeapProfiler::GetSnapshotsCount() {
122 return snapshots_.length();
126 HeapSnapshot* HeapProfiler::GetSnapshot(int index) {
127 return snapshots_.at(index);
131 SnapshotObjectId HeapProfiler::GetSnapshotObjectId(Handle<Object> obj) {
132 if (!obj->IsHeapObject())
133 return v8::HeapProfiler::kUnknownObjectId;
134 return ids_->FindEntry(HeapObject::cast(*obj)->address());
138 void HeapProfiler::ObjectMoveEvent(Address from, Address to, int size) {
139 bool known_object = ids_->MoveObject(from, to, size);
140 if (!known_object && !allocation_tracker_.is_empty()) {
141 allocation_tracker_->address_to_trace()->MoveObject(from, to, size);
146 void HeapProfiler::AllocationEvent(Address addr, int size) {
147 DisallowHeapAllocation no_allocation;
148 if (!allocation_tracker_.is_empty()) {
149 allocation_tracker_->AllocationEvent(addr, size);
154 void HeapProfiler::UpdateObjectSizeEvent(Address addr, int size) {
155 ids_->UpdateObjectSize(addr, size);
159 void HeapProfiler::SetRetainedObjectInfo(UniqueId id,
160 RetainedObjectInfo* info) {
161 // TODO(yurus, marja): Don't route this information through GlobalHandles.
162 heap()->isolate()->global_handles()->SetRetainedObjectInfo(id, info);
166 Handle<HeapObject> HeapProfiler::FindHeapObjectById(SnapshotObjectId id) {
167 HeapObject* object = NULL;
168 HeapIterator iterator(heap(), HeapIterator::kFilterUnreachable);
169 // Make sure that object with the given id is still reachable.
170 for (HeapObject* obj = iterator.next();
172 obj = iterator.next()) {
173 if (ids_->FindEntry(obj->address()) == id) {
174 DCHECK(object == NULL);
176 // Can't break -- kFilterUnreachable requires full heap traversal.
179 return object != NULL ? Handle<HeapObject>(object) : Handle<HeapObject>();
183 void HeapProfiler::ClearHeapObjectMap() {
184 ids_.Reset(new HeapObjectsMap(heap()));
185 if (!is_tracking_allocations()) is_tracking_object_moves_ = false;
189 } } // namespace v8::internal