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 private: | |
48 friend class TestMemoryPressureStatsCollector; | |
49 | |
50 // The reporting delegate. This can be changed for unittesting. | |
51 scoped_ptr<ReportingDelegate> reporting_delegate_; | |
52 | |
53 // The time source. This can be changed for unittesting. | |
54 scoped_ptr<base::TickClock> tick_clock_; | |
55 | |
56 // Buckets of time that have been spent in different pressure levels, but | |
57 // not yet reported. At every call to UpdateStatistics these buckets will be | |
58 // drained as much as possible and reported. | |
rkaplow
2015/09/03 22:15:07
maybe add a bit more about what "drained as much a
chrisha
2015/09/08 15:47:14
Done.
| |
59 base::TimeDelta unreported_cumulative_time_[UMA_MEMORY_PRESSURE_LEVEL_COUNT]; | |
60 | |
61 // The last observed pressure level and the time at which it was observed, and | |
62 // the time when this pressure level started. | |
63 MemoryPressureLevel last_pressure_level_; | |
64 base::TimeTicks last_update_time_; | |
65 | |
66 DISALLOW_COPY_AND_ASSIGN(MemoryPressureStatsCollector); | |
67 }; | |
68 | |
69 // An abstract reporting delegate used as a testing seam. | |
70 class MemoryPressureStatsCollector::ReportingDelegate { | |
71 public: | |
72 ReportingDelegate() {} | |
73 virtual ~ReportingDelegate() {} | |
74 | |
75 // Sends a report that the system spent the given number of |seconds| at the | |
76 // given |pressure_level|, cumulatively (potentially split across multiple | |
77 // pressure changes since the last report was sent). | |
78 virtual void ReportCumulativeTime(MemoryPressureLevel pressure_level, | |
79 int seconds) = 0; | |
80 | |
81 // Reports a memory pressure level change. | |
82 virtual void ReportLevelChange(MemoryPressureLevel old_pressure_level, | |
83 MemoryPressureLevel new_pressure_level) = 0; | |
84 | |
85 private: | |
86 DISALLOW_COPY_AND_ASSIGN(ReportingDelegate); | |
87 }; | |
88 | |
89 // The default reporting delegate, which delivers stats via UMA. | |
90 class MemoryPressureStatsCollector::UmaReportingDelegate | |
91 : public ReportingDelegate { | |
92 public: | |
93 UmaReportingDelegate() {} | |
94 ~UmaReportingDelegate() override {} | |
95 | |
96 // ReportingDelegate: | |
97 void ReportCumulativeTime(MemoryPressureLevel pressure_level, | |
98 int seconds) override; | |
99 void ReportLevelChange(MemoryPressureLevel old_pressure_level, | |
100 MemoryPressureLevel new_pressure_level) override; | |
101 | |
102 private: | |
103 DISALLOW_COPY_AND_ASSIGN(UmaReportingDelegate); | |
104 }; | |
105 | |
106 } // namespace memory_pressure | |
107 | |
108 #endif // COMPONENTS_MEMORY_PRESSURE_MEMORY_PRESSURE_STATS_COLLECTOR_H_ | |
OLD | NEW |