| 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" | |
| 17 #include "base/mac/scoped_mach_vm.h" | 16 #include "base/mac/scoped_mach_vm.h" |
| 18 #include "base/metrics/field_trial.h" | |
| 19 #include "base/metrics/histogram_macros.h" | |
| 20 #include "base/posix/eintr_wrapper.h" | 17 #include "base/posix/eintr_wrapper.h" |
| 21 #include "base/posix/safe_strerror.h" | 18 #include "base/posix/safe_strerror.h" |
| 22 #include "base/process/process_metrics.h" | 19 #include "base/process/process_metrics.h" |
| 23 #include "base/profiler/scoped_tracker.h" | 20 #include "base/profiler/scoped_tracker.h" |
| 24 #include "base/scoped_generic.h" | 21 #include "base/scoped_generic.h" |
| 25 #include "base/strings/utf_string_conversions.h" | 22 #include "base/strings/utf_string_conversions.h" |
| 26 | 23 |
| 27 #if defined(OS_MACOSX) | 24 #if defined(OS_MACOSX) |
| 28 #include "base/mac/foundation_util.h" | 25 #include "base/mac/foundation_util.h" |
| 29 #endif // OS_MACOSX | 26 #endif // OS_MACOSX |
| 30 | 27 |
| 31 namespace base { | 28 namespace base { |
| 32 | 29 |
| 33 namespace { | 30 namespace { |
| 34 | 31 |
| 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 | |
| 67 // Returns whether the operation succeeded. | 32 // Returns whether the operation succeeded. |
| 68 // |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 |
| 69 // ownership of the underlying memory object. | 34 // ownership of the underlying memory object. |
| 70 // |handle| is the handle to copy. | 35 // |handle| is the handle to copy. |
| 71 // If |handle| is already mapped, |mapped_addr| is its mapped location. | 36 // If |handle| is already mapped, |mapped_addr| is its mapped location. |
| 72 // Otherwise, |mapped_addr| should be |nullptr|. | 37 // Otherwise, |mapped_addr| should be |nullptr|. |
| 73 bool MakeMachSharedMemoryHandleReadOnly(SharedMemoryHandle* new_handle, | 38 bool MakeMachSharedMemoryHandleReadOnly(SharedMemoryHandle* new_handle, |
| 74 SharedMemoryHandle handle, | 39 SharedMemoryHandle handle, |
| 75 void* mapped_addr) { | 40 void* mapped_addr) { |
| 76 if (!handle.IsValid()) | 41 if (!handle.IsValid()) |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 255 return CreateAnonymousPosix(size) && Map(size); | 220 return CreateAnonymousPosix(size) && Map(size); |
| 256 } | 221 } |
| 257 | 222 |
| 258 bool SharedMemory::CreateAnonymousPosix(size_t size) { | 223 bool SharedMemory::CreateAnonymousPosix(size_t size) { |
| 259 SharedMemoryCreateOptions options; | 224 SharedMemoryCreateOptions options; |
| 260 options.type = SharedMemoryHandle::POSIX; | 225 options.type = SharedMemoryHandle::POSIX; |
| 261 options.size = size; | 226 options.size = size; |
| 262 return Create(options); | 227 return Create(options); |
| 263 } | 228 } |
| 264 | 229 |
| 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 | |
| 281 // static | 230 // static |
| 282 bool SharedMemory::GetSizeFromSharedMemoryHandle( | 231 bool SharedMemory::GetSizeFromSharedMemoryHandle( |
| 283 const SharedMemoryHandle& handle, | 232 const SharedMemoryHandle& handle, |
| 284 size_t* size) { | 233 size_t* size) { |
| 285 return handle.GetSize(size); | 234 return handle.GetSize(size); |
| 286 } | 235 } |
| 287 | 236 |
| 288 // Chromium mostly only uses the unique/private shmem as specified by | 237 // Chromium mostly only uses the unique/private shmem as specified by |
| 289 // "name == L"". The exception is in the StatsTable. | 238 // "name == L"". The exception is in the StatsTable. |
| 290 bool SharedMemory::Create(const SharedMemoryCreateOptions& options) { | 239 bool SharedMemory::Create(const SharedMemoryCreateOptions& options) { |
| 291 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466437 | 240 // TODO(erikchen): Remove ScopedTracker below once http://crbug.com/466437 |
| 292 // is fixed. | 241 // is fixed. |
| 293 tracked_objects::ScopedTracker tracking_profile1( | 242 tracked_objects::ScopedTracker tracking_profile1( |
| 294 FROM_HERE_WITH_EXPLICIT_FUNCTION( | 243 FROM_HERE_WITH_EXPLICIT_FUNCTION( |
| 295 "466437 SharedMemory::Create::Start")); | 244 "466437 SharedMemory::Create::Start")); |
| 296 DCHECK(!shm_.IsValid()); | 245 DCHECK(!shm_.IsValid()); |
| 297 if (options.size == 0) return false; | 246 if (options.size == 0) return false; |
| 298 | 247 |
| 299 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())) |
| 300 return false; | 249 return false; |
| 301 | 250 |
| 302 EmitMechanism(options.type); | |
| 303 | |
| 304 if (options.type == SharedMemoryHandle::MACH) { | 251 if (options.type == SharedMemoryHandle::MACH) { |
| 305 shm_ = SharedMemoryHandle(options.size); | 252 shm_ = SharedMemoryHandle(options.size); |
| 306 requested_size_ = options.size; | 253 requested_size_ = options.size; |
| 307 return shm_.IsValid(); | 254 return shm_.IsValid(); |
| 308 } | 255 } |
| 309 | 256 |
| 310 // 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 |
| 311 // users and local instrumentation shows that this is a real problem. | 258 // users and local instrumentation shows that this is a real problem. |
| 312 // https://code.google.com/p/chromium/issues/detail?id=466437 | 259 // https://code.google.com/p/chromium/issues/detail?id=466437 |
| 313 base::ThreadRestrictions::ScopedAllowIO allow_io; | 260 base::ThreadRestrictions::ScopedAllowIO allow_io; |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 494 | 441 |
| 495 if (close_self) { | 442 if (close_self) { |
| 496 Unmap(); | 443 Unmap(); |
| 497 Close(); | 444 Close(); |
| 498 } | 445 } |
| 499 | 446 |
| 500 return true; | 447 return true; |
| 501 } | 448 } |
| 502 | 449 |
| 503 } // namespace base | 450 } // namespace base |
| OLD | NEW |