Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/memory/discardable_shared_memory.h" | 5 #include "base/memory/discardable_shared_memory.h" |
| 6 | 6 |
| 7 #if defined(OS_POSIX) && !defined(OS_NACL) | 7 #if defined(OS_POSIX) && !defined(OS_NACL) |
| 8 // For madvise() which is available on all POSIX compatible systems. | 8 // For madvise() which is available on all POSIX compatible systems. |
| 9 #include <sys/mman.h> | 9 #include <sys/mman.h> |
| 10 #endif | 10 #endif |
| 11 | 11 |
| 12 #include <algorithm> | 12 #include <algorithm> |
| 13 | 13 |
| 14 #include "base/atomicops.h" | 14 #include "base/atomicops.h" |
| 15 #include "base/bits.h" | 15 #include "base/bits.h" |
| 16 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 #include "base/numerics/safe_math.h" | 17 #include "base/numerics/safe_math.h" |
| 18 #include "base/process/process_metrics.h" | 18 #include "base/process/process_metrics.h" |
| 19 | 19 |
| 20 #if defined(OS_ANDROID) | 20 #if defined(OS_ANDROID) |
| 21 #include "third_party/ashmem/ashmem.h" | 21 #include "third_party/ashmem/ashmem.h" |
| 22 #endif | 22 #endif |
| 23 | 23 |
| 24 #if defined(OS_WIN) | |
| 25 #include "base/win/windows_version.h" | |
| 26 #endif | |
| 27 | |
| 24 namespace base { | 28 namespace base { |
| 25 namespace { | 29 namespace { |
| 26 | 30 |
| 27 // Use a machine-sized pointer as atomic type. It will use the Atomic32 or | 31 // Use a machine-sized pointer as atomic type. It will use the Atomic32 or |
| 28 // Atomic64 routines, depending on the architecture. | 32 // Atomic64 routines, depending on the architecture. |
| 29 typedef intptr_t AtomicType; | 33 typedef intptr_t AtomicType; |
| 30 typedef uintptr_t UAtomicType; | 34 typedef uintptr_t UAtomicType; |
| 31 | 35 |
| 32 // Template specialization for timestamp serialization/deserialization. This | 36 // Template specialization for timestamp serialization/deserialization. This |
| 33 // is used to serialize timestamps using Unix time on systems where AtomicType | 37 // is used to serialize timestamps using Unix time on systems where AtomicType |
| (...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 207 locked_page_count_ += end - start; | 211 locked_page_count_ += end - start; |
| 208 #if DCHECK_IS_ON() | 212 #if DCHECK_IS_ON() |
| 209 // Detect incorrect usage by keeping track of exactly what pages are locked. | 213 // Detect incorrect usage by keeping track of exactly what pages are locked. |
| 210 for (auto page = start; page < end; ++page) { | 214 for (auto page = start; page < end; ++page) { |
| 211 auto result = locked_pages_.insert(page); | 215 auto result = locked_pages_.insert(page); |
| 212 DCHECK(result.second); | 216 DCHECK(result.second); |
| 213 } | 217 } |
| 214 DCHECK_EQ(locked_pages_.size(), locked_page_count_); | 218 DCHECK_EQ(locked_pages_.size(), locked_page_count_); |
| 215 #endif | 219 #endif |
| 216 | 220 |
| 221 // Pin pages if supported. | |
| 217 #if defined(OS_ANDROID) | 222 #if defined(OS_ANDROID) |
| 218 SharedMemoryHandle handle = shared_memory_.handle(); | 223 SharedMemoryHandle handle = shared_memory_.handle(); |
| 219 if (SharedMemory::IsHandleValid(handle)) { | 224 if (SharedMemory::IsHandleValid(handle)) { |
| 220 if (ashmem_pin_region( | 225 if (ashmem_pin_region( |
| 221 handle.fd, AlignToPageSize(sizeof(SharedState)) + offset, length)) { | 226 handle.fd, AlignToPageSize(sizeof(SharedState)) + offset, length)) { |
| 222 return PURGED; | 227 return PURGED; |
| 223 } | 228 } |
| 224 } | 229 } |
| 230 #elif defined(OS_WIN) | |
| 231 if (base::win::GetVersion() >= base::win::VERSION_WIN8) { | |
| 232 if (!VirtualAlloc(reinterpret_cast<char*>(shared_memory_.memory()) + | |
| 233 AlignToPageSize(sizeof(SharedState)) + offset, | |
| 234 length, MEM_RESET_UNDO, PAGE_READWRITE)) { | |
| 235 return PURGED; | |
| 236 } | |
| 237 } | |
| 225 #endif | 238 #endif |
| 226 | 239 |
| 227 return SUCCESS; | 240 return SUCCESS; |
| 228 } | 241 } |
| 229 | 242 |
| 230 void DiscardableSharedMemory::Unlock(size_t offset, size_t length) { | 243 void DiscardableSharedMemory::Unlock(size_t offset, size_t length) { |
| 231 DCHECK_EQ(AlignToPageSize(offset), offset); | 244 DCHECK_EQ(AlignToPageSize(offset), offset); |
| 232 DCHECK_EQ(AlignToPageSize(length), length); | 245 DCHECK_EQ(AlignToPageSize(length), length); |
| 233 | 246 |
| 234 // Calls to this function must be synchronized properly. | 247 // Calls to this function must be synchronized properly. |
| 235 DFAKE_SCOPED_LOCK(thread_collision_warner_); | 248 DFAKE_SCOPED_LOCK(thread_collision_warner_); |
| 236 | 249 |
| 237 // Zero for length means "everything onward". | 250 // Zero for length means "everything onward". |
| 238 if (!length) | 251 if (!length) |
| 239 length = AlignToPageSize(mapped_size_) - offset; | 252 length = AlignToPageSize(mapped_size_) - offset; |
| 240 | 253 |
| 241 DCHECK(shared_memory_.memory()); | 254 DCHECK(shared_memory_.memory()); |
| 242 | 255 |
| 256 // Unpin pages if supported. | |
| 243 #if defined(OS_ANDROID) | 257 #if defined(OS_ANDROID) |
| 244 SharedMemoryHandle handle = shared_memory_.handle(); | 258 SharedMemoryHandle handle = shared_memory_.handle(); |
| 245 if (SharedMemory::IsHandleValid(handle)) { | 259 if (SharedMemory::IsHandleValid(handle)) { |
| 246 if (ashmem_unpin_region( | 260 if (ashmem_unpin_region( |
| 247 handle.fd, AlignToPageSize(sizeof(SharedState)) + offset, length)) { | 261 handle.fd, AlignToPageSize(sizeof(SharedState)) + offset, length)) { |
| 248 DPLOG(ERROR) << "ashmem_unpin_region() failed"; | 262 DPLOG(ERROR) << "ashmem_unpin_region() failed"; |
| 249 } | 263 } |
| 250 } | 264 } |
| 265 #elif defined(OS_WIN) | |
| 266 if (base::win::GetVersion() >= base::win::VERSION_WIN8) { | |
| 267 if (!VirtualAlloc(reinterpret_cast<char*>(shared_memory_.memory()) + | |
| 268 AlignToPageSize(sizeof(SharedState)) + offset, | |
| 269 length, MEM_RESET, PAGE_READWRITE)) { | |
| 270 DPLOG(ERROR) << "VirtualAlloc() MEM_RESET failed in Unlock()"; | |
| 271 } | |
| 251 #endif | 272 #endif |
| 273 } | |
| 252 | 274 |
| 253 size_t start = offset / base::GetPageSize(); | 275 size_t start = offset / base::GetPageSize(); |
| 254 size_t end = start + length / base::GetPageSize(); | 276 size_t end = start + length / base::GetPageSize(); |
| 255 DCHECK_LT(start, end); | 277 DCHECK_LT(start, end); |
| 256 DCHECK_LE(end, AlignToPageSize(mapped_size_) / base::GetPageSize()); | 278 DCHECK_LE(end, AlignToPageSize(mapped_size_) / base::GetPageSize()); |
| 257 | 279 |
| 258 // Remove pages from |locked_page_count_|. | 280 // Remove pages from |locked_page_count_|. |
| 259 // Note: Unlocking a page that is not locked is an error. | 281 // Note: Unlocking a page that is not locked is an error. |
| 260 DCHECK_GE(locked_page_count_, end - start); | 282 DCHECK_GE(locked_page_count_, end - start); |
| 261 locked_page_count_ -= end - start; | 283 locked_page_count_ -= end - start; |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 333 #define MADV_PURGE_ARGUMENT MADV_FREE | 355 #define MADV_PURGE_ARGUMENT MADV_FREE |
| 334 #endif | 356 #endif |
| 335 // Advise the kernel to remove resources associated with purged pages. | 357 // Advise the kernel to remove resources associated with purged pages. |
| 336 // Subsequent accesses of memory pages will succeed, but might result in | 358 // Subsequent accesses of memory pages will succeed, but might result in |
| 337 // zero-fill-on-demand pages. | 359 // zero-fill-on-demand pages. |
| 338 if (madvise(reinterpret_cast<char*>(shared_memory_.memory()) + | 360 if (madvise(reinterpret_cast<char*>(shared_memory_.memory()) + |
| 339 AlignToPageSize(sizeof(SharedState)), | 361 AlignToPageSize(sizeof(SharedState)), |
| 340 AlignToPageSize(mapped_size_), MADV_PURGE_ARGUMENT)) { | 362 AlignToPageSize(mapped_size_), MADV_PURGE_ARGUMENT)) { |
| 341 DPLOG(ERROR) << "madvise() failed"; | 363 DPLOG(ERROR) << "madvise() failed"; |
| 342 } | 364 } |
| 365 #elif defined(OS_WIN) | |
| 366 // MEM_DECOMMIT the purged pages to release the physical storage, | |
| 367 // either in memory or in the paging file on disk. Pages remain RESERVED. | |
| 368 // NOTE: this is the best that can be done from the browser, until the | |
| 369 // child/renderer notices the purge and releases its own handles. | |
|
reveman
2015/11/03 01:42:31
nit: concepts such as browser and renderer process
penny
2015/11/03 02:17:08
Done.
| |
| 370 if (!VirtualFree(reinterpret_cast<char*>(shared_memory_.memory()) + | |
| 371 AlignToPageSize(sizeof(SharedState)), | |
| 372 AlignToPageSize(mapped_size_), MEM_DECOMMIT)) { | |
| 373 DPLOG(ERROR) << "VirtualFree() MEM_DECOMMIT failed in Purge()"; | |
| 374 } | |
| 343 #endif | 375 #endif |
| 344 | 376 |
| 345 last_known_usage_ = Time(); | 377 last_known_usage_ = Time(); |
| 346 return true; | 378 return true; |
| 347 } | 379 } |
| 348 | 380 |
| 349 bool DiscardableSharedMemory::IsMemoryResident() const { | 381 bool DiscardableSharedMemory::IsMemoryResident() const { |
| 350 DCHECK(shared_memory_.memory()); | 382 DCHECK(shared_memory_.memory()); |
| 351 | 383 |
| 352 SharedState result(subtle::NoBarrier_Load( | 384 SharedState result(subtle::NoBarrier_Load( |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 367 | 399 |
| 368 void DiscardableSharedMemory::Close() { | 400 void DiscardableSharedMemory::Close() { |
| 369 shared_memory_.Close(); | 401 shared_memory_.Close(); |
| 370 } | 402 } |
| 371 | 403 |
| 372 Time DiscardableSharedMemory::Now() const { | 404 Time DiscardableSharedMemory::Now() const { |
| 373 return Time::Now(); | 405 return Time::Now(); |
| 374 } | 406 } |
| 375 | 407 |
| 376 } // namespace base | 408 } // namespace base |
| OLD | NEW |