OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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_MEMORY_PRESSURE_MEMORY_PRESSURE_STATS_COLLECTOR_H_ |
| 6 #define COMPONENTS_MEMORY_PRESSURE_MEMORY_PRESSURE_STATS_COLLECTOR_H_ |
| 7 |
| 8 #include "base/time/tick_clock.h" |
| 9 #include "components/memory_pressure/memory_pressure_listener.h" |
| 10 |
| 11 namespace memory_pressure { |
| 12 |
| 13 // Enumeration of UMA memory pressure levels. This needs to be kept in sync with |
| 14 // histograms.xml and the memory pressure levels defined in |
| 15 // MemoryPressureListener. |
| 16 enum MemoryPressureLevelUMA { |
| 17 UMA_MEMORY_PRESSURE_LEVEL_NONE = 0, |
| 18 UMA_MEMORY_PRESSURE_LEVEL_MODERATE = 1, |
| 19 UMA_MEMORY_PRESSURE_LEVEL_CRITICAL = 2, |
| 20 // This must be the last value in the enum. |
| 21 UMA_MEMORY_PRESSURE_LEVEL_COUNT, |
| 22 }; |
| 23 |
| 24 // Class that is responsible for collecting and eventually reporting memory |
| 25 // pressure statistics. |
| 26 // |
| 27 // On platforms with a polling memory pressure implementation the |
| 28 // UpdateStatistics function will be invoked every time the pressure is polled. |
| 29 // On non-polling platforms (Mac, Android) it will be invoked on a periodic |
| 30 // timer, and at the moment of pressure level changes. |
| 31 class MemoryPressureStatsCollector { |
| 32 public: |
| 33 using MemoryPressureLevel = MemoryPressureListener::MemoryPressureLevel; |
| 34 |
| 35 // Delegate class which will do the actual statistics reporting. |
| 36 class ReportingDelegate; |
| 37 |
| 38 // The default reporting delegate. |
| 39 class UmaReportingDelegate; |
| 40 |
| 41 MemoryPressureStatsCollector(); |
| 42 |
| 43 // This is to be called periodically to ensure that up to date statistics |
| 44 // have been reported. |
| 45 void UpdateStatistics(MemoryPressureLevel current_pressure_level); |
| 46 |
| 47 // Testing seam for configuring the reporting delegate in use. |
| 48 void set_reporting_delegate( |
| 49 scoped_ptr<ReportingDelegate> reporting_delegate) { |
| 50 reporting_delegate_ = reporting_delegate.Pass(); |
| 51 } |
| 52 |
| 53 // Testing seam for configuring the tick clock in use. |
| 54 void set_tick_clock(scoped_ptr<base::TickClock> tick_clock) { |
| 55 tick_clock_ = tick_clock.Pass(); |
| 56 } |
| 57 |
| 58 private: |
| 59 friend class TestMemoryPressureStatsCollector; |
| 60 |
| 61 // The reporting delegate. This can be changed for unittesting. |
| 62 scoped_ptr<ReportingDelegate> reporting_delegate_; |
| 63 |
| 64 // The time source. This can be changed for unittesting. |
| 65 scoped_ptr<base::TickClock> tick_clock_; |
| 66 |
| 67 // Buckets of time that have been spent in different pressure levels, but |
| 68 // not yet reported. At every call to UpdateStatistics these buckets will be |
| 69 // drained as much as possible and reported. |
| 70 base::TimeDelta unreported_cumulative_time_[UMA_MEMORY_PRESSURE_LEVEL_COUNT]; |
| 71 |
| 72 // The last observed pressure level and the time at which it was observed, and |
| 73 // the time when this pressure level started. |
| 74 MemoryPressureLevel last_pressure_level_; |
| 75 base::TimeTicks last_update_time_; |
| 76 |
| 77 DISALLOW_COPY_AND_ASSIGN(MemoryPressureStatsCollector); |
| 78 }; |
| 79 |
| 80 // An abstract reporting delegate used as a testing seam. |
| 81 class MemoryPressureStatsCollector::ReportingDelegate { |
| 82 public: |
| 83 ReportingDelegate() {} |
| 84 virtual ~ReportingDelegate() {} |
| 85 |
| 86 // Sends a report that the system spent the given amount of |time| at the |
| 87 // given |pressure_level|, cumulatively (potentially split across multiple |
| 88 // pressure changes since the last report was sent). If the report can only |
| 89 // record a fraction of the time it is to subtract the reported portion and |
| 90 // leave behind the unreported portion to be considered in a later report. |
| 91 virtual void ReportCumulativeTime(MemoryPressureLevel pressure_level, |
| 92 base::TimeDelta* time) = 0; |
| 93 |
| 94 // Reports a memory pressure level change. |
| 95 virtual void ReportLevelChange(MemoryPressureLevel old_pressure_level, |
| 96 MemoryPressureLevel new_pressure_level) = 0; |
| 97 |
| 98 private: |
| 99 DISALLOW_COPY_AND_ASSIGN(ReportingDelegate); |
| 100 }; |
| 101 |
| 102 // The default reporting delegate, which delivers stats via UMA. |
| 103 class MemoryPressureStatsCollector::UmaReportingDelegate |
| 104 : public ReportingDelegate { |
| 105 public: |
| 106 UmaReportingDelegate() {} |
| 107 ~UmaReportingDelegate() override {} |
| 108 |
| 109 // ReportingDelegate: |
| 110 void ReportCumulativeTime(MemoryPressureLevel pressure_level, |
| 111 base::TimeDelta* time) override; |
| 112 void ReportLevelChange(MemoryPressureLevel old_pressure_level, |
| 113 MemoryPressureLevel new_pressure_level) override; |
| 114 |
| 115 private: |
| 116 DISALLOW_COPY_AND_ASSIGN(UmaReportingDelegate); |
| 117 }; |
| 118 |
| 119 } // namespace memory_pressure |
| 120 |
| 121 #endif // COMPONENTS_MEMORY_PRESSURE_MEMORY_PRESSURE_STATS_COLLECTOR_H_ |
OLD | NEW |