Move discardable memory to //components from //content
BUG=654678
Review-Url: https://codereview.chromium.org/2459733002
Cr-Commit-Position: refs/heads/master@{#429380}
diff --git a/components/BUILD.gn b/components/BUILD.gn
index 5f7df6e..debccc7 100644
--- a/components/BUILD.gn
+++ b/components/BUILD.gn
@@ -170,6 +170,8 @@
"//components/data_reduction_proxy/core/browser:unit_tests",
"//components/data_reduction_proxy/core/common:unit_tests",
"//components/data_use_measurement/content:unit_tests",
+ "//components/discardable_memory/common:unit_tests",
+ "//components/discardable_memory/service:unit_tests",
"//components/display_compositor:unit_tests",
"//components/dom_distiller/content/browser:unit_tests",
"//components/domain_reliability:unit_tests",
@@ -476,5 +478,10 @@
"//testing/perf",
"//url",
]
+
+ if (!is_ios) {
+ sources += [ "discardable_memory/common/discardable_shared_memory_heap_perftest.cc" ]
+ deps += [ "//components/discardable_memory/common" ]
+ }
}
}
diff --git a/components/discardable_memory/OWNERS b/components/discardable_memory/OWNERS
new file mode 100644
index 0000000..157836ab
--- /dev/null
+++ b/components/discardable_memory/OWNERS
@@ -0,0 +1 @@
[email protected]
diff --git a/components/discardable_memory/client/BUILD.gn b/components/discardable_memory/client/BUILD.gn
new file mode 100644
index 0000000..583a8ee
--- /dev/null
+++ b/components/discardable_memory/client/BUILD.gn
@@ -0,0 +1,21 @@
+# Copyright 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.
+
+import("//testing/test.gni")
+
+component("client") {
+ output_name = "discardable_memory_client"
+
+ defines = [ "DISCARDABLE_MEMORY_IMPLEMENTATION" ]
+
+ sources = [
+ "client_discardable_shared_memory_manager.cc",
+ "client_discardable_shared_memory_manager.h",
+ ]
+
+ deps = [
+ "//base",
+ "//components/discardable_memory/common",
+ ]
+}
diff --git a/content/child/child_discardable_shared_memory_manager.cc b/components/discardable_memory/client/client_discardable_shared_memory_manager.cc
similarity index 83%
rename from content/child/child_discardable_shared_memory_manager.cc
rename to components/discardable_memory/client/client_discardable_shared_memory_manager.cc
index 1d1aa4c..22239e1 100644
--- a/content/child/child_discardable_shared_memory_manager.cc
+++ b/components/discardable_memory/client/client_discardable_shared_memory_manager.cc
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "content/child/child_discardable_shared_memory_manager.h"
+#include "components/discardable_memory/client/client_discardable_shared_memory_manager.h"
#include <inttypes.h>
@@ -24,9 +24,8 @@
#include "base/threading/thread_task_runner_handle.h"
#include "base/trace_event/memory_dump_manager.h"
#include "base/trace_event/trace_event.h"
-#include "content/common/child_process_messages.h"
-namespace content {
+namespace discardable_memory {
namespace {
// Default allocation size.
@@ -37,7 +36,7 @@
class DiscardableMemoryImpl : public base::DiscardableMemory {
public:
- DiscardableMemoryImpl(ChildDiscardableSharedMemoryManager* manager,
+ DiscardableMemoryImpl(ClientDiscardableSharedMemoryManager* manager,
std::unique_ptr<DiscardableSharedMemoryHeap::Span> span)
: manager_(manager), span_(std::move(span)), is_locked_(true) {}
@@ -76,7 +75,7 @@
}
private:
- ChildDiscardableSharedMemoryManager* const manager_;
+ ClientDiscardableSharedMemoryManager* const manager_;
std::unique_ptr<DiscardableSharedMemoryHeap::Span> span_;
bool is_locked_;
@@ -84,22 +83,22 @@
};
void SendDeletedDiscardableSharedMemoryMessage(
- scoped_refptr<ThreadSafeSender> sender,
+ ClientDiscardableSharedMemoryManager::Delegate* delegate,
DiscardableSharedMemoryId id) {
- sender->Send(new ChildProcessHostMsg_DeletedDiscardableSharedMemory(id));
+ delegate->DeletedDiscardableSharedMemory(id);
}
} // namespace
-ChildDiscardableSharedMemoryManager::ChildDiscardableSharedMemoryManager(
- ThreadSafeSender* sender)
- : heap_(base::GetPageSize()), sender_(sender) {
+ClientDiscardableSharedMemoryManager::ClientDiscardableSharedMemoryManager(
+ Delegate* delegate)
+ : heap_(base::GetPageSize()), delegate_(delegate) {
base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
- this, "ChildDiscardableSharedMemoryManager",
+ this, "ClientDiscardableSharedMemoryManager",
base::ThreadTaskRunnerHandle::Get());
}
-ChildDiscardableSharedMemoryManager::~ChildDiscardableSharedMemoryManager() {
+ClientDiscardableSharedMemoryManager::~ClientDiscardableSharedMemoryManager() {
base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider(
this);
// TODO(reveman): Determine if this DCHECK can be enabled. crbug.com/430533
@@ -109,14 +108,15 @@
}
std::unique_ptr<base::DiscardableMemory>
-ChildDiscardableSharedMemoryManager::AllocateLockedDiscardableMemory(
+ClientDiscardableSharedMemoryManager::AllocateLockedDiscardableMemory(
size_t size) {
base::AutoLock lock(lock_);
DCHECK_NE(size, 0u);
+ auto size_in_kb = static_cast<base::HistogramBase::Sample>(size / 1024);
UMA_HISTOGRAM_CUSTOM_COUNTS("Memory.DiscardableAllocationSize",
- size / 1024, // In KB
+ size_in_kb, // In KB
1,
4 * 1024 * 1024, // 4 GB
50);
@@ -189,9 +189,10 @@
AllocateLockedDiscardableSharedMemory(allocation_size_in_bytes, new_id));
// Create span for allocated memory.
- std::unique_ptr<DiscardableSharedMemoryHeap::Span> new_span(heap_.Grow(
- std::move(shared_memory), allocation_size_in_bytes, new_id,
- base::Bind(&SendDeletedDiscardableSharedMemoryMessage, sender_, new_id)));
+ std::unique_ptr<DiscardableSharedMemoryHeap::Span> new_span(
+ heap_.Grow(std::move(shared_memory), allocation_size_in_bytes, new_id,
+ base::Bind(&SendDeletedDiscardableSharedMemoryMessage,
+ delegate_, new_id)));
new_span->set_is_locked(true);
// Unlock and insert any left over memory into free lists.
@@ -211,7 +212,7 @@
return base::MakeUnique<DiscardableMemoryImpl>(this, std::move(new_span));
}
-bool ChildDiscardableSharedMemoryManager::OnMemoryDump(
+bool ClientDiscardableSharedMemoryManager::OnMemoryDump(
const base::trace_event::MemoryDumpArgs& args,
base::trace_event::ProcessMemoryDump* pmd) {
base::AutoLock lock(lock_);
@@ -235,8 +236,8 @@
return heap_.OnMemoryDump(pmd);
}
-ChildDiscardableSharedMemoryManager::Statistics
-ChildDiscardableSharedMemoryManager::GetStatistics() const {
+ClientDiscardableSharedMemoryManager::Statistics
+ClientDiscardableSharedMemoryManager::GetStatistics() const {
base::AutoLock lock(lock_);
Statistics stats;
stats.total_size = heap_.GetSize();
@@ -244,7 +245,7 @@
return stats;
}
-void ChildDiscardableSharedMemoryManager::ReleaseFreeMemory() {
+void ClientDiscardableSharedMemoryManager::ReleaseFreeMemory() {
base::AutoLock lock(lock_);
size_t heap_size_prior_to_releasing_memory = heap_.GetSize();
@@ -257,7 +258,7 @@
MemoryUsageChanged(heap_.GetSize(), heap_.GetSizeOfFreeLists());
}
-bool ChildDiscardableSharedMemoryManager::LockSpan(
+bool ClientDiscardableSharedMemoryManager::LockSpan(
DiscardableSharedMemoryHeap::Span* span) {
base::AutoLock lock(lock_);
@@ -265,7 +266,7 @@
return false;
size_t offset = span->start() * base::GetPageSize() -
- reinterpret_cast<size_t>(span->shared_memory()->memory());
+ reinterpret_cast<size_t>(span->shared_memory()->memory());
size_t length = span->length() * base::GetPageSize();
switch (span->shared_memory()->Lock(offset, length)) {
@@ -284,20 +285,20 @@
return false;
}
-void ChildDiscardableSharedMemoryManager::UnlockSpan(
+void ClientDiscardableSharedMemoryManager::UnlockSpan(
DiscardableSharedMemoryHeap::Span* span) {
base::AutoLock lock(lock_);
DCHECK(span->shared_memory());
size_t offset = span->start() * base::GetPageSize() -
- reinterpret_cast<size_t>(span->shared_memory()->memory());
+ reinterpret_cast<size_t>(span->shared_memory()->memory());
size_t length = span->length() * base::GetPageSize();
span->set_is_locked(false);
return span->shared_memory()->Unlock(offset, length);
}
-void ChildDiscardableSharedMemoryManager::ReleaseSpan(
+void ClientDiscardableSharedMemoryManager::ReleaseSpan(
std::unique_ptr<DiscardableSharedMemoryHeap::Span> span) {
base::AutoLock lock(lock_);
@@ -312,7 +313,7 @@
}
base::trace_event::MemoryAllocatorDump*
-ChildDiscardableSharedMemoryManager::CreateMemoryAllocatorDump(
+ClientDiscardableSharedMemoryManager::CreateMemoryAllocatorDump(
DiscardableSharedMemoryHeap::Span* span,
const char* name,
base::trace_event::ProcessMemoryDump* pmd) const {
@@ -321,18 +322,16 @@
}
std::unique_ptr<base::DiscardableSharedMemory>
-ChildDiscardableSharedMemoryManager::AllocateLockedDiscardableSharedMemory(
+ClientDiscardableSharedMemoryManager::AllocateLockedDiscardableSharedMemory(
size_t size,
DiscardableSharedMemoryId id) {
TRACE_EVENT2("renderer",
- "ChildDiscardableSharedMemoryManager::"
+ "ClientDiscardableSharedMemoryManager::"
"AllocateLockedDiscardableSharedMemory",
"size", size, "id", id);
base::SharedMemoryHandle handle = base::SharedMemory::NULLHandle();
- sender_->Send(
- new ChildProcessHostMsg_SyncAllocateLockedDiscardableSharedMemory(
- size, id, &handle));
+ delegate_->AllocateLockedDiscardableSharedMemory(size, id, &handle);
std::unique_ptr<base::DiscardableSharedMemory> memory(
new base::DiscardableSharedMemory(handle));
if (!memory->Map(size))
@@ -340,7 +339,7 @@
return memory;
}
-void ChildDiscardableSharedMemoryManager::MemoryUsageChanged(
+void ClientDiscardableSharedMemoryManager::MemoryUsageChanged(
size_t new_bytes_total,
size_t new_bytes_free) const {
static const char kDiscardableMemoryAllocatedKey[] =
@@ -353,4 +352,4 @@
base::Uint64ToString(new_bytes_free));
}
-} // namespace content
+} // namespace discardable_memory
diff --git a/content/child/child_discardable_shared_memory_manager.h b/components/discardable_memory/client/client_discardable_shared_memory_manager.h
similarity index 61%
rename from content/child/child_discardable_shared_memory_manager.h
rename to components/discardable_memory/client/client_discardable_shared_memory_manager.h
index 9f83e1c1..76215bc 100644
--- a/content/child/child_discardable_shared_memory_manager.h
+++ b/components/discardable_memory/client/client_discardable_shared_memory_manager.h
@@ -2,31 +2,44 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#ifndef CONTENT_CHILD_CHILD_DISCARDABLE_SHARED_MEMORY_MANAGER_H_
-#define CONTENT_CHILD_CHILD_DISCARDABLE_SHARED_MEMORY_MANAGER_H_
+#ifndef COMPONENTS_DISCARDABLE_MEMORY_CLIENT_CLIENT_DISCARDABLE_SHARED_MEMORY_MANAGER_H_
+#define COMPONENTS_DISCARDABLE_MEMORY_CLIENT_CLIENT_DISCARDABLE_SHARED_MEMORY_MANAGER_H_
#include <stddef.h>
#include "base/macros.h"
#include "base/memory/discardable_memory_allocator.h"
#include "base/memory/ref_counted.h"
+#include "base/memory/shared_memory_handle.h"
#include "base/synchronization/lock.h"
#include "base/trace_event/memory_dump_provider.h"
-#include "content/child/thread_safe_sender.h"
-#include "content/common/content_export.h"
-#include "content/common/discardable_shared_memory_heap.h"
-#include "content/common/host_discardable_shared_memory_manager.h"
+#include "components/discardable_memory/common/discardable_memory_export.h"
+#include "components/discardable_memory/common/discardable_shared_memory_heap.h"
+#include "components/discardable_memory/common/discardable_shared_memory_id.h"
-namespace content {
+namespace discardable_memory {
// Implementation of DiscardableMemoryAllocator that allocates
// discardable memory segments through the browser process.
-class CONTENT_EXPORT ChildDiscardableSharedMemoryManager
+class DISCARDABLE_MEMORY_EXPORT ClientDiscardableSharedMemoryManager
: public base::DiscardableMemoryAllocator,
public base::trace_event::MemoryDumpProvider {
public:
- explicit ChildDiscardableSharedMemoryManager(ThreadSafeSender* sender);
- ~ChildDiscardableSharedMemoryManager() override;
+ class Delegate {
+ public:
+ virtual void AllocateLockedDiscardableSharedMemory(
+ size_t size,
+ DiscardableSharedMemoryId id,
+ base::SharedMemoryHandle* handle) = 0;
+ virtual void DeletedDiscardableSharedMemory(
+ DiscardableSharedMemoryId id) = 0;
+
+ protected:
+ virtual ~Delegate() {}
+ };
+
+ explicit ClientDiscardableSharedMemoryManager(Delegate* delegate);
+ ~ClientDiscardableSharedMemoryManager() override;
// Overridden from base::DiscardableMemoryAllocator:
std::unique_ptr<base::DiscardableMemory> AllocateLockedDiscardableMemory(
@@ -64,11 +77,11 @@
mutable base::Lock lock_;
DiscardableSharedMemoryHeap heap_;
- scoped_refptr<ThreadSafeSender> sender_;
+ Delegate* const delegate_;
- DISALLOW_COPY_AND_ASSIGN(ChildDiscardableSharedMemoryManager);
+ DISALLOW_COPY_AND_ASSIGN(ClientDiscardableSharedMemoryManager);
};
-} // namespace content
+} // namespace discardable_memory
-#endif // CONTENT_CHILD_CHILD_DISCARDABLE_SHARED_MEMORY_MANAGER_H_
+#endif // COMPONENTS_DISCARDABLE_MEMORY_CLIENT_CLIENT_DISCARDABLE_SHARED_MEMORY_MANAGER_H_
diff --git a/components/discardable_memory/common/BUILD.gn b/components/discardable_memory/common/BUILD.gn
new file mode 100644
index 0000000..9450c87
--- /dev/null
+++ b/components/discardable_memory/common/BUILD.gn
@@ -0,0 +1,36 @@
+# Copyright 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.
+
+import("//testing/test.gni")
+
+component("common") {
+ output_name = "discardable_memory_common"
+
+ defines = [ "DISCARDABLE_MEMORY_IMPLEMENTATION" ]
+
+ sources = [
+ "discardable_memory_export.h",
+ "discardable_shared_memory_heap.cc",
+ "discardable_shared_memory_heap.h",
+ "discardable_shared_memory_id.h",
+ ]
+
+ deps = [
+ "//base",
+ ]
+}
+
+source_set("unit_tests") {
+ testonly = true
+
+ sources = [
+ "discardable_shared_memory_heap_unittest.cc",
+ ]
+
+ deps = [
+ ":common",
+ "//base",
+ "//testing/gtest",
+ ]
+}
diff --git a/components/discardable_memory/common/discardable_memory_export.h b/components/discardable_memory/common/discardable_memory_export.h
new file mode 100644
index 0000000..32942538
--- /dev/null
+++ b/components/discardable_memory/common/discardable_memory_export.h
@@ -0,0 +1,29 @@
+// 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 COMPONENTS_DISCARDABLE_MEMORY_COMMON_DISCARDABLE_MEMORY_EXPORT_H_
+#define COMPONENTS_DISCARDABLE_MEMORY_COMMON_DISCARDABLE_MEMORY_EXPORT_H_
+
+#if defined(COMPONENT_BUILD)
+#if defined(WIN32)
+
+#if defined(DISCARDABLE_MEMORY_IMPLEMENTATION)
+#define DISCARDABLE_MEMORY_EXPORT __declspec(dllexport)
+#else
+#define DISCARDABLE_MEMORY_EXPORT __declspec(dllimport)
+#endif // defined(DISCARDABLE_MEMORY_IMPLEMENTATION)
+
+#else // defined(WIN32)
+#if defined(DISCARDABLE_MEMORY_IMPLEMENTATION)
+#define DISCARDABLE_MEMORY_EXPORT __attribute__((visibility("default")))
+#else
+#define DISCARDABLE_MEMORY_EXPORT
+#endif
+#endif
+
+#else // defined(COMPONENT_BUILD)
+#define DISCARDABLE_MEMORY_EXPORT
+#endif
+
+#endif // COMPONENTS_DISCARDABLE_MEMORY_COMMON_DISCARDABLE_MEMORY_EXPORT_H_
diff --git a/content/common/discardable_shared_memory_heap.cc b/components/discardable_memory/common/discardable_shared_memory_heap.cc
similarity index 96%
rename from content/common/discardable_shared_memory_heap.cc
rename to components/discardable_memory/common/discardable_shared_memory_heap.cc
index b6559af..12d8eb6c 100644
--- a/content/common/discardable_shared_memory_heap.cc
+++ b/components/discardable_memory/common/discardable_shared_memory_heap.cc
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "content/common/discardable_shared_memory_heap.h"
+#include "components/discardable_memory/common/discardable_shared_memory_heap.h"
#include <algorithm>
#include <utility>
@@ -14,7 +14,7 @@
#include "base/strings/stringprintf.h"
#include "base/trace_event/memory_dump_manager.h"
-namespace content {
+namespace discardable_memory {
namespace {
bool IsPowerOfTwo(size_t x) {
@@ -36,8 +36,7 @@
length_(length),
is_locked_(false) {}
-DiscardableSharedMemoryHeap::Span::~Span() {
-}
+DiscardableSharedMemoryHeap::Span::~Span() {}
DiscardableSharedMemoryHeap::ScopedMemorySegment::ScopedMemorySegment(
DiscardableSharedMemoryHeap* heap,
@@ -254,11 +253,10 @@
bool DiscardableSharedMemoryHeap::OnMemoryDump(
base::trace_event::ProcessMemoryDump* pmd) {
- std::for_each(
- memory_segments_.begin(), memory_segments_.end(),
- [pmd](const ScopedMemorySegment* segment) {
- segment->OnMemoryDump(pmd);
- });
+ std::for_each(memory_segments_.begin(), memory_segments_.end(),
+ [pmd](const ScopedMemorySegment* segment) {
+ segment->OnMemoryDump(pmd);
+ });
return true;
}
@@ -280,7 +278,7 @@
DiscardableSharedMemoryHeap::Carve(Span* span, size_t blocks) {
std::unique_ptr<Span> serving = RemoveFromFreeList(span);
- const int extra = serving->length_ - blocks;
+ const size_t extra = serving->length_ - blocks;
if (extra) {
std::unique_ptr<Span> leftover(
new Span(serving->shared_memory_, serving->start_ + blocks, extra));
@@ -418,7 +416,7 @@
locked_objects_size_in_bytes);
// Emit an ownership edge towards a global allocator dump node. This allows
- // to avoid double-counting segments when both browser and child process emit
+ // to avoid double-counting segments when both browser and client process emit
// them. In the special case of single-process-mode, this will be the only
// dumper active and the single ownership edge will become a no-op in the UI.
// The global dump is created as a weak dump so that the segment is removed if
@@ -438,7 +436,8 @@
allocated_objects_size_in_bytes);
// By creating an edge with a higher |importance| (w.r.t. browser-side dumps)
- // the tracing UI will account the effective size of the segment to the child.
+ // the tracing UI will account the effective size of the segment to the
+ // client.
const int kImportance = 2;
pmd->AddOwnershipEdge(segment_dump->guid(), shared_segment_guid, kImportance);
}
@@ -474,4 +473,4 @@
return (*it)->CreateMemoryAllocatorDump(span, block_size_, name, pmd);
}
-} // namespace content
+} // namespace discardable_memory
diff --git a/content/common/discardable_shared_memory_heap.h b/components/discardable_memory/common/discardable_shared_memory_heap.h
similarity index 91%
rename from content/common/discardable_shared_memory_heap.h
rename to components/discardable_memory/common/discardable_shared_memory_heap.h
index 6df1a16..e49ed14 100644
--- a/content/common/discardable_shared_memory_heap.h
+++ b/components/discardable_memory/common/discardable_shared_memory_heap.h
@@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#ifndef CONTENT_COMMON_DISCARDABLE_SHARED_MEMORY_HEAP_H_
-#define CONTENT_COMMON_DISCARDABLE_SHARED_MEMORY_HEAP_H_
+#ifndef COMPONENTS_DISCARDABLE_MEMORY_COMMON_DISCARDABLE_SHARED_MEMORY_HEAP_H_
+#define COMPONENTS_DISCARDABLE_MEMORY_COMMON_DISCARDABLE_SHARED_MEMORY_HEAP_H_
#include <stddef.h>
#include <stdint.h>
@@ -16,19 +16,19 @@
#include "base/macros.h"
#include "base/memory/scoped_vector.h"
#include "base/trace_event/process_memory_dump.h"
-#include "content/common/content_export.h"
+#include "components/discardable_memory/common/discardable_memory_export.h"
namespace base {
class DiscardableSharedMemory;
}
-namespace content {
+namespace discardable_memory {
// Implements a heap of discardable shared memory. An array of free lists
// is used to keep track of free blocks.
-class CONTENT_EXPORT DiscardableSharedMemoryHeap {
+class DISCARDABLE_MEMORY_EXPORT DiscardableSharedMemoryHeap {
public:
- class CONTENT_EXPORT Span : public base::LinkNode<Span> {
+ class DISCARDABLE_MEMORY_EXPORT Span : public base::LinkNode<Span> {
public:
~Span();
@@ -181,6 +181,6 @@
DISALLOW_COPY_AND_ASSIGN(DiscardableSharedMemoryHeap);
};
-} // namespace content
+} // namespace discardable_memory
-#endif // CONTENT_COMMON_DISCARDABLE_SHARED_MEMORY_HEAP_H_
+#endif // COMPONENTS_DISCARDABLE_MEMORY_COMMON_DISCARDABLE_SHARED_MEMORY_HEAP_H_
diff --git a/content/common/discardable_shared_memory_heap_perftest.cc b/components/discardable_memory/common/discardable_shared_memory_heap_perftest.cc
similarity index 94%
rename from content/common/discardable_shared_memory_heap_perftest.cc
rename to components/discardable_memory/common/discardable_shared_memory_heap_perftest.cc
index 8dc3d588..f4eb81e 100644
--- a/content/common/discardable_shared_memory_heap_perftest.cc
+++ b/components/discardable_memory/common/discardable_shared_memory_heap_perftest.cc
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "content/common/discardable_shared_memory_heap.h"
+#include "components/discardable_memory/common/discardable_shared_memory_heap.h"
#include <stddef.h>
#include <algorithm>
@@ -18,14 +18,13 @@
#include "testing/gtest/include/gtest/gtest.h"
#include "testing/perf/perf_test.h"
-namespace content {
+namespace discardable_memory {
namespace {
const int kTimeLimitMs = 2000;
const int kTimeCheckInterval = 8192;
-void NullTask() {
-}
+void NullTask() {}
TEST(DiscardableSharedMemoryHeapTest, SearchFreeLists) {
size_t block_size = base::GetPageSize();
@@ -98,4 +97,4 @@
}
} // namespace
-} // namespace content
+} // namespace discardable_memory
diff --git a/content/common/discardable_shared_memory_heap_unittest.cc b/components/discardable_memory/common/discardable_shared_memory_heap_unittest.cc
similarity index 98%
rename from content/common/discardable_shared_memory_heap_unittest.cc
rename to components/discardable_memory/common/discardable_shared_memory_heap_unittest.cc
index f391ff3..117cb9b5 100644
--- a/content/common/discardable_shared_memory_heap_unittest.cc
+++ b/components/discardable_memory/common/discardable_shared_memory_heap_unittest.cc
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "content/common/discardable_shared_memory_heap.h"
+#include "components/discardable_memory/common/discardable_shared_memory_heap.h"
#include <stddef.h>
#include <utility>
@@ -12,11 +12,10 @@
#include "base/process/process_metrics.h"
#include "testing/gtest/include/gtest/gtest.h"
-namespace content {
+namespace discardable_memory {
namespace {
-void NullTask() {
-}
+void NullTask() {}
TEST(DiscardableSharedMemoryHeapTest, Basic) {
size_t block_size = base::GetPageSize();
diff --git a/components/discardable_memory/common/discardable_shared_memory_id.h b/components/discardable_memory/common/discardable_shared_memory_id.h
new file mode 100644
index 0000000..53f98a5
--- /dev/null
+++ b/components/discardable_memory/common/discardable_shared_memory_id.h
@@ -0,0 +1,17 @@
+// Copyright 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 COMPONENTS_DISCARDABLE_MEMORY_COMMON_DISCARDABLE_SHARED_MEMORY_ID_H_
+#define COMPONENTS_DISCARDABLE_MEMORY_COMMON_DISCARDABLE_SHARED_MEMORY_ID_H_
+
+#include <stddef.h>
+#include <stdint.h>
+
+namespace discardable_memory {
+
+typedef int32_t DiscardableSharedMemoryId;
+
+} // namespace discardable_memory
+
+#endif // COMPONENTS_DISCARDABLE_MEMORY_COMMON_DISCARDABLE_SHARED_MEMORY_ID_H_
diff --git a/components/discardable_memory/service/BUILD.gn b/components/discardable_memory/service/BUILD.gn
new file mode 100644
index 0000000..4061d72
--- /dev/null
+++ b/components/discardable_memory/service/BUILD.gn
@@ -0,0 +1,35 @@
+# Copyright 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.
+
+import("//testing/test.gni")
+
+component("service") {
+ output_name = "discardable_memory_service"
+
+ defines = [ "DISCARDABLE_MEMORY_IMPLEMENTATION" ]
+
+ sources = [
+ "discardable_shared_memory_manager.cc",
+ "discardable_shared_memory_manager.h",
+ ]
+
+ deps = [
+ "//base",
+ "//components/discardable_memory/common",
+ ]
+}
+
+source_set("unit_tests") {
+ testonly = true
+
+ sources = [
+ "discardable_shared_memory_manager_unittest.cc",
+ ]
+
+ deps = [
+ ":service",
+ "//base",
+ "//testing/gtest",
+ ]
+}
diff --git a/content/common/host_discardable_shared_memory_manager.cc b/components/discardable_memory/service/discardable_shared_memory_manager.cc
similarity index 78%
rename from content/common/host_discardable_shared_memory_manager.cc
rename to components/discardable_memory/service/discardable_shared_memory_manager.cc
index e349fca..1036dc8 100644
--- a/content/common/host_discardable_shared_memory_manager.cc
+++ b/components/discardable_memory/service/discardable_shared_memory_manager.cc
@@ -2,7 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#include "content/common/host_discardable_shared_memory_manager.h"
+#include "components/discardable_memory/service/discardable_shared_memory_manager.h"
#include <algorithm>
#include <utility>
@@ -10,6 +10,7 @@
#include "base/atomic_sequence_num.h"
#include "base/bind.h"
#include "base/callback.h"
+#include "base/command_line.h"
#include "base/debug/crash_logging.h"
#include "base/lazy_instance.h"
#include "base/macros.h"
@@ -27,9 +28,7 @@
#include "base/trace_event/process_memory_dump.h"
#include "base/trace_event/trace_event.h"
#include "build/build_config.h"
-#include "content/common/child_process_host_impl.h"
-#include "content/common/discardable_shared_memory_heap.h"
-#include "content/public/common/child_process_host.h"
+#include "components/discardable_memory/common/discardable_shared_memory_heap.h"
#if defined(OS_LINUX)
#include "base/files/file_path.h"
@@ -37,9 +36,27 @@
#include "base/metrics/histogram_macros.h"
#endif
-namespace content {
+namespace discardable_memory {
namespace {
+const char kSingleProcess[] = "single-process";
+
+const int kInvalidUniqueClientID = -1;
+
+const uint64_t kBrowserTracingProcessId = std::numeric_limits<uint64_t>::max();
+
+uint64_t ClientProcessUniqueIdToTracingProcessId(int client_id) {
+ // TODO(penghuang): Move this function to right place.
+ // https://crbug.com/661257
+ if (base::CommandLine::ForCurrentProcess()->HasSwitch(kSingleProcess))
+ return kBrowserTracingProcessId;
+ // The hash value is incremented so that the tracing id is never equal to
+ // MemoryDumpManager::kInvalidTracingProcessId.
+ return static_cast<uint64_t>(base::Hash(
+ reinterpret_cast<const char*>(&client_id), sizeof(client_id))) +
+ 1;
+}
+
class DiscardableMemoryImpl : public base::DiscardableMemory {
public:
DiscardableMemoryImpl(
@@ -146,7 +163,7 @@
base::SysInfo::AmountOfPhysicalMemory() / 4);
}
-base::LazyInstance<HostDiscardableSharedMemoryManager>
+base::LazyInstance<DiscardableSharedMemoryManager>
g_discardable_shared_memory_manager = LAZY_INSTANCE_INITIALIZER;
const int kEnforceMemoryPolicyDelayMs = 1000;
@@ -156,19 +173,18 @@
} // namespace
-HostDiscardableSharedMemoryManager::MemorySegment::MemorySegment(
+DiscardableSharedMemoryManager::MemorySegment::MemorySegment(
std::unique_ptr<base::DiscardableSharedMemory> memory)
: memory_(std::move(memory)) {}
-HostDiscardableSharedMemoryManager::MemorySegment::~MemorySegment() {
-}
+DiscardableSharedMemoryManager::MemorySegment::~MemorySegment() {}
-HostDiscardableSharedMemoryManager::HostDiscardableSharedMemoryManager()
+DiscardableSharedMemoryManager::DiscardableSharedMemoryManager()
: default_memory_limit_(GetDefaultMemoryLimit()),
memory_limit_(default_memory_limit_),
bytes_allocated_(0),
memory_pressure_listener_(new base::MemoryPressureListener(
- base::Bind(&HostDiscardableSharedMemoryManager::OnMemoryPressure,
+ base::Bind(&DiscardableSharedMemoryManager::OnMemoryPressure,
base::Unretained(this)))),
// Current thread might not have a task runner in tests.
enforce_memory_policy_task_runner_(base::ThreadTaskRunnerHandle::Get()),
@@ -176,27 +192,25 @@
weak_ptr_factory_(this) {
DCHECK_NE(memory_limit_, 0u);
enforce_memory_policy_callback_ =
- base::Bind(&HostDiscardableSharedMemoryManager::EnforceMemoryPolicy,
+ base::Bind(&DiscardableSharedMemoryManager::EnforceMemoryPolicy,
weak_ptr_factory_.GetWeakPtr());
base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
- this, "HostDiscardableSharedMemoryManager",
+ this, "DiscardableSharedMemoryManager",
base::ThreadTaskRunnerHandle::Get());
base::MemoryCoordinatorClientRegistry::GetInstance()->Register(this);
}
-HostDiscardableSharedMemoryManager::~HostDiscardableSharedMemoryManager() {
+DiscardableSharedMemoryManager::~DiscardableSharedMemoryManager() {
base::trace_event::MemoryDumpManager::GetInstance()->UnregisterDumpProvider(
this);
}
-HostDiscardableSharedMemoryManager*
-HostDiscardableSharedMemoryManager::current() {
+DiscardableSharedMemoryManager* DiscardableSharedMemoryManager::current() {
return g_discardable_shared_memory_manager.Pointer();
}
std::unique_ptr<base::DiscardableMemory>
-HostDiscardableSharedMemoryManager::AllocateLockedDiscardableMemory(
- size_t size) {
+DiscardableSharedMemoryManager::AllocateLockedDiscardableMemory(size_t size) {
DCHECK_NE(size, 0u);
DiscardableSharedMemoryId new_id =
@@ -206,9 +220,8 @@
// Note: Use DiscardableSharedMemoryHeap for in-process allocation
// of discardable memory if the cost of each allocation is too high.
base::SharedMemoryHandle handle;
- AllocateLockedDiscardableSharedMemory(current_process_handle,
- ChildProcessHost::kInvalidUniqueID,
- size, new_id, &handle);
+ AllocateLockedDiscardableSharedMemory(
+ current_process_handle, kInvalidUniqueClientID, size, new_id, &handle);
std::unique_ptr<base::DiscardableSharedMemory> memory(
new base::DiscardableSharedMemory(handle));
if (!memory->Map(size))
@@ -218,11 +231,11 @@
return base::MakeUnique<DiscardableMemoryImpl>(
std::move(memory),
base::Bind(
- &HostDiscardableSharedMemoryManager::DeletedDiscardableSharedMemory,
- base::Unretained(this), new_id, ChildProcessHost::kInvalidUniqueID));
+ &DiscardableSharedMemoryManager::DeletedDiscardableSharedMemory,
+ base::Unretained(this), new_id, kInvalidUniqueClientID));
}
-bool HostDiscardableSharedMemoryManager::OnMemoryDump(
+bool DiscardableSharedMemoryManager::OnMemoryDump(
const base::trace_event::MemoryDumpArgs& args,
base::trace_event::ProcessMemoryDump* pmd) {
if (args.level_of_detail ==
@@ -236,10 +249,10 @@
}
base::AutoLock lock(lock_);
- for (const auto& process_entry : processes_) {
- const int child_process_id = process_entry.first;
- const MemorySegmentMap& process_segments = process_entry.second;
- for (const auto& segment_entry : process_segments) {
+ for (const auto& client_entry : clients_) {
+ const int client_id = client_entry.first;
+ const MemorySegmentMap& client_segments = client_entry.second;
+ for (const auto& segment_entry : client_segments) {
const int segment_id = segment_entry.first;
const MemorySegment* segment = segment_entry.second.get();
if (!segment->memory()->mapped_size())
@@ -247,7 +260,7 @@
// The "size" will be inherited form the shared global dump.
std::string dump_name = base::StringPrintf(
- "discardable/process_%x/segment_%d", child_process_id, segment_id);
+ "discardable/process_%x/segment_%d", client_id, segment_id);
base::trace_event::MemoryAllocatorDump* dump =
pmd->CreateAllocatorDump(dump_name);
@@ -261,16 +274,15 @@
segment->memory()->IsMemoryLocked() ? segment->memory()->mapped_size()
: 0u);
- // Create the cross-process ownership edge. If the child creates a
+ // Create the cross-process ownership edge. If the client creates a
// corresponding dump for the same segment, this will avoid to
// double-count them in tracing. If, instead, no other process will emit a
// dump with the same guid, the segment will be accounted to the browser.
- const uint64_t child_tracing_process_id =
- ChildProcessHostImpl::ChildProcessUniqueIdToTracingProcessId(
- child_process_id);
+ const uint64_t client_tracing_id =
+ ClientProcessUniqueIdToTracingProcessId(client_id);
base::trace_event::MemoryAllocatorDumpGuid shared_segment_guid =
DiscardableSharedMemoryHeap::GetSegmentGUIDForTracing(
- child_tracing_process_id, segment_id);
+ client_tracing_id, segment_id);
pmd->CreateSharedGlobalAllocatorDump(shared_segment_guid);
pmd->AddOwnershipEdge(dump->guid(), shared_segment_guid);
@@ -294,62 +306,62 @@
return true;
}
-void HostDiscardableSharedMemoryManager::
- AllocateLockedDiscardableSharedMemoryForChild(
+void DiscardableSharedMemoryManager::
+ AllocateLockedDiscardableSharedMemoryForClient(
base::ProcessHandle process_handle,
- int child_process_id,
+ int client_id,
size_t size,
DiscardableSharedMemoryId id,
base::SharedMemoryHandle* shared_memory_handle) {
- AllocateLockedDiscardableSharedMemory(process_handle, child_process_id, size,
- id, shared_memory_handle);
+ AllocateLockedDiscardableSharedMemory(process_handle, client_id, size, id,
+ shared_memory_handle);
}
-void HostDiscardableSharedMemoryManager::ChildDeletedDiscardableSharedMemory(
+void DiscardableSharedMemoryManager::ClientDeletedDiscardableSharedMemory(
DiscardableSharedMemoryId id,
- int child_process_id) {
- DeletedDiscardableSharedMemory(id, child_process_id);
+ int client_id) {
+ DeletedDiscardableSharedMemory(id, client_id);
}
-void HostDiscardableSharedMemoryManager::ProcessRemoved(int child_process_id) {
+void DiscardableSharedMemoryManager::ClientRemoved(int client_id) {
base::AutoLock lock(lock_);
- ProcessMap::iterator process_it = processes_.find(child_process_id);
- if (process_it == processes_.end())
+ auto it = clients_.find(client_id);
+ if (it == clients_.end())
return;
size_t bytes_allocated_before_releasing_memory = bytes_allocated_;
- for (auto& segment_it : process_it->second)
+ for (auto& segment_it : it->second)
ReleaseMemory(segment_it.second->memory());
- processes_.erase(process_it);
+ clients_.erase(it);
if (bytes_allocated_ != bytes_allocated_before_releasing_memory)
BytesAllocatedChanged(bytes_allocated_);
}
-void HostDiscardableSharedMemoryManager::SetMemoryLimit(size_t limit) {
+void DiscardableSharedMemoryManager::SetMemoryLimit(size_t limit) {
base::AutoLock lock(lock_);
memory_limit_ = limit;
ReduceMemoryUsageUntilWithinMemoryLimit();
}
-void HostDiscardableSharedMemoryManager::EnforceMemoryPolicy() {
+void DiscardableSharedMemoryManager::EnforceMemoryPolicy() {
base::AutoLock lock(lock_);
enforce_memory_policy_pending_ = false;
ReduceMemoryUsageUntilWithinMemoryLimit();
}
-size_t HostDiscardableSharedMemoryManager::GetBytesAllocated() {
+size_t DiscardableSharedMemoryManager::GetBytesAllocated() {
base::AutoLock lock(lock_);
return bytes_allocated_;
}
-void HostDiscardableSharedMemoryManager::OnMemoryStateChange(
+void DiscardableSharedMemoryManager::OnMemoryStateChange(
base::MemoryState state) {
switch (state) {
case base::MemoryState::NORMAL:
@@ -359,25 +371,25 @@
SetMemoryLimit(0);
break;
case base::MemoryState::SUSPENDED:
- // Note that SUSPENDED never occurs in the main browser process so far.
- // Fall through.
+ // Note that SUSPENDED never occurs in the main browser process so far.
+ // Fall through.
case base::MemoryState::UNKNOWN:
NOTREACHED();
break;
}
}
-void HostDiscardableSharedMemoryManager::AllocateLockedDiscardableSharedMemory(
+void DiscardableSharedMemoryManager::AllocateLockedDiscardableSharedMemory(
base::ProcessHandle process_handle,
- int client_process_id,
+ int client_id,
size_t size,
DiscardableSharedMemoryId id,
base::SharedMemoryHandle* shared_memory_handle) {
base::AutoLock lock(lock_);
// Make sure |id| is not already in use.
- MemorySegmentMap& process_segments = processes_[client_process_id];
- if (process_segments.find(id) != process_segments.end()) {
+ MemorySegmentMap& client_segments = clients_[client_id];
+ if (client_segments.find(id) != client_segments.end()) {
LOG(ERROR) << "Invalid discardable shared memory ID";
*shared_memory_handle = base::SharedMemory::NULLHandle();
return;
@@ -424,7 +436,7 @@
BytesAllocatedChanged(bytes_allocated_);
scoped_refptr<MemorySegment> segment(new MemorySegment(std::move(memory)));
- process_segments[id] = segment.get();
+ client_segments[id] = segment.get();
segments_.push_back(segment.get());
std::push_heap(segments_.begin(), segments_.end(), CompareMemoryUsageTime);
@@ -432,15 +444,15 @@
ScheduleEnforceMemoryPolicy();
}
-void HostDiscardableSharedMemoryManager::DeletedDiscardableSharedMemory(
+void DiscardableSharedMemoryManager::DeletedDiscardableSharedMemory(
DiscardableSharedMemoryId id,
- int client_process_id) {
+ int client_id) {
base::AutoLock lock(lock_);
- MemorySegmentMap& process_segments = processes_[client_process_id];
+ MemorySegmentMap& client_segments = clients_[client_id];
- MemorySegmentMap::iterator segment_it = process_segments.find(id);
- if (segment_it == process_segments.end()) {
+ MemorySegmentMap::iterator segment_it = client_segments.find(id);
+ if (segment_it == client_segments.end()) {
LOG(ERROR) << "Invalid discardable shared memory ID";
return;
}
@@ -449,13 +461,13 @@
ReleaseMemory(segment_it->second->memory());
- process_segments.erase(segment_it);
+ client_segments.erase(segment_it);
if (bytes_allocated_ != bytes_allocated_before_releasing_memory)
BytesAllocatedChanged(bytes_allocated_);
}
-void HostDiscardableSharedMemoryManager::OnMemoryPressure(
+void DiscardableSharedMemoryManager::OnMemoryPressure(
base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) {
base::AutoLock lock(lock_);
@@ -473,8 +485,7 @@
}
}
-void
-HostDiscardableSharedMemoryManager::ReduceMemoryUsageUntilWithinMemoryLimit() {
+void DiscardableSharedMemoryManager::ReduceMemoryUsageUntilWithinMemoryLimit() {
lock_.AssertAcquired();
if (bytes_allocated_ <= memory_limit_)
@@ -485,13 +496,12 @@
ScheduleEnforceMemoryPolicy();
}
-void HostDiscardableSharedMemoryManager::ReduceMemoryUsageUntilWithinLimit(
+void DiscardableSharedMemoryManager::ReduceMemoryUsageUntilWithinLimit(
size_t limit) {
TRACE_EVENT1("renderer_host",
- "HostDiscardableSharedMemoryManager::"
+ "DiscardableSharedMemoryManager::"
"ReduceMemoryUsageUntilWithinLimit",
- "bytes_allocated",
- bytes_allocated_);
+ "bytes_allocated", bytes_allocated_);
// Usage time of currently locked segments are updated to this time and
// we stop eviction attempts as soon as we come across a segment that we've
@@ -534,7 +544,7 @@
BytesAllocatedChanged(bytes_allocated_);
}
-void HostDiscardableSharedMemoryManager::ReleaseMemory(
+void DiscardableSharedMemoryManager::ReleaseMemory(
base::DiscardableSharedMemory* memory) {
lock_.AssertAcquired();
@@ -543,8 +553,8 @@
bytes_allocated_ -= size;
// This will unmap the memory segment and drop our reference. The result
- // is that the memory will be released to the OS if the child process is
- // no longer referencing it.
+ // is that the memory will be released to the OS if the client is no longer
+ // referencing it.
// Note: We intentionally leave the segment in the |segments| vector to
// avoid reconstructing the heap. The element will be removed from the heap
// when its last usage time is older than all other segments.
@@ -552,7 +562,7 @@
memory->Close();
}
-void HostDiscardableSharedMemoryManager::BytesAllocatedChanged(
+void DiscardableSharedMemoryManager::BytesAllocatedChanged(
size_t new_bytes_allocated) const {
static const char kTotalDiscardableMemoryAllocatedKey[] =
"total-discardable-memory-allocated";
@@ -560,11 +570,11 @@
base::Uint64ToString(new_bytes_allocated));
}
-base::Time HostDiscardableSharedMemoryManager::Now() const {
+base::Time DiscardableSharedMemoryManager::Now() const {
return base::Time::Now();
}
-void HostDiscardableSharedMemoryManager::ScheduleEnforceMemoryPolicy() {
+void DiscardableSharedMemoryManager::ScheduleEnforceMemoryPolicy() {
lock_.AssertAcquired();
if (enforce_memory_policy_pending_)
@@ -577,4 +587,4 @@
base::TimeDelta::FromMilliseconds(kEnforceMemoryPolicyDelayMs));
}
-} // namespace content
+} // namespace discardable_memory
diff --git a/content/common/host_discardable_shared_memory_manager.h b/components/discardable_memory/service/discardable_shared_memory_manager.h
similarity index 69%
rename from content/common/host_discardable_shared_memory_manager.h
rename to components/discardable_memory/service/discardable_shared_memory_manager.h
index 59a939a..7ccadf1 100644
--- a/content/common/host_discardable_shared_memory_manager.h
+++ b/components/discardable_memory/service/discardable_shared_memory_manager.h
@@ -2,8 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-#ifndef CONTENT_COMMON_HOST_DISCARDABLE_SHARED_MEMORY_MANAGER_H_
-#define CONTENT_COMMON_HOST_DISCARDABLE_SHARED_MEMORY_MANAGER_H_
+#ifndef COMPONENTS_DISCARDABLE_MEMORY_SERVICE_DISCARDABLE_SHARED_MEMORY_MANAGER_H_
+#define COMPONENTS_DISCARDABLE_MEMORY_SERVICE_DISCARDABLE_SHARED_MEMORY_MANAGER_H_
#include <stddef.h>
#include <stdint.h>
@@ -26,24 +26,26 @@
#include "base/synchronization/lock.h"
#include "base/threading/thread_task_runner_handle.h"
#include "base/trace_event/memory_dump_provider.h"
-#include "content/common/content_export.h"
+#include "components/discardable_memory/common/discardable_memory_export.h"
+#include "components/discardable_memory/common/discardable_shared_memory_id.h"
-namespace content {
-typedef int32_t DiscardableSharedMemoryId;
+namespace discardable_memory {
// Implementation of DiscardableMemoryAllocator that allocates and manages
-// discardable memory segments for the browser process and child processes.
+// discardable memory segments for the process which hosts this class, and
+// for remote processes which request discardable memory from this class via
+// IPC.
// This class is thread-safe and instances can safely be used on any thread.
-class CONTENT_EXPORT HostDiscardableSharedMemoryManager
+class DISCARDABLE_MEMORY_EXPORT DiscardableSharedMemoryManager
: public base::DiscardableMemoryAllocator,
public base::trace_event::MemoryDumpProvider,
public base::MemoryCoordinatorClient {
public:
- HostDiscardableSharedMemoryManager();
- ~HostDiscardableSharedMemoryManager() override;
+ DiscardableSharedMemoryManager();
+ ~DiscardableSharedMemoryManager() override;
// Returns a singleton instance.
- static HostDiscardableSharedMemoryManager* current();
+ static DiscardableSharedMemoryManager* current();
// Overridden from base::DiscardableMemoryAllocator:
std::unique_ptr<base::DiscardableMemory> AllocateLockedDiscardableMemory(
@@ -53,24 +55,25 @@
bool OnMemoryDump(const base::trace_event::MemoryDumpArgs& args,
base::trace_event::ProcessMemoryDump* pmd) override;
+ // TODO(penghuang): Get ride of the |process_handle| when we switch to mojo.
// This allocates a discardable memory segment for |process_handle|.
// A valid shared memory handle is returned on success.
- void AllocateLockedDiscardableSharedMemoryForChild(
+ void AllocateLockedDiscardableSharedMemoryForClient(
base::ProcessHandle process_handle,
- int child_process_id,
+ int client_id,
size_t size,
DiscardableSharedMemoryId id,
base::SharedMemoryHandle* shared_memory_handle);
- // Call this to notify the manager that child process associated with
- // |child_process_id| has deleted discardable memory segment with |id|.
- void ChildDeletedDiscardableSharedMemory(DiscardableSharedMemoryId id,
- int child_process_id);
+ // Call this to notify the manager that client process associated with
+ // |client_id| has deleted discardable memory segment with |id|.
+ void ClientDeletedDiscardableSharedMemory(DiscardableSharedMemoryId id,
+ int client_id);
- // Call this to notify the manager that child process associated with
- // |child_process_id| has been removed. The manager will use this to release
- // memory segments allocated for child process to the OS.
- void ProcessRemoved(int child_process_id);
+ // Call this to notify the manager that client associated with |client_id|
+ // has been removed. The manager will use this to release memory segments
+ // allocated for client to the OS.
+ void ClientRemoved(int client_id);
// The maximum number of bytes of memory that may be allocated. This will
// cause memory usage to be reduced if currently above |limit|.
@@ -108,14 +111,15 @@
// base::MemoryCoordinatorClient implementation:
void OnMemoryStateChange(base::MemoryState state) override;
+ // TODO(penghuang): Get ride of the |process_handle| when we switch to mojo.
void AllocateLockedDiscardableSharedMemory(
base::ProcessHandle process_handle,
- int client_process_id,
+ int client_id,
size_t size,
DiscardableSharedMemoryId id,
base::SharedMemoryHandle* shared_memory_handle);
void DeletedDiscardableSharedMemory(DiscardableSharedMemoryId id,
- int client_process_id);
+ int client_id);
void OnMemoryPressure(
base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level);
void ReduceMemoryUsageUntilWithinMemoryLimit();
@@ -129,9 +133,10 @@
base::Lock lock_;
typedef base::hash_map<DiscardableSharedMemoryId,
- scoped_refptr<MemorySegment>> MemorySegmentMap;
- typedef base::hash_map<int, MemorySegmentMap> ProcessMap;
- ProcessMap processes_;
+ scoped_refptr<MemorySegment>>
+ MemorySegmentMap;
+ typedef base::hash_map<int, MemorySegmentMap> ClientMap;
+ ClientMap clients_;
// Note: The elements in |segments_| are arranged in such a way that they form
// a heap. The LRU memory segment always first.
typedef std::vector<scoped_refptr<MemorySegment>> MemorySegmentVector;
@@ -144,11 +149,11 @@
enforce_memory_policy_task_runner_;
base::Closure enforce_memory_policy_callback_;
bool enforce_memory_policy_pending_;
- base::WeakPtrFactory<HostDiscardableSharedMemoryManager> weak_ptr_factory_;
+ base::WeakPtrFactory<DiscardableSharedMemoryManager> weak_ptr_factory_;
- DISALLOW_COPY_AND_ASSIGN(HostDiscardableSharedMemoryManager);
+ DISALLOW_COPY_AND_ASSIGN(DiscardableSharedMemoryManager);
};
-} // namespace content
+} // namespace discardable_memory
-#endif // CONTENT_COMMON_HOST_DISCARDABLE_SHARED_MEMORY_MANAGER_H_
+#endif // COMPONENTS_DISCARDABLE_MEMORY_SERVICE_DISCARDABLE_SHARED_MEMORY_MANAGER_H_
diff --git a/content/common/host_discardable_shared_memory_manager_unittest.cc b/components/discardable_memory/service/discardable_shared_memory_manager_unittest.cc
similarity index 72%
rename from content/common/host_discardable_shared_memory_manager_unittest.cc
rename to components/discardable_memory/service/discardable_shared_memory_manager_unittest.cc
index fcd39ba..f190736 100644
--- a/content/common/host_discardable_shared_memory_manager_unittest.cc
+++ b/components/discardable_memory/service/discardable_shared_memory_manager_unittest.cc
@@ -1,20 +1,21 @@
// Copyright 2014 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/common/host_discardable_shared_memory_manager.h"
+#include "components/discardable_memory/service/discardable_shared_memory_manager.h"
#include <stddef.h>
#include <stdint.h>
#include <string.h>
+#include "base/message_loop/message_loop.h"
#include "base/threading/simple_thread.h"
-#include "content/public/common/child_process_host.h"
#include "testing/gtest/include/gtest/gtest.h"
-namespace content {
+namespace discardable_memory {
namespace {
+const int kInvalidUniqueID = -1;
+
class TestDiscardableSharedMemory : public base::DiscardableSharedMemory {
public:
TestDiscardableSharedMemory() {}
@@ -31,10 +32,10 @@
base::Time now_;
};
-class TestHostDiscardableSharedMemoryManager
- : public HostDiscardableSharedMemoryManager {
+class TestDiscardableSharedMemoryManager
+ : public DiscardableSharedMemoryManager {
public:
- TestHostDiscardableSharedMemoryManager()
+ TestDiscardableSharedMemoryManager()
: enforce_memory_policy_pending_(false) {}
void SetNow(base::Time now) { now_ = now; }
@@ -47,7 +48,7 @@
}
private:
- // Overriden from HostDiscardableSharedMemoryManager:
+ // Overriden from DiscardableSharedMemoryManager:
base::Time Now() const override { return now_; }
void ScheduleEnforceMemoryPolicy() override {
enforce_memory_policy_pending_ = true;
@@ -57,27 +58,27 @@
bool enforce_memory_policy_pending_;
};
-class HostDiscardableSharedMemoryManagerTest : public testing::Test {
+class DiscardableSharedMemoryManagerTest : public testing::Test {
protected:
// Overridden from testing::Test:
void SetUp() override {
- manager_.reset(new TestHostDiscardableSharedMemoryManager);
+ manager_.reset(new TestDiscardableSharedMemoryManager);
}
- // HostDiscardableSharedMemoryManager requires a message loop.
+ // DiscardableSharedMemoryManager requires a message loop.
base::MessageLoop message_loop_;
- std::unique_ptr<TestHostDiscardableSharedMemoryManager> manager_;
+ std::unique_ptr<TestDiscardableSharedMemoryManager> manager_;
};
-TEST_F(HostDiscardableSharedMemoryManagerTest, AllocateForChild) {
+TEST_F(DiscardableSharedMemoryManagerTest, AllocateForClient) {
const int kDataSize = 1024;
uint8_t data[kDataSize];
memset(data, 0x80, kDataSize);
base::SharedMemoryHandle shared_handle;
- manager_->AllocateLockedDiscardableSharedMemoryForChild(
- base::GetCurrentProcessHandle(), ChildProcessHost::kInvalidUniqueID,
- kDataSize, 0, &shared_handle);
+ manager_->AllocateLockedDiscardableSharedMemoryForClient(
+ base::GetCurrentProcessHandle(), kInvalidUniqueID, kDataSize, 0,
+ &shared_handle);
ASSERT_TRUE(base::SharedMemory::IsHandleValid(shared_handle));
TestDiscardableSharedMemory memory(shared_handle);
@@ -93,13 +94,13 @@
memory.Unlock(0, 0);
}
-TEST_F(HostDiscardableSharedMemoryManagerTest, Purge) {
+TEST_F(DiscardableSharedMemoryManagerTest, Purge) {
const int kDataSize = 1024;
base::SharedMemoryHandle shared_handle1;
- manager_->AllocateLockedDiscardableSharedMemoryForChild(
- base::GetCurrentProcessHandle(), ChildProcessHost::kInvalidUniqueID,
- kDataSize, 1, &shared_handle1);
+ manager_->AllocateLockedDiscardableSharedMemoryForClient(
+ base::GetCurrentProcessHandle(), kInvalidUniqueID, kDataSize, 1,
+ &shared_handle1);
ASSERT_TRUE(base::SharedMemory::IsHandleValid(shared_handle1));
TestDiscardableSharedMemory memory1(shared_handle1);
@@ -107,9 +108,9 @@
ASSERT_TRUE(rv);
base::SharedMemoryHandle shared_handle2;
- manager_->AllocateLockedDiscardableSharedMemoryForChild(
- base::GetCurrentProcessHandle(), ChildProcessHost::kInvalidUniqueID,
- kDataSize, 2, &shared_handle2);
+ manager_->AllocateLockedDiscardableSharedMemoryForClient(
+ base::GetCurrentProcessHandle(), kInvalidUniqueID, kDataSize, 2,
+ &shared_handle2);
ASSERT_TRUE(base::SharedMemory::IsHandleValid(shared_handle2));
TestDiscardableSharedMemory memory2(shared_handle2);
@@ -159,13 +160,13 @@
EXPECT_EQ(base::DiscardableSharedMemory::SUCCESS, lock_rv);
}
-TEST_F(HostDiscardableSharedMemoryManagerTest, EnforceMemoryPolicy) {
+TEST_F(DiscardableSharedMemoryManagerTest, EnforceMemoryPolicy) {
const int kDataSize = 1024;
base::SharedMemoryHandle shared_handle;
- manager_->AllocateLockedDiscardableSharedMemoryForChild(
- base::GetCurrentProcessHandle(), ChildProcessHost::kInvalidUniqueID,
- kDataSize, 0, &shared_handle);
+ manager_->AllocateLockedDiscardableSharedMemoryForClient(
+ base::GetCurrentProcessHandle(), kInvalidUniqueID, kDataSize, 0,
+ &shared_handle);
ASSERT_TRUE(base::SharedMemory::IsHandleValid(shared_handle));
TestDiscardableSharedMemory memory(shared_handle);
@@ -197,14 +198,14 @@
EXPECT_EQ(base::DiscardableSharedMemory::FAILED, memory.Lock(0, 0));
}
-TEST_F(HostDiscardableSharedMemoryManagerTest,
+TEST_F(DiscardableSharedMemoryManagerTest,
ReduceMemoryAfterSegmentHasBeenDeleted) {
const int kDataSize = 1024;
base::SharedMemoryHandle shared_handle1;
- manager_->AllocateLockedDiscardableSharedMemoryForChild(
- base::GetCurrentProcessHandle(), ChildProcessHost::kInvalidUniqueID,
- kDataSize, 1, &shared_handle1);
+ manager_->AllocateLockedDiscardableSharedMemoryForClient(
+ base::GetCurrentProcessHandle(), kInvalidUniqueID, kDataSize, 1,
+ &shared_handle1);
ASSERT_TRUE(base::SharedMemory::IsHandleValid(shared_handle1));
TestDiscardableSharedMemory memory1(shared_handle1);
@@ -212,9 +213,9 @@
ASSERT_TRUE(rv);
base::SharedMemoryHandle shared_handle2;
- manager_->AllocateLockedDiscardableSharedMemoryForChild(
- base::GetCurrentProcessHandle(), ChildProcessHost::kInvalidUniqueID,
- kDataSize, 2, &shared_handle2);
+ manager_->AllocateLockedDiscardableSharedMemoryForClient(
+ base::GetCurrentProcessHandle(), kInvalidUniqueID, kDataSize, 2,
+ &shared_handle2);
ASSERT_TRUE(base::SharedMemory::IsHandleValid(shared_handle2));
TestDiscardableSharedMemory memory2(shared_handle2);
@@ -226,8 +227,7 @@
memory1.Unlock(0, 0);
memory1.Unmap();
memory1.Close();
- manager_->ChildDeletedDiscardableSharedMemory(
- 1, ChildProcessHost::kInvalidUniqueID);
+ manager_->ClientDeletedDiscardableSharedMemory(1, kInvalidUniqueID);
// Make sure the manager is able to reduce memory after the segment 1 was
// deleted.
@@ -239,41 +239,38 @@
memory2.Unlock(0, 0);
}
-class HostDiscardableSharedMemoryManagerScheduleEnforceMemoryPolicyTest
+class DiscardableSharedMemoryManagerScheduleEnforceMemoryPolicyTest
: public testing::Test {
protected:
// Overridden from testing::Test:
- void SetUp() override {
- manager_.reset(new HostDiscardableSharedMemoryManager);
- }
+ void SetUp() override { manager_.reset(new DiscardableSharedMemoryManager); }
- // HostDiscardableSharedMemoryManager requires a message loop.
+ // DiscardableSharedMemoryManager requires a message loop.
base::MessageLoop message_loop_;
- std::unique_ptr<HostDiscardableSharedMemoryManager> manager_;
+ std::unique_ptr<DiscardableSharedMemoryManager> manager_;
};
class SetMemoryLimitRunner : public base::DelegateSimpleThread::Delegate {
public:
- SetMemoryLimitRunner(HostDiscardableSharedMemoryManager* manager,
- size_t limit)
+ SetMemoryLimitRunner(DiscardableSharedMemoryManager* manager, size_t limit)
: manager_(manager), limit_(limit) {}
~SetMemoryLimitRunner() override {}
void Run() override { manager_->SetMemoryLimit(limit_); }
private:
- HostDiscardableSharedMemoryManager* const manager_;
+ DiscardableSharedMemoryManager* const manager_;
const size_t limit_;
};
-TEST_F(HostDiscardableSharedMemoryManagerScheduleEnforceMemoryPolicyTest,
+TEST_F(DiscardableSharedMemoryManagerScheduleEnforceMemoryPolicyTest,
SetMemoryLimitOnSimpleThread) {
const int kDataSize = 1024;
base::SharedMemoryHandle shared_handle;
- manager_->AllocateLockedDiscardableSharedMemoryForChild(
- base::GetCurrentProcessHandle(), ChildProcessHost::kInvalidUniqueID,
- kDataSize, 0, &shared_handle);
+ manager_->AllocateLockedDiscardableSharedMemoryForClient(
+ base::GetCurrentProcessHandle(), kInvalidUniqueID, kDataSize, 0,
+ &shared_handle);
ASSERT_TRUE(base::SharedMemory::IsHandleValid(shared_handle));
// Set the memory limit to a value that will require EnforceMemoryPolicy()
@@ -285,4 +282,4 @@
}
} // namespace
-} // namespace content
+} // namespace discardable_memory
diff --git a/content/browser/BUILD.gn b/content/browser/BUILD.gn
index a90fafd..9a4898e4 100644
--- a/content/browser/BUILD.gn
+++ b/content/browser/BUILD.gn
@@ -34,6 +34,8 @@
"//base/third_party/dynamic_annotations",
"//cc",
"//cc/surfaces",
+ "//components/discardable_memory/common",
+ "//components/discardable_memory/service",
"//components/display_compositor",
"//components/filesystem:lib",
"//components/leveldb:lib",
diff --git a/content/browser/DEPS b/content/browser/DEPS
index 52cd194..7b94828 100644
--- a/content/browser/DEPS
+++ b/content/browser/DEPS
@@ -1,6 +1,8 @@
include_rules = [
# Allow inclusion of specific components that we depend on.
# See comment in content/DEPS for which components are allowed.
+ "+components/discardable_memory/common",
+ "+components/discardable_memory/service",
"+components/filesystem",
"+components/leveldb",
"+components/link_header_util",
diff --git a/content/browser/browser_main_loop.cc b/content/browser/browser_main_loop.cc
index b6524087..b3fd63a 100644
--- a/content/browser/browser_main_loop.cc
+++ b/content/browser/browser_main_loop.cc
@@ -37,6 +37,7 @@
#include "base/trace_event/memory_dump_manager.h"
#include "base/trace_event/trace_event.h"
#include "build/build_config.h"
+#include "components/discardable_memory/service/discardable_shared_memory_manager.h"
#include "components/tracing/browser/trace_config_file.h"
#include "components/tracing/common/process_metrics_memory_dump_provider.h"
#include "components/tracing/common/trace_to_console.h"
@@ -68,7 +69,6 @@
#include "content/browser/webui/content_web_ui_controller_factory.h"
#include "content/browser/webui/url_data_manager.h"
#include "content/common/content_switches_internal.h"
-#include "content/common/host_discardable_shared_memory_manager.h"
#include "content/common/host_shared_bitmap_manager.h"
#include "content/common/service_manager/service_manager_connection_impl.h"
#include "content/public/browser/browser_main_parts.h"
@@ -634,7 +634,7 @@
// in-process Android WebView. crbug.com/503724 tracks proper fix.
if (!parsed_command_line_.HasSwitch(switches::kSingleProcess)) {
base::DiscardableMemoryAllocator::SetInstance(
- HostDiscardableSharedMemoryManager::current());
+ discardable_memory::DiscardableSharedMemoryManager::current());
}
if (parts_)
diff --git a/content/browser/renderer_host/render_message_filter.cc b/content/browser/renderer_host/render_message_filter.cc
index 897ae25..ebade5ff 100644
--- a/content/browser/renderer_host/render_message_filter.cc
+++ b/content/browser/renderer_host/render_message_filter.cc
@@ -162,7 +162,7 @@
BrowserGpuMemoryBufferManager::current();
if (gpu_memory_buffer_manager)
gpu_memory_buffer_manager->ProcessRemoved(PeerHandle(), render_process_id_);
- HostDiscardableSharedMemoryManager::current()->ProcessRemoved(
+ discardable_memory::DiscardableSharedMemoryManager::current()->ClientRemoved(
render_process_id_);
}
@@ -392,11 +392,11 @@
void RenderMessageFilter::AllocateLockedDiscardableSharedMemoryOnFileThread(
uint32_t size,
- DiscardableSharedMemoryId id,
+ discardable_memory::DiscardableSharedMemoryId id,
IPC::Message* reply_msg) {
base::SharedMemoryHandle handle;
- HostDiscardableSharedMemoryManager::current()
- ->AllocateLockedDiscardableSharedMemoryForChild(
+ discardable_memory::DiscardableSharedMemoryManager::current()
+ ->AllocateLockedDiscardableSharedMemoryForClient(
PeerHandle(), render_process_id_, size, id, &handle);
ChildProcessHostMsg_SyncAllocateLockedDiscardableSharedMemory::
WriteReplyParams(reply_msg, handle);
@@ -405,7 +405,7 @@
void RenderMessageFilter::OnAllocateLockedDiscardableSharedMemory(
uint32_t size,
- DiscardableSharedMemoryId id,
+ discardable_memory::DiscardableSharedMemoryId id,
IPC::Message* reply_msg) {
BrowserThread::PostTask(
BrowserThread::FILE_USER_BLOCKING, FROM_HERE,
@@ -415,13 +415,13 @@
}
void RenderMessageFilter::DeletedDiscardableSharedMemoryOnFileThread(
- DiscardableSharedMemoryId id) {
- HostDiscardableSharedMemoryManager::current()
- ->ChildDeletedDiscardableSharedMemory(id, render_process_id_);
+ discardable_memory::DiscardableSharedMemoryId id) {
+ discardable_memory::DiscardableSharedMemoryManager::current()
+ ->ClientDeletedDiscardableSharedMemory(id, render_process_id_);
}
void RenderMessageFilter::OnDeletedDiscardableSharedMemory(
- DiscardableSharedMemoryId id) {
+ discardable_memory::DiscardableSharedMemoryId id) {
BrowserThread::PostTask(
BrowserThread::FILE_USER_BLOCKING, FROM_HERE,
base::Bind(
diff --git a/content/browser/renderer_host/render_message_filter.h b/content/browser/renderer_host/render_message_filter.h
index 098a4d9..46e17ef 100644
--- a/content/browser/renderer_host/render_message_filter.h
+++ b/content/browser/renderer_host/render_message_filter.h
@@ -20,8 +20,8 @@
#include "base/strings/string16.h"
#include "build/build_config.h"
#include "cc/resources/shared_bitmap_manager.h"
+#include "components/discardable_memory/service/discardable_shared_memory_manager.h"
#include "content/common/cache_storage/cache_storage_types.h"
-#include "content/common/host_discardable_shared_memory_manager.h"
#include "content/common/host_shared_bitmap_manager.h"
#include "content/common/render_message_filter.mojom.h"
#include "content/public/browser/browser_associated_interface.h"
@@ -167,13 +167,16 @@
// Browser side discardable shared memory allocation.
void AllocateLockedDiscardableSharedMemoryOnFileThread(
uint32_t size,
- DiscardableSharedMemoryId id,
+ discardable_memory::DiscardableSharedMemoryId id,
IPC::Message* reply_message);
- void OnAllocateLockedDiscardableSharedMemory(uint32_t size,
- DiscardableSharedMemoryId id,
- IPC::Message* reply_message);
- void DeletedDiscardableSharedMemoryOnFileThread(DiscardableSharedMemoryId id);
- void OnDeletedDiscardableSharedMemory(DiscardableSharedMemoryId id);
+ void OnAllocateLockedDiscardableSharedMemory(
+ uint32_t size,
+ discardable_memory::DiscardableSharedMemoryId id,
+ IPC::Message* reply_message);
+ void DeletedDiscardableSharedMemoryOnFileThread(
+ discardable_memory::DiscardableSharedMemoryId id);
+ void OnDeletedDiscardableSharedMemory(
+ discardable_memory::DiscardableSharedMemoryId id);
#if defined(OS_LINUX)
void SetThreadPriorityOnFileThread(base::PlatformThreadId ns_tid,
diff --git a/content/child/BUILD.gn b/content/child/BUILD.gn
index 2102ac61..6e79326 100644
--- a/content/child/BUILD.gn
+++ b/content/child/BUILD.gn
@@ -55,8 +55,6 @@
"blob_storage/webblobregistry_impl.h",
"browser_font_resource_trusted.cc",
"browser_font_resource_trusted.h",
- "child_discardable_shared_memory_manager.cc",
- "child_discardable_shared_memory_manager.h",
"child_gpu_memory_buffer_manager.cc",
"child_gpu_memory_buffer_manager.h",
"child_histogram_message_filter.cc",
@@ -226,6 +224,7 @@
deps = [
"//base",
+ "//components/discardable_memory/client",
"//components/mime_util",
"//components/tracing",
"//components/tracing:startup_tracing",
diff --git a/content/child/DEPS b/content/child/DEPS
index 355badd..c122594 100644
--- a/content/child/DEPS
+++ b/content/child/DEPS
@@ -1,6 +1,8 @@
include_rules = [
# Allow inclusion of specific components that we depend on.
# See comment in content/DEPS for which components are allowed.
+ "+components/discardable_memory/client",
+ "+components/discardable_memory/common",
"+components/mime_util",
"+components/scheduler/child",
"+components/scheduler/common",
@@ -14,3 +16,9 @@
"+services/service_manager",
"+v8/include/v8.h"
]
+
+specific_include_rules = {
+ "child_thread_impl_browsertest\.*": [
+ "+components/discardable_memory/service",
+ ],
+}
diff --git a/content/child/child_thread_impl.cc b/content/child/child_thread_impl.cc
index 308da5ed..927427d 100644
--- a/content/child/child_thread_impl.cc
+++ b/content/child/child_thread_impl.cc
@@ -32,8 +32,8 @@
#include "base/timer/elapsed_timer.h"
#include "base/tracked_objects.h"
#include "build/build_config.h"
+#include "components/discardable_memory/client/client_discardable_shared_memory_manager.h"
#include "components/tracing/child/child_trace_message_filter.h"
-#include "content/child/child_discardable_shared_memory_manager.h"
#include "content/child/child_histogram_message_filter.h"
#include "content/child/child_process.h"
#include "content/child/child_resource_message_filter.h"
@@ -354,6 +354,33 @@
return handled;
}
+class ChildThreadImpl::ClientDiscardableSharedMemoryManagerDelegate
+ : public discardable_memory::ClientDiscardableSharedMemoryManager::
+ Delegate {
+ public:
+ explicit ClientDiscardableSharedMemoryManagerDelegate(
+ scoped_refptr<ThreadSafeSender> sender)
+ : sender_(sender) {}
+ ~ClientDiscardableSharedMemoryManagerDelegate() override {}
+
+ void AllocateLockedDiscardableSharedMemory(
+ size_t size,
+ discardable_memory::DiscardableSharedMemoryId id,
+ base::SharedMemoryHandle* handle) override {
+ sender_->Send(
+ new ChildProcessHostMsg_SyncAllocateLockedDiscardableSharedMemory(
+ size, id, handle));
+ }
+
+ void DeletedDiscardableSharedMemory(
+ discardable_memory::DiscardableSharedMemoryId id) override {
+ sender_->Send(new ChildProcessHostMsg_DeletedDiscardableSharedMemory(id));
+ }
+
+ private:
+ scoped_refptr<ThreadSafeSender> sender_;
+};
+
ChildThreadImpl::ChildThreadImpl()
: route_provider_binding_(this),
associated_interface_provider_bindings_(
@@ -564,8 +591,12 @@
shared_bitmap_manager_.reset(
new ChildSharedBitmapManager(thread_safe_sender()));
- discardable_shared_memory_manager_.reset(
- new ChildDiscardableSharedMemoryManager(thread_safe_sender()));
+ client_discardable_shared_memory_manager_delegate_ =
+ base::MakeUnique<ClientDiscardableSharedMemoryManagerDelegate>(
+ thread_safe_sender());
+ discardable_shared_memory_manager_ = base::MakeUnique<
+ discardable_memory::ClientDiscardableSharedMemoryManager>(
+ client_discardable_shared_memory_manager_delegate_.get());
}
ChildThreadImpl::~ChildThreadImpl() {
diff --git a/content/child/child_thread_impl.h b/content/child/child_thread_impl.h
index 590c1ff3..a5bac15 100644
--- a/content/child/child_thread_impl.h
+++ b/content/child/child_thread_impl.h
@@ -52,9 +52,12 @@
class WebFrame;
} // namespace blink
+namespace discardable_memory {
+class ClientDiscardableSharedMemoryManager;
+} // namespace discardable_memory
+
namespace content {
class ChildMessageFilter;
-class ChildDiscardableSharedMemoryManager;
class ChildHistogramMessageFilter;
class ChildResourceMessageFilter;
class ChildSharedBitmapManager;
@@ -142,8 +145,8 @@
return shared_bitmap_manager_.get();
}
- ChildDiscardableSharedMemoryManager* discardable_shared_memory_manager()
- const {
+ discardable_memory::ClientDiscardableSharedMemoryManager*
+ discardable_shared_memory_manager() const {
return discardable_shared_memory_manager_.get();
}
@@ -244,6 +247,8 @@
IPC::Sender* const sender_;
};
+ class ClientDiscardableSharedMemoryManagerDelegate;
+
void Init(const Options& options);
// We create the channel first without connecting it so we can add filters
@@ -331,9 +336,12 @@
std::unique_ptr<ChildSharedBitmapManager> shared_bitmap_manager_;
- std::unique_ptr<ChildDiscardableSharedMemoryManager>
+ std::unique_ptr<discardable_memory::ClientDiscardableSharedMemoryManager>
discardable_shared_memory_manager_;
+ std::unique_ptr<ClientDiscardableSharedMemoryManagerDelegate>
+ client_discardable_shared_memory_manager_delegate_;
+
std::unique_ptr<base::PowerMonitor> power_monitor_;
scoped_refptr<base::SequencedTaskRunner> browser_process_io_runner_;
diff --git a/content/child/child_thread_impl_browsertest.cc b/content/child/child_thread_impl_browsertest.cc
index 05d3244..ab21ddd4 100644
--- a/content/child/child_thread_impl_browsertest.cc
+++ b/content/child/child_thread_impl_browsertest.cc
@@ -13,9 +13,9 @@
#include "base/memory/discardable_memory.h"
#include "base/memory/scoped_vector.h"
#include "base/time/time.h"
-#include "content/child/child_discardable_shared_memory_manager.h"
+#include "components/discardable_memory/client/client_discardable_shared_memory_manager.h"
+#include "components/discardable_memory/service/discardable_shared_memory_manager.h"
#include "content/child/child_gpu_memory_buffer_manager.h"
-#include "content/common/host_discardable_shared_memory_manager.h"
#include "content/public/common/content_switches.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
@@ -43,7 +43,7 @@
base::Unretained(this)));
}
- ChildDiscardableSharedMemoryManager*
+ discardable_memory::ClientDiscardableSharedMemoryManager*
child_discardable_shared_memory_manager() {
return child_discardable_shared_memory_manager_;
}
@@ -54,7 +54,8 @@
ChildThreadImpl::current()->discardable_shared_memory_manager();
}
- ChildDiscardableSharedMemoryManager* child_discardable_shared_memory_manager_;
+ discardable_memory::ClientDiscardableSharedMemoryManager*
+ child_discardable_shared_memory_manager_;
};
IN_PROC_BROWSER_TEST_F(ChildThreadImplBrowserTest, LockDiscardableMemory) {
@@ -71,7 +72,8 @@
memory->Unlock();
// Purge all unlocked memory.
- HostDiscardableSharedMemoryManager::current()->SetMemoryLimit(0);
+ discardable_memory::DiscardableSharedMemoryManager::current()->SetMemoryLimit(
+ 0);
// Should fail as memory should have been purged.
EXPECT_FALSE(memory->Lock());
@@ -106,7 +108,8 @@
EXPECT_TRUE(memory);
memory.reset();
- EXPECT_GE(HostDiscardableSharedMemoryManager::current()->GetBytesAllocated(),
+ EXPECT_GE(discardable_memory::DiscardableSharedMemoryManager::current()
+ ->GetBytesAllocated(),
kSize);
child_discardable_shared_memory_manager()->ReleaseFreeMemory();
@@ -115,7 +118,8 @@
base::TimeTicks end =
base::TimeTicks::Now() + base::TimeDelta::FromSeconds(5);
while (base::TimeTicks::Now() < end) {
- if (!HostDiscardableSharedMemoryManager::current()->GetBytesAllocated())
+ if (!discardable_memory::DiscardableSharedMemoryManager::current()
+ ->GetBytesAllocated())
break;
}
diff --git a/content/common/BUILD.gn b/content/common/BUILD.gn
index 7f179163..d975ab3 100644
--- a/content/common/BUILD.gn
+++ b/content/common/BUILD.gn
@@ -93,8 +93,6 @@
"database_messages.h",
"date_time_suggestion.h",
"devtools_messages.h",
- "discardable_shared_memory_heap.cc",
- "discardable_shared_memory_heap.h",
"dom_storage/dom_storage_map.cc",
"dom_storage/dom_storage_map.h",
"dom_storage/dom_storage_messages.h",
@@ -135,8 +133,6 @@
"gpu/client/context_provider_command_buffer.cc",
"gpu/client/context_provider_command_buffer.h",
"gpu_host_messages.h",
- "host_discardable_shared_memory_manager.cc",
- "host_discardable_shared_memory_manager.h",
"host_shared_bitmap_manager.cc",
"host_shared_bitmap_manager.h",
"in_process_child_thread_params.cc",
@@ -356,6 +352,7 @@
"//build/util:webkit_version",
"//cc/ipc",
"//cc/surfaces",
+ "//components/discardable_memory/common",
"//components/tracing",
"//components/tracing:startup_tracing",
"//content:resources",
diff --git a/content/common/DEPS b/content/common/DEPS
index 1831aed..1708c6d1 100644
--- a/content/common/DEPS
+++ b/content/common/DEPS
@@ -1,6 +1,7 @@
include_rules = [
"-storage/browser",
+ "+components/discardable_memory/common",
"+device/base/synchronization",
"+services/service_manager/public/cpp",
"+services/video_capture/public/interfaces",
diff --git a/content/common/child_process_messages.h b/content/common/child_process_messages.h
index 1809063e..c74214a 100644
--- a/content/common/child_process_messages.h
+++ b/content/common/child_process_messages.h
@@ -15,9 +15,9 @@
#include "base/values.h"
#include "build/build_config.h"
#include "cc/resources/shared_bitmap_manager.h"
+#include "components/discardable_memory/common/discardable_shared_memory_id.h"
#include "content/common/content_export.h"
#include "content/common/content_param_traits_macros.h"
-#include "content/common/host_discardable_shared_memory_manager.h"
#include "gpu/command_buffer/common/sync_token.h"
#include "gpu/ipc/common/gpu_param_traits_macros.h"
#include "ipc/ipc_channel_handle.h"
@@ -216,13 +216,13 @@
IPC_SYNC_MESSAGE_CONTROL2_1(
ChildProcessHostMsg_SyncAllocateLockedDiscardableSharedMemory,
uint32_t /* size */,
- content::DiscardableSharedMemoryId,
+ discardable_memory::DiscardableSharedMemoryId,
base::SharedMemoryHandle)
// Informs the browser that the child deleted a block of discardable shared
// memory.
IPC_MESSAGE_CONTROL1(ChildProcessHostMsg_DeletedDiscardableSharedMemory,
- content::DiscardableSharedMemoryId)
+ discardable_memory::DiscardableSharedMemoryId)
#if defined(OS_LINUX)
// Asks the browser to change the priority of thread.
diff --git a/content/ppapi_plugin/BUILD.gn b/content/ppapi_plugin/BUILD.gn
index 88a2b280..d60abd7 100644
--- a/content/ppapi_plugin/BUILD.gn
+++ b/content/ppapi_plugin/BUILD.gn
@@ -45,6 +45,7 @@
deps = [
"//base",
+ "//components/discardable_memory/client",
"//content:export",
"//content/child",
"//content/public/child:child_sources",
diff --git a/content/ppapi_plugin/DEPS b/content/ppapi_plugin/DEPS
index 9959c2f..165fc1c 100644
--- a/content/ppapi_plugin/DEPS
+++ b/content/ppapi_plugin/DEPS
@@ -1,4 +1,5 @@
include_rules = [
+ "+components/discardable_memory/client",
"+content/child",
"+gin/public/isolate_holder.h",
"+gin/v8_initializer.h",
diff --git a/content/ppapi_plugin/ppapi_thread.cc b/content/ppapi_plugin/ppapi_thread.cc
index a719424..5829f83 100644
--- a/content/ppapi_plugin/ppapi_thread.cc
+++ b/content/ppapi_plugin/ppapi_thread.cc
@@ -25,8 +25,8 @@
#include "base/time/time.h"
#include "base/trace_event/trace_event.h"
#include "build/build_config.h"
+#include "components/discardable_memory/client/client_discardable_shared_memory_manager.h"
#include "content/child/browser_font_resource_trusted.h"
-#include "content/child/child_discardable_shared_memory_manager.h"
#include "content/child/child_process.h"
#include "content/common/child_process_messages.h"
#include "content/ppapi_plugin/broker_process_dispatcher.h"
diff --git a/content/renderer/BUILD.gn b/content/renderer/BUILD.gn
index aaadc67e..e700247 100644
--- a/content/renderer/BUILD.gn
+++ b/content/renderer/BUILD.gn
@@ -403,6 +403,7 @@
"//cc/proto",
"//cc/surfaces",
"//cc/surfaces:surface_id",
+ "//components/discardable_memory/client",
"//components/url_formatter",
"//content:resources",
"//content/child",
diff --git a/content/renderer/DEPS b/content/renderer/DEPS
index d9da341..03e52e6c 100644
--- a/content/renderer/DEPS
+++ b/content/renderer/DEPS
@@ -1,6 +1,7 @@
include_rules = [
# Allow inclusion of specific components that we depend on.
# See comment in content/DEPS for which components are allowed.
+ "+components/discardable_memory/client",
"+components/scheduler",
"+components/url_formatter",
diff --git a/content/renderer/render_thread_impl.cc b/content/renderer/render_thread_impl.cc
index cbffff5b..da60867b 100644
--- a/content/renderer/render_thread_impl.cc
+++ b/content/renderer/render_thread_impl.cc
@@ -48,10 +48,10 @@
#include "cc/raster/task_graph_runner.h"
#include "cc/trees/layer_tree_host_common.h"
#include "cc/trees/layer_tree_settings.h"
+#include "components/discardable_memory/client/client_discardable_shared_memory_manager.h"
#include "content/child/appcache/appcache_dispatcher.h"
#include "content/child/appcache/appcache_frontend_impl.h"
#include "content/child/blob_storage/blob_message_filter.h"
-#include "content/child/child_discardable_shared_memory_manager.h"
#include "content/child/child_gpu_memory_buffer_manager.h"
#include "content/child/child_histogram_message_filter.h"
#include "content/child/child_resource_message_filter.h"
@@ -1848,8 +1848,9 @@
UMA_HISTOGRAM_MEMORY_MB("PurgeAndSuspend.Memory.MallocMB",
malloc_usage / 1024 / 1024);
- ChildDiscardableSharedMemoryManager::Statistics discardable_stats =
- ChildThreadImpl::discardable_shared_memory_manager()->GetStatistics();
+ discardable_memory::ClientDiscardableSharedMemoryManager::Statistics
+ discardable_stats =
+ ChildThreadImpl::discardable_shared_memory_manager()->GetStatistics();
size_t discardable_usage =
discardable_stats.total_size - discardable_stats.freelist_size;
UMA_HISTOGRAM_MEMORY_KB("PurgeAndSuspend.Memory.DiscardableKB",
diff --git a/content/renderer/render_thread_impl.h b/content/renderer/render_thread_impl.h
index 6872d04..a442ac0 100644
--- a/content/renderer/render_thread_impl.h
+++ b/content/renderer/render_thread_impl.h
@@ -23,7 +23,6 @@
#include "base/threading/thread_checker.h"
#include "base/timer/timer.h"
#include "build/build_config.h"
-#include "content/child/child_discardable_shared_memory_manager.h"
#include "content/child/child_thread_impl.h"
#include "content/child/memory/child_memory_coordinator_impl.h"
#include "content/common/associated_interface_registry_impl.h"
diff --git a/content/test/BUILD.gn b/content/test/BUILD.gn
index 5c82c85..926fa4f 100644
--- a/content/test/BUILD.gn
+++ b/content/test/BUILD.gn
@@ -662,6 +662,8 @@
":test_support",
":web_ui_test_mojo_bindings",
"//base/test:test_support",
+ "//components/discardable_memory/client",
+ "//components/discardable_memory/common",
"//content:resources",
"//content/app:both_for_content_tests",
"//content/browser:for_content_tests",
@@ -885,6 +887,12 @@
"../renderer/resource_fetcher_browsertest.cc",
"../renderer/savable_resources_browsertest.cc",
]
+
+ deps += [
+ "//components/discardable_memory/client",
+ "//components/discardable_memory/common",
+ "//components/discardable_memory/service",
+ ]
}
if (use_aura) {
@@ -1264,10 +1272,8 @@
"../common/cursors/webcursor_unittest.cc",
"../common/database_connections_unittest.cc",
"../common/database_identifier_unittest.cc",
- "../common/discardable_shared_memory_heap_unittest.cc",
"../common/dom_storage/dom_storage_map_unittest.cc",
"../common/fileapi/file_system_util_unittest.cc",
- "../common/host_discardable_shared_memory_manager_unittest.cc",
"../common/host_shared_bitmap_manager_unittest.cc",
"../common/indexed_db/indexed_db_key_unittest.cc",
"../common/input/event_with_latency_info_unittest.cc",
@@ -1677,7 +1683,6 @@
sources = [
"../browser/renderer_host/input/input_router_impl_perftest.cc",
- "../common/discardable_shared_memory_heap_perftest.cc",
"../test/run_all_perftests.cc",
]
deps = [