[rocprofv3-avail] Rework rocprofv3-avail tool (#312)

---------

Co-authored-by: vlaindic_amdeng <vladimir.indic@amd.com>

[ROCm/rocprofiler-sdk commit: 80d60d8535]
This commit is contained in:
Nagaraj, Sriraksha
2025-06-06 13:51:37 -05:00
committed by GitHub
parent e5097d6a36
commit 3a62fee4ac
24 changed files with 1672 additions and 897 deletions
@@ -4,6 +4,8 @@
rocprofiler_activate_clang_tidy()
add_subdirectory(rocprofv3_avail_module)
# Adding main rocprofv3
configure_file(rocprofv3.py ${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}/rocprofv3 @ONLY)
@@ -14,11 +16,11 @@ install(
WORLD_EXECUTE
COMPONENT tools)
configure_file(rocprofv3_avail.py
${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}/rocprofv3_avail COPYONLY)
configure_file(rocprofv3-avail.py
${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}/rocprofv3-avail COPYONLY)
install(
FILES ${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}/rocprofv3_avail
FILES ${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}/rocprofv3-avail
DESTINATION ${CMAKE_INSTALL_BINDIR}
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ
WORLD_EXECUTE
+425
View File
@@ -0,0 +1,425 @@
#!/usr/bin/env python3
# MIT License
#
# Copyright (c) 2024-2025 Advanced Micro Devices, Inc. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import os
import argparse
import sys
from rocprofv3_avail_module import avail
def format_help(formatter, w=120, h=40):
"""Return a wider HelpFormatter, if possible."""
try:
kwargs = {"width": w, "max_help_position": h}
formatter(None, **kwargs)
return lambda prog: formatter(prog, **kwargs)
except TypeError:
return formatter
def strtobool(val):
"""Convert a string representation of truth to true or false.
True values are 'y', 'yes', 't', 'true', 'on', and '1'; false values
are 'n', 'no', 'f', 'false', 'off', and '0'. Raises ValueError if
'val' is anything else.
"""
if isinstance(val, (list, tuple)):
if len(val) > 1:
val_type = type(val).__name__
raise ValueError(f"invalid truth value {val} (type={val_type})")
else:
val = val[0]
if isinstance(val, bool):
return val
elif isinstance(val, str) and val.lower() in ("y", "yes", "t", "true", "on", "1"):
return True
elif isinstance(val, str) and val.lower() in ("n", "no", "f", "false", "off", "0"):
return False
else:
val_type = type(val).__name__
raise ValueError(f"invalid truth value {val} (type={val_type})")
class booleanArgAction(argparse.Action):
def __call__(self, parser, args, value, option_string=None):
setattr(args, self.dest, strtobool(value))
def parse_arguments(args=None):
usage_examples = """
%(prog)s, e.g.
$ rocprofv3-avail [<rocprofv3-avail-option> ...]
$ rocprofv3-avail -- avail-hw-counters
"""
def add_list_options(subparsers):
list_command = subparsers.add_parser(
"list", help="List options for hw counters, agents and pc-sampling support"
)
add_parser_bool_argument(list_command, "--pmc", help="List counters")
add_parser_bool_argument(
list_command, "--agent", help="List basic info of agents"
)
add_parser_bool_argument(
list_command, "--pc-sampling", help="List agents supporting pc-sampling"
)
list_command.set_defaults(func=process_list)
def add_info_options(subparsers):
info_command = subparsers.add_parser(
"info",
help="Info options for detailed information of counters, agents, and pc-sampling configurations",
)
info_command.add_argument("--pmc", nargs="*", help="PMC info")
info_command.add_argument(
"--pc-sampling", nargs="*", help="Detailed PC Sampling info"
)
info_command.set_defaults(func=process_info)
def add_pmc_check_options(subparsers):
pmc_check_command = subparsers.add_parser(
"pmc-check", help="Checking counters collection support on agents"
)
pmc_check_command.add_argument(
"pmc", nargs="*", default=None, help="List of PMC names"
)
pmc_check_command.set_defaults(func=process_pmc_check)
def add_parser_bool_argument(gparser, *args, **kwargs):
gparser.add_argument(
*args,
**kwargs,
action=booleanArgAction,
nargs="?",
const=True,
type=str,
required=False,
metavar="BOOL",
)
# Create the parser
parser = argparse.ArgumentParser(
description="ROCProfilerV3-avail Run Script",
usage="%(prog)s [options] ",
epilog=usage_examples,
formatter_class=format_help(argparse.RawTextHelpFormatter),
)
parser.add_argument(
"-d",
"--device",
help="device index, device on a node to apply the sub-commands on",
type=int,
default=None,
)
subparsers = parser.add_subparsers(
dest="command", help="rocprofv3-avail sub commands"
)
add_list_options(subparsers)
add_info_options(subparsers)
add_pmc_check_options(subparsers)
return parser.parse_args(args[:])
def get_basic_agent_info(info):
basic_info = {}
req_keys = [
"cpu_cores_count",
"simd_count",
"max_waves_per_simd",
"runtime_visibility",
"wave_front_size",
"num_xcc",
"cu_count",
"array_count",
"num_shader_banks",
"simd_arrays_per_engine",
"cu_per_simd_array",
"simd_per_cu",
"gfx_target_version",
"max_waves_per_cu",
"gpu_id",
"workgroup_max_dim",
"grid_max_dim",
"name",
"vendor_name",
"product_name",
"model_name",
"runtime_visibility",
"node_id",
"logical_node_id",
"logical_node_type_id",
]
for key in req_keys:
basic_info.update({key: info[key]})
return basic_info
def list_basic_agent(args, list_counters):
def print_agent_counter(counters, info):
names = ["{:20}".format(counter.name) for counter in counters]
print(" PMC:\n")
for idx in range(0, len(names), int(len(counters) / 20)):
print(" {}".format(" ".join(names[idx : (idx + 5)])))
def print_basic_info(info):
print("GPU:{}\n".format(info["logical_node_type_id"]))
print("\n".join([" {:20}: {}".format(key, itr) for key, itr in info.items()]))
if not list_counters:
print("\n")
agent_info_map = avail.get_agent_info_map()
agent_counters = avail.get_counters()
for agent, info in agent_info_map.items():
if (
info["type"] == 2
and args.device is not None
and info["logical_node_type_id"] == args.device
):
print_basic_info(get_basic_agent_info(info))
if list_counters:
print_agent_counter(agent_counters[agent], info)
break
elif info["type"] == 2 and args.device is None:
print_basic_info(get_basic_agent_info(info))
if list_counters:
print_agent_counter(agent_counters[agent], info)
def list_pc_sampling(args):
sampling_agents = avail.get_pc_sample_configs()
agent_info_map = avail.get_agent_info_map()
print("Agents supporting PC Sampling\n")
for agent in sampling_agents.keys():
info = agent_info_map[agent]
print("GPU:{}\nNAME:{}\n".format(info["logical_node_type_id"], info["name"]))
def info_pc_sampling(args):
sampling_agents = avail.get_pc_sample_configs()
agent_info_map = avail.get_agent_info_map()
for agent, configs in sampling_agents.items():
info = agent_info_map[agent]
print("GPU:{}\nNAME:{}".format(info["logical_node_type_id"], info["name"]))
print("configs:")
for config in configs:
print(config)
print("\n")
print("\n")
def listing(args):
def print_agent_counter(counters, info):
names = ["{:20}".format(counter.name) for counter in counters]
print("PMC:\n")
for idx in range(0, len(names), int(len(counters) / 20)):
print(" {}".format(" ".join(names[idx : (idx + 5)])))
agent_counters = avail.get_counters()
agent_info_map = avail.get_agent_info_map()
for agent, info in agent_info_map.items():
if (
info["type"] == 2
and args.device is not None
and info["logical_node_type_id"] == args.device
):
print("GPU:{}\nNAME:{}".format(info["logical_node_type_id"], info["name"]))
print_agent_counter(agent_counters[agent], info)
break
elif info["type"] == 2 and args.device is None:
print("GPU:{}\nNAME:{}".format(info["logical_node_type_id"], info["name"]))
print_agent_counter(agent_counters[agent], info)
def info_pmc(args):
agent_counters = avail.get_counters()
agent_info_map = avail.get_agent_info_map()
def print_pmc_info(args, pmc_counters):
if not args.pmc:
for counter in pmc_counters:
print(counter)
print("\n")
else:
for pmc in args.pmc:
for counter in pmc_counters:
if pmc == counter.get_as_dict()["counter_name"]:
print(counter)
print("\n")
for agent, info in agent_info_map.items():
if (
info["type"] == 2
and args.device is not None
and info["logical_node_type_id"] == args.device
):
print("GPU:{}\nNAME:{}".format(info["logical_node_type_id"], info["name"]))
print_pmc_info(args, agent_counters[agent])
break
elif info["type"] == 2 and args.device is None:
print("GPU:{}\nNAME:{}".format(info["logical_node_type_id"], info["name"]))
print_pmc_info(args, agent_counters[agent])
def process_info(args):
if args.pmc is None and args.pc_sampling is None:
list_basic_agent(args, True)
if args.pmc is not None:
info_pmc(args)
if args.pc_sampling is not None:
os.environ["ROCPROFILER_PC_SAMPLING_BETA_ENABLED"] = "on"
info_pc_sampling(args)
def process_list(args):
if not args.agent and args.pc_sampling is None:
listing(args)
if args.agent:
list_basic_agent(args, False)
if args.pc_sampling:
os.environ["ROCPROFILER_PC_SAMPLING_BETA_ENABLED"] = "on"
list_pc_sampling(args)
def process_pmc_check(args):
def get_device_agent(device_id):
for agent, info in agent_info_map.items():
if info["type"] == 2 and info["logical_node_type_id"] == device_id:
return agent
avail.fatal_error("Invalid device id : {}".format(device_id))
def get_gpu_agents():
agent_ids = []
for agent, info in agent_info_map.items():
if info["type"] == 2:
agent_ids.append(agent)
return agent_ids
def get_counter_handle(counter_name):
agent_counters = avail.get_counters()
for agent, counters in agent_counters.items():
for counter in counters:
if counter.get_as_dict()["counter_name"] == counter_name:
return counter.counter_handle
avail.fatal_error("Invalid counter name")
def get_counter_names(pmc_list, agent):
agent_counters = avail.get_counters()
counter_names = []
for pmc in pmc_list:
for counter in agent_counters[agent]:
if counter.counter_handle == pmc:
counter_names.append(counter.name)
return counter_names
def get_logical_node_type_id(agent_id):
for agent, info in agent_info_map.items():
if info["type"] == 2 and info["id"]["handle"] == agent_id:
return info["logical_node_type_id"]
def process_qualifiers(pmc):
res = pmc.split(":")
if len(res) > 2:
avail.fatal_error("Invalid format for pmc-check")
if len(res) == 2:
qualifiers_list = []
qualifiers = res[1]
if "device" not in qualifiers:
avail.fatal_error("Incorrect input format for device index")
qualifiers = qualifiers.split(",")
for qualifier in qualifiers:
qualifiers_list.append(dict([qualifier.split("=")]))
return (res[0], qualifiers_list)
return (res[0], None)
if not args.pmc:
avail.fatal_error("Provide counter to check")
device_pmc = {}
agent_info_map = avail.get_agent_info_map()
for pmc in args.pmc:
counter, qualifiers = process_qualifiers(pmc)
if qualifiers is None:
if args.device is not None:
agent_handle = get_device_agent(args.device)
if agent_handle not in device_pmc.keys():
device_pmc.setdefault(agent_handle, [])
device_pmc[agent_handle].append(get_counter_handle(counter))
else:
agent_ids = get_gpu_agents()
for agent_handle in agent_ids:
if agent_handle not in device_pmc.keys():
device_pmc.setdefault(agent_handle, [])
device_pmc[agent_handle].append(get_counter_handle(counter))
else:
for itr in qualifiers:
agent_handle = get_device_agent(int(itr["device"]))
if agent_handle not in device_pmc.keys():
device_pmc.setdefault(agent_handle, [])
device_pmc[agent_handle].append(get_counter_handle(counter))
if avail.check_pmc(device_pmc) is True:
for agent, pmc in device_pmc.items():
device_id = get_logical_node_type_id(agent)
pmc_names = get_counter_names(pmc, agent)
print(
"Following input counters can be collected together on GPU:{}\t{}".format(
device_id, "\t".join(pmc_names)
)
)
def main(argv=None):
ROCPROFV3_AVAIL_DIR = os.path.dirname(os.path.realpath(__file__))
ROCM_DIR = os.path.dirname(ROCPROFV3_AVAIL_DIR)
ROCPROF_LIST_AVAIL_TOOL_LIBRARY = (
f"{ROCM_DIR}/libexec/rocprofiler-sdk/librocprofv3-list-avail.so"
)
avail.loadLibrary.libname = os.environ.get(
"ROCPROF_LIST_AVAIL_TOOL_LIBRARY", ROCPROF_LIST_AVAIL_TOOL_LIBRARY
)
args = parse_arguments(argv)
if args.command:
args.func(args)
if __name__ == "__main__":
ec = main(sys.argv[1:])
sys.exit(ec)
@@ -1321,11 +1321,25 @@ def run(app_args, args, **kwargs):
if args.list_avail:
update_env("ROCPROFILER_PC_SAMPLING_BETA_ENABLED", "on")
path = os.path.join(f"{ROCM_DIR}", "bin/rocprofv3_avail")
path = os.path.join(f"{ROCM_DIR}", "bin/rocprofv3-avail")
if app_args:
exit_code = subprocess.check_call([sys.executable, path], env=app_env)
exit_code = subprocess.check_call(
[sys.executable, path, "info"],
env=app_env,
)
if exit_code != 0:
fatal_error("rocprofv3-avail exit with error")
exit_code = subprocess.check_call(
[sys.executable, path, "info", "--pc-sampling"],
env=app_env,
)
else:
app_args = [sys.executable, path]
app_args = [sys.executable, path, "info"]
exit_code = subprocess.check_call(
[sys.executable, path, "info", "--pc-sampling"],
env=app_env,
)
elif not app_args and not args.echo:
log_config(app_env)
@@ -1,455 +0,0 @@
#!/usr/bin/env python3
# MIT License
#
# Copyright (c) 2024-2025 Advanced Micro Devices, Inc. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import ctypes
import os
import io
import csv
import socket
import sys
def fatal_error(msg, exit_code=1):
sys.stderr.write(f"Fatal error: {msg}\n")
sys.stderr.flush()
sys.exit(exit_code)
class derived_counter:
def __init__(
self, counter_name, counter_description, counter_expression, counter_dimensions
):
self.name = counter_name
self.description = counter_description
self.expression = counter_expression
self.dimensions = counter_dimensions
class basic_counter:
def __init__(
self, counter_name, counter_description, counter_block, counter_dimensions
):
self.name = counter_name
self.description = counter_description
self.block = counter_block
self.dimensions = counter_dimensions
class pc_config:
def __init__(self, config_method, config_unit, min_interval, max_interval):
self.method = config_method
self.unit = config_unit
self.min_interval = min_interval
self.max_interval = max_interval
ROCPROFV3_AVAIL_DIR = os.path.dirname(os.path.realpath(__file__))
ROCM_DIR = os.path.dirname(ROCPROFV3_AVAIL_DIR)
ROCPROF_LIST_AVAIL_TOOL_LIBRARY = (
f"{ROCM_DIR}/libexec/rocprofiler-sdk/librocprofv3-list-avail.so"
)
MAX_STR = 256
libname = os.environ.get(
"ROCPROF_LIST_AVAIL_TOOL_LIBRARY", ROCPROF_LIST_AVAIL_TOOL_LIBRARY
)
c_lib = ctypes.CDLL(libname)
if c_lib is None:
fatal_error(f"Error opening {libname}")
c_lib.get_number_of_counters.restype = ctypes.c_ulong
c_lib.get_number_of_pc_sample_configs.restype = ctypes.c_ulong
c_lib.get_number_of_dimensions.restype = ctypes.c_ulong
c_lib.get_number_of_counters.argtypes = [ctypes.c_int]
c_lib.get_number_of_pc_sample_configs.argtypes = [ctypes.c_int]
c_lib.get_number_of_dimensions.argtypes = [ctypes.c_int]
c_lib.get_pc_sample_config.argtypes = [
ctypes.c_ulong,
ctypes.c_ulong,
ctypes.POINTER(ctypes.POINTER(ctypes.c_char * MAX_STR)),
ctypes.POINTER(ctypes.POINTER(ctypes.c_char * MAX_STR)),
ctypes.POINTER(ctypes.c_ulong),
ctypes.POINTER(ctypes.c_ulong),
]
c_lib.get_counters_info.argtypes = [
ctypes.c_ulong,
ctypes.c_int,
ctypes.POINTER(ctypes.c_ulong),
ctypes.POINTER(ctypes.POINTER(ctypes.c_char * MAX_STR)),
ctypes.POINTER(ctypes.POINTER(ctypes.c_char * MAX_STR)),
ctypes.POINTER(ctypes.c_int),
]
c_lib.get_counter_expression.argtypes = [
ctypes.c_ulong,
ctypes.c_int,
ctypes.POINTER(ctypes.POINTER(ctypes.c_char * MAX_STR)),
]
c_lib.get_counter_dimension.argtypes = [
ctypes.c_ulong,
ctypes.c_ulong,
ctypes.POINTER(ctypes.c_ulong),
ctypes.POINTER(ctypes.POINTER(ctypes.c_char * MAX_STR)),
ctypes.POINTER(ctypes.c_ulong),
]
c_lib.get_counter_block.argtypes = [
ctypes.c_ulong,
ctypes.c_ulong,
ctypes.POINTER(ctypes.POINTER(ctypes.c_char * MAX_STR)),
]
c_lib.get_number_of_agents.restype = ctypes.c_size_t
c_lib.get_agent_node_id.restype = ctypes.c_ulong
c_lib.get_agent_node_id.argtypes = [ctypes.c_int]
agent_derived_counter_map = dict()
agent_basic_counter_map = dict()
agent_pc_sample_config_map = dict()
def get_counters(node_id):
no_of_counters = c_lib.get_number_of_counters(node_id)
basic_counters = []
derived_counters = []
for counter_idx in range(0, no_of_counters):
name_args = ctypes.POINTER(ctypes.c_char * MAX_STR)()
description_args = ctypes.POINTER(ctypes.c_char * MAX_STR)()
block_args = ctypes.POINTER(ctypes.c_char * MAX_STR)()
is_derived_args = ctypes.c_int()
counter_id_args = ctypes.c_ulong()
c_lib.get_counters_info(
node_id,
counter_idx,
ctypes.byref(counter_id_args),
name_args,
description_args,
ctypes.byref(is_derived_args),
)
is_derived = is_derived_args.value
counter_id = counter_id_args.value
no_of_dimensions = c_lib.get_number_of_dimensions(counter_id)
name = ctypes.cast(name_args, ctypes.c_char_p).value.decode("utf-8")
description = ctypes.cast(description_args, ctypes.c_char_p).value.decode("utf-8")
dimensions_stream = io.StringIO()
for dim in range(0, no_of_dimensions):
dim_name_args = ctypes.POINTER(ctypes.c_char * MAX_STR)()
dim_instance_args = ctypes.c_ulong()
dimension_id_args = ctypes.c_ulong()
c_lib.get_counter_dimension(
counter_id,
dim,
ctypes.byref(dimension_id_args),
dim_name_args,
ctypes.byref(dim_instance_args),
)
dim_name = ctypes.cast(dim_name_args, ctypes.c_char_p).value.decode("utf-8")
dim_instance = dim_instance_args.value
dimensions_stream.write(dim_name)
dimensions_stream.write("[0:")
dimensions_stream.write(str(dim_instance))
dimensions_stream.write("]")
if dim != no_of_dimensions - 1:
dimensions_stream.write("\t")
if is_derived:
expression_args = ctypes.POINTER(ctypes.c_char * MAX_STR)()
c_lib.get_counter_expression(node_id, counter_idx, expression_args)
counter_expression = ctypes.cast(
expression_args, ctypes.c_char_p
).value.decode("utf-8")
derived_counters.append(
derived_counter(
name, description, counter_expression, dimensions_stream.getvalue()
)
)
else:
block_args = ctypes.POINTER(ctypes.c_char * MAX_STR)()
c_lib.get_counter_block(node_id, counter_idx, block_args)
block = ctypes.cast(block_args, ctypes.c_char_p).value.decode("utf-8")
basic_counters.append(
basic_counter(name, description, block, dimensions_stream.getvalue())
)
dimensions_stream.close()
agent_derived_counter_map[node_id] = derived_counters
agent_basic_counter_map[node_id] = basic_counters
def get_pc_sample_configs(node_id):
no_of_pc_sample_configs = c_lib.get_number_of_pc_sample_configs(node_id)
pc_sample_configs = []
if no_of_pc_sample_configs:
for config_idx in range(0, no_of_pc_sample_configs):
method_args = ctypes.POINTER(ctypes.c_char * MAX_STR)()
unit_args = ctypes.POINTER(ctypes.c_char * MAX_STR)()
min_interval = ctypes.c_ulong()
max_interval = ctypes.c_ulong()
c_lib.get_pc_sample_config(
node_id,
config_idx,
method_args,
unit_args,
ctypes.byref(min_interval),
ctypes.byref(max_interval),
)
method = ctypes.cast(method_args, ctypes.c_char_p).value.decode("utf-8")
unit = ctypes.cast(unit_args, ctypes.c_char_p).value.decode("utf-8")
pc_sample_configs.append(
pc_config(method, unit, min_interval.value, max_interval.value)
)
agent_pc_sample_config_map[node_id] = pc_sample_configs
def process_filename(file_path, file_type):
filename = os.environ.get(
"ROCPROF_OUTPUT_FILE_NAME", socket.gethostname() + "/" + str(os.getpid())
)
if os.path.exists(file_path) and os.path.isfile(file_path):
fatal_error("ROCPROFILER_OUTPUT_PATH already exists and is not a directory")
elif not os.path.exists(file_path):
os.makedirs(file_path)
output_filename = ""
if file_type == "derived":
output_filename = filename + "_" + "derived_metrics" + ".csv"
elif file_type == "basic":
output_filename = filename + "_" + "basic_metrics" + ".csv"
elif file_type == "pc_sample_config":
output_filename = filename + "_" + "pc_sample_config" + ".csv"
output_path = os.path.join(file_path, output_filename)
output_path_parent = os.path.dirname(output_path)
if not os.path.exists(output_path_parent):
os.makedirs(output_path_parent)
elif os.path.exists(output_path_parent) and os.path.isfile(output_path_parent):
fatal_error("ROCPROFILER_OUTPUT_PATH already exists and is not a directory")
return output_path
def generate_output(agent_ids):
list_avail_file = os.environ.get("ROCPROF_OUTPUT_LIST_AVAIL_FILE")
if list_avail_file:
file_path = os.environ.get("ROCPROF_OUTPUT_PATH")
derived_output_file = process_filename(file_path, "derived")
basic_output_file = process_filename(file_path, "basic")
pc_sample_config_file = process_filename(file_path, "pc_sample_config")
with open(derived_output_file, "w") as csvfile:
print(f"Opened result file: {derived_output_file}")
fieldnames = ["Agent_Id", "Name", "Description", "Expression", "Dimensions"]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for node_id, counters in agent_derived_counter_map.items():
for counter in counters:
writer.writerow(
{
"Agent_Id": node_id,
"Name": counter.name,
"Description": counter.description,
"Expression": counter.expression,
"Dimensions": counter.dimensions,
}
)
with open(basic_output_file, "w") as csvfile:
print(f"Opened result file: {basic_output_file}")
fieldnames = ["Agent_Id", "Name", "Description", "Block", "Dimensions"]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for node_id, counters in agent_basic_counter_map.items():
for counter in counters:
if counter.block:
writer.writerow(
{
"Agent_Id": node_id,
"Name": counter.name,
"Description": counter.description,
"Block": counter.block,
"Dimensions": counter.dimensions,
}
)
with open(pc_sample_config_file, "w") as csvfile:
print(f"Opened result file: {pc_sample_config_file}")
fieldnames = [
"Agent_Id",
"Method",
"Unit",
"Minimum_Interval",
"Maximum_Interval",
]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
for node_id, configs in agent_pc_sample_config_map.items():
for config in configs:
writer.writerow(
{
"Agent_Id": node_id,
"Method": config.method,
"Unit": config.unit,
"Minimum_Interval": config.min_interval,
"Maximum_Interval": config.max_interval,
}
)
else:
for node_id in agent_ids:
if node_id in agent_basic_counter_map.keys():
basic_counters_stream = io.StringIO()
counters = agent_basic_counter_map[node_id]
for counter in counters:
if counter.block:
basic_counters_stream.write(f"gpu-agent:{node_id}\n")
basic_counters_stream.write("Name:")
basic_counters_stream.write("\t")
basic_counters_stream.write(str(counter.name))
basic_counters_stream.write("\n")
basic_counters_stream.write("Description:")
basic_counters_stream.write("\t")
basic_counters_stream.write(str(counter.description))
basic_counters_stream.write("\n")
basic_counters_stream.write("Block:")
basic_counters_stream.write("\t")
basic_counters_stream.write(str(counter.block))
basic_counters_stream.write("\n")
basic_counters_stream.write("Dimensions:")
basic_counters_stream.write("\t")
basic_counters_stream.write(str(counter.dimensions))
basic_counters_stream.write("\n\n")
basic_counters = basic_counters_stream.getvalue()
print(basic_counters)
basic_counters_stream.close()
if node_id in agent_derived_counter_map.keys():
derived_counters_stream = io.StringIO()
counters = agent_derived_counter_map[node_id]
for counter in counters:
derived_counters_stream.write(f"gpu-agent:{node_id}\n")
derived_counters_stream.write("Name:")
derived_counters_stream.write("\t")
derived_counters_stream.write(str(counter.name))
derived_counters_stream.write("\n")
derived_counters_stream.write("Description:")
derived_counters_stream.write("\t")
derived_counters_stream.write(str(counter.description))
derived_counters_stream.write("\n")
derived_counters_stream.write("Expression:")
derived_counters_stream.write("\t")
derived_counters_stream.write(str(counter.expression))
derived_counters_stream.write("\n")
derived_counters_stream.write("Dimensions:")
derived_counters_stream.write("\t")
derived_counters_stream.write(str(counter.dimensions))
derived_counters_stream.write("\n\n")
derived_counters = derived_counters_stream.getvalue()
print(derived_counters)
derived_counters_stream.close()
if node_id in agent_pc_sample_config_map.keys():
pc_sample_config_stream = io.StringIO()
configs = agent_pc_sample_config_map[node_id]
for config in configs:
pc_sample_config_stream.write("Method:")
pc_sample_config_stream.write("\t")
pc_sample_config_stream.write(str(config.method))
pc_sample_config_stream.write("\n")
pc_sample_config_stream.write("Unit:")
pc_sample_config_stream.write("\t")
pc_sample_config_stream.write(str(config.unit))
pc_sample_config_stream.write("\n")
pc_sample_config_stream.write("Minimum_Interval:")
pc_sample_config_stream.write("\t")
pc_sample_config_stream.write(str(config.min_interval))
pc_sample_config_stream.write("\n")
pc_sample_config_stream.write("Maximum_Interval:")
pc_sample_config_stream.write("\t")
pc_sample_config_stream.write(str(config.max_interval))
pc_sample_config_stream.write("\n")
pc_sample = pc_sample_config_stream.getvalue()
print(
"List available PC Sample Configurations for node_id\t"
+ str(node_id)
+ "\n"
)
print(pc_sample)
print("\n")
pc_sample_config_stream.close()
else:
print("PC Sampling not supported on node_id\t" + str(node_id) + "\n")
if __name__ == "__main__":
# Load the shared library into ctypes
c_lib.avail_tool_init()
no_of_agents = c_lib.get_number_of_agents()
agent_ids = []
for idx in range(0, no_of_agents):
node_id = c_lib.get_agent_node_id(idx)
agent_ids.append(node_id)
get_counters(node_id)
get_pc_sample_configs(node_id)
generate_output(agent_ids)
@@ -0,0 +1,37 @@
# MIT License
#
# Copyright (c) 2023-2025 Advanced Micro Devices, Inc. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
set(PACKAGE_OUTPUT_DIR
${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}/rocprofv3_avail_module)
set(ROCPROFV3_AVAIL_SOURCES __init__.py avail.py)
foreach(_FILE ${ROCPROFV3_AVAIL_SOURCES})
configure_file(${CMAKE_CURRENT_LIST_DIR}/${_FILE} ${PACKAGE_OUTPUT_DIR}/${_FILE}
COPYONLY)
install(
FILES ${PACKAGE_OUTPUT_DIR}/${_FILE}
DESTINATION ${CMAKE_INSTALL_BINDIR}/rocprofv3_avail_module
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE
WORLD_READ WORLD_EXECUTE
COMPONENT tools)
endforeach()
@@ -0,0 +1,23 @@
# MIT License
#
# Copyright (c) 2023-2025 Advanced Micro Devices, Inc. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
from __future__ import absolute_import
@@ -0,0 +1,499 @@
#!/usr/bin/env python3
# MIT License
#
# Copyright (c) 2024-2025 Advanced Micro Devices, Inc. All rights reserved.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import sys
import ctypes
import json
def fatal_error(msg, exit_code=1):
sys.stderr.write(f"Fatal error: {msg}\n")
sys.stderr.flush()
sys.exit(exit_code)
def build_counter_string(obj):
counter_str = "\n".join(
["{:20}: {}".format(key, itr) for key, itr in obj.get_as_dict().items()]
)
counter_str = counter_str + "\n"
for dim in obj.dimensions:
counter_str = counter_str + dim.__str__()
counter_str = counter_str + "\n"
return counter_str
class dimension:
columns = ["dimension_id", "dimension_name", "dimension_instances"]
def __init__(self, dimension_id, dimension_name, dimension_instances):
self.id = dimension_id
self.name = dimension_name
self.instances = dimension_instances
def get_as_dict(self):
return dict(zip((self.columns), [self.id, self.name, self.instances]))
def __str__(self):
dimension = "{:20}: {}\n".format(
"dimension_name", self.get_as_dict()["dimension_name"]
)
dimension += "{:20}: [0:{}]".format(
"dimension_instances", self.get_as_dict()["dimension_instances"]
)
return dimension
class counter:
columns = ["counter_name", "description"]
def __init__(
self,
counter_handle,
counter_name,
counter_description,
counter_dimensions,
is_hw_constant,
):
self.name = counter_name
self.counter_handle = counter_handle
self.description = counter_description
self.dimensions = counter_dimensions
self.is_hw_constant = is_hw_constant
def get_as_dict(self):
return dict(zip((self.columns), [self.name, self.description]))
def __str__(self):
return "\n".join(
["{:20}: {}".format(key, itr) for key, itr in self.get_as_dict().items()]
)
class derived_counter(counter):
columns = ["counter_name", "description", "expression"]
def __init__(
self,
counter_handle,
counter_name,
counter_description,
counter_expression,
counter_dimensions,
is_hw_constant,
):
super().__init__(
counter_handle,
counter_name,
counter_description,
counter_dimensions,
is_hw_constant,
)
self.expression = counter_expression
def get_as_dict(self):
return dict(zip((self.columns), [self.name, self.description, self.expression]))
def __str__(self):
return build_counter_string(self)
class basic_counter(counter):
columns = ["counter_name", "description", "block"]
def __init__(
self,
counter_handle,
counter_name,
counter_description,
counter_block,
counter_dimensions,
is_hw_constant,
):
super().__init__(
counter_handle,
counter_name,
counter_description,
counter_dimensions,
is_hw_constant,
)
self.block = counter_block
def get_as_dict(self):
return dict(zip((self.columns), [self.name, self.description, self.block]))
def __str__(self):
return build_counter_string(self)
class pc_config:
columns = ["method", "unit", "min_interval", "max_interval", "flags"]
def __init__(self, config_method, config_unit, min_interval, max_interval, flags):
self.method = self.get_method_string(config_method.value)
self.unit = self.get_unit_string(config_unit.value)
self.min_interval = min_interval
self.max_interval = max_interval
self.flags = flags
def __str__(self):
return "\n".join(
[
" {:20}:{}".format(
key,
itr if key == "method" or key == "unit" else self.get_value(key, itr),
)
for key, itr in self.get_as_dict().items()
]
)
@staticmethod
def get_value(key, itr):
if key == "min_interval" or key == "max_interval":
return itr.value
elif key == "flags":
if itr.value == 1:
return "interval pow2"
else:
return "none"
else:
fatal_error("Incorrect key")
@staticmethod
def get_method_string(key):
method_map = {1: "stochastic", 2: "host_trap"}
return method_map[key]
@staticmethod
def get_unit_string(key):
unit_map = {1: "instructions", 2: "cycle", 3: "time"}
return unit_map[key]
def get_as_dict(self):
return dict(
zip(
(self.columns),
[
self.method,
self.unit,
self.min_interval,
self.max_interval,
self.flags,
],
)
)
class loadLibrary:
libname = None
c_lib = None
def get_library():
get_library.libname = None
if loadLibrary.c_lib is None:
loadLibrary.c_lib = ctypes.CDLL(loadLibrary.libname)
return loadLibrary.c_lib
def get_string_value(str_ptr):
return ctypes.cast(str_ptr, ctypes.c_char_p).value.decode("utf-8")
def get_agent_info(agent_handle):
lib = get_library()
lib.agent_info.argtypes = [ctypes.c_ulong, ctypes.POINTER(ctypes.c_char_p)]
agent_info_str = ctypes.c_char_p()
lib.agent_info(agent_handle, ctypes.byref(agent_info_str))
return json.loads(agent_info_str.value.decode("utf-8"))
def get_number_of_counters(agent_handle):
lib = get_library()
lib.get_number_of_counters.restype = ctypes.c_ulong
lib.get_number_of_counters.argtypes = [ctypes.c_ulong]
return lib.get_number_of_agent_counters(agent_handle)
def get_number_agents():
lib = get_library()
lib.get_number_of_agents.restype = ctypes.c_ulong
return lib.get_number_of_agents()
def get_agent_handles():
lib = get_library()
num_agents = get_number_agents()
lib.agent_handles.argtypes = [ctypes.c_ulong * num_agents, ctypes.c_ulong]
agent_handles_arr = (ctypes.c_ulong * num_agents)()
lib.agent_handles(agent_handles_arr, num_agents)
return list(agent_handles_arr)
def get_agent_info_map():
agent_info_map = {}
agents = get_agent_handles()
for agent in agents:
agent_info_map[agent] = get_agent_info(agent)
return agent_info_map
def get_number_of_agent_counters(agent_handle):
lib = get_library()
lib.get_number_of_agent_counters.argtypes = [ctypes.c_ulong]
return lib.get_number_of_agent_counters(agent_handle)
def get_agent_counter_handles(agent_handle):
lib = get_library()
num_counters = get_number_of_agent_counters(agent_handle)
lib.agent_counter_handles.argtypes = [
ctypes.c_ulong * num_counters,
ctypes.c_ulong,
ctypes.c_ulong,
]
counter_handles = (ctypes.c_ulong * num_counters)()
lib.agent_counter_handles(counter_handles, agent_handle, num_counters)
return list(counter_handles)
def get_dimensions(counter_handle):
lib = get_library()
lib.get_number_of_dimensions.argtypes = [ctypes.c_ulong]
lib.get_number_of_dimensions.restype = ctypes.c_ulong
num_dims = lib.get_number_of_dimensions(counter_handle)
lib.counter_dimension_ids.argtypes = [
ctypes.c_ulong,
ctypes.c_ulong * num_dims,
ctypes.c_uint,
]
dims_ids = (ctypes.c_ulong * num_dims)()
lib.counter_dimension.argtypes = [
ctypes.c_ulong,
ctypes.c_ulong,
ctypes.POINTER(ctypes.c_char_p),
ctypes.POINTER(ctypes.c_uint),
]
lib.counter_dimension_ids(counter_handle, dims_ids, num_dims)
dimensions = []
for dim_id in list(dims_ids):
dimension_name = ctypes.c_char_p()
dimension_instance = ctypes.c_uint()
lib.counter_dimension(
counter_handle,
dim_id,
ctypes.byref(dimension_name),
ctypes.byref(dimension_instance),
)
dim = dimension(
dim_id, get_string_value(dimension_name), dimension_instance.value
)
dimensions.append(dim)
return dimensions
def get_counters():
agent_counters = {}
agents = get_agent_handles()
agent_counters = {}
agent_info_map = get_agent_info_map()
for agent in agents:
if agent_info_map[agent]["type"] != 2:
continue
agent_counters.setdefault(agent, [])
counters = get_agent_counter_handles(agent)
if counters:
for counter_id in list(counters):
counter_info = get_counter_info(counter_id)
agent_counters[agent].append(counter_info)
return agent_counters
def get_pc_sample_configs():
agent_pc_sample_config = {}
agents = get_agent_handles()
for agent in agents:
configs = get_pc_sample_config(agent)
if len(configs) > 0:
agent_pc_sample_config[agent] = configs
return agent_pc_sample_config
def get_counter_info(counter_handle):
lib = get_library()
lib.counter_info.argtypes = [
ctypes.c_ulong,
ctypes.POINTER(ctypes.c_char_p),
ctypes.POINTER(ctypes.c_char_p),
ctypes.POINTER(ctypes.c_uint),
ctypes.POINTER(ctypes.c_uint),
]
counter_name = ctypes.c_char_p()
counter_description = ctypes.c_char_p()
is_derived = ctypes.c_uint()
is_hw_constant = ctypes.c_uint()
lib.counter_info(
counter_handle,
ctypes.byref(counter_name),
ctypes.byref(counter_description),
ctypes.byref(is_derived),
ctypes.byref(is_hw_constant),
)
if is_derived.value == 1:
lib.counter_expression.argtypes = [
ctypes.c_ulong,
ctypes.POINTER(ctypes.c_char_p),
]
expression = ctypes.c_char_p()
lib.counter_expression(counter_handle, ctypes.byref(expression))
dimensions = get_dimensions(counter_handle)
return derived_counter(
counter_handle,
get_string_value(counter_name),
get_string_value(counter_description),
get_string_value(expression),
dimensions,
is_hw_constant,
)
elif not is_hw_constant.value:
lib.counter_block.argtypes = [ctypes.c_ulong, ctypes.POINTER(ctypes.c_char_p)]
block = ctypes.c_char_p()
lib.counter_block(counter_handle, ctypes.byref(block))
dimensions = get_dimensions(counter_handle)
return basic_counter(
counter_handle,
get_string_value(counter_name),
get_string_value(counter_description),
get_string_value(block),
dimensions,
is_hw_constant,
)
else:
return counter(
counter_handle,
get_string_value(counter_name),
get_string_value(counter_description),
[],
is_hw_constant.value,
)
def get_number_of_pc_sample_configs(agent_handle):
lib = get_library()
lib.get_number_of_pc_sample_configs.argtypes = [ctypes.c_ulong]
lib.get_number_of_pc_sample_configs.restype = ctypes.c_ulong
return lib.get_number_of_pc_sample_configs(agent_handle)
def get_pc_sample_config(agent_handle):
lib = get_library()
num_configs = get_number_of_pc_sample_configs(agent_handle)
lib.pc_sample_config.argtypes = [
ctypes.c_ulong,
ctypes.c_ulong,
ctypes.POINTER(ctypes.c_ulong),
ctypes.POINTER(ctypes.c_ulong),
ctypes.POINTER(ctypes.c_ulong),
ctypes.POINTER(ctypes.c_ulong),
ctypes.POINTER(ctypes.c_ulong),
]
pc_configs = []
for config in range(0, num_configs):
method = (ctypes.c_ulong)()
unit = (ctypes.c_ulong)()
max_interval = (ctypes.c_ulong)()
min_interval = (ctypes.c_ulong)()
flags = (ctypes.c_ulong)()
lib.pc_sample_config(
agent_handle,
config,
ctypes.byref(method),
ctypes.byref(unit),
ctypes.byref(min_interval),
ctypes.byref(max_interval),
flags,
)
pc_configs.append(
pc_config(
method,
unit,
min_interval,
max_interval,
flags,
)
)
return pc_configs
def check_pmc(agent_counter):
lib = get_library()
def get_counter_names(counter_ids):
counter_names = []
for counter_id in counter_ids:
counter = get_counter_info(counter_id)
if counter.counter_handle == counter_id:
counter_names.append(counter.name)
return counter_names
def get_agent_name(agent_id):
agent_info_map = get_agent_info_map()
for agent, info in agent_info_map.items():
if agent == agent_id:
return info["name"]
for agent, counter_ids in agent_counter.items():
num_counters = len(counter_ids)
counters = (ctypes.c_ulong * num_counters)(*counter_ids)
lib.is_counter_set.argtypes = [
ctypes.c_ulong * num_counters,
ctypes.c_ulong,
ctypes.c_ulong,
]
lib.is_counter_set.restype = ctypes.c_bool
if lib.is_counter_set(counters, agent, num_counters) is False:
fatal_error(
"{} not collected on agent {}".format(
" ".join(get_counter_names(counter_ids)), get_agent_name(agent)
)
)
return True