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 "content/child/child_thread_impl.h" | 5 #include "content/child/child_thread_impl.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <string.h> | 8 #include <string.h> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/bind.h" | 11 #include "base/bind.h" |
12 #include "base/command_line.h" | 12 #include "base/command_line.h" |
13 #include "base/memory/discardable_memory.h" | 13 #include "base/memory/discardable_memory.h" |
14 #include "base/memory/scoped_vector.h" | 14 #include "base/memory/scoped_vector.h" |
15 #include "base/time/time.h" | 15 #include "base/time/time.h" |
16 #include "content/child/child_discardable_shared_memory_manager.h" | 16 #include "components/discardable_memory/client/child_discardable_shared_memory_m
anager.h" |
| 17 #include "components/discardable_memory/common/discardable_shared_memory_manager
.h" |
17 #include "content/child/child_gpu_memory_buffer_manager.h" | 18 #include "content/child/child_gpu_memory_buffer_manager.h" |
18 #include "content/common/host_discardable_shared_memory_manager.h" | |
19 #include "content/public/common/content_switches.h" | 19 #include "content/public/common/content_switches.h" |
20 #include "content/public/test/content_browser_test.h" | 20 #include "content/public/test/content_browser_test.h" |
21 #include "content/public/test/content_browser_test_utils.h" | 21 #include "content/public/test/content_browser_test_utils.h" |
22 #include "content/shell/browser/shell.h" | 22 #include "content/shell/browser/shell.h" |
23 #include "gpu/ipc/client/gpu_memory_buffer_impl.h" | 23 #include "gpu/ipc/client/gpu_memory_buffer_impl.h" |
24 #include "ui/gfx/buffer_format_util.h" | 24 #include "ui/gfx/buffer_format_util.h" |
25 #include "url/gurl.h" | 25 #include "url/gurl.h" |
26 | 26 |
27 namespace content { | 27 namespace content { |
28 namespace { | 28 namespace { |
29 | 29 |
30 class ChildThreadImplBrowserTest : public ContentBrowserTest { | 30 class ChildThreadImplBrowserTest : public ContentBrowserTest { |
31 public: | 31 public: |
32 ChildThreadImplBrowserTest() | 32 ChildThreadImplBrowserTest() |
33 : child_discardable_shared_memory_manager_(nullptr) {} | 33 : child_discardable_shared_memory_manager_(nullptr) {} |
34 | 34 |
35 // Overridden from BrowserTestBase: | 35 // Overridden from BrowserTestBase: |
36 void SetUpCommandLine(base::CommandLine* command_line) override { | 36 void SetUpCommandLine(base::CommandLine* command_line) override { |
37 command_line->AppendSwitch(switches::kSingleProcess); | 37 command_line->AppendSwitch(switches::kSingleProcess); |
38 } | 38 } |
39 void SetUpOnMainThread() override { | 39 void SetUpOnMainThread() override { |
40 NavigateToURL(shell(), GURL(url::kAboutBlankURL)); | 40 NavigateToURL(shell(), GURL(url::kAboutBlankURL)); |
41 PostTaskToInProcessRendererAndWait( | 41 PostTaskToInProcessRendererAndWait( |
42 base::Bind(&ChildThreadImplBrowserTest::SetUpOnChildThread, | 42 base::Bind(&ChildThreadImplBrowserTest::SetUpOnChildThread, |
43 base::Unretained(this))); | 43 base::Unretained(this))); |
44 } | 44 } |
45 | 45 |
46 ChildDiscardableSharedMemoryManager* | 46 discardable_memory::ChildDiscardableSharedMemoryManager* |
47 child_discardable_shared_memory_manager() { | 47 child_discardable_shared_memory_manager() { |
48 return child_discardable_shared_memory_manager_; | 48 return child_discardable_shared_memory_manager_; |
49 } | 49 } |
50 | 50 |
51 private: | 51 private: |
52 void SetUpOnChildThread() { | 52 void SetUpOnChildThread() { |
53 child_discardable_shared_memory_manager_ = | 53 child_discardable_shared_memory_manager_ = |
54 ChildThreadImpl::current()->discardable_shared_memory_manager(); | 54 ChildThreadImpl::current()->discardable_shared_memory_manager(); |
55 } | 55 } |
56 | 56 |
57 ChildDiscardableSharedMemoryManager* child_discardable_shared_memory_manager_; | 57 discardable_memory::ChildDiscardableSharedMemoryManager* |
| 58 child_discardable_shared_memory_manager_; |
58 }; | 59 }; |
59 | 60 |
60 IN_PROC_BROWSER_TEST_F(ChildThreadImplBrowserTest, LockDiscardableMemory) { | 61 IN_PROC_BROWSER_TEST_F(ChildThreadImplBrowserTest, LockDiscardableMemory) { |
61 const size_t kSize = 1024 * 1024; // 1MiB. | 62 const size_t kSize = 1024 * 1024; // 1MiB. |
62 | 63 |
63 std::unique_ptr<base::DiscardableMemory> memory = | 64 std::unique_ptr<base::DiscardableMemory> memory = |
64 child_discardable_shared_memory_manager() | 65 child_discardable_shared_memory_manager() |
65 ->AllocateLockedDiscardableMemory(kSize); | 66 ->AllocateLockedDiscardableMemory(kSize); |
66 | 67 |
67 ASSERT_TRUE(memory); | 68 ASSERT_TRUE(memory); |
68 void* addr = memory->data(); | 69 void* addr = memory->data(); |
69 ASSERT_NE(nullptr, addr); | 70 ASSERT_NE(nullptr, addr); |
70 | 71 |
71 memory->Unlock(); | 72 memory->Unlock(); |
72 | 73 |
73 // Purge all unlocked memory. | 74 // Purge all unlocked memory. |
74 HostDiscardableSharedMemoryManager::current()->SetMemoryLimit(0); | 75 discardable_memory::DiscardableSharedMemoryManager::current()->SetMemoryLimit( |
| 76 0); |
75 | 77 |
76 // Should fail as memory should have been purged. | 78 // Should fail as memory should have been purged. |
77 EXPECT_FALSE(memory->Lock()); | 79 EXPECT_FALSE(memory->Lock()); |
78 } | 80 } |
79 | 81 |
80 IN_PROC_BROWSER_TEST_F(ChildThreadImplBrowserTest, | 82 IN_PROC_BROWSER_TEST_F(ChildThreadImplBrowserTest, |
81 DiscardableMemoryAddressSpace) { | 83 DiscardableMemoryAddressSpace) { |
82 const size_t kLargeSize = 4 * 1024 * 1024; // 4MiB. | 84 const size_t kLargeSize = 4 * 1024 * 1024; // 4MiB. |
83 const size_t kNumberOfInstances = 1024 + 1; // >4GiB total. | 85 const size_t kNumberOfInstances = 1024 + 1; // >4GiB total. |
84 | 86 |
(...skipping 14 matching lines...) Expand all Loading... |
99 ReleaseFreeDiscardableMemory) { | 101 ReleaseFreeDiscardableMemory) { |
100 const size_t kSize = 1024 * 1024; // 1MiB. | 102 const size_t kSize = 1024 * 1024; // 1MiB. |
101 | 103 |
102 std::unique_ptr<base::DiscardableMemory> memory = | 104 std::unique_ptr<base::DiscardableMemory> memory = |
103 child_discardable_shared_memory_manager() | 105 child_discardable_shared_memory_manager() |
104 ->AllocateLockedDiscardableMemory(kSize); | 106 ->AllocateLockedDiscardableMemory(kSize); |
105 | 107 |
106 EXPECT_TRUE(memory); | 108 EXPECT_TRUE(memory); |
107 memory.reset(); | 109 memory.reset(); |
108 | 110 |
109 EXPECT_GE(HostDiscardableSharedMemoryManager::current()->GetBytesAllocated(), | 111 EXPECT_GE(discardable_memory::DiscardableSharedMemoryManager::current() |
| 112 ->GetBytesAllocated(), |
110 kSize); | 113 kSize); |
111 | 114 |
112 child_discardable_shared_memory_manager()->ReleaseFreeMemory(); | 115 child_discardable_shared_memory_manager()->ReleaseFreeMemory(); |
113 | 116 |
114 // Busy wait for host memory usage to be reduced. | 117 // Busy wait for host memory usage to be reduced. |
115 base::TimeTicks end = | 118 base::TimeTicks end = |
116 base::TimeTicks::Now() + base::TimeDelta::FromSeconds(5); | 119 base::TimeTicks::Now() + base::TimeDelta::FromSeconds(5); |
117 while (base::TimeTicks::Now() < end) { | 120 while (base::TimeTicks::Now() < end) { |
118 if (!HostDiscardableSharedMemoryManager::current()->GetBytesAllocated()) | 121 if (!discardable_memory::DiscardableSharedMemoryManager::current() |
| 122 ->GetBytesAllocated()) |
119 break; | 123 break; |
120 } | 124 } |
121 | 125 |
122 EXPECT_LT(base::TimeTicks::Now(), end); | 126 EXPECT_LT(base::TimeTicks::Now(), end); |
123 } | 127 } |
124 | 128 |
125 } // namespace | 129 } // namespace |
126 } // namespace content | 130 } // namespace content |
OLD | NEW |