heap-profiler.cc 6.05 KB
Newer Older
1
// Copyright 2009-2010 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5
#include "src/profiler/heap-profiler.h"
6

7
#include "src/api.h"
8
#include "src/debug/debug.h"
9 10
#include "src/profiler/allocation-tracker.h"
#include "src/profiler/heap-snapshot-generator-inl.h"
11
#include "src/profiler/sampling-heap-profiler.h"
12 13 14 15

namespace v8 {
namespace internal {

16
HeapProfiler::HeapProfiler(Heap* heap)
17 18
    : ids_(new HeapObjectsMap(heap)),
      names_(new StringsStorage(heap)),
19
      is_tracking_object_moves_(false) {
20 21 22
}


23 24 25 26 27
static void DeleteHeapSnapshot(HeapSnapshot** snapshot_ptr) {
  delete *snapshot_ptr;
}


28
HeapProfiler::~HeapProfiler() {
29 30
  snapshots_.Iterate(DeleteHeapSnapshot);
  snapshots_.Clear();
31 32
}

33

34
void HeapProfiler::DeleteAllSnapshots() {
35 36
  snapshots_.Iterate(DeleteHeapSnapshot);
  snapshots_.Clear();
37
  names_.reset(new StringsStorage(heap()));
38 39 40 41 42
}


void HeapProfiler::RemoveSnapshot(HeapSnapshot* snapshot) {
  snapshots_.RemoveElement(snapshot);
43 44 45
}


46 47
void HeapProfiler::DefineWrapperClass(
    uint16_t class_id, v8::HeapProfiler::WrapperInfoCallback callback) {
48
  DCHECK(class_id != v8::HeapProfiler::kPersistentHandleNoClassId);
49 50 51
  if (wrapper_callbacks_.length() <= class_id) {
    wrapper_callbacks_.AddBlock(
        NULL, class_id - wrapper_callbacks_.length() + 1);
52
  }
53
  wrapper_callbacks_[class_id] = callback;
54 55 56 57 58
}


v8::RetainedObjectInfo* HeapProfiler::ExecuteWrapperClassCallback(
    uint16_t class_id, Object** wrapper) {
59 60
  if (wrapper_callbacks_.length() <= class_id) return NULL;
  return wrapper_callbacks_[class_id](
61 62 63 64
      class_id, Utils::ToLocal(Handle<Object>(wrapper)));
}


65
HeapSnapshot* HeapProfiler::TakeSnapshot(
66 67
    v8::ActivityControl* control,
    v8::HeapProfiler::ObjectNameResolver* resolver) {
68
  HeapSnapshot* result = new HeapSnapshot(this);
69 70 71 72 73
  {
    HeapSnapshotGenerator generator(result, control, resolver, heap());
    if (!generator.GenerateSnapshot()) {
      delete result;
      result = NULL;
74 75
    } else {
      snapshots_.Add(result);
76
    }
77
  }
78
  ids_->RemoveDeadEntries();
79
  is_tracking_object_moves_ = true;
80 81 82 83

  heap()->isolate()->debug()->feature_tracker()->Track(
      DebugFeatureTracker::kHeapSnapshot);

84 85 86
  return result;
}

87 88 89
bool HeapProfiler::StartSamplingHeapProfiler(
    uint64_t sample_interval, int stack_depth,
    v8::HeapProfiler::SamplingFlags flags) {
90 91 92
  if (sampling_heap_profiler_.get()) {
    return false;
  }
93
  sampling_heap_profiler_.reset(new SamplingHeapProfiler(
94
      heap(), names_.get(), sample_interval, stack_depth, flags));
95 96 97 98 99
  return true;
}


void HeapProfiler::StopSamplingHeapProfiler() {
100
  sampling_heap_profiler_.reset();
101 102 103 104 105 106 107 108 109 110 111 112
}


v8::AllocationProfile* HeapProfiler::GetAllocationProfile() {
  if (sampling_heap_profiler_.get()) {
    return sampling_heap_profiler_->GetAllocationProfile();
  } else {
    return nullptr;
  }
}


113
void HeapProfiler::StartHeapObjectsTracking(bool track_allocations) {
114
  ids_->UpdateHeapObjectsMap();
115
  is_tracking_object_moves_ = true;
116
  DCHECK(!is_tracking_allocations());
117
  if (track_allocations) {
118
    allocation_tracker_.reset(new AllocationTracker(ids_.get(), names_.get()));
119
    heap()->DisableInlineAllocation();
120 121
    heap()->isolate()->debug()->feature_tracker()->Track(
        DebugFeatureTracker::kAllocationTracking);
122
  }
123 124 125
}


126 127 128
SnapshotObjectId HeapProfiler::PushHeapObjectsStats(OutputStream* stream,
                                                    int64_t* timestamp_us) {
  return ids_->PushHeapObjectsStats(stream, timestamp_us);
129 130 131
}


132
void HeapProfiler::StopHeapObjectsTracking() {
133 134
  ids_->StopHeapObjectsTracking();
  if (is_tracking_allocations()) {
135
    allocation_tracker_.reset();
136 137
    heap()->EnableInlineAllocation();
  }
138 139
}

140

141
size_t HeapProfiler::GetMemorySizeUsedByProfiler() {
142 143 144 145 146 147 148 149
  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;
150 151 152
}


153
int HeapProfiler::GetSnapshotsCount() {
154
  return snapshots_.length();
155 156 157 158
}


HeapSnapshot* HeapProfiler::GetSnapshot(int index) {
159
  return snapshots_.at(index);
160 161 162
}


163 164 165
SnapshotObjectId HeapProfiler::GetSnapshotObjectId(Handle<Object> obj) {
  if (!obj->IsHeapObject())
    return v8::HeapProfiler::kUnknownObjectId;
166
  return ids_->FindEntry(HeapObject::cast(*obj)->address());
167 168 169
}


170
void HeapProfiler::ObjectMoveEvent(Address from, Address to, int size) {
171
  base::LockGuard<base::Mutex> guard(&profiler_mutex_);
172
  bool known_object = ids_->MoveObject(from, to, size);
173
  if (!known_object && allocation_tracker_) {
174 175
    allocation_tracker_->address_to_trace()->MoveObject(from, to, size);
  }
176 177
}

178

179
void HeapProfiler::AllocationEvent(Address addr, int size) {
180
  DisallowHeapAllocation no_allocation;
181
  if (allocation_tracker_) {
182 183
    allocation_tracker_->AllocationEvent(addr, size);
  }
184 185 186 187
}


void HeapProfiler::UpdateObjectSizeEvent(Address addr, int size) {
188
  ids_->UpdateObjectSize(addr, size);
189 190 191
}


192 193 194 195 196
void HeapProfiler::SetRetainedObjectInfo(UniqueId id,
                                         RetainedObjectInfo* info) {
  // TODO(yurus, marja): Don't route this information through GlobalHandles.
  heap()->isolate()->global_handles()->SetRetainedObjectInfo(id, info);
}
197

198

199 200
Handle<HeapObject> HeapProfiler::FindHeapObjectById(SnapshotObjectId id) {
  HeapObject* object = NULL;
201
  HeapIterator iterator(heap(), HeapIterator::kFilterUnreachable);
202 203 204 205 206
  // 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) {
207
      DCHECK(object == NULL);
208 209 210 211 212 213 214 215
      object = obj;
      // Can't break -- kFilterUnreachable requires full heap traversal.
    }
  }
  return object != NULL ? Handle<HeapObject>(object) : Handle<HeapObject>();
}


216
void HeapProfiler::ClearHeapObjectMap() {
217
  ids_.reset(new HeapObjectsMap(heap()));
218 219 220 221
  if (!is_tracking_allocations()) is_tracking_object_moves_ = false;
}


222 223 224
Heap* HeapProfiler::heap() const { return ids_->heap(); }


225 226
}  // namespace internal
}  // namespace v8