ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 1 | // Copyright 2009-2010 the V8 project authors. All rights reserved. |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 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" |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 31 | #include "profile-generator.h" |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 32 | |
| 33 | namespace v8 { |
| 34 | namespace internal { |
| 35 | |
| 36 | |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 37 | HeapProfiler::HeapProfiler() |
| 38 | : snapshots_(new HeapSnapshotsCollection()), |
| 39 | next_snapshot_uid_(1) { |
| 40 | } |
| 41 | |
| 42 | |
| 43 | HeapProfiler::~HeapProfiler() { |
| 44 | delete snapshots_; |
| 45 | } |
| 46 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 47 | |
| 48 | void HeapProfiler::ResetSnapshots() { |
| 49 | delete snapshots_; |
| 50 | snapshots_ = new HeapSnapshotsCollection(); |
| 51 | } |
| 52 | |
| 53 | |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 54 | void HeapProfiler::Setup() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 55 | Isolate* isolate = Isolate::Current(); |
| 56 | if (isolate->heap_profiler() == NULL) { |
| 57 | isolate->set_heap_profiler(new HeapProfiler()); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 58 | } |
| 59 | } |
| 60 | |
| 61 | |
| 62 | void HeapProfiler::TearDown() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 63 | Isolate* isolate = Isolate::Current(); |
| 64 | delete isolate->heap_profiler(); |
| 65 | isolate->set_heap_profiler(NULL); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 69 | HeapSnapshot* HeapProfiler::TakeSnapshot(const char* name, |
| 70 | int type, |
| 71 | v8::ActivityControl* control) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 72 | ASSERT(Isolate::Current()->heap_profiler() != NULL); |
| 73 | return Isolate::Current()->heap_profiler()->TakeSnapshotImpl(name, |
| 74 | type, |
| 75 | control); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 79 | HeapSnapshot* HeapProfiler::TakeSnapshot(String* name, |
| 80 | int type, |
| 81 | v8::ActivityControl* control) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 82 | ASSERT(Isolate::Current()->heap_profiler() != NULL); |
| 83 | return Isolate::Current()->heap_profiler()->TakeSnapshotImpl(name, |
| 84 | type, |
| 85 | control); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 89 | void HeapProfiler::DefineWrapperClass( |
| 90 | uint16_t class_id, v8::HeapProfiler::WrapperInfoCallback callback) { |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 91 | ASSERT(class_id != v8::HeapProfiler::kPersistentHandleNoClassId); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 92 | if (wrapper_callbacks_.length() <= class_id) { |
| 93 | wrapper_callbacks_.AddBlock( |
| 94 | NULL, class_id - wrapper_callbacks_.length() + 1); |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 95 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 96 | wrapper_callbacks_[class_id] = callback; |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | |
| 100 | v8::RetainedObjectInfo* HeapProfiler::ExecuteWrapperClassCallback( |
| 101 | uint16_t class_id, Object** wrapper) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 102 | if (wrapper_callbacks_.length() <= class_id) return NULL; |
| 103 | return wrapper_callbacks_[class_id]( |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 104 | class_id, Utils::ToLocal(Handle<Object>(wrapper))); |
| 105 | } |
| 106 | |
| 107 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 108 | HeapSnapshot* HeapProfiler::TakeSnapshotImpl(const char* name, |
| 109 | int type, |
| 110 | v8::ActivityControl* control) { |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 111 | HeapSnapshot::Type s_type = static_cast<HeapSnapshot::Type>(type); |
| 112 | HeapSnapshot* result = |
| 113 | snapshots_->NewSnapshot(s_type, name, next_snapshot_uid_++); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 114 | bool generation_completed = true; |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 115 | switch (s_type) { |
| 116 | case HeapSnapshot::kFull: { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 117 | HEAP->CollectAllGarbage(true); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 118 | HeapSnapshotGenerator generator(result, control); |
| 119 | generation_completed = generator.GenerateSnapshot(); |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 120 | break; |
| 121 | } |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 122 | default: |
| 123 | UNREACHABLE(); |
| 124 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 125 | if (!generation_completed) { |
| 126 | delete result; |
| 127 | result = NULL; |
| 128 | } |
| 129 | snapshots_->SnapshotGenerationFinished(result); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 130 | return result; |
| 131 | } |
| 132 | |
| 133 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 134 | HeapSnapshot* HeapProfiler::TakeSnapshotImpl(String* name, |
| 135 | int type, |
| 136 | v8::ActivityControl* control) { |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 137 | return TakeSnapshotImpl(snapshots_->names()->GetName(name), type, control); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | |
| 141 | int HeapProfiler::GetSnapshotsCount() { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 142 | HeapProfiler* profiler = Isolate::Current()->heap_profiler(); |
| 143 | ASSERT(profiler != NULL); |
| 144 | return profiler->snapshots_->snapshots()->length(); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | |
| 148 | HeapSnapshot* HeapProfiler::GetSnapshot(int index) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 149 | HeapProfiler* profiler = Isolate::Current()->heap_profiler(); |
| 150 | ASSERT(profiler != NULL); |
| 151 | return profiler->snapshots_->snapshots()->at(index); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | |
| 155 | HeapSnapshot* HeapProfiler::FindSnapshot(unsigned uid) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 156 | HeapProfiler* profiler = Isolate::Current()->heap_profiler(); |
| 157 | ASSERT(profiler != NULL); |
| 158 | return profiler->snapshots_->GetSnapshot(uid); |
| 159 | } |
| 160 | |
| 161 | |
| 162 | void HeapProfiler::DeleteAllSnapshots() { |
| 163 | HeapProfiler* profiler = Isolate::Current()->heap_profiler(); |
| 164 | ASSERT(profiler != NULL); |
| 165 | profiler->ResetSnapshots(); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 169 | void HeapProfiler::ObjectMoveEvent(Address from, Address to) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 170 | snapshots_->ObjectMoveEvent(from, to); |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 171 | } |
| 172 | |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 173 | |
| 174 | } } // namespace v8::internal |