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/debug/alias.h" | 13 #include "base/debug/alias.h" |
| 14 #include "base/feature_list.h" | 14 #include "base/feature_list.h" |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/metrics/field_trial_param_associator.h" | |
| 16 #include "base/pickle.h" | 17 #include "base/pickle.h" |
| 17 #include "base/process/memory.h" | 18 #include "base/process/memory.h" |
| 18 #include "base/rand_util.h" | 19 #include "base/rand_util.h" |
| 19 #include "base/strings/string_number_conversions.h" | 20 #include "base/strings/string_number_conversions.h" |
| 20 #include "base/strings/string_util.h" | 21 #include "base/strings/string_util.h" |
| 21 #include "base/strings/stringprintf.h" | 22 #include "base/strings/stringprintf.h" |
| 22 #include "base/strings/utf_string_conversions.h" | 23 #include "base/strings/utf_string_conversions.h" |
| 23 | 24 |
| 24 namespace base { | 25 namespace base { |
| 25 | 26 |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 64 // Expected size for 32/64-bit check. | 65 // Expected size for 32/64-bit check. |
| 65 static constexpr size_t kExpectedInstanceSize = 8; | 66 static constexpr size_t kExpectedInstanceSize = 8; |
| 66 | 67 |
| 67 // Whether or not this field trial is activated. This is really just a boolean | 68 // Whether or not this field trial is activated. This is really just a boolean |
| 68 // but marked as a uint32_t for portability reasons. | 69 // but marked as a uint32_t for portability reasons. |
| 69 uint32_t activated; | 70 uint32_t activated; |
| 70 | 71 |
| 71 // Size of the pickled structure, NOT the total size of this entry. | 72 // Size of the pickled structure, NOT the total size of this entry. |
| 72 uint32_t size; | 73 uint32_t size; |
| 73 | 74 |
| 75 // Returns an iterator over the data containing names and params. | |
| 76 PickleIterator GetPickleIterator() const { | |
| 77 char* src = reinterpret_cast<char*>(const_cast<FieldTrialEntry*>(this)) + | |
| 78 sizeof(FieldTrialEntry); | |
| 79 | |
| 80 Pickle pickle(src, size); | |
| 81 return PickleIterator(pickle); | |
| 82 } | |
| 83 | |
| 84 // Takes the iterator and writes out the first two items into |trial_name| and | |
| 85 // |group_name|. | |
| 86 bool ReadStringPair(PickleIterator* iter, | |
| 87 StringPiece* trial_name, | |
| 88 StringPiece* group_name) const { | |
| 89 if (!iter->ReadStringPiece(trial_name)) | |
| 90 return false; | |
| 91 if (!iter->ReadStringPiece(group_name)) | |
| 92 return false; | |
| 93 return true; | |
| 94 } | |
| 95 | |
| 74 // Calling this is only valid when the entry is initialized. That is, it | 96 // Calling this is only valid when the entry is initialized. That is, it |
| 75 // resides in shared memory and has a pickle containing the trial name and | 97 // resides in shared memory and has a pickle containing the trial name and |
| 76 // group name following it. | 98 // group name following it. |
| 77 bool GetTrialAndGroupName(StringPiece* trial_name, | 99 bool GetTrialAndGroupName(StringPiece* trial_name, |
| 78 StringPiece* group_name) const { | 100 StringPiece* group_name) const { |
| 79 char* src = reinterpret_cast<char*>(const_cast<FieldTrialEntry*>(this)) + | 101 PickleIterator iter = GetPickleIterator(); |
| 80 sizeof(FieldTrialEntry); | 102 return ReadStringPair(&iter, trial_name, group_name); |
| 103 } | |
| 81 | 104 |
| 82 Pickle pickle(src, size); | 105 // Calling this is only valid when the entry is initialized as well. Reads the |
| 83 PickleIterator pickle_iter(pickle); | 106 // parameters following the trial and group name and stores them as key-value |
| 107 // mappings in |params|. | |
| 108 bool GetParams(std::map<std::string, std::string>* params) const { | |
| 109 PickleIterator iter = GetPickleIterator(); | |
| 110 StringPiece tmp; | |
| 111 if (!ReadStringPair(&iter, &tmp, &tmp)) | |
| 112 return false; | |
| 84 | 113 |
| 85 if (!pickle_iter.ReadStringPiece(trial_name)) | 114 while (true) { |
| 86 return false; | 115 StringPiece key; |
| 87 if (!pickle_iter.ReadStringPiece(group_name)) | 116 StringPiece value; |
| 88 return false; | 117 if (!iter.ReadStringPiece(&key)) |
| 89 return true; | 118 return true; // No more params to read. |
| 119 if (!iter.ReadStringPiece(&value)) | |
| 120 return false; | |
| 121 (*params)[key.as_string()] = value.as_string(); | |
| 122 } | |
| 90 } | 123 } |
| 91 }; | 124 }; |
| 92 | 125 |
| 126 // Writes out the field trial's contents (via trial_state) to the pickle. The | |
| 127 // format of the pickle looks like: | |
| 128 // TrialName, GroupName, ParamKey1, ParamValue1, ParamKey2, ParamValue2, ... | |
| 129 // If there are no parameters, then it just ends at GroupName. | |
| 130 bool PickleFieldTrial(const FieldTrial::State& trial_state, Pickle* pickle) { | |
| 131 if (!pickle->WriteString(trial_state.trial_name)) | |
| 132 return false; | |
| 133 if (!pickle->WriteString(trial_state.group_name)) | |
| 134 return false; | |
| 135 | |
| 136 // Get field trial params. | |
| 137 std::map<std::string, std::string> params; | |
| 138 FieldTrialParamAssociator::GetInstance()->GetFieldTrialParamsWithoutFallback( | |
| 139 trial_state.trial_name.as_string(), trial_state.group_name.as_string(), | |
| 140 ¶ms); | |
| 141 | |
| 142 // Write params to pickle. | |
| 143 for (const auto& param : params) { | |
| 144 pickle->WriteString(StringPiece(param.first)); | |
| 145 pickle->WriteString(StringPiece(param.second)); | |
| 146 } | |
| 147 return true; | |
| 148 } | |
| 149 | |
| 93 // Created a time value based on |year|, |month| and |day_of_month| parameters. | 150 // Created a time value based on |year|, |month| and |day_of_month| parameters. |
| 94 Time CreateTimeFromParams(int year, int month, int day_of_month) { | 151 Time CreateTimeFromParams(int year, int month, int day_of_month) { |
| 95 DCHECK_GT(year, 1970); | 152 DCHECK_GT(year, 1970); |
| 96 DCHECK_GT(month, 0); | 153 DCHECK_GT(month, 0); |
| 97 DCHECK_LT(month, 13); | 154 DCHECK_LT(month, 13); |
| 98 DCHECK_GT(day_of_month, 0); | 155 DCHECK_GT(day_of_month, 0); |
| 99 DCHECK_LT(day_of_month, 32); | 156 DCHECK_LT(day_of_month, 32); |
| 100 | 157 |
| 101 Time::Exploded exploded; | 158 Time::Exploded exploded; |
| 102 exploded.year = year; | 159 exploded.year = year; |
| (...skipping 474 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 577 | 634 |
| 578 // static | 635 // static |
| 579 std::string FieldTrialList::FindFullName(const std::string& trial_name) { | 636 std::string FieldTrialList::FindFullName(const std::string& trial_name) { |
| 580 FieldTrial* field_trial = Find(trial_name); | 637 FieldTrial* field_trial = Find(trial_name); |
| 581 if (field_trial) | 638 if (field_trial) |
| 582 return field_trial->group_name(); | 639 return field_trial->group_name(); |
| 583 return std::string(); | 640 return std::string(); |
| 584 } | 641 } |
| 585 | 642 |
| 586 // static | 643 // static |
| 644 bool FieldTrialList::GetParamsFromSharedMemory( | |
| 645 FieldTrial* field_trial, | |
| 646 std::map<std::string, std::string>* params) { | |
| 647 DCHECK(global_); | |
| 648 // If the field trial allocator is not set up yet, then there are several | |
| 649 // cases: | |
| 650 // - We are in the browser process and the allocator has not been set up | |
| 651 // yet. If we got here, then we couldn't find the params in | |
| 652 // FieldTrialParamAssociator, so it's definitely not here. Return false. | |
| 653 // - Using shared memory for field trials is not enabled. If we got here, | |
| 654 // then there's nothing in shared memory. Return false. | |
| 655 // - We are in the child process and the allocator has not been set up yet. | |
| 656 // If this is the case, then you are calling this too early. The field trial | |
| 657 // allocator should get set up very early in the lifecycle. Try to see if | |
| 658 // you can call it after it's been set up. | |
| 659 if (!global_->field_trial_allocator_) | |
| 660 return false; | |
| 661 | |
| 662 // If ref_ isn't set, then the field trial data can't be in shared memory. | |
| 663 if (!field_trial->ref_) | |
| 664 return false; | |
| 665 | |
| 666 const FieldTrialEntry* entry = | |
| 667 global_->field_trial_allocator_->GetAsObject<const FieldTrialEntry>( | |
| 668 field_trial->ref_, kFieldTrialType); | |
| 669 return entry->GetParams(params); | |
| 670 } | |
| 671 | |
| 672 // static | |
| 673 void FieldTrialList::ClearParamsFromSharedMemoryForTesting() { | |
|
Alexei Svitkine (slow)
2016/11/23 17:23:39
Use the lock for this.
lawrencewu
2016/11/23 19:07:33
Done.
| |
| 674 if (!global_ || !global_->field_trial_allocator_) | |
| 675 return; | |
| 676 // To clear the params, we iterate through every item in the allocator, copy | |
| 677 // just the trial and group name into a newly-allocated segment and then clear | |
| 678 // the existing item. | |
| 679 SharedPersistentMemoryAllocator* allocator = | |
|
Alexei Svitkine (slow)
2016/11/23 17:23:39
FieldTrialAllocator
Change the other types below
lawrencewu
2016/11/23 19:07:33
Done.
| |
| 680 global_->field_trial_allocator_.get(); | |
| 681 PersistentMemoryAllocator::Iterator mem_iter(allocator); | |
| 682 | |
| 683 SharedPersistentMemoryAllocator::Reference prev_ref; | |
| 684 while ((prev_ref = mem_iter.GetNextOfType(kFieldTrialType)) != | |
| 685 SharedPersistentMemoryAllocator::kReferenceNull) { | |
| 686 // Get the existing field trial entry in shared memory. | |
| 687 const FieldTrialEntry* prev_entry = | |
| 688 allocator->GetAsObject<const FieldTrialEntry>(prev_ref, | |
| 689 kFieldTrialType); | |
| 690 StringPiece trial_name; | |
| 691 StringPiece group_name; | |
| 692 prev_entry->GetTrialAndGroupName(&trial_name, &group_name); | |
|
Alexei Svitkine (slow)
2016/11/23 17:23:39
Check return value?
lawrencewu
2016/11/23 19:07:33
Done by continuing if we failed to get the names.
| |
| 693 | |
| 694 // Write a new entry, minus the params. | |
| 695 Pickle pickle; | |
| 696 pickle.WriteString(trial_name); | |
| 697 pickle.WriteString(group_name); | |
| 698 size_t total_size = sizeof(FieldTrialEntry) + pickle.size(); | |
| 699 SharedPersistentMemoryAllocator::Reference new_ref = | |
| 700 allocator->Allocate(total_size, kFieldTrialType); | |
| 701 FieldTrialEntry* new_entry = | |
| 702 allocator->GetAsObject<FieldTrialEntry>(new_ref, kFieldTrialType); | |
| 703 new_entry->activated = prev_entry->activated; | |
| 704 new_entry->size = pickle.size(); | |
| 705 | |
| 706 // Update the ref on the field trial. | |
| 707 FieldTrial* trial = Find(trial_name.as_string()); | |
| 708 trial->ref_ = new_ref; | |
| 709 | |
| 710 // Mark the existing entry as unused. | |
| 711 uint32_t oldType = allocator->GetType(prev_ref); | |
|
Alexei Svitkine (slow)
2016/11/23 17:23:39
old_type
lawrencewu
2016/11/23 19:07:33
Fixed.
| |
| 712 allocator->ChangeType(prev_ref, 0, oldType); | |
| 713 } | |
| 714 } | |
| 715 | |
| 716 // static | |
| 587 bool FieldTrialList::TrialExists(const std::string& trial_name) { | 717 bool FieldTrialList::TrialExists(const std::string& trial_name) { |
| 588 return Find(trial_name) != NULL; | 718 return Find(trial_name) != NULL; |
| 589 } | 719 } |
| 590 | 720 |
| 591 // static | 721 // static |
| 592 bool FieldTrialList::IsTrialActive(const std::string& trial_name) { | 722 bool FieldTrialList::IsTrialActive(const std::string& trial_name) { |
| 593 FieldTrial* field_trial = Find(trial_name); | 723 FieldTrial* field_trial = Find(trial_name); |
| 594 FieldTrial::ActiveGroup active_group; | 724 FieldTrial::ActiveGroup active_group; |
| 595 return field_trial && field_trial->GetActiveGroup(&active_group); | 725 return field_trial && field_trial->GetActiveGroup(&active_group); |
| 596 } | 726 } |
| (...skipping 399 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 996 FieldTrial::State trial_state; | 1126 FieldTrial::State trial_state; |
| 997 if (!field_trial->GetStateWhileLocked(&trial_state)) | 1127 if (!field_trial->GetStateWhileLocked(&trial_state)) |
| 998 return; | 1128 return; |
| 999 | 1129 |
| 1000 // Or if we've already added it. We must check after GetState since it can | 1130 // Or if we've already added it. We must check after GetState since it can |
| 1001 // also add to the allocator. | 1131 // also add to the allocator. |
| 1002 if (field_trial->ref_) | 1132 if (field_trial->ref_) |
| 1003 return; | 1133 return; |
| 1004 | 1134 |
| 1005 Pickle pickle; | 1135 Pickle pickle; |
| 1006 pickle.WriteString(trial_state.trial_name); | 1136 if (!PickleFieldTrial(trial_state, &pickle)) |
| 1007 pickle.WriteString(trial_state.group_name); | 1137 return; |
| 1008 | 1138 |
| 1009 size_t total_size = sizeof(FieldTrialEntry) + pickle.size(); | 1139 size_t total_size = sizeof(FieldTrialEntry) + pickle.size(); |
| 1010 FieldTrial::FieldTrialRef ref = | 1140 FieldTrial::FieldTrialRef ref = |
| 1011 allocator->Allocate(total_size, kFieldTrialType); | 1141 allocator->Allocate(total_size, kFieldTrialType); |
| 1012 if (ref == FieldTrialAllocator::kReferenceNull) | 1142 if (ref == FieldTrialAllocator::kReferenceNull) |
| 1013 return; | 1143 return; |
| 1014 | 1144 |
| 1015 FieldTrialEntry* entry = | 1145 FieldTrialEntry* entry = |
| 1016 allocator->GetAsObject<FieldTrialEntry>(ref, kFieldTrialType); | 1146 allocator->GetAsObject<FieldTrialEntry>(ref, kFieldTrialType); |
| 1017 entry->activated = trial_state.activated; | 1147 entry->activated = trial_state.activated; |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1075 return; | 1205 return; |
| 1076 } | 1206 } |
| 1077 AutoLock auto_lock(global_->lock_); | 1207 AutoLock auto_lock(global_->lock_); |
| 1078 CHECK(!global_->PreLockedFind(trial->trial_name())) << trial->trial_name(); | 1208 CHECK(!global_->PreLockedFind(trial->trial_name())) << trial->trial_name(); |
| 1079 trial->AddRef(); | 1209 trial->AddRef(); |
| 1080 trial->SetTrialRegistered(); | 1210 trial->SetTrialRegistered(); |
| 1081 global_->registered_[trial->trial_name()] = trial; | 1211 global_->registered_[trial->trial_name()] = trial; |
| 1082 } | 1212 } |
| 1083 | 1213 |
| 1084 } // namespace base | 1214 } // namespace base |
| OLD | NEW |