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 #include "base/trace_event/sharded_allocation_register.h" | |
6 | |
7 #include "base/trace_event/trace_event_memory_overhead.h" | |
8 #include "build/build_config.h" | |
9 | |
10 namespace base { | |
11 namespace trace_event { | |
12 | |
13 // This number affects the bucket and capacity counts of AllocationRegister at | |
14 // "base/trace_event/heap_profiler_allocation_register.h". | |
15 #if defined(OS_ANDROID) || defined(OS_IOS) | |
16 size_t ShardCount = 1; | |
17 #else | |
18 size_t ShardCount = 64; | |
19 #endif | |
20 | |
21 size_t HashAddress(const void* address) { | |
Primiano Tucci (use gerrit)
2017/05/22 16:37:51
since this is copy-pasted from a header that is in
erikchen
2017/05/22 17:10:43
Done.
| |
22 // The multiplicative hashing scheme from [Knuth 1998]. | |
23 const uintptr_t key = reinterpret_cast<uintptr_t>(address); | |
24 const uintptr_t a = 131101; | |
25 const uintptr_t shift = 15; | |
26 const uintptr_t h = (key * a) >> shift; | |
27 return h; | |
28 } | |
29 | |
30 ShardedAllocationRegister::ShardedAllocationRegister() : enabled_(false) {} | |
31 | |
32 ShardedAllocationRegister::~ShardedAllocationRegister() = default; | |
33 | |
34 void ShardedAllocationRegister::Initialize() { | |
35 allocation_registers_.resize(ShardCount); | |
36 for (std::unique_ptr<RegisterAndLock>& ral : allocation_registers_) | |
37 ral.reset(new RegisterAndLock); | |
Primiano Tucci (use gerrit)
2017/05/22 16:37:51
doesn't make a difference here (so I don't care ab
erikchen
2017/05/22 17:10:43
Done.
| |
38 } | |
39 | |
40 bool ShardedAllocationRegister::IsInitialized() const { | |
41 return !allocation_registers_.empty(); | |
42 } | |
43 | |
44 void ShardedAllocationRegister::SetEnabled(bool enabled) { | |
Primiano Tucci (use gerrit)
2017/05/22 16:37:50
I would remove this method and do this in initiali
erikchen
2017/05/22 17:10:43
Done. [SetEnabled and SetDisabled]
| |
45 base::subtle::Release_Store(&enabled_, enabled ? 1 : 0); | |
46 } | |
47 | |
48 bool ShardedAllocationRegister::IsEnabled() const { | |
49 return base::subtle::Acquire_Load(&enabled_); | |
Primiano Tucci (use gerrit)
2017/05/22 16:37:51
I'd make move this to the header file and turn int
erikchen
2017/05/22 17:10:43
Done.
| |
50 } | |
51 | |
52 bool ShardedAllocationRegister::Insert(const void* address, | |
53 size_t size, | |
54 const AllocationContext& context) { | |
55 size_t index = HashAddress(address) % ShardCount; | |
56 std::unique_ptr<RegisterAndLock>& ral = allocation_registers_[index]; | |
57 AutoLock lock(ral->lock); | |
58 return ral->allocation_register.Insert(address, size, context); | |
59 } | |
60 | |
61 void ShardedAllocationRegister::Remove(const void* address) { | |
62 size_t index = HashAddress(address) % ShardCount; | |
63 std::unique_ptr<RegisterAndLock>& ral = allocation_registers_[index]; | |
64 AutoLock lock(ral->lock); | |
65 return ral->allocation_register.Remove(address); | |
66 } | |
67 | |
68 void ShardedAllocationRegister::EstimateTraceMemoryOverhead( | |
69 TraceEventMemoryOverhead* overhead) const { | |
70 size_t allocated = 0; | |
71 size_t resident = 0; | |
72 for (const std::unique_ptr<RegisterAndLock>& ral : allocation_registers_) { | |
73 AutoLock lock(ral->lock); | |
74 allocated += ral->allocation_register.EstimateAllocatedMemory(); | |
75 resident += ral->allocation_register.EstimateResidentMemory(); | |
76 } | |
77 | |
78 overhead->Add(TraceEventMemoryOverhead::kHeapProfilerAllocationRegister, | |
79 allocated, resident); | |
80 } | |
81 | |
82 ShardedAllocationRegister::OutputMetrics | |
83 ShardedAllocationRegister::UpdateAndReturnsMetrics(MetricsMap& map) const { | |
84 OutputMetrics output_metrics; | |
85 output_metrics.size = 0; | |
86 output_metrics.count = 0; | |
87 for (const std::unique_ptr<RegisterAndLock>& ral : allocation_registers_) { | |
88 AutoLock lock(ral->lock); | |
89 for (const auto& alloc_size : ral->allocation_register) { | |
90 AllocationMetrics& metrics = map[alloc_size.context]; | |
91 metrics.size += alloc_size.size; | |
92 metrics.count++; | |
93 | |
94 output_metrics.size += alloc_size.size; | |
95 output_metrics.count++; | |
96 } | |
97 } | |
98 return output_metrics; | |
99 } | |
100 | |
101 ShardedAllocationRegister::RegisterAndLock::RegisterAndLock() = default; | |
102 ShardedAllocationRegister::RegisterAndLock::~RegisterAndLock() = default; | |
103 | |
104 } // namespace trace_event | |
105 } // namespace base | |
OLD | NEW |