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[] = "field_trial_allocator"; | |
42 const uint32_t kFieldTrialType = 0xABA17E13 + 1; // Version #1 of type | |
bcwhite
2016/10/14 14:23:46
// SHA1(FieldTrialEntry) v1
So that others know w
lawrencewu
2016/10/19 16:21:28
Done.
| |
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 | |
58 char* GetTrialName(FieldTrialEntry* fte) { | |
Alexei Svitkine (slow)
2016/10/17 14:46:50
Make this actually a member of FieldTrialEntry - i
lawrencewu
2016/10/19 16:21:28
Done.
| |
59 char* src = (char*)fte; | |
Alexei Svitkine (slow)
2016/10/17 14:46:50
Per the style guide, you must use C++ style casts
lawrencewu
2016/10/19 16:21:28
Done.
| |
60 return src + sizeof(FieldTrialEntry); | |
61 } | |
62 | |
63 char* GetGroupName(FieldTrialEntry* fte) { | |
64 char* src = (char*)fte; | |
65 return src + fte->group_name_offset; | |
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 | |
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) { | |
637 if (field_trial_allocator) { | |
638 handles.push_back( | |
639 field_trial_allocator->shared_memory()->handle().GetHandle()); | |
640 } | |
641 } | |
642 #endif | |
643 | |
644 // static | |
645 void FieldTrialList::CreateTrialsFromSharedMemory( | |
646 std::unique_ptr<base::SharedMemory> shm) { | |
647 base::SharedPersistentMemoryAllocator shalloc(std::move(shm), 0, | |
648 kAllocatorName, true); | |
649 base::PersistentMemoryAllocator::Iterator iter(&shalloc); | |
650 | |
651 base::SharedPersistentMemoryAllocator::Reference ref; | |
652 while ((ref = iter.GetNextOfType(kFieldTrialType)) != | |
653 base::SharedPersistentMemoryAllocator::kReferenceNull) { | |
654 FieldTrialEntry* fte = | |
655 shalloc.GetAsObject<FieldTrialEntry>(ref, kFieldTrialType); | |
656 char* trial_name = GetTrialName(fte); | |
657 char* group_name = GetGroupName(fte); | |
658 FieldTrial* trial = CreateFieldTrial(trial_name, group_name); | |
659 | |
660 if (fte->activated) { | |
661 // Call |group()| to mark the trial as "used" and notify observers, if | |
662 // any. This is useful to ensure that field trials created in child | |
663 // processes are properly reported in crash reports. | |
664 trial->group(); | |
665 } | |
666 } | |
667 } | |
668 | |
609 // static | 669 // static |
610 std::unique_ptr<base::SharedMemory> FieldTrialList::CopyFieldTrialStateToFlags( | 670 void FieldTrialList::CopyFieldTrialStateToFlags( |
611 const char* field_trial_handle_switch, | 671 const char* field_trial_handle_switch, |
612 base::CommandLine* cmd_line) { | 672 base::CommandLine* cmd_line) { |
673 #if defined(OS_WIN) | |
674 // Use shared memory to pass the state if the feature is enabled, otherwise | |
675 // fallback to passing it via the command line as a string. | |
676 if (kUseSharedMemoryForFieldTrials) { | |
677 base::SharedMemory* shm = field_trial_allocator->shared_memory(); | |
678 | |
679 // HANDLE is just typedef'd to void *. We basically cast the handle into an | |
680 // int (uintptr_t, to be exact), stringify the int, and pass it as a | |
681 // command-line flag. The child process will do the reverse conversions to | |
682 // retrieve the handle. See http://stackoverflow.com/a/153077 | |
683 auto uintptr_handle = | |
684 reinterpret_cast<uintptr_t>(shm->handle().GetHandle()); | |
685 std::string field_trial_handle = std::to_string(uintptr_handle); | |
686 | |
687 cmd_line->AppendSwitchASCII(field_trial_handle_switch, field_trial_handle); | |
688 return; | |
689 } | |
690 #endif | |
691 | |
613 std::string field_trial_states; | 692 std::string field_trial_states; |
614 base::FieldTrialList::AllStatesToString(&field_trial_states); | 693 base::FieldTrialList::AllStatesToString(&field_trial_states); |
615 if (!field_trial_states.empty()) { | 694 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, | 695 cmd_line->AppendSwitchASCII(switches::kForceFieldTrials, |
637 field_trial_states); | 696 field_trial_states); |
638 } | 697 } |
639 return std::unique_ptr<base::SharedMemory>(nullptr); | |
640 } | 698 } |
641 | 699 |
642 // static | 700 // static |
643 FieldTrial* FieldTrialList::CreateFieldTrial( | 701 FieldTrial* FieldTrialList::CreateFieldTrial( |
644 const std::string& name, | 702 const std::string& name, |
645 const std::string& group_name) { | 703 const std::string& group_name) { |
646 DCHECK(global_); | 704 DCHECK(global_); |
647 DCHECK_GE(name.size(), 0u); | 705 DCHECK_GE(name.size(), 0u); |
648 DCHECK_GE(group_name.size(), 0u); | 706 DCHECK_GE(group_name.size(), 0u); |
649 if (name.empty() || group_name.empty() || !global_) | 707 if (name.empty() || group_name.empty() || !global_) |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
687 { | 745 { |
688 AutoLock auto_lock(global_->lock_); | 746 AutoLock auto_lock(global_->lock_); |
689 if (field_trial->group_reported_) | 747 if (field_trial->group_reported_) |
690 return; | 748 return; |
691 field_trial->group_reported_ = true; | 749 field_trial->group_reported_ = true; |
692 } | 750 } |
693 | 751 |
694 if (!field_trial->enable_field_trial_) | 752 if (!field_trial->enable_field_trial_) |
695 return; | 753 return; |
696 | 754 |
755 if (kUseSharedMemoryForFieldTrials) { | |
756 FieldTrialList::UpdateFieldTrialAllocator(field_trial); | |
Alexei Svitkine (slow)
2016/10/17 14:46:50
Please make this lazy.
That is, someone could use
lawrencewu
2016/10/19 16:21:28
Done.
| |
757 } | |
758 | |
697 global_->observer_list_->Notify( | 759 global_->observer_list_->Notify( |
698 FROM_HERE, &FieldTrialList::Observer::OnFieldTrialGroupFinalized, | 760 FROM_HERE, &FieldTrialList::Observer::OnFieldTrialGroupFinalized, |
699 field_trial->trial_name(), field_trial->group_name_internal()); | 761 field_trial->trial_name(), field_trial->group_name_internal()); |
700 } | 762 } |
701 | 763 |
702 // static | 764 // static |
765 void FieldTrialList::UpdateFieldTrialAllocator(FieldTrial* field_trial) { | |
766 // Create the allocator if not already created. | |
767 if (field_trial_allocator == nullptr) { | |
768 std::unique_ptr<base::SharedMemory> shm(new base::SharedMemory()); | |
769 shm->CreateAndMapAnonymous(4 << 10); // 4 KiB = one page | |
770 | |
771 field_trial_allocator = new base::SharedPersistentMemoryAllocator( | |
772 std::move(shm), 0, kAllocatorName, false); | |
bcwhite
2016/10/14 14:23:46
If you call
allocator->CreateTrackingHistograms(k
lawrencewu
2016/10/19 16:21:28
asvitkine and I decided to punt on this for later,
| |
773 } | |
774 | |
775 // We allocate just enough memory to fit the FTE struct, trial name, and group | |
776 // name. | |
777 FieldTrial::State trial_state; | |
778 field_trial->GetState(&trial_state); | |
779 | |
780 std::string trial_name; | |
781 trial_state.trial_name.CopyToString(&trial_name); | |
782 size_t trial_name_size = trial_name.size() + 1; | |
783 | |
784 std::string group_name; | |
785 trial_state.group_name.CopyToString(&group_name); | |
786 size_t group_name_size = group_name.size() + 1; | |
787 | |
788 uint32_t trial_name_offset = sizeof(FieldTrialEntry); | |
789 uint32_t group_name_offset = trial_name_offset + trial_name_size; | |
790 size_t size = sizeof(FieldTrialEntry) + trial_name_size + group_name_size; | |
791 | |
792 base::SharedPersistentMemoryAllocator::Reference ref = | |
793 field_trial_allocator->Allocate(size, kFieldTrialType); | |
bcwhite
2016/10/14 14:23:46
You need to handle the failure case here. If the
lawrencewu
2016/10/19 16:21:29
Done.
| |
794 | |
795 char* dst = field_trial_allocator->GetAsObject<char>(ref, kFieldTrialType); | |
796 FieldTrialEntry fte = {trial_state.activated, group_name_offset}; | |
797 memcpy(dst, &fte, sizeof(FieldTrialEntry)); | |
798 memcpy(dst + trial_name_offset, trial_name.c_str(), trial_name_size); | |
799 memcpy(dst + group_name_offset, group_name.c_str(), group_name_size); | |
800 | |
801 field_trial_allocator->MakeIterable(ref); | |
802 } | |
803 | |
804 // static | |
703 size_t FieldTrialList::GetFieldTrialCount() { | 805 size_t FieldTrialList::GetFieldTrialCount() { |
704 if (!global_) | 806 if (!global_) |
705 return 0; | 807 return 0; |
706 AutoLock auto_lock(global_->lock_); | 808 AutoLock auto_lock(global_->lock_); |
707 return global_->registered_.size(); | 809 return global_->registered_.size(); |
708 } | 810 } |
709 | 811 |
710 // static | 812 // static |
711 const FieldTrial::EntropyProvider* | 813 const FieldTrial::EntropyProvider* |
712 FieldTrialList::GetEntropyProviderForOneTimeRandomization() { | 814 FieldTrialList::GetEntropyProviderForOneTimeRandomization() { |
(...skipping 19 matching lines...) Expand all Loading... | |
732 return; | 834 return; |
733 } | 835 } |
734 AutoLock auto_lock(global_->lock_); | 836 AutoLock auto_lock(global_->lock_); |
735 CHECK(!global_->PreLockedFind(trial->trial_name())) << trial->trial_name(); | 837 CHECK(!global_->PreLockedFind(trial->trial_name())) << trial->trial_name(); |
736 trial->AddRef(); | 838 trial->AddRef(); |
737 trial->SetTrialRegistered(); | 839 trial->SetTrialRegistered(); |
738 global_->registered_[trial->trial_name()] = trial; | 840 global_->registered_[trial->trial_name()] = trial; |
739 } | 841 } |
740 | 842 |
741 } // namespace base | 843 } // namespace base |
OLD | NEW |