OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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/browser/fragmentation_checker_win.h" |
| 6 |
| 7 #include <windows.h> |
| 8 #include <winioctl.h> |
| 9 |
| 10 #include <vector> |
| 11 |
| 12 #include "base/file_path.h" |
| 13 #include "base/logging.h" |
| 14 #include "base/metrics/histogram.h" |
| 15 #include "base/platform_file.h" |
| 16 #include "base/path_service.h" |
| 17 |
| 18 namespace { |
| 19 |
| 20 size_t ComputeRetrievalPointersBufferSize(int number_of_extents) { |
| 21 RETRIEVAL_POINTERS_BUFFER buffer; |
| 22 return sizeof(buffer) + (number_of_extents - 1) * sizeof(buffer.Extents); |
| 23 } |
| 24 |
| 25 } // namespace |
| 26 |
| 27 namespace fragmentation_checker { |
| 28 |
| 29 int CountFileExtents(const FilePath& file_path) { |
| 30 int file_extents_count = 0; |
| 31 |
| 32 base::PlatformFileError error_code = base::PLATFORM_FILE_ERROR_FAILED; |
| 33 base::PlatformFile file_handle = CreatePlatformFile( |
| 34 file_path, |
| 35 base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, |
| 36 NULL, |
| 37 &error_code); |
| 38 if (error_code == base::PLATFORM_FILE_OK) { |
| 39 STARTING_VCN_INPUT_BUFFER starting_vcn_input_buffer = {0}; |
| 40 |
| 41 // Compute an output size capable of holding 16 extents at first. This will |
| 42 // fail when the number of extents exceeds 16, in which case we make |
| 43 // a bigger buffer capable of holding up to kMaxExtentCounts. |
| 44 int extents_guess = 16; |
| 45 size_t output_size = ComputeRetrievalPointersBufferSize(extents_guess); |
| 46 std::vector<uint8> retrieval_pointers_buffer(output_size); |
| 47 |
| 48 DWORD bytes_returned = 0; |
| 49 |
| 50 bool result = false; |
| 51 do { |
| 52 result = DeviceIoControl( |
| 53 file_handle, |
| 54 FSCTL_GET_RETRIEVAL_POINTERS, |
| 55 reinterpret_cast<void*>(&starting_vcn_input_buffer), |
| 56 sizeof(starting_vcn_input_buffer), |
| 57 reinterpret_cast<void*>(&retrieval_pointers_buffer[0]), |
| 58 retrieval_pointers_buffer.size(), |
| 59 &bytes_returned, |
| 60 NULL) != FALSE; |
| 61 |
| 62 if (!result) { |
| 63 if (GetLastError() == ERROR_MORE_DATA) { |
| 64 // Grow the extents we can handle |
| 65 extents_guess *= 2; |
| 66 if (extents_guess > kMaxExtentCount) { |
| 67 LOG(ERROR) << "FSCTL_GET_RETRIEVAL_POINTERS output buffer exceeded " |
| 68 "maximum size."; |
| 69 file_extents_count = kMaxExtentCount; |
| 70 break; |
| 71 } |
| 72 output_size = ComputeRetrievalPointersBufferSize(extents_guess); |
| 73 retrieval_pointers_buffer.assign(output_size, 0); |
| 74 } else { |
| 75 PLOG(ERROR) << "FSCTL_GET_RETRIEVAL_POINTERS failed."; |
| 76 break; |
| 77 } |
| 78 } |
| 79 } while (!result); |
| 80 |
| 81 if (result) { |
| 82 RETRIEVAL_POINTERS_BUFFER* retrieval_pointers = |
| 83 reinterpret_cast<RETRIEVAL_POINTERS_BUFFER*>( |
| 84 &retrieval_pointers_buffer[0]); |
| 85 file_extents_count = static_cast<int>(retrieval_pointers->ExtentCount); |
| 86 } else { |
| 87 LOG(ERROR) << "Failed to retrieve extents."; |
| 88 } |
| 89 } else { |
| 90 LOG(ERROR) << "Failed to open module file to check extents. Error code = " |
| 91 << error_code; |
| 92 } |
| 93 |
| 94 return file_extents_count; |
| 95 } |
| 96 |
| 97 void RecordFragmentationMetricForCurrentModule() { |
| 98 FilePath module_path; |
| 99 if (PathService::Get(base::FILE_MODULE, &module_path)) { |
| 100 int file_extent_count = CountFileExtents(module_path); |
| 101 UMA_HISTOGRAM_CUSTOM_COUNTS("Fragmentation.ModuleExtents", |
| 102 file_extent_count, |
| 103 0, |
| 104 kMaxExtentCount, |
| 105 50); |
| 106 } else { |
| 107 NOTREACHED() << "Could not get path to current module."; |
| 108 } |
| 109 } |
| 110 |
| 111 } // namespace fragmentation_checker |
OLD | NEW |