From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [IPv6:2a0f:8001:1:32::40]) by lore.proxmox.com (Postfix) with ESMTPS id 9EA321FF0C2 for ; Thu, 03 Dec 2026 11:53:23 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 9D05721789; Thu, 03 Dec 2026 11:53:12 +0100 (CET) From: Elias Huhsovitz To: pve-devel@lists.proxmox.com Subject: [PATCH common v6 2/5] cpuset: generalize to improve usability for sysfs cpusets. Date: Thu, 3 Dec 2026 11:52:57 +0100 Message-ID: <20261203105300.70656-3-e.huhsovitz@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261203105300.70656-1-e.huhsovitz@proxmox.com> References: <20261203105300.70656-1-e.huhsovitz@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1796295186844 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.387 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: JJ5WXLON2JCQ62SQZNNKA7OIDWIG7TFG X-Message-ID-Hash: JJ5WXLON2JCQ62SQZNNKA7OIDWIG7TFG X-MailFrom: e.huhsovitz@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Elias Huhsovitz X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Add from_str/from_file to create cpuset directly from string/file. Utilize from_file in existing new_from_path subroutine. Add intersect between two CpuSet. Add member_keys to get a list of all unsorted members. Remove redundant for loop in is_equal by adding guard clause that compares set sizes. Signed-off-by: Elias Huhsovitz --- src/PVE/CpuSet.pm | 76 +++++++++++++++++++++++++++++++++++++---------- 1 file changed, 61 insertions(+), 15 deletions(-) diff --git a/src/PVE/CpuSet.pm b/src/PVE/CpuSet.pm index 7023b8f..f3e8c20 100644 --- a/src/PVE/CpuSet.pm +++ b/src/PVE/CpuSet.pm @@ -6,6 +6,8 @@ use PVE::File; use PVE::ProcFSTools; use PVE::Tools; +use constant MAX_MEMBERS => 2048; + sub new { my ($class, $members) = @_; @@ -15,6 +17,27 @@ sub new { return $self; } +sub from_range { + my ($class, $from, $to) = @_; + + my $members = {}; + add_range_to_members($members, $from, $to); + + return $class->new($members); +} + +sub from_str { + my ($class, $s, $delim) = @_; + my (undef, $members) = PVE::CpuSet::parse_cpuset($s, $delim); + return $class->new($members); +} + +sub from_file { + my ($class, $path) = @_; + my $content = PVE::File::file_read_first_line($path) // ''; + return $class->from_str($content); +} + # Create a new set with the contents of a cgroup-v1 subdirectory. # Deprecated: sub new_from_cgroup { @@ -24,7 +47,7 @@ sub new_from_cgroup { } # Create a new set from the contents of a complete path to a cgroup directory. -sub new_from_path { +sub new_from_path { # TODO: naming is inaccurate and should be changed to e.g. 'from_cgroup_path' my ($class, $path, $effective) = @_; my $filename; @@ -38,28 +61,36 @@ sub new_from_path { $filename = "$path/cpuset.cpus"; } - my $set_text = PVE::File::file_read_first_line($filename) // ''; + return $class->from_file($filename); +} - my ($count, $members) = parse_cpuset($set_text); +sub add_range_to_members { + my ($members, $from, $to) = @_; + my $diff = $to - $from; + die "invalid range: $from-$to ($to < $from)\n" if $diff < 0; + die "exceed maximum CpuSet capacity " . MAX_MEMBERS . ". Requested $diff" + if $diff > MAX_MEMBERS - 1; - return $class->new($members); + my $count = 0; + for my $i ($from .. $to) { + $members->{$i} = 1; + $count++; + } + return $count; } sub parse_cpuset { - my ($set_text) = @_; + my ($set_text, $delim) = @_; + $delim //= ','; # default to comma my $members = {}; my $count = 0; - foreach my $part (split(/,/, $set_text)) { + foreach my $part (split(/\Q$delim\E/, $set_text)) { if ($part =~ /^\s*(\d+)(?:-(\d+))?\s*$/) { my ($from, $to) = ($1, $2); $to //= $1; - die "invalid range: $part ($to < $from)\n" if $to < $from; - for (my $i = $from; $i <= $to; $i++) { - $members->{$i} = 1; - $count++; - } + $count += add_range_to_members($members, $from, $to); } else { die "invalid range: $part\n"; } @@ -122,6 +153,16 @@ sub delete { return $count; } +sub intersect { + my ($self, $other) = @_; + + my $res = __PACKAGE__->new(); + for my $cpu (keys $self->{members}->%*) { + $res->insert($cpu) if $other->has($cpu); + } + return $res; +} + sub has { my ($self, $cpuid) = @_; @@ -136,6 +177,12 @@ sub members { return @sorted_members; } +# use members() if order matters +sub members_unsorted { + my ($self) = @_; + return keys %{ $self->{members} }; +} + sub size { my ($self) = @_; @@ -148,11 +195,10 @@ sub is_equal { my $members1 = $self->{members}; my $members2 = $set2->{members}; + return 0 if keys(%$members1) != keys(%$members2); + foreach my $id (keys %$members1) { - return 0 if !$members2->{$id}; - } - foreach my $id (keys %$members2) { - return 0 if !$members1->{$id}; + return 0 if !exists $members2->{$id}; } return 1; -- 2.47.3