diff options
Diffstat (limited to 'vespalib')
7 files changed, 136 insertions, 0 deletions
diff --git a/vespalib/CMakeLists.txt b/vespalib/CMakeLists.txt index c2756f89288..6d71b2d05be 100644 --- a/vespalib/CMakeLists.txt +++ b/vespalib/CMakeLists.txt @@ -132,6 +132,7 @@ vespa_define_module( src/tests/util/generationhandler src/tests/util/generationhandler_stress src/tests/util/md5 + src/tests/util/mmap_file_allocator_factory src/tests/util/rcuvector src/tests/util/reusable_set src/tests/util/size_literals diff --git a/vespalib/src/tests/util/mmap_file_allocator_factory/.gitignore b/vespalib/src/tests/util/mmap_file_allocator_factory/.gitignore new file mode 100644 index 00000000000..a18e9aac589 --- /dev/null +++ b/vespalib/src/tests/util/mmap_file_allocator_factory/.gitignore @@ -0,0 +1 @@ +/mmap-file-allocator-factory-dir diff --git a/vespalib/src/tests/util/mmap_file_allocator_factory/CMakeLists.txt b/vespalib/src/tests/util/mmap_file_allocator_factory/CMakeLists.txt new file mode 100644 index 00000000000..c6122659a22 --- /dev/null +++ b/vespalib/src/tests/util/mmap_file_allocator_factory/CMakeLists.txt @@ -0,0 +1,9 @@ +# Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +vespa_add_executable(vespalib_mmap_file_allocator_factory_test_app TEST + SOURCES + mmap_file_allocator_factory_test.cpp + DEPENDS + vespalib + GTest::GTest +) +vespa_add_test(NAME vespalib_mmap_file_allocator_factory_test_app COMMAND vespalib_mmap_file_allocator_factory_test_app) diff --git a/vespalib/src/tests/util/mmap_file_allocator_factory/mmap_file_allocator_factory_test.cpp b/vespalib/src/tests/util/mmap_file_allocator_factory/mmap_file_allocator_factory_test.cpp new file mode 100644 index 00000000000..f5c0317f623 --- /dev/null +++ b/vespalib/src/tests/util/mmap_file_allocator_factory/mmap_file_allocator_factory_test.cpp @@ -0,0 +1,49 @@ +// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#include <vespa/vespalib/util/mmap_file_allocator_factory.h> +#include <vespa/vespalib/util/mmap_file_allocator.h> +#include <vespa/vespalib/util/memory_allocator.h> +#include <vespa/vespalib/gtest/gtest.h> + +using vespalib::alloc::MemoryAllocator; +using vespalib::alloc::MmapFileAllocator; +using vespalib::alloc::MmapFileAllocatorFactory; + +namespace { + +vespalib::string basedir("mmap-file-allocator-factory-dir"); + +bool is_mmap_file_allocator(const MemoryAllocator *allocator) +{ + return dynamic_cast<const MmapFileAllocator *>(allocator) != nullptr; +} + +} + +TEST(MmapFileAllocatorFactoryTest, empty_dir_gives_no_allocator) +{ + MmapFileAllocatorFactory::instance().setup(""); + auto allocator = MmapFileAllocatorFactory::instance().make_memory_allocator("foo"); + EXPECT_FALSE(allocator); +} + +TEST(MmapFileAllocatorFactoryTest, nonempty_dir_gives_allocator) +{ + MmapFileAllocatorFactory::instance().setup(basedir); + auto allocator0 = MmapFileAllocatorFactory::instance().make_memory_allocator("foo"); + auto allocator1 = MmapFileAllocatorFactory::instance().make_memory_allocator("bar"); + EXPECT_TRUE(is_mmap_file_allocator(allocator0.get())); + EXPECT_TRUE(is_mmap_file_allocator(allocator1.get())); + vespalib::string allocator0_dir(basedir + "/0.foo"); + vespalib::string allocator1_dir(basedir + "/1.bar"); + EXPECT_TRUE(isDirectory(allocator0_dir)); + EXPECT_TRUE(isDirectory(allocator1_dir)); + allocator0.reset(); + EXPECT_FALSE(isDirectory(allocator0_dir)); + allocator1.reset(); + EXPECT_FALSE(isDirectory(allocator1_dir)); + MmapFileAllocatorFactory::instance().setup(""); + rmdir(basedir, true); +} + +GTEST_MAIN_RUN_ALL_TESTS() diff --git a/vespalib/src/vespa/vespalib/util/CMakeLists.txt b/vespalib/src/vespa/vespalib/util/CMakeLists.txt index fcac7cde412..d934a7b38ca 100644 --- a/vespalib/src/vespa/vespalib/util/CMakeLists.txt +++ b/vespalib/src/vespa/vespalib/util/CMakeLists.txt @@ -32,6 +32,7 @@ vespa_add_library(vespalib_vespalib_util OBJECT lz4compressor.cpp md5.c mmap_file_allocator.cpp + mmap_file_allocator_factory.cpp printable.cpp priority_queue.cpp random.cpp diff --git a/vespalib/src/vespa/vespalib/util/mmap_file_allocator_factory.cpp b/vespalib/src/vespa/vespalib/util/mmap_file_allocator_factory.cpp new file mode 100644 index 00000000000..a575c08309d --- /dev/null +++ b/vespalib/src/vespa/vespalib/util/mmap_file_allocator_factory.cpp @@ -0,0 +1,44 @@ +// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#include "mmap_file_allocator_factory.h" +#include "mmap_file_allocator.h" +#include <vespa/vespalib/stllike/asciistream.h> + +namespace vespalib::alloc { + +MmapFileAllocatorFactory::MmapFileAllocatorFactory() + : _dir_name(), + _generation(0) +{ +} + +MmapFileAllocatorFactory::~MmapFileAllocatorFactory() +{ +} + +void +MmapFileAllocatorFactory::setup(const vespalib::string& dir_name) +{ + _dir_name = dir_name; + _generation = 0; +} + +std::unique_ptr<MemoryAllocator> +MmapFileAllocatorFactory::make_memory_allocator(const vespalib::string& name) +{ + if (_dir_name.empty()) { + return {}; + } + vespalib::asciistream os; + os << _dir_name << "/" << _generation.fetch_add(1) << "." << name; + return std::make_unique<MmapFileAllocator>(os.str()); +}; + +MmapFileAllocatorFactory& +MmapFileAllocatorFactory::instance() +{ + static MmapFileAllocatorFactory instance; + return instance; +} + +} diff --git a/vespalib/src/vespa/vespalib/util/mmap_file_allocator_factory.h b/vespalib/src/vespa/vespalib/util/mmap_file_allocator_factory.h new file mode 100644 index 00000000000..b2586787845 --- /dev/null +++ b/vespalib/src/vespa/vespalib/util/mmap_file_allocator_factory.h @@ -0,0 +1,31 @@ +// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. + +#pragma once + +#include <vespa/vespalib/stllike/string.h> +#include <memory> +#include <atomic> + +namespace vespalib::alloc { + +class MemoryAllocator; + +/* + * Class for creating an mmap file allocator on demand. + */ +class MmapFileAllocatorFactory { + vespalib::string _dir_name; + std::atomic<uint64_t> _generation; + + MmapFileAllocatorFactory(); + ~MmapFileAllocatorFactory(); + MmapFileAllocatorFactory(const MmapFileAllocatorFactory &) = delete; + MmapFileAllocatorFactory& operator=(const MmapFileAllocatorFactory &) = delete; +public: + void setup(const vespalib::string &dir_name); + std::unique_ptr<MemoryAllocator> make_memory_allocator(const vespalib::string& name); + + static MmapFileAllocatorFactory& instance(); +}; + +} |