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 29 matching lines...) Expand all Loading... |
40 // sampled is 1-exp(-S/R). This function uses the above probability to | 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 | 41 // approximate the true number of allocations with size *size* given that |
42 // *count* samples were observed. | 42 // *count* samples were observed. |
43 v8::AllocationProfile::Allocation SamplingHeapProfiler::ScaleSample( | 43 v8::AllocationProfile::Allocation SamplingHeapProfiler::ScaleSample( |
44 size_t size, unsigned int count) { | 44 size_t size, unsigned int count) { |
45 double scale = 1.0 / (1.0 - std::exp(-static_cast<double>(size) / rate_)); | 45 double scale = 1.0 / (1.0 - std::exp(-static_cast<double>(size) / rate_)); |
46 // Round count instead of truncating. | 46 // Round count instead of truncating. |
47 return {size, static_cast<unsigned int>(count * scale + 0.5)}; | 47 return {size, static_cast<unsigned int>(count * scale + 0.5)}; |
48 } | 48 } |
49 | 49 |
50 SamplingHeapProfiler::SamplingHeapProfiler(Heap* heap, StringsStorage* names, | 50 SamplingHeapProfiler::SamplingHeapProfiler( |
51 uint64_t rate, int stack_depth) | 51 Heap* heap, StringsStorage* names, uint64_t rate, int stack_depth, |
| 52 v8::HeapProfiler::SamplingFlags flags) |
52 : isolate_(heap->isolate()), | 53 : isolate_(heap->isolate()), |
53 heap_(heap), | 54 heap_(heap), |
54 new_space_observer_(new SamplingAllocationObserver( | 55 new_space_observer_(new SamplingAllocationObserver( |
55 heap_, static_cast<intptr_t>(rate), rate, this, | 56 heap_, static_cast<intptr_t>(rate), rate, this, |
56 heap->isolate()->random_number_generator())), | 57 heap->isolate()->random_number_generator())), |
57 other_spaces_observer_(new SamplingAllocationObserver( | 58 other_spaces_observer_(new SamplingAllocationObserver( |
58 heap_, static_cast<intptr_t>(rate), rate, this, | 59 heap_, static_cast<intptr_t>(rate), rate, this, |
59 heap->isolate()->random_number_generator())), | 60 heap->isolate()->random_number_generator())), |
60 names_(names), | 61 names_(names), |
61 profile_root_(nullptr, "(root)", v8::UnboundScript::kNoScriptId, 0), | 62 profile_root_(nullptr, "(root)", v8::UnboundScript::kNoScriptId, 0), |
62 samples_(), | 63 samples_(), |
63 stack_depth_(stack_depth), | 64 stack_depth_(stack_depth), |
64 rate_(rate) { | 65 rate_(rate), |
| 66 flags_(flags) { |
65 CHECK_GT(rate_, 0); | 67 CHECK_GT(rate_, 0); |
66 heap->new_space()->AddAllocationObserver(new_space_observer_.get()); | 68 heap->new_space()->AddAllocationObserver(new_space_observer_.get()); |
67 AllSpaces spaces(heap); | 69 AllSpaces spaces(heap); |
68 for (Space* space = spaces.next(); space != nullptr; space = spaces.next()) { | 70 for (Space* space = spaces.next(); space != nullptr; space = spaces.next()) { |
69 if (space != heap->new_space()) { | 71 if (space != heap->new_space()) { |
70 space->AddAllocationObserver(other_spaces_observer_.get()); | 72 space->AddAllocationObserver(other_spaces_observer_.get()); |
71 } | 73 } |
72 } | 74 } |
73 } | 75 } |
74 | 76 |
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 // not processed. | 251 // not processed. |
250 for (size_t i = 0; i < child_len; i++) { | 252 for (size_t i = 0; i < child_len; i++) { |
251 current->children.push_back( | 253 current->children.push_back( |
252 TranslateAllocationNode(profile, node->children_[i], scripts)); | 254 TranslateAllocationNode(profile, node->children_[i], scripts)); |
253 } | 255 } |
254 node->pinned_ = false; | 256 node->pinned_ = false; |
255 return current; | 257 return current; |
256 } | 258 } |
257 | 259 |
258 v8::AllocationProfile* SamplingHeapProfiler::GetAllocationProfile() { | 260 v8::AllocationProfile* SamplingHeapProfiler::GetAllocationProfile() { |
| 261 if (flags_ & v8::HeapProfiler::kSamplingForceGC) { |
| 262 isolate_->heap()->CollectAllGarbage(Heap::kNoGCFlags, |
| 263 "SamplingHeapProfiler"); |
| 264 } |
259 // To resolve positions to line/column numbers, we will need to look up | 265 // To resolve positions to line/column numbers, we will need to look up |
260 // scripts. Build a map to allow fast mapping from script id to script. | 266 // scripts. Build a map to allow fast mapping from script id to script. |
261 std::map<int, Handle<Script>> scripts; | 267 std::map<int, Handle<Script>> scripts; |
262 { | 268 { |
263 Script::Iterator iterator(isolate_); | 269 Script::Iterator iterator(isolate_); |
264 while (Script* script = iterator.Next()) { | 270 while (Script* script = iterator.Next()) { |
265 scripts[script->id()] = handle(script); | 271 scripts[script->id()] = handle(script); |
266 } | 272 } |
267 } | 273 } |
268 auto profile = new v8::internal::AllocationProfile(); | 274 auto profile = new v8::internal::AllocationProfile(); |
269 TranslateAllocationNode(profile, &profile_root_, scripts); | 275 TranslateAllocationNode(profile, &profile_root_, scripts); |
270 return profile; | 276 return profile; |
271 } | 277 } |
272 | 278 |
273 | 279 |
274 } // namespace internal | 280 } // namespace internal |
275 } // namespace v8 | 281 } // namespace v8 |
OLD | NEW |