Commit cf1b5ae2 authored by Erik Chen's avatar Erik Chen Committed by Commit Bot

Call MADV_FREE_REUSE in implementation of discardable shared memory.

This is required to correctly perform accounting of shared memory on macOS.

Bug: 823915
Change-Id: I75a3bc4951e38bc24cc8c2b7867cfdd952fb860d
Reviewed-on: https://chromium-review.googlesource.com/974443Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarMark Mentovai <mark@chromium.org>
Commit-Queue: Daniel Cheng <dcheng@chromium.org>
Commit-Queue: Erik Chen <erikchen@chromium.org>
Cr-Commit-Position: refs/heads/master@{#545602}
parent 9a45a2d0
...@@ -112,11 +112,11 @@ size_t AlignToPageSize(size_t size) { ...@@ -112,11 +112,11 @@ size_t AlignToPageSize(size_t size) {
// base address at which |memory| is mapped, and that |offset| and |length| // base address at which |memory| is mapped, and that |offset| and |length|
// are page-aligned by the caller. // are page-aligned by the caller.
#if defined(OS_ANDROID)
// Returns SUCCESS on platforms which do not support discardable pages. // Returns SUCCESS on platforms which do not support discardable pages.
DiscardableSharedMemory::LockResult LockPages(const SharedMemory& memory, DiscardableSharedMemory::LockResult LockPages(const SharedMemory& memory,
size_t offset, size_t offset,
size_t length) { size_t length) {
#if defined(OS_ANDROID)
SharedMemoryHandle handle = memory.handle(); SharedMemoryHandle handle = memory.handle();
if (handle.IsValid()) { if (handle.IsValid()) {
int pin_result = ashmem_pin_region(handle.GetHandle(), offset, length); int pin_result = ashmem_pin_region(handle.GetHandle(), offset, length);
...@@ -125,9 +125,9 @@ DiscardableSharedMemory::LockResult LockPages(const SharedMemory& memory, ...@@ -125,9 +125,9 @@ DiscardableSharedMemory::LockResult LockPages(const SharedMemory& memory,
if (pin_result < 0) if (pin_result < 0)
return DiscardableSharedMemory::FAILED; return DiscardableSharedMemory::FAILED;
} }
#endif
return DiscardableSharedMemory::SUCCESS; return DiscardableSharedMemory::SUCCESS;
} }
#endif
// UnlockPages() is a no-op on platforms not supporting discardable pages. // UnlockPages() is a no-op on platforms not supporting discardable pages.
void UnlockPages(const SharedMemory& memory, size_t offset, size_t length) { void UnlockPages(const SharedMemory& memory, size_t offset, size_t length) {
...@@ -265,9 +265,32 @@ DiscardableSharedMemory::LockResult DiscardableSharedMemory::Lock( ...@@ -265,9 +265,32 @@ DiscardableSharedMemory::LockResult DiscardableSharedMemory::Lock(
if (!length) if (!length)
return PURGED; return PURGED;
#if defined(OS_ANDROID)
// Ensure that the platform won't discard the required pages. // Ensure that the platform won't discard the required pages.
return LockPages(shared_memory_, return LockPages(shared_memory_,
AlignToPageSize(sizeof(SharedState)) + offset, length); AlignToPageSize(sizeof(SharedState)) + offset, length);
#elif defined(OS_MACOSX)
// On macOS, there is no mechanism to lock pages. However, we do need to call
// madvise(MADV_FREE_REUSE) in order to correctly update accounting for memory
// footprint via task_info().
//
// Note that calling madvise(MADV_FREE_REUSE) on regions that haven't had
// madvise(MADV_FREE_REUSABLE) called on them has no effect.
//
// Note that the corresponding call to MADV_FREE_REUSABLE is in Purge(), since
// that's where the memory is actually released, rather than Unlock(), which
// is a no-op on macOS.
//
// For more information, see
// https://bugs.chromium.org/p/chromium/issues/detail?id=823915.
if (madvise(reinterpret_cast<char*>(shared_memory_.memory()) +
AlignToPageSize(sizeof(SharedState)),
AlignToPageSize(mapped_size_), MADV_FREE_REUSE))
;
return DiscardableSharedMemory::SUCCESS;
#else
return DiscardableSharedMemory::SUCCESS;
#endif
} }
void DiscardableSharedMemory::Unlock(size_t offset, size_t length) { void DiscardableSharedMemory::Unlock(size_t offset, size_t length) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment