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 struct FieldTrialEntry { | |
55 bool activated; | |
Alexei Svitkine (slow)
2016/10/24 14:40:27
I don't think your current code updates this when
lawrencewu
2016/10/24 15:35:13
I added a comment and will tackle this in the next
| |
56 uint32_t group_name_offset; | |
57 | |
58 const char* GetTrialName() const { | |
59 const char* src = | |
60 reinterpret_cast<char*>(const_cast<FieldTrialEntry*>(this)); | |
61 return src + sizeof(FieldTrialEntry); | |
62 } | |
63 | |
64 const char* GetGroupName() const { | |
65 const char* src = | |
66 reinterpret_cast<char*>(const_cast<FieldTrialEntry*>(this)); | |
67 return src + this->group_name_offset; | |
68 } | |
69 }; | |
41 | 70 |
42 // Created a time value based on |year|, |month| and |day_of_month| parameters. | 71 // Created a time value based on |year|, |month| and |day_of_month| parameters. |
43 Time CreateTimeFromParams(int year, int month, int day_of_month) { | 72 Time CreateTimeFromParams(int year, int month, int day_of_month) { |
44 DCHECK_GT(year, 1970); | 73 DCHECK_GT(year, 1970); |
45 DCHECK_GT(month, 0); | 74 DCHECK_GT(month, 0); |
46 DCHECK_LT(month, 13); | 75 DCHECK_LT(month, 13); |
47 DCHECK_GT(day_of_month, 0); | 76 DCHECK_GT(day_of_month, 0); |
48 DCHECK_LT(day_of_month, 32); | 77 DCHECK_LT(day_of_month, 32); |
49 | 78 |
50 Time::Exploded exploded; | 79 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); | 146 trials_string_piece.substr(next_item, name_end - next_item); |
118 entry.group_name = | 147 entry.group_name = |
119 trials_string_piece.substr(name_end + 1, group_name_end - name_end - 1); | 148 trials_string_piece.substr(name_end + 1, group_name_end - name_end - 1); |
120 next_item = group_name_end + 1; | 149 next_item = group_name_end + 1; |
121 | 150 |
122 entries->push_back(entry); | 151 entries->push_back(entry); |
123 } | 152 } |
124 return true; | 153 return true; |
125 } | 154 } |
126 | 155 |
156 void AddForceFieldTrialsFlag(base::CommandLine* cmd_line) { | |
157 std::string field_trial_states; | |
158 FieldTrialList::AllStatesToString(&field_trial_states); | |
159 if (!field_trial_states.empty()) { | |
160 cmd_line->AppendSwitchASCII(switches::kForceFieldTrials, | |
161 field_trial_states); | |
162 } | |
163 } | |
164 | |
165 #if defined(OS_WIN) | |
166 HANDLE CreateReadOnlyHandle(base::SharedPersistentMemoryAllocator* allocator) { | |
167 HANDLE src = allocator->shared_memory()->handle().GetHandle(); | |
168 ProcessHandle process = GetCurrentProcess(); | |
169 DWORD access = SECTION_MAP_READ | SECTION_QUERY; | |
170 HANDLE dst; | |
171 if (!::DuplicateHandle(process, src, process, &dst, access, true, 0)) | |
172 return NULL; | |
Alexei Svitkine (slow)
2016/10/24 14:40:27
Don't use NULL in new code.
Use nullptr - unless
lawrencewu
2016/10/24 15:35:13
Used nullptr and initialized it in the header file
| |
173 return dst; | |
174 } | |
175 #endif | |
176 | |
127 } // namespace | 177 } // namespace |
128 | 178 |
129 // statics | 179 // statics |
130 const int FieldTrial::kNotFinalized = -1; | 180 const int FieldTrial::kNotFinalized = -1; |
131 const int FieldTrial::kDefaultGroupNumber = 0; | 181 const int FieldTrial::kDefaultGroupNumber = 0; |
182 const int kFieldTrialAllocationSize = 4 << 10; // 4 KiB = one page | |
132 bool FieldTrial::enable_benchmarking_ = false; | 183 bool FieldTrial::enable_benchmarking_ = false; |
133 | 184 |
134 int FieldTrialList::kNoExpirationYear = 0; | 185 int FieldTrialList::kNoExpirationYear = 0; |
135 | 186 |
136 //------------------------------------------------------------------------------ | 187 //------------------------------------------------------------------------------ |
137 // FieldTrial methods and members. | 188 // FieldTrial methods and members. |
138 | 189 |
139 FieldTrial::EntropyProvider::~EntropyProvider() { | 190 FieldTrial::EntropyProvider::~EntropyProvider() { |
140 } | 191 } |
141 | 192 |
(...skipping 437 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
579 std::string arg = cmd_line.GetSwitchValueASCII(field_trial_handle_switch); | 630 std::string arg = cmd_line.GetSwitchValueASCII(field_trial_handle_switch); |
580 size_t token = arg.find(","); | 631 size_t token = arg.find(","); |
581 int field_trial_handle = std::stoi(arg.substr(0, token)); | 632 int field_trial_handle = std::stoi(arg.substr(0, token)); |
582 int field_trial_length = std::stoi(arg.substr(token + 1, arg.length())); | 633 int field_trial_length = std::stoi(arg.substr(token + 1, arg.length())); |
583 | 634 |
584 HANDLE handle = reinterpret_cast<HANDLE>(field_trial_handle); | 635 HANDLE handle = reinterpret_cast<HANDLE>(field_trial_handle); |
585 base::SharedMemoryHandle shm_handle = | 636 base::SharedMemoryHandle shm_handle = |
586 base::SharedMemoryHandle(handle, base::GetCurrentProcId()); | 637 base::SharedMemoryHandle(handle, base::GetCurrentProcId()); |
587 | 638 |
588 // Gets deleted when it gets out of scope, but that's OK because we need it | 639 // 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. | 640 // only for the duration of this method. |
590 base::SharedMemory shared_memory(shm_handle, false); | 641 std::unique_ptr<base::SharedMemory> shm( |
591 shared_memory.Map(field_trial_length); | 642 new base::SharedMemory(shm_handle, true)); |
643 if (!shm.get()->Map(field_trial_length)) | |
644 base::TerminateBecauseOutOfMemory(field_trial_length); | |
592 | 645 |
593 char* field_trial_state = static_cast<char*>(shared_memory.memory()); | 646 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; | 647 return; |
598 } | 648 } |
599 #endif | 649 #endif |
600 | 650 |
601 if (cmd_line.HasSwitch(switches::kForceFieldTrials)) { | 651 if (cmd_line.HasSwitch(switches::kForceFieldTrials)) { |
602 bool result = FieldTrialList::CreateTrialsFromString( | 652 bool result = FieldTrialList::CreateTrialsFromString( |
603 cmd_line.GetSwitchValueASCII(switches::kForceFieldTrials), | 653 cmd_line.GetSwitchValueASCII(switches::kForceFieldTrials), |
604 std::set<std::string>()); | 654 std::set<std::string>()); |
605 DCHECK(result); | 655 DCHECK(result); |
606 } | 656 } |
607 } | 657 } |
608 | 658 |
659 #if defined(OS_WIN) | |
660 // static | |
661 void FieldTrialList::AppendFieldTrialHandleIfNeeded( | |
662 base::HandlesToInheritVector* handles) { | |
663 handles->push_back(global_->readonly_allocator_handle_); | |
Alexei Svitkine (slow)
2016/10/24 14:40:27
You only want to do this if it's no null, right?
lawrencewu
2016/10/24 15:35:13
Yep, done.
| |
664 } | |
665 #endif | |
666 | |
667 // static | |
668 void FieldTrialList::CreateTrialsFromSharedMemory( | |
669 std::unique_ptr<base::SharedMemory> shm) { | |
670 const base::SharedPersistentMemoryAllocator shalloc(std::move(shm), 0, | |
671 kAllocatorName, true); | |
672 base::PersistentMemoryAllocator::Iterator iter(&shalloc); | |
673 | |
674 base::SharedPersistentMemoryAllocator::Reference ref; | |
675 while ((ref = iter.GetNextOfType(kFieldTrialType)) != | |
676 base::SharedPersistentMemoryAllocator::kReferenceNull) { | |
677 const FieldTrialEntry* entry = | |
678 shalloc.GetAsObject<const FieldTrialEntry>(ref, kFieldTrialType); | |
679 FieldTrial* trial = | |
680 CreateFieldTrial(entry->GetTrialName(), entry->GetGroupName()); | |
681 | |
682 if (entry->activated) { | |
683 // Call |group()| to mark the trial as "used" and notify observers, if | |
684 // any. This is useful to ensure that field trials created in child | |
685 // processes are properly reported in crash reports. | |
686 trial->group(); | |
687 } | |
688 } | |
689 } | |
690 | |
609 // static | 691 // static |
610 std::unique_ptr<base::SharedMemory> FieldTrialList::CopyFieldTrialStateToFlags( | 692 void FieldTrialList::CopyFieldTrialStateToFlags( |
611 const char* field_trial_handle_switch, | 693 const char* field_trial_handle_switch, |
612 base::CommandLine* cmd_line) { | 694 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) | 695 #if defined(OS_WIN) |
619 if (kUseSharedMemoryForFieldTrials) { | 696 // Use shared memory to pass the state if the feature is enabled, otherwise |
620 std::unique_ptr<base::SharedMemory> shm(new base::SharedMemory()); | 697 // fallback to passing it via the command line as a string. |
621 size_t length = field_trial_states.size() + 1; | 698 if (kUseSharedMemoryForFieldTrials) { |
622 shm->CreateAndMapAnonymous(length); | 699 FieldTrialList::InstantiateFieldTrialAllocatorIfNeeded(); |
623 memcpy(shm->memory(), field_trial_states.c_str(), length); | 700 // If the readonly handle didn't get duplicated properly, then fallback to |
701 // original behavior. | |
702 if (!global_->readonly_allocator_handle_) { | |
703 AddForceFieldTrialsFlag(cmd_line); | |
704 return; | |
705 } | |
624 | 706 |
625 // HANDLE is just typedef'd to void * | 707 // HANDLE is just typedef'd to void *. We basically cast the handle into an |
626 auto uintptr_handle = | 708 // int (uintptr_t, to be exact), stringify the int, and pass it as a |
627 reinterpret_cast<std::uintptr_t>(shm->handle().GetHandle()); | 709 // command-line flag. The child process will do the reverse conversions to |
628 std::string field_trial_handle = | 710 // retrieve the handle. See http://stackoverflow.com/a/153077 |
629 std::to_string(uintptr_handle) + "," + std::to_string(length); | 711 auto uintptr_handle = |
712 reinterpret_cast<uintptr_t>(global_->readonly_allocator_handle_); | |
713 size_t field_trial_length = | |
714 global_->field_trial_allocator_->shared_memory()->mapped_size(); | |
715 std::string field_trial_handle = std::to_string(uintptr_handle) + "," + | |
716 std::to_string(field_trial_length); | |
630 | 717 |
631 cmd_line->AppendSwitchASCII(field_trial_handle_switch, | 718 cmd_line->AppendSwitchASCII(field_trial_handle_switch, field_trial_handle); |
632 field_trial_handle); | 719 return; |
633 return shm; | 720 } |
634 } | |
635 #endif | 721 #endif |
636 cmd_line->AppendSwitchASCII(switches::kForceFieldTrials, | 722 |
637 field_trial_states); | 723 AddForceFieldTrialsFlag(cmd_line); |
638 } | |
639 return std::unique_ptr<base::SharedMemory>(nullptr); | |
640 } | 724 } |
641 | 725 |
642 // static | 726 // static |
643 FieldTrial* FieldTrialList::CreateFieldTrial( | 727 FieldTrial* FieldTrialList::CreateFieldTrial( |
644 const std::string& name, | 728 const std::string& name, |
645 const std::string& group_name) { | 729 const std::string& group_name) { |
646 DCHECK(global_); | 730 DCHECK(global_); |
647 DCHECK_GE(name.size(), 0u); | 731 DCHECK_GE(name.size(), 0u); |
648 DCHECK_GE(group_name.size(), 0u); | 732 DCHECK_GE(group_name.size(), 0u); |
649 if (name.empty() || group_name.empty() || !global_) | 733 if (name.empty() || group_name.empty() || !global_) |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
682 // static | 766 // static |
683 void FieldTrialList::NotifyFieldTrialGroupSelection(FieldTrial* field_trial) { | 767 void FieldTrialList::NotifyFieldTrialGroupSelection(FieldTrial* field_trial) { |
684 if (!global_) | 768 if (!global_) |
685 return; | 769 return; |
686 | 770 |
687 { | 771 { |
688 AutoLock auto_lock(global_->lock_); | 772 AutoLock auto_lock(global_->lock_); |
689 if (field_trial->group_reported_) | 773 if (field_trial->group_reported_) |
690 return; | 774 return; |
691 field_trial->group_reported_ = true; | 775 field_trial->group_reported_ = true; |
776 | |
777 if (!field_trial->enable_field_trial_) | |
778 return; | |
779 | |
780 if (kUseSharedMemoryForFieldTrials) | |
781 field_trial->AddToAllocatorWhileLocked( | |
782 global_->field_trial_allocator_.get()); | |
692 } | 783 } |
693 | 784 |
694 if (!field_trial->enable_field_trial_) | |
695 return; | |
696 | |
697 global_->observer_list_->Notify( | 785 global_->observer_list_->Notify( |
698 FROM_HERE, &FieldTrialList::Observer::OnFieldTrialGroupFinalized, | 786 FROM_HERE, &FieldTrialList::Observer::OnFieldTrialGroupFinalized, |
699 field_trial->trial_name(), field_trial->group_name_internal()); | 787 field_trial->trial_name(), field_trial->group_name_internal()); |
700 } | 788 } |
701 | 789 |
702 // static | 790 // static |
791 void FieldTrialList::InstantiateFieldTrialAllocatorIfNeeded() { | |
792 AutoLock auto_lock(global_->lock_); | |
793 // Create the allocator if not already created and add all existing trials. | |
794 if (global_->field_trial_allocator_ != nullptr) | |
795 return; | |
796 | |
797 std::unique_ptr<base::SharedMemory> shm(new base::SharedMemory()); | |
798 if (!shm->CreateAndMapAnonymous(kFieldTrialAllocationSize)) | |
799 base::TerminateBecauseOutOfMemory(kFieldTrialAllocationSize); | |
800 | |
801 // TODO(lawrencewu): call UpdateTrackingHistograms() when all field trials | |
802 // have been registered (perhaps in the destructor?) | |
803 global_->field_trial_allocator_.reset( | |
804 new base::SharedPersistentMemoryAllocator(std::move(shm), 0, | |
805 kAllocatorName, false)); | |
806 global_->field_trial_allocator_->CreateTrackingHistograms(kAllocatorName); | |
807 | |
808 // Add all existing field trials. | |
809 for (const auto& registered : global_->registered_) { | |
810 registered.second->AddToAllocatorWhileLocked( | |
811 global_->field_trial_allocator_.get()); | |
812 } | |
813 | |
814 #if defined(OS_WIN) | |
815 // Set |readonly_allocator_handle_| so we can pass it to be inherited and via | |
816 // the command line. | |
817 global_->readonly_allocator_handle_ = | |
818 CreateReadOnlyHandle(global_->field_trial_allocator_.get()); | |
819 #endif | |
820 } | |
821 | |
822 void FieldTrial::AddToAllocatorWhileLocked( | |
823 base::SharedPersistentMemoryAllocator* allocator) { | |
824 // Don't do anything if the allocator hasn't been instantiated yet. | |
825 if (allocator == nullptr) | |
826 return; | |
827 | |
828 // We allocate just enough memory to fit the FTE struct, trial name, and group | |
829 // name. If there's no more memory available, quit early. | |
830 State trial_state; | |
831 GetState(&trial_state); | |
Alexei Svitkine (slow)
2016/10/24 14:40:27
This function has a return value - if it's false,
lawrencewu
2016/10/24 15:35:13
Done.
| |
832 | |
833 size_t trial_name_size = trial_state.trial_name.size() + 1; | |
834 size_t group_name_size = trial_state.group_name.size() + 1; | |
835 | |
836 uint32_t trial_name_offset = sizeof(FieldTrialEntry); | |
837 uint32_t group_name_offset = trial_name_offset + trial_name_size; | |
838 size_t size = sizeof(FieldTrialEntry) + trial_name_size + group_name_size; | |
839 | |
840 base::SharedPersistentMemoryAllocator::Reference ref = | |
841 allocator->Allocate(size, kFieldTrialType); | |
842 if (ref == base::SharedPersistentMemoryAllocator::kReferenceNull) | |
843 return; | |
844 | |
845 FieldTrialEntry* entry = | |
846 allocator->GetAsObject<FieldTrialEntry>(ref, kFieldTrialType); | |
847 entry->activated = trial_state.activated; | |
848 entry->group_name_offset = group_name_offset; | |
849 memcpy(reinterpret_cast<char*>(entry) + trial_name_offset, | |
850 trial_state.trial_name.data(), trial_name_size); | |
851 memcpy(reinterpret_cast<char*>(entry) + group_name_offset, | |
852 trial_state.group_name.data(), group_name_size); | |
853 | |
854 allocator->MakeIterable(ref); | |
855 } | |
856 | |
857 // static | |
703 size_t FieldTrialList::GetFieldTrialCount() { | 858 size_t FieldTrialList::GetFieldTrialCount() { |
704 if (!global_) | 859 if (!global_) |
705 return 0; | 860 return 0; |
706 AutoLock auto_lock(global_->lock_); | 861 AutoLock auto_lock(global_->lock_); |
707 return global_->registered_.size(); | 862 return global_->registered_.size(); |
708 } | 863 } |
709 | 864 |
710 // static | 865 // static |
711 const FieldTrial::EntropyProvider* | 866 const FieldTrial::EntropyProvider* |
712 FieldTrialList::GetEntropyProviderForOneTimeRandomization() { | 867 FieldTrialList::GetEntropyProviderForOneTimeRandomization() { |
(...skipping 19 matching lines...) Expand all Loading... | |
732 return; | 887 return; |
733 } | 888 } |
734 AutoLock auto_lock(global_->lock_); | 889 AutoLock auto_lock(global_->lock_); |
735 CHECK(!global_->PreLockedFind(trial->trial_name())) << trial->trial_name(); | 890 CHECK(!global_->PreLockedFind(trial->trial_name())) << trial->trial_name(); |
736 trial->AddRef(); | 891 trial->AddRef(); |
737 trial->SetTrialRegistered(); | 892 trial->SetTrialRegistered(); |
738 global_->registered_[trial->trial_name()] = trial; | 893 global_->registered_[trial->trial_name()] = trial; |
739 } | 894 } |
740 | 895 |
741 } // namespace base | 896 } // namespace base |
OLD | NEW |