File: oa-equations-codegen.py

package info (click to toggle)
intel-gpu-tools 2.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 63,360 kB
  • sloc: xml: 781,458; ansic: 360,567; python: 8,336; yacc: 2,781; perl: 1,196; sh: 1,177; lex: 487; asm: 227; lisp: 35; makefile: 30
file content (261 lines) | stat: -rw-r--r-- 7,302 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#!/usr/bin/env python3
#
# SPDX-License-Identifier: MIT
#
# Copyright © 2024 Intel Corporation
#
#
# 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 argparse
import os
import sys
import textwrap

import codegen

h = None
c = None

hashed_funcs = {}

def data_type_to_ctype(ret_type):
    if ret_type == "uint64":
        return "uint64_t"
    elif ret_type == "float":
        return "double"
    else:
        raise Exception("Unhandled case for mapping \"" + ret_type + "\" to a C type")


def output_counter_read(gen, set, counter):
    if counter.read_hash in hashed_funcs:
        return

    c("\n")
    c("/* {0} :: {1} */".format(set.name, counter.get('name')))

    ret_type = counter.get('data_type')
    ret_ctype = data_type_to_ctype(ret_type)
    read_eq = counter.get('equation')

    c(ret_ctype)
    c(counter.read_sym + "(const struct intel_xe_perf *perf,\n")
    c.indent(len(counter.read_sym) + 1)
    c("const struct intel_xe_perf_metric_set *metric_set,\n")
    c("uint64_t *accumulator)\n")
    c.outdent(len(counter.read_sym) + 1)

    c("{")
    c.indent(4)

    gen.output_rpn_equation_code(set, counter, read_eq)

    c.outdent(4)
    c("}")

    hashed_funcs[counter.read_hash] = counter.read_sym


def output_counter_read_definition(gen, set, counter):
    if counter.read_hash in hashed_funcs:
        h("#define %s \\" % counter.read_sym)
        h.indent(4)
        h("%s" % hashed_funcs[counter.read_hash])
        h.outdent(4)
    else:
        ret_type = counter.get('data_type')
        ret_ctype = data_type_to_ctype(ret_type)
        read_eq = counter.get('equation')

        h(ret_ctype)
        h(counter.read_sym + "(const struct intel_xe_perf *perf,\n")
        h.indent(len(counter.read_sym) + 1)
        h("const struct intel_xe_perf_metric_set *metric_set,\n")
        h("uint64_t *accumulator);\n")
        h.outdent(len(counter.read_sym) + 1)

        hashed_funcs[counter.read_hash] = counter.read_sym


def output_counter_max(gen, set, counter):
    max_eq = counter.get('max_equation')

    if not max_eq or max_eq == "100":
        return

    if counter.max_hash in hashed_funcs:
        return

    c("\n")
    c("/* {0} :: {1} */".format(set.name, counter.get('name')))

    ret_type = counter.get('data_type')
    ret_ctype = data_type_to_ctype(ret_type)

    c(ret_ctype)
    c(counter.max_sym + "(const struct intel_xe_perf *perf,\n")
    c.indent(len(counter.max_sym) + 1)
    c("const struct intel_xe_perf_metric_set *metric_set,\n")
    c("uint64_t *accumulator)\n")
    c.outdent(len(counter.max_sym) + 1)

    c("{")
    c.indent(4)

    gen.output_rpn_equation_code(set, counter, max_eq)

    c.outdent(4)
    c("}")

    hashed_funcs[counter.max_hash] = counter.max_sym


def output_counter_max_definition(gen, set, counter):
    max_eq = counter.get('max_equation')

    if not max_eq or max_eq == "100":
        return

    if counter.max_hash in hashed_funcs:
        h("#define %s \\" % counter.max_sym)
        h.indent(4)
        h("%s" % hashed_funcs[counter.max_hash])
        h.outdent(4)
        h("\n")
    else:
        ret_type = counter.get('data_type')
        ret_ctype = data_type_to_ctype(ret_type)

        h(ret_ctype)

        h(counter.max_sym + "(const struct intel_xe_perf *perf,")
        h.indent(len(counter.max_sym) + 1)
        h("const struct intel_xe_perf_metric_set *metric_set,")
        h("uint64_t *accumulator);")
        h.outdent(len(counter.max_sym) + 1)
        h("\n")

        hashed_funcs[counter.max_hash] = counter.max_sym


def generate_equations(args, gens):
    global hashed_funcs

    header_file = os.path.basename(args.header)
    header_define = header_file.replace('.', '_').upper()

    hashed_funcs = {}
    c(textwrap.dedent("""\
        #include <stdlib.h>
        #include <string.h>

        #include "xe/xe_oa.h"
        #include "%s"

        #define MIN(x, y) (((x) < (y)) ? (x) : (y))
        #define MAX(a, b) (((a) > (b)) ? (a) : (b))

        double
        percentage_max_callback_float(const struct intel_xe_perf *perf,
                                      const struct intel_xe_perf_metric_set *metric_set,
                                      uint64_t *accumulator)
        {
           return 100;
        }

        uint64_t
        percentage_max_callback_uint64(const struct intel_xe_perf *perf,
                                       const struct intel_xe_perf_metric_set *metric_set,
                                       uint64_t *accumulator)
        {
           return 100;
        }

        """ % os.path.basename(args.header)))

    # Print out all equation functions.
    for gen in gens:
        for set in gen.sets:
            for counter in set.counters:
                output_counter_read(gen, set, counter)
                output_counter_max(gen, set, counter)

    hashed_funcs = {}
    h(textwrap.dedent("""\
        #ifndef __%s__
        #define __%s__

        #include <stddef.h>
        #include <stdint.h>
        #include <stdbool.h>

        struct intel_xe_perf;
        struct intel_xe_perf_metric_set;

        double
        percentage_max_callback_float(const struct intel_xe_perf *perf,
                                      const struct intel_xe_perf_metric_set *metric_set,
                                      uint64_t *accumulator);
        uint64_t
        percentage_max_callback_uint64(const struct intel_xe_perf *perf,
                                       const struct intel_xe_perf_metric_set *metric_set,
                                       uint64_t *accumulator);

        """ % (header_define, header_define)))

    # Print out all equation functions.
    for gen in gens:
        for set in gen.sets:
            for counter in set.counters:
                output_counter_read_definition(gen, set, counter)
                output_counter_max_definition(gen, set, counter)

    h(textwrap.dedent("""\

        #endif /* __%s__ */
        """ % header_define))


def main():
    global c
    global h

    parser = argparse.ArgumentParser()
    parser.add_argument("--header", help="Header file to write")
    parser.add_argument("--code", help="C file to write")
    parser.add_argument("xml_files", nargs='+', help="List of xml metrics files to process")

    args = parser.parse_args()

    # Note: either arg may == None
    h = codegen.Codegen(args.header)
    c = codegen.Codegen(args.code)

    gens = []
    for xml_file in args.xml_files:
        gens.append(codegen.Gen(xml_file, c))

    copyright = textwrap.dedent("""\
        /* Autogenerated file, DO NOT EDIT manually! generated by {} */
        // SPDX-License-Identifier: MIT
        /*
         * Copyright © 2024 Intel Corporation
         */

        """).format(os.path.basename(__file__))

    h(copyright)
    c(copyright)

    generate_equations(args, gens)


if __name__ == '__main__':
    main()