Skip to content

Instantly share code, notes, and snippets.

@aerosol
Created December 22, 2025 07:57
Show Gist options
  • Select an option

  • Save aerosol/2e725bc2739e03786ba9808a9c3b6667 to your computer and use it in GitHub Desktop.

Select an option

Save aerosol/2e725bc2739e03786ba9808a9c3b6667 to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# slowci.sh
# Usage: ./slowci.sh <percentage> <cores> -- command [args...]
# Example: ./slowci.sh 20 4 -- make test
set -e
if [[ $# -lt 4 || "$3" != "--" ]]; then
echo "Usage: $0 <percentage> <cores> -- command [args...]"
exit 1
fi
PERCENT=$1
NUM_CORES=$2
shift 3
CMD=("$@")
# Validate percentage
if ! [[ $PERCENT =~ ^[0-9]+$ ]] || [ "$PERCENT" -le 0 ] || [ "$PERCENT" -gt 100 ]; then
echo "Error: percentage must be 1-100"
exit 1
fi
# Validate cores
if ! [[ $NUM_CORES =~ ^[0-9]+$ ]] || [ "$NUM_CORES" -le 0 ]; then
echo "Error: cores must be >0"
exit 1
fi
CGROUP="/sys/fs/cgroup/slowci_tmp_$$"
sudo mkdir -p "$CGROUP"
# Calculate cpu.max values
PERIOD=100000 # default period in µs
MAX=$((NUM_CORES * PERIOD * PERCENT / 100))
echo "$MAX $((NUM_CORES * PERIOD))" | sudo tee "$CGROUP/cpu.max" >/dev/null
# Add this shell to the cgroup
echo $$ | sudo tee "$CGROUP/cgroup.procs" >/dev/null
# Pin to cores using taskset
CORE_LIST=$(seq -s, 0 $((NUM_CORES-1)))
taskset -c "$CORE_LIST" "${CMD[@]}"
# Cleanup cgroup
sudo rmdir "$CGROUP"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment