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

Add cgroupsv2 support #760

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions support/build/php
Original file line number Diff line number Diff line change
Expand Up @@ -188,22 +188,43 @@ EOF
cat > ${OUT_PREFIX}/bin/profile.php.sh <<'EOF'
export PATH="$HOME/.heroku/php/bin:$HOME/.heroku/php/sbin:$PATH"

# read memory limit of dyno
mlib="/sys/fs/cgroup/memory/memory.limit_in_bytes"
# Check for memory limit in cgroupsv1 or cgroupsv2
# cgroupsv1 memory limit file
mlib_v1="/sys/fs/cgroup/memory/memory.limit_in_bytes"
# cgroupsv2 memory limit file
mlib_v2="/sys/fs/cgroup/memory.max"

# get php.ini location; don't forget to suppress INI scan dir to prevent e.g. New Relic from starting
php_ini_path=$(PHP_INI_SCAN_DIR= php -r 'echo get_cfg_var("cfg_file_path");')
if [[ -f "$mlib" && -n "$php_ini_path" ]]; then

# Check if php.ini path exists and cgroups memory limit file is present
if [[ -n "$php_ini_path" ]]; then
php_cli_ini_path=$(dirname "$php_ini_path")/php-cli.ini
# create php-cli.ini from php.ini unless it exists
# we can't cp -n instead because php_ini_path would already be php-cli.ini and that would error
if [[ "$php_ini_path" != "$php_cli_ini_path" ]]; then
cp "$php_ini_path" "$php_cli_ini_path"
fi
# compute memory limit, up to 16 GB

# Default maximum memory limit of 16 GB
max_memory_limit=$(( 16 * 1024 * 1024 * 1024 ))
sys_memory_limit=$(cat "$mlib")

# Determine which cgroup version to use for memory limit
if [[ -f "$mlib_v1" ]]; then
# cgroupsv1: read memory limit from memory.limit_in_bytes
sys_memory_limit=$(cat "$mlib_v1")
elif [[ -f "$mlib_v2" && "$(cat "$mlib_v2")" != "max" ]]; then
# cgroupsv2: read memory limit from memory.max, skip if "max" (no limit)
sys_memory_limit=$(cat "$mlib_v2")
else
# Fallback if no memory limit is found
sys_memory_limit=$max_memory_limit
fi

# Ensure memory limit doesn't exceed 16 GB
memory_limit=$(( sys_memory_limit > max_memory_limit ? max_memory_limit : sys_memory_limit ))
# append to php-cli.ini

# Append memory limit to php-cli.ini
echo $'\n;inserted by ~/.profile.d/ script' >> "$php_cli_ini_path"
echo "memory_limit=$memory_limit" >> "$php_cli_ini_path"
fi
Expand Down
Loading