Chromium Code Reviews| 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/metrics/field_trial.h" | 5 #include "base/metrics/field_trial.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/base_switches.h" | 10 #include "base/base_switches.h" |
| 11 #include "base/build_time.h" | 11 #include "base/build_time.h" |
| 12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 13 #include "base/feature_list.h" | 13 #include "base/feature_list.h" |
| 14 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/metrics/histogram_macros.h" | |
| 16 #include "base/pickle.h" | 15 #include "base/pickle.h" |
| 17 #include "base/process/memory.h" | 16 #include "base/process/memory.h" |
| 18 #include "base/rand_util.h" | 17 #include "base/rand_util.h" |
| 19 #include "base/strings/string_number_conversions.h" | 18 #include "base/strings/string_number_conversions.h" |
| 20 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
| 21 #include "base/strings/stringprintf.h" | 20 #include "base/strings/stringprintf.h" |
| 22 #include "base/strings/utf_string_conversions.h" | 21 #include "base/strings/utf_string_conversions.h" |
| 23 | 22 |
| 24 namespace base { | 23 namespace base { |
| 25 | 24 |
| 26 namespace { | 25 namespace { |
| 27 | 26 |
| 28 // Define a separator character to use when creating a persistent form of an | 27 // Define a separator character to use when creating a persistent form of an |
| 29 // instance. This is intended for use as a command line argument, passed to a | 28 // instance. This is intended for use as a command line argument, passed to a |
| 30 // second process to mimic our state (i.e., provide the same group name). | 29 // second process to mimic our state (i.e., provide the same group name). |
| 31 const char kPersistentStringSeparator = '/'; // Currently a slash. | 30 const char kPersistentStringSeparator = '/'; // Currently a slash. |
| 32 | 31 |
| 33 // Define a marker character to be used as a prefix to a trial name on the | 32 // Define a marker character to be used as a prefix to a trial name on the |
| 34 // command line which forces its activation. | 33 // command line which forces its activation. |
| 35 const char kActivationMarker = '*'; | 34 const char kActivationMarker = '*'; |
| 36 | 35 |
| 37 // Use shared memory to communicate field trial (experiment) state. Set to false | 36 // Use shared memory to communicate field trial (experiment) state. Set to false |
| 38 // for now while the implementation is fleshed out (e.g. data format, single | 37 // for now while the implementation is fleshed out (e.g. data format, single |
| 39 // shared memory segment). See https://codereview.chromium.org/2365273004/ and | 38 // shared memory segment). See https://codereview.chromium.org/2365273004/ and |
| 40 // crbug.com/653874 | 39 // crbug.com/653874 |
| 41 const bool kUseSharedMemoryForFieldTrials = false; | 40 const bool kUseSharedMemoryForFieldTrials = false; |
| 42 | 41 |
| 43 // Constants for the field trial allocator. | 42 // Constants for the field trial allocator. |
| 44 const char kAllocatorName[] = "FieldTrialAllocator"; | 43 const char kAllocatorName[] = "FieldTrialAllocator"; |
| 45 const uint32_t kFieldTrialType = 0xABA17E13 + 1; // SHA1(FieldTrialEntry) v1 | 44 const uint32_t kFieldTrialType = 0xABA17E13 + 2; // SHA1(FieldTrialEntry) v2 |
| 45 | |
| 46 // We allocate 64 KiB to hold all the field trial data. This should be enough, | |
| 47 // as currently we use ~8KiB for the field trials, and ~10KiB for experiment | |
| 48 // parameters (as of 9/11/2016). This also doesn't allocate all 64 KiB at once | |
| 49 // -- the pages only get mapped to physical memory when they are touched. If the | |
| 50 // size of the allocated field trials does get larger than 64 KiB, then we will | |
| 51 // drop some field trials in child processes, leading to an inconsistent view | |
| 52 // between browser and child processes and possibly causing crashes (see | |
| 53 // crbug.com/661617). | |
| 46 #if !defined(OS_NACL) | 54 #if !defined(OS_NACL) |
| 47 const size_t kFieldTrialAllocationSize = 4 << 10; // 4 KiB = one page | 55 const size_t kFieldTrialAllocationSize = 64 << 10; // 64 KiB |
| 48 #endif | 56 #endif |
| 49 | 57 |
| 50 // We create one FieldTrialEntry per field trial in shared memory, via | 58 // We create one FieldTrialEntry per field trial in shared memory, via |
| 51 // AddToAllocatorWhileLocked. The FieldTrialEntry is followed by a base::Pickle | 59 // AddToAllocatorWhileLocked. The FieldTrialEntry is followed by a base::Pickle |
| 52 // object that we unpickle and read from. | 60 // object that we unpickle and read from. Any changes to this structure requires |
| 61 // a bump in kFieldTrialType id defined above. | |
| 53 struct FieldTrialEntry { | 62 struct FieldTrialEntry { |
| 54 bool activated; | 63 uint32_t activated; |
|
Alexei Svitkine (slow)
2016/11/09 18:14:27
Please add a comment above this field. Especially
lawrencewu
2016/11/09 18:20:48
Somehow, this never actually gets done -- Done.
| |
| 55 | 64 |
| 56 // Size of the pickled structure, NOT the total size of this entry. | 65 // Size of the pickled structure, NOT the total size of this entry. |
| 57 uint32_t size; | 66 uint32_t size; |
| 58 | 67 |
| 59 // Calling this is only valid when the entry is initialized. That is, it | 68 // Calling this is only valid when the entry is initialized. That is, it |
| 60 // resides in shared memory and has a pickle containing the trial name and | 69 // resides in shared memory and has a pickle containing the trial name and |
| 61 // group name following it. | 70 // group name following it. |
| 62 bool GetTrialAndGroupName(StringPiece* trial_name, | 71 bool GetTrialAndGroupName(StringPiece* trial_name, |
| 63 StringPiece* group_name) const { | 72 StringPiece* group_name) const { |
| 64 char* src = reinterpret_cast<char*>(const_cast<FieldTrialEntry*>(this)) + | 73 char* src = reinterpret_cast<char*>(const_cast<FieldTrialEntry*>(this)) + |
| (...skipping 656 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 721 // command-line flag. The child process will do the reverse conversions to | 730 // command-line flag. The child process will do the reverse conversions to |
| 722 // retrieve the handle. See http://stackoverflow.com/a/153077 | 731 // retrieve the handle. See http://stackoverflow.com/a/153077 |
| 723 auto uintptr_handle = | 732 auto uintptr_handle = |
| 724 reinterpret_cast<uintptr_t>(global_->readonly_allocator_handle_); | 733 reinterpret_cast<uintptr_t>(global_->readonly_allocator_handle_); |
| 725 size_t field_trial_length = | 734 size_t field_trial_length = |
| 726 global_->field_trial_allocator_->shared_memory()->mapped_size(); | 735 global_->field_trial_allocator_->shared_memory()->mapped_size(); |
| 727 std::string field_trial_handle = std::to_string(uintptr_handle) + "," + | 736 std::string field_trial_handle = std::to_string(uintptr_handle) + "," + |
| 728 std::to_string(field_trial_length); | 737 std::to_string(field_trial_length); |
| 729 | 738 |
| 730 cmd_line->AppendSwitchASCII(field_trial_handle_switch, field_trial_handle); | 739 cmd_line->AppendSwitchASCII(field_trial_handle_switch, field_trial_handle); |
| 731 UMA_HISTOGRAM_COUNTS_10000("UMA.FieldTrialAllocator.Size", | 740 global_->field_trial_allocator_->UpdateTrackingHistograms(); |
| 732 field_trial_length); | |
| 733 return; | 741 return; |
| 734 } | 742 } |
| 735 #endif | 743 #endif |
| 736 | 744 |
| 737 AddForceFieldTrialsFlag(cmd_line); | 745 AddForceFieldTrialsFlag(cmd_line); |
| 738 } | 746 } |
| 739 | 747 |
| 740 // static | 748 // static |
| 741 FieldTrial* FieldTrialList::CreateFieldTrial( | 749 FieldTrial* FieldTrialList::CreateFieldTrial( |
| 742 const std::string& name, | 750 const std::string& name, |
| (...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 985 return; | 993 return; |
| 986 } | 994 } |
| 987 AutoLock auto_lock(global_->lock_); | 995 AutoLock auto_lock(global_->lock_); |
| 988 CHECK(!global_->PreLockedFind(trial->trial_name())) << trial->trial_name(); | 996 CHECK(!global_->PreLockedFind(trial->trial_name())) << trial->trial_name(); |
| 989 trial->AddRef(); | 997 trial->AddRef(); |
| 990 trial->SetTrialRegistered(); | 998 trial->SetTrialRegistered(); |
| 991 global_->registered_[trial->trial_name()] = trial; | 999 global_->registered_[trial->trial_name()] = trial; |
| 992 } | 1000 } |
| 993 | 1001 |
| 994 } // namespace base | 1002 } // namespace base |
| OLD | NEW |