Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / components / memory_pressure / direct_memory_pressure_calculator.h
blob6774b344ddd7f66d0c8635ea73084112531da259
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.
5 #ifndef COMPONENTS_MEMORY_PRESSURE_DIRECT_MEMORY_PRESSURE_CALCULATOR_H_
6 #define COMPONENTS_MEMORY_PRESSURE_DIRECT_MEMORY_PRESSURE_CALCULATOR_H_
8 #include "components/memory_pressure/memory_pressure_calculator.h"
10 #include "base/process/process_metrics.h"
12 namespace memory_pressure {
14 #if defined(MEMORY_PRESSURE_IS_POLLING)
16 // OS-specific implementation of MemoryPressureCalculator. This is only defined
17 // and used on platforms that do not have native memory pressure signals
18 // (ChromeOS, Linux, Windows). OSes that do have native signals simply hook into
19 // the appropriate subsystem (Android, Mac OS X).
20 class DirectMemoryPressureCalculator : public MemoryPressureCalculator {
21 public:
22 #if defined(OS_WIN)
23 // Exposed for unittesting. See .cc file for detailed discussion of these
24 // constants.
25 static const int kLargeMemoryThresholdMb;
26 static const int kSmallMemoryDefaultModerateThresholdMb;
27 static const int kSmallMemoryDefaultCriticalThresholdMb;
28 static const int kLargeMemoryDefaultModerateThresholdMb;
29 static const int kLargeMemoryDefaultCriticalThresholdMb;
30 #endif
32 // Default constructor. Will choose thresholds automatically based on the
33 // actual amount of system memory installed.
34 DirectMemoryPressureCalculator();
36 // Constructor with explicit memory thresholds. These represent the amount of
37 // free memory below which the applicable memory pressure state applies.
38 DirectMemoryPressureCalculator(int moderate_threshold_mb,
39 int critical_threshold_mb);
41 ~DirectMemoryPressureCalculator() override {}
43 // Calculates the current pressure level.
44 MemoryPressureLevel CalculateCurrentPressureLevel() override;
46 #if defined(OS_WIN)
47 int moderate_threshold_mb() const { return moderate_threshold_mb_; }
48 int critical_threshold_mb() const { return critical_threshold_mb_; }
49 #endif
51 private:
52 friend class TestDirectMemoryPressureCalculator;
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;
58 #if defined(OS_WIN)
59 // Uses GetSystemMemoryInfo to automatically infer appropriate values for
60 // moderate_threshold_mb_ and critical_threshold_mb_.
61 void InferThresholds();
63 // Threshold amounts of available memory that trigger pressure levels. See
64 // memory_pressure_monitor_win.cc for a discussion of reasonable values for
65 // these.
66 int moderate_threshold_mb_;
67 int critical_threshold_mb_;
68 #endif
70 DISALLOW_COPY_AND_ASSIGN(DirectMemoryPressureCalculator);
73 #endif // defined(MEMORY_PRESSURE_IS_POLLING)
75 } // namespace memory_pressure
77 #endif // COMPONENTS_MEMORY_PRESSURE_DIRECT_MEMORY_PRESSURE_CALCULATOR_H_