ampere_research/pytorch/power.py

55 lines
1.9 KiB
Python
Raw Normal View History

2024-11-28 00:04:57 -05:00
#! /bin/python3
2024-12-02 23:32:33 -05:00
# Why does this program continuously print instead of accumulating the results
# and printing them at the end? Because of indefinite program runtime!
2024-11-28 00:04:57 -05:00
# For the sake of simplicity, this script will only return the power info
# for the first socket of Ampere Altra nodes.
import argparse, subprocess
2024-12-02 23:32:33 -05:00
import sys
2024-11-28 00:04:57 -05:00
parser = argparse.ArgumentParser()
2024-12-09 10:57:15 -05:00
parser.add_argument('program', nargs='*')
2024-11-28 00:04:57 -05:00
parser.add_argument('-s', '--seconds', type=int)
args = parser.parse_args()
arch = subprocess.run(['uname', '-m'], stdout=subprocess.PIPE, text=True).stdout.strip()
if arch == "aarch64":
import time
upper_bound = args.seconds if args.seconds is not None else -1
i = 0
while i != upper_bound:
proc = subprocess.Popen(['sensors'], stdout=subprocess.PIPE)
2024-12-09 10:57:15 -05:00
proc = subprocess.Popen(
['awk', '/CPU power:/ {print $3; exit}'],
stdin=proc.stdout,
stdout=subprocess.PIPE,
text=True)
2024-11-28 00:04:57 -05:00
power = proc.communicate()[0].strip().split('\n')[0]
print(power)
i += 1
time.sleep(1)
elif arch == "x86_64":
2024-12-09 10:57:15 -05:00
if args.seconds is None:
proc = subprocess.Popen(
['turbostat', '-s', 'PkgWatt'] + args.program,
stdout=subprocess.DEVNULL,
stderr=subprocess.PIPE)
proc = subprocess.Popen(
['sed', '-n', '/PkgWatt/{n;p}'],
stdin=proc.stderr,
stdout=subprocess.PIPE,
text=True)
else:
proc = subprocess.Popen(
['turbostat', '-s', 'PkgWatt', '-n', str(args.seconds), '-i', '1'],
stdout=subprocess.PIPE)
proc = subprocess.Popen(
['sed', '-n', '/PkgWatt/{n;p}'],
stdin=proc.stdout,
stdout=subprocess.PIPE,
text=True)
power = proc.communicate()[0].strip().split('\n')
print('\n'.join(power))