Skip to content

Instantly share code, notes, and snippets.

@wcoastsands
Last active March 30, 2022 20:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wcoastsands/6534ff4aca83963cca3a8a57559c2457 to your computer and use it in GitHub Desktop.
Save wcoastsands/6534ff4aca83963cca3a8a57559c2457 to your computer and use it in GitHub Desktop.
Thermal bench test for Raspberry Pi
#!/bin/bash
SOURCE="${BASH_SOURCE[0]}"
# Resolve $SOURCE until the file is no longer a symlink.
while [ -h "$SOURCE" ]; do
DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
# If $SOURCE was a relative symlink, we need to resolve it
# relative to the path where the symlink file was located.
done
DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
# Default interval time in seconds.
INTERVAL=5
# Regular expression to validate argument as a number.
REGEX='^[0-9]+([.][0-9]+)?$'
if [[ $1 =~ $REGEX ]]; then
INTERVAL=$1
elif [ ! -z "$1" ]; then
echo "Iteratively reports the thermal status of the system."
echo
echo "Usage:"
echo " ./rpi-thermal-monitor.sh [help|interval]"
echo
echo "Examples:"
echo " ./rpi-thermal-monitor.sh"
echo " ./rpi-thermal-monitor.sh 10"
echo " ./rpi-thermal-monitor.sh 1.5"
echo " ./rpi-thermal-monitor.sh help"
echo
echo "Options:"
echo " help Displays this documentation."
echo " interval Sets the interval time in seconds between status updates."
echo
echo "By default, the update interval time is set to $INTERVAL seconds."
echo
echo "See 'rpi-thermal-status.sh help' for more details."
exit
fi
clear
while true; do
$DIR/rpi-thermal-status.sh inline
echo -ne "-- Press [CTRL+C] to quit.\r"
sleep $INTERVAL
done
exit
#!/bin/bash
CPU=$(</sys/class/thermal/thermal_zone0/temp)
CPU="temp=$(echo "scale=1; $CPU/1000" | bc)'C"
GPU=$(/opt/vc/bin/vcgencmd measure_temp)
FREQUENCY=$(/opt/vc/bin/vcgencmd measure_clock arm)
THROTTLED=$(/opt/vc/bin/vcgencmd get_throttled)
if [ "$1" = "inline" ]; then
echo "$(date) @ $(hostname) cpu_$CPU gpu_$GPU $FREQUENCY $THROTTLED"
elif [ -n "$1" ]; then
echo "Reports the current temperatures, frequency, and throttled state of the system."
echo
echo "Usage:"
echo " ./rpi-thermal-status.sh [help|inline]"
echo
echo "Examples:"
echo " ./rpi-thermal-status.sh"
echo " ./rpi-thermal-status.sh inline"
echo " ./rpi-thermal-status.sh help"
echo
echo "Options:"
echo " help Displays this documentation."
echo " inline Generates output using a single-line format."
echo
echo "By default, output is generated in a multi-line format. Use the \"inline\" option"
echo "to generate output in a single-line format instead."
echo
echo "The throttled state of the system is described as a bit pattern."
echo
echo "Bit Meaning"
echo " 0 Under-voltage detected"
echo " 1 Arm frequency capped"
echo " 2 Currently throttled"
echo " 3 Soft temperature limit active"
echo " 16 Under-voltage has occurred"
echo " 17 Arm frequency capped has occurred"
echo " 18 Throttling has occurred"
echo " 19 Soft temperature limit has occurred"
echo
echo "For example:"
echo " The hex value 0x60002 equates to 0110 0000 0000 0000 0010 in binary."
echo " The active bits (written as 1) are at binary index 1, 17, and 18 (right to left)."
echo " Therefore, in this example the arm frequency is currently capped (1), "
echo " and has previously been both capped (17) and throttled (18)."
else
echo "$(date) @ $(hostname)"
echo "----------------------------------"
echo "CPU Temp => $CPU"
echo "GPU Temp => $GPU"
echo "Frequency => $FREQUENCY"
echo "Throttled => $THROTTLED"
fi
exit
#!/bin/bash
SOURCE="${BASH_SOURCE[0]}"
# Resolve $SOURCE until the file is no longer a symlink.
while [ -h "$SOURCE" ]; do
DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
# If $SOURCE was a relative symlink, we need to resolve it
# relative to the path where the symlink file was located.
done
DIR="$( cd -P "$( dirname "$SOURCE" )" >/dev/null 2>&1 && pwd )"
# The number of tests to perform.
COUNT=8
# Regular expression to validate argument as an interger.
REGEX='^[0-9]+$'
if [[ $1 =~ $REGEX ]]; then
COUNT=$1
elif [ -n "$1" ]; then
echo "Performs a series of bench tests, and reports the thermal status after each iteration."
echo
echo "Usage:"
echo " ./rpi-thermal-test.sh [help|count]"
echo
echo "Examples:"
echo " ./rpi-thermal-test.sh"
echo " ./rpi-thermal-test.sh 3"
echo " ./rpi-thermal-test.sh help"
echo
echo "Options:"
echo " help Displays this documentation."
echo " count Sets the number of bench tests to perform."
echo
echo "By default, a count of $COUNT bench tests will be performed. The results at line 0 are initial readings "
echo "to establish a baseline for comparison with results of following bench tests."
echo
echo "See 'rpi-thermal-status.sh help' for more details."
exit
fi
echo "Running thermal bench test..."
uptime
echo "0. $($DIR/rpi-thermal-status.sh inline)"
for i in $(seq 1 $COUNT)
do
echo -ne "-- Performing test $i of $COUNT. Please wait, or press [CTRL+C] to quit.\r"
sysbench --test=cpu --cpu-max-prime=20000 --num-threads=4 run >/dev/null 2>&1
echo "$i. $($DIR/rpi-thermal-status.sh inline)"
done
uptime
echo "Done."
exit
@wcoastsands
Copy link
Author

