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