[PATCH ha-manager v4 09/28] sim: hardware: add set-dynamic-stats command

Daniel Kral
2026-04-02T12:49:54Z

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

Add command to set dynamic service stats and handle respective commands
set-dynamic-stats and set-static-stats analogously.

Signed-off-by: Dominik Rusovac <d.rusovac@proxmox.com>
Signed-off-by: Daniel Kral <d.kral@proxmox.com>
---
changes v3 -> v4:
- none

src/PVE/HA/Sim/Hardware.pm | 34 ++++++++++++++++++++++++++--------
src/PVE/HA/Sim/RTHardware.pm | 4 +++-
2 files changed, 29 insertions(+), 9 deletions(-)

diff --git a/src/PVE/HA/Sim/Hardware.pm b/src/PVE/HA/Sim/Hardware.pm
index 3439bc36..b641f3c9 100644
--- a/src/PVE/HA/Sim/Hardware.pm
+++ b/src/PVE/HA/Sim/Hardware.pm
@@ -795,7 +795,8 @@ sub get_cfs_state {
# service <sid> stop <timeout>
# service <sid> lock/unlock [lockname]
# service <sid> add <node> [<request-state=started>] [<running=0>]
-# service <sid> set-static-stats <maxcpu> <maxmem>
+# service <sid> set-static-stats [maxcpu <cores>] [maxmem <MiB>]
+# service <sid> set-dynamic-stats [cpu <cores>] [mem <MiB>]
# service <sid> delete
sub sim_hardware_cmd {
my ($self, $cmdstr, $logid) = @_;
@@ -954,15 +955,32 @@ sub sim_hardware_cmd {
$params[2] || 0,
);

- } elsif ($action eq 'set-static-stats') {
- die "sim_hardware_cmd: missing maxcpu for '$action' command" if !$params[0];
- die "sim_hardware_cmd: missing maxmem for '$action' command" if !$params[1];
+ } elsif ($action eq 'set-static-stats' || $action eq 'set-dynamic-stats') {
+ die "sim_hardware_cmd: missing target stat for '$action' command"
+ if !@params;

- $self->set_static_service_stats(
- $sid,
- { maxcpu => 0.0 + $params[0], maxmem => int($params[1]) },
- );
+ my $conversions =
+ $action eq 'set-static-stats'
+ ? { maxcpu => sub { 0.0 + $_[0] }, maxmem => sub { $_[0] * 1024**2 } }
+ : { cpu => sub { 0.0 + $_[0] }, mem => sub { $_[0] * 1024**2 } };

+ my %new_stats;
+ for my ($target, $val) (@params) {
+ die "sim_hardware_cmd: missing value for '$action $target' command"
+ if !defined($val);
+
+ my $convert = $conversions->{$target}
+ or die
+ "sim_hardware_cmd: unknown target stat '$target' for '$action' command";
+
+ $new_stats{$target} = $convert->($val);
+ }
+
+ if ($action eq 'set-static-stats') {
+ $self->set_static_service_stats($sid, \%new_stats);
+ } else {
+ $self->set_dynamic_service_stats($sid, \%new_stats);
+ }
} elsif ($action eq 'manual-migrate') {

die "sim_hardware_cmd: missing target node for '$action' command"
diff --git a/src/PVE/HA/Sim/RTHardware.pm b/src/PVE/HA/Sim/RTHardware.pm
index 9a83d098..9528f542 100644
--- a/src/PVE/HA/Sim/RTHardware.pm
+++ b/src/PVE/HA/Sim/RTHardware.pm
@@ -532,7 +532,9 @@ sub show_service_add_dialog {

my $maxcpu = $cpu_count_spin->get_value();
my $maxmem = $memory_spin->get_value();
- $self->sim_hardware_cmd("service $sid set-static-stats $maxcpu $maxmem", 'command');
+ $self->sim_hardware_cmd(
+ "service $sid set-static-stats maxcpu $maxcpu maxmem $maxmem", 'command',
+ );

$self->add_service_to_gui($sid);
}
--
2.47.3