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 // Whether or not this field trial is activated. This is really just a boolean |
| 64 // but marked as a uint32_t for portability reasons. | |
| 65 uint32_t activated; | |
| 55 | 66 |
| 56 // Size of the pickled structure, NOT the total size of this entry. | 67 // Size of the pickled structure, NOT the total size of this entry. |
| 57 uint32_t size; | 68 uint32_t size; |
| 58 | 69 |
| 59 // Calling this is only valid when the entry is initialized. That is, it | 70 // 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 | 71 // resides in shared memory and has a pickle containing the trial name and |
| 61 // group name following it. | 72 // group name following it. |
| 62 bool GetTrialAndGroupName(StringPiece* trial_name, | 73 bool GetTrialAndGroupName(StringPiece* trial_name, |
| 63 StringPiece* group_name) const { | 74 StringPiece* group_name) const { |
| 64 char* src = reinterpret_cast<char*>(const_cast<FieldTrialEntry*>(this)) + | 75 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 | 732 // command-line flag. The child process will do the reverse conversions to |
| 722 // retrieve the handle. See http://stackoverflow.com/a/153077 | 733 // retrieve the handle. See http://stackoverflow.com/a/153077 |
| 723 auto uintptr_handle = | 734 auto uintptr_handle = |
| 724 reinterpret_cast<uintptr_t>(global_->readonly_allocator_handle_); | 735 reinterpret_cast<uintptr_t>(global_->readonly_allocator_handle_); |
| 725 size_t field_trial_length = | 736 size_t field_trial_length = |
| 726 global_->field_trial_allocator_->shared_memory()->mapped_size(); | 737 global_->field_trial_allocator_->shared_memory()->mapped_size(); |
| 727 std::string field_trial_handle = std::to_string(uintptr_handle) + "," + | 738 std::string field_trial_handle = std::to_string(uintptr_handle) + "," + |
| 728 std::to_string(field_trial_length); | 739 std::to_string(field_trial_length); |
| 729 | 740 |
| 730 cmd_line->AppendSwitchASCII(field_trial_handle_switch, field_trial_handle); | 741 cmd_line->AppendSwitchASCII(field_trial_handle_switch, field_trial_handle); |
| 731 UMA_HISTOGRAM_COUNTS_10000("UMA.FieldTrialAllocator.Size", | 742 global_->field_trial_allocator_->UpdateTrackingHistograms(); |
| 732 field_trial_length); | |
| 733 return; | 743 return; |
| 734 } | 744 } |
| 735 #endif | 745 #endif |
| 736 | 746 |
| 737 AddForceFieldTrialsFlag(cmd_line); | 747 AddForceFieldTrialsFlag(cmd_line); |
| 738 } | 748 } |
| 739 | 749 |
| 740 // static | 750 // static |
| 741 FieldTrial* FieldTrialList::CreateFieldTrial( | 751 FieldTrial* FieldTrialList::CreateFieldTrial( |
| 742 const std::string& name, | 752 const std::string& name, |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 897 // Don't do anything if the allocator hasn't been instantiated yet. | 907 // Don't do anything if the allocator hasn't been instantiated yet. |
| 898 if (allocator == nullptr) | 908 if (allocator == nullptr) |
| 899 return; | 909 return; |
| 900 | 910 |
| 901 // Or if the allocator is read only, which means we are in a child process and | 911 // Or if the allocator is read only, which means we are in a child process and |
| 902 // shouldn't be writing to it. | 912 // shouldn't be writing to it. |
| 903 if (allocator->IsReadonly()) | 913 if (allocator->IsReadonly()) |
| 904 return; | 914 return; |
| 905 | 915 |
| 906 // Or if we've already added it. | 916 // Or if we've already added it. |
| 907 if (field_trial->ref_ != SharedPersistentMemoryAllocator::kReferenceNull) | 917 if (field_trial->ref_ != SharedPersistentMemoryAllocator::kReferenceNull) |
|
bcwhite
2016/11/09 18:57:37
It's acceptable to use just "!value" to check vali
lawrencewu
2016/11/09 19:15:00
Done.
| |
| 908 return; | 918 return; |
| 909 | 919 |
| 910 FieldTrial::State trial_state; | 920 FieldTrial::State trial_state; |
| 911 if (!field_trial->GetState(&trial_state)) | 921 if (!field_trial->GetState(&trial_state)) |
| 912 return; | 922 return; |
| 913 | 923 |
| 914 Pickle pickle; | 924 Pickle pickle; |
| 915 pickle.WriteString(trial_state.trial_name); | 925 pickle.WriteString(trial_state.trial_name); |
| 916 pickle.WriteString(trial_state.group_name); | 926 pickle.WriteString(trial_state.group_name); |
| 917 | 927 |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 985 return; | 995 return; |
| 986 } | 996 } |
| 987 AutoLock auto_lock(global_->lock_); | 997 AutoLock auto_lock(global_->lock_); |
| 988 CHECK(!global_->PreLockedFind(trial->trial_name())) << trial->trial_name(); | 998 CHECK(!global_->PreLockedFind(trial->trial_name())) << trial->trial_name(); |
| 989 trial->AddRef(); | 999 trial->AddRef(); |
| 990 trial->SetTrialRegistered(); | 1000 trial->SetTrialRegistered(); |
| 991 global_->registered_[trial->trial_name()] = trial; | 1001 global_->registered_[trial->trial_name()] = trial; |
| 992 } | 1002 } |
| 993 | 1003 |
| 994 } // namespace base | 1004 } // namespace base |
| OLD | NEW |