system_memory_stats_recorder_linux.cc 4.0 KB

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