Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(352)

Side by Side Diff: base/memory/discardable_shared_memory.cc

Issue 1427163003: [Discardable Shared Memory] Adding Windows support. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/memory/discardable_shared_memory.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
91 return static_cast<SharedState*>(shared_memory.memory()); 95 return static_cast<SharedState*>(shared_memory.memory());
92 } 96 }
93 97
94 // Round up |size| to a multiple of page size. 98 // Round up |size| to a multiple of page size.
95 size_t AlignToPageSize(size_t size) { 99 size_t AlignToPageSize(size_t size) {
96 return bits::Align(size, base::GetPageSize()); 100 return bits::Align(size, base::GetPageSize());
97 } 101 }
98 102
99 } // namespace 103 } // namespace
100 104
105 // Indicates whether Lock/Unlock pinning is supported on this platform.
106 // Call DoPinning() to access this information from within class functions.
107 // static
108 char DiscardableSharedMemory::pinning_supported_ = -1;
109
110 // Lazy initialize pinning_supported_ to 0 (false) or 1 (true).
111 // -1 when unchecked.
112 bool DiscardableSharedMemory::DoPinning() {
113 bool supported = false;
114 switch (pinning_supported_) {
115 case 1:
116 supported = true;
117 case 0:
118 supported = false;
119 case -1:
120 pinning_supported_ = 0;
121 #if defined(OS_ANDROID)
122 // Ashmem support.
123 pinning_supported_ = 1;
124 supported = true;
125 #elif defined(OS_WIN)
126 // Check windows version.
127 // We need >= 8 for MEM_RESET_UNDO.
128 if (base::win::GetVersion() >= base::win::VERSION_WIN8) {
reveman 2015/11/03 00:23:57 base::win::GetVersion() is cheap and already cache
penny 2015/11/03 01:14:47 Thank for this! I missed the fact that OSInfo act
129 pinning_supported_ = 1;
130 supported = true;
131 }
132 #endif
133 }
134 return supported;
135 }
136
101 DiscardableSharedMemory::DiscardableSharedMemory() 137 DiscardableSharedMemory::DiscardableSharedMemory()
102 : mapped_size_(0), locked_page_count_(0) { 138 : mapped_size_(0), locked_page_count_(0) {
103 } 139 }
104 140
105 DiscardableSharedMemory::DiscardableSharedMemory( 141 DiscardableSharedMemory::DiscardableSharedMemory(
106 SharedMemoryHandle shared_memory_handle) 142 SharedMemoryHandle shared_memory_handle)
107 : shared_memory_(shared_memory_handle, false), 143 : shared_memory_(shared_memory_handle, false),
108 mapped_size_(0), 144 mapped_size_(0),
109 locked_page_count_(0) { 145 locked_page_count_(0) {
110 } 146 }
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after
207 locked_page_count_ += end - start; 243 locked_page_count_ += end - start;
208 #if DCHECK_IS_ON() 244 #if DCHECK_IS_ON()
209 // Detect incorrect usage by keeping track of exactly what pages are locked. 245 // Detect incorrect usage by keeping track of exactly what pages are locked.
210 for (auto page = start; page < end; ++page) { 246 for (auto page = start; page < end; ++page) {
211 auto result = locked_pages_.insert(page); 247 auto result = locked_pages_.insert(page);
212 DCHECK(result.second); 248 DCHECK(result.second);
213 } 249 }
214 DCHECK_EQ(locked_pages_.size(), locked_page_count_); 250 DCHECK_EQ(locked_pages_.size(), locked_page_count_);
215 #endif 251 #endif
216 252
253 if (DoPinning()) {
reveman 2015/11/03 00:23:57 this check is never false on android so unnecessar
penny 2015/11/03 01:14:47 Done.
217 #if defined(OS_ANDROID) 254 #if defined(OS_ANDROID)
218 SharedMemoryHandle handle = shared_memory_.handle(); 255 SharedMemoryHandle handle = shared_memory_.handle();
219 if (SharedMemory::IsHandleValid(handle)) { 256 if (SharedMemory::IsHandleValid(handle)) {
220 if (ashmem_pin_region( 257 if (ashmem_pin_region(
221 handle.fd, AlignToPageSize(sizeof(SharedState)) + offset, length)) { 258 handle.fd, AlignToPageSize(sizeof(SharedState)) + offset, length)) {
259 return PURGED;
260 }
261 }
262 #elif defined(OS_WIN)
263 if (nullptr == VirtualAlloc(reinterpret_cast<char*>(
reveman 2015/11/03 00:23:57 nit: chromium style is "if (!VirtualAlloc(reinte..
penny 2015/11/03 01:14:47 Done.
264 shared_memory_.memory()) +
265 AlignToPageSize(
266 sizeof(SharedState)) +
267 offset,
268 length,
269 MEM_RESET_UNDO,
270 PAGE_READWRITE)) {
222 return PURGED; 271 return PURGED;
223 } 272 }
273 #endif
224 } 274 }
225 #endif
226 275
227 return SUCCESS; 276 return SUCCESS;
228 } 277 }
229 278
230 void DiscardableSharedMemory::Unlock(size_t offset, size_t length) { 279 void DiscardableSharedMemory::Unlock(size_t offset, size_t length) {
231 DCHECK_EQ(AlignToPageSize(offset), offset); 280 DCHECK_EQ(AlignToPageSize(offset), offset);
232 DCHECK_EQ(AlignToPageSize(length), length); 281 DCHECK_EQ(AlignToPageSize(length), length);
233 282
234 // Calls to this function must be synchronized properly. 283 // Calls to this function must be synchronized properly.
235 DFAKE_SCOPED_LOCK(thread_collision_warner_); 284 DFAKE_SCOPED_LOCK(thread_collision_warner_);
236 285
237 // Zero for length means "everything onward". 286 // Zero for length means "everything onward".
238 if (!length) 287 if (!length)
239 length = AlignToPageSize(mapped_size_) - offset; 288 length = AlignToPageSize(mapped_size_) - offset;
240 289
241 DCHECK(shared_memory_.memory()); 290 DCHECK(shared_memory_.memory());
242 291
292 if (DoPinning()) {
243 #if defined(OS_ANDROID) 293 #if defined(OS_ANDROID)
244 SharedMemoryHandle handle = shared_memory_.handle(); 294 SharedMemoryHandle handle = shared_memory_.handle();
245 if (SharedMemory::IsHandleValid(handle)) { 295 if (SharedMemory::IsHandleValid(handle)) {
246 if (ashmem_unpin_region( 296 if (ashmem_unpin_region(
247 handle.fd, AlignToPageSize(sizeof(SharedState)) + offset, length)) { 297 handle.fd, AlignToPageSize(sizeof(SharedState)) + offset, length)) {
248 DPLOG(ERROR) << "ashmem_unpin_region() failed"; 298 DPLOG(ERROR) << "ashmem_unpin_region() failed";
299 }
249 } 300 }
301 #elif defined(OS_WIN)
302 if (nullptr == VirtualAlloc(reinterpret_cast<char*>(
reveman 2015/11/03 00:23:57 if (!VirtualAlloc(reinte...
penny 2015/11/03 01:14:47 Done.
303 shared_memory_.memory()) +
304 AlignToPageSize(
305 sizeof(SharedState)) +
306 offset,
307 length,
308 MEM_RESET,
309 PAGE_READWRITE)) {
310 DPLOG(ERROR) << "VirtualAlloc() MEM_RESET failed in Unlock()";
311 }
312 #endif
250 } 313 }
251 #endif
252 314
253 size_t start = offset / base::GetPageSize(); 315 size_t start = offset / base::GetPageSize();
254 size_t end = start + length / base::GetPageSize(); 316 size_t end = start + length / base::GetPageSize();
255 DCHECK_LT(start, end); 317 DCHECK_LT(start, end);
256 DCHECK_LE(end, AlignToPageSize(mapped_size_) / base::GetPageSize()); 318 DCHECK_LE(end, AlignToPageSize(mapped_size_) / base::GetPageSize());
257 319
258 // Remove pages from |locked_page_count_|. 320 // Remove pages from |locked_page_count_|.
259 // Note: Unlocking a page that is not locked is an error. 321 // Note: Unlocking a page that is not locked is an error.
260 DCHECK_GE(locked_page_count_, end - start); 322 DCHECK_GE(locked_page_count_, end - start);
261 locked_page_count_ -= end - start; 323 locked_page_count_ -= end - start;
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after
333 #define MADV_PURGE_ARGUMENT MADV_FREE 395 #define MADV_PURGE_ARGUMENT MADV_FREE
334 #endif 396 #endif
335 // Advise the kernel to remove resources associated with purged pages. 397 // Advise the kernel to remove resources associated with purged pages.
336 // Subsequent accesses of memory pages will succeed, but might result in 398 // Subsequent accesses of memory pages will succeed, but might result in
337 // zero-fill-on-demand pages. 399 // zero-fill-on-demand pages.
338 if (madvise(reinterpret_cast<char*>(shared_memory_.memory()) + 400 if (madvise(reinterpret_cast<char*>(shared_memory_.memory()) +
339 AlignToPageSize(sizeof(SharedState)), 401 AlignToPageSize(sizeof(SharedState)),
340 AlignToPageSize(mapped_size_), MADV_PURGE_ARGUMENT)) { 402 AlignToPageSize(mapped_size_), MADV_PURGE_ARGUMENT)) {
341 DPLOG(ERROR) << "madvise() failed"; 403 DPLOG(ERROR) << "madvise() failed";
342 } 404 }
405 #elif defined(OS_WIN)
406 // MEM_DECOMMIT the purged pages to release the physical storage,
407 // either in memory or in the paging file on disk. Pages remain RESERVED.
408 // NOTE: this is the best that can be done from the browser, until the
409 // child/renderer notices the purge and releases its own handles.
410 if (!VirtualFree(reinterpret_cast<char*>(shared_memory_.memory()) +
411 AlignToPageSize(sizeof(SharedState)),
412 AlignToPageSize(mapped_size_),
413 MEM_DECOMMIT)) {
414 DPLOG(ERROR) << "VirtualFree() MEM_DECOMMIT failed in Purge()";
415 }
343 #endif 416 #endif
344 417
345 last_known_usage_ = Time(); 418 last_known_usage_ = Time();
346 return true; 419 return true;
347 } 420 }
348 421
349 bool DiscardableSharedMemory::IsMemoryResident() const { 422 bool DiscardableSharedMemory::IsMemoryResident() const {
350 DCHECK(shared_memory_.memory()); 423 DCHECK(shared_memory_.memory());
351 424
352 SharedState result(subtle::NoBarrier_Load( 425 SharedState result(subtle::NoBarrier_Load(
(...skipping 14 matching lines...) Expand all
367 440
368 void DiscardableSharedMemory::Close() { 441 void DiscardableSharedMemory::Close() {
369 shared_memory_.Close(); 442 shared_memory_.Close();
370 } 443 }
371 444
372 Time DiscardableSharedMemory::Now() const { 445 Time DiscardableSharedMemory::Now() const {
373 return Time::Now(); 446 return Time::Now();
374 } 447 }
375 448
376 } // namespace base 449 } // namespace base
OLDNEW
« no previous file with comments | « base/memory/discardable_shared_memory.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698