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" |
(...skipping 15 matching lines...) Expand all Loading... |
26 if (FLAG_sampling_heap_profiler_suppress_randomness) { | 26 if (FLAG_sampling_heap_profiler_suppress_randomness) { |
27 return static_cast<intptr_t>(rate); | 27 return static_cast<intptr_t>(rate); |
28 } | 28 } |
29 double u = random_->NextDouble(); | 29 double u = random_->NextDouble(); |
30 double next = (-std::log(u)) * rate; | 30 double next = (-std::log(u)) * rate; |
31 return next < kPointerSize | 31 return next < kPointerSize |
32 ? kPointerSize | 32 ? kPointerSize |
33 : (next > INT_MAX ? INT_MAX : static_cast<intptr_t>(next)); | 33 : (next > INT_MAX ? INT_MAX : static_cast<intptr_t>(next)); |
34 } | 34 } |
35 | 35 |
| 36 // Samples were collected according to a poisson process. Since we have not |
| 37 // recorded all allocations, we must approximate the shape of the underlying |
| 38 // space of allocations based on the samples we have collected. Given that |
| 39 // we sample at rate R, the probability that an allocation of size S will be |
| 40 // sampled is 1-exp(-S/R). This function uses the above probability to |
| 41 // approximate the true number of allocations with size *size* given that |
| 42 // *count* samples were observed. |
| 43 v8::AllocationProfile::Allocation SamplingHeapProfiler::ScaleSample( |
| 44 size_t size, unsigned int count) { |
| 45 double scale = 1.0 / (1.0 - std::exp(-static_cast<double>(size) / rate_)); |
| 46 // Round count instead of truncating. |
| 47 return {size, static_cast<unsigned int>(count * scale + 0.5)}; |
| 48 } |
| 49 |
36 SamplingHeapProfiler::SamplingHeapProfiler(Heap* heap, StringsStorage* names, | 50 SamplingHeapProfiler::SamplingHeapProfiler(Heap* heap, StringsStorage* names, |
37 uint64_t rate, int stack_depth) | 51 uint64_t rate, int stack_depth) |
38 : isolate_(heap->isolate()), | 52 : isolate_(heap->isolate()), |
39 heap_(heap), | 53 heap_(heap), |
40 new_space_observer_(new SamplingAllocationObserver( | 54 new_space_observer_(new SamplingAllocationObserver( |
41 heap_, static_cast<intptr_t>(rate), rate, this, | 55 heap_, static_cast<intptr_t>(rate), rate, this, |
42 heap->isolate()->random_number_generator())), | 56 heap->isolate()->random_number_generator())), |
43 other_spaces_observer_(new SamplingAllocationObserver( | 57 other_spaces_observer_(new SamplingAllocationObserver( |
44 heap_, static_cast<intptr_t>(rate), rate, this, | 58 heap_, static_cast<intptr_t>(rate), rate, this, |
45 heap->isolate()->random_number_generator())), | 59 heap->isolate()->random_number_generator())), |
46 names_(names), | 60 names_(names), |
47 profile_root_("(root)", v8::UnboundScript::kNoScriptId, 0), | 61 profile_root_("(root)", v8::UnboundScript::kNoScriptId, 0), |
48 samples_(), | 62 samples_(), |
49 stack_depth_(stack_depth) { | 63 stack_depth_(stack_depth), |
| 64 rate_(rate) { |
| 65 CHECK_GT(rate_, 0); |
50 heap->new_space()->AddAllocationObserver(new_space_observer_.get()); | 66 heap->new_space()->AddAllocationObserver(new_space_observer_.get()); |
51 AllSpaces spaces(heap); | 67 AllSpaces spaces(heap); |
52 for (Space* space = spaces.next(); space != NULL; space = spaces.next()) { | 68 for (Space* space = spaces.next(); space != NULL; space = spaces.next()) { |
53 if (space != heap->new_space()) { | 69 if (space != heap->new_space()) { |
54 space->AddAllocationObserver(other_spaces_observer_.get()); | 70 space->AddAllocationObserver(other_spaces_observer_.get()); |
55 } | 71 } |
56 } | 72 } |
57 } | 73 } |
58 | 74 |
59 | 75 |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 if (script->name()->IsName()) { | 205 if (script->name()->IsName()) { |
190 Name* name = Name::cast(script->name()); | 206 Name* name = Name::cast(script->name()); |
191 script_name = ToApiHandle<v8::String>( | 207 script_name = ToApiHandle<v8::String>( |
192 isolate_->factory()->InternalizeUtf8String(names_->GetName(name))); | 208 isolate_->factory()->InternalizeUtf8String(names_->GetName(name))); |
193 } | 209 } |
194 Handle<Script> script_handle(script); | 210 Handle<Script> script_handle(script); |
195 | 211 |
196 line = 1 + Script::GetLineNumber(script_handle, node->script_position_); | 212 line = 1 + Script::GetLineNumber(script_handle, node->script_position_); |
197 column = 1 + Script::GetColumnNumber(script_handle, node->script_position_); | 213 column = 1 + Script::GetColumnNumber(script_handle, node->script_position_); |
198 for (auto alloc : node->allocations_) { | 214 for (auto alloc : node->allocations_) { |
199 allocations.push_back({alloc.first, alloc.second}); | 215 allocations.push_back(ScaleSample(alloc.first, alloc.second)); |
200 } | 216 } |
201 } | 217 } |
202 | 218 |
203 profile->nodes().push_back(v8::AllocationProfile::Node( | 219 profile->nodes().push_back(v8::AllocationProfile::Node( |
204 {ToApiHandle<v8::String>( | 220 {ToApiHandle<v8::String>( |
205 isolate_->factory()->InternalizeUtf8String(node->name_)), | 221 isolate_->factory()->InternalizeUtf8String(node->name_)), |
206 script_name, node->script_id_, node->script_position_, line, column, | 222 script_name, node->script_id_, node->script_position_, line, column, |
207 std::vector<v8::AllocationProfile::Node*>(), allocations})); | 223 std::vector<v8::AllocationProfile::Node*>(), allocations})); |
208 v8::AllocationProfile::Node* current = &profile->nodes().back(); | 224 v8::AllocationProfile::Node* current = &profile->nodes().back(); |
209 for (auto child : node->children_) { | 225 for (auto child : node->children_) { |
(...skipping 18 matching lines...) Expand all Loading... |
228 auto profile = new v8::internal::AllocationProfile(); | 244 auto profile = new v8::internal::AllocationProfile(); |
229 | 245 |
230 TranslateAllocationNode(profile, &profile_root_, scripts); | 246 TranslateAllocationNode(profile, &profile_root_, scripts); |
231 | 247 |
232 return profile; | 248 return profile; |
233 } | 249 } |
234 | 250 |
235 | 251 |
236 } // namespace internal | 252 } // namespace internal |
237 } // namespace v8 | 253 } // namespace v8 |
OLD | NEW |