Remove connection_history.js from remoting's JS tests.
[chromium-blink-merge.git] / base / memory / discardable_memory_allocator_android.cc
blobaea9451185030a91092a66991201481f3d402ee2
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"
7 #include <algorithm>
8 #include <cmath>
9 #include <set>
10 #include <utility>
12 #include "base/basictypes.h"
13 #include "base/containers/hash_tables.h"
14 #include "base/logging.h"
15 #include "base/memory/discardable_memory.h"
16 #include "base/memory/discardable_memory_android.h"
17 #include "base/memory/scoped_vector.h"
18 #include "base/synchronization/lock.h"
19 #include "base/threading/thread_checker.h"
21 // The allocator consists of three parts (classes):
22 // - DiscardableMemoryAllocator: entry point of all allocations (through its
23 // Allocate() method) that are dispatched to the AshmemRegion instances (which
24 // it owns).
25 // - AshmemRegion: manages allocations and destructions inside a single large
26 // (e.g. 32 MBytes) ashmem region.
27 // - DiscardableAshmemChunk: class implementing the DiscardableMemory interface
28 // whose instances are returned to the client. DiscardableAshmemChunk lets the
29 // client seamlessly operate on a subrange of the ashmem region managed by
30 // AshmemRegion.
32 namespace base {
33 namespace {
35 // Only tolerate fragmentation in used chunks *caused by the client* (as opposed
36 // to the allocator when a free chunk is reused). The client can cause such
37 // fragmentation by e.g. requesting 4097 bytes. This size would be rounded up to
38 // 8192 by the allocator which would cause 4095 bytes of fragmentation (which is
39 // currently the maximum allowed). If the client requests 4096 bytes and a free
40 // chunk of 8192 bytes is available then the free chunk gets splitted into two
41 // pieces to minimize fragmentation (since 8192 - 4096 = 4096 which is greater
42 // than 4095).
43 // TODO(pliard): tune this if splitting chunks too often leads to performance
44 // issues.
45 const size_t kMaxChunkFragmentationBytes = 4096 - 1;
47 const size_t kMinAshmemRegionSize = 32 * 1024 * 1024;
49 // Returns 0 if the provided size is too high to be aligned.
50 size_t AlignToNextPage(size_t size) {
51 const size_t kPageSize = 4096;
52 DCHECK_EQ(static_cast<int>(kPageSize), getpagesize());
53 if (size > std::numeric_limits<size_t>::max() - kPageSize + 1)
54 return 0;
55 const size_t mask = ~(kPageSize - 1);
56 return (size + kPageSize - 1) & mask;
59 } // namespace
61 namespace internal {
63 class DiscardableMemoryAllocator::DiscardableAshmemChunk
64 : public DiscardableMemory {
65 public:
66 // Note that |ashmem_region| must outlive |this|.
67 DiscardableAshmemChunk(AshmemRegion* ashmem_region,
68 int fd,
69 void* address,
70 size_t offset,
71 size_t size)
72 : ashmem_region_(ashmem_region),
73 fd_(fd),
74 address_(address),
75 offset_(offset),
76 size_(size),
77 locked_(true) {
80 // Implemented below AshmemRegion since this requires the full definition of
81 // AshmemRegion.
82 virtual ~DiscardableAshmemChunk();
84 // DiscardableMemory:
85 virtual DiscardableMemoryLockStatus Lock() OVERRIDE {
86 DCHECK(!locked_);
87 locked_ = true;
88 return internal::LockAshmemRegion(fd_, offset_, size_, address_);
91 virtual void Unlock() OVERRIDE {
92 DCHECK(locked_);
93 locked_ = false;
94 internal::UnlockAshmemRegion(fd_, offset_, size_, address_);
97 virtual void* Memory() const OVERRIDE {
98 return address_;
101 private:
102 AshmemRegion* const ashmem_region_;
103 const int fd_;
104 void* const address_;
105 const size_t offset_;
106 const size_t size_;
107 bool locked_;
109 DISALLOW_COPY_AND_ASSIGN(DiscardableAshmemChunk);
112 class DiscardableMemoryAllocator::AshmemRegion {
113 public:
114 // Note that |allocator| must outlive |this|.
115 static scoped_ptr<AshmemRegion> Create(
116 size_t size,
117 const std::string& name,
118 DiscardableMemoryAllocator* allocator) {
119 DCHECK_EQ(size, AlignToNextPage(size));
120 int fd;
121 void* base;
122 if (!internal::CreateAshmemRegion(name.c_str(), size, &fd, &base))
123 return scoped_ptr<AshmemRegion>();
124 return make_scoped_ptr(new AshmemRegion(fd, size, base, allocator));
127 ~AshmemRegion() {
128 const bool result = internal::CloseAshmemRegion(fd_, size_, base_);
129 DCHECK(result);
132 // Returns a new instance of DiscardableMemory whose size is greater or equal
133 // than |actual_size| (which is expected to be greater or equal than
134 // |client_requested_size|).
135 // Allocation works as follows:
136 // 1) Reuse a previously freed chunk and return it if it succeeded. See
137 // ReuseFreeChunk_Locked() below for more information.
138 // 2) If no free chunk could be reused and the region is not big enough for
139 // the requested size then NULL is returned.
140 // 3) If there is enough room in the ashmem region then a new chunk is
141 // returned. This new chunk starts at |offset_| which is the end of the
142 // previously highest chunk in the region.
143 scoped_ptr<DiscardableMemory> Allocate_Locked(size_t client_requested_size,
144 size_t actual_size) {
145 DCHECK_LE(client_requested_size, actual_size);
146 allocator_->lock_.AssertAcquired();
147 scoped_ptr<DiscardableMemory> memory = ReuseFreeChunk_Locked(
148 client_requested_size, actual_size);
149 if (memory)
150 return memory.Pass();
151 if (size_ - offset_ < actual_size) {
152 // This region does not have enough space left to hold the requested size.
153 return scoped_ptr<DiscardableMemory>();
155 void* const address = static_cast<char*>(base_) + offset_;
156 memory.reset(
157 new DiscardableAshmemChunk(this, fd_, address, offset_, actual_size));
158 used_to_previous_chunk_map_.insert(
159 std::make_pair(address, highest_allocated_chunk_));
160 highest_allocated_chunk_ = address;
161 offset_ += actual_size;
162 DCHECK_LE(offset_, size_);
163 return memory.Pass();
166 void OnChunkDeletion(void* chunk, size_t size) {
167 AutoLock auto_lock(allocator_->lock_);
168 MergeAndAddFreeChunk_Locked(chunk, size);
169 // Note that |this| might be deleted beyond this point.
172 private:
173 struct FreeChunk {
174 FreeChunk(void* previous_chunk, void* start, size_t size)
175 : previous_chunk(previous_chunk),
176 start(start),
177 size(size) {
180 void* const previous_chunk;
181 void* const start;
182 const size_t size;
184 bool is_null() const { return !start; }
186 bool operator<(const FreeChunk& other) const {
187 return size < other.size;
191 // Note that |allocator| must outlive |this|.
192 AshmemRegion(int fd,
193 size_t size,
194 void* base,
195 DiscardableMemoryAllocator* allocator)
196 : fd_(fd),
197 size_(size),
198 base_(base),
199 allocator_(allocator),
200 highest_allocated_chunk_(NULL),
201 offset_(0) {
202 DCHECK_GE(fd_, 0);
203 DCHECK_GE(size, kMinAshmemRegionSize);
204 DCHECK(base);
205 DCHECK(allocator);
208 // Tries to reuse a previously freed chunk by doing a closest size match.
209 scoped_ptr<DiscardableMemory> ReuseFreeChunk_Locked(
210 size_t client_requested_size,
211 size_t actual_size) {
212 allocator_->lock_.AssertAcquired();
213 const FreeChunk reused_chunk = RemoveFreeChunkFromIterator_Locked(
214 free_chunks_.lower_bound(FreeChunk(NULL, NULL, actual_size)));
215 if (reused_chunk.is_null())
216 return scoped_ptr<DiscardableMemory>();
218 used_to_previous_chunk_map_.insert(
219 std::make_pair(reused_chunk.start, reused_chunk.previous_chunk));
220 size_t reused_chunk_size = reused_chunk.size;
221 // |client_requested_size| is used below rather than |actual_size| to
222 // reflect the amount of bytes that would not be usable by the client (i.e.
223 // wasted). Using |actual_size| instead would not allow us to detect
224 // fragmentation caused by the client if he did misaligned allocations.
225 DCHECK_GE(reused_chunk.size, client_requested_size);
226 const size_t fragmentation_bytes =
227 reused_chunk.size - client_requested_size;
228 if (fragmentation_bytes > kMaxChunkFragmentationBytes) {
229 // Split the free chunk being recycled so that its unused tail doesn't get
230 // reused (i.e. locked) which would prevent it from being evicted under
231 // memory pressure.
232 reused_chunk_size = actual_size;
233 void* const new_chunk_start =
234 static_cast<char*>(reused_chunk.start) + actual_size;
235 DCHECK_GT(reused_chunk.size, actual_size);
236 const size_t new_chunk_size = reused_chunk.size - actual_size;
237 // Note that merging is not needed here since there can't be contiguous
238 // free chunks at this point.
239 AddFreeChunk_Locked(
240 FreeChunk(reused_chunk.start, new_chunk_start, new_chunk_size));
242 const size_t offset =
243 static_cast<char*>(reused_chunk.start) - static_cast<char*>(base_);
244 internal::LockAshmemRegion(
245 fd_, offset, reused_chunk_size, reused_chunk.start);
246 scoped_ptr<DiscardableMemory> memory(
247 new DiscardableAshmemChunk(this, fd_, reused_chunk.start, offset,
248 reused_chunk_size));
249 return memory.Pass();
252 // Makes the chunk identified with the provided arguments free and possibly
253 // merges this chunk with the previous and next contiguous ones.
254 // If the provided chunk is the only one used (and going to be freed) in the
255 // region then the internal ashmem region is closed so that the underlying
256 // physical pages are immediately released.
257 // Note that free chunks are unlocked therefore they can be reclaimed by the
258 // kernel if needed (under memory pressure) but they are not immediately
259 // released unfortunately since madvise(MADV_REMOVE) and
260 // fallocate(FALLOC_FL_PUNCH_HOLE) don't seem to work on ashmem. This might
261 // change in versions of kernel >=3.5 though. The fact that free chunks are
262 // not immediately released is the reason why we are trying to minimize
263 // fragmentation in order not to cause "artificial" memory pressure.
264 void MergeAndAddFreeChunk_Locked(void* chunk, size_t size) {
265 allocator_->lock_.AssertAcquired();
266 size_t new_free_chunk_size = size;
267 // Merge with the previous chunk.
268 void* first_free_chunk = chunk;
269 DCHECK(!used_to_previous_chunk_map_.empty());
270 const hash_map<void*, void*>::iterator previous_chunk_it =
271 used_to_previous_chunk_map_.find(chunk);
272 DCHECK(previous_chunk_it != used_to_previous_chunk_map_.end());
273 void* previous_chunk = previous_chunk_it->second;
274 used_to_previous_chunk_map_.erase(previous_chunk_it);
275 if (previous_chunk) {
276 const FreeChunk free_chunk = RemoveFreeChunk_Locked(previous_chunk);
277 if (!free_chunk.is_null()) {
278 new_free_chunk_size += free_chunk.size;
279 first_free_chunk = previous_chunk;
280 // There should not be more contiguous previous free chunks.
281 DCHECK(!address_to_free_chunk_map_.count(free_chunk.previous_chunk));
284 // Merge with the next chunk if free and present.
285 void* next_chunk = static_cast<char*>(chunk) + size;
286 const FreeChunk next_free_chunk = RemoveFreeChunk_Locked(next_chunk);
287 if (!next_free_chunk.is_null()) {
288 new_free_chunk_size += next_free_chunk.size;
289 // Same as above.
290 DCHECK(!address_to_free_chunk_map_.count(static_cast<char*>(next_chunk) +
291 next_free_chunk.size));
293 const bool whole_ashmem_region_is_free =
294 used_to_previous_chunk_map_.empty();
295 if (!whole_ashmem_region_is_free) {
296 AddFreeChunk_Locked(
297 FreeChunk(previous_chunk, first_free_chunk, new_free_chunk_size));
298 return;
300 // The whole ashmem region is free thus it can be deleted.
301 DCHECK_EQ(base_, first_free_chunk);
302 DCHECK(free_chunks_.empty());
303 DCHECK(address_to_free_chunk_map_.empty());
304 DCHECK(used_to_previous_chunk_map_.empty());
305 allocator_->DeleteAshmemRegion_Locked(this); // Deletes |this|.
308 void AddFreeChunk_Locked(const FreeChunk& free_chunk) {
309 allocator_->lock_.AssertAcquired();
310 const std::multiset<FreeChunk>::iterator it = free_chunks_.insert(
311 free_chunk);
312 address_to_free_chunk_map_.insert(std::make_pair(free_chunk.start, it));
313 // Update the next used contiguous chunk, if any, since its previous chunk
314 // may have changed due to free chunks merging/splitting.
315 void* const next_used_contiguous_chunk =
316 static_cast<char*>(free_chunk.start) + free_chunk.size;
317 hash_map<void*, void*>::iterator previous_it =
318 used_to_previous_chunk_map_.find(next_used_contiguous_chunk);
319 if (previous_it != used_to_previous_chunk_map_.end())
320 previous_it->second = free_chunk.start;
323 // Finds and removes the free chunk, if any, whose start address is
324 // |chunk_start|. Returns a copy of the unlinked free chunk or a free chunk
325 // whose content is null if it was not found.
326 FreeChunk RemoveFreeChunk_Locked(void* chunk_start) {
327 allocator_->lock_.AssertAcquired();
328 const hash_map<
329 void*, std::multiset<FreeChunk>::iterator>::iterator it =
330 address_to_free_chunk_map_.find(chunk_start);
331 if (it == address_to_free_chunk_map_.end())
332 return FreeChunk(NULL, NULL, 0U);
333 return RemoveFreeChunkFromIterator_Locked(it->second);
336 // Same as above but takes an iterator in.
337 FreeChunk RemoveFreeChunkFromIterator_Locked(
338 std::multiset<FreeChunk>::iterator free_chunk_it) {
339 allocator_->lock_.AssertAcquired();
340 if (free_chunk_it == free_chunks_.end())
341 return FreeChunk(NULL, NULL, 0U);
342 DCHECK(free_chunk_it != free_chunks_.end());
343 const FreeChunk free_chunk(*free_chunk_it);
344 address_to_free_chunk_map_.erase(free_chunk_it->start);
345 free_chunks_.erase(free_chunk_it);
346 return free_chunk;
349 const int fd_;
350 const size_t size_;
351 void* const base_;
352 DiscardableMemoryAllocator* const allocator_;
353 void* highest_allocated_chunk_;
354 // Points to the end of |highest_allocated_chunk_|.
355 size_t offset_;
356 // Allows free chunks recycling (lookup, insertion and removal) in O(log N).
357 // Note that FreeChunk values are indexed by their size and also note that
358 // multiple free chunks can have the same size (which is why multiset<> is
359 // used instead of e.g. set<>).
360 std::multiset<FreeChunk> free_chunks_;
361 // Used while merging free contiguous chunks to erase free chunks (from their
362 // start address) in constant time. Note that multiset<>::{insert,erase}()
363 // don't invalidate iterators (except the one for the element being removed
364 // obviously).
365 hash_map<
366 void*, std::multiset<FreeChunk>::iterator> address_to_free_chunk_map_;
367 // Maps the address of *used* chunks to the address of their previous
368 // contiguous chunk.
369 hash_map<void*, void*> used_to_previous_chunk_map_;
371 DISALLOW_COPY_AND_ASSIGN(AshmemRegion);
374 DiscardableMemoryAllocator::DiscardableAshmemChunk::~DiscardableAshmemChunk() {
375 if (locked_)
376 internal::UnlockAshmemRegion(fd_, offset_, size_, address_);
377 ashmem_region_->OnChunkDeletion(address_, size_);
380 DiscardableMemoryAllocator::DiscardableMemoryAllocator(
381 const std::string& name,
382 size_t ashmem_region_size)
383 : name_(name),
384 ashmem_region_size_(
385 std::max(kMinAshmemRegionSize, AlignToNextPage(ashmem_region_size))),
386 last_ashmem_region_size_(0) {
387 DCHECK_GE(ashmem_region_size_, kMinAshmemRegionSize);
390 DiscardableMemoryAllocator::~DiscardableMemoryAllocator() {
391 DCHECK(thread_checker_.CalledOnValidThread());
392 DCHECK(ashmem_regions_.empty());
395 scoped_ptr<DiscardableMemory> DiscardableMemoryAllocator::Allocate(
396 size_t size) {
397 const size_t aligned_size = AlignToNextPage(size);
398 if (!aligned_size)
399 return scoped_ptr<DiscardableMemory>();
400 // TODO(pliard): make this function less naive by e.g. moving the free chunks
401 // multiset to the allocator itself in order to decrease even more
402 // fragmentation/speedup allocation. Note that there should not be more than a
403 // couple (=5) of AshmemRegion instances in practice though.
404 AutoLock auto_lock(lock_);
405 DCHECK_LE(ashmem_regions_.size(), 5U);
406 for (ScopedVector<AshmemRegion>::iterator it = ashmem_regions_.begin();
407 it != ashmem_regions_.end(); ++it) {
408 scoped_ptr<DiscardableMemory> memory(
409 (*it)->Allocate_Locked(size, aligned_size));
410 if (memory)
411 return memory.Pass();
413 // The creation of the (large) ashmem region might fail if the address space
414 // is too fragmented. In case creation fails the allocator retries by
415 // repetitively dividing the size by 2.
416 const size_t min_region_size = std::max(kMinAshmemRegionSize, aligned_size);
417 for (size_t region_size = std::max(ashmem_region_size_, aligned_size);
418 region_size >= min_region_size;
419 region_size = AlignToNextPage(region_size / 2)) {
420 scoped_ptr<AshmemRegion> new_region(
421 AshmemRegion::Create(region_size, name_.c_str(), this));
422 if (!new_region)
423 continue;
424 last_ashmem_region_size_ = region_size;
425 ashmem_regions_.push_back(new_region.release());
426 return ashmem_regions_.back()->Allocate_Locked(size, aligned_size);
428 // TODO(pliard): consider adding an histogram to see how often this happens.
429 return scoped_ptr<DiscardableMemory>();
432 size_t DiscardableMemoryAllocator::last_ashmem_region_size() const {
433 AutoLock auto_lock(lock_);
434 return last_ashmem_region_size_;
437 void DiscardableMemoryAllocator::DeleteAshmemRegion_Locked(
438 AshmemRegion* region) {
439 lock_.AssertAcquired();
440 // Note that there should not be more than a couple of ashmem region instances
441 // in |ashmem_regions_|.
442 DCHECK_LE(ashmem_regions_.size(), 5U);
443 const ScopedVector<AshmemRegion>::iterator it = std::find(
444 ashmem_regions_.begin(), ashmem_regions_.end(), region);
445 DCHECK_NE(ashmem_regions_.end(), it);
446 std::swap(*it, ashmem_regions_.back());
447 ashmem_regions_.pop_back();
450 } // namespace internal
451 } // namespace base