Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / base / memory / memory_pressure_monitor_chromeos.h
blobff8992a56314ae33b29bac7dc4c40c20f6af42b6
1 // Copyright 2014 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.
5 #ifndef BASE_MEMORY_MEMORY_PRESSURE_MONITOR_CHROMEOS_H_
6 #define BASE_MEMORY_MEMORY_PRESSURE_MONITOR_CHROMEOS_H_
8 #include "base/base_export.h"
9 #include "base/files/scoped_file.h"
10 #include "base/gtest_prod_util.h"
11 #include "base/macros.h"
12 #include "base/memory/memory_pressure_listener.h"
13 #include "base/memory/memory_pressure_monitor.h"
14 #include "base/memory/weak_ptr.h"
15 #include "base/timer/timer.h"
17 namespace base {
18 namespace chromeos {
20 class TestMemoryPressureMonitor;
22 ////////////////////////////////////////////////////////////////////////////////
23 // MemoryPressureMonitor
25 // A class to handle the observation of our free memory. It notifies the
26 // MemoryPressureListener of memory fill level changes, so that it can take
27 // action to reduce memory resources accordingly.
29 class BASE_EXPORT MemoryPressureMonitor : public base::MemoryPressureMonitor {
30 public:
31 using GetUsedMemoryInPercentCallback = int (*)();
33 // There are two memory pressure events:
34 // MODERATE - which will mainly release caches.
35 // CRITICAL - which will discard tabs.
36 // The |MemoryPressureThresholds| enum selects the strategy of firing these
37 // events: A conservative strategy will keep as much content in memory as
38 // possible (causing the system to swap to zram) and an aggressive strategy
39 // will release memory earlier to avoid swapping.
40 enum MemoryPressureThresholds {
41 // Use the system default.
42 THRESHOLD_DEFAULT = 0,
43 // Try to keep as much content in memory as possible.
44 THRESHOLD_CONSERVATIVE = 1,
45 // Discard caches earlier, allowing to keep more tabs in memory.
46 THRESHOLD_AGGRESSIVE_CACHE_DISCARD = 2,
47 // Discard tabs earlier, allowing the system to get faster.
48 THRESHOLD_AGGRESSIVE_TAB_DISCARD = 3,
49 // Discard caches and tabs earlier to allow the system to be faster.
50 THRESHOLD_AGGRESSIVE = 4
53 explicit MemoryPressureMonitor(MemoryPressureThresholds thresholds);
54 ~MemoryPressureMonitor() override;
56 // Redo the memory pressure calculation soon and call again if a critical
57 // memory pressure prevails. Note that this call will trigger an asynchronous
58 // action which gives the system time to release memory back into the pool.
59 void ScheduleEarlyCheck();
61 // Get the current memory pressure level.
62 MemoryPressureListener::MemoryPressureLevel GetCurrentPressureLevel() const
63 override;
65 // Returns a type-casted version of the current memory pressure monitor. A
66 // simple wrapper to base::MemoryPressureMonitor::Get.
67 static MemoryPressureMonitor* Get();
69 private:
70 friend TestMemoryPressureMonitor;
71 // Starts observing the memory fill level.
72 // Calls to StartObserving should always be matched with calls to
73 // StopObserving.
74 void StartObserving();
76 // Stop observing the memory fill level.
77 // May be safely called if StartObserving has not been called.
78 void StopObserving();
80 // The function which gets periodically called to check any changes in the
81 // memory pressure. It will report pressure changes as well as continuous
82 // critical pressure levels.
83 void CheckMemoryPressure();
85 // The function periodically checks the memory pressure changes and records
86 // the UMA histogram statistics for the current memory pressure level.
87 void CheckMemoryPressureAndRecordStatistics();
89 // Get the memory pressure in percent (virtual for testing).
90 virtual int GetUsedMemoryInPercent();
92 // The current memory pressure.
93 base::MemoryPressureListener::MemoryPressureLevel
94 current_memory_pressure_level_;
96 // A periodic timer to check for resource pressure changes. This will get
97 // replaced by a kernel triggered event system (see crbug.com/381196).
98 base::RepeatingTimer<MemoryPressureMonitor> timer_;
100 // To slow down the amount of moderate pressure event calls, this counter
101 // gets used to count the number of events since the last event occured.
102 int moderate_pressure_repeat_count_;
104 // The thresholds for moderate and critical pressure.
105 const int moderate_pressure_threshold_percent_;
106 const int critical_pressure_threshold_percent_;
108 // File descriptor used to detect low memory condition.
109 ScopedFD low_mem_file_;
111 base::WeakPtrFactory<MemoryPressureMonitor> weak_ptr_factory_;
113 DISALLOW_COPY_AND_ASSIGN(MemoryPressureMonitor);
116 } // namespace chromeos
117 } // namespace base
119 #endif // BASE_MEMORY_MEMORY_PRESSURE_MONITOR_CHROMEOS_H_