public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Elias Huhsovitz <e.huhsovitz@proxmox.com>
To: pve-devel@lists.proxmox.com
Cc: Elias Huhsovitz <e.huhsovitz@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	[thread overview]
Message-ID: <20261203105300.70656-3-e.huhsovitz@proxmox.com> (raw)
In-Reply-To: <20261203105300.70656-1-e.huhsovitz@proxmox.com>

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 <e.huhsovitz@proxmox.com>
---
 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





  parent reply	other threads:[~2026-12-03 10:53 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 10:52 [PATCH common/qemu-server v6 0/5] vCPU binding for qemu guests Elias Huhsovitz
2026-12-03 10:52 ` [PATCH common v6 1/5] syscall: add sched_setaffinity Elias Huhsovitz
2026-12-03 10:52 ` Elias Huhsovitz [this message]
2026-12-03 10:52 ` [PATCH qemu-server v6 3/5] helpers: return integer values for parse_number_sets Elias Huhsovitz
2026-12-03 10:52 ` [PATCH qemu-server v6 4/5] memory: unify NUMA config parsing and resolution Elias Huhsovitz
2026-12-03 11:34   ` Elias Huhsovitz
2026-12-03 10:53 ` [PATCH qemu-server v6 5/5] cpu: add affinity module Elias Huhsovitz

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261203105300.70656-3-e.huhsovitz@proxmox.com \
    --to=e.huhsovitz@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal