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/metrics/histogram_macros.h" | 15 #include "base/metrics/histogram_macros.h" |
16 #include "base/pickle.h" | |
16 #include "base/process/memory.h" | 17 #include "base/process/memory.h" |
17 #include "base/rand_util.h" | 18 #include "base/rand_util.h" |
18 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
19 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
20 #include "base/strings/stringprintf.h" | 21 #include "base/strings/stringprintf.h" |
21 #include "base/strings/utf_string_conversions.h" | 22 #include "base/strings/utf_string_conversions.h" |
22 | 23 |
23 namespace base { | 24 namespace base { |
24 | 25 |
25 namespace { | 26 namespace { |
(...skipping 13 matching lines...) Expand all Loading... | |
39 // crbug.com/653874 | 40 // crbug.com/653874 |
40 const bool kUseSharedMemoryForFieldTrials = false; | 41 const bool kUseSharedMemoryForFieldTrials = false; |
41 | 42 |
42 // Constants for the field trial allocator. | 43 // Constants for the field trial allocator. |
43 const char kAllocatorName[] = "FieldTrialAllocator"; | 44 const char kAllocatorName[] = "FieldTrialAllocator"; |
44 const uint32_t kFieldTrialType = 0xABA17E13 + 1; // SHA1(FieldTrialEntry) v1 | 45 const uint32_t kFieldTrialType = 0xABA17E13 + 1; // SHA1(FieldTrialEntry) v1 |
45 #if !defined(OS_NACL) | 46 #if !defined(OS_NACL) |
46 const size_t kFieldTrialAllocationSize = 4 << 10; // 4 KiB = one page | 47 const size_t kFieldTrialAllocationSize = 4 << 10; // 4 KiB = one page |
47 #endif | 48 #endif |
48 | 49 |
49 // We create one FieldTrialEntry struct per field trial in shared memory (via | 50 // We create one FieldTrialEntry per field trial in shared memory, via |
50 // field_trial_allocator_). It contains whether or not it's activated, and the | 51 // AddToAllocatorWhileLocked. It contains whether or not it's activated, and a |
51 // offset from the start of the allocated memory to the group name. We don't | 52 // base::Pickle object that we unpickle and read from. |
52 // need the offset into the trial name because that's always a fixed position | |
53 // from the start of the struct. Two strings will be appended to the end of this | |
54 // structure in allocated memory, so the result will look like this: | |
55 // --------------------------------- | |
56 // | fte | trial_name | group_name | | |
57 // --------------------------------- | |
58 struct FieldTrialEntry { | 53 struct FieldTrialEntry { |
59 bool activated; | 54 bool activated; |
60 uint32_t group_name_offset; | 55 // Size of the pickled structure, NOT the total size of this entry. |
61 | 56 uint32_t size; |
62 const char* GetTrialName() const { | 57 // Space for this will be added during allocation. This is really variable |
63 const char* src = | 58 // length but we can't have flexible arrays in structs. |
64 reinterpret_cast<char*>(const_cast<FieldTrialEntry*>(this)); | 59 char data[1]; |
65 return src + sizeof(FieldTrialEntry); | |
66 } | |
67 | |
68 const char* GetGroupName() const { | |
69 const char* src = | |
70 reinterpret_cast<char*>(const_cast<FieldTrialEntry*>(this)); | |
71 return src + this->group_name_offset; | |
72 } | |
73 }; | 60 }; |
74 | 61 |
75 // Created a time value based on |year|, |month| and |day_of_month| parameters. | 62 // Created a time value based on |year|, |month| and |day_of_month| parameters. |
76 Time CreateTimeFromParams(int year, int month, int day_of_month) { | 63 Time CreateTimeFromParams(int year, int month, int day_of_month) { |
77 DCHECK_GT(year, 1970); | 64 DCHECK_GT(year, 1970); |
78 DCHECK_GT(month, 0); | 65 DCHECK_GT(month, 0); |
79 DCHECK_LT(month, 13); | 66 DCHECK_LT(month, 13); |
80 DCHECK_GT(day_of_month, 0); | 67 DCHECK_GT(day_of_month, 0); |
81 DCHECK_LT(day_of_month, 32); | 68 DCHECK_LT(day_of_month, 32); |
82 | 69 |
(...skipping 708 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
791 return 0; | 778 return 0; |
792 AutoLock auto_lock(global_->lock_); | 779 AutoLock auto_lock(global_->lock_); |
793 return global_->registered_.size(); | 780 return global_->registered_.size(); |
794 } | 781 } |
795 | 782 |
796 // static | 783 // static |
797 void FieldTrialList::CreateTrialsFromSharedMemory( | 784 void FieldTrialList::CreateTrialsFromSharedMemory( |
798 std::unique_ptr<SharedMemory> shm) { | 785 std::unique_ptr<SharedMemory> shm) { |
799 const SharedPersistentMemoryAllocator shalloc(std::move(shm), 0, | 786 const SharedPersistentMemoryAllocator shalloc(std::move(shm), 0, |
800 kAllocatorName, true); | 787 kAllocatorName, true); |
801 PersistentMemoryAllocator::Iterator iter(&shalloc); | 788 PersistentMemoryAllocator::Iterator mem_iter(&shalloc); |
802 | 789 |
803 SharedPersistentMemoryAllocator::Reference ref; | 790 SharedPersistentMemoryAllocator::Reference ref; |
804 while ((ref = iter.GetNextOfType(kFieldTrialType)) != | 791 while ((ref = mem_iter.GetNextOfType(kFieldTrialType)) != |
805 SharedPersistentMemoryAllocator::kReferenceNull) { | 792 SharedPersistentMemoryAllocator::kReferenceNull) { |
806 const FieldTrialEntry* entry = | 793 const FieldTrialEntry* entry = |
807 shalloc.GetAsObject<const FieldTrialEntry>(ref, kFieldTrialType); | 794 shalloc.GetAsObject<const FieldTrialEntry>(ref, kFieldTrialType); |
795 Pickle pickle(entry->data, entry->size); | |
796 PickleIterator pickle_iter(pickle); | |
797 | |
798 StringPiece trial_name; | |
799 StringPiece group_name; | |
800 if (!pickle_iter.ReadStringPiece(&trial_name)) | |
801 continue; | |
Alexei Svitkine (slow)
2016/10/27 21:36:50
I'm not a fan of this error handling. Can you make
lawrencewu
2016/10/28 14:23:03
Done.
| |
802 if (!pickle_iter.ReadStringPiece(&group_name)) | |
803 continue; | |
808 FieldTrial* trial = | 804 FieldTrial* trial = |
809 CreateFieldTrial(entry->GetTrialName(), entry->GetGroupName()); | 805 CreateFieldTrial(trial_name.as_string(), group_name.as_string()); |
Alexei Svitkine (slow)
2016/10/27 21:05:02
Can you add a TODO above the statement to convert
lawrencewu
2016/10/27 21:24:19
Done.
| |
810 | 806 |
811 if (entry->activated) { | 807 if (entry->activated) { |
812 // Call |group()| to mark the trial as "used" and notify observers, if | 808 // Call |group()| to mark the trial as "used" and notify observers, if |
813 // any. This is useful to ensure that field trials created in child | 809 // any. This is useful to ensure that field trials created in child |
814 // processes are properly reported in crash reports. | 810 // processes are properly reported in crash reports. |
815 trial->group(); | 811 trial->group(); |
816 } | 812 } |
817 } | 813 } |
818 } | 814 } |
819 | 815 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
859 return; | 855 return; |
860 | 856 |
861 // Or if we've already added it. | 857 // Or if we've already added it. |
862 if (field_trial->ref_ != SharedPersistentMemoryAllocator::kReferenceNull) | 858 if (field_trial->ref_ != SharedPersistentMemoryAllocator::kReferenceNull) |
863 return; | 859 return; |
864 | 860 |
865 FieldTrial::State trial_state; | 861 FieldTrial::State trial_state; |
866 if (!field_trial->GetState(&trial_state)) | 862 if (!field_trial->GetState(&trial_state)) |
867 return; | 863 return; |
868 | 864 |
869 size_t trial_name_size = trial_state.trial_name.size() + 1; | 865 Pickle pickle; |
870 size_t group_name_size = trial_state.group_name.size() + 1; | 866 pickle.WriteString(trial_state.trial_name); |
871 size_t size = sizeof(FieldTrialEntry) + trial_name_size + group_name_size; | 867 pickle.WriteString(trial_state.group_name); |
872 | 868 |
873 // Allocate just enough memory to fit the FieldTrialEntry struct, trial name, | 869 size_t total_size = sizeof(bool) + +sizeof(uint32_t) + pickle.size(); |
Alexei Svitkine (slow)
2016/10/27 21:05:02
You have an extra + in front of sizeof.
lawrencewu
2016/10/27 21:24:19
Fixed.
Alexei Svitkine (slow)
2016/10/27 21:36:49
This size calculation seems error prone.
How abou
lawrencewu
2016/10/28 14:23:03
That sounds better, done.
Alexei Svitkine (slow)
2016/10/28 15:25:29
I don't see the change - did you forget to upload
| |
874 // and group name. | |
875 SharedPersistentMemoryAllocator::Reference ref = | 870 SharedPersistentMemoryAllocator::Reference ref = |
876 allocator->Allocate(size, kFieldTrialType); | 871 allocator->Allocate(total_size, kFieldTrialType); |
877 if (ref == SharedPersistentMemoryAllocator::kReferenceNull) | 872 if (ref == SharedPersistentMemoryAllocator::kReferenceNull) |
878 return; | 873 return; |
879 | 874 |
880 // Calculate offsets into the shared memory segment. | |
881 FieldTrialEntry* entry = | 875 FieldTrialEntry* entry = |
882 allocator->GetAsObject<FieldTrialEntry>(ref, kFieldTrialType); | 876 allocator->GetAsObject<FieldTrialEntry>(ref, kFieldTrialType); |
883 uint32_t trial_name_offset = sizeof(FieldTrialEntry); | |
884 uint32_t group_name_offset = trial_name_offset + trial_name_size; | |
885 | |
886 // Copy over the data. | |
887 entry->activated = trial_state.activated; | 877 entry->activated = trial_state.activated; |
888 entry->group_name_offset = group_name_offset; | 878 entry->size = pickle.size(); |
889 char* trial_name = reinterpret_cast<char*>(entry) + trial_name_offset; | 879 memcpy(entry->data, pickle.data(), pickle.size()); |
Alexei Svitkine (slow)
2016/10/27 21:05:02
This is doing an extra copy. Let's avoid that - si
lawrencewu
2016/10/27 21:24:19
I'm not sure we should do this. PickleSizer return
Alexei Svitkine (slow)
2016/10/27 21:36:49
PickleSizer specifically documents that it's meant
lawrencewu
2016/10/28 14:23:03
The docs say that PickleSizer accurately computes
| |
890 char* group_name = reinterpret_cast<char*>(entry) + group_name_offset; | |
891 memcpy(trial_name, trial_state.trial_name.data(), trial_name_size); | |
892 memcpy(group_name, trial_state.group_name.data(), group_name_size); | |
893 | |
894 // Null terminate the strings. | |
895 trial_name[trial_state.trial_name.size()] = '\0'; | |
896 group_name[trial_state.group_name.size()] = '\0'; | |
897 | 880 |
898 allocator->MakeIterable(ref); | 881 allocator->MakeIterable(ref); |
899 field_trial->ref_ = ref; | 882 field_trial->ref_ = ref; |
900 } | 883 } |
901 | 884 |
902 // static | 885 // static |
903 void FieldTrialList::ActivateFieldTrialEntryWhileLocked( | 886 void FieldTrialList::ActivateFieldTrialEntryWhileLocked( |
904 FieldTrial* field_trial) { | 887 FieldTrial* field_trial) { |
905 SharedPersistentMemoryAllocator* allocator = | 888 SharedPersistentMemoryAllocator* allocator = |
906 global_->field_trial_allocator_.get(); | 889 global_->field_trial_allocator_.get(); |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
944 return; | 927 return; |
945 } | 928 } |
946 AutoLock auto_lock(global_->lock_); | 929 AutoLock auto_lock(global_->lock_); |
947 CHECK(!global_->PreLockedFind(trial->trial_name())) << trial->trial_name(); | 930 CHECK(!global_->PreLockedFind(trial->trial_name())) << trial->trial_name(); |
948 trial->AddRef(); | 931 trial->AddRef(); |
949 trial->SetTrialRegistered(); | 932 trial->SetTrialRegistered(); |
950 global_->registered_[trial->trial_name()] = trial; | 933 global_->registered_[trial->trial_name()] = trial; |
951 } | 934 } |
952 | 935 |
953 } // namespace base | 936 } // namespace base |
OLD | NEW |