malloc_dump_provider.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. #ifndef BASE_TRACE_EVENT_MALLOC_DUMP_PROVIDER_H_
  5. #define BASE_TRACE_EVENT_MALLOC_DUMP_PROVIDER_H_
  6. #include "base/allocator/buildflags.h"
  7. #include "base/base_export.h"
  8. #include "base/memory/raw_ptr.h"
  9. #include "base/memory/singleton.h"
  10. #include "base/synchronization/lock.h"
  11. #include "base/time/time.h"
  12. #include "base/trace_event/memory_dump_provider.h"
  13. #include "build/build_config.h"
  14. #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_ANDROID) || \
  15. BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC)
  16. #define MALLOC_MEMORY_TRACING_SUPPORTED
  17. #endif
  18. #if BUILDFLAG(USE_PARTITION_ALLOC)
  19. #include "base/allocator/partition_allocator/partition_stats.h"
  20. #endif
  21. namespace base {
  22. namespace trace_event {
  23. class MemoryAllocatorDump;
  24. // Dump provider which collects process-wide memory stats.
  25. class BASE_EXPORT MallocDumpProvider : public MemoryDumpProvider {
  26. public:
  27. // Name of the allocated_objects dump. Use this to declare suballocator dumps
  28. // from other dump providers.
  29. static const char kAllocatedObjects[];
  30. static MallocDumpProvider* GetInstance();
  31. MallocDumpProvider(const MallocDumpProvider&) = delete;
  32. MallocDumpProvider& operator=(const MallocDumpProvider&) = delete;
  33. // MemoryDumpProvider implementation.
  34. bool OnMemoryDump(const MemoryDumpArgs& args,
  35. ProcessMemoryDump* pmd) override;
  36. private:
  37. friend struct DefaultSingletonTraits<MallocDumpProvider>;
  38. MallocDumpProvider();
  39. ~MallocDumpProvider() override;
  40. void ReportPerMinuteStats(uint64_t syscall_count,
  41. size_t cumulative_brp_quarantined_bytes,
  42. size_t cumulative_brp_quarantined_count,
  43. MemoryAllocatorDump* malloc_dump,
  44. MemoryAllocatorDump* partition_alloc_dump);
  45. bool emit_metrics_on_memory_dump_
  46. GUARDED_BY(emit_metrics_on_memory_dump_lock_) = true;
  47. base::Lock emit_metrics_on_memory_dump_lock_;
  48. #if BUILDFLAG(USE_PARTITION_ALLOC_AS_MALLOC)
  49. // To be accurate, this requires the dump provider to be created very early,
  50. // which is the case. The alternative would be to drop the first data point,
  51. // which is not desirable as early process activity is highly relevant.
  52. base::TimeTicks last_memory_dump_time_ = base::TimeTicks::Now();
  53. uint64_t last_syscall_count_ = 0;
  54. size_t last_cumulative_brp_quarantined_bytes_ = 0;
  55. size_t last_cumulative_brp_quarantined_count_ = 0;
  56. #endif
  57. };
  58. #if BUILDFLAG(USE_PARTITION_ALLOC)
  59. // This class is used to invert the dependency of PartitionAlloc on the
  60. // PartitionAllocMemoryDumpProvider. This implements an interface that will
  61. // be called with memory statistics for each bucket in the allocator.
  62. class BASE_EXPORT MemoryDumpPartitionStatsDumper final
  63. : public partition_alloc::PartitionStatsDumper {
  64. public:
  65. MemoryDumpPartitionStatsDumper(const char* root_name,
  66. ProcessMemoryDump* memory_dump,
  67. MemoryDumpLevelOfDetail level_of_detail);
  68. static const char* kPartitionsDumpName;
  69. // PartitionStatsDumper implementation.
  70. void PartitionDumpTotals(
  71. const char* partition_name,
  72. const partition_alloc::PartitionMemoryStats*) override;
  73. void PartitionsDumpBucketStats(
  74. const char* partition_name,
  75. const partition_alloc::PartitionBucketMemoryStats*) override;
  76. size_t total_mmapped_bytes() const { return total_mmapped_bytes_; }
  77. size_t total_resident_bytes() const { return total_resident_bytes_; }
  78. size_t total_active_bytes() const { return total_active_bytes_; }
  79. size_t total_active_count() const { return total_active_count_; }
  80. uint64_t syscall_count() const { return syscall_count_; }
  81. size_t cumulative_brp_quarantined_bytes() const {
  82. return cumulative_brp_quarantined_bytes_;
  83. }
  84. size_t cumulative_brp_quarantined_count() const {
  85. return cumulative_brp_quarantined_count_;
  86. }
  87. private:
  88. const char* root_name_;
  89. raw_ptr<base::trace_event::ProcessMemoryDump> memory_dump_;
  90. uint64_t uid_ = 0;
  91. size_t total_mmapped_bytes_ = 0;
  92. size_t total_resident_bytes_ = 0;
  93. size_t total_active_bytes_ = 0;
  94. size_t total_active_count_ = 0;
  95. uint64_t syscall_count_ = 0;
  96. size_t cumulative_brp_quarantined_bytes_ = 0;
  97. size_t cumulative_brp_quarantined_count_ = 0;
  98. bool detailed_;
  99. };
  100. #endif // BUILDFLAG(USE_PARTITION_ALLOC)
  101. } // namespace trace_event
  102. } // namespace base
  103. #endif // BASE_TRACE_EVENT_MALLOC_DUMP_PROVIDER_H_