X-Git-Url: https://git.proxmox.com/?p=pve-common.git;a=blobdiff_plain;f=src%2FPVE%2FCpuSet.pm;h=92fd18fe73a86c6120f5f0f70547aa1eb8275fd5;hp=ef660b5c52ca04d2fe6fc2b5de6d905790ba4232;hb=e97f807c388c10250f442b1f16c5315df2ffc2af;hpb=a1c3f18e0689a1a2a59f8d8d90242338f8e4394b diff --git a/src/PVE/CpuSet.pm b/src/PVE/CpuSet.pm index ef660b5..92fd18f 100644 --- a/src/PVE/CpuSet.pm +++ b/src/PVE/CpuSet.pm @@ -3,8 +3,7 @@ package PVE::CpuSet; use strict; use warnings; use PVE::Tools; - -our $MAX_CPUID = 256; # should be enough for the next years +use PVE::ProcFSTools; sub new { my ($this) = @_; @@ -34,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; @@ -68,7 +65,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 { @@ -77,7 +74,6 @@ sub insert { 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++; @@ -92,7 +88,6 @@ sub delete { 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++; @@ -111,7 +106,7 @@ sub has { sub members { my ($self) = @_; - return sort keys %{$self->{members}}; + return sort { $a <=> $b } keys %{$self->{members}}; } sub size { @@ -136,4 +131,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;