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. Included in the header so that | |
16 // UMA_MEMORY_PRESSURE_LEVEL_COUNT is available. | |
17 enum MemoryPressureLevelUMA { | |
18 UMA_MEMORY_PRESSURE_LEVEL_NONE = 0, | |
19 UMA_MEMORY_PRESSURE_LEVEL_MODERATE = 1, | |
20 UMA_MEMORY_PRESSURE_LEVEL_CRITICAL = 2, | |
21 // This must be the last value in the enum. | |
22 UMA_MEMORY_PRESSURE_LEVEL_COUNT, | |
23 }; | |
24 | |
25 // Class that is responsible for collecting and eventually reporting memory | |
26 // pressure statistics. | |
27 // | |
28 // On platforms with a polling memory pressure implementation the | |
29 // UpdateStatistics function will be invoked every time the pressure is polled. | |
30 // On non-polling platforms (Mac, Android) it will be invoked on a periodic | |
31 // timer, and at the moment of pressure level changes. | |
32 class MemoryPressureStatsCollector { | |
33 public: | |
34 using MemoryPressureLevel = MemoryPressureListener::MemoryPressureLevel; | |
35 | |
36 // An abstract reporting delegate used as a testing seam. | |
37 class ReportingDelegate; | |
38 // The default reporting delegate, which delivers stats via UMA. | |
39 class UmaReportingDelegate; | |
40 | |
41 // The provided |reporting_delegate| and |tick_clock| must outlive this class. | |
42 MemoryPressureStatsCollector(ReportingDelegate* reporting_delegate, | |
43 base::TickClock* tick_clock); | |
44 | |
45 // This is to be called periodically to ensure that up to date statistics | |
46 // have been reported. | |
47 void UpdateStatistics(MemoryPressureLevel current_pressure_level); | |
48 | |
49 private: | |
50 friend class TestMemoryPressureStatsCollector; | |
51 | |
52 // The reporting delegate in use. This indirection creates a test seam. | |
53 ReportingDelegate* reporting_delegate_; | |
54 | |
55 // The tick clock in use. This class is intended to be owned by a class with | |
56 // a tick clock, and ownership remains there. Also intended as a test seam. | |
57 base::TickClock* tick_clock_; | |
58 | |
59 // Buckets of time that have been spent in different pressure levels, but | |
60 // not yet reported. At every call to UpdateStatistics these buckets will be | |
61 // drained as much as possible and reported. | |
62 base::TimeDelta unreported_cumulative_time_[UMA_MEMORY_PRESSURE_LEVEL_COUNT]; | |
63 | |
64 // The last observed pressure level and the time at which it was observed, and | |
65 // the time when this pressure level started. | |
66 MemoryPressureLevel last_pressure_level_; | |
67 base::TimeTicks last_update_time_; | |
68 | |
69 DISALLOW_COPY_AND_ASSIGN(MemoryPressureStatsCollector); | |
70 }; | |
71 | |
72 // An abstract reporting delegate used as a testing seam. | |
73 class MemoryPressureStatsCollector::ReportingDelegate { | |
grt (UTC plus 2)
2015/09/04 19:46:19
instead of introducing a layer between MPSC and UM
chrisha
2015/09/04 20:31:24
I'm not averse to this, I just didn't even know it
| |
74 public: | |
75 ReportingDelegate() {} | |
grt (UTC plus 2)
2015/09/04 19:46:19
nit: make the ctor and dtors protected via:
prote
chrisha
2015/09/04 20:31:24
Grrr... I'm still not used to "= default" (despite
| |
76 virtual ~ReportingDelegate() {} | |
77 | |
78 // Sends a report that the system spent the given number of |seconds| at the | |
79 // given |pressure_level|, cumulatively (potentially split across multiple | |
80 // pressure changes since the last report was sent). | |
81 virtual void ReportCumulativeTime(MemoryPressureLevel pressure_level, | |
82 int seconds) = 0; | |
83 | |
84 // Reports a memory pressure level change. | |
85 virtual void ReportLevelChange(MemoryPressureLevel old_pressure_level, | |
86 MemoryPressureLevel new_pressure_level) = 0; | |
87 | |
88 private: | |
89 DISALLOW_COPY_AND_ASSIGN(ReportingDelegate); | |
90 }; | |
91 | |
92 // The default reporting delegate, which delivers stats via UMA. | |
93 class MemoryPressureStatsCollector::UmaReportingDelegate | |
94 : public ReportingDelegate { | |
95 public: | |
96 UmaReportingDelegate() {} | |
grt (UTC plus 2)
2015/09/04 19:46:19
= default;
chrisha
2015/09/08 15:47:15
No longer applicable.
| |
97 ~UmaReportingDelegate() override {} | |
grt (UTC plus 2)
2015/09/04 19:46:19
hmm, can you = default; here as well rather than o
chrisha
2015/09/08 15:47:14
No longer applicable.
| |
98 | |
99 // ReportingDelegate: | |
100 void ReportCumulativeTime(MemoryPressureLevel pressure_level, | |
101 int seconds) override; | |
102 void ReportLevelChange(MemoryPressureLevel old_pressure_level, | |
103 MemoryPressureLevel new_pressure_level) override; | |
104 | |
105 private: | |
106 DISALLOW_COPY_AND_ASSIGN(UmaReportingDelegate); | |
107 }; | |
108 | |
109 } // namespace memory_pressure | |
110 | |
111 #endif // COMPONENTS_MEMORY_PRESSURE_MEMORY_PRESSURE_STATS_COLLECTOR_H_ | |
OLD | NEW |