OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/shared_memory.h" | 5 #include "base/memory/shared_memory.h" |
6 | 6 |
7 #include <fcntl.h> | 7 #include <fcntl.h> |
8 #include <mach/mach_vm.h> | 8 #include <mach/mach_vm.h> |
9 #include <sys/mman.h> | 9 #include <sys/mman.h> |
10 #include <sys/stat.h> | 10 #include <sys/stat.h> |
11 #include <unistd.h> | 11 #include <unistd.h> |
12 | 12 |
13 #include "base/feature_list.h" | |
14 #include "base/files/file_util.h" | 13 #include "base/files/file_util.h" |
15 #include "base/files/scoped_file.h" | 14 #include "base/files/scoped_file.h" |
16 #include "base/logging.h" | 15 #include "base/logging.h" |
17 #include "base/mac/scoped_mach_vm.h" | 16 #include "base/mac/scoped_mach_vm.h" |
18 #include "base/metrics/histogram_macros.h" | |
19 #include "base/posix/eintr_wrapper.h" | 17 #include "base/posix/eintr_wrapper.h" |
20 #include "base/posix/safe_strerror.h" | 18 #include "base/posix/safe_strerror.h" |
21 #include "base/process/process_metrics.h" | 19 #include "base/process/process_metrics.h" |
22 #include "base/profiler/scoped_tracker.h" | 20 #include "base/profiler/scoped_tracker.h" |
23 #include "base/scoped_generic.h" | 21 #include "base/scoped_generic.h" |
24 #include "base/strings/utf_string_conversions.h" | 22 #include "base/strings/utf_string_conversions.h" |
25 | 23 |
26 #if defined(OS_MACOSX) | 24 #if defined(OS_MACOSX) |
27 #include "base/mac/foundation_util.h" | 25 #include "base/mac/foundation_util.h" |
28 #endif // OS_MACOSX | 26 #endif // OS_MACOSX |
29 | 27 |
30 namespace base { | 28 namespace base { |
31 | 29 |
32 namespace { | 30 namespace { |
33 | 31 |
34 const base::Feature kMacMemoryMechanismMach { | |
35 "MacSharedMemoryMechanismMach", base::FEATURE_ENABLED_BY_DEFAULT | |
36 }; | |
37 | |
38 SharedMemoryHandle::Type GetABTestMechanism() { | |
39 return base::FeatureList::IsEnabled(kMacMemoryMechanismMach) | |
40 ? SharedMemoryHandle::MACH | |
41 : SharedMemoryHandle::POSIX; | |
42 } | |
43 | |
44 // Emits a histogram entry indicating which type of SharedMemory was created. | |
45 void EmitMechanism(SharedMemoryHandle::Type type) { | |
46 UMA_HISTOGRAM_ENUMERATION("OSX.SharedMemory.Mechanism", type, | |
47 SharedMemoryHandle::TypeMax); | |
48 } | |
49 | |
50 // Returns whether the operation succeeded. | 32 // Returns whether the operation succeeded. |
51 // |new_handle| is an output variable, populated on success. The caller takes | 33 // |new_handle| is an output variable, populated on success. The caller takes |
52 // ownership of the underlying memory object. | 34 // ownership of the underlying memory object. |
53 // |handle| is the handle to copy. | 35 // |handle| is the handle to copy. |
54 // If |handle| is already mapped, |mapped_addr| is its mapped location. | 36 // If |handle| is already mapped, |mapped_addr| is its mapped location. |
55 // Otherwise, |mapped_addr| should be |nullptr|. | 37 // Otherwise, |mapped_addr| should be |nullptr|. |
56 bool MakeMachSharedMemoryHandleReadOnly(SharedMemoryHandle* new_handle, | 38 bool MakeMachSharedMemoryHandleReadOnly(SharedMemoryHandle* new_handle, |
57 SharedMemoryHandle handle, | 39 SharedMemoryHandle handle, |
58 void* mapped_addr) { | 40 void* mapped_addr) { |
59 if (!handle.IsValid()) | 41 if (!handle.IsValid()) |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
238 return CreateAnonymousPosix(size) && Map(size); | 220 return CreateAnonymousPosix(size) && Map(size); |
239 } | 221 } |
240 | 222 |
241 bool SharedMemory::CreateAnonymousPosix(size_t size) { | 223 bool SharedMemory::CreateAnonymousPosix(size_t size) { |
242 SharedMemoryCreateOptions options; | 224 SharedMemoryCreateOptions options; |
243 options.type = SharedMemoryHandle::POSIX; | 225 options.type = SharedMemoryHandle::POSIX; |
244 options.size = size; | 226 options.size = size; |
245 return Create(options); | 227 return Create(options); |
246 } | 228 } |
247 | 229 |
248 bool SharedMemory::CreateAndMapAnonymousMach(size_t size) { | |
249 SharedMemoryCreateOptions options; | |
250 | |
251 // A/B test the mechanism. Once the experiment is over, this will always be | |
252 // set to SharedMemoryHandle::MACH. | |
253 // http://crbug.com/547261 | |
254 options.type = GetABTestMechanism(); | |
255 options.size = size; | |
256 return Create(options) && Map(size); | |
257 } | |
258 | |
259 // static | 230 // static |
260 bool SharedMemory::GetSizeFromSharedMemoryHandle( | 231 bool SharedMemory::GetSizeFromSharedMemoryHandle( |
261 const SharedMemoryHandle& handle, | 232 const SharedMemoryHandle& handle, |
262 size_t* size) { | 233 size_t* size) { |
263 return handle.GetSize(size); | 234 return handle.GetSize(size); |
264 } | 235 } |
265 | 236 |
266 // Chromium mostly only uses the unique/private shmem as specified by | 237 // Chromium mostly only uses the unique/private shmem as specified by |
267 // "name == L"". The exception is in the StatsTable. | 238 // "name == L"". The exception is in the StatsTable. |
268 bool SharedMemory::Create(const SharedMemoryCreateOptions& options) { | 239 bool SharedMemory::Create(const SharedMemoryCreateOptions& options) { |
269 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466437 | 240 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466437 |
270 // is fixed. | 241 // is fixed. |
271 tracked_objects::ScopedTracker tracking_profile1( | 242 tracked_objects::ScopedTracker tracking_profile1( |
272 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 243 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
273 "466437 SharedMemory::Create::Start")); | 244 "466437 SharedMemory::Create::Start")); |
274 DCHECK(!shm_.IsValid()); | 245 DCHECK(!shm_.IsValid()); |
275 if (options.size == 0) return false; | 246 if (options.size == 0) return false; |
276 | 247 |
277 if (options.size > static_cast<size_t>(std::numeric_limits<int>::max())) | 248 if (options.size > static_cast<size_t>(std::numeric_limits<int>::max())) |
278 return false; | 249 return false; |
279 | 250 |
280 EmitMechanism(options.type); | |
281 | |
282 if (options.type == SharedMemoryHandle::MACH) { | 251 if (options.type == SharedMemoryHandle::MACH) { |
283 shm_ = SharedMemoryHandle(options.size); | 252 shm_ = SharedMemoryHandle(options.size); |
284 requested_size_ = options.size; | 253 requested_size_ = options.size; |
285 return shm_.IsValid(); | 254 return shm_.IsValid(); |
286 } | 255 } |
287 | 256 |
288 // This function theoretically can block on the disk. Both profiling of real | 257 // This function theoretically can block on the disk. Both profiling of real |
289 // users and local instrumentation shows that this is a real problem. | 258 // users and local instrumentation shows that this is a real problem. |
290 // https://code.google.com/p/chromium/issues/detail?id=466437 | 259 // https://code.google.com/p/chromium/issues/detail?id=466437 |
291 base::ThreadRestrictions::ScopedAllowIO allow_io; | 260 base::ThreadRestrictions::ScopedAllowIO allow_io; |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
472 | 441 |
473 if (close_self) { | 442 if (close_self) { |
474 Unmap(); | 443 Unmap(); |
475 Close(); | 444 Close(); |
476 } | 445 } |
477 | 446 |
478 return true; | 447 return true; |
479 } | 448 } |
480 | 449 |
481 } // namespace base | 450 } // namespace base |
OLD | NEW |