blob: f32a4d764072178e2c8ea0a4b0a28d0dcfb69d96 [file] [log] [blame]
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/heap_profiling/in_process/heap_profiler_controller.h"
#include "base/sampling_heap_profiler/sampling_heap_profiler.h"
#include "base/test/bind.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "build/build_config.h"
#include "components/metrics/call_stack_profile_builder.h"
#include "components/metrics/call_stack_profile_metrics_provider.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/metrics_proto/sampled_profile.pb.h"
// TODO(crbug.com/961073): Fix memory leaks in tests and re-enable on LSAN.
#ifdef LEAK_SANITIZER
#define MAYBE_EmptyProfileIsNotEmitted DISABLED_EmptyProfileIsNotEmitted
#else
#define MAYBE_EmptyProfileIsNotEmitted EmptyProfileIsNotEmitted
#endif
class HeapProfilerControllerTest : public testing::Test {
protected:
base::test::TaskEnvironment task_environment{
base::test::TaskEnvironment::TimeSource::MOCK_TIME};
};
TEST_F(HeapProfilerControllerTest, MAYBE_EmptyProfileIsNotEmitted) {
HeapProfilerController controller;
metrics::CallStackProfileBuilder::SetBrowserProcessReceiverCallback(
base::BindLambdaForTesting(
[](base::TimeTicks time, metrics::SampledProfile profile) {
ADD_FAILURE();
}));
controller.Start();
task_environment.FastForwardBy(base::TimeDelta::FromDays(365));
}
// Sampling profiler is not capable of unwinding stack on Android under tests.
#if !defined(OS_ANDROID)
TEST_F(HeapProfilerControllerTest, ProfileCollectionsScheduler) {
constexpr size_t kAllocationSize = 42 * 1024;
constexpr int kSnapshotsToCollect = 3;
auto controller = std::make_unique<HeapProfilerController>();
int profile_count = 0;
auto check_profile = [&](base::TimeTicks time,
metrics::SampledProfile profile) {
EXPECT_EQ(metrics::SampledProfile::PERIODIC_HEAP_COLLECTION,
profile.trigger_event());
EXPECT_LT(0, profile.call_stack_profile().stack_sample_size());
bool found = false;
for (const metrics::CallStackProfile::StackSample& sample :
profile.call_stack_profile().stack_sample()) {
if (sample.has_weight() &&
static_cast<size_t>(sample.weight()) >= kAllocationSize) {
found = true;
break;
}
}
EXPECT_TRUE(found);
if (++profile_count == kSnapshotsToCollect)
controller.reset();
};
base::test::ScopedFeatureList feature_list;
feature_list.InitAndEnableFeature(
metrics::CallStackProfileMetricsProvider::kHeapProfilerReporting);
metrics::CallStackProfileBuilder::SetBrowserProcessReceiverCallback(
base::BindLambdaForTesting(check_profile));
base::SamplingHeapProfiler::Get()->SetSamplingInterval(1024);
controller->Start();
auto* sampler = base::PoissonAllocationSampler::Get();
sampler->SuppressRandomnessForTest(true);
sampler->RecordAlloc(reinterpret_cast<void*>(0x1337), kAllocationSize,
base::PoissonAllocationSampler::kMalloc, nullptr);
sampler->RecordAlloc(reinterpret_cast<void*>(0x7331), kAllocationSize,
base::PoissonAllocationSampler::kMalloc, nullptr);
task_environment.FastForwardUntilNoTasksRemain();
EXPECT_LE(kSnapshotsToCollect, profile_count);
}
#endif
TEST_F(HeapProfilerControllerTest, MergeSamples) {
using Sample = base::SamplingHeapProfiler::Sample;
Sample sample1(/*size=*/5, /*total=*/100, /*ordinal=*/1);
sample1.stack = {reinterpret_cast<void*>(0x1), reinterpret_cast<void*>(0x2)};
Sample sample2(/*size=*/6, /*total=*/102, /*ordinal=*/2);
sample2.stack = {reinterpret_cast<void*>(0x1), reinterpret_cast<void*>(0x3)};
Sample sample3(/*size=*/7, /*total=*/105, /*ordinal=*/3);
sample3.stack = {reinterpret_cast<void*>(0x1), reinterpret_cast<void*>(0x2)};
std::vector<Sample> samples = {sample1, sample2, sample3};
HeapProfilerController::SampleMap map =
HeapProfilerController::MergeSamples(samples);
ASSERT_EQ(map.size(), 2u);
auto it = map.find(sample1);
ASSERT_TRUE(it != map.end());
EXPECT_EQ(it->second.count, 35u); // 100 / 5 + 105 / 7 = 35
EXPECT_EQ(it->second.total, 205u);
it = map.find(sample2);
ASSERT_TRUE(it != map.end());
EXPECT_EQ(it->second.count, 17u); // 102 / 6 = 17
EXPECT_EQ(it->second.total, 102u);
}