Skip to content

Commit

Permalink
factorize cgroups detection (for proper jobs/load settings)
Browse files Browse the repository at this point in the history
  • Loading branch information
benoit-pierre committed Apr 7, 2024
1 parent 13b4e2e commit 4b03192
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 25 deletions.
22 changes: 2 additions & 20 deletions .ci/build_script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,6 @@ fi
mkdir -p "${CCACHE_DIR}"
echo "using cache dir: ${CCACHE_DIR}"

PROCS="$(getconf _NPROCESSORS_ONLN)"
if [[ -r /sys/fs/cgroup/cpu/cpu.shares ]]; then
CG_PROCS=$(($(cat /sys/fs/cgroup/cpu/cpu.shares) / 1024))
echo "running under cgroups, allocated vs physical cores: ${CG_PROCS} / ${PROCS}"
PARALLEL_JOBS=$((CG_PROCS + (CG_PROCS + 1) / 2))
PARALLEL_LOAD="${PROCS}"
else
PARALLEL_JOBS=$((PROCS + (PROCS + 1) / 2))
PARALLEL_LOAD="${PROCS}"
fi
echo "PARALLEL_JOBS: ${PARALLEL_JOBS}"
echo "PARALLEL_LOAD: ${PARALLEL_LOAD}"

travis_retry make fetchthirdparty TARGET=

docker-make() {
Expand All @@ -43,15 +30,10 @@ docker-make() {
/bin/bash -c "$(printf '%s && ' "${cmdlist[@]}")true"
}

makeargs=(
PARALLEL_JOBS="${PARALLEL_JOBS}"
PARALLEL_LOAD="${PARALLEL_LOAD}"
TARGET="${TARGET}"
)
if [[ -z "${DOCKER_IMG}" ]]; then
'./.ci/cache_restore_post.sh'
trap './.ci/cache_save_pre.sh' EXIT
make "${makeargs[@]}" "$@"
make TARGET="${TARGET}" "$@"
else
docker-make "${makeargs[@]}" VERBOSE=1 "$@"
docker-make TARGET="${TARGET}" VERBOSE=1 "$@"
fi
3 changes: 0 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,6 @@ jobs:
id: build
run: |
export MACOSX_DEPLOYMENT_TARGET=11;
PROCS="$(getconf _NPROCESSORS_ONLN)"
export PARALLEL_JOBS="$((PROCS + (PROCS + 1) / 2))"
export PARALLEL_LOAD="${PROCS}"
make fetchthirdparty && make
- name: Build cache pre-save
Expand Down
19 changes: 17 additions & 2 deletions Makefile.defs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,25 @@
SHELL = env PATH='$(PATH)' bash

ifeq (,$(PARALLEL_JOBS))
PARALLEL_JOBS := $(or $(patsubst -j%,%,$(filter -j%,$(MAKEFLAGS))),$(shell getconf _NPROCESSORS_ONLN))
PARALLEL_JOBS := $(or $(patsubst -j%,%,$(filter -j%,$(MAKEFLAGS))))
endif
ifeq (,$(PARALLEL_LOAD))
PARALLEL_LOAD := $(or $(patsubst -l%,%,$(filter -l%,$(MAKEFLAGS))),$(shell getconf _NPROCESSORS_ONLN))
PARALLEL_LOAD := $(or $(patsubst -l%,%,$(filter -l%,$(MAKEFLAGS))))
endif
ifeq (,$(and $(PARALLEL_JOBS),$(PARALLEL_LOAD)))
CORES := $(shell getconf _NPROCESSORS_ONLN)
ALLOCATED_CORES := $(CORES)
# Detect when running under cgroups, so we don't try
# to run with `make/ninja -j36 …` on some CI workers.
ifneq (,$(wildcard /sys/fs/cgroup/cpu/cpu.shares))
ALLOCATED_CORES := $(shell echo $$(($$(cat /sys/fs/cgroup/cpu/cpu.shares) / 1024)))
endif
ifeq (,$(PARALLEL_JOBS))
PARALLEL_JOBS := $(if $(CI),$(shell echo $$(($(ALLOCATED_CORES) + ($(ALLOCATED_CORES) + 1) / 2))),$(ALLOCATED_CORES))
endif
ifeq (,$(PARALLEL_LOAD))
PARALLEL_LOAD := $(CORES)
endif
endif
export PARALLEL_JOBS
export PARALLEL_LOAD
Expand Down

0 comments on commit 4b03192

Please sign in to comment.