[PATCH ha-manager v2 26/40] sim: hardware: add getters for dynamic {node,service} stats

Daniel Kral
2026-03-24T18:35:26Z

From: Dominik Rusovac <d.rusovac@proxmox.com>

Aggregation of dynamic node stats is lazy.

Getters log on warning level in case of overcommitted stats.

Signed-off-by: Dominik Rusovac <d.rusovac@proxmox.com>
Signed-off-by: Daniel Kral <d.kral@proxmox.com>
---
changes v1 -> v2:
- new!

wrt v1:
- keep each commit functional on its own
- allow testing overcommitted scenarios

src/PVE/HA/Sim/Env.pm | 12 ++++++++
src/PVE/HA/Sim/Hardware.pm | 59 ++++++++++++++++++++++++++++++++++++++
2 files changed, 71 insertions(+)

diff --git a/src/PVE/HA/Sim/Env.pm b/src/PVE/HA/Sim/Env.pm
index ad51245c..65d4efad 100644
--- a/src/PVE/HA/Sim/Env.pm
+++ b/src/PVE/HA/Sim/Env.pm
@@ -500,12 +500,24 @@ sub get_static_service_stats {
return $self->{hardware}->get_static_service_stats();
}

+sub get_dynamic_service_stats {
+ my ($self) = @_;
+
+ return $self->{hardware}->get_dynamic_service_stats();
+}
+
sub get_static_node_stats {
my ($self) = @_;

return $self->{hardware}->get_static_node_stats();
}

+sub get_dynamic_node_stats {
+ my ($self) = @_;
+
+ return $self->{hardware}->get_dynamic_node_stats();
+}
+
sub get_node_version {
my ($self, $node) = @_;

diff --git a/src/PVE/HA/Sim/Hardware.pm b/src/PVE/HA/Sim/Hardware.pm
index 89180ad7..c9362fd6 100644
--- a/src/PVE/HA/Sim/Hardware.pm
+++ b/src/PVE/HA/Sim/Hardware.pm
@@ -1196,6 +1196,27 @@ sub get_static_service_stats {
return $stats;
}

+sub get_dynamic_service_stats {
+ my ($self) = @_;
+
+ my $stats = get_cluster_service_stats($self);
+ my $static_stats = $self->read_static_service_stats();
+ my $dynamic_stats = $self->read_dynamic_service_stats();
+
+ for my $sid (keys %$stats) {
+ $stats->{$sid}->{usage} = {
+ $static_stats->{$sid}->%*, $dynamic_stats->{$sid}->%*,
+ };
+
+ $self->log('warning', "overcommitted cpu on '$sid'")
+ if $stats->{$sid}->{usage}->{cpu} > $stats->{$sid}->{usage}->{maxcpu};
+ $self->log('warning', "overcommitted mem on '$sid'")
+ if $stats->{$sid}->{usage}->{mem} > $stats->{$sid}->{usage}->{maxmem};
+ }
+
+ return $stats;
+}
+
sub get_static_node_stats {
my ($self) = @_;

@@ -1209,6 +1230,44 @@ sub get_static_node_stats {
return $stats;
}

+sub get_dynamic_node_stats {
+ my ($self) = @_;
+
+ my $stats = $self->get_static_node_stats();
+ for my $node (keys %$stats) {
+ $stats->{$node}->{maxcpu} = $stats->{$node}->{maxcpu} // $default_node_maxcpu;
+ $stats->{$node}->{cpu} = $stats->{$node}->{cpu} // 0.0;
+ $stats->{$node}->{maxmem} = $stats->{$node}->{maxmem} // $default_node_maxmem;
+ $stats->{$node}->{mem} = $stats->{$node}->{mem} // 0;
+ }
+
+ my $service_conf = $self->read_service_config();
+ my $dynamic_service_stats = $self->get_dynamic_service_stats();
+
+ my $cstatus = $self->read_hardware_status_nolock();
+ my $node_service_status = { map { $_ => $self->read_service_status($_) } keys %$cstatus };
+
+ for my $sid (keys %$service_conf) {
+ my $node = $service_conf->{$sid}->{node};
+
+ if ($node_service_status->{$node}->{$sid}) {
+ my ($cpu, $mem) = $dynamic_service_stats->{$sid}->{usage}->@{qw(cpu mem)};
+
+ die "unknown cpu load for '$sid'" if !defined($cpu);
+ $stats->{$node}->{cpu} += $cpu;
+ $self->log('warning', "overcommitted cpu on '$node'")
+ if $stats->{$node}->{cpu} > $stats->{$node}->{maxcpu};
+
+ die "unknown memory usage for '$sid'" if !defined($mem);
+ $stats->{$node}->{mem} += $mem;
+ $self->log('warning', "overcommitted mem on '$node'")
+ if $stats->{$node}->{mem} > $stats->{$node}->{maxmem};
+ }
+ }
+
+ return $stats;
+}
+
sub get_node_version {
my ($self, $node) = @_;

--
2.47.3