2024-12-30 09:06:47 +00:00
|
|
|
use std::{path::PathBuf, sync::Arc};
|
2024-12-22 15:09:30 +00:00
|
|
|
|
2025-04-22 01:41:02 +00:00
|
|
|
use tuwunel_core::{
|
2025-12-24 16:36:38 +00:00
|
|
|
Server, debug,
|
|
|
|
|
debug::INFO_SPAN_LEVEL,
|
|
|
|
|
debug_info, debug_warn, expected, info, is_equal_to,
|
2024-12-22 15:09:30 +00:00
|
|
|
utils::{
|
2025-12-24 16:36:38 +00:00
|
|
|
BoolExt,
|
2024-12-23 04:32:28 +00:00
|
|
|
math::usize_from_f64,
|
2024-12-30 09:06:47 +00:00
|
|
|
result::LogDebugErr,
|
2024-12-23 04:32:28 +00:00
|
|
|
stream,
|
2025-01-01 06:08:20 +00:00
|
|
|
stream::{AMPLIFICATION_LIMIT, WIDTH_LIMIT},
|
2025-12-24 16:36:38 +00:00
|
|
|
sys::{
|
|
|
|
|
compute::{CORES_MAX, available_parallelism, is_core_available},
|
|
|
|
|
storage,
|
|
|
|
|
},
|
2024-12-22 15:09:30 +00:00
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
use super::{QUEUE_LIMIT, WORKER_LIMIT};
|
|
|
|
|
|
2025-12-24 16:36:38 +00:00
|
|
|
#[tracing::instrument(
|
|
|
|
|
level = INFO_SPAN_LEVEL,
|
|
|
|
|
skip_all,
|
|
|
|
|
ret(level = "trace"),
|
|
|
|
|
)]
|
2024-12-22 15:09:30 +00:00
|
|
|
pub(super) fn configure(server: &Arc<Server>) -> (usize, Vec<usize>, Vec<usize>) {
|
|
|
|
|
let config = &server.config;
|
2025-12-24 16:36:38 +00:00
|
|
|
let num_cores = available_parallelism();
|
2024-12-22 15:09:30 +00:00
|
|
|
|
|
|
|
|
// This finds the block device and gathers all the properties we need.
|
2025-01-06 01:30:22 +00:00
|
|
|
let path: PathBuf = config.database_path.clone();
|
2025-04-22 04:42:26 +00:00
|
|
|
let device_name = storage::name_from_path(&path)
|
|
|
|
|
.log_debug_err()
|
|
|
|
|
.ok();
|
2025-12-24 16:36:38 +00:00
|
|
|
|
|
|
|
|
let devices = storage::md_discover(&path);
|
|
|
|
|
let topology_detected = devices.md.is_empty().is_false();
|
|
|
|
|
debug!(?topology_detected, ?device_name, ?devices);
|
2024-12-22 15:09:30 +00:00
|
|
|
|
|
|
|
|
// The default worker count is masked-on if we didn't find better information.
|
2025-12-24 16:36:38 +00:00
|
|
|
let default_worker_count = topology_detected
|
|
|
|
|
.is_false()
|
2025-04-22 04:42:26 +00:00
|
|
|
.then_some(config.db_pool_workers);
|
2024-12-22 15:09:30 +00:00
|
|
|
|
2025-12-24 16:36:38 +00:00
|
|
|
// Sum the total number of possible tags. When no hardware detected this will
|
|
|
|
|
// default to the default_worker_count
|
|
|
|
|
let total_tags = devices
|
|
|
|
|
.md
|
|
|
|
|
.iter()
|
|
|
|
|
.flat_map(|md| md.mq.iter())
|
|
|
|
|
.filter(|mq| mq.cpu_list.iter().copied().any(is_core_available))
|
|
|
|
|
.filter_map(|mq| mq.nr_tags)
|
|
|
|
|
.chain(default_worker_count)
|
|
|
|
|
.fold(0_usize, usize::saturating_add);
|
|
|
|
|
|
2024-12-22 15:09:30 +00:00
|
|
|
// Determine the worker groupings. Each indice represents a hardware queue and
|
2025-12-24 16:36:38 +00:00
|
|
|
// contains the number of workers which will service it. This vector is
|
|
|
|
|
// truncated to the number of cores on systems which have multiple hardware
|
|
|
|
|
// queues per core. When no hardware is detected this defaults to one queue with
|
|
|
|
|
// a default count of workers.
|
|
|
|
|
let worker_counts: Vec<_> = devices
|
|
|
|
|
.md
|
2024-12-22 15:09:30 +00:00
|
|
|
.iter()
|
2025-12-24 16:36:38 +00:00
|
|
|
.inspect(|md| debug!(?md))
|
|
|
|
|
.flat_map(|md| md.mq.iter())
|
2024-12-22 15:09:30 +00:00
|
|
|
.filter(|mq| mq.cpu_list.iter().copied().any(is_core_available))
|
|
|
|
|
.map(|mq| {
|
2025-01-06 01:30:22 +00:00
|
|
|
let shares = mq
|
|
|
|
|
.cpu_list
|
|
|
|
|
.iter()
|
|
|
|
|
.filter(|&&id| is_core_available(id))
|
|
|
|
|
.count()
|
|
|
|
|
.max(1);
|
|
|
|
|
|
2025-04-22 04:42:26 +00:00
|
|
|
let limit = config
|
|
|
|
|
.db_pool_workers_limit
|
|
|
|
|
.saturating_mul(shares);
|
2025-01-06 01:30:22 +00:00
|
|
|
|
2025-12-24 16:36:38 +00:00
|
|
|
let limit = devices.md.iter().fold(0_usize, |acc, mq| {
|
|
|
|
|
mq.nr_requests
|
|
|
|
|
.map(|nr| nr.min(limit))
|
|
|
|
|
.or(Some(limit))
|
|
|
|
|
.map(|nr| acc.saturating_add(nr))
|
|
|
|
|
.unwrap_or(acc)
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
debug!(?mq, ?shares, ?limit);
|
2025-01-06 01:30:22 +00:00
|
|
|
|
|
|
|
|
mq.nr_tags.unwrap_or(WORKER_LIMIT.0).min(limit)
|
2024-12-22 15:09:30 +00:00
|
|
|
})
|
|
|
|
|
.chain(default_worker_count)
|
2025-12-24 16:36:38 +00:00
|
|
|
.take(num_cores)
|
2024-12-22 15:09:30 +00:00
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
// Determine our software queue size for each hardware queue. This is the mpmc
|
|
|
|
|
// between the tokio worker and the pool worker.
|
|
|
|
|
let queue_sizes: Vec<_> = worker_counts
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|worker_count| {
|
|
|
|
|
worker_count
|
|
|
|
|
.saturating_mul(config.db_pool_queue_mult)
|
|
|
|
|
.clamp(QUEUE_LIMIT.0, QUEUE_LIMIT.1)
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
|
2025-12-24 16:36:38 +00:00
|
|
|
// Determine the CPU affinities of each hardware queue. Each indice is a core
|
|
|
|
|
// and each value is the associated hardware queue. On systems which share
|
|
|
|
|
// queues between cores some values will be repeated; on systems with multiple
|
|
|
|
|
// queues per core the affinities are assumed to match and we don't require a
|
|
|
|
|
// vector of vectors. There is a little hiftiness going on because cpu's which
|
|
|
|
|
// are not available to the process are filtered out, similar to the
|
|
|
|
|
// worker_counts.
|
|
|
|
|
let topology = devices
|
|
|
|
|
.md
|
2024-12-22 15:09:30 +00:00
|
|
|
.iter()
|
2025-12-24 16:36:38 +00:00
|
|
|
.flat_map(|md| md.mq.iter())
|
|
|
|
|
.fold(vec![0; CORES_MAX], |mut topology, mq| {
|
2024-12-22 15:09:30 +00:00
|
|
|
mq.cpu_list
|
|
|
|
|
.iter()
|
|
|
|
|
.filter(|&&id| is_core_available(id))
|
|
|
|
|
.for_each(|&id| {
|
|
|
|
|
topology[id] = mq.id;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
topology
|
2025-12-24 16:36:38 +00:00
|
|
|
})
|
|
|
|
|
.into_iter()
|
|
|
|
|
.take(num_cores)
|
|
|
|
|
.collect();
|
2024-12-22 15:09:30 +00:00
|
|
|
|
|
|
|
|
// Regardless of the capacity of all queues we establish some limit on the total
|
|
|
|
|
// number of workers; this is hopefully hinted by nr_requests.
|
2025-12-24 16:36:38 +00:00
|
|
|
let max_workers = devices
|
|
|
|
|
.md
|
2025-01-06 01:30:22 +00:00
|
|
|
.iter()
|
2025-12-24 16:36:38 +00:00
|
|
|
.flat_map(|md| md.mq.iter())
|
2025-01-06 01:30:22 +00:00
|
|
|
.filter_map(|mq| mq.nr_tags)
|
|
|
|
|
.chain(default_worker_count)
|
|
|
|
|
.fold(0_usize, usize::saturating_add)
|
|
|
|
|
.clamp(WORKER_LIMIT.0, WORKER_LIMIT.1);
|
2024-12-22 15:09:30 +00:00
|
|
|
|
|
|
|
|
// Determine the final worker count which we'll be spawning.
|
|
|
|
|
let total_workers = worker_counts
|
|
|
|
|
.iter()
|
|
|
|
|
.sum::<usize>()
|
|
|
|
|
.clamp(WORKER_LIMIT.0, max_workers);
|
|
|
|
|
|
2024-12-23 04:32:28 +00:00
|
|
|
// After computing all of the above we can update the global automatic stream
|
|
|
|
|
// width, hopefully with a better value tailored to this system.
|
2025-12-24 16:36:38 +00:00
|
|
|
let num_queues = queue_sizes.len();
|
2024-12-23 04:32:28 +00:00
|
|
|
if config.stream_width_scale > 0.0 {
|
2025-12-24 16:36:38 +00:00
|
|
|
update_stream_width(server, num_queues, total_workers, total_tags);
|
2024-12-23 04:32:28 +00:00
|
|
|
}
|
|
|
|
|
|
2025-12-24 16:36:38 +00:00
|
|
|
if topology_detected {
|
|
|
|
|
debug_info!(?topology, ?worker_counts, ?queue_sizes, "Frontend topology",);
|
|
|
|
|
info!(
|
|
|
|
|
device_name = ?device_name.as_deref().unwrap_or("None"),
|
|
|
|
|
?num_cores,
|
|
|
|
|
?num_queues,
|
|
|
|
|
?total_workers,
|
|
|
|
|
?total_tags,
|
|
|
|
|
stream_width = ?stream::automatic_width(),
|
|
|
|
|
amplification = ?stream::automatic_amplification(),
|
|
|
|
|
"Frontend topology",
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
debug_warn!(
|
|
|
|
|
device_name = ?device_name.as_deref().unwrap_or("None"),
|
|
|
|
|
?total_workers,
|
|
|
|
|
stream_width = ?stream::automatic_width(),
|
|
|
|
|
amplification = ?stream::automatic_amplification(),
|
|
|
|
|
"Storage hardware not detected for database directory; assuming defaults.",
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-01-06 01:30:22 +00:00
|
|
|
|
|
|
|
|
assert!(total_workers > 0, "some workers expected");
|
|
|
|
|
assert!(!queue_sizes.is_empty(), "some queues expected");
|
|
|
|
|
assert!(
|
|
|
|
|
!queue_sizes.iter().copied().any(is_equal_to!(0)),
|
|
|
|
|
"positive queue sizes expected"
|
|
|
|
|
);
|
2024-12-22 15:09:30 +00:00
|
|
|
|
|
|
|
|
(total_workers, queue_sizes, topology)
|
|
|
|
|
}
|
2024-12-23 04:32:28 +00:00
|
|
|
|
|
|
|
|
#[allow(clippy::as_conversions, clippy::cast_precision_loss)]
|
2025-12-24 16:36:38 +00:00
|
|
|
fn update_stream_width(
|
|
|
|
|
server: &Arc<Server>,
|
|
|
|
|
num_queues: usize,
|
|
|
|
|
total_workers: usize,
|
|
|
|
|
total_tags: usize,
|
|
|
|
|
) {
|
2024-12-23 04:32:28 +00:00
|
|
|
let config = &server.config;
|
|
|
|
|
let scale: f64 = config.stream_width_scale.min(100.0).into();
|
2025-12-24 16:36:38 +00:00
|
|
|
let auto_scale = total_tags as f64 / total_workers as f64;
|
|
|
|
|
let auto_scale_width = auto_scale / num_queues as f64;
|
2025-01-01 06:08:20 +00:00
|
|
|
|
2025-12-24 16:36:38 +00:00
|
|
|
let req_width = expected!(total_workers / num_queues).next_multiple_of(8);
|
|
|
|
|
let req_width = req_width as f64 * auto_scale_width.clamp(1.0, 4.0);
|
2024-12-23 04:32:28 +00:00
|
|
|
let req_width = usize_from_f64(req_width * scale)
|
|
|
|
|
.expect("failed to convert f64 to usize")
|
2025-12-24 16:36:38 +00:00
|
|
|
.next_multiple_of(4)
|
2024-12-23 04:32:28 +00:00
|
|
|
.clamp(WIDTH_LIMIT.0, WIDTH_LIMIT.1);
|
|
|
|
|
|
2025-12-24 16:36:38 +00:00
|
|
|
let req_amp = config.stream_amplification as f64 * auto_scale.clamp(1.0, 4.0);
|
2025-01-01 06:08:20 +00:00
|
|
|
let req_amp = usize_from_f64(req_amp * scale)
|
|
|
|
|
.expect("failed to convert f64 to usize")
|
2025-12-24 16:36:38 +00:00
|
|
|
.next_multiple_of(64)
|
2025-01-01 06:08:20 +00:00
|
|
|
.clamp(AMPLIFICATION_LIMIT.0, AMPLIFICATION_LIMIT.1);
|
|
|
|
|
|
2024-12-23 04:32:28 +00:00
|
|
|
let (old_width, new_width) = stream::set_width(req_width);
|
2025-01-01 06:08:20 +00:00
|
|
|
let (old_amp, new_amp) = stream::set_amplification(req_amp);
|
2024-12-23 04:32:28 +00:00
|
|
|
debug!(
|
2025-12-24 16:36:38 +00:00
|
|
|
config_scale = ?config.stream_width_scale,
|
|
|
|
|
?auto_scale,
|
|
|
|
|
?auto_scale_width,
|
2024-12-23 04:32:28 +00:00
|
|
|
?req_width,
|
|
|
|
|
?old_width,
|
|
|
|
|
?new_width,
|
2025-01-01 06:08:20 +00:00
|
|
|
?old_amp,
|
|
|
|
|
?new_amp,
|
2024-12-23 04:32:28 +00:00
|
|
|
"Updated global stream width"
|
|
|
|
|
);
|
|
|
|
|
}
|