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 FileMetricsProvider(const scoped_refptr<base::TaskRunner>& task_runner, | |
| 55 PrefService* local_state); | |
| 56 ~FileMetricsProvider() override; | |
| 57 | |
| 58 // Indicate a file to be monitored and how the file is used. Because some | |
| 59 // metadata may persist across process restarts, preferences entries are | |
| 60 // used based on the |prefs_key| name. Call RegisterPrefs() with the same | |
| 61 // name to create the necessary keys in advance. Set |prefs_key| empty | |
| 62 // if no persistence is required. | |
| 63 void RegisterFile(const base::FilePath& path, FileType type, | |
| 64 const base::StringPiece prefs_key); | |
| 65 | |
| 66 // Registers all necessary preferences for maintaining persistent state | |
| 67 // about a monitored file across process restarts. The |prefs_key| is | |
| 68 // typically the filename. | |
| 69 static void RegisterPrefs(PrefRegistrySimple* prefs, | |
| 70 const base::StringPiece prefs_key); | |
| 71 | |
| 72 private: | |
| 73 friend class FileMetricsProviderTest; | |
| 74 FRIEND_TEST_ALL_PREFIXES(FileMetricsProviderTest, AccessMetrics); | |
| 75 | |
| 76 // Information about files being monitored; defined and used exclusively | |
| 77 // inside the .cc file. | |
| 78 struct FileInfo; | |
| 79 using FileInfoList = std::list<scoped_ptr<FileInfo>>; | |
| 80 | |
| 81 // Checks a list of files (on a task-runner allowed to do I/O) to see if | |
| 82 // any should be processed during the next histogram collection. | |
| 83 static void CheckAndMapNewMetricFilesOnTaskRunner(FileInfoList* files); | |
| 84 | |
| 85 // Checks an individual file as part of CheckAndMapNewMetricFilesOnTaskRunner. | |
| 86 static int CheckAndMapNewMetrics(FileInfo* file); | |
|
Alexei Svitkine (slow)
2016/02/18 20:06:16
Document the return value in the comment.
Actuall
bcwhite
2016/02/18 20:13:31
I tried doing only a forward-declaration of the en
grt (UTC plus 2)
2016/02/18 20:19:52
One nice perk to the new "enum class" over old "en
bcwhite
2016/02/18 22:23:05
Done.
I tried Greg's solution using "enum class"
grt (UTC plus 2)
2016/02/19 14:42:39
Yeah, you need creative casting to make it work. T
| |
| 87 | |
| 88 // Creates a task to check all monitored files for updates. | |
| 89 void ScheduleFilesCheck(); | |
| 90 | |
| 91 // Creates a PersistentMemoryAllocator for a file that has been marked to | |
| 92 // have its metrics collected. | |
| 93 void CreateAllocatorForFile(FileInfo* file); | |
| 94 | |
| 95 // Records all histograms from a given file via a snapshot-manager. | |
| 96 void RecordHistogramSnapshotsFromFile( | |
| 97 base::HistogramSnapshotManager* snapshot_manager, FileInfo* file); | |
|
Alexei Svitkine (slow)
2016/02/18 20:06:16
Nit: 1 param per line if the first one wraps.
bcwhite
2016/02/18 22:23:05
Done.
| |
| 98 | |
| 99 // Takes a list of files checked by an external task and determines what | |
| 100 // to do with each. | |
| 101 void RecordFilesChecked(FileInfoList* checked); | |
| 102 | |
| 103 // Updates the persistent state information to show a file as being read. | |
| 104 void RecordFileAsSeen(FileInfo* file); | |
| 105 | |
| 106 // metrics::MetricsDataProvider: | |
| 107 void OnDidCreateMetricsLog() override; | |
| 108 void RecordHistogramSnapshots( | |
| 109 base::HistogramSnapshotManager* snapshot_manager) override; | |
| 110 | |
| 111 // A task-runner capable of performing I/O. | |
| 112 scoped_refptr<base::TaskRunner> task_runner_; | |
| 113 | |
| 114 // A list of files not currently active that need to be checked for changes. | |
| 115 FileInfoList files_to_check_; | |
| 116 | |
| 117 // A list of files that have data to be read and reported. | |
| 118 FileInfoList files_to_read_; | |
| 119 | |
| 120 // The preferences-service used to store persistent state about files. | |
| 121 PrefService* pref_service_; | |
| 122 | |
| 123 base::ThreadChecker thread_checker_; | |
| 124 base::WeakPtrFactory<FileMetricsProvider> weak_factory_; | |
| 125 | |
| 126 DISALLOW_COPY_AND_ASSIGN(FileMetricsProvider); | |
| 127 }; | |
| 128 | |
| 129 } // namespace metrics | |
| 130 | |
| 131 #endif // COMPONENTS_METRICS_FILE_METRICS_PROVIDER_H_ | |
| OLD | NEW |