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/synchronization/lock.h" |
| 17 #include "base/threading/thread_checker.h" |
| 18 #include "base/time/time.h" |
| 19 #include "components/metrics/metrics_provider.h" |
| 20 |
| 21 class PrefRegistrySimple; |
| 22 class PrefService; |
| 23 |
| 24 namespace base { |
| 25 class MemoryMappedFile; |
| 26 class PersistentMemoryAllocator; |
| 27 class TaskRunner; |
| 28 } |
| 29 |
| 30 namespace metrics { |
| 31 |
| 32 // FileMetricsProvider gathers and logs histograms written to files on disk. |
| 33 // Any number of files can be registered and will be polled once per upload |
| 34 // cycle (at startup and about every 30 minutes thereafter) for data to send. |
| 35 class FileMetricsProvider |
| 36 : public metrics::MetricsProvider { |
| 37 public: |
| 38 enum FileType { |
| 39 // "Atomic" files are a collection of histograms that are written |
| 40 // completely in a single atomic operation (typically a write followed |
| 41 // by an atomic rename) and the file is never updated again except to |
| 42 // be replaced by a completely new set of histograms. This is the only |
| 43 // option that can be used if the file is not writeable by *this* |
| 44 // process. |
| 45 FILE_HISTOGRAMS_ATOMIC, |
| 46 |
| 47 // "Active" files may be open by one or more other processes and updated |
| 48 // at any time with new samples or new histograms. Such files may also be |
| 49 // inactive for any period of time only to be opened again and have new |
| 50 // data written to them. The file should probably never be deleted because |
| 51 // there would be no guarantee that the data has been reported. |
| 52 // TODO(bcwhite): Enable when read/write mem-mapped files are supported. |
| 53 //FILE_HISTOGRAMS_ACTIVE, |
| 54 }; |
| 55 |
| 56 private: |
| 57 // Information about files being monitored, defined and used exclusively |
| 58 // inside the .cc file. |
| 59 struct FileInformation; |
| 60 |
| 61 public: |
| 62 using FileInformationList = std::list<scoped_ptr<FileInformation>>; |
| 63 |
| 64 FileMetricsProvider(const scoped_refptr<base::TaskRunner>& task_runner, |
| 65 PrefService* local_state); |
| 66 ~FileMetricsProvider() override; |
| 67 |
| 68 // Indicate a file to be monitored and how the file is used. Because some |
| 69 // metadata must persist across process restarts, preferences entries are |
| 70 // used based on the |prefs_key| name. Call RegisterPrefs() with the same |
| 71 // name to create the necessary keys in advance. |
| 72 void RegisterFile(const base::FilePath& path, FileType type, |
| 73 const base::StringPiece& prefs_key); |
| 74 |
| 75 static void RegisterPrefs(PrefRegistrySimple* prefs, |
| 76 const base::StringPiece& key); |
| 77 |
| 78 private: |
| 79 friend class FileMetricsProviderTest; |
| 80 FRIEND_TEST_ALL_PREFIXES(FileMetricsProviderTest, AccessMetrics); |
| 81 |
| 82 static void CheckAndMapNewMetricFilesOnTaskRunner( |
| 83 FileInformationList* files); |
| 84 static bool CheckAndMapNewMetrics(FileInformation* file); |
| 85 |
| 86 void ScheduleFilesCheck(); |
| 87 void RecordFilesChecked(FileInformationList* checked); |
| 88 void RecordFileAsSeen(FileInformation* file); |
| 89 |
| 90 // metrics::MetricsDataProvider: |
| 91 void OnDidCreateMetricsLog() override; |
| 92 void RecordHistogramSnapshots(base::HistogramSnapshotManager* hsm) override; |
| 93 |
| 94 scoped_refptr<base::TaskRunner> task_runner_; |
| 95 FileInformationList files_to_check_; |
| 96 FileInformationList files_to_read_; |
| 97 PrefService* pref_service_; |
| 98 |
| 99 base::ThreadChecker thread_checker_; |
| 100 base::WeakPtrFactory<FileMetricsProvider> weak_factory_; |
| 101 |
| 102 DISALLOW_COPY_AND_ASSIGN(FileMetricsProvider); |
| 103 }; |
| 104 |
| 105 } // namespace metrics |
| 106 |
| 107 #endif // COMPONENTS_METRICS_FILE_METRICS_PROVIDER_H_ |
OLD | NEW |