| 1 |
# Copyright © 2021 Collabora, Ltd. |
| 2 |
# SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
import argparse |
| 5 |
import textwrap |
| 6 |
import os |
| 7 |
|
| 8 |
import xml.etree.ElementTree as et |
| 9 |
|
| 10 |
|
| 11 |
class SourceFile: |
| 12 |
def __init__(self, filename): |
| 13 |
self.file = open(filename, 'w') |
| 14 |
self._indent = 0 |
| 15 |
|
| 16 |
def write(self, *args): |
| 17 |
code = ' '.join(map(str,args)) |
| 18 |
for line in code.splitlines(): |
| 19 |
text = ''.rjust(self._indent) + line |
| 20 |
self.file.write(text.rstrip() + "\n") |
| 21 |
|
| 22 |
def indent(self, n): |
| 23 |
self._indent += n |
| 24 |
|
| 25 |
def outdent(self, n): |
| 26 |
self._indent -= n |
| 27 |
|
| 28 |
|
| 29 |
class Counter: |
| 30 |
# category Category owning the counter |
| 31 |
# xml XML representation of itself |
| 32 |
def __init__(self, category, xml): |
| 33 |
self.category = category |
| 34 |
self.xml = xml |
| 35 |
self.name = self.xml.get("name") |
| 36 |
self.desc = self.xml.get("description") |
| 37 |
self.units = self.xml.get("units") |
| 38 |
self.offset = int(self.xml.get("offset")) |
| 39 |
self.underscore_name = self.xml.get("counter").lower() |
| 40 |
|
| 41 |
|
| 42 |
class Category: |
| 43 |
# product Product owning the gategory |
| 44 |
# xml XML representation of itself |
| 45 |
def __init__(self, product, xml): |
| 46 |
self.product = product |
| 47 |
self.xml = xml |
| 48 |
self.name = self.xml.get("name") |
| 49 |
self.underscore_name = self.name.lower().replace(' ', '_') |
| 50 |
|
| 51 |
xml_counters = self.xml.findall("event") |
| 52 |
self.counters = [] |
| 53 |
for xml_counter in xml_counters: |
| 54 |
self.counters.append(Counter(self, xml_counter)) |
| 55 |
|
| 56 |
|
| 57 |
# Wraps an entire *.xml file. |
| 58 |
class Product: |
| 59 |
def __init__(self, filename): |
| 60 |
self.filename = filename |
| 61 |
self.xml = et.parse(self.filename) |
| 62 |
self.name = self.xml.getroot().get('id') |
| 63 |
self.id = self.name.lower() |
| 64 |
self.categories = [] |
| 65 |
|
| 66 |
for xml_cat in self.xml.findall(".//category"): |
| 67 |
self.categories.append(Category(self, xml_cat)) |
| 68 |
|
| 69 |
|
| 70 |
def main(): |
| 71 |
parser = argparse.ArgumentParser() |
| 72 |
parser.add_argument("--header", help="Header file to write", required=True) |
| 73 |
parser.add_argument("--code", help="C file to write", required=True) |
| 74 |
parser.add_argument("xml_files", nargs='+', help="List of xml metrics files to process") |
| 75 |
|
| 76 |
args = parser.parse_args() |
| 77 |
|
| 78 |
c = SourceFile(args.code) |
| 79 |
h = SourceFile(args.header) |
| 80 |
|
| 81 |
prods = [] |
| 82 |
for xml_file in args.xml_files: |
| 83 |
prods.append(Product(xml_file)) |
| 84 |
|
| 85 |
tab_size = 3 |
| 86 |
|
| 87 |
copyright = textwrap.dedent("""\ |
| 88 |
/* Autogenerated file, DO NOT EDIT manually! generated by {} |
| 89 |
* |
| 90 |
* Copyright © 2021 Arm Limited |
| 91 |
* Copyright © 2021 Collabora Ltd. |
| 92 |
* SPDX-License-Identifier: MIT |
| 93 |
*/ |
| 94 |
|
| 95 |
""").format(os.path.basename(__file__)) |
| 96 |
|
| 97 |
h.write(copyright) |
| 98 |
h.write(textwrap.dedent("""\ |
| 99 |
#ifndef PAN_PERF_METRICS_H |
| 100 |
#define PAN_PERF_METRICS_H |
| 101 |
|
| 102 |
#include "perf/pan_perf.h" |
| 103 |
|
| 104 |
""")) |
| 105 |
|
| 106 |
c.write(copyright) |
| 107 |
c.write("#include \"" + os.path.basename(args.header) + "\"") |
| 108 |
c.write(textwrap.dedent("""\ |
| 109 |
|
| 110 |
#include <util/macros.h> |
| 111 |
""")) |
| 112 |
|
| 113 |
for prod in prods: |
| 114 |
c.write(textwrap.dedent(""" |
| 115 |
static void UNUSED |
| 116 |
static_asserts_%s(void) |
| 117 |
{ |
| 118 |
""" % prod.id)) |
| 119 |
c.indent(tab_size) |
| 120 |
|
| 121 |
n_categories = len(prod.categories) |
| 122 |
c.write("STATIC_ASSERT(%u <= PAN_PERF_MAX_CATEGORIES);" % n_categories) |
| 123 |
n_counters = 0 |
| 124 |
for category in prod.categories: |
| 125 |
category_counters_count = len(category.counters) |
| 126 |
c.write("STATIC_ASSERT(%u <= PAN_PERF_MAX_COUNTERS);" % category_counters_count) |
| 127 |
n_counters += category_counters_count |
| 128 |
|
| 129 |
c.outdent(tab_size) |
| 130 |
c.write("}\n") |
| 131 |
|
| 132 |
|
| 133 |
current_struct_name = "pan_perf_config_%s" % prod.id |
| 134 |
c.write("\nconst struct pan_perf_config %s = {" % current_struct_name) |
| 135 |
c.indent(tab_size) |
| 136 |
|
| 137 |
c.write(".name = \"%s\"," % prod.name) |
| 138 |
c.write(".n_categories = %u," % len(prod.categories)) |
| 139 |
|
| 140 |
c.write(".categories = {") |
| 141 |
c.indent(tab_size) |
| 142 |
|
| 143 |
counter_id = 0 |
| 144 |
|
| 145 |
for i in range(0, len(prod.categories)): |
| 146 |
category = prod.categories[i] |
| 147 |
|
| 148 |
c.write("{") |
| 149 |
c.indent(tab_size) |
| 150 |
c.write(".name = \"%s\"," % (category.name)) |
| 151 |
c.write(".n_counters = %u," % (len(category.counters))) |
| 152 |
c.write(".counters = {") |
| 153 |
c.indent(tab_size) |
| 154 |
|
| 155 |
for j in range(0, len(category.counters)): |
| 156 |
counter = category.counters[j] |
| 157 |
|
| 158 |
assert counter_id < n_counters |
| 159 |
c.write("{") |
| 160 |
c.indent(tab_size) |
| 161 |
|
| 162 |
c.write(".name = \"%s\"," % (counter.name)) |
| 163 |
c.write(".desc = \"%s\"," % (counter.desc.replace("\\", "\\\\"))) |
| 164 |
c.write(".symbol_name = \"%s\"," % (counter.underscore_name)) |
| 165 |
c.write(".units = PAN_PERF_COUNTER_UNITS_%s," % (counter.units.upper())) |
| 166 |
c.write(".offset = %u," % (counter.offset)) |
| 167 |
c.write(".category_index = %u," % i) |
| 168 |
|
| 169 |
c.outdent(tab_size) |
| 170 |
c.write("}, // counter") |
| 171 |
|
| 172 |
counter_id += 1 |
| 173 |
|
| 174 |
c.outdent(tab_size) |
| 175 |
c.write("}, // counters") |
| 176 |
|
| 177 |
c.outdent(tab_size) |
| 178 |
c.write("}, // category") |
| 179 |
|
| 180 |
c.outdent(tab_size) |
| 181 |
c.write("}, // categories") |
| 182 |
|
| 183 |
c.outdent(tab_size) |
| 184 |
c.write("}; // %s\n" % current_struct_name) |
| 185 |
|
| 186 |
h.write("extern const struct pan_perf_config * pan_perf_configs[%u];\n" % len(prods)) |
| 187 |
|
| 188 |
c.write("\nconst struct pan_perf_config * pan_perf_configs[] = {") |
| 189 |
c.indent(tab_size) |
| 190 |
for prod in prods: |
| 191 |
c.write("&pan_perf_config_%s," % prod.id) |
| 192 |
c.outdent(tab_size) |
| 193 |
c.write("};") |
| 194 |
|
| 195 |
h.write("\n#endif // PAN_PERF_METRICS_H") |
| 196 |
|
| 197 |
|
| 198 |
if __name__ == '__main__': |
| 199 |
main() |