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/process/memory.h" | |
15 #include "base/rand_util.h" | 16 #include "base/rand_util.h" |
16 #include "base/strings/string_number_conversions.h" | 17 #include "base/strings/string_number_conversions.h" |
17 #include "base/strings/string_util.h" | 18 #include "base/strings/string_util.h" |
18 #include "base/strings/stringprintf.h" | 19 #include "base/strings/stringprintf.h" |
19 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
20 | 21 |
21 namespace base { | 22 namespace base { |
22 | 23 |
23 namespace { | 24 namespace { |
24 | 25 |
25 // Define a separator character to use when creating a persistent form of an | 26 // Define a separator character to use when creating a persistent form of an |
26 // instance. This is intended for use as a command line argument, passed to a | 27 // instance. This is intended for use as a command line argument, passed to a |
27 // second process to mimic our state (i.e., provide the same group name). | 28 // second process to mimic our state (i.e., provide the same group name). |
28 const char kPersistentStringSeparator = '/'; // Currently a slash. | 29 const char kPersistentStringSeparator = '/'; // Currently a slash. |
29 | 30 |
30 // Define a marker character to be used as a prefix to a trial name on the | 31 // Define a marker character to be used as a prefix to a trial name on the |
31 // command line which forces its activation. | 32 // command line which forces its activation. |
32 const char kActivationMarker = '*'; | 33 const char kActivationMarker = '*'; |
33 | 34 |
34 // Use shared memory to communicate field trial (experiment) state. Set to false | 35 // Use shared memory to communicate field trial (experiment) state. Set to false |
35 // for now while the implementation is fleshed out (e.g. data format, single | 36 // for now while the implementation is fleshed out (e.g. data format, single |
36 // shared memory segment). See https://codereview.chromium.org/2365273004/ and | 37 // shared memory segment). See https://codereview.chromium.org/2365273004/ and |
37 // crbug.com/653874 | 38 // crbug.com/653874 |
38 #if defined(OS_WIN) | |
39 const bool kUseSharedMemoryForFieldTrials = false; | 39 const bool kUseSharedMemoryForFieldTrials = false; |
40 #endif | 40 |
41 // Constants for the field trial allocator. | |
42 const char kAllocatorName[] = "FieldTrialAllocator"; | |
43 const uint32_t kFieldTrialType = 0xABA17E13 + 1; // SHA1(FieldTrialEntry) v1 | |
44 | |
45 // We create one FieldTrialEntry struct per field trial in shared memory (via | |
46 // field_trial_allocator_). It contains whether or not it's activated, and the | |
47 // offset from the start of the allocated memory to the group name. We don't | |
48 // need the offset into the trial name because that's always a fixed position | |
49 // from the start of the struct. Two strings will be appended to the end of this | |
50 // structure in allocated memory, so the result will look like this: | |
51 // --------------------------------- | |
52 // | fte | trial_name | group_name | | |
53 // --------------------------------- | |
54 // TODO(lawrencewu): Actually update the activated flag. | |
55 struct FieldTrialEntry { | |
56 bool activated; | |
57 uint32_t group_name_offset; | |
58 | |
59 const char* GetTrialName() const { | |
60 const char* src = | |
61 reinterpret_cast<char*>(const_cast<FieldTrialEntry*>(this)); | |
62 return src + sizeof(FieldTrialEntry); | |
63 } | |
64 | |
65 const char* GetGroupName() const { | |
66 const char* src = | |
67 reinterpret_cast<char*>(const_cast<FieldTrialEntry*>(this)); | |
68 return src + this->group_name_offset; | |
69 } | |
70 }; | |
41 | 71 |
42 // Created a time value based on |year|, |month| and |day_of_month| parameters. | 72 // Created a time value based on |year|, |month| and |day_of_month| parameters. |
43 Time CreateTimeFromParams(int year, int month, int day_of_month) { | 73 Time CreateTimeFromParams(int year, int month, int day_of_month) { |
44 DCHECK_GT(year, 1970); | 74 DCHECK_GT(year, 1970); |
45 DCHECK_GT(month, 0); | 75 DCHECK_GT(month, 0); |
46 DCHECK_LT(month, 13); | 76 DCHECK_LT(month, 13); |
47 DCHECK_GT(day_of_month, 0); | 77 DCHECK_GT(day_of_month, 0); |
48 DCHECK_LT(day_of_month, 32); | 78 DCHECK_LT(day_of_month, 32); |
49 | 79 |
50 Time::Exploded exploded; | 80 Time::Exploded exploded; |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
117 trials_string_piece.substr(next_item, name_end - next_item); | 147 trials_string_piece.substr(next_item, name_end - next_item); |
118 entry.group_name = | 148 entry.group_name = |
119 trials_string_piece.substr(name_end + 1, group_name_end - name_end - 1); | 149 trials_string_piece.substr(name_end + 1, group_name_end - name_end - 1); |
120 next_item = group_name_end + 1; | 150 next_item = group_name_end + 1; |
121 | 151 |
122 entries->push_back(entry); | 152 entries->push_back(entry); |
123 } | 153 } |
124 return true; | 154 return true; |
125 } | 155 } |
126 | 156 |
157 void AddForceFieldTrialsFlag(base::CommandLine* cmd_line) { | |
158 std::string field_trial_states; | |
159 FieldTrialList::AllStatesToString(&field_trial_states); | |
160 if (!field_trial_states.empty()) { | |
161 cmd_line->AppendSwitchASCII(switches::kForceFieldTrials, | |
162 field_trial_states); | |
163 } | |
164 } | |
165 | |
166 #if defined(OS_WIN) | |
167 HANDLE CreateReadOnlyHandle(base::SharedPersistentMemoryAllocator* allocator) { | |
168 HANDLE src = allocator->shared_memory()->handle().GetHandle(); | |
169 ProcessHandle process = GetCurrentProcess(); | |
170 DWORD access = SECTION_MAP_READ | SECTION_QUERY; | |
171 HANDLE dst; | |
172 if (!::DuplicateHandle(process, src, process, &dst, access, true, 0)) | |
173 return nullptr; | |
174 return dst; | |
175 } | |
176 #endif | |
177 | |
127 } // namespace | 178 } // namespace |
128 | 179 |
129 // statics | 180 // statics |
130 const int FieldTrial::kNotFinalized = -1; | 181 const int FieldTrial::kNotFinalized = -1; |
131 const int FieldTrial::kDefaultGroupNumber = 0; | 182 const int FieldTrial::kDefaultGroupNumber = 0; |
183 const int kFieldTrialAllocationSize = 4 << 10; // 4 KiB = one page | |
132 bool FieldTrial::enable_benchmarking_ = false; | 184 bool FieldTrial::enable_benchmarking_ = false; |
133 | 185 |
134 int FieldTrialList::kNoExpirationYear = 0; | 186 int FieldTrialList::kNoExpirationYear = 0; |
135 | 187 |
136 //------------------------------------------------------------------------------ | 188 //------------------------------------------------------------------------------ |
137 // FieldTrial methods and members. | 189 // FieldTrial methods and members. |
138 | 190 |
139 FieldTrial::EntropyProvider::~EntropyProvider() { | 191 FieldTrial::EntropyProvider::~EntropyProvider() { |
140 } | 192 } |
141 | 193 |
(...skipping 437 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
579 std::string arg = cmd_line.GetSwitchValueASCII(field_trial_handle_switch); | 631 std::string arg = cmd_line.GetSwitchValueASCII(field_trial_handle_switch); |
580 size_t token = arg.find(","); | 632 size_t token = arg.find(","); |
581 int field_trial_handle = std::stoi(arg.substr(0, token)); | 633 int field_trial_handle = std::stoi(arg.substr(0, token)); |
582 int field_trial_length = std::stoi(arg.substr(token + 1, arg.length())); | 634 int field_trial_length = std::stoi(arg.substr(token + 1, arg.length())); |
583 | 635 |
584 HANDLE handle = reinterpret_cast<HANDLE>(field_trial_handle); | 636 HANDLE handle = reinterpret_cast<HANDLE>(field_trial_handle); |
585 base::SharedMemoryHandle shm_handle = | 637 base::SharedMemoryHandle shm_handle = |
586 base::SharedMemoryHandle(handle, base::GetCurrentProcId()); | 638 base::SharedMemoryHandle(handle, base::GetCurrentProcId()); |
587 | 639 |
588 // Gets deleted when it gets out of scope, but that's OK because we need it | 640 // Gets deleted when it gets out of scope, but that's OK because we need it |
589 // only for the duration of this call currently anyway. | 641 // only for the duration of this method. |
590 base::SharedMemory shared_memory(shm_handle, false); | 642 std::unique_ptr<base::SharedMemory> shm( |
591 shared_memory.Map(field_trial_length); | 643 new base::SharedMemory(shm_handle, true)); |
644 if (!shm.get()->Map(field_trial_length)) | |
645 base::TerminateBecauseOutOfMemory(field_trial_length); | |
592 | 646 |
593 char* field_trial_state = static_cast<char*>(shared_memory.memory()); | 647 FieldTrialList::CreateTrialsFromSharedMemory(std::move(shm)); |
594 bool result = FieldTrialList::CreateTrialsFromString( | |
595 std::string(field_trial_state), std::set<std::string>()); | |
596 DCHECK(result); | |
597 return; | 648 return; |
598 } | 649 } |
599 #endif | 650 #endif |
600 | 651 |
601 if (cmd_line.HasSwitch(switches::kForceFieldTrials)) { | 652 if (cmd_line.HasSwitch(switches::kForceFieldTrials)) { |
602 bool result = FieldTrialList::CreateTrialsFromString( | 653 bool result = FieldTrialList::CreateTrialsFromString( |
603 cmd_line.GetSwitchValueASCII(switches::kForceFieldTrials), | 654 cmd_line.GetSwitchValueASCII(switches::kForceFieldTrials), |
604 std::set<std::string>()); | 655 std::set<std::string>()); |
605 DCHECK(result); | 656 DCHECK(result); |
606 } | 657 } |
607 } | 658 } |
608 | 659 |
660 #if defined(OS_WIN) | |
661 // static | |
662 void FieldTrialList::AppendFieldTrialHandleIfNeeded( | |
663 base::HandlesToInheritVector* handles) { | |
664 FieldTrialList::InstantiateFieldTrialAllocatorIfNeeded(); | |
665 if (global_->readonly_allocator_handle_) | |
666 handles->push_back(global_->readonly_allocator_handle_); | |
667 } | |
668 #endif | |
669 | |
670 // static | |
671 void FieldTrialList::CreateTrialsFromSharedMemory( | |
672 std::unique_ptr<base::SharedMemory> shm) { | |
673 const base::SharedPersistentMemoryAllocator shalloc(std::move(shm), 0, | |
674 kAllocatorName, true); | |
675 base::PersistentMemoryAllocator::Iterator iter(&shalloc); | |
676 | |
677 base::SharedPersistentMemoryAllocator::Reference ref; | |
678 while ((ref = iter.GetNextOfType(kFieldTrialType)) != | |
679 base::SharedPersistentMemoryAllocator::kReferenceNull) { | |
680 const FieldTrialEntry* entry = | |
681 shalloc.GetAsObject<const FieldTrialEntry>(ref, kFieldTrialType); | |
682 FieldTrial* trial = | |
683 CreateFieldTrial(entry->GetTrialName(), entry->GetGroupName()); | |
684 | |
685 if (entry->activated) { | |
686 // Call |group()| to mark the trial as "used" and notify observers, if | |
687 // any. This is useful to ensure that field trials created in child | |
688 // processes are properly reported in crash reports. | |
689 trial->group(); | |
690 } | |
691 } | |
692 } | |
693 | |
609 // static | 694 // static |
610 std::unique_ptr<base::SharedMemory> FieldTrialList::CopyFieldTrialStateToFlags( | 695 void FieldTrialList::CopyFieldTrialStateToFlags( |
611 const char* field_trial_handle_switch, | 696 const char* field_trial_handle_switch, |
612 base::CommandLine* cmd_line) { | 697 base::CommandLine* cmd_line) { |
613 std::string field_trial_states; | |
614 base::FieldTrialList::AllStatesToString(&field_trial_states); | |
615 if (!field_trial_states.empty()) { | |
616 // Use shared memory to pass the state if the feature is enabled, otherwise | |
617 // fallback to passing it via the command line as a string. | |
618 #if defined(OS_WIN) | 698 #if defined(OS_WIN) |
619 if (kUseSharedMemoryForFieldTrials) { | 699 // Use shared memory to pass the state if the feature is enabled, otherwise |
620 std::unique_ptr<base::SharedMemory> shm(new base::SharedMemory()); | 700 // fallback to passing it via the command line as a string. |
621 size_t length = field_trial_states.size() + 1; | 701 if (kUseSharedMemoryForFieldTrials) { |
622 shm->CreateAndMapAnonymous(length); | 702 FieldTrialList::InstantiateFieldTrialAllocatorIfNeeded(); |
623 memcpy(shm->memory(), field_trial_states.c_str(), length); | 703 // If the readonly handle didn't get duplicated properly, then fallback to |
704 // original behavior. | |
705 if (!global_->readonly_allocator_handle_) { | |
706 AddForceFieldTrialsFlag(cmd_line); | |
707 return; | |
708 } | |
624 | 709 |
625 // HANDLE is just typedef'd to void * | 710 // HANDLE is just typedef'd to void *. We basically cast the handle into an |
626 auto uintptr_handle = | 711 // int (uintptr_t, to be exact), stringify the int, and pass it as a |
627 reinterpret_cast<std::uintptr_t>(shm->handle().GetHandle()); | 712 // command-line flag. The child process will do the reverse conversions to |
628 std::string field_trial_handle = | 713 // retrieve the handle. See http://stackoverflow.com/a/153077 |
629 std::to_string(uintptr_handle) + "," + std::to_string(length); | 714 auto uintptr_handle = |
715 reinterpret_cast<uintptr_t>(global_->readonly_allocator_handle_); | |
716 size_t field_trial_length = | |
717 global_->field_trial_allocator_->shared_memory()->mapped_size(); | |
718 std::string field_trial_handle = std::to_string(uintptr_handle) + "," + | |
719 std::to_string(field_trial_length); | |
630 | 720 |
631 cmd_line->AppendSwitchASCII(field_trial_handle_switch, | 721 cmd_line->AppendSwitchASCII(field_trial_handle_switch, field_trial_handle); |
632 field_trial_handle); | 722 return; |
633 return shm; | 723 } |
634 } | |
635 #endif | 724 #endif |
636 cmd_line->AppendSwitchASCII(switches::kForceFieldTrials, | 725 |
637 field_trial_states); | 726 AddForceFieldTrialsFlag(cmd_line); |
638 } | |
639 return std::unique_ptr<base::SharedMemory>(nullptr); | |
640 } | 727 } |
641 | 728 |
642 // static | 729 // static |
643 FieldTrial* FieldTrialList::CreateFieldTrial( | 730 FieldTrial* FieldTrialList::CreateFieldTrial( |
644 const std::string& name, | 731 const std::string& name, |
645 const std::string& group_name) { | 732 const std::string& group_name) { |
646 DCHECK(global_); | 733 DCHECK(global_); |
647 DCHECK_GE(name.size(), 0u); | 734 DCHECK_GE(name.size(), 0u); |
648 DCHECK_GE(group_name.size(), 0u); | 735 DCHECK_GE(group_name.size(), 0u); |
649 if (name.empty() || group_name.empty() || !global_) | 736 if (name.empty() || group_name.empty() || !global_) |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
682 // static | 769 // static |
683 void FieldTrialList::NotifyFieldTrialGroupSelection(FieldTrial* field_trial) { | 770 void FieldTrialList::NotifyFieldTrialGroupSelection(FieldTrial* field_trial) { |
684 if (!global_) | 771 if (!global_) |
685 return; | 772 return; |
686 | 773 |
687 { | 774 { |
688 AutoLock auto_lock(global_->lock_); | 775 AutoLock auto_lock(global_->lock_); |
689 if (field_trial->group_reported_) | 776 if (field_trial->group_reported_) |
690 return; | 777 return; |
691 field_trial->group_reported_ = true; | 778 field_trial->group_reported_ = true; |
779 | |
780 if (!field_trial->enable_field_trial_) | |
781 return; | |
782 | |
783 if (kUseSharedMemoryForFieldTrials) { | |
784 field_trial->AddToAllocatorWhileLocked( | |
785 global_->field_trial_allocator_.get()); | |
786 } | |
692 } | 787 } |
693 | 788 |
694 if (!field_trial->enable_field_trial_) | |
695 return; | |
696 | |
697 global_->observer_list_->Notify( | 789 global_->observer_list_->Notify( |
698 FROM_HERE, &FieldTrialList::Observer::OnFieldTrialGroupFinalized, | 790 FROM_HERE, &FieldTrialList::Observer::OnFieldTrialGroupFinalized, |
699 field_trial->trial_name(), field_trial->group_name_internal()); | 791 field_trial->trial_name(), field_trial->group_name_internal()); |
700 } | 792 } |
701 | 793 |
702 // static | 794 // static |
795 void FieldTrialList::InstantiateFieldTrialAllocatorIfNeeded() { | |
796 AutoLock auto_lock(global_->lock_); | |
797 // Create the allocator if not already created and add all existing trials. | |
798 if (global_->field_trial_allocator_ != nullptr) | |
799 return; | |
800 | |
801 std::unique_ptr<base::SharedMemory> shm(new base::SharedMemory()); | |
802 if (!shm->CreateAndMapAnonymous(kFieldTrialAllocationSize)) | |
803 base::TerminateBecauseOutOfMemory(kFieldTrialAllocationSize); | |
804 | |
805 // TODO(lawrencewu): call UpdateTrackingHistograms() when all field trials | |
806 // have been registered (perhaps in the destructor?) | |
807 global_->field_trial_allocator_.reset( | |
808 new base::SharedPersistentMemoryAllocator(std::move(shm), 0, | |
809 kAllocatorName, false)); | |
810 global_->field_trial_allocator_->CreateTrackingHistograms(kAllocatorName); | |
811 | |
812 // Add all existing field trials. | |
813 for (const auto& registered : global_->registered_) { | |
814 registered.second->AddToAllocatorWhileLocked( | |
815 global_->field_trial_allocator_.get()); | |
816 } | |
817 | |
818 #if defined(OS_WIN) | |
819 // Set |readonly_allocator_handle_| so we can pass it to be inherited and via | |
820 // the command line. | |
821 global_->readonly_allocator_handle_ = | |
822 CreateReadOnlyHandle(global_->field_trial_allocator_.get()); | |
823 #endif | |
824 } | |
825 | |
826 void FieldTrial::AddToAllocatorWhileLocked( | |
827 base::SharedPersistentMemoryAllocator* allocator) { | |
828 // Don't do anything if the allocator hasn't been instantiated yet. | |
829 if (allocator == nullptr) | |
830 return; | |
831 | |
832 // We allocate just enough memory to fit the FieldTrialEntry struct, trial | |
833 // name, and group name. If there's no more memory available or we couldn't | |
834 // get the field trial state, quit early. | |
Alexei Svitkine (slow)
2016/10/24 15:41:56
Nit: Seems like the first part of the comment (the
lawrencewu
2016/10/24 15:51:19
Moved the comment down and omitted the second part
| |
835 State trial_state; | |
836 if (!GetState(&trial_state)) | |
837 return; | |
838 | |
839 size_t trial_name_size = trial_state.trial_name.size() + 1; | |
840 size_t group_name_size = trial_state.group_name.size() + 1; | |
841 size_t size = sizeof(FieldTrialEntry) + trial_name_size + group_name_size; | |
842 | |
843 base::SharedPersistentMemoryAllocator::Reference ref = | |
844 allocator->Allocate(size, kFieldTrialType); | |
845 if (ref == base::SharedPersistentMemoryAllocator::kReferenceNull) | |
846 return; | |
847 | |
848 // Calculate offsets into the shared memory segment. | |
849 FieldTrialEntry* entry = | |
850 allocator->GetAsObject<FieldTrialEntry>(ref, kFieldTrialType); | |
851 uint32_t trial_name_offset = sizeof(FieldTrialEntry); | |
852 uint32_t group_name_offset = trial_name_offset + trial_name_size; | |
853 | |
854 // Copy over the data. | |
855 entry->activated = trial_state.activated; | |
856 entry->group_name_offset = group_name_offset; | |
857 char* trial_name = reinterpret_cast<char*>(entry) + trial_name_offset; | |
858 char* group_name = reinterpret_cast<char*>(entry) + group_name_offset; | |
859 memcpy(trial_name, trial_state.trial_name.data(), trial_name_size); | |
860 memcpy(group_name, trial_state.group_name.data(), group_name_size); | |
861 | |
862 // Set the null terminator, just in case. | |
Alexei Svitkine (slow)
2016/10/24 15:41:57
Nit: How about "Null terminate the strings."
It's
lawrencewu
2016/10/24 15:51:19
Done.
| |
863 char* end_of_trial_name = trial_name + trial_state.trial_name.size(); | |
864 char* end_of_group_name = group_name + trial_state.group_name.size(); | |
865 *end_of_trial_name = '\0'; | |
Alexei Svitkine (slow)
2016/10/24 15:41:57
Nit: How about:
trial_name[trial_state.trial_name
lawrencewu
2016/10/24 15:51:19
Done.
| |
866 *end_of_group_name = '\0'; | |
867 | |
868 allocator->MakeIterable(ref); | |
869 } | |
870 | |
871 // static | |
703 size_t FieldTrialList::GetFieldTrialCount() { | 872 size_t FieldTrialList::GetFieldTrialCount() { |
704 if (!global_) | 873 if (!global_) |
705 return 0; | 874 return 0; |
706 AutoLock auto_lock(global_->lock_); | 875 AutoLock auto_lock(global_->lock_); |
707 return global_->registered_.size(); | 876 return global_->registered_.size(); |
708 } | 877 } |
709 | 878 |
710 // static | 879 // static |
711 const FieldTrial::EntropyProvider* | 880 const FieldTrial::EntropyProvider* |
712 FieldTrialList::GetEntropyProviderForOneTimeRandomization() { | 881 FieldTrialList::GetEntropyProviderForOneTimeRandomization() { |
(...skipping 19 matching lines...) Expand all Loading... | |
732 return; | 901 return; |
733 } | 902 } |
734 AutoLock auto_lock(global_->lock_); | 903 AutoLock auto_lock(global_->lock_); |
735 CHECK(!global_->PreLockedFind(trial->trial_name())) << trial->trial_name(); | 904 CHECK(!global_->PreLockedFind(trial->trial_name())) << trial->trial_name(); |
736 trial->AddRef(); | 905 trial->AddRef(); |
737 trial->SetTrialRegistered(); | 906 trial->SetTrialRegistered(); |
738 global_->registered_[trial->trial_name()] = trial; | 907 global_->registered_[trial->trial_name()] = trial; |
739 } | 908 } |
740 | 909 |
741 } // namespace base | 910 } // namespace base |
OLD | NEW |