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