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 #include "components/memory_pressure/direct_memory_pressure_calculator_linux.h" | |
6 | |
7 #include "base/process/process_metrics.h" | |
8 #include "base/sys_info.h" | |
9 | |
10 namespace memory_pressure { | |
11 | |
12 namespace { | |
13 | |
14 static const int kKBperMB = 1024; | |
chrisha
2016/01/20 20:55:43
pedantic nit: kKiBperMiB
Georges Khalil
2016/01/21 14:47:44
Done.
| |
15 | |
16 } // namespace | |
17 | |
18 // Threshholds at which we consider the system being under moderate/critical | |
chrisha
2016/01/20 20:55:43
Thresholds*
Georges Khalil
2016/01/21 14:47:44
Done.
| |
19 // memory pressure. | |
20 const int DirectMemoryPressureCalculator::kDefaultModerateThresholdPc = 70; | |
21 const int DirectMemoryPressureCalculator::kDefaultCriticalThresholdPc = 90; | |
chrisha
2016/01/20 20:55:43
I imagine Pc = Percent? Just spell it out already,
Georges Khalil
2016/01/21 14:47:44
Done.
| |
22 | |
23 DirectMemoryPressureCalculator::DirectMemoryPressureCalculator() | |
24 : moderate_threshold_mb_(0), critical_threshold_mb_(0) { | |
25 InferThresholds(); | |
26 } | |
27 | |
28 DirectMemoryPressureCalculator::DirectMemoryPressureCalculator( | |
29 int moderate_threshold_mb, | |
30 int critical_threshold_mb) | |
31 : moderate_threshold_mb_(moderate_threshold_mb), | |
32 critical_threshold_mb_(critical_threshold_mb) { | |
33 DCHECK_GE(moderate_threshold_mb_, critical_threshold_mb_); | |
34 DCHECK_LE(0, critical_threshold_mb_); | |
35 } | |
36 | |
37 DirectMemoryPressureCalculator::MemoryPressureLevel | |
38 DirectMemoryPressureCalculator::CalculateCurrentPressureLevel() { | |
39 base::SystemMemoryInfoKB mem_info = {}; | |
40 if (!GetSystemMemoryInfo(&mem_info)) | |
41 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; | |
42 | |
43 // How much system memory is actively available for use right now, in MBs. | |
44 int phys_free = mem_info.free / kKBperMB; | |
45 | |
46 // Determine if the physical memory is under critical memory pressure. | |
47 if (phys_free <= critical_threshold_mb_) | |
48 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL; | |
49 | |
50 // Determine if the physical memory is under moderate memory pressure. | |
51 if (phys_free <= moderate_threshold_mb_) | |
52 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE; | |
53 | |
54 // No memory pressure was detected. | |
55 return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; | |
56 } | |
57 | |
58 bool DirectMemoryPressureCalculator::GetSystemMemoryInfo( | |
59 base::SystemMemoryInfoKB* mem_info) const { | |
60 return base::GetSystemMemoryInfo(mem_info); | |
61 } | |
62 | |
63 void DirectMemoryPressureCalculator::InferThresholds() { | |
64 base::SystemMemoryInfoKB mem_info = {}; | |
65 if (!GetSystemMemoryInfo(&mem_info)) | |
66 return; | |
67 | |
68 moderate_threshold_mb_ = | |
69 mem_info.total * (100 - kDefaultModerateThresholdPc) / 100 / kKBperMB; | |
70 critical_threshold_mb_ = | |
71 mem_info.total * (100 - kDefaultCriticalThresholdPc) / 100 / kKBperMB; | |
72 } | |
73 | |
74 } // namespace memory_pressure | |
OLD | NEW |