1b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch// Copyright 2009-2010 the V8 project authors. All rights reserved.
2a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// Redistribution and use in source and binary forms, with or without
3a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// modification, are permitted provided that the following conditions are
4a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// met:
5a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//
6a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//     * Redistributions of source code must retain the above copyright
7a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       notice, this list of conditions and the following disclaimer.
8a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//     * Redistributions in binary form must reproduce the above
9a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       copyright notice, this list of conditions and the following
10a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       disclaimer in the documentation and/or other materials provided
11a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       with the distribution.
12a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//     * Neither the name of Google Inc. nor the names of its
13a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       contributors may be used to endorse or promote products derived
14a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//       from this software without specific prior written permission.
15a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block//
16a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
28a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#include "v8.h"
29a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
30a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block#include "heap-profiler.h"
319dcf7e2f83591d471e88bf7d230651900b8e424bKristian Monsen#include "profile-generator.h"
32a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
33a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocknamespace v8 {
34a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Blocknamespace internal {
35a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
36a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block
379dcf7e2f83591d471e88bf7d230651900b8e424bKristian MonsenHeapProfiler::HeapProfiler()
389dcf7e2f83591d471e88bf7d230651900b8e424bKristian Monsen    : snapshots_(new HeapSnapshotsCollection()),
399dcf7e2f83591d471e88bf7d230651900b8e424bKristian Monsen      next_snapshot_uid_(1) {
409dcf7e2f83591d471e88bf7d230651900b8e424bKristian Monsen}
419dcf7e2f83591d471e88bf7d230651900b8e424bKristian Monsen
429dcf7e2f83591d471e88bf7d230651900b8e424bKristian Monsen
439dcf7e2f83591d471e88bf7d230651900b8e424bKristian MonsenHeapProfiler::~HeapProfiler() {
449dcf7e2f83591d471e88bf7d230651900b8e424bKristian Monsen  delete snapshots_;
459dcf7e2f83591d471e88bf7d230651900b8e424bKristian Monsen}
469dcf7e2f83591d471e88bf7d230651900b8e424bKristian Monsen
4744f0eee88ff00398ff7f715fab053374d808c90dSteve Block
4844f0eee88ff00398ff7f715fab053374d808c90dSteve Blockvoid HeapProfiler::ResetSnapshots() {
4944f0eee88ff00398ff7f715fab053374d808c90dSteve Block  delete snapshots_;
5044f0eee88ff00398ff7f715fab053374d808c90dSteve Block  snapshots_ = new HeapSnapshotsCollection();
5144f0eee88ff00398ff7f715fab053374d808c90dSteve Block}
5244f0eee88ff00398ff7f715fab053374d808c90dSteve Block
5344f0eee88ff00398ff7f715fab053374d808c90dSteve Block
543ef787dbeca8a5fb1086949cda830dccee07bfbdBen Murdochvoid HeapProfiler::SetUp() {
5544f0eee88ff00398ff7f715fab053374d808c90dSteve Block  Isolate* isolate = Isolate::Current();
5644f0eee88ff00398ff7f715fab053374d808c90dSteve Block  if (isolate->heap_profiler() == NULL) {
5744f0eee88ff00398ff7f715fab053374d808c90dSteve Block    isolate->set_heap_profiler(new HeapProfiler());
589dcf7e2f83591d471e88bf7d230651900b8e424bKristian Monsen  }
599dcf7e2f83591d471e88bf7d230651900b8e424bKristian Monsen}
609dcf7e2f83591d471e88bf7d230651900b8e424bKristian Monsen
619dcf7e2f83591d471e88bf7d230651900b8e424bKristian Monsen
629dcf7e2f83591d471e88bf7d230651900b8e424bKristian Monsenvoid HeapProfiler::TearDown() {
6344f0eee88ff00398ff7f715fab053374d808c90dSteve Block  Isolate* isolate = Isolate::Current();
6444f0eee88ff00398ff7f715fab053374d808c90dSteve Block  delete isolate->heap_profiler();
6544f0eee88ff00398ff7f715fab053374d808c90dSteve Block  isolate->set_heap_profiler(NULL);
669dcf7e2f83591d471e88bf7d230651900b8e424bKristian Monsen}
679dcf7e2f83591d471e88bf7d230651900b8e424bKristian Monsen
689dcf7e2f83591d471e88bf7d230651900b8e424bKristian Monsen
69b0fe1620dcb4135ac3ab2d66ff93072373911299Ben MurdochHeapSnapshot* HeapProfiler::TakeSnapshot(const char* name,
70b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch                                         int type,
71b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch                                         v8::ActivityControl* control) {
7244f0eee88ff00398ff7f715fab053374d808c90dSteve Block  ASSERT(Isolate::Current()->heap_profiler() != NULL);
7344f0eee88ff00398ff7f715fab053374d808c90dSteve Block  return Isolate::Current()->heap_profiler()->TakeSnapshotImpl(name,
7444f0eee88ff00398ff7f715fab053374d808c90dSteve Block                                                               type,
7544f0eee88ff00398ff7f715fab053374d808c90dSteve Block                                                               control);
769dcf7e2f83591d471e88bf7d230651900b8e424bKristian Monsen}
779dcf7e2f83591d471e88bf7d230651900b8e424bKristian Monsen
789dcf7e2f83591d471e88bf7d230651900b8e424bKristian Monsen
79b0fe1620dcb4135ac3ab2d66ff93072373911299Ben MurdochHeapSnapshot* HeapProfiler::TakeSnapshot(String* name,
80b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch                                         int type,
81b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch                                         v8::ActivityControl* control) {
8244f0eee88ff00398ff7f715fab053374d808c90dSteve Block  ASSERT(Isolate::Current()->heap_profiler() != NULL);
8344f0eee88ff00398ff7f715fab053374d808c90dSteve Block  return Isolate::Current()->heap_profiler()->TakeSnapshotImpl(name,
8444f0eee88ff00398ff7f715fab053374d808c90dSteve Block                                                               type,
8544f0eee88ff00398ff7f715fab053374d808c90dSteve Block                                                               control);
8644f0eee88ff00398ff7f715fab053374d808c90dSteve Block}
8744f0eee88ff00398ff7f715fab053374d808c90dSteve Block
8844f0eee88ff00398ff7f715fab053374d808c90dSteve Block
8944f0eee88ff00398ff7f715fab053374d808c90dSteve Blockvoid HeapProfiler::DefineWrapperClass(
9044f0eee88ff00398ff7f715fab053374d808c90dSteve Block    uint16_t class_id, v8::HeapProfiler::WrapperInfoCallback callback) {
9144f0eee88ff00398ff7f715fab053374d808c90dSteve Block  ASSERT(class_id != v8::HeapProfiler::kPersistentHandleNoClassId);
9244f0eee88ff00398ff7f715fab053374d808c90dSteve Block  if (wrapper_callbacks_.length() <= class_id) {
9344f0eee88ff00398ff7f715fab053374d808c90dSteve Block    wrapper_callbacks_.AddBlock(
9444f0eee88ff00398ff7f715fab053374d808c90dSteve Block        NULL, class_id - wrapper_callbacks_.length() + 1);
9544f0eee88ff00398ff7f715fab053374d808c90dSteve Block  }
9644f0eee88ff00398ff7f715fab053374d808c90dSteve Block  wrapper_callbacks_[class_id] = callback;
9744f0eee88ff00398ff7f715fab053374d808c90dSteve Block}
9844f0eee88ff00398ff7f715fab053374d808c90dSteve Block
9944f0eee88ff00398ff7f715fab053374d808c90dSteve Block
10044f0eee88ff00398ff7f715fab053374d808c90dSteve Blockv8::RetainedObjectInfo* HeapProfiler::ExecuteWrapperClassCallback(
10144f0eee88ff00398ff7f715fab053374d808c90dSteve Block    uint16_t class_id, Object** wrapper) {
10244f0eee88ff00398ff7f715fab053374d808c90dSteve Block  if (wrapper_callbacks_.length() <= class_id) return NULL;
10344f0eee88ff00398ff7f715fab053374d808c90dSteve Block  return wrapper_callbacks_[class_id](
10444f0eee88ff00398ff7f715fab053374d808c90dSteve Block      class_id, Utils::ToLocal(Handle<Object>(wrapper)));
1059dcf7e2f83591d471e88bf7d230651900b8e424bKristian Monsen}
1069dcf7e2f83591d471e88bf7d230651900b8e424bKristian Monsen
1079dcf7e2f83591d471e88bf7d230651900b8e424bKristian Monsen
108b0fe1620dcb4135ac3ab2d66ff93072373911299Ben MurdochHeapSnapshot* HeapProfiler::TakeSnapshotImpl(const char* name,
109b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch                                             int type,
110b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch                                             v8::ActivityControl* control) {
111791712a13f1814dd3ab5d1a5ab8ff5dbc476f6d6Steve Block  HeapSnapshot::Type s_type = static_cast<HeapSnapshot::Type>(type);
112791712a13f1814dd3ab5d1a5ab8ff5dbc476f6d6Steve Block  HeapSnapshot* result =
113791712a13f1814dd3ab5d1a5ab8ff5dbc476f6d6Steve Block      snapshots_->NewSnapshot(s_type, name, next_snapshot_uid_++);
114b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  bool generation_completed = true;
115791712a13f1814dd3ab5d1a5ab8ff5dbc476f6d6Steve Block  switch (s_type) {
116791712a13f1814dd3ab5d1a5ab8ff5dbc476f6d6Steve Block    case HeapSnapshot::kFull: {
117b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      HeapSnapshotGenerator generator(result, control);
118b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch      generation_completed = generator.GenerateSnapshot();
119791712a13f1814dd3ab5d1a5ab8ff5dbc476f6d6Steve Block      break;
120791712a13f1814dd3ab5d1a5ab8ff5dbc476f6d6Steve Block    }
121791712a13f1814dd3ab5d1a5ab8ff5dbc476f6d6Steve Block    default:
122791712a13f1814dd3ab5d1a5ab8ff5dbc476f6d6Steve Block      UNREACHABLE();
123791712a13f1814dd3ab5d1a5ab8ff5dbc476f6d6Steve Block  }
124b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  if (!generation_completed) {
125b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    delete result;
126b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch    result = NULL;
127b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  }
128b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch  snapshots_->SnapshotGenerationFinished(result);
1299dcf7e2f83591d471e88bf7d230651900b8e424bKristian Monsen  return result;
1309dcf7e2f83591d471e88bf7d230651900b8e424bKristian Monsen}
1319dcf7e2f83591d471e88bf7d230651900b8e424bKristian Monsen
1329dcf7e2f83591d471e88bf7d230651900b8e424bKristian Monsen
133b0fe1620dcb4135ac3ab2d66ff93072373911299Ben MurdochHeapSnapshot* HeapProfiler::TakeSnapshotImpl(String* name,
134b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch                                             int type,
135b0fe1620dcb4135ac3ab2d66ff93072373911299Ben Murdoch                                             v8::ActivityControl* control) {
13644f0eee88ff00398ff7f715fab053374d808c90dSteve Block  return TakeSnapshotImpl(snapshots_->names()->GetName(name), type, control);
1379dcf7e2f83591d471e88bf7d230651900b8e424bKristian Monsen}
1389dcf7e2f83591d471e88bf7d230651900b8e424bKristian Monsen
1399dcf7e2f83591d471e88bf7d230651900b8e424bKristian Monsen
1409dcf7e2f83591d471e88bf7d230651900b8e424bKristian Monsenint HeapProfiler::GetSnapshotsCount() {
14144f0eee88ff00398ff7f715fab053374d808c90dSteve Block  HeapProfiler* profiler = Isolate::Current()->heap_profiler();
14244f0eee88ff00398ff7f715fab053374d808c90dSteve Block  ASSERT(profiler != NULL);
14344f0eee88ff00398ff7f715fab053374d808c90dSteve Block  return profiler->snapshots_->snapshots()->length();
1449dcf7e2f83591d471e88bf7d230651900b8e424bKristian Monsen}
1459dcf7e2f83591d471e88bf7d230651900b8e424bKristian Monsen
1469dcf7e2f83591d471e88bf7d230651900b8e424bKristian Monsen
1479dcf7e2f83591d471e88bf7d230651900b8e424bKristian MonsenHeapSnapshot* HeapProfiler::GetSnapshot(int index) {
14844f0eee88ff00398ff7f715fab053374d808c90dSteve Block  HeapProfiler* profiler = Isolate::Current()->heap_profiler();
14944f0eee88ff00398ff7f715fab053374d808c90dSteve Block  ASSERT(profiler != NULL);
15044f0eee88ff00398ff7f715fab053374d808c90dSteve Block  return profiler->snapshots_->snapshots()->at(index);
1519dcf7e2f83591d471e88bf7d230651900b8e424bKristian Monsen}
1529dcf7e2f83591d471e88bf7d230651900b8e424bKristian Monsen
1539dcf7e2f83591d471e88bf7d230651900b8e424bKristian Monsen
1549dcf7e2f83591d471e88bf7d230651900b8e424bKristian MonsenHeapSnapshot* HeapProfiler::FindSnapshot(unsigned uid) {
15544f0eee88ff00398ff7f715fab053374d808c90dSteve Block  HeapProfiler* profiler = Isolate::Current()->heap_profiler();
15644f0eee88ff00398ff7f715fab053374d808c90dSteve Block  ASSERT(profiler != NULL);
15744f0eee88ff00398ff7f715fab053374d808c90dSteve Block  return profiler->snapshots_->GetSnapshot(uid);
15844f0eee88ff00398ff7f715fab053374d808c90dSteve Block}
15944f0eee88ff00398ff7f715fab053374d808c90dSteve Block
16044f0eee88ff00398ff7f715fab053374d808c90dSteve Block
16144f0eee88ff00398ff7f715fab053374d808c90dSteve Blockvoid HeapProfiler::DeleteAllSnapshots() {
16244f0eee88ff00398ff7f715fab053374d808c90dSteve Block  HeapProfiler* profiler = Isolate::Current()->heap_profiler();
16344f0eee88ff00398ff7f715fab053374d808c90dSteve Block  ASSERT(profiler != NULL);
16444f0eee88ff00398ff7f715fab053374d808c90dSteve Block  profiler->ResetSnapshots();
1659dcf7e2f83591d471e88bf7d230651900b8e424bKristian Monsen}
1669dcf7e2f83591d471e88bf7d230651900b8e424bKristian Monsen
1679dcf7e2f83591d471e88bf7d230651900b8e424bKristian Monsen
1683bec4d28b1f388dbc06a9c4276e1a03e86c52b04Ben Murdochvoid HeapProfiler::ObjectMoveEvent(Address from, Address to) {
16944f0eee88ff00398ff7f715fab053374d808c90dSteve Block  snapshots_->ObjectMoveEvent(from, to);
1703bec4d28b1f388dbc06a9c4276e1a03e86c52b04Ben Murdoch}
1713bec4d28b1f388dbc06a9c4276e1a03e86c52b04Ben Murdoch
1723bec4d28b1f388dbc06a9c4276e1a03e86c52b04Ben Murdoch
173a7e24c173cf37484693b9abb38e494fa7bd7baebSteve Block} }  // namespace v8::internal
174