Chromium Code Reviews| 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 about every 30 minutes thereafter) for data to send. | |
| 34 class FileMetricsProvider : public metrics::MetricsProvider { | |
| 35 public: | |
| 36 enum FileType { | |
| 37 // "Atomic" files are a collection of histograms that are written | |
| 38 // completely in a single atomic operation (typically a write followed | |
| 39 // by an atomic rename) and the file is never updated again except to | |
| 40 // be replaced by a completely new set of histograms. This is the only | |
| 41 // option that can be used if the file is not writeable by *this* | |
| 42 // process. | |
| 43 FILE_HISTOGRAMS_ATOMIC, | |
| 44 | |
| 45 // "Active" files may be open by one or more other processes and updated | |
| 46 // at any time with new samples or new histograms. Such files may also be | |
| 47 // inactive for any period of time only to be opened again and have new | |
| 48 // data written to them. The file should probably never be deleted because | |
| 49 // there would be no guarantee that the data has been reported. | |
| 50 // TODO(bcwhite): Enable when read/write mem-mapped files are supported. | |
| 51 //FILE_HISTOGRAMS_ACTIVE, | |
| 52 }; | |
| 53 | |
| 54 private: | |
|
grt (UTC plus 2)
2016/02/18 16:44:07
get rid of this private block, moving FileInfo dow
bcwhite
2016/02/18 19:35:02
Done.
| |
| 55 // Information about files being monitored, defined and used exclusively | |
| 56 // inside the .cc file. | |
| 57 struct FileInfo; | |
| 58 | |
| 59 public: | |
| 60 FileMetricsProvider(const scoped_refptr<base::TaskRunner>& task_runner, | |
| 61 PrefService* local_state); | |
| 62 ~FileMetricsProvider() override; | |
| 63 | |
| 64 // Indicate a file to be monitored and how the file is used. Because some | |
| 65 // metadata may persist across process restarts, preferences entries are | |
| 66 // used based on the |prefs_key| name. Call RegisterPrefs() with the same | |
| 67 // name to create the necessary keys in advance. Set |prefs_key| empty | |
| 68 // if no persistence is required. | |
| 69 void RegisterFile(const base::FilePath& path, FileType type, | |
| 70 const base::StringPiece prefs_key); | |
| 71 | |
| 72 // Registers all necessary preferences for maintaining persistent state | |
| 73 // about a monitored file across process restarts. The |prefs_key| is | |
| 74 // typically the filename. | |
| 75 static void RegisterPrefs(PrefRegistrySimple* prefs, | |
| 76 const base::StringPiece prefs_key); | |
| 77 | |
| 78 private: | |
| 79 friend class FileMetricsProviderTest; | |
| 80 FRIEND_TEST_ALL_PREFIXES(FileMetricsProviderTest, AccessMetrics); | |
| 81 | |
| 82 using FileInfoList = std::list<scoped_ptr<FileInfo>>; | |
| 83 | |
| 84 // Checks a list of files (on a task-runner allowed to do I/O) to see if | |
| 85 // any should be processed during the next histogram collection. | |
| 86 static void CheckAndMapNewMetricFilesOnTaskRunner(FileInfoList* files); | |
| 87 | |
| 88 // Checks an individual file as part of CheckAndMapNewMetricFilesOnTaskRunner. | |
| 89 static void CheckAndMapNewMetrics(FileInfo* file); | |
| 90 | |
| 91 // Creates a task to check all monitored files for updates. | |
| 92 void ScheduleFilesCheck(); | |
| 93 | |
| 94 // Creates a PersistentMemoryAllocator for a file that has been marked to | |
| 95 // have its metrics collected. | |
| 96 void CreateAllocatorForFile(FileInfo* file); | |
| 97 | |
| 98 // Records all histograms from a given file via a snapshot-manager. | |
| 99 void FileMetricsProvider::RecordHistogramSnapshotsFromFile( | |
| 100 base::HistogramSnapshotManager* snapshot_manager, FileInfo* file); | |
| 101 | |
| 102 // Takes a list of files checked by an external task and determines what | |
| 103 // to do with each. | |
| 104 void RecordFilesChecked(FileInfoList* checked); | |
| 105 | |
| 106 // Updates the persistent state information to show a file as being read. | |
| 107 void RecordFileAsSeen(FileInfo* file); | |
| 108 | |
| 109 // metrics::MetricsDataProvider: | |
| 110 void OnDidCreateMetricsLog() override; | |
| 111 void RecordHistogramSnapshots( | |
| 112 base::HistogramSnapshotManager* snapshot_manager) override; | |
| 113 | |
| 114 // A task-runner capable of performing I/O. | |
| 115 scoped_refptr<base::TaskRunner> task_runner_; | |
| 116 | |
| 117 // A list of files not currently active that need to be checked for changes. | |
| 118 FileInfoList files_to_check_; | |
| 119 | |
| 120 // A list of files that have data to be read and reported. | |
| 121 FileInfoList files_to_read_; | |
| 122 | |
| 123 // The preferences-service used to store persistent state about files. | |
| 124 PrefService* pref_service_; | |
| 125 | |
| 126 base::ThreadChecker thread_checker_; | |
| 127 base::WeakPtrFactory<FileMetricsProvider> weak_factory_; | |
| 128 | |
| 129 DISALLOW_COPY_AND_ASSIGN(FileMetricsProvider); | |
| 130 }; | |
| 131 | |
| 132 } // namespace metrics | |
| 133 | |
| 134 #endif // COMPONENTS_METRICS_FILE_METRICS_PROVIDER_H_ | |
| OLD | NEW |