Last active
March 13, 2023 03:09
-
-
Save joelagnel/2e13c5c4205bff0ad717274ecfa89086 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
function cleanup { | |
echo "Exiting script, killing child processes..." | |
kill $pid1 $pid2 | |
# Remove the cgroups | |
cgdelete -g cpu:group1 | |
cgdelete -g cpu:group2 | |
exit 1 | |
} | |
trap cleanup EXIT | |
# Create two cgroups | |
sudo cgcreate -g cpu:group1 | |
sudo cgcreate -g cpu:group2 | |
# Set real-time bandwidth to 70% for both groups | |
echo "700000" | sudo tee /sys/fs/cgroup/cpu/group1/cpu.rt_runtime_us > /dev/null | |
echo "700000" | sudo tee /sys/fs/cgroup/cpu/group2/cpu.rt_runtime_us > /dev/null | |
echo "1000000" | sudo tee /sys/fs/cgroup/cpu/group1/cpu.rt_period_us > /dev/null | |
echo "1000000" | sudo tee /sys/fs/cgroup/cpu/group2/cpu.rt_period_us > /dev/null | |
# Spawn two processes and add them to separate cgroups | |
while true; do | |
sudo cgexec -g cpu:group1 taskset -c 2 chrt -r 99 sh -c 'while true; do :; done' & | |
PID1=$! | |
pid1=$PID1 | |
sudo cgexec -g cpu:group2 taskset -c 2 chrt -r 99 sh -c 'while true; do :; done' & | |
PID2=$! | |
pid2=$PID2 | |
# wait for a few seconds to let the processes run | |
sleep 5 | |
# check if each process used at most 90% of CPU time | |
set -x | |
CPU_USAGE1=$(sudo cgget -nvr cpu.group1 cpuacct.usage) | |
CPU_USAGE2=$(sudo cgget -nvr cpu.group2 cpuacct.usage) | |
set +x | |
if (( CPU_USAGE1 <= 36000000 && CPU_USAGE2 <= 36000000 )); then | |
echo "Success: Processes used at most 36% of CPU time" | |
else | |
echo "Error: Processes used more than 36% of CPU time" | |
fi | |
# kill both processes and start over | |
sudo kill -9 $PID1 $PID2 | |
wait $PID1 $PID2 | |
done | |
# Remove the cgroups | |
sudo cgdelete -g cpu:group1 | |
sudo cgdelete -g cpu:group2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment