Chromium Code Reviews| 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 const bool kUseSharedMemoryForFieldTrials = false; | |
| 39 | |
| 40 // Allocator used to instantiate field trial in child processes. In the | |
| 41 // future, we may want to move this to a more generic place if we want to | |
| 42 // start passing more data other than field trials. | |
| 43 base::SharedPersistentMemoryAllocator* g_field_trial_allocator = nullptr; | |
| 38 #if defined(OS_WIN) | 44 #if defined(OS_WIN) |
| 39 const bool kUseSharedMemoryForFieldTrials = false; | 45 HANDLE g_readonly_allocator_handle = 0; |
| 40 #endif | 46 #endif |
| 41 | 47 |
| 48 // Constants for the field trial allocator. | |
| 49 const char kAllocatorName[] = "FieldTrialAllocator"; | |
| 50 const uint32_t kFieldTrialType = 0xABA17E13 + 1; // SHA1(FieldTrialEntry) v1 | |
| 51 | |
| 52 // We create one FieldTrialEntry struct per field trial in shared memory (via | |
| 53 // g_field_trial_allocator). It contains whether or not it's activated, and the | |
| 54 // offset from the start of the allocated memory to the group name. We don't | |
| 55 // need the offset into the trial name because that's always a fixed position | |
| 56 // from the start of the struct. Two strings will be appended to the end of this | |
| 57 // structure in allocated memory, so the result will look like this: | |
| 58 // --------------------------------- | |
| 59 // | fte | trial_name | group_name | | |
| 60 // --------------------------------- | |
| 61 struct FieldTrialEntry { | |
| 62 bool activated; | |
| 63 uint32_t group_name_offset; | |
| 64 | |
| 65 const char* GetTrialName() const { | |
| 66 const char* src = | |
| 67 reinterpret_cast<char*>(const_cast<FieldTrialEntry*>(this)); | |
| 68 return src + sizeof(FieldTrialEntry); | |
| 69 } | |
| 70 | |
| 71 const char* GetGroupName() const { | |
| 72 const char* src = | |
| 73 reinterpret_cast<char*>(const_cast<FieldTrialEntry*>(this)); | |
| 74 return src + this->group_name_offset; | |
| 75 } | |
| 76 }; | |
| 77 | |
| 42 // Created a time value based on |year|, |month| and |day_of_month| parameters. | 78 // Created a time value based on |year|, |month| and |day_of_month| parameters. |
| 43 Time CreateTimeFromParams(int year, int month, int day_of_month) { | 79 Time CreateTimeFromParams(int year, int month, int day_of_month) { |
| 44 DCHECK_GT(year, 1970); | 80 DCHECK_GT(year, 1970); |
| 45 DCHECK_GT(month, 0); | 81 DCHECK_GT(month, 0); |
| 46 DCHECK_LT(month, 13); | 82 DCHECK_LT(month, 13); |
| 47 DCHECK_GT(day_of_month, 0); | 83 DCHECK_GT(day_of_month, 0); |
| 48 DCHECK_LT(day_of_month, 32); | 84 DCHECK_LT(day_of_month, 32); |
| 49 | 85 |
| 50 Time::Exploded exploded; | 86 Time::Exploded exploded; |
| 51 exploded.year = year; | 87 exploded.year = year; |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 122 entries->push_back(entry); | 158 entries->push_back(entry); |
| 123 } | 159 } |
| 124 return true; | 160 return true; |
| 125 } | 161 } |
| 126 | 162 |
| 127 } // namespace | 163 } // namespace |
| 128 | 164 |
| 129 // statics | 165 // statics |
| 130 const int FieldTrial::kNotFinalized = -1; | 166 const int FieldTrial::kNotFinalized = -1; |
| 131 const int FieldTrial::kDefaultGroupNumber = 0; | 167 const int FieldTrial::kDefaultGroupNumber = 0; |
| 168 const int kFieldTrialAllocationSize = 4 << 10; // 4 KiB = one page | |
| 132 bool FieldTrial::enable_benchmarking_ = false; | 169 bool FieldTrial::enable_benchmarking_ = false; |
| 133 | 170 |
| 134 int FieldTrialList::kNoExpirationYear = 0; | 171 int FieldTrialList::kNoExpirationYear = 0; |
| 135 | 172 |
| 136 //------------------------------------------------------------------------------ | 173 //------------------------------------------------------------------------------ |
| 137 // FieldTrial methods and members. | 174 // FieldTrial methods and members. |
| 138 | 175 |
| 139 FieldTrial::EntropyProvider::~EntropyProvider() { | 176 FieldTrial::EntropyProvider::~EntropyProvider() { |
| 140 } | 177 } |
| 141 | 178 |
| (...skipping 437 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 579 std::string arg = cmd_line.GetSwitchValueASCII(field_trial_handle_switch); | 616 std::string arg = cmd_line.GetSwitchValueASCII(field_trial_handle_switch); |
| 580 size_t token = arg.find(","); | 617 size_t token = arg.find(","); |
| 581 int field_trial_handle = std::stoi(arg.substr(0, token)); | 618 int field_trial_handle = std::stoi(arg.substr(0, token)); |
| 582 int field_trial_length = std::stoi(arg.substr(token + 1, arg.length())); | 619 int field_trial_length = std::stoi(arg.substr(token + 1, arg.length())); |
| 583 | 620 |
| 584 HANDLE handle = reinterpret_cast<HANDLE>(field_trial_handle); | 621 HANDLE handle = reinterpret_cast<HANDLE>(field_trial_handle); |
| 585 base::SharedMemoryHandle shm_handle = | 622 base::SharedMemoryHandle shm_handle = |
| 586 base::SharedMemoryHandle(handle, base::GetCurrentProcId()); | 623 base::SharedMemoryHandle(handle, base::GetCurrentProcId()); |
| 587 | 624 |
| 588 // Gets deleted when it gets out of scope, but that's OK because we need it | 625 // 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. | 626 // only for the duration of this method. |
| 590 base::SharedMemory shared_memory(shm_handle, false); | 627 std::unique_ptr<base::SharedMemory> shm( |
| 591 shared_memory.Map(field_trial_length); | 628 new base::SharedMemory(shm_handle, true)); |
| 629 shm.get()->Map(field_trial_length); | |
|
bcwhite
2016/10/20 21:14:06
Map() will return false if it fails which can happ
lawrencewu
2016/10/21 16:41:58
Done.
| |
| 592 | 630 |
| 593 char* field_trial_state = static_cast<char*>(shared_memory.memory()); | 631 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; | 632 return; |
| 598 } | 633 } |
| 599 #endif | 634 #endif |
| 600 | 635 |
| 601 if (cmd_line.HasSwitch(switches::kForceFieldTrials)) { | 636 if (cmd_line.HasSwitch(switches::kForceFieldTrials)) { |
| 602 bool result = FieldTrialList::CreateTrialsFromString( | 637 bool result = FieldTrialList::CreateTrialsFromString( |
| 603 cmd_line.GetSwitchValueASCII(switches::kForceFieldTrials), | 638 cmd_line.GetSwitchValueASCII(switches::kForceFieldTrials), |
| 604 std::set<std::string>()); | 639 std::set<std::string>()); |
| 605 DCHECK(result); | 640 DCHECK(result); |
| 606 } | 641 } |
| 607 } | 642 } |
| 608 | 643 |
| 644 #if defined(OS_WIN) | |
| 645 // static | |
| 646 void FieldTrialList::AppendFieldTrialHandleIfNeeded( | |
| 647 base::HandlesToInheritVector* handles) { | |
| 648 FieldTrialList::InstantiateFieldTrialAllocatorIfNeeded(); | |
| 649 | |
| 650 // Need to get a new, read-only handle to share to the child. | |
| 651 handles->push_back(g_readonly_allocator_handle); | |
| 652 } | |
| 653 #endif | |
| 654 | |
| 655 // static | |
| 656 void FieldTrialList::CreateTrialsFromSharedMemory( | |
| 657 std::unique_ptr<base::SharedMemory> shm) { | |
| 658 const base::SharedPersistentMemoryAllocator shalloc(std::move(shm), 0, | |
| 659 kAllocatorName, true); | |
| 660 base::PersistentMemoryAllocator::Iterator iter(&shalloc); | |
| 661 | |
| 662 base::SharedPersistentMemoryAllocator::Reference ref; | |
| 663 while ((ref = iter.GetNextOfType(kFieldTrialType)) != | |
| 664 base::SharedPersistentMemoryAllocator::kReferenceNull) { | |
| 665 const FieldTrialEntry* entry = | |
| 666 shalloc.GetAsObject<const FieldTrialEntry>(ref, kFieldTrialType); | |
| 667 FieldTrial* trial = | |
| 668 CreateFieldTrial(entry->GetTrialName(), entry->GetGroupName()); | |
| 669 | |
| 670 if (entry->activated) { | |
| 671 // Call |group()| to mark the trial as "used" and notify observers, if | |
| 672 // any. This is useful to ensure that field trials created in child | |
| 673 // processes are properly reported in crash reports. | |
| 674 trial->group(); | |
| 675 } | |
| 676 } | |
| 677 } | |
| 678 | |
| 609 // static | 679 // static |
| 610 std::unique_ptr<base::SharedMemory> FieldTrialList::CopyFieldTrialStateToFlags( | 680 void FieldTrialList::CopyFieldTrialStateToFlags( |
| 611 const char* field_trial_handle_switch, | 681 const char* field_trial_handle_switch, |
| 612 base::CommandLine* cmd_line) { | 682 base::CommandLine* cmd_line) { |
| 683 #if defined(OS_WIN) | |
| 684 // Use shared memory to pass the state if the feature is enabled, otherwise | |
| 685 // fallback to passing it via the command line as a string. | |
| 686 if (kUseSharedMemoryForFieldTrials) { | |
| 687 FieldTrialList::InstantiateFieldTrialAllocatorIfNeeded(); | |
| 688 // HANDLE is just typedef'd to void *. We basically cast the handle into an | |
| 689 // int (uintptr_t, to be exact), stringify the int, and pass it as a | |
| 690 // command-line flag. The child process will do the reverse conversions to | |
| 691 // retrieve the handle. See http://stackoverflow.com/a/153077 | |
| 692 auto uintptr_handle = | |
| 693 reinterpret_cast<uintptr_t>(g_readonly_allocator_handle); | |
| 694 size_t field_trial_length = | |
| 695 g_field_trial_allocator->shared_memory()->mapped_size(); | |
|
bcwhite
2016/10/20 21:14:06
Instead of UpdateTrackingHistograms you could also
lawrencewu
2016/10/23 20:01:56
Ack, will do later.
| |
| 696 std::string field_trial_handle = std::to_string(uintptr_handle) + "," + | |
| 697 std::to_string(field_trial_length); | |
| 698 | |
| 699 cmd_line->AppendSwitchASCII(field_trial_handle_switch, field_trial_handle); | |
| 700 return; | |
| 701 } | |
| 702 #endif | |
| 703 | |
| 613 std::string field_trial_states; | 704 std::string field_trial_states; |
| 614 base::FieldTrialList::AllStatesToString(&field_trial_states); | 705 base::FieldTrialList::AllStatesToString(&field_trial_states); |
| 615 if (!field_trial_states.empty()) { | 706 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, | 707 cmd_line->AppendSwitchASCII(switches::kForceFieldTrials, |
| 637 field_trial_states); | 708 field_trial_states); |
| 638 } | 709 } |
| 639 return std::unique_ptr<base::SharedMemory>(nullptr); | |
| 640 } | 710 } |
| 641 | 711 |
| 642 // static | 712 // static |
| 713 base::SharedPersistentMemoryAllocator* | |
| 714 FieldTrialList::GetFieldTrialAllocatorForTesting() { | |
| 715 return g_field_trial_allocator; | |
| 716 } | |
| 717 | |
| 718 // static | |
| 643 FieldTrial* FieldTrialList::CreateFieldTrial( | 719 FieldTrial* FieldTrialList::CreateFieldTrial( |
| 644 const std::string& name, | 720 const std::string& name, |
| 645 const std::string& group_name) { | 721 const std::string& group_name) { |
| 646 DCHECK(global_); | 722 DCHECK(global_); |
| 647 DCHECK_GE(name.size(), 0u); | 723 DCHECK_GE(name.size(), 0u); |
| 648 DCHECK_GE(group_name.size(), 0u); | 724 DCHECK_GE(group_name.size(), 0u); |
| 649 if (name.empty() || group_name.empty() || !global_) | 725 if (name.empty() || group_name.empty() || !global_) |
| 650 return NULL; | 726 return NULL; |
| 651 | 727 |
| 652 FieldTrial* field_trial = FieldTrialList::Find(name); | 728 FieldTrial* field_trial = FieldTrialList::Find(name); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 687 { | 763 { |
| 688 AutoLock auto_lock(global_->lock_); | 764 AutoLock auto_lock(global_->lock_); |
| 689 if (field_trial->group_reported_) | 765 if (field_trial->group_reported_) |
| 690 return; | 766 return; |
| 691 field_trial->group_reported_ = true; | 767 field_trial->group_reported_ = true; |
| 692 } | 768 } |
| 693 | 769 |
| 694 if (!field_trial->enable_field_trial_) | 770 if (!field_trial->enable_field_trial_) |
| 695 return; | 771 return; |
| 696 | 772 |
| 773 if (kUseSharedMemoryForFieldTrials) { | |
| 774 FieldTrialList::UpdateFieldTrialAllocator(field_trial); | |
| 775 } | |
| 776 | |
| 697 global_->observer_list_->Notify( | 777 global_->observer_list_->Notify( |
| 698 FROM_HERE, &FieldTrialList::Observer::OnFieldTrialGroupFinalized, | 778 FROM_HERE, &FieldTrialList::Observer::OnFieldTrialGroupFinalized, |
| 699 field_trial->trial_name(), field_trial->group_name_internal()); | 779 field_trial->trial_name(), field_trial->group_name_internal()); |
| 700 } | 780 } |
| 701 | 781 |
| 702 // static | 782 // static |
| 783 void FieldTrialList::InstantiateFieldTrialAllocatorIfNeeded() { | |
| 784 AutoLock auto_lock(global_->lock_); | |
| 785 // Create the allocator if not already created and add all existing trials. | |
| 786 if (g_field_trial_allocator == nullptr) { | |
| 787 std::unique_ptr<base::SharedMemory> shm(new base::SharedMemory()); | |
| 788 shm->CreateAndMapAnonymous(kFieldTrialAllocationSize); | |
| 789 | |
| 790 // TODO(lawrencewu): call UpdateTrackingHistograms() when all field trials | |
| 791 // have been registered (perhaps in the destructor?) | |
| 792 g_field_trial_allocator = new base::SharedPersistentMemoryAllocator( | |
| 793 std::move(shm), 0, kAllocatorName, false); | |
| 794 g_field_trial_allocator->CreateTrackingHistograms(kAllocatorName); | |
| 795 | |
| 796 // Add all existing field trials. | |
| 797 for (const auto& registered : global_->registered_) { | |
| 798 FieldTrialList::UpdateFieldTrialAllocator(registered.second); | |
| 799 } | |
| 800 | |
| 801 #if defined(OS_WIN) | |
| 802 // Duplicate and set |g_readonly_allocator_handle| so we can pass it to be | |
| 803 // inherited and via the command line. | |
| 804 HANDLE src = g_field_trial_allocator->shared_memory()->handle().GetHandle(); | |
| 805 ProcessHandle process = GetCurrentProcess(); | |
| 806 DWORD access = SECTION_MAP_READ | SECTION_QUERY; | |
| 807 ::DuplicateHandle(process, src, process, &g_readonly_allocator_handle, | |
| 808 access, true, 0); | |
| 809 #endif | |
| 810 } | |
| 811 } | |
| 812 | |
| 813 // static | |
| 814 void FieldTrialList::UpdateFieldTrialAllocator(FieldTrial* field_trial) { | |
| 815 // Don't do anything if the allocator hasn't been instantiated yet. | |
| 816 if (g_field_trial_allocator == nullptr) | |
|
bcwhite
2016/10/21 13:48:29
Since we've established that there could be parall
lawrencewu
2016/10/23 20:01:56
Done.
| |
| 817 return; | |
| 818 | |
| 819 // We allocate just enough memory to fit the FTE struct, trial name, and group | |
| 820 // name. If there's no more memory available, quit early. | |
| 821 FieldTrial::State trial_state; | |
| 822 field_trial->GetState(&trial_state); | |
| 823 | |
| 824 std::string trial_name; | |
| 825 trial_state.trial_name.CopyToString(&trial_name); | |
| 826 size_t trial_name_size = trial_name.size() + 1; | |
| 827 | |
| 828 std::string group_name; | |
| 829 trial_state.group_name.CopyToString(&group_name); | |
| 830 size_t group_name_size = group_name.size() + 1; | |
| 831 | |
| 832 uint32_t trial_name_offset = sizeof(FieldTrialEntry); | |
| 833 uint32_t group_name_offset = trial_name_offset + trial_name_size; | |
| 834 size_t size = sizeof(FieldTrialEntry) + trial_name_size + group_name_size; | |
| 835 | |
| 836 base::SharedPersistentMemoryAllocator::Reference ref = | |
| 837 g_field_trial_allocator->Allocate(size, kFieldTrialType); | |
| 838 if (ref == base::SharedPersistentMemoryAllocator::kReferenceNull) | |
| 839 return; | |
| 840 | |
| 841 FieldTrialEntry* dst = g_field_trial_allocator->GetAsObject<FieldTrialEntry>( | |
| 842 ref, kFieldTrialType); | |
| 843 FieldTrialEntry entry = {trial_state.activated, group_name_offset}; | |
| 844 *dst = entry; | |
| 845 memcpy(reinterpret_cast<char*>(dst) + trial_name_offset, trial_name.c_str(), | |
| 846 trial_name_size); | |
| 847 memcpy(reinterpret_cast<char*>(dst) + group_name_offset, group_name.c_str(), | |
| 848 group_name_size); | |
| 849 | |
| 850 g_field_trial_allocator->MakeIterable(ref); | |
| 851 } | |
| 852 | |
| 853 // static | |
| 703 size_t FieldTrialList::GetFieldTrialCount() { | 854 size_t FieldTrialList::GetFieldTrialCount() { |
| 704 if (!global_) | 855 if (!global_) |
| 705 return 0; | 856 return 0; |
| 706 AutoLock auto_lock(global_->lock_); | 857 AutoLock auto_lock(global_->lock_); |
| 707 return global_->registered_.size(); | 858 return global_->registered_.size(); |
| 708 } | 859 } |
| 709 | 860 |
| 710 // static | 861 // static |
| 711 const FieldTrial::EntropyProvider* | 862 const FieldTrial::EntropyProvider* |
| 712 FieldTrialList::GetEntropyProviderForOneTimeRandomization() { | 863 FieldTrialList::GetEntropyProviderForOneTimeRandomization() { |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 732 return; | 883 return; |
| 733 } | 884 } |
| 734 AutoLock auto_lock(global_->lock_); | 885 AutoLock auto_lock(global_->lock_); |
| 735 CHECK(!global_->PreLockedFind(trial->trial_name())) << trial->trial_name(); | 886 CHECK(!global_->PreLockedFind(trial->trial_name())) << trial->trial_name(); |
| 736 trial->AddRef(); | 887 trial->AddRef(); |
| 737 trial->SetTrialRegistered(); | 888 trial->SetTrialRegistered(); |
| 738 global_->registered_[trial->trial_name()] = trial; | 889 global_->registered_[trial->trial_name()] = trial; |
| 739 } | 890 } |
| 740 | 891 |
| 741 } // namespace base | 892 } // namespace base |
| OLD | NEW |