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
|
// Copyright 2020 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/390223051): Remove C-library calls to fix the errors.
#pragma allow_unsafe_libc_calls
#endif
#include "third_party/blink/renderer/platform/disk_data_allocator.h"
#include <cstring>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "base/files/file.h"
#include "base/files/file_util.h"
#include "base/rand_util.h"
#include "base/test/scoped_feature_list.h"
#include "base/test/task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/renderer/platform/disk_data_allocator_test_utils.h"
#include "third_party/blink/renderer/platform/disk_data_metadata.h"
using ThreadPoolExecutionMode =
base::test::TaskEnvironment::ThreadPoolExecutionMode;
namespace blink {
class DiskDataAllocatorTest : public ::testing::Test {
public:
explicit DiskDataAllocatorTest(
ThreadPoolExecutionMode thread_pool_execution_mode =
ThreadPoolExecutionMode::DEFAULT)
: task_environment_(base::test::TaskEnvironment::TimeSource::MOCK_TIME,
thread_pool_execution_mode) {}
static std::vector<std::unique_ptr<DiskDataMetadata>>
Allocate(InMemoryDataAllocator* allocator, size_t size, size_t count) {
std::string random_data = base::RandBytesAsString(size);
std::vector<std::unique_ptr<DiskDataMetadata>> all_metadata;
for (size_t i = 0; i < count; i++) {
auto reserved_chunk = allocator->TryReserveChunk(random_data.size());
auto metadata = allocator->Write(std::move(reserved_chunk),
base::as_byte_span(random_data));
EXPECT_TRUE(metadata);
EXPECT_EQ(metadata->start_offset(), static_cast<int64_t>(i * size));
all_metadata.push_back(std::move(metadata));
}
return all_metadata;
}
protected:
void SetUp() override {
// On some platforms, initialization takes time, though it happens when
// base::ThreadTicks is used. To prevent flakiness depending on test
// execution ordering, force initialization.
if (base::ThreadTicks::IsSupported())
base::ThreadTicks::WaitUntilInitialized();
}
base::test::TaskEnvironment task_environment_;
};
TEST_F(DiskDataAllocatorTest, ReserveChunk) {
InMemoryDataAllocator allocator;
auto reserved_chunk_1 = allocator.TryReserveChunk(100);
auto metadata_1 = reserved_chunk_1->Take();
EXPECT_EQ(0, metadata_1->start_offset());
auto reserved_chunk_2 = allocator.TryReserveChunk(100);
auto metadata_2 = reserved_chunk_2->Take();
EXPECT_EQ(100, metadata_2->start_offset());
// Reserved chunk can be released via |Discard()|
allocator.Discard(std::move(metadata_2));
// Second chunk is reused.
auto reserved_chunk_3 = allocator.TryReserveChunk(100);
auto metadata_3 = reserved_chunk_3->Take();
EXPECT_EQ(100, metadata_3->start_offset());
// If a ReservedChunk is destructed with DiskDataMetadata, the chunk is
// released automatically.
auto reserved_chunk_4 = allocator.TryReserveChunk(300);
reserved_chunk_4 = nullptr;
auto reserved_chunk_5 = allocator.TryReserveChunk(100);
auto metadata_5 = reserved_chunk_5->Take();
EXPECT_EQ(200, metadata_5->start_offset());
}
TEST_F(DiskDataAllocatorTest, ReadWrite) {
InMemoryDataAllocator allocator;
constexpr size_t kSize = 1000;
std::string random_data = base::RandBytesAsString(kSize);
auto reserved_chunk = allocator.TryReserveChunk(kSize);
ASSERT_TRUE(reserved_chunk);
auto metadata = allocator.Write(std::move(reserved_chunk),
base::as_byte_span(random_data));
EXPECT_TRUE(metadata);
EXPECT_EQ(kSize, metadata->size());
auto read_data = std::vector<char>(kSize);
allocator.Read(*metadata, base::as_writable_bytes(base::span(read_data)));
EXPECT_EQ(0, memcmp(&read_data[0], random_data.c_str(), kSize));
}
TEST_F(DiskDataAllocatorTest, ReadWriteDiscardMultiple) {
InMemoryDataAllocator allocator;
std::vector<std::pair<std::unique_ptr<DiskDataMetadata>, std::string>>
data_written;
for (int i = 0; i < 10; i++) {
int size = base::RandInt(100, 1000);
auto data = base::RandBytesAsString(size);
auto reserved_chunk = allocator.TryReserveChunk(size);
ASSERT_TRUE(reserved_chunk);
auto metadata =
allocator.Write(std::move(reserved_chunk), base::as_byte_span(data));
EXPECT_TRUE(metadata);
data_written.emplace_back(std::move(metadata), data);
}
base::RandomShuffle(data_written.begin(), data_written.end());
for (const auto& p : data_written) {
size_t size = p.first->size();
auto read_data = std::vector<char>(size);
allocator.Read(*p.first, base::as_writable_bytes(base::span(read_data)));
EXPECT_EQ(0, memcmp(&read_data[0], &p.second[0], size));
}
base::RandomShuffle(data_written.begin(), data_written.end());
for (auto& p : data_written) {
auto metadata = std::move(p.first);
allocator.Discard(std::move(metadata));
}
}
TEST_F(DiskDataAllocatorTest, WriteEventuallyFail) {
InMemoryDataAllocator allocator;
constexpr size_t kSize = 1 << 18;
std::string random_data = base::RandBytesAsString(kSize);
static_assert(4 * kSize == InMemoryDataAllocator::kMaxSize, "");
for (int i = 0; i < 4; i++) {
auto reserved_chunk = allocator.TryReserveChunk(random_data.size());
ASSERT_TRUE(reserved_chunk);
auto metadata = allocator.Write(std::move(reserved_chunk),
base::as_byte_span(random_data));
EXPECT_TRUE(metadata);
}
auto reserved_chunk = allocator.TryReserveChunk(random_data.size());
ASSERT_TRUE(reserved_chunk);
auto metadata = allocator.Write(std::move(reserved_chunk),
base::as_byte_span(random_data));
EXPECT_FALSE(metadata);
EXPECT_FALSE(allocator.may_write());
}
TEST_F(DiskDataAllocatorTest, CanReuseFreedChunk) {
InMemoryDataAllocator allocator;
constexpr size_t kSize = 1 << 10;
std::vector<std::unique_ptr<DiskDataMetadata>> all_metadata;
for (int i = 0; i < 10; i++) {
std::string random_data = base::RandBytesAsString(kSize);
auto reserved_chunk = allocator.TryReserveChunk(random_data.size());
ASSERT_TRUE(reserved_chunk);
auto metadata = allocator.Write(std::move(reserved_chunk),
base::as_byte_span(random_data));
EXPECT_TRUE(metadata);
all_metadata.push_back(std::move(metadata));
}
auto metadata = std::move(all_metadata[4]);
ASSERT_TRUE(metadata);
int64_t start_offset = metadata->start_offset();
allocator.Discard(std::move(metadata));
std::string random_data = base::RandBytesAsString(kSize);
auto reserved_chunk = allocator.TryReserveChunk(random_data.size());
ASSERT_TRUE(reserved_chunk);
auto new_metadata = allocator.Write(std::move(reserved_chunk),
base::as_byte_span(random_data));
EXPECT_TRUE(new_metadata);
EXPECT_EQ(new_metadata->start_offset(), start_offset);
}
TEST_F(DiskDataAllocatorTest, ExactThenWorstFit) {
InMemoryDataAllocator allocator;
int count = 10;
size_t size_increment = 1000;
std::vector<std::unique_ptr<DiskDataMetadata>> all_metadata;
size_t size = 10000;
// Allocate a bunch of random-sized
for (int i = 0; i < count; i++) {
std::string random_data = base::RandBytesAsString(size);
auto reserved_chunk = allocator.TryReserveChunk(random_data.size());
ASSERT_TRUE(reserved_chunk);
auto metadata = allocator.Write(std::move(reserved_chunk),
base::as_byte_span(random_data));
EXPECT_TRUE(metadata);
all_metadata.push_back(std::move(metadata));
size += size_increment;
}
auto& hole_metadata = all_metadata[4];
size_t hole_size = hole_metadata->size();
int64_t hole_offset = hole_metadata->start_offset();
allocator.Discard(std::move(hole_metadata));
auto& larger_hole_metadata = all_metadata[9];
int64_t larger_hole_offset = larger_hole_metadata->start_offset();
allocator.Discard(std::move(larger_hole_metadata));
std::string random_data = base::RandBytesAsString(hole_size);
auto reserved_chunk = allocator.TryReserveChunk(random_data.size());
ASSERT_TRUE(reserved_chunk);
auto metadata = allocator.Write(std::move(reserved_chunk),
base::as_byte_span(random_data));
EXPECT_TRUE(metadata);
// Exact fit.
EXPECT_EQ(metadata->start_offset(), hole_offset);
allocator.Discard(std::move(metadata));
// -1 to check that this is not best fit.
random_data = base::RandBytesAsString(hole_size - 1);
reserved_chunk = allocator.TryReserveChunk(random_data.size());
ASSERT_TRUE(reserved_chunk);
metadata = allocator.Write(std::move(reserved_chunk),
base::as_byte_span(random_data));
EXPECT_TRUE(metadata);
EXPECT_EQ(metadata->start_offset(), larger_hole_offset);
}
TEST_F(DiskDataAllocatorTest, FreeChunksMerging) {
constexpr size_t kSize = 100;
auto allocator = std::make_unique<InMemoryDataAllocator>();
auto chunks = Allocate(allocator.get(), kSize, 4);
EXPECT_EQ(static_cast<int64_t>(4 * kSize), allocator->disk_footprint());
EXPECT_EQ(0u, allocator->free_chunks_size());
// Layout is (indices in |chunks|):
// | 0 | 1 | 2 | 3 |
// Discarding a higher index after a lower one triggers merging on the left.
// Merge left.
allocator->Discard(std::move(chunks[0]));
EXPECT_EQ(1u, allocator->FreeChunks().size());
allocator->Discard(std::move(chunks[1]));
EXPECT_EQ(1u, allocator->FreeChunks().size());
EXPECT_EQ(2 * kSize, allocator->FreeChunks().begin()->second);
allocator->Discard(std::move(chunks[2]));
EXPECT_EQ(1u, allocator->FreeChunks().size());
EXPECT_EQ(3 * kSize, allocator->FreeChunks().begin()->second);
EXPECT_EQ(3 * kSize, allocator->free_chunks_size());
allocator->Discard(std::move(chunks[3]));
EXPECT_EQ(1u, allocator->FreeChunks().size());
EXPECT_EQ(4 * kSize, allocator->FreeChunks().begin()->second);
EXPECT_EQ(static_cast<int64_t>(4 * kSize), allocator->disk_footprint());
allocator = std::make_unique<InMemoryDataAllocator>();
chunks = Allocate(allocator.get(), kSize, 4);
// Merge right.
allocator->Discard(std::move(chunks[3]));
EXPECT_EQ(1u, allocator->FreeChunks().size());
allocator->Discard(std::move(chunks[2]));
EXPECT_EQ(1u, allocator->FreeChunks().size());
EXPECT_EQ(2 * kSize, allocator->FreeChunks().begin()->second);
allocator->Discard(std::move(chunks[0]));
EXPECT_EQ(2u, allocator->FreeChunks().size());
EXPECT_EQ(3 * kSize, allocator->free_chunks_size());
// Multiple merges: left, then right.
allocator->Discard(std::move(chunks[1]));
EXPECT_EQ(1u, allocator->FreeChunks().size());
allocator = std::make_unique<InMemoryDataAllocator>();
chunks = Allocate(allocator.get(), kSize, 4);
// Left then right merging.
allocator->Discard(std::move(chunks[0]));
allocator->Discard(std::move(chunks[2]));
EXPECT_EQ(2u, allocator->FreeChunks().size());
allocator->Discard(std::move(chunks[1]));
EXPECT_EQ(1u, allocator->FreeChunks().size());
}
TEST_F(DiskDataAllocatorTest, ProvideInvalidFile) {
DiskDataAllocator allocator;
EXPECT_FALSE(allocator.may_write());
allocator.ProvideTemporaryFile(base::File());
EXPECT_FALSE(allocator.may_write());
}
TEST_F(DiskDataAllocatorTest, ProvideValidFile) {
base::FilePath path;
if (!base::CreateTemporaryFile(&path))
GTEST_SKIP() << "Cannot create temporary file.";
int flags = base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_READ |
base::File::FLAG_WRITE | base::File::FLAG_DELETE_ON_CLOSE;
auto file = base::File(base::FilePath(path), flags);
if (!file.IsValid())
GTEST_SKIP() << "Cannot create temporary file.";
DiskDataAllocator allocator;
EXPECT_FALSE(allocator.may_write());
allocator.ProvideTemporaryFile(std::move(file));
EXPECT_TRUE(allocator.may_write());
// Test read/write with a real file.
constexpr size_t kSize = 1000;
std::string random_data = base::RandBytesAsString(kSize);
auto reserved_chunk = allocator.TryReserveChunk(random_data.size());
ASSERT_TRUE(reserved_chunk);
auto metadata = allocator.Write(std::move(reserved_chunk),
base::as_byte_span(random_data));
if (!metadata) {
GTEST_SKIP() << "Disk full?";
}
EXPECT_EQ(kSize, metadata->size());
auto read_data = std::vector<char>(kSize);
allocator.Read(*metadata, base::as_writable_bytes(base::span(read_data)));
EXPECT_EQ(0, memcmp(&read_data[0], random_data.c_str(), kSize));
}
TEST_F(DiskDataAllocatorTest, WriteWithLimitedCapacity) {
base::test::ScopedFeatureList features;
const std::vector<base::test::FeatureRefAndParams> enabled_features = {
{features::kCompressParkableStrings, {{"max_disk_capacity_mb", "1"}}}};
features.InitWithFeaturesAndParameters(enabled_features, {});
InMemoryDataAllocator allocator;
constexpr size_t kMB = 1024 * 1024;
{
// If we use max capacity, another reservation should not be possible.
auto reserved_chunk = allocator.TryReserveChunk(kMB);
ASSERT_TRUE(reserved_chunk);
auto reserved_chunk_failed = allocator.TryReserveChunk(1);
ASSERT_FALSE(reserved_chunk_failed);
// |reserved_chunk| will be released after this line.
}
// Tested condition:
// | 1 (1MB - 1000) | free (500) | 3 (100) | free (400) |
std::string random_data_1 = base::RandBytesAsString(kMB - 1000);
auto reserved_chunk = allocator.TryReserveChunk(random_data_1.size());
ASSERT_TRUE(reserved_chunk);
auto metadata_1 = allocator.Write(std::move(reserved_chunk),
base::as_byte_span(random_data_1));
EXPECT_TRUE(metadata_1);
std::string random_data_2 = base::RandBytesAsString(500);
reserved_chunk = allocator.TryReserveChunk(random_data_2.size());
ASSERT_TRUE(reserved_chunk);
auto metadata_2 = allocator.Write(std::move(reserved_chunk),
base::as_byte_span(random_data_2));
EXPECT_TRUE(metadata_2);
std::string random_data_3 = base::RandBytesAsString(100);
reserved_chunk = allocator.TryReserveChunk(random_data_3.size());
ASSERT_TRUE(reserved_chunk);
auto metadata_3 = allocator.Write(std::move(reserved_chunk),
base::as_byte_span(random_data_3));
EXPECT_TRUE(metadata_3);
allocator.Discard(std::move(metadata_2));
// Second slot should be available.
reserved_chunk = allocator.TryReserveChunk(450);
ASSERT_TRUE(reserved_chunk);
// Second slot is reserved. Now we should not find available slot.
std::string random_data_4 = base::RandBytesAsString(450);
auto reserved_chunk_2 = allocator.TryReserveChunk(random_data_4.size());
ASSERT_FALSE(reserved_chunk_2);
}
} // namespace blink
|