Path: blob/21.2-virgl/src/panfrost/perf/pan_gen_perf.py
4560 views
# Copyright © 2021 Collabora, Ltd.1# Author: Antonio Caggiano <[email protected]>23# Permission is hereby granted, free of charge, to any person obtaining a copy4# of this software and associated documentation files (the "Software"), to deal5# in the Software without restriction, including without limitation the rights6# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell7# copies of the Software, and to permit persons to whom the Software is8# furnished to do so, subject to the following conditions:910# The above copyright notice and this permission notice shall be included in11# all copies or substantial portions of the Software.1213# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR14# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,15# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE16# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER17# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,18# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN19# THE SOFTWARE.2021import argparse22import textwrap23import os2425import xml.etree.ElementTree as et262728class SourceFile:29def __init__(self, filename):30self.file = open(filename, 'w')31self._indent = 03233def write(self, *args):34code = ' '.join(map(str,args))35for line in code.splitlines():36text = ''.rjust(self._indent) + line37self.file.write(text.rstrip() + "\n")3839def indent(self, n):40self._indent += n4142def outdent(self, n):43self._indent -= n444546class Counter:47# category Category owning the counter48# xml XML representation of itself49def __init__(self, category, xml):50self.category = category51self.xml = xml52self.name = self.xml.get("name")53self.desc = self.xml.get("description")54self.units = self.xml.get("units")55self.offset = int(self.xml.get("offset"))56self.underscore_name = self.xml.get("counter").lower()575859class Category:60# product Product owning the gategory61# xml XML representation of itself62def __init__(self, product, xml):63self.product = product64self.xml = xml65self.name = self.xml.get("name")66self.underscore_name = self.name.lower().replace(' ', '_')6768xml_counters = self.xml.findall("event")69self.counters = []70for xml_counter in xml_counters:71self.counters.append(Counter(self, xml_counter))727374# Wraps an entire *.xml file.75class Product:76def __init__(self, filename):77self.filename = filename78self.xml = et.parse(self.filename)79self.id = self.xml.getroot().get('id').lower()80self.categories = []8182for xml_cat in self.xml.findall(".//category"):83self.categories.append(Category(self, xml_cat))848586def main():87parser = argparse.ArgumentParser()88parser.add_argument("--header", help="Header file to write", required=True)89parser.add_argument("--code", help="C file to write", required=True)90parser.add_argument("xml_files", nargs='+', help="List of xml metrics files to process")9192args = parser.parse_args()9394c = SourceFile(args.code)95h = SourceFile(args.header)9697prods = []98for xml_file in args.xml_files:99prods.append(Product(xml_file))100101tab_size = 3102103copyright = textwrap.dedent("""\104/* Autogenerated file, DO NOT EDIT manually! generated by {}105*106* Copyright © 2021 Arm Limited107* Copyright © 2021 Collabora Ltd.108*109* Permission is hereby granted, free of charge, to any person obtaining a110* copy of this software and associated documentation files (the "Software"),111* to deal in the Software without restriction, including without limitation112* the rights to use, copy, modify, merge, publish, distribute, sublicense,113* and/or sell copies of the Software, and to permit persons to whom the114* Software is furnished to do so, subject to the following conditions:115*116* The above copyright notice and this permission notice (including the next117* paragraph) shall be included in all copies or substantial portions of the118* Software.119*120* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR121* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,122* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL123* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER124* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING125* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER126* DEALINGS IN THE SOFTWARE.127*/128129""").format(os.path.basename(__file__))130131h.write(copyright)132h.write(textwrap.dedent("""\133#ifndef PAN_PERF_METRICS_H134#define PAN_PERF_METRICS_H135136#include "perf/pan_perf.h"137138"""))139140c.write(copyright)141c.write("#include \"" + os.path.basename(args.header) + "\"")142c.write(textwrap.dedent("""\143144#include <util/macros.h>145"""))146147for prod in prods:148h.write("extern const struct panfrost_perf_config panfrost_perf_config_%s;\n" % prod.id)149150for prod in prods:151c.write(textwrap.dedent("""152static void UNUSED153static_asserts_%s(void)154{155""" % prod.id))156c.indent(tab_size)157158n_categories = len(prod.categories)159c.write("STATIC_ASSERT(%u <= PAN_PERF_MAX_CATEGORIES);" % n_categories)160n_counters = 0161for category in prod.categories:162category_counters_count = len(category.counters)163c.write("STATIC_ASSERT(%u <= PAN_PERF_MAX_COUNTERS);" % category_counters_count)164n_counters += category_counters_count165166c.outdent(tab_size)167c.write("}\n")168169170current_struct_name = "panfrost_perf_config_%s" % prod.id171c.write("\nconst struct panfrost_perf_config %s = {" % current_struct_name)172c.indent(tab_size)173174c.write(".n_categories = %u," % len(prod.categories))175176c.write(".categories = {")177c.indent(tab_size)178179counter_id = 0180181for i in range(0, len(prod.categories)):182category = prod.categories[i]183184c.write("{")185c.indent(tab_size)186c.write(".name = \"%s\"," % (category.name))187c.write(".n_counters = %u," % (len(category.counters)))188c.write(".counters = {")189c.indent(tab_size)190191for j in range(0, len(category.counters)):192counter = category.counters[j]193194assert counter_id < n_counters195c.write("{")196c.indent(tab_size)197198c.write(".name = \"%s\"," % (counter.name))199c.write(".desc = \"%s\"," % (counter.desc.replace("\\", "\\\\")))200c.write(".symbol_name = \"%s\"," % (counter.underscore_name))201c.write(".units = PAN_PERF_COUNTER_UNITS_%s," % (counter.units.upper()))202c.write(".offset = %u," % (counter.offset))203c.write(".category = &%s.categories[%u]," % (current_struct_name, i))204205c.outdent(tab_size)206c.write("}, // counter")207208counter_id += 1209210c.outdent(tab_size)211c.write("}, // counters")212213c.outdent(tab_size)214c.write("}, // category")215216c.outdent(tab_size)217c.write("}, // categories")218219c.outdent(tab_size)220c.write("}; // %s\n" % current_struct_name)221222h.write("\n#endif // PAN_PERF_METRICS_H")223224225if __name__ == '__main__':226main()227228229