blob: c9f1d501daf18e311d53754d3e6d05e215b9a51c [file] [log] [blame]
ager@chromium.org5f0c45f2010-12-17 08:51:21 +00001// Copyright 2009-2010 the V8 project authors. All rights reserved.
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +00002// 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"
ulan@chromium.org2e04b582013-02-21 14:06:02 +000031#include "heap-snapshot-generator-inl.h"
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +000032
33namespace v8 {
34namespace internal {
35
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +000036HeapProfiler::HeapProfiler(Heap* heap)
37 : snapshots_(new HeapSnapshotsCollection(heap)),
whesse@chromium.org2c186ca2010-06-16 11:32:39 +000038 next_snapshot_uid_(1) {
39}
40
41
42HeapProfiler::~HeapProfiler() {
43 delete snapshots_;
44}
45
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000046
47void HeapProfiler::ResetSnapshots() {
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +000048 Heap* the_heap = heap();
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000049 delete snapshots_;
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +000050 snapshots_ = new HeapSnapshotsCollection(the_heap);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000051}
52
53
erik.corry@gmail.comf2038fb2012-01-16 11:42:08 +000054void HeapProfiler::SetUp() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000055 Isolate* isolate = Isolate::Current();
56 if (isolate->heap_profiler() == NULL) {
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +000057 isolate->set_heap_profiler(new HeapProfiler(isolate->heap()));
whesse@chromium.org2c186ca2010-06-16 11:32:39 +000058 }
59}
60
61
62void HeapProfiler::TearDown() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000063 Isolate* isolate = Isolate::Current();
64 delete isolate->heap_profiler();
65 isolate->set_heap_profiler(NULL);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +000066}
67
68
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +000069HeapSnapshot* HeapProfiler::TakeSnapshot(
70 const char* name,
71 int type,
72 v8::ActivityControl* control,
73 v8::HeapProfiler::ObjectNameResolver* resolver) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000074 ASSERT(Isolate::Current()->heap_profiler() != NULL);
75 return Isolate::Current()->heap_profiler()->TakeSnapshotImpl(name,
76 type,
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +000077 control,
78 resolver);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +000079}
80
81
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +000082HeapSnapshot* HeapProfiler::TakeSnapshot(
83 String* name,
84 int type,
85 v8::ActivityControl* control,
86 v8::HeapProfiler::ObjectNameResolver* resolver) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000087 ASSERT(Isolate::Current()->heap_profiler() != NULL);
88 return Isolate::Current()->heap_profiler()->TakeSnapshotImpl(name,
89 type,
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +000090 control,
91 resolver);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +000092}
93
94
jkummerow@chromium.org28faa982012-04-13 09:58:30 +000095void HeapProfiler::StartHeapObjectsTracking() {
96 ASSERT(Isolate::Current()->heap_profiler() != NULL);
97 Isolate::Current()->heap_profiler()->StartHeapObjectsTrackingImpl();
98}
99
100
101void HeapProfiler::StopHeapObjectsTracking() {
102 ASSERT(Isolate::Current()->heap_profiler() != NULL);
103 Isolate::Current()->heap_profiler()->StopHeapObjectsTrackingImpl();
104}
105
106
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000107SnapshotObjectId HeapProfiler::PushHeapObjectsStats(v8::OutputStream* stream) {
jkummerow@chromium.org28faa982012-04-13 09:58:30 +0000108 ASSERT(Isolate::Current()->heap_profiler() != NULL);
109 return Isolate::Current()->heap_profiler()->PushHeapObjectsStatsImpl(stream);
110}
111
112
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000113void HeapProfiler::DefineWrapperClass(
114 uint16_t class_id, v8::HeapProfiler::WrapperInfoCallback callback) {
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000115 ASSERT(class_id != v8::HeapProfiler::kPersistentHandleNoClassId);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000116 if (wrapper_callbacks_.length() <= class_id) {
117 wrapper_callbacks_.AddBlock(
118 NULL, class_id - wrapper_callbacks_.length() + 1);
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000119 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000120 wrapper_callbacks_[class_id] = callback;
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000121}
122
123
124v8::RetainedObjectInfo* HeapProfiler::ExecuteWrapperClassCallback(
125 uint16_t class_id, Object** wrapper) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000126 if (wrapper_callbacks_.length() <= class_id) return NULL;
127 return wrapper_callbacks_[class_id](
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000128 class_id, Utils::ToLocal(Handle<Object>(wrapper)));
129}
130
131
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000132HeapSnapshot* HeapProfiler::TakeSnapshotImpl(
133 const char* name,
134 int type,
135 v8::ActivityControl* control,
136 v8::HeapProfiler::ObjectNameResolver* resolver) {
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000137 HeapSnapshot::Type s_type = static_cast<HeapSnapshot::Type>(type);
138 HeapSnapshot* result =
139 snapshots_->NewSnapshot(s_type, name, next_snapshot_uid_++);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000140 bool generation_completed = true;
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000141 switch (s_type) {
142 case HeapSnapshot::kFull: {
hpayer@chromium.org7c3372b2013-02-13 17:26:04 +0000143 HeapSnapshotGenerator generator(result, control, resolver, heap());
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000144 generation_completed = generator.GenerateSnapshot();
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000145 break;
146 }
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000147 default:
148 UNREACHABLE();
149 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000150 if (!generation_completed) {
151 delete result;
152 result = NULL;
153 }
154 snapshots_->SnapshotGenerationFinished(result);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000155 return result;
156}
157
158
mstarzinger@chromium.org32280cf2012-12-06 17:32:37 +0000159HeapSnapshot* HeapProfiler::TakeSnapshotImpl(
160 String* name,
161 int type,
162 v8::ActivityControl* control,
163 v8::HeapProfiler::ObjectNameResolver* resolver) {
164 return TakeSnapshotImpl(snapshots_->names()->GetName(name), type, control,
165 resolver);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000166}
167
jkummerow@chromium.org28faa982012-04-13 09:58:30 +0000168void HeapProfiler::StartHeapObjectsTrackingImpl() {
169 snapshots_->StartHeapObjectsTracking();
170}
171
172
rossberg@chromium.org400388e2012-06-06 09:29:22 +0000173SnapshotObjectId HeapProfiler::PushHeapObjectsStatsImpl(OutputStream* stream) {
174 return snapshots_->PushHeapObjectsStats(stream);
jkummerow@chromium.org28faa982012-04-13 09:58:30 +0000175}
176
177
178void HeapProfiler::StopHeapObjectsTrackingImpl() {
179 snapshots_->StopHeapObjectsTracking();
180}
181
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000182
mmassi@chromium.org7028c052012-06-13 11:51:58 +0000183size_t HeapProfiler::GetMemorySizeUsedByProfiler() {
184 HeapProfiler* profiler = Isolate::Current()->heap_profiler();
185 ASSERT(profiler != NULL);
186 size_t size = profiler->snapshots_->GetUsedMemorySize();
187 return size;
188}
189
190
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000191int HeapProfiler::GetSnapshotsCount() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000192 HeapProfiler* profiler = Isolate::Current()->heap_profiler();
193 ASSERT(profiler != NULL);
194 return profiler->snapshots_->snapshots()->length();
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000195}
196
197
198HeapSnapshot* HeapProfiler::GetSnapshot(int index) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000199 HeapProfiler* profiler = Isolate::Current()->heap_profiler();
200 ASSERT(profiler != NULL);
201 return profiler->snapshots_->snapshots()->at(index);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000202}
203
204
205HeapSnapshot* HeapProfiler::FindSnapshot(unsigned uid) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000206 HeapProfiler* profiler = Isolate::Current()->heap_profiler();
207 ASSERT(profiler != NULL);
208 return profiler->snapshots_->GetSnapshot(uid);
209}
210
211
erik.corry@gmail.comed49e962012-04-17 11:57:53 +0000212SnapshotObjectId HeapProfiler::GetSnapshotObjectId(Handle<Object> obj) {
213 if (!obj->IsHeapObject())
214 return v8::HeapProfiler::kUnknownObjectId;
215 HeapProfiler* profiler = Isolate::Current()->heap_profiler();
216 ASSERT(profiler != NULL);
217 return profiler->snapshots_->FindObjectId(HeapObject::cast(*obj)->address());
218}
219
220
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000221void HeapProfiler::DeleteAllSnapshots() {
222 HeapProfiler* profiler = Isolate::Current()->heap_profiler();
223 ASSERT(profiler != NULL);
224 profiler->ResetSnapshots();
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000225}
226
227
ricow@chromium.org4980dff2010-07-19 08:33:45 +0000228void HeapProfiler::ObjectMoveEvent(Address from, Address to) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000229 snapshots_->ObjectMoveEvent(from, to);
ricow@chromium.org4980dff2010-07-19 08:33:45 +0000230}
231
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000232
233} } // namespace v8::internal