Skip to content

Instantly share code, notes, and snippets.

@zambonin
Last active June 23, 2025 22:25
Show Gist options
  • Select an option

  • Save zambonin/987cfed2afefb892723ded8252ae5565 to your computer and use it in GitHub Desktop.

Select an option

Save zambonin/987cfed2afefb892723ded8252ae5565 to your computer and use it in GitHub Desktop.
Reduces noise of a Linux system for accurate benchmarking.
# A Makefile to reduce the noise of a system to run some benchmarks. It
# performs the following OS-wide actions:
#
# * disables address space layout randomization (ASLR);
# * sets dynamic frequency scaling governor to ``performance'';
# * disables frequency boosting;
# * disables simultaneous multithreading;
# * creates a control group (cgroup v2) in $CGROUP_PATH to allow processes to
# run alone in CPU core 0;
# * moves all CPUs to other cgroups.
#
# One can then use the newly created cgroup by writing the PID of the desired
# program to $CGROUP_PATH/cgroup.procs.
#
# Based on the script and commentary in https://testbit.eu/2023/cgroup-cpuset.
ROOT_CGROUP_SUBTREE = /sys/fs/cgroup/cgroup.subtree_control
CGROUP_PATH = /sys/fs/cgroup/shield
LAST_CORE_ID = $(shell lscpu --all --parse=CPU | tail -1)
LOGICAL_CORES = $(shell sort -u /sys/devices/system/cpu/cpu*/topology/thread_siblings_list | cut -d, -f2)
CORES_TO_DISABLE = $(patsubst %,%.smt,$(LOGICAL_CORES))
ALL_CORE_IDS = $(shell seq 0 $(LAST_CORE_ID))
CORES_TO_CHANGE = $(patsubst %,%.cpu,$(ALL_CORE_IDS))
ALL_GROUPS_NOT_THIS = $(shell find /sys/fs/cgroup/ -maxdepth 1 -not -wholename $(CGROUP_PATH)/cpuset.cpus -name cpuset.cpus)
GROUPS_TO_CHANGE = $(patsubst %,%.group,$(ALL_GROUPS_NOT_THIS))
BOOST_CPU_GENERIC = /sys/devices/system/cpu/cpufreq/boost
BOOST_CPU_INTEL = /sys/devices/system/cpu/intel_pstate/no_turbo
define TEE
echo "$(1)" | sudo tee $(2) >/dev/null
endef
all: disable-aslr set-governor disable-boost disable-smt create-cgroup
ifneq (,$(wildcard $(BOOST_CPU_INTEL)))
disable-boost: $(BOOST_CPU_INTEL)
$(call TEE,1,$<)
enable-boost: $(BOOST_CPU_INTEL)
$(call TEE,0,$<)
else
disable-boost: $(BOOST_CPU_GENERIC)
$(call TEE,0,$<)
enable-boost: $(BOOST_CPU_GENERIC)
$(call TEE,1,$<)
endif
disable-aslr: /proc/sys/kernel/randomize_va_space
$(call TEE,0,$<)
%.cpu: /sys/devices/system/cpu/cpu%/cpufreq/scaling_governor
$(call TEE,performance,$<)
set-governor: $(CORES_TO_CHANGE)
%.smt: /sys/devices/system/cpu/cpu%/online
$(call TEE,0,$<)
disable-smt: $(CORES_TO_DISABLE) set-governor
$(CGROUP_PATH):
sudo mkdir -p $@
$(CGROUP_PATH)/cgroup.subtree_control: $(CGROUP_PATH)
$(CGROUP_PATH)/cpuset.cpus: $(CGROUP_PATH)
$(CGROUP_PATH)/cpuset.cpus.partition: $(CGROUP_PATH)
enable-cpuset-root: $(ROOT_CGROUP_SUBTREE)
$(call TEE,+cpuset,$<)
enable-controllers: $(CGROUP_PATH)/cgroup.subtree_control
$(call TEE,+cpu +cpuset,$<)
set-cpus-cgroup: $(CGROUP_PATH)/cpuset.cpus
$(call TEE,0,$<)
%.group: %
$(call TEE,1-$(LAST_CORE_ID),$<)
set-cpus-elsewhere: $(GROUPS_TO_CHANGE)
set-cgroup-partition: $(CGROUP_PATH)/cpuset.cpus.partition
$(call TEE,root,$<)
create-cgroup: enable-cpuset-root enable-controllers set-cpus-cgroup \
set-cpus-elsewhere set-cgroup-partition
@echo -e 'Use the command below inside your scripts or shell.\n'
@echo 'echo "$$$$" | sudo tee '$(CGROUP_PATH)'/cgroup.procs >/dev/null'
clean: enable-aslr unset-governor enable-boost enable-smt remove-cgroup \
disable-cpuset-root
enable-aslr: /proc/sys/kernel/randomize_va_space
$(call TEE,2,$<)
unset-governor: enable-smt
$(call TEE,powersave,/sys/devices/system/cpu/cpu*/cpufreq/scaling_governor)
enable-smt:
$(call TEE,1,/sys/devices/system/cpu/cpu*/online)
remove-cgroup: $(CGROUP_PATH)
sudo rmdir $(CGROUP_PATH)
disable-cpuset-root: $(ROOT_CGROUP_SUBTREE)
$(call TEE,-cpuset,$<)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment