1 // Copyright 2015 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/metrics/system_memory_stats_recorder.h"
7 #include "base/metrics/histogram_macros.h"
8 #include "base/process/process_metrics.h"
9 #include "build/build_config.h"
13 // Record a size in megabytes, a potential interval from 250MB up to 32 GB.
14 #define UMA_HISTOGRAM_ALLOCATED_MEGABYTES(name, sample) \
15 UMA_HISTOGRAM_CUSTOM_COUNTS(name, sample, 250, 32768, 50)
17 // Records a statistics |sample| for UMA histogram |name|
18 // using a linear distribution of buckets.
19 #define UMA_HISTOGRAM_LINEAR(name, sample, max, buckets) \
20 STATIC_HISTOGRAM_POINTER_BLOCK( \
22 base::LinearHistogram::FactoryGet( \
24 1, /* Minimum. The 0 bin for underflow is automatically added. */ \
25 max + 1, /* Ensure bucket size of |maximum| / |bucket_count|. */ \
26 buckets + 2, /* Account for the underflow and overflow bins. */ \
27 base::Histogram::kUmaTargetedHistogramFlag))
29 #define UMA_HISTOGRAM_MEGABYTES_LINEAR(name, sample) \
30 UMA_HISTOGRAM_LINEAR(name, sample, 2500, 50)
32 void RecordMemoryStats(RecordMemoryStatsType type) {
33 #if BUILDFLAG(IS_CHROMEOS)
34 // Record graphics GEM object size in a histogram with 50 MB buckets.
36 bool mem_gpu_result = false;
37 base::GraphicsMemoryInfoKB gpu_memory;
38 mem_gpu_result = base::GetGraphicsMemoryInfo(&gpu_memory);
41 mem_gpu_mb = gpu_memory.gpu_memory_size / 1024 / 1024;
43 case RECORD_MEMORY_STATS_EXTENSIONS_OOM_KILLED:
44 UMA_HISTOGRAM_MEGABYTES_LINEAR(
45 "Memory.OOMKill.Extensions.MemGraphicsMB", mem_gpu_mb);
53 base::SystemMemoryInfoKB memory;
54 if (base::GetSystemMemoryInfo(&memory)) {
55 // On Intel, graphics objects are in anonymous pages, but on ARM they are
56 // not. For a total "allocated count" add in graphics pages on ARM.
57 int mem_allocated_mb = (memory.active_anon + memory.inactive_anon) / 1024;
58 #if BUILDFLAG(IS_CHROMEOS) && defined(ARCH_CPU_ARM_FAMILY)
59 mem_allocated_mb += mem_gpu_mb;
62 int mem_available_mb =
63 (memory.active_file + memory.inactive_file + memory.free) / 1024;
66 case RECORD_MEMORY_STATS_CONTENTS_OOM_KILLED: {
67 UMA_HISTOGRAM_ALLOCATED_MEGABYTES(
68 "Memory.OOMKill.Contents.MemAllocatedMB", mem_allocated_mb);
71 case RECORD_MEMORY_STATS_EXTENSIONS_OOM_KILLED: {
72 UMA_HISTOGRAM_ALLOCATED_MEGABYTES(
73 "Memory.OOMKill.Extensions.MemAllocatedMB", mem_allocated_mb);
74 UMA_HISTOGRAM_LARGE_MEMORY_MB(
75 "Memory.OOMKill.Extensions.MemAvailableMB", mem_available_mb);
80 #if BUILDFLAG(IS_CHROMEOS)
81 // Record shared memory (used by renderer/GPU buffers).
82 int mem_shmem_mb = memory.shmem / 1024;
84 case RECORD_MEMORY_STATS_CONTENTS_OOM_KILLED:
85 UMA_HISTOGRAM_MEGABYTES_LINEAR("Memory.OOMKill.Contents.MemShmemMB",
88 case RECORD_MEMORY_STATS_EXTENSIONS_OOM_KILLED:
89 UMA_HISTOGRAM_MEGABYTES_LINEAR("Memory.OOMKill.Extensions.MemShmemMB",
97 } // namespace metrics