[pve-devel] [PATCH container 1/7] config: add pool usage helper
Fabian Grünbichler
f.gruenbichler at proxmox.com
Wed Apr 10 15:12:59 CEST 2024
to avoid repeating those calculations all over the place.
Signed-off-by: Fabian Grünbichler <f.gruenbichler at proxmox.com>
---
src/PVE/LXC/Config.pm | 35 +++++++++++++++++++++++++++++++++++
1 file changed, 35 insertions(+)
diff --git a/src/PVE/LXC/Config.pm b/src/PVE/LXC/Config.pm
index 5ac1446..908d64a 100644
--- a/src/PVE/LXC/Config.pm
+++ b/src/PVE/LXC/Config.pm
@@ -11,6 +11,7 @@ use PVE::DataCenterConfig;
use PVE::GuestHelpers;
use PVE::INotify;
use PVE::JSONSchema qw(get_standard_option);
+use PVE::ProcFSTools;
use PVE::Tools;
use PVE::LXC;
@@ -1877,4 +1878,38 @@ sub foreach_passthrough_device {
}
}
+my $cpuinfo = PVE::ProcFSTools::read_cpuinfo();
+
+# for determining pool usage vs limits
+#
+# this gives the higher of pending or currently configured
+sub get_pool_usage {
+ my ($class, $conf) = @_;
+
+ my $usage = {};
+
+ my $get_max = sub {
+ my $max = 0;
+
+ for my $curr (@_) {
+ $max = $curr if defined($curr) && $curr > $max;
+ }
+
+ return $max;
+ };
+
+ $usage->{cpu} = $get_max->((
+ $conf->{pending}->{cores},
+ $conf->{cores},
+ ));
+ $usage->{cpu} = $cpuinfo->{cpus} if !$usage->{cpu};
+
+ my $swap = $get_max->(($conf->{pending}->{swap}, $conf->{swap}));
+ my $mem = $get_max->(($conf->{pending}->{memory}, $conf->{memory}, 512));
+ $usage->{mem} = $mem+$swap;
+ $usage->{mem} *= 1024*1024;
+
+ return $usage;
+}
+
1;
--
2.39.2
More information about the pve-devel
mailing list