OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/trace_event/heap_profiler_allocation_register.h" | 5 #include "base/trace_event/heap_profiler_allocation_register.h" |
6 | 6 |
7 #include "base/trace_event/trace_event_memory_overhead.h" | 7 #include "base/trace_event/trace_event_memory_overhead.h" |
8 | 8 |
9 namespace base { | 9 namespace base { |
10 namespace trace_event { | 10 namespace trace_event { |
11 | 11 |
12 AllocationRegister::AllocationRegister() | 12 AllocationRegister::AllocationRegister() |
13 : AllocationRegister(kNumBuckets * kNumCellsPerBucket) {} | 13 // Reserve enough address space to store |kNumCells| entries if necessary, |
14 | |
15 AllocationRegister::AllocationRegister(uint32_t num_cells) | |
16 // Reserve enough address space to store |num_cells_| entries if necessary, | |
17 // with a guard page after it to crash the program when attempting to store | 14 // with a guard page after it to crash the program when attempting to store |
18 // more entries. | 15 // more entries. |
19 : num_cells_(num_cells), | 16 : cells_(static_cast<Cell*>(AllocateVirtualMemory(kNumCells * |
20 cells_(static_cast<Cell*>(AllocateVirtualMemory(num_cells_ * | |
21 sizeof(Cell)))), | 17 sizeof(Cell)))), |
22 buckets_(static_cast<CellIndex*>( | 18 buckets_(static_cast<CellIndex*>( |
23 AllocateVirtualMemory(kNumBuckets * sizeof(CellIndex)))), | 19 AllocateVirtualMemory(kNumBuckets * sizeof(CellIndex)))), |
24 | 20 |
25 // The free list is empty. The first unused cell is cell 1, because index | 21 // The free list is empty. The first unused cell is cell 1, because index |
26 // 0 is used as list terminator. | 22 // 0 is used as list terminator. |
27 free_list_(0), | 23 free_list_(0), |
28 next_unused_cell_(1) {} | 24 next_unused_cell_(1) {} |
29 | 25 |
30 | |
31 AllocationRegister::~AllocationRegister() { | 26 AllocationRegister::~AllocationRegister() { |
32 FreeVirtualMemory(buckets_, kNumBuckets * sizeof(CellIndex)); | 27 FreeVirtualMemory(buckets_, kNumBuckets * sizeof(CellIndex)); |
33 FreeVirtualMemory(cells_, num_cells_ * sizeof(Cell)); | 28 FreeVirtualMemory(cells_, kNumCells * sizeof(Cell)); |
34 } | 29 } |
35 | 30 |
36 void AllocationRegister::Insert(void* address, | 31 void AllocationRegister::Insert(void* address, |
37 size_t size, | 32 size_t size, |
38 AllocationContext context) { | 33 AllocationContext context) { |
39 DCHECK(address != nullptr); | 34 DCHECK(address != nullptr); |
40 | 35 |
41 CellIndex* idx_ptr = Lookup(address); | 36 CellIndex* idx_ptr = Lookup(address); |
42 | 37 |
43 // If the index is 0, the address is not yet present, so insert it. | 38 // If the index is 0, the address is not yet present, so insert it. |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 // was reserved for |cells_|), |next_unused_cell_| can be an index outside of | 137 // was reserved for |cells_|), |next_unused_cell_| can be an index outside of |
143 // the allocated storage. A guard page is allocated there to crash the | 138 // the allocated storage. A guard page is allocated there to crash the |
144 // program in that case. There are alternative solutions: | 139 // program in that case. There are alternative solutions: |
145 // - Deal with it, increase capacity by reallocating |cells_|. | 140 // - Deal with it, increase capacity by reallocating |cells_|. |
146 // - Refuse to insert and let the caller deal with it. | 141 // - Refuse to insert and let the caller deal with it. |
147 // Because free cells are re-used before accessing fresh cells with a higher | 142 // Because free cells are re-used before accessing fresh cells with a higher |
148 // index, and because reserving address space without touching it is cheap, | 143 // index, and because reserving address space without touching it is cheap, |
149 // the simplest solution is to just allocate a humongous chunk of address | 144 // the simplest solution is to just allocate a humongous chunk of address |
150 // space. | 145 // space. |
151 | 146 |
152 DCHECK_LT(next_unused_cell_, num_cells_ + 1); | 147 DCHECK_LT(next_unused_cell_, kNumCells + 1); |
153 | 148 |
154 return idx; | 149 return idx; |
155 } | 150 } |
156 | 151 |
157 // static | 152 // static |
158 uint32_t AllocationRegister::Hash(void* address) { | 153 uint32_t AllocationRegister::Hash(void* address) { |
159 // The multiplicative hashing scheme from [Knuth 1998]. The value of |a| has | 154 // The multiplicative hashing scheme from [Knuth 1998]. The value of |a| has |
160 // been chosen carefully based on measurements with real-word data (addresses | 155 // been chosen carefully based on measurements with real-word data (addresses |
161 // recorded from a Chrome trace run). It is the first prime after 2^17. For | 156 // recorded from a Chrome trace run). It is the first prime after 2^17. For |
162 // |shift|, 13, 14 and 15 yield good results. These values are tuned to 2^18 | 157 // |shift|, 13, 14 and 15 yield good results. These values are tuned to 2^18 |
(...skipping 15 matching lines...) Expand all Loading... |
178 size_t resident = sizeof(AllocationRegister) | 173 size_t resident = sizeof(AllocationRegister) |
179 // Include size of touched cells (size of |*cells_|). | 174 // Include size of touched cells (size of |*cells_|). |
180 + sizeof(Cell) * next_unused_cell_ | 175 + sizeof(Cell) * next_unused_cell_ |
181 // Size of |*buckets_|. | 176 // Size of |*buckets_|. |
182 + sizeof(CellIndex) * kNumBuckets; | 177 + sizeof(CellIndex) * kNumBuckets; |
183 overhead->Add("AllocationRegister", allocated, resident); | 178 overhead->Add("AllocationRegister", allocated, resident); |
184 } | 179 } |
185 | 180 |
186 } // namespace trace_event | 181 } // namespace trace_event |
187 } // namespace base | 182 } // namespace base |
OLD | NEW |