-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecond_perf_script
More file actions
executable file
·63 lines (48 loc) · 1.9 KB
/
second_perf_script
File metadata and controls
executable file
·63 lines (48 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/bin/bash
#This script controls the green LED of the Pi according to the CPU usage
# exits the script when any command fails
set -e
# specifying all excutables' paths
readonly TOP=/usr/bin/top
readonly GREP=/bin/grep
readonly CUT=/usr/bin/cut
readonly BC=/usr/bin/bc
readonly SUDO=/usr/bin/sudo
readonly TEE=/usr/bin/tee
readonly SLEEP=/bin/sleep
# if the scripts catches a "USR2" signal, or an interruption signal, it will call the reset act led function
trap reset_act_led USR2 SIGHUP SIGINT SIGTERM
# resets led trigger state to default and exits the script
reset_act_led() {
echo mmc0 | $SUDO $TEE /sys/class/leds/led0/trigger >/dev/null
exit
}
# turns on green LED
turn_on_act_led() {
echo 1 | $SUDO $TEE /sys/class/leds/led0/brightness >/dev/null
}
# turns off green LED
turn_off_act_led() {
echo 0 | $SUDO $TEE /sys/class/leds/led0/brightness >/dev/null
}
# removes green LED trigger before going into the while loop so we can manually turn on and off the LED
echo none | $SUDO $TEE /sys/class/leds/led0/trigger >/dev/null
while true #creates an infinite loop until the script exits itself
do
cpu_total=100
one_second=1
# get the percentage of cpu idle using top command, and get the numeric value using grep and cut
cpu_idle=$($TOP -b -n 1 | $GREP "%Cpu(s):" | $CUT -d ',' -f 4 | $GREP -o '[0-9]*\.[0-9]*')
# get the percentage of cpu usage from all users and processes
cpu_usage=$(echo "$cpu_total - $cpu_idle" | $BC -l)
# fraction of the second that the LED will stay on
led_on_time=$(echo "$cpu_usage / $cpu_total" | $BC -l)
# fraction of the second that the LED will stay off
led_off_time=$(echo "$one_second - $led_on_time" | $BC -l)
# turns on led, and sleeps for the calculated fraction of a second
turn_on_act_led
$SLEEP "$led_on_time"
# turns off led, and sleeps for the rest of the second
turn_off_act_led
$SLEEP "$led_off_time"
done