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

Unified Diff: components/memory_pressure/memory_pressure_stats_collector.h

Issue 1310043004: Create MemoryPressureStatsCollector. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 3 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/memory_pressure/memory_pressure_stats_collector.h
diff --git a/components/memory_pressure/memory_pressure_stats_collector.h b/components/memory_pressure/memory_pressure_stats_collector.h
new file mode 100644
index 0000000000000000000000000000000000000000..8be4cbf309dd33d92075b6e1296be190b8937c02
--- /dev/null
+++ b/components/memory_pressure/memory_pressure_stats_collector.h
@@ -0,0 +1,121 @@
+// Copyright 2015 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_MEMORY_PRESSURE_MEMORY_PRESSURE_STATS_COLLECTOR_H_
+#define COMPONENTS_MEMORY_PRESSURE_MEMORY_PRESSURE_STATS_COLLECTOR_H_
+
+#include "base/time/tick_clock.h"
+#include "components/memory_pressure/memory_pressure_listener.h"
+
+namespace memory_pressure {
+
+// Enumeration of UMA memory pressure levels. This needs to be kept in sync with
+// histograms.xml and the memory pressure levels defined in
+// MemoryPressureListener.
+enum MemoryPressureLevelUMA {
+ UMA_MEMORY_PRESSURE_LEVEL_NONE = 0,
+ UMA_MEMORY_PRESSURE_LEVEL_MODERATE = 1,
+ UMA_MEMORY_PRESSURE_LEVEL_CRITICAL = 2,
+ // This must be the last value in the enum.
+ UMA_MEMORY_PRESSURE_LEVEL_COUNT,
+};
+
+// Class that is responsible for collecting and eventually reporting memory
+// pressure statistics.
+//
+// On platforms with a polling memory pressure implementation the
+// UpdateStatistics function will be invoked every time the pressure is polled.
+// On non-polling platforms (Mac, Android) it will be invoked on a periodic
+// timer, and at the moment of pressure level changes.
+class MemoryPressureStatsCollector {
+ public:
+ using MemoryPressureLevel = MemoryPressureListener::MemoryPressureLevel;
+
+ // Delegate class which will do the actual statistics reporting.
+ class ReportingDelegate;
+
+ // The default reporting delegate.
+ class UmaReportingDelegate;
+
+ MemoryPressureStatsCollector();
+
+ // This is to be called periodically to ensure that up to date statistics
+ // have been reported.
+ void UpdateStatistics(MemoryPressureLevel current_pressure_level);
+
+ // Testing seam for configuring the reporting delegate in use.
+ void set_reporting_delegate(
+ scoped_ptr<ReportingDelegate> reporting_delegate) {
+ reporting_delegate_ = reporting_delegate.Pass();
+ }
+
+ // Testing seam for configuring the tick clock in use.
+ void set_tick_clock(scoped_ptr<base::TickClock> tick_clock) {
+ tick_clock_ = tick_clock.Pass();
+ }
+
+ private:
+ friend class TestMemoryPressureStatsCollector;
+
+ // The reporting delegate. This can be changed for unittesting.
+ scoped_ptr<ReportingDelegate> reporting_delegate_;
+
+ // The time source. This can be changed for unittesting.
+ scoped_ptr<base::TickClock> tick_clock_;
+
+ // Buckets of time that have been spent in different pressure levels, but
+ // not yet reported. At every call to UpdateStatistics these buckets will be
+ // drained as much as possible and reported.
+ base::TimeDelta unreported_cumulative_time_[UMA_MEMORY_PRESSURE_LEVEL_COUNT];
+
+ // The last observed pressure level and the time at which it was observed, and
+ // the time when this pressure level started.
+ MemoryPressureLevel last_pressure_level_;
+ base::TimeTicks last_update_time_;
+
+ DISALLOW_COPY_AND_ASSIGN(MemoryPressureStatsCollector);
+};
+
+// An abstract reporting delegate used as a testing seam.
+class MemoryPressureStatsCollector::ReportingDelegate {
+ public:
+ ReportingDelegate() {}
+ virtual ~ReportingDelegate() {}
+
+ // Sends a report that the system spent the given amount of |time| at the
+ // given |pressure_level|, cumulatively (potentially split across multiple
+ // pressure changes since the last report was sent). If the report can only
+ // record a fraction of the time it is to subtract the reported portion and
+ // leave behind the unreported portion to be considered in a later report.
+ virtual void ReportCumulativeTime(MemoryPressureLevel pressure_level,
+ base::TimeDelta* time) = 0;
+
+ // Reports a memory pressure level change.
+ virtual void ReportLevelChange(MemoryPressureLevel old_pressure_level,
+ MemoryPressureLevel new_pressure_level) = 0;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(ReportingDelegate);
+};
+
+// The default reporting delegate, which delivers stats via UMA.
+class MemoryPressureStatsCollector::UmaReportingDelegate
+ : public ReportingDelegate {
+ public:
+ UmaReportingDelegate() {}
+ ~UmaReportingDelegate() override {}
+
+ // ReportingDelegate:
+ void ReportCumulativeTime(MemoryPressureLevel pressure_level,
+ base::TimeDelta* time) override;
+ void ReportLevelChange(MemoryPressureLevel old_pressure_level,
+ MemoryPressureLevel new_pressure_level) override;
+
+ private:
+ DISALLOW_COPY_AND_ASSIGN(UmaReportingDelegate);
+};
+
+} // namespace memory_pressure
+
+#endif // COMPONENTS_MEMORY_PRESSURE_MEMORY_PRESSURE_STATS_COLLECTOR_H_

Powered by Google App Engine
This is Rietveld 408576698