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 // Note: MEM_RESET is not technically gated on Win8. However, this Unlock |
| 268 // function needs to match the Lock behaviour (MEM_RESET_UNDO) to properly |
| 269 // implement memory pinning. It needs to bias towards preserving the |
| 270 // contents of memory between an Unlock and next Lock. |
| 271 if (!VirtualAlloc(reinterpret_cast<char*>(shared_memory_.memory()) + |
| 272 AlignToPageSize(sizeof(SharedState)) + offset, |
| 273 length, MEM_RESET, PAGE_READWRITE)) { |
| 274 DPLOG(ERROR) << "VirtualAlloc() MEM_RESET failed in Unlock()"; |
| 275 } |
| 276 } |
251 #endif | 277 #endif |
252 | 278 |
253 size_t start = offset / base::GetPageSize(); | 279 size_t start = offset / base::GetPageSize(); |
254 size_t end = start + length / base::GetPageSize(); | 280 size_t end = start + length / base::GetPageSize(); |
255 DCHECK_LT(start, end); | 281 DCHECK_LT(start, end); |
256 DCHECK_LE(end, AlignToPageSize(mapped_size_) / base::GetPageSize()); | 282 DCHECK_LE(end, AlignToPageSize(mapped_size_) / base::GetPageSize()); |
257 | 283 |
258 // Remove pages from |locked_page_count_|. | 284 // Remove pages from |locked_page_count_|. |
259 // Note: Unlocking a page that is not locked is an error. | 285 // Note: Unlocking a page that is not locked is an error. |
260 DCHECK_GE(locked_page_count_, end - start); | 286 DCHECK_GE(locked_page_count_, end - start); |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
316 // was incorrect or memory was locked. In the second case, the caller should | 342 // was incorrect or memory was locked. In the second case, the caller should |
317 // most likely wait for some amount of time before attempting to purge the | 343 // most likely wait for some amount of time before attempting to purge the |
318 // the memory again. | 344 // the memory again. |
319 if (result.value.u != old_state.value.u) { | 345 if (result.value.u != old_state.value.u) { |
320 last_known_usage_ = result.GetLockState() == SharedState::LOCKED | 346 last_known_usage_ = result.GetLockState() == SharedState::LOCKED |
321 ? current_time | 347 ? current_time |
322 : result.GetTimestamp(); | 348 : result.GetTimestamp(); |
323 return false; | 349 return false; |
324 } | 350 } |
325 | 351 |
| 352 // The next section will release as much resource as can be done |
| 353 // from the purging process, until the client process notices the |
| 354 // purge and releases its own references. |
| 355 // Note: this memory will not be accessed again. The segment will be |
| 356 // freed asynchronously at a later time, so just do the best |
| 357 // immediately. |
326 #if defined(OS_POSIX) && !defined(OS_NACL) | 358 #if defined(OS_POSIX) && !defined(OS_NACL) |
327 // Linux and Android provide MADV_REMOVE which is preferred as it has a | 359 // Linux and Android provide MADV_REMOVE which is preferred as it has a |
328 // behavior that can be verified in tests. Other POSIX flavors (MacOSX, BSDs), | 360 // behavior that can be verified in tests. Other POSIX flavors (MacOSX, BSDs), |
329 // provide MADV_FREE which has the same result but memory is purged lazily. | 361 // provide MADV_FREE which has the same result but memory is purged lazily. |
330 #if defined(OS_LINUX) || defined(OS_ANDROID) | 362 #if defined(OS_LINUX) || defined(OS_ANDROID) |
331 #define MADV_PURGE_ARGUMENT MADV_REMOVE | 363 #define MADV_PURGE_ARGUMENT MADV_REMOVE |
332 #else | 364 #else |
333 #define MADV_PURGE_ARGUMENT MADV_FREE | 365 #define MADV_PURGE_ARGUMENT MADV_FREE |
334 #endif | 366 #endif |
335 // Advise the kernel to remove resources associated with purged pages. | 367 // Advise the kernel to remove resources associated with purged pages. |
336 // Subsequent accesses of memory pages will succeed, but might result in | 368 // Subsequent accesses of memory pages will succeed, but might result in |
337 // zero-fill-on-demand pages. | 369 // zero-fill-on-demand pages. |
338 if (madvise(reinterpret_cast<char*>(shared_memory_.memory()) + | 370 if (madvise(reinterpret_cast<char*>(shared_memory_.memory()) + |
339 AlignToPageSize(sizeof(SharedState)), | 371 AlignToPageSize(sizeof(SharedState)), |
340 AlignToPageSize(mapped_size_), MADV_PURGE_ARGUMENT)) { | 372 AlignToPageSize(mapped_size_), MADV_PURGE_ARGUMENT)) { |
341 DPLOG(ERROR) << "madvise() failed"; | 373 DPLOG(ERROR) << "madvise() failed"; |
342 } | 374 } |
| 375 #elif defined(OS_WIN) |
| 376 // MEM_DECOMMIT the purged pages to release the physical storage, |
| 377 // either in memory or in the paging file on disk. Pages remain RESERVED. |
| 378 if (!VirtualFree(reinterpret_cast<char*>(shared_memory_.memory()) + |
| 379 AlignToPageSize(sizeof(SharedState)), |
| 380 AlignToPageSize(mapped_size_), MEM_DECOMMIT)) { |
| 381 DPLOG(ERROR) << "VirtualFree() MEM_DECOMMIT failed in Purge()"; |
| 382 } |
343 #endif | 383 #endif |
344 | 384 |
345 last_known_usage_ = Time(); | 385 last_known_usage_ = Time(); |
346 return true; | 386 return true; |
347 } | 387 } |
348 | 388 |
349 bool DiscardableSharedMemory::IsMemoryResident() const { | 389 bool DiscardableSharedMemory::IsMemoryResident() const { |
350 DCHECK(shared_memory_.memory()); | 390 DCHECK(shared_memory_.memory()); |
351 | 391 |
352 SharedState result(subtle::NoBarrier_Load( | 392 SharedState result(subtle::NoBarrier_Load( |
(...skipping 14 matching lines...) Expand all Loading... |
367 | 407 |
368 void DiscardableSharedMemory::Close() { | 408 void DiscardableSharedMemory::Close() { |
369 shared_memory_.Close(); | 409 shared_memory_.Close(); |
370 } | 410 } |
371 | 411 |
372 Time DiscardableSharedMemory::Now() const { | 412 Time DiscardableSharedMemory::Now() const { |
373 return Time::Now(); | 413 return Time::Now(); |
374 } | 414 } |
375 | 415 |
376 } // namespace base | 416 } // namespace base |
OLD | NEW |