deps: update v8 to 4.3.61.21
[platform/upstream/nodejs.git] / deps / v8 / src / 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/v8.h"
6
7 #include "src/heap-profiler.h"
8
9 #include "src/allocation-tracker.h"
10 #include "src/heap-snapshot-generator-inl.h"
11
12 namespace v8 {
13 namespace internal {
14
15 HeapProfiler::HeapProfiler(Heap* heap)
16     : ids_(new HeapObjectsMap(heap)),
17       names_(new StringsStorage(heap)),
18       is_tracking_object_moves_(false) {
19 }
20
21
22 static void DeleteHeapSnapshot(HeapSnapshot** snapshot_ptr) {
23   delete *snapshot_ptr;
24 }
25
26
27 HeapProfiler::~HeapProfiler() {
28   snapshots_.Iterate(DeleteHeapSnapshot);
29   snapshots_.Clear();
30 }
31
32
33 void HeapProfiler::DeleteAllSnapshots() {
34   snapshots_.Iterate(DeleteHeapSnapshot);
35   snapshots_.Clear();
36   names_.Reset(new StringsStorage(heap()));
37 }
38
39
40 void HeapProfiler::RemoveSnapshot(HeapSnapshot* snapshot) {
41   snapshots_.RemoveElement(snapshot);
42 }
43
44
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);
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_.length() <= class_id) return NULL;
59   return wrapper_callbacks_[class_id](
60       class_id, Utils::ToLocal(Handle<Object>(wrapper)));
61 }
62
63
64 HeapSnapshot* HeapProfiler::TakeSnapshot(
65     v8::ActivityControl* control,
66     v8::HeapProfiler::ObjectNameResolver* resolver) {
67   HeapSnapshot* result = new HeapSnapshot(this);
68   {
69     HeapSnapshotGenerator generator(result, control, resolver, heap());
70     if (!generator.GenerateSnapshot()) {
71       delete result;
72       result = NULL;
73     } else {
74       snapshots_.Add(result);
75     }
76   }
77   ids_->RemoveDeadEntries();
78   is_tracking_object_moves_ = true;
79   return result;
80 }
81
82
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();
90   }
91 }
92
93
94 SnapshotObjectId HeapProfiler::PushHeapObjectsStats(OutputStream* stream,
95                                                     int64_t* timestamp_us) {
96   return ids_->PushHeapObjectsStats(stream, timestamp_us);
97 }
98
99
100 void HeapProfiler::StopHeapObjectsTracking() {
101   ids_->StopHeapObjectsTracking();
102   if (is_tracking_allocations()) {
103     allocation_tracker_.Reset(NULL);
104     heap()->EnableInlineAllocation();
105   }
106 }
107
108
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();
116   }
117   return size;
118 }
119
120
121 int HeapProfiler::GetSnapshotsCount() {
122   return snapshots_.length();
123 }
124
125
126 HeapSnapshot* HeapProfiler::GetSnapshot(int index) {
127   return snapshots_.at(index);
128 }
129
130
131 SnapshotObjectId HeapProfiler::GetSnapshotObjectId(Handle<Object> obj) {
132   if (!obj->IsHeapObject())
133     return v8::HeapProfiler::kUnknownObjectId;
134   return ids_->FindEntry(HeapObject::cast(*obj)->address());
135 }
136
137
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);
142   }
143 }
144
145
146 void HeapProfiler::AllocationEvent(Address addr, int size) {
147   DisallowHeapAllocation no_allocation;
148   if (!allocation_tracker_.is_empty()) {
149     allocation_tracker_->AllocationEvent(addr, size);
150   }
151 }
152
153
154 void HeapProfiler::UpdateObjectSizeEvent(Address addr, int size) {
155   ids_->UpdateObjectSize(addr, size);
156 }
157
158
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);
163 }
164
165
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();
171        obj != NULL;
172        obj = iterator.next()) {
173     if (ids_->FindEntry(obj->address()) == id) {
174       DCHECK(object == NULL);
175       object = obj;
176       // Can't break -- kFilterUnreachable requires full heap traversal.
177     }
178   }
179   return object != NULL ? Handle<HeapObject>(object) : Handle<HeapObject>();
180 }
181
182
183 void HeapProfiler::ClearHeapObjectMap() {
184   ids_.Reset(new HeapObjectsMap(heap()));
185   if (!is_tracking_allocations()) is_tracking_object_moves_ = false;
186 }
187
188
189 } }  // namespace v8::internal