From 0a82dfe2e0972afea588864add78fd653fa4afda Mon Sep 17 00:00:00 2001 From: Antonio SJ Musumeci Date: Sat, 21 Nov 2026 17:48:28 -0500 Subject: [PATCH] fix: cap thread pools at 64 and warn when clamped - Limit read-thread-count, process-thread-count and the cor/cosr pool size in func.readdir to 64 threads per pool. Apply the cap after resolving automatic and CPU-divided counts; keep disabled process pools disabled and preserve the configured strings. - Log a syslog warning identifying the setting and limit when clamped. For readdir, warn only when applying a valid concurrent policy, not during validation. - Document the fixed cap and warning behavior. Queue depths retain their separate limits and use the clamped thread count. - Resolve negative FUSE counts without overflowing on INT_MIN. --- mkdocs/docs/config/func_readdir.md | 10 ++++++++-- mkdocs/docs/config/options.md | 19 ++++++++++++------- mkdocs/docs/config/threads.md | 7 +++++++ src/fuse_readdir_factory.cpp | 16 +++++++++++++--- vendored/libfuse/include/thread_pool.hpp | 12 ++++++++++++ vendored/libfuse/lib/fuse_loop.cpp | 22 +++++++++++++++++----- 6 files changed, 69 insertions(+), 17 deletions(-) diff --git a/mkdocs/docs/config/func_readdir.md b/mkdocs/docs/config/func_readdir.md index 1a928daf..8249e83c 100644 --- a/mkdocs/docs/config/func_readdir.md +++ b/mkdocs/docs/config/func_readdir.md @@ -9,13 +9,19 @@ | Policy | Description | | ------ | ----------- | | seq | "sequential" : Iterate sequentially over branches in the order defined in `branches`. This is the default and traditional behavior found prior to the readdir policy introduction. This will be increasingly slower as more branches are added to the pool. Especially if needing to wait for drives to spin up or network filesystems to respond. | -| cosr:N:M | "concurrent open, sequential read" : Concurrently open branch directories using a thread pool and process them in the order defined in `branches`. This keeps memory and CPU usage low while also reducing the time spent waiting on branches to respond. `N` is the number of threads. If negative it will be the core count divided by `abs(N)`. `M` is the queue depth. If either value is `0` it will be decided based on system configuration. | +| cosr:N:M | "concurrent open, sequential read" : Concurrently open branch directories using a thread pool and process them in the order defined in `branches`. This keeps memory and CPU usage low while also reducing the time spent waiting on branches to respond. `N` is the number of threads, at most 64. If negative it will be the core count divided by `abs(N)`. `M` is the queue depth. If either value is `0` it will be decided based on system configuration. | | cosr:N | cosr:N:M with M = 0 | | cosr | cosr:N:M with N = 0 and M = 0 | -| cor:N:M | "concurrent open and read" : Concurrently open branch directories and immediately start reading their contents using a thread pool. This will result in slightly higher memory and CPU usage but reduced latency. Particularly when using higher latency / slower speed network filesystem branches. Unlike `seq` and `cosr` the order of files could change due the async nature of the thread pool. This should not be a problem since the order of files returned in not guaranteed. `N` is the number of threads. If negative it will be the core count divided by `abs(N)`. `M` is the queue depth. If either value is `0` it will be decided based on system configuration. | +| cor:N:M | "concurrent open and read" : Concurrently open branch directories and immediately start reading their contents using a thread pool. This will result in slightly higher memory and CPU usage but reduced latency. Particularly when using higher latency / slower speed network filesystem branches. Unlike `seq` and `cosr` the order of files could change due the async nature of the thread pool. This should not be a problem since the order of files returned in not guaranteed. `N` is the number of threads, at most 64. If negative it will be the core count divided by `abs(N)`. `M` is the queue depth. If either value is `0` it will be decided based on system configuration. | | cor:N | cosr:N:M with M = 0 | | cor | cosr:N:M with N = 0 and M = 0 | +The 64-thread cap also applies to resolved counts. When a valid `cor` +or `cosr` setting exceeds the limit, applying it clamps the count to +64 and logs a syslog warning (`LOG_WARNING`). Queue depths are not +capped to 64; the total queue depth is calculated using the capped +thread count. + Keep in mind that `readdir` mostly just provides a list of file names in a directory and possibly some basic metadata about said files. To know details about the files, as one would see from commands like diff --git a/mkdocs/docs/config/options.md b/mkdocs/docs/config/options.md index e26535b9..607484c6 100644 --- a/mkdocs/docs/config/options.md +++ b/mkdocs/docs/config/options.md @@ -140,21 +140,24 @@ config file. thread count is set negative it will look up the number of cores then divide by the absolute value. ie. threads=-2 on an 8 core machine will result in 8 / 2 = 4 threads. There will always be at - least 1 thread. If set to -1 in combination with - `process-thread-count` then it will try to pick reasonable values - based on CPU thread count. NOTE: higher number of threads increases - parallelism but usually decreases throughput. (default: 0) + least 1 thread and at most 64, including counts resolved from automatic + or negative settings. Counts above 64 are clamped with a syslog warning + (`LOG_WARNING`). If set to -1 in combination with `process-thread-count` + then it will try to pick reasonable values based on CPU thread count. NOTE: + higher number of threads increases parallelism but usually + decreases throughput. (default: 0) * **[process-thread-count](threads.md)=INT**: Enables separate thread pool to asynchronously process FUSE requests. In this mode `read-thread-count` refers to the number of threads reading FUSE messages which are dispatched to process threads. -1 means disabled - otherwise acts like `read-thread-count`. (default: -1) + otherwise acts like `read-thread-count`, including the 64-thread cap + and warning when clamped. (default: -1) * **[process-thread-queue-depth](threads.md)=UINT**: Sets the number of requests any single process thread can have queued up at one time. Meaning the total memory usage of the queues is queue depth multiplied by the number of process threads plus read thread count. 0 sets the depth to the same as the process thread - count. (default: 2) + count. Queue depths are not capped to 64. (default: 2) * **[pin-threads](pin-threads.md)=STR**: Selects a strategy to pin threads to CPUs (default: unset) * **[flush-on-close](flush-on-close.md)=never|always|opened-for-write**: @@ -173,7 +176,9 @@ config file. types. Example: **func.getattr=newest** * **[func.readdir](func_readdir.md)=seq|cosr|cor|cosr:INT|cor:INT**: Sets `readdir` policy. INT value sets the number of threads to use - for concurrency. (default: seq) + for concurrency, at most 64. Counts above 64 are clamped with a syslog + warning (`LOG_WARNING`) when applied. Queue depths are not capped to + 64. (default: seq) * **[category.action](functions_categories_policies.md)=POLICY**: Sets policy of all FUSE functions in the action category. (default: epall) diff --git a/mkdocs/docs/config/threads.md b/mkdocs/docs/config/threads.md index 29f2985a..917612e2 100644 --- a/mkdocs/docs/config/threads.md +++ b/mkdocs/docs/config/threads.md @@ -3,6 +3,13 @@ There are multiple thread pools used in mergerfs to provide parallel behaviors. +Each thread pool is limited to 64 threads. This applies to +`read-thread-count`, `process-thread-count` and the pool size in +[func.readdir](func_readdir.md). Counts resolved from automatic or +negative settings are also capped. When an applied setting exceeds +the limit, mergerfs reduces the count to 64 and logs a syslog warning +(`LOG_WARNING`). Queue depths are not capped to 64. + ## read-thread-count diff --git a/src/fuse_readdir_factory.cpp b/src/fuse_readdir_factory.cpp index 91d578d5..b7a69eec 100644 --- a/src/fuse_readdir_factory.cpp +++ b/src/fuse_readdir_factory.cpp @@ -23,7 +23,10 @@ #include "fuse_readdir_seq.hpp" #include "from_string.hpp" +#include "syslog.hpp" +#include "thread_pool.hpp" +#include #include #include #include @@ -42,7 +45,8 @@ void _read_cfg(const std::string_view str_, std::string &type_, int &concurrency_, - int &max_queue_depth_) + int &max_queue_depth_, + const bool warn_) { std::string type; int concurrency; @@ -86,6 +90,8 @@ _read_cfg(const std::string_view str_, if(concurrency <= 0) concurrency = 1; + const bool clamped = (concurrency > ThreadPool::max_thread_count()); + concurrency = std::min(concurrency,ThreadPool::max_thread_count()); if(max_queue_depth <= 0) max_queue_depth = 2; @@ -96,6 +102,10 @@ _read_cfg(const std::string_view str_, else max_queue_depth *= concurrency; + if(warn_ && clamped && ((type == "cor") || (type == "cosr"))) + SysLog::warning("func.readdir={} exceeds the thread limit; clamping to {} threads", + str_,ThreadPool::max_thread_count()); + type_ = type; concurrency_ = concurrency; max_queue_depth_ = max_queue_depth; @@ -108,7 +118,7 @@ FUSE::ReadDirFactory::valid(const std::string_view str_) int max_queue_depth; std::string type; - ::_read_cfg(str_,type,concurrency,max_queue_depth); + ::_read_cfg(str_,type,concurrency,max_queue_depth,false); return ((type == "seq") || (type == "cosr") || @@ -122,7 +132,7 @@ FUSE::ReadDirFactory::make(const std::string_view str_) int max_queue_depth; std::string type; - ::_read_cfg(str_,type,concurrency,max_queue_depth); + ::_read_cfg(str_,type,concurrency,max_queue_depth,true); if(type == "seq") return std::make_shared(); diff --git a/vendored/libfuse/include/thread_pool.hpp b/vendored/libfuse/include/thread_pool.hpp index d32061d5..1f57c7ad 100644 --- a/vendored/libfuse/include/thread_pool.hpp +++ b/vendored/libfuse/include/thread_pool.hpp @@ -110,6 +110,9 @@ public: std::future> enqueue_task(FuncType&& func_); +public: + static constexpr int max_thread_count(); + public: std::vector threads() const; ThreadPool::PToken ptoken(); @@ -125,6 +128,15 @@ private: +// Cap configured pools to bound their memory and thread creation costs. +inline +constexpr int +ThreadPool::max_thread_count() +{ + return 64; +} + + inline ThreadPool::ThreadPool(const unsigned thread_count_, const unsigned max_queue_depth_, diff --git a/vendored/libfuse/lib/fuse_loop.cpp b/vendored/libfuse/lib/fuse_loop.cpp index f554dea8..37998813 100644 --- a/vendored/libfuse/lib/fuse_loop.cpp +++ b/vendored/libfuse/lib/fuse_loop.cpp @@ -199,7 +199,8 @@ struct SyncWorker static int -_calculate_thread_count(const int raw_thread_count_) +_calculate_thread_count(const int raw_thread_count_, + const char *option_) { int thread_count = 1; @@ -210,7 +211,7 @@ _calculate_thread_count(const int raw_thread_count_) } else if(raw_thread_count_ < 0) { - thread_count = (::_nproc() / -raw_thread_count_); + thread_count = (::_nproc() / -(long long)raw_thread_count_); thread_count = std::max(1,thread_count); } else if(raw_thread_count_ > 0) @@ -218,6 +219,14 @@ _calculate_thread_count(const int raw_thread_count_) thread_count = raw_thread_count_; } + if(thread_count > ThreadPool::max_thread_count()) + { + SysLog::warning("{}={} requests {} threads; limiting pool to {}", + option_,raw_thread_count_,thread_count, + ThreadPool::max_thread_count()); + thread_count = ThreadPool::max_thread_count(); + } + return thread_count; } @@ -246,13 +255,16 @@ _calculate_thread_counts(int *read_thread_count_, else if((*read_thread_count_ == 0) && (*process_thread_count_ != -1)) { *read_thread_count_ = 2; - *process_thread_count_ = ::_calculate_thread_count(*process_thread_count_); + *process_thread_count_ = ::_calculate_thread_count(*process_thread_count_, + "process-thread-count"); } else { - *read_thread_count_ = ::_calculate_thread_count(*read_thread_count_); + *read_thread_count_ = ::_calculate_thread_count(*read_thread_count_, + "read-thread-count"); if(*process_thread_count_ != -1) - *process_thread_count_ = ::_calculate_thread_count(*process_thread_count_); + *process_thread_count_ = ::_calculate_thread_count(*process_thread_count_, + "process-thread-count"); } if(*process_thread_queue_depth_ <= 0)