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 445F21FF0DE for ; Thu, 19 Nov 2026 13:28:38 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id EB720216F3; Thu, 19 Nov 2026 13:28:35 +0100 (CET) Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Thu, 19 Nov 2026 13:28:30 +0100 Message-Id: Subject: Re: [PATCH proxmox v2 04/19] fix #7993: resource-scheduling: node: add weighted node load From: "Daniel Kral" To: "Dominik Rusovac" , X-Mailer: aerc 0.22.0-45-gc30c872fde7b-dirty References: <20261112094437.1951271-1-d.rusovac@proxmox.com> <20261112094437.1951271-5-d.rusovac@proxmox.com> In-Reply-To: <20261112094437.1951271-5-d.rusovac@proxmox.com> X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1795091310525 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.441 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: CGB4GB5JOLIIP3UGMOOV2GMMVJNAGQHG X-Message-ID-Hash: CGB4GB5JOLIIP3UGMOOV2GMMVJNAGQHG X-MailFrom: d.kral@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: nit: drop the 'fix #7993' as this is only prepatory for adding weights later, which is the actual fix With or without the nits, consider as: Reviewed-by: Daniel Kral On Thu Nov 12, 2026 at 10:44 AM CET, Dominik Rusovac wrote: > 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 functio= n and > then set the public fields, if required > - exhaustive deconstructuring in weighted_load function will forc= e > compile error for new field, which is desirable > - a new field must default to 0.0, otherwise a caller starting fr= om > default function might silently get a different weighted_load > result > =20 > dispatched changes into this new patch to make the series more organi= zed > =20 > [0] https://lore.proxmox.com/all/20261021085932.1196302-4-d.rusovac@p= roxmox.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-s= cheduling/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; > =20 > use crate::resource::ResourceStats; > =20 > +/// 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-negati= ve. > + pub cpu_weight: f64, > + /// Weight of [`NodeStats::mem_load`]. Must be finite and non-negati= ve. > + 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 =3D 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 [wei= ghted arithmetic mean] of > + /// [`Self::cpu_load`] and [`Self::mem_load`] with respect to `weigh= ts`. nit: Returns the [weighted] combined node usage as a percentage with respect to `weights`. Just to not expose too much implementation detail in the docs, which needs to be changed if node stat and weight fields should ever be added in the future, but this is just a matter of taste, so can also be ignored. > + /// > + /// With equal weights, this is the same as [`Self::load`]. > + /// > + /// [weighted arithmetic mean]: https://en.wikipedia.org/wiki/Weight= ed_arithmetic_mean > + pub fn weighted_load(&self, weights: &NodeStatsWeights) -> f64 { > + let NodeStatsWeights { > + cpu_weight, > + mem_weight, > + } =3D *weights; > + > + let weights_sum =3D cpu_weight + mem_weight; > + > + if weights_sum =3D=3D 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 > + } > } > =20 > /// 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 =3D 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 =3D 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 > + ); > + } > +}