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

Unified 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: test needs to clear out statistics-recorder before releasing histogram memory 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 side-by-side diff with in-line comments
Download patch
Index: components/metrics/file_metrics_provider.h
diff --git a/components/metrics/file_metrics_provider.h b/components/metrics/file_metrics_provider.h
new file mode 100644
index 0000000000000000000000000000000000000000..2962b0d688b2d40d503830301aded4e2a18ac83d
--- /dev/null
+++ b/components/metrics/file_metrics_provider.h
@@ -0,0 +1,118 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef COMPONENTS_METRICS_FILE_METRICS_PROVIDER_H_
+#define COMPONENTS_METRICS_FILE_METRICS_PROVIDER_H_
+
+#include <list>
+#include <string>
+
+#include "base/callback.h"
+#include "base/files/file_path.h"
+#include "base/gtest_prod_util.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/synchronization/lock.h"
+#include "base/time/time.h"
+#include "components/metrics/metrics_provider.h"
+
+class PrefRegistrySimple;
+class PrefService;
+
+namespace base {
+class MemoryMappedFile;
+class PersistentMemoryAllocator;
+class TaskRunner;
+}
+
+namespace metrics {
+
+// FileMetricsProvider gathers and logs histograms written to files on disk.
+// Any number of files can be registered and will be polled once per upload
+// cycle (at startup and about every 30 minutes thereafter) for data to send.
+class FileMetricsProvider
+ : public metrics::MetricsProvider {
+ public:
+ enum FileType {
+ // "Atomic" files are a collection of histograms that are written
+ // completely in a single atomic operation (typically a write followed
+ // by an atomic rename) and the file is never updated again except to
+ // be replaced by a completely new set of histograms. This is the only
+ // option that can be used if the file is not writeable by *this*
+ // process.
+ FILE_HISTOGRAMS_ATOMIC,
+
+ // "Active" files may be open by one or more other processes and updated
+ // at any time with new samples or new histograms. Such files may also be
+ // inactive for any period of time only to be opened again and have new
+ // data written to them. Because the file will be actively watched by *this*
+ // process, the underlying file cannot be deleted on operating systems
+ // that do not support delete-while-open (e.g. Windows).
+ // TODO(bcwhite): Enable when read/write mem-mapped files are supported.
+ //FILE_HISTOGRAMS_ACTIVE,
+ };
+
+ private:
+ // This is fully defined in the header file (rather than just a forward
+ // 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.
+ struct FileInformation {
+ FileInformation();
+ ~FileInformation();
+
+ base::FilePath path;
+ FileType type;
+ std::string prefs_key;
+ base::Time last_seen;
+ 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.
+ scoped_ptr<base::MemoryMappedFile> file_to_release;
+ scoped_ptr<base::PersistentMemoryAllocator> allocator;
+ scoped_ptr<base::PersistentMemoryAllocator> allocator_to_release;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(FileInformation);
+ };
+
+ public:
+ using FileInformationList = std::list<scoped_ptr<FileInformation>>;
+
+ FileMetricsProvider(const scoped_refptr<base::TaskRunner>& task_runner,
+ PrefService* local_state);
+ ~FileMetricsProvider() override;
+
+ // Indicate a file to be monitored and how the file is used. Because some
+ // metadata must persist across process restarts, preferences entries are
+ // used based on the |prefs_key| name. Call RegisterPrefs() with the same
+ // name to create the necessary keys in advance.
+ void RegisterFile(const base::FilePath& path, FileType type,
+ const base::StringPiece& prefs_key);
+
+ static void RegisterPrefs(PrefRegistrySimple* prefs,
+ const base::StringPiece& key);
+
+ private:
+ friend class FileMetricsProviderTest;
+ FRIEND_TEST_ALL_PREFIXES(FileMetricsProviderTest, AccessMetrics);
+
+ static void CheckAndMapNewMetricFilesOnTaskRunner(
+ FileInformationList* files);
+ static bool CheckAndMapNewMetrics(FileInformation* file);
+
+ void ScheduleFilesCheck();
+ void RecordFilesChecked(FileInformationList* checked);
+ void RecordFileAsSeen(FileInformation* file);
+
+ // metrics::MetricsDataProvider:
+ void OnDidCreateMetricsLog() override;
+ void RecordHistogramSnapshots(base::HistogramSnapshotManager* hsm) override;
+
+ scoped_refptr<base::TaskRunner> task_runner_;
+ FileInformationList files_to_check_;
+ FileInformationList files_to_read_;
+ PrefService* pref_service_;
+
+ DISALLOW_COPY_AND_ASSIGN(FileMetricsProvider);
+};
+
+} // namespace metrics
+
+#endif // COMPONENTS_METRICS_FILE_METRICS_PROVIDER_H_

Powered by Google App Engine
This is Rietveld 408576698