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

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: Enable partial pre-read experiment Created 8 years, 11 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>
11 #include <shlobj.h> 11 #include <shlobj.h>
12 #include <time.h> 12 #include <time.h>
13 13
14 #include <algorithm>
14 #include <limits> 15 #include <limits>
15 #include <string> 16 #include <string>
16 17
17 #include "base/file_path.h" 18 #include "base/file_path.h"
18 #include "base/logging.h" 19 #include "base/logging.h"
20 #include "base/memory/scoped_ptr.h"
19 #include "base/metrics/histogram.h" 21 #include "base/metrics/histogram.h"
20 #include "base/string_number_conversions.h" 22 #include "base/string_number_conversions.h"
21 #include "base/string_util.h" 23 #include "base/string_util.h"
22 #include "base/threading/thread_restrictions.h" 24 #include "base/threading/thread_restrictions.h"
23 #include "base/time.h" 25 #include "base/time.h"
24 #include "base/utf_string_conversions.h" 26 #include "base/utf_string_conversions.h"
25 #include "base/win/pe_image.h" 27 #include "base/win/pe_image.h"
26 #include "base/win/scoped_comptr.h" 28 #include "base/win/scoped_comptr.h"
27 #include "base/win/scoped_handle.h" 29 #include "base/win/scoped_handle.h"
28 #include "base/win/win_util.h" 30 #include "base/win/win_util.h"
(...skipping 1069 matching lines...) Expand 10 before | Expand all | Expand 10 after
1098 bool success = false; 1100 bool success = false;
1099 HANDLE cp = GetCurrentProcess(); 1101 HANDLE cp = GetCurrentProcess();
1100 if (::GetMappedFileNameW(cp, file_view, mapped_file_path, kMaxPathLength)) { 1102 if (::GetMappedFileNameW(cp, file_view, mapped_file_path, kMaxPathLength)) {
1101 *nt_path = FilePath(mapped_file_path); 1103 *nt_path = FilePath(mapped_file_path);
1102 success = true; 1104 success = true;
1103 } 1105 }
1104 ::UnmapViewOfFile(file_view); 1106 ::UnmapViewOfFile(file_view);
1105 return success; 1107 return success;
1106 } 1108 }
1107 1109
1110 namespace {
1111
1112 // The PE file headers usually fit into a single 1KB page, and a PE file must
1113 // at least contain the initial page with the headers. That said, as long as
1114 // we expect at least sizeof(IMAGE_DOS_HEADER) bytes, we're ok.
Evan Martin 2012/01/27 20:13:58 This comment is about PE files, but it doesn't des
Roger McFarlane (Google) 2012/01/27 21:25:54 Fixed the comments and chose a better name for the
1115 const size_t kMinPreReadBufferSize = 0x400;
1116
1117 // A handy symbolic constant.
1118 const size_t kOneHundredPercent = 100;
1119
1120 void StaticAssertions() {
1121 COMPILE_ASSERT(kMinPreReadBufferSize >= sizeof(IMAGE_DOS_HEADER),
1122 min_pre_read_buffer_size_must_be_bigger_than_dos_header);
1123 }
1124
1125 // This struct provides a deallocation functor for use with scoped_ptr<T>
1126 // allocated with ::VirtualAlloc().
1127 struct ScopedPtrVirtualFree {
1128 void operator() (void* ptr) {
1129 ::VirtualFree(ptr, 0, MEM_RELEASE);
1130 }
1131 };
1132
1133 // A wrapper for the Win32 ::SetFilePointer() function with some error checking.
1134 bool SetFilePointer(HANDLE file_handle, size_t position) {
1135 return position <= std::numeric_limits<LONG>::max() &&
1136 ::SetFilePointer(file_handle,
1137 static_cast<LONG>(position),
1138 NULL,
1139 FILE_BEGIN) != INVALID_SET_FILE_POINTER;
1140 }
1141
1142 // A helper function to read the next @p bytes_to_read bytes from the file
1143 // given by @p file_handle into @p buffer.
1144 bool ReadNextBytes(HANDLE file_handle, void* buffer, size_t bytes_to_read) {
1145 DCHECK(file_handle != INVALID_HANDLE_VALUE);
1146 DCHECK(buffer != NULL);
1147 DCHECK(bytes_to_read > 0);
1148
1149 DWORD bytes_read = 0;
1150 return bytes_to_read <= std::numeric_limits<DWORD>::max() &&
1151 ::ReadFile(file_handle,
1152 buffer,
1153 static_cast<DWORD>(bytes_to_read),
1154 &bytes_read,
1155 NULL) &&
1156 bytes_read == bytes_to_read;
1157 }
1158
1159 // A helper function to extend the @p current_buffer of bytes such that it
1160 // contains @p desired_length bytes read from @p file.
1161 // @pre It is assumed that file has been used to sequentially populate
1162 // @p current_buffer thus far and is already positioned at the
1163 // appropriate read position.
1164 bool ReadMissingBytes(HANDLE file_handle,
1165 std::vector<uint8>* current_buffer,
1166 size_t desired_length) {
1167 DCHECK(file_handle != INVALID_HANDLE_VALUE);
1168 DCHECK(current_buffer != NULL);
1169
1170 size_t current_length = current_buffer->size();
1171 if (current_length >= desired_length)
1172 return true;
1173
1174 size_t bytes_to_read = desired_length - current_length;
1175 current_buffer->resize(desired_length);
1176 return ReadNextBytes(file_handle,
1177 &((*current_buffer)[current_length]),
1178 bytes_to_read);
1179 }
1180
1181 // Return a @p percentage of the number of initialized bytes in the given
1182 // @p section.
1183 //
1184 // This returns a percentage of the lesser of the size of the raw data in
1185 // the section and the virtual size of the section.
1186 //
1187 // Note that sections can have their tails implicitly initialized to zero
1188 // (i.e., their virtual size is larger than the raw size) and that raw data
1189 // is padded to the PE page size if the entire section is initialized (i.e.,
1190 // their raw data size will be larger than the virtual size).
1191 //
1192 // Any data after the initialized portion of the section will be soft-faulted
1193 // in (very quickly) as needed, so we don't need to include it in the returned
1194 // length.
1195 size_t GetPercentageOfSectionLength(const IMAGE_SECTION_HEADER* section,
1196 size_t percentage) {
1197 DCHECK(section != NULL);
1198 DCHECK_GT(percentage, 0);
1199 DCHECK_LE(percentage, kOneHundredPercent);
1200
1201 size_t initialized_length = std::min(section->SizeOfRawData,
1202 section->Misc.VirtualSize);
1203
1204 if (initialized_length == 0)
1205 return 0;
1206
1207 size_t length = (initialized_length * percentage) / kOneHundredPercent;
1208
1209 return std::max<size_t>(length, 1);
1210 }
1211
1212 // Helper function to read through a @p percentage of the given @p section
1213 // of the file given by @p file_handle. The @p temp_buffer is (re)used as
1214 // a transient storage area as the section is read in chunks of
1215 // @p temp_buffer_size bytes.
1216 bool ReadThroughSection(HANDLE file_handle,
1217 const IMAGE_SECTION_HEADER* section,
1218 size_t percentage,
1219 void* temp_buffer,
1220 size_t temp_buffer_size) {
1221 DCHECK(file_handle != INVALID_HANDLE_VALUE);
1222 DCHECK(section != NULL);
1223 DCHECK_LE(percentage, kOneHundredPercent);
1224 DCHECK(temp_buffer != NULL);
1225 DCHECK_GE(temp_buffer_size, kMinPreReadBufferSize);
1226
1227 size_t bytes_to_read = GetPercentageOfSectionLength(section, percentage);
1228 if (bytes_to_read == 0)
1229 return true;
1230
1231 if (!SetFilePointer(file_handle, section->PointerToRawData))
1232 return false;
1233
1234 // Read all chunks except the last one.
1235 size_t max_chunk_size = temp_buffer_size;
1236 while (bytes_to_read > max_chunk_size) {
1237 if (!ReadNextBytes(file_handle, temp_buffer, max_chunk_size))
1238 return false;
1239 bytes_to_read -= max_chunk_size;
1240 }
1241
1242 // Read the last (possibly partial) chunk and return.
1243 return ReadNextBytes(file_handle, temp_buffer, bytes_to_read);
1244 }
1245
1246 // A helper function to touch all pages in the range
1247 // [base_addr, base_addr + length).
1248 void TouchPagesInRange(void* base_addr, size_t length) {
1249 DCHECK(base_addr != NULL);
1250 DCHECK(length > 0);
1251
1252 // Get the system info so we know the page size.
1253 SYSTEM_INFO system_info;
1254 GetSystemInfo(&system_info);
1255
1256 // We don't want to read outside the byte range (which could trigger an
1257 // access violation), so let's figure out the exact locations of the first
1258 // and final bytes we want to read.
1259 volatile uint8* touch_ptr = reinterpret_cast<uint8*>(base_addr);
1260 volatile uint8* final_touch_ptr = touch_ptr + length - 1;
1261
1262 // Read the memory in the range [touch_ptr, final_touch_ptr] with a stride
1263 // of the system page size, to ensure that it's been paged in.
1264 uint8 dummy;
1265 while (touch_ptr < final_touch_ptr) {
1266 dummy = *touch_ptr;
1267 touch_ptr += system_info.dwPageSize;
1268 }
1269 dummy = *final_touch_ptr;
1270 }
1271
1272 } // namespace
1273
1274 namespace internal {
1275
1276 BASE_EXPORT bool PartialPreReadImageOnDisk(const wchar_t* file_path,
1277 size_t percentage,
1278 size_t max_chunk_size) {
1279 // TODO(rogerm): change this to have the number of bytes pre-read per
1280 // section be driven by a static table within the PE file (defaulting to
1281 // full read if it's not there?) that's initialized by the optimization
1282 // toolchain.
1283 DCHECK(file_path != NULL);
1284
1285 if (percentage == 0)
1286 return true;
1287
1288 if (percentage > kOneHundredPercent)
1289 percentage = kOneHundredPercent;
1290
1291 // Validate/setup max_chunk_size, imposing a 1MB minimum on the chunk size.
1292 const size_t kMinChunkSize = 1024 * 1024;
1293 max_chunk_size = std::max(max_chunk_size, kMinChunkSize);
1294
1295 // Open the file.
1296 base::win::ScopedHandle file(
1297 CreateFile(file_path,
1298 GENERIC_READ,
1299 FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
1300 NULL,
1301 OPEN_EXISTING,
1302 FILE_FLAG_SEQUENTIAL_SCAN,
1303 NULL));
1304
1305 if (!file.IsValid())
1306 return false;
1307
1308 // Allocate a resizable buffer for the headers. We initially reserve as much
1309 // space as we typically see as the header size for chrome.dll and other
1310 // PE images.
1311 std::vector<uint8> headers;
1312 headers.reserve(kMinPreReadBufferSize);
1313
1314 // Read, hopefully, all of the headers.
1315 if (!ReadMissingBytes(file, &headers, kMinPreReadBufferSize))
1316 return false;
1317
1318 // The DOS header starts at offset 0 and allows us to get the offset of the
1319 // NT headers. Let's ensure we've read enough to capture the NT headers.
1320 size_t nt_headers_start =
1321 reinterpret_cast<IMAGE_DOS_HEADER*>(&headers[0])->e_lfanew;
1322 size_t nt_headers_end = nt_headers_start + sizeof(IMAGE_NT_HEADERS);
1323 if (!ReadMissingBytes(file, &headers, nt_headers_end))
1324 return false;
1325
1326 // Now that we've got the NT headers we can get the total header size,
1327 // including all of the section headers. Let's ensure we've read enough
1328 // to capture all of the header data.
1329 size_t size_of_headers = reinterpret_cast<IMAGE_NT_HEADERS*>(
1330 &headers[nt_headers_start])->OptionalHeader.SizeOfHeaders;
1331 if (!ReadMissingBytes(file, &headers, size_of_headers))
1332 return false;
1333
1334 // Now we have all of the headers. This is enough to let us use the PEImage
1335 // wrapper to query the structure of the image.
1336 base::win::PEImage pe_image(reinterpret_cast<HMODULE>(&headers[0]));
1337 CHECK(pe_image.VerifyMagic());
1338
1339 // Allocate a buffer to hold the pre-read bytes.
1340 scoped_ptr_malloc<uint8, ScopedPtrVirtualFree> buffer(
1341 reinterpret_cast<uint8*>(
1342 ::VirtualAlloc(NULL, max_chunk_size, MEM_COMMIT, PAGE_READWRITE)));
1343 if (buffer.get() == NULL)
1344 return false;
1345
1346 // Iterate over each section, reading in a percentage of each.
1347 const IMAGE_SECTION_HEADER* section = NULL;
1348 for (UINT i = 0; (section = pe_image.GetSectionHeader(i)) != NULL; ++i) {
1349 CHECK_LE(reinterpret_cast<const uint8*>(section + 1),
1350 &headers[0] + headers.size());
1351 if (!ReadThroughSection(
1352 file, section, percentage, buffer.get(), max_chunk_size))
1353 return false;
1354 }
1355
1356 // We're done.
1357 return true;
1358 }
1359
1360 BASE_EXPORT bool PartialPreReadImageInMemory(const wchar_t* file_path,
1361 size_t percentage) {
1362 // TODO(rogerm): change this to have the number of bytes pre-read per
1363 // section be driven by a static table within the PE file (defaulting to
1364 // full read if it's not there?) that's initialized by the optimization
1365 // toolchain.
1366 DCHECK(file_path != NULL);
1367
1368 if (percentage == 0)
1369 return true;
1370
1371 if (percentage > kOneHundredPercent)
1372 percentage = kOneHundredPercent;
1373
1374 HMODULE dll_module = ::LoadLibraryExW(
1375 file_path,
1376 NULL,
1377 LOAD_WITH_ALTERED_SEARCH_PATH | DONT_RESOLVE_DLL_REFERENCES);
1378
1379 if (!dll_module)
1380 return false;
1381
1382 base::win::PEImage pe_image(dll_module);
1383 CHECK(pe_image.VerifyMagic());
1384
1385 // Iterate over each section, stepping through a percentage of each to page
1386 // it in off the disk.
1387 const IMAGE_SECTION_HEADER* section = NULL;
1388 for (UINT i = 0; (section = pe_image.GetSectionHeader(i)) != NULL; ++i) {
1389 // Get the extent we want to touch.
1390 size_t length = GetPercentageOfSectionLength(section, percentage);
1391 if (length == 0)
1392 continue;
1393 uint8* start =
1394 static_cast<uint8*>(pe_image.RVAToAddr(section->VirtualAddress));
1395
1396 // Verify that the extent we're going to touch falls inside the section
1397 // we expect it to (and by implication, inside the pe_image).
1398 CHECK_EQ(section,
1399 pe_image.GetImageSectionFromAddr(start));
1400 CHECK_EQ(section,
1401 pe_image.GetImageSectionFromAddr(start + length - 1));
1402
1403 // Page in the section range.
1404 TouchPagesInRange(start, length);
1405 }
1406
1407 FreeLibrary(dll_module);
1408
1409 return true;
1410 }
1411
1412 } // namespace file_util::internal
1413
1108 bool PreReadImage(const wchar_t* file_path, size_t size_to_read, 1414 bool PreReadImage(const wchar_t* file_path, size_t size_to_read,
1109 size_t step_size) { 1415 size_t step_size) {
1110 base::ThreadRestrictions::AssertIOAllowed(); 1416 base::ThreadRestrictions::AssertIOAllowed();
1111 if (base::win::GetVersion() > base::win::VERSION_XP) { 1417 if (base::win::GetVersion() > base::win::VERSION_XP) {
1112 // Vista+ branch. On these OSes, the forced reads through the DLL actually 1418 // Vista+ branch. On these OSes, the forced reads through the DLL actually
1113 // slows warm starts. The solution is to sequentially read file contents 1419 // slows warm starts. The solution is to sequentially read file contents
1114 // with an optional cap on total amount to read. 1420 // with an optional cap on total amount to read.
1115 base::win::ScopedHandle file( 1421 base::win::ScopedHandle file(
1116 CreateFile(file_path, 1422 CreateFile(file_path,
1117 GENERIC_READ, 1423 GENERIC_READ,
(...skipping 21 matching lines...) Expand all
1139 size_t total_read = 0; 1445 size_t total_read = 0;
1140 while (::ReadFile(file, buffer, actual_step_size, &len, NULL) && 1446 while (::ReadFile(file, buffer, actual_step_size, &len, NULL) &&
1141 len > 0 && 1447 len > 0 &&
1142 (size_to_read ? total_read < size_to_read : true)) { 1448 (size_to_read ? total_read < size_to_read : true)) {
1143 total_read += static_cast<size_t>(len); 1449 total_read += static_cast<size_t>(len);
1144 } 1450 }
1145 ::VirtualFree(buffer, 0, MEM_RELEASE); 1451 ::VirtualFree(buffer, 0, MEM_RELEASE);
1146 } else { 1452 } else {
1147 // WinXP branch. Here, reading the DLL from disk doesn't do 1453 // 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 1454 // what we want so instead we pull the pages into memory by loading
1149 // the DLL and touching pages at a stride. 1455 // the DLL and touching pages at a stride. We use the system's page
1456 // size as the stride, ignoring the passed in step_size, to make sure
1457 // each page in the range is touched.
1150 HMODULE dll_module = ::LoadLibraryExW( 1458 HMODULE dll_module = ::LoadLibraryExW(
1151 file_path, 1459 file_path,
1152 NULL, 1460 NULL,
1153 LOAD_WITH_ALTERED_SEARCH_PATH | DONT_RESOLVE_DLL_REFERENCES); 1461 LOAD_WITH_ALTERED_SEARCH_PATH | DONT_RESOLVE_DLL_REFERENCES);
1154 1462
1155 if (!dll_module) 1463 if (!dll_module)
1156 return false; 1464 return false;
1157 1465
1158 base::win::PEImage pe_image(dll_module); 1466 base::win::PEImage pe_image(dll_module);
1467 CHECK(pe_image.VerifyMagic());
1468
1469 // We don't want to read past the end of the module (which could trigger
1470 // an access violation), so make sure to check the image size.
1159 PIMAGE_NT_HEADERS nt_headers = pe_image.GetNTHeaders(); 1471 PIMAGE_NT_HEADERS nt_headers = pe_image.GetNTHeaders();
1160 size_t actual_size_to_read = size_to_read ? size_to_read : 1472 size_t dll_module_length = std::min(
1161 nt_headers->OptionalHeader.SizeOfImage; 1473 size_to_read ? size_to_read : ~0,
1162 volatile uint8* touch = reinterpret_cast<uint8*>(dll_module); 1474 static_cast<size_t>(nt_headers->OptionalHeader.SizeOfImage));
1163 size_t offset = 0; 1475
1164 while (offset < actual_size_to_read) { 1476 // Page in then release the module.
1165 uint8 unused = *(touch + offset); 1477 TouchPagesInRange(dll_module, dll_module_length);
1166 offset += step_size;
1167 }
1168 FreeLibrary(dll_module); 1478 FreeLibrary(dll_module);
1169 } 1479 }
1170 1480
1171 return true; 1481 return true;
1172 } 1482 }
1173 1483
1484 bool PartialPreReadImage(const wchar_t* file_path,
1485 size_t percentage,
1486 size_t max_chunk_size) {
1487 base::ThreadRestrictions::AssertIOAllowed();
1488
1489 if (percentage >= kOneHundredPercent) {
1490 // If we're reading the whole image, we don't need to parse headers and
1491 // navigate sections, the basic PreReadImage() can be used to just step
1492 // blindly through the entire file / address-space.
1493 return PreReadImage(file_path, 0, max_chunk_size);
1494 }
1495
1496 using internal::PartialPreReadImageInMemory;
1497 using internal::PartialPreReadImageOnDisk;
1498
1499 if (base::win::GetVersion() > base::win::VERSION_XP) {
1500 // Vista+ branch. On these OSes, we warm up the Image by reading its
1501 // file off the disk.
1502 return PartialPreReadImageOnDisk(file_path, percentage, max_chunk_size);
1503 }
1504
1505 // WinXP branch. For XP, reading the image from disk doesn't do what we want
1506 // so instead we pull the pages into memory by loading the DLL and touching
1507 // initialized pages at a stride.
1508 return PartialPreReadImageInMemory(file_path, percentage);
1509 }
1510
1174 } // namespace file_util 1511 } // namespace file_util
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698