]>
git.proxmox.com Git - pve-common.git/blob - src/PVE/CGroup.pm
45b9e7c12a0b7a426b09851e64caf5d5d97dd987
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");
347 # FIXME: For the cgv1 equivalent of `total_cache` we may need to sum up
348 # the values in `memory.stat`...
351 $res->{swap
} = $swap;
352 } elsif ($ver == 1) {
353 # cgroupv1 environment:
354 my $stat = parse_flat_keyed_file
(file_get_contents
("$path/memory.stat"));
355 my $mem = file_get_contents
("$path/memory.usage_in_bytes");
356 my $memsw = file_get_contents
("$path/memory.memsw.usage_in_bytes");
357 chomp ($mem, $memsw);
359 $res->{mem
} = $mem - $stat->{total_cache
};
360 $res->{swap
} = $memsw - $mem;
362 die "bad cgroup version: $ver\n";
368 # Change the memory limit for this container.
370 # Dies on error (including a not-running or currently-shutting-down guest).
371 sub change_memory_limit
{
372 my ($self, $mem_bytes, $swap_bytes) = @_;
374 my ($path, $ver) = $self->get_path('memory', 1);
375 if (!defined($path)) {
376 die "trying to change memory cgroup values: container not running\n";
377 } elsif ($ver == 2) {
378 PVE
::ProcFSTools
::write_proc_entry
("$path/memory.swap.max", $swap_bytes)
379 if defined($swap_bytes);
380 PVE
::ProcFSTools
::write_proc_entry
("$path/memory.max", $mem_bytes)
381 if defined($mem_bytes);
382 } elsif ($ver == 1) {
383 # With cgroupv1 we cannot control memory and swap limits separately.
384 # This also means that since the two values aren't independent, we need to handle
385 # growing and shrinking separately.
386 my $path_mem = "$path/memory.limit_in_bytes";
387 my $path_memsw = "$path/memory.memsw.limit_in_bytes";
389 my $old_mem_bytes = file_get_contents
($path_mem);
390 my $old_memsw_bytes = file_get_contents
($path_memsw);
391 chomp($old_mem_bytes, $old_memsw_bytes);
393 $mem_bytes //= $old_mem_bytes;
394 $swap_bytes //= $old_memsw_bytes - $old_mem_bytes;
395 my $memsw_bytes = $mem_bytes + $swap_bytes;
397 if ($memsw_bytes > $old_memsw_bytes) {
398 # Growing the limit means growing the combined limit first, then pulling the
400 PVE
::ProcFSTools
::write_proc_entry
($path_memsw, $memsw_bytes);
401 PVE
::ProcFSTools
::write_proc_entry
($path_mem, $mem_bytes);
403 # Shrinking means we first need to shrink the mem-only memsw cannot be
405 PVE
::ProcFSTools
::write_proc_entry
($path_mem, $mem_bytes);
406 PVE
::ProcFSTools
::write_proc_entry
($path_memsw, $memsw_bytes);
409 die "bad cgroup version: $ver\n";
412 # return a truth value
416 # Change the cpu quota for a container.
418 # Dies on error (including a not-running or currently-shutting-down guest).
419 sub change_cpu_quota
{
420 my ($self, $quota, $period) = @_;
422 die "quota without period not allowed\n" if !defined($period) && defined($quota);
424 my ($path, $ver) = $self->get_path('cpu', 1);
425 if (!defined($path)) {
426 die "trying to change cpu quota cgroup values: container not running\n";
427 } elsif ($ver == 2) {
428 # cgroupv2 environment, an undefined (unlimited) quota is defined as "max"
430 $quota //= 'max'; # unlimited
431 if (defined($quota)) {
432 PVE
::ProcFSTools
::write_proc_entry
("$path/cpu.max", "$quota $period");
434 # we're allowed to only write the quota:
435 PVE
::ProcFSTools
::write_proc_entry
("$path/cpu.max", 'max');
437 } elsif ($ver == 1) {
438 $quota //= -1; # unlimited
440 PVE
::ProcFSTools
::write_proc_entry
("$path/cpu.cfs_period_us", $period);
441 PVE
::ProcFSTools
::write_proc_entry
("$path/cpu.cfs_quota_us", $quota);
443 die "bad cgroup version: $ver\n";
446 # return a truth value
450 # Change the cpu "shares" for a container.
452 # In cgroupv1 we used a value in `[0..500000]` with a default of 1024.
454 # In cgroupv2 we do not have "shares", we have "weights" in the range
455 # of `[1..10000]` with a default of 100.
457 # Since the default values don't match when scaling linearly, we use the
458 # values we get as-is and simply error for values >10000 in cgroupv2.
460 # It is left to the user to figure this out for now.
462 # Dies on error (including a not-running or currently-shutting-down guest).
463 sub change_cpu_shares
{
464 my ($self, $shares, $cgroupv1_default) = @_;
466 my ($path, $ver) = $self->get_path('cpu', 1);
467 if (!defined($path)) {
468 die "trying to change cpu shares/weight cgroup values: container not running\n";
469 } elsif ($ver == 2) {
470 # the cgroupv2 documentation defines the default to 100
472 die "cpu weight (shares) must be in range [1, 10000]\n" if $shares < 1 || $shares > 10000;
473 PVE
::ProcFSTools
::write_proc_entry
("$path/cpu.weight", $shares);
474 } elsif ($ver == 1) {
476 PVE
::ProcFSTools
::write_proc_entry
("$path/cpu.shares", $shares // $cgroupv1_default);
478 die "bad cgroup version: $ver\n";
481 # return a truth value
485 my sub v1_freeze_thaw
{
486 my ($self, $controller_path, $freeze) = @_;
487 my $path = $self->get_subdir('freezer', 1)
488 or die "trying to freeze container: container not running\n";
489 $path = "$controller_path/$path/freezer.state";
491 my $data = $freeze ?
'FROZEN' : 'THAWED';
492 PVE
::ProcFSTools
::write_proc_entry
($path, $data);
494 # Here we just poll the freezer.state once per second.
496 my $state = file_get_contents
($path);
498 last if $state eq $data;
502 my sub v2_freeze_thaw
{
503 my ($self, $controller_path, $freeze) = @_;
504 my $path = $self->get_subdir(undef, 1)
505 or die "trying to freeze container: container not running\n";
506 $path = "$controller_path/$path";
508 my $desired_state = $freeze ?
1 : 0;
510 # cgroupv2 supports poll events on cgroup.events which contains the frozen
512 my $fh = IO
::File-
>new("$path/cgroup.events", 'r')
513 or die "failed to open $path/cgroup.events file: $!\n";
514 my $select = IO
::Select-
>new();
517 PVE
::ProcFSTools
::write_proc_entry
("$path/cgroup.freeze", $desired_state);
523 $data = parse_flat_keyed_file
($data);
524 last if $data->{frozen
} == $desired_state;
525 my @handles = $select->has_exception();
528 or die "failed to rewind cgroup.events file: $!\n";
532 # Freeze or unfreeze a container.
534 # This will freeze the container at its outer (limiting) cgroup path. We use
535 # this instead of `lxc-freeze` as `lxc-freeze` from lxc4 will not be able to
536 # fetch the cgroup path from contaienrs still running on lxc3.
538 my ($self, $freeze) = @_;
540 my $controller_path = find_cgroup_controller
('freezer');
541 if (defined($controller_path)) {
542 return v1_freeze_thaw
($self, $controller_path, $freeze);
544 # cgroupv2 always has a freezer, there can be both cgv1 and cgv2
545 # freezers, but we'll prefer v1 when it's available as that's what lxc
547 return v2_freeze_thaw
($self, cgroupv2_base_path
(), $freeze);