]> git.proxmox.com Git - pve-common.git/blobdiff - src/PVE/CpuSet.pm
bump version to 6.0-18
[pve-common.git] / src / PVE / CpuSet.pm
index 753b3cdec4cac5edc64a251ef9f464593cb034bb..b7dc4f50f5754c2cc598054c0898529090936016 100644 (file)
@@ -5,28 +5,13 @@ use warnings;
 use PVE::Tools;
 use PVE::ProcFSTools;
 
-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) = @_;
 
     my $class = ref($this) || $this;
 
     my $self = bless { members => {} }, $class;
-    
-    max_cpuid() if !defined($MAX_CPUID); # initialize $MAX_CPUID
-    
+
     return $self;
 }
 
@@ -39,7 +24,7 @@ sub new_from_cgroup {
     my $set_text = PVE::Tools::file_read_firstline($filename) // '';
 
     my $cpuset = $this->new();
-    
+
     my $members = $cpuset->{members};
 
     my $count = 0;
@@ -48,8 +33,6 @@ sub new_from_cgroup {
        if ($part =~ /^\s*(\d+)(?:-(\d+))?\s*$/) {
            my ($from, $to) = ($1, $2);
            $to //= $1;
-           die "cpu id '$from' is out of range\n" if $from >= $MAX_CPUID;
-           die "cpu id '$to' is out of range\n" if $to >= $MAX_CPUID;
            die "invalid range: $part ($to < $from)\n" if $to < $from;
            for (my $i = $from; $i <= $to; $i++) {
                $members->{$i} = 1;
@@ -82,16 +65,15 @@ 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 {
     my ($self, @members) = @_;
 
     my $count = 0;
-    
+
     foreach my $cpu (@members) {
-       die "cpu id '$cpu' is out of range\n" if $cpu >= $MAX_CPUID;
        next if $self->{members}->{$cpu};
        $self->{members}->{$cpu} = 1;
        $count++;
@@ -104,9 +86,8 @@ sub delete {
     my ($self, @members) = @_;
 
     my $count = 0;
-    
+
     foreach my $cpu (@members) {
-       die "cpu id '$cpu' is out of range\n" if $cpu >= $MAX_CPUID;
        next if !$self->{members}->{$cpu};
        delete $self->{members}->{$cpu};
        $count++;
@@ -125,8 +106,8 @@ sub has {
 sub members {
     my ($self) = @_;
 
-    return sort keys %{$self->{members}};
-}    
+    return sort { $a <=> $b } keys %{$self->{members}};
+}
 
 sub size {
     my ($self) = @_;
@@ -146,7 +127,7 @@ sub is_equal {
     foreach my $id (keys %$members2) {
        return 0 if !$members1->{$id};
     }
-    
+
     return 1;
 }
 
@@ -164,8 +145,12 @@ sub short_string {
            $next = $cpu;
        } else {
            $res .= ',' if length($res);
-           $res .= "$last-$next";
-           $last = $next = undef;
+           if ($last != $next) {
+               $res .= "$last-$next";
+           } else {
+               $res .= "$last";
+           }
+           $last = $next = $cpu;
        }
     }