Files
rocm-systems/source/scripts/convert-counter-collection-yaml-schema.py
T
Welton, Benjamin 33e43e66d3 [SDK] Standardize rocprofiler-sdk counter definition YAML schema (#370)
* Convert YAML Format

Convert YAML format and reader to properly read the YAML.

Comparison between output's from the YAML show only changes in ordering
of architectures (and ids).

* Test fixes

* Add script for converting the YAML schema to source/scripts

* Update documentation

* Change the extra counter code block to YAML

* Add missing new line at EOF

* remove name issues

---------

Co-authored-by: Benjamin Welton <bewelton@amd.com>
Co-authored-by: Jonathan R. Madsen <jonathanrmadsen@gmail.com>
2025-05-14 13:31:51 -05:00

92 lines
2.2 KiB
Python
Executable File

#!/usr/bin/env python3
import sys
import yaml
import argparse
def fatal_error(msg, exit_code=1):
sys.stderr.write(f"Fatal error: {msg}\n")
sys.stderr.flush()
sys.exit(exit_code)
def parse_yaml(yaml_file):
try:
with open(yaml_file, "r") as file:
data = yaml.safe_load(file)
return data
except yaml.YAMLError as exc:
fatal_error(f"{exc}")
def dump_yaml(data, ostream):
yaml.dump(
data,
ostream,
default_flow_style=False,
canonical=False,
indent=1,
width=120,
sort_keys=False,
)
def convert_yaml_schema(inp):
data = {"rocprofiler-sdk": {}}
data["rocprofiler-sdk"]["counters-schema-version"] = inp["schema-version"]
data["rocprofiler-sdk"]["counters"] = []
for key, itr in inp.items():
if key == "schema-version":
pass
else:
entry = {
"name": key,
"description": itr["description"],
"properties": [],
"definitions": [],
}
for arch, aitr in itr["architectures"].items():
definition = {"architectures": sorted(arch.split("/"))}
# expression or block + event
for dkey, ditr in aitr.items():
definition[dkey] = ditr
# add the arch-specific counter definition
entry["definitions"].append(definition)
# add the counter entry
data["rocprofiler-sdk"]["counters"].append(entry)
return data
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-i", "--input", help="Input file", type=str, required=True)
parser.add_argument(
"-o",
"--output",
help="Output file (stdout if not provided)",
type=str,
default=None,
required=False,
)
args = parser.parse_args(sys.argv[1:])
inp = parse_yaml(args.input)
if "rocprofiler-sdk" not in inp.keys():
data = convert_yaml_schema(inp)
else:
data = inp
if args.output is None:
dump_yaml(data, sys.stdout)
else:
with open(args.output, "w") as outfile:
dump_yaml(data, outfile)