0
Files
src/content/renderer/discardable_memory_utils.cc
Gordon Guan 96c21ee851 Integrate DiscardableMemory using MADV_FREE with renderer
Generalize usage of discardable memory in the renderer process to use
higher-level discardable memory primitives (DiscardableMemoryAllocator
and DiscardableMemory).

Implement a factory method to create a discardable memory allocator
implementation in renderer context depending on platform support.

Note that usage of MADV_FREE DiscardableMemory is gated by a feature
flag. Discardable memory should behave exactly as before this
change if the feature flag is not enabled.

Binary-Size: Unused ForTest method not being removed by linker
Bug: 1014513
Change-Id: I94afd2347a6ecbe254cd2d1a3ad667a6624293ac
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1850758
Commit-Queue: Gordon Guan <gordonguan@google.com>
Reviewed-by: Peng Huang <penghuang@chromium.org>
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Reviewed-by: Benoit L <lizeb@chromium.org>
Reviewed-by: Egor Pasko <pasko@chromium.org>
Cr-Commit-Position: refs/heads/master@{#713010}
2019-11-06 15:05:39 +00:00

77 lines
2.3 KiB
C++

// Copyright 2019 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 <utility>
#include "base/command_line.h"
#include "content/child/child_process.h"
#include "content/child/child_thread_impl.h"
#include "content/public/common/content_switches.h"
#include "content/renderer/discardable_memory_utils.h"
#if defined(OS_POSIX)
#include <sys/mman.h>
#include <sys/utsname.h>
#include "base/memory/madv_free_discardable_memory_allocator_posix.h"
#include "base/memory/madv_free_discardable_memory_posix.h"
#endif
#if defined(OS_ANDROID)
#include <third_party/ashmem/ashmem.h>
#endif
namespace content {
static const base::Feature kMadvFreeDiscardableMemoryFeature{
"MadvFreeDiscardableMemory", base::FEATURE_DISABLED_BY_DEFAULT};
namespace {
DiscardableMemoryBacking GetPlatformDiscardableMemoryBacking() {
#if defined(OS_ANDROID)
if (ashmem_device_is_supported())
return DiscardableMemoryBacking::kSharedMemory;
#endif
bool madv_free_supported = false;
#if defined(OS_POSIX)
madv_free_supported =
base::GetMadvFreeSupport() == base::MadvFreeSupport::kSupported;
#endif
if (base::FeatureList::IsEnabled(kMadvFreeDiscardableMemoryFeature) &&
madv_free_supported) {
return DiscardableMemoryBacking::kMadvFree;
}
return DiscardableMemoryBacking::kSharedMemory;
}
} // namespace
DiscardableMemoryBacking GetDiscardableMemoryBacking() {
static DiscardableMemoryBacking backing =
GetPlatformDiscardableMemoryBacking();
return backing;
}
std::unique_ptr<base::DiscardableMemoryAllocator>
CreateDiscardableMemoryAllocator() {
if (GetDiscardableMemoryBacking() == DiscardableMemoryBacking::kMadvFree) {
#if defined(OS_POSIX)
DVLOG(1) << "Using MADV_FREE for discardable memory";
return std::make_unique<base::MadvFreeDiscardableMemoryAllocatorPosix>();
#endif
}
DVLOG(1) << "Using shared memory for discardable memory";
mojo::PendingRemote<discardable_memory::mojom::DiscardableSharedMemoryManager>
manager_remote;
ChildThread::Get()->BindHostReceiver(
manager_remote.InitWithNewPipeAndPassReceiver());
return std::make_unique<
discardable_memory::ClientDiscardableSharedMemoryManager>(
std::move(manager_remote), ChildProcess::current()->io_task_runner());
}
} // namespace content