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.
5 #include "base/memory/discardable_memory_allocator_android.h"
16 #include "base/basictypes.h"
17 #include "base/containers/hash_tables.h"
18 #include "base/file_util.h"
19 #include "base/logging.h"
20 #include "base/memory/discardable_memory.h"
21 #include "base/memory/scoped_vector.h"
22 #include "base/synchronization/lock.h"
23 #include "base/threading/thread_checker.h"
24 #include "third_party/ashmem/ashmem.h"
26 // The allocator consists of three parts (classes):
27 // - DiscardableMemoryAllocator: entry point of all allocations (through its
28 // Allocate() method) that are dispatched to the AshmemRegion instances (which
30 // - AshmemRegion: manages allocations and destructions inside a single large
31 // (e.g. 32 MBytes) ashmem region.
32 // - DiscardableAshmemChunk: class implementing the DiscardableMemory interface
33 // whose instances are returned to the client. DiscardableAshmemChunk lets the
34 // client seamlessly operate on a subrange of the ashmem region managed by
40 // Only tolerate fragmentation in used chunks *caused by the client* (as opposed
41 // to the allocator when a free chunk is reused). The client can cause such
42 // fragmentation by e.g. requesting 4097 bytes. This size would be rounded up to
43 // 8192 by the allocator which would cause 4095 bytes of fragmentation (which is
44 // currently the maximum allowed). If the client requests 4096 bytes and a free
45 // chunk of 8192 bytes is available then the free chunk gets splitted into two
46 // pieces to minimize fragmentation (since 8192 - 4096 = 4096 which is greater
48 // TODO(pliard): tune this if splitting chunks too often leads to performance
50 const size_t kMaxChunkFragmentationBytes
= 4096 - 1;
52 const size_t kMinAshmemRegionSize
= 32 * 1024 * 1024;
54 // Returns 0 if the provided size is too high to be aligned.
55 size_t AlignToNextPage(size_t size
) {
56 const size_t kPageSize
= 4096;
57 DCHECK_EQ(static_cast<int>(kPageSize
), getpagesize());
58 if (size
> std::numeric_limits
<size_t>::max() - kPageSize
+ 1)
60 const size_t mask
= ~(kPageSize
- 1);
61 return (size
+ kPageSize
- 1) & mask
;
64 bool CreateAshmemRegion(const char* name
,
68 int fd
= ashmem_create_region(name
, size
);
70 DLOG(ERROR
) << "ashmem_create_region() failed";
73 file_util::ScopedFD
fd_closer(&fd
);
75 const int err
= ashmem_set_prot_region(fd
, PROT_READ
| PROT_WRITE
);
77 DLOG(ERROR
) << "Error " << err
<< " when setting protection of ashmem";
81 // There is a problem using MAP_PRIVATE here. As we are constantly calling
82 // Lock() and Unlock(), data could get lost if they are not written to the
83 // underlying file when Unlock() gets called.
84 void* const address
= mmap(
85 NULL
, size
, PROT_READ
| PROT_WRITE
, MAP_SHARED
, fd
, 0);
86 if (address
== MAP_FAILED
) {
87 DPLOG(ERROR
) << "Failed to map memory.";
91 ignore_result(fd_closer
.release());
93 *out_address
= address
;
97 bool CloseAshmemRegion(int fd
, size_t size
, void* address
) {
98 if (munmap(address
, size
) == -1) {
99 DPLOG(ERROR
) << "Failed to unmap memory.";
103 return close(fd
) == 0;
106 DiscardableMemoryLockStatus
LockAshmemRegion(int fd
,
109 const void* address
) {
110 const int result
= ashmem_pin_region(fd
, off
, size
);
111 DCHECK_EQ(0, mprotect(address
, size
, PROT_READ
| PROT_WRITE
));
112 return result
== ASHMEM_WAS_PURGED
? DISCARDABLE_MEMORY_LOCK_STATUS_PURGED
113 : DISCARDABLE_MEMORY_LOCK_STATUS_SUCCESS
;
116 bool UnlockAshmemRegion(int fd
, size_t off
, size_t size
, const void* address
) {
117 const int failed
= ashmem_unpin_region(fd
, off
, size
);
119 DLOG(ERROR
) << "Failed to unpin memory.";
120 // This allows us to catch accesses to unlocked memory.
121 DCHECK_EQ(0, mprotect(address
, size
, PROT_NONE
));
129 class DiscardableMemoryAllocator::DiscardableAshmemChunk
130 : public DiscardableMemory
{
132 // Note that |ashmem_region| must outlive |this|.
133 DiscardableAshmemChunk(AshmemRegion
* ashmem_region
,
138 : ashmem_region_(ashmem_region
),
146 // Implemented below AshmemRegion since this requires the full definition of
148 virtual ~DiscardableAshmemChunk();
150 // DiscardableMemory:
151 virtual DiscardableMemoryLockStatus
Lock() OVERRIDE
{
154 return LockAshmemRegion(fd_
, offset_
, size_
, address_
);
157 virtual void Unlock() OVERRIDE
{
160 UnlockAshmemRegion(fd_
, offset_
, size_
, address_
);
163 virtual void* Memory() const OVERRIDE
{
168 AshmemRegion
* const ashmem_region_
;
170 void* const address_
;
171 const size_t offset_
;
175 DISALLOW_COPY_AND_ASSIGN(DiscardableAshmemChunk
);
178 class DiscardableMemoryAllocator::AshmemRegion
{
180 // Note that |allocator| must outlive |this|.
181 static scoped_ptr
<AshmemRegion
> Create(
183 const std::string
& name
,
184 DiscardableMemoryAllocator
* allocator
) {
185 DCHECK_EQ(size
, AlignToNextPage(size
));
188 if (!CreateAshmemRegion(name
.c_str(), size
, &fd
, &base
))
189 return scoped_ptr
<AshmemRegion
>();
190 return make_scoped_ptr(new AshmemRegion(fd
, size
, base
, allocator
));
194 const bool result
= CloseAshmemRegion(fd_
, size_
, base_
);
196 DCHECK(!highest_allocated_chunk_
);
199 // Returns a new instance of DiscardableMemory whose size is greater or equal
200 // than |actual_size| (which is expected to be greater or equal than
201 // |client_requested_size|).
202 // Allocation works as follows:
203 // 1) Reuse a previously freed chunk and return it if it succeeded. See
204 // ReuseFreeChunk_Locked() below for more information.
205 // 2) If no free chunk could be reused and the region is not big enough for
206 // the requested size then NULL is returned.
207 // 3) If there is enough room in the ashmem region then a new chunk is
208 // returned. This new chunk starts at |offset_| which is the end of the
209 // previously highest chunk in the region.
210 scoped_ptr
<DiscardableMemory
> Allocate_Locked(size_t client_requested_size
,
211 size_t actual_size
) {
212 DCHECK_LE(client_requested_size
, actual_size
);
213 allocator_
->lock_
.AssertAcquired();
215 // Check that the |highest_allocated_chunk_| field doesn't contain a stale
216 // pointer. It should point to either a free chunk or a used chunk.
217 DCHECK(!highest_allocated_chunk_
||
218 address_to_free_chunk_map_
.find(highest_allocated_chunk_
) !=
219 address_to_free_chunk_map_
.end() ||
220 used_to_previous_chunk_map_
.find(highest_allocated_chunk_
) !=
221 used_to_previous_chunk_map_
.end());
223 scoped_ptr
<DiscardableMemory
> memory
= ReuseFreeChunk_Locked(
224 client_requested_size
, actual_size
);
226 return memory
.Pass();
228 if (size_
- offset_
< actual_size
) {
229 // This region does not have enough space left to hold the requested size.
230 return scoped_ptr
<DiscardableMemory
>();
233 void* const address
= static_cast<char*>(base_
) + offset_
;
235 new DiscardableAshmemChunk(this, fd_
, address
, offset_
, actual_size
));
237 used_to_previous_chunk_map_
.insert(
238 std::make_pair(address
, highest_allocated_chunk_
));
239 highest_allocated_chunk_
= address
;
240 offset_
+= actual_size
;
241 DCHECK_LE(offset_
, size_
);
242 return memory
.Pass();
245 void OnChunkDeletion(void* chunk
, size_t size
) {
246 AutoLock
auto_lock(allocator_
->lock_
);
247 MergeAndAddFreeChunk_Locked(chunk
, size
);
248 // Note that |this| might be deleted beyond this point.
253 FreeChunk() : previous_chunk(NULL
), start(NULL
), size(0) {}
255 explicit FreeChunk(size_t size
)
256 : previous_chunk(NULL
),
261 FreeChunk(void* previous_chunk
, void* start
, size_t size
)
262 : previous_chunk(previous_chunk
),
265 DCHECK_LT(previous_chunk
, start
);
268 void* const previous_chunk
;
272 bool is_null() const { return !start
; }
274 bool operator<(const FreeChunk
& other
) const {
275 return size
< other
.size
;
279 // Note that |allocator| must outlive |this|.
283 DiscardableMemoryAllocator
* allocator
)
287 allocator_(allocator
),
288 highest_allocated_chunk_(NULL
),
291 DCHECK_GE(size
, kMinAshmemRegionSize
);
296 // Tries to reuse a previously freed chunk by doing a closest size match.
297 scoped_ptr
<DiscardableMemory
> ReuseFreeChunk_Locked(
298 size_t client_requested_size
,
299 size_t actual_size
) {
300 allocator_
->lock_
.AssertAcquired();
301 const FreeChunk reused_chunk
= RemoveFreeChunkFromIterator_Locked(
302 free_chunks_
.lower_bound(FreeChunk(actual_size
)));
303 if (reused_chunk
.is_null())
304 return scoped_ptr
<DiscardableMemory
>();
306 used_to_previous_chunk_map_
.insert(
307 std::make_pair(reused_chunk
.start
, reused_chunk
.previous_chunk
));
308 size_t reused_chunk_size
= reused_chunk
.size
;
309 // |client_requested_size| is used below rather than |actual_size| to
310 // reflect the amount of bytes that would not be usable by the client (i.e.
311 // wasted). Using |actual_size| instead would not allow us to detect
312 // fragmentation caused by the client if he did misaligned allocations.
313 DCHECK_GE(reused_chunk
.size
, client_requested_size
);
314 const size_t fragmentation_bytes
=
315 reused_chunk
.size
- client_requested_size
;
317 if (fragmentation_bytes
> kMaxChunkFragmentationBytes
) {
318 // Split the free chunk being recycled so that its unused tail doesn't get
319 // reused (i.e. locked) which would prevent it from being evicted under
321 reused_chunk_size
= actual_size
;
322 void* const new_chunk_start
=
323 static_cast<char*>(reused_chunk
.start
) + actual_size
;
324 if (reused_chunk
.start
== highest_allocated_chunk_
) {
325 // We also need to update the pointer to the highest allocated chunk in
326 // case we are splitting the highest chunk.
327 highest_allocated_chunk_
= new_chunk_start
;
329 DCHECK_GT(reused_chunk
.size
, actual_size
);
330 const size_t new_chunk_size
= reused_chunk
.size
- actual_size
;
331 // Note that merging is not needed here since there can't be contiguous
332 // free chunks at this point.
334 FreeChunk(reused_chunk
.start
, new_chunk_start
, new_chunk_size
));
337 const size_t offset
=
338 static_cast<char*>(reused_chunk
.start
) - static_cast<char*>(base_
);
339 LockAshmemRegion(fd_
, offset
, reused_chunk_size
, reused_chunk
.start
);
340 scoped_ptr
<DiscardableMemory
> memory(
341 new DiscardableAshmemChunk(this, fd_
, reused_chunk
.start
, offset
,
343 return memory
.Pass();
346 // Makes the chunk identified with the provided arguments free and possibly
347 // merges this chunk with the previous and next contiguous ones.
348 // If the provided chunk is the only one used (and going to be freed) in the
349 // region then the internal ashmem region is closed so that the underlying
350 // physical pages are immediately released.
351 // Note that free chunks are unlocked therefore they can be reclaimed by the
352 // kernel if needed (under memory pressure) but they are not immediately
353 // released unfortunately since madvise(MADV_REMOVE) and
354 // fallocate(FALLOC_FL_PUNCH_HOLE) don't seem to work on ashmem. This might
355 // change in versions of kernel >=3.5 though. The fact that free chunks are
356 // not immediately released is the reason why we are trying to minimize
357 // fragmentation in order not to cause "artificial" memory pressure.
358 void MergeAndAddFreeChunk_Locked(void* chunk
, size_t size
) {
359 allocator_
->lock_
.AssertAcquired();
360 size_t new_free_chunk_size
= size
;
361 // Merge with the previous chunk.
362 void* first_free_chunk
= chunk
;
363 DCHECK(!used_to_previous_chunk_map_
.empty());
364 const hash_map
<void*, void*>::iterator previous_chunk_it
=
365 used_to_previous_chunk_map_
.find(chunk
);
366 DCHECK(previous_chunk_it
!= used_to_previous_chunk_map_
.end());
367 void* previous_chunk
= previous_chunk_it
->second
;
368 used_to_previous_chunk_map_
.erase(previous_chunk_it
);
370 if (previous_chunk
) {
371 const FreeChunk free_chunk
= RemoveFreeChunk_Locked(previous_chunk
);
372 if (!free_chunk
.is_null()) {
373 new_free_chunk_size
+= free_chunk
.size
;
374 first_free_chunk
= previous_chunk
;
375 if (chunk
== highest_allocated_chunk_
)
376 highest_allocated_chunk_
= previous_chunk
;
378 // There should not be more contiguous previous free chunks.
379 previous_chunk
= free_chunk
.previous_chunk
;
380 DCHECK(!address_to_free_chunk_map_
.count(previous_chunk
));
384 // Merge with the next chunk if free and present.
385 void* next_chunk
= static_cast<char*>(chunk
) + size
;
386 const FreeChunk next_free_chunk
= RemoveFreeChunk_Locked(next_chunk
);
387 if (!next_free_chunk
.is_null()) {
388 new_free_chunk_size
+= next_free_chunk
.size
;
389 if (next_free_chunk
.start
== highest_allocated_chunk_
)
390 highest_allocated_chunk_
= first_free_chunk
;
393 DCHECK(!address_to_free_chunk_map_
.count(static_cast<char*>(next_chunk
) +
394 next_free_chunk
.size
));
397 const bool whole_ashmem_region_is_free
=
398 used_to_previous_chunk_map_
.empty();
399 if (!whole_ashmem_region_is_free
) {
401 FreeChunk(previous_chunk
, first_free_chunk
, new_free_chunk_size
));
405 // The whole ashmem region is free thus it can be deleted.
406 DCHECK_EQ(base_
, first_free_chunk
);
407 DCHECK_EQ(base_
, highest_allocated_chunk_
);
408 DCHECK(free_chunks_
.empty());
409 DCHECK(address_to_free_chunk_map_
.empty());
410 DCHECK(used_to_previous_chunk_map_
.empty());
411 highest_allocated_chunk_
= NULL
;
412 allocator_
->DeleteAshmemRegion_Locked(this); // Deletes |this|.
415 void AddFreeChunk_Locked(const FreeChunk
& free_chunk
) {
416 allocator_
->lock_
.AssertAcquired();
417 const std::multiset
<FreeChunk
>::iterator it
= free_chunks_
.insert(
419 address_to_free_chunk_map_
.insert(std::make_pair(free_chunk
.start
, it
));
420 // Update the next used contiguous chunk, if any, since its previous chunk
421 // may have changed due to free chunks merging/splitting.
422 void* const next_used_contiguous_chunk
=
423 static_cast<char*>(free_chunk
.start
) + free_chunk
.size
;
424 hash_map
<void*, void*>::iterator previous_it
=
425 used_to_previous_chunk_map_
.find(next_used_contiguous_chunk
);
426 if (previous_it
!= used_to_previous_chunk_map_
.end())
427 previous_it
->second
= free_chunk
.start
;
430 // Finds and removes the free chunk, if any, whose start address is
431 // |chunk_start|. Returns a copy of the unlinked free chunk or a free chunk
432 // whose content is null if it was not found.
433 FreeChunk
RemoveFreeChunk_Locked(void* chunk_start
) {
434 allocator_
->lock_
.AssertAcquired();
436 void*, std::multiset
<FreeChunk
>::iterator
>::iterator it
=
437 address_to_free_chunk_map_
.find(chunk_start
);
438 if (it
== address_to_free_chunk_map_
.end())
440 return RemoveFreeChunkFromIterator_Locked(it
->second
);
443 // Same as above but takes an iterator in.
444 FreeChunk
RemoveFreeChunkFromIterator_Locked(
445 std::multiset
<FreeChunk
>::iterator free_chunk_it
) {
446 allocator_
->lock_
.AssertAcquired();
447 if (free_chunk_it
== free_chunks_
.end())
449 DCHECK(free_chunk_it
!= free_chunks_
.end());
450 const FreeChunk
free_chunk(*free_chunk_it
);
451 address_to_free_chunk_map_
.erase(free_chunk_it
->start
);
452 free_chunks_
.erase(free_chunk_it
);
459 DiscardableMemoryAllocator
* const allocator_
;
460 // Points to the chunk with the highest address in the region. This pointer
461 // needs to be carefully updated when chunks are merged/split.
462 void* highest_allocated_chunk_
;
463 // Points to the end of |highest_allocated_chunk_|.
465 // Allows free chunks recycling (lookup, insertion and removal) in O(log N).
466 // Note that FreeChunk values are indexed by their size and also note that
467 // multiple free chunks can have the same size (which is why multiset<> is
468 // used instead of e.g. set<>).
469 std::multiset
<FreeChunk
> free_chunks_
;
470 // Used while merging free contiguous chunks to erase free chunks (from their
471 // start address) in constant time. Note that multiset<>::{insert,erase}()
472 // don't invalidate iterators (except the one for the element being removed
475 void*, std::multiset
<FreeChunk
>::iterator
> address_to_free_chunk_map_
;
476 // Maps the address of *used* chunks to the address of their previous
478 hash_map
<void*, void*> used_to_previous_chunk_map_
;
480 DISALLOW_COPY_AND_ASSIGN(AshmemRegion
);
483 DiscardableMemoryAllocator::DiscardableAshmemChunk::~DiscardableAshmemChunk() {
485 UnlockAshmemRegion(fd_
, offset_
, size_
, address_
);
486 ashmem_region_
->OnChunkDeletion(address_
, size_
);
489 DiscardableMemoryAllocator::DiscardableMemoryAllocator(
490 const std::string
& name
,
491 size_t ashmem_region_size
)
494 std::max(kMinAshmemRegionSize
, AlignToNextPage(ashmem_region_size
))),
495 last_ashmem_region_size_(0) {
496 DCHECK_GE(ashmem_region_size_
, kMinAshmemRegionSize
);
499 DiscardableMemoryAllocator::~DiscardableMemoryAllocator() {
500 DCHECK(thread_checker_
.CalledOnValidThread());
501 DCHECK(ashmem_regions_
.empty());
504 scoped_ptr
<DiscardableMemory
> DiscardableMemoryAllocator::Allocate(
506 const size_t aligned_size
= AlignToNextPage(size
);
508 return scoped_ptr
<DiscardableMemory
>();
509 // TODO(pliard): make this function less naive by e.g. moving the free chunks
510 // multiset to the allocator itself in order to decrease even more
511 // fragmentation/speedup allocation. Note that there should not be more than a
512 // couple (=5) of AshmemRegion instances in practice though.
513 AutoLock
auto_lock(lock_
);
514 DCHECK_LE(ashmem_regions_
.size(), 5U);
515 for (ScopedVector
<AshmemRegion
>::iterator it
= ashmem_regions_
.begin();
516 it
!= ashmem_regions_
.end(); ++it
) {
517 scoped_ptr
<DiscardableMemory
> memory(
518 (*it
)->Allocate_Locked(size
, aligned_size
));
520 return memory
.Pass();
522 // The creation of the (large) ashmem region might fail if the address space
523 // is too fragmented. In case creation fails the allocator retries by
524 // repetitively dividing the size by 2.
525 const size_t min_region_size
= std::max(kMinAshmemRegionSize
, aligned_size
);
526 for (size_t region_size
= std::max(ashmem_region_size_
, aligned_size
);
527 region_size
>= min_region_size
;
528 region_size
= AlignToNextPage(region_size
/ 2)) {
529 scoped_ptr
<AshmemRegion
> new_region(
530 AshmemRegion::Create(region_size
, name_
.c_str(), this));
533 last_ashmem_region_size_
= region_size
;
534 ashmem_regions_
.push_back(new_region
.release());
535 return ashmem_regions_
.back()->Allocate_Locked(size
, aligned_size
);
537 // TODO(pliard): consider adding an histogram to see how often this happens.
538 return scoped_ptr
<DiscardableMemory
>();
541 size_t DiscardableMemoryAllocator::last_ashmem_region_size() const {
542 AutoLock
auto_lock(lock_
);
543 return last_ashmem_region_size_
;
546 void DiscardableMemoryAllocator::DeleteAshmemRegion_Locked(
547 AshmemRegion
* region
) {
548 lock_
.AssertAcquired();
549 // Note that there should not be more than a couple of ashmem region instances
550 // in |ashmem_regions_|.
551 DCHECK_LE(ashmem_regions_
.size(), 5U);
552 const ScopedVector
<AshmemRegion
>::iterator it
= std::find(
553 ashmem_regions_
.begin(), ashmem_regions_
.end(), region
);
554 DCHECK_NE(ashmem_regions_
.end(), it
);
555 std::swap(*it
, ashmem_regions_
.back());
556 ashmem_regions_
.pop_back();
559 } // namespace internal