refactor: lazily initialize block cache workers

This commit is contained in:
Marcus Holland-Moritz 2025-08-18 17:30:03 +02:00
parent 7f587a1b6d
commit 9a87749358
4 changed files with 24 additions and 13 deletions

View File

@ -43,7 +43,6 @@ struct block_cache_options {
size_t num_workers{0};
double decompress_ratio{1.0};
bool mm_release{true};
bool init_workers{true};
bool disable_block_integrity_check{false};
size_t sequential_access_detector_threshold{0};
block_cache_allocation_mode allocation_mode{

View File

@ -37,9 +37,9 @@ namespace dwarfs::reader {
std::ostream& operator<<(std::ostream& os, block_cache_options const& opts) {
os << fmt::format(
"max_bytes={}, num_workers={}, decompress_ratio={}, mm_release={}, "
"init_workers={}, disable_block_integrity_check={}",
"disable_block_integrity_check={}",
opts.max_bytes, opts.num_workers, opts.decompress_ratio, opts.mm_release,
opts.init_workers, opts.disable_block_integrity_check);
opts.disable_block_integrity_check);
return os;
}

View File

@ -244,13 +244,6 @@ class block_cache_ final : public block_cache::impl {
options.sequential_access_detector_threshold)}
, os_{os}
, options_(options) {
if (options.init_workers) {
wg_ = worker_group(lgr, os_, "blkcache",
std::max(options.num_workers > 0
? options.num_workers
: hardware_concurrency(),
static_cast<size_t>(1)));
}
cache_.set_prune_hook(
[this](size_t block_no, std::shared_ptr<cached_block>&& block) {
on_block_removed("evicted", block_no, std::move(block));
@ -263,9 +256,13 @@ class block_cache_ final : public block_cache::impl {
tidy_runner_.stop();
{
std::unique_lock lock(mx_wg_);
if (wg_) {
wg_.stop();
}
}
if (!blocks_created_.load()) {
return;
@ -604,7 +601,22 @@ class block_cache_ final : public block_cache::impl {
std::memory_order_relaxed);
}
void init_worker_group() const {
std::unique_lock lock(mx_wg_);
if (!wg_) {
wg_ = worker_group(LOG_GET_LOGGER, os_, "blkcache",
std::max(options_.num_workers > 0
? options_.num_workers
: hardware_concurrency(),
static_cast<size_t>(1)));
}
}
void enqueue_job(std::shared_ptr<block_request_set> brs) const {
// lazy initialization of worker group
std::call_once(wg_init_flag_, [this] { init_worker_group(); });
std::shared_lock lock(mx_wg_);
// Lambda needs to be mutable so we can actually move out of it
@ -789,6 +801,7 @@ class block_cache_ final : public block_cache::impl {
mutable std::shared_mutex mx_wg_;
mutable worker_group wg_;
mutable std::once_flag wg_init_flag_;
std::vector<fs_section> block_;
std::shared_ptr<mmif> mm_;
byte_buffer_factory buffer_factory_;

View File

@ -1492,7 +1492,6 @@ void load_filesystem(dwarfs_userdata& userdata) {
fsopts.block_cache.num_workers = opts.workers;
fsopts.block_cache.decompress_ratio = opts.decompress_ratio;
fsopts.block_cache.mm_release = !opts.cache_image;
fsopts.block_cache.init_workers = false;
fsopts.block_cache.sequential_access_detector_threshold =
opts.seq_detector_threshold;
fsopts.block_cache.allocation_mode = opts.block_allocator;