1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
6 #include "base/command_line.h"
7 #include "base/memory/discardable_memory.h"
8 #include "content/child/child_discardable_shared_memory_manager.h"
9 #include "content/child/child_thread_impl.h"
10 #include "content/common/host_discardable_shared_memory_manager.h"
11 #include "content/public/common/content_switches.h"
12 #include "content/public/test/content_browser_test.h"
13 #include "content/public/test/content_browser_test_utils.h"
14 #include "content/shell/browser/shell.h"
19 class ChildDiscardableSharedMemoryManagerBrowserTest
20 : public ContentBrowserTest
{
22 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
23 command_line
->AppendSwitch(switches::kSingleProcess
);
26 static void ReleaseFreeMemory() {
27 ChildThreadImpl::current()
28 ->discardable_shared_memory_manager()
29 ->ReleaseFreeMemory();
32 static void AllocateLockedMemory(
34 scoped_ptr
<base::DiscardableMemory
>* memory
) {
35 *memory
= ChildThreadImpl::current()
36 ->discardable_shared_memory_manager()
37 ->AllocateLockedDiscardableMemory(size
);
40 static void LockMemory(base::DiscardableMemory
* memory
, bool* result
) {
41 *result
= memory
->Lock();
44 static void UnlockMemory(base::DiscardableMemory
* memory
) {
48 static void FreeMemory(scoped_ptr
<base::DiscardableMemory
> memory
) {}
51 IN_PROC_BROWSER_TEST_F(ChildDiscardableSharedMemoryManagerBrowserTest
,
52 DISABLED_LockMemory
) {
53 const size_t kSize
= 1024 * 1024; // 1MiB.
55 NavigateToURL(shell(), GURL(url::kAboutBlankURL
));
57 scoped_ptr
<base::DiscardableMemory
> memory
;
58 PostTaskToInProcessRendererAndWait(base::Bind(
59 &ChildDiscardableSharedMemoryManagerBrowserTest::AllocateLockedMemory
,
63 void* addr
= memory
->Memory();
64 ASSERT_NE(nullptr, addr
);
66 PostTaskToInProcessRendererAndWait(
67 base::Bind(&ChildDiscardableSharedMemoryManagerBrowserTest::UnlockMemory
,
70 // Purge all unlocked memory.
71 HostDiscardableSharedMemoryManager::current()->SetMemoryLimit(0);
74 PostTaskToInProcessRendererAndWait(
75 base::Bind(&ChildDiscardableSharedMemoryManagerBrowserTest::LockMemory
,
76 memory
.get(), &result
));
78 // Should fail as memory should have been purged.
81 PostTaskToInProcessRendererAndWait(
82 base::Bind(&ChildDiscardableSharedMemoryManagerBrowserTest::FreeMemory
,
83 base::Passed(&memory
)));
86 IN_PROC_BROWSER_TEST_F(ChildDiscardableSharedMemoryManagerBrowserTest
,
87 DISABLED_AddressSpace
) {
88 const size_t kLargeSize
= 4 * 1024 * 1024; // 4MiB.
89 const size_t kNumberOfInstances
= 1024 + 1; // >4GiB total.
91 NavigateToURL(shell(), GURL(url::kAboutBlankURL
));
93 scoped_ptr
<base::DiscardableMemory
> instances
[kNumberOfInstances
];
94 for (auto& memory
: instances
) {
95 PostTaskToInProcessRendererAndWait(base::Bind(
96 &ChildDiscardableSharedMemoryManagerBrowserTest::AllocateLockedMemory
,
97 kLargeSize
, &memory
));
99 void* addr
= memory
->Memory();
100 ASSERT_NE(nullptr, addr
);
101 PostTaskToInProcessRendererAndWait(base::Bind(
102 &ChildDiscardableSharedMemoryManagerBrowserTest::UnlockMemory
,
106 for (auto& memory
: instances
) {
107 PostTaskToInProcessRendererAndWait(
108 base::Bind(&ChildDiscardableSharedMemoryManagerBrowserTest::FreeMemory
,
109 base::Passed(&memory
)));
113 IN_PROC_BROWSER_TEST_F(ChildDiscardableSharedMemoryManagerBrowserTest
,
114 DISABLED_ReleaseFreeMemory
) {
115 const size_t kSize
= 1024 * 1024; // 1MiB.
117 NavigateToURL(shell(), GURL(url::kAboutBlankURL
));
119 scoped_ptr
<base::DiscardableMemory
> memory
;
120 PostTaskToInProcessRendererAndWait(base::Bind(
121 &ChildDiscardableSharedMemoryManagerBrowserTest::AllocateLockedMemory
,
123 PostTaskToInProcessRendererAndWait(
124 base::Bind(&ChildDiscardableSharedMemoryManagerBrowserTest::UnlockMemory
,
126 PostTaskToInProcessRendererAndWait(
127 base::Bind(&ChildDiscardableSharedMemoryManagerBrowserTest::FreeMemory
,
128 base::Passed(&memory
)));
130 EXPECT_GE(HostDiscardableSharedMemoryManager::current()->GetBytesAllocated(),
133 PostTaskToInProcessRendererAndWait(base::Bind(
134 &ChildDiscardableSharedMemoryManagerBrowserTest::ReleaseFreeMemory
));
136 EXPECT_EQ(HostDiscardableSharedMemoryManager::current()->GetBytesAllocated(),