]> git.proxmox.com Git - pve-common.git/blame_incremental - src/PVE/CpuSet.pm
fix #4299: network : disable_ipv6: fix path checking
[pve-common.git] / src / PVE / CpuSet.pm
... / ...
CommitLineData
1package PVE::CpuSet;
2
3use strict;
4use warnings;
5use PVE::Tools;
6use PVE::ProcFSTools;
7
8sub 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:
19sub 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.
26sub 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
47sub 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:
71sub 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).
79sub 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
96sub 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
110sub 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
124sub has {
125 my ($self, $cpuid) = @_;
126
127 return $self->{members}->{$cpuid};
128}
129
130# members: this list is always sorted!
131sub members {
132 my ($self) = @_;
133
134 return sort { $a <=> $b } keys %{$self->{members}};
135}
136
137sub size {
138 my ($self) = @_;
139
140 return scalar(keys %{$self->{members}});
141}
142
143sub is_equal {
144 my ($self, $set2) = @_;
145
146 my $members1 = $self->{members};
147 my $members2 = $set2->{members};
148
149 foreach my $id (keys %$members1) {
150 return 0 if !$members2->{$id};
151 }
152 foreach my $id (keys %$members2) {
153 return 0 if !$members1->{$id};
154 }
155
156 return 1;
157}
158
159sub short_string {
160 my ($self) = @_;
161
162 my @members = $self->members();
163
164 my $res = '';
165 my ($last, $next);
166 foreach my $cpu (@members) {
167 if (!defined($last)) {
168 $last = $next = $cpu;
169 } elsif (($next + 1) == $cpu) {
170 $next = $cpu;
171 } else {
172 $res .= ',' if length($res);
173 if ($last != $next) {
174 $res .= "$last-$next";
175 } else {
176 $res .= "$last";
177 }
178 $last = $next = $cpu;
179 }
180 }
181
182 if (defined($last)) {
183 $res .= ',' if length($res);
184 if ($last != $next) {
185 $res .= "$last-$next";
186 } else {
187 $res .= "$last";
188 }
189 }
190
191 return $res;
192}
193
1941;