]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/CpuSet.pm
CpuSet write_to_cgroup: catch errors from close()
[pve-common.git] / src / PVE / CpuSet.pm
index ef660b5c52ca04d2fe6fc2b5de6d905790ba4232..9f76f385d27843282cc617167b751020d88539bd 100644 (file)
@@ -3,8 +3,20 @@ package PVE::CpuSet;
 use strict;
 use warnings;
 use PVE::Tools;
+use PVE::ProcFSTools;
 
-our $MAX_CPUID = 256; # should be enough for the next years
+my $MAX_CPUID;
+
+sub max_cpuid {
+
+    return $MAX_CPUID if defined($MAX_CPUID);
+
+    my $cpuinfo = PVE::ProcFSTools::read_cpuinfo();
+
+    $MAX_CPUID = $cpuinfo->{cpus} || 1;
+
+    return $MAX_CPUID;
+}
 
 sub new {
     my ($this) = @_;
@@ -12,7 +24,9 @@ sub new {
     my $class = ref($this) || $this;
 
     my $self = bless { members => {} }, $class;
-
+    
+    max_cpuid() if !defined($MAX_CPUID); # initialize $MAX_CPUID
+    
     return $self;
 }
 
@@ -68,7 +82,7 @@ sub write_to_cgroup {
 
     open(my $fh, '>', $filename) || die "failed to open '$filename' - $!\n";
     PVE::Tools::safe_print($filename, $fh, "$value\n");
-    close($fh);
+    close($fh) || die "failed to close '$filename' - $!\n";
 }
 
 sub insert {
@@ -136,4 +150,39 @@ sub is_equal {
     return 1;
 }
 
+sub short_string {
+    my ($self) = @_;
+
+    my @members = $self->members();
+
+    my $res = '';
+    my ($last, $next);
+    foreach my $cpu (@members) {
+       if (!defined($last)) {
+           $last = $next = $cpu;
+       } elsif (($next + 1) == $cpu) {
+           $next = $cpu;
+       } else {
+           $res .= ',' if length($res);
+           if ($last != $next) {
+               $res .= "$last-$next";
+           } else {
+               $res .= "$last";
+           }
+           $last = $next = $cpu;
+       }
+    }
+
+    if (defined($last)) {
+       $res .= ',' if length($res);
+       if ($last != $next) {
+           $res .= "$last-$next";
+       } else {
+           $res .= "$last";
+       }
+    }
+
+    return $res;
+}
+
 1;