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/synchronization/lock.h" | |
16 #include "base/time/time.h" | |
17 #include "components/metrics/metrics_provider.h" | |
18 | |
19 class PrefRegistrySimple; | |
20 class PrefService; | |
21 | |
22 namespace base { | |
23 class MemoryMappedFile; | |
24 class PersistentMemoryAllocator; | |
25 class TaskRunner; | |
26 } | |
27 | |
28 namespace metrics { | |
29 | |
30 // FileMetricsProvider gathers and logs histograms written to files on disk. | |
31 // Any number of files can be registered and will be polled once per upload | |
32 // cycle (at startup and about every 30 minutes thereafter) for data to send. | |
33 class FileMetricsProvider | |
34 : 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. Because the file will be actively watched by *this* | |
49 // process, the underlying file cannot be deleted on operating systems | |
50 // that do not support delete-while-open (e.g. Windows). | |
51 // TODO(bcwhite): Enable when read/write mem-mapped files are supported. | |
52 //FILE_HISTOGRAMS_ACTIVE, | |
53 }; | |
54 | |
55 private: | |
56 // This is fully defined in the header file (rather than just a forward | |
57 // declaration) so it can be known to tests. | |
grt (UTC plus 2)
2016/02/15 15:42:39
this is no longer true. is now a good time to move
bcwhite
2016/02/15 19:22:11
Done.
| |
58 struct FileInformation { | |
59 FileInformation(); | |
60 ~FileInformation(); | |
61 | |
62 base::FilePath path; | |
63 FileType type; | |
64 std::string prefs_key; | |
65 base::Time last_seen; | |
66 scoped_ptr<base::MemoryMappedFile> mapped; | |
grt (UTC plus 2)
2016/02/15 15:42:39
i think it would be useful to document the various
bcwhite
2016/02/15 19:22:11
Done.
| |
67 scoped_ptr<base::MemoryMappedFile> file_to_release; | |
68 scoped_ptr<base::PersistentMemoryAllocator> allocator; | |
69 scoped_ptr<base::PersistentMemoryAllocator> allocator_to_release; | |
70 | |
71 private: | |
72 DISALLOW_COPY_AND_ASSIGN(FileInformation); | |
73 }; | |
74 | |
75 public: | |
76 using FileInformationList = std::list<scoped_ptr<FileInformation>>; | |
77 | |
78 FileMetricsProvider(const scoped_refptr<base::TaskRunner>& task_runner, | |
79 PrefService* local_state); | |
80 ~FileMetricsProvider() override; | |
81 | |
82 // Indicate a file to be monitored and how the file is used. Because some | |
83 // metadata must persist across process restarts, preferences entries are | |
84 // used based on the |prefs_key| name. Call RegisterPrefs() with the same | |
85 // name to create the necessary keys in advance. | |
86 void RegisterFile(const base::FilePath& path, FileType type, | |
87 const base::StringPiece& prefs_key); | |
88 | |
89 static void RegisterPrefs(PrefRegistrySimple* prefs, | |
90 const base::StringPiece& key); | |
91 | |
92 private: | |
93 friend class FileMetricsProviderTest; | |
94 FRIEND_TEST_ALL_PREFIXES(FileMetricsProviderTest, AccessMetrics); | |
95 | |
96 static void CheckAndMapNewMetricFilesOnTaskRunner( | |
97 FileInformationList* files); | |
98 static bool CheckAndMapNewMetrics(FileInformation* file); | |
99 | |
100 void ScheduleFilesCheck(); | |
101 void RecordFilesChecked(FileInformationList* checked); | |
102 void RecordFileAsSeen(FileInformation* file); | |
103 | |
104 // metrics::MetricsDataProvider: | |
105 void OnDidCreateMetricsLog() override; | |
106 void RecordHistogramSnapshots(base::HistogramSnapshotManager* hsm) override; | |
107 | |
108 scoped_refptr<base::TaskRunner> task_runner_; | |
109 FileInformationList files_to_check_; | |
110 FileInformationList files_to_read_; | |
111 PrefService* pref_service_; | |
112 | |
113 DISALLOW_COPY_AND_ASSIGN(FileMetricsProvider); | |
114 }; | |
115 | |
116 } // namespace metrics | |
117 | |
118 #endif // COMPONENTS_METRICS_FILE_METRICS_PROVIDER_H_ | |
OLD | NEW |