2010-12-15 08:07:27 +00:00
|
|
|
// Copyright 2009-2010 the V8 project authors. All rights reserved.
|
2014-04-29 06:42:26 +00:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file.
|
2009-09-16 13:41:24 +00:00
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/v8.h"
|
2009-09-16 13:41:24 +00:00
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/heap-profiler.h"
|
2013-12-03 09:48:30 +00:00
|
|
|
|
2014-06-03 08:12:43 +00:00
|
|
|
#include "src/allocation-tracker.h"
|
|
|
|
#include "src/heap-snapshot-generator-inl.h"
|
2009-09-16 13:41:24 +00:00
|
|
|
|
|
|
|
namespace v8 {
|
|
|
|
namespace internal {
|
|
|
|
|
2013-02-11 13:02:20 +00:00
|
|
|
HeapProfiler::HeapProfiler(Heap* heap)
|
2013-12-03 09:48:30 +00:00
|
|
|
: ids_(new HeapObjectsMap(heap)),
|
|
|
|
names_(new StringsStorage(heap)),
|
2013-11-29 09:47:32 +00:00
|
|
|
is_tracking_object_moves_(false) {
|
2010-06-15 11:44:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-03 09:48:30 +00:00
|
|
|
static void DeleteHeapSnapshot(HeapSnapshot** snapshot_ptr) {
|
|
|
|
delete *snapshot_ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-15 11:44:07 +00:00
|
|
|
HeapProfiler::~HeapProfiler() {
|
2013-12-03 09:48:30 +00:00
|
|
|
snapshots_.Iterate(DeleteHeapSnapshot);
|
|
|
|
snapshots_.Clear();
|
2010-06-15 11:44:07 +00:00
|
|
|
}
|
|
|
|
|
2011-03-22 16:10:01 +00:00
|
|
|
|
2013-04-02 08:03:01 +00:00
|
|
|
void HeapProfiler::DeleteAllSnapshots() {
|
2013-12-03 09:48:30 +00:00
|
|
|
snapshots_.Iterate(DeleteHeapSnapshot);
|
|
|
|
snapshots_.Clear();
|
|
|
|
names_.Reset(new StringsStorage(heap()));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void HeapProfiler::RemoveSnapshot(HeapSnapshot* snapshot) {
|
|
|
|
snapshots_.RemoveElement(snapshot);
|
2011-03-22 16:10:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-03-10 12:05:31 +00:00
|
|
|
void HeapProfiler::DefineWrapperClass(
|
|
|
|
uint16_t class_id, v8::HeapProfiler::WrapperInfoCallback callback) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(class_id != v8::HeapProfiler::kPersistentHandleNoClassId);
|
2011-03-18 20:35:07 +00:00
|
|
|
if (wrapper_callbacks_.length() <= class_id) {
|
|
|
|
wrapper_callbacks_.AddBlock(
|
|
|
|
NULL, class_id - wrapper_callbacks_.length() + 1);
|
2011-03-10 12:05:31 +00:00
|
|
|
}
|
2011-03-18 20:35:07 +00:00
|
|
|
wrapper_callbacks_[class_id] = callback;
|
2011-03-10 12:05:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
v8::RetainedObjectInfo* HeapProfiler::ExecuteWrapperClassCallback(
|
|
|
|
uint16_t class_id, Object** wrapper) {
|
2011-03-18 20:35:07 +00:00
|
|
|
if (wrapper_callbacks_.length() <= class_id) return NULL;
|
|
|
|
return wrapper_callbacks_[class_id](
|
2011-03-10 12:05:31 +00:00
|
|
|
class_id, Utils::ToLocal(Handle<Object>(wrapper)));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-02 08:03:01 +00:00
|
|
|
HeapSnapshot* HeapProfiler::TakeSnapshot(
|
2012-12-04 17:17:55 +00:00
|
|
|
v8::ActivityControl* control,
|
|
|
|
v8::HeapProfiler::ObjectNameResolver* resolver) {
|
2015-03-10 15:14:01 +00:00
|
|
|
HeapSnapshot* result = new HeapSnapshot(this);
|
2013-04-02 08:09:59 +00:00
|
|
|
{
|
|
|
|
HeapSnapshotGenerator generator(result, control, resolver, heap());
|
|
|
|
if (!generator.GenerateSnapshot()) {
|
|
|
|
delete result;
|
|
|
|
result = NULL;
|
2013-12-03 09:48:30 +00:00
|
|
|
} else {
|
|
|
|
snapshots_.Add(result);
|
2010-08-18 08:19:29 +00:00
|
|
|
}
|
2010-12-13 10:42:06 +00:00
|
|
|
}
|
2013-12-03 09:48:30 +00:00
|
|
|
ids_->RemoveDeadEntries();
|
2013-11-29 09:47:32 +00:00
|
|
|
is_tracking_object_moves_ = true;
|
2010-06-15 11:44:07 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-02 14:27:24 +00:00
|
|
|
void HeapProfiler::StartHeapObjectsTracking(bool track_allocations) {
|
2013-12-03 09:48:30 +00:00
|
|
|
ids_->UpdateHeapObjectsMap();
|
2013-11-29 09:47:32 +00:00
|
|
|
is_tracking_object_moves_ = true;
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(!is_tracking_allocations());
|
2013-12-02 14:27:24 +00:00
|
|
|
if (track_allocations) {
|
2013-12-09 07:41:20 +00:00
|
|
|
allocation_tracker_.Reset(new AllocationTracker(ids_.get(), names_.get()));
|
2013-12-02 14:27:24 +00:00
|
|
|
heap()->DisableInlineAllocation();
|
|
|
|
}
|
2012-04-13 08:52:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-03-26 08:49:52 +00:00
|
|
|
SnapshotObjectId HeapProfiler::PushHeapObjectsStats(OutputStream* stream,
|
|
|
|
int64_t* timestamp_us) {
|
|
|
|
return ids_->PushHeapObjectsStats(stream, timestamp_us);
|
2012-04-13 08:52:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-02 08:03:01 +00:00
|
|
|
void HeapProfiler::StopHeapObjectsTracking() {
|
2013-12-03 09:48:30 +00:00
|
|
|
ids_->StopHeapObjectsTracking();
|
|
|
|
if (is_tracking_allocations()) {
|
|
|
|
allocation_tracker_.Reset(NULL);
|
2013-12-02 14:27:24 +00:00
|
|
|
heap()->EnableInlineAllocation();
|
|
|
|
}
|
2012-04-13 08:52:25 +00:00
|
|
|
}
|
|
|
|
|
2010-06-15 11:44:07 +00:00
|
|
|
|
2012-06-13 11:02:24 +00:00
|
|
|
size_t HeapProfiler::GetMemorySizeUsedByProfiler() {
|
2013-12-03 09:48:30 +00:00
|
|
|
size_t size = sizeof(*this);
|
|
|
|
size += names_->GetUsedMemorySize();
|
|
|
|
size += ids_->GetUsedMemorySize();
|
|
|
|
size += GetMemoryUsedByList(snapshots_);
|
|
|
|
for (int i = 0; i < snapshots_.length(); ++i) {
|
|
|
|
size += snapshots_[i]->RawSnapshotSize();
|
|
|
|
}
|
|
|
|
return size;
|
2012-06-13 11:02:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-06-15 11:44:07 +00:00
|
|
|
int HeapProfiler::GetSnapshotsCount() {
|
2013-12-03 09:48:30 +00:00
|
|
|
return snapshots_.length();
|
2010-06-15 11:44:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
HeapSnapshot* HeapProfiler::GetSnapshot(int index) {
|
2013-12-03 09:48:30 +00:00
|
|
|
return snapshots_.at(index);
|
2010-06-15 11:44:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-04-16 15:36:19 +00:00
|
|
|
SnapshotObjectId HeapProfiler::GetSnapshotObjectId(Handle<Object> obj) {
|
|
|
|
if (!obj->IsHeapObject())
|
|
|
|
return v8::HeapProfiler::kUnknownObjectId;
|
2013-12-03 09:48:30 +00:00
|
|
|
return ids_->FindEntry(HeapObject::cast(*obj)->address());
|
2011-03-22 16:10:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-14 12:41:28 +00:00
|
|
|
void HeapProfiler::ObjectMoveEvent(Address from, Address to, int size) {
|
2014-03-07 16:13:22 +00:00
|
|
|
bool known_object = ids_->MoveObject(from, to, size);
|
|
|
|
if (!known_object && !allocation_tracker_.is_empty()) {
|
|
|
|
allocation_tracker_->address_to_trace()->MoveObject(from, to, size);
|
|
|
|
}
|
2010-07-15 13:21:50 +00:00
|
|
|
}
|
|
|
|
|
2013-10-14 12:41:28 +00:00
|
|
|
|
2013-11-29 09:54:38 +00:00
|
|
|
void HeapProfiler::AllocationEvent(Address addr, int size) {
|
2013-12-03 09:48:30 +00:00
|
|
|
DisallowHeapAllocation no_allocation;
|
|
|
|
if (!allocation_tracker_.is_empty()) {
|
|
|
|
allocation_tracker_->AllocationEvent(addr, size);
|
|
|
|
}
|
2013-10-14 12:41:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void HeapProfiler::UpdateObjectSizeEvent(Address addr, int size) {
|
2013-12-03 09:48:30 +00:00
|
|
|
ids_->UpdateObjectSize(addr, size);
|
2013-10-14 12:41:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-04-24 15:59:23 +00:00
|
|
|
void HeapProfiler::SetRetainedObjectInfo(UniqueId id,
|
|
|
|
RetainedObjectInfo* info) {
|
|
|
|
// TODO(yurus, marja): Don't route this information through GlobalHandles.
|
|
|
|
heap()->isolate()->global_handles()->SetRetainedObjectInfo(id, info);
|
|
|
|
}
|
2009-09-16 13:41:24 +00:00
|
|
|
|
2013-10-14 12:41:28 +00:00
|
|
|
|
2013-12-03 09:48:30 +00:00
|
|
|
Handle<HeapObject> HeapProfiler::FindHeapObjectById(SnapshotObjectId id) {
|
|
|
|
HeapObject* object = NULL;
|
|
|
|
HeapIterator iterator(heap(), HeapIterator::kFilterUnreachable);
|
|
|
|
// Make sure that object with the given id is still reachable.
|
|
|
|
for (HeapObject* obj = iterator.next();
|
|
|
|
obj != NULL;
|
|
|
|
obj = iterator.next()) {
|
|
|
|
if (ids_->FindEntry(obj->address()) == id) {
|
2014-08-04 11:34:54 +00:00
|
|
|
DCHECK(object == NULL);
|
2013-12-03 09:48:30 +00:00
|
|
|
object = obj;
|
|
|
|
// Can't break -- kFilterUnreachable requires full heap traversal.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return object != NULL ? Handle<HeapObject>(object) : Handle<HeapObject>();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-12-18 08:17:03 +00:00
|
|
|
void HeapProfiler::ClearHeapObjectMap() {
|
|
|
|
ids_.Reset(new HeapObjectsMap(heap()));
|
|
|
|
if (!is_tracking_allocations()) is_tracking_object_moves_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-16 13:41:24 +00:00
|
|
|
} } // namespace v8::internal
|