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" |
(...skipping 17 matching lines...) Expand all Loading... | |
28 const char kPersistentStringSeparator = '/'; // Currently a slash. | 28 const char kPersistentStringSeparator = '/'; // Currently a slash. |
29 | 29 |
30 // Define a marker character to be used as a prefix to a trial name on the | 30 // Define a marker character to be used as a prefix to a trial name on the |
31 // command line which forces its activation. | 31 // command line which forces its activation. |
32 const char kActivationMarker = '*'; | 32 const char kActivationMarker = '*'; |
33 | 33 |
34 // Use shared memory to communicate field trial (experiment) state. Set to false | 34 // 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 | 35 // for now while the implementation is fleshed out (e.g. data format, single |
36 // shared memory segment). See https://codereview.chromium.org/2365273004/ and | 36 // shared memory segment). See https://codereview.chromium.org/2365273004/ and |
37 // crbug.com/653874 | 37 // crbug.com/653874 |
38 #if defined(OS_WIN) | |
39 const bool kUseSharedMemoryForFieldTrials = false; | 38 const bool kUseSharedMemoryForFieldTrials = false; |
40 #endif | 39 |
40 // Constants for the field trial allocator. | |
41 const char kAllocatorName[] = "FieldTrialAllocator"; | |
42 const uint32_t kFieldTrialType = 0xABA17E13 + 1; // SHA1(FieldTrialEntry) v1 | |
43 | |
44 // We create one FieldTrialEntry struct per field trial in shared memory (via | |
45 // field_trial_allocator). It contains whether or not it's activated, and the | |
46 // offset from the start of the allocated memory to the group name. We don't | |
47 // need the offset into the trial name because that's always a fixed position | |
48 // from the start of the struct. Two strings will be appended to the end of this | |
49 // structure in allocated memory, so the result will look like this: | |
50 // --------------------------------- | |
51 // | fte | trial_name | group_name | | |
52 // --------------------------------- | |
53 struct FieldTrialEntry { | |
54 bool activated; | |
55 uint32_t group_name_offset; | |
56 | |
57 char* GetTrialName() const { | |
Alexei Svitkine (slow)
2016/10/19 16:32:49
const char*
Same below.
lawrencewu
2016/10/19 20:18:26
Done.
| |
58 char* src = reinterpret_cast<char*>(const_cast<FieldTrialEntry*>(this)); | |
59 return src + sizeof(FieldTrialEntry); | |
60 } | |
61 | |
62 char* GetGroupName() const { | |
63 char* src = reinterpret_cast<char*>(const_cast<FieldTrialEntry*>(this)); | |
64 return src + this->group_name_offset; | |
65 } | |
66 }; | |
41 | 67 |
42 // Created a time value based on |year|, |month| and |day_of_month| parameters. | 68 // Created a time value based on |year|, |month| and |day_of_month| parameters. |
43 Time CreateTimeFromParams(int year, int month, int day_of_month) { | 69 Time CreateTimeFromParams(int year, int month, int day_of_month) { |
44 DCHECK_GT(year, 1970); | 70 DCHECK_GT(year, 1970); |
45 DCHECK_GT(month, 0); | 71 DCHECK_GT(month, 0); |
46 DCHECK_LT(month, 13); | 72 DCHECK_LT(month, 13); |
47 DCHECK_GT(day_of_month, 0); | 73 DCHECK_GT(day_of_month, 0); |
48 DCHECK_LT(day_of_month, 32); | 74 DCHECK_LT(day_of_month, 32); |
49 | 75 |
50 Time::Exploded exploded; | 76 Time::Exploded exploded; |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
125 } | 151 } |
126 | 152 |
127 } // namespace | 153 } // namespace |
128 | 154 |
129 // statics | 155 // statics |
130 const int FieldTrial::kNotFinalized = -1; | 156 const int FieldTrial::kNotFinalized = -1; |
131 const int FieldTrial::kDefaultGroupNumber = 0; | 157 const int FieldTrial::kDefaultGroupNumber = 0; |
132 bool FieldTrial::enable_benchmarking_ = false; | 158 bool FieldTrial::enable_benchmarking_ = false; |
133 | 159 |
134 int FieldTrialList::kNoExpirationYear = 0; | 160 int FieldTrialList::kNoExpirationYear = 0; |
161 base::SharedPersistentMemoryAllocator* FieldTrialList::field_trial_allocator = | |
162 nullptr; | |
135 | 163 |
136 //------------------------------------------------------------------------------ | 164 //------------------------------------------------------------------------------ |
137 // FieldTrial methods and members. | 165 // FieldTrial methods and members. |
138 | 166 |
139 FieldTrial::EntropyProvider::~EntropyProvider() { | 167 FieldTrial::EntropyProvider::~EntropyProvider() { |
140 } | 168 } |
141 | 169 |
142 FieldTrial::State::State() : activated(false) {} | 170 FieldTrial::State::State() : activated(false) {} |
143 | 171 |
144 FieldTrial::State::State(const State& other) = default; | 172 FieldTrial::State::State(const State& other) = default; |
(...skipping 425 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
570 | 598 |
571 // static | 599 // static |
572 void FieldTrialList::CreateTrialsFromCommandLine( | 600 void FieldTrialList::CreateTrialsFromCommandLine( |
573 const base::CommandLine& cmd_line, | 601 const base::CommandLine& cmd_line, |
574 const char* field_trial_handle_switch) { | 602 const char* field_trial_handle_switch) { |
575 DCHECK(global_); | 603 DCHECK(global_); |
576 | 604 |
577 #if defined(OS_WIN) | 605 #if defined(OS_WIN) |
578 if (cmd_line.HasSwitch(field_trial_handle_switch)) { | 606 if (cmd_line.HasSwitch(field_trial_handle_switch)) { |
579 std::string arg = cmd_line.GetSwitchValueASCII(field_trial_handle_switch); | 607 std::string arg = cmd_line.GetSwitchValueASCII(field_trial_handle_switch); |
580 size_t token = arg.find(","); | 608 int field_trial_handle = std::stoi(arg); |
581 int field_trial_handle = std::stoi(arg.substr(0, token)); | |
582 int field_trial_length = std::stoi(arg.substr(token + 1, arg.length())); | |
583 | 609 |
584 HANDLE handle = reinterpret_cast<HANDLE>(field_trial_handle); | 610 HANDLE handle = reinterpret_cast<HANDLE>(field_trial_handle); |
585 base::SharedMemoryHandle shm_handle = | 611 base::SharedMemoryHandle shm_handle = |
586 base::SharedMemoryHandle(handle, base::GetCurrentProcId()); | 612 base::SharedMemoryHandle(handle, base::GetCurrentProcId()); |
587 | 613 |
588 // Gets deleted when it gets out of scope, but that's OK because we need it | 614 // 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. | 615 // only for the duration of this method. |
590 base::SharedMemory shared_memory(shm_handle, false); | 616 std::unique_ptr<base::SharedMemory> shm( |
591 shared_memory.Map(field_trial_length); | 617 new base::SharedMemory(shm_handle, false)); |
618 shm.get()->Map(4 << 10); // 4 KiB = one page | |
Alexei Svitkine (slow)
2016/10/19 16:32:48
Shouldn't this be using field_trial_length?
lawrencewu
2016/10/19 20:18:26
I don't think so -- allocated memory isn't guarant
Alexei Svitkine (slow)
2016/10/19 20:39:36
But that part can be passed from the browser using
lawrencewu
2016/10/20 17:36:05
Ah I see, yes that can be passed. Done.
| |
592 | 619 |
593 char* field_trial_state = static_cast<char*>(shared_memory.memory()); | 620 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; | 621 return; |
598 } | 622 } |
599 #endif | 623 #endif |
600 | 624 |
601 if (cmd_line.HasSwitch(switches::kForceFieldTrials)) { | 625 if (cmd_line.HasSwitch(switches::kForceFieldTrials)) { |
602 bool result = FieldTrialList::CreateTrialsFromString( | 626 bool result = FieldTrialList::CreateTrialsFromString( |
603 cmd_line.GetSwitchValueASCII(switches::kForceFieldTrials), | 627 cmd_line.GetSwitchValueASCII(switches::kForceFieldTrials), |
604 std::set<std::string>()); | 628 std::set<std::string>()); |
605 DCHECK(result); | 629 DCHECK(result); |
606 } | 630 } |
607 } | 631 } |
608 | 632 |
633 #if defined(OS_WIN) | |
634 // static | |
635 void FieldTrialList::AppendFieldTrialHandleIfNeeded( | |
636 base::HandlesToInheritVector& handles) { | |
Alexei Svitkine (slow)
2016/10/19 16:32:48
Parameters shouldn't be passed by non-const refs,
lawrencewu
2016/10/19 20:18:26
Done.
| |
637 // If the allocator has not been created at this point, then instantiate it | |
638 // and add all the existing field trials. | |
639 { | |
640 AutoLock auto_lock(global_->lock_); | |
641 // Create the allocator if not already created. | |
642 if (field_trial_allocator == nullptr) { | |
643 std::unique_ptr<base::SharedMemory> shm(new base::SharedMemory()); | |
644 shm->CreateAndMapAnonymous(4 << 10); // 4 KiB = one page | |
645 | |
646 field_trial_allocator = new base::SharedPersistentMemoryAllocator( | |
647 std::move(shm), 0, kAllocatorName, false); | |
648 field_trial_allocator->CreateTrackingHistograms(kAllocatorName); | |
649 // TODO(lawrencewu): call UpdateTrackingHistograms() when all field trials | |
650 // have been registered (perhaps in the destructor?) | |
651 } | |
652 } | |
653 | |
654 for (const auto& registered : global_->registered_) { | |
655 FieldTrialList::UpdateFieldTrialAllocator(registered.second); | |
Alexei Svitkine (slow)
2016/10/19 16:32:48
Wouldn't this cause entries to be added multiple t
lawrencewu
2016/10/19 20:18:26
Yes, you're right. I moved this up into where the
Alexei Svitkine (slow)
2016/10/19 20:39:36
Can this be covered by a unit test?
lawrencewu
2016/10/20 17:36:05
Done.
| |
656 } | |
657 | |
658 // Need to get a new, read-only handle to share to the child. | |
659 HANDLE src = field_trial_allocator->shared_memory()->handle().GetHandle(); | |
660 ProcessHandle process = GetCurrentProcess(); | |
661 DWORD access = SECTION_MAP_READ | SECTION_QUERY; | |
662 HANDLE dst; | |
663 ::DuplicateHandle(process, src, process, &dst, access, true, 0); | |
664 handles.push_back(dst); | |
665 } | |
666 #endif | |
667 | |
668 // static | |
669 void FieldTrialList::CreateTrialsFromSharedMemory( | |
670 std::unique_ptr<base::SharedMemory> shm) { | |
671 const base::SharedPersistentMemoryAllocator shalloc(std::move(shm), 0, | |
672 kAllocatorName, true); | |
673 base::PersistentMemoryAllocator::Iterator iter(&shalloc); | |
674 | |
675 base::SharedPersistentMemoryAllocator::Reference ref; | |
676 while ((ref = iter.GetNextOfType(kFieldTrialType)) != | |
677 base::SharedPersistentMemoryAllocator::kReferenceNull) { | |
678 const FieldTrialEntry* fte = | |
Alexei Svitkine (slow)
2016/10/19 16:32:49
Nit: fte -> entry
as acronyms are discouraged gen
lawrencewu
2016/10/19 20:18:26
Done.
| |
679 shalloc.GetAsObject<const FieldTrialEntry>(ref, kFieldTrialType); | |
680 char* trial_name = fte->GetTrialName(); | |
681 char* group_name = fte->GetGroupName(); | |
Alexei Svitkine (slow)
2016/10/19 16:32:48
Just inline these into the CreateFieldTrial() call
lawrencewu
2016/10/19 20:18:25
Done.
| |
682 FieldTrial* trial = CreateFieldTrial(trial_name, group_name); | |
683 | |
684 if (fte->activated) { | |
685 // Call |group()| to mark the trial as "used" and notify observers, if | |
686 // any. This is useful to ensure that field trials created in child | |
687 // processes are properly reported in crash reports. | |
688 trial->group(); | |
689 } | |
690 } | |
691 } | |
692 | |
609 // static | 693 // static |
610 std::unique_ptr<base::SharedMemory> FieldTrialList::CopyFieldTrialStateToFlags( | 694 void FieldTrialList::CopyFieldTrialStateToFlags( |
611 const char* field_trial_handle_switch, | 695 const char* field_trial_handle_switch, |
612 base::CommandLine* cmd_line) { | 696 base::CommandLine* cmd_line) { |
697 #if defined(OS_WIN) | |
698 // Use shared memory to pass the state if the feature is enabled, otherwise | |
699 // fallback to passing it via the command line as a string. | |
700 if (kUseSharedMemoryForFieldTrials) { | |
701 base::SharedMemory* shm = field_trial_allocator->shared_memory(); | |
702 | |
703 // HANDLE is just typedef'd to void *. We basically cast the handle into an | |
704 // int (uintptr_t, to be exact), stringify the int, and pass it as a | |
705 // command-line flag. The child process will do the reverse conversions to | |
706 // retrieve the handle. See http://stackoverflow.com/a/153077 | |
707 auto uintptr_handle = | |
708 reinterpret_cast<uintptr_t>(shm->handle().GetHandle()); | |
709 std::string field_trial_handle = std::to_string(uintptr_handle); | |
710 | |
711 cmd_line->AppendSwitchASCII(field_trial_handle_switch, field_trial_handle); | |
712 return; | |
713 } | |
714 #endif | |
715 | |
613 std::string field_trial_states; | 716 std::string field_trial_states; |
614 base::FieldTrialList::AllStatesToString(&field_trial_states); | 717 base::FieldTrialList::AllStatesToString(&field_trial_states); |
615 if (!field_trial_states.empty()) { | 718 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) | |
619 if (kUseSharedMemoryForFieldTrials) { | |
620 std::unique_ptr<base::SharedMemory> shm(new base::SharedMemory()); | |
621 size_t length = field_trial_states.size() + 1; | |
622 shm->CreateAndMapAnonymous(length); | |
623 memcpy(shm->memory(), field_trial_states.c_str(), length); | |
624 | |
625 // HANDLE is just typedef'd to void * | |
626 auto uintptr_handle = | |
627 reinterpret_cast<std::uintptr_t>(shm->handle().GetHandle()); | |
628 std::string field_trial_handle = | |
629 std::to_string(uintptr_handle) + "," + std::to_string(length); | |
630 | |
631 cmd_line->AppendSwitchASCII(field_trial_handle_switch, | |
632 field_trial_handle); | |
633 return shm; | |
634 } | |
635 #endif | |
636 cmd_line->AppendSwitchASCII(switches::kForceFieldTrials, | 719 cmd_line->AppendSwitchASCII(switches::kForceFieldTrials, |
637 field_trial_states); | 720 field_trial_states); |
638 } | 721 } |
639 return std::unique_ptr<base::SharedMemory>(nullptr); | |
640 } | 722 } |
641 | 723 |
642 // static | 724 // static |
643 FieldTrial* FieldTrialList::CreateFieldTrial( | 725 FieldTrial* FieldTrialList::CreateFieldTrial( |
644 const std::string& name, | 726 const std::string& name, |
645 const std::string& group_name) { | 727 const std::string& group_name) { |
646 DCHECK(global_); | 728 DCHECK(global_); |
647 DCHECK_GE(name.size(), 0u); | 729 DCHECK_GE(name.size(), 0u); |
648 DCHECK_GE(group_name.size(), 0u); | 730 DCHECK_GE(group_name.size(), 0u); |
649 if (name.empty() || group_name.empty() || !global_) | 731 if (name.empty() || group_name.empty() || !global_) |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
687 { | 769 { |
688 AutoLock auto_lock(global_->lock_); | 770 AutoLock auto_lock(global_->lock_); |
689 if (field_trial->group_reported_) | 771 if (field_trial->group_reported_) |
690 return; | 772 return; |
691 field_trial->group_reported_ = true; | 773 field_trial->group_reported_ = true; |
692 } | 774 } |
693 | 775 |
694 if (!field_trial->enable_field_trial_) | 776 if (!field_trial->enable_field_trial_) |
695 return; | 777 return; |
696 | 778 |
779 if (kUseSharedMemoryForFieldTrials) { | |
780 FieldTrialList::UpdateFieldTrialAllocator(field_trial); | |
781 } | |
782 | |
697 global_->observer_list_->Notify( | 783 global_->observer_list_->Notify( |
698 FROM_HERE, &FieldTrialList::Observer::OnFieldTrialGroupFinalized, | 784 FROM_HERE, &FieldTrialList::Observer::OnFieldTrialGroupFinalized, |
699 field_trial->trial_name(), field_trial->group_name_internal()); | 785 field_trial->trial_name(), field_trial->group_name_internal()); |
700 } | 786 } |
701 | 787 |
702 // static | 788 // static |
789 void FieldTrialList::UpdateFieldTrialAllocator(FieldTrial* field_trial) { | |
Alexei Svitkine (slow)
2016/10/19 16:32:48
This should be in the anonymous namespace at the t
lawrencewu
2016/10/19 20:18:25
This function references the |field_trial_allocato
Alexei Svitkine (slow)
2016/10/19 20:39:36
If field_trial_allocator is not used outside of th
lawrencewu
2016/10/20 17:36:05
Moved.
| |
790 // Don't do anything if the allocator hasn't been instantiated yet. | |
791 if (field_trial_allocator == nullptr) | |
792 return; | |
793 | |
794 // We allocate just enough memory to fit the FTE struct, trial name, and group | |
795 // name. If there's no more memory available, quit early. | |
796 FieldTrial::State trial_state; | |
797 field_trial->GetState(&trial_state); | |
798 | |
799 std::string trial_name; | |
800 trial_state.trial_name.CopyToString(&trial_name); | |
801 size_t trial_name_size = trial_name.size() + 1; | |
802 | |
803 std::string group_name; | |
804 trial_state.group_name.CopyToString(&group_name); | |
805 size_t group_name_size = group_name.size() + 1; | |
806 | |
807 uint32_t trial_name_offset = sizeof(FieldTrialEntry); | |
808 uint32_t group_name_offset = trial_name_offset + trial_name_size; | |
809 size_t size = sizeof(FieldTrialEntry) + trial_name_size + group_name_size; | |
810 | |
811 base::SharedPersistentMemoryAllocator::Reference ref = | |
812 field_trial_allocator->Allocate(size, kFieldTrialType); | |
813 if (ref == base::SharedPersistentMemoryAllocator::kReferenceNull) | |
814 return; | |
815 | |
816 char* dst = field_trial_allocator->GetAsObject<char>(ref, kFieldTrialType); | |
817 FieldTrialEntry fte = {trial_state.activated, group_name_offset}; | |
818 memcpy(dst, &fte, sizeof(FieldTrialEntry)); | |
Alexei Svitkine (slow)
2016/10/19 16:32:49
Just cast dst to FTE* and set it directly rather t
lawrencewu
2016/10/19 20:18:26
Done.
| |
819 memcpy(dst + trial_name_offset, trial_name.c_str(), trial_name_size); | |
820 memcpy(dst + group_name_offset, group_name.c_str(), group_name_size); | |
821 | |
822 field_trial_allocator->MakeIterable(ref); | |
823 } | |
824 | |
825 // static | |
703 size_t FieldTrialList::GetFieldTrialCount() { | 826 size_t FieldTrialList::GetFieldTrialCount() { |
704 if (!global_) | 827 if (!global_) |
705 return 0; | 828 return 0; |
706 AutoLock auto_lock(global_->lock_); | 829 AutoLock auto_lock(global_->lock_); |
707 return global_->registered_.size(); | 830 return global_->registered_.size(); |
708 } | 831 } |
709 | 832 |
710 // static | 833 // static |
711 const FieldTrial::EntropyProvider* | 834 const FieldTrial::EntropyProvider* |
712 FieldTrialList::GetEntropyProviderForOneTimeRandomization() { | 835 FieldTrialList::GetEntropyProviderForOneTimeRandomization() { |
(...skipping 19 matching lines...) Expand all Loading... | |
732 return; | 855 return; |
733 } | 856 } |
734 AutoLock auto_lock(global_->lock_); | 857 AutoLock auto_lock(global_->lock_); |
735 CHECK(!global_->PreLockedFind(trial->trial_name())) << trial->trial_name(); | 858 CHECK(!global_->PreLockedFind(trial->trial_name())) << trial->trial_name(); |
736 trial->AddRef(); | 859 trial->AddRef(); |
737 trial->SetTrialRegistered(); | 860 trial->SetTrialRegistered(); |
738 global_->registered_[trial->trial_name()] = trial; | 861 global_->registered_[trial->trial_name()] = trial; |
739 } | 862 } |
740 | 863 |
741 } // namespace base | 864 } // namespace base |
OLD | NEW |