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 size_t 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 430 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
572 void FieldTrialList::CreateTrialsFromCommandLine( | 624 void FieldTrialList::CreateTrialsFromCommandLine( |
573 const base::CommandLine& cmd_line, | 625 const base::CommandLine& cmd_line, |
574 const char* field_trial_handle_switch) { | 626 const char* field_trial_handle_switch) { |
575 DCHECK(global_); | 627 DCHECK(global_); |
576 | 628 |
577 #if defined(OS_WIN) | 629 #if defined(OS_WIN) |
578 if (cmd_line.HasSwitch(field_trial_handle_switch)) { | 630 if (cmd_line.HasSwitch(field_trial_handle_switch)) { |
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 size_t 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) { | |
Alexei Svitkine (slow)
2016/10/24 19:57:01
Nit: Remove all the base:: prefixes throughout sin
lawrencewu
2016/10/24 21:17:16
Done.
| |
664 if (!global_) | |
665 return; | |
666 if (kUseSharedMemoryForFieldTrials) { | |
667 FieldTrialList::InstantiateFieldTrialAllocatorIfNeeded(); | |
Alexei Svitkine (slow)
2016/10/24 19:57:01
Nit: Remove FieldTrialList::
lawrencewu
2016/10/24 21:17:16
Done.
| |
668 if (global_->readonly_allocator_handle_) | |
669 handles->push_back(global_->readonly_allocator_handle_); | |
670 } | |
671 } | |
672 #endif | |
673 | |
674 // static | |
675 void FieldTrialList::CreateTrialsFromSharedMemory( | |
676 std::unique_ptr<base::SharedMemory> shm) { | |
677 const base::SharedPersistentMemoryAllocator shalloc(std::move(shm), 0, | |
678 kAllocatorName, true); | |
679 base::PersistentMemoryAllocator::Iterator iter(&shalloc); | |
680 | |
681 base::SharedPersistentMemoryAllocator::Reference ref; | |
682 while ((ref = iter.GetNextOfType(kFieldTrialType)) != | |
683 base::SharedPersistentMemoryAllocator::kReferenceNull) { | |
684 const FieldTrialEntry* entry = | |
685 shalloc.GetAsObject<const FieldTrialEntry>(ref, kFieldTrialType); | |
686 FieldTrial* trial = | |
687 CreateFieldTrial(entry->GetTrialName(), entry->GetGroupName()); | |
688 | |
689 if (entry->activated) { | |
690 // Call |group()| to mark the trial as "used" and notify observers, if | |
691 // any. This is useful to ensure that field trials created in child | |
692 // processes are properly reported in crash reports. | |
693 trial->group(); | |
694 } | |
695 } | |
696 } | |
697 | |
609 // static | 698 // static |
610 std::unique_ptr<base::SharedMemory> FieldTrialList::CopyFieldTrialStateToFlags( | 699 void FieldTrialList::CopyFieldTrialStateToFlags( |
611 const char* field_trial_handle_switch, | 700 const char* field_trial_handle_switch, |
612 base::CommandLine* cmd_line) { | 701 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) | 702 #if defined(OS_WIN) |
619 if (kUseSharedMemoryForFieldTrials) { | 703 // Use shared memory to pass the state if the feature is enabled, otherwise |
620 std::unique_ptr<base::SharedMemory> shm(new base::SharedMemory()); | 704 // fallback to passing it via the command line as a string. |
621 size_t length = field_trial_states.size() + 1; | 705 if (kUseSharedMemoryForFieldTrials) { |
622 shm->CreateAndMapAnonymous(length); | 706 FieldTrialList::InstantiateFieldTrialAllocatorIfNeeded(); |
Alexei Svitkine (slow)
2016/10/24 19:57:01
Nit: Remove FieldTrialList::
lawrencewu
2016/10/24 21:17:16
Done.
| |
623 memcpy(shm->memory(), field_trial_states.c_str(), length); | 707 // If the readonly handle didn't get duplicated properly, then fallback to |
708 // original behavior. | |
709 if (!global_->readonly_allocator_handle_) { | |
710 AddForceFieldTrialsFlag(cmd_line); | |
711 return; | |
712 } | |
624 | 713 |
625 // HANDLE is just typedef'd to void * | 714 // HANDLE is just typedef'd to void *. We basically cast the handle into an |
626 auto uintptr_handle = | 715 // int (uintptr_t, to be exact), stringify the int, and pass it as a |
627 reinterpret_cast<std::uintptr_t>(shm->handle().GetHandle()); | 716 // command-line flag. The child process will do the reverse conversions to |
628 std::string field_trial_handle = | 717 // retrieve the handle. See http://stackoverflow.com/a/153077 |
629 std::to_string(uintptr_handle) + "," + std::to_string(length); | 718 auto uintptr_handle = |
719 reinterpret_cast<uintptr_t>(global_->readonly_allocator_handle_); | |
720 size_t field_trial_length = | |
721 global_->field_trial_allocator_->shared_memory()->mapped_size(); | |
722 std::string field_trial_handle = std::to_string(uintptr_handle) + "," + | |
723 std::to_string(field_trial_length); | |
630 | 724 |
631 cmd_line->AppendSwitchASCII(field_trial_handle_switch, | 725 cmd_line->AppendSwitchASCII(field_trial_handle_switch, field_trial_handle); |
632 field_trial_handle); | 726 return; |
633 return shm; | 727 } |
634 } | |
635 #endif | 728 #endif |
636 cmd_line->AppendSwitchASCII(switches::kForceFieldTrials, | 729 |
637 field_trial_states); | 730 AddForceFieldTrialsFlag(cmd_line); |
638 } | |
639 return std::unique_ptr<base::SharedMemory>(nullptr); | |
640 } | 731 } |
641 | 732 |
642 // static | 733 // static |
643 FieldTrial* FieldTrialList::CreateFieldTrial( | 734 FieldTrial* FieldTrialList::CreateFieldTrial( |
644 const std::string& name, | 735 const std::string& name, |
645 const std::string& group_name) { | 736 const std::string& group_name) { |
646 DCHECK(global_); | 737 DCHECK(global_); |
647 DCHECK_GE(name.size(), 0u); | 738 DCHECK_GE(name.size(), 0u); |
648 DCHECK_GE(group_name.size(), 0u); | 739 DCHECK_GE(group_name.size(), 0u); |
649 if (name.empty() || group_name.empty() || !global_) | 740 if (name.empty() || group_name.empty() || !global_) |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
682 // static | 773 // static |
683 void FieldTrialList::NotifyFieldTrialGroupSelection(FieldTrial* field_trial) { | 774 void FieldTrialList::NotifyFieldTrialGroupSelection(FieldTrial* field_trial) { |
684 if (!global_) | 775 if (!global_) |
685 return; | 776 return; |
686 | 777 |
687 { | 778 { |
688 AutoLock auto_lock(global_->lock_); | 779 AutoLock auto_lock(global_->lock_); |
689 if (field_trial->group_reported_) | 780 if (field_trial->group_reported_) |
690 return; | 781 return; |
691 field_trial->group_reported_ = true; | 782 field_trial->group_reported_ = true; |
783 | |
784 if (!field_trial->enable_field_trial_) | |
785 return; | |
786 | |
787 if (kUseSharedMemoryForFieldTrials) { | |
788 field_trial->AddToAllocatorWhileLocked( | |
789 global_->field_trial_allocator_.get()); | |
790 } | |
692 } | 791 } |
693 | 792 |
694 if (!field_trial->enable_field_trial_) | |
695 return; | |
696 | |
697 global_->observer_list_->Notify( | 793 global_->observer_list_->Notify( |
698 FROM_HERE, &FieldTrialList::Observer::OnFieldTrialGroupFinalized, | 794 FROM_HERE, &FieldTrialList::Observer::OnFieldTrialGroupFinalized, |
699 field_trial->trial_name(), field_trial->group_name_internal()); | 795 field_trial->trial_name(), field_trial->group_name_internal()); |
700 } | 796 } |
701 | 797 |
702 // static | 798 // static |
799 void FieldTrialList::InstantiateFieldTrialAllocatorIfNeeded() { | |
800 AutoLock auto_lock(global_->lock_); | |
801 // Create the allocator if not already created and add all existing trials. | |
802 if (global_->field_trial_allocator_ != nullptr) | |
803 return; | |
804 | |
805 std::unique_ptr<base::SharedMemory> shm(new base::SharedMemory()); | |
806 if (!shm->CreateAndMapAnonymous(kFieldTrialAllocationSize)) | |
807 base::TerminateBecauseOutOfMemory(kFieldTrialAllocationSize); | |
808 | |
809 // TODO(lawrencewu): call UpdateTrackingHistograms() when all field trials | |
810 // have been registered (perhaps in the destructor?) | |
811 global_->field_trial_allocator_.reset( | |
812 new base::SharedPersistentMemoryAllocator(std::move(shm), 0, | |
813 kAllocatorName, false)); | |
814 global_->field_trial_allocator_->CreateTrackingHistograms(kAllocatorName); | |
815 | |
816 // Add all existing field trials. | |
817 for (const auto& registered : global_->registered_) { | |
818 registered.second->AddToAllocatorWhileLocked( | |
819 global_->field_trial_allocator_.get()); | |
820 } | |
821 | |
822 #if defined(OS_WIN) | |
823 // Set |readonly_allocator_handle_| so we can pass it to be inherited and via | |
824 // the command line. | |
825 global_->readonly_allocator_handle_ = | |
826 CreateReadOnlyHandle(global_->field_trial_allocator_.get()); | |
827 #endif | |
828 } | |
829 | |
830 void FieldTrial::AddToAllocatorWhileLocked( | |
831 base::SharedPersistentMemoryAllocator* allocator) { | |
832 // Don't do anything if the allocator hasn't been instantiated yet. | |
833 if (allocator == nullptr) | |
834 return; | |
835 | |
836 State trial_state; | |
837 if (!GetState(&trial_state)) | |
838 return; | |
839 | |
840 size_t trial_name_size = trial_state.trial_name.size() + 1; | |
841 size_t group_name_size = trial_state.group_name.size() + 1; | |
842 size_t size = sizeof(FieldTrialEntry) + trial_name_size + group_name_size; | |
843 | |
844 // Allocate just enough memory to fit the FieldTrialEntry struct, trial name, | |
845 // and group name. | |
846 base::SharedPersistentMemoryAllocator::Reference ref = | |
847 allocator->Allocate(size, kFieldTrialType); | |
848 if (ref == base::SharedPersistentMemoryAllocator::kReferenceNull) | |
849 return; | |
850 | |
851 // Calculate offsets into the shared memory segment. | |
852 FieldTrialEntry* entry = | |
853 allocator->GetAsObject<FieldTrialEntry>(ref, kFieldTrialType); | |
854 uint32_t trial_name_offset = sizeof(FieldTrialEntry); | |
855 uint32_t group_name_offset = trial_name_offset + trial_name_size; | |
856 | |
857 // Copy over the data. | |
858 entry->activated = trial_state.activated; | |
859 entry->group_name_offset = group_name_offset; | |
860 char* trial_name = reinterpret_cast<char*>(entry) + trial_name_offset; | |
861 char* group_name = reinterpret_cast<char*>(entry) + group_name_offset; | |
862 memcpy(trial_name, trial_state.trial_name.data(), trial_name_size); | |
863 memcpy(group_name, trial_state.group_name.data(), group_name_size); | |
864 | |
865 // Null terminate the strings. | |
866 trial_name[trial_state.trial_name.size()] = '\0'; | |
867 group_name[trial_state.group_name.size()] = '\0'; | |
868 | |
869 allocator->MakeIterable(ref); | |
870 } | |
871 | |
872 // static | |
703 size_t FieldTrialList::GetFieldTrialCount() { | 873 size_t FieldTrialList::GetFieldTrialCount() { |
704 if (!global_) | 874 if (!global_) |
705 return 0; | 875 return 0; |
706 AutoLock auto_lock(global_->lock_); | 876 AutoLock auto_lock(global_->lock_); |
707 return global_->registered_.size(); | 877 return global_->registered_.size(); |
708 } | 878 } |
709 | 879 |
710 // static | 880 // static |
711 const FieldTrial::EntropyProvider* | 881 const FieldTrial::EntropyProvider* |
712 FieldTrialList::GetEntropyProviderForOneTimeRandomization() { | 882 FieldTrialList::GetEntropyProviderForOneTimeRandomization() { |
(...skipping 19 matching lines...) Expand all Loading... | |
732 return; | 902 return; |
733 } | 903 } |
734 AutoLock auto_lock(global_->lock_); | 904 AutoLock auto_lock(global_->lock_); |
735 CHECK(!global_->PreLockedFind(trial->trial_name())) << trial->trial_name(); | 905 CHECK(!global_->PreLockedFind(trial->trial_name())) << trial->trial_name(); |
736 trial->AddRef(); | 906 trial->AddRef(); |
737 trial->SetTrialRegistered(); | 907 trial->SetTrialRegistered(); |
738 global_->registered_[trial->trial_name()] = trial; | 908 global_->registered_[trial->trial_name()] = trial; |
739 } | 909 } |
740 | 910 |
741 } // namespace base | 911 } // namespace base |
OLD | NEW |