1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
|
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/40285824): Remove this and convert code to safer constructs.
#pragma allow_unsafe_buffers
#endif
#include "gpu/command_buffer/client/mapped_memory.h"
#include <stddef.h>
#include <stdint.h>
#include <array>
#include <list>
#include <memory>
#include "base/functional/bind.h"
#include "base/run_loop.h"
#include "base/test/task_environment.h"
#include "gpu/command_buffer/client/cmd_buffer_helper.h"
#include "gpu/command_buffer/client/command_buffer_direct_locked.h"
#include "gpu/command_buffer/service/mocks.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace gpu {
using testing::Return;
using testing::Mock;
using testing::Truly;
using testing::Sequence;
using testing::DoAll;
using testing::Invoke;
using testing::_;
class MappedMemoryTestBase : public testing::Test {
protected:
static const unsigned int kBufferSize = 1024;
void SetUp() override {
command_buffer_ = std::make_unique<CommandBufferDirectLocked>();
api_mock_ = std::make_unique<AsyncAPIMock>(true, command_buffer_.get(),
command_buffer_->service());
// ignore noops in the mock - we don't want to inspect the internals of the
// helper.
EXPECT_CALL(*api_mock_, DoCommand(cmd::kNoop, 0, _))
.WillRepeatedly(Return(error::kNoError));
// Forward the SetToken calls to the engine
EXPECT_CALL(*api_mock_.get(), DoCommand(cmd::kSetToken, 1, _))
.WillRepeatedly(DoAll(Invoke(api_mock_.get(), &AsyncAPIMock::SetToken),
Return(error::kNoError)));
helper_ = std::make_unique<CommandBufferHelper>(command_buffer_.get());
helper_->Initialize(kBufferSize);
}
int32_t GetToken() { return command_buffer_->GetLastState().token; }
std::unique_ptr<CommandBufferDirectLocked> command_buffer_;
std::unique_ptr<AsyncAPIMock> api_mock_;
std::unique_ptr<CommandBufferHelper> helper_;
base::test::SingleThreadTaskEnvironment task_environment_;
};
const unsigned int MappedMemoryTestBase::kBufferSize;
// Test fixture for MemoryChunk test - Creates a MemoryChunk, using a
// CommandBufferHelper with a mock AsyncAPIInterface for its interface (calling
// it directly, not through the RPC mechanism), making sure Noops are ignored
// and SetToken are properly forwarded to the engine.
class MemoryChunkTest : public MappedMemoryTestBase {
protected:
static const int32_t kShmId = 123;
void SetUp() override {
MappedMemoryTestBase::SetUp();
base::UnsafeSharedMemoryRegion shared_memory_region =
base::UnsafeSharedMemoryRegion::Create(kBufferSize);
base::WritableSharedMemoryMapping shared_memory_mapping =
shared_memory_region.Map();
buffer_ = MakeBufferFromSharedMemory(std::move(shared_memory_region),
std::move(shared_memory_mapping));
chunk_ = std::make_unique<MemoryChunk>(kShmId, buffer_, helper_.get());
}
void TearDown() override {
// If the CommandExecutor posts any tasks, this forces them to run.
base::RunLoop().RunUntilIdle();
MappedMemoryTestBase::TearDown();
}
uint8_t* buffer_memory() { return static_cast<uint8_t*>(buffer_->memory()); }
std::unique_ptr<MemoryChunk> chunk_;
scoped_refptr<gpu::Buffer> buffer_;
};
const int32_t MemoryChunkTest::kShmId;
TEST_F(MemoryChunkTest, Basic) {
const unsigned int kSize = 16;
EXPECT_EQ(kShmId, chunk_->shm_id());
EXPECT_EQ(kBufferSize, chunk_->GetLargestFreeSizeWithoutWaiting());
EXPECT_EQ(kBufferSize, chunk_->GetLargestFreeSizeWithWaiting());
EXPECT_EQ(kBufferSize, chunk_->GetSize());
void *pointer = chunk_->Alloc(kSize);
ASSERT_TRUE(pointer);
EXPECT_LE(buffer_->memory(), static_cast<uint8_t*>(pointer));
EXPECT_GE(kBufferSize,
static_cast<uint8_t*>(pointer) - buffer_memory() + kSize);
EXPECT_EQ(kBufferSize - kSize, chunk_->GetLargestFreeSizeWithoutWaiting());
EXPECT_EQ(kBufferSize - kSize, chunk_->GetLargestFreeSizeWithWaiting());
EXPECT_EQ(kBufferSize, chunk_->GetSize());
chunk_->Free(pointer);
EXPECT_EQ(kBufferSize, chunk_->GetLargestFreeSizeWithoutWaiting());
EXPECT_EQ(kBufferSize, chunk_->GetLargestFreeSizeWithWaiting());
uint8_t* pointer_char = static_cast<uint8_t*>(chunk_->Alloc(kSize));
ASSERT_TRUE(pointer_char);
EXPECT_LE(buffer_memory(), pointer_char);
EXPECT_GE(buffer_memory() + kBufferSize, pointer_char + kSize);
EXPECT_EQ(kBufferSize - kSize, chunk_->GetLargestFreeSizeWithoutWaiting());
EXPECT_EQ(kBufferSize - kSize, chunk_->GetLargestFreeSizeWithWaiting());
chunk_->Free(pointer_char);
EXPECT_EQ(kBufferSize, chunk_->GetLargestFreeSizeWithoutWaiting());
EXPECT_EQ(kBufferSize, chunk_->GetLargestFreeSizeWithWaiting());
}
class MappedMemoryManagerTest : public MappedMemoryTestBase {
public:
MappedMemoryManager* manager() const {
return manager_.get();
}
protected:
void SetUp() override {
MappedMemoryTestBase::SetUp();
manager_ = std::make_unique<MappedMemoryManager>(
helper_.get(), MappedMemoryManager::kNoLimit);
}
void TearDown() override {
// If the CommandExecutor posts any tasks, this forces them to run.
base::RunLoop().RunUntilIdle();
manager_.reset();
MappedMemoryTestBase::TearDown();
}
std::unique_ptr<MappedMemoryManager> manager_;
};
TEST_F(MappedMemoryManagerTest, Basic) {
const unsigned int kSize = 1024;
// Check we can alloc.
int32_t id1 = -1;
unsigned int offset1 = 0xFFFFFFFFU;
void* mem1 = manager_->Alloc(kSize, &id1, &offset1);
ASSERT_TRUE(mem1);
EXPECT_NE(-1, id1);
EXPECT_EQ(0u, offset1);
// Check if we free and realloc the same size we get the same memory
int32_t id2 = -1;
unsigned int offset2 = 0xFFFFFFFFU;
manager_->Free(mem1);
void* mem2 = manager_->Alloc(kSize, &id2, &offset2);
EXPECT_EQ(mem1, mem2);
EXPECT_EQ(id1, id2);
EXPECT_EQ(offset1, offset2);
// Check if we allocate again we get different shared memory
int32_t id3 = -1;
unsigned int offset3 = 0xFFFFFFFFU;
void* mem3 = manager_->Alloc(kSize, &id3, &offset3);
ASSERT_TRUE(mem3 != nullptr);
EXPECT_NE(mem2, mem3);
EXPECT_NE(id2, id3);
EXPECT_EQ(0u, offset3);
// Free 3 and allocate 2 half size blocks.
manager_->Free(mem3);
int32_t id4 = -1;
int32_t id5 = -1;
unsigned int offset4 = 0xFFFFFFFFU;
unsigned int offset5 = 0xFFFFFFFFU;
void* mem4 = manager_->Alloc(kSize / 2, &id4, &offset4);
void* mem5 = manager_->Alloc(kSize / 2, &id5, &offset5);
ASSERT_TRUE(mem4 != nullptr);
ASSERT_TRUE(mem5 != nullptr);
EXPECT_EQ(id3, id4);
EXPECT_EQ(id4, id5);
EXPECT_EQ(0u, offset4);
EXPECT_EQ(kSize / 2u, offset5);
manager_->Free(mem4);
manager_->Free(mem2);
manager_->Free(mem5);
}
TEST_F(MappedMemoryManagerTest, FreePendingToken) {
const unsigned int kSize = 128;
const unsigned int kAllocCount = (kBufferSize / kSize) * 2;
CHECK(kAllocCount * kSize == kBufferSize * 2);
// Allocate several buffers across multiple chunks.
std::array<void*, kAllocCount> pointers;
for (unsigned int i = 0; i < kAllocCount; ++i) {
int32_t id = -1;
unsigned int offset = 0xFFFFFFFFu;
pointers[i] = manager_->Alloc(kSize, &id, &offset);
EXPECT_TRUE(pointers[i]);
EXPECT_NE(id, -1);
EXPECT_NE(offset, 0xFFFFFFFFu);
}
// Free one successful allocation, pending fence.
int32_t token = helper_.get()->InsertToken();
manager_->FreePendingToken(pointers[0], token);
// The way we hooked up the helper and engine, it won't process commands
// until it has to wait for something. Which means the token shouldn't have
// passed yet at this point.
EXPECT_GT(token, GetToken());
// Force it to read up to the token
helper_->Finish();
// Check that the token has indeed passed.
EXPECT_LE(token, GetToken());
// This allocation should use the spot just freed above.
int32_t new_id = -1;
unsigned int new_offset = 0xFFFFFFFFu;
void* new_ptr = manager_->Alloc(kSize, &new_id, &new_offset);
EXPECT_TRUE(new_ptr);
EXPECT_EQ(new_ptr, pointers[0]);
EXPECT_NE(new_id, -1);
EXPECT_NE(new_offset, 0xFFFFFFFFu);
// Free up everything.
manager_->Free(new_ptr);
for (unsigned int i = 1; i < kAllocCount; ++i) {
manager_->Free(pointers[i]);
}
}
TEST_F(MappedMemoryManagerTest, FreeUnused) {
command_buffer_->LockFlush();
int32_t id = -1;
unsigned int offset = 0xFFFFFFFFU;
const unsigned int kAllocSize = 2048;
manager_->set_chunk_size_multiple(kAllocSize * 2);
void* m1 = manager_->Alloc(kAllocSize, &id, &offset);
void* m2 = manager_->Alloc(kAllocSize, &id, &offset);
ASSERT_TRUE(m1 != nullptr);
ASSERT_TRUE(m2 != nullptr);
// m1 and m2 fit in one chunk
EXPECT_EQ(1u, manager_->num_chunks());
void* m3 = manager_->Alloc(kAllocSize, &id, &offset);
ASSERT_TRUE(m3 != nullptr);
// m3 needs another chunk
EXPECT_EQ(2u, manager_->num_chunks());
// Nothing to free, both chunks are in-use.
manager_->FreeUnused();
EXPECT_EQ(2u, manager_->num_chunks());
manager_->Free(m3);
EXPECT_EQ(2u, manager_->num_chunks());
// The second chunk is no longer in use, we can remove.
manager_->FreeUnused();
EXPECT_EQ(1u, manager_->num_chunks());
int32_t token = helper_->InsertToken();
manager_->FreePendingToken(m1, token);
// The way we hooked up the helper and engine, it won't process commands
// until it has to wait for something. Which means the token shouldn't have
// passed yet at this point.
EXPECT_GT(token, GetToken());
EXPECT_EQ(1u, manager_->num_chunks());
int old_flush_count = command_buffer_->FlushCount();
// The remaining chunk is still busy, can't free it.
manager_->FreeUnused();
EXPECT_EQ(1u, manager_->num_chunks());
// This should not have caused a Flush or a Finish.
EXPECT_GT(token, GetToken());
EXPECT_EQ(old_flush_count, command_buffer_->FlushCount());
manager_->Free(m2);
EXPECT_EQ(1u, manager_->num_chunks());
// The remaining chunk is free pending token, we can release the shared
// memory.
manager_->FreeUnused();
EXPECT_EQ(0u, manager_->num_chunks());
// This should have triggered a Flush, but not forced a Finish, i.e. the token
// shouldn't have passed yet.
EXPECT_EQ(old_flush_count + 1, command_buffer_->FlushCount());
EXPECT_GT(token, GetToken());
}
TEST_F(MappedMemoryManagerTest, ChunkSizeMultiple) {
const unsigned int kSize = 1024;
manager_->set_chunk_size_multiple(kSize * 2);
// Check if we allocate less than the chunk size multiple we get
// chunks arounded up.
int32_t id1 = -1;
unsigned int offset1 = 0xFFFFFFFFU;
void* mem1 = manager_->Alloc(kSize, &id1, &offset1);
int32_t id2 = -1;
unsigned int offset2 = 0xFFFFFFFFU;
void* mem2 = manager_->Alloc(kSize, &id2, &offset2);
int32_t id3 = -1;
unsigned int offset3 = 0xFFFFFFFFU;
void* mem3 = manager_->Alloc(kSize, &id3, &offset3);
ASSERT_TRUE(mem1);
ASSERT_TRUE(mem2);
ASSERT_TRUE(mem3);
EXPECT_NE(-1, id1);
EXPECT_EQ(id1, id2);
EXPECT_NE(id2, id3);
EXPECT_EQ(0u, offset1);
EXPECT_EQ(kSize, offset2);
EXPECT_EQ(0u, offset3);
manager_->Free(mem1);
manager_->Free(mem2);
manager_->Free(mem3);
}
TEST_F(MappedMemoryManagerTest, UnusedMemoryLimit) {
const unsigned int kChunkSize = 2048;
// Reset the manager with a memory limit.
manager_ = std::make_unique<MappedMemoryManager>(helper_.get(), kChunkSize);
manager_->set_chunk_size_multiple(kChunkSize);
// Allocate one chunk worth of memory.
int32_t id1 = -1;
unsigned int offset1 = 0xFFFFFFFFU;
void* mem1 = manager_->Alloc(kChunkSize, &id1, &offset1);
ASSERT_TRUE(mem1);
EXPECT_NE(-1, id1);
EXPECT_EQ(0u, offset1);
// Allocate half a chunk worth of memory again.
// The same chunk will be used.
int32_t id2 = -1;
unsigned int offset2 = 0xFFFFFFFFU;
void* mem2 = manager_->Alloc(kChunkSize, &id2, &offset2);
ASSERT_TRUE(mem2);
EXPECT_NE(-1, id2);
EXPECT_EQ(0u, offset2);
// Expect two chunks to be allocated, exceeding the limit,
// since all memory is in use.
EXPECT_EQ(2 * kChunkSize, manager_->allocated_memory());
manager_->Free(mem1);
manager_->Free(mem2);
}
TEST_F(MappedMemoryManagerTest, MemoryLimitWithReuse) {
const unsigned int kSize = 1024;
// Reset the manager with a memory limit.
manager_ = std::make_unique<MappedMemoryManager>(helper_.get(), kSize);
const unsigned int kChunkSize = 2 * 1024;
manager_->set_chunk_size_multiple(kChunkSize);
// Allocate half a chunk worth of memory.
int32_t id1 = -1;
unsigned int offset1 = 0xFFFFFFFFU;
void* mem1 = manager_->Alloc(kSize, &id1, &offset1);
ASSERT_TRUE(mem1);
EXPECT_NE(-1, id1);
EXPECT_EQ(0u, offset1);
// Allocate half a chunk worth of memory again.
// The same chunk will be used.
int32_t id2 = -1;
unsigned int offset2 = 0xFFFFFFFFU;
void* mem2 = manager_->Alloc(kSize, &id2, &offset2);
ASSERT_TRUE(mem2);
EXPECT_NE(-1, id2);
EXPECT_EQ(kSize, offset2);
// Free one successful allocation, pending fence.
int32_t token = helper_.get()->InsertToken();
manager_->FreePendingToken(mem2, token);
// The way we hooked up the helper and engine, it won't process commands
// until it has to wait for something. Which means the token shouldn't have
// passed yet at this point.
EXPECT_GT(token, GetToken());
// Since we didn't call helper_.finish() the token did not pass.
// We won't be able to claim the free memory without waiting and
// as we've already met the memory limit we'll have to wait
// on the token.
int32_t id3 = -1;
unsigned int offset3 = 0xFFFFFFFFU;
void* mem3 = manager_->Alloc(kSize, &id3, &offset3);
ASSERT_TRUE(mem3);
EXPECT_NE(-1, id3);
// It will reuse the space from the second allocation just freed.
EXPECT_EQ(kSize, offset3);
// Expect one chunk to be allocated
EXPECT_EQ(1 * kChunkSize, manager_->allocated_memory());
manager_->Free(mem1);
manager_->Free(mem3);
}
TEST_F(MappedMemoryManagerTest, MaxAllocationTest) {
const unsigned int kSize = 1024;
// Reset the manager with a memory limit.
manager_ = std::make_unique<MappedMemoryManager>(helper_.get(), kSize);
const size_t kLimit = 512;
manager_->set_chunk_size_multiple(kLimit);
// Allocate twice the limit worth of memory (currently unbounded).
int32_t id1 = -1;
unsigned int offset1 = 0xFFFFFFFFU;
void* mem1 = manager_->Alloc(kLimit, &id1, &offset1);
ASSERT_TRUE(mem1);
EXPECT_NE(-1, id1);
EXPECT_EQ(0u, offset1);
int32_t id2 = -1;
unsigned int offset2 = 0xFFFFFFFFU;
void* mem2 = manager_->Alloc(kLimit, &id2, &offset2);
ASSERT_TRUE(mem2);
EXPECT_NE(-1, id2);
EXPECT_EQ(0u, offset2);
manager_->set_max_allocated_bytes(kLimit);
// A new allocation should now fail.
int32_t id3 = -1;
unsigned int offset3 = 0xFFFFFFFFU;
void* mem3 = manager_->Alloc(kLimit, &id3, &offset3);
ASSERT_FALSE(mem3);
EXPECT_EQ(-1, id3);
EXPECT_EQ(0xFFFFFFFFU, offset3);
manager_->Free(mem2);
// New allocation is over the limit but should reuse allocated space
int32_t id4 = -1;
unsigned int offset4 = 0xFFFFFFFFU;
void* mem4 = manager_->Alloc(kLimit, &id4, &offset4);
ASSERT_TRUE(mem4);
EXPECT_EQ(id2, id4);
EXPECT_EQ(offset2, offset4);
manager_->Free(mem1);
manager_->Free(mem4);
}
} // namespace gpu
|