Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support cgroups v1 and v2 for memory and CPU limits #732

Draft
wants to merge 21 commits into
base: main
Choose a base branch
from
Draft

Conversation

dzuelke
Copy link
Contributor

@dzuelke dzuelke commented Jun 20, 2024

The boot scripts now try to find group membership, controller, and mount info from procfs, then read a suitable value:

  • memory.limit_in_bytes for v1
  • memory.high for v2 (the recommended way of setting limits)
    • if that does not exist, read memory.max (e.g. Docker's -m)
      • if that does not exist, read memory.low (e.g. Docker's --memory-reservation)
        • if that does not exist, read memory.min

The routines that handle this are in bin/util/cgroups.sh. They guard against silly values (such as Docker's memory.limit_in_bytes at 9223372036854771712 on unlimited containers, that would be about 8 Exbibytes) and have options for overriding procfs and cgroupfs locations for testing, as well as a verbose mode. Primary "API" is the cgroup_util_read_cgroup_memory_limit function.

If nothing is found, the boot scripts try falling back to plainly reading /sys/fs/cgroup/memory/memory.limit_in_bytes directly, as before, using the cgroup_util_read_cgroup_memory_limit_with_fallback function, which wraps cgroup_util_read_cgroup_memory_limit.

Tests cover set -e and set -u environments, as well as regular and verbose mode of the functions, in all possible permutations.

Closes #699

GUS-W-16050052
GUS-W-16050059


Also now uses nproc to get the number of cores, falling back to getconf for e.g. BSD systems (the boot scripts can be used on their own).


For easier review, the test fixtures are in a separate commit.

@dzuelke dzuelke requested a review from a team as a code owner June 20, 2024 02:33
bin/heroku-php-apache2 Outdated Show resolved Hide resolved
@dzuelke dzuelke force-pushed the cgroupsv2 branch 4 times, most recently from 6a71104 to 638bc92 Compare June 25, 2024 16:05
Covers Docker in various permutations, as well as Heroku's past, present, and possible future behavior on CR and PS.
Main convenience function to use is 'cgroup_util_read_cgroup_memory_limit'.

It will determine the presence of v1 or v2 controllers, traverse the hierarchy upwards until the mount root for v1, and, for v2, prefer memory.high over memory.max, by default falling back to memory.low.

It will reject read limits that exceed a given maximum value, or 'MemFree' from 'free' as an alternative.

For testing purposes, a location other than '/proc' can be provided for determining base cgroup mount, controller, and process membership information; similarly, a prefix can be supplied that will be prepended to any discovered cgroup hierarchy paths.

GUS-W-16050052
Not as part of integration tests, but against Ubuntu 20/22/24 to cover any potential differences in used programs (e.g. 'free' only got '-L' fairly recently) or Bash behavior
It applies cgroup cpuset limits, but it's GNU coreutils only, so we have to fall back to getconf.

Also ensure pipefail mode in the command substitution to catch all error cases.
…r cgroupsv2

Drop the -L option, it's better to have one good default behavior.
…/mountinfo as input to have a _contents suffix for clarity
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

cgroupsv2 support
2 participants