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/macros.h" | |
9 #include "base/time/time.h" | |
10 #include "components/memory_pressure/memory_pressure_listener.h" | |
11 | |
12 namespace base { | |
13 class TickClock; | |
14 } | |
15 | |
16 namespace memory_pressure { | |
17 | |
18 // Enumeration of UMA memory pressure levels. The pressure level enum defined in | |
19 // MemoryPressureListener is non-contiguous. This enum is a contiguous version | |
20 // of that for use with UMA. Both it and histograms.xml must be kept in sync | |
21 // with the MemoryPressureListener enum. Included in the header so that | |
22 // UMA_MEMORY_PRESSURE_LEVEL_COUNT is available. | |
23 enum MemoryPressureLevelUMA { | |
24 UMA_MEMORY_PRESSURE_LEVEL_NONE = 0, | |
25 UMA_MEMORY_PRESSURE_LEVEL_MODERATE = 1, | |
26 UMA_MEMORY_PRESSURE_LEVEL_CRITICAL = 2, | |
27 // This must be the last value in the enum. | |
28 UMA_MEMORY_PRESSURE_LEVEL_COUNT, | |
29 }; | |
30 | |
31 // Enumeration of UMA pressure level changes. This needs to be kept in sync | |
32 // with histograms.xml and the memory pressure levels defined in | |
33 // MemoryPressureListener. Exposed for unittesting. | |
34 enum MemoryPressureLevelChangeUMA { | |
35 UMA_MEMORY_PRESSURE_LEVEL_CHANGE_NONE_TO_MODERATE = 0, | |
36 UMA_MEMORY_PRESSURE_LEVEL_CHANGE_NONE_TO_CRITICAL = 1, | |
37 UMA_MEMORY_PRESSURE_LEVEL_CHANGE_MODERATE_TO_CRITICAL = 2, | |
38 UMA_MEMORY_PRESSURE_LEVEL_CHANGE_CRITICAL_TO_MODERATE = 3, | |
39 UMA_MEMORY_PRESSURE_LEVEL_CHANGE_CRITICAL_TO_NONE = 4, | |
40 UMA_MEMORY_PRESSURE_LEVEL_CHANGE_MODERATE_TO_NONE = 5, | |
41 // This must be the last value in the enum. | |
42 UMA_MEMORY_PRESSURE_LEVEL_CHANGE_COUNT | |
43 }; | |
44 | |
45 // Class that is responsible for collecting and eventually reporting memory | |
46 // pressure statistics. Contributes to the "Memory.PressureLevel" and | |
47 // "Memory.PressureLevelChanges" histograms. | |
48 // | |
49 // On platforms with a polling memory pressure implementation the | |
50 // UpdateStatistics function will be invoked every time the pressure is polled. | |
51 // On non-polling platforms (Mac, Android) it will be invoked on a periodic | |
52 // timer, and at the moment of pressure level changes. | |
53 class MemoryPressureStatsCollector { | |
54 public: | |
55 using MemoryPressureLevel = MemoryPressureListener::MemoryPressureLevel; | |
56 | |
57 // The provided |tick_clock| must outlive this class. | |
58 MemoryPressureStatsCollector(base::TickClock* tick_clock); | |
59 | |
60 // This is to be called periodically to ensure that up to date statistics | |
61 // have been reported. | |
62 void UpdateStatistics(MemoryPressureLevel current_pressure_level); | |
63 | |
64 private: | |
65 friend class TestMemoryPressureStatsCollector; | |
66 | |
67 // Helper functions for delivering collected statistics. | |
68 static void ReportCumulativeTime(MemoryPressureLevel pressure_level, | |
69 int seconds); | |
70 static void ReportLevelChange(MemoryPressureLevel old_pressure_level, | |
71 MemoryPressureLevel new_pressure_level); | |
72 | |
73 // The tick clock in use. This class is intended to be owned by a class with | |
74 // a tick clock, and ownership remains there. Also intended as a test seam. | |
75 base::TickClock* tick_clock_; | |
76 | |
77 // Buckets of time that have been spent in different pressure levels, but | |
78 // not yet reported. At every call to UpdateStatistics these buckets will be | |
79 // drained of as many 'full' seconds of time as possible and reported via | |
80 // UMA. The remaining time (< 1000 ms) will be left in the bucket to be rolled | |
81 // into a later UMA report. | |
82 base::TimeDelta unreported_cumulative_time_[UMA_MEMORY_PRESSURE_LEVEL_COUNT]; | |
83 | |
84 // The last observed pressure level and the time at which it was observed, and | |
85 // the time when this pressure level started. | |
86 MemoryPressureLevel last_pressure_level_; | |
87 base::TimeTicks last_update_time_; | |
88 | |
89 DISALLOW_COPY_AND_ASSIGN(MemoryPressureStatsCollector); | |
90 }; | |
91 | |
92 } // namespace memory_pressure | |
93 | |
94 #endif // COMPONENTS_MEMORY_PRESSURE_MEMORY_PRESSURE_STATS_COLLECTOR_H_ | |
OLD | NEW |