21681b88a8626bc785f885ae8499362ce5518c46
3 # This package should deal with figuring out the right cgroup path for a
4 # container (via the command socket), reading and writing cgroup values, and
5 # handling cgroup v1 & v2 differences.
7 # Note that the long term plan is to have resource manage functions instead of
8 # dealing with cgroup files on the outside.
25 # We don't want to do a command socket round trip for every cgroup read/write,
26 # so any cgroup function needs to have the container's path cached, so this
27 # package has to be instantiated.
29 # LXC keeps separate paths by controller (although they're normally all the
30 # same, in our # case anyway), so we cache them by controller as well.
32 my ($class, $vmid) = @_;
34 my $self = { vmid
=> $vmid };
36 return bless $self, $class;
39 # Get the v1 controller list.
41 # Returns a set (hash mapping names to `1`) of cgroupv1 controllers, and an
42 # optional boolean whether a unified (cgroupv2) hierarchy exists.
44 # Deprecated: Use `get_cgroup_controllers()` instead.
45 sub get_v1_controllers
{
48 my $data = PVE
::Tools
::file_get_contents
('/proc/self/cgroup');
49 while ($data =~ /^\d+:([^:\n]*):.*$/gm) {
52 $v1->{$_} = 1 foreach split(/,/, $type);
57 return wantarray ?
($v1, $v2) : $v1;
60 # Get the set v2 controller list from the `cgroup.controllers` file.
61 my sub get_v2_controllers
{
62 my $v2 = eval { file_get_contents
('/sys/fs/cgroup/cgroup.controllers') }
63 || eval { file_get_contents
('/sys/fs/cgroup/unified/cgroup.controllers') };
64 return undef if !defined $v2;
66 # It's a simple space separated list:
67 return { map { $_ => 1 } split(/\s+/, $v2) };
70 my $CGROUP_CONTROLLERS = undef;
71 # Get a list of controllers enabled in each cgroup subsystem.
73 # This is a more complete version of `PVE::LXC::get_cgroup_subsystems`.
75 # Returns 2 sets (hashes mapping controller names to `1`), one for each cgroup
77 sub get_cgroup_controllers
() {
78 if (!defined($CGROUP_CONTROLLERS)) {
79 my ($v1, undef) = get_v1_controllers
();
80 my $v2 = get_v2_controllers
();
82 $CGROUP_CONTROLLERS = [$v1, $v2];
85 return $CGROUP_CONTROLLERS->@*;
88 my $CGROUP_MODE = undef;
89 # Figure out which cgroup mode we're operating under:
91 # Returns 1 if cgroupv1 controllers exist (hybrid or legacy mode), and 2 in a
92 # cgroupv2-only environment.
94 # NOTE: To fully support a hybrid layout it is better to use functions like
95 # `cpuset_controller_path`.
97 # This is a function, not a method!
99 if (!defined($CGROUP_MODE)) {
100 my ($v1, $v2) = get_cgroup_controllers
();
102 # hybrid or legacy mode
109 die "unknown cgroup mode\n" if !defined($CGROUP_MODE);
113 my $CGROUPV2_PATH = undef;
114 sub cgroupv2_base_path
() {
115 if (!defined($CGROUPV2_PATH)) {
116 if (cgroup_mode
() == 2) {
117 $CGROUPV2_PATH = '/sys/fs/cgroup';
119 $CGROUPV2_PATH = '/sys/fs/cgroup/unified';
122 return $CGROUPV2_PATH;
125 # Find a cgroup controller and return its path and version.
127 # LXC initializes the unified hierarchy first, so if a controller is
128 # available via both we favor cgroupv2 here as well.
130 # Returns nothing if the controller is not available.
132 sub find_cgroup_controller
($) {
133 my ($controller) = @_;
135 my ($v1, $v2) = get_cgroup_controllers
();
137 if (!defined($controller) || $v2->{$controller}) {
138 my $path = cgroupv2_base_path
();
139 return wantarray ?
($path, 2) : $path;
142 if (defined($controller) && $v1->{$controller}) {
143 my $path = "/sys/fs/cgroup/$controller";
144 return wantarray ?
($path, 1) : $path;
150 my $CG_PATH_CPUSET = undef;
151 my $CG_VER_CPUSET = undef;
152 # Find the cpuset cgroup controller.
154 # This is a function, not a method!
155 sub cpuset_controller_path
() {
156 if (!defined($CG_PATH_CPUSET)) {
157 ($CG_PATH_CPUSET, $CG_VER_CPUSET) = find_cgroup_controller
('cpuset')
158 or die "failed to find cpuset controller\n";
161 return wantarray ?
($CG_PATH_CPUSET, $CG_VER_CPUSET) : $CG_PATH_CPUSET;
164 # Get a subdirectory (without the cgroup mount point) for a controller.
166 my ($self, $controller, $limiting) = @_;
168 die "implement in subclass";
171 # Get path and version for a controller.
173 # `$controller` may be `undef`, see get_subdir above for details.
175 # Returns either just the path, or the path and cgroup version as a tuple.
177 my ($self, $controller, $limiting) = @_;
178 # Find the controller before querying the lxc monitor via a socket:
179 my ($cgpath, $ver) = find_cgroup_controller
($controller)
182 my $path = $self->get_subdir($controller, $limiting)
185 $path = "$cgpath/$path";
186 return wantarray ?
($path, $ver) : $path;
189 # Convenience method to get the path info if the first existing controller.
191 # Returns the same as `get_path`.
193 my ($self, $limiting, @controllers) = @_;
196 for my $c (@controllers) {
197 ($path, $ver) = $self->get_path($c, $limiting);
198 last if defined $path;
200 return wantarray ?
($path, $ver) : $path;
203 # Parse a 'Nested keyed' file:
205 # See kernel documentation `admin-guide/cgroup-v2.rst` 4.1.
206 my sub parse_nested_keyed_file
($) {
209 foreach my $line (split(/\n/, $data)) {
210 my ($key, @values) = split(/\s+/, $line);
212 my $d = ($res->{$key} = {});
214 foreach my $value (@values) {
215 if (my ($key, $value) = ($value =~ /^([^=]+)=(.*)$/)) {
218 warn "bad key=value pair in nested keyed file\n";
225 # Parse a 'Flat keyed' file:
227 # See kernel documentation `admin-guide/cgroup-v2.rst` 4.1.
228 my sub parse_flat_keyed_file
($) {
231 foreach my $line (split(/\n/, $data)) {
232 if (my ($key, $value) = ($line =~ /^(\S+)\s+(.*)$/)) {
233 $res->{$key} = $value;
235 warn "bad 'key value' pair in flat keyed file\n";
241 # Parse out 'diskread' and 'diskwrite' values from I/O stats for this container.
250 # With cgroupv1 we have a 'blkio' controller, with cgroupv2 it's just 'io':
251 my ($path, $ver) = $self->get_any_path(1, 'io', 'blkio');
252 if (!defined($path)) {
253 # container not running
255 } elsif ($ver == 2) {
256 # cgroupv2 environment, io controller enabled
257 my $io_stat = file_get_contents
("$path/io.stat");
259 my $data = parse_nested_keyed_file
($io_stat);
260 foreach my $dev (keys %$data) {
261 my $dev = $data->{$dev};
262 if (my $b = $dev->{rbytes
}) {
263 $res->{diskread
} += $b;
265 if (my $b = $dev->{wbytes
}) {
266 $res->{diskread
} += $b;
271 } elsif ($ver == 1) {
272 # cgroupv1 environment:
273 my $io = file_get_contents
("$path/blkio.throttle.io_service_bytes_recursive");
274 foreach my $line (split(/\n/, $io)) {
275 if (my ($type, $bytes) = ($line =~ /^\d+:\d+\s+(Read|Write)\s+(\d+)$/)) {
276 $res->{diskread
} += $bytes if $type eq 'Read';
277 $res->{diskwrite
} += $bytes if $type eq 'Write';
283 die "bad cgroup version: $ver\n";
286 # container not running
290 # Read utime and stime for this container from the cpuacct cgroup.
291 # Values are in milliseconds!
300 my ($path, $ver) = $self->get_any_path(1, 'cpuacct', 'cpu');
301 if (!defined($path)) {
302 # container not running
304 } elsif ($ver == 2) {
305 my $data = eval { file_get_contents
("$path/cpu.stat") };
307 # or no io controller available:
308 return undef if !defined($data);
310 $data = parse_flat_keyed_file
($data);
311 $res->{utime} = int($data->{user_usec
} / 1000);
312 $res->{stime
} = int($data->{system_usec
} / 1000);
313 } elsif ($ver == 1) {
314 # cgroupv1 environment:
315 my $clock_ticks = POSIX
::sysconf
(&POSIX
::_SC_CLK_TCK
);
316 my $clk_to_usec = 1000 / $clock_ticks;
318 my $data = parse_flat_keyed_file
(file_get_contents
("$path/cpuacct.stat"));
319 $res->{utime} = int($data->{user
} * $clk_to_usec);
320 $res->{stime
} = int($data->{system} * $clk_to_usec);
322 die "bad cgroup version: $ver\n";
328 # Parse some memory data from `memory.stat`
329 sub get_memory_stat
{
337 my ($path, $ver) = $self->get_path('memory', 1);
338 if (!defined($path)) {
339 # container most likely isn't running
341 } elsif ($ver == 2) {
342 my $mem = file_get_contents
("$path/memory.current");
343 my $swap = file_get_contents
("$path/memory.swap.current");
344 my $stat = parse_flat_keyed_file
(file_get_contents
("$path/memory.stat"));
348 $res->{mem
} = $mem - $stat->{file
};
349 $res->{swap
} = $swap;
350 } elsif ($ver == 1) {
351 # cgroupv1 environment:
352 my $stat = parse_flat_keyed_file
(file_get_contents
("$path/memory.stat"));
353 my $mem = file_get_contents
("$path/memory.usage_in_bytes");
354 my $memsw = file_get_contents
("$path/memory.memsw.usage_in_bytes");
355 chomp ($mem, $memsw);
357 $res->{mem
} = $mem - $stat->{total_cache
};
358 $res->{swap
} = $memsw - $mem;
360 die "bad cgroup version: $ver\n";
366 sub get_pressure_stat
{
371 some
=> { avg10
=> 0, avg60
=> 0, avg300
=> 0 }
374 some
=> { avg10
=> 0, avg60
=> 0, avg300
=> 0 },
375 full
=> { avg10
=> 0, avg60
=> 0, avg300
=> 0 }
378 some
=> { avg10
=> 0, avg60
=> 0, avg300
=> 0 },
379 full
=> { avg10
=> 0, avg60
=> 0, avg300
=> 0 }
383 my ($path, $version) = $self->get_path(undef, 1);
384 if (!defined($path)) {
385 return $res; # container or VM most likely isn't running, retrun zero stats
386 } elsif ($version == 1) {
387 return undef; # v1 controller does not provides pressure stat
388 } elsif ($version == 2) {
389 for my $type (qw(cpu memory io)) {
390 my $stats = PVE
::ProcFSTools
::parse_pressure
("$path/$type.pressure");
391 $res->{$type} = $stats if $stats;
394 die "bad cgroup version: $version\n";
400 # Change the memory limit for this container.
402 # Dies on error (including a not-running or currently-shutting-down guest).
403 sub change_memory_limit
{
404 my ($self, $mem_bytes, $swap_bytes) = @_;
406 my ($path, $ver) = $self->get_path('memory', 1);
407 if (!defined($path)) {
408 die "trying to change memory cgroup values: container not running\n";
409 } elsif ($ver == 2) {
410 PVE
::ProcFSTools
::write_proc_entry
("$path/memory.swap.max", $swap_bytes)
411 if defined($swap_bytes);
412 PVE
::ProcFSTools
::write_proc_entry
("$path/memory.max", $mem_bytes)
413 if defined($mem_bytes);
414 } elsif ($ver == 1) {
415 # With cgroupv1 we cannot control memory and swap limits separately.
416 # This also means that since the two values aren't independent, we need to handle
417 # growing and shrinking separately.
418 my $path_mem = "$path/memory.limit_in_bytes";
419 my $path_memsw = "$path/memory.memsw.limit_in_bytes";
421 my $old_mem_bytes = file_get_contents
($path_mem);
422 my $old_memsw_bytes = file_get_contents
($path_memsw);
423 chomp($old_mem_bytes, $old_memsw_bytes);
425 $mem_bytes //= $old_mem_bytes;
426 $swap_bytes //= $old_memsw_bytes - $old_mem_bytes;
427 my $memsw_bytes = $mem_bytes + $swap_bytes;
429 if ($memsw_bytes > $old_memsw_bytes) {
430 # Growing the limit means growing the combined limit first, then pulling the
432 PVE
::ProcFSTools
::write_proc_entry
($path_memsw, $memsw_bytes);
433 PVE
::ProcFSTools
::write_proc_entry
($path_mem, $mem_bytes);
435 # Shrinking means we first need to shrink the mem-only memsw cannot be
437 PVE
::ProcFSTools
::write_proc_entry
($path_mem, $mem_bytes);
438 PVE
::ProcFSTools
::write_proc_entry
($path_memsw, $memsw_bytes);
441 die "bad cgroup version: $ver\n";
444 # return a truth value
448 # Change the cpu quota for a container.
450 # Dies on error (including a not-running or currently-shutting-down guest).
451 sub change_cpu_quota
{
452 my ($self, $quota, $period) = @_;
454 die "quota without period not allowed\n" if !defined($period) && defined($quota);
456 my ($path, $ver) = $self->get_path('cpu', 1);
457 if (!defined($path)) {
458 die "trying to change cpu quota cgroup values: container not running\n";
459 } elsif ($ver == 2) {
460 # cgroupv2 environment, an undefined (unlimited) quota is defined as "max"
462 $quota //= 'max'; # unlimited
463 if (defined($quota)) {
464 PVE
::ProcFSTools
::write_proc_entry
("$path/cpu.max", "$quota $period");
466 # we're allowed to only write the quota:
467 PVE
::ProcFSTools
::write_proc_entry
("$path/cpu.max", 'max');
469 } elsif ($ver == 1) {
470 $quota //= -1; # unlimited
472 PVE
::ProcFSTools
::write_proc_entry
("$path/cpu.cfs_period_us", $period);
473 PVE
::ProcFSTools
::write_proc_entry
("$path/cpu.cfs_quota_us", $quota);
475 die "bad cgroup version: $ver\n";
478 # return a truth value
482 # Change the cpu "shares" for a container.
484 # In cgroupv1 we used a value in `[0..500000]` with a default of 1024.
486 # In cgroupv2 we do not have "shares", we have "weights" in the range
487 # of `[1..10000]` with a default of 100.
489 # Since the default values don't match when scaling linearly, we use the
490 # values we get as-is and simply error for values >10000 in cgroupv2.
492 # It is left to the user to figure this out for now.
494 # Dies on error (including a not-running or currently-shutting-down guest).
495 sub change_cpu_shares
{
496 my ($self, $shares, $cgroupv1_default) = @_;
498 my ($path, $ver) = $self->get_path('cpu', 1);
499 if (!defined($path)) {
500 die "trying to change cpu shares/weight cgroup values: container not running\n";
501 } elsif ($ver == 2) {
502 # the cgroupv2 documentation defines the default to 100
504 die "cpu weight (shares) must be in range [1, 10000]\n" if $shares < 1 || $shares > 10000;
505 PVE
::ProcFSTools
::write_proc_entry
("$path/cpu.weight", $shares);
506 } elsif ($ver == 1) {
508 PVE
::ProcFSTools
::write_proc_entry
("$path/cpu.shares", $shares // $cgroupv1_default);
510 die "bad cgroup version: $ver\n";
513 # return a truth value
517 my sub v1_freeze_thaw
{
518 my ($self, $controller_path, $freeze) = @_;
519 my $path = $self->get_subdir('freezer', 1)
520 or die "trying to freeze container: container not running\n";
521 $path = "$controller_path/$path/freezer.state";
523 my $data = $freeze ?
'FROZEN' : 'THAWED';
524 PVE
::ProcFSTools
::write_proc_entry
($path, $data);
526 # Here we just poll the freezer.state once per second.
528 my $state = file_get_contents
($path);
530 last if $state eq $data;
534 my sub v2_freeze_thaw
{
535 my ($self, $controller_path, $freeze) = @_;
536 my $path = $self->get_subdir(undef, 1)
537 or die "trying to freeze container: container not running\n";
538 $path = "$controller_path/$path";
540 my $desired_state = $freeze ?
1 : 0;
542 # cgroupv2 supports poll events on cgroup.events which contains the frozen
544 my $fh = IO
::File-
>new("$path/cgroup.events", 'r')
545 or die "failed to open $path/cgroup.events file: $!\n";
546 my $select = IO
::Select-
>new();
549 PVE
::ProcFSTools
::write_proc_entry
("$path/cgroup.freeze", $desired_state);
555 $data = parse_flat_keyed_file
($data);
556 last if $data->{frozen
} == $desired_state;
557 my @handles = $select->has_exception();
560 or die "failed to rewind cgroup.events file: $!\n";
564 # Freeze or unfreeze a container.
566 # This will freeze the container at its outer (limiting) cgroup path. We use
567 # this instead of `lxc-freeze` as `lxc-freeze` from lxc4 will not be able to
568 # fetch the cgroup path from contaienrs still running on lxc3.
570 my ($self, $freeze) = @_;
572 my $controller_path = find_cgroup_controller
('freezer');
573 if (defined($controller_path)) {
574 return v1_freeze_thaw
($self, $controller_path, $freeze);
576 # cgroupv2 always has a freezer, there can be both cgv1 and cgv2
577 # freezers, but we'll prefer v1 when it's available as that's what lxc
579 return v2_freeze_thaw
($self, cgroupv2_base_path
(), $freeze);