Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(590)

Side by Side Diff: components/metrics/file_metrics_provider.h

Issue 1537743006: Persist setup metrics and have Chrome report them during UMA upload. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@shared-histograms
Patch Set: fixed test Created 4 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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.
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;
67 scoped_ptr<base::PersistentMemoryAllocator> allocator;
68
69 private:
70 DISALLOW_COPY_AND_ASSIGN(FileInformation);
71 };
72
73 public:
74 using FileInformationList = std::list<scoped_ptr<FileInformation>>;
75
76 FileMetricsProvider(const scoped_refptr<base::TaskRunner>& task_runner,
77 PrefService* local_state);
78 ~FileMetricsProvider() override;
79
80 // Indicate a file to be monitored and how the file is used. Because some
81 // metadata must persist across process restarts, preferences entries are
82 // used based on the |prefs_key| name. Call RegisterPrefs() with the same
83 // name to create the necessary keys in advance.
84 void RegisterFile(const base::FilePath& path, FileType type,
85 const base::StringPiece& prefs_key);
86
87 static void RegisterPrefs(PrefRegistrySimple* prefs,
88 const base::StringPiece& key);
89
90 private:
91 friend class FileMetricsProviderTest;
92 FRIEND_TEST_ALL_PREFIXES(FileMetricsProviderTest, AccessMetrics);
93
94 static void CheckAndMapNewMetricFilesOnTaskRunner(
95 FileInformationList* files);
96 static bool CheckAndMapNewMetrics(FileInformation* file);
97
98 void ScheduleFilesCheck();
99 void RecordFilesChecked(FileInformationList* checked);
100 void RecordFileAsSeen(FileInformation* file);
101
102 // metrics::MetricsDataProvider:
103 void OnDidCreateMetricsLog() override;
104 void RecordHistogramSnapshots(base::HistogramSnapshotManager* hsm) override;
105
106 scoped_refptr<base::TaskRunner> task_runner_;
107 FileInformationList files_to_check_;
108 FileInformationList files_to_read_;
109 PrefService* pref_service_;
110
111 DISALLOW_COPY_AND_ASSIGN(FileMetricsProvider);
112 };
113
114 } // namespace metrics
115
116 #endif // COMPONENTS_METRICS_FILE_METRICS_PROVIDER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698