OLD | NEW |
| (Empty) |
1 // Copyright 2016 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_DIRECT_MEMORY_PRESSURE_CALCULATOR_LINUX_H_ | |
6 #define COMPONENTS_MEMORY_PRESSURE_DIRECT_MEMORY_PRESSURE_CALCULATOR_LINUX_H_ | |
7 | |
8 #include "base/macros.h" | |
9 #include "base/process/process_metrics.h" | |
10 #include "components/memory_pressure/memory_pressure_calculator.h" | |
11 | |
12 namespace base { | |
13 class TimeDelta; | |
14 } | |
15 | |
16 namespace memory_pressure { | |
17 | |
18 // OS-specific implementation of MemoryPressureCalculator. This is only defined | |
19 // and used on platforms that do not have native memory pressure signals | |
20 // (ChromeOS, Linux, Windows). OSes that do have native signals simply hook into | |
21 // the appropriate subsystem (Android, Mac OS X). | |
22 class DirectMemoryPressureCalculator : public MemoryPressureCalculator { | |
23 public: | |
24 // Exposed for unittesting. See .cc file for detailed discussion of these | |
25 // constants. | |
26 static const int kDefaultModerateThresholdPc; | |
27 static const int kDefaultCriticalThresholdPc; | |
28 | |
29 // Default constructor. Will choose thresholds automatically based on the | |
30 // actual amount of system memory installed. | |
31 DirectMemoryPressureCalculator(); | |
32 | |
33 // Constructor with explicit memory thresholds. These represent the amount of | |
34 // free memory below which the applicable memory pressure state applies. | |
35 DirectMemoryPressureCalculator(int moderate_threshold_mb, | |
36 int critical_threshold_mb); | |
37 | |
38 ~DirectMemoryPressureCalculator() override {} | |
39 | |
40 // Calculates the current pressure level. | |
41 MemoryPressureLevel CalculateCurrentPressureLevel() override; | |
42 | |
43 int moderate_threshold_mb() const { return moderate_threshold_mb_; } | |
44 int critical_threshold_mb() const { return critical_threshold_mb_; } | |
45 | |
46 private: | |
47 friend class TestDirectMemoryPressureCalculator; | |
48 | |
49 MemoryPressureLevel PressureCausedByThrashing( | |
50 const base::SystemMemoryInfoKB& mem_info); | |
51 MemoryPressureLevel PressureCausedByOOM( | |
52 const base::SystemMemoryInfoKB& mem_info); | |
53 | |
54 // Gets system memory status. This is virtual as a unittesting hook and by | |
55 // default this invokes base::GetSystemMemoryInfo. | |
56 virtual bool GetSystemMemoryInfo(base::SystemMemoryInfoKB* mem_info) const; | |
57 | |
58 // We use CPU time instead of real time because this eliminates the variable | |
59 // of average system load. Consumer machines are idle most of the time, so if | |
60 // we used real time, the average faults/sec would be very low. As soon as | |
61 // the user loads anything, the instantaneous faults/sec would increase, and | |
62 // we would overreport the memory pressure. | |
63 // | |
64 // We also ignore system time because we don't want to include the time the OS | |
65 // takes to swap pages in/out on behalf of processes. | |
66 // | |
67 // Virtual as a unittesting hook. | |
68 virtual base::TimeDelta GetUserCpuTimeSinceBoot() const; | |
69 | |
70 // Uses GetSystemMemoryInfo to automatically infer appropriate values for | |
71 // moderate_threshold_mb_ and critical_threshold_mb_. | |
72 void InferThresholds(); | |
73 | |
74 // Initialize the page fault monitor state so CalculateCurrentPressureLevel | |
75 // will have a delta to analyze. | |
76 virtual void InitPageFaultMonitor(); | |
77 | |
78 // Computes last_major_page_faults_/last_user_exec_time_ with some | |
79 // protection. | |
80 double AverageFaultsPerSecond() const; | |
81 | |
82 // Threshold amounts of available memory that trigger pressure levels. See | |
83 // memory_pressure_monitor_win.cc for a discussion of reasonable values for | |
84 // these. | |
85 int moderate_threshold_mb_; | |
86 int critical_threshold_mb_; | |
87 | |
88 // State needed by the hard page fault monitor. These values are assumed | |
89 // not to overflow. | |
90 base::TimeDelta last_user_exec_time_; | |
91 uint64_t last_major_page_faults_; | |
92 // An exponentially weighted moving average of the sampled faults/sec. | |
93 double current_faults_per_second_; | |
94 | |
95 double low_pass_half_life_seconds_; | |
96 | |
97 // |current_faults_per_second_| must be at least |kModerateMultiplier| * | |
98 // AverageFaultsPerSecond() to report moderate pressure, and similarly for | |
99 // critical pressure. Non-const members for unit testing. | |
100 // | |
101 // TODO(thomasanderson): Experimentally determine the correct value for these | |
102 // constants. | |
103 double moderate_multiplier_ = 5.0; | |
104 double critical_multiplier_ = 10.0; | |
105 | |
106 DISALLOW_COPY_AND_ASSIGN(DirectMemoryPressureCalculator); | |
107 }; | |
108 | |
109 } // namespace memory_pressure | |
110 | |
111 #endif // COMPONENTS_MEMORY_PRESSURE_DIRECT_MEMORY_PRESSURE_CALCULATOR_LINUX_H_ | |
OLD | NEW |