-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrocprofwrap.py
More file actions
96 lines (71 loc) · 2.59 KB
/
Copy pathrocprofwrap.py
File metadata and controls
96 lines (71 loc) · 2.59 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import os
import sys
import time
import argparse
import signal
import subprocess
def stopProf():
# Send stop signal to profiling processes
for ps in profProc:
ps.send_signal(signal.SIGINT)
# Waiting for profiling processes to finsih
for ps in profProc:
ps.wait()
def cleanUp():
stopProf()
app.terminate()
app.wait()
parser = argparse.ArgumentParser(
prog="rocprofwrap",
description="Profiles power,frequency, voltage, and performance counters for MI products.",
epilog="Example: python run.py --cmd=\"ls -lh\" --gpus=0 --prefix=metrics.csv --redirect=logfile.txt --counters_file=counters.json"
)
parser.add_argument("--cmd", help="Application command", required=True)
parser.add_argument("--gpus", default="0", help="List of GPUs to profile "+"(default: %(default)s)", required=True)
parser.add_argument("--prefix", default="metrics.csv", help="Filename prefix for data output "+"(default: %(default)s)", required=True)
parser.add_argument("--redirect", default=None, help="Filename for stdout log file.")
# add one more argument to specify the counters included in file to profile
parser.add_argument("--counters_file",
default=None,
help="Optional JSON file containing hardware counter configurations. "
"If not provided, default counters will be used.",required=False)
args = parser.parse_args()
CMD = args.cmd
GPUS = args.gpus.split(",")
PREFIX = args.prefix
REDIRECT = args.redirect
COUNTERS_FILE = args.counters_file
profProc = []
if "WRAPPER_ROOT" not in os.environ:
print("rocprofwrap: Please set your WRAPPER_ROOT environment variable.")
sys.exit()
try:
for gid in GPUS:
ps = [os.getenv("WRAPPER_ROOT")+"/gpuprof", PREFIX+"_"+gid, gid]
# Add counters file if provided
if COUNTERS_FILE is not None:
if os.path.exists(COUNTERS_FILE):
ps.append(COUNTERS_FILE)
else:
print(f"Warning: Counters file {COUNTERS_FILE} not found. Using default counters.")
profProc.append(subprocess.Popen(ps))
except Exception as e:
stopProf()
print("rocprofwrap: ", e)
sys.exit()
try:
if REDIRECT is not None:
with open(REDIRECT, "w") as fp:
app = subprocess.Popen(CMD, shell=True, stdout=fp)
else:
app = subprocess.Popen(CMD, shell=True)
except Exception as e:
cleanUp()
print("rocprofwrap: ", e)
sys.exit()
# Wait until the application finishes
while app.poll() is None:
time.sleep(0.1)
cleanUp()
print("Detected that the application ended.")
sys.exit()