]> git.proxmox.com Git - pve-manager.git/blob - PVE/AutoBalloon.pm
edf7332d98ff4edd82a08ef19fd0224bb00a734b
[pve-manager.git] / PVE / AutoBalloon.pm
1 package PVE::AutoBalloon;
2
3 use warnings;
4 use strict;
5
6 sub compute_alg1 {
7 my ($vmstatus, $goal, $maxchange, $debug) = @_;
8
9 my $log = sub { print @_ if $debug; };
10
11 my $change_func = sub {
12 my ($res, $idlist, $bytes) = @_;
13
14 my $rest = $bytes;
15 my $repeat = 1;
16 my $done_hash = {};
17 my $progress = 1;
18
19 while ($rest && $repeat && $progress) {
20 $repeat = 0;
21 $progress = 0;
22
23 my $shares_total = 0;
24 my $alloc_old = 0;
25
26 foreach my $vmid (@$idlist) {
27 next if defined($done_hash->{$vmid});
28 my $d = $vmstatus->{$vmid};
29 my $balloon = defined($res->{$vmid}) ? $res->{$vmid} : $d->{balloon};
30 $alloc_old += $balloon - $d->{balloon_min};
31 $shares_total += $d->{shares} || 1000;
32 }
33
34 my $changes = 0;
35
36 my $alloc_new = $alloc_old + $rest;
37
38 &$log("shares_total: $shares_total $alloc_new\n");
39
40 foreach my $vmid (@$idlist) {
41 next if defined($done_hash->{$vmid});
42 my $d = $vmstatus->{$vmid};
43 my $shares = $d->{shares} || 1000;
44 my $desired = $d->{balloon_min} + int(($alloc_new/$shares_total)*$shares);
45
46 if ($desired > $d->{maxmem}) {
47 $desired = $d->{maxmem};
48 $repeat = 1;
49 } elsif ($desired < $d->{balloon_min}) {
50 $desired = $d->{balloon_min};
51 $repeat = 1;
52 }
53
54 my ($new, $balloon);
55 if (($bytes > 0) && ($desired - $d->{balloon}) > 0) { # grow
56 $new = $d->{balloon} + $maxchange;
57 $balloon = $new > $desired ? $desired : $new;
58 } elsif (($desired - $d->{balloon}) < 0) { # shrink
59 $new = $d->{balloon} - $maxchange;
60 $balloon = $new > $desired ? $new : $desired;
61 } else {
62 $done_hash->{$vmid} = 1;
63 next;
64 }
65
66 my $diff = $balloon - $d->{balloon};
67 if ($diff != 0) {
68 my $oldballoon = defined($res->{$vmid}) ? $res->{$vmid} : $d->{balloon};
69 $res->{$vmid} = $balloon;
70 my $change = $balloon - $oldballoon;
71 if ($change != 0) {
72 $changes += $change;
73 my $absdiff = $diff > 0 ? $diff : -$diff;
74 $progress += $absdiff;
75 $repeat = 1;
76 }
77 &$log("change request for $vmid ($balloon, $diff, $desired, $new, $changes, $progress)\n");
78 }
79 }
80
81 $rest -= $changes;
82 }
83
84 return $rest;
85 };
86
87
88 my $idlist = []; # list of VMs with working balloon river
89 my $idlist1 = []; # list of VMs with memory pressure
90 my $idlist2 = []; # list of VMs with enough free memory
91
92 foreach my $vmid (keys %$vmstatus) {
93 my $d = $vmstatus->{$vmid};
94 next if !$d->{balloon}; # skip if balloon driver not running
95 next if !$d->{balloon_min}; # skip if balloon value not set in config
96
97 push @$idlist, $vmid;
98
99 if ($d->{freemem} &&
100 ($d->{freemem} > $d->{balloon_min}*0.25) &&
101 ($d->{balloon} >= $d->{balloon_min})) {
102 push @$idlist2, $vmid;
103 &$log("idlist2 $vmid $d->{balloon}, $d->{balloon_min}, $d->{freemem}\n");
104 } else {
105 push @$idlist1, $vmid;
106 &$log("idlist1 $vmid $d->{balloon}, $d->{balloon_min}, $d->{freemem}\n");
107 }
108 }
109
110 my $res = {};
111
112 if ($goal > 10*1024*1024) {
113 &$log("grow request start $goal\n");
114 # priorize VMs with memory pressure
115 my $rest = &$change_func($res, $idlist1, $goal);
116 if ($rest >= $goal) { # no progress ==> consider all VMs
117 &$log("grow request loop $rest\n");
118 $rest = &$change_func($res, $idlist, $rest);
119 }
120 &$log("grow request end $rest\n");
121
122 } elsif ($goal < -10*1024*1024) {
123 &$log("shrink request $goal\n");
124 # priorize VMs with enough free memory
125 my $rest = &$change_func($res, $idlist2, $goal);
126 if ($rest <= $goal) { # no progress ==> consider all VMs
127 &$log("shrink request loop $rest\n");
128 $rest = &$change_func($res, $idlist, $rest);
129 }
130 &$log("shrink request end $rest\n");
131 } else {
132 &$log("do nothing\n");
133 # do nothing - requested change to small
134 }
135
136 foreach my $vmid (@$idlist) {
137 next if !$res->{$vmid};
138 my $d = $vmstatus->{$vmid};
139 my $diff = int($res->{$vmid} - $d->{balloon});
140 my $absdiff = $diff < 0 ? -$diff : $diff;
141 &$log("BALLOON $vmid to $res->{$vmid} ($diff)\n");
142 }
143 return $res;
144 }
145
146 1;