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 "base/memory/scoped_vector.h"
9 #include "base/time/time.h"
10 #include "content/child/child_discardable_shared_memory_manager.h"
11 #include "content/child/child_thread_impl.h"
12 #include "content/common/host_discardable_shared_memory_manager.h"
13 #include "content/public/common/content_switches.h"
14 #include "content/public/test/content_browser_test.h"
15 #include "content/public/test/content_browser_test_utils.h"
16 #include "content/shell/browser/shell.h"
21 class ChildThreadImplBrowserTest
: public ContentBrowserTest
{
23 ChildThreadImplBrowserTest()
24 : child_discardable_shared_memory_manager_(nullptr) {}
26 // Overridden from BrowserTestBase:
27 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
28 command_line
->AppendSwitch(switches::kSingleProcess
);
30 void SetUpOnMainThread() override
{
31 NavigateToURL(shell(), GURL(url::kAboutBlankURL
));
32 PostTaskToInProcessRendererAndWait(
33 base::Bind(&ChildThreadImplBrowserTest::SetUpOnChildThread
, this));
36 ChildDiscardableSharedMemoryManager
*
37 child_discardable_shared_memory_manager() {
38 return child_discardable_shared_memory_manager_
;
42 void SetUpOnChildThread() {
43 child_discardable_shared_memory_manager_
=
44 ChildThreadImpl::current()->discardable_shared_memory_manager();
47 ChildDiscardableSharedMemoryManager
* child_discardable_shared_memory_manager_
;
50 IN_PROC_BROWSER_TEST_F(ChildThreadImplBrowserTest
,
51 DISABLED_LockDiscardableMemory
) {
52 const size_t kSize
= 1024 * 1024; // 1MiB.
54 scoped_ptr
<base::DiscardableMemory
> memory
=
55 child_discardable_shared_memory_manager()
56 ->AllocateLockedDiscardableMemory(kSize
);
59 void* addr
= memory
->data();
60 ASSERT_NE(nullptr, addr
);
64 // Purge all unlocked memory.
65 HostDiscardableSharedMemoryManager::current()->SetMemoryLimit(0);
67 // Should fail as memory should have been purged.
68 EXPECT_FALSE(memory
->Lock());
71 IN_PROC_BROWSER_TEST_F(ChildThreadImplBrowserTest
,
72 DISABLED_DiscardableMemoryAddressSpace
) {
73 const size_t kLargeSize
= 4 * 1024 * 1024; // 4MiB.
74 const size_t kNumberOfInstances
= 1024 + 1; // >4GiB total.
76 ScopedVector
<base::DiscardableMemory
> instances
;
77 for (size_t i
= 0; i
< kNumberOfInstances
; ++i
) {
78 scoped_ptr
<base::DiscardableMemory
> memory
=
79 child_discardable_shared_memory_manager()
80 ->AllocateLockedDiscardableMemory(kLargeSize
);
82 void* addr
= memory
->data();
83 ASSERT_NE(nullptr, addr
);
85 instances
.push_back(memory
.Pass());
89 IN_PROC_BROWSER_TEST_F(ChildThreadImplBrowserTest
,
90 DISABLED_ReleaseFreeDiscardableMemory
) {
91 const size_t kSize
= 1024 * 1024; // 1MiB.
93 scoped_ptr
<base::DiscardableMemory
> memory
=
94 child_discardable_shared_memory_manager()
95 ->AllocateLockedDiscardableMemory(kSize
);
100 EXPECT_GE(HostDiscardableSharedMemoryManager::current()->GetBytesAllocated(),
103 child_discardable_shared_memory_manager()->ReleaseFreeMemory();
105 // Busy wait for host memory usage to be reduced.
106 base::TimeTicks end
=
107 base::TimeTicks::Now() + base::TimeDelta::FromSeconds(5);
108 while (base::TimeTicks::Now() < end
) {
109 if (!HostDiscardableSharedMemoryManager::current()->GetBytesAllocated())
113 EXPECT_LT(base::TimeTicks::Now(), end
);