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 | |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 30 | #include "deoptimizer.h" |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 31 | #include "heap-profiler.h" |
ulan@chromium.org | 2e04b58 | 2013-02-21 14:06:02 +0000 | [diff] [blame] | 32 | #include "heap-snapshot-generator-inl.h" |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 33 | |
| 34 | namespace v8 { |
| 35 | namespace internal { |
| 36 | |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 37 | HeapProfiler::HeapProfiler(Heap* heap) |
| 38 | : snapshots_(new HeapSnapshotsCollection(heap)), |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 39 | next_snapshot_uid_(1), |
| 40 | is_tracking_allocations_(false) { |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | |
| 44 | HeapProfiler::~HeapProfiler() { |
| 45 | delete snapshots_; |
| 46 | } |
| 47 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 48 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 49 | void HeapProfiler::DeleteAllSnapshots() { |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 50 | Heap* the_heap = heap(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 51 | delete snapshots_; |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 52 | snapshots_ = new HeapSnapshotsCollection(the_heap); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 56 | void HeapProfiler::DefineWrapperClass( |
| 57 | uint16_t class_id, v8::HeapProfiler::WrapperInfoCallback callback) { |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 58 | ASSERT(class_id != v8::HeapProfiler::kPersistentHandleNoClassId); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 59 | if (wrapper_callbacks_.length() <= class_id) { |
| 60 | wrapper_callbacks_.AddBlock( |
| 61 | NULL, class_id - wrapper_callbacks_.length() + 1); |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 62 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 63 | wrapper_callbacks_[class_id] = callback; |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | |
| 67 | v8::RetainedObjectInfo* HeapProfiler::ExecuteWrapperClassCallback( |
| 68 | uint16_t class_id, Object** wrapper) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 69 | if (wrapper_callbacks_.length() <= class_id) return NULL; |
| 70 | return wrapper_callbacks_[class_id]( |
whesse@chromium.org | b08986c | 2011-03-14 16:13:42 +0000 | [diff] [blame] | 71 | class_id, Utils::ToLocal(Handle<Object>(wrapper))); |
| 72 | } |
| 73 | |
| 74 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 75 | HeapSnapshot* HeapProfiler::TakeSnapshot( |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 76 | const char* name, |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 77 | v8::ActivityControl* control, |
| 78 | v8::HeapProfiler::ObjectNameResolver* resolver) { |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 79 | HeapSnapshot* result = snapshots_->NewSnapshot(name, next_snapshot_uid_++); |
| 80 | { |
| 81 | HeapSnapshotGenerator generator(result, control, resolver, heap()); |
| 82 | if (!generator.GenerateSnapshot()) { |
| 83 | delete result; |
| 84 | result = NULL; |
erik.corry@gmail.com | 145eff5 | 2010-08-23 11:36:18 +0000 | [diff] [blame] | 85 | } |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 86 | } |
| 87 | snapshots_->SnapshotGenerationFinished(result); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 88 | return result; |
| 89 | } |
| 90 | |
| 91 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 92 | HeapSnapshot* HeapProfiler::TakeSnapshot( |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 93 | String* name, |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 94 | v8::ActivityControl* control, |
| 95 | v8::HeapProfiler::ObjectNameResolver* resolver) { |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 96 | return TakeSnapshot(snapshots_->names()->GetName(name), control, resolver); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 97 | } |
| 98 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 99 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 100 | void HeapProfiler::StartHeapObjectsTracking() { |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 101 | snapshots_->StartHeapObjectsTracking(); |
| 102 | } |
| 103 | |
| 104 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 105 | SnapshotObjectId HeapProfiler::PushHeapObjectsStats(OutputStream* stream) { |
rossberg@chromium.org | 400388e | 2012-06-06 09:29:22 +0000 | [diff] [blame] | 106 | return snapshots_->PushHeapObjectsStats(stream); |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 110 | void HeapProfiler::StopHeapObjectsTracking() { |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 111 | snapshots_->StopHeapObjectsTracking(); |
| 112 | } |
| 113 | |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 114 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 115 | size_t HeapProfiler::GetMemorySizeUsedByProfiler() { |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 116 | return snapshots_->GetUsedMemorySize(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 120 | int HeapProfiler::GetSnapshotsCount() { |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 121 | return snapshots_->snapshots()->length(); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | |
| 125 | HeapSnapshot* HeapProfiler::GetSnapshot(int index) { |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 126 | return snapshots_->snapshots()->at(index); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 130 | SnapshotObjectId HeapProfiler::GetSnapshotObjectId(Handle<Object> obj) { |
| 131 | if (!obj->IsHeapObject()) |
| 132 | return v8::HeapProfiler::kUnknownObjectId; |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 133 | return snapshots_->FindObjectId(HeapObject::cast(*obj)->address()); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 137 | void HeapProfiler::ObjectMoveEvent(Address from, Address to, int size) { |
| 138 | snapshots_->ObjectMoveEvent(from, to, size); |
ricow@chromium.org | 4980dff | 2010-07-19 08:33:45 +0000 | [diff] [blame] | 139 | } |
| 140 | |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 141 | |
| 142 | void HeapProfiler::NewObjectEvent(Address addr, int size) { |
| 143 | snapshots_->NewObjectEvent(addr, size); |
| 144 | } |
| 145 | |
| 146 | |
| 147 | void HeapProfiler::UpdateObjectSizeEvent(Address addr, int size) { |
| 148 | snapshots_->UpdateObjectSizeEvent(addr, size); |
| 149 | } |
| 150 | |
| 151 | |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 152 | void HeapProfiler::SetRetainedObjectInfo(UniqueId id, |
| 153 | RetainedObjectInfo* info) { |
| 154 | // TODO(yurus, marja): Don't route this information through GlobalHandles. |
| 155 | heap()->isolate()->global_handles()->SetRetainedObjectInfo(id, info); |
| 156 | } |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 157 | |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 158 | |
| 159 | void HeapProfiler::StartHeapAllocationsRecording() { |
| 160 | StartHeapObjectsTracking(); |
| 161 | is_tracking_allocations_ = true; |
| 162 | DropCompiledCode(); |
| 163 | snapshots_->UpdateHeapObjectsMap(); |
| 164 | } |
| 165 | |
| 166 | |
| 167 | void HeapProfiler::StopHeapAllocationsRecording() { |
| 168 | StopHeapObjectsTracking(); |
| 169 | is_tracking_allocations_ = false; |
| 170 | DropCompiledCode(); |
| 171 | } |
| 172 | |
| 173 | |
| 174 | void HeapProfiler::RecordObjectAllocationFromMasm(Isolate* isolate, |
| 175 | Address obj, |
| 176 | int size) { |
| 177 | isolate->heap_profiler()->NewObjectEvent(obj, size); |
| 178 | } |
| 179 | |
| 180 | |
| 181 | void HeapProfiler::DropCompiledCode() { |
| 182 | Isolate* isolate = heap()->isolate(); |
| 183 | HandleScope scope(isolate); |
| 184 | |
| 185 | if (FLAG_concurrent_recompilation) { |
| 186 | isolate->optimizing_compiler_thread()->Flush(); |
| 187 | } |
| 188 | |
| 189 | Deoptimizer::DeoptimizeAll(isolate); |
| 190 | |
| 191 | Handle<Code> lazy_compile = |
| 192 | Handle<Code>(isolate->builtins()->builtin(Builtins::kLazyCompile)); |
| 193 | |
| 194 | heap()->CollectAllGarbage(Heap::kMakeHeapIterableMask, |
| 195 | "switch allocations tracking"); |
| 196 | |
| 197 | DisallowHeapAllocation no_allocation; |
| 198 | |
| 199 | HeapIterator iterator(heap()); |
| 200 | HeapObject* obj = NULL; |
| 201 | while (((obj = iterator.next()) != NULL)) { |
| 202 | if (obj->IsJSFunction()) { |
| 203 | JSFunction* function = JSFunction::cast(obj); |
| 204 | SharedFunctionInfo* shared = function->shared(); |
| 205 | |
| 206 | if (!shared->allows_lazy_compilation()) continue; |
| 207 | if (!shared->script()->IsScript()) continue; |
| 208 | |
| 209 | Code::Kind kind = function->code()->kind(); |
| 210 | if (kind == Code::FUNCTION || kind == Code::BUILTIN) { |
| 211 | function->set_code(*lazy_compile); |
| 212 | shared->set_code(*lazy_compile); |
| 213 | } |
| 214 | } |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | |
christian.plesner.hansen@gmail.com | 2bc58ef | 2009-09-22 10:00:30 +0000 | [diff] [blame] | 219 | } } // namespace v8::internal |