statd: refactor update_node_status

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
Thomas Lamprecht 2019-11-13 08:42:00 +01:00
parent 7887310045
commit 0dd73a7fec

View File

@ -144,40 +144,28 @@ sub update_node_status {
sub auto_balloning { sub auto_balloning {
my ($vmstatus) = @_; my ($vmstatus) = @_;
my $log = sub { my $log = sub { $opt_debug and printf @_ };
return if !$opt_debug;
print @_;
};
my $hostmeminfo = PVE::ProcFSTools::read_meminfo(); my $hostmeminfo = PVE::ProcFSTools::read_meminfo();
# NOTE: to debug, run 'pvestatd -d' and set memtotal here
# to debug, run 'pvestatd -d' and set memtotal here
#$hostmeminfo->{memtotal} = int(2*1024*1024*1024/0.8); # you can set this to test #$hostmeminfo->{memtotal} = int(2*1024*1024*1024/0.8); # you can set this to test
my $hostfreemem = $hostmeminfo->{memtotal} - $hostmeminfo->{memused}; my $hostfreemem = $hostmeminfo->{memtotal} - $hostmeminfo->{memused};
# we try to use about 80% host memory # try to use ~80% host memory; goal is the change amount required to achieve that
# goal: we want to change memory usage by this amount (positive or negative) my $goal = int($hostmeminfo->{memtotal} * 0.8 - $hostmeminfo->{memused});
my $goal = int($hostmeminfo->{memtotal}*0.8 - $hostmeminfo->{memused}); $log->("host goal: $goal free: $hostfreemem total: $hostmeminfo->{memtotal}\n");
my $maxchange = 100*1024*1024; my $maxchange = 100*1024*1024;
my $res = PVE::AutoBalloon::compute_alg1($vmstatus, $goal, $maxchange); my $res = PVE::AutoBalloon::compute_alg1($vmstatus, $goal, $maxchange);
&$log("host goal: $goal free: $hostfreemem total: $hostmeminfo->{memtotal}\n");
foreach my $vmid (keys %$vmstatus) { for my $vmid (sort keys %$res) {
next if !$res->{$vmid}; my $target = int($res->{$vmid});
my $d = $vmstatus->{$vmid}; my $current = int($vmstatus->{$vmid}->{balloon});
my $diff = int($res->{$vmid} - $d->{balloon}); next if $target == $current; # no need to change
my $absdiff = $diff < 0 ? -$diff : $diff;
if ($absdiff > 0) { $log->("BALLOON $vmid to $target (%d)\n", $target - $current);
&$log("BALLOON $vmid to $res->{$vmid} ($diff)\n"); eval { PVE::QemuServer::vm_mon_cmd($vmid, "balloon", value => $target) };
eval { warn $@ if $@;
PVE::QemuServer::vm_mon_cmd($vmid, "balloon",
value => int($res->{$vmid}));
};
warn $@ if $@;
}
} }
} }