From 898f594a944269e121bc64cf77abad367674591e Mon Sep 17 00:00:00 2001 From: Jonathan Siegel <248302+usiegj00@users.noreply.github.com> Date: Tue, 15 Oct 2024 07:12:52 +0900 Subject: [PATCH] Add cgroupsv2 support --- support/build/php | 33 +++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/support/build/php b/support/build/php index 190150525..b85acefe6 100755 --- a/support/build/php +++ b/support/build/php @@ -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