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 <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 bool PreReadExpirementHasExpired() { | |
| 43 static const int kPreReadExpiryYear = 2014; | |
| 44 static const int kPreReadExpiryMonth = 7; | |
| 45 static const int kPreReadExpiryDay = 1; | |
| 46 static const char kBuildTimeStr[] = __DATE__ " " __TIME__; | |
| 47 | |
| 48 // Get the timestamp of the build. | |
| 49 base::Time build_time; | |
| 50 bool result = base::Time::FromString(kBuildTimeStr, &build_time); | |
| 51 DCHECK(result); | |
| 52 | |
| 53 // Get the timestamp at which the experiment expires. | |
| 54 base::Time::Exploded exploded = {0}; | |
| 55 exploded.year = kPreReadExpiryYear; | |
| 56 exploded.month = kPreReadExpiryMonth; | |
| 57 exploded.day_of_month = kPreReadExpiryDay; | |
| 58 base::Time expiration_time = base::Time::FromLocalExploded(exploded); | |
| 59 | |
| 60 // Return true if the experiment is expired. | |
| 61 return (build_time > expiration_time); | |
| 62 } | |
| 63 | |
| 64 uint8 GetPreReadPercentage() { | |
| 65 // By default use the old behaviour: read 100%. | |
| 66 const uint8 kDefaultPercentage = 100; | |
| 67 COMPILE_ASSERT(kDefaultPercentage <= 100, default_percentage_too_large); | |
| 68 COMPILE_ASSERT(kDefaultPercentage % 5 == 0, default_percentage_not_mult_5); | |
| 69 | |
| 70 scoped_ptr<base::Environment> env(base::Environment::Create()); | |
| 71 env->SetVar(chrome::kPreReadEnvironmentVariable, | |
| 72 base::StringPrintf("%d%%-Default", kDefaultPercentage)); | |
| 73 | |
| 74 // If the expirement has expired use the default pre-read level. | |
| 75 if (PreReadExpirementHasExpired()) | |
| 76 return kDefaultPercentage; | |
| 77 | |
| 78 // If this user isn't reporting metrics, use the default pre-read leve. | |
|
Alexei Svitkine (slow)
2013/08/28 17:30:50
The general rule is to avoid giving different beha
Roger McFarlane (Chromium)
2013/08/29 15:28:37
Fair enough.
I copied this "opt-out" logic from t
| |
| 79 base::string16 metrics_id; | |
| 80 if (!GoogleUpdateSettings::GetMetricsId(&metrics_id) || metrics_id.empty()) | |
| 81 return kDefaultPercentage; | |
| 82 | |
| 83 // Otherwise, use the metrics id as the basis for deriving what level or | |
| 84 // pre-read to do. To interpret the metrics id as a random number we hash | |
| 85 // it and intepret the first 8 bytes of it as a unit-interval representing | |
| 86 // die-toss for being in the experiment population and the second 8 bytes | |
| 87 // as the die-toss for the experiment group. | |
| 88 unsigned char sha1_hash[base::kSHA1Length]; | |
| 89 base::SHA1HashBytes( | |
| 90 reinterpret_cast<const unsigned char*>(metrics_id.c_str()), | |
|
Alexei Svitkine (slow)
2013/08/28 17:30:50
Do the results need to be persistent?
i.e. does t
Roger McFarlane (Chromium)
2013/08/29 15:28:37
Yes, they need to be persistent.
The OS pre-fetch
| |
| 91 metrics_id.size() * sizeof(metrics_id[0]), | |
| 92 sha1_hash); | |
| 93 COMPILE_ASSERT(2 * sizeof(uint64) < sizeof(sha1_hash), need_more_data); | |
| 94 const uint64* population_bits = reinterpret_cast<uint64*>(&sha1_hash[0]); | |
| 95 const uint64* group_bits = population_bits + 1; | |
| 96 | |
| 97 // We limit experiment populations to 1% of the Stable and 10% of the Beta | |
| 98 // and Dev channels. | |
| 99 double population = base::BitsToOpenEndedUnitInterval(*population_bits); | |
| 100 const string16 channel(GoogleUpdateSettings::GetChromeChannel( | |
| 101 GoogleUpdateSettings::IsSystemInstall())); | |
| 102 if ((channel == installer::kChromeChannelStable && population > 0.01) || | |
| 103 ((channel == installer::kChromeChannelBeta || | |
| 104 channel == installer::kChromeChannelDev) && population > 0.10)) { | |
| 105 return kDefaultPercentage; | |
| 106 } | |
| 107 | |
| 108 // We divide the experiment population into groups pre-reading at 5 percent | |
| 109 // increments. This is 21 groups of 5 -- to include the range [100, 105) -- | |
| 110 // rounded down to the nearest 5. | |
| 111 double group = base::BitsToOpenEndedUnitInterval(*group_bits); | |
| 112 uint8 percentage = static_cast<uint8>(group * 21.0) * 5; | |
| 113 DCHECK_GE(100, percentage); | |
| 114 DCHECK_EQ(0, percentage % 5); | |
| 115 | |
| 116 const char* format_str = | |
| 117 (percentage == kDefaultPercentage) ? "%d%%-Control" : "%d%%"; | |
| 118 | |
| 119 env->SetVar(chrome::kPreReadEnvironmentVariable, | |
| 120 base::StringPrintf(format_str, percentage)); | |
| 121 | |
| 122 return percentage; | |
| 123 } | |
| 124 | |
| 40 // Expects that |dir| has a trailing backslash. |dir| is modified so it | 125 // 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 | 126 // 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. | 127 // value not being null to determine if this path contains a valid dll. |
| 43 HMODULE LoadChromeWithDirectory(string16* dir) { | 128 HMODULE LoadChromeWithDirectory(string16* dir) { |
| 44 ::SetCurrentDirectoryW(dir->c_str()); | 129 ::SetCurrentDirectoryW(dir->c_str()); |
| 45 const CommandLine& cmd_line = *CommandLine::ForCurrentProcess(); | 130 const CommandLine& cmd_line = *CommandLine::ForCurrentProcess(); |
| 46 #if !defined(CHROME_MULTIPLE_DLL) | 131 #if !defined(CHROME_MULTIPLE_DLL) |
| 47 const wchar_t* dll_name = installer::kChromeDll; | 132 const wchar_t* dll_name = installer::kChromeDll; |
| 48 #else | 133 #else |
| 49 const wchar_t* dll_name = cmd_line.HasSwitch(switches::kProcessType) ? | 134 const wchar_t* dll_name = cmd_line.HasSwitch(switches::kProcessType) ? |
| 50 installer::kChromeChildDll : installer::kChromeDll; | 135 installer::kChromeChildDll : installer::kChromeDll; |
| 51 #endif | 136 #endif |
| 52 dir->append(dll_name); | 137 dir->append(dll_name); |
| 53 | 138 |
| 54 #if !defined(WIN_DISABLE_PREREAD) | 139 #if !defined(WIN_DISABLE_PREREAD) |
| 55 // On Win7 with Syzygy, pre-read is a win. There've very little difference | 140 // 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% | 141 // 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 | 142 // performs slightly better than 100%. On XP, pre-read is generally a |
| 58 // performance loss. | 143 // performance loss. |
| 59 if (!cmd_line.HasSwitch(switches::kProcessType)) { | 144 if (!cmd_line.HasSwitch(switches::kProcessType)) { |
| 60 const size_t kStepSize = 1024 * 1024; | 145 static const size_t kStepSize = 1024 * 1024; |
| 61 uint8 percent = base::win::GetVersion() > base::win::VERSION_XP ? 25 : 0; | 146 uint8 percentage = GetPreReadPercentage(); |
| 62 ImagePreReader::PartialPreReadImage(dir->c_str(), percent, kStepSize); | 147 ImagePreReader::PartialPreReadImage(dir->c_str(), percentage, kStepSize); |
| 63 } | 148 } |
| 64 #endif | 149 #endif |
| 65 | 150 |
| 66 return ::LoadLibraryExW(dir->c_str(), NULL, | 151 return ::LoadLibraryExW(dir->c_str(), NULL, |
| 67 LOAD_WITH_ALTERED_SEARCH_PATH); | 152 LOAD_WITH_ALTERED_SEARCH_PATH); |
| 68 } | 153 } |
| 69 | 154 |
| 70 void RecordDidRun(const string16& dll_path) { | 155 void RecordDidRun(const string16& dll_path) { |
| 71 bool system_level = !InstallUtil::IsPerUserInstall(dll_path.c_str()); | 156 bool system_level = !InstallUtil::IsPerUserInstall(dll_path.c_str()); |
| 72 GoogleUpdateSettings::UpdateDidRunState(true, system_level); | 157 GoogleUpdateSettings::UpdateDidRunState(true, system_level); |
| (...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 238 } | 323 } |
| 239 }; | 324 }; |
| 240 | 325 |
| 241 MainDllLoader* MakeMainDllLoader() { | 326 MainDllLoader* MakeMainDllLoader() { |
| 242 #if defined(GOOGLE_CHROME_BUILD) | 327 #if defined(GOOGLE_CHROME_BUILD) |
| 243 return new ChromeDllLoader(); | 328 return new ChromeDllLoader(); |
| 244 #else | 329 #else |
| 245 return new ChromiumDllLoader(); | 330 return new ChromiumDllLoader(); |
| 246 #endif | 331 #endif |
| 247 } | 332 } |
| OLD | NEW |