blob: 7e613e91733158d36cf554779e8f3f1cbe0dda2d [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"
whesse@chromium.org2c186ca2010-06-16 11:32:39 +000031#include "profile-generator.h"
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +000032
33namespace v8 {
34namespace internal {
35
36
whesse@chromium.org2c186ca2010-06-16 11:32:39 +000037HeapProfiler::HeapProfiler()
38 : snapshots_(new HeapSnapshotsCollection()),
39 next_snapshot_uid_(1) {
40}
41
42
43HeapProfiler::~HeapProfiler() {
44 delete snapshots_;
45}
46
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000047
48void HeapProfiler::ResetSnapshots() {
49 delete snapshots_;
50 snapshots_ = new HeapSnapshotsCollection();
51}
52
53
whesse@chromium.org2c186ca2010-06-16 11:32:39 +000054void HeapProfiler::Setup() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000055 Isolate* isolate = Isolate::Current();
56 if (isolate->heap_profiler() == NULL) {
57 isolate->set_heap_profiler(new HeapProfiler());
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
ager@chromium.org5f0c45f2010-12-17 08:51:21 +000069HeapSnapshot* HeapProfiler::TakeSnapshot(const char* name,
70 int type,
71 v8::ActivityControl* control) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000072 ASSERT(Isolate::Current()->heap_profiler() != NULL);
73 return Isolate::Current()->heap_profiler()->TakeSnapshotImpl(name,
74 type,
75 control);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +000076}
77
78
ager@chromium.org5f0c45f2010-12-17 08:51:21 +000079HeapSnapshot* HeapProfiler::TakeSnapshot(String* name,
80 int type,
81 v8::ActivityControl* control) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000082 ASSERT(Isolate::Current()->heap_profiler() != NULL);
83 return Isolate::Current()->heap_profiler()->TakeSnapshotImpl(name,
84 type,
85 control);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +000086}
87
88
whesse@chromium.orgb08986c2011-03-14 16:13:42 +000089void HeapProfiler::DefineWrapperClass(
90 uint16_t class_id, v8::HeapProfiler::WrapperInfoCallback callback) {
whesse@chromium.orgb08986c2011-03-14 16:13:42 +000091 ASSERT(class_id != v8::HeapProfiler::kPersistentHandleNoClassId);
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000092 if (wrapper_callbacks_.length() <= class_id) {
93 wrapper_callbacks_.AddBlock(
94 NULL, class_id - wrapper_callbacks_.length() + 1);
whesse@chromium.orgb08986c2011-03-14 16:13:42 +000095 }
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +000096 wrapper_callbacks_[class_id] = callback;
whesse@chromium.orgb08986c2011-03-14 16:13:42 +000097}
98
99
100v8::RetainedObjectInfo* HeapProfiler::ExecuteWrapperClassCallback(
101 uint16_t class_id, Object** wrapper) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000102 if (wrapper_callbacks_.length() <= class_id) return NULL;
103 return wrapper_callbacks_[class_id](
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000104 class_id, Utils::ToLocal(Handle<Object>(wrapper)));
105}
106
107
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000108HeapSnapshot* HeapProfiler::TakeSnapshotImpl(const char* name,
109 int type,
110 v8::ActivityControl* control) {
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000111 HeapSnapshot::Type s_type = static_cast<HeapSnapshot::Type>(type);
112 HeapSnapshot* result =
113 snapshots_->NewSnapshot(s_type, name, next_snapshot_uid_++);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000114 bool generation_completed = true;
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000115 switch (s_type) {
116 case HeapSnapshot::kFull: {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000117 HEAP->CollectAllGarbage(true);
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000118 HeapSnapshotGenerator generator(result, control);
119 generation_completed = generator.GenerateSnapshot();
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000120 break;
121 }
erik.corry@gmail.com145eff52010-08-23 11:36:18 +0000122 default:
123 UNREACHABLE();
124 }
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000125 if (!generation_completed) {
126 delete result;
127 result = NULL;
128 }
129 snapshots_->SnapshotGenerationFinished(result);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000130 return result;
131}
132
133
ager@chromium.org5f0c45f2010-12-17 08:51:21 +0000134HeapSnapshot* HeapProfiler::TakeSnapshotImpl(String* name,
135 int type,
136 v8::ActivityControl* control) {
whesse@chromium.orgb08986c2011-03-14 16:13:42 +0000137 return TakeSnapshotImpl(snapshots_->names()->GetName(name), type, control);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000138}
139
140
141int HeapProfiler::GetSnapshotsCount() {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000142 HeapProfiler* profiler = Isolate::Current()->heap_profiler();
143 ASSERT(profiler != NULL);
144 return profiler->snapshots_->snapshots()->length();
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000145}
146
147
148HeapSnapshot* HeapProfiler::GetSnapshot(int index) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000149 HeapProfiler* profiler = Isolate::Current()->heap_profiler();
150 ASSERT(profiler != NULL);
151 return profiler->snapshots_->snapshots()->at(index);
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000152}
153
154
155HeapSnapshot* HeapProfiler::FindSnapshot(unsigned uid) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000156 HeapProfiler* profiler = Isolate::Current()->heap_profiler();
157 ASSERT(profiler != NULL);
158 return profiler->snapshots_->GetSnapshot(uid);
159}
160
161
162void HeapProfiler::DeleteAllSnapshots() {
163 HeapProfiler* profiler = Isolate::Current()->heap_profiler();
164 ASSERT(profiler != NULL);
165 profiler->ResetSnapshots();
whesse@chromium.org2c186ca2010-06-16 11:32:39 +0000166}
167
168
ricow@chromium.org4980dff2010-07-19 08:33:45 +0000169void HeapProfiler::ObjectMoveEvent(Address from, Address to) {
sgjesse@chromium.orgea88ce92011-03-23 11:19:56 +0000170 snapshots_->ObjectMoveEvent(from, to);
ricow@chromium.org4980dff2010-07-19 08:33:45 +0000171}
172
christian.plesner.hansen@gmail.com2bc58ef2009-09-22 10:00:30 +0000173
174} } // namespace v8::internal