OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 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 | |
5 #ifndef BASE_TRACE_EVENT_SHARDED_ALLOCATION_REGISTER_H_ | |
6 #define BASE_TRACE_EVENT_SHARDED_ALLOCATION_REGISTER_H_ | |
7 | |
8 #include <memory> | |
9 #include <unordered_map> | |
10 #include <vector> | |
11 | |
12 #include "base/atomicops.h" | |
13 #include "base/base_export.h" | |
14 #include "base/macros.h" | |
15 #include "base/synchronization/lock.h" | |
16 #include "base/trace_event/heap_profiler_allocation_register.h" | |
17 | |
18 namespace base { | |
19 namespace trace_event { | |
20 | |
21 class TraceEventMemoryOverhead; | |
22 | |
23 // This container holds allocations, and context for each allocation [in the | |
24 // form of a back trace]. | |
25 // This container is thread-safe. | |
26 class BASE_EXPORT ShardedAllocationRegister { | |
27 public: | |
28 ShardedAllocationRegister(); | |
29 | |
30 // Must be initialized before the container can be used. | |
31 void Initialize(); | |
32 bool IsInitialized() const; | |
33 | |
34 void SetEnabled(bool enabled); | |
35 bool IsEnabled() const; | |
Primiano Tucci (use gerrit)
2017/05/22 16:37:51
this could be an in-line is_enabled() const { ret
erikchen
2017/05/22 17:10:43
Done.
| |
36 | |
37 ~ShardedAllocationRegister(); | |
38 | |
39 // Inserts allocation details into the container. If the address was present | |
40 // already, its details are updated. |address| must not be null. | |
41 // | |
42 // Returns true if an insert occurred. Inserts may fail because the table | |
43 // is full. | |
44 bool Insert(const void* address, | |
45 size_t size, | |
46 const AllocationContext& context); | |
47 | |
48 // Removes the address from the container if it is present. It is ok to call | |
49 // this with a null pointer. | |
50 void Remove(const void* address); | |
51 | |
52 // Estimates memory overhead including |sizeof(AllocationRegister)|. | |
53 void EstimateTraceMemoryOverhead(TraceEventMemoryOverhead* overhead) const; | |
54 | |
55 using MetricsMap = std::unordered_map<AllocationContext, AllocationMetrics>; | |
Primiano Tucci (use gerrit)
2017/05/22 16:37:51
nit: structs and type declarations should go on th
erikchen
2017/05/22 17:10:43
Done.
| |
56 struct OutputMetrics { | |
57 // Total size of allocated objects. | |
58 size_t size; | |
59 // Total count of allocated objects. | |
60 size_t count; | |
61 }; | |
62 | |
63 // Updates |map| with all allocated objects and their statistics. | |
64 // Returns aggregate statistics. | |
65 OutputMetrics UpdateAndReturnsMetrics(MetricsMap& map) const; | |
66 | |
67 private: | |
68 struct RegisterAndLock { | |
69 RegisterAndLock(); | |
70 ~RegisterAndLock(); | |
71 AllocationRegister allocation_register; | |
72 Lock lock; | |
73 }; | |
74 std::vector<std::unique_ptr<RegisterAndLock>> allocation_registers_; | |
75 | |
76 // This member needs to be checked on every allocation and deallocation [fast | |
77 // path] when heap profiling is enabled. Using a lock here causes significant | |
78 // contention. | |
79 base::subtle::Atomic32 enabled_; | |
80 | |
81 DISALLOW_COPY_AND_ASSIGN(ShardedAllocationRegister); | |
82 }; | |
83 | |
84 } // namespace trace_event | |
85 } // namespace base | |
86 | |
87 #endif // BASE_TRACE_EVENT_SHARDED_ALLOCATION_REGISTER_H_ | |
OLD | NEW |