From a035c86d0ea06a64a1b2628563a83d2f679d3848 Mon Sep 17 00:00:00 2001 From: SuperQ Date: Mon, 9 Sep 2024 05:13:26 +0200 Subject: [PATCH] Automatically set GOMEMLIMIT In setup_environment, set the `GOMEMLIMIT` env var to help Go builds, tests, and processes from OOMing the environment. Signed-off-by: SuperQ --- actions/setup_environment/action.yml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/actions/setup_environment/action.yml b/actions/setup_environment/action.yml index d9e7b99..7ca8a53 100644 --- a/actions/setup_environment/action.yml +++ b/actions/setup_environment/action.yml @@ -12,6 +12,10 @@ inputs: type: boolean description: Whether to enable multibuild docker default: false + memlimit_ratio: + type: string + description: The ratio of memory reserved for Go + default: "0.8" runs: using: composite steps: @@ -32,6 +36,18 @@ runs: key: ${{ runner.os }}-npm-${{ hashFiles('web/ui/package-lock.json') }} restore-keys: | ${{ runner.os }}-npm- + - name: Set GOMEMLIMIT + run: | + ratio="${{ inputs.memlimit_ratio }}" + # Get the memory limit from cgroups v2. + cgroup=$(awk -F':' '{print $3}' /proc/self/cgroup) + cgroup_mem_limit=$(< "/sys/fs/cgroup/${cgroup}/memory.max") + if [[ "${cgroup_mem_limit}" != "max" ]] ; then + echo "${cgroup_mem_limit}" | awk -v "ratio=${ratio}" '{printf "GOMEMLIMIT=%.0fKiB\n", $1 / 1024 * ratio}' >> "$GITHUB_ENV" + exit 0 + fi + # Fallback to the system memory limit. + awk -v "ratio=${ratio}" '$1 == "MemTotal:" {printf "GOMEMLIMIT=%.0fKiB\n", $2 * ratio}' /proc/meminfo >> "$GITHUB_ENV" - run: make promu shell: bash if: inputs.enable_go == 'true'