#include "components/heap_profiling/in_process/heap_profiler_controller.h"
#include <cmath>
#include <limits>
#include <memory>
#include <optional>
#include <string>
#include <string_view>
#include <tuple>
#include <utility>
#include <vector>
#include "base/allocator/dispatcher/reentry_guard.h"
#include "base/check_op.h"
#include "base/command_line.h"
#include "base/feature_list.h"
#include "base/functional/bind.h"
#include "base/functional/callback.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/metrics_hashes.h"
#include "base/notreached.h"
#include "base/numerics/clamped_math.h"
#include "base/profiler/frame.h"
#include "base/profiler/metadata_recorder.h"
#include "base/profiler/module_cache.h"
#include "base/profiler/process_type.h"
#include "base/rand_util.h"
#include "base/sampling_heap_profiler/sampling_heap_profiler.h"
#include "base/sequence_checker.h"
#include "base/strings/strcat.h"
#include "base/task/sequenced_task_runner.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "base/time/time.h"
#include "base/types/pass_key.h"
#include "components/heap_profiling/in_process/browser_process_snapshot_controller.h"
#include "components/heap_profiling/in_process/heap_profiler_parameters.h"
#include "components/heap_profiling/in_process/switches.h"
#include "components/metrics/call_stacks/call_stack_profile_builder.h"
#include "components/services/heap_profiling/public/cpp/merge_samples.h"
#include "components/version_info/channel.h"
#include "third_party/abseil-cpp/absl/cleanup/cleanup.h"
namespace heap_profiling {
namespace {
ProcessType;
HeapProfilerController* g_instance = …;
base::TimeDelta RandomInterval(base::TimeDelta mean) { … }
bool HasProcessHistogramName(ProcessType process_type) { … }
std::string ProcessHistogramName(std::string_view base_name,
ProcessType process_type) { … }
double GetChannelProbability(version_info::Channel channel,
const HeapProfilerParameters& params) { … }
std::pair<bool, std::optional<std::string>> DecideIfCollectionIsEnabled(
version_info::Channel channel,
ProcessType process_type) { … }
}
HeapProfilerController::SnapshotParams::SnapshotParams(
std::optional<base::TimeDelta> mean_interval,
bool use_random_interval,
scoped_refptr<StoppedFlag> stopped,
ProcessType process_type,
base::TimeTicks profiler_creation_time,
base::OnceClosure on_first_snapshot_callback)
: … { … }
HeapProfilerController::SnapshotParams::SnapshotParams(
scoped_refptr<StoppedFlag> stopped,
ProcessType process_type,
base::TimeTicks profiler_creation_time,
uint32_t process_probability_pct,
size_t process_index,
base::OnceClosure on_first_snapshot_callback)
: … { … }
HeapProfilerController::SnapshotParams::~SnapshotParams() = default;
HeapProfilerController::SnapshotParams::SnapshotParams(SnapshotParams&& other) =
default;
HeapProfilerController::SnapshotParams&
HeapProfilerController::SnapshotParams::operator=(SnapshotParams&& other) =
default;
HeapProfilerController* HeapProfilerController::GetInstance() { … }
HeapProfilerController::HeapProfilerController(version_info::Channel channel,
ProcessType process_type)
: … { … }
HeapProfilerController::~HeapProfilerController() { … }
bool HeapProfilerController::StartIfEnabled() { … }
bool HeapProfilerController::GetSyntheticFieldTrial(
std::string& trial_name,
std::string& group_name) const { … }
void HeapProfilerController::SuppressRandomnessForTesting() { … }
void HeapProfilerController::SetFirstSnapshotCallbackForTesting(
base::OnceClosure callback) { … }
void HeapProfilerController::AppendCommandLineSwitchForChildProcess(
base::CommandLine* command_line,
base::ProfilerProcessType child_process_type,
int child_process_id) const { … }
BrowserProcessSnapshotController*
HeapProfilerController::GetBrowserProcessSnapshotController() const { … }
void HeapProfilerController::TakeSnapshotInChildProcess(
base::PassKey<ChildProcessSnapshotController>,
uint32_t process_probability_pct,
size_t process_index) { … }
void HeapProfilerController::AppendCommandLineSwitchForTesting(
base::CommandLine* command_line,
base::ProfilerProcessType child_process_type,
int child_process_id,
BrowserProcessSnapshotController* snapshot_controller) { … }
void HeapProfilerController::AppendCommandLineSwitchInternal(
base::CommandLine* command_line,
base::ProfilerProcessType child_process_type,
int child_process_id,
BrowserProcessSnapshotController* snapshot_controller) { … }
void HeapProfilerController::ScheduleNextSnapshot(SnapshotParams params) { … }
void HeapProfilerController::TakeSnapshot(SnapshotParams params) { … }
void HeapProfilerController::RetrieveAndSendSnapshot(
ProcessType process_type,
base::TimeDelta time_since_profiler_creation,
uint32_t process_probability_pct,
size_t process_index) { … }
}