Skip to content

Instantly share code, notes, and snippets.

@simonsj
Created May 16, 2018 19:02
Show Gist options
  • Select an option

  • Save simonsj/ca7f532df95eec401a53f13ad31cbaff to your computer and use it in GitHub Desktop.

Select an option

Save simonsj/ca7f532df95eec401a53f13ad31cbaff to your computer and use it in GitHub Desktop.
How much time can be attributed to CPU throttling?
#!/bin/sh
# throttle-count.sh -- "how much time can be attributed to CPU throttling?"
stat_nodes=$(find /sys/fs/cgroup/cpu/kubepods -type f -name 'cpu.stat')
for stat_node in $stat_nodes; do
#
# Grab cpu.stat values which will end up looking something like:
#
# cat /sys/fs/cgroup/cpu/kubepods/burstable/pod0617de57-592a-11e8-841b-a0369f943158/537897a2687ae36ef6ef0da76a0e04392dc26d8891ca394f43cddb2526189711/cpu.stat
# nr_periods 62029
# nr_throttled 1552
# throttled_time 284632614873
#
# Where per https://www.kernel.org/doc/Documentation/scheduler/sched-bwc.txt:
#
# Statistics
# ----------
# A group's bandwidth statistics are exported via 3 fields in cpu.stat.
#
# cpu.stat:
# - nr_periods: Number of enforcement intervals that have elapsed.
# - nr_throttled: Number of times the group has been throttled/limited.
# - throttled_time: The total time duration (in nanoseconds) for which entities
# of the group have been throttled.
#
stat_value=$(cat $stat_node)
#
# Skip uninteresting stats that cite 0 values for everything.
#
if echo "$stat_value" | grep -q -e "^nr_throttled 0$"; then
continue
fi
periods=$(echo "$stat_value" |
grep -F -e 'nr_periods' | awk '{print $2}')
periods_throttled=$(echo "$stat_value" |
grep -F -e 'nr_throttled' | awk '{print $2}')
# nanoseconds
throttled_time_ns=$(echo "$stat_value" |
grep -F -e 'throttled_time' | awk '{print $2}')
# milliseconds
throttled_time_ms=$(( $throttled_time_ns / 1000000 ))
# seconds
throttled_time_s=$(( $throttled_time_ms / 1000 ))
# minutes
throttled_time_m=$(( $throttled_time_s / 60 ))
# hours
throttled_time_h=$(( $throttled_time_m / 60 ))
# daze
throttled_time_d=$(( $throttled_time_h / 24 ))
#
# output
#
cat - <<EOF
{"stat-node":"$stat_node"
,"periods":"$periods"
,"periods-throttled":"$periods_throttled"
,"throttled-time-ns":"$throttled_time_ns"
,"throttled-time-ms":"$throttled_time_ms"
,"throttled-time-s":"$throttled_time_s"
,"throttled-time-m":"$throttled_time_m"
,"throttled-time-h":"$throttled_time_h"
,"throttled-time-d":"$throttled_time_d"
}
EOF
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment