Skip to content

Commit

Permalink
#31 Rewrite cleanup_crash.sh to work more reliably and match new cgro…
Browse files Browse the repository at this point in the history
…up naming scheme

Sidenote: Wow, do people actually willingly write scripts in bash? Is that a case of Stockholm syndrome?

closes #31
  • Loading branch information
MatejKafka committed Apr 26, 2021
1 parent b0e6dc2 commit 1fd12c3
Showing 1 changed file with 62 additions and 41 deletions.
103 changes: 62 additions & 41 deletions src/cleanup_crash.sh
Original file line number Diff line number Diff line change
@@ -1,53 +1,74 @@
#!/bin/bash

#if [ -z "$1" ] then
# CG_NAME="demos"
#else
# CG_NAME="$1"
#fi

FREEZER="/sys/fs/cgroup/freezer/demos"
CPUSET="/sys/fs/cgroup/cpuset/demos"
UNIFIED="/sys/fs/cgroup/unified/user.slice/user-1000.slice/demos"

# kill all procs in freezer
procs_files=($(find "$FREEZER" -name *.procs))

for i in "${procs_files[@]}"
do
while read line
do
if [ $line != $$ ]
then
kill $line
fi
done < "$i"
done
#!/usr/bin/env bash
set -Eeuo pipefail
shopt -s extglob nullglob globstar

# NOTE: hybrid cgroup mode is assumed here;
# after switch to unified cgroups, this will need to be updated

if [[ $EUID > 0 ]]; then
>&2 echo "Please run this script as root"
exit 1
fi

# unfreeze all freezers
freezers=($(find "$FREEZER" -name *freezer.state))

for i in "${freezers[@]}"
do
#echo "$i"
echo "THAWED" > "$i"
# stop all running demos instances
# found using the cgroup name, which includes scheduler PID
freezer=(/sys/fs/cgroup/freezer/demos-+([0-9]))
for pid in "${freezer[@]##*-}"; do
if [[ ! -d "/proc/$pid" ]]; then continue; fi
echo "STOPPING DEMOS INSTANCE: $pid"
kill -INT "$pid"
done
# give time to demos instances to exit; ideally, this should be a `wait` with timeout
# (in case the scheduler process is unresponsive)
sleep 0.1


# remove all cgroups
d="/*"
rmdir `ls -d $FREEZER$d$d/ 2>/dev/null ` 2>/dev/null
rmdir `ls -d $FREEZER$d/ 2>/dev/null ` 2>/dev/null
rmdir `ls -d $CPUSET$d$d/ 2>/dev/null ` 2>/dev/null
rmdir `ls -d $CPUSET$d/ 2>/dev/null ` 2>/dev/null
rmdir `ls -d $UNIFIED$d$d/ 2>/dev/null ` 2>/dev/null
rmdir `ls -d $UNIFIED$d/ 2>/dev/null ` 2>/dev/null
# find all demos cgroups (demos-<scheduler_pid>)
freezer=(/sys/fs/cgroup/freezer/demos-+([0-9]))
cpuset=(/sys/fs/cgroup/cpuset/demos-+([0-9]))
unified=(
/sys/fs/cgroup/unified/demos-+([0-9])
/sys/fs/cgroup/unified/user.slice/user-+([0-9]).slice/*/demos-+([0-9])
/sys/fs/cgroup/unified/user.slice/user-+([0-9]).slice/*/run-*/demos-+([0-9])
)
echo "FOUND FREEZER:" "${freezer[@]}"
echo "FOUND CPUSET:" "${cpuset[@]}"
echo "FOUND UNIFIED:" "${unified[@]}"

if [[ ${#freezer[@]} -gt 0 ]]; then
# https://askubuntu.com/questions/343727/filenames-with-spaces-breaking-for-loop-find-command
# kill all processes in demos freezers
find "${freezer[@]}" -name 'cgroup.procs' -print0 | while IFS= read -r -d '' proc_file; do
while IFS= read -r line; do
echo "KILLING PROCESS: $line"
kill $line
done <"$proc_file"
done

# unfreeze all demos freezers - this allows the processes to end
find "${freezer[@]}" -name 'freezer.state' -print0 | while IFS= read -r -d '' state_file; do
echo "THAWED" >"$state_file"
done
fi

# remove all demos cgroups
for cgdir in "${freezer[@]}" "${cpuset[@]}" "${unified[@]}"; do
# first, delete grandchildren (proc cgroups), then delete children (partition cgroups), then whole cgroup
echo "REMOVING CGROUP: $cgdir"
# `|| true`, because under systemd-run, cgroup root seems to be automatically deleted
# with last child and `find` then cannot delete it and complains
find "$cgdir" -maxdepth 2 -type d -delete || true
done


# reset CPU frequency scaling state
if [[ -f "/sys/devices/system/cpu/intel_pstate/status" ]]; then
# for Intel, it is enough to reset driver to active mode
echo "active" > "/sys/devices/system/cpu/intel_pstate/status"
echo "RESETTING intel_pstate DRIVER TO 'active' STATE"
echo "active" >"/sys/devices/system/cpu/intel_pstate/status"
elif [[ -d "/sys/devices/system/cpu/cpufreq" ]]; then
echo "RESETTING CPUFREQ GOVERNORS TO 'schedutil'"
# for others, switch all policies to schedutil (and hope that was the default)
echo "schedutil" | tee /sys/devices/system/cpu/cpufreq/policy*/scaling_governor
echo "schedutil" | tee /sys/devices/system/cpu/cpufreq/policy*/scaling_governor >/dev/null
fi

0 comments on commit 1fd12c3

Please sign in to comment.