OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "components/startup_metric_utils/common/pre_read_field_trial_utils_win. h" | 5 #include "components/startup_metric_utils/common/pre_read_field_trial_utils_win. h" |
6 | 6 |
7 #include "base/callback.h" | 7 #include "base/callback.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/metrics/field_trial.h" | 9 #include "base/metrics/field_trial.h" |
10 #include "base/strings/string_util.h" | 10 #include "base/strings/string_util.h" |
(...skipping 17 matching lines...) Expand all Loading... | |
28 // Variation names for the PreRead field trial. | 28 // Variation names for the PreRead field trial. |
29 const base::char16 kNoPreReadVariationName[] = L"NoPreRead"; | 29 const base::char16 kNoPreReadVariationName[] = L"NoPreRead"; |
30 const base::char16 kHighPriorityVariationName[] = L"HighPriority"; | 30 const base::char16 kHighPriorityVariationName[] = L"HighPriority"; |
31 const base::char16 kOnlyIfColdVariationName[] = L"OnlyIfCold"; | 31 const base::char16 kOnlyIfColdVariationName[] = L"OnlyIfCold"; |
32 const base::char16 kPrefetchVirtualMemoryVariationName[] = | 32 const base::char16 kPrefetchVirtualMemoryVariationName[] = |
33 L"PrefetchVirtualMemory"; | 33 L"PrefetchVirtualMemory"; |
34 | 34 |
35 // Registry key in which the PreRead field trial group is stored. | 35 // Registry key in which the PreRead field trial group is stored. |
36 const base::char16 kPreReadFieldTrialRegistryKey[] = L"\\PreReadFieldTrial"; | 36 const base::char16 kPreReadFieldTrialRegistryKey[] = L"\\PreReadFieldTrial"; |
37 | 37 |
38 // Pre-read options to use for the current process. This is initialized by | |
39 // InitializePreReadOptions(). | |
40 int g_pre_read_options = 0; | |
41 | |
42 // True once |g_pre_read_options| has been initialized. | |
43 bool g_pre_read_options_initialized = false; | |
gab
2016/01/20 20:09:39
I'd like to avoid this global in non-dcheck builds
fdoray
2016/01/20 21:58:53
Done, I added a PRE_READ_OPTION_UNINITIALIZED bit.
| |
44 | |
38 // Returns the registry path in which the PreRead group is stored. | 45 // Returns the registry path in which the PreRead group is stored. |
39 base::string16 GetPreReadRegistryPath( | 46 base::string16 GetPreReadRegistryPath( |
40 const base::string16& product_registry_path) { | 47 const base::string16& product_registry_path) { |
41 return product_registry_path + kPreReadFieldTrialRegistryKey; | 48 return product_registry_path + kPreReadFieldTrialRegistryKey; |
42 } | 49 } |
43 | 50 |
44 } // namespace | 51 } // namespace |
45 | 52 |
46 void GetPreReadOptions(const base::string16& product_registry_path, | 53 void InitializePreReadOptions(const base::string16& product_registry_path) { |
47 bool* no_pre_read, | |
48 bool* high_priority, | |
49 bool* only_if_cold, | |
50 bool* prefetch_virtual_memory) { | |
51 DCHECK(!product_registry_path.empty()); | 54 DCHECK(!product_registry_path.empty()); |
52 DCHECK(no_pre_read); | 55 DCHECK(!g_pre_read_options_initialized); |
53 DCHECK(high_priority); | |
54 DCHECK(only_if_cold); | |
55 DCHECK(prefetch_virtual_memory); | |
56 | 56 |
57 // Open the PreRead field trial's registry key. | 57 // Open the PreRead field trial's registry key. |
58 const base::string16 registry_path = | 58 const base::string16 registry_path = |
59 GetPreReadRegistryPath(product_registry_path); | 59 GetPreReadRegistryPath(product_registry_path); |
60 const base::win::RegKey key(HKEY_CURRENT_USER, registry_path.c_str(), | 60 const base::win::RegKey key(HKEY_CURRENT_USER, registry_path.c_str(), |
61 KEY_QUERY_VALUE); | 61 KEY_QUERY_VALUE); |
62 | 62 |
63 // Set the PreRead field trial's options. | 63 // Set the PreRead field trial's options. |
64 struct VariationMapping { | 64 struct VariationMapping { |
65 const base::char16* name; | 65 const base::char16* name; |
66 bool* variable; | 66 PreReadOptions bit; |
67 } const variations_mappings[] = { | 67 } const variations_mappings[] = { |
68 {kNoPreReadVariationName, no_pre_read}, | 68 {kNoPreReadVariationName, PRE_READ_OPTION_NO_PRE_READ}, |
69 {kHighPriorityVariationName, high_priority}, | 69 {kHighPriorityVariationName, PRE_READ_OPTION_HIGH_PRIORITY}, |
70 {kOnlyIfColdVariationName, only_if_cold}, | 70 {kOnlyIfColdVariationName, PRE_READ_OPTION_ONLY_IF_COLD}, |
71 {kPrefetchVirtualMemoryVariationName, prefetch_virtual_memory}, | 71 {kPrefetchVirtualMemoryVariationName, |
72 PRE_READ_OPTION_PREFETCH_VIRTUAL_MEMORY}, | |
72 }; | 73 }; |
73 | 74 |
74 for (const auto& mapping : variations_mappings) { | 75 for (const auto& mapping : variations_mappings) { |
75 // Set the option variable to true if the corresponding value is found in | 76 // Set the option variable to true if the corresponding value is found in |
76 // the registry. Set to false otherwise (default behavior). | 77 // the registry. Set to false otherwise (default behavior). |
77 DWORD value = 0; | 78 DWORD value = 0; |
78 *mapping.variable = key.ReadValueDW(mapping.name, &value) == ERROR_SUCCESS; | 79 if (key.ReadValueDW(mapping.name, &value) == ERROR_SUCCESS) { |
79 DCHECK(!*mapping.variable || value == 1); | 80 DCHECK(value == 1); |
gab
2016/01/20 20:09:40
DCHECK_EQ(1, value)
fdoray
2016/01/20 21:58:53
Done.
| |
81 g_pre_read_options &= mapping.bit; | |
82 } | |
80 } | 83 } |
84 | |
85 g_pre_read_options_initialized = true; | |
86 } | |
87 | |
88 int GetPreReadOptions() { | |
89 DCHECK(g_pre_read_options_initialized); | |
90 return g_pre_read_options; | |
81 } | 91 } |
82 | 92 |
83 void UpdatePreReadOptions(const base::string16& product_registry_path) { | 93 void UpdatePreReadOptions(const base::string16& product_registry_path) { |
84 DCHECK(!product_registry_path.empty()); | 94 DCHECK(!product_registry_path.empty()); |
85 | 95 |
86 const base::string16 registry_path = | 96 const base::string16 registry_path = |
87 GetPreReadRegistryPath(product_registry_path); | 97 GetPreReadRegistryPath(product_registry_path); |
88 | 98 |
89 // Clear the experiment key. That way, when the trial is shut down, the | 99 // Clear the experiment key. That way, when the trial is shut down, the |
90 // registry will be cleaned automatically. Also, this prevents variation | 100 // registry will be cleaned automatically. Also, this prevents variation |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
137 base::string16 group; | 147 base::string16 group; |
138 key.ReadValue(L"", &group); | 148 key.ReadValue(L"", &group); |
139 | 149 |
140 if (!group.empty()) { | 150 if (!group.empty()) { |
141 register_synthetic_field_trial.Run(kPreReadSyntheticFieldTrialName, | 151 register_synthetic_field_trial.Run(kPreReadSyntheticFieldTrialName, |
142 base::UTF16ToUTF8(group)); | 152 base::UTF16ToUTF8(group)); |
143 } | 153 } |
144 } | 154 } |
145 | 155 |
146 } // namespace startup_metric_utils | 156 } // namespace startup_metric_utils |
OLD | NEW |