Skip to content

Commit 0d7e824

Browse files
authored
Merge pull request #2 from froonix/feature/steal-guest
cpu: Implement steal/guest/guest_nice counters
2 parents 7f6616d + 5f55dcf commit 0d7e824

File tree

1 file changed

+41
-9
lines changed

1 file changed

+41
-9
lines changed

plugins/cpu

+41-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
config_cpu() {
22
extinfo=""
3-
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
4-
extinfo="iowait irq softirq"
5-
fi
3+
fields=$(grep '^cpu ' /proc/stat | wc -w)
4+
if [ "$fields" -gt 5 ]; then extinfo="$extinfo iowait irq softirq"; fi
5+
if [ "$fields" -gt 8 ]; then extinfo="$extinfo steal"; fi
6+
if [ "$fields" -gt 9 ]; then extinfo="$extinfo guest"; fi
7+
if [ "$fields" -gt 10 ]; then extinfo="$extinfo guest_nice"; fi
68
# shellcheck disable=SC2126
79
NCPU=$(grep '^cpu[0-9]\+ ' /proc/stat | wc -l)
810
PERCENT=$((NCPU * 100))
@@ -45,7 +47,7 @@ config_cpu() {
4547
echo "idle.max 5000"
4648
echo "idle.type DERIVE"
4749
echo "idle.info Idle CPU time"
48-
if [ -n "$extinfo" ]; then
50+
if [ "$fields" -gt 5 ]; then
4951
echo "iowait.label iowait"
5052
echo "iowait.draw STACK"
5153
echo "iowait.min 0"
@@ -65,20 +67,50 @@ config_cpu() {
6567
echo "softirq.type DERIVE"
6668
echo "softirq.info CPU time spent handling 'batched' interrupts"
6769
fi
70+
if [ "$fields" -gt 8 ]; then
71+
echo "steal.label steal"
72+
echo "steal.draw STACK"
73+
echo "steal.min 0"
74+
echo "steal.max 5000"
75+
echo "steal.type DERIVE"
76+
echo "steal.info The time that a virtual CPU had runnable tasks, but the virtual CPU itself was not running"
77+
fi
78+
if [ "$fields" -gt 9 ]; then
79+
echo "guest.label guest"
80+
echo "guest.draw STACK"
81+
echo "guest.min 0"
82+
echo "guest.max 5000"
83+
echo "guest.type DERIVE"
84+
echo "guest.info The time spent running a virtual CPU for guest operating systems"
85+
fi
86+
if [ "$fields" -gt 10 ]; then
87+
echo "guest_nice.label guest_nice"
88+
echo "guest_nice.draw STACK"
89+
echo "guest_nice.min 0"
90+
echo "guest_nice.max 5000"
91+
echo "guest_nice.type DERIVE"
92+
echo "guest_nice.info The time spent running a virtual CPU for a niced guest operating system"
93+
fi
6894
}
6995
fetch_cpu() {
70-
extinfo=""
71-
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
72-
extinfo="iowait irq softirq"
73-
fi
96+
fields=$(grep '^cpu ' /proc/stat | wc -w)
7497
CINFO=$(grep '^cpu ' /proc/stat | cut -c6-)
7598
echo "user.value" "$(echo "$CINFO" | cut -d " " -f 1)"
7699
echo "nice.value" "$(echo "$CINFO" | cut -d " " -f 2)"
77100
echo "system.value" "$(echo "$CINFO" | cut -d " " -f 3)"
78101
echo "idle.value" "$(echo "$CINFO" | cut -d " " -f 4)"
79-
if [ -n "$extinfo" ]; then
102+
if [ "$fields" -gt 5 ]; then
80103
echo "iowait.value" "$(echo "$CINFO" | cut -d " " -f 5)"
81104
echo "irq.value" "$(echo "$CINFO" | cut -d " " -f 6)"
82105
echo "softirq.value" "$(echo "$CINFO" | cut -d " " -f 7)"
83106
fi
107+
if [ "$fields" -gt 8 ]; then
108+
echo "steal.value" "$(echo "$CINFO" | cut -d " " -f 8)"
109+
fi
110+
if [ "$fields" -gt 9 ]; then
111+
echo "guest.value" "$(echo "$CINFO" | cut -d " " -f 9)"
112+
fi
113+
if [ "$fields" -gt 10 ]; then
114+
echo "guest_nice.value" "$(echo "$CINFO" | cut -d " " -f 10)"
115+
fi
84116
}

0 commit comments

Comments
 (0)