blob: 4f54ea8e4ac4f4bed6129babe83c80750f6a595e [file] [log] [blame]
// Copyright 2017 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 "base/macros.h"
#include "base/memory/weak_ptr.h"
#include "base/process/process_handle.h"
#include "base/timer/timer.h"
#include "services/resource_coordinator/public/cpp/memory_instrumentation/global_memory_dump.h"
namespace heap_profiling {
class ProfilingProcessHost;
// BackgroundProfilingTriggers is used on the browser process to trigger the
// collection of memory dumps and upload the results to the slow-reports
// service. BackgroundProfilingTriggers class sets a periodic timer and
// interacts with ProfilingProcessHost to trigger and upload memory dumps.
// When started, memory information is collected every hour to check if any
// process is over the trigger threshold. Once a report is uploaded, the
// collection interval is changed to once every 12 hours.
class BackgroundProfilingTriggers {
explicit BackgroundProfilingTriggers(ProfilingProcessHost* host);
virtual ~BackgroundProfilingTriggers();
// Register a periodic timer calling |PerformMemoryUsageChecks|.
void StartTimer();
// High water mark for private footprint of each profiled pid at time of
// upload. Results are stored in |kb|.
// Exposed to subclasses for testing.
std::map<base::ProcessId, uint32_t> pmf_at_last_upload_;
friend class FakeBackgroundProfilingTriggers;
// Returns true if trace uploads are allowed.
bool IsAllowedToUpload() const;
// Returns true if a control report should be sent for the given
// |content_process_type|.
// Virtual for testing.
virtual bool ShouldTriggerControlReport(int content_process_type) const;
// Returns true if |private_footprint_kb| is large enough to trigger
// a report for the given |content_process_type|.
bool IsOverTriggerThreshold(int content_process_type,
uint32_t private_footprint_kb);
// Check the current memory usage and send a slow-report if needed.
void PerformMemoryUsageChecks();
// Called when the memory dump is received. Performs
// checks on memory usage and trigger a memory report with
// |TriggerMemoryReportForProcess| if needed.
void OnReceivedMemoryDump(
std::vector<base::ProcessId> profiled_pids,
bool success,
std::unique_ptr<memory_instrumentation::GlobalMemoryDump> dump);
// Virtual for testing. Called when a memory report needs to be send.
virtual void TriggerMemoryReport(std::string trigger_name);
ProfilingProcessHost* host_;
// Timer to periodically check memory consumption and upload a slow-report.
base::RepeatingTimer timer_;
base::WeakPtrFactory<BackgroundProfilingTriggers> weak_ptr_factory_;
} // namespace heap_profiling