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