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 } |
| 63 |
| 64 FileMetricsProvider::~FileMetricsProvider() {} |
| 65 |
| 66 void FileMetricsProvider::RegisterFile(const base::FilePath& path, |
| 67 FileType type, |
| 68 const base::StringPiece prefs_key) { |
| 69 DCHECK(thread_checker_.CalledOnValidThread()); |
| 70 |
| 71 scoped_ptr<FileInfo> file(new FileInfo()); |
| 72 file->path = path; |
| 73 file->type = type; |
| 74 file->prefs_key = prefs_key.as_string(); |
| 75 |
| 76 // |prefs_key| may be empty if the caller does not wish to persist the |
| 77 // state across instances of the program. |
| 78 if (pref_service_ && !prefs_key.empty()) { |
| 79 file->last_seen = base::Time::FromInternalValue( |
| 80 pref_service_->GetInt64(metrics::prefs::kMetricsLastSeenPrefix + |
| 81 file->prefs_key)); |
| 82 } |
| 83 |
| 84 files_to_check_.push_back(std::move(file)); |
| 85 } |
| 86 |
| 87 // static |
| 88 void FileMetricsProvider::RegisterPrefs(PrefRegistrySimple* prefs, |
| 89 const base::StringPiece prefs_key) { |
| 90 prefs->RegisterInt64Pref(metrics::prefs::kMetricsLastSeenPrefix + |
| 91 prefs_key.as_string(), 0); |
| 92 } |
| 93 |
| 94 // static |
| 95 void FileMetricsProvider::CheckAndMapNewMetricFilesOnTaskRunner( |
| 96 FileMetricsProvider::FileInfoList* files) { |
| 97 // This method has all state information passed in |files| and is intended |
| 98 // to run on a worker thread rather than the UI thread. |
| 99 for (scoped_ptr<FileInfo>& file : *files) { |
| 100 AccessResult result = CheckAndMapNewMetrics(file.get()); |
| 101 // Some results are not reported in order to keep the dashboard clean. |
| 102 if (result != ACCESS_RESULT_DOESNT_EXIST && |
| 103 result != ACCESS_RESULT_NOT_MODIFIED) { |
| 104 UMA_HISTOGRAM_ENUMERATION( |
| 105 "UMA.FileMetricsProvider.AccessResult", result, ACCESS_RESULT_MAX); |
| 106 } |
| 107 } |
| 108 } |
| 109 |
| 110 // This method has all state information passed in |file| and is intended |
| 111 // to run on a worker thread rather than the UI thread. |
| 112 // static |
| 113 FileMetricsProvider::AccessResult FileMetricsProvider::CheckAndMapNewMetrics( |
| 114 FileMetricsProvider::FileInfo* file) { |
| 115 DCHECK(!file->mapped); |
| 116 DCHECK(file->data.empty()); |
| 117 |
| 118 base::File::Info info; |
| 119 if (!base::GetFileInfo(file->path, &info)) |
| 120 return ACCESS_RESULT_DOESNT_EXIST; |
| 121 |
| 122 if (info.is_directory || info.size == 0) |
| 123 return ACCESS_RESULT_INVALID_FILE; |
| 124 |
| 125 if (file->last_seen >= info.last_modified) |
| 126 return ACCESS_RESULT_NOT_MODIFIED; |
| 127 |
| 128 // A new file of metrics has been found. Map it into memory. |
| 129 // TODO(bcwhite): Make this open read/write when supported for "active". |
| 130 file->mapped.reset(new base::MemoryMappedFile()); |
| 131 if (!file->mapped->Initialize(file->path)) { |
| 132 file->mapped.reset(); |
| 133 return ACCESS_RESULT_SYSTEM_MAP_FAILURE; |
| 134 } |
| 135 |
| 136 // Ensure any problems below don't occur repeatedly. |
| 137 file->last_seen = info.last_modified; |
| 138 |
| 139 // Test the validity of the file contents. |
| 140 if (!base::FilePersistentMemoryAllocator::IsFileAcceptable(*file->mapped)) |
| 141 return ACCESS_RESULT_INVALID_CONTENTS; |
| 142 |
| 143 // For an "atomic" file, immediately copy the data into local memory and |
| 144 // release the file so that it is not held open. |
| 145 if (file->type == FILE_HISTOGRAMS_ATOMIC) { |
| 146 file->data.assign(file->mapped->data(), |
| 147 file->mapped->data() + file->mapped->length()); |
| 148 file->mapped.reset(); |
| 149 } |
| 150 |
| 151 return ACCESS_RESULT_SUCCESS; |
| 152 } |
| 153 |
| 154 void FileMetricsProvider::ScheduleFilesCheck() { |
| 155 DCHECK(thread_checker_.CalledOnValidThread()); |
| 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) { |
| 176 DCHECK(thread_checker_.CalledOnValidThread()); |
| 177 base::PersistentMemoryAllocator::Iterator hist_iter; |
| 178 file->allocator->CreateIterator(&hist_iter); |
| 179 |
| 180 int histogram_count = 0; |
| 181 while (true) { |
| 182 scoped_ptr<base::HistogramBase> histogram( |
| 183 base::GetNextPersistentHistogram(file->allocator.get(), &hist_iter)); |
| 184 if (!histogram) |
| 185 break; |
| 186 if (file->type == FILE_HISTOGRAMS_ATOMIC) |
| 187 snapshot_manager->PrepareAbsoluteTakingOwnership(std::move(histogram)); |
| 188 else |
| 189 snapshot_manager->PrepareDeltaTakingOwnership(std::move(histogram)); |
| 190 ++histogram_count; |
| 191 } |
| 192 |
| 193 DVLOG(1) << "Reported " << histogram_count << " histograms from " |
| 194 << file->path.value(); |
| 195 } |
| 196 |
| 197 void FileMetricsProvider::CreateAllocatorForFile(FileInfo* file) { |
| 198 DCHECK(!file->allocator); |
| 199 |
| 200 // File data was validated earlier. Files are not considered "untrusted" |
| 201 // as some processes might be (e.g. Renderer) so there's no need to check |
| 202 // again to try to thwart some malicious actor that may have modified the |
| 203 // data between then and now. |
| 204 if (file->mapped) { |
| 205 DCHECK(file->data.empty()); |
| 206 // TODO(bcwhite): Make this do read/write when supported for "active". |
| 207 file->allocator.reset(new base::FilePersistentMemoryAllocator( |
| 208 std::move(file->mapped), 0, std::string())); |
| 209 } else { |
| 210 DCHECK(!file->mapped); |
| 211 file->allocator.reset(new base::PersistentMemoryAllocator( |
| 212 &file->data[0], file->data.size(), 0, 0, "", true)); |
| 213 } |
| 214 } |
| 215 |
| 216 void FileMetricsProvider::RecordFilesChecked(FileInfoList* checked) { |
| 217 DCHECK(thread_checker_.CalledOnValidThread()); |
| 218 |
| 219 // Move each processed file to either the "to-read" list (for processing) or |
| 220 // the "to-check" list (for future checking). |
| 221 for (auto iter = checked->begin(); iter != checked->end();) { |
| 222 auto temp = iter++; |
| 223 const FileInfo* file = temp->get(); |
| 224 if (file->mapped || !file->data.empty()) |
| 225 files_to_read_.splice(files_to_read_.end(), *checked, temp); |
| 226 else |
| 227 files_to_check_.splice(files_to_check_.end(), *checked, temp); |
| 228 } |
| 229 } |
| 230 |
| 231 void FileMetricsProvider::RecordFileAsSeen(FileInfo* file) { |
| 232 DCHECK(thread_checker_.CalledOnValidThread()); |
| 233 |
| 234 if (pref_service_ && !file->prefs_key.empty()) { |
| 235 pref_service_->SetInt64(metrics::prefs::kMetricsLastSeenPrefix + |
| 236 file->prefs_key, |
| 237 file->last_seen.ToInternalValue()); |
| 238 } |
| 239 } |
| 240 |
| 241 void FileMetricsProvider::OnDidCreateMetricsLog() { |
| 242 DCHECK(thread_checker_.CalledOnValidThread()); |
| 243 |
| 244 // Move finished metric files back to list of monitored files. |
| 245 for (auto iter = files_to_read_.begin(); iter != files_to_read_.end();) { |
| 246 auto temp = iter++; |
| 247 const FileInfo* file = temp->get(); |
| 248 if (!file->allocator && !file->mapped && file->data.empty()) |
| 249 files_to_check_.splice(files_to_check_.end(), files_to_read_, temp); |
| 250 } |
| 251 |
| 252 // Schedule a check to see if there are new metrics to load. If so, they |
| 253 // will be reported during the next collection run after this one. The |
| 254 // check is run off of the worker-pool so as to not cause delays on the |
| 255 // main UI thread (which is currently where metric collection is done). |
| 256 ScheduleFilesCheck(); |
| 257 } |
| 258 |
| 259 void FileMetricsProvider::RecordHistogramSnapshots( |
| 260 base::HistogramSnapshotManager* snapshot_manager) { |
| 261 DCHECK(thread_checker_.CalledOnValidThread()); |
| 262 |
| 263 for (scoped_ptr<FileInfo>& file : files_to_read_) { |
| 264 // If the file is mapped or loaded then it needs to have an allocator |
| 265 // attached to it in order to read histograms out of it. |
| 266 if (file->mapped || !file->data.empty()) |
| 267 CreateAllocatorForFile(file.get()); |
| 268 |
| 269 // A file should not be under "files to read" unless it has an allocator |
| 270 // or is memory-mapped (at which point it will have received an allocator |
| 271 // above). However, if this method gets called twice before the scheduled- |
| 272 // files-check has a chance to clean up, this may trigger. This also |
| 273 // catches the case where creating an allocator from the file has failed. |
| 274 if (!file->allocator) |
| 275 continue; |
| 276 |
| 277 // Dump all histograms contained within the file to the snapshot-manager. |
| 278 RecordHistogramSnapshotsFromFile(snapshot_manager, file.get()); |
| 279 |
| 280 // Atomic files are read once and then ignored unless they change. |
| 281 if (file->type == FILE_HISTOGRAMS_ATOMIC) { |
| 282 DCHECK(!file->mapped); |
| 283 file->allocator.reset(); |
| 284 file->data.clear(); |
| 285 RecordFileAsSeen(file.get()); |
| 286 } |
| 287 } |
| 288 } |
| 289 |
| 290 } // namespace metrics |
OLD | NEW |