Browse Source

Setup for cgroup2 load accounting (cpu usage)

master
Ralph Rönnquist 7 months ago
committed by root
parent
commit
02ef3ae31c
  1. 50
      cgroups2-usage
  2. 66
      cgroupsetup.sh

50
cgroups2-usage

@ -0,0 +1,50 @@
#!/bin/bash
#
# Collect cgroup2 accounting usage numbers
#
# This is run on the master node once every 3 minutes, to prepare the
# "load measures" into rwhodsnap loadhistories.sqlite
DB="/var/cache/rwhodsnap/loadhistories2.sqlite"
# Current minute stamp
M=$(( $(date +%s) / 60 ))
# Number of microseconds/100 between updates
R=1800000
# H V W
update() {
L="$(newlisp -e "(int (div (sub $2 $3) $R))")"
echo "update $* $L" >&2
cat <<EOF | sqlite3 "$DB"
.timeout 1000
insert or replace into measure values('$1','$2');
insert into history values('$1','$M','$L');
EOF
}
NODENOW="0"
NODESUM="0"
CMD="grep usage_usec /sys/fs/cgroup/cpuset/*/cpu.stat"
( /root/doall.sh "$CMD" ; echo END ) |& \
while read P V ; do
if [ -z "$V" ] ; then
if [ -n "$NODE" ] ; then
update "$NODE" "$NODESUM" "$NODENOW"
fi
NODE="$P"
NODENOW="0"
NODESUM="0"
continue
fi
H="$(basename ${P%/*})"
W="$(sqlite3 $DB "select value from measure where host='$H';")"
NODESUM="$(newlisp -e "(add $V $NODESUM)")"
if [ "$H" = "$NODE" ] ; then
NODENOW="$W"
else
update "$H" "$V" "${W:-0}"
fi
done

66
cgroupsetup.sh

@ -0,0 +1,66 @@
#!/bin/bash
#
# This script sets up cgroup2 accounting cgroups for nodes by
# distributing qemu-system-* and gnt-start processes into separate
# groups
## Setup up cgroups2 account, unless done
grep -q "cgroup_root /sys/fs/cgroup tmpfs" /proc/mounts || \
mount -t tmpfs cgroup_root /sys/fs/cgroup
[ -d /sys/fs/cgroup/cpuset ] || \
mkdir /sys/fs/cgroup/cpuset
grep -q "cpuset /sys/fs/cgroup/cpuset cgroup2" /proc/mounts || \
mount -t cgroup2 cpuset /sys/fs/cgroup/cpuset
typeset -A GROUP
## Capture all current groups
for PG in $(ls /sys/fs/cgroup/cpuset/*/cgroup.procs) ; do
G=${PG%/*}
G=${G##*/}
for P in $(cat $PG) ; do
GROUP[$P]=$G
done
done
# add pid to group
addtogroup() {
echo adding $1 to $2 >&2
GROUP[$1]=$2
mkdir -p /sys/fs/cgroup/cpuset/$2
echo $1 > /sys/fs/cgroup/cpuset/$2/cgroup.procs 2>/dev/null
}
qemu_name() {
< /proc/$1/cmdline xargs -0 -n 1 echo | grep -A1 -e-name | tail -n1
}
subhost_name() {
< /proc/$1/cmdline xargs -0 -n 1 echo | tail -n1
}
addtogroup 0 $(hostname -s)
for P in $(pgrep qemu-system) ; do
addtogroup $P "$(qemu_name $P | sed 's/\..*//')"
done
for P in $(pgrep gnt-start) ; do
addtogroup $P "$(subhost_name $P | sed 's/\..*//')"
done
for i in {1..100} ; do
AGAIN=false
for P in $(cat /sys/fs/cgroup/cpuset/cgroup.procs) ; do
PP=( $(ps -oppid -p $P | tail -n1) )
[ -z "$PP" ] && continue
if [ -z "${GROUP[$PP]}" ] ; then
echo "no group for $PP" >&2
AGAIN=true
else
addtogroup $P "${GROUP[$PP]}"
fi
done
$AGAIN || break
echo "again $AGAIN"
done
Loading…
Cancel
Save