all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Dominik Rusovac <d.rusovac@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH proxmox v3 04/19] resource-scheduling: node: add weighted node load
Date: Tue,  1 Dec 2026 11:16:44 +0100	[thread overview]
Message-ID: <20261201101659.3690943-5-d.rusovac@proxmox.com> (raw)
In-Reply-To: <20261201101659.3690943-1-d.rusovac@proxmox.com>

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 <d.rusovac@proxmox.com>
Reviewed-by: Daniel Kral <d.kral@proxmox.com>
---

Notes:
    changes since v2:
    * reword docs to not expose too much implementation details
    * remove 'fix #7993' in commit subject
    
    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 | 111 ++++++++++++++++++++++++
 1 file changed, 111 insertions(+)

diff --git a/proxmox-resource-scheduling/src/node.rs b/proxmox-resource-scheduling/src/node.rs
index 3c7ed393..64322007 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,25 @@ impl NodeStats {
     pub fn load(&self) -> f64 {
         (self.cpu_load() + self.mem_load()) / 2.0
     }
+
+    /// Returns the weighted combined node usage as a percentage with respect to `weights`.
+    ///
+    /// With equal weights, this is the same as [`Self::load`].
+    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 +146,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





  parent reply	other threads:[~2026-12-01 10:18 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 10:16 [RFC PATCH-SERIES cluster/ha-manager/manager/proxmox{,-perl-rs} v3 00/19] fix #7993: implement weighted crs load balancing Dominik Rusovac
2026-12-01 10:16 ` [PATCH proxmox v3 01/19] resource-scheduling: fix typos Dominik Rusovac
2026-12-01 10:16 ` [PATCH proxmox v3 02/19] resource-scheduling: scheduler: refine score_* docs Dominik Rusovac
2026-12-01 10:16 ` [PATCH proxmox v3 03/19] resource-scheduling: scheduler: abstract post-migration stats logic Dominik Rusovac
2026-12-01 10:16 ` Dominik Rusovac [this message]
2026-12-01 10:16 ` [PATCH proxmox v3 05/19] resource-scheduling: scheduler: make imbalance with candidate public Dominik Rusovac
2026-12-01 10:16 ` [PATCH proxmox v3 06/19] fix #7993: resource-scheduling: scheduler: add weights Dominik Rusovac
2026-12-01 10:16 ` [PATCH proxmox v3 07/19] workspace,resource-scheduling: add proptest as dev-dependency Dominik Rusovac
2026-12-01 10:16 ` [PATCH proxmox v3 08/19] resource-scheduling: scheduler: add property tests for weights Dominik Rusovac
2026-12-01 10:16 ` [PATCH proxmox-perl-rs v3 09/19] fix #7993: pve-rs: resource-scheduling: add scheduler options Dominik Rusovac
2026-12-01 10:16 ` [PATCH pve-cluster v3 10/19] fix #7993: datacenter config: add ha-{cpu,mem}-weight crs options Dominik Rusovac
2026-12-01 10:16 ` [PATCH pve-manager v3 11/19] ui: form: crs options: make fine-tuning options advanced Dominik Rusovac
2026-12-01 10:16 ` [PATCH pve-manager v3 12/19] fix #7993: ui: form: crs options: add weights Dominik Rusovac
2026-12-01 10:16 ` [PATCH pve-ha-manager v3 13/19] manager: remove obsolete argument Dominik Rusovac
2026-12-01 10:16 ` [PATCH pve-ha-manager v3 14/19] manager: rename update_crs_scheduler_mode to update_crs_settings Dominik Rusovac
2026-12-01 10:16 ` [PATCH pve-ha-manager v3 15/19] manager: factor out crs option update helper Dominik Rusovac
2026-12-01 10:16 ` [PATCH pve-ha-manager v3 16/19] manager: log changes to crs settings Dominik Rusovac
2026-12-01 10:16 ` [PATCH pve-ha-manager v3 17/19] sim: hardware: allow changing datacenter settings at runtime Dominik Rusovac
2026-12-01 10:16 ` [PATCH pve-ha-manager v3 18/19] fix #7993: manager,usage: use weights for static and dynamic mode Dominik Rusovac
2026-12-01 10:16 ` [PATCH pve-ha-manager v3 19/19] test: add automatic rebalancing test cases with weights Dominik Rusovac

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261201101659.3690943-5-d.rusovac@proxmox.com \
    --to=d.rusovac@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal