Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(313)

Side by Side Diff: athena/resource_manager/delegate/resource_manager_delegate.cc

Issue 511383003: Adding proper GetUsedMemoryInPercent calculation (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <algorithm>
7 #include <string> 8 #include <string>
8 9
9 #include "base/logging.h" 10 #include "base/logging.h"
10 #include "base/macros.h" 11 #include "base/macros.h"
11 #include "base/process/process_metrics.h" 12 #include "base/process/process_metrics.h"
12 13
13 namespace athena { 14 namespace athena {
14 15
15 namespace { 16 namespace {
16 // This is the minimum amount of time in milliseconds between checks for 17 // This is the minimum amount of time in milliseconds between checks for
17 // memory pressure. 18 // memory pressure.
18 const int kMemoryPressureIntervalMs = 750; 19 const int kMemoryPressureIntervalMs = 750;
19 } // namespace 20 } // namespace
20 21
21 class ResourceManagerDelegateImpl : public ResourceManagerDelegate { 22 class ResourceManagerDelegateImpl : public ResourceManagerDelegate {
22 public: 23 public:
23 ResourceManagerDelegateImpl() {} 24 ResourceManagerDelegateImpl() {}
24 virtual ~ResourceManagerDelegateImpl() {} 25 virtual ~ResourceManagerDelegateImpl() {}
25 26
26 private: 27 private:
27 virtual int GetUsedMemoryInPercent() OVERRIDE { 28 virtual int GetUsedMemoryInPercent() OVERRIDE {
28 base::SystemMemoryInfoKB memory; 29 base::SystemMemoryInfoKB info;
29 // TODO(skuhne): According to semenzato this calculation has to change. 30 if (base::GetSystemMemoryInfo(&info) && info.total > 0 && info.free >= 0) {
Luigi Semenzato 2014/08/29 15:37:36 Not sure that you should check info.total and info
Mr4D (OOO till 08-26) 2014/08/29 16:45:47 Done.
30 if (base::GetSystemMemoryInfo(&memory) && 31 // TODO(skuhne): Instead of adding the kernel memory pressure calculation
Luigi Semenzato 2014/08/29 15:37:36 Do you want to open a bug to keep track of this?
Mr4D (OOO till 08-26) 2014/08/29 16:45:47 Done.
31 memory.total > 0 && memory.free >= 0) { 32 // logic here, we should have a kernel mechanism similar to the low memory
32 return ((memory.total - memory.free) * 100) / memory.total; 33 // notifier in ChromeOS which offers multiple pressure states.
34
35 // The available memory consists of "real" and virtual (z)ram memory.
36 // Since swapp-able memory uses a non pre-deterministic compression and
Luigi Semenzato 2014/08/29 15:37:36 "swappable" may be better
Mr4D (OOO till 08-26) 2014/08/29 16:45:46 Done.
37 // the compression creates it's own "dynamic" in the system, it gets
Luigi Semenzato 2014/08/29 15:37:36 its
Mr4D (OOO till 08-26) 2014/08/29 16:45:46 Done.
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 // We don't know this value - it's kernel internal.
Luigi Semenzato 2014/08/29 15:37:36 Just use 50Mb. You're already copying kernel inte
Mr4D (OOO till 08-26) 2014/08/29 16:45:46 Done.
45 int min_file_memory = 0;
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;
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;
55
56 int percentage = ((total_memory - available_memory) * 100) / total_memory;
57 return std::min(0, std::max(percentage, 100));
Luigi Semenzato 2014/08/29 15:37:36 available memory can be assumed to be positive, so
Mr4D (OOO till 08-26) 2014/08/29 16:45:47 Done. I might be wrong, but what if the compressio
Luigi Semenzato 2014/08/29 18:42:56 Ah, good question. This may be a concern in gener
33 } 58 }
34 LOG(WARNING) << "Cannot determine the free memory of the system."; 59 LOG(WARNING) << "Cannot determine the free memory of the system.";
35 return 0; 60 return 0;
36 } 61 }
37 62
38 virtual int MemoryPressureIntervalInMS() OVERRIDE { 63 virtual int MemoryPressureIntervalInMS() OVERRIDE {
39 return kMemoryPressureIntervalMs; 64 return kMemoryPressureIntervalMs;
40 } 65 }
41 DISALLOW_COPY_AND_ASSIGN(ResourceManagerDelegateImpl); 66 DISALLOW_COPY_AND_ASSIGN(ResourceManagerDelegateImpl);
42 }; 67 };
43 68
44 // static 69 // static
45 ResourceManagerDelegate* 70 ResourceManagerDelegate*
46 ResourceManagerDelegate::CreateResourceManagerDelegate() { 71 ResourceManagerDelegate::CreateResourceManagerDelegate() {
47 return new ResourceManagerDelegateImpl; 72 return new ResourceManagerDelegateImpl;
48 } 73 }
49 74
50 } // namespace athena 75 } // namespace athena
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698