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" |
| 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; | |
| 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 #if defined(OS_WIN) | |
| 157 HANDLE CreateReadOnlyHandle(base::SharedPersistentMemoryAllocator* allocator) { | |
| 158 HANDLE dst; | |
|
Alexei Svitkine (slow)
2016/10/21 20:52:21
Nit: Move this right before it's needed i.e. above
lawrencewu
2016/10/23 20:01:56
Done.
| |
| 159 HANDLE src = allocator->shared_memory()->handle().GetHandle(); | |
| 160 ProcessHandle process = GetCurrentProcess(); | |
| 161 DWORD access = SECTION_MAP_READ | SECTION_QUERY; | |
| 162 ::DuplicateHandle(process, src, process, &dst, access, true, 0); | |
|
Alexei Svitkine (slow)
2016/10/21 20:52:21
This function can potentially fail - as indicated
lawrencewu
2016/10/23 20:01:56
Done.
| |
| 163 return dst; | |
| 164 } | |
| 165 #endif | |
| 166 | |
| 127 } // namespace | 167 } // namespace |
| 128 | 168 |
| 129 // statics | 169 // statics |
| 130 const int FieldTrial::kNotFinalized = -1; | 170 const int FieldTrial::kNotFinalized = -1; |
| 131 const int FieldTrial::kDefaultGroupNumber = 0; | 171 const int FieldTrial::kDefaultGroupNumber = 0; |
| 172 const int kFieldTrialAllocationSize = 4 << 10; // 4 KiB = one page | |
| 132 bool FieldTrial::enable_benchmarking_ = false; | 173 bool FieldTrial::enable_benchmarking_ = false; |
| 133 | 174 |
| 134 int FieldTrialList::kNoExpirationYear = 0; | 175 int FieldTrialList::kNoExpirationYear = 0; |
| 135 | 176 |
| 136 //------------------------------------------------------------------------------ | 177 //------------------------------------------------------------------------------ |
| 137 // FieldTrial methods and members. | 178 // FieldTrial methods and members. |
| 138 | 179 |
| 139 FieldTrial::EntropyProvider::~EntropyProvider() { | 180 FieldTrial::EntropyProvider::~EntropyProvider() { |
| 140 } | 181 } |
| 141 | 182 |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 325 observer_list_(new ObserverListThreadSafe<FieldTrialList::Observer>( | 366 observer_list_(new ObserverListThreadSafe<FieldTrialList::Observer>( |
| 326 ObserverListBase<FieldTrialList::Observer>::NOTIFY_EXISTING_ONLY)) { | 367 ObserverListBase<FieldTrialList::Observer>::NOTIFY_EXISTING_ONLY)) { |
| 327 DCHECK(!global_); | 368 DCHECK(!global_); |
| 328 DCHECK(!used_without_global_); | 369 DCHECK(!used_without_global_); |
| 329 global_ = this; | 370 global_ = this; |
| 330 | 371 |
| 331 Time two_years_from_build_time = GetBuildTime() + TimeDelta::FromDays(730); | 372 Time two_years_from_build_time = GetBuildTime() + TimeDelta::FromDays(730); |
| 332 Time::Exploded exploded; | 373 Time::Exploded exploded; |
| 333 two_years_from_build_time.LocalExplode(&exploded); | 374 two_years_from_build_time.LocalExplode(&exploded); |
| 334 kNoExpirationYear = exploded.year; | 375 kNoExpirationYear = exploded.year; |
| 376 | |
| 377 field_trial_allocator_ = nullptr; | |
| 378 #if defined(OS_WIN) | |
| 379 readonly_allocator_handle_ = 0; | |
| 380 #endif | |
| 335 } | 381 } |
| 336 | 382 |
| 337 FieldTrialList::~FieldTrialList() { | 383 FieldTrialList::~FieldTrialList() { |
| 338 AutoLock auto_lock(lock_); | 384 AutoLock auto_lock(lock_); |
| 339 while (!registered_.empty()) { | 385 while (!registered_.empty()) { |
| 340 RegistrationMap::iterator it = registered_.begin(); | 386 RegistrationMap::iterator it = registered_.begin(); |
| 341 it->second->Release(); | 387 it->second->Release(); |
| 342 registered_.erase(it->first); | 388 registered_.erase(it->first); |
| 343 } | 389 } |
| 344 DCHECK_EQ(this, global_); | 390 DCHECK_EQ(this, global_); |
| (...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 579 std::string arg = cmd_line.GetSwitchValueASCII(field_trial_handle_switch); | 625 std::string arg = cmd_line.GetSwitchValueASCII(field_trial_handle_switch); |
| 580 size_t token = arg.find(","); | 626 size_t token = arg.find(","); |
| 581 int field_trial_handle = std::stoi(arg.substr(0, token)); | 627 int field_trial_handle = std::stoi(arg.substr(0, token)); |
| 582 int field_trial_length = std::stoi(arg.substr(token + 1, arg.length())); | 628 int field_trial_length = std::stoi(arg.substr(token + 1, arg.length())); |
| 583 | 629 |
| 584 HANDLE handle = reinterpret_cast<HANDLE>(field_trial_handle); | 630 HANDLE handle = reinterpret_cast<HANDLE>(field_trial_handle); |
| 585 base::SharedMemoryHandle shm_handle = | 631 base::SharedMemoryHandle shm_handle = |
| 586 base::SharedMemoryHandle(handle, base::GetCurrentProcId()); | 632 base::SharedMemoryHandle(handle, base::GetCurrentProcId()); |
| 587 | 633 |
| 588 // Gets deleted when it gets out of scope, but that's OK because we need it | 634 // 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. | 635 // only for the duration of this method. |
| 590 base::SharedMemory shared_memory(shm_handle, false); | 636 std::unique_ptr<base::SharedMemory> shm( |
| 591 shared_memory.Map(field_trial_length); | 637 new base::SharedMemory(shm_handle, true)); |
| 638 if (!shm.get()->Map(field_trial_length)) | |
| 639 base::TerminateBecauseOutOfMemory(field_trial_length); | |
| 592 | 640 |
| 593 char* field_trial_state = static_cast<char*>(shared_memory.memory()); | 641 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; | 642 return; |
| 598 } | 643 } |
| 599 #endif | 644 #endif |
| 600 | 645 |
| 601 if (cmd_line.HasSwitch(switches::kForceFieldTrials)) { | 646 if (cmd_line.HasSwitch(switches::kForceFieldTrials)) { |
| 602 bool result = FieldTrialList::CreateTrialsFromString( | 647 bool result = FieldTrialList::CreateTrialsFromString( |
| 603 cmd_line.GetSwitchValueASCII(switches::kForceFieldTrials), | 648 cmd_line.GetSwitchValueASCII(switches::kForceFieldTrials), |
| 604 std::set<std::string>()); | 649 std::set<std::string>()); |
| 605 DCHECK(result); | 650 DCHECK(result); |
| 606 } | 651 } |
| 607 } | 652 } |
| 608 | 653 |
| 654 #if defined(OS_WIN) | |
| 655 // static | |
| 656 void FieldTrialList::AppendFieldTrialHandleIfNeeded( | |
| 657 base::HandlesToInheritVector* handles) { | |
| 658 FieldTrialList::InstantiateFieldTrialAllocatorIfNeeded(); | |
|
Alexei Svitkine (slow)
2016/10/24 14:40:27
Wait, what happened to this line?
Are you removin
lawrencewu
2016/10/24 15:35:13
Added the line back. For some reason I thought I w
| |
| 659 | |
| 660 // Need to get a new, read-only handle to share to the child. | |
| 661 handles->push_back(global_->readonly_allocator_handle_); | |
| 662 } | |
| 663 #endif | |
| 664 | |
| 665 // static | |
| 666 void FieldTrialList::CreateTrialsFromSharedMemory( | |
| 667 std::unique_ptr<base::SharedMemory> shm) { | |
| 668 const base::SharedPersistentMemoryAllocator shalloc(std::move(shm), 0, | |
| 669 kAllocatorName, true); | |
| 670 base::PersistentMemoryAllocator::Iterator iter(&shalloc); | |
| 671 | |
| 672 base::SharedPersistentMemoryAllocator::Reference ref; | |
| 673 while ((ref = iter.GetNextOfType(kFieldTrialType)) != | |
| 674 base::SharedPersistentMemoryAllocator::kReferenceNull) { | |
| 675 const FieldTrialEntry* entry = | |
| 676 shalloc.GetAsObject<const FieldTrialEntry>(ref, kFieldTrialType); | |
| 677 FieldTrial* trial = | |
| 678 CreateFieldTrial(entry->GetTrialName(), entry->GetGroupName()); | |
| 679 | |
| 680 if (entry->activated) { | |
| 681 // Call |group()| to mark the trial as "used" and notify observers, if | |
| 682 // any. This is useful to ensure that field trials created in child | |
| 683 // processes are properly reported in crash reports. | |
| 684 trial->group(); | |
| 685 } | |
| 686 } | |
| 687 } | |
| 688 | |
| 609 // static | 689 // static |
| 610 std::unique_ptr<base::SharedMemory> FieldTrialList::CopyFieldTrialStateToFlags( | 690 void FieldTrialList::CopyFieldTrialStateToFlags( |
| 611 const char* field_trial_handle_switch, | 691 const char* field_trial_handle_switch, |
| 612 base::CommandLine* cmd_line) { | 692 base::CommandLine* cmd_line) { |
| 693 #if defined(OS_WIN) | |
| 694 // Use shared memory to pass the state if the feature is enabled, otherwise | |
| 695 // fallback to passing it via the command line as a string. | |
| 696 if (kUseSharedMemoryForFieldTrials) { | |
| 697 FieldTrialList::InstantiateFieldTrialAllocatorIfNeeded(); | |
| 698 // HANDLE is just typedef'd to void *. We basically cast the handle into an | |
| 699 // int (uintptr_t, to be exact), stringify the int, and pass it as a | |
| 700 // command-line flag. The child process will do the reverse conversions to | |
| 701 // retrieve the handle. See http://stackoverflow.com/a/153077 | |
| 702 auto uintptr_handle = | |
| 703 reinterpret_cast<uintptr_t>(global_->readonly_allocator_handle_); | |
| 704 size_t field_trial_length = | |
| 705 global_->field_trial_allocator_->shared_memory()->mapped_size(); | |
| 706 std::string field_trial_handle = std::to_string(uintptr_handle) + "," + | |
| 707 std::to_string(field_trial_length); | |
| 708 | |
| 709 cmd_line->AppendSwitchASCII(field_trial_handle_switch, field_trial_handle); | |
| 710 return; | |
| 711 } | |
| 712 #endif | |
| 713 | |
| 613 std::string field_trial_states; | 714 std::string field_trial_states; |
| 614 base::FieldTrialList::AllStatesToString(&field_trial_states); | 715 base::FieldTrialList::AllStatesToString(&field_trial_states); |
| 615 if (!field_trial_states.empty()) { | 716 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, | 717 cmd_line->AppendSwitchASCII(switches::kForceFieldTrials, |
| 637 field_trial_states); | 718 field_trial_states); |
| 638 } | 719 } |
| 639 return std::unique_ptr<base::SharedMemory>(nullptr); | |
| 640 } | 720 } |
| 641 | 721 |
| 642 // static | 722 // static |
| 643 FieldTrial* FieldTrialList::CreateFieldTrial( | 723 FieldTrial* FieldTrialList::CreateFieldTrial( |
| 644 const std::string& name, | 724 const std::string& name, |
| 645 const std::string& group_name) { | 725 const std::string& group_name) { |
| 646 DCHECK(global_); | 726 DCHECK(global_); |
| 647 DCHECK_GE(name.size(), 0u); | 727 DCHECK_GE(name.size(), 0u); |
| 648 DCHECK_GE(group_name.size(), 0u); | 728 DCHECK_GE(group_name.size(), 0u); |
| 649 if (name.empty() || group_name.empty() || !global_) | 729 if (name.empty() || group_name.empty() || !global_) |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 682 // static | 762 // static |
| 683 void FieldTrialList::NotifyFieldTrialGroupSelection(FieldTrial* field_trial) { | 763 void FieldTrialList::NotifyFieldTrialGroupSelection(FieldTrial* field_trial) { |
| 684 if (!global_) | 764 if (!global_) |
| 685 return; | 765 return; |
| 686 | 766 |
| 687 { | 767 { |
| 688 AutoLock auto_lock(global_->lock_); | 768 AutoLock auto_lock(global_->lock_); |
| 689 if (field_trial->group_reported_) | 769 if (field_trial->group_reported_) |
| 690 return; | 770 return; |
| 691 field_trial->group_reported_ = true; | 771 field_trial->group_reported_ = true; |
| 772 | |
| 773 if (!field_trial->enable_field_trial_) | |
| 774 return; | |
| 775 | |
| 776 if (kUseSharedMemoryForFieldTrials) | |
|
Alexei Svitkine (slow)
2016/10/21 20:52:21
Nit: {} since now it's more than one line.
lawrencewu
2016/10/23 20:01:56
Done.
Alexei Svitkine (slow)
2016/10/24 14:40:27
Doesn't look done to me.
lawrencewu
2016/10/24 15:35:13
Woops, actually done now.
| |
| 777 field_trial->AddToAllocatorWhileLocked( | |
| 778 global_->field_trial_allocator_.get()); | |
| 692 } | 779 } |
| 693 | 780 |
| 694 if (!field_trial->enable_field_trial_) | |
| 695 return; | |
| 696 | |
| 697 global_->observer_list_->Notify( | 781 global_->observer_list_->Notify( |
| 698 FROM_HERE, &FieldTrialList::Observer::OnFieldTrialGroupFinalized, | 782 FROM_HERE, &FieldTrialList::Observer::OnFieldTrialGroupFinalized, |
| 699 field_trial->trial_name(), field_trial->group_name_internal()); | 783 field_trial->trial_name(), field_trial->group_name_internal()); |
| 700 } | 784 } |
| 701 | 785 |
| 702 // static | 786 // static |
| 787 void FieldTrialList::InstantiateFieldTrialAllocatorIfNeeded() { | |
| 788 AutoLock auto_lock(global_->lock_); | |
| 789 // Create the allocator if not already created and add all existing trials. | |
| 790 if (global_->field_trial_allocator_ != nullptr) | |
| 791 return; | |
| 792 | |
| 793 std::unique_ptr<base::SharedMemory> shm(new base::SharedMemory()); | |
| 794 if (!shm->CreateAndMapAnonymous(kFieldTrialAllocationSize)) | |
| 795 base::TerminateBecauseOutOfMemory(kFieldTrialAllocationSize); | |
| 796 | |
| 797 // TODO(lawrencewu): call UpdateTrackingHistograms() when all field trials | |
| 798 // have been registered (perhaps in the destructor?) | |
| 799 global_->field_trial_allocator_.reset( | |
| 800 new base::SharedPersistentMemoryAllocator(std::move(shm), 0, | |
| 801 kAllocatorName, false)); | |
| 802 global_->field_trial_allocator_->CreateTrackingHistograms(kAllocatorName); | |
| 803 | |
| 804 // Add all existing field trials. | |
| 805 for (const auto& registered : global_->registered_) { | |
| 806 registered.second->AddToAllocatorWhileLocked( | |
| 807 global_->field_trial_allocator_.get()); | |
| 808 } | |
| 809 | |
| 810 #if defined(OS_WIN) | |
| 811 // Set |readonly_allocator_handle_| so we can pass it to be inherited and via | |
| 812 // the command line. | |
| 813 global_->readonly_allocator_handle_ = | |
| 814 CreateReadOnlyHandle(global_->field_trial_allocator_.get()); | |
| 815 #endif | |
| 816 } | |
| 817 | |
| 818 void FieldTrial::AddToAllocatorWhileLocked( | |
| 819 base::SharedPersistentMemoryAllocator* allocator) { | |
| 820 // Don't do anything if the allocator hasn't been instantiated yet. | |
| 821 if (allocator == nullptr) | |
| 822 return; | |
| 823 | |
| 824 // We allocate just enough memory to fit the FTE struct, trial name, and group | |
| 825 // name. If there's no more memory available, quit early. | |
| 826 FieldTrial::State trial_state; | |
|
Alexei Svitkine (slow)
2016/10/21 20:52:21
Nit: Do you ned FieldTrial:: here?
lawrencewu
2016/10/23 20:01:56
Nope, removed.
| |
| 827 this->GetState(&trial_state); | |
|
Alexei Svitkine (slow)
2016/10/21 20:52:21
Nit: No need for this->
lawrencewu
2016/10/23 20:01:56
Done.
| |
| 828 | |
| 829 std::string trial_name; | |
|
Alexei Svitkine (slow)
2016/10/21 20:52:21
I don't think these string copies here are necessa
lawrencewu
2016/10/23 20:01:56
Done. The memory is guaranteed to be 0-initialized
Alexei Svitkine (slow)
2016/10/24 14:40:27
Let's just explicitly 0 the null terminator charac
lawrencewu
2016/10/24 15:35:13
Sure, done.
| |
| 830 trial_state.trial_name.CopyToString(&trial_name); | |
| 831 size_t trial_name_size = trial_name.size() + 1; | |
| 832 | |
| 833 std::string group_name; | |
| 834 trial_state.group_name.CopyToString(&group_name); | |
| 835 size_t group_name_size = group_name.size() + 1; | |
| 836 | |
| 837 uint32_t trial_name_offset = sizeof(FieldTrialEntry); | |
| 838 uint32_t group_name_offset = trial_name_offset + trial_name_size; | |
| 839 size_t size = sizeof(FieldTrialEntry) + trial_name_size + group_name_size; | |
| 840 | |
| 841 base::SharedPersistentMemoryAllocator::Reference ref = | |
| 842 allocator->Allocate(size, kFieldTrialType); | |
| 843 if (ref == base::SharedPersistentMemoryAllocator::kReferenceNull) | |
| 844 return; | |
| 845 | |
| 846 FieldTrialEntry* dst = | |
| 847 allocator->GetAsObject<FieldTrialEntry>(ref, kFieldTrialType); | |
| 848 FieldTrialEntry entry = {trial_state.activated, group_name_offset}; | |
| 849 *dst = entry; | |
|
Alexei Svitkine (slow)
2016/10/21 20:52:21
Nit:
Instead of the extra copy, how about:
Field
lawrencewu
2016/10/23 20:01:56
Yup that's definitely cleaner. Done.
| |
| 850 memcpy(reinterpret_cast<char*>(dst) + trial_name_offset, trial_name.c_str(), | |
| 851 trial_name_size); | |
| 852 memcpy(reinterpret_cast<char*>(dst) + group_name_offset, group_name.c_str(), | |
| 853 group_name_size); | |
| 854 | |
| 855 allocator->MakeIterable(ref); | |
| 856 } | |
| 857 | |
| 858 // static | |
| 703 size_t FieldTrialList::GetFieldTrialCount() { | 859 size_t FieldTrialList::GetFieldTrialCount() { |
| 704 if (!global_) | 860 if (!global_) |
| 705 return 0; | 861 return 0; |
| 706 AutoLock auto_lock(global_->lock_); | 862 AutoLock auto_lock(global_->lock_); |
| 707 return global_->registered_.size(); | 863 return global_->registered_.size(); |
| 708 } | 864 } |
| 709 | 865 |
| 710 // static | 866 // static |
| 711 const FieldTrial::EntropyProvider* | 867 const FieldTrial::EntropyProvider* |
| 712 FieldTrialList::GetEntropyProviderForOneTimeRandomization() { | 868 FieldTrialList::GetEntropyProviderForOneTimeRandomization() { |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 732 return; | 888 return; |
| 733 } | 889 } |
| 734 AutoLock auto_lock(global_->lock_); | 890 AutoLock auto_lock(global_->lock_); |
| 735 CHECK(!global_->PreLockedFind(trial->trial_name())) << trial->trial_name(); | 891 CHECK(!global_->PreLockedFind(trial->trial_name())) << trial->trial_name(); |
| 736 trial->AddRef(); | 892 trial->AddRef(); |
| 737 trial->SetTrialRegistered(); | 893 trial->SetTrialRegistered(); |
| 738 global_->registered_[trial->trial_name()] = trial; | 894 global_->registered_[trial->trial_name()] = trial; |
| 739 } | 895 } |
| 740 | 896 |
| 741 } // namespace base | 897 } // namespace base |
| OLD | NEW |