Commit 98900b02 authored by bcwhite's avatar bcwhite Committed by Commit bot

Added memory-monitor for ChromeOS.

BUG=644397

Review-Url: https://codereview.chromium.org/2332423002
Cr-Commit-Position: refs/heads/master@{#419544}
parent e9236744
......@@ -867,8 +867,6 @@ source_set("browser") {
"memory/memory_message_filter.h",
"memory/memory_monitor.cc",
"memory/memory_monitor.h",
"memory/memory_monitor_linux.cc",
"memory/memory_monitor_linux.h",
"memory/memory_monitor_win.cc",
"memory/memory_monitor_win.h",
"memory/memory_pressure_controller_impl.cc",
......@@ -1383,6 +1381,19 @@ source_set("browser") {
deps += [ "//ui/events" ]
}
# ChromeOS also defines linux but their memory-monitors conflict.
if (is_chromeos) {
sources += [
"memory/memory_monitor_chromeos.cc",
"memory/memory_monitor_chromeos.h",
]
} else {
sources += [
"memory/memory_monitor_linux.cc",
"memory/memory_monitor_linux.h",
]
}
if (!is_win) {
sources += [
"file_descriptor_info_impl.cc",
......
// Copyright (c) 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 "content/browser/memory/memory_monitor_chromeos.h"
#include "base/memory/ptr_util.h"
#include "base/process/process_metrics.h"
namespace content {
namespace {
// The number of bits to shift to convert KiB to MiB.
const int kShiftKiBtoMiB = 10;
} // namespace
MemoryMonitorChromeOS::MemoryMonitorChromeOS(MemoryMonitorDelegate* delegate)
: delegate_(delegate) {}
MemoryMonitorChromeOS::~MemoryMonitorChromeOS() {}
int MemoryMonitorChromeOS::GetFreeMemoryUntilCriticalMB() {
base::SystemMemoryInfoKB mem_info = {};
delegate_->GetSystemMemoryInfo(&mem_info);
// The available memory consists of "real" and virtual (z)ram memory.
// Since swappable memory uses a non pre-deterministic compression and
// the compression creates its own "dynamic" in the system, it gets
// de-emphasized by the |kSwapWeight| factor.
const int kSwapWeight = 4;
// The kernel internally uses 50MB.
const int kMinFileMemory = 50 * 1024;
// Most file memory can be easily reclaimed.
int file_memory = mem_info.active_file + mem_info.inactive_file;
// unless it is dirty or it's a minimal portion which is required.
file_memory -= mem_info.dirty + kMinFileMemory;
// Available memory is the sum of free, swap and easy reclaimable memory.
return (mem_info.free + mem_info.swap_free / kSwapWeight + file_memory) >>
kShiftKiBtoMiB;
}
// static
std::unique_ptr<MemoryMonitorChromeOS> MemoryMonitorChromeOS::Create(
MemoryMonitorDelegate* delegate) {
return base::MakeUnique<MemoryMonitorChromeOS>(delegate);
}
// Implementation of factory function defined in memory_monitor.h.
std::unique_ptr<MemoryMonitor> CreateMemoryMonitor() {
return MemoryMonitorChromeOS::Create(MemoryMonitorDelegate::GetInstance());
}
} // namespace content
// Copyright (c) 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.
#ifndef CONTENT_BROWSER_MEMORY_MEMORY_MONITOR_CHROMEOS_H_
#define CONTENT_BROWSER_MEMORY_MEMORY_MONITOR_CHROMEOS_H_
#include "content/browser/memory/memory_monitor.h"
#include "content/common/content_export.h"
namespace content {
// A memory monitor for the ChromeOS platform.
class CONTENT_EXPORT MemoryMonitorChromeOS : public MemoryMonitor {
public:
MemoryMonitorChromeOS(MemoryMonitorDelegate* delegate);
~MemoryMonitorChromeOS() override;
// MemoryMonitor:
int GetFreeMemoryUntilCriticalMB() override;
// Factory function to create an instance of this class.
static std::unique_ptr<MemoryMonitorChromeOS> Create(
MemoryMonitorDelegate* delegate);
private:
// The delegate to be used for retrieving system memory information. Used as a
// testing seam.
MemoryMonitorDelegate* delegate_;
};
} // namespace content
#endif // CONTENT_BROWSER_MEMORY_MEMORY_MONITOR_CHROMEOS_H_
// Copyright (c) 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 "content/browser/memory/memory_monitor_chromeos.h"
#include "content/browser/memory/test_memory_monitor.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace content {
namespace {
// A delegate that allows mocking the various inputs to MemoryMonitorChromeOS.
class TestMemoryMonitorChromeOSDelegate : public TestMemoryMonitorDelegate {
public:
TestMemoryMonitorChromeOSDelegate() {}
void SetFreeMemoryKB(int free_kb,
int swap_kb,
int active_kb,
int inactive_kb,
int dirty_kb) {
mem_info_.free = free_kb;
mem_info_.swap_free = swap_kb;
mem_info_.active_file = active_kb;
mem_info_.inactive_file = inactive_kb;
mem_info_.dirty = dirty_kb;
}
private:
DISALLOW_COPY_AND_ASSIGN(TestMemoryMonitorChromeOSDelegate);
};
class TestMemoryMonitorChromeOS : public MemoryMonitorChromeOS {};
static const int kKBperMB = 1024;
} // namespace
class MemoryMonitorChromeOSTest : public testing::Test {
public:
TestMemoryMonitorChromeOSDelegate delegate_;
std::unique_ptr<MemoryMonitorChromeOS> monitor_;
};
TEST_F(MemoryMonitorChromeOSTest, Create) {
delegate_.SetTotalMemoryKB(100000 * kKBperMB);
monitor_ = MemoryMonitorChromeOS::Create(&delegate_);
EXPECT_EQ(0U, delegate_.calls());
}
TEST_F(MemoryMonitorChromeOSTest, GetFreeMemoryUntilCriticalMB) {
delegate_.SetTotalMemoryKB(1000 * kKBperMB);
monitor_.reset(new MemoryMonitorChromeOS(&delegate_));
EXPECT_EQ(0u, delegate_.calls());
delegate_.SetFreeMemoryKB(256 * kKBperMB, 128 * kKBperMB, 64 * kKBperMB,
32 * kKBperMB, 16 * kKBperMB);
EXPECT_EQ(318, monitor_->GetFreeMemoryUntilCriticalMB());
EXPECT_EQ(1U, delegate_.calls());
}
} // namespace content
......@@ -8,10 +8,6 @@
#include "content/browser/memory/memory_monitor.h"
#include "content/common/content_export.h"
namespace base {
struct SystemMemoryInfoKB;
} // namespace base
namespace content {
// A memory monitor for the Linux platform.
......
......@@ -1095,7 +1095,6 @@ test("content_unittests") {
"../browser/media/session/media_session_controller_unittest.cc",
"../browser/media/session/media_session_uma_helper_unittest.cc",
"../browser/memory/memory_coordinator_unittest.cc",
"../browser/memory/memory_monitor_linux_unittest.cc",
"../browser/memory/memory_monitor_win_unittest.cc",
"../browser/memory/test_memory_monitor.cc",
"../browser/memory/test_memory_monitor.h",
......@@ -1320,6 +1319,13 @@ test("content_unittests") {
"run_all_unittests.cc",
]
# ChromeOS also defines linux but their memory-monitors conflict.
if (is_chromeos) {
sources += [ "../browser/memory/memory_monitor_chromeos_unittest.cc" ]
} else {
sources += [ "../browser/memory/memory_monitor_linux_unittest.cc" ]
}
if (is_android || is_linux || is_mac || is_win) {
data = [
"$root_out_dir/content_shell.pak",
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment