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 #include "components/memory_pressure/direct_memory_pressure_calculator_win.h" | |
6 | |
7 #include "base/logging.h" | |
8 #include "base/process/process_metrics.h" | |
9 | |
10 namespace memory_pressure { | |
11 | |
12 #if defined(MEMORY_PRESSURE_IS_POLLING) | |
13 | |
14 namespace { | |
15 | |
16 const int kKBperMB = 1024; | |
17 | |
18 } // namespace | |
19 | |
20 // A system is considered 'high memory' if it has more than 1.5GB of system | |
21 // memory available for use by the memory manager (not reserved for hardware | |
22 // and drivers). This is a fuzzy version of the ~2GB discussed below. | |
23 const int DirectMemoryPressureCalculator::kLargeMemoryThresholdMb = 1536; | |
24 | |
25 // These are the default thresholds used for systems with < ~2GB of physical | |
26 // memory. Such systems have been observed to always maintain ~100MB of | |
27 // available memory, paging until that is the case. To try to avoid paging a | |
28 // threshold slightly above this is chosen. The moderate threshold is slightly | |
29 // less grounded in reality and chosen as 2.5x critical. | |
30 const int | |
31 DirectMemoryPressureCalculator::kSmallMemoryDefaultModerateThresholdMb = | |
32 500; | |
33 const int | |
34 DirectMemoryPressureCalculator::kSmallMemoryDefaultCriticalThresholdMb = | |
35 200; | |
36 | |
37 // These are the default thresholds used for systems with >= ~2GB of physical | |
38 // memory. Such systems have been observed to always maintain ~300MB of | |
39 // available memory, paging until that is the case. | |
40 const int | |
41 DirectMemoryPressureCalculator::kLargeMemoryDefaultModerateThresholdMb = | |
42 1000; | |
43 const int | |
44 DirectMemoryPressureCalculator::kLargeMemoryDefaultCriticalThresholdMb = | |
45 400; | |
46 | |
47 DirectMemoryPressureCalculator::DirectMemoryPressureCalculator() | |
48 : moderate_threshold_mb_(0), critical_threshold_mb_(0) { | |
49 InferThresholds(); | |
50 } | |
51 | |
52 DirectMemoryPressureCalculator::DirectMemoryPressureCalculator( | |
53 int moderate_threshold_mb, | |
54 int critical_threshold_mb) | |
55 : moderate_threshold_mb_(moderate_threshold_mb), | |
56 critical_threshold_mb_(critical_threshold_mb) { | |
57 DCHECK_GE(moderate_threshold_mb_, critical_threshold_mb_); | |
58 DCHECK_LE(0, critical_threshold_mb_); | |
59 } | |
60 | |
61 DirectMemoryPressureCalculator::MemoryPressureLevel | |
62 DirectMemoryPressureCalculator::CalculateCurrentPressureLevel() { | |
63 base::SystemMemoryInfoKB mem_info = {}; | |
64 if (!GetSystemMemoryInfo(&mem_info)) | |
65 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; | |
66 | |
67 // How much system memory is actively available for use right now, in MBs. | |
68 int phys_free = mem_info.avail_phys / kKBperMB; | |
69 | |
70 // TODO(chrisha): This should eventually care about address space pressure, | |
71 // but the browser process (where this is running) effectively never runs out | |
72 // of address space. Renderers occasionally do, but it does them no good to | |
73 // have the browser process monitor address space pressure. Long term, | |
74 // renderers should run their own address space pressure monitors and act | |
75 // accordingly, with the browser making cross-process decisions based on | |
76 // system memory pressure. | |
77 | |
78 // Determine if the physical memory is under critical memory pressure. | |
79 if (phys_free <= critical_threshold_mb_) | |
80 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL; | |
81 | |
82 // Determine if the physical memory is under moderate memory pressure. | |
83 if (phys_free <= moderate_threshold_mb_) | |
84 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE; | |
85 | |
86 // No memory pressure was detected. | |
87 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; | |
88 } | |
89 | |
90 bool DirectMemoryPressureCalculator::GetSystemMemoryInfo( | |
91 base::SystemMemoryInfoKB* mem_info) const { | |
92 return base::GetSystemMemoryInfo(mem_info); | |
93 } | |
94 | |
95 void DirectMemoryPressureCalculator::InferThresholds() { | |
96 // Determine if the memory installed is 'large' or 'small'. Default to 'large' | |
97 // on failure, which uses more conservative thresholds. | |
98 bool large_memory = true; | |
99 base::SystemMemoryInfoKB mem_info = {}; | |
100 if (GetSystemMemoryInfo(&mem_info)) { | |
101 large_memory = mem_info.total / kKBperMB >= | |
102 DirectMemoryPressureCalculator::kLargeMemoryThresholdMb; | |
103 } | |
104 | |
105 if (large_memory) { | |
106 moderate_threshold_mb_ = kLargeMemoryDefaultModerateThresholdMb; | |
107 critical_threshold_mb_ = kLargeMemoryDefaultCriticalThresholdMb; | |
108 } else { | |
109 moderate_threshold_mb_ = kSmallMemoryDefaultModerateThresholdMb; | |
110 critical_threshold_mb_ = kSmallMemoryDefaultCriticalThresholdMb; | |
111 } | |
112 } | |
113 | |
114 #endif // defined(MEMORY_PRESSURE_IS_POLLING) | |
115 | |
116 } // namespace memory_pressure | |
OLD | NEW |