1 // Copyright 2014 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 #include "athena/resource_manager/public/resource_manager_delegate.h"
9 #include "base/logging.h"
10 #include "base/macros.h"
11 #include "base/process/process_metrics.h"
16 // This is the minimum amount of time in milliseconds between checks for
18 const int kMemoryPressureIntervalMs
= 750;
21 class ResourceManagerDelegateImpl
: public ResourceManagerDelegate
{
23 ResourceManagerDelegateImpl() {}
24 virtual ~ResourceManagerDelegateImpl() {}
27 virtual int GetUsedMemoryInPercent() override
{
28 base::SystemMemoryInfoKB info
;
29 if (!base::GetSystemMemoryInfo(&info
)) {
30 LOG(WARNING
) << "Cannot determine the free memory of the system.";
33 // TODO(skuhne): Instead of adding the kernel memory pressure calculation
34 // logic here, we should have a kernel mechanism similar to the low memory
35 // notifier in ChromeOS which offers multiple pressure states.
36 // To track this, we have crbug.com/381196.
38 // The available memory consists of "real" and virtual (z)ram memory.
39 // Since swappable memory uses a non pre-deterministic compression and
40 // the compression creates its own "dynamic" in the system, it gets
41 // de-emphasized by the |kSwapWeight| factor.
42 const int kSwapWeight
= 4;
44 // The total memory we have is the "real memory" plus the virtual (z)ram.
45 int total_memory
= info
.total
+ info
.swap_total
/ kSwapWeight
;
47 // The kernel internally uses 50MB.
48 const int kMinFileMemory
= 50 * 1024;
50 // Most file memory can be easily reclaimed.
51 int file_memory
= info
.active_file
+ info
.inactive_file
;
52 // unless it is dirty or it's a minimal portion which is required.
53 file_memory
-= info
.dirty
+ kMinFileMemory
;
55 // Available memory is the sum of free, swap and easy reclaimable memory.
56 int available_memory
=
57 info
.free
+ info
.swap_free
/ kSwapWeight
+ file_memory
;
59 DCHECK(available_memory
< total_memory
);
60 int percentage
= ((total_memory
- available_memory
) * 100) / total_memory
;
64 virtual int MemoryPressureIntervalInMS() override
{
65 return kMemoryPressureIntervalMs
;
67 DISALLOW_COPY_AND_ASSIGN(ResourceManagerDelegateImpl
);
71 ResourceManagerDelegate
*
72 ResourceManagerDelegate::CreateResourceManagerDelegate() {
73 return new ResourceManagerDelegateImpl
;