Commit f823e9e7 authored by Alexei Filippov's avatar Alexei Filippov Committed by Commit Bot

[sampling heap profiler] Fix accounting for the very first allocation on a thread.

The sampler used to always record the very first allocation on each thread
because the accumulator is initialized with zero. The patch makes such
allocations be treates as all the others.

BUG=803276

Change-Id: I3eeec1e069ecc640e6c9756f707dfc336d16e159
Reviewed-on: https://chromium-review.googlesource.com/1257597
Commit-Queue: Alexei Filippov <alph@chromium.org>
Reviewed-by: default avatarErik Chen <erikchen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#596306}
parent 07832750
......@@ -120,6 +120,15 @@ const size_t kDefaultSamplingIntervalBytes = 128 * 1024;
// Accumulated bytes towards sample thread local key.
TLSKey g_accumulated_bytes_tls;
// A boolean used to distinguish first allocation on a thread.
// false - first allocation on the thread.
// true - otherwise
// Since g_accumulated_bytes_tls is initialized with zero the very first
// allocation on a thread would always trigger the sample, thus skewing the
// profile towards such allocations. To mitigate that we use the flag to
// ensure the first allocation is properly accounted.
TLSKey g_sampling_interval_initialized_tls;
// Controls if sample intervals should not be randomized. Used for testing.
bool g_deterministic;
......@@ -294,6 +303,7 @@ void PoissonAllocationSampler::Init() {
ReentryGuard::Init();
TLSInit(&g_internal_reentry_guard);
TLSInit(&g_accumulated_bytes_tls);
TLSInit(&g_sampling_interval_initialized_tls);
return true;
}();
ignore_result(init_once);
......@@ -411,6 +421,16 @@ void PoissonAllocationSampler::DoRecordAlloc(intptr_t accumulated_bytes,
TLSSetValue(g_accumulated_bytes_tls, accumulated_bytes);
if (UNLIKELY(!TLSGetValue(g_sampling_interval_initialized_tls))) {
TLSSetValue(g_sampling_interval_initialized_tls, true);
// This is the very first allocation on the thread. It always produces an
// extra sample because g_accumulated_bytes_tls is initialized with zero
// due to TLS semantics.
// Make sure we don't count this extra sample.
if (!--samples)
return;
}
if (UNLIKELY(TLSGetValue(g_internal_reentry_guard)))
return;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment