Index: base/metrics/field_trial.cc |
diff --git a/base/metrics/field_trial.cc b/base/metrics/field_trial.cc |
index eb4b05e3466ac2e5511e4508dd4f80c596c221b5..47fdc4c7904639c1fef0f02b452ecd1257c9703c 100644 |
--- a/base/metrics/field_trial.cc |
+++ b/base/metrics/field_trial.cc |
@@ -21,6 +21,10 @@ |
#include "base/strings/stringprintf.h" |
#include "base/strings/utf_string_conversions.h" |
+#if defined(OS_POSIX) |
+#include "base/posix/global_descriptors.h" |
+#endif |
+ |
namespace base { |
namespace { |
@@ -233,6 +237,14 @@ HANDLE CreateReadOnlyHandle(FieldTrialList::FieldTrialAllocator* allocator) { |
return nullptr; |
return dst; |
} |
+#elif defined(OS_POSIX) && !defined(OS_NACL) |
Alexei Svitkine (slow)
2016/11/23 17:49:55
Instead of these #elif chains that are confusing -
lawrencewu
2016/11/23 18:59:44
Done.
|
+int CreateReadOnlyHandle(FieldTrialList::FieldTrialAllocator* allocator) { |
+ SharedMemoryHandle new_handle; |
+ allocator->shared_memory()->ShareReadOnlyToProcess(GetCurrentProcessHandle(), |
+ &new_handle); |
+ int fd = SharedMemory::GetFdFromSharedMemoryHandle(new_handle); |
+ return fd; |
Alexei Svitkine (slow)
2016/11/23 17:49:55
Nit: Merge with line above.
lawrencewu
2016/11/23 18:59:44
Done.
|
+} |
#endif |
} // namespace |
@@ -737,15 +749,16 @@ 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); |
+#if defined(OS_WIN) |
+ bool result = CreateTrialsFromWindowsSwitch(arg); |
+ DCHECK(result); |
+#elif defined(OS_POSIX) && !defined(OS_NACL) |
+ bool result = CreateTrialsFromPosixSwitch(arg); |
DCHECK(result); |
- } |
#endif |
+ } |
if (cmd_line.HasSwitch(switches::kForceFieldTrials)) { |
bool result = FieldTrialList::CreateTrialsFromString( |
@@ -767,6 +780,18 @@ void FieldTrialList::AppendFieldTrialHandleIfNeeded( |
handles->push_back(global_->readonly_allocator_handle_); |
} |
} |
+#elif defined(OS_POSIX) && !defined(OS_NACL) |
+// static |
+int FieldTrialList::FieldTrialHandle() { |
+ if (!global_) |
+ return -1; |
+ if (kUseSharedMemoryForFieldTrials) { |
+ InstantiateFieldTrialAllocatorIfNeeded(); |
+ if (global_->readonly_allocator_handle_ != -1) |
+ return global_->readonly_allocator_handle_; |
+ } |
+ return -1; |
+} |
#endif |
// static |
@@ -802,6 +827,24 @@ void FieldTrialList::CopyFieldTrialStateToFlags( |
global_->field_trial_allocator_->UpdateTrackingHistograms(); |
return; |
} |
+#elif defined(OS_POSIX) && !defined(OS_NACL) |
+ // 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) { |
+ InstantiateFieldTrialAllocatorIfNeeded(); |
+ // If the readonly handle didn't get duplicated properly, then fallback to |
+ // original behavior. |
+ if (global_->readonly_allocator_handle_ == -1) { |
+ 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); |
@@ -892,9 +935,26 @@ size_t FieldTrialList::GetFieldTrialCount() { |
#if defined(OS_WIN) |
// static |
-bool FieldTrialList::CreateTrialsFromWindowsHandle(HANDLE handle) { |
+bool FieldTrialList::CreateTrialsFromWindowsSwitch(std::string cli_switch) { |
+ int field_trial_handle = std::stoi(cli_switch); |
+ HANDLE handle = reinterpret_cast<HANDLE>(field_trial_handle); |
SharedMemoryHandle shm_handle(handle, GetCurrentProcId()); |
+ return FieldTrialList::CreateTrialsFromSharedMemoryHandle(shm_handle); |
+} |
+#elif defined(OS_POSIX) && !defined(OS_NACL) |
+// static |
+bool FieldTrialList::CreateTrialsFromPosixSwitch(std::string cli_switch) { |
+ int fd_key = std::stoi(cli_switch); |
+ int fd = GlobalDescriptors::GetInstance()->Get(fd_key); |
+ SharedMemoryHandle shm_handle(fd, true); |
+ return FieldTrialList::CreateTrialsFromSharedMemoryHandle(shm_handle); |
+} |
+#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)); |
@@ -984,8 +1044,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( |
@@ -997,7 +1064,7 @@ void FieldTrialList::InstantiateFieldTrialAllocatorIfNeeded() { |
AddToAllocatorWhileLocked(registered.second); |
} |
-#if defined(OS_WIN) |
+#if defined(OS_WIN) || defined(OS_POSIX) |
// Set |readonly_allocator_handle_| so we can pass it to be inherited and |
// via the command line. |
global_->readonly_allocator_handle_ = |