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::Unsample( | |
44 size_t size, unsigned int count) { | |
45 double scale = 1.0 / (1.0 - std::exp(-size / rate_)); | |
ofrobots
2016/02/19 05:01:27
Need an assertion (probably in the constructor, pe
mattloring
2016/02/19 05:16:10
What form do assertions take here? Is that the sam
| |
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) { | |
50 heap->new_space()->AddAllocationObserver(new_space_observer_.get()); | 65 heap->new_space()->AddAllocationObserver(new_space_observer_.get()); |
51 AllSpaces spaces(heap); | 66 AllSpaces spaces(heap); |
52 for (Space* space = spaces.next(); space != NULL; space = spaces.next()) { | 67 for (Space* space = spaces.next(); space != NULL; space = spaces.next()) { |
53 if (space != heap->new_space()) { | 68 if (space != heap->new_space()) { |
54 space->AddAllocationObserver(other_spaces_observer_.get()); | 69 space->AddAllocationObserver(other_spaces_observer_.get()); |
55 } | 70 } |
56 } | 71 } |
57 } | 72 } |
58 | 73 |
59 | 74 |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
189 if (script->name()->IsName()) { | 204 if (script->name()->IsName()) { |
190 Name* name = Name::cast(script->name()); | 205 Name* name = Name::cast(script->name()); |
191 script_name = ToApiHandle<v8::String>( | 206 script_name = ToApiHandle<v8::String>( |
192 isolate_->factory()->InternalizeUtf8String(names_->GetName(name))); | 207 isolate_->factory()->InternalizeUtf8String(names_->GetName(name))); |
193 } | 208 } |
194 Handle<Script> script_handle(script); | 209 Handle<Script> script_handle(script); |
195 | 210 |
196 line = 1 + Script::GetLineNumber(script_handle, node->script_position_); | 211 line = 1 + Script::GetLineNumber(script_handle, node->script_position_); |
197 column = 1 + Script::GetColumnNumber(script_handle, node->script_position_); | 212 column = 1 + Script::GetColumnNumber(script_handle, node->script_position_); |
198 for (auto alloc : node->allocations_) { | 213 for (auto alloc : node->allocations_) { |
199 allocations.push_back({alloc.first, alloc.second}); | 214 allocations.push_back(Unsample(alloc.first, alloc.second)); |
200 } | 215 } |
201 } | 216 } |
202 | 217 |
203 profile->nodes().push_back(v8::AllocationProfile::Node( | 218 profile->nodes().push_back(v8::AllocationProfile::Node( |
204 {ToApiHandle<v8::String>( | 219 {ToApiHandle<v8::String>( |
205 isolate_->factory()->InternalizeUtf8String(node->name_)), | 220 isolate_->factory()->InternalizeUtf8String(node->name_)), |
206 script_name, node->script_id_, node->script_position_, line, column, | 221 script_name, node->script_id_, node->script_position_, line, column, |
207 std::vector<v8::AllocationProfile::Node*>(), allocations})); | 222 std::vector<v8::AllocationProfile::Node*>(), allocations})); |
208 v8::AllocationProfile::Node* current = &profile->nodes().back(); | 223 v8::AllocationProfile::Node* current = &profile->nodes().back(); |
209 for (auto child : node->children_) { | 224 for (auto child : node->children_) { |
(...skipping 18 matching lines...) Expand all Loading... | |
228 auto profile = new v8::internal::AllocationProfile(); | 243 auto profile = new v8::internal::AllocationProfile(); |
229 | 244 |
230 TranslateAllocationNode(profile, &profile_root_, scripts); | 245 TranslateAllocationNode(profile, &profile_root_, scripts); |
231 | 246 |
232 return profile; | 247 return profile; |
233 } | 248 } |
234 | 249 |
235 | 250 |
236 } // namespace internal | 251 } // namespace internal |
237 } // namespace v8 | 252 } // namespace v8 |
OLD | NEW |