Index: src/profiler/sampling-heap-profiler.cc |
diff --git a/src/profiler/sampling-heap-profiler.cc b/src/profiler/sampling-heap-profiler.cc |
index 4127858123af5400b0c5415214d1c62c88291898..ac481b09a33c5455f1e7ab8981b0b17529075f4f 100644 |
--- a/src/profiler/sampling-heap-profiler.cc |
+++ b/src/profiler/sampling-heap-profiler.cc |
@@ -33,6 +33,20 @@ intptr_t SamplingAllocationObserver::GetNextSampleInterval(uint64_t rate) { |
: (next > INT_MAX ? INT_MAX : static_cast<intptr_t>(next)); |
} |
+// Samples were collected according to a poisson process. Since we have not |
+// recorded all allocations, we must approximate the shape of the underlying |
+// space of allocations based on the samples we have collected. Given that |
+// we sample at rate R, the probability that an allocation of size S will be |
+// sampled is 1-exp(-S/R). This function uses the above probability to |
+// approximate the true number of allocations with size *size* given that |
+// *count* samples were observed. |
+v8::AllocationProfile::Allocation SamplingHeapProfiler::ScaleSample( |
+ size_t size, unsigned int count) { |
+ double scale = 1.0 / (1.0 - std::exp(-static_cast<double>(size) / rate_)); |
+ // Round count instead of truncating. |
+ return {size, static_cast<unsigned int>(count * scale + 0.5)}; |
+} |
+ |
SamplingHeapProfiler::SamplingHeapProfiler(Heap* heap, StringsStorage* names, |
uint64_t rate, int stack_depth) |
: isolate_(heap->isolate()), |
@@ -46,7 +60,9 @@ SamplingHeapProfiler::SamplingHeapProfiler(Heap* heap, StringsStorage* names, |
names_(names), |
profile_root_("(root)", v8::UnboundScript::kNoScriptId, 0), |
samples_(), |
- stack_depth_(stack_depth) { |
+ stack_depth_(stack_depth), |
+ rate_(rate) { |
+ CHECK_GT(rate_, 0); |
heap->new_space()->AddAllocationObserver(new_space_observer_.get()); |
AllSpaces spaces(heap); |
for (Space* space = spaces.next(); space != NULL; space = spaces.next()) { |
@@ -196,7 +212,7 @@ v8::AllocationProfile::Node* SamplingHeapProfiler::TranslateAllocationNode( |
line = 1 + Script::GetLineNumber(script_handle, node->script_position_); |
column = 1 + Script::GetColumnNumber(script_handle, node->script_position_); |
for (auto alloc : node->allocations_) { |
- allocations.push_back({alloc.first, alloc.second}); |
+ allocations.push_back(ScaleSample(alloc.first, alloc.second)); |
} |
} |