OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 "components/metrics/file_metrics_provider.h" | |
6 | |
7 #include "base/command_line.h" | |
8 #include "base/files/file.h" | |
9 #include "base/files/file_util.h" | |
10 #include "base/files/memory_mapped_file.h" | |
11 #include "base/logging.h" | |
12 #include "base/metrics/histogram_base.h" | |
13 #include "base/metrics/histogram_macros.h" | |
14 #include "base/metrics/histogram_persistence.h" | |
15 #include "base/metrics/persistent_memory_allocator.h" | |
16 #include "base/task_runner.h" | |
17 #include "base/time/time.h" | |
18 #include "components/metrics/metrics_pref_names.h" | |
19 #include "components/metrics/metrics_service.h" | |
20 #include "components/prefs/pref_registry_simple.h" | |
21 #include "components/prefs/pref_service.h" | |
22 | |
23 | |
24 namespace metrics { | |
25 | |
26 // This structure stores all the information about the files being monitored | |
27 // and their current reporting state. | |
28 struct FileMetricsProvider::FileInfo { | |
29 FileInfo() {} | |
30 ~FileInfo() {} | |
31 | |
32 // Where on disk the file is located. | |
33 base::FilePath path; | |
34 | |
35 // How to access this file (atomic/active). | |
36 FileType type; | |
37 | |
38 // Name used inside prefs to persistent metadata. | |
39 std::string prefs_key; | |
40 | |
41 // The last-seen time of this file to detect change. | |
42 base::Time last_seen; | |
43 | |
44 // Once a file has been recognized as needing to be read, it is |mapped| | |
45 // into memory. If that file is "atomic" then the data from that file | |
46 // will be copied to |data| and the mapped file released. If the file is | |
47 // "active", it remains mapped and nothing is copied to local memory. | |
48 std::vector<uint8_t> data; | |
49 scoped_ptr<base::MemoryMappedFile> mapped; | |
50 scoped_ptr<base::PersistentMemoryAllocator> allocator; | |
51 | |
52 private: | |
53 DISALLOW_COPY_AND_ASSIGN(FileInfo); | |
54 }; | |
55 | |
56 FileMetricsProvider::FileMetricsProvider( | |
57 const scoped_refptr<base::TaskRunner>& task_runner, | |
58 PrefService* local_state) | |
59 : task_runner_(task_runner), | |
60 pref_service_(local_state), | |
61 weak_factory_(this) { | |
62 DCHECK(thread_checker_.CalledOnValidThread()); | |
Alexei Svitkine (slow)
2016/02/19 20:09:44
Nit: No need to do it here since this will always
bcwhite
2016/02/19 20:32:56
Done.
| |
63 } | |
64 | |
65 FileMetricsProvider::~FileMetricsProvider() {} | |
66 | |
67 void FileMetricsProvider::RegisterFile(const base::FilePath& path, | |
68 FileType type, | |
69 const base::StringPiece prefs_key) { | |
70 DCHECK(thread_checker_.CalledOnValidThread()); | |
71 | |
72 scoped_ptr<FileInfo> file(new FileInfo()); | |
73 file->path = path; | |
74 file->type = type; | |
75 file->prefs_key = prefs_key.as_string(); | |
76 | |
77 // |prefs_key| may be empty if the caller does not wish to persist the | |
78 // state across instances of the program. | |
79 if (pref_service_ && !prefs_key.empty()) { | |
80 file->last_seen = base::Time::FromInternalValue( | |
81 pref_service_->GetInt64(metrics::prefs::kMetricsLastSeenPrefix + | |
82 file->prefs_key)); | |
83 } | |
84 | |
85 files_to_check_.push_back(std::move(file)); | |
86 } | |
87 | |
88 // static | |
89 void FileMetricsProvider::RegisterPrefs(PrefRegistrySimple* prefs, | |
90 const base::StringPiece prefs_key) { | |
91 prefs->RegisterInt64Pref(metrics::prefs::kMetricsLastSeenPrefix + | |
92 prefs_key.as_string(), 0); | |
93 } | |
94 | |
95 // This method has all state information passed in |files| and is intended | |
96 // to run on a worker thread rather than the UI thread. | |
Alexei Svitkine (slow)
2016/02/19 20:09:44
Nit: The comment above the method should just be "
bcwhite
2016/02/19 20:32:56
Done.
| |
97 // static | |
98 void FileMetricsProvider::CheckAndMapNewMetricFilesOnTaskRunner( | |
99 FileMetricsProvider::FileInfoList* files) { | |
100 for (scoped_ptr<FileInfo>& file : *files) { | |
101 AccessResult result = CheckAndMapNewMetrics(file.get()); | |
102 // Some results are not reported in order to keep the dashboard clean. | |
103 if (result != ACCESS_RESULT_DOESNT_EXIST && | |
104 result != ACCESS_RESULT_NOT_MODIFIED) { | |
105 UMA_HISTOGRAM_ENUMERATION( | |
106 "UMA.FileMetricsProvider.AccessResult", result, ACCESS_RESULT_MAX); | |
107 } | |
108 } | |
109 } | |
110 | |
111 // This method has all state information passed in |file| and is intended | |
112 // to run on a worker thread rather than the UI thread. | |
113 // static | |
114 FileMetricsProvider::AccessResult FileMetricsProvider::CheckAndMapNewMetrics( | |
115 FileMetricsProvider::FileInfo* file) { | |
Alexei Svitkine (slow)
2016/02/19 20:09:44
Add thread check here.
Alexei Svitkine (slow)
2016/02/19 20:11:40
Sorry, ignore this one.
bcwhite
2016/02/19 20:32:56
Acknowledged.
| |
116 DCHECK(!file->mapped); | |
117 DCHECK(file->data.empty()); | |
118 | |
119 base::File::Info info; | |
120 if (!base::GetFileInfo(file->path, &info)) | |
121 return ACCESS_RESULT_DOESNT_EXIST; | |
122 | |
123 if (info.is_directory || info.size == 0) | |
124 return ACCESS_RESULT_INVALID_FILE; | |
125 | |
126 if (file->last_seen >= info.last_modified) | |
127 return ACCESS_RESULT_NOT_MODIFIED; | |
128 | |
129 // A new file of metrics has been found. Map it into memory. | |
130 // TODO(bcwhite): Make this open read/write when supported for "active". | |
131 file->mapped.reset(new base::MemoryMappedFile()); | |
132 if (!file->mapped->Initialize(file->path)) { | |
133 file->mapped.reset(); | |
134 return ACCESS_RESULT_SYSTEM_MAP_FAILURE; | |
135 } | |
136 | |
137 // Ensure any problems below don't occur repeatedly. | |
138 file->last_seen = info.last_modified; | |
139 | |
140 // Test the validity of the file contents. | |
141 if (!base::FilePersistentMemoryAllocator::IsFileAcceptable(*file->mapped)) | |
142 return ACCESS_RESULT_INVALID_CONTENTS; | |
143 | |
144 // For an "atomic" file, immediately copy the data into local memory and | |
145 // release the file so that it is not held open. | |
146 if (file->type == FILE_HISTOGRAMS_ATOMIC) { | |
147 file->data.assign(file->mapped->data(), | |
148 file->mapped->data() + file->mapped->length()); | |
149 file->mapped.reset(); | |
150 } | |
151 | |
152 return ACCESS_RESULT_SUCCESS; | |
153 } | |
154 | |
155 void FileMetricsProvider::ScheduleFilesCheck() { | |
Alexei Svitkine (slow)
2016/02/19 20:09:44
Add thread check here.
bcwhite
2016/02/19 20:32:56
Done.
| |
156 if (files_to_check_.empty()) | |
157 return; | |
158 | |
159 // Create an independent list of files for checking. This will be Owned() | |
160 // by the reply call given to the task-runner, to be deleted when that call | |
161 // has returned. It is also passed Unretained() to the task itself, safe | |
162 // because that must complete before the reply runs. | |
163 FileInfoList* check_list = new FileInfoList(); | |
164 std::swap(files_to_check_, *check_list); | |
165 task_runner_->PostTaskAndReply( | |
166 FROM_HERE, | |
167 base::Bind(&FileMetricsProvider::CheckAndMapNewMetricFilesOnTaskRunner, | |
168 base::Unretained(check_list)), | |
169 base::Bind(&FileMetricsProvider::RecordFilesChecked, | |
170 weak_factory_.GetWeakPtr(), base::Owned(check_list))); | |
171 } | |
172 | |
173 void FileMetricsProvider::RecordHistogramSnapshotsFromFile( | |
174 base::HistogramSnapshotManager* snapshot_manager, | |
175 FileInfo* file) { | |
Alexei Svitkine (slow)
2016/02/19 20:09:44
Add thread check here.
bcwhite
2016/02/19 20:32:56
Done.
| |
176 base::PersistentMemoryAllocator::Iterator hist_iter; | |
177 file->allocator->CreateIterator(&hist_iter); | |
178 | |
179 int histogram_count = 0; | |
180 while (true) { | |
181 scoped_ptr<base::HistogramBase> histogram( | |
182 base::GetNextPersistentHistogram(file->allocator.get(), &hist_iter)); | |
183 if (!histogram) | |
184 break; | |
185 if (file->type == FILE_HISTOGRAMS_ATOMIC) | |
186 snapshot_manager->PrepareAbsoluteTakingOwnership(std::move(histogram)); | |
187 else | |
188 snapshot_manager->PrepareDeltaTakingOwnership(std::move(histogram)); | |
189 ++histogram_count; | |
190 } | |
191 | |
192 DVLOG(1) << "Reported " << histogram_count << " histograms from " | |
193 << file->path.value(); | |
194 } | |
195 | |
196 void FileMetricsProvider::CreateAllocatorForFile(FileInfo* file) { | |
197 DCHECK(!file->allocator); | |
198 | |
199 // File data was validated earlier. Files are not considered "untrusted" | |
200 // as some processes might be (e.g. Renderer) so there's no need to check | |
201 // again to try to thwart some malicious actor that may have modified the | |
202 // data between then and now. | |
203 if (file->mapped) { | |
204 DCHECK(file->data.empty()); | |
205 // TODO(bcwhite): Make this do read/write when supported for "active". | |
206 file->allocator.reset(new base::FilePersistentMemoryAllocator( | |
207 std::move(file->mapped), 0, std::string())); | |
208 } else { | |
209 DCHECK(!file->mapped); | |
210 file->allocator.reset(new base::PersistentMemoryAllocator( | |
211 &file->data[0], file->data.size(), 0, 0, "", true)); | |
212 } | |
213 } | |
214 | |
215 void FileMetricsProvider::RecordFilesChecked(FileInfoList* checked) { | |
216 DCHECK(thread_checker_.CalledOnValidThread()); | |
217 | |
218 // Move each processed file to either the "to-read" list (for processing) or | |
219 // the "to-check" list (for future checking). | |
220 for (auto iter = checked->begin(); iter != checked->end();) { | |
221 auto temp = iter++; | |
222 const FileInfo* file = temp->get(); | |
223 if (file->mapped || !file->data.empty()) | |
224 files_to_read_.splice(files_to_read_.end(), *checked, temp); | |
225 else | |
226 files_to_check_.splice(files_to_check_.end(), *checked, temp); | |
227 } | |
228 } | |
229 | |
230 void FileMetricsProvider::RecordFileAsSeen(FileInfo* file) { | |
Alexei Svitkine (slow)
2016/02/19 20:11:41
Add thread check here.
bcwhite
2016/02/19 20:32:56
Done.
| |
231 if (pref_service_ && !file->prefs_key.empty()) { | |
232 pref_service_->SetInt64(metrics::prefs::kMetricsLastSeenPrefix + | |
233 file->prefs_key, | |
234 file->last_seen.ToInternalValue()); | |
235 } | |
236 } | |
237 | |
238 void FileMetricsProvider::OnDidCreateMetricsLog() { | |
239 DCHECK(thread_checker_.CalledOnValidThread()); | |
240 | |
241 // Move finished metric files back to list of monitored files. | |
242 for (auto iter = files_to_read_.begin(); iter != files_to_read_.end();) { | |
243 auto temp = iter++; | |
244 const FileInfo* file = temp->get(); | |
245 if (!file->allocator && !file->mapped && file->data.empty()) | |
246 files_to_check_.splice(files_to_check_.end(), files_to_read_, temp); | |
247 } | |
248 | |
249 // Schedule a check to see if there are new metrics to load. If so, they | |
250 // will be reported during the next collection run after this one. The | |
251 // check is run off of the worker-pool so as to not cause delays on the | |
252 // main UI thread (which is currently where metric collection is done). | |
253 ScheduleFilesCheck(); | |
254 } | |
255 | |
256 void FileMetricsProvider::RecordHistogramSnapshots( | |
257 base::HistogramSnapshotManager* snapshot_manager) { | |
258 DCHECK(thread_checker_.CalledOnValidThread()); | |
259 | |
260 for (scoped_ptr<FileInfo>& file : files_to_read_) { | |
261 // If the file is mapped or loaded then it needs to have an allocator | |
262 // attached to it in order to read histograms out of it. | |
263 if (file->mapped || !file->data.empty()) | |
264 CreateAllocatorForFile(file.get()); | |
265 | |
266 // A file should not be under "files to read" unless it has an allocator | |
267 // or is memory-mapped (at which point it will have received an allocator | |
268 // above). However, if this method gets called twice before the scheduled- | |
269 // files-check has a chance to clean up, this may trigger. This also | |
270 // catches the case where creating an allocator from the file has failed. | |
271 if (!file->allocator) | |
272 continue; | |
273 | |
274 // Dump all histograms contained within the file to the snapshot-manager. | |
275 RecordHistogramSnapshotsFromFile(snapshot_manager, file.get()); | |
276 | |
277 // Atomic files are read once and then ignored unless they change. | |
278 if (file->type == FILE_HISTOGRAMS_ATOMIC) { | |
279 DCHECK(!file->mapped); | |
280 file->allocator.reset(); | |
281 file->data.clear(); | |
282 RecordFileAsSeen(file.get()); | |
283 } | |
284 } | |
285 } | |
286 | |
287 } // namespace metrics | |
OLD | NEW |