Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(125)

Side by Side Diff: chrome/app/client_util.cc

Issue 23534009: Re-enable pre-read experiment as a finch field trial. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address comments from chrisha and asvitkine. Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 <windows.h> 5 #include <windows.h>
6 #include <shlwapi.h> 6 #include <shlwapi.h>
7 7
8 #include "base/command_line.h" 8 #include "base/command_line.h"
9 #include "base/debug/trace_event.h" 9 #include "base/debug/trace_event.h"
10 #include "base/environment.h" 10 #include "base/environment.h"
11 #include "base/file_version_info.h" 11 #include "base/file_version_info.h"
12 #include "base/logging.h" 12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_ptr.h"
14 #include "base/rand_util.h" // For PreRead experiment.
15 #include "base/sha1.h" // For PreRead experiment.
14 #include "base/strings/string16.h" 16 #include "base/strings/string16.h"
15 #include "base/strings/string_util.h" 17 #include "base/strings/string_util.h"
16 #include "base/strings/stringprintf.h" 18 #include "base/strings/stringprintf.h"
17 #include "base/strings/utf_string_conversions.h" 19 #include "base/strings/utf_string_conversions.h"
18 #include "base/version.h" 20 #include "base/version.h"
19 #include "base/win/windows_version.h" 21 #include "base/win/windows_version.h"
20 #include "chrome/app/breakpad_win.h" 22 #include "chrome/app/breakpad_win.h"
21 #include "chrome/app/client_util.h" 23 #include "chrome/app/client_util.h"
22 #include "chrome/app/image_pre_reader_win.h" 24 #include "chrome/app/image_pre_reader_win.h"
23 #include "chrome/common/chrome_constants.h" 25 #include "chrome/common/chrome_constants.h"
24 #include "chrome/common/chrome_result_codes.h" 26 #include "chrome/common/chrome_result_codes.h"
25 #include "chrome/common/chrome_switches.h" 27 #include "chrome/common/chrome_switches.h"
26 #include "chrome/common/env_vars.h" 28 #include "chrome/common/env_vars.h"
27 #include "chrome/installer/util/browser_distribution.h" 29 #include "chrome/installer/util/browser_distribution.h"
28 #include "chrome/installer/util/channel_info.h" 30 #include "chrome/installer/util/channel_info.h"
29 #include "chrome/installer/util/google_update_constants.h" 31 #include "chrome/installer/util/google_update_constants.h"
30 #include "chrome/installer/util/google_update_settings.h" 32 #include "chrome/installer/util/google_update_settings.h"
31 #include "chrome/installer/util/install_util.h" 33 #include "chrome/installer/util/install_util.h"
32 #include "chrome/installer/util/util_constants.h" 34 #include "chrome/installer/util/util_constants.h"
33 35
34 namespace { 36 namespace {
35 // The entry point signature of chrome.dll. 37 // The entry point signature of chrome.dll.
36 typedef int (*DLL_MAIN)(HINSTANCE, sandbox::SandboxInterfaceInfo*); 38 typedef int (*DLL_MAIN)(HINSTANCE, sandbox::SandboxInterfaceInfo*);
37 39
38 typedef void (*RelaunchChromeBrowserWithNewCommandLineIfNeededFunc)(); 40 typedef void (*RelaunchChromeBrowserWithNewCommandLineIfNeededFunc)();
39 41
42 // Returns true if the build date for this module precedes the expiry date
43 // for the pre-read experiment.
44 bool PreReadExperimentIsActive() {
45 static const int kPreReadExpiryYear = 2014;
46 static const int kPreReadExpiryMonth = 7;
47 static const int kPreReadExpiryDay = 1;
48 static const char kBuildTimeStr[] = __DATE__ " " __TIME__;
Alexei Svitkine (slow) 2013/09/03 14:59:16 Nit: static keyword isn't needed for any of these
Roger McFarlane (Chromium) 2013/09/03 18:38:43 Done.
49
50 // Get the timestamp of the build.
51 base::Time build_time;
52 bool result = base::Time::FromString(kBuildTimeStr, &build_time);
53 DCHECK(result);
54
55 // Get the timestamp at which the experiment expires.
56 base::Time::Exploded exploded = {0};
57 exploded.year = kPreReadExpiryYear;
58 exploded.month = kPreReadExpiryMonth;
59 exploded.day_of_month = kPreReadExpiryDay;
60 base::Time expiration_time = base::Time::FromLocalExploded(exploded);
61
62 // Return true if the build time predates the expiration time..
63 return build_time < expiration_time;
64 }
65
66 // Get random unit values, i.e., in the range (0, 1), denoting a die-toss for
67 // being in an experiment population and experimental group thereof.
68 void GetPreReadPopulationAndGroup(double* population, double* group) {
69 DCHECK(population != NULL);
70 DCHECK(group != NULL);
71
72 // By default we use the metrics id for the user as stable pseudo-random
73 // input to a hash.
74 base::string16 metrics_id;
75 GoogleUpdateSettings::GetMetricsId(&metrics_id);
76
77 // If this user has not metrics id, we fall back to a purely random value
78 // per browser session.
79 const size_t kLength = 16;
80 std::string random_value(metrics_id.empty() ? base::RandBytesAsString(kLength)
81 : base::WideToUTF8(metrics_id));
82
83 // To interpret the value as a random number we hash it and read the first 8
84 // bytes of the hash as a unit-interval representing a die-toss for being in
85 // the experiment population and the second 8 bytes as a die-toss for being
86 // in various experiment groups.
87 unsigned char sha1_hash[base::kSHA1Length];
88 base::SHA1HashBytes(
89 reinterpret_cast<const unsigned char*>(random_value.c_str()),
90 random_value.size() * sizeof(random_value[0]),
91 sha1_hash);
92 COMPILE_ASSERT(2 * sizeof(uint64) < sizeof(sha1_hash), need_more_data);
93 const uint64* random_bits = reinterpret_cast<uint64*>(&sha1_hash[0]);
94
95 // Convert the bits into unit-intervals and return.
96 *population = base::BitsToOpenEndedUnitInterval(random_bits[0]);
97 *group = base::BitsToOpenEndedUnitInterval(random_bits[1]);
98 }
99
100 // Gets the amount of pre-read to use as well as the experiment group in which
101 // the user falls.
102 size_t InitPreReadPercentage() {
103 // By default use the old behaviour: read 100%.
104 static const uint8 kDefaultPercentage = 100;
105 static const char kDefaultFormatStr[] = "%d%%-Default";
106 static const char kControlFormatStr[] = "%d%%-Control";
107 static const char kGroupFormatStr[] = "%d%%";
108
109 COMPILE_ASSERT(kDefaultPercentage <= 100, default_percentage_too_large);
110 COMPILE_ASSERT(kDefaultPercentage % 5 == 0, default_percentage_not_mult_5);
111
112 // Roll the dice to determine if this user is in the experiment and if so,
113 // in which experimental group.
114 double population = 0.0;
115 double group = 0.0;
116 GetPreReadPopulationAndGroup(&population, &group);
117
118 // We limit experiment populations to 1% of the Stable and 10% of each of
119 // the other channels.
120 const string16 channel(GoogleUpdateSettings::GetChromeChannel(
121 GoogleUpdateSettings::IsSystemInstall()));
122 double threshold = (channel == installer::kChromeChannelStable) ? 0.01 : 0.10;
123
124 // If the experiment has expired use the default pre-read level. Otherwise,
125 // those not in the experiment population also use the default pre-read level.
126 size_t value = kDefaultPercentage;
127 const char* format_str = kDefaultFormatStr;
128 if (PreReadExperimentIsActive() && (population <= threshold)) {
129 // We divide the experiment population into groups pre-reading at 5 percent
130 // increments. This is 21 groups of 5 -- to include the range [100, 105) --
131 // rounded down to the nearest 5.
132 value = static_cast<size_t>(group * 21.0) * 5;
133 DCHECK_LE(value, 100u);
134 DCHECK_EQ(0u, value % 5);
135 format_str =
136 (value == kDefaultPercentage) ? kControlFormatStr : kGroupFormatStr;
137 }
138
139 // Generate the group name corresponding to this percentage value.
140 std::string group_name;
141 base::SStringPrintf(&group_name, format_str, value);
142
143 // Persiste the group name to the environment so that it can be used for
144 // reporting.
145 scoped_ptr<base::Environment> env(base::Environment::Create());
146 env->SetVar(chrome::kPreReadEnvironmentVariable, group_name);
147
148 // Return the percentage value to be used.
149 return value;
150 }
151
40 // Expects that |dir| has a trailing backslash. |dir| is modified so it 152 // Expects that |dir| has a trailing backslash. |dir| is modified so it
41 // contains the full path that was tried. Caller must check for the return 153 // contains the full path that was tried. Caller must check for the return
42 // value not being null to determine if this path contains a valid dll. 154 // value not being null to determine if this path contains a valid dll.
43 HMODULE LoadChromeWithDirectory(string16* dir) { 155 HMODULE LoadChromeWithDirectory(string16* dir) {
44 ::SetCurrentDirectoryW(dir->c_str()); 156 ::SetCurrentDirectoryW(dir->c_str());
45 const CommandLine& cmd_line = *CommandLine::ForCurrentProcess(); 157 const CommandLine& cmd_line = *CommandLine::ForCurrentProcess();
46 #if !defined(CHROME_MULTIPLE_DLL) 158 #if !defined(CHROME_MULTIPLE_DLL)
47 const wchar_t* dll_name = installer::kChromeDll; 159 const wchar_t* dll_name = installer::kChromeDll;
48 #else 160 #else
49 const wchar_t* dll_name = cmd_line.HasSwitch(switches::kProcessType) ? 161 const wchar_t* dll_name = cmd_line.HasSwitch(switches::kProcessType) ?
50 installer::kChromeChildDll : installer::kChromeDll; 162 installer::kChromeChildDll : installer::kChromeDll;
51 #endif 163 #endif
52 dir->append(dll_name); 164 dir->append(dll_name);
53 165
54 #if !defined(WIN_DISABLE_PREREAD) 166 #if !defined(WIN_DISABLE_PREREAD)
55 // On Win7 with Syzygy, pre-read is a win. There've very little difference 167 // On Win7 with Syzygy, pre-read is a win. There've very little difference
56 // between 25% and 100%. For cold starts, with or without prefetch 25% 168 // between 25% and 100%. For cold starts, with or without prefetch 25%
57 // performs slightly better than 100%. On XP, pre-read is generally a 169 // performs slightly better than 100%. On XP, pre-read is generally a
58 // performance loss. 170 // performance loss.
59 if (!cmd_line.HasSwitch(switches::kProcessType)) { 171 if (!cmd_line.HasSwitch(switches::kProcessType)) {
60 const size_t kStepSize = 1024 * 1024; 172 const size_t kStepSize = 1024 * 1024;
61 uint8 percent = base::win::GetVersion() > base::win::VERSION_XP ? 25 : 0; 173 size_t percentage = InitPreReadPercentage();
62 ImagePreReader::PartialPreReadImage(dir->c_str(), percent, kStepSize); 174 ImagePreReader::PartialPreReadImage(dir->c_str(), percentage, kStepSize);
63 } 175 }
64 #endif 176 #endif
65 177
66 return ::LoadLibraryExW(dir->c_str(), NULL, 178 return ::LoadLibraryExW(dir->c_str(), NULL,
67 LOAD_WITH_ALTERED_SEARCH_PATH); 179 LOAD_WITH_ALTERED_SEARCH_PATH);
68 } 180 }
69 181
70 void RecordDidRun(const string16& dll_path) { 182 void RecordDidRun(const string16& dll_path) {
71 bool system_level = !InstallUtil::IsPerUserInstall(dll_path.c_str()); 183 bool system_level = !InstallUtil::IsPerUserInstall(dll_path.c_str());
72 GoogleUpdateSettings::UpdateDidRunState(true, system_level); 184 GoogleUpdateSettings::UpdateDidRunState(true, system_level);
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after
238 } 350 }
239 }; 351 };
240 352
241 MainDllLoader* MakeMainDllLoader() { 353 MainDllLoader* MakeMainDllLoader() {
242 #if defined(GOOGLE_CHROME_BUILD) 354 #if defined(GOOGLE_CHROME_BUILD)
243 return new ChromeDllLoader(); 355 return new ChromeDllLoader();
244 #else 356 #else
245 return new ChromiumDllLoader(); 357 return new ChromiumDllLoader();
246 #endif 358 #endif
247 } 359 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698