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 #ifndef COMPONENTS_METRICS_FILE_METRICS_PROVIDER_H_ |
| 6 #define COMPONENTS_METRICS_FILE_METRICS_PROVIDER_H_ |
| 7 |
| 8 #include <list> |
| 9 #include <string> |
| 10 |
| 11 #include "base/callback.h" |
| 12 #include "base/files/file_path.h" |
| 13 #include "base/gtest_prod_util.h" |
| 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/memory/weak_ptr.h" |
| 16 #include "base/threading/thread_checker.h" |
| 17 #include "base/time/time.h" |
| 18 #include "components/metrics/metrics_provider.h" |
| 19 |
| 20 class PrefRegistrySimple; |
| 21 class PrefService; |
| 22 |
| 23 namespace base { |
| 24 class MemoryMappedFile; |
| 25 class PersistentMemoryAllocator; |
| 26 class TaskRunner; |
| 27 } |
| 28 |
| 29 namespace metrics { |
| 30 |
| 31 // FileMetricsProvider gathers and logs histograms written to files on disk. |
| 32 // Any number of files can be registered and will be polled once per upload |
| 33 // cycle (at startup and periodically thereafter -- about every 30 minutes |
| 34 // for desktop) for data to send. |
| 35 class FileMetricsProvider : public metrics::MetricsProvider { |
| 36 public: |
| 37 enum FileType { |
| 38 // "Atomic" files are a collection of histograms that are written |
| 39 // completely in a single atomic operation (typically a write followed |
| 40 // by an atomic rename) and the file is never updated again except to |
| 41 // be replaced by a completely new set of histograms. This is the only |
| 42 // option that can be used if the file is not writeable by *this* |
| 43 // process. |
| 44 FILE_HISTOGRAMS_ATOMIC, |
| 45 |
| 46 // "Active" files may be open by one or more other processes and updated |
| 47 // at any time with new samples or new histograms. Such files may also be |
| 48 // inactive for any period of time only to be opened again and have new |
| 49 // data written to them. The file should probably never be deleted because |
| 50 // there would be no guarantee that the data has been reported. |
| 51 // TODO(bcwhite): Enable when read/write mem-mapped files are supported. |
| 52 //FILE_HISTOGRAMS_ACTIVE, |
| 53 }; |
| 54 |
| 55 FileMetricsProvider(const scoped_refptr<base::TaskRunner>& task_runner, |
| 56 PrefService* local_state); |
| 57 ~FileMetricsProvider() override; |
| 58 |
| 59 // Indicates a file to be monitored and how the file is used. Because some |
| 60 // metadata may persist across process restarts, preferences entries are |
| 61 // used based on the |prefs_key| name. Call RegisterPrefs() with the same |
| 62 // name to create the necessary keys in advance. Set |prefs_key| empty |
| 63 // if no persistence is required. |
| 64 void RegisterFile(const base::FilePath& path, |
| 65 FileType type, |
| 66 const base::StringPiece prefs_key); |
| 67 |
| 68 // Registers all necessary preferences for maintaining persistent state |
| 69 // about a monitored file across process restarts. The |prefs_key| is |
| 70 // typically the filename. |
| 71 static void RegisterPrefs(PrefRegistrySimple* prefs, |
| 72 const base::StringPiece prefs_key); |
| 73 |
| 74 private: |
| 75 friend class FileMetricsProviderTest; |
| 76 FRIEND_TEST_ALL_PREFIXES(FileMetricsProviderTest, AccessMetrics); |
| 77 |
| 78 // The different results that can occur accessing a file. |
| 79 enum AccessResult { |
| 80 // File was successfully mapped. |
| 81 ACCESS_RESULT_SUCCESS, |
| 82 |
| 83 // File does not exist. |
| 84 ACCESS_RESULT_DOESNT_EXIST, |
| 85 |
| 86 // File exists but not modified since last read. |
| 87 ACCESS_RESULT_NOT_MODIFIED, |
| 88 |
| 89 // File is not valid: is a directory or zero-size. |
| 90 ACCESS_RESULT_INVALID_FILE, |
| 91 |
| 92 // System could not map file into memory. |
| 93 ACCESS_RESULT_SYSTEM_MAP_FAILURE, |
| 94 |
| 95 // File had invalid contents. |
| 96 ACCESS_RESULT_INVALID_CONTENTS, |
| 97 |
| 98 ACCESS_RESULT_MAX |
| 99 }; |
| 100 |
| 101 // Information about files being monitored; defined and used exclusively |
| 102 // inside the .cc file. |
| 103 struct FileInfo; |
| 104 using FileInfoList = std::list<scoped_ptr<FileInfo>>; |
| 105 |
| 106 // Checks a list of files (on a task-runner allowed to do I/O) to see if |
| 107 // any should be processed during the next histogram collection. |
| 108 static void CheckAndMapNewMetricFilesOnTaskRunner(FileInfoList* files); |
| 109 |
| 110 // Checks an individual file as part of CheckAndMapNewMetricFilesOnTaskRunner. |
| 111 static AccessResult CheckAndMapNewMetrics(FileInfo* file); |
| 112 |
| 113 // Creates a task to check all monitored files for updates. |
| 114 void ScheduleFilesCheck(); |
| 115 |
| 116 // Creates a PersistentMemoryAllocator for a file that has been marked to |
| 117 // have its metrics collected. |
| 118 void CreateAllocatorForFile(FileInfo* file); |
| 119 |
| 120 // Records all histograms from a given file via a snapshot-manager. |
| 121 void RecordHistogramSnapshotsFromFile( |
| 122 base::HistogramSnapshotManager* snapshot_manager, |
| 123 FileInfo* file); |
| 124 |
| 125 // Takes a list of files checked by an external task and determines what |
| 126 // to do with each. |
| 127 void RecordFilesChecked(FileInfoList* checked); |
| 128 |
| 129 // Updates the persistent state information to show a file as being read. |
| 130 void RecordFileAsSeen(FileInfo* file); |
| 131 |
| 132 // metrics::MetricsDataProvider: |
| 133 void OnDidCreateMetricsLog() override; |
| 134 void RecordHistogramSnapshots( |
| 135 base::HistogramSnapshotManager* snapshot_manager) override; |
| 136 |
| 137 // A task-runner capable of performing I/O. |
| 138 scoped_refptr<base::TaskRunner> task_runner_; |
| 139 |
| 140 // A list of files not currently active that need to be checked for changes. |
| 141 FileInfoList files_to_check_; |
| 142 |
| 143 // A list of files that have data to be read and reported. |
| 144 FileInfoList files_to_read_; |
| 145 |
| 146 // The preferences-service used to store persistent state about files. |
| 147 PrefService* pref_service_; |
| 148 |
| 149 base::ThreadChecker thread_checker_; |
| 150 base::WeakPtrFactory<FileMetricsProvider> weak_factory_; |
| 151 |
| 152 DISALLOW_COPY_AND_ASSIGN(FileMetricsProvider); |
| 153 }; |
| 154 |
| 155 } // namespace metrics |
| 156 |
| 157 #endif // COMPONENTS_METRICS_FILE_METRICS_PROVIDER_H_ |
OLD | NEW |