OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/command_line.h" | 6 #include "base/command_line.h" |
| 7 #include "base/memory/discardable_memory.h" |
7 #include "content/child/child_discardable_shared_memory_manager.h" | 8 #include "content/child/child_discardable_shared_memory_manager.h" |
8 #include "content/child/child_thread_impl.h" | 9 #include "content/child/child_thread_impl.h" |
9 #include "content/common/host_discardable_shared_memory_manager.h" | 10 #include "content/common/host_discardable_shared_memory_manager.h" |
10 #include "content/public/common/content_switches.h" | 11 #include "content/public/common/content_switches.h" |
11 #include "content/public/test/content_browser_test.h" | 12 #include "content/public/test/content_browser_test.h" |
12 #include "content/public/test/content_browser_test_utils.h" | 13 #include "content/public/test/content_browser_test_utils.h" |
13 #include "content/shell/browser/shell.h" | 14 #include "content/shell/browser/shell.h" |
14 #include "url/gurl.h" | 15 #include "url/gurl.h" |
15 | 16 |
16 namespace content { | 17 namespace content { |
17 | 18 |
18 class ChildDiscardableSharedMemoryManagerBrowserTest | 19 class ChildDiscardableSharedMemoryManagerBrowserTest |
19 : public ContentBrowserTest { | 20 : public ContentBrowserTest { |
20 public: | 21 public: |
21 void SetUpCommandLine(base::CommandLine* command_line) override { | 22 void SetUpCommandLine(base::CommandLine* command_line) override { |
22 command_line->AppendSwitch(switches::kSingleProcess); | 23 command_line->AppendSwitch(switches::kSingleProcess); |
23 } | 24 } |
24 | 25 |
25 static void ReleaseFreeMemory() { | 26 static void ReleaseFreeMemory() { |
26 ChildThreadImpl::current() | 27 ChildThreadImpl::current() |
27 ->discardable_shared_memory_manager() | 28 ->discardable_shared_memory_manager() |
28 ->ReleaseFreeMemory(); | 29 ->ReleaseFreeMemory(); |
29 } | 30 } |
30 | 31 |
31 static void AllocateLockedMemory( | 32 static void AllocateLockedMemory( |
32 size_t size, | 33 size_t size, |
33 scoped_ptr<base::DiscardableMemoryShmemChunk>* memory) { | 34 scoped_ptr<base::DiscardableMemory>* memory) { |
34 *memory = ChildThreadImpl::current() | 35 *memory = ChildThreadImpl::current() |
35 ->discardable_shared_memory_manager() | 36 ->discardable_shared_memory_manager() |
36 ->AllocateLockedDiscardableMemory(size); | 37 ->AllocateLockedDiscardableMemory(size); |
37 } | 38 } |
38 | 39 |
39 static void LockMemory(base::DiscardableMemoryShmemChunk* memory, | 40 static void LockMemory(base::DiscardableMemory* memory, bool* result) { |
40 bool* result) { | |
41 *result = memory->Lock(); | 41 *result = memory->Lock(); |
42 } | 42 } |
43 | 43 |
44 static void UnlockMemory(base::DiscardableMemoryShmemChunk* memory) { | 44 static void UnlockMemory(base::DiscardableMemory* memory) { |
45 memory->Unlock(); | 45 memory->Unlock(); |
46 } | 46 } |
47 | 47 |
48 static void FreeMemory(scoped_ptr<base::DiscardableMemoryShmemChunk> memory) { | 48 static void FreeMemory(scoped_ptr<base::DiscardableMemory> memory) {} |
49 } | |
50 }; | 49 }; |
51 | 50 |
52 IN_PROC_BROWSER_TEST_F(ChildDiscardableSharedMemoryManagerBrowserTest, | 51 IN_PROC_BROWSER_TEST_F(ChildDiscardableSharedMemoryManagerBrowserTest, |
53 DISABLED_LockMemory) { | 52 DISABLED_LockMemory) { |
54 const size_t kSize = 1024 * 1024; // 1MiB. | 53 const size_t kSize = 1024 * 1024; // 1MiB. |
55 | 54 |
56 NavigateToURL(shell(), GURL(url::kAboutBlankURL)); | 55 NavigateToURL(shell(), GURL(url::kAboutBlankURL)); |
57 | 56 |
58 scoped_ptr<base::DiscardableMemoryShmemChunk> memory; | 57 scoped_ptr<base::DiscardableMemory> memory; |
59 PostTaskToInProcessRendererAndWait(base::Bind( | 58 PostTaskToInProcessRendererAndWait(base::Bind( |
60 &ChildDiscardableSharedMemoryManagerBrowserTest::AllocateLockedMemory, | 59 &ChildDiscardableSharedMemoryManagerBrowserTest::AllocateLockedMemory, |
61 kSize, &memory)); | 60 kSize, &memory)); |
62 | 61 |
63 ASSERT_TRUE(memory); | 62 ASSERT_TRUE(memory); |
64 void* addr = memory->Memory(); | 63 void* addr = memory->Memory(); |
65 ASSERT_NE(nullptr, addr); | 64 ASSERT_NE(nullptr, addr); |
66 | 65 |
67 PostTaskToInProcessRendererAndWait( | 66 PostTaskToInProcessRendererAndWait( |
68 base::Bind(&ChildDiscardableSharedMemoryManagerBrowserTest::UnlockMemory, | 67 base::Bind(&ChildDiscardableSharedMemoryManagerBrowserTest::UnlockMemory, |
(...skipping 15 matching lines...) Expand all Loading... |
84 base::Passed(&memory))); | 83 base::Passed(&memory))); |
85 } | 84 } |
86 | 85 |
87 IN_PROC_BROWSER_TEST_F(ChildDiscardableSharedMemoryManagerBrowserTest, | 86 IN_PROC_BROWSER_TEST_F(ChildDiscardableSharedMemoryManagerBrowserTest, |
88 DISABLED_AddressSpace) { | 87 DISABLED_AddressSpace) { |
89 const size_t kLargeSize = 4 * 1024 * 1024; // 4MiB. | 88 const size_t kLargeSize = 4 * 1024 * 1024; // 4MiB. |
90 const size_t kNumberOfInstances = 1024 + 1; // >4GiB total. | 89 const size_t kNumberOfInstances = 1024 + 1; // >4GiB total. |
91 | 90 |
92 NavigateToURL(shell(), GURL(url::kAboutBlankURL)); | 91 NavigateToURL(shell(), GURL(url::kAboutBlankURL)); |
93 | 92 |
94 scoped_ptr<base::DiscardableMemoryShmemChunk> instances[kNumberOfInstances]; | 93 scoped_ptr<base::DiscardableMemory> instances[kNumberOfInstances]; |
95 for (auto& memory : instances) { | 94 for (auto& memory : instances) { |
96 PostTaskToInProcessRendererAndWait(base::Bind( | 95 PostTaskToInProcessRendererAndWait(base::Bind( |
97 &ChildDiscardableSharedMemoryManagerBrowserTest::AllocateLockedMemory, | 96 &ChildDiscardableSharedMemoryManagerBrowserTest::AllocateLockedMemory, |
98 kLargeSize, &memory)); | 97 kLargeSize, &memory)); |
99 ASSERT_TRUE(memory); | 98 ASSERT_TRUE(memory); |
100 void* addr = memory->Memory(); | 99 void* addr = memory->Memory(); |
101 ASSERT_NE(nullptr, addr); | 100 ASSERT_NE(nullptr, addr); |
102 PostTaskToInProcessRendererAndWait(base::Bind( | 101 PostTaskToInProcessRendererAndWait(base::Bind( |
103 &ChildDiscardableSharedMemoryManagerBrowserTest::UnlockMemory, | 102 &ChildDiscardableSharedMemoryManagerBrowserTest::UnlockMemory, |
104 memory.get())); | 103 memory.get())); |
105 } | 104 } |
106 | 105 |
107 for (auto& memory : instances) { | 106 for (auto& memory : instances) { |
108 PostTaskToInProcessRendererAndWait( | 107 PostTaskToInProcessRendererAndWait( |
109 base::Bind(&ChildDiscardableSharedMemoryManagerBrowserTest::FreeMemory, | 108 base::Bind(&ChildDiscardableSharedMemoryManagerBrowserTest::FreeMemory, |
110 base::Passed(&memory))); | 109 base::Passed(&memory))); |
111 } | 110 } |
112 } | 111 } |
113 | 112 |
114 IN_PROC_BROWSER_TEST_F(ChildDiscardableSharedMemoryManagerBrowserTest, | 113 IN_PROC_BROWSER_TEST_F(ChildDiscardableSharedMemoryManagerBrowserTest, |
115 DISABLED_ReleaseFreeMemory) { | 114 DISABLED_ReleaseFreeMemory) { |
116 const size_t kSize = 1024 * 1024; // 1MiB. | 115 const size_t kSize = 1024 * 1024; // 1MiB. |
117 | 116 |
118 NavigateToURL(shell(), GURL(url::kAboutBlankURL)); | 117 NavigateToURL(shell(), GURL(url::kAboutBlankURL)); |
119 | 118 |
120 scoped_ptr<base::DiscardableMemoryShmemChunk> memory; | 119 scoped_ptr<base::DiscardableMemory> memory; |
121 PostTaskToInProcessRendererAndWait(base::Bind( | 120 PostTaskToInProcessRendererAndWait(base::Bind( |
122 &ChildDiscardableSharedMemoryManagerBrowserTest::AllocateLockedMemory, | 121 &ChildDiscardableSharedMemoryManagerBrowserTest::AllocateLockedMemory, |
123 kSize, &memory)); | 122 kSize, &memory)); |
124 PostTaskToInProcessRendererAndWait( | 123 PostTaskToInProcessRendererAndWait( |
125 base::Bind(&ChildDiscardableSharedMemoryManagerBrowserTest::UnlockMemory, | 124 base::Bind(&ChildDiscardableSharedMemoryManagerBrowserTest::UnlockMemory, |
126 memory.get())); | 125 memory.get())); |
127 PostTaskToInProcessRendererAndWait( | 126 PostTaskToInProcessRendererAndWait( |
128 base::Bind(&ChildDiscardableSharedMemoryManagerBrowserTest::FreeMemory, | 127 base::Bind(&ChildDiscardableSharedMemoryManagerBrowserTest::FreeMemory, |
129 base::Passed(&memory))); | 128 base::Passed(&memory))); |
130 | 129 |
131 EXPECT_GE(HostDiscardableSharedMemoryManager::current()->GetBytesAllocated(), | 130 EXPECT_GE(HostDiscardableSharedMemoryManager::current()->GetBytesAllocated(), |
132 kSize); | 131 kSize); |
133 | 132 |
134 PostTaskToInProcessRendererAndWait(base::Bind( | 133 PostTaskToInProcessRendererAndWait(base::Bind( |
135 &ChildDiscardableSharedMemoryManagerBrowserTest::ReleaseFreeMemory)); | 134 &ChildDiscardableSharedMemoryManagerBrowserTest::ReleaseFreeMemory)); |
136 | 135 |
137 EXPECT_EQ(HostDiscardableSharedMemoryManager::current()->GetBytesAllocated(), | 136 EXPECT_EQ(HostDiscardableSharedMemoryManager::current()->GetBytesAllocated(), |
138 0u); | 137 0u); |
139 } | 138 } |
140 | 139 |
141 } // content | 140 } // content |
OLD | NEW |