Chromium Code Reviews| Index: chrome/browser/fragmentation_checker_win.cc |
| =================================================================== |
| --- chrome/browser/fragmentation_checker_win.cc (revision 0) |
| +++ chrome/browser/fragmentation_checker_win.cc (revision 0) |
| @@ -0,0 +1,121 @@ |
| +// Copyright (c) 2011 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. |
| + |
| +#include "chrome/browser/fragmentation_checker_win.h" |
| + |
| +#include <windows.h> |
| +#include <winioctl.h> |
| + |
| +#include <algorithm> |
| +#include <vector> |
| + |
| +#include "base/at_exit.h" |
| +#include "base/basictypes.h" |
| +#include "base/command_line.h" |
| +#include "base/file_path.h" |
| +#include "base/file_util.h" |
| +#include "base/logging.h" |
| +#include "base/metrics/histogram.h" |
| +#include "base/path_service.h" |
| + |
| +namespace { |
| + |
| +const int kMaxExtentCount = 1 << 16; |
| + |
| +size_t ComputeRetrievalPointersBufferSize(int number_of_extents) { |
| + RETRIEVAL_POINTERS_BUFFER buffer; |
| + return sizeof(buffer) + (number_of_extents - 1) * sizeof(buffer.Extents); |
| +} |
| + |
| +} // namespace |
| + |
| +namespace fragmentation_checker { |
| + |
| +bool CountFileExtents(const FilePath& file_path, int* file_extents_count) { |
| + DCHECK(file_extents_count); |
| + |
| + bool success = false; |
| + |
| + base::PlatformFileError error_code = base::PLATFORM_FILE_ERROR_FAILED; |
| + base::PlatformFile file_handle = CreatePlatformFile( |
| + file_path, |
| + base::PLATFORM_FILE_OPEN | base::PLATFORM_FILE_READ, |
| + NULL, |
| + &error_code); |
| + if (error_code == base::PLATFORM_FILE_OK) { |
| + STARTING_VCN_INPUT_BUFFER starting_vcn_input_buffer = {0}; |
| + |
| + // Compute an output size capable of holding 16 extents at first. This will |
| + // fail when the number of extents exceeds 16, in which case we make |
| + // a bigger buffer capable of holding up to kMaxExtentCounts. |
| + int extents_guess = 16; |
| + size_t output_size = ComputeRetrievalPointersBufferSize(extents_guess); |
| + std::vector<uint8> retrieval_pointers_buffer(output_size); |
| + |
| + DWORD bytes_returned = 0; |
| + |
| + bool result = false; |
| + do { |
| + result = DeviceIoControl( |
| + file_handle, |
| + FSCTL_GET_RETRIEVAL_POINTERS, |
| + reinterpret_cast<void*>(&starting_vcn_input_buffer), |
| + sizeof(starting_vcn_input_buffer), |
| + reinterpret_cast<void*>(&retrieval_pointers_buffer[0]), |
| + retrieval_pointers_buffer.size(), |
| + &bytes_returned, |
| + NULL) != FALSE; |
| + |
| + if (!result) { |
| + if (GetLastError() == ERROR_MORE_DATA) { |
| + // Grow the extents we can handle |
| + extents_guess *= 2; |
| + if (extents_guess > kMaxExtentCount) { |
|
grt (UTC plus 2)
2011/10/01 10:59:38
?
*file_extents_count = kMaxExtentCount;
return tr
robertshield
2011/10/03 01:46:36
Done.
|
| + LOG(ERROR) << "FSCTL_GET_RETRIEVAL_POINTERS output buffer exceeded " |
| + "maximum size."; |
| + break; |
| + } |
| + output_size = ComputeRetrievalPointersBufferSize(extents_guess); |
| + retrieval_pointers_buffer.assign(output_size, 0); |
| + } else { |
| + PLOG(ERROR) << "FSCTL_GET_RETRIEVAL_POINTERS failed."; |
| + break; |
| + } |
| + } |
| + } while (!result); |
| + |
| + if (result) { |
| + RETRIEVAL_POINTERS_BUFFER* retrieval_pointers = |
| + reinterpret_cast<RETRIEVAL_POINTERS_BUFFER*>( |
| + &retrieval_pointers_buffer[0]); |
| + *file_extents_count = static_cast<int>(retrieval_pointers->ExtentCount); |
| + success = true; |
| + } else { |
| + LOG(ERROR) << "Failed to retrieve extents."; |
| + } |
| + } else { |
| + LOG(ERROR) << "Failed to open module file to check extents. Error code = " |
| + << error_code; |
| + } |
| + |
| + return success; |
| +} |
| + |
| +void RecordFragmentationMetricForCurrentModule() { |
| + FilePath module_path; |
| + if (PathService::Get(base::FILE_MODULE, &module_path)) { |
| + int file_extent_count = 0; |
| + if (CountFileExtents(module_path, &file_extent_count)) { |
| + UMA_HISTOGRAM_CUSTOM_COUNTS("Fragmentation.ModuleExtents", |
| + file_extent_count, |
| + 1, |
|
grt (UTC plus 2)
2011/10/01 11:04:09
wdyt about sending up 0 as the count when the coun
grt (UTC plus 2)
2011/10/01 22:34:53
reached -> read (i.e., CountFileExtents returns fa
robertshield
2011/10/03 01:46:36
Done.
robertshield
2011/10/03 01:46:36
Sounds reasonable. I modified the CountFileExtents
|
| + kMaxExtentCount, |
| + 50); |
| + } |
| + } else { |
| + NOTREACHED() << "Could not get path to current module."; |
| + } |
| +} |
| + |
| +} // namespace fragmentation_checker |
| Property changes on: chrome\browser\fragmentation_checker_win.cc |
| ___________________________________________________________________ |
| Added: svn:eol-style |
| + LF |