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