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/files/file_util.h" | 13 #include "base/files/file_util.h" |
14 #include "base/files/scoped_file.h" | 14 #include "base/files/scoped_file.h" |
15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/mac/mac_util.h" |
16 #include "base/mac/scoped_mach_vm.h" | 17 #include "base/mac/scoped_mach_vm.h" |
| 18 #include "base/metrics/field_trial.h" |
| 19 #include "base/metrics/histogram_macros.h" |
17 #include "base/posix/eintr_wrapper.h" | 20 #include "base/posix/eintr_wrapper.h" |
18 #include "base/posix/safe_strerror.h" | 21 #include "base/posix/safe_strerror.h" |
19 #include "base/process/process_metrics.h" | 22 #include "base/process/process_metrics.h" |
20 #include "base/profiler/scoped_tracker.h" | 23 #include "base/profiler/scoped_tracker.h" |
21 #include "base/scoped_generic.h" | 24 #include "base/scoped_generic.h" |
22 #include "base/strings/utf_string_conversions.h" | 25 #include "base/strings/utf_string_conversions.h" |
23 | 26 |
24 #if defined(OS_MACOSX) | 27 #if defined(OS_MACOSX) |
25 #include "base/mac/foundation_util.h" | 28 #include "base/mac/foundation_util.h" |
26 #endif // OS_MACOSX | 29 #endif // OS_MACOSX |
27 | 30 |
28 namespace base { | 31 namespace base { |
29 | 32 |
30 namespace { | 33 namespace { |
31 | 34 |
| 35 const char kTrialName[] = "MacMemoryMechanism"; |
| 36 const char kTrialMach[] = "Mach"; |
| 37 const char kTrialPosix[] = "Posix"; |
| 38 |
| 39 SharedMemoryHandle::Type GetABTestMechanism() { |
| 40 static bool found_group = false; |
| 41 static SharedMemoryHandle::Type group = SharedMemoryHandle::MACH; |
| 42 |
| 43 if (found_group) |
| 44 return group; |
| 45 |
| 46 const std::string group_name = |
| 47 base::FieldTrialList::FindFullName(kTrialName); |
| 48 if (group_name == kTrialMach) { |
| 49 group = SharedMemoryHandle::MACH; |
| 50 found_group = true; |
| 51 } else if (group_name == kTrialPosix) { |
| 52 group = SharedMemoryHandle::POSIX; |
| 53 found_group = true; |
| 54 } else { |
| 55 group = SharedMemoryHandle::MACH; |
| 56 } |
| 57 |
| 58 return group; |
| 59 } |
| 60 |
| 61 // Emits a histogram entry indicating which type of SharedMemory was created. |
| 62 void EmitMechanism(SharedMemoryHandle::Type type) { |
| 63 UMA_HISTOGRAM_ENUMERATION("OSX.SharedMemory.Mechanism", type, |
| 64 SharedMemoryHandle::TypeMax); |
| 65 } |
| 66 |
32 // Returns whether the operation succeeded. | 67 // Returns whether the operation succeeded. |
33 // |new_handle| is an output variable, populated on success. The caller takes | 68 // |new_handle| is an output variable, populated on success. The caller takes |
34 // ownership of the underlying memory object. | 69 // ownership of the underlying memory object. |
35 // |handle| is the handle to copy. | 70 // |handle| is the handle to copy. |
36 // If |handle| is already mapped, |mapped_addr| is its mapped location. | 71 // If |handle| is already mapped, |mapped_addr| is its mapped location. |
37 // Otherwise, |mapped_addr| should be |nullptr|. | 72 // Otherwise, |mapped_addr| should be |nullptr|. |
38 bool MakeMachSharedMemoryHandleReadOnly(SharedMemoryHandle* new_handle, | 73 bool MakeMachSharedMemoryHandleReadOnly(SharedMemoryHandle* new_handle, |
39 SharedMemoryHandle handle, | 74 SharedMemoryHandle handle, |
40 void* mapped_addr) { | 75 void* mapped_addr) { |
41 if (!handle.IsValid()) | 76 if (!handle.IsValid()) |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
220 return CreateAnonymousPosix(size) && Map(size); | 255 return CreateAnonymousPosix(size) && Map(size); |
221 } | 256 } |
222 | 257 |
223 bool SharedMemory::CreateAnonymousPosix(size_t size) { | 258 bool SharedMemory::CreateAnonymousPosix(size_t size) { |
224 SharedMemoryCreateOptions options; | 259 SharedMemoryCreateOptions options; |
225 options.type = SharedMemoryHandle::POSIX; | 260 options.type = SharedMemoryHandle::POSIX; |
226 options.size = size; | 261 options.size = size; |
227 return Create(options); | 262 return Create(options); |
228 } | 263 } |
229 | 264 |
| 265 bool SharedMemory::CreateAndMapAnonymousMach(size_t size) { |
| 266 SharedMemoryCreateOptions options; |
| 267 |
| 268 if (mac::IsOSLionOrLater()) { |
| 269 // A/B test the mechanism. Once the experiment is over, this will always be |
| 270 // set to SharedMemoryHandle::MACH. |
| 271 // http://crbug.com/547261 |
| 272 options.type = GetABTestMechanism(); |
| 273 } else { |
| 274 // Mach shared memory isn't supported on OSX 10.6 or older. |
| 275 options.type = SharedMemoryHandle::POSIX; |
| 276 } |
| 277 options.size = size; |
| 278 return Create(options) && Map(size); |
| 279 } |
| 280 |
230 // static | 281 // static |
231 bool SharedMemory::GetSizeFromSharedMemoryHandle( | 282 bool SharedMemory::GetSizeFromSharedMemoryHandle( |
232 const SharedMemoryHandle& handle, | 283 const SharedMemoryHandle& handle, |
233 size_t* size) { | 284 size_t* size) { |
234 return handle.GetSize(size); | 285 return handle.GetSize(size); |
235 } | 286 } |
236 | 287 |
237 // Chromium mostly only uses the unique/private shmem as specified by | 288 // Chromium mostly only uses the unique/private shmem as specified by |
238 // "name == L"". The exception is in the StatsTable. | 289 // "name == L"". The exception is in the StatsTable. |
239 bool SharedMemory::Create(const SharedMemoryCreateOptions& options) { | 290 bool SharedMemory::Create(const SharedMemoryCreateOptions& options) { |
240 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466437 | 291 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466437 |
241 // is fixed. | 292 // is fixed. |
242 tracked_objects::ScopedTracker tracking_profile1( | 293 tracked_objects::ScopedTracker tracking_profile1( |
243 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 294 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
244 "466437 SharedMemory::Create::Start")); | 295 "466437 SharedMemory::Create::Start")); |
245 DCHECK(!shm_.IsValid()); | 296 DCHECK(!shm_.IsValid()); |
246 if (options.size == 0) return false; | 297 if (options.size == 0) return false; |
247 | 298 |
248 if (options.size > static_cast<size_t>(std::numeric_limits<int>::max())) | 299 if (options.size > static_cast<size_t>(std::numeric_limits<int>::max())) |
249 return false; | 300 return false; |
250 | 301 |
| 302 EmitMechanism(options.type); |
| 303 |
251 if (options.type == SharedMemoryHandle::MACH) { | 304 if (options.type == SharedMemoryHandle::MACH) { |
252 shm_ = SharedMemoryHandle(options.size); | 305 shm_ = SharedMemoryHandle(options.size); |
253 requested_size_ = options.size; | 306 requested_size_ = options.size; |
254 return shm_.IsValid(); | 307 return shm_.IsValid(); |
255 } | 308 } |
256 | 309 |
257 // This function theoretically can block on the disk. Both profiling of real | 310 // This function theoretically can block on the disk. Both profiling of real |
258 // users and local instrumentation shows that this is a real problem. | 311 // users and local instrumentation shows that this is a real problem. |
259 // https://code.google.com/p/chromium/issues/detail?id=466437 | 312 // https://code.google.com/p/chromium/issues/detail?id=466437 |
260 base::ThreadRestrictions::ScopedAllowIO allow_io; | 313 base::ThreadRestrictions::ScopedAllowIO allow_io; |
(...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
441 | 494 |
442 if (close_self) { | 495 if (close_self) { |
443 Unmap(); | 496 Unmap(); |
444 Close(); | 497 Close(); |
445 } | 498 } |
446 | 499 |
447 return true; | 500 return true; |
448 } | 501 } |
449 | 502 |
450 } // namespace base | 503 } // namespace base |
OLD | NEW |