OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "athena/resource_manager/public/resource_manager_delegate.h" | 5 #include "athena/resource_manager/public/resource_manager_delegate.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
11 #include "base/process/process_metrics.h" | 11 #include "base/process/process_metrics.h" |
12 | 12 |
13 namespace athena { | 13 namespace athena { |
14 | 14 |
15 namespace { | 15 namespace { |
16 // This is the minimum amount of time in milliseconds between checks for | 16 // This is the minimum amount of time in milliseconds between checks for |
17 // memory pressure. | 17 // memory pressure. |
18 const int kMemoryPressureIntervalMs = 750; | 18 const int kMemoryPressureIntervalMs = 750; |
19 } // namespace | 19 } // namespace |
20 | 20 |
21 class ResourceManagerDelegateImpl : public ResourceManagerDelegate { | 21 class ResourceManagerDelegateImpl : public ResourceManagerDelegate { |
22 public: | 22 public: |
23 ResourceManagerDelegateImpl() {} | 23 ResourceManagerDelegateImpl() {} |
24 virtual ~ResourceManagerDelegateImpl() {} | 24 virtual ~ResourceManagerDelegateImpl() {} |
25 | 25 |
26 private: | 26 private: |
27 virtual int GetUsedMemoryInPercent() OVERRIDE { | 27 virtual int GetUsedMemoryInPercent() OVERRIDE { |
28 base::SystemMemoryInfoKB memory; | 28 base::SystemMemoryInfoKB info; |
29 // TODO(skuhne): According to semenzato this calculation has to change. | 29 if (base::GetSystemMemoryInfo(&info)) { |
Jun Mukai
2014/08/29 22:59:32
Early exit like if(!base::GetSystemMemoryInfo(&inf
Mr4D (OOO till 08-26)
2014/08/29 23:44:03
Done.
| |
30 if (base::GetSystemMemoryInfo(&memory) && | 30 // TODO(skuhne): Instead of adding the kernel memory pressure calculation |
31 memory.total > 0 && memory.free >= 0) { | 31 // logic here, we should have a kernel mechanism similar to the low memory |
32 return ((memory.total - memory.free) * 100) / memory.total; | 32 // notifier in ChromeOS which offers multiple pressure states. |
33 // To track this, we have crbug.com/381196. | |
34 | |
35 // The available memory consists of "real" and virtual (z)ram memory. | |
36 // Since swappable memory uses a non pre-deterministic compression and | |
37 // the compression creates its own "dynamic" in the system, it gets | |
38 // de-emphasized by the |kSwapWeight| factor. | |
39 const int kSwapWeight = 4; | |
40 | |
41 // The total memory we have is the "real memory" plus the virtual (z)ram. | |
42 int total_memory = info.total + info.swap_total / kSwapWeight; | |
43 | |
44 // The kernel internally uses 50MB. | |
45 int min_file_memory = 50 * 1024; | |
Jun Mukai
2014/08/29 22:59:32
const int kMinFileMemory = ...
Mr4D (OOO till 08-26)
2014/08/29 23:44:03
Done.
| |
46 | |
47 // Most file memory can be easily reclaimed. | |
48 int file_memory = info.active_file + info.inactive_file; | |
49 // unless it is dirty or it's a minimal portion which is required. | |
50 file_memory -= info.dirty - min_file_memory; | |
Jun Mukai
2014/08/29 22:59:33
If dirty is less than 50MB, it will increase file_
Mr4D (OOO till 08-26)
2014/08/29 23:44:03
sign got now corrected.
| |
51 | |
52 // Available memory is the sum of free, swap and easy reclaimable memory. | |
53 int available_memory = | |
54 info.free + info.swap_free / kSwapWeight + file_memory; | |
Jun Mukai
2014/08/29 22:59:33
or this could be std::min(file_memory, min_file_me
Mr4D (OOO till 08-26)
2014/08/29 23:44:03
According to our semenzato that isn't a problem. (
| |
55 | |
56 DCHECK(available_memory < total_memory); | |
57 int percentage = ((total_memory - available_memory) * 100) / total_memory; | |
58 return percentage; | |
33 } | 59 } |
34 LOG(WARNING) << "Cannot determine the free memory of the system."; | 60 LOG(WARNING) << "Cannot determine the free memory of the system."; |
35 return 0; | 61 return 0; |
36 } | 62 } |
37 | 63 |
38 virtual int MemoryPressureIntervalInMS() OVERRIDE { | 64 virtual int MemoryPressureIntervalInMS() OVERRIDE { |
39 return kMemoryPressureIntervalMs; | 65 return kMemoryPressureIntervalMs; |
40 } | 66 } |
41 DISALLOW_COPY_AND_ASSIGN(ResourceManagerDelegateImpl); | 67 DISALLOW_COPY_AND_ASSIGN(ResourceManagerDelegateImpl); |
42 }; | 68 }; |
43 | 69 |
44 // static | 70 // static |
45 ResourceManagerDelegate* | 71 ResourceManagerDelegate* |
46 ResourceManagerDelegate::CreateResourceManagerDelegate() { | 72 ResourceManagerDelegate::CreateResourceManagerDelegate() { |
47 return new ResourceManagerDelegateImpl; | 73 return new ResourceManagerDelegateImpl; |
48 } | 74 } |
49 | 75 |
50 } // namespace athena | 76 } // namespace athena |
OLD | NEW |