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
3// found in the LICENSE file.
4
5// This file defines utility functions to pre-read a PE Image in order to
6// avoid hard page faults when the image is subsequently loaded into memory
7// for execution.
8
9#ifndef CHROME_APP_IMAGE_PRE_READER_WIN_H_
10#define CHROME_APP_IMAGE_PRE_READER_WIN_H_
11
12#include "base/basictypes.h"
13
14// This class defines static helper functions to pre-read a PE Image in order
15// to avoid hard page faults when the image is subsequently loaded into memory
16// for execution.
17class ImagePreReader {
18 public:
19  // Reads the file passed in as a PE Image and touches pages to avoid
20  // subsequent hard page faults during LoadLibrary. The size to be pre-read
21  // is passed in. If it is 0 then the whole file is paged in. The step size
22  // which indicates the number of bytes to skip after every page touched is
23  // also passed in.
24  //
25  // This function checks the Windows version to determine which pre-reading
26  // mechanism to use.
27  static bool PreReadImage(const wchar_t* file_path,
28                           size_t size_to_read,
29                           size_t step_size);
30
31  // Loads the file passed in as PE Image and touches a percentage of the
32  // pages in each of the image's sections to avoid subsequent hard page
33  // faults during LoadLibrary.
34  //
35  // This function checks the Windows version to determine which pre-reading
36  // mechanism to use.
37  //
38  // The percentage of the file to be read is an integral value between 0 and
39  // 100, inclusive. If it is 0 then this is a NOP, if it is 100 (or greater)
40  // then the whole file is paged in sequentially via PreReadImage. Otherwise,
41  // for each section, in order, the given percentage of the blocks in that
42  // section are paged in, starting at the beginning of each section. For
43  // example: if percentage is 30 and there is a .text section and a .data
44  // section, then the first 30% of .text will be paged and the first 30% of
45  // .data will be paged in.
46  //
47  // The max_chunk_size indicates the number of bytes to read off the disk in
48  // each step (for Vista and greater, where this is the way the pages are
49  // warmed).
50  //
51  // This function is intended to be used in the context of a PE image with
52  // an optimized layout, such that the blocks in each section are arranged
53  // with the data and code most needed for startup moved to the front.
54  // See also: http://code.google.com/p/chromium/issues/detail?id=98508
55  static bool PartialPreReadImage(const wchar_t* file_path,
56                                  uint8 percentage,
57                                  size_t max_chunk_size);
58
59  // Helper function used by PartialPreReadImage on Windows versions (Vista+)
60  // where reading through the file on disk serves to warm up the page cache.
61  // Exported for unit-testing purposes.
62  static bool PartialPreReadImageOnDisk(const wchar_t* file_path,
63                                        uint8 percentage,
64                                        size_t max_chunk_size);
65
66  // Helper function used by PartialPreReadImage on Windows versions (XP) where
67  // cheaply loading the image then stepping through its address space serves
68  // to warm up the page cache. Exported for unit-testing purposes.
69  static bool PartialPreReadImageInMemory(const wchar_t* file_path,
70                                          uint8 percentage);
71};  // namespace internal
72
73#endif  // CHROME_APP_IMAGE_PRE_READER_WIN_H_
74