cpu: Implement steal/guest/guest_nice counters

steal (since Linux 2.6.11)
       (8) Stolen time, which is the time spent in
       other operating systems when running in a virtu‐
       alized environment

guest (since Linux 2.6.24)
       (9) Time spent running a virtual CPU for guest
       operating systems under the control of the Linux
       kernel.

guest_nice (since Linux 2.6.33)
       (10) Time spent running a niced guest (virtual
       CPU for guest operating systems under the con‐
       trol of the Linux kernel).
This commit is contained in:
Christian Schrötter 2020-05-08 16:16:19 +02:00
parent 9d596643cc
commit 2c20918fd4
No known key found for this signature in database
GPG key ID: 8038DEBE14AD09A4

View file

@ -1,7 +1,17 @@
config_cpu() { config_cpu() {
extinfo="" extinfo=""
if grep -q '^cpu \{1,\}[0-9]\{1,\} \{1,\}[0-9]\{1,\} \{1,\}[0-9]\{1,\} \{1,\}[0-9]\{1,\} \{1,\}[0-9]\{1,\} \{1,\}[0-9]\{1,\} \{1,\}[0-9]\{1,\}' /proc/stat; then fields=$(grep '^cpu ' /proc/stat | wc -w)
extinfo="iowait irq softirq" if [ "$fields" -gt 5 ]; then
extinfo="$extinfo iowait irq softirq"
if [ "$fields" -gt 8 ]; then
extinfo="$extinfo steal"
if [ "$fields" -gt 9 ]; then
extinfo="$extinfo guest"
if [ "$fields" -gt 10 ]; then
extinfo="$extinfo guest_nice"
fi
fi
fi
fi fi
# shellcheck disable=SC2126 # shellcheck disable=SC2126
NCPU=$(grep '^cpu[0-9]\+ ' /proc/stat | wc -l) NCPU=$(grep '^cpu[0-9]\+ ' /proc/stat | wc -l)
@ -45,7 +55,7 @@ config_cpu() {
echo "idle.max 5000" echo "idle.max 5000"
echo "idle.type DERIVE" echo "idle.type DERIVE"
echo "idle.info Idle CPU time" echo "idle.info Idle CPU time"
if [ -n "$extinfo" ]; then if [ "$fields" -gt 5 ]; then
echo "iowait.label iowait" echo "iowait.label iowait"
echo "iowait.draw STACK" echo "iowait.draw STACK"
echo "iowait.min 0" echo "iowait.min 0"
@ -64,21 +74,51 @@ config_cpu() {
echo "softirq.max 5000" echo "softirq.max 5000"
echo "softirq.type DERIVE" echo "softirq.type DERIVE"
echo "softirq.info CPU time spent handling 'batched' interrupts" echo "softirq.info CPU time spent handling 'batched' interrupts"
if [ "$fields" -gt 8 ]; then
echo "steal.label steal"
echo "steal.draw STACK"
echo "steal.min 0"
echo "steal.max 5000"
echo "steal.type DERIVE"
echo "steal.info The time that a virtual CPU had runnable tasks, but the virtual CPU itself was not running"
if [ "$fields" -gt 9 ]; then
echo "guest.label guest"
echo "guest.draw STACK"
echo "guest.min 0"
echo "guest.max 5000"
echo "guest.type DERIVE"
echo "guest.info The time spent running a virtual CPU for guest operating systems"
if [ "$fields" -gt 10 ]; then
echo "guest_nice.label guest_nice"
echo "guest_nice.draw STACK"
echo "guest_nice.min 0"
echo "guest_nice.max 5000"
echo "guest_nice.type DERIVE"
echo "guest_nice.info The time spent running a virtual CPU for a niced guest operating system"
fi
fi
fi
fi fi
} }
fetch_cpu() { fetch_cpu() {
extinfo="" fields=$(grep '^cpu ' /proc/stat | wc -w)
if grep -q '^cpu \{1,\}[0-9]\{1,\} \{1,\}[0-9]\{1,\} \{1,\}[0-9]\{1,\} \{1,\}[0-9]\{1,\} \{1,\}[0-9]\{1,\} \{1,\}[0-9]\{1,\} \{1,\}[0-9]\{1,\}' /proc/stat; then
extinfo="iowait irq softirq"
fi
CINFO=$(grep '^cpu ' /proc/stat | cut -c6-) CINFO=$(grep '^cpu ' /proc/stat | cut -c6-)
echo "user.value" "$(echo "$CINFO" | cut -d " " -f 1)" echo "user.value" "$(echo "$CINFO" | cut -d " " -f 1)"
echo "nice.value" "$(echo "$CINFO" | cut -d " " -f 2)" echo "nice.value" "$(echo "$CINFO" | cut -d " " -f 2)"
echo "system.value" "$(echo "$CINFO" | cut -d " " -f 3)" echo "system.value" "$(echo "$CINFO" | cut -d " " -f 3)"
echo "idle.value" "$(echo "$CINFO" | cut -d " " -f 4)" echo "idle.value" "$(echo "$CINFO" | cut -d " " -f 4)"
if [ -n "$extinfo" ]; then if [ "$fields" -gt 5 ]; then
echo "iowait.value" "$(echo "$CINFO" | cut -d " " -f 5)" echo "iowait.value" "$(echo "$CINFO" | cut -d " " -f 5)"
echo "irq.value" "$(echo "$CINFO" | cut -d " " -f 6)" echo "irq.value" "$(echo "$CINFO" | cut -d " " -f 6)"
echo "softirq.value" "$(echo "$CINFO" | cut -d " " -f 7)" echo "softirq.value" "$(echo "$CINFO" | cut -d " " -f 7)"
if [ "$fields" -gt 8 ]; then
echo "steal.value" "$(echo "$CINFO" | cut -d " " -f 8)"
if [ "$fields" -gt 9 ]; then
echo "guest.value" "$(echo "$CINFO" | cut -d " " -f 9)"
if [ "$fields" -gt 10 ]; then
echo "guest_nice.value" "$(echo "$CINFO" | cut -d " " -f 10)"
fi
fi
fi
fi fi
} }