Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(485)

Side by Side Diff: base/metrics/field_trial.cc

Issue 2463223002: Store field trial parameters in shared memory (Closed)
Patch Set: address comments Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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,
bcwhite 2016/11/23 21:03:52 The name of this method is generic. If you use st
lawrencewu 2016/11/23 21:49:20 I don't think we can totally reuse this method, si
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))
bcwhite 2016/11/23 21:03:52 Can you just reuse ReadStringPair here? iter must
lawrencewu 2016/11/23 21:49:20 Unfortunately, no, there doesn't seem to be a "has
bcwhite 2016/11/24 18:29:47 You can reuse the existing method with a little cr
lawrencewu 2016/11/24 21:50:35 That's very clever -- done.
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 &params);
141
142 // Write params to pickle.
143 for (const auto& param : params) {
144 pickle->WriteString(StringPiece(param.first));
bcwhite 2016/11/23 21:03:52 You're testing the return value of WriteString abo
lawrencewu 2016/11/23 21:49:20 Fixed.
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 783 matching lines...) Expand 10 before | Expand all | Expand 10 after
886 } 943 }
887 944
888 // static 945 // static
889 size_t FieldTrialList::GetFieldTrialCount() { 946 size_t FieldTrialList::GetFieldTrialCount() {
890 if (!global_) 947 if (!global_)
891 return 0; 948 return 0;
892 AutoLock auto_lock(global_->lock_); 949 AutoLock auto_lock(global_->lock_);
893 return global_->registered_.size(); 950 return global_->registered_.size();
894 } 951 }
895 952
953 // static
954 bool FieldTrialList::GetParamsFromSharedMemory(
955 FieldTrial* field_trial,
956 std::map<std::string, std::string>* params) {
957 DCHECK(global_);
958 // If the field trial allocator is not set up yet, then there are several
959 // cases:
960 // - We are in the browser process and the allocator has not been set up
961 // yet. If we got here, then we couldn't find the params in
962 // FieldTrialParamAssociator, so it's definitely not here. Return false.
963 // - Using shared memory for field trials is not enabled. If we got here,
964 // then there's nothing in shared memory. Return false.
965 // - We are in the child process and the allocator has not been set up yet.
966 // If this is the case, then you are calling this too early. The field trial
967 // allocator should get set up very early in the lifecycle. Try to see if
968 // you can call it after it's been set up.
969 if (!global_->field_trial_allocator_)
970 return false;
971
972 // If ref_ isn't set, then the field trial data can't be in shared memory.
973 if (!field_trial->ref_)
974 return false;
975
976 const FieldTrialEntry* entry =
977 global_->field_trial_allocator_->GetAsObject<const FieldTrialEntry>(
978 field_trial->ref_, kFieldTrialType);
bcwhite 2016/11/23 21:03:52 Verify: GetAllocSize() >= sizeof(entry) + entry->s
lawrencewu 2016/11/23 21:49:20 Added.
979 return entry->GetParams(params);
980 }
981
982 // static
983 void FieldTrialList::ClearParamsFromSharedMemoryForTesting() {
984 if (!global_ || !global_->field_trial_allocator_)
985 return;
986 AutoLock auto_lock(global_->lock_);
bcwhite 2016/11/23 21:03:52 The comment in the .h file says lock_ protects acc
lawrencewu 2016/11/23 21:49:20 Changed comment to reflect that lock_ also protect
987
988 // To clear the params, we iterate through every item in the allocator, copy
989 // just the trial and group name into a newly-allocated segment and then clear
990 // the existing item.
991 FieldTrialAllocator* allocator = global_->field_trial_allocator_.get();
992 FieldTrialAllocator::Iterator mem_iter(allocator);
993
994 FieldTrial::FieldTrialRef prev_ref;
995 while ((prev_ref = mem_iter.GetNextOfType(kFieldTrialType)) !=
996 FieldTrialAllocator::kReferenceNull) {
997 // Get the existing field trial entry in shared memory.
998 const FieldTrialEntry* prev_entry =
999 allocator->GetAsObject<const FieldTrialEntry>(prev_ref,
1000 kFieldTrialType);
1001 StringPiece trial_name;
1002 StringPiece group_name;
1003 if (!prev_entry->GetTrialAndGroupName(&trial_name, &group_name))
1004 continue;
1005
1006 // Write a new entry, minus the params.
1007 Pickle pickle;
1008 pickle.WriteString(trial_name);
1009 pickle.WriteString(group_name);
1010 size_t total_size = sizeof(FieldTrialEntry) + pickle.size();
1011 FieldTrial::FieldTrialRef new_ref =
1012 allocator->Allocate(total_size, kFieldTrialType);
1013 FieldTrialEntry* new_entry =
1014 allocator->GetAsObject<FieldTrialEntry>(new_ref, kFieldTrialType);
1015 new_entry->activated = prev_entry->activated;
1016 new_entry->size = pickle.size();
bcwhite 2016/11/23 21:03:52 Don't you need to copy the pickle data?
lawrencewu 2016/11/23 21:49:20 Whoops, good catch -- yes, fixed.
bcwhite 2016/11/24 18:29:47 If your tests didn't catch that then your tests ar
lawrencewu 2016/11/24 21:50:35 Done. I found another bug while writing the test,
1017
1018 // Update the ref on the field trial.
1019 FieldTrial* trial = Find(trial_name.as_string());
1020 trial->ref_ = new_ref;
1021
1022 // Mark the existing entry as unused.
1023 uint32_t old_type = allocator->GetType(prev_ref);
bcwhite 2016/11/23 21:03:52 You already know from line 995 that the type is kF
lawrencewu 2016/11/23 21:49:20 Fixed.
1024 allocator->ChangeType(prev_ref, 0, old_type);
1025 }
1026 }
1027
896 #if defined(OS_WIN) 1028 #if defined(OS_WIN)
897 // static 1029 // static
898 bool FieldTrialList::CreateTrialsFromWindowsHandle(HANDLE handle) { 1030 bool FieldTrialList::CreateTrialsFromWindowsHandle(HANDLE handle) {
899 SharedMemoryHandle shm_handle(handle, GetCurrentProcId()); 1031 SharedMemoryHandle shm_handle(handle, GetCurrentProcId());
900 1032
901 // shm gets deleted when it gets out of scope, but that's OK because we need 1033 // shm gets deleted when it gets out of scope, but that's OK because we need
902 // it only for the duration of this method. 1034 // it only for the duration of this method.
903 std::unique_ptr<SharedMemory> shm(new SharedMemory(shm_handle, true)); 1035 std::unique_ptr<SharedMemory> shm(new SharedMemory(shm_handle, true));
904 if (!shm.get()->Map(kFieldTrialAllocationSize)) 1036 if (!shm.get()->Map(kFieldTrialAllocationSize))
905 TerminateBecauseOutOfMemory(kFieldTrialAllocationSize); 1037 TerminateBecauseOutOfMemory(kFieldTrialAllocationSize);
(...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after
1029 FieldTrial::State trial_state; 1161 FieldTrial::State trial_state;
1030 if (!field_trial->GetStateWhileLocked(&trial_state)) 1162 if (!field_trial->GetStateWhileLocked(&trial_state))
1031 return; 1163 return;
1032 1164
1033 // Or if we've already added it. We must check after GetState since it can 1165 // Or if we've already added it. We must check after GetState since it can
1034 // also add to the allocator. 1166 // also add to the allocator.
1035 if (field_trial->ref_) 1167 if (field_trial->ref_)
1036 return; 1168 return;
1037 1169
1038 Pickle pickle; 1170 Pickle pickle;
1039 pickle.WriteString(trial_state.trial_name); 1171 if (!PickleFieldTrial(trial_state, &pickle))
1040 pickle.WriteString(trial_state.group_name); 1172 return;
1041 1173
1042 size_t total_size = sizeof(FieldTrialEntry) + pickle.size(); 1174 size_t total_size = sizeof(FieldTrialEntry) + pickle.size();
1043 FieldTrial::FieldTrialRef ref = 1175 FieldTrial::FieldTrialRef ref =
1044 allocator->Allocate(total_size, kFieldTrialType); 1176 allocator->Allocate(total_size, kFieldTrialType);
1045 if (ref == FieldTrialAllocator::kReferenceNull) 1177 if (ref == FieldTrialAllocator::kReferenceNull)
1046 return; 1178 return;
1047 1179
1048 FieldTrialEntry* entry = 1180 FieldTrialEntry* entry =
1049 allocator->GetAsObject<FieldTrialEntry>(ref, kFieldTrialType); 1181 allocator->GetAsObject<FieldTrialEntry>(ref, kFieldTrialType);
1050 entry->activated = trial_state.activated; 1182 entry->activated = trial_state.activated;
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
1108 return; 1240 return;
1109 } 1241 }
1110 AutoLock auto_lock(global_->lock_); 1242 AutoLock auto_lock(global_->lock_);
1111 CHECK(!global_->PreLockedFind(trial->trial_name())) << trial->trial_name(); 1243 CHECK(!global_->PreLockedFind(trial->trial_name())) << trial->trial_name();
1112 trial->AddRef(); 1244 trial->AddRef();
1113 trial->SetTrialRegistered(); 1245 trial->SetTrialRegistered();
1114 global_->registered_[trial->trial_name()] = trial; 1246 global_->registered_[trial->trial_name()] = trial;
1115 } 1247 }
1116 1248
1117 } // namespace base 1249 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698