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

Side by Side Diff: base/file_util_win.cc

Issue 9235053: Add a PartialPreReadImage function to file_util (on Windows). (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: make image_pre_reader project windows only Created 8 years, 10 months 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
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 "base/file_util.h" 5 #include "base/file_util.h"
6 6
7 #include <windows.h> 7 #include <windows.h>
8 #include <propvarutil.h> 8 #include <propvarutil.h>
9 #include <psapi.h> 9 #include <psapi.h>
10 #include <shellapi.h> 10 #include <shellapi.h>
(...skipping 1087 matching lines...) Expand 10 before | Expand all | Expand 10 after
1098 bool success = false; 1098 bool success = false;
1099 HANDLE cp = GetCurrentProcess(); 1099 HANDLE cp = GetCurrentProcess();
1100 if (::GetMappedFileNameW(cp, file_view, mapped_file_path, kMaxPathLength)) { 1100 if (::GetMappedFileNameW(cp, file_view, mapped_file_path, kMaxPathLength)) {
1101 *nt_path = FilePath(mapped_file_path); 1101 *nt_path = FilePath(mapped_file_path);
1102 success = true; 1102 success = true;
1103 } 1103 }
1104 ::UnmapViewOfFile(file_view); 1104 ::UnmapViewOfFile(file_view);
1105 return success; 1105 return success;
1106 } 1106 }
1107 1107
1108 bool PreReadImage(const wchar_t* file_path, size_t size_to_read,
1109 size_t step_size) {
1110 base::ThreadRestrictions::AssertIOAllowed();
1111 if (base::win::GetVersion() > base::win::VERSION_XP) {
1112 // Vista+ branch. On these OSes, the forced reads through the DLL actually
1113 // slows warm starts. The solution is to sequentially read file contents
1114 // with an optional cap on total amount to read.
1115 base::win::ScopedHandle file(
1116 CreateFile(file_path,
1117 GENERIC_READ,
1118 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1119 NULL,
1120 OPEN_EXISTING,
1121 FILE_FLAG_SEQUENTIAL_SCAN,
1122 NULL));
1123
1124 if (!file.IsValid())
1125 return false;
1126
1127 // Default to 1MB sequential reads.
1128 const DWORD actual_step_size = std::max(static_cast<DWORD>(step_size),
1129 static_cast<DWORD>(1024*1024));
1130 LPVOID buffer = ::VirtualAlloc(NULL,
1131 actual_step_size,
1132 MEM_COMMIT,
1133 PAGE_READWRITE);
1134
1135 if (buffer == NULL)
1136 return false;
1137
1138 DWORD len;
1139 size_t total_read = 0;
1140 while (::ReadFile(file, buffer, actual_step_size, &len, NULL) &&
1141 len > 0 &&
1142 (size_to_read ? total_read < size_to_read : true)) {
1143 total_read += static_cast<size_t>(len);
1144 }
1145 ::VirtualFree(buffer, 0, MEM_RELEASE);
1146 } else {
1147 // WinXP branch. Here, reading the DLL from disk doesn't do
1148 // what we want so instead we pull the pages into memory by loading
1149 // the DLL and touching pages at a stride.
1150 HMODULE dll_module = ::LoadLibraryExW(
1151 file_path,
1152 NULL,
1153 LOAD_WITH_ALTERED_SEARCH_PATH | DONT_RESOLVE_DLL_REFERENCES);
1154
1155 if (!dll_module)
1156 return false;
1157
1158 base::win::PEImage pe_image(dll_module);
1159 PIMAGE_NT_HEADERS nt_headers = pe_image.GetNTHeaders();
1160 size_t actual_size_to_read = size_to_read ? size_to_read :
1161 nt_headers->OptionalHeader.SizeOfImage;
1162 volatile uint8* touch = reinterpret_cast<uint8*>(dll_module);
1163 size_t offset = 0;
1164 while (offset < actual_size_to_read) {
1165 uint8 unused = *(touch + offset);
1166 offset += step_size;
1167 }
1168 FreeLibrary(dll_module);
1169 }
1170
1171 return true;
1172 }
1173
1174 } // namespace file_util 1108 } // namespace file_util
OLDNEW
« no previous file with comments | « base/file_util.h ('k') | chrome/app/client_util.cc » ('j') | chrome/app/client_util.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698