OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 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/memory/discardable_memory_allocator_android.h" | |
6 | |
7 #include <algorithm> | |
8 #include <cmath> | |
9 #include <set> | |
10 #include <utility> | |
11 | |
12 #include "base/basictypes.h" | |
13 #include "base/bind.h" | |
14 #include "base/callback.h" | |
15 #include "base/compiler_specific.h" | |
16 #include "base/containers/hash_tables.h" | |
17 #include "base/logging.h" | |
18 #include "base/memory/discardable_memory.h" | |
19 #include "base/memory/discardable_memory_android.h" | |
20 #include "base/memory/scoped_vector.h" | |
21 #include "base/strings/stringprintf.h" | |
22 #include "base/synchronization/lock.h" | |
23 #include "base/threading/thread_checker.h" | |
24 | |
25 // The allocator consists of three parts (classes): | |
26 // - DiscardableMemoryAllocator: entry point of all allocations (through its | |
27 // Allocate() method) that are dispatched to the AshmemRegion instances (which | |
28 // it owns). | |
29 // - AshmemRegion: manages allocations and destructions inside a single large | |
30 // (e.g. 32 MBytes) ashmem region. | |
31 // - DiscardableAshmemChunk: class implementing the DiscardableMemory interface | |
32 // whose instances are returned to the client. DiscardableAshmemChunk lets the | |
33 // client seamlessly operate on a subrange of the ashmem region managed by | |
34 // AshmemRegion. | |
35 | |
36 namespace base { | |
37 namespace { | |
38 | |
39 // Allow 8 KBytes of fragmentation inside used chunks. | |
40 const size_t kMaxChunkFragmentationBytes = 8192; | |
41 | |
42 class DiscardableAshmemChunk : public DiscardableMemory { | |
43 public: | |
44 // Note that this is not replaced with base::Callback to avoid the extra heap | |
45 // allocation and atomicops. | |
46 class DeletionObserver { | |
47 public: | |
48 virtual void OnChunkDeletion(void* previous_chunk, | |
49 void* chunk, | |
50 size_t size) = 0; | |
51 | |
52 protected: | |
53 virtual ~DeletionObserver() {} | |
54 }; | |
55 | |
56 // Note that |deletion_observer| must outlive |this|. | |
57 DiscardableAshmemChunk(DeletionObserver* deletion_observer, | |
58 int fd, | |
59 void* previous_chunk, | |
60 void* address, | |
61 size_t offset, | |
62 size_t size) | |
63 : deletion_observer_(deletion_observer), | |
64 fd_(fd), | |
65 previous_chunk_(previous_chunk), | |
66 address_(address), | |
67 offset_(offset), | |
68 size_(size), | |
69 locked_(true) { | |
70 } | |
71 | |
72 virtual ~DiscardableAshmemChunk() { | |
73 if (locked_) | |
74 internal::UnlockAshmemRegion(fd_, offset_, size_, address_); | |
75 deletion_observer_->OnChunkDeletion(previous_chunk_, address_, size_); | |
76 } | |
77 | |
78 // DiscardableMemory: | |
79 virtual LockDiscardableMemoryStatus Lock() OVERRIDE { | |
80 DCHECK(!locked_); | |
81 locked_ = true; | |
82 return internal::LockAshmemRegion(fd_, offset_, size_, address_); | |
83 } | |
84 | |
85 virtual void Unlock() OVERRIDE { | |
86 DCHECK(locked_); | |
87 locked_ = false; | |
88 internal::UnlockAshmemRegion(fd_, offset_, size_, address_); | |
89 } | |
90 | |
91 virtual void* Memory() const OVERRIDE { | |
92 return address_; | |
93 } | |
94 | |
95 private: | |
96 DeletionObserver* const deletion_observer_; | |
97 const int fd_; | |
98 void* const previous_chunk_; | |
99 void* const address_; | |
100 const size_t offset_; | |
101 const size_t size_; | |
102 bool locked_; | |
103 | |
104 DISALLOW_COPY_AND_ASSIGN(DiscardableAshmemChunk); | |
105 }; | |
106 | |
107 } // namespace | |
108 | |
109 namespace internal { | |
110 | |
111 class DiscardableMemoryAllocator::AshmemRegion | |
112 : public DiscardableAshmemChunk::DeletionObserver { | |
113 public: | |
114 typedef Callback<void (scoped_ptr<AshmemRegion>)> DeletionCallback; | |
115 | |
116 static scoped_ptr<AshmemRegion> Create( | |
117 size_t size, | |
118 const std::string& name, | |
119 Lock* lock, | |
willchan no longer on Chromium
2013/11/19 02:19:43
DiscardableMemoryAllocator::AshmemRegion is a nest
Philippe
2013/11/19 15:43:00
Agreed, although I'm still doing the heap allocati
| |
120 const DeletionCallback& deletion_callback) { | |
121 int fd; | |
122 void* base; | |
123 if (!internal::CreateAshmemRegion(name.c_str(), size, &fd, &base)) | |
124 return scoped_ptr<AshmemRegion>(); | |
125 return make_scoped_ptr( | |
126 new AshmemRegion(fd, size, base, lock, deletion_callback)); | |
127 } | |
128 | |
129 virtual ~AshmemRegion() { | |
130 const bool result = internal::CloseAshmemRegion(fd_, size_, base_); | |
131 DCHECK(result); | |
132 } | |
133 | |
134 scoped_ptr<DiscardableMemory> Allocate(size_t client_requested_size, | |
willchan no longer on Chromium
2013/11/19 02:19:43
Can you document this function?
Philippe
2013/11/19 15:43:00
Yes, sorry.
| |
135 size_t actual_size) { | |
136 lock_->AssertAcquired(); | |
137 scoped_ptr<DiscardableMemory> memory = RecycleFreeChunk( | |
138 client_requested_size, actual_size); | |
139 if (memory) | |
140 return memory.Pass(); | |
141 if (size_ - offset_ < actual_size) { | |
142 // This region does not have enough space left to hold the requested size. | |
143 return scoped_ptr<DiscardableMemory>(); | |
144 } | |
145 void* const address = static_cast<char*>(base_) + offset_; | |
146 memory.reset( | |
147 new DiscardableAshmemChunk(this, fd_, last_allocated_chunk_, address, | |
willchan no longer on Chromium
2013/11/19 02:19:43
Is this the highest allocated chunk? What do you m
Philippe
2013/11/19 15:43:00
Good point. Renamed to |highest_allocated_chunk_|.
| |
148 offset_, actual_size)); | |
149 last_allocated_chunk_ = address; | |
150 offset_ += actual_size; | |
151 return memory.Pass(); | |
152 } | |
153 | |
154 private: | |
155 struct FreeChunk { | |
156 FreeChunk(void* previous_chunk, void* start, size_t size) | |
157 : previous_chunk(previous_chunk), | |
158 start(start), | |
159 size(size) { | |
160 } | |
161 | |
162 void* const previous_chunk; | |
163 void* const start; | |
164 const size_t size; | |
165 | |
166 bool is_null() const { return !start; } | |
167 | |
168 bool operator<(const FreeChunk& other) const { | |
169 return size < other.size; | |
170 } | |
171 }; | |
172 | |
173 AshmemRegion(int fd, | |
174 size_t size, | |
175 void* base, | |
176 Lock* lock, | |
177 const DeletionCallback& deletion_callback) | |
178 : fd_(fd), | |
179 size_(size), | |
180 base_(base), | |
181 offset_(0), | |
182 lock_(lock), | |
183 deletion_callback_(deletion_callback), | |
184 last_allocated_chunk_(NULL) { | |
185 } | |
186 | |
187 // DiscardableAshmemChunk::DeletionObserver: | |
188 virtual void OnChunkDeletion(void* previous_chunk, | |
189 void* chunk, | |
190 size_t size) OVERRIDE { | |
191 base::AutoLock auto_lock(*lock_); | |
192 MergeAndAddFreeChunk(previous_chunk, chunk, size); | |
193 } | |
194 | |
195 // Tries to reuse a previously freed chunk by doing a closest size match. | |
196 scoped_ptr<DiscardableMemory> RecycleFreeChunk(size_t client_requested_size, | |
197 size_t actual_size) { | |
198 lock_->AssertAcquired(); | |
199 const std::multiset<FreeChunk>::iterator chunk_it = | |
200 free_chunks_.lower_bound(FreeChunk(NULL, NULL, actual_size)); | |
201 if (chunk_it == free_chunks_.end()) | |
202 return scoped_ptr<DiscardableMemory>(); | |
203 size_t recycled_chunk_size = chunk_it->size; | |
204 const size_t fragmentation_bytes = chunk_it->size - client_requested_size; | |
205 if (fragmentation_bytes >= kMaxChunkFragmentationBytes) { | |
206 // Split the free chunk being recycled if it's too large so that its | |
207 // unused tail doesn't get recycled (i.e. locked) which would prevent it | |
208 // from being evicted under memory pressure. | |
209 void* const previous_chunk = chunk_it->start; | |
210 void* const chunk_start = | |
211 static_cast<char*>(chunk_it->start) + actual_size; | |
212 const size_t chunk_size = chunk_it->size - actual_size; | |
213 // Note that merging is not needed here since there can't be contiguous | |
214 // free chunks at this point. | |
215 AddFreeChunk(FreeChunk(previous_chunk, chunk_start, chunk_size)); | |
216 recycled_chunk_size = actual_size; | |
217 } | |
218 const size_t offset = | |
219 static_cast<char*>(chunk_it->start) - static_cast<char*>(base_); | |
220 internal::LockAshmemRegion( | |
221 fd_, offset, recycled_chunk_size, chunk_it->start); | |
222 scoped_ptr<DiscardableMemory> memory( | |
223 new DiscardableAshmemChunk( | |
224 this, fd_, chunk_it->previous_chunk, chunk_it->start, offset, | |
225 recycled_chunk_size)); | |
226 free_chunk_for_address_.erase(reinterpret_cast<uintptr_t>(chunk_it->start)); | |
227 free_chunks_.erase(chunk_it); | |
228 return memory.Pass(); | |
229 } | |
230 | |
231 // Makes the chunk identified with the provided arguments free and possibly | |
232 // merges this chunk with the previous and next contiguous ones according to | |
233 // the value of |chunk_merging_flags|. | |
willchan no longer on Chromium
2013/11/19 02:19:43
chunk_merging_flags?
Philippe
2013/11/19 15:43:00
This was stale. I removed it.
| |
234 // If the provided chunk is the only one used (and going to be freed) in the | |
235 // region then the internal ashmem region is closed so that the underlying | |
236 // physical pages are immediately released. | |
237 // Note that free chunks are unlocked therefore they can be reclaimed by the | |
238 // kernel if needed (under memory pressure) but they are not immediately | |
239 // released unfortunately since madvise(MADV_REMOVE) and | |
240 // fallocate(FALLOC_FL_PUNCH_HOLE) don't seem to work on ashmem. This might | |
241 // change in versions of kernel >=3.5 though. The fact that free chunks are | |
242 // not immediately released is the reason why we are trying to minimize | |
243 // fragmentation. | |
willchan no longer on Chromium
2013/11/19 02:19:43
This comment section wasn't completely clear. Just
Philippe
2013/11/19 15:43:00
Yeah, this is correct. We are trying to minimize f
| |
244 void MergeAndAddFreeChunk(void* previous_chunk, void* chunk, size_t size) { | |
willchan no longer on Chromium
2013/11/19 02:19:43
I defer to you, but I kinda like adding a suffix t
Philippe
2013/11/19 15:43:00
Yeah, good idea. I used the assert at the beginnin
| |
245 lock_->AssertAcquired(); | |
246 size_t new_free_chunk_size = size; | |
247 // Merge with the previous chunks. | |
248 void* first_free_chunk = chunk; | |
249 while (previous_chunk) { | |
250 const FreeChunk free_chunk = RemoveFreeChunk(previous_chunk); | |
251 if (free_chunk.is_null()) | |
252 break; | |
253 new_free_chunk_size += free_chunk.size; | |
254 first_free_chunk = previous_chunk; | |
255 previous_chunk = free_chunk.previous_chunk; | |
256 } | |
257 // Merge with the next chunks. | |
258 const void* next_chunk = static_cast<const char*>(chunk) + size; | |
259 while (true) { | |
260 const FreeChunk free_chunk = RemoveFreeChunk(next_chunk); | |
261 if (free_chunk.is_null()) | |
262 break; | |
263 new_free_chunk_size += free_chunk.size; | |
264 next_chunk = static_cast<const char*>(next_chunk) + free_chunk.size; | |
265 } | |
266 const bool whole_ashmem_region_is_free = new_free_chunk_size == size_; | |
267 if (!whole_ashmem_region_is_free) { | |
268 AddFreeChunk( | |
269 FreeChunk(previous_chunk, first_free_chunk, new_free_chunk_size)); | |
270 return; | |
271 } | |
272 // The whole ashmem region is free thus it can be deleted. | |
273 DCHECK_EQ(size_, new_free_chunk_size); | |
274 DCHECK(free_chunks_.empty() && free_chunk_for_address_.empty()); | |
275 deletion_callback_.Run(make_scoped_ptr(this)); // Deletes |this|. | |
willchan no longer on Chromium
2013/11/19 02:19:43
I dislike this, because in my mind, this is saying
Philippe
2013/11/19 15:43:00
Yes. It was a form of indirect self-deletion IMO w
| |
276 } | |
277 | |
278 void AddFreeChunk(const FreeChunk& free_chunk) { | |
willchan no longer on Chromium
2013/11/19 02:19:43
This should be locked already, right? AssertAcquir
Philippe
2013/11/19 15:43:00
Yes, the assert was missing.
| |
279 const std::multiset<FreeChunk>::iterator it = free_chunks_.insert( | |
280 free_chunk); | |
281 free_chunk_for_address_.insert( | |
282 std::make_pair(reinterpret_cast<uintptr_t>(free_chunk.start), it)); | |
283 } | |
284 | |
285 // Finds and removes the free chunk, if any, whose start address is | |
286 // |chunk_start|. Returns a copy of the unlinked free chunk or a free chunk | |
287 // whose content is null if it was not found. | |
288 FreeChunk RemoveFreeChunk(const void* chunk_start) { | |
289 lock_->AssertAcquired(); | |
290 const base::hash_map< | |
291 uintptr_t, std::multiset<FreeChunk>::iterator>::iterator it = | |
292 free_chunk_for_address_.find( | |
293 reinterpret_cast<uintptr_t>(chunk_start)); | |
294 if (it == free_chunk_for_address_.end()) | |
295 return FreeChunk(NULL, NULL, 0U); | |
296 const std::multiset<FreeChunk>::iterator free_chunk_it = it->second; | |
297 const FreeChunk free_chunk(*free_chunk_it); | |
298 DCHECK_EQ(chunk_start, free_chunk.start); | |
299 free_chunk_for_address_.erase(it); | |
300 free_chunks_.erase(free_chunk_it); | |
301 return free_chunk; | |
302 } | |
303 | |
304 const int fd_; | |
305 const size_t size_; | |
306 void* const base_; | |
307 size_t offset_; | |
308 base::Lock* const lock_; | |
309 const DeletionCallback deletion_callback_; | |
310 void* last_allocated_chunk_; | |
311 std::multiset<FreeChunk> free_chunks_; | |
willchan no longer on Chromium
2013/11/19 02:19:43
FYI, I'm spending a lot of time evaluating your da
Philippe
2013/11/19 15:43:00
Yeah, sorry. I added a comment here and below.
| |
312 base::hash_map< | |
313 uintptr_t, std::multiset<FreeChunk>::iterator> free_chunk_for_address_; | |
314 | |
315 DISALLOW_COPY_AND_ASSIGN(AshmemRegion); | |
316 }; | |
317 | |
318 DiscardableMemoryAllocator::DiscardableMemoryAllocator(const std::string& name) | |
319 : name_(name) { | |
320 } | |
321 | |
322 DiscardableMemoryAllocator::~DiscardableMemoryAllocator() { | |
323 DCHECK(thread_checker_.CalledOnValidThread()); | |
324 } | |
325 | |
326 scoped_ptr<DiscardableMemory> DiscardableMemoryAllocator::Allocate( | |
willchan no longer on Chromium
2013/11/19 02:19:43
This allocation scheme sure is naive :) That's fin
Philippe
2013/11/19 15:43:00
Yeah :) I added a TODO. I favored simplicity here
| |
327 size_t size) { | |
328 const size_t aligned_size = internal::AlignToNextPage(size); | |
329 base::AutoLock auto_lock(lock_); | |
330 for (ScopedVector<AshmemRegion>::iterator it = ashmem_regions_.begin(); | |
331 it != ashmem_regions_.end(); ++it) { | |
332 scoped_ptr<DiscardableMemory> memory((*it)->Allocate(size, aligned_size)); | |
333 if (memory) | |
334 return memory.Pass(); | |
335 } | |
336 scoped_ptr<AshmemRegion> new_region( | |
337 AshmemRegion::Create( | |
338 std::max(static_cast<size_t>(kMinAshmemRegionSize), aligned_size), | |
339 name_.c_str(), | |
340 &lock_, | |
341 base::Bind(&DiscardableMemoryAllocator::DeleteAshmemRegion, | |
342 base::Unretained(this)))); | |
343 if (!new_region) { | |
344 // TODO(pliard): consider adding an histogram to see how often this happens. | |
345 return scoped_ptr<DiscardableMemory>(); | |
346 } | |
347 ashmem_regions_.push_back(new_region.release()); | |
348 return ashmem_regions_.back()->Allocate(size, aligned_size); | |
349 } | |
350 | |
351 void DiscardableMemoryAllocator::DeleteAshmemRegion( | |
352 scoped_ptr<AshmemRegion> region) { | |
353 lock_.AssertAcquired(); | |
354 // Note that there should not be more than a couple of ashmem region instances | |
355 // in |ashmem_regions_|. | |
356 const ScopedVector<AshmemRegion>::iterator it = std::find( | |
357 ashmem_regions_.begin(), ashmem_regions_.end(), region.get()); | |
358 DCHECK_NE(ashmem_regions_.end(), it); | |
359 std::swap(*it, ashmem_regions_.back()); | |
360 ashmem_regions_.resize(ashmem_regions_.size() - 1); | |
361 // |region| was deleted by the resize() above. | |
362 ignore_result(region.release()); | |
363 } | |
364 | |
365 } // namespace internal | |
366 } // namespace base | |
OLD | NEW |