use std::borrow::Cow;
use std::path::{Path, PathBuf};
use serde::Serialize;
pub const NUMA_PIN_UNSUPPORTED_PLATFORM_CODE: &str = "numa_pin_unsupported_platform";
pub const NUMA_PIN_DISABLED_CODE: &str = "numa_pin_disabled";
pub const NUMA_PIN_LINUX_NOT_IMPLEMENTED_CODE: &str = "numa_pin_linux_not_implemented";
pub const STATUS_GRAPH_NUMA_PIN_SCHEMA_V1: &str = "ee.status.graph.numa_pin.v1";
pub const GRAPH_SNAPSHOT_NUMA_HINT_SCHEMA_V1: &str = "ee.graph.snapshot_numa_hint.v1";
pub const NUMA_PIN_DISABLE_ENV: &str = "EE_GRAPH_NUMA_PIN_DISABLE";
pub const NUMA_PIN_NODE_ENV: &str = "EE_GRAPH_NUMA_PIN_NODE";
pub const NUMA_PIN_POPULATE_ENV: &str = "EE_GRAPH_NUMA_PIN_POPULATE";
pub const NUMA_PIN_PREFERRED_NODE_AUTO: &str = "auto";
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum NumaPinPlatform {
Linux,
MacosUnsupported,
WindowsUnsupported,
OtherUnsupported,
}
impl NumaPinPlatform {
#[must_use]
pub fn detect() -> Self {
if cfg!(target_os = "linux") {
Self::Linux
} else if cfg!(target_os = "macos") {
Self::MacosUnsupported
} else if cfg!(target_os = "windows") {
Self::WindowsUnsupported
} else {
Self::OtherUnsupported
}
}
#[must_use]
pub fn is_supported(self) -> bool {
matches!(self, Self::Linux)
}
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub enum NumaPinPreference {
#[default]
Auto,
Node(i32),
}
impl NumaPinPreference {
#[must_use]
pub fn as_str(self) -> Cow<'static, str> {
match self {
Self::Auto => Cow::Borrowed(NUMA_PIN_PREFERRED_NODE_AUTO),
Self::Node(node) => Cow::Owned(node.to_string()),
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct NumaPinConfig {
pub enabled: bool,
pub preferred_node: NumaPinPreference,
pub populate_on_load: bool,
}
impl Default for NumaPinConfig {
fn default() -> Self {
Self {
enabled: true,
preferred_node: NumaPinPreference::Auto,
populate_on_load: true,
}
}
}
impl NumaPinConfig {
#[must_use]
pub fn disabled() -> Self {
Self {
enabled: false,
..Self::default()
}
}
#[must_use]
pub fn with_preferred_node(mut self, preference: NumaPinPreference) -> Self {
self.preferred_node = preference;
self
}
#[must_use]
pub fn with_populate_on_load(mut self, populate: bool) -> Self {
self.populate_on_load = populate;
self
}
#[must_use]
pub fn from_environment_with_reader<F, G>(reader: F, mut on_unparseable: G) -> Self
where
F: Fn(&'static str) -> Option<String>,
G: FnMut(&'static str, &str),
{
let mut config = Self::default();
if let Some(raw) = reader(NUMA_PIN_DISABLE_ENV) {
match parse_env_bool(&raw) {
Some(disabled) => config.enabled = !disabled,
None => on_unparseable(NUMA_PIN_DISABLE_ENV, &raw),
}
}
if let Some(raw) = reader(NUMA_PIN_NODE_ENV) {
match parse_env_preferred_node(&raw) {
Some(preference) => config.preferred_node = preference,
None => on_unparseable(NUMA_PIN_NODE_ENV, &raw),
}
}
if let Some(raw) = reader(NUMA_PIN_POPULATE_ENV) {
match parse_env_bool(&raw) {
Some(populate) => config.populate_on_load = populate,
None => on_unparseable(NUMA_PIN_POPULATE_ENV, &raw),
}
}
config
}
}
fn parse_env_bool(raw: &str) -> Option<bool> {
match raw.trim().to_ascii_lowercase().as_str() {
"true" | "1" | "yes" | "on" => Some(true),
"false" | "0" | "no" | "off" => Some(false),
_ => None,
}
}
fn parse_env_preferred_node(raw: &str) -> Option<NumaPinPreference> {
let trimmed = raw.trim();
if trimmed.eq_ignore_ascii_case(NUMA_PIN_PREFERRED_NODE_AUTO) {
return Some(NumaPinPreference::Auto);
}
let node = trimmed.parse::<i32>().ok()?;
(node >= 0).then_some(NumaPinPreference::Node(node))
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum NumaPinFallbackPath {
None,
SoftwareNotImplemented,
MadviseWillneed,
HeapOnly,
DisabledByOperator,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum NumaPinMappingKind {
None,
ReadOnlyMmap,
HeapOnly,
}
impl NumaPinMappingKind {
#[must_use]
pub fn is_mmap(self) -> bool {
matches!(self, Self::ReadOnlyMmap)
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct NumaPinPlan {
pub platform: NumaPinPlatform,
pub supported: bool,
pub enabled: bool,
pub mapping_kind: NumaPinMappingKind,
pub bind_requested: bool,
pub preferred_node: Cow<'static, str>,
pub populate_requested: bool,
pub snapshot_bytes: u64,
pub snapshot_path: PathBuf,
pub fallback_path: NumaPinFallbackPath,
pub degraded_codes: Vec<&'static str>,
}
impl NumaPinPlan {
fn for_platform(
platform: NumaPinPlatform,
snapshot_path: &Path,
snapshot_bytes: u64,
config: &NumaPinConfig,
) -> Self {
let mut plan = Self {
platform,
supported: platform.is_supported(),
enabled: config.enabled,
mapping_kind: NumaPinMappingKind::None,
bind_requested: false,
preferred_node: config.preferred_node.as_str(),
populate_requested: config.populate_on_load,
snapshot_bytes,
snapshot_path: snapshot_path.to_path_buf(),
fallback_path: NumaPinFallbackPath::None,
degraded_codes: Vec::new(),
};
if !config.enabled {
plan.fallback_path = NumaPinFallbackPath::DisabledByOperator;
plan.push_unique_code(NUMA_PIN_DISABLED_CODE);
return plan;
}
match platform {
NumaPinPlatform::Linux => {
plan.mapping_kind = NumaPinMappingKind::ReadOnlyMmap;
plan.bind_requested = true;
plan.fallback_path = NumaPinFallbackPath::SoftwareNotImplemented;
plan.push_unique_code(NUMA_PIN_LINUX_NOT_IMPLEMENTED_CODE);
}
NumaPinPlatform::MacosUnsupported => {
plan.mapping_kind = NumaPinMappingKind::ReadOnlyMmap;
plan.fallback_path = NumaPinFallbackPath::MadviseWillneed;
plan.push_unique_code(NUMA_PIN_UNSUPPORTED_PLATFORM_CODE);
}
NumaPinPlatform::WindowsUnsupported | NumaPinPlatform::OtherUnsupported => {
plan.mapping_kind = NumaPinMappingKind::HeapOnly;
plan.fallback_path = NumaPinFallbackPath::HeapOnly;
plan.push_unique_code(NUMA_PIN_UNSUPPORTED_PLATFORM_CODE);
}
}
plan
}
fn push_unique_code(&mut self, code: &'static str) {
if !self.degraded_codes.contains(&code) {
self.degraded_codes.push(code);
}
}
}
#[derive(Clone, Copy, Debug)]
pub struct GraphSnapshotNumaHintInput<'a> {
pub snapshot_id: &'a str,
pub graph_type: &'a str,
pub snapshot_version: u64,
pub source_generation: u32,
pub content_hash: &'a str,
pub snapshot_path: &'a Path,
pub snapshot_bytes: u64,
pub config: NumaPinConfig,
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GraphSnapshotNumaHintRecord {
pub schema: &'static str,
pub snapshot_id: String,
pub graph_type: String,
pub snapshot_version: u64,
pub source_generation: u32,
pub content_hash: String,
pub snapshot_path: PathBuf,
pub snapshot_bytes: u64,
pub requested_node: Cow<'static, str>,
pub map_populate_requested: bool,
pub mapping_kind: NumaPinMappingKind,
pub bind_requested: bool,
pub fallback_path: NumaPinFallbackPath,
pub degraded_codes: Vec<&'static str>,
}
#[must_use]
pub fn graph_snapshot_numa_hint(
input: GraphSnapshotNumaHintInput<'_>,
) -> GraphSnapshotNumaHintRecord {
let plan = plan_snapshot_pin(input.snapshot_path, input.snapshot_bytes, &input.config);
GraphSnapshotNumaHintRecord {
schema: GRAPH_SNAPSHOT_NUMA_HINT_SCHEMA_V1,
snapshot_id: input.snapshot_id.to_owned(),
graph_type: input.graph_type.to_owned(),
snapshot_version: input.snapshot_version,
source_generation: input.source_generation,
content_hash: input.content_hash.to_owned(),
snapshot_path: input.snapshot_path.to_path_buf(),
snapshot_bytes: input.snapshot_bytes,
requested_node: plan.preferred_node,
map_populate_requested: plan.populate_requested,
mapping_kind: plan.mapping_kind,
bind_requested: plan.bind_requested,
fallback_path: plan.fallback_path,
degraded_codes: plan.degraded_codes,
}
}
#[derive(Clone, Debug, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct NumaPinResult {
pub schema: &'static str,
pub platform: NumaPinPlatform,
pub supported: bool,
pub enabled: bool,
pub attempted: bool,
pub succeeded: bool,
pub preferred_node: Cow<'static, str>,
pub populate_requested: bool,
pub bytes_resident: u64,
pub populated: bool,
pub fallback_path: NumaPinFallbackPath,
pub snapshot_path: Option<PathBuf>,
pub degraded_codes: Vec<&'static str>,
}
impl NumaPinResult {
fn base(platform: NumaPinPlatform, config: &NumaPinConfig, snapshot_path: &Path) -> Self {
Self {
schema: STATUS_GRAPH_NUMA_PIN_SCHEMA_V1,
platform,
supported: platform.is_supported(),
enabled: config.enabled,
attempted: false,
succeeded: false,
preferred_node: config.preferred_node.as_str(),
populate_requested: config.populate_on_load,
bytes_resident: 0,
populated: false,
fallback_path: NumaPinFallbackPath::None,
snapshot_path: Some(snapshot_path.to_path_buf()),
degraded_codes: Vec::new(),
}
}
}
#[must_use]
pub fn detect_preferred_node() -> Option<i32> {
None
}
#[must_use]
pub fn platform_support() -> NumaPinPlatform {
NumaPinPlatform::detect()
}
#[must_use]
pub fn plan_snapshot_pin(
snapshot_path: &Path,
snapshot_bytes: u64,
config: &NumaPinConfig,
) -> NumaPinPlan {
NumaPinPlan::for_platform(
NumaPinPlatform::detect(),
snapshot_path,
snapshot_bytes,
config,
)
}
pub fn pin_snapshot_blob(snapshot_path: &Path, config: &NumaPinConfig) -> NumaPinResult {
let plan = plan_snapshot_pin(snapshot_path, 0, config);
let mut result = NumaPinResult::base(plan.platform, config, snapshot_path);
if !config.enabled {
result.fallback_path = plan.fallback_path;
result.degraded_codes = plan.degraded_codes;
return result;
}
match plan.platform {
NumaPinPlatform::Linux => {
result.attempted = plan.bind_requested;
result.fallback_path = plan.fallback_path;
result.degraded_codes = plan.degraded_codes;
result
}
NumaPinPlatform::MacosUnsupported => {
result.fallback_path = plan.fallback_path;
result.degraded_codes = plan.degraded_codes;
result
}
NumaPinPlatform::WindowsUnsupported | NumaPinPlatform::OtherUnsupported => {
result.fallback_path = plan.fallback_path;
result.degraded_codes = plan.degraded_codes;
result
}
}
}
pub const NUMA_UNAVAILABLE_ON_MACOS_CODE: &str = "numa_unavailable_on_macos";
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct NumaPinningAdapterOutcome {
pub executed: bool,
pub degraded_code: Option<&'static str>,
}
impl NumaPinningAdapterOutcome {
#[must_use]
pub const fn executed() -> Self {
Self {
executed: true,
degraded_code: None,
}
}
#[must_use]
pub const fn degraded(code: &'static str) -> Self {
Self {
executed: false,
degraded_code: Some(code),
}
}
}
pub trait NumaPinningAdapter: Send + Sync {
fn platform(&self) -> NumaPinPlatform;
fn pin_mmap(&self, snapshot_path: &Path, populate: bool) -> NumaPinningAdapterOutcome {
let _ = (snapshot_path, populate);
NumaPinningAdapterOutcome::degraded(NUMA_PIN_LINUX_NOT_IMPLEMENTED_CODE)
}
fn set_node_affinity(&self, node: Option<i32>) -> NumaPinningAdapterOutcome;
}
#[derive(Clone, Copy, Debug, Default)]
pub struct MacosNumaPinningAdapter;
impl NumaPinningAdapter for MacosNumaPinningAdapter {
fn platform(&self) -> NumaPinPlatform {
NumaPinPlatform::MacosUnsupported
}
fn pin_mmap(&self, _snapshot_path: &Path, _populate: bool) -> NumaPinningAdapterOutcome {
NumaPinningAdapterOutcome::degraded(NUMA_PIN_UNSUPPORTED_PLATFORM_CODE)
}
fn set_node_affinity(&self, _node: Option<i32>) -> NumaPinningAdapterOutcome {
NumaPinningAdapterOutcome::degraded(NUMA_UNAVAILABLE_ON_MACOS_CODE)
}
}
#[derive(Clone, Copy, Debug, Default)]
pub struct UnsupportedPlatformNumaPinningAdapter;
impl NumaPinningAdapter for UnsupportedPlatformNumaPinningAdapter {
fn platform(&self) -> NumaPinPlatform {
NumaPinPlatform::detect()
}
fn pin_mmap(&self, _snapshot_path: &Path, _populate: bool) -> NumaPinningAdapterOutcome {
NumaPinningAdapterOutcome::degraded(NUMA_PIN_UNSUPPORTED_PLATFORM_CODE)
}
fn set_node_affinity(&self, _node: Option<i32>) -> NumaPinningAdapterOutcome {
NumaPinningAdapterOutcome::degraded(NUMA_PIN_UNSUPPORTED_PLATFORM_CODE)
}
}
#[derive(Clone, Copy, Debug, Default)]
pub struct LinuxNumaPinningAdapter;
impl NumaPinningAdapter for LinuxNumaPinningAdapter {
fn platform(&self) -> NumaPinPlatform {
#[cfg(target_os = "linux")]
{
NumaPinPlatform::Linux
}
#[cfg(not(target_os = "linux"))]
{
NumaPinPlatform::detect()
}
}
#[cfg(target_os = "linux")]
fn pin_mmap(&self, _snapshot_path: &Path, _populate: bool) -> NumaPinningAdapterOutcome {
NumaPinningAdapterOutcome::degraded(NUMA_PIN_LINUX_NOT_IMPLEMENTED_CODE)
}
#[cfg(not(target_os = "linux"))]
fn pin_mmap(&self, _snapshot_path: &Path, _populate: bool) -> NumaPinningAdapterOutcome {
NumaPinningAdapterOutcome::degraded(NUMA_PIN_UNSUPPORTED_PLATFORM_CODE)
}
#[cfg(target_os = "linux")]
fn set_node_affinity(&self, _node: Option<i32>) -> NumaPinningAdapterOutcome {
NumaPinningAdapterOutcome::degraded(NUMA_PIN_LINUX_NOT_IMPLEMENTED_CODE)
}
#[cfg(not(target_os = "linux"))]
fn set_node_affinity(&self, _node: Option<i32>) -> NumaPinningAdapterOutcome {
NumaPinningAdapterOutcome::degraded(NUMA_PIN_UNSUPPORTED_PLATFORM_CODE)
}
}
#[must_use]
pub fn default_numa_pinning_adapter() -> &'static dyn NumaPinningAdapter {
#[cfg(target_os = "linux")]
static LINUX_ADAPTER: LinuxNumaPinningAdapter = LinuxNumaPinningAdapter;
#[cfg(target_os = "macos")]
static MACOS_ADAPTER: MacosNumaPinningAdapter = MacosNumaPinningAdapter;
#[cfg(all(not(target_os = "linux"), not(target_os = "macos")))]
static UNSUPPORTED_ADAPTER: UnsupportedPlatformNumaPinningAdapter =
UnsupportedPlatformNumaPinningAdapter;
#[cfg(target_os = "linux")]
{
&LINUX_ADAPTER
}
#[cfg(target_os = "macos")]
{
&MACOS_ADAPTER
}
#[cfg(all(not(target_os = "linux"), not(target_os = "macos")))]
{
&UNSUPPORTED_ADAPTER
}
}
#[cfg(test)]
mod tests {
use std::cell::RefCell;
use std::collections::HashMap;
use std::path::Path;
#[cfg(not(target_os = "linux"))]
use super::NUMA_PIN_UNSUPPORTED_PLATFORM_CODE;
use super::{
GRAPH_SNAPSHOT_NUMA_HINT_SCHEMA_V1, GraphSnapshotNumaHintInput, NUMA_PIN_DISABLE_ENV,
NUMA_PIN_DISABLED_CODE, NUMA_PIN_NODE_ENV, NUMA_PIN_POPULATE_ENV,
NUMA_PIN_PREFERRED_NODE_AUTO, NumaPinConfig, NumaPinFallbackPath, NumaPinMappingKind,
NumaPinPlan, NumaPinPlatform, NumaPinPreference, NumaPinResult,
STATUS_GRAPH_NUMA_PIN_SCHEMA_V1, detect_preferred_node, graph_snapshot_numa_hint,
parse_env_bool, parse_env_preferred_node, pin_snapshot_blob, plan_snapshot_pin,
platform_support,
};
fn fake_snapshot_path() -> &'static Path {
Path::new("/tmp/ee-numa-pin-fake-snapshot.bin")
}
fn env_reader_from<'a>(
entries: &'a [(&'static str, &'static str)],
) -> impl Fn(&'static str) -> Option<String> + 'a {
let map: HashMap<&'static str, &'static str> = entries.iter().copied().collect();
move |name: &'static str| map.get(name).map(|value| (*value).to_owned())
}
fn assert_no_duplicate_codes(result: &NumaPinResult) {
let mut seen = std::collections::BTreeSet::new();
for code in &result.degraded_codes {
assert!(
seen.insert(*code),
"duplicate degraded code {code} in {:?}",
result.degraded_codes
);
}
}
fn assert_plan_has_no_duplicate_codes(plan: &NumaPinPlan) {
let mut seen = std::collections::BTreeSet::new();
for code in &plan.degraded_codes {
assert!(
seen.insert(*code),
"duplicate degraded code {code} in {:?}",
plan.degraded_codes
);
}
}
#[test]
fn default_config_is_enabled_and_auto() {
let config = NumaPinConfig::default();
assert!(config.enabled);
assert_eq!(config.preferred_node, NumaPinPreference::Auto);
assert!(config.populate_on_load);
}
#[test]
fn disabled_config_short_circuits_with_disabled_code() {
let result = pin_snapshot_blob(fake_snapshot_path(), &NumaPinConfig::disabled());
assert!(!result.enabled);
assert!(!result.attempted);
assert!(!result.succeeded);
assert_eq!(
result.fallback_path,
NumaPinFallbackPath::DisabledByOperator
);
assert_eq!(result.degraded_codes, vec![NUMA_PIN_DISABLED_CODE]);
assert_no_duplicate_codes(&result);
}
#[test]
fn disabled_pin_plan_never_requests_mapping_or_bind() {
let plan = plan_snapshot_pin(fake_snapshot_path(), 4096, &NumaPinConfig::disabled());
assert_eq!(plan.mapping_kind, NumaPinMappingKind::None);
assert!(!plan.mapping_kind.is_mmap());
assert!(!plan.bind_requested);
assert_eq!(plan.snapshot_bytes, 4096);
assert_eq!(plan.fallback_path, NumaPinFallbackPath::DisabledByOperator);
assert_eq!(plan.degraded_codes, vec![NUMA_PIN_DISABLED_CODE]);
assert_plan_has_no_duplicate_codes(&plan);
}
#[test]
fn preferred_node_renders_auto_and_explicit_consistently() {
assert_eq!(
NumaPinPreference::Auto.as_str(),
NUMA_PIN_PREFERRED_NODE_AUTO
);
assert_eq!(NumaPinPreference::Node(0).as_str(), "0");
assert_eq!(NumaPinPreference::Node(7).as_str(), "7");
}
#[test]
fn parse_env_bool_accepts_operator_vocabulary() {
for raw in ["true", "TRUE", "1", "yes", "YES", "on", " ON "] {
assert_eq!(parse_env_bool(raw), Some(true));
}
for raw in ["false", "FALSE", "0", "no", "NO", "off", " OFF "] {
assert_eq!(parse_env_bool(raw), Some(false));
}
for raw in ["maybe", "2", "", " "] {
assert_eq!(parse_env_bool(raw), None);
}
}
#[test]
fn parse_env_preferred_node_accepts_auto_and_non_negative_nodes() {
assert_eq!(
parse_env_preferred_node("auto"),
Some(NumaPinPreference::Auto)
);
assert_eq!(
parse_env_preferred_node(" AUTO "),
Some(NumaPinPreference::Auto)
);
assert_eq!(
parse_env_preferred_node("0"),
Some(NumaPinPreference::Node(0))
);
assert_eq!(
parse_env_preferred_node("7"),
Some(NumaPinPreference::Node(7))
);
assert_eq!(parse_env_preferred_node("-1"), None);
assert_eq!(parse_env_preferred_node("socket0"), None);
}
#[test]
fn from_environment_with_empty_reader_yields_default_config() {
let unparseable: RefCell<Vec<(&'static str, String)>> = RefCell::new(Vec::new());
let config = NumaPinConfig::from_environment_with_reader(
|_name| None,
|name, raw| unparseable.borrow_mut().push((name, raw.to_owned())),
);
assert_eq!(config, NumaPinConfig::default());
assert!(
unparseable.borrow().is_empty(),
"missing values must not trigger on_unparseable: {:?}",
unparseable.borrow()
);
}
#[test]
fn from_environment_parses_disable_node_and_populate_controls() {
let unparseable: RefCell<Vec<(&'static str, String)>> = RefCell::new(Vec::new());
let reader = env_reader_from(&[
(NUMA_PIN_DISABLE_ENV, "yes"),
(NUMA_PIN_NODE_ENV, "2"),
(NUMA_PIN_POPULATE_ENV, "false"),
]);
let config = NumaPinConfig::from_environment_with_reader(reader, |name, raw| {
unparseable.borrow_mut().push((name, raw.to_owned()))
});
assert!(!config.enabled);
assert_eq!(config.preferred_node, NumaPinPreference::Node(2));
assert!(!config.populate_on_load);
assert!(unparseable.borrow().is_empty());
}
#[test]
fn from_environment_records_unparseable_values_without_changing_defaults() {
let unparseable: RefCell<Vec<(&'static str, String)>> = RefCell::new(Vec::new());
let reader = env_reader_from(&[
(NUMA_PIN_DISABLE_ENV, "maybe"),
(NUMA_PIN_NODE_ENV, "-4"),
(NUMA_PIN_POPULATE_ENV, "sometimes"),
]);
let config = NumaPinConfig::from_environment_with_reader(reader, |name, raw| {
unparseable.borrow_mut().push((name, raw.to_owned()))
});
assert_eq!(config, NumaPinConfig::default());
let log = unparseable.borrow();
assert_eq!(log.len(), 3);
assert_eq!(log[0], (NUMA_PIN_DISABLE_ENV, "maybe".to_string()));
assert_eq!(log[1], (NUMA_PIN_NODE_ENV, "-4".to_string()));
assert_eq!(log[2], (NUMA_PIN_POPULATE_ENV, "sometimes".to_string()));
}
#[test]
fn from_environment_constants_match_swarmx4_spec() {
assert_eq!(NUMA_PIN_DISABLE_ENV, "EE_GRAPH_NUMA_PIN_DISABLE");
assert_eq!(NUMA_PIN_NODE_ENV, "EE_GRAPH_NUMA_PIN_NODE");
assert_eq!(NUMA_PIN_POPULATE_ENV, "EE_GRAPH_NUMA_PIN_POPULATE");
}
#[test]
fn detect_preferred_node_returns_none_on_scaffold() {
assert_eq!(detect_preferred_node(), None);
}
#[cfg(target_os = "linux")]
#[test]
fn linux_pin_plan_requests_readonly_mmap_and_bind_without_claiming_success() {
let plan = plan_snapshot_pin(
fake_snapshot_path(),
128 * 1024 * 1024,
&NumaPinConfig::default().with_preferred_node(NumaPinPreference::Node(1)),
);
assert_eq!(plan.platform, NumaPinPlatform::Linux);
assert!(plan.supported);
assert_eq!(plan.mapping_kind, NumaPinMappingKind::ReadOnlyMmap);
assert!(plan.mapping_kind.is_mmap());
assert!(plan.bind_requested);
assert_eq!(plan.preferred_node, "1");
assert_eq!(plan.snapshot_bytes, 128 * 1024 * 1024);
assert_eq!(
plan.fallback_path,
NumaPinFallbackPath::SoftwareNotImplemented
);
assert_eq!(
plan.degraded_codes,
vec![NUMA_PIN_LINUX_NOT_IMPLEMENTED_CODE]
);
assert_plan_has_no_duplicate_codes(&plan);
}
#[cfg(not(target_os = "linux"))]
#[test]
fn non_linux_pin_plan_is_deterministic_and_non_binding() {
let plan = plan_snapshot_pin(
fake_snapshot_path(),
128 * 1024 * 1024,
&NumaPinConfig::default().with_preferred_node(NumaPinPreference::Node(1)),
);
assert!(!plan.supported);
assert!(!plan.bind_requested);
assert_eq!(plan.preferred_node, "1");
assert_eq!(plan.snapshot_bytes, 128 * 1024 * 1024);
assert!(
plan.degraded_codes
.iter()
.any(|code| *code == NUMA_PIN_UNSUPPORTED_PLATFORM_CODE)
);
match plan.platform {
NumaPinPlatform::MacosUnsupported => {
assert_eq!(plan.mapping_kind, NumaPinMappingKind::ReadOnlyMmap);
assert_eq!(plan.fallback_path, NumaPinFallbackPath::MadviseWillneed);
}
NumaPinPlatform::WindowsUnsupported | NumaPinPlatform::OtherUnsupported => {
assert_eq!(plan.mapping_kind, NumaPinMappingKind::HeapOnly);
assert_eq!(plan.fallback_path, NumaPinFallbackPath::HeapOnly);
}
NumaPinPlatform::Linux => panic!("linux cfg should run the linux-specific plan test"),
}
assert_plan_has_no_duplicate_codes(&plan);
}
#[test]
fn platform_support_is_consistent_with_cfg() {
let platform = platform_support();
if cfg!(target_os = "linux") {
assert_eq!(platform, NumaPinPlatform::Linux);
assert!(platform.is_supported());
} else if cfg!(target_os = "macos") {
assert_eq!(platform, NumaPinPlatform::MacosUnsupported);
assert!(!platform.is_supported());
} else if cfg!(target_os = "windows") {
assert_eq!(platform, NumaPinPlatform::WindowsUnsupported);
assert!(!platform.is_supported());
} else {
assert_eq!(platform, NumaPinPlatform::OtherUnsupported);
assert!(!platform.is_supported());
}
}
#[cfg(not(target_os = "linux"))]
#[test]
fn non_linux_platform_returns_unsupported_code() {
let result = pin_snapshot_blob(fake_snapshot_path(), &NumaPinConfig::default());
assert!(!result.supported);
assert!(result.enabled);
assert!(!result.attempted);
assert!(!result.succeeded);
assert_eq!(result.bytes_resident, 0);
assert!(!result.populated);
assert!(matches!(
result.fallback_path,
NumaPinFallbackPath::MadviseWillneed
| NumaPinFallbackPath::HeapOnly
| NumaPinFallbackPath::DisabledByOperator
));
assert!(
result
.degraded_codes
.iter()
.any(|code| *code == NUMA_PIN_UNSUPPORTED_PLATFORM_CODE)
);
assert_no_duplicate_codes(&result);
}
#[cfg(target_os = "linux")]
#[test]
fn linux_scaffold_reports_not_implemented_without_claiming_success() {
let result = pin_snapshot_blob(fake_snapshot_path(), &NumaPinConfig::default());
assert_eq!(result.platform, NumaPinPlatform::Linux);
assert!(result.supported);
assert!(result.enabled);
assert!(result.attempted);
assert!(!result.succeeded, "scaffold must not claim success");
assert!(!result.populated, "scaffold must not claim populated pages");
assert_eq!(
result.fallback_path,
NumaPinFallbackPath::SoftwareNotImplemented
);
assert!(
result
.degraded_codes
.iter()
.any(|code| *code == NUMA_PIN_LINUX_NOT_IMPLEMENTED_CODE)
);
assert_no_duplicate_codes(&result);
}
#[test]
fn result_schema_matches_documented_id() {
let result = pin_snapshot_blob(fake_snapshot_path(), &NumaPinConfig::default());
assert_eq!(result.schema, STATUS_GRAPH_NUMA_PIN_SCHEMA_V1);
assert_eq!(
STATUS_GRAPH_NUMA_PIN_SCHEMA_V1,
"ee.status.graph.numa_pin.v1"
);
}
#[test]
fn config_builder_methods_round_trip() {
let config = NumaPinConfig::default()
.with_preferred_node(NumaPinPreference::Node(3))
.with_populate_on_load(false);
assert_eq!(config.preferred_node, NumaPinPreference::Node(3));
assert!(!config.populate_on_load);
assert!(config.enabled);
}
#[test]
fn graph_snapshot_numa_hint_records_side_table_fields_from_plan() {
let hint = graph_snapshot_numa_hint(GraphSnapshotNumaHintInput {
snapshot_id: "snap-01",
graph_type: "memory_links",
snapshot_version: 17,
source_generation: 42,
content_hash: "blake3:abc123",
snapshot_path: Path::new("/var/lib/ee/snapshots/graph.bin"),
snapshot_bytes: 128 * 1024 * 1024,
config: NumaPinConfig::default().with_preferred_node(NumaPinPreference::Node(1)),
});
assert_eq!(hint.schema, GRAPH_SNAPSHOT_NUMA_HINT_SCHEMA_V1);
assert_eq!(hint.snapshot_id, "snap-01");
assert_eq!(hint.graph_type, "memory_links");
assert_eq!(hint.snapshot_version, 17);
assert_eq!(hint.source_generation, 42);
assert_eq!(hint.content_hash, "blake3:abc123");
assert_eq!(
hint.snapshot_path.as_path(),
Path::new("/var/lib/ee/snapshots/graph.bin")
);
assert_eq!(hint.snapshot_bytes, 128 * 1024 * 1024);
assert_eq!(hint.requested_node, "1");
assert!(hint.map_populate_requested);
if cfg!(target_os = "linux") {
assert_eq!(hint.mapping_kind, NumaPinMappingKind::ReadOnlyMmap);
assert!(hint.bind_requested);
} else {
assert!(!hint.bind_requested);
}
}
#[test]
fn disabled_graph_snapshot_numa_hint_never_requests_mapping() {
let hint = graph_snapshot_numa_hint(GraphSnapshotNumaHintInput {
snapshot_id: "snap-disabled",
graph_type: "revision_dag",
snapshot_version: 1,
source_generation: 3,
content_hash: "blake3:def456",
snapshot_path: fake_snapshot_path(),
snapshot_bytes: 4096,
config: NumaPinConfig::disabled(),
});
assert_eq!(hint.schema, GRAPH_SNAPSHOT_NUMA_HINT_SCHEMA_V1);
assert_eq!(hint.mapping_kind, NumaPinMappingKind::None);
assert!(!hint.bind_requested);
assert_eq!(hint.fallback_path, NumaPinFallbackPath::DisabledByOperator);
assert_eq!(hint.degraded_codes, vec![NUMA_PIN_DISABLED_CODE]);
}
#[test]
fn graph_snapshot_numa_hint_serializes_camel_case_fields() {
let hint = graph_snapshot_numa_hint(GraphSnapshotNumaHintInput {
snapshot_id: "snap-json",
graph_type: "causal_evidence",
snapshot_version: 5,
source_generation: 8,
content_hash: "blake3:json",
snapshot_path: fake_snapshot_path(),
snapshot_bytes: 8192,
config: NumaPinConfig::disabled(),
});
let serialized = serde_json::to_value(&hint).expect("serialize hint");
for key in [
"schema",
"snapshotId",
"graphType",
"snapshotVersion",
"sourceGeneration",
"contentHash",
"snapshotPath",
"snapshotBytes",
"requestedNode",
"mapPopulateRequested",
"mappingKind",
"bindRequested",
"fallbackPath",
"degradedCodes",
] {
assert!(
serialized.get(key).is_some(),
"expected field {key} in serialized hint {serialized}"
);
}
assert_eq!(
serialized.get("schema").and_then(|value| value.as_str()),
Some(GRAPH_SNAPSHOT_NUMA_HINT_SCHEMA_V1)
);
assert_eq!(
serialized
.get("fallbackPath")
.and_then(|value| value.as_str()),
Some("disabled_by_operator")
);
}
#[test]
fn pin_snapshot_blob_preserves_snapshot_path_in_result() {
let path = Path::new("/var/lib/ee/snapshots/example.bin");
let result = pin_snapshot_blob(path, &NumaPinConfig::default());
assert_eq!(result.snapshot_path.as_deref(), Some(path));
}
#[test]
fn result_serializes_with_camel_case_fields() {
let result = pin_snapshot_blob(fake_snapshot_path(), &NumaPinConfig::disabled());
let serialized = serde_json::to_value(&result).expect("serialize result");
for key in [
"schema",
"platform",
"supported",
"enabled",
"attempted",
"succeeded",
"preferredNode",
"populateRequested",
"bytesResident",
"populated",
"fallbackPath",
"snapshotPath",
"degradedCodes",
] {
assert!(
serialized.get(key).is_some(),
"expected field {key} in serialized result {serialized}"
);
}
assert_eq!(
serialized
.get("fallbackPath")
.and_then(|value| value.as_str()),
Some("disabled_by_operator")
);
}
use super::{
LinuxNumaPinningAdapter, MacosNumaPinningAdapter, NUMA_PIN_LINUX_NOT_IMPLEMENTED_CODE,
NUMA_UNAVAILABLE_ON_MACOS_CODE, NumaPinningAdapter, NumaPinningAdapterOutcome,
UnsupportedPlatformNumaPinningAdapter, default_numa_pinning_adapter,
};
#[test]
fn macos_adapter_set_node_affinity_emits_numa_unavailable_on_macos() {
let adapter = MacosNumaPinningAdapter;
let outcome = adapter.set_node_affinity(Some(0));
assert!(!outcome.executed, "macOS adapter must not claim execution");
assert_eq!(
outcome.degraded_code,
Some(NUMA_UNAVAILABLE_ON_MACOS_CODE),
"macOS adapter must emit the load-bearing bd-1prrl.3 degraded code"
);
let auto_outcome = adapter.set_node_affinity(None);
assert_eq!(
auto_outcome.degraded_code,
Some(NUMA_UNAVAILABLE_ON_MACOS_CODE),
"macOS adapter must emit the same code for the auto-node request"
);
}
#[test]
fn macos_adapter_pin_mmap_falls_through_to_unsupported_platform() {
let adapter = MacosNumaPinningAdapter;
let outcome = adapter.pin_mmap(fake_snapshot_path(), true);
assert!(!outcome.executed);
assert_eq!(
outcome.degraded_code,
Some(super::NUMA_PIN_UNSUPPORTED_PLATFORM_CODE),
);
assert_eq!(adapter.platform(), NumaPinPlatform::MacosUnsupported);
}
#[test]
fn linux_adapter_emits_not_implemented_until_safe_wrapper_lands() {
let adapter = LinuxNumaPinningAdapter;
let expected_platform = if cfg!(target_os = "linux") {
NumaPinPlatform::Linux
} else {
NumaPinPlatform::detect()
};
assert_eq!(adapter.platform(), expected_platform);
let outcome = adapter.pin_mmap(fake_snapshot_path(), true);
let expected_code = if cfg!(target_os = "linux") {
NUMA_PIN_LINUX_NOT_IMPLEMENTED_CODE
} else {
super::NUMA_PIN_UNSUPPORTED_PLATFORM_CODE
};
assert!(!outcome.executed);
assert_eq!(outcome.degraded_code, Some(expected_code));
}
#[test]
fn unsupported_platform_adapter_reports_runtime_platform() {
let adapter = UnsupportedPlatformNumaPinningAdapter;
assert_eq!(adapter.platform(), NumaPinPlatform::detect());
let mmap_outcome = adapter.pin_mmap(fake_snapshot_path(), true);
assert!(!mmap_outcome.executed);
assert_eq!(
mmap_outcome.degraded_code,
Some(super::NUMA_PIN_UNSUPPORTED_PLATFORM_CODE)
);
let affinity_outcome = adapter.set_node_affinity(Some(0));
assert!(!affinity_outcome.executed);
assert_eq!(
affinity_outcome.degraded_code,
Some(super::NUMA_PIN_UNSUPPORTED_PLATFORM_CODE)
);
}
#[test]
fn default_adapter_selects_platform_specific_implementation() {
let adapter = default_numa_pinning_adapter();
assert_eq!(adapter.platform(), NumaPinPlatform::detect());
}
#[test]
fn outcome_helpers_round_trip_through_executed_and_degraded() {
let executed = NumaPinningAdapterOutcome::executed();
assert!(executed.executed);
assert_eq!(executed.degraded_code, None);
let degraded = NumaPinningAdapterOutcome::degraded(NUMA_UNAVAILABLE_ON_MACOS_CODE);
assert!(!degraded.executed);
assert_eq!(degraded.degraded_code, Some(NUMA_UNAVAILABLE_ON_MACOS_CODE));
}
#[test]
fn trait_dispatch_through_box_dyn_compiles_and_runs_on_every_platform() {
fn assert_send_sync<T: Send + Sync + ?Sized>() {}
assert_send_sync::<dyn NumaPinningAdapter>();
let adapter = default_numa_pinning_adapter();
let outcome = adapter.set_node_affinity(Some(0));
assert!(!outcome.executed);
assert!(outcome.degraded_code.is_some());
}
}