| OLD | NEW |
| 1 // Copyright 2009-2010 the V8 project authors. All rights reserved. | 1 // Copyright 2009-2010 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | 27 |
| 28 #include "v8.h" | 28 #include "v8.h" |
| 29 | 29 |
| 30 #include "heap-profiler.h" | 30 #include "heap-profiler.h" |
| 31 |
| 32 #include "allocation-tracker.h" |
| 31 #include "heap-snapshot-generator-inl.h" | 33 #include "heap-snapshot-generator-inl.h" |
| 32 | 34 |
| 33 namespace v8 { | 35 namespace v8 { |
| 34 namespace internal { | 36 namespace internal { |
| 35 | 37 |
| 36 HeapProfiler::HeapProfiler(Heap* heap) | 38 HeapProfiler::HeapProfiler(Heap* heap) |
| 37 : snapshots_(new HeapSnapshotsCollection(heap)), | 39 : ids_(new HeapObjectsMap(heap)), |
| 40 names_(new StringsStorage(heap)), |
| 38 next_snapshot_uid_(1), | 41 next_snapshot_uid_(1), |
| 39 is_tracking_allocations_(false) { | 42 is_tracking_object_moves_(false) { |
| 43 } |
| 44 |
| 45 |
| 46 static void DeleteHeapSnapshot(HeapSnapshot** snapshot_ptr) { |
| 47 delete *snapshot_ptr; |
| 40 } | 48 } |
| 41 | 49 |
| 42 | 50 |
| 43 HeapProfiler::~HeapProfiler() { | 51 HeapProfiler::~HeapProfiler() { |
| 44 delete snapshots_; | 52 snapshots_.Iterate(DeleteHeapSnapshot); |
| 53 snapshots_.Clear(); |
| 45 } | 54 } |
| 46 | 55 |
| 47 | 56 |
| 48 void HeapProfiler::DeleteAllSnapshots() { | 57 void HeapProfiler::DeleteAllSnapshots() { |
| 49 Heap* the_heap = heap(); | 58 snapshots_.Iterate(DeleteHeapSnapshot); |
| 50 delete snapshots_; | 59 snapshots_.Clear(); |
| 51 snapshots_ = new HeapSnapshotsCollection(the_heap); | 60 names_.Reset(new StringsStorage(heap())); |
| 52 } | 61 } |
| 53 | 62 |
| 54 | 63 |
| 64 void HeapProfiler::RemoveSnapshot(HeapSnapshot* snapshot) { |
| 65 snapshots_.RemoveElement(snapshot); |
| 66 } |
| 67 |
| 68 |
| 55 void HeapProfiler::DefineWrapperClass( | 69 void HeapProfiler::DefineWrapperClass( |
| 56 uint16_t class_id, v8::HeapProfiler::WrapperInfoCallback callback) { | 70 uint16_t class_id, v8::HeapProfiler::WrapperInfoCallback callback) { |
| 57 ASSERT(class_id != v8::HeapProfiler::kPersistentHandleNoClassId); | 71 ASSERT(class_id != v8::HeapProfiler::kPersistentHandleNoClassId); |
| 58 if (wrapper_callbacks_.length() <= class_id) { | 72 if (wrapper_callbacks_.length() <= class_id) { |
| 59 wrapper_callbacks_.AddBlock( | 73 wrapper_callbacks_.AddBlock( |
| 60 NULL, class_id - wrapper_callbacks_.length() + 1); | 74 NULL, class_id - wrapper_callbacks_.length() + 1); |
| 61 } | 75 } |
| 62 wrapper_callbacks_[class_id] = callback; | 76 wrapper_callbacks_[class_id] = callback; |
| 63 } | 77 } |
| 64 | 78 |
| 65 | 79 |
| 66 v8::RetainedObjectInfo* HeapProfiler::ExecuteWrapperClassCallback( | 80 v8::RetainedObjectInfo* HeapProfiler::ExecuteWrapperClassCallback( |
| 67 uint16_t class_id, Object** wrapper) { | 81 uint16_t class_id, Object** wrapper) { |
| 68 if (wrapper_callbacks_.length() <= class_id) return NULL; | 82 if (wrapper_callbacks_.length() <= class_id) return NULL; |
| 69 return wrapper_callbacks_[class_id]( | 83 return wrapper_callbacks_[class_id]( |
| 70 class_id, Utils::ToLocal(Handle<Object>(wrapper))); | 84 class_id, Utils::ToLocal(Handle<Object>(wrapper))); |
| 71 } | 85 } |
| 72 | 86 |
| 73 | 87 |
| 74 HeapSnapshot* HeapProfiler::TakeSnapshot( | 88 HeapSnapshot* HeapProfiler::TakeSnapshot( |
| 75 const char* name, | 89 const char* name, |
| 76 v8::ActivityControl* control, | 90 v8::ActivityControl* control, |
| 77 v8::HeapProfiler::ObjectNameResolver* resolver) { | 91 v8::HeapProfiler::ObjectNameResolver* resolver) { |
| 78 HeapSnapshot* result = snapshots_->NewSnapshot(name, next_snapshot_uid_++); | 92 HeapSnapshot* result = new HeapSnapshot(this, name, next_snapshot_uid_++); |
| 79 { | 93 { |
| 80 HeapSnapshotGenerator generator(result, control, resolver, heap()); | 94 HeapSnapshotGenerator generator(result, control, resolver, heap()); |
| 81 if (!generator.GenerateSnapshot()) { | 95 if (!generator.GenerateSnapshot()) { |
| 82 delete result; | 96 delete result; |
| 83 result = NULL; | 97 result = NULL; |
| 98 } else { |
| 99 snapshots_.Add(result); |
| 84 } | 100 } |
| 85 } | 101 } |
| 86 snapshots_->SnapshotGenerationFinished(result); | 102 ids_->RemoveDeadEntries(); |
| 103 is_tracking_object_moves_ = true; |
| 87 return result; | 104 return result; |
| 88 } | 105 } |
| 89 | 106 |
| 90 | 107 |
| 91 HeapSnapshot* HeapProfiler::TakeSnapshot( | 108 HeapSnapshot* HeapProfiler::TakeSnapshot( |
| 92 String* name, | 109 String* name, |
| 93 v8::ActivityControl* control, | 110 v8::ActivityControl* control, |
| 94 v8::HeapProfiler::ObjectNameResolver* resolver) { | 111 v8::HeapProfiler::ObjectNameResolver* resolver) { |
| 95 return TakeSnapshot(snapshots_->names()->GetName(name), control, resolver); | 112 return TakeSnapshot(names_->GetName(name), control, resolver); |
| 96 } | 113 } |
| 97 | 114 |
| 98 | 115 |
| 99 void HeapProfiler::StartHeapObjectsTracking() { | 116 void HeapProfiler::StartHeapObjectsTracking(bool track_allocations) { |
| 100 snapshots_->StartHeapObjectsTracking(); | 117 ids_->UpdateHeapObjectsMap(); |
| 118 is_tracking_object_moves_ = true; |
| 119 ASSERT(!is_tracking_allocations()); |
| 120 if (track_allocations) { |
| 121 allocation_tracker_.Reset(new AllocationTracker(*ids_, *names_)); |
| 122 heap()->DisableInlineAllocation(); |
| 123 } |
| 101 } | 124 } |
| 102 | 125 |
| 103 | 126 |
| 104 SnapshotObjectId HeapProfiler::PushHeapObjectsStats(OutputStream* stream) { | 127 SnapshotObjectId HeapProfiler::PushHeapObjectsStats(OutputStream* stream) { |
| 105 return snapshots_->PushHeapObjectsStats(stream); | 128 return ids_->PushHeapObjectsStats(stream); |
| 106 } | 129 } |
| 107 | 130 |
| 108 | 131 |
| 109 void HeapProfiler::StopHeapObjectsTracking() { | 132 void HeapProfiler::StopHeapObjectsTracking() { |
| 110 snapshots_->StopHeapObjectsTracking(); | 133 ids_->StopHeapObjectsTracking(); |
| 134 if (is_tracking_allocations()) { |
| 135 allocation_tracker_.Reset(NULL); |
| 136 heap()->EnableInlineAllocation(); |
| 137 } |
| 111 } | 138 } |
| 112 | 139 |
| 113 | 140 |
| 114 size_t HeapProfiler::GetMemorySizeUsedByProfiler() { | 141 size_t HeapProfiler::GetMemorySizeUsedByProfiler() { |
| 115 return snapshots_->GetUsedMemorySize(); | 142 size_t size = sizeof(*this); |
| 143 size += names_->GetUsedMemorySize(); |
| 144 size += ids_->GetUsedMemorySize(); |
| 145 size += GetMemoryUsedByList(snapshots_); |
| 146 for (int i = 0; i < snapshots_.length(); ++i) { |
| 147 size += snapshots_[i]->RawSnapshotSize(); |
| 148 } |
| 149 return size; |
| 116 } | 150 } |
| 117 | 151 |
| 118 | 152 |
| 119 int HeapProfiler::GetSnapshotsCount() { | 153 int HeapProfiler::GetSnapshotsCount() { |
| 120 return snapshots_->snapshots()->length(); | 154 return snapshots_.length(); |
| 121 } | 155 } |
| 122 | 156 |
| 123 | 157 |
| 124 HeapSnapshot* HeapProfiler::GetSnapshot(int index) { | 158 HeapSnapshot* HeapProfiler::GetSnapshot(int index) { |
| 125 return snapshots_->snapshots()->at(index); | 159 return snapshots_.at(index); |
| 126 } | 160 } |
| 127 | 161 |
| 128 | 162 |
| 129 SnapshotObjectId HeapProfiler::GetSnapshotObjectId(Handle<Object> obj) { | 163 SnapshotObjectId HeapProfiler::GetSnapshotObjectId(Handle<Object> obj) { |
| 130 if (!obj->IsHeapObject()) | 164 if (!obj->IsHeapObject()) |
| 131 return v8::HeapProfiler::kUnknownObjectId; | 165 return v8::HeapProfiler::kUnknownObjectId; |
| 132 return snapshots_->FindObjectId(HeapObject::cast(*obj)->address()); | 166 return ids_->FindEntry(HeapObject::cast(*obj)->address()); |
| 133 } | 167 } |
| 134 | 168 |
| 135 | 169 |
| 136 void HeapProfiler::ObjectMoveEvent(Address from, Address to, int size) { | 170 void HeapProfiler::ObjectMoveEvent(Address from, Address to, int size) { |
| 137 snapshots_->ObjectMoveEvent(from, to, size); | 171 ids_->MoveObject(from, to, size); |
| 138 } | 172 } |
| 139 | 173 |
| 140 | 174 |
| 141 void HeapProfiler::NewObjectEvent(Address addr, int size) { | 175 void HeapProfiler::AllocationEvent(Address addr, int size) { |
| 142 snapshots_->NewObjectEvent(addr, size); | 176 DisallowHeapAllocation no_allocation; |
| 177 if (!allocation_tracker_.is_empty()) { |
| 178 allocation_tracker_->AllocationEvent(addr, size); |
| 179 } |
| 143 } | 180 } |
| 144 | 181 |
| 145 | 182 |
| 146 void HeapProfiler::UpdateObjectSizeEvent(Address addr, int size) { | 183 void HeapProfiler::UpdateObjectSizeEvent(Address addr, int size) { |
| 147 snapshots_->UpdateObjectSizeEvent(addr, size); | 184 ids_->UpdateObjectSize(addr, size); |
| 148 } | 185 } |
| 149 | 186 |
| 150 | 187 |
| 151 void HeapProfiler::SetRetainedObjectInfo(UniqueId id, | 188 void HeapProfiler::SetRetainedObjectInfo(UniqueId id, |
| 152 RetainedObjectInfo* info) { | 189 RetainedObjectInfo* info) { |
| 153 // TODO(yurus, marja): Don't route this information through GlobalHandles. | 190 // TODO(yurus, marja): Don't route this information through GlobalHandles. |
| 154 heap()->isolate()->global_handles()->SetRetainedObjectInfo(id, info); | 191 heap()->isolate()->global_handles()->SetRetainedObjectInfo(id, info); |
| 155 } | 192 } |
| 156 | 193 |
| 157 | 194 |
| 158 void HeapProfiler::StartHeapAllocationsRecording() { | 195 Handle<HeapObject> HeapProfiler::FindHeapObjectById(SnapshotObjectId id) { |
| 159 StartHeapObjectsTracking(); | 196 heap()->CollectAllGarbage(Heap::kMakeHeapIterableMask, |
| 160 heap()->DisableInlineAllocation(); | 197 "HeapProfiler::FindHeapObjectById"); |
| 161 is_tracking_allocations_ = true; | 198 DisallowHeapAllocation no_allocation; |
| 162 snapshots_->UpdateHeapObjectsMap(); | 199 HeapObject* object = NULL; |
| 200 HeapIterator iterator(heap(), HeapIterator::kFilterUnreachable); |
| 201 // Make sure that object with the given id is still reachable. |
| 202 for (HeapObject* obj = iterator.next(); |
| 203 obj != NULL; |
| 204 obj = iterator.next()) { |
| 205 if (ids_->FindEntry(obj->address()) == id) { |
| 206 ASSERT(object == NULL); |
| 207 object = obj; |
| 208 // Can't break -- kFilterUnreachable requires full heap traversal. |
| 209 } |
| 210 } |
| 211 return object != NULL ? Handle<HeapObject>(object) : Handle<HeapObject>(); |
| 163 } | 212 } |
| 164 | 213 |
| 165 | 214 |
| 166 void HeapProfiler::StopHeapAllocationsRecording() { | |
| 167 StopHeapObjectsTracking(); | |
| 168 heap()->EnableInlineAllocation(); | |
| 169 is_tracking_allocations_ = false; | |
| 170 } | |
| 171 | |
| 172 | |
| 173 } } // namespace v8::internal | 215 } } // namespace v8::internal |
| OLD | NEW |