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" |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 31 | #include "heap-snapshot-generator-inl.h" |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 32 | |
| 33 | namespace v8 { |
| 34 | namespace internal { |
| 35 | |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 36 | HeapProfiler::HeapProfiler(Heap* heap) |
| 37 | : snapshots_(new HeapSnapshotsCollection(heap)), |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 38 | next_snapshot_uid_(1) { |
| 39 | } |
| 40 | |
| 41 | |
| 42 | HeapProfiler::~HeapProfiler() { |
| 43 | delete snapshots_; |
| 44 | } |
| 45 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 46 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 47 | void HeapProfiler::DeleteAllSnapshots() { |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 48 | Heap* the_heap = heap(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 49 | delete snapshots_; |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 50 | snapshots_ = new HeapSnapshotsCollection(the_heap); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 54 | void HeapProfiler::DefineWrapperClass( |
| 55 | uint16_t class_id, v8::HeapProfiler::WrapperInfoCallback callback) { |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 56 | ASSERT(class_id != v8::HeapProfiler::kPersistentHandleNoClassId); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 57 | if (wrapper_callbacks_.length() <= class_id) { |
| 58 | wrapper_callbacks_.AddBlock( |
| 59 | NULL, class_id - wrapper_callbacks_.length() + 1); |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 60 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 61 | wrapper_callbacks_[class_id] = callback; |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | |
| 65 | v8::RetainedObjectInfo* HeapProfiler::ExecuteWrapperClassCallback( |
| 66 | uint16_t class_id, Object** wrapper) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 67 | if (wrapper_callbacks_.length() <= class_id) return NULL; |
| 68 | return wrapper_callbacks_[class_id]( |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 69 | class_id, Utils::ToLocal(Handle<Object>(wrapper))); |
| 70 | } |
| 71 | |
| 72 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 73 | HeapSnapshot* HeapProfiler::TakeSnapshot( |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 74 | const char* name, |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 75 | v8::ActivityControl* control, |
| 76 | v8::HeapProfiler::ObjectNameResolver* resolver) { |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 77 | HeapSnapshot* result = snapshots_->NewSnapshot(name, next_snapshot_uid_++); |
| 78 | { |
| 79 | HeapSnapshotGenerator generator(result, control, resolver, heap()); |
| 80 | if (!generator.GenerateSnapshot()) { |
| 81 | delete result; |
| 82 | result = NULL; |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 83 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 84 | } |
| 85 | snapshots_->SnapshotGenerationFinished(result); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 86 | return result; |
| 87 | } |
| 88 | |
| 89 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 90 | HeapSnapshot* HeapProfiler::TakeSnapshot( |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 91 | String* name, |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 92 | v8::ActivityControl* control, |
| 93 | v8::HeapProfiler::ObjectNameResolver* resolver) { |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 94 | return TakeSnapshot(snapshots_->names()->GetName(name), control, resolver); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 95 | } |
| 96 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 97 | void HeapProfiler::StartHeapObjectsTracking() { |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 98 | snapshots_->StartHeapObjectsTracking(); |
| 99 | } |
| 100 | |
| 101 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 102 | SnapshotObjectId HeapProfiler::PushHeapObjectsStats(OutputStream* stream) { |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 103 | return snapshots_->PushHeapObjectsStats(stream); |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 107 | void HeapProfiler::StopHeapObjectsTracking() { |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 108 | snapshots_->StopHeapObjectsTracking(); |
| 109 | } |
| 110 | |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 111 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 112 | size_t HeapProfiler::GetMemorySizeUsedByProfiler() { |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 113 | return snapshots_->GetUsedMemorySize(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 117 | int HeapProfiler::GetSnapshotsCount() { |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 118 | return snapshots_->snapshots()->length(); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | |
| 122 | HeapSnapshot* HeapProfiler::GetSnapshot(int index) { |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 123 | return snapshots_->snapshots()->at(index); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | |
| 127 | HeapSnapshot* HeapProfiler::FindSnapshot(unsigned uid) { |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 128 | return snapshots_->GetSnapshot(uid); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 132 | SnapshotObjectId HeapProfiler::GetSnapshotObjectId(Handle<Object> obj) { |
| 133 | if (!obj->IsHeapObject()) |
| 134 | return v8::HeapProfiler::kUnknownObjectId; |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 135 | return snapshots_->FindObjectId(HeapObject::cast(*obj)->address()); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 139 | void HeapProfiler::ObjectMoveEvent(Address from, Address to) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 140 | snapshots_->ObjectMoveEvent(from, to); |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 141 | } |
| 142 | |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 143 | void HeapProfiler::SetRetainedObjectInfo(UniqueId id, |
| 144 | RetainedObjectInfo* info) { |
| 145 | // TODO(yurus, marja): Don't route this information through GlobalHandles. |
| 146 | heap()->isolate()->global_handles()->SetRetainedObjectInfo(id, info); |
| 147 | } |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 148 | |
| 149 | } } // namespace v8::internal |