1 # Copyright (c) 2015-2017 Intel Corporation
3 # Permission is hereby granted, free of charge, to any person obtaining a
4 # copy of this software and associated documentation files (the "Software"),
5 # to deal in the Software without restriction, including without limitation
6 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
7 # and/or sell copies of the Software, and to permit persons to whom the
8 # Software is furnished to do so, subject to the following conditions:
10 # The above copyright notice and this permission notice (including the next
11 # paragraph) shall be included in all copies or substantial portions of the
14 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
27 import xml
.etree
.ElementTree
as et
35 code
= ' '.join(map(str,args
))
36 for line
in code
.splitlines():
37 text
= ''.rjust(_c_indent
) + line
38 c_file
.write(text
.rstrip() + "\n")
40 # indented, but no trailing newline...
41 def c_line_start(code
):
42 c_file
.write(''.rjust(_c_indent
) + code
)
48 _c_indent
= _c_indent
+ n
51 _c_indent
= _c_indent
- n
57 code
= ' '.join(map(str,args
))
58 for line
in code
.splitlines():
59 text
= ''.rjust(_h_indent
) + line
60 header_file
.write(text
.rstrip() + "\n")
64 _h_indent
= _h_indent
+ n
67 _h_indent
= _h_indent
- n
70 def emit_fadd(tmp_id
, args
):
71 c("double tmp{0} = {1} + {2};".format(tmp_id
, args
[1], args
[0]))
74 # Be careful to check for divide by zero...
75 def emit_fdiv(tmp_id
, args
):
76 c("double tmp{0} = {1};".format(tmp_id
, args
[1]))
77 c("double tmp{0} = {1};".format(tmp_id
+ 1, args
[0]))
78 c("double tmp{0} = tmp{1} ? tmp{2} / tmp{1} : 0;".format(tmp_id
+ 2, tmp_id
+ 1, tmp_id
))
81 def emit_fmax(tmp_id
, args
):
82 c("double tmp{0} = {1};".format(tmp_id
, args
[1]))
83 c("double tmp{0} = {1};".format(tmp_id
+ 1, args
[0]))
84 c("double tmp{0} = MAX(tmp{1}, tmp{2});".format(tmp_id
+ 2, tmp_id
, tmp_id
+ 1))
87 def emit_fmul(tmp_id
, args
):
88 c("double tmp{0} = {1} * {2};".format(tmp_id
, args
[1], args
[0]))
91 def emit_fsub(tmp_id
, args
):
92 c("double tmp{0} = {1} - {2};".format(tmp_id
, args
[1], args
[0]))
95 def emit_read(tmp_id
, args
):
96 type = args
[1].lower()
97 c("uint64_t tmp{0} = accumulator[query->{1}_offset + {2}];".format(tmp_id
, type, args
[0]))
100 def emit_uadd(tmp_id
, args
):
101 c("uint64_t tmp{0} = {1} + {2};".format(tmp_id
, args
[1], args
[0]))
104 # Be careful to check for divide by zero...
105 def emit_udiv(tmp_id
, args
):
106 c("uint64_t tmp{0} = {1};".format(tmp_id
, args
[1]))
107 c("uint64_t tmp{0} = {1};".format(tmp_id
+ 1, args
[0]))
108 c("uint64_t tmp{0} = tmp{1} ? tmp{2} / tmp{1} : 0;".format(tmp_id
+ 2, tmp_id
+ 1, tmp_id
))
111 def emit_umul(tmp_id
, args
):
112 c("uint64_t tmp{0} = {1} * {2};".format(tmp_id
, args
[1], args
[0]))
115 def emit_usub(tmp_id
, args
):
116 c("uint64_t tmp{0} = {1} - {2};".format(tmp_id
, args
[1], args
[0]))
119 def emit_umin(tmp_id
, args
):
120 c("uint64_t tmp{0} = MIN({1}, {2});".format(tmp_id
, args
[1], args
[0]))
123 def emit_lshft(tmp_id
, args
):
124 c("uint64_t tmp{0} = {1} << {2};".format(tmp_id
, args
[1], args
[0]))
127 def emit_rshft(tmp_id
, args
):
128 c("uint64_t tmp{0} = {1} >> {2};".format(tmp_id
, args
[1], args
[0]))
131 def emit_and(tmp_id
, args
):
132 c("uint64_t tmp{0} = {1} & {2};".format(tmp_id
, args
[1], args
[0]))
136 # (n operands, emitter)
137 ops
["FADD"] = (2, emit_fadd
)
138 ops
["FDIV"] = (2, emit_fdiv
)
139 ops
["FMAX"] = (2, emit_fmax
)
140 ops
["FMUL"] = (2, emit_fmul
)
141 ops
["FSUB"] = (2, emit_fsub
)
142 ops
["READ"] = (2, emit_read
)
143 ops
["UADD"] = (2, emit_uadd
)
144 ops
["UDIV"] = (2, emit_udiv
)
145 ops
["UMUL"] = (2, emit_umul
)
146 ops
["USUB"] = (2, emit_usub
)
147 ops
["UMIN"] = (2, emit_umin
)
148 ops
["<<"] = (2, emit_lshft
)
149 ops
[">>"] = (2, emit_rshft
)
150 ops
["AND"] = (2, emit_and
)
154 return "(" + subexp
+ ")"
158 def splice_bitwise_and(args
):
159 return brkt(args
[1]) + " & " + brkt(args
[0])
161 def splice_logical_and(args
):
162 return brkt(args
[1]) + " && " + brkt(args
[0])
164 def splice_ult(args
):
165 return brkt(args
[1]) + " < " + brkt(args
[0])
167 def splice_ugte(args
):
168 return brkt(args
[1]) + " >= " + brkt(args
[0])
171 # (n operands, splicer)
172 exp_ops
["AND"] = (2, splice_bitwise_and
)
173 exp_ops
["UGTE"] = (2, splice_ugte
)
174 exp_ops
["ULT"] = (2, splice_ult
)
175 exp_ops
["&&"] = (2, splice_logical_and
)
179 hw_vars
["$EuCoresTotalCount"] = "perf->sys_vars.n_eus"
180 hw_vars
["$EuSlicesTotalCount"] = "perf->sys_vars.n_eu_slices"
181 hw_vars
["$EuSubslicesTotalCount"] = "perf->sys_vars.n_eu_sub_slices"
182 hw_vars
["$EuThreadsCount"] = "perf->sys_vars.eu_threads_count"
183 hw_vars
["$SliceMask"] = "perf->sys_vars.slice_mask"
184 # subslice_mask is interchangeable with subslice/dual-subslice since Gen12+
185 # only has dual subslices which can be assimilated with 16EUs subslices.
186 hw_vars
["$SubsliceMask"] = "perf->sys_vars.subslice_mask"
187 hw_vars
["$DualSubsliceMask"] = "perf->sys_vars.subslice_mask"
188 hw_vars
["$GpuTimestampFrequency"] = "perf->sys_vars.timestamp_frequency"
189 hw_vars
["$GpuMinFrequency"] = "perf->sys_vars.gt_min_freq"
190 hw_vars
["$GpuMaxFrequency"] = "perf->sys_vars.gt_max_freq"
191 hw_vars
["$SkuRevisionId"] = "perf->sys_vars.revision"
193 def output_rpn_equation_code(set, counter
, equation
):
194 c("/* RPN equation: " + equation
+ " */")
195 tokens
= equation
.split()
202 while stack
and stack
[-1] in ops
:
204 argc
, callback
= ops
[op
]
206 for i
in range(0, argc
):
207 operand
= stack
.pop()
208 if operand
[0] == "$":
209 if operand
in hw_vars
:
210 operand
= hw_vars
[operand
]
211 elif operand
in set.counter_vars
:
212 reference
= set.counter_vars
[operand
]
213 operand
= set.read_funcs
[operand
[1:]] + "(perf, query, accumulator)"
215 raise Exception("Failed to resolve variable " + operand
+ " in equation " + equation
+ " for " + set.name
+ " :: " + counter
.get('name'));
218 tmp_id
= callback(tmp_id
, args
)
220 tmp
= "tmp{0}".format(tmp_id
- 1)
224 raise Exception("Spurious empty rpn code for " + set.name
+ " :: " +
225 counter
.get('name') + ".\nThis is probably due to some unhandled RPN function, in the equation \"" +
231 value
= hw_vars
[value
]
232 if value
in set.counter_vars
:
233 value
= set.read_funcs
[value
[1:]] + "(perf, query, accumulator)"
235 c("\nreturn " + value
+ ";")
237 def splice_rpn_expression(set, counter
, expression
):
238 tokens
= expression
.split()
243 while stack
and stack
[-1] in exp_ops
:
245 argc
, callback
= exp_ops
[op
]
247 for i
in range(0, argc
):
248 operand
= stack
.pop()
249 if operand
[0] == "$":
250 if operand
in hw_vars
:
251 operand
= hw_vars
[operand
]
253 raise Exception("Failed to resolve variable " + operand
+ " in expression " + expression
+ " for " + set.name
+ " :: " + counter
.get('name'));
256 subexp
= callback(args
)
261 raise Exception("Spurious empty rpn expression for " + set.name
+ " :: " +
262 counter
.get('name') + ".\nThis is probably due to some unhandled RPN operation, in the expression \"" +
267 def output_counter_read(gen
, set, counter
):
269 c("/* {0} :: {1} */".format(set.name
, counter
.get('name')))
271 if counter
.read_hash
in hashed_funcs
:
272 c("#define %s \\" % counter
.read_sym
)
274 c("%s" % hashed_funcs
[counter
.read_hash
])
277 ret_type
= counter
.get('data_type')
278 if ret_type
== "uint64":
279 ret_type
= "uint64_t"
281 read_eq
= counter
.get('equation')
283 c("static " + ret_type
)
284 c(counter
.read_sym
+ "(UNUSED struct gen_perf_config *perf,\n")
285 c_indent(len(counter
.read_sym
) + 1)
286 c("const struct gen_perf_query_info *query,\n")
287 c("const uint64_t *accumulator)\n")
288 c_outdent(len(counter
.read_sym
) + 1)
292 output_rpn_equation_code(set, counter
, read_eq
)
296 hashed_funcs
[counter
.read_hash
] = counter
.read_sym
299 def output_counter_max(gen
, set, counter
):
300 max_eq
= counter
.get('max_equation')
302 if not counter
.has_max_func():
306 c("/* {0} :: {1} */".format(set.name
, counter
.get('name')))
308 if counter
.max_hash
in hashed_funcs
:
309 c("#define %s \\" % counter
.max_sym())
311 c("%s" % hashed_funcs
[counter
.max_hash
])
314 ret_type
= counter
.get('data_type')
315 if ret_type
== "uint64":
316 ret_type
= "uint64_t"
318 c("static " + ret_type
)
319 c(counter
.max_sym() + "(struct gen_perf_config *perf)\n")
322 output_rpn_equation_code(set, counter
, max_eq
)
326 hashed_funcs
[counter
.max_hash
] = counter
.max_sym()
329 c_type_sizes
= { "uint32_t": 4, "uint64_t": 8, "float": 4, "double": 8, "bool": 4 }
331 return c_type_sizes
[c_type
]
333 def pot_align(base
, pot_alignment
):
334 return (base
+ pot_alignment
- 1) & ~
(pot_alignment
- 1);
336 semantic_type_map
= {
341 def output_availability(set, availability
, counter_name
):
342 expression
= splice_rpn_expression(set, counter_name
, availability
)
343 lines
= expression
.split(' && ')
346 c("if (" + lines
[0] + ") {")
348 c("if (" + lines
[0] + " &&")
350 for i
in range(1, (n_lines
- 1)):
352 c(lines
[(n_lines
- 1)] + ") {")
356 def output_units(unit
):
357 return unit
.replace(' ', '_').upper()
360 def output_counter_report(set, counter
, current_offset
):
361 data_type
= counter
.get('data_type')
362 data_type_uc
= data_type
.upper()
366 c_type
= c_type
+ "_t"
368 semantic_type
= counter
.get('semantic_type')
369 if semantic_type
in semantic_type_map
:
370 semantic_type
= semantic_type_map
[semantic_type
]
372 semantic_type_uc
= semantic_type
.upper()
376 availability
= counter
.get('availability')
378 output_availability(set, availability
, counter
.get('name'))
381 c("counter = &query->counters[query->n_counters++];\n")
382 c("counter->oa_counter_read_" + data_type
+ " = " + set.read_funcs
[counter
.get('symbol_name')] + ";\n")
383 c("counter->name = \"" + counter
.get('name') + "\";\n")
384 c("counter->desc = \"" + counter
.get('description') + "\";\n")
385 c("counter->symbol_name = \"" + counter
.get('symbol_name') + "\";\n")
386 c("counter->category = \"" + counter
.get('mdapi_group') + "\";\n")
387 c("counter->type = GEN_PERF_COUNTER_TYPE_" + semantic_type_uc
+ ";\n")
388 c("counter->data_type = GEN_PERF_COUNTER_DATA_TYPE_" + data_type_uc
+ ";\n")
389 c("counter->units = GEN_PERF_COUNTER_UNITS_" + output_units(counter
.get('units')) + ";\n")
390 c("counter->raw_max = " + set.max_values
[counter
.get('symbol_name')] + ";\n")
392 current_offset
= pot_align(current_offset
, sizeof(c_type
))
393 c("counter->offset = " + str(current_offset
) + ";\n")
399 return current_offset
+ sizeof(c_type
)
405 'OA': 'b_counter_regs',
408 def compute_register_lengths(set):
409 register_lengths
= {}
410 register_configs
= set.findall('register_config')
411 for register_config
in register_configs
:
412 t
= register_types
[register_config
.get('type')]
413 if t
not in register_lengths
:
414 register_lengths
[t
] = len(register_config
.findall('register'))
416 register_lengths
[t
] += len(register_config
.findall('register'))
418 return register_lengths
421 def generate_register_configs(set):
422 register_configs
= set.findall('register_config')
424 for register_config
in register_configs
:
425 t
= register_types
[register_config
.get('type')]
427 availability
= register_config
.get('availability')
429 output_availability(set, availability
, register_config
.get('type') + ' register config')
432 registers
= register_config
.findall('register')
433 c("static const struct gen_perf_query_register_prog %s[] = {" % t
)
435 for register
in registers
:
436 c("{ .reg = %s, .val = %s }," % (register
.get('address'), register
.get('value')))
439 c("query->config.%s = %s;" % (t
, t
))
440 c("query->config.n_%s = ARRAY_SIZE(%s);" % (t
, t
))
448 # Wraps a <counter> element from the oa-*.xml files.
450 def __init__(self
, set, xml
):
453 self
.read_hash
= None
456 self
.read_sym
= "{0}__{1}__{2}__read".format(self
.set.gen
.chipset
,
457 self
.set.underscore_name
,
458 self
.xml
.get('underscore_name'))
461 return self
.xml
.get(prop
)
463 # Compute the hash of a counter's equation by expanding (including all the
464 # sub-equations it depends on)
465 def compute_hashes(self
):
466 if self
.read_hash
is not None:
469 def replace_token(token
):
472 if token
not in self
.set.counter_vars
:
474 self
.set.counter_vars
[token
].compute_hashes()
475 return self
.set.counter_vars
[token
].read_hash
477 read_eq
= self
.xml
.get('equation')
478 self
.read_hash
= ' '.join(map(replace_token
, read_eq
.split()))
480 max_eq
= self
.xml
.get('max_equation')
482 self
.max_hash
= ' '.join(map(replace_token
, max_eq
.split()))
484 def has_max_func(self
):
485 max_eq
= self
.xml
.get('max_equation')
495 for token
in max_eq
.split():
496 if token
[0] == '$' and token
not in hw_vars
:
501 assert self
.has_max_func()
502 return "{0}__{1}__{2}__max".format(self
.set.gen
.chipset
,
503 self
.set.underscore_name
,
504 self
.xml
.get('underscore_name'))
507 max_eq
= self
.xml
.get('max_equation')
509 return "0 /* undefined */"
512 return "{0}".format(float(max_eq
))
516 for token
in max_eq
.split():
517 if token
[0] == '$' and token
not in hw_vars
:
518 return "0 /* unsupported (varies over time) */"
520 return "{0}__{1}__{2}__max(perf)".format(self
.set.gen
.chipset
,
521 self
.set.underscore_name
,
522 self
.xml
.get('underscore_name'))
524 # Wraps a <set> element from the oa-*.xml files.
526 def __init__(self
, gen
, xml
):
530 self
.counter_vars
= {}
534 xml_counters
= self
.xml
.findall("counter")
536 for xml_counter
in xml_counters
:
537 counter
= Counter(self
, xml_counter
)
538 self
.counters
.append(counter
)
539 self
.counter_vars
["$" + counter
.get('symbol_name')] = counter
540 self
.read_funcs
[counter
.get('symbol_name')] = counter
.read_sym
541 self
.max_values
[counter
.get('symbol_name')] = counter
.max_value()
543 for counter
in self
.counters
:
544 counter
.compute_hashes()
547 def hw_config_guid(self
):
548 return self
.xml
.get('hw_config_guid')
552 return self
.xml
.get('name')
555 def symbol_name(self
):
556 return self
.xml
.get('symbol_name')
559 def underscore_name(self
):
560 return self
.xml
.get('underscore_name')
562 def findall(self
, path
):
563 return self
.xml
.findall(path
)
565 def find(self
, path
):
566 return self
.xml
.find(path
)
569 # Wraps an entire oa-*.xml file.
571 def __init__(self
, filename
):
572 self
.filename
= filename
573 self
.xml
= et
.parse(self
.filename
)
574 self
.chipset
= self
.xml
.find('.//set').get('chipset').lower()
577 for xml_set
in self
.xml
.findall(".//set"):
578 self
.sets
.append(Set(self
, xml_set
))
585 parser
= argparse
.ArgumentParser()
586 parser
.add_argument("--header", help="Header file to write", required
=True)
587 parser
.add_argument("--code", help="C file to write", required
=True)
588 parser
.add_argument("xml_files", nargs
='+', help="List of xml metrics files to process")
590 args
= parser
.parse_args()
592 c_file
= open(args
.code
, 'w')
593 header_file
= open(args
.header
, 'w')
596 for xml_file
in args
.xml_files
:
597 gens
.append(Gen(xml_file
))
600 copyright
= textwrap
.dedent("""\
601 /* Autogenerated file, DO NOT EDIT manually! generated by {}
603 * Copyright (c) 2015 Intel Corporation
605 * Permission is hereby granted, free of charge, to any person obtaining a
606 * copy of this software and associated documentation files (the "Software"),
607 * to deal in the Software without restriction, including without limitation
608 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
609 * and/or sell copies of the Software, and to permit persons to whom the
610 * Software is furnished to do so, subject to the following conditions:
612 * The above copyright notice and this permission notice (including the next
613 * paragraph) shall be included in all copies or substantial portions of the
616 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
617 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
618 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
619 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
620 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
621 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
622 * DEALINGS IN THE SOFTWARE.
625 """).format(os
.path
.basename(__file__
))
628 h(textwrap
.dedent("""\
631 struct gen_perf_config;
636 c(textwrap
.dedent("""\
640 #include <drm-uapi/i915_drm.h>
642 #include "util/hash_table.h"
643 #include "util/ralloc.h"
647 c("#include \"" + os
.path
.basename(args
.header
) + "\"")
649 c(textwrap
.dedent("""\
650 #include "perf/gen_perf.h"
653 #define MIN(a, b) ((a < b) ? (a) : (b))
654 #define MAX(a, b) ((a > b) ? (a) : (b))
659 # Print out all equation functions.
662 for counter
in set.counters
:
663 output_counter_read(gen
, set, counter
)
664 output_counter_max(gen
, set, counter
)
666 # Print out all metric sets registration functions for each set in each
670 counters
= set.counters
674 c("{0}_register_{1}_counter_query(struct gen_perf_config *perf)\n".format(gen
.chipset
, set.underscore_name
))
678 c("struct gen_perf_query_info *query = rzalloc(perf, struct gen_perf_query_info);\n")
680 c("query->kind = GEN_PERF_QUERY_TYPE_OA;\n")
681 c("query->name = \"" + set.name
+ "\";\n")
682 c("query->symbol_name = \"" + set.symbol_name
+ "\";\n")
683 c("query->guid = \"" + set.hw_config_guid
+ "\";\n")
685 c("query->counters = rzalloc_array(query, struct gen_perf_query_counter, %u);" % len(counters
))
686 c("query->n_counters = 0;")
687 c("query->oa_metrics_set_id = 0; /* determined at runtime, via sysfs */")
689 if gen
.chipset
== "hsw":
690 c(textwrap
.dedent("""\
691 query->oa_format = I915_OA_FORMAT_A45_B8_C8;
692 /* Accumulation buffer offsets... */
693 query->gpu_time_offset = 0;
695 query->b_offset = 46;
696 query->c_offset = 54;
699 c(textwrap
.dedent("""\
700 query->oa_format = I915_OA_FORMAT_A32u40_A4u32_B8_C8;
701 /* Accumulation buffer offsets... */
702 query->gpu_time_offset = 0;
703 query->gpu_clock_offset = 1;
705 query->b_offset = 38;
706 query->c_offset = 46;
711 c("struct gen_perf_query_counter *counter = query->counters;\n")
714 c("/* Note: we're assuming there can't be any variation in the definition ")
715 c(" * of a query between contexts so it's ok to describe a query within a ")
716 c(" * global variable which only needs to be initialized once... */")
717 c("\nif (!query->data_size) {")
720 generate_register_configs(set)
723 for counter
in counters
:
724 offset
= output_counter_report(set, counter
, offset
)
727 c("\nquery->data_size = counter->offset + gen_perf_query_counter_get_size(counter);\n")
732 c("\n_mesa_hash_table_insert(perf->oa_metrics_table, query->guid, query);")
737 h("void gen_oa_register_queries_" + gen
.chipset
+ "(struct gen_perf_config *perf);\n")
740 c("gen_oa_register_queries_" + gen
.chipset
+ "(struct gen_perf_config *perf)")
745 c("{0}_register_{1}_counter_query(perf);".format(gen
.chipset
, set.underscore_name
))
751 if __name__
== '__main__':