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. | |
Alexei Svitkine (slow)
2016/02/19 18:16:40
The 30 minutes is a desktop-specific interval. Ple
bcwhite
2016/02/19 19:50:15
Done.
| |
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 | |
Alexei Svitkine (slow)
2016/02/19 18:16:40
Nit: Indicates
bcwhite
2016/02/19 19:50:15
Done.
| |
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, | |
Alexei Svitkine (slow)
2016/02/19 18:16:40
Nit: 1 param per line.
bcwhite
2016/02/19 19:50:15
Done.
| |
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 // The different results that can occur accessing a file. | |
77 enum AccessResult { | |
78 // File was successfully mapped. | |
79 ACCESS_RESULT_SUCCESS, | |
80 | |
81 // File does not exist. | |
82 ACCESS_RESULT_DOESNT_EXIST, | |
83 | |
84 // File exists but not modified since last read. | |
85 ACCESS_RESULT_NOT_MODIFIED, | |
86 | |
87 // File is not valid: is a directory or zero-size. | |
88 ACCESS_RESULT_INVALID_FILE, | |
89 | |
90 // System could not map file into memory. | |
91 ACCESS_RESULT_SYSTEM_MAP_FAILURE, | |
92 | |
93 // File had invalid contents. | |
94 ACCESS_RESULT_INVALID_CONTENTS, | |
95 | |
96 ACCESS_RESULT_MAX | |
97 }; | |
98 | |
99 // Information about files being monitored; defined and used exclusively | |
100 // inside the .cc file. | |
101 struct FileInfo; | |
102 using FileInfoList = std::list<scoped_ptr<FileInfo>>; | |
103 | |
104 // Checks a list of files (on a task-runner allowed to do I/O) to see if | |
105 // any should be processed during the next histogram collection. | |
106 static void CheckAndMapNewMetricFilesOnTaskRunner(FileInfoList* files); | |
107 | |
108 // Checks an individual file as part of CheckAndMapNewMetricFilesOnTaskRunner. | |
109 static AccessResult CheckAndMapNewMetrics(FileInfo* file); | |
110 | |
111 // Creates a task to check all monitored files for updates. | |
112 void ScheduleFilesCheck(); | |
113 | |
114 // Creates a PersistentMemoryAllocator for a file that has been marked to | |
115 // have its metrics collected. | |
116 void CreateAllocatorForFile(FileInfo* file); | |
117 | |
118 // Records all histograms from a given file via a snapshot-manager. | |
119 void RecordHistogramSnapshotsFromFile( | |
120 base::HistogramSnapshotManager* snapshot_manager, | |
121 FileInfo* file); | |
122 | |
123 // Takes a list of files checked by an external task and determines what | |
124 // to do with each. | |
125 void RecordFilesChecked(FileInfoList* checked); | |
126 | |
127 // Updates the persistent state information to show a file as being read. | |
128 void RecordFileAsSeen(FileInfo* file); | |
129 | |
130 // metrics::MetricsDataProvider: | |
131 void OnDidCreateMetricsLog() override; | |
132 void RecordHistogramSnapshots( | |
133 base::HistogramSnapshotManager* snapshot_manager) override; | |
134 | |
135 // A task-runner capable of performing I/O. | |
136 scoped_refptr<base::TaskRunner> task_runner_; | |
137 | |
138 // A list of files not currently active that need to be checked for changes. | |
139 FileInfoList files_to_check_; | |
140 | |
141 // A list of files that have data to be read and reported. | |
142 FileInfoList files_to_read_; | |
143 | |
144 // The preferences-service used to store persistent state about files. | |
145 PrefService* pref_service_; | |
146 | |
147 base::ThreadChecker thread_checker_; | |
148 base::WeakPtrFactory<FileMetricsProvider> weak_factory_; | |
149 | |
150 DISALLOW_COPY_AND_ASSIGN(FileMetricsProvider); | |
151 }; | |
152 | |
153 } // namespace metrics | |
154 | |
155 #endif // COMPONENTS_METRICS_FILE_METRICS_PROVIDER_H_ | |
OLD | NEW |