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/field_trial_param_associator.h" | |
| 15 #include "base/metrics/histogram_macros.h" | 16 #include "base/metrics/histogram_macros.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 { |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 44 const char kAllocatorName[] = "FieldTrialAllocator"; | 45 const char kAllocatorName[] = "FieldTrialAllocator"; |
| 45 const uint32_t kFieldTrialType = 0xABA17E13 + 1; // SHA1(FieldTrialEntry) v1 | 46 const uint32_t kFieldTrialType = 0xABA17E13 + 1; // SHA1(FieldTrialEntry) v1 |
| 46 #if !defined(OS_NACL) | 47 #if !defined(OS_NACL) |
| 47 const size_t kFieldTrialAllocationSize = 4 << 10; // 4 KiB = one page | 48 const size_t kFieldTrialAllocationSize = 4 << 10; // 4 KiB = one page |
| 48 #endif | 49 #endif |
| 49 | 50 |
| 50 // We create one FieldTrialEntry per field trial in shared memory, via | 51 // We create one FieldTrialEntry per field trial in shared memory, via |
| 51 // AddToAllocatorWhileLocked. The FieldTrialEntry is followed by a base::Pickle | 52 // AddToAllocatorWhileLocked. The FieldTrialEntry is followed by a base::Pickle |
| 52 // object that we unpickle and read from. | 53 // object that we unpickle and read from. |
| 53 struct FieldTrialEntry { | 54 struct FieldTrialEntry { |
| 54 bool activated; | 55 bool activated; |
|
Alexei Svitkine (slow)
2016/11/01 14:57:16
Hmm, I thought I left a comment on your previous C
lawrencewu
2016/11/01 18:29:05
Oops, I must have missed that. Done.
| |
| 55 | 56 |
| 56 // Size of the pickled structure, NOT the total size of this entry. | 57 // Size of the pickled structure, NOT the total size of this entry. |
| 57 uint32_t size; | 58 uint32_t size; |
|
Alexei Svitkine (slow)
2016/11/01 14:57:16
Add a blank line below this and a comment about th
lawrencewu
2016/11/01 18:29:05
Done.
| |
| 59 PickleIterator GetPickleIterator() const { | |
| 60 char* src = reinterpret_cast<char*>(const_cast<FieldTrialEntry*>(this)) + | |
| 61 sizeof(FieldTrialEntry); | |
| 62 | |
| 63 Pickle pickle(src, size); | |
| 64 return PickleIterator(pickle); | |
| 65 } | |
| 58 | 66 |
| 59 // Calling this is only valid when the entry is initialized. That is, it | 67 // 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 | 68 // resides in shared memory and has a pickle containing the trial name and |
| 61 // group name following it. | 69 // group name following it. |
| 62 bool GetTrialAndGroupName(StringPiece* trial_name, | 70 bool GetTrialAndGroupName(StringPiece* trial_name, |
| 63 StringPiece* group_name) const { | 71 StringPiece* group_name) const { |
| 64 char* src = reinterpret_cast<char*>(const_cast<FieldTrialEntry*>(this)) + | 72 PickleIterator iter = GetPickleIterator(); |
| 65 sizeof(FieldTrialEntry); | 73 if (!iter.ReadStringPiece(trial_name)) |
| 66 | |
| 67 Pickle pickle(src, size); | |
| 68 PickleIterator pickle_iter(pickle); | |
| 69 | |
| 70 if (!pickle_iter.ReadStringPiece(trial_name)) | |
| 71 return false; | 74 return false; |
| 72 if (!pickle_iter.ReadStringPiece(group_name)) | 75 if (!iter.ReadStringPiece(group_name)) |
| 73 return false; | 76 return false; |
| 74 return true; | 77 return true; |
| 75 } | 78 } |
| 76 }; | 79 }; |
| 77 | 80 |
| 78 // Created a time value based on |year|, |month| and |day_of_month| parameters. | 81 // Created a time value based on |year|, |month| and |day_of_month| parameters. |
| 79 Time CreateTimeFromParams(int year, int month, int day_of_month) { | 82 Time CreateTimeFromParams(int year, int month, int day_of_month) { |
| 80 DCHECK_GT(year, 1970); | 83 DCHECK_GT(year, 1970); |
| 81 DCHECK_GT(month, 0); | 84 DCHECK_GT(month, 0); |
| 82 DCHECK_LT(month, 13); | 85 DCHECK_LT(month, 13); |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 174 HANDLE src = allocator->shared_memory()->handle().GetHandle(); | 177 HANDLE src = allocator->shared_memory()->handle().GetHandle(); |
| 175 ProcessHandle process = GetCurrentProcess(); | 178 ProcessHandle process = GetCurrentProcess(); |
| 176 DWORD access = SECTION_MAP_READ | SECTION_QUERY; | 179 DWORD access = SECTION_MAP_READ | SECTION_QUERY; |
| 177 HANDLE dst; | 180 HANDLE dst; |
| 178 if (!::DuplicateHandle(process, src, process, &dst, access, true, 0)) | 181 if (!::DuplicateHandle(process, src, process, &dst, access, true, 0)) |
| 179 return nullptr; | 182 return nullptr; |
| 180 return dst; | 183 return dst; |
| 181 } | 184 } |
| 182 #endif | 185 #endif |
| 183 | 186 |
| 187 bool PickleFieldTrialState(const FieldTrial::State& trial_state, | |
| 188 Pickle* pickle) { | |
| 189 pickle->WriteString(trial_state.trial_name); | |
| 190 pickle->WriteString(trial_state.group_name); | |
| 191 | |
| 192 std::map<std::string, std::string> params; | |
| 193 if (!FieldTrialParamAssociator::GetInstance()->GetFieldTrialParamsWhileLocked( | |
| 194 trial_state.trial_name.as_string(), | |
| 195 trial_state.group_name.as_string(), ¶ms)) | |
|
Alexei Svitkine (slow)
2016/11/01 14:57:16
Nit: {}'s
lawrencewu
2016/11/01 18:29:05
Done.
| |
| 196 return false; | |
| 197 | |
| 198 for (const auto& param : params) { | |
| 199 pickle->WriteString(StringPiece(param.first)); | |
| 200 pickle->WriteString(StringPiece(param.second)); | |
|
Alexei Svitkine (slow)
2016/11/01 14:57:16
Can you also add the changes to actually read from
lawrencewu
2016/11/01 18:29:05
Done.
| |
| 201 } | |
| 202 return true; | |
| 203 } | |
| 204 | |
| 184 } // namespace | 205 } // namespace |
| 185 | 206 |
| 186 // statics | 207 // statics |
| 187 const int FieldTrial::kNotFinalized = -1; | 208 const int FieldTrial::kNotFinalized = -1; |
| 188 const int FieldTrial::kDefaultGroupNumber = 0; | 209 const int FieldTrial::kDefaultGroupNumber = 0; |
| 189 bool FieldTrial::enable_benchmarking_ = false; | 210 bool FieldTrial::enable_benchmarking_ = false; |
| 190 | 211 |
| 191 int FieldTrialList::kNoExpirationYear = 0; | 212 int FieldTrialList::kNoExpirationYear = 0; |
| 192 | 213 |
| 193 //------------------------------------------------------------------------------ | 214 //------------------------------------------------------------------------------ |
| (...skipping 703 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 897 | 918 |
| 898 // Or if we've already added it. | 919 // Or if we've already added it. |
| 899 if (field_trial->ref_ != SharedPersistentMemoryAllocator::kReferenceNull) | 920 if (field_trial->ref_ != SharedPersistentMemoryAllocator::kReferenceNull) |
| 900 return; | 921 return; |
| 901 | 922 |
| 902 FieldTrial::State trial_state; | 923 FieldTrial::State trial_state; |
| 903 if (!field_trial->GetState(&trial_state)) | 924 if (!field_trial->GetState(&trial_state)) |
| 904 return; | 925 return; |
| 905 | 926 |
| 906 Pickle pickle; | 927 Pickle pickle; |
| 907 pickle.WriteString(trial_state.trial_name); | 928 if (!PickleFieldTrialState(trial_state, &pickle)) |
| 908 pickle.WriteString(trial_state.group_name); | 929 return; |
| 909 | 930 |
| 910 size_t total_size = sizeof(FieldTrialEntry) + pickle.size(); | 931 size_t total_size = sizeof(FieldTrialEntry) + pickle.size(); |
| 911 SharedPersistentMemoryAllocator::Reference ref = | 932 SharedPersistentMemoryAllocator::Reference ref = |
| 912 allocator->Allocate(total_size, kFieldTrialType); | 933 allocator->Allocate(total_size, kFieldTrialType); |
| 913 if (ref == SharedPersistentMemoryAllocator::kReferenceNull) | 934 if (ref == SharedPersistentMemoryAllocator::kReferenceNull) |
| 914 return; | 935 return; |
| 915 | 936 |
| 916 FieldTrialEntry* entry = | 937 FieldTrialEntry* entry = |
| 917 allocator->GetAsObject<FieldTrialEntry>(ref, kFieldTrialType); | 938 allocator->GetAsObject<FieldTrialEntry>(ref, kFieldTrialType); |
| 918 entry->activated = trial_state.activated; | 939 entry->activated = trial_state.activated; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 972 return; | 993 return; |
| 973 } | 994 } |
| 974 AutoLock auto_lock(global_->lock_); | 995 AutoLock auto_lock(global_->lock_); |
| 975 CHECK(!global_->PreLockedFind(trial->trial_name())) << trial->trial_name(); | 996 CHECK(!global_->PreLockedFind(trial->trial_name())) << trial->trial_name(); |
| 976 trial->AddRef(); | 997 trial->AddRef(); |
| 977 trial->SetTrialRegistered(); | 998 trial->SetTrialRegistered(); |
| 978 global_->registered_[trial->trial_name()] = trial; | 999 global_->registered_[trial->trial_name()] = trial; |
| 979 } | 1000 } |
| 980 | 1001 |
| 981 } // namespace base | 1002 } // namespace base |
| OLD | NEW |