1 // Copyright 2009-2010 the V8 project authors. All rights reserved. 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" 31 #include "heap-snapshot-generator-inl.h" 32 33 namespace v8 { 34 namespace internal { 35 36 HeapProfiler::HeapProfiler(Heap* heap) 37 : snapshots_(new HeapSnapshotsCollection(heap)), 38 next_snapshot_uid_(1) { 39 } 40 41 42 HeapProfiler::~HeapProfiler() { 43 delete snapshots_; 44 } 45 46 47 void HeapProfiler::DeleteAllSnapshots() { 48 Heap* the_heap = heap(); 49 delete snapshots_; 50 snapshots_ = new HeapSnapshotsCollection(the_heap); 51 } 52 53 54 void HeapProfiler::DefineWrapperClass( 55 uint16_t class_id, v8::HeapProfiler::WrapperInfoCallback callback) { 56 ASSERT(class_id != v8::HeapProfiler::kPersistentHandleNoClassId); 57 if (wrapper_callbacks_.length() <= class_id) { 58 wrapper_callbacks_.AddBlock( 59 NULL, class_id - wrapper_callbacks_.length() + 1); 60 } 61 wrapper_callbacks_[class_id] = callback; 62 } 63 64 65 v8::RetainedObjectInfo* HeapProfiler::ExecuteWrapperClassCallback( 66 uint16_t class_id, Object** wrapper) { 67 if (wrapper_callbacks_.length() <= class_id) return NULL; 68 return wrapper_callbacks_[class_id]( 69 class_id, Utils::ToLocal(Handle<Object>(wrapper))); 70 } 71 72 73 HeapSnapshot* HeapProfiler::TakeSnapshot( 74 const char* name, 75 v8::ActivityControl* control, 76 v8::HeapProfiler::ObjectNameResolver* resolver) { 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; 83 } 84 } 85 snapshots_->SnapshotGenerationFinished(result); 86 return result; 87 } 88 89 90 HeapSnapshot* HeapProfiler::TakeSnapshot( 91 String* name, 92 v8::ActivityControl* control, 93 v8::HeapProfiler::ObjectNameResolver* resolver) { 94 return TakeSnapshot(snapshots_->names()->GetName(name), control, resolver); 95 } 96 97 98 void HeapProfiler::StartHeapObjectsTracking() { 99 snapshots_->StartHeapObjectsTracking(); 100 } 101 102 103 SnapshotObjectId HeapProfiler::PushHeapObjectsStats(OutputStream* stream) { 104 return snapshots_->PushHeapObjectsStats(stream); 105 } 106 107 108 void HeapProfiler::StopHeapObjectsTracking() { 109 snapshots_->StopHeapObjectsTracking(); 110 } 111 112 113 size_t HeapProfiler::GetMemorySizeUsedByProfiler() { 114 return snapshots_->GetUsedMemorySize(); 115 } 116 117 118 int HeapProfiler::GetSnapshotsCount() { 119 return snapshots_->snapshots()->length(); 120 } 121 122 123 HeapSnapshot* HeapProfiler::GetSnapshot(int index) { 124 return snapshots_->snapshots()->at(index); 125 } 126 127 128 SnapshotObjectId HeapProfiler::GetSnapshotObjectId(Handle<Object> obj) { 129 if (!obj->IsHeapObject()) 130 return v8::HeapProfiler::kUnknownObjectId; 131 return snapshots_->FindObjectId(HeapObject::cast(*obj)->address()); 132 } 133 134 135 void HeapProfiler::ObjectMoveEvent(Address from, Address to) { 136 snapshots_->ObjectMoveEvent(from, to); 137 } 138 139 void HeapProfiler::SetRetainedObjectInfo(UniqueId id, 140 RetainedObjectInfo* info) { 141 // TODO(yurus, marja): Don't route this information through GlobalHandles. 142 heap()->isolate()->global_handles()->SetRetainedObjectInfo(id, info); 143 } 144 145 } } // namespace v8::internal 146