Chromium Code Reviews| Index: base/metrics/field_trial.cc |
| diff --git a/base/metrics/field_trial.cc b/base/metrics/field_trial.cc |
| index 7dc5cb454e55877d7e345bbe13c1a4acca4d2711..0a650967a27d11cc8f2b0dfe645592fa22fac4ba 100644 |
| --- a/base/metrics/field_trial.cc |
| +++ b/base/metrics/field_trial.cc |
| @@ -21,6 +21,17 @@ |
| #include "base/strings/stringprintf.h" |
| #include "base/strings/utf_string_conversions.h" |
| +// On systems that use the zygote process to spawn child processes, we must |
| +// retrieve the correct fd using the mapping in GlobalDescriptors. |
| +#if defined(OS_POSIX) && !defined(OS_NACL) && !defined(OS_MACOSX) && \ |
| + !defined(OS_ANDROID) |
| +#define POSIX_WITH_ZYGOTE 1 |
| +#endif |
| + |
| +#if defined(POSIX_WITH_ZYGOTE) |
| +#include "base/posix/global_descriptors.h" |
| +#endif |
| + |
| namespace base { |
| namespace { |
| @@ -233,11 +244,20 @@ HANDLE CreateReadOnlyHandle(FieldTrialList::FieldTrialAllocator* allocator) { |
| DWORD access = SECTION_MAP_READ | SECTION_QUERY; |
| HANDLE dst; |
| if (!::DuplicateHandle(process, src, process, &dst, access, true, 0)) |
| - return nullptr; |
| + return kInvalidPlatformHandle; |
| return dst; |
| } |
| #endif |
| +#if defined(POSIX_WITH_ZYGOTE) |
| +int CreateReadOnlyHandle(FieldTrialList::FieldTrialAllocator* allocator) { |
| + SharedMemoryHandle new_handle; |
| + allocator->shared_memory()->ShareReadOnlyToProcess(GetCurrentProcessHandle(), |
| + &new_handle); |
| + return SharedMemory::GetFdFromSharedMemoryHandle(new_handle); |
| +} |
| +#endif |
| + |
| } // namespace |
| // statics |
| @@ -740,15 +760,12 @@ void FieldTrialList::CreateTrialsFromCommandLine( |
| const char* field_trial_handle_switch) { |
| global_->create_trials_from_command_line_called_ = true; |
| -#if defined(OS_WIN) && !defined(OS_NACL) |
| if (cmd_line.HasSwitch(field_trial_handle_switch)) { |
| - std::string arg = cmd_line.GetSwitchValueASCII(field_trial_handle_switch); |
| - int field_trial_handle = std::stoi(arg); |
| - HANDLE handle = reinterpret_cast<HANDLE>(field_trial_handle); |
| - bool result = CreateTrialsFromWindowsHandle(handle); |
| + std::string handle_switch = |
| + cmd_line.GetSwitchValueASCII(field_trial_handle_switch); |
| + bool result = CreateTrialsFromHandleSwitch(handle_switch); |
| DCHECK(result); |
| } |
| -#endif |
| if (cmd_line.HasSwitch(switches::kForceFieldTrials)) { |
| bool result = FieldTrialList::CreateTrialsFromString( |
| @@ -772,6 +789,20 @@ void FieldTrialList::AppendFieldTrialHandleIfNeeded( |
| } |
| #endif |
| +#if defined(OS_POSIX) && !defined(OS_NACL) |
| +// static |
| +int FieldTrialList::GetFieldTrialHandle() { |
| + if (!global_) |
| + return kInvalidPlatformFile; |
| + if (kUseSharedMemoryForFieldTrials) { |
| + InstantiateFieldTrialAllocatorIfNeeded(); |
| + if (global_->readonly_allocator_handle_ != kInvalidPlatformFile) |
| + return global_->readonly_allocator_handle_; |
| + } |
| + return kInvalidPlatformFile; |
| +} |
| +#endif |
| + |
| // static |
| void FieldTrialList::CopyFieldTrialStateToFlags( |
| const char* field_trial_handle_switch, |
| @@ -807,6 +838,26 @@ void FieldTrialList::CopyFieldTrialStateToFlags( |
| } |
| #endif |
| +#if defined(POSIX_WITH_ZYGOTE) |
| + // Use shared memory to pass the state if the feature is enabled, otherwise |
| + // fallback to passing it via the command line as a string. |
| + if (kUseSharedMemoryForFieldTrials) { |
|
Alexei Svitkine (slow)
2016/11/24 18:23:19
This block seems to be almost identical to the one
lawrencewu
2016/11/24 18:48:59
Made nearly identical, still have to do some windo
|
| + InstantiateFieldTrialAllocatorIfNeeded(); |
| + // If the readonly handle didn't get duplicated properly, then fallback to |
| + // original behavior. |
| + if (global_->readonly_allocator_handle_ == kInvalidPlatformFile) { |
| + AddForceFieldTrialsFlag(cmd_line); |
| + return; |
| + } |
| + |
| + std::string field_trial_handle = |
| + std::to_string(global_->readonly_allocator_handle_); |
| + cmd_line->AppendSwitchASCII(field_trial_handle_switch, field_trial_handle); |
| + global_->field_trial_allocator_->UpdateTrackingHistograms(); |
| + return; |
| + } |
| +#endif |
| + |
| AddForceFieldTrialsFlag(cmd_line); |
| } |
| @@ -893,11 +944,32 @@ size_t FieldTrialList::GetFieldTrialCount() { |
| return global_->registered_.size(); |
| } |
| -#if defined(OS_WIN) |
| // static |
| -bool FieldTrialList::CreateTrialsFromWindowsHandle(HANDLE handle) { |
| +bool FieldTrialList::CreateTrialsFromHandleSwitch( |
| + const std::string& handle_switch) { |
| +#if defined(OS_WIN) |
| + int field_trial_handle = std::stoi(handle_switch); |
| + HANDLE handle = reinterpret_cast<HANDLE>(field_trial_handle); |
| SharedMemoryHandle shm_handle(handle, GetCurrentProcId()); |
| + return FieldTrialList::CreateTrialsFromSharedMemoryHandle(shm_handle); |
| +#endif |
| +#if defined(POSIX_WITH_ZYGOTE) |
| + int fd_key = std::stoi(handle_switch); |
| + int fd = GlobalDescriptors::GetInstance()->Get(fd_key); |
| + SharedMemoryHandle shm_handle(fd, true); |
| + return FieldTrialList::CreateTrialsFromSharedMemoryHandle(shm_handle); |
| +#endif |
| + |
| +#if !defined(OS_WIN) && !defined(POSIX_WITH_ZYGOTE) |
| + return false; |
| +#endif |
| +} |
| + |
| +#if !defined(OS_NACL) |
| +// static |
| +bool FieldTrialList::CreateTrialsFromSharedMemoryHandle( |
| + SharedMemoryHandle shm_handle) { |
| // shm gets deleted when it gets out of scope, but that's OK because we need |
| // it only for the duration of this method. |
| std::unique_ptr<SharedMemory> shm(new SharedMemory(shm_handle, true)); |
| @@ -987,8 +1059,15 @@ void FieldTrialList::InstantiateFieldTrialAllocatorIfNeeded() { |
| if (global_->field_trial_allocator_ != nullptr) |
| return; |
| + SharedMemoryCreateOptions options; |
| + options.size = kFieldTrialAllocationSize; |
| + options.share_read_only = true; |
| + |
| std::unique_ptr<SharedMemory> shm(new SharedMemory()); |
| - if (!shm->CreateAndMapAnonymous(kFieldTrialAllocationSize)) |
| + if (!shm->Create(options)) |
| + TerminateBecauseOutOfMemory(kFieldTrialAllocationSize); |
| + |
| + if (!shm->Map(kFieldTrialAllocationSize)) |
| TerminateBecauseOutOfMemory(kFieldTrialAllocationSize); |
| global_->field_trial_allocator_.reset( |
| @@ -1000,7 +1079,7 @@ void FieldTrialList::InstantiateFieldTrialAllocatorIfNeeded() { |
| AddToAllocatorWhileLocked(registered.second); |
| } |
| -#if defined(OS_WIN) |
| +#if defined(OS_WIN) || defined(POSIX_WITH_ZYGOTE) |
| // Set |readonly_allocator_handle_| so we can pass it to be inherited and |
| // via the command line. |
| global_->readonly_allocator_handle_ = |