Skip to content

Commit

Permalink
(WIP) decoupling segment storage
Browse files Browse the repository at this point in the history
  • Loading branch information
KIwabuchi committed Dec 1, 2023
1 parent 85d6099 commit be278be
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 63 deletions.
22 changes: 10 additions & 12 deletions include/metall/kernel/data_storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ class data_storage {
using imp_type = data_storage_impl;

public:
// TODO: Change design
using size_type = typename imp_type::size_type;
using different_type = typename imp_type::different_type;

using path_type = typename imp_type::path_type;

data_storage() = default;
Expand All @@ -39,7 +35,7 @@ class data_storage {
/// \brief Gets the size of an existing segment.
/// This is a static version of size() method.
/// \param base_path A path to a segment.
inline static size_type get_size(const path_type &base_path) {
inline static std::size_t get_size(const path_type &base_path) {
return imp_type::get_size(base_path);
}

Expand Down Expand Up @@ -70,8 +66,9 @@ class data_storage {
/// vm_region_size The size of a reserved VM region. \param vm_region The
/// address of a reserved VM region. \block_size The block size. \return
/// Return true if success; otherwise, false.
inline bool create(const path_type &base_path, const size_type vm_region_size,
void *const vm_region, const size_type block_size) {
inline bool create(const path_type &base_path,
const std::size_t vm_region_size, void *const vm_region,
const std::size_t block_size) {
return m_impl.create(base_path, vm_region_size, vm_region, block_size);
}

Expand All @@ -81,7 +78,7 @@ class data_storage {
/// vm_region_size The size of a VM region. \param vm_region The address of a
/// VM region. \param read_only If true, this segment is read only. \return
/// Return true if success; otherwise, false.
inline bool open(const path_type &base_path, const size_type vm_region_size,
inline bool open(const path_type &base_path, const std::size_t vm_region_size,
void *const vm_region, const bool read_only) {
return m_impl.open(base_path, vm_region_size, vm_region, read_only);
}
Expand All @@ -90,7 +87,7 @@ class data_storage {
/// \param request_size A segment size to extend to.
/// \return Returns true if the segment is extended to or already larger than
/// the requested size. Returns false on failure.
inline bool extend(const size_type request_size) {
inline bool extend(const std::size_t request_size) {
return m_impl.extend(request_size);
}

Expand All @@ -107,7 +104,8 @@ class data_storage {
/// The actual behavior depends on the running system.
/// \param offset An offset to the region from the beginning of the segment.
/// \param nbytes The size of the region.
inline bool free_region(const different_type offset, const size_type nbytes) {
inline bool free_region(const std::ptrdiff_t offset,
const std::size_t nbytes) {
return m_impl.free_region(offset, nbytes);
}

Expand All @@ -117,11 +115,11 @@ class data_storage {

/// \brief Returns the current size.
/// \return The current segment size.
inline size_type size() const { return m_impl.size(); }
inline std::size_t size() const { return m_impl.size(); }

/// \brief Returns the page size.
/// \return The page size of the system.
inline size_type page_size() const { return m_impl.page_size(); }
inline std::size_t page_size() const { return m_impl.page_size(); }

/// \brief Checks if the segment is read only.
/// \return Returns true if the segment is read only; otherwise, returns
Expand Down
101 changes: 50 additions & 51 deletions include/metall/kernel/mmap_data_storage.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ namespace mdtl = metall::mtlldetail;

class mmap_data_storage {
public:
using size_type = std::size_t;
using different_type = std::ptrdiff_t;
using path_type = std::string;

inline static path_type get_path(const path_type &base_path) {
Expand Down Expand Up @@ -105,9 +103,9 @@ class mmap_data_storage {
/// \brief Gets the size of an existing segment.
/// This is a static version of size() method.
/// \param base_path A path to a segment.
static size_type get_size(const std::string &base_path) {
static std::size_t get_size(const path_type &base_path) {
int block_no = 0;
size_type total_file_size = 0;
std::size_t total_file_size = 0;
while (true) {
const auto file_name = priv_make_block_file_name(base_path, block_no);
if (!mdtl::file_exist(file_name)) {
Expand All @@ -122,7 +120,7 @@ class mmap_data_storage {
/// \brief Checks if a segment is openable.
/// \param base_path A path to a segment.
/// \return Return true if success; otherwise, false.
static bool openable(const std::string &base_path) {
static bool openable(const path_type &base_path) {
const auto file_name = priv_make_block_file_name(base_path, 0);
return mdtl::file_exist(file_name);
}
Expand All @@ -134,8 +132,8 @@ class mmap_data_storage {
/// \param max_num_threads The maximum number of threads to use.
/// If <= 0 is given, the value is automatically determined.
/// \return Return true if success; otherwise, false.
static bool copy(const std::string &source_path,
const std::string &destination_path, const bool clone,
static bool copy(const path_type &source_path,
const path_type &destination_path, const bool clone,
const int max_num_threads) {
return priv_copy(source_path, destination_path, clone, max_num_threads);
}
Expand All @@ -146,8 +144,8 @@ class mmap_data_storage {
/// vm_region_size The size of a reserved VM region. \param vm_region The
/// address of a reserved VM region. \block_size The block size. \return
/// Return true if success; otherwise, false.
bool create(const std::string &base_path, const size_type vm_region_size,
void *const vm_region, const size_type block_size) {
bool create(const path_type &base_path, const std::size_t vm_region_size,
void *const vm_region, const std::size_t block_size) {
return priv_create(base_path, vm_region_size, vm_region, block_size);
}

Expand All @@ -157,7 +155,7 @@ class mmap_data_storage {
/// vm_region_size The size of a VM region. \param vm_region The address of a
/// VM region. \param read_only If true, this segment is read only. \return
/// Return true if success; otherwise, false.
bool open(const std::string &base_path, const size_type vm_region_size,
bool open(const path_type &base_path, const std::size_t vm_region_size,
void *const vm_region, const bool read_only) {
return priv_open(base_path, vm_region_size, vm_region, read_only);
}
Expand All @@ -166,7 +164,7 @@ class mmap_data_storage {
/// \param request_size A segment size to extend to.
/// \return Returns true if the segment is extended to or already larger than
/// the requested size. Returns false on failure.
bool extend(const size_type request_size) {
bool extend(const std::size_t request_size) {
return priv_extend(request_size);
}

Expand All @@ -183,7 +181,7 @@ class mmap_data_storage {
/// The actual behavior depends on the running system.
/// \param offset An offset to the region from the beginning of the segment.
/// \param nbytes The size of the region.
bool free_region(const different_type offset, const size_type nbytes) {
bool free_region(const std::ptrdiff_t offset, const std::size_t nbytes) {
return priv_free_region(
offset, nbytes); // Failing this operation is not a critical error
}
Expand All @@ -194,11 +192,11 @@ class mmap_data_storage {

/// \brief Returns the current size.
/// \return The current segment size.
size_type size() const { return m_current_segment_size; }
std::size_t size() const { return m_current_segment_size; }

/// \brief Returns the page size.
/// \return The page size of the system.
size_type page_size() const { return m_system_page_size; }
std::size_t page_size() const { return m_system_page_size; }

/// \brief Checks if the segment is read only.
/// \return Returns true if the segment is read only; otherwise, returns
Expand All @@ -215,8 +213,8 @@ class mmap_data_storage {
bool check_sanity() const { return !m_broken; }

private:
static std::string priv_make_block_file_name(const std::string &base_path,
const size_type n) {
static path_type priv_make_block_file_name(const path_type &base_path,
const std::size_t n) {
return base_path + "/block-" + std::to_string(n);
}

Expand All @@ -241,8 +239,8 @@ class mmap_data_storage {
m_block_size > 0);
}

static bool priv_copy(const std::string &source_path,
const std::string &destination_path, const bool clone,
static bool priv_copy(const path_type &source_path,
const path_type &destination_path, const bool clone,
const int max_num_threads) {
if (!mdtl::directory_exist(destination_path)) {
if (!mdtl::create_directory(destination_path)) {
Expand All @@ -267,8 +265,8 @@ class mmap_data_storage {
return false;
}

bool priv_create(const std::string &base_path, const size_type vm_region_size,
void *const vm_region, const size_type block_size) {
bool priv_create(const path_type &base_path, const std::size_t vm_region_size,
void *const vm_region, const std::size_t block_size) {
if (!check_sanity()) return false;
if (is_open())
return false; // Cannot open multiple segments simultaneously.
Expand Down Expand Up @@ -315,7 +313,7 @@ class mmap_data_storage {
return true;
}

bool priv_open(const std::string &base_path, const size_type vm_region_size,
bool priv_open(const path_type &base_path, const std::size_t vm_region_size,
void *const vm_region, const bool read_only) {
if (!check_sanity()) return false;
if (is_open())
Expand Down Expand Up @@ -343,7 +341,7 @@ class mmap_data_storage {

const auto file_size = mdtl::get_file_size(file_name);
assert(file_size % page_size() == 0);
if (m_block_size > 0 && m_block_size != (size_type)file_size) {
if (m_block_size > 0 && m_block_size != (std::size_t)file_size) {
logger::out(logger::level::error, __FILE__, __LINE__,
"File sizes are not the same");
priv_destroy_segment();
Expand Down Expand Up @@ -386,7 +384,7 @@ class mmap_data_storage {
return true;
}

bool priv_extend(const size_type request_size) {
bool priv_extend(const std::size_t request_size) {
if (!is_open()) return false;

if (m_read_only) {
Expand Down Expand Up @@ -419,11 +417,11 @@ class mmap_data_storage {
return true;
}

bool priv_create_new_map(const std::string &base_path,
const size_type block_number,
const size_type file_size,
const different_type segment_offset) {
const std::string file_name =
bool priv_create_new_map(const path_type &base_path,
const std::size_t block_number,
const std::size_t file_size,
const std::ptrdiff_t segment_offset) {
const path_type file_name =
priv_make_block_file_name(base_path, block_number);
{
std::string s("Create and extend a file " + file_name + " with " +
Expand All @@ -433,7 +431,7 @@ class mmap_data_storage {

if (!mdtl::create_file(file_name)) return false;
if (!mdtl::extend_file_size(file_name, file_size)) return false;
if (static_cast<size_type>(mdtl::get_file_size(file_name)) < file_size) {
if (static_cast<std::size_t>(mdtl::get_file_size(file_name)) < file_size) {
std::string s("Failed to create and extend file: " + file_name);
logger::out(logger::level::error, __FILE__, __LINE__, s.c_str());
return false;
Expand All @@ -459,8 +457,8 @@ class mmap_data_storage {
return true;
}

int priv_map_file(const std::string &path, const size_type file_size,
const different_type segment_offset,
int priv_map_file(const path_type &path, const std::size_t file_size,
const std::ptrdiff_t segment_offset,
const bool read_only) const {
assert(!path.empty());
assert(file_size > 0);
Expand Down Expand Up @@ -501,8 +499,8 @@ class mmap_data_storage {
return ret.first;
}

int priv_map_anonymous(const std::string &path, const size_type region_size,
const different_type segment_offset) const {
int priv_map_anonymous(const path_type &path, const std::size_t region_size,
const std::ptrdiff_t segment_offset) const {
assert(!path.empty());
assert(region_size > 0);
assert(segment_offset >= 0);
Expand Down Expand Up @@ -643,8 +641,8 @@ class mmap_data_storage {
return num_successes == m_block_fd_list.size();
}

bool priv_free_region(const different_type offset,
const size_type nbytes) const {
bool priv_free_region(const std::ptrdiff_t offset,
const std::size_t nbytes) const {
if (!is_open() || m_read_only) return false;

if (offset + nbytes > m_current_segment_size) return false;
Expand All @@ -663,26 +661,26 @@ class mmap_data_storage {
return priv_uncommit_pages(offset, nbytes);
}

bool priv_uncommit_pages_and_free_file_space(const different_type offset,
const size_type nbytes) const {
bool priv_uncommit_pages_and_free_file_space(const std::ptrdiff_t offset,
const std::size_t nbytes) const {
return mdtl::uncommit_shared_pages_and_free_file_space(
static_cast<char *>(m_segment) + offset, nbytes);
}

bool priv_uncommit_pages(const different_type offset,
const size_type nbytes) const {
bool priv_uncommit_pages(const std::ptrdiff_t offset,
const std::size_t nbytes) const {
return mdtl::uncommit_shared_pages(static_cast<char *>(m_segment) + offset,
nbytes);
}

bool priv_uncommit_private_anonymous_pages(const different_type offset,
const size_type nbytes) const {
bool priv_uncommit_private_anonymous_pages(const std::ptrdiff_t offset,
const std::size_t nbytes) const {
return mdtl::uncommit_private_anonymous_pages(
static_cast<char *>(m_segment) + offset, nbytes);
}

#ifdef METALL_USE_ANONYMOUS_NEW_MAP
bool priv_sync_anonymous_map(const size_type block_no) {
bool priv_sync_anonymous_map(const std::size_t block_no) {
assert(m_anonymous_map_flag_list[block_no]);
{
std::string s("Sync anonymous map at block " + std::to_string(block_no));
Expand Down Expand Up @@ -735,19 +733,20 @@ class mmap_data_storage {
return true;
}

bool priv_test_file_space_free(const std::string &base_path) {
bool priv_test_file_space_free(const path_type &base_path) {
#ifdef METALL_DISABLE_FREE_FILE_SPACE
m_free_file_space = false;
return true;
#endif

assert(m_system_page_size > 0);
const std::string file_path(base_path + "/test");
const size_type file_size = m_system_page_size * 2;
const path_type file_path(base_path + "/test");
const std::size_t file_size = m_system_page_size * 2;

if (!mdtl::create_file(file_path)) return false;
if (!mdtl::extend_file_size(file_path, file_size)) return false;
assert(static_cast<size_type>(mdtl::get_file_size(file_path)) >= file_size);
assert(static_cast<std::size_t>(mdtl::get_file_size(file_path)) >=
file_size);

const auto ret =
mdtl::map_file_write_mode(file_path, nullptr, file_size, 0);
Expand Down Expand Up @@ -795,15 +794,15 @@ class mmap_data_storage {
// Private fields
// -------------------- //
ssize_t m_system_page_size{0};
size_type m_num_blocks{0};
size_type m_vm_region_size{0};
size_type m_current_segment_size{0};
std::size_t m_num_blocks{0};
std::size_t m_vm_region_size{0};
std::size_t m_current_segment_size{0};
void *m_segment{nullptr};
std::string m_base_path;
path_type m_base_path;
bool m_read_only{false};
bool m_free_file_space{true};
std::vector<int> m_block_fd_list;
size_type m_block_size{0};
std::size_t m_block_size{0};
bool m_broken{false};
#ifdef METALL_USE_ANONYMOUS_NEW_MAP
std::vector<int> m_anonymous_map_flag_list;
Expand Down

0 comments on commit be278be

Please sign in to comment.