blob: d86ce5ec32731a28e2c9651e99b8a37094628cf7 [file] [log] [blame]
Ben Murdochb0fe1622011-05-05 13:52:32 +01001// Copyright 2009-2010 the V8 project authors. All rights reserved.
Ben Murdochb8a8cc12014-11-26 15:28:44 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
Steve Blocka7e24c12009-10-30 11:49:00 +00004
Ben Murdochb8a8cc12014-11-26 15:28:44 +00005#include "src/v8.h"
Steve Blocka7e24c12009-10-30 11:49:00 +00006
Ben Murdochb8a8cc12014-11-26 15:28:44 +00007#include "src/heap-profiler.h"
8
9#include "src/allocation-tracker.h"
10#include "src/heap-snapshot-generator-inl.h"
Steve Blocka7e24c12009-10-30 11:49:00 +000011
12namespace v8 {
13namespace internal {
14
Ben Murdochb8a8cc12014-11-26 15:28:44 +000015HeapProfiler::HeapProfiler(Heap* heap)
16 : ids_(new HeapObjectsMap(heap)),
17 names_(new StringsStorage(heap)),
18 next_snapshot_uid_(1),
19 is_tracking_object_moves_(false) {
20}
Steve Blocka7e24c12009-10-30 11:49:00 +000021
Ben Murdochb8a8cc12014-11-26 15:28:44 +000022
23static void DeleteHeapSnapshot(HeapSnapshot** snapshot_ptr) {
24 delete *snapshot_ptr;
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010025}
26
27
28HeapProfiler::~HeapProfiler() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000029 snapshots_.Iterate(DeleteHeapSnapshot);
30 snapshots_.Clear();
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010031}
32
Steve Block44f0eee2011-05-26 01:26:41 +010033
Ben Murdochb8a8cc12014-11-26 15:28:44 +000034void HeapProfiler::DeleteAllSnapshots() {
35 snapshots_.Iterate(DeleteHeapSnapshot);
36 snapshots_.Clear();
37 names_.Reset(new StringsStorage(heap()));
Steve Block44f0eee2011-05-26 01:26:41 +010038}
39
40
Ben Murdochb8a8cc12014-11-26 15:28:44 +000041void HeapProfiler::RemoveSnapshot(HeapSnapshot* snapshot) {
42 snapshots_.RemoveElement(snapshot);
Steve Block44f0eee2011-05-26 01:26:41 +010043}
44
45
46void HeapProfiler::DefineWrapperClass(
47 uint16_t class_id, v8::HeapProfiler::WrapperInfoCallback callback) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +000048 DCHECK(class_id != v8::HeapProfiler::kPersistentHandleNoClassId);
Steve Block44f0eee2011-05-26 01:26:41 +010049 if (wrapper_callbacks_.length() <= class_id) {
50 wrapper_callbacks_.AddBlock(
51 NULL, class_id - wrapper_callbacks_.length() + 1);
52 }
53 wrapper_callbacks_[class_id] = callback;
54}
55
56
57v8::RetainedObjectInfo* HeapProfiler::ExecuteWrapperClassCallback(
58 uint16_t class_id, Object** wrapper) {
59 if (wrapper_callbacks_.length() <= class_id) return NULL;
60 return wrapper_callbacks_[class_id](
61 class_id, Utils::ToLocal(Handle<Object>(wrapper)));
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010062}
63
64
Ben Murdochb8a8cc12014-11-26 15:28:44 +000065HeapSnapshot* HeapProfiler::TakeSnapshot(
66 const char* name,
67 v8::ActivityControl* control,
68 v8::HeapProfiler::ObjectNameResolver* resolver) {
69 HeapSnapshot* result = new HeapSnapshot(this, name, next_snapshot_uid_++);
70 {
71 HeapSnapshotGenerator generator(result, control, resolver, heap());
72 if (!generator.GenerateSnapshot()) {
73 delete result;
74 result = NULL;
75 } else {
76 snapshots_.Add(result);
Steve Block791712a2010-08-27 10:21:07 +010077 }
Steve Block791712a2010-08-27 10:21:07 +010078 }
Ben Murdochb8a8cc12014-11-26 15:28:44 +000079 ids_->RemoveDeadEntries();
80 is_tracking_object_moves_ = true;
Kristian Monsen9dcf7e22010-06-28 14:14:28 +010081 return result;
82}
83
84
Ben Murdochb8a8cc12014-11-26 15:28:44 +000085HeapSnapshot* HeapProfiler::TakeSnapshot(
86 String* name,
87 v8::ActivityControl* control,
88 v8::HeapProfiler::ObjectNameResolver* resolver) {
89 return TakeSnapshot(names_->GetName(name), control, resolver);
90}
91
92
93void HeapProfiler::StartHeapObjectsTracking(bool track_allocations) {
94 ids_->UpdateHeapObjectsMap();
95 is_tracking_object_moves_ = true;
96 DCHECK(!is_tracking_allocations());
97 if (track_allocations) {
98 allocation_tracker_.Reset(new AllocationTracker(ids_.get(), names_.get()));
99 heap()->DisableInlineAllocation();
100 }
101}
102
103
104SnapshotObjectId HeapProfiler::PushHeapObjectsStats(OutputStream* stream) {
105 return ids_->PushHeapObjectsStats(stream);
106}
107
108
109void HeapProfiler::StopHeapObjectsTracking() {
110 ids_->StopHeapObjectsTracking();
111 if (is_tracking_allocations()) {
112 allocation_tracker_.Reset(NULL);
113 heap()->EnableInlineAllocation();
114 }
115}
116
117
118size_t HeapProfiler::GetMemorySizeUsedByProfiler() {
119 size_t size = sizeof(*this);
120 size += names_->GetUsedMemorySize();
121 size += ids_->GetUsedMemorySize();
122 size += GetMemoryUsedByList(snapshots_);
123 for (int i = 0; i < snapshots_.length(); ++i) {
124 size += snapshots_[i]->RawSnapshotSize();
125 }
126 return size;
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100127}
128
129
130int HeapProfiler::GetSnapshotsCount() {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000131 return snapshots_.length();
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100132}
133
134
135HeapSnapshot* HeapProfiler::GetSnapshot(int index) {
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000136 return snapshots_.at(index);
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100137}
138
139
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000140SnapshotObjectId HeapProfiler::GetSnapshotObjectId(Handle<Object> obj) {
141 if (!obj->IsHeapObject())
142 return v8::HeapProfiler::kUnknownObjectId;
143 return ids_->FindEntry(HeapObject::cast(*obj)->address());
Steve Block44f0eee2011-05-26 01:26:41 +0100144}
145
146
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000147void HeapProfiler::ObjectMoveEvent(Address from, Address to, int size) {
148 bool known_object = ids_->MoveObject(from, to, size);
149 if (!known_object && !allocation_tracker_.is_empty()) {
150 allocation_tracker_->address_to_trace()->MoveObject(from, to, size);
151 }
Kristian Monsen9dcf7e22010-06-28 14:14:28 +0100152}
153
154
Ben Murdochb8a8cc12014-11-26 15:28:44 +0000155void HeapProfiler::AllocationEvent(Address addr, int size) {
156 DisallowHeapAllocation no_allocation;
157 if (!allocation_tracker_.is_empty()) {
158 allocation_tracker_->AllocationEvent(addr, size);
159 }
160}
161
162
163void HeapProfiler::UpdateObjectSizeEvent(Address addr, int size) {
164 ids_->UpdateObjectSize(addr, size);
165}
166
167
168void HeapProfiler::SetRetainedObjectInfo(UniqueId id,
169 RetainedObjectInfo* info) {
170 // TODO(yurus, marja): Don't route this information through GlobalHandles.
171 heap()->isolate()->global_handles()->SetRetainedObjectInfo(id, info);
172}
173
174
175Handle<HeapObject> HeapProfiler::FindHeapObjectById(SnapshotObjectId id) {
176 HeapObject* object = NULL;
177 HeapIterator iterator(heap(), HeapIterator::kFilterUnreachable);
178 // Make sure that object with the given id is still reachable.
179 for (HeapObject* obj = iterator.next();
180 obj != NULL;
181 obj = iterator.next()) {
182 if (ids_->FindEntry(obj->address()) == id) {
183 DCHECK(object == NULL);
184 object = obj;
185 // Can't break -- kFilterUnreachable requires full heap traversal.
186 }
187 }
188 return object != NULL ? Handle<HeapObject>(object) : Handle<HeapObject>();
189}
190
191
192void HeapProfiler::ClearHeapObjectMap() {
193 ids_.Reset(new HeapObjectsMap(heap()));
194 if (!is_tracking_allocations()) is_tracking_object_moves_ = false;
Ben Murdoch3bec4d22010-07-22 14:51:16 +0100195}
196
197
Steve Blocka7e24c12009-10-30 11:49:00 +0000198} } // namespace v8::internal