Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(191)

Side by Side Diff: gpu/command_buffer/client/mapped_memory_unittest.cc

Issue 23130004: Enforce a memory limit on MappedMemoryManager (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Set chunk size Created 7 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "gpu/command_buffer/client/mapped_memory.h" 5 #include "gpu/command_buffer/client/mapped_memory.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/memory/scoped_ptr.h" 8 #include "base/memory/scoped_ptr.h"
9 #include "base/message_loop/message_loop.h" 9 #include "base/message_loop/message_loop.h"
10 #include "gpu/command_buffer/client/cmd_buffer_helper.h" 10 #include "gpu/command_buffer/client/cmd_buffer_helper.h"
(...skipping 131 matching lines...) Expand 10 before | Expand all | Expand 10 after
142 EXPECT_EQ(kBufferSize - kSize, chunk_->GetLargestFreeSizeWithWaiting()); 142 EXPECT_EQ(kBufferSize - kSize, chunk_->GetLargestFreeSizeWithWaiting());
143 chunk_->Free(pointer_char); 143 chunk_->Free(pointer_char);
144 EXPECT_EQ(kBufferSize, chunk_->GetLargestFreeSizeWithoutWaiting()); 144 EXPECT_EQ(kBufferSize, chunk_->GetLargestFreeSizeWithoutWaiting());
145 EXPECT_EQ(kBufferSize, chunk_->GetLargestFreeSizeWithWaiting()); 145 EXPECT_EQ(kBufferSize, chunk_->GetLargestFreeSizeWithWaiting());
146 } 146 }
147 147
148 class MappedMemoryManagerTest : public MappedMemoryTestBase { 148 class MappedMemoryManagerTest : public MappedMemoryTestBase {
149 protected: 149 protected:
150 virtual void SetUp() { 150 virtual void SetUp() {
151 MappedMemoryTestBase::SetUp(); 151 MappedMemoryTestBase::SetUp();
152 manager_.reset(new MappedMemoryManager(helper_.get())); 152 manager_.reset(new MappedMemoryManager(
153 helper_.get(), MappedMemoryManager::kNoLimit));
153 } 154 }
154 155
155 virtual void TearDown() { 156 virtual void TearDown() {
156 // If the GpuScheduler posts any tasks, this forces them to run. 157 // If the GpuScheduler posts any tasks, this forces them to run.
157 base::MessageLoop::current()->RunUntilIdle(); 158 base::MessageLoop::current()->RunUntilIdle();
158 manager_.reset(); 159 manager_.reset();
159 MappedMemoryTestBase::TearDown(); 160 MappedMemoryTestBase::TearDown();
160 } 161 }
161 162
162 scoped_ptr<MappedMemoryManager> manager_; 163 scoped_ptr<MappedMemoryManager> manager_;
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
299 ASSERT_TRUE(mem2); 300 ASSERT_TRUE(mem2);
300 ASSERT_TRUE(mem3); 301 ASSERT_TRUE(mem3);
301 EXPECT_NE(-1, id1); 302 EXPECT_NE(-1, id1);
302 EXPECT_EQ(id1, id2); 303 EXPECT_EQ(id1, id2);
303 EXPECT_NE(id2, id3); 304 EXPECT_NE(id2, id3);
304 EXPECT_EQ(0u, offset1); 305 EXPECT_EQ(0u, offset1);
305 EXPECT_EQ(kSize, offset2); 306 EXPECT_EQ(kSize, offset2);
306 EXPECT_EQ(0u, offset3); 307 EXPECT_EQ(0u, offset3);
307 } 308 }
308 309
310 TEST_F(MappedMemoryManagerTest, MemoryLimit) {
no sievers 2013/08/20 02:46:54 nit: (MappedMemoryManagerTest, UnusedMemoryLimit)
kaanb 2013/08/20 22:15:46 Done.
311 const unsigned int kSize = 1024;
312 // Reset the manager with a memory limit.
313 manager_.reset(new MappedMemoryManager(helper_.get(), 2 * kSize));
314 const unsigned int kChunkSize = 2 * 1024;
315 manager_->set_chunk_size_multiple(2 * kSize);
316
317 // Allocate half a chunk worth of memory.
318 int32 id1 = -1;
319 unsigned int offset1 = 0xFFFFFFFFU;
320 void* mem1 = manager_->Alloc(kSize, &id1, &offset1);
321 ASSERT_TRUE(mem1);
322 EXPECT_NE(-1, id1);
323 EXPECT_EQ(0u, offset1);
324
325 // Allocate half a chunk worth of memory again.
326 // The same chunk will be used.
327 int32 id2 = -1;
328 unsigned int offset2 = 0xFFFFFFFFU;
329 void* mem2 = manager_->Alloc(kSize, &id2, &offset2);
330 ASSERT_TRUE(mem2);
331 EXPECT_NE(-1, id2);
332 EXPECT_EQ(kSize, offset2);
333
334 // Allocate half a chunk worth of memory.
335 int32 id3 = -1;
336 unsigned int offset3 = 0xFFFFFFFFU;
337 void* mem3 = manager_->Alloc(kSize, &id3, &offset3);
338 ASSERT_TRUE(mem3);
339 EXPECT_NE(-1, id3);
340 EXPECT_EQ(0u, offset3);
341
342 // Expect two chunks to be allocated, even though
343 // the memory limit was set to be one chunk.
no sievers 2013/08/20 02:46:54 nit: maybe just alloc 1 chunk and then another chu
kaanb 2013/08/20 22:15:46 Done.
344 // This proves the memory limit is a soft limit, rather
345 // than a hard one.
346 EXPECT_EQ(2 * kChunkSize, manager_->allocated_memory());
347 }
348
349 TEST_F(MappedMemoryManagerTest, MemoryLimitWithReuse) {
350 const unsigned int kSize = 1024;
351 // Reset the manager with a memory limit.
352 manager_.reset(new MappedMemoryManager(helper_.get(), kSize));
353 const unsigned int kChunkSize = 2 * 1024;
354 manager_->set_chunk_size_multiple(kChunkSize);
355
356 // Allocate half a chunk worth of memory.
357 int32 id1 = -1;
358 unsigned int offset1 = 0xFFFFFFFFU;
359 void* mem1 = manager_->Alloc(kSize, &id1, &offset1);
360 ASSERT_TRUE(mem1);
361 EXPECT_NE(-1, id1);
362 EXPECT_EQ(0u, offset1);
363
364 // Allocate half a chunk worth of memory again.
365 // The same chunk will be used.
366 int32 id2 = -1;
367 unsigned int offset2 = 0xFFFFFFFFU;
368 void* mem2 = manager_->Alloc(kSize, &id2, &offset2);
369 ASSERT_TRUE(mem2);
370 EXPECT_NE(-1, id2);
371 EXPECT_EQ(kSize, offset2);
372
373 // Free one successful allocation, pending fence.
374 int32 token = helper_.get()->InsertToken();
375 manager_->FreePendingToken(mem2, token);
376
377 // The way we hooked up the helper and engine, it won't process commands
378 // until it has to wait for something. Which means the token shouldn't have
379 // passed yet at this point.
380 EXPECT_GT(token, GetToken());
381
382 // Since we didn't call helper_.finish() the token did not pass.
383 // We won't be able to claim the free memory without waiting and
384 // as we've already met the memory limit we'll have to wait
385 // on the token.
386 int32 id3 = -1;
387 unsigned int offset3 = 0xFFFFFFFFU;
388 void* mem3 = manager_->Alloc(kSize, &id3, &offset3);
389 ASSERT_TRUE(mem3);
390 EXPECT_NE(-1, id3);
391 // It will reuse the space from the second allocation just freed.
392 EXPECT_EQ(kSize, offset3);
393
394 // Expect one chunk to be allocated
395 EXPECT_EQ(1 * kChunkSize, manager_->allocated_memory());
396 }
397
309 } // namespace gpu 398 } // namespace gpu
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698