refactor(cached_block): move to internal namespace

This commit is contained in:
Marcus Holland-Moritz 2024-07-28 11:00:31 +02:00
parent 7ff742d38d
commit b273e2fd3d
7 changed files with 43 additions and 28 deletions

View File

@ -630,10 +630,10 @@ list(APPEND LIBDWARFS_COMMON_SRC
list(APPEND LIBDWARFS_READER_SRC list(APPEND LIBDWARFS_READER_SRC
src/dwarfs/block_range.cpp src/dwarfs/block_range.cpp
src/dwarfs/cached_block.cpp
src/dwarfs/filesystem_v2.cpp src/dwarfs/filesystem_v2.cpp
src/dwarfs/fs_section.cpp src/dwarfs/fs_section.cpp
src/dwarfs/internal/block_cache.cpp src/dwarfs/internal/block_cache.cpp
src/dwarfs/internal/cached_block.cpp
src/dwarfs/internal/inode_reader_v2.cpp src/dwarfs/internal/inode_reader_v2.cpp
src/dwarfs/internal/metadata_types.cpp src/dwarfs/internal/metadata_types.cpp
src/dwarfs/internal/metadata_v2.cpp src/dwarfs/internal/metadata_v2.cpp

View File

@ -27,13 +27,17 @@
namespace dwarfs { namespace dwarfs {
namespace internal {
class cached_block; class cached_block;
} // namespace internal
class block_range { class block_range {
public: public:
block_range(uint8_t const* data, size_t offset, size_t size); block_range(uint8_t const* data, size_t offset, size_t size);
block_range(std::shared_ptr<cached_block const> block, size_t offset, block_range(std::shared_ptr<internal::cached_block const> block,
size_t size); size_t offset, size_t size);
auto data() const { return span_.data(); } auto data() const { return span_.data(); }
auto begin() const { return span_.begin(); } auto begin() const { return span_.begin(); }
@ -42,7 +46,7 @@ class block_range {
private: private:
std::span<uint8_t const> span_; std::span<uint8_t const> span_;
std::shared_ptr<cached_block const> block_; std::shared_ptr<internal::cached_block const> block_;
}; };
} // namespace dwarfs } // namespace dwarfs

View File

@ -32,6 +32,8 @@ class logger;
class fs_section; class fs_section;
class mmif; class mmif;
namespace internal {
class cached_block { class cached_block {
public: public:
static std::unique_ptr<cached_block> static std::unique_ptr<cached_block>
@ -50,4 +52,6 @@ class cached_block {
virtual bool any_pages_swapped_out(std::vector<uint8_t>& tmp) const = 0; virtual bool any_pages_swapped_out(std::vector<uint8_t>& tmp) const = 0;
}; };
} // namespace internal
} // namespace dwarfs } // namespace dwarfs

View File

@ -22,8 +22,8 @@
#include <fmt/format.h> #include <fmt/format.h>
#include <dwarfs/block_range.h> #include <dwarfs/block_range.h>
#include <dwarfs/cached_block.h>
#include <dwarfs/error.h> #include <dwarfs/error.h>
#include <dwarfs/internal/cached_block.h>
namespace dwarfs { namespace dwarfs {
@ -34,7 +34,7 @@ block_range::block_range(uint8_t const* data, size_t offset, size_t size)
} }
} }
block_range::block_range(std::shared_ptr<cached_block const> block, block_range::block_range(std::shared_ptr<internal::cached_block const> block,
size_t offset, size_t size) size_t offset, size_t size)
: span_{block->data() + offset, size} : span_{block->data() + offset, size}
, block_{std::move(block)} { , block_{std::move(block)} {

View File

@ -42,9 +42,9 @@
#include <folly/stats/Histogram.h> #include <folly/stats/Histogram.h>
#include <folly/system/ThreadName.h> #include <folly/system/ThreadName.h>
#include <dwarfs/cached_block.h>
#include <dwarfs/fs_section.h> #include <dwarfs/fs_section.h>
#include <dwarfs/internal/block_cache.h> #include <dwarfs/internal/block_cache.h>
#include <dwarfs/internal/cached_block.h>
#include <dwarfs/internal/worker_group.h> #include <dwarfs/internal/worker_group.h>
#include <dwarfs/logger.h> #include <dwarfs/logger.h>
#include <dwarfs/mmif.h> #include <dwarfs/mmif.h>
@ -146,7 +146,7 @@ class block_request {
size_t end() const { return end_; } size_t end() const { return end_; }
void fulfill(std::shared_ptr<cached_block const> block) { void fulfill(std::shared_ptr<internal::cached_block const> block) {
promise_.set_value(block_range(std::move(block), begin_, end_ - begin_)); promise_.set_value(block_range(std::move(block), begin_, end_ - begin_));
} }
@ -160,7 +160,8 @@ class block_request {
class block_request_set { class block_request_set {
public: public:
block_request_set(std::shared_ptr<cached_block> block, size_t block_no) block_request_set(std::shared_ptr<internal::cached_block> block,
size_t block_no)
: range_end_(0) : range_end_(0)
, block_(std::move(block)) , block_(std::move(block))
, block_no_(block_no) {} , block_no_(block_no) {}
@ -196,14 +197,14 @@ class block_request_set {
bool empty() const { return queue_.empty(); } bool empty() const { return queue_.empty(); }
std::shared_ptr<cached_block> block() const { return block_; } std::shared_ptr<internal::cached_block> block() const { return block_; }
size_t block_no() const { return block_no_; } size_t block_no() const { return block_no_; }
private: private:
std::vector<block_request> queue_; std::vector<block_request> queue_;
size_t range_end_; size_t range_end_;
std::shared_ptr<cached_block> block_; std::shared_ptr<internal::cached_block> block_;
const size_t block_no_; const size_t block_no_;
}; };
@ -327,7 +328,8 @@ class block_cache_ final : public block_cache::impl {
cache_.~lru_type(); cache_.~lru_type();
new (&cache_) lru_type(max_blocks); new (&cache_) lru_type(max_blocks);
cache_.setPruneHook( cache_.setPruneHook(
[this](size_t block_no, std::shared_ptr<cached_block>&& block) { [this](size_t block_no,
std::shared_ptr<internal::cached_block>&& block) {
LOG_DEBUG << "evicting block " << block_no LOG_DEBUG << "evicting block " << block_no
<< " from cache, decompression ratio = " << " from cache, decompression ratio = "
<< double(block->range_end()) / << double(block->range_end()) /
@ -552,9 +554,10 @@ class block_cache_ final : public block_cache::impl {
void create_cached_block(size_t block_no, std::promise<block_range>&& promise, void create_cached_block(size_t block_no, std::promise<block_range>&& promise,
size_t offset, size_t range_end) const { size_t offset, size_t range_end) const {
try { try {
std::shared_ptr<cached_block> block = cached_block::create( std::shared_ptr<internal::cached_block> block =
LOG_GET_LOGGER, DWARFS_NOTHROW(block_.at(block_no)), mm_, internal::cached_block::create(
options_.mm_release, options_.disable_block_integrity_check); LOG_GET_LOGGER, DWARFS_NOTHROW(block_.at(block_no)), mm_,
options_.mm_release, options_.disable_block_integrity_check);
blocks_created_.fetch_add(1, std::memory_order_relaxed); blocks_created_.fetch_add(1, std::memory_order_relaxed);
// Make a new set for the block // Make a new set for the block
@ -582,7 +585,7 @@ class block_cache_ final : public block_cache::impl {
tidy_thread_.join(); tidy_thread_.join();
} }
void update_block_stats(cached_block const& cb) { void update_block_stats(internal::cached_block const& cb) {
if (cb.range_end() < cb.uncompressed_size()) { if (cb.range_end() < cb.uncompressed_size()) {
partially_decompressed_.fetch_add(1, std::memory_order_relaxed); partially_decompressed_.fetch_add(1, std::memory_order_relaxed);
} }
@ -725,16 +728,16 @@ class block_cache_ final : public block_cache::impl {
std::cv_status::timeout) { std::cv_status::timeout) {
switch (tidy_config_.strategy) { switch (tidy_config_.strategy) {
case cache_tidy_strategy::EXPIRY_TIME: case cache_tidy_strategy::EXPIRY_TIME:
remove_block_if( remove_block_if([tp = std::chrono::steady_clock::now() -
[tp = std::chrono::steady_clock::now() - tidy_config_.expiry_time](
tidy_config_.expiry_time](cached_block const& blk) { internal::cached_block const& blk) {
return blk.last_used_before(tp); return blk.last_used_before(tp);
}); });
break; break;
case cache_tidy_strategy::BLOCK_SWAPPED_OUT: { case cache_tidy_strategy::BLOCK_SWAPPED_OUT: {
std::vector<uint8_t> tmp; std::vector<uint8_t> tmp;
remove_block_if([&tmp](cached_block const& blk) { remove_block_if([&tmp](internal::cached_block const& blk) {
return blk.any_pages_swapped_out(tmp); return blk.any_pages_swapped_out(tmp);
}); });
} break; } break;
@ -747,7 +750,7 @@ class block_cache_ final : public block_cache::impl {
} }
using lru_type = using lru_type =
folly::EvictingCacheMap<size_t, std::shared_ptr<cached_block>>; folly::EvictingCacheMap<size_t, std::shared_ptr<internal::cached_block>>;
mutable std::mutex mx_; mutable std::mutex mx_;
mutable lru_type cache_; mutable lru_type cache_;

View File

@ -26,13 +26,15 @@
#endif #endif
#include <dwarfs/block_compressor.h> #include <dwarfs/block_compressor.h>
#include <dwarfs/cached_block.h>
#include <dwarfs/error.h> #include <dwarfs/error.h>
#include <dwarfs/fs_section.h> #include <dwarfs/fs_section.h>
#include <dwarfs/internal/cached_block.h>
#include <dwarfs/logger.h> #include <dwarfs/logger.h>
#include <dwarfs/mmif.h> #include <dwarfs/mmif.h>
namespace dwarfs { namespace dwarfs::internal {
namespace {
template <typename LoggerPolicy> template <typename LoggerPolicy>
class cached_block_ final : public cached_block { class cached_block_ final : public cached_block {
@ -127,6 +129,8 @@ class cached_block_ final : public cached_block {
std::chrono::steady_clock::time_point last_access_; std::chrono::steady_clock::time_point last_access_;
}; };
} // namespace
std::unique_ptr<cached_block> std::unique_ptr<cached_block>
cached_block::create(logger& lgr, fs_section const& b, std::shared_ptr<mmif> mm, cached_block::create(logger& lgr, fs_section const& b, std::shared_ptr<mmif> mm,
bool release, bool disable_integrity_check) { bool release, bool disable_integrity_check) {
@ -135,4 +139,4 @@ cached_block::create(logger& lgr, fs_section const& b, std::shared_ptr<mmif> mm,
lgr, b, std::move(mm), release, disable_integrity_check); lgr, b, std::move(mm), release, disable_integrity_check);
} }
} // namespace dwarfs } // namespace dwarfs::internal

View File

@ -33,9 +33,9 @@
#include <folly/container/Enumerate.h> #include <folly/container/Enumerate.h>
#include <dwarfs/block_range.h> #include <dwarfs/block_range.h>
#include <dwarfs/cached_block.h>
#include <dwarfs/error.h> #include <dwarfs/error.h>
#include <dwarfs/filesystem_v2.h> #include <dwarfs/filesystem_v2.h>
#include <dwarfs/internal/cached_block.h>
#include <dwarfs_tool_main.h> #include <dwarfs_tool_main.h>
#include "mmap_mock.h" #include "mmap_mock.h"
@ -46,7 +46,7 @@ using namespace dwarfs;
namespace { namespace {
class mock_cached_block : public cached_block { class mock_cached_block : public internal::cached_block {
public: public:
mock_cached_block() = default; mock_cached_block() = default;
mock_cached_block(std::span<uint8_t const> span) mock_cached_block(std::span<uint8_t const> span)