Linux Notes: CPU Control
- The information presented here is intended for educational use by qualified computer technologists.
- The information presented here is provided free of charge, as-is, with no warranty of any kind.
Edit: 2022-01-12 (updated the script)
BASH script to control CPU cores
Notes:
- this is an unfinished script (the NUMA stuff was never implemented)
- I used this on a 2-socket HPE DL385p_gen8 on June-2021 to disable all the cores associated with the second CPU
- this saved me from having to drive 160 km to swap out a box and/or unplug one CPU chip
- also, I did this on a running system so there is the convenience of not having to shutdown etc.
- this script should work on a 2-socket DL380p (but never tested by me)
- this script should work on a 4-socket DL580 or DL585 (but never tested by me)
- caveats:
- this script begins with three safety parameters (SOCKETS, CORES, THREADS). These must be changed for any platform
other than the one I wrote this for so BE VERY CAREFUL.
- If you do not know what you are doing then please call your resident computer technologist.
- changes made by this script are not permanent (so it you screw up then just power off followed by a
reboot)
#!/bin/sh
# ====================================================
# file : neil_cpu_control.sh
# author : Neil Rieck
# edit : 2021-06-18
# edit : 2021-06-19
# edit : 2022-01-12
# notes : USE AT YOUR OWN RISK!
# hardware:
# 1) display hardware:
# lscpu | grep 'Socket(s)'
# lscpu | grep 'Core(s)'
# lscpu | grep 'Thread(s)'
# lscpu -e (or lscpu -p)
# lscpu -y (default)
# 2) examples for a typical 2-socket DL385p
# a) display CPUs
# ls -la /sys/devices/system/cpu
# b) displays the current CPU configuration
# lscpu
# yields:
# NUMA node0 CPU(s): 0,2,4,6,8,10
# NUMA node1 CPU(s): 12,14,16,18,20,22
# NUMA node2 CPU(s): 1,3,5,7,9,11
# NUMA node3 CPU(s): 13,15,17,19,21,23
# c) after running this tool
# yields
# NUMA node0 CPU(s): 0,2,4,6,8,10
# NUMA node1 CPU(s): 12,14,16,18,20,22
# NUMA node2 CPU(s):
# NUMA node3 CPU(s):
# ====================================================
set -e # stop on error
SOCKETS=2 # number of CPU chips
CORES=6 # cores per chip
THREADS=2 # threads per core
MAXPR=$((SOCKETS * CORES * THREADS)) # max processors
HELP1=" example: "$0" noun unit verb optional-safeword"
HELP2=" example: "$0" socket 2 enable"
HELP=${HELP1}$'\n'${HELP2}
PASS="ncc1701d"
# ====================================================
echo ""
echo "pgm: "$0
echo "note: the first chip is in socket #1"
echo "===================================="
echo "-i-hardware:"
lscpu | grep 'Socket(s)'
lscpu | grep 'Core(s)'
lscpu | grep 'Thread(s)'
echo "-i-script constants:"
echo " sockets : "${SOCKETS}
echo " cores-per-socket: "${CORES}
echo " max-processors : "${MAXPR}
# ====================================================
if [ "$1" == "" ]
then
NOUN="blank"
else
NOUN=$1
fi
if [ "$2" == "" ]
then
UNIT=-1
else
UNIT=$2
fi
if [ "$3" == "" ]
then
VERB=-1
else
VERB=$3
fi
if [ "$4" == "" ]
then
SAFEWORD="test"
else
SAFEWORD=$4
fi
# ====================================================
if [ $SOCKETS != 2 ]; then
echo "-e-only for use on 2-socket systems"
exit -1
fi
if [[ ( $NOUN != "socket" ) && ( $NOUN != "numa" ) ]]; then
echo "-e-bad NOUN (not one of: socket, numa)"
printf "%s\n\n" "${HELP}"
exit -1
fi
if [[ (( $UNIT -le 0 )) || (( $UNIT -gt $SOCKETS )) ]]; then
echo "-e-bad UNIT (not legal or not in range)"
printf "%s\n\n" "${HELP}"
exit -1
fi
if [[ ( $VERB != "enable" ) && ( $VERB != "disable" ) ]]; then
echo "-e-bad VERB. (not one of: enable, disable)"
printf "%s\n\n" "${HELP}"
exit -1
fi
if [[ ( $SAFEWORD != $PASS ) && ( $SAFEWORD != "test" ) ]]; then
echo "-e-bad SAFEWORD. (defaults to test mode)";
printf "%s\n\n" "${HELP}"
exit -1
fi
#======================================
# doit()
# entry: i = CPU#
# NOUN
# VERB
#======================================
function doit() {
if [[ $VERB = "disable" ]]; then
STATE="0"
else
STATE="1"
fi
# we always skip processor-0 just in case
if [[ ${i} = 0 ]]; then
echo "skip" ${STATE} "> /sys/devices/system/cpu/cpu"${i}"/online skip"
return 0
fi
if [[ $SAFEWORD = $PASS ]]; then
echo "writing" $STATE "> /sys/devices/system/cpu/cpu"${i}"/online"
echo ${STATE} > /sys/devices/system/cpu/cpu${i}/online
else
echo "script-test" ${STATE} "> /sys/devices/system/cpu/cpu"${i}"/online"
fi
return 0
}
echo "-i-processing begins:"
case $NOUN in
"socket" )
start=$((UNIT-1))
stop=$((MAXPR))
step=$((SOCKETS))
echo "start: "${start}" stop: "${stop}" step: "${step}
for (( i=${start}; i<${stop}; i=i+${step} )); do
doit;
done
;;
"numa")
echo "-w-not implemented"
esac
#
echo "done"
exit 0

Back to
Home
Neil Rieck
Waterloo, Ontario, Canada.