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 // We sample with a Poisson process, with constant average sampling interval. | |
20 // This follows the exponential probability distribution with parameter | |
21 // λ = 1/rate where rate is the average number of bytes between samples. | |
22 // | |
23 // Let u be a uniformly distributed random number between 0 and 1, then | |
24 // next_sample = (- ln u) / λ | |
25 intptr_t SamplingAllocationObserver::GetNextSampleInterval( | |
26 base::RandomNumberGenerator* random, uint64_t rate) { | |
27 if (FLAG_sampling_heap_profiler_suppress_randomness) { | |
28 return rate; | |
29 } | |
30 double u = random->NextDouble(); | |
31 double next = (-std::log(u)) * rate; | |
32 return next < kPointerSize | |
33 ? kPointerSize | |
34 : (next > INT_MAX ? INT_MAX : static_cast<intptr_t>(next)); | |
35 } | |
36 | |
19 SamplingHeapProfiler::SamplingHeapProfiler(Heap* heap, StringsStorage* names, | 37 SamplingHeapProfiler::SamplingHeapProfiler(Heap* heap, StringsStorage* names, |
20 uint64_t rate, int stack_depth) | 38 uint64_t rate, int stack_depth) |
21 : InlineAllocationObserver(GetNextSampleInterval( | 39 : isolate_(heap->isolate()), |
22 heap->isolate()->random_number_generator(), rate)), | |
23 isolate_(heap->isolate()), | |
24 heap_(heap), | 40 heap_(heap), |
25 random_(isolate_->random_number_generator()), | |
26 names_(names), | 41 names_(names), |
27 samples_(), | 42 samples_(), |
28 rate_(rate), | |
29 stack_depth_(stack_depth) { | 43 stack_depth_(stack_depth) { |
30 heap->new_space()->AddInlineAllocationObserver(this); | 44 new_space_observer_ = new SamplingAllocationObserver( |
45 heap_, rate, rate, this, heap->isolate()->random_number_generator()); | |
46 heap->new_space()->AddAllocationObserver(new_space_observer_); | |
47 other_spaces_observer_ = new SamplingAllocationObserver( | |
48 heap_, rate, rate, this, heap->isolate()->random_number_generator()); | |
Hannes Payer (out of office)
2016/02/08 10:13:48
Use the AllSpaces iterator.
mattloring
2016/02/09 20:20:05
Done.
| |
49 heap->old_space()->AddAllocationObserver(other_spaces_observer_); | |
50 heap->code_space()->AddAllocationObserver(other_spaces_observer_); | |
51 heap->map_space()->AddAllocationObserver(other_spaces_observer_); | |
52 heap->lo_space()->AddAllocationObserver(other_spaces_observer_); | |
31 } | 53 } |
32 | 54 |
33 | 55 |
34 SamplingHeapProfiler::~SamplingHeapProfiler() { | 56 SamplingHeapProfiler::~SamplingHeapProfiler() { |
35 heap_->new_space()->RemoveInlineAllocationObserver(this); | 57 heap_->new_space()->RemoveAllocationObserver(new_space_observer_); |
Hannes Payer (out of office)
2016/02/08 10:13:48
Use the AllSpaces iterator.
mattloring
2016/02/09 20:20:05
Done.
| |
58 heap_->old_space()->RemoveAllocationObserver(other_spaces_observer_); | |
59 heap_->code_space()->RemoveAllocationObserver(other_spaces_observer_); | |
60 heap_->map_space()->RemoveAllocationObserver(other_spaces_observer_); | |
61 heap_->lo_space()->RemoveAllocationObserver(other_spaces_observer_); | |
36 | 62 |
37 // Clear samples and drop all the weak references we are keeping. | 63 // Clear samples and drop all the weak references we are keeping. |
38 std::set<SampledAllocation*>::iterator it; | 64 std::set<SampledAllocation*>::iterator it; |
39 for (it = samples_.begin(); it != samples_.end(); ++it) { | 65 for (it = samples_.begin(); it != samples_.end(); ++it) { |
40 delete *it; | 66 delete *it; |
41 } | 67 } |
42 std::set<SampledAllocation*> empty; | 68 std::set<SampledAllocation*> empty; |
43 samples_.swap(empty); | 69 samples_.swap(empty); |
44 } | 70 } |
45 | 71 |
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 | 72 |
54 void SamplingHeapProfiler::SampleObject(Address soon_object, size_t size) { | 73 void SamplingHeapProfiler::SampleObject(Address soon_object, size_t size) { |
55 DisallowHeapAllocation no_allocation; | 74 DisallowHeapAllocation no_allocation; |
56 | 75 |
57 HandleScope scope(isolate_); | 76 HandleScope scope(isolate_); |
58 HeapObject* heap_object = HeapObject::FromAddress(soon_object); | 77 HeapObject* heap_object = HeapObject::FromAddress(soon_object); |
59 Handle<Object> obj(heap_object, isolate_); | 78 Handle<Object> obj(heap_object, isolate_); |
60 | 79 |
61 // Mark the new block as FreeSpace to make sure the heap is iterable while we | 80 // Mark the new block as FreeSpace to make sure the heap is iterable while we |
62 // are taking the sample. | 81 // are taking the sample. |
63 heap()->CreateFillerObjectAt(soon_object, static_cast<int>(size)); | 82 heap()->CreateFillerObjectAt(soon_object, static_cast<int>(size)); |
64 | 83 |
65 Local<v8::Value> loc = v8::Utils::ToLocal(obj); | 84 Local<v8::Value> loc = v8::Utils::ToLocal(obj); |
66 | 85 |
67 SampledAllocation* sample = | 86 SampledAllocation* sample = |
68 new SampledAllocation(this, isolate_, loc, size, stack_depth_); | 87 new SampledAllocation(this, isolate_, loc, size, stack_depth_); |
69 samples_.insert(sample); | 88 samples_.insert(sample); |
70 } | 89 } |
71 | 90 |
72 | 91 |
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( | 92 void SamplingHeapProfiler::SampledAllocation::OnWeakCallback( |
93 const WeakCallbackInfo<SampledAllocation>& data) { | 93 const WeakCallbackInfo<SampledAllocation>& data) { |
94 SampledAllocation* sample = data.GetParameter(); | 94 SampledAllocation* sample = data.GetParameter(); |
95 sample->sampling_heap_profiler_->samples_.erase(sample); | 95 sample->sampling_heap_profiler_->samples_.erase(sample); |
96 delete sample; | 96 delete sample; |
97 } | 97 } |
98 | 98 |
99 | 99 |
100 SamplingHeapProfiler::FunctionInfo::FunctionInfo(SharedFunctionInfo* shared, | 100 SamplingHeapProfiler::FunctionInfo::FunctionInfo(SharedFunctionInfo* shared, |
101 StringsStorage* names) | 101 StringsStorage* names) |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
244 Node* node = AddStack(profile, scripts, allocation->get_stack()); | 244 Node* node = AddStack(profile, scripts, allocation->get_stack()); |
245 node->allocations.push_back({allocation->get_size(), 1}); | 245 node->allocations.push_back({allocation->get_size(), 1}); |
246 } | 246 } |
247 | 247 |
248 return profile; | 248 return profile; |
249 } | 249 } |
250 | 250 |
251 | 251 |
252 } // namespace internal | 252 } // namespace internal |
253 } // namespace v8 | 253 } // namespace v8 |
OLD | NEW |