Add 'projects/rocprofiler-sdk/' from commit 'bf0fad1d5406fbc51403ba1aa9621a9d4a9bce2b'

git-subtree-dir: projects/rocprofiler-sdk
git-subtree-mainline: 50a90550e9
git-subtree-split: bf0fad1d54
This commit is contained in:
systems-assistant[bot]
2025-07-22 22:52:46 +00:00
کامیت ad0fb25ed5
1272فایلهای تغییر یافته به همراه230117 افزوده شده و 0 حذف شده
@@ -0,0 +1,27 @@
#
#
#
rocprofiler_activate_clang_tidy()
add_subdirectory(rocprofv3_avail_module)
# Adding main rocprofv3
configure_file(rocprofv3.py ${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}/rocprofv3 @ONLY)
install(
FILES ${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}/rocprofv3
DESTINATION ${CMAKE_INSTALL_BINDIR}
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ
WORLD_EXECUTE
COMPONENT tools)
configure_file(rocprofv3-avail.py
${PROJECT_BINARY_DIR}/${CMAKE_INSTALL_BINDIR}/rocprofv3-avail COPYONLY)
install(
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
COMPONENT tools)
@@ -0,0 +1,477 @@
#!/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 get_number_columns(max_name_len):
total_column_width = 120
if sys.stdout.isatty():
total_column_width = os.get_terminal_size().columns
width = total_column_width / (max_name_len + 1)
if width < 1:
return 1
return int(width)
def list_basic_agent(args, list_counters):
def print_agent_counter(counters):
names_len = [len(counter.name) for counter in counters]
names = [
"{name:{width}}".format(name=counter.name, width=max(names_len))
for counter in counters
]
columns = get_number_columns(max(names_len))
print("{:30}:\n".format("PMC"))
for idx in range(0, len(names), columns):
print("{}".format(" ".join(names[idx : (idx + columns)])))
print("\n")
def print_basic_info(info):
print("GPU:{}\n".format(info["logical_node_type_id"]))
print("\n".join(["{:30}:\t{}".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 dict(sorted(agent_info_map.items())).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])
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])
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 dict(sorted(sampling_agents.items())).keys():
info = agent_info_map[agent]
print(
"{:8}:\t{}\n{:8}:\t{}".format(
"GPU", info["logical_node_type_id"], "Name", info["name"]
)
)
print("\n")
def info_pc_sampling(args):
sampling_agents = avail.get_pc_sample_configs()
agent_info_map = avail.get_agent_info_map()
for agent, configs in dict(sorted(sampling_agents.items())).items():
info = agent_info_map[agent]
print(
"{:8}:\t{}\n{:8}:\t{}".format(
"GPU", info["logical_node_type_id"], "Name", info["name"]
)
)
print("{:8}:".format("configs"))
for config in configs:
print(config)
print("\n")
print("\n")
def listing(args):
def print_agent_counter(counters):
names_len = [len(counter.name) for counter in counters]
names = [
"{name:{width}}".format(name=counter.name, width=max(names_len))
for counter in counters
]
columns = get_number_columns(max(names_len))
print("{:30}:\n".format("PMC"))
for idx in range(0, len(names), columns):
print("{:30}".format(" ".join(names[idx : (idx + columns)])))
agent_counters = avail.get_counters()
agent_info_map = avail.get_agent_info_map()
for agent, info in dict(sorted(agent_info_map.items())).items():
if (
info["type"] == 2
and args.device is not None
and info["logical_node_type_id"] == args.device
):
print(
"{:30}:\t{}\n{:30}:\t{}".format(
"GPU", info["logical_node_type_id"], "Name", info["name"]
)
)
print_agent_counter(agent_counters[agent])
print("\n")
break
elif info["type"] == 2 and args.device is None:
print(
"{:30}:\t{}\n{:30}:\t{}".format(
"GPU", info["logical_node_type_id"], "Name", info["name"]
)
)
print_agent_counter(agent_counters[agent])
print("\n")
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 dict(sorted(agent_info_map.items())).items():
if (
info["type"] == 2
and args.device is not None
and info["logical_node_type_id"] == args.device
):
print(
"{}:{}\n{}:{}".format(
"GPU", info["logical_node_type_id"], "Name", info["name"]
)
)
print_pmc_info(args, agent_counters[agent])
break
elif info["type"] == 2 and args.device is None:
print(
"{}:{}\n{}:{}".format(
"GPU", info["logical_node_type_id"], "Name", 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 args.agent is None and args.pc_sampling is None and args.pmc is None:
listing(args)
if args.agent:
list_basic_agent(args, False)
if args.pmc:
listing(args)
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}/lib/rocprofiler-sdk/librocprofv3-list-avail.so"
)
os.environ["ROCPROFILER_METRICS_PATH"] = f"{ROCM_DIR}/share/rocprofiler-sdk"
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)
تفاوت فایلی نمایش داده نمی شود زیرا این فایل بسیار بزرگ است Diff را بارگزاری کن
@@ -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,495 @@
#!/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}:\t{}".format(key, itr) for key, itr in obj.get_as_dict().items()]
)
counter_str += "\n" + "{:20}:\t".format("Dimensions")
counter_str += " ".join(dim.__str__() for dim in obj.dimensions)
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 = "{}[0:{}]".format(
self.get_as_dict()["Dimension_Name"],
self.get_as_dict()["Dimension_Instances"] - 1,
)
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}:\t{}".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}:\t{}".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