| OLD | NEW |
| 1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/profiler/sampling-heap-profiler.h" | 5 #include "src/profiler/sampling-heap-profiler.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include "src/api.h" | 9 #include "src/api.h" |
| 10 #include "src/base/utils/random-number-generator.h" | 10 #include "src/base/utils/random-number-generator.h" |
| 11 #include "src/frames-inl.h" | 11 #include "src/frames-inl.h" |
| 12 #include "src/heap/heap.h" | 12 #include "src/heap/heap.h" |
| 13 #include "src/isolate.h" | 13 #include "src/isolate.h" |
| 14 #include "src/profiler/strings-storage.h" | 14 #include "src/profiler/strings-storage.h" |
| 15 | 15 |
| 16 namespace v8 { | 16 namespace v8 { |
| 17 namespace internal { | 17 namespace internal { |
| 18 | 18 |
| 19 |
| 20 // We sample with a Poisson process, with constant average sampling interval. |
| 21 // This follows the exponential probability distribution with parameter |
| 22 // λ = 1/rate where rate is the average number of bytes between samples. |
| 23 // |
| 24 // Let u be a uniformly distributed random number between 0 and 1, then |
| 25 // next_sample = (- ln u) / λ |
| 26 intptr_t SamplingAllocationObserver::GetNextSampleInterval( |
| 27 base::RandomNumberGenerator* random, uint64_t rate) { |
| 28 if (FLAG_sampling_heap_profiler_suppress_randomness) { |
| 29 return rate; |
| 30 } |
| 31 double u = random->NextDouble(); |
| 32 double next = (-std::log(u)) * rate; |
| 33 return next < kPointerSize |
| 34 ? kPointerSize |
| 35 : (next > INT_MAX ? INT_MAX : static_cast<intptr_t>(next)); |
| 36 } |
| 37 |
| 38 |
| 19 SamplingHeapProfiler::SamplingHeapProfiler(Heap* heap, StringsStorage* names, | 39 SamplingHeapProfiler::SamplingHeapProfiler(Heap* heap, StringsStorage* names, |
| 20 uint64_t rate, int stack_depth) | 40 uint64_t rate, int stack_depth) |
| 21 : InlineAllocationObserver(GetNextSampleInterval( | 41 : isolate_(heap->isolate()), |
| 22 heap->isolate()->random_number_generator(), rate)), | |
| 23 isolate_(heap->isolate()), | |
| 24 heap_(heap), | 42 heap_(heap), |
| 25 random_(isolate_->random_number_generator()), | |
| 26 names_(names), | 43 names_(names), |
| 27 samples_(), | 44 samples_(), |
| 28 rate_(rate), | |
| 29 stack_depth_(stack_depth) { | 45 stack_depth_(stack_depth) { |
| 30 heap->new_space()->AddInlineAllocationObserver(this); | 46 new_space_observer_ = new SamplingAllocationObserver( |
| 47 heap_, rate, rate, this, heap->isolate()->random_number_generator()); |
| 48 heap->new_space()->AddInlineAllocationObserver(new_space_observer_); |
| 49 other_spaces_observer_ = new SamplingAllocationObserver( |
| 50 heap_, rate, rate, this, heap->isolate()->random_number_generator()); |
| 51 heap->old_space()->AddAllocationObserver(other_spaces_observer_); |
| 52 heap->code_space()->AddAllocationObserver(other_spaces_observer_); |
| 53 heap->map_space()->AddAllocationObserver(other_spaces_observer_); |
| 54 heap->lo_space()->AddAllocationObserver(other_spaces_observer_); |
| 31 } | 55 } |
| 32 | 56 |
| 33 | 57 |
| 34 SamplingHeapProfiler::~SamplingHeapProfiler() { | 58 SamplingHeapProfiler::~SamplingHeapProfiler() { |
| 35 heap_->new_space()->RemoveInlineAllocationObserver(this); | 59 heap_->new_space()->RemoveInlineAllocationObserver(new_space_observer_); |
| 60 heap_->old_space()->RemoveAllocationObserver(other_spaces_observer_); |
| 61 heap_->code_space()->RemoveAllocationObserver(other_spaces_observer_); |
| 62 heap_->map_space()->RemoveAllocationObserver(other_spaces_observer_); |
| 63 heap_->lo_space()->RemoveAllocationObserver(other_spaces_observer_); |
| 36 | 64 |
| 37 // Clear samples and drop all the weak references we are keeping. | 65 // Clear samples and drop all the weak references we are keeping. |
| 38 std::set<SampledAllocation*>::iterator it; | 66 std::set<SampledAllocation*>::iterator it; |
| 39 for (it = samples_.begin(); it != samples_.end(); ++it) { | 67 for (it = samples_.begin(); it != samples_.end(); ++it) { |
| 40 delete *it; | 68 delete *it; |
| 41 } | 69 } |
| 42 std::set<SampledAllocation*> empty; | 70 std::set<SampledAllocation*> empty; |
| 43 samples_.swap(empty); | 71 samples_.swap(empty); |
| 44 } | 72 } |
| 45 | 73 |
| 46 void SamplingHeapProfiler::Step(int bytes_allocated, Address soon_object, | |
| 47 size_t size) { | |
| 48 DCHECK(heap_->gc_state() == Heap::NOT_IN_GC); | |
| 49 DCHECK(soon_object); | |
| 50 SampleObject(soon_object, size); | |
| 51 } | |
| 52 | |
| 53 | 74 |
| 54 void SamplingHeapProfiler::SampleObject(Address soon_object, size_t size) { | 75 void SamplingHeapProfiler::SampleObject(Address soon_object, size_t size) { |
| 55 DisallowHeapAllocation no_allocation; | 76 DisallowHeapAllocation no_allocation; |
| 56 | 77 |
| 57 HandleScope scope(isolate_); | 78 HandleScope scope(isolate_); |
| 58 HeapObject* heap_object = HeapObject::FromAddress(soon_object); | 79 HeapObject* heap_object = HeapObject::FromAddress(soon_object); |
| 59 Handle<Object> obj(heap_object, isolate_); | 80 Handle<Object> obj(heap_object, isolate_); |
| 60 | 81 |
| 61 // Mark the new block as FreeSpace to make sure the heap is iterable while we | 82 // Mark the new block as FreeSpace to make sure the heap is iterable while we |
| 62 // are taking the sample. | 83 // are taking the sample. |
| 63 heap()->CreateFillerObjectAt(soon_object, static_cast<int>(size)); | 84 heap()->CreateFillerObjectAt(soon_object, static_cast<int>(size)); |
| 64 | 85 |
| 65 Local<v8::Value> loc = v8::Utils::ToLocal(obj); | 86 Local<v8::Value> loc = v8::Utils::ToLocal(obj); |
| 66 | 87 |
| 67 SampledAllocation* sample = | 88 SampledAllocation* sample = |
| 68 new SampledAllocation(this, isolate_, loc, size, stack_depth_); | 89 new SampledAllocation(this, isolate_, loc, size, stack_depth_); |
| 69 samples_.insert(sample); | 90 samples_.insert(sample); |
| 70 } | 91 } |
| 71 | 92 |
| 72 | 93 |
| 73 // We sample with a Poisson process, with constant average sampling interval. | |
| 74 // This follows the exponential probability distribution with parameter | |
| 75 // λ = 1/rate where rate is the average number of bytes between samples. | |
| 76 // | |
| 77 // Let u be a uniformly distributed random number between 0 and 1, then | |
| 78 // next_sample = (- ln u) / λ | |
| 79 intptr_t SamplingHeapProfiler::GetNextSampleInterval( | |
| 80 base::RandomNumberGenerator* random, uint64_t rate) { | |
| 81 if (FLAG_sampling_heap_profiler_suppress_randomness) { | |
| 82 return rate; | |
| 83 } | |
| 84 double u = random->NextDouble(); | |
| 85 double next = (-std::log(u)) * rate; | |
| 86 return next < kPointerSize | |
| 87 ? kPointerSize | |
| 88 : (next > INT_MAX ? INT_MAX : static_cast<intptr_t>(next)); | |
| 89 } | |
| 90 | |
| 91 | |
| 92 void SamplingHeapProfiler::SampledAllocation::OnWeakCallback( | 94 void SamplingHeapProfiler::SampledAllocation::OnWeakCallback( |
| 93 const WeakCallbackInfo<SampledAllocation>& data) { | 95 const WeakCallbackInfo<SampledAllocation>& data) { |
| 94 SampledAllocation* sample = data.GetParameter(); | 96 SampledAllocation* sample = data.GetParameter(); |
| 95 sample->sampling_heap_profiler_->samples_.erase(sample); | 97 sample->sampling_heap_profiler_->samples_.erase(sample); |
| 96 delete sample; | 98 delete sample; |
| 97 } | 99 } |
| 98 | 100 |
| 99 | 101 |
| 100 SamplingHeapProfiler::FunctionInfo::FunctionInfo(SharedFunctionInfo* shared, | 102 SamplingHeapProfiler::FunctionInfo::FunctionInfo(SharedFunctionInfo* shared, |
| 101 StringsStorage* names) | 103 StringsStorage* names) |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 244 Node* node = AddStack(profile, scripts, allocation->get_stack()); | 246 Node* node = AddStack(profile, scripts, allocation->get_stack()); |
| 245 node->allocations.push_back({allocation->get_size(), 1}); | 247 node->allocations.push_back({allocation->get_size(), 1}); |
| 246 } | 248 } |
| 247 | 249 |
| 248 return profile; | 250 return profile; |
| 249 } | 251 } |
| 250 | 252 |
| 251 | 253 |
| 252 } // namespace internal | 254 } // namespace internal |
| 253 } // namespace v8 | 255 } // namespace v8 |
| OLD | NEW |