#include "chrome/browser/metrics/process_memory_metrics_emitter.h"
#include <set>
#include "base/feature_list.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/ref_counted.h"
#include "base/run_loop.h"
#include "base/strings/stringprintf.h"
#include "base/task/single_thread_task_runner.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/trace_event_analyzer.h"
#include "base/threading/thread_restrictions.h"
#include "base/trace_event/memory_dump_manager.h"
#include "base/trace_event/trace_config_memory_test_util.h"
#include "build/build_config.h"
#include "chrome/browser/extensions/extension_browsertest.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/tracing.h"
#include "chrome/test/base/ui_test_utils.h"
#include "components/ukm/test_ukm_recorder.h"
#include "content/public/browser/network_service_util.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/test_utils.h"
#include "extensions/buildflags/buildflags.h"
#include "net/dns/mock_host_resolver.h"
#include "partition_alloc/buildflags.h"
#include "services/metrics/public/cpp/ukm_builders.h"
#include "services/metrics/public/cpp/ukm_source.h"
#include "services/network/public/cpp/features.h"
#include "services/resource_coordinator/public/cpp/memory_instrumentation/memory_instrumentation.h"
#include "services/resource_coordinator/public/cpp/memory_instrumentation/os_metrics.h"
#include "url/gurl.h"
#include "url/url_constants.h"
#if BUILDFLAG(ENABLE_EXTENSIONS)
#include "extensions/browser/process_manager.h"
#include "extensions/common/extension.h"
#include "extensions/test/extension_background_page_waiter.h"
#include "extensions/test/test_extension_dir.h"
#endif
namespace {
MemoryDumpDeterminism;
MemoryDumpType;
GlobalMemoryDump;
ProcessType;
#if BUILDFLAG(ENABLE_EXTENSIONS)
Extension;
ProcessManager;
TestExtensionDir;
#endif
UkmEntry;
enum class ValueRestriction { … };
int GetNumRenderers(Browser* browser) { … }
void RequestGlobalDumpCallback(base::OnceClosure quit_closure,
bool success,
uint64_t) { … }
void OnStartTracingDoneCallback(
base::trace_event::MemoryDumpLevelOfDetail dump_type,
base::OnceClosure quit_closure) { … }
class ProcessMemoryMetricsEmitterFake : public ProcessMemoryMetricsEmitter { … };
void CheckMemoryMetric(const std::string& name,
const base::HistogramTester& histogram_tester,
int count,
ValueRestriction value_restriction,
int number_of_processes = 1u) { … }
void CheckExperimentalMemoryMetricsForProcessType(
const base::HistogramTester& histogram_tester,
int count,
const char* process_type,
int number_of_processes) { … }
void CheckExperimentalMemoryMetrics(
const base::HistogramTester& histogram_tester,
int count,
int number_of_renderer_processes,
int number_of_extension_processes) { … }
void CheckStableMemoryMetrics(const base::HistogramTester& histogram_tester,
int count,
int number_of_renderer_processes,
int number_of_extension_processes) { … }
void CheckAllMemoryMetrics(const base::HistogramTester& histogram_tester,
int count,
int number_of_renderer_processes = 1u,
int number_of_extension_processes = 0u) { … }
}
class ProcessMemoryMetricsEmitterTest
: public extensions::ExtensionBrowserTest { … };
IN_PROC_BROWSER_TEST_F(ProcessMemoryMetricsEmitterTest,
DISABLED_FetchAndEmitMetrics) { … }
#if BUILDFLAG(ENABLE_EXTENSIONS)
#if defined(ADDRESS_SANITIZER) || defined(MEMORY_SANITIZER) || \
BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || \
BUILDFLAG(IS_MAC)
#define MAYBE_FetchAndEmitMetricsWithExtensions …
#else
#define MAYBE_FetchAndEmitMetricsWithExtensions …
#endif
IN_PROC_BROWSER_TEST_F(ProcessMemoryMetricsEmitterTest,
MAYBE_FetchAndEmitMetricsWithExtensions) { … }
#if defined(ADDRESS_SANITIZER) || defined(MEMORY_SANITIZER)
#define MAYBE_FetchAndEmitMetricsWithHostedApps …
#else
#define MAYBE_FetchAndEmitMetricsWithHostedApps …
#endif
IN_PROC_BROWSER_TEST_F(ProcessMemoryMetricsEmitterTest,
MAYBE_FetchAndEmitMetricsWithHostedApps) { … }
#endif
IN_PROC_BROWSER_TEST_F(ProcessMemoryMetricsEmitterTest,
DISABLED_FetchDuringTrace) { … }
IN_PROC_BROWSER_TEST_F(ProcessMemoryMetricsEmitterTest,
DISABLED_FetchThreeTimes) { … }
#if defined(ADDRESS_SANITIZER) || defined(MEMORY_SANITIZER) || \
BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_MAC) || \
BUILDFLAG(IS_WIN)
#define MAYBE_ForegroundAndBackgroundPages …
#else
#define MAYBE_ForegroundAndBackgroundPages …
#endif
IN_PROC_BROWSER_TEST_F(ProcessMemoryMetricsEmitterTest,
MAYBE_ForegroundAndBackgroundPages) { … }
#if defined(OFFICIAL_BUILD)
#define MAYBE_RendererBuildId …
#else
#define MAYBE_RendererBuildId …
#endif
IN_PROC_BROWSER_TEST_F(ProcessMemoryMetricsEmitterTest, MAYBE_RendererBuildId) { … }