From 23c614d2de259e6926c20069345d634ae1712662 Mon Sep 17 00:00:00 2001 From: Birte Kristina Friesel Date: Fri, 17 Jan 2025 14:05:31 +0100 Subject: Add helper to determine system-wide energy and power usage via RAPL To Do: Automatically determine (and subtract) idle power usage --- bin/run-with-rapl | 32 ++++++++++++++++++++++++++++++++ libexec/rapl-to-dfatool.py | 27 +++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) create mode 100755 bin/run-with-rapl create mode 100755 libexec/rapl-to-dfatool.py diff --git a/bin/run-with-rapl b/bin/run-with-rapl new file mode 100755 index 0000000..54d2d9c --- /dev/null +++ b/bin/run-with-rapl @@ -0,0 +1,32 @@ +#!/bin/sh + +DFATOOL="$(dirname "$0")/.." + +if test -z "${COUNTERS}"; then + COUNTERS="$(ls -1 /sys/class/powercap)" +fi + +NAMES= +UJ_FILES= +for counter in ${COUNTERS}; do + if test -e /sys/class/powercap/${counter}/name && test -e /sys/class/powercap/${counter}/energy_uj; then + NAMES="${NAMES} $(cat /sys/class/powercap/${counter}/name)_${counter} " + UJ_FILES="${UJ_FILES} /sys/class/powercap/${counter}/energy_uj" + fi +done + +if ! cat ${UJ_FILES} > /dev/null; then + echo "Unable to read all counters (${UJ_FILES})" >&2 + echo "You may need to run sudo chmod a+r /sys/class/powercap/*/energy_uj" >&2 + exit 1 +fi + +OUTPUT=$(mktemp) + +RAPL_START=$(cat ${UJ_FILES}) +3>${OUTPUT} perf stat -x, -e duration_time --log-fd 3 "$@" +RAPL_END=$(cat ${UJ_FILES}) + +"${DFATOOL}/libexec/rapl-to-dfatool.py" "$(cat ${OUTPUT})" "${NAMES}" "${RAPL_START}" "${RAPL_END}" + +rm -f ${OUTPUT} diff --git a/libexec/rapl-to-dfatool.py b/libexec/rapl-to-dfatool.py new file mode 100755 index 0000000..5ab4c38 --- /dev/null +++ b/libexec/rapl-to-dfatool.py @@ -0,0 +1,27 @@ +#!/usr/bin/env python3 + +import sys + + +def main(perf_line, rapl_names, rapl_start, rapl_stop): + duration_ns = int(perf_line.split(",")[3]) + + rapl_names = rapl_names.split() + rapl_start = rapl_start.split() + rapl_stop = rapl_stop.split() + + buf = [f"duration_ns={duration_ns}"] + + for i in range(len(rapl_names)): + uj_start = int(rapl_start[i]) + uj_stop = int(rapl_stop[i]) + buf.append(f"{rapl_names[i]}_energy_uj={uj_stop - uj_start}") + buf.append( + f"{rapl_names[i]}_power_W={(uj_stop - uj_start) * 1000 / duration_ns}" + ) + + print(" ".join(buf)) + + +if __name__ == "__main__": + main(*sys.argv[1:]) -- cgit v1.2.3