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

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

Issue 1412673006: Transform ImagePreReader into PreReadFile. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@a_get_module_path
Patch Set: git cl format Created 5 years, 1 month 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
(Empty)
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 #include "chrome/app/file_pre_reader_win.h"
6
7 #include <windows.h>
8 #include <algorithm>
9
10 #include "base/files/memory_mapped_file.h"
11 #include "base/logging.h"
12 #include "base/threading/thread_restrictions.h"
13 #include "base/win/scoped_handle.h"
14 #include "base/win/windows_version.h"
15
16 namespace {
17
18 // A helper function to touch all pages in the range
19 // [base_addr, base_addr + length).
20 void TouchPagesInRange(const void* base_addr, size_t length) {
21 DCHECK(base_addr != NULL);
22 DCHECK(length > 0);
23
24 // Get the system info so we know the page size. Also, make sure we use a
25 // non-zero value for the page size; GetSystemInfo() is hookable/patchable,
26 // and you never know what shenanigans someone could get up to.
27 SYSTEM_INFO system_info = {};
28 GetSystemInfo(&system_info);
29 if (system_info.dwPageSize == 0)
30 system_info.dwPageSize = 4096;
31
32 // We don't want to read outside the byte range (which could trigger an
33 // access violation), so let's figure out the exact locations of the first
34 // and final bytes we want to read.
35 volatile uint8 const* touch_ptr = reinterpret_cast<uint8 const*>(base_addr);
grt (UTC plus 2) 2015/10/29 19:15:06 uint8_t here and elsewhere
fdoray 2015/10/30 21:08:54 Done.
36 volatile uint8 const* final_touch_ptr = touch_ptr + length - 1;
37
38 // Read the memory in the range [touch_ptr, final_touch_ptr] with a stride
39 // of the system page size, to ensure that it's been paged in.
40 uint8 dummy;
41 while (touch_ptr < final_touch_ptr) {
42 dummy = *touch_ptr;
43 touch_ptr += system_info.dwPageSize;
44 }
45 dummy = *final_touch_ptr;
46 }
47
48 } // namespace
49
50 bool FilePreReader::PreReadImage(const wchar_t* file_path, size_t step_size) {
51 base::ThreadRestrictions::AssertIOAllowed();
52 if (base::win::GetVersion() > base::win::VERSION_XP) {
53 // Vista+ branch. On these OSes, the forced reads through the DLL actually
54 // slows warm starts. The solution is to sequentially read file contents
55 // with an optional cap on total amount to read.
56 base::win::ScopedHandle file(
grt (UTC plus 2) 2015/10/29 19:15:07 please add a comment that base::File isn't used be
fdoray 2015/10/30 21:08:54 Done. base::File::FLAG_SEQUENTIAL_SCAN was added i
57 CreateFile(file_path, GENERIC_READ,
58 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
59 OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL));
60
61 if (!file.IsValid())
62 return false;
63
64 // Default to 1MB sequential reads.
65 const DWORD actual_step_size = std::max(static_cast<DWORD>(step_size),
66 static_cast<DWORD>(1024 * 1024));
67 LPVOID buffer =
68 ::VirtualAlloc(NULL, actual_step_size, MEM_COMMIT, PAGE_READWRITE);
69
70 if (buffer == NULL)
71 return false;
72
73 DWORD len;
74 size_t total_read = 0;
grt (UTC plus 2) 2015/10/29 19:15:06 file sizes are 64bits on windows, so uint64_t. bet
fdoray 2015/10/30 21:08:54 Done.
75 while (::ReadFile(file.Get(), buffer, actual_step_size, &len, NULL) &&
76 len > 0) {
77 total_read += static_cast<size_t>(len);
78 }
79 ::VirtualFree(buffer, 0, MEM_RELEASE);
80 } else {
81 // WinXP branch. Here, reading the DLL from disk doesn't do
82 // what we want so instead we pull the pages into memory and touch pages at
83 // a stride. We use the system's page size as the stride, ignoring the
84 // passed in step_size, to make sure each page in the range is touched.
85 base::MemoryMappedFile file_memory_map;
86 CHECK(file_memory_map.Initialize(base::FilePath(file_path)));
grt (UTC plus 2) 2015/10/29 19:15:06 why crash here but not on line 62?
fdoray 2015/10/30 21:08:54 Done. We shouldn't crash.
87 TouchPagesInRange(file_memory_map.data(), file_memory_map.length());
88 }
89
90 return true;
91 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698