From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [IPv6:2a0f:8001:1:32::40]) by lore.proxmox.com (Postfix) with ESMTPS id 38A771FF0B6 for ; Thu, 12 Nov 2026 10:45:38 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 48BEE21728; Thu, 12 Nov 2026 10:44:52 +0100 (CET) From: Dominik Rusovac To: pve-devel@lists.proxmox.com Subject: [PATCH proxmox v2 04/19] fix #7993: resource-scheduling: node: add weighted node load Date: Thu, 12 Nov 2026 10:44:22 +0100 Message-ID: <20261112094437.1951271-5-d.rusovac@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261112094437.1951271-1-d.rusovac@proxmox.com> References: <20261112094437.1951271-1-d.rusovac@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1794476685383 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.043 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: QOY42HOUEEA5O5LY5YIB3QGGD5YA7FY4 X-Message-ID-Hash: QOY42HOUEEA5O5LY5YIB3QGGD5YA7FY4 X-MailFrom: d.rusovac@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: add weighted node load, which computes the node load as the weighted arithmetic mean [0] of CPU load and memory usage, alongside basic unit tests this is in preparation for letting users prioritize balancing one resource over the other [0] https://en.wikipedia.org/wiki/Weighted_arithmetic_mean Signed-off-by: Dominik Rusovac --- Notes: new with changes regarding patch [0] in v1: * add weighted_load function * add NodeStatsWeights struct - make NodeStatsWeights #[non_exhaustive] - callers ought to start constructing weights via default function and then set the public fields, if required - exhaustive deconstructuring in weighted_load function will force compile error for new field, which is desirable - a new field must default to 0.0, otherwise a caller starting from default function might silently get a different weighted_load result dispatched changes into this new patch to make the series more organized [0] https://lore.proxmox.com/all/20261021085932.1196302-4-d.rusovac@proxmox.com/ proxmox-resource-scheduling/src/node.rs | 114 ++++++++++++++++++++++++ 1 file changed, 114 insertions(+) diff --git a/proxmox-resource-scheduling/src/node.rs b/proxmox-resource-scheduling/src/node.rs index 3c7ed393..98568bb7 100644 --- a/proxmox-resource-scheduling/src/node.rs +++ b/proxmox-resource-scheduling/src/node.rs @@ -2,6 +2,39 @@ use std::collections::HashSet; use crate::resource::ResourceStats; +/// Weights controlling how much the CPU load and the memory usage of a node contribute to its +/// [`NodeStats::weighted_load`]. +/// +/// Only the ratio of the weights matters, not their absolute magnitude. A weight of 0.0 means that +/// the respective resource does not contribute at all. +/// +/// Callers outside this crate start from [`NodeStatsWeights::default`] and set the weights they +/// need. +#[non_exhaustive] +#[derive(Copy, Clone, PartialEq, Debug)] +pub struct NodeStatsWeights { + /// Weight of [`NodeStats::cpu_load`]. Must be finite and non-negative. + pub cpu_weight: f64, + /// Weight of [`NodeStats::mem_load`]. Must be finite and non-negative. + pub mem_weight: f64, +} + +impl NodeStatsWeights { + /// Equal weights, making [`NodeStats::weighted_load`] the same as [`NodeStats::load`]. + /// + /// NOTE: any pair of equal weights bigger than 0.0 works here + pub const EQUAL: Self = Self { + cpu_weight: 1.0, + mem_weight: 1.0, + }; +} + +impl Default for NodeStatsWeights { + fn default() -> Self { + Self::EQUAL + } +} + /// Usage statistics of a node. #[derive(Copy, Clone, PartialEq, PartialOrd, Debug, Default)] pub struct NodeStats { @@ -55,6 +88,28 @@ impl NodeStats { pub fn load(&self) -> f64 { (self.cpu_load() + self.mem_load()) / 2.0 } + + /// Returns a combined node usage as a percentage, which is the [weighted arithmetic mean] of + /// [`Self::cpu_load`] and [`Self::mem_load`] with respect to `weights`. + /// + /// With equal weights, this is the same as [`Self::load`]. + /// + /// [weighted arithmetic mean]: https://en.wikipedia.org/wiki/Weighted_arithmetic_mean + pub fn weighted_load(&self, weights: &NodeStatsWeights) -> f64 { + let NodeStatsWeights { + cpu_weight, + mem_weight, + } = *weights; + + let weights_sum = cpu_weight + mem_weight; + + if weights_sum == 0.0 { + // neither should contribute to load, so there is no load + return 0.0; + } + + (self.cpu_load() * cpu_weight + self.mem_load() * mem_weight) / weights_sum + } } /// A node in the cluster context. @@ -94,3 +149,62 @@ impl Node { self.resources.contains(sid) } } + +#[cfg(test)] +mod tests { + use super::*; + + fn stats() -> NodeStats { + NodeStats { + cpu: 4.0, + maxcpu: 16, + mem: 48 << 30, + maxmem: 64 << 30, + } + } + + #[test] + fn test_weighted_load_with_equal_weights_is_load() { + let stats = stats(); + + assert_eq!(stats.weighted_load(&NodeStatsWeights::EQUAL), stats.load()); + assert_eq!( + stats.weighted_load(&NodeStatsWeights { + cpu_weight: 3.0, + mem_weight: 3.0 + }), + stats.load() + ); + } + + #[test] + fn test_weighted_load() { + let stats = stats(); + + assert_eq!( + stats.weighted_load(&NodeStatsWeights { + cpu_weight: 1.0, + mem_weight: 0.0 + }), + stats.cpu_load() + ); + assert_eq!( + stats.weighted_load(&NodeStatsWeights { + cpu_weight: 0.0, + mem_weight: 1.0 + }), + stats.mem_load() + ); + } + + #[test] + fn test_weighted_load_with_zero_weights_is_zero() { + assert_eq!( + stats().weighted_load(&NodeStatsWeights { + cpu_weight: 0.0, + mem_weight: 0.0 + }), + 0.0 + ); + } +} -- 2.47.3