summaryrefslogtreecommitdiffstats
path: root/chrome/app/image_pre_reader_win.h
blob: 96acc5611721b5a1987e1916a94f95893b5415f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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_