1 // Copyright 2009-2010 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are
4 // met:
5 //
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided
11 // with the distribution.
12 // * Neither the name of Google Inc. nor the names of its
13 // contributors may be used to endorse or promote products derived
14 // from this software without specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28 #include "v8.h"
29
30 #include "heap-profiler.h"
31
32 #include "allocation-tracker.h"
33 #include "heap-snapshot-generator-inl.h"
34
35 namespace v8 {
36 namespace internal {
37
HeapProfiler(Heap * heap)38 HeapProfiler::HeapProfiler(Heap* heap)
39 : ids_(new HeapObjectsMap(heap)),
40 names_(new StringsStorage(heap)),
41 next_snapshot_uid_(1),
42 is_tracking_object_moves_(false) {
43 }
44
45
DeleteHeapSnapshot(HeapSnapshot ** snapshot_ptr)46 static void DeleteHeapSnapshot(HeapSnapshot** snapshot_ptr) {
47 delete *snapshot_ptr;
48 }
49
50
~HeapProfiler()51 HeapProfiler::~HeapProfiler() {
52 snapshots_.Iterate(DeleteHeapSnapshot);
53 snapshots_.Clear();
54 }
55
56
DeleteAllSnapshots()57 void HeapProfiler::DeleteAllSnapshots() {
58 snapshots_.Iterate(DeleteHeapSnapshot);
59 snapshots_.Clear();
60 names_.Reset(new StringsStorage(heap()));
61 }
62
63
RemoveSnapshot(HeapSnapshot * snapshot)64 void HeapProfiler::RemoveSnapshot(HeapSnapshot* snapshot) {
65 snapshots_.RemoveElement(snapshot);
66 }
67
68
DefineWrapperClass(uint16_t class_id,v8::HeapProfiler::WrapperInfoCallback callback)69 void HeapProfiler::DefineWrapperClass(
70 uint16_t class_id, v8::HeapProfiler::WrapperInfoCallback callback) {
71 ASSERT(class_id != v8::HeapProfiler::kPersistentHandleNoClassId);
72 if (wrapper_callbacks_.length() <= class_id) {
73 wrapper_callbacks_.AddBlock(
74 NULL, class_id - wrapper_callbacks_.length() + 1);
75 }
76 wrapper_callbacks_[class_id] = callback;
77 }
78
79
ExecuteWrapperClassCallback(uint16_t class_id,Object ** wrapper)80 v8::RetainedObjectInfo* HeapProfiler::ExecuteWrapperClassCallback(
81 uint16_t class_id, Object** wrapper) {
82 if (wrapper_callbacks_.length() <= class_id) return NULL;
83 return wrapper_callbacks_[class_id](
84 class_id, Utils::ToLocal(Handle<Object>(wrapper)));
85 }
86
87
TakeSnapshot(const char * name,v8::ActivityControl * control,v8::HeapProfiler::ObjectNameResolver * resolver)88 HeapSnapshot* HeapProfiler::TakeSnapshot(
89 const char* name,
90 v8::ActivityControl* control,
91 v8::HeapProfiler::ObjectNameResolver* resolver) {
92 HeapSnapshot* result = new HeapSnapshot(this, name, next_snapshot_uid_++);
93 {
94 HeapSnapshotGenerator generator(result, control, resolver, heap());
95 if (!generator.GenerateSnapshot()) {
96 delete result;
97 result = NULL;
98 } else {
99 snapshots_.Add(result);
100 }
101 }
102 ids_->RemoveDeadEntries();
103 is_tracking_object_moves_ = true;
104 return result;
105 }
106
107
TakeSnapshot(String * name,v8::ActivityControl * control,v8::HeapProfiler::ObjectNameResolver * resolver)108 HeapSnapshot* HeapProfiler::TakeSnapshot(
109 String* name,
110 v8::ActivityControl* control,
111 v8::HeapProfiler::ObjectNameResolver* resolver) {
112 return TakeSnapshot(names_->GetName(name), control, resolver);
113 }
114
115
StartHeapObjectsTracking(bool track_allocations)116 void HeapProfiler::StartHeapObjectsTracking(bool track_allocations) {
117 ids_->UpdateHeapObjectsMap();
118 is_tracking_object_moves_ = true;
119 ASSERT(!is_tracking_allocations());
120 if (track_allocations) {
121 allocation_tracker_.Reset(new AllocationTracker(*ids_, *names_));
122 heap()->DisableInlineAllocation();
123 }
124 }
125
126
PushHeapObjectsStats(OutputStream * stream)127 SnapshotObjectId HeapProfiler::PushHeapObjectsStats(OutputStream* stream) {
128 return ids_->PushHeapObjectsStats(stream);
129 }
130
131
StopHeapObjectsTracking()132 void HeapProfiler::StopHeapObjectsTracking() {
133 ids_->StopHeapObjectsTracking();
134 if (is_tracking_allocations()) {
135 allocation_tracker_.Reset(NULL);
136 heap()->EnableInlineAllocation();
137 }
138 }
139
140
GetMemorySizeUsedByProfiler()141 size_t HeapProfiler::GetMemorySizeUsedByProfiler() {
142 size_t size = sizeof(*this);
143 size += names_->GetUsedMemorySize();
144 size += ids_->GetUsedMemorySize();
145 size += GetMemoryUsedByList(snapshots_);
146 for (int i = 0; i < snapshots_.length(); ++i) {
147 size += snapshots_[i]->RawSnapshotSize();
148 }
149 return size;
150 }
151
152
GetSnapshotsCount()153 int HeapProfiler::GetSnapshotsCount() {
154 return snapshots_.length();
155 }
156
157
GetSnapshot(int index)158 HeapSnapshot* HeapProfiler::GetSnapshot(int index) {
159 return snapshots_.at(index);
160 }
161
162
GetSnapshotObjectId(Handle<Object> obj)163 SnapshotObjectId HeapProfiler::GetSnapshotObjectId(Handle<Object> obj) {
164 if (!obj->IsHeapObject())
165 return v8::HeapProfiler::kUnknownObjectId;
166 return ids_->FindEntry(HeapObject::cast(*obj)->address());
167 }
168
169
ObjectMoveEvent(Address from,Address to,int size)170 void HeapProfiler::ObjectMoveEvent(Address from, Address to, int size) {
171 ids_->MoveObject(from, to, size);
172 }
173
174
AllocationEvent(Address addr,int size)175 void HeapProfiler::AllocationEvent(Address addr, int size) {
176 DisallowHeapAllocation no_allocation;
177 if (!allocation_tracker_.is_empty()) {
178 allocation_tracker_->AllocationEvent(addr, size);
179 }
180 }
181
182
UpdateObjectSizeEvent(Address addr,int size)183 void HeapProfiler::UpdateObjectSizeEvent(Address addr, int size) {
184 ids_->UpdateObjectSize(addr, size);
185 }
186
187
SetRetainedObjectInfo(UniqueId id,RetainedObjectInfo * info)188 void HeapProfiler::SetRetainedObjectInfo(UniqueId id,
189 RetainedObjectInfo* info) {
190 // TODO(yurus, marja): Don't route this information through GlobalHandles.
191 heap()->isolate()->global_handles()->SetRetainedObjectInfo(id, info);
192 }
193
194
FindHeapObjectById(SnapshotObjectId id)195 Handle<HeapObject> HeapProfiler::FindHeapObjectById(SnapshotObjectId id) {
196 heap()->CollectAllGarbage(Heap::kMakeHeapIterableMask,
197 "HeapProfiler::FindHeapObjectById");
198 DisallowHeapAllocation no_allocation;
199 HeapObject* object = NULL;
200 HeapIterator iterator(heap(), HeapIterator::kFilterUnreachable);
201 // Make sure that object with the given id is still reachable.
202 for (HeapObject* obj = iterator.next();
203 obj != NULL;
204 obj = iterator.next()) {
205 if (ids_->FindEntry(obj->address()) == id) {
206 ASSERT(object == NULL);
207 object = obj;
208 // Can't break -- kFilterUnreachable requires full heap traversal.
209 }
210 }
211 return object != NULL ? Handle<HeapObject>(object) : Handle<HeapObject>();
212 }
213
214
215 } } // namespace v8::internal
216