]> git.proxmox.com Git - pve-common.git/blob - src/PVE/CpuSet.pm
bump version to 8.2.1
[pve-common.git] / src / PVE / CpuSet.pm
1 package PVE::CpuSet;
2
3 use strict;
4 use warnings;
5 use PVE::Tools;
6 use PVE::ProcFSTools;
7
8 sub new {
9 my ($class, $members) = @_;
10
11 $members //= {};
12 my $self = bless { members => $members }, $class;
13
14 return $self;
15 }
16
17 # Create a new set with the contents of a cgroup-v1 subdirectory.
18 # Deprecated:
19 sub new_from_cgroup {
20 my ($class, $cgroup, $effective) = @_;
21
22 return $class->new_from_path("/sys/fs/cgroup/cpuset/$cgroup", $effective);
23 }
24
25 # Create a new set from the contents of a complete path to a cgroup directory.
26 sub new_from_path {
27 my ($class, $path, $effective) = @_;
28
29 my $filename;
30 if ($effective) {
31 $filename = "$path/cpuset.effective_cpus";
32 if (!-e $filename) {
33 # cgroupv2:
34 $filename = "$path/cpuset.cpus.effective";
35 }
36 } else {
37 $filename = "$path/cpuset.cpus";
38 }
39
40 my $set_text = PVE::Tools::file_read_firstline($filename) // '';
41
42 my ($count, $members) = parse_cpuset($set_text);
43
44 return $class->new($members);
45 }
46
47 sub parse_cpuset {
48 my ($set_text) = @_;
49
50 my $members = {};
51 my $count = 0;
52
53 foreach my $part (split(/,/, $set_text)) {
54 if ($part =~ /^\s*(\d+)(?:-(\d+))?\s*$/) {
55 my ($from, $to) = ($1, $2);
56 $to //= $1;
57 die "invalid range: $part ($to < $from)\n" if $to < $from;
58 for (my $i = $from; $i <= $to; $i++) {
59 $members->{$i} = 1;
60 $count++;
61 };
62 } else {
63 die "invalid range: $part\n";
64 }
65 }
66
67 return ($count, $members);
68 }
69
70 # Deprecated:
71 sub write_to_cgroup {
72 my ($self, $cgroup) = @_;
73
74 return $self->write_to_path("/sys/fs/cgroup/cpuset/$cgroup");
75 }
76
77 # Takes the cgroup directory containing the cpuset.cpus file (to be closer to
78 # new_from_path behavior this doesn't take the complete file name).
79 sub write_to_path {
80 my ($self, $path) = @_;
81
82 my $filename = "$path/cpuset.cpus";
83
84 my $value = '';
85 my @members = $self->members();
86 foreach my $cpuid (@members) {
87 $value .= ',' if length($value);
88 $value .= $cpuid;
89 }
90
91 open(my $fh, '>', $filename) || die "failed to open '$filename' - $!\n";
92 PVE::Tools::safe_print($filename, $fh, "$value\n");
93 close($fh) || die "failed to close '$filename' - $!\n";
94 }
95
96 sub insert {
97 my ($self, @members) = @_;
98
99 my $count = 0;
100
101 foreach my $cpu (@members) {
102 next if $self->{members}->{$cpu};
103 $self->{members}->{$cpu} = 1;
104 $count++;
105 }
106
107 return $count;
108 }
109
110 sub delete {
111 my ($self, @members) = @_;
112
113 my $count = 0;
114
115 foreach my $cpu (@members) {
116 next if !$self->{members}->{$cpu};
117 delete $self->{members}->{$cpu};
118 $count++;
119 }
120
121 return $count;
122 }
123
124 sub has {
125 my ($self, $cpuid) = @_;
126
127 return $self->{members}->{$cpuid};
128 }
129
130 # members: this list is always sorted!
131 sub members {
132 my ($self) = @_;
133
134 my @sorted_members = sort { $a <=> $b } keys %{$self->{members}};
135 return @sorted_members;
136 }
137
138 sub size {
139 my ($self) = @_;
140
141 return scalar(keys %{$self->{members}});
142 }
143
144 sub is_equal {
145 my ($self, $set2) = @_;
146
147 my $members1 = $self->{members};
148 my $members2 = $set2->{members};
149
150 foreach my $id (keys %$members1) {
151 return 0 if !$members2->{$id};
152 }
153 foreach my $id (keys %$members2) {
154 return 0 if !$members1->{$id};
155 }
156
157 return 1;
158 }
159
160 sub short_string {
161 my ($self) = @_;
162
163 my @members = $self->members();
164
165 my $res = '';
166 my ($last, $next);
167 foreach my $cpu (@members) {
168 if (!defined($last)) {
169 $last = $next = $cpu;
170 } elsif (($next + 1) == $cpu) {
171 $next = $cpu;
172 } else {
173 $res .= ',' if length($res);
174 if ($last != $next) {
175 $res .= "$last-$next";
176 } else {
177 $res .= "$last";
178 }
179 $last = $next = $cpu;
180 }
181 }
182
183 if (defined($last)) {
184 $res .= ',' if length($res);
185 if ($last != $next) {
186 $res .= "$last-$next";
187 } else {
188 $res .= "$last";
189 }
190 }
191
192 return $res;
193 }
194
195 1;