• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 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_ALLOCATOR_PARTITION_ALLOCATOR_PARTITION_STATS_H_
6 #define BASE_ALLOCATOR_PARTITION_ALLOCATOR_PARTITION_STATS_H_
7 
8 #include <cstddef>
9 #include <cstdint>
10 
11 #include "base/allocator/partition_allocator/partition_alloc_base/component_export.h"
12 #include "base/allocator/partition_allocator/partition_alloc_buildflags.h"
13 #include "base/allocator/partition_allocator/partition_alloc_config.h"
14 #include "base/allocator/partition_allocator/partition_alloc_constants.h"
15 
16 namespace partition_alloc {
17 
18 // Most of these are not populated if PA_THREAD_CACHE_ENABLE_STATISTICS is not
19 // defined.
20 struct ThreadCacheStats {
21   uint64_t alloc_count;   // Total allocation requests.
22   uint64_t alloc_hits;    // Thread cache hits.
23   uint64_t alloc_misses;  // Thread cache misses.
24 
25   // Allocation failure details:
26   uint64_t alloc_miss_empty;
27   uint64_t alloc_miss_too_large;
28 
29   // Cache fill details:
30   uint64_t cache_fill_count;
31   uint64_t cache_fill_hits;
32   uint64_t cache_fill_misses;  // Object too large.
33 
34   uint64_t batch_fill_count;  // Number of central allocator requests.
35 
36   // Memory cost:
37   uint32_t bucket_total_memory;
38   uint32_t metadata_overhead;
39 
40 #if PA_CONFIG(THREAD_CACHE_ALLOC_STATS)
41   uint64_t allocs_per_bucket_[internal::kNumBuckets + 1];
42 #endif  // PA_CONFIG(THREAD_CACHE_ALLOC_STATS)
43 };
44 
45 // Per-thread allocation statistics. Only covers allocations made through the
46 // partition linked to the thread cache. As the allocator doesn't record
47 // requested sizes in most cases, the data there will be an overestimate of the
48 // actually requested sizes. It is also not expected to sum up to anything
49 // meaningful across threads, due to the lack of synchronization. Figures there
50 // are cumulative, not net. Since the data below is per-thread, note a thread
51 // can deallocate more than it allocated.
52 struct ThreadAllocStats {
53   uint64_t alloc_count;
54   uint64_t alloc_total_size;
55   uint64_t dealloc_count;
56   uint64_t dealloc_total_size;
57 };
58 
59 // Struct used to retrieve total memory usage of a partition. Used by
60 // PartitionStatsDumper implementation.
61 struct PartitionMemoryStats {
62   size_t total_mmapped_bytes;    // Total bytes mmap()-ed from the system.
63   size_t total_committed_bytes;  // Total size of committed pages.
64   size_t max_committed_bytes;    // Max size of committed pages.
65   size_t total_allocated_bytes;  // Total size of allcoations.
66   size_t max_allocated_bytes;    // Max size of allocations.
67   size_t total_resident_bytes;   // Total bytes provisioned by the partition.
68   size_t total_active_bytes;     // Total active bytes in the partition.
69   size_t total_active_count;  // Total count of active objects in the partition.
70   size_t total_decommittable_bytes;  // Total bytes that could be decommitted.
71   size_t total_discardable_bytes;    // Total bytes that could be discarded.
72 #if BUILDFLAG(ENABLE_BACKUP_REF_PTR_SUPPORT)
73   size_t
74       total_brp_quarantined_bytes;  // Total bytes that are quarantined by BRP.
75   size_t total_brp_quarantined_count;       // Total number of slots that are
76                                             // quarantined by BRP.
77   size_t cumulative_brp_quarantined_bytes;  // Cumulative bytes that are
78                                             // quarantined by BRP.
79   size_t cumulative_brp_quarantined_count;  // Cumulative number of slots that
80                                             // are quarantined by BRP.
81 #endif
82 
83   bool has_thread_cache;
84   ThreadCacheStats current_thread_cache_stats;
85   ThreadCacheStats all_thread_caches_stats;
86 
87   // Count and total duration of system calls made since process start. May not
88   // be reported on all platforms.
89   uint64_t syscall_count;
90   uint64_t syscall_total_time_ns;
91 };
92 
93 // Struct used to retrieve memory statistics about a partition bucket. Used by
94 // PartitionStatsDumper implementation.
95 struct PartitionBucketMemoryStats {
96   bool is_valid;       // Used to check if the stats is valid.
97   bool is_direct_map;  // True if this is a direct mapping; size will not be
98                        // unique.
99   uint32_t bucket_slot_size;          // The size of the slot in bytes.
100   uint32_t allocated_slot_span_size;  // Total size the slot span allocated
101                                       // from the system (committed pages).
102   uint32_t active_bytes;              // Total active bytes used in the bucket.
103   uint32_t active_count;    // Total active objects allocated in the bucket.
104   uint32_t resident_bytes;  // Total bytes provisioned in the bucket.
105   uint32_t decommittable_bytes;    // Total bytes that could be decommitted.
106   uint32_t discardable_bytes;      // Total bytes that could be discarded.
107   uint32_t num_full_slot_spans;    // Number of slot spans with all slots
108                                    // allocated.
109   uint32_t num_active_slot_spans;  // Number of slot spans that have at least
110                                    // one provisioned slot.
111   uint32_t num_empty_slot_spans;   // Number of slot spans that are empty
112                                    // but not decommitted.
113   uint32_t num_decommitted_slot_spans;  // Number of slot spans that are empty
114                                         // and decommitted.
115 };
116 
117 // Interface that is passed to PartitionDumpStats and
118 // PartitionDumpStats for using the memory statistics.
PA_COMPONENT_EXPORT(PARTITION_ALLOC)119 class PA_COMPONENT_EXPORT(PARTITION_ALLOC) PartitionStatsDumper {
120  public:
121   // Called to dump total memory used by partition, once per partition.
122   virtual void PartitionDumpTotals(const char* partition_name,
123                                    const PartitionMemoryStats*) = 0;
124 
125   // Called to dump stats about buckets, for each bucket.
126   virtual void PartitionsDumpBucketStats(const char* partition_name,
127                                          const PartitionBucketMemoryStats*) = 0;
128 };
129 
130 // Simple version of PartitionStatsDumper, storing the returned stats in stats_.
131 // Does not handle per-bucket stats.
PA_COMPONENT_EXPORT(PARTITION_ALLOC)132 class PA_COMPONENT_EXPORT(PARTITION_ALLOC) SimplePartitionStatsDumper
133     : public PartitionStatsDumper {
134  public:
135   SimplePartitionStatsDumper();
136 
137   void PartitionDumpTotals(const char* partition_name,
138                            const PartitionMemoryStats* memory_stats) override;
139 
140   void PartitionsDumpBucketStats(const char* partition_name,
141                                  const PartitionBucketMemoryStats*) override {}
142 
143   const PartitionMemoryStats& stats() const { return stats_; }
144 
145  private:
146   PartitionMemoryStats stats_;
147 };
148 
149 }  // namespace partition_alloc
150 
151 #endif  // BASE_ALLOCATOR_PARTITION_ALLOCATOR_PARTITION_STATS_H_
152