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 "content/child/child_discardable_shared_memory_manager.h"
8 #include "content/child/child_thread_impl.h"
9 #include "content/common/host_discardable_shared_memory_manager.h"
10 #include "content/public/common/content_switches.h"
11 #include "content/public/test/content_browser_test.h"
12 #include "content/public/test/content_browser_test_utils.h"
13 #include "content/shell/browser/shell.h"
18 class ChildDiscardableSharedMemoryManagerBrowserTest
19 : public ContentBrowserTest
{
21 void SetUpCommandLine(base::CommandLine
* command_line
) override
{
22 command_line
->AppendSwitch(switches::kSingleProcess
);
23 command_line
->AppendSwitch(switches::kDisableGpu
);
26 static void ReleaseFreeMemory() {
27 ChildThreadImpl::current()
28 ->discardable_shared_memory_manager()
29 ->ReleaseFreeMemory();
32 static void AllocateLockedMemory(
34 scoped_ptr
<base::DiscardableMemoryShmemChunk
>* memory
) {
35 *memory
= ChildThreadImpl::current()
36 ->discardable_shared_memory_manager()
37 ->AllocateLockedDiscardableMemory(size
);
40 static void LockMemory(base::DiscardableMemoryShmemChunk
* memory
,
42 *result
= memory
->Lock();
45 static void UnlockMemory(base::DiscardableMemoryShmemChunk
* memory
) {
49 static void FreeMemory(scoped_ptr
<base::DiscardableMemoryShmemChunk
> memory
) {
54 #define MAYBE_LockMemory DISABLED_LockMemory
56 #define MAYBE_LockMemory LockMemory
59 IN_PROC_BROWSER_TEST_F(ChildDiscardableSharedMemoryManagerBrowserTest
,
61 const size_t kSize
= 1024 * 1024; // 1MiB.
63 NavigateToURL(shell(), GURL(url::kAboutBlankURL
));
65 scoped_ptr
<base::DiscardableMemoryShmemChunk
> memory
;
66 PostTaskToInProcessRendererAndWait(base::Bind(
67 &ChildDiscardableSharedMemoryManagerBrowserTest::AllocateLockedMemory
,
71 void* addr
= memory
->Memory();
72 ASSERT_NE(nullptr, addr
);
74 PostTaskToInProcessRendererAndWait(
75 base::Bind(&ChildDiscardableSharedMemoryManagerBrowserTest::UnlockMemory
,
78 // Purge all unlocked memory.
79 HostDiscardableSharedMemoryManager::current()->SetMemoryLimit(0);
82 PostTaskToInProcessRendererAndWait(
83 base::Bind(&ChildDiscardableSharedMemoryManagerBrowserTest::LockMemory
,
84 memory
.get(), &result
));
86 // Should fail as memory should have been purged.
89 PostTaskToInProcessRendererAndWait(
90 base::Bind(&ChildDiscardableSharedMemoryManagerBrowserTest::FreeMemory
,
91 base::Passed(&memory
)));
94 IN_PROC_BROWSER_TEST_F(ChildDiscardableSharedMemoryManagerBrowserTest
,
96 const size_t kLargeSize
= 4 * 1024 * 1024; // 4MiB.
97 const size_t kNumberOfInstances
= 1024 + 1; // >4GiB total.
99 NavigateToURL(shell(), GURL(url::kAboutBlankURL
));
101 scoped_ptr
<base::DiscardableMemoryShmemChunk
> instances
[kNumberOfInstances
];
102 for (auto& memory
: instances
) {
103 PostTaskToInProcessRendererAndWait(base::Bind(
104 &ChildDiscardableSharedMemoryManagerBrowserTest::AllocateLockedMemory
,
105 kLargeSize
, &memory
));
107 void* addr
= memory
->Memory();
108 ASSERT_NE(nullptr, addr
);
109 PostTaskToInProcessRendererAndWait(base::Bind(
110 &ChildDiscardableSharedMemoryManagerBrowserTest::UnlockMemory
,
114 for (auto& memory
: instances
) {
115 PostTaskToInProcessRendererAndWait(
116 base::Bind(&ChildDiscardableSharedMemoryManagerBrowserTest::FreeMemory
,
117 base::Passed(&memory
)));