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