Chromium Code Reviews| Index: components/memory_pressure/direct_memory_pressure_calculator_linux.cc |
| diff --git a/components/memory_pressure/direct_memory_pressure_calculator_linux.cc b/components/memory_pressure/direct_memory_pressure_calculator_linux.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..696e13d87265a9f9b0f97b9f476df2b2d306a01a |
| --- /dev/null |
| +++ b/components/memory_pressure/direct_memory_pressure_calculator_linux.cc |
| @@ -0,0 +1,74 @@ |
| +// Copyright 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "components/memory_pressure/direct_memory_pressure_calculator_linux.h" |
| + |
| +#include "base/process/process_metrics.h" |
| +#include "base/sys_info.h" |
| + |
| +namespace memory_pressure { |
| + |
| +namespace { |
| + |
| +static const int kKBperMB = 1024; |
|
chrisha
2016/01/20 20:55:43
pedantic nit: kKiBperMiB
Georges Khalil
2016/01/21 14:47:44
Done.
|
| + |
| +} // namespace |
| + |
| +// 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.
|
| +// memory pressure. |
| +const int DirectMemoryPressureCalculator::kDefaultModerateThresholdPc = 70; |
| +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.
|
| + |
| +DirectMemoryPressureCalculator::DirectMemoryPressureCalculator() |
| + : moderate_threshold_mb_(0), critical_threshold_mb_(0) { |
| + InferThresholds(); |
| +} |
| + |
| +DirectMemoryPressureCalculator::DirectMemoryPressureCalculator( |
| + int moderate_threshold_mb, |
| + int critical_threshold_mb) |
| + : moderate_threshold_mb_(moderate_threshold_mb), |
| + critical_threshold_mb_(critical_threshold_mb) { |
| + DCHECK_GE(moderate_threshold_mb_, critical_threshold_mb_); |
| + DCHECK_LE(0, critical_threshold_mb_); |
| +} |
| + |
| +DirectMemoryPressureCalculator::MemoryPressureLevel |
| +DirectMemoryPressureCalculator::CalculateCurrentPressureLevel() { |
| + base::SystemMemoryInfoKB mem_info = {}; |
| + if (!GetSystemMemoryInfo(&mem_info)) |
| + return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; |
| + |
| + // How much system memory is actively available for use right now, in MBs. |
| + int phys_free = mem_info.free / kKBperMB; |
| + |
| + // Determine if the physical memory is under critical memory pressure. |
| + if (phys_free <= critical_threshold_mb_) |
| + return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL; |
| + |
| + // Determine if the physical memory is under moderate memory pressure. |
| + if (phys_free <= moderate_threshold_mb_) |
| + return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE; |
| + |
| + // No memory pressure was detected. |
| + return MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE; |
| +} |
| + |
| +bool DirectMemoryPressureCalculator::GetSystemMemoryInfo( |
| + base::SystemMemoryInfoKB* mem_info) const { |
| + return base::GetSystemMemoryInfo(mem_info); |
| +} |
| + |
| +void DirectMemoryPressureCalculator::InferThresholds() { |
| + base::SystemMemoryInfoKB mem_info = {}; |
| + if (!GetSystemMemoryInfo(&mem_info)) |
| + return; |
| + |
| + moderate_threshold_mb_ = |
| + mem_info.total * (100 - kDefaultModerateThresholdPc) / 100 / kKBperMB; |
| + critical_threshold_mb_ = |
| + mem_info.total * (100 - kDefaultCriticalThresholdPc) / 100 / kKBperMB; |
| +} |
| + |
| +} // namespace memory_pressure |