summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--chrome/app/client_util.cc56
-rw-r--r--chrome/app/image_pre_reader_win.cc420
-rw-r--r--chrome/app/image_pre_reader_win.h74
-rw-r--r--chrome/chrome_exe.gypi12
-rw-r--r--chrome_frame/chrome_frame.gyp1
-rw-r--r--chrome_frame/test/perf/chrome_frame_perftest.cc155
6 files changed, 695 insertions, 23 deletions
diff --git a/chrome/app/client_util.cc b/chrome/app/client_util.cc
index 93aa26e..77a7e08 100644
--- a/chrome/app/client_util.cc
+++ b/chrome/app/client_util.cc
@@ -19,6 +19,7 @@
#include "base/win/registry.h"
#include "chrome/app/breakpad_win.h"
#include "chrome/app/client_util.h"
+#include "chrome/app/image_pre_reader_win.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_result_codes.h"
#include "chrome/common/chrome_switches.h"
@@ -183,9 +184,26 @@ HMODULE LoadChromeWithDirectory(std::wstring* dir) {
// TODO(ananta): Investigate this and tune.
const size_t kStepSize = 4 * 1024;
+ // We hypothesize that pre-reading only the bytes actually touched during
+ // startup should improve startup time. The Syzygy toolchain attempts to
+ // optimize the binary layout of chrome.dll, rearranging the code and data
+ // blocks such that temporally related blocks (i.e., code and data used in
+ // startup, browser, renderer, etc) are grouped together, and that blocks
+ // used early in the process lifecycle occur earlier in their sections.
+ // Our most recent results in the lab show that around 20% of code and 30%
+ // of data is touched during startup. The value below is an experiment
+ // to see what happens to startup time when we read just a percentage
+ // of each section of the binary versus reading the entire thing.
+ // TODO(rogerm): Investigate/validate this and (if benefical) automate
+ // the process of determining how much to read from each section
+ // and embed that info somewhere.
+ const DWORD kPreReadPercentage = 25;
+
DWORD pre_read_size = 0;
+ DWORD pre_read_percentage = kPreReadPercentage;
DWORD pre_read_step_size = kStepSize;
DWORD pre_read = 1;
+ bool use_registry = false;
// TODO(chrisha): This path should not be ChromeFrame specific, and it
// should not be hard-coded with 'Google' in the path. Rather, it should
@@ -193,9 +211,12 @@ HMODULE LoadChromeWithDirectory(std::wstring* dir) {
base::win::RegKey key(HKEY_CURRENT_USER, L"Software\\Google\\ChromeFrame",
KEY_QUERY_VALUE);
if (key.Valid()) {
- key.ReadValueDW(L"PreReadSize", &pre_read_size);
- key.ReadValueDW(L"PreReadStepSize", &pre_read_step_size);
- key.ReadValueDW(L"PreRead", &pre_read);
+ use_registry = (key.ReadValueDW(L"PreRead", &pre_read) == ERROR_SUCCESS);
+ if (use_registry) {
+ key.ReadValueDW(L"PreReadPercentage", &pre_read_percentage);
+ key.ReadValueDW(L"PreReadSize", &pre_read_size);
+ key.ReadValueDW(L"PreReadStepSize", &pre_read_step_size);
+ }
key.Close();
}
@@ -210,23 +231,36 @@ HMODULE LoadChromeWithDirectory(std::wstring* dir) {
// to chrome.dll via an environment variable, which indicates to chrome.dll
// that the experiment is running, causing it to report sub-histogram
// results.
-
+ //
+ // If we've read pre-read settings from the registry, then someone has
+ // specifically set their pre-read options and is not participating in
+ // the experiment.
+ //
// If the experiment is running, indicate it to chrome.dll via an
- // environment variable.
- if (GetPreReadExperimentGroup(&pre_read)) {
+ // environment variable. A pre_read value of 1 indicates that a full
+ // (100%, the current default behaviour) pre-read is to be performed,
+ // while a pre_read value of 0 indicates a partial pre-read is to be
+ // performed, up to the configured percentage.
+ if (!use_registry && GetPreReadExperimentGroup(&pre_read)) {
DCHECK(pre_read == 0 || pre_read == 1);
scoped_ptr<base::Environment> env(base::Environment::Create());
env->SetVar(chrome::kPreReadEnvironmentVariable,
pre_read ? "1" : "0");
+ pre_read_percentage = kPreReadPercentage;
}
#endif // if defined(GOOGLE_CHROME_BUILD)
#endif // if defined(OS_WIN)
- if (pre_read) {
- TRACE_EVENT_BEGIN_ETW("PreReadImage", 0, "");
- file_util::PreReadImage(dir->c_str(), pre_read_size, pre_read_step_size);
- TRACE_EVENT_END_ETW("PreReadImage", 0, "");
- }
+ // Clamp the DWORD percentage to fit into a uint8 that's <= 100.
+ uint8 percentage_to_read = static_cast<uint8>(
+ std::min<DWORD>(pre_read ? 100 : pre_read_percentage, 100));
+
+ // Perform the full or partial pre-read.
+ TRACE_EVENT_BEGIN_ETW("PreReadImage", 0, "");
+ ImagePreReader::PartialPreReadImage(dir->c_str(),
+ percentage_to_read,
+ pre_read_step_size);
+ TRACE_EVENT_END_ETW("PreReadImage", 0, "");
}
#endif // NDEBUG
#endif // WIN_DISABLE_PREREAD
diff --git a/chrome/app/image_pre_reader_win.cc b/chrome/app/image_pre_reader_win.cc
new file mode 100644
index 0000000..146d1c8
--- /dev/null
+++ b/chrome/app/image_pre_reader_win.cc
@@ -0,0 +1,420 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/app/image_pre_reader_win.h"
+
+#include <windows.h>
+#include <algorithm>
+#include <limits>
+#include <vector>
+
+#include "base/logging.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/threading/thread_restrictions.h"
+#include "base/win/pe_image.h"
+#include "base/win/scoped_handle.h"
+#include "base/win/windows_version.h"
+
+namespace {
+
+// The minimum buffer size to allocate when reading the PE file headers.
+//
+// The PE file headers usually fit into a single 1KB page, and a PE file must
+// at least contain the initial page with the headers. That said, as long as
+// we expect at least sizeof(IMAGE_DOS_HEADER) bytes, we're ok.
+const size_t kMinHeaderBufferSize = 0x400;
+
+// A handy symbolic constant.
+const uint8 kOneHundredPercent = 100;
+
+void StaticAssertions() {
+ COMPILE_ASSERT(kMinHeaderBufferSize >= sizeof(IMAGE_DOS_HEADER),
+ min_header_buffer_size_at_least_as_big_as_the_dos_header);
+}
+
+// This struct provides a deallocation functor for use with scoped_ptr<T>
+// allocated with ::VirtualAlloc().
+struct ScopedPtrVirtualFree {
+ void operator() (void* ptr) {
+ ::VirtualFree(ptr, 0, MEM_RELEASE);
+ }
+};
+
+// A wrapper for the Win32 ::SetFilePointer() function with some error checking.
+bool SetFilePointer(HANDLE file_handle, size_t position) {
+ return position <= static_cast<size_t>(std::numeric_limits<LONG>::max()) &&
+ ::SetFilePointer(file_handle,
+ static_cast<LONG>(position),
+ NULL,
+ FILE_BEGIN) != INVALID_SET_FILE_POINTER;
+}
+
+// A helper function to read the next |bytes_to_read| bytes from the file
+// given by |file_handle| into |buffer|.
+bool ReadNextBytes(HANDLE file_handle, void* buffer, size_t bytes_to_read) {
+ DCHECK(file_handle != INVALID_HANDLE_VALUE);
+ DCHECK(buffer != NULL);
+ DCHECK(bytes_to_read > 0);
+
+ DWORD bytes_read = 0;
+ return bytes_to_read <= std::numeric_limits<DWORD>::max() &&
+ ::ReadFile(file_handle,
+ buffer,
+ static_cast<DWORD>(bytes_to_read),
+ &bytes_read,
+ NULL) &&
+ bytes_read == bytes_to_read;
+}
+
+// A helper function to extend the |current_buffer| of bytes such that it
+// contains |desired_length| bytes read from the file given by |file_handle|.
+//
+// It is assumed that |file_handle| has been used to sequentially populate
+// |current_buffer| thus far and is already positioned at the appropriate
+// read location.
+bool ReadMissingBytes(HANDLE file_handle,
+ std::vector<uint8>* current_buffer,
+ size_t desired_length) {
+ DCHECK(file_handle != INVALID_HANDLE_VALUE);
+ DCHECK(current_buffer != NULL);
+
+ size_t current_length = current_buffer->size();
+ if (current_length >= desired_length)
+ return true;
+
+ size_t bytes_to_read = desired_length - current_length;
+ current_buffer->resize(desired_length);
+ return ReadNextBytes(file_handle,
+ &(current_buffer->at(current_length)),
+ bytes_to_read);
+}
+
+// Return a |percentage| of the number of initialized bytes in the given
+// |section|.
+//
+// This returns a percentage of the lesser of the size of the raw data in
+// the section and the virtual size of the section.
+//
+// Note that sections can have their tails implicitly initialized to zero
+// (i.e., their virtual size is larger than the raw size) and that raw data
+// is padded to the PE page size if the entire section is initialized (i.e.,
+// their raw data size will be larger than the virtual size).
+//
+// Any data after the initialized portion of the section will be soft-faulted
+// in (very quickly) as needed, so we don't need to include it in the returned
+// length.
+size_t GetPercentageOfSectionLength(const IMAGE_SECTION_HEADER* section,
+ uint8 percentage) {
+ DCHECK(section != NULL);
+ DCHECK_GT(percentage, 0);
+ DCHECK_LE(percentage, kOneHundredPercent);
+
+ size_t initialized_length = std::min(section->SizeOfRawData,
+ section->Misc.VirtualSize);
+
+ if (initialized_length == 0)
+ return 0;
+
+ size_t length = (initialized_length * percentage) / kOneHundredPercent;
+
+ return std::max<size_t>(length, 1);
+}
+
+// Helper function to read through a |percentage| of the given |section|
+// of the file denoted by |file_handle|. The |temp_buffer| is (re)used as
+// a transient storage area as the section is read in chunks of
+// |temp_buffer_size| bytes.
+bool ReadThroughSection(HANDLE file_handle,
+ const IMAGE_SECTION_HEADER* section,
+ uint8 percentage,
+ void* temp_buffer,
+ size_t temp_buffer_size) {
+ DCHECK(file_handle != INVALID_HANDLE_VALUE);
+ DCHECK(section != NULL);
+ DCHECK_LE(percentage, kOneHundredPercent);
+ DCHECK(temp_buffer != NULL);
+ DCHECK(temp_buffer_size > 0);
+
+ size_t bytes_to_read = GetPercentageOfSectionLength(section, percentage);
+ if (bytes_to_read == 0)
+ return true;
+
+ if (!SetFilePointer(file_handle, section->PointerToRawData))
+ return false;
+
+ // Read all chunks except the last one.
+ while (bytes_to_read > temp_buffer_size) {
+ if (!ReadNextBytes(file_handle, temp_buffer, temp_buffer_size))
+ return false;
+ bytes_to_read -= temp_buffer_size;
+ }
+
+ // Read the last (possibly partial) chunk and return.
+ DCHECK(bytes_to_read > 0);
+ DCHECK(bytes_to_read <= temp_buffer_size);
+ return ReadNextBytes(file_handle, temp_buffer, bytes_to_read);
+}
+
+// A helper function to touch all pages in the range
+// [base_addr, base_addr + length).
+void TouchPagesInRange(void* base_addr, size_t length) {
+ DCHECK(base_addr != NULL);
+ DCHECK(length > 0);
+
+ // Get the system info so we know the page size. Also, make sure we use a
+ // non-zero value for the page size; GetSystemInfo() is hookable/patchable,
+ // and you never know what shenanigans someone could get up to.
+ SYSTEM_INFO system_info = {};
+ GetSystemInfo(&system_info);
+ if (system_info.dwPageSize == 0)
+ system_info.dwPageSize = 4096;
+
+ // We don't want to read outside the byte range (which could trigger an
+ // access violation), so let's figure out the exact locations of the first
+ // and final bytes we want to read.
+ volatile uint8* touch_ptr = reinterpret_cast<uint8*>(base_addr);
+ volatile uint8* final_touch_ptr = touch_ptr + length - 1;
+
+ // Read the memory in the range [touch_ptr, final_touch_ptr] with a stride
+ // of the system page size, to ensure that it's been paged in.
+ uint8 dummy;
+ while (touch_ptr < final_touch_ptr) {
+ dummy = *touch_ptr;
+ touch_ptr += system_info.dwPageSize;
+ }
+ dummy = *final_touch_ptr;
+}
+
+} // namespace
+
+bool ImagePreReader::PartialPreReadImageOnDisk(const wchar_t* file_path,
+ uint8 percentage,
+ size_t max_chunk_size) {
+ // TODO(rogerm): change this to have the number of bytes pre-read per
+ // section be driven by a static table within the PE file (defaulting to
+ // full read if it's not there?) that's initialized by the optimization
+ // toolchain.
+ DCHECK(file_path != NULL);
+
+ if (percentage == 0)
+ return true;
+
+ if (percentage > kOneHundredPercent)
+ percentage = kOneHundredPercent;
+
+ // Validate/setup max_chunk_size, imposing a 1MB minimum on the chunk size.
+ const size_t kMinChunkSize = 1024 * 1024;
+ max_chunk_size = std::max(max_chunk_size, kMinChunkSize);
+
+ // Open the file.
+ base::win::ScopedHandle file(
+ CreateFile(file_path,
+ GENERIC_READ,
+ FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
+ NULL,
+ OPEN_EXISTING,
+ FILE_FLAG_SEQUENTIAL_SCAN,
+ NULL));
+
+ if (!file.IsValid())
+ return false;
+
+ // Allocate a resizable buffer for the headers. We initially reserve as much
+ // space as we typically see as the header size for chrome.dll and other
+ // PE images.
+ std::vector<uint8> headers;
+ headers.reserve(kMinHeaderBufferSize);
+
+ // Read, hopefully, all of the headers.
+ if (!ReadMissingBytes(file, &headers, kMinHeaderBufferSize))
+ return false;
+
+ // The DOS header starts at offset 0 and allows us to get the offset of the
+ // NT headers. Let's ensure we've read enough to capture the NT headers.
+ size_t nt_headers_start =
+ reinterpret_cast<IMAGE_DOS_HEADER*>(&headers[0])->e_lfanew;
+ size_t nt_headers_end = nt_headers_start + sizeof(IMAGE_NT_HEADERS);
+ if (!ReadMissingBytes(file, &headers, nt_headers_end))
+ return false;
+
+ // Now that we've got the NT headers we can get the total header size,
+ // including all of the section headers. Let's ensure we've read enough
+ // to capture all of the header data.
+ size_t size_of_headers = reinterpret_cast<IMAGE_NT_HEADERS*>(
+ &headers[nt_headers_start])->OptionalHeader.SizeOfHeaders;
+ if (!ReadMissingBytes(file, &headers, size_of_headers))
+ return false;
+
+ // Now we have all of the headers. This is enough to let us use the PEImage
+ // wrapper to query the structure of the image.
+ base::win::PEImage pe_image(reinterpret_cast<HMODULE>(&headers[0]));
+ CHECK(pe_image.VerifyMagic());
+
+ // Allocate a buffer to hold the pre-read bytes.
+ scoped_ptr_malloc<uint8, ScopedPtrVirtualFree> buffer(
+ reinterpret_cast<uint8*>(
+ ::VirtualAlloc(NULL, max_chunk_size, MEM_COMMIT, PAGE_READWRITE)));
+ if (buffer.get() == NULL)
+ return false;
+
+ // Iterate over each section, reading in a percentage of each.
+ const IMAGE_SECTION_HEADER* section = NULL;
+ for (UINT i = 0; (section = pe_image.GetSectionHeader(i)) != NULL; ++i) {
+ CHECK_LE(reinterpret_cast<const uint8*>(section + 1),
+ &headers[0] + headers.size());
+ if (!ReadThroughSection(
+ file, section, percentage, buffer.get(), max_chunk_size))
+ return false;
+ }
+
+ // We're done.
+ return true;
+}
+
+bool ImagePreReader::PartialPreReadImageInMemory(const wchar_t* file_path,
+ uint8 percentage) {
+ // TODO(rogerm): change this to have the number of bytes pre-read per
+ // section be driven by a static table within the PE file (defaulting to
+ // full read if it's not there?) that's initialized by the optimization
+ // toolchain.
+ DCHECK(file_path != NULL);
+
+ if (percentage == 0)
+ return true;
+
+ if (percentage > kOneHundredPercent)
+ percentage = kOneHundredPercent;
+
+ HMODULE dll_module = ::LoadLibraryExW(
+ file_path,
+ NULL,
+ LOAD_WITH_ALTERED_SEARCH_PATH | DONT_RESOLVE_DLL_REFERENCES);
+
+ if (!dll_module)
+ return false;
+
+ base::win::PEImage pe_image(dll_module);
+ CHECK(pe_image.VerifyMagic());
+
+ // Iterate over each section, stepping through a percentage of each to page
+ // it in off the disk.
+ const IMAGE_SECTION_HEADER* section = NULL;
+ for (UINT i = 0; (section = pe_image.GetSectionHeader(i)) != NULL; ++i) {
+ // Get the extent we want to touch.
+ size_t length = GetPercentageOfSectionLength(section, percentage);
+ if (length == 0)
+ continue;
+ uint8* start =
+ static_cast<uint8*>(pe_image.RVAToAddr(section->VirtualAddress));
+
+ // Verify that the extent we're going to touch falls inside the section
+ // we expect it to (and by implication, inside the pe_image).
+ CHECK_EQ(section,
+ pe_image.GetImageSectionFromAddr(start));
+ CHECK_EQ(section,
+ pe_image.GetImageSectionFromAddr(start + length - 1));
+
+ // Page in the section range.
+ TouchPagesInRange(start, length);
+ }
+
+ FreeLibrary(dll_module);
+
+ return true;
+}
+
+bool ImagePreReader::PreReadImage(const wchar_t* file_path,
+ size_t size_to_read,
+ size_t step_size) {
+ base::ThreadRestrictions::AssertIOAllowed();
+ if (base::win::GetVersion() > base::win::VERSION_XP) {
+ // Vista+ branch. On these OSes, the forced reads through the DLL actually
+ // slows warm starts. The solution is to sequentially read file contents
+ // with an optional cap on total amount to read.
+ base::win::ScopedHandle file(
+ CreateFile(file_path,
+ GENERIC_READ,
+ FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
+ NULL,
+ OPEN_EXISTING,
+ FILE_FLAG_SEQUENTIAL_SCAN,
+ NULL));
+
+ if (!file.IsValid())
+ return false;
+
+ // Default to 1MB sequential reads.
+ const DWORD actual_step_size = std::max(static_cast<DWORD>(step_size),
+ static_cast<DWORD>(1024*1024));
+ LPVOID buffer = ::VirtualAlloc(NULL,
+ actual_step_size,
+ MEM_COMMIT,
+ PAGE_READWRITE);
+
+ if (buffer == NULL)
+ return false;
+
+ DWORD len;
+ size_t total_read = 0;
+ while (::ReadFile(file, buffer, actual_step_size, &len, NULL) &&
+ len > 0 &&
+ (size_to_read ? total_read < size_to_read : true)) {
+ total_read += static_cast<size_t>(len);
+ }
+ ::VirtualFree(buffer, 0, MEM_RELEASE);
+ } else {
+ // WinXP branch. Here, reading the DLL from disk doesn't do
+ // what we want so instead we pull the pages into memory by loading
+ // the DLL and touching pages at a stride. We use the system's page
+ // size as the stride, ignoring the passed in step_size, to make sure
+ // each page in the range is touched.
+ HMODULE dll_module = ::LoadLibraryExW(
+ file_path,
+ NULL,
+ LOAD_WITH_ALTERED_SEARCH_PATH | DONT_RESOLVE_DLL_REFERENCES);
+
+ if (!dll_module)
+ return false;
+
+ base::win::PEImage pe_image(dll_module);
+ CHECK(pe_image.VerifyMagic());
+
+ // We don't want to read past the end of the module (which could trigger
+ // an access violation), so make sure to check the image size.
+ PIMAGE_NT_HEADERS nt_headers = pe_image.GetNTHeaders();
+ size_t dll_module_length = std::min(
+ size_to_read ? size_to_read : ~0,
+ static_cast<size_t>(nt_headers->OptionalHeader.SizeOfImage));
+
+ // Page in then release the module.
+ TouchPagesInRange(dll_module, dll_module_length);
+ FreeLibrary(dll_module);
+ }
+
+ return true;
+}
+
+bool ImagePreReader::PartialPreReadImage(const wchar_t* file_path,
+ uint8 percentage,
+ size_t max_chunk_size) {
+ base::ThreadRestrictions::AssertIOAllowed();
+
+ if (percentage >= kOneHundredPercent) {
+ // If we're reading the whole image, we don't need to parse headers and
+ // navigate sections, the basic PreReadImage() can be used to just step
+ // blindly through the entire file / address-space.
+ return PreReadImage(file_path, 0, max_chunk_size);
+ }
+
+ if (base::win::GetVersion() > base::win::VERSION_XP) {
+ // Vista+ branch. On these OSes, we warm up the Image by reading its
+ // file off the disk.
+ return PartialPreReadImageOnDisk(file_path, percentage, max_chunk_size);
+ }
+
+ // WinXP branch. For XP, reading the image from disk doesn't do what we want
+ // so instead we pull the pages into memory by loading the DLL and touching
+ // initialized pages at a stride.
+ return PartialPreReadImageInMemory(file_path, percentage);
+}
diff --git a/chrome/app/image_pre_reader_win.h b/chrome/app/image_pre_reader_win.h
new file mode 100644
index 0000000..96acc56
--- /dev/null
+++ b/chrome/app/image_pre_reader_win.h
@@ -0,0 +1,74 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// This file defines utility functions to pre-read a PE Image in order to
+// avoid hard page faults when the image is subsequently loaded into memory
+// for execution.
+
+#ifndef CHROME_APP_IMAGE_PRE_READER_WIN_H_
+#define CHROME_APP_IMAGE_PRE_READER_WIN_H_
+#pragma once
+
+#include "base/basictypes.h"
+
+// This class defines static helper functions to pre-read a PE Image in order
+// to avoid hard page faults when the image is subsequently loaded into memory
+// for execution.
+class ImagePreReader {
+ public:
+ // Reads the file passed in as a PE Image and touches pages to avoid
+ // subsequent hard page faults during LoadLibrary. The size to be pre-read
+ // is passed in. If it is 0 then the whole file is paged in. The step size
+ // which indicates the number of bytes to skip after every page touched is
+ // also passed in.
+ //
+ // This function checks the Windows version to determine which pre-reading
+ // mechanism to use.
+ static bool PreReadImage(const wchar_t* file_path,
+ size_t size_to_read,
+ size_t step_size);
+
+ // Loads the file passed in as PE Image and touches a percentage of the
+ // pages in each of the image's sections to avoid subsequent hard page
+ // faults during LoadLibrary.
+ //
+ // This function checks the Windows version to determine which pre-reading
+ // mechanism to use.
+ //
+ // The percentage of the file to be read is an integral value between 0 and
+ // 100, inclusive. If it is 0 then this is a NOP, if it is 100 (or greater)
+ // then the whole file is paged in sequentially via PreReadImage. Otherwise,
+ // for each section, in order, the given percentage of the blocks in that
+ // section are paged in, starting at the beginning of each section. For
+ // example: if percentage is 30 and there is a .text section and a .data
+ // section, then the first 30% of .text will be paged and the first 30% of
+ // .data will be paged in.
+ //
+ // The max_chunk_size indicates the number of bytes to read off the disk in
+ // each step (for Vista and greater, where this is the way the pages are
+ // warmed).
+ //
+ // This function is intended to be used in the context of a PE image with
+ // an optimized layout, such that the blocks in each section are arranged
+ // with the data and code most needed for startup moved to the front.
+ // See also: http://code.google.com/p/chromium/issues/detail?id=98508
+ static bool PartialPreReadImage(const wchar_t* file_path,
+ uint8 percentage,
+ size_t max_chunk_size);
+
+ // Helper function used by PartialPreReadImage on Windows versions (Vista+)
+ // where reading through the file on disk serves to warm up the page cache.
+ // Exported for unit-testing purposes.
+ static bool PartialPreReadImageOnDisk(const wchar_t* file_path,
+ uint8 percentage,
+ size_t max_chunk_size);
+
+ // Helper function used by PartialPreReadImage on Windows versions (XP) where
+ // cheaply loading the image then stepping through its address space serves
+ // to warm up the page cache. Exported for unit-testing purposes.
+ static bool PartialPreReadImageInMemory(const wchar_t* file_path,
+ uint8 percentage);
+}; // namespace internal
+
+#endif // CHROME_APP_IMAGE_PRE_READER_WIN_H_
diff --git a/chrome/chrome_exe.gypi b/chrome/chrome_exe.gypi
index 5d52924..b023323 100644
--- a/chrome/chrome_exe.gypi
+++ b/chrome/chrome_exe.gypi
@@ -454,6 +454,7 @@
'chrome_version_resources',
'installer_util',
'installer_util_strings',
+ 'image_pre_reader',
'../base/base.gyp:base',
'../breakpad/breakpad.gyp:breakpad_handler',
'../breakpad/breakpad.gyp:breakpad_sender',
@@ -510,6 +511,17 @@
['OS=="win"', {
'targets': [
{
+ 'target_name': 'image_pre_reader',
+ 'type': 'static_library',
+ 'sources': [
+ 'app/image_pre_reader_win.cc',
+ 'app/image_pre_reader_win.h',
+ ],
+ 'dependencies': [
+ '../base/base.gyp:base',
+ ],
+ },
+ {
'target_name': 'chrome_nacl_win64',
'type': 'executable',
'product_name': 'nacl64',
diff --git a/chrome_frame/chrome_frame.gyp b/chrome_frame/chrome_frame.gyp
index 95ce7c5..0d9c124 100644
--- a/chrome_frame/chrome_frame.gyp
+++ b/chrome_frame/chrome_frame.gyp
@@ -302,6 +302,7 @@
'../chrome/chrome.gyp:common',
'../chrome/chrome.gyp:browser',
'../chrome/chrome.gyp:debugger',
+ '../chrome/chrome.gyp:image_pre_reader',
'../chrome/chrome.gyp:test_support_ui',
'../chrome/chrome.gyp:utility',
'../content/content.gyp:content_gpu',
diff --git a/chrome_frame/test/perf/chrome_frame_perftest.cc b/chrome_frame/test/perf/chrome_frame_perftest.cc
index 06de343..14faa14 100644
--- a/chrome_frame/test/perf/chrome_frame_perftest.cc
+++ b/chrome_frame/test/perf/chrome_frame_perftest.cc
@@ -1,4 +1,4 @@
-// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome_frame/test/perf/chrome_frame_perftest.h"
@@ -26,6 +26,7 @@
#include "base/win/scoped_bstr.h"
#include "base/win/scoped_comptr.h"
#include "base/win/scoped_variant.h"
+#include "chrome/app/image_pre_reader_win.h"
#include "chrome/common/chrome_constants.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_paths_internal.h"
@@ -408,23 +409,39 @@ class ChromeFrameStartupTestActiveX : public ChromeFrameStartupTest {
class ChromeFrameBinariesLoadTest : public ChromeFrameStartupTestActiveX {
static const size_t kStepSize = 4 * 1024;
public:
+ enum PreReadType {
+ kPreReadNone,
+ kPreReadPartial,
+ kPreReadFull
+ };
+
ChromeFrameBinariesLoadTest()
- : pre_read_(false),
+ : pre_read_type_(kPreReadNone),
step_size_(kStepSize),
- bytes_to_read_(0) {}
+ bytes_to_read_(0),
+ percentage_to_preread_(25) {}
protected:
virtual void RunStartupTestImpl(TimeTicks* start_time,
TimeTicks* end_time) {
*start_time = TimeTicks::Now();
- if (pre_read_) {
- EXPECT_TRUE(file_util::PreReadImage(chrome_exe_.value().c_str(),
- bytes_to_read_,
- step_size_));
- EXPECT_TRUE(file_util::PreReadImage(chrome_dll_.value().c_str(),
- bytes_to_read_,
- step_size_));
+ if (pre_read_type_ == kPreReadFull) {
+ EXPECT_TRUE(ImagePreReader::PreReadImage(chrome_exe_.value().c_str(),
+ bytes_to_read_,
+ step_size_));
+ EXPECT_TRUE(ImagePreReader::PreReadImage(chrome_dll_.value().c_str(),
+ bytes_to_read_,
+ step_size_));
+ } else if (pre_read_type_ == kPreReadPartial) {
+ EXPECT_TRUE(
+ ImagePreReader::PartialPreReadImage(chrome_exe_.value().c_str(),
+ percentage_to_preread_,
+ step_size_));
+ EXPECT_TRUE(
+ ImagePreReader::PartialPreReadImage(chrome_dll_.value().c_str(),
+ percentage_to_preread_,
+ step_size_));
}
HMODULE chrome_exe = LoadLibrary(chrome_exe_.value().c_str());
@@ -439,9 +456,10 @@ class ChromeFrameBinariesLoadTest : public ChromeFrameStartupTestActiveX {
FreeLibrary(chrome_dll);
}
- bool pre_read_;
+ PreReadType pre_read_type_;
size_t bytes_to_read_;
size_t step_size_;
+ uint8 percentage_to_preread_;
};
// This class provides functionality to run the startup performance test for
@@ -926,6 +944,91 @@ class SilverlightCreationTest : public ChromeFrameStartupTest {
}
};
+// TODO(rogerm): Flesh out the *PreReadImage* tests to validate an observed
+// change in paging behaviour between raw loading and pre-reading.
+
+// TODO(rogerm): Add checks to the *PreReadImage* tests to validate the
+// handling of invalid pe files and paths as input.
+
+TEST(ImagePreReader, PreReadImage) {
+ FilePath current_exe;
+ ASSERT_TRUE(PathService::Get(base::FILE_EXE, &current_exe));
+
+ int64 file_size_64 = 0;
+ ASSERT_TRUE(file_util::GetFileSize(current_exe, &file_size_64));
+ ASSERT_TRUE(file_size_64 < std::numeric_limits<std::size_t>::max());
+ size_t file_size = static_cast<size_t>(file_size_64);
+
+ const wchar_t* module_path = current_exe.value().c_str();
+ const size_t kStepSize = 2 * 1024 * 1024;
+
+ ASSERT_TRUE(
+ ImagePreReader::PreReadImage(module_path, 0, kStepSize));
+ ASSERT_TRUE(
+ ImagePreReader::PreReadImage(module_path, file_size / 4, kStepSize));
+ ASSERT_TRUE(
+ ImagePreReader::PreReadImage(module_path, file_size / 2, kStepSize));
+ ASSERT_TRUE(
+ ImagePreReader::PreReadImage(module_path, file_size, kStepSize));
+ ASSERT_TRUE(
+ ImagePreReader::PreReadImage(module_path, file_size * 2, kStepSize));
+}
+
+TEST(ImagePreReader, PartialPreReadImage) {
+ FilePath current_exe;
+ ASSERT_TRUE(PathService::Get(base::FILE_EXE, &current_exe));
+
+ const wchar_t* module_path = current_exe.value().c_str();
+ const size_t kStepSize = 2 * 1024 * 1024;
+
+ ASSERT_TRUE(
+ ImagePreReader::PartialPreReadImage(module_path, 0, kStepSize));
+ ASSERT_TRUE(
+ ImagePreReader::PartialPreReadImage(module_path, 25, kStepSize));
+ ASSERT_TRUE(
+ ImagePreReader::PartialPreReadImage(module_path, 50, kStepSize));
+ ASSERT_TRUE(
+ ImagePreReader::PartialPreReadImage(module_path, 100, kStepSize));
+ ASSERT_TRUE(
+ ImagePreReader::PartialPreReadImage(module_path, 150, kStepSize));
+}
+
+TEST(ImagePreReader, PartialPreReadImageOnDisk) {
+ FilePath current_exe;
+ ASSERT_TRUE(PathService::Get(base::FILE_EXE, &current_exe));
+
+ const wchar_t* module_path = current_exe.value().c_str();
+ const size_t kChunkSize = 2 * 1024 * 1024;
+
+ ASSERT_TRUE(
+ ImagePreReader::PartialPreReadImageOnDisk(module_path, 0, kChunkSize));
+ ASSERT_TRUE(
+ ImagePreReader::PartialPreReadImageOnDisk(module_path, 25, kChunkSize));
+ ASSERT_TRUE(
+ ImagePreReader::PartialPreReadImageOnDisk(module_path, 50, kChunkSize));
+ ASSERT_TRUE(
+ ImagePreReader::PartialPreReadImageOnDisk(module_path, 100, kChunkSize));
+ ASSERT_TRUE(
+ ImagePreReader::PartialPreReadImageOnDisk(module_path, 150, kChunkSize));
+}
+
+TEST(ImagePreReader, PartialPreReadImageInMemory) {
+ FilePath current_exe;
+ ASSERT_TRUE(PathService::Get(base::FILE_EXE, &current_exe));
+ const wchar_t* module_path = current_exe.value().c_str();
+
+ ASSERT_TRUE(
+ ImagePreReader::PartialPreReadImageInMemory(module_path, 0));
+ ASSERT_TRUE(
+ ImagePreReader::PartialPreReadImageInMemory(module_path, 25));
+ ASSERT_TRUE(
+ ImagePreReader::PartialPreReadImageInMemory(module_path, 50));
+ ASSERT_TRUE(
+ ImagePreReader::PartialPreReadImageInMemory(module_path, 100));
+ ASSERT_TRUE(
+ ImagePreReader::PartialPreReadImageInMemory(module_path, 150));
+}
+
TEST(ChromeFramePerf, DISABLED_HostActiveX) {
// TODO(stoyan): Create a low integrity level thread && perform the test there
SimpleModule module;
@@ -989,12 +1092,40 @@ TEST_F(ChromeFrameBinariesLoadTest, PerfCold) {
TEST_F(ChromeFrameBinariesLoadTest, PerfColdPreRead) {
FilePath binaries_to_evict[] = {chrome_exe_, chrome_dll_};
- pre_read_ = true;
+ pre_read_type_ = kPreReadFull;
RunStartupTest("binary_load_cold_preread", "t", "", true /* cold */,
arraysize(binaries_to_evict), binaries_to_evict,
false /* not important */, false);
}
+TEST_F(ChromeFrameBinariesLoadTest, PerfColdPartialPreRead15) {
+ FilePath binaries_to_evict[] = {chrome_exe_, chrome_dll_};
+ pre_read_type_ = kPreReadPartial;
+ percentage_to_preread_ = 15;
+ RunStartupTest("binary_load_cold_partial_preread", "t", "", true /* cold */,
+ arraysize(binaries_to_evict), binaries_to_evict,
+ false /* not important */, false);
+}
+
+
+TEST_F(ChromeFrameBinariesLoadTest, PerfColdPartialPreRead25) {
+ FilePath binaries_to_evict[] = {chrome_exe_, chrome_dll_};
+ pre_read_type_ = kPreReadPartial;
+ percentage_to_preread_ = 25;
+ RunStartupTest("binary_load_cold_partial_preread", "t", "", true /* cold */,
+ arraysize(binaries_to_evict), binaries_to_evict,
+ false /* not important */, false);
+}
+
+TEST_F(ChromeFrameBinariesLoadTest, PerfColdPartialPreRead40) {
+ FilePath binaries_to_evict[] = {chrome_exe_, chrome_dll_};
+ pre_read_type_ = kPreReadPartial;
+ percentage_to_preread_ = 40;
+ RunStartupTest("binary_load_cold_partial_preread", "t", "", true /* cold */,
+ arraysize(binaries_to_evict), binaries_to_evict,
+ false /* not important */, false);
+}
+
TEST_F(ChromeFrameStartupTestActiveXReference, PerfWarm) {
RunStartupTest("warm", "t_ref", "about:blank", false /* cold */, 0, NULL,
true /* important */, false);