wcoastsands commented May 25, 2019

Sample output from rpi-thermal-test.sh when run on a Kano Computer Kit Touch with Raspberry Pi model 3B:

~ $ ./rpi-thermal-test.sh
Running thermal bench test...
0. Sun May 26 08:21:04 PDT 2019 @ kano  cpu_temp=57.9'C  gpu_temp=58.0'C  frequency(45)=600000000  throttled=0x50000
1. Sun May 26 08:23:10 PDT 2019 @ kano  cpu_temp=83.8'C  gpu_temp=83.8'C  frequency(45)=765000000  throttled=0x70002
2. Sun May 26 08:25:48 PDT 2019 @ kano  cpu_temp=84.3'C  gpu_temp=85.4'C  frequency(45)=711000000  throttled=0x70002
3. Sun May 26 08:28:42 PDT 2019 @ kano  cpu_temp=85.9'C  gpu_temp=85.4'C  frequency(45)=603000000  throttled=0x70002
4. Sun May 26 08:31:48 PDT 2019 @ kano  cpu_temp=85.9'C  gpu_temp=85.4'C  frequency(45)=603000000  throttled=0x70002
5. Sun May 26 08:35:00 PDT 2019 @ kano  cpu_temp=85.9'C  gpu_temp=86.0'C  frequency(45)=603000000  throttled=0x70002
6. Sun May 26 08:38:17 PDT 2019 @ kano  cpu_temp=85.9'C  gpu_temp=86.0'C  frequency(45)=603000000  throttled=0x70002
7. Sun May 26 08:41:35 PDT 2019 @ kano  cpu_temp=85.9'C  gpu_temp=86.0'C  frequency(45)=603000000  throttled=0x70002
8. Sun May 26 08:44:56 PDT 2019 @ kano  cpu_temp=86.5'C  gpu_temp=86.0'C  frequency(45)=549000000  throttled=0x70006
Done.

In this sample, the throttled status indicates frequency capping occurred during the test, with throttling toward the end, while also indicating that reports of under-voltage, arm frequency capping, and throttling had previously occurred. The results at line 0 are initial readings to establish a baseline for the following bench tests.

Use the help option to view documentation:

~ $ ./rpi-thermal-monitor.sh help
Iteratively reports the thermal status of the system.

Usage:
 ./rpi-thermal-monitor.sh [help|interval]

Examples:
 ./rpi-thermal-monitor.sh
 ./rpi-thermal-monitor.sh 10
 ./rpi-thermal-monitor.sh 1.5
 ./rpi-thermal-monitor.sh help

Options:
 help      Displays this documentation.
 interval  Sets the interval time in seconds between status updates.

By default, the update interval time is set to 5 seconds.

See 'rpi-thermal-status.sh help' for more details.
~ $ ./rpi-thermal-status.sh help
Reports the current temperatures, frequency, and throttled state of the system.

Usage:
 ./rpi-thermal-status.sh [help|inline]

Examples:
 ./rpi-thermal-status.sh
 ./rpi-thermal-status.sh inline
 ./rpi-thermal-status.sh help

Options:
 help     Displays this documentation.
 inline   Generates output using a single-line format.

By default, output is generated in a multi-line format. Use the "inline" option
to generate output in a single-line format instead.

The throttled state of the system is described as a bit pattern.

Bit   Meaning
 0     Under-voltage detected
 1     Arm frequency capped
 2     Currently throttled
 3     Soft temperature limit active
 16    Under-voltage has occurred
 17    Arm frequency capped has occurred
 18    Throttling has occurred
 19    Soft temperature limit has occurred

For example:
 The hex value 0x60002 equates to 0110 0000 0000 0000 0010 in binary.
 The active bits (written as 1) are at binary index 1, 17, and 18 (right to left).
 Therefore, in this example the arm frequency is currently capped (1),
 and has previously been both capped (17) and throttled (18).
~ $ ./rpi-thermal-test.sh help
Performs a series of bench tests, and reports the thermal status after each iteration.

Usage:
 ./rpi-thermal-test.sh [help|count]

Examples:
 ./rpi-thermal-test.sh
 ./rpi-thermal-test.sh 3
 ./rpi-thermal-test.sh help

Options:
 help    Displays this documentation.
 count   Sets the number of bench tests to perform.

By default, a count of 8 bench tests will be performed. The results at line 0 are initial readings
to establish a baseline for comparison with results of following bench tests.

See 'rpi-thermal-status.sh help' for more details.

See vcgencmd documentation for more info.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment