Skip to content

Commit

Permalink
basic power monitoring script
Browse files Browse the repository at this point in the history
  • Loading branch information
carns committed Oct 4, 2024
1 parent 05f91cf commit f05a639
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions src/quintain-cpu-power-watcher.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/bin/bash

# report power usage in watts over 1 second intervals for each powercap rapl
# zone

# derived from example at https://www.reddit.com/r/linuxquestions/comments/1dp81bq/hardware_power_consumption/`
# changes:
# - some bug fixes in the watts conversion
# - track each powercap/rapl zone separately, and report its name
# - use floating point timestamps and calculations

# you can temporarily enable access to the required counters (i.e. on a
# development laptop) by running the following:
#
# sudo chmod -R a+r /sys/class/powercap/intel-rapl

declare -A zones
declare -A old_zones

old_time=0
while :; do
# figure out how many zones are available; store current microjoules value
# for each in an associative array
for i in `ls /sys/class/powercap/intel-rapl/ |grep intel-rapl:`; do
name=`cat /sys/class/powercap/intel-rapl/$i/name`
energy_uj=`cat /sys/class/powercap/intel-rapl/$i/energy_uj`
zones[$name]=$energy_uj
done

# current timestamp in seconds and nanoseconds
time=$(date +%s.%N)

if [ ${#old_zones[@]} -ne 0 ]; then
# normal case. calculate power in watts for each zone based on
# elapsed time and difference in reported microjoules
echo -n "$time"
for i in "${!zones[@]}"; do
watts=`echo "(${zones[$i]} - ${old_zones[$i]}) / (($time - $old_time) * 10^6)" | bc -l`
echo -e -n "\t$watts"
done
echo -e -n "\n"
else
# first loop iteration. we don't have enough data yet to report
# watts, but we use this opportunity to print a header row
echo -n "# <timestamp>"
for i in "${!zones[@]}"; do
echo -e -n "\t<$i>"
done
echo -e -n "\n"
fi

# save old time and microjoules
old_time=$time
for i in "${!zones[@]}"; do
old_zones[$i]=${zones[$i]}
done

# one second interval
sleep 1
done

0 comments on commit f05a639

Please sign in to comment.