scx_utils/
cli.rs

1// Copyright (c) Meta Platforms, Inc. and affiliates.
2//
3// This software may be used and distributed according to the terms of the
4// GNU General Public License version 2.
5
6//! CLI argument utilities for sched_ext schedulers.
7//!
8
9use crate::topology::{NR_PARTITION_MAX_CORES, NR_PARTITION_MIN_CORES};
10use anyhow::{bail, Result};
11use clap::Args;
12
13/// Topology configuration arguments
14#[derive(Args, Debug, Clone)]
15pub struct TopologyArgs {
16    /// Configure virtual LLC partitioning with min and max cores per partition.
17    /// Format: --virt-llc [min_cores-max_cores]
18    ///
19    /// Virtual LLCs allow splitting large LLC domains into smaller partitions
20    /// to improve scheduling locality and reduce contention. The min and max
21    /// values define the range of cores that can be grouped together in each
22    /// virtual LLC partition.
23    ///
24    /// Examples:
25    ///   --virt-llc 2-8    (partition with 2-8 cores each)
26    ///   --virt-llc        (use default range: 2-8 cores)
27    #[clap(
28        long = "virt-llc",
29        value_delimiter = '-',
30        num_args = 0..=2,
31        value_names = ["MIN_CORES", "MAX_CORES"],
32        help = "Enable virtual LLC partitioning with optional core range (format: min-max, defaults to 2-8)"
33    )]
34    pub virt_llc: Option<Vec<usize>>,
35}
36
37impl TopologyArgs {
38    /// Get the virtual LLC configuration as a tuple of (min_cores, max_cores).
39    /// Returns None if virtual LLC is not configured.
40    /// If configured with no arguments, returns the default range from NR_PARTITION constants.
41    pub fn get_nr_cores_per_vllc(&self) -> Option<(usize, usize)> {
42        match &self.virt_llc {
43            Some(values) if values.len() == 2 => Some((values[0], values[1])),
44            Some(values) if values.is_empty() => {
45                Some((*NR_PARTITION_MIN_CORES, *NR_PARTITION_MAX_CORES))
46            }
47            _ => None,
48        }
49    }
50
51    pub fn validate(&self) -> Result<()> {
52        if let Some((min_cores, max_cores)) = self.get_nr_cores_per_vllc() {
53            if min_cores == 0 {
54                bail!("Minimum cores for virtual LLC must be greater than 0");
55            }
56            if min_cores > max_cores {
57                bail!(
58                    "Minimum cores ({}) cannot be greater than maximum cores ({})",
59                    min_cores,
60                    max_cores
61                );
62            }
63        }
64        Ok(())
65    }
66}
67
68impl Default for TopologyArgs {
69    fn default() -> Self {
70        Self { virt_llc: None }
71    }
72}