intel/perf: adapt to platforms like Solaris without d_type in struct dirent
[mesa.git] / src / intel / perf / gen_perf.py
1 # Copyright (c) 2015-2017 Intel Corporation
2 #
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:
9 #
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
12 # Software.
13 #
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
20 # IN THE SOFTWARE.
21
22 import argparse
23 import os
24 import sys
25 import textwrap
26
27 import xml.etree.cElementTree as et
28
29 hashed_funcs = {}
30
31 c_file = None
32 _c_indent = 0
33
34 def c(*args):
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")
39
40 # indented, but no trailing newline...
41 def c_line_start(code):
42 c_file.write(''.rjust(_c_indent) + code)
43 def c_raw(code):
44 c_file.write(code)
45
46 def c_indent(n):
47 global _c_indent
48 _c_indent = _c_indent + n
49 def c_outdent(n):
50 global _c_indent
51 _c_indent = _c_indent - n
52
53 header_file = None
54 _h_indent = 0
55
56 def h(*args):
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")
61
62 def h_indent(n):
63 global _c_indent
64 _h_indent = _h_indent + n
65 def h_outdent(n):
66 global _c_indent
67 _h_indent = _h_indent - n
68
69
70 def emit_fadd(tmp_id, args):
71 c("double tmp{0} = {1} + {2};".format(tmp_id, args[1], args[0]))
72 return tmp_id + 1
73
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))
79 return tmp_id + 3
80
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))
85 return tmp_id + 3
86
87 def emit_fmul(tmp_id, args):
88 c("double tmp{0} = {1} * {2};".format(tmp_id, args[1], args[0]))
89 return tmp_id + 1
90
91 def emit_fsub(tmp_id, args):
92 c("double tmp{0} = {1} - {2};".format(tmp_id, args[1], args[0]))
93 return tmp_id + 1
94
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]))
98 return tmp_id + 1
99
100 def emit_uadd(tmp_id, args):
101 c("uint64_t tmp{0} = {1} + {2};".format(tmp_id, args[1], args[0]))
102 return tmp_id + 1
103
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))
109 return tmp_id + 3
110
111 def emit_umul(tmp_id, args):
112 c("uint64_t tmp{0} = {1} * {2};".format(tmp_id, args[1], args[0]))
113 return tmp_id + 1
114
115 def emit_usub(tmp_id, args):
116 c("uint64_t tmp{0} = {1} - {2};".format(tmp_id, args[1], args[0]))
117 return tmp_id + 1
118
119 def emit_umin(tmp_id, args):
120 c("uint64_t tmp{0} = MIN({1}, {2});".format(tmp_id, args[1], args[0]))
121 return tmp_id + 1
122
123 def emit_lshft(tmp_id, args):
124 c("uint64_t tmp{0} = {1} << {2};".format(tmp_id, args[1], args[0]))
125 return tmp_id + 1
126
127 def emit_rshft(tmp_id, args):
128 c("uint64_t tmp{0} = {1} >> {2};".format(tmp_id, args[1], args[0]))
129 return tmp_id + 1
130
131 def emit_and(tmp_id, args):
132 c("uint64_t tmp{0} = {1} & {2};".format(tmp_id, args[1], args[0]))
133 return tmp_id + 1
134
135 ops = {}
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)
151
152 def brkt(subexp):
153 if " " in subexp:
154 return "(" + subexp + ")"
155 else:
156 return subexp
157
158 def splice_bitwise_and(args):
159 return brkt(args[1]) + " & " + brkt(args[0])
160
161 def splice_logical_and(args):
162 return brkt(args[1]) + " && " + brkt(args[0])
163
164 def splice_ult(args):
165 return brkt(args[1]) + " < " + brkt(args[0])
166
167 def splice_ugte(args):
168 return brkt(args[1]) + " >= " + brkt(args[0])
169
170 exp_ops = {}
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)
176
177
178 hw_vars = {}
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"
192
193 def output_rpn_equation_code(set, counter, equation):
194 c("/* RPN equation: " + equation + " */")
195 tokens = equation.split()
196 stack = []
197 tmp_id = 0
198 tmp = None
199
200 for token in tokens:
201 stack.append(token)
202 while stack and stack[-1] in ops:
203 op = stack.pop()
204 argc, callback = ops[op]
205 args = []
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)"
214 else:
215 raise Exception("Failed to resolve variable " + operand + " in equation " + equation + " for " + set.name + " :: " + counter.get('name'));
216 args.append(operand)
217
218 tmp_id = callback(tmp_id, args)
219
220 tmp = "tmp{0}".format(tmp_id - 1)
221 stack.append(tmp)
222
223 if len(stack) != 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 \"" +
226 equation + "\"")
227
228 value = stack[-1]
229
230 if value in hw_vars:
231 value = hw_vars[value]
232 if value in set.counter_vars:
233 value = set.read_funcs[value[1:]] + "(perf, query, accumulator)"
234
235 c("\nreturn " + value + ";")
236
237 def splice_rpn_expression(set, counter, expression):
238 tokens = expression.split()
239 stack = []
240
241 for token in tokens:
242 stack.append(token)
243 while stack and stack[-1] in exp_ops:
244 op = stack.pop()
245 argc, callback = exp_ops[op]
246 args = []
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]
252 else:
253 raise Exception("Failed to resolve variable " + operand + " in expression " + expression + " for " + set.name + " :: " + counter.get('name'));
254 args.append(operand)
255
256 subexp = callback(args)
257
258 stack.append(subexp)
259
260 if len(stack) != 1:
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 \"" +
263 expression + "\"")
264
265 return stack[-1]
266
267 def output_counter_read(gen, set, counter):
268 c("\n")
269 c("/* {0} :: {1} */".format(set.name, counter.get('name')))
270
271 if counter.read_hash in hashed_funcs:
272 c("#define %s \\" % counter.read_sym)
273 c_indent(3)
274 c("%s" % hashed_funcs[counter.read_hash])
275 c_outdent(3)
276 else:
277 ret_type = counter.get('data_type')
278 if ret_type == "uint64":
279 ret_type = "uint64_t"
280
281 read_eq = counter.get('equation')
282
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)
289
290 c("{")
291 c_indent(3)
292 output_rpn_equation_code(set, counter, read_eq)
293 c_outdent(3)
294 c("}")
295
296 hashed_funcs[counter.read_hash] = counter.read_sym
297
298
299 def output_counter_max(gen, set, counter):
300 max_eq = counter.get('max_equation')
301
302 if not counter.has_max_func():
303 return
304
305 c("\n")
306 c("/* {0} :: {1} */".format(set.name, counter.get('name')))
307
308 if counter.max_hash in hashed_funcs:
309 c("#define %s \\" % counter.max_sym())
310 c_indent(3)
311 c("%s" % hashed_funcs[counter.max_hash])
312 c_outdent(3)
313 else:
314 ret_type = counter.get('data_type')
315 if ret_type == "uint64":
316 ret_type = "uint64_t"
317
318 c("static " + ret_type)
319 c(counter.max_sym() + "(struct gen_perf_config *perf)\n")
320 c("{")
321 c_indent(3)
322 output_rpn_equation_code(set, counter, max_eq)
323 c_outdent(3)
324 c("}")
325
326 hashed_funcs[counter.max_hash] = counter.max_sym()
327
328
329 c_type_sizes = { "uint32_t": 4, "uint64_t": 8, "float": 4, "double": 8, "bool": 4 }
330 def sizeof(c_type):
331 return c_type_sizes[c_type]
332
333 def pot_align(base, pot_alignment):
334 return (base + pot_alignment - 1) & ~(pot_alignment - 1);
335
336 semantic_type_map = {
337 "duration": "raw",
338 "ratio": "event"
339 }
340
341 def output_availability(set, availability, counter_name):
342 expression = splice_rpn_expression(set, counter_name, availability)
343 lines = expression.split(' && ')
344 n_lines = len(lines)
345 if n_lines == 1:
346 c("if (" + lines[0] + ") {")
347 else:
348 c("if (" + lines[0] + " &&")
349 c_indent(4)
350 for i in range(1, (n_lines - 1)):
351 c(lines[i] + " &&")
352 c(lines[(n_lines - 1)] + ") {")
353 c_outdent(4)
354
355
356 def output_counter_report(set, counter, current_offset):
357 data_type = counter.get('data_type')
358 data_type_uc = data_type.upper()
359 c_type = data_type
360
361 if "uint" in c_type:
362 c_type = c_type + "_t"
363
364 semantic_type = counter.get('semantic_type')
365 if semantic_type in semantic_type_map:
366 semantic_type = semantic_type_map[semantic_type]
367
368 semantic_type_uc = semantic_type.upper()
369
370 c("\n")
371
372 availability = counter.get('availability')
373 if availability:
374 output_availability(set, availability, counter.get('name'))
375 c_indent(3)
376
377 c("counter = &query->counters[query->n_counters++];\n")
378 c("counter->oa_counter_read_" + data_type + " = " + set.read_funcs[counter.get('symbol_name')] + ";\n")
379 c("counter->name = \"" + counter.get('name') + "\";\n")
380 c("counter->desc = \"" + counter.get('description') + "\";\n")
381 c("counter->type = GEN_PERF_COUNTER_TYPE_" + semantic_type_uc + ";\n")
382 c("counter->data_type = GEN_PERF_COUNTER_DATA_TYPE_" + data_type_uc + ";\n")
383 c("counter->raw_max = " + set.max_values[counter.get('symbol_name')] + ";\n")
384
385 current_offset = pot_align(current_offset, sizeof(c_type))
386 c("counter->offset = " + str(current_offset) + ";\n")
387
388 if availability:
389 c_outdent(3);
390 c("}")
391
392 return current_offset + sizeof(c_type)
393
394
395 register_types = {
396 'FLEX': 'flex_regs',
397 'NOA': 'mux_regs',
398 'OA': 'b_counter_regs',
399 }
400
401 def compute_register_lengths(set):
402 register_lengths = {}
403 register_configs = set.findall('register_config')
404 for register_config in register_configs:
405 t = register_types[register_config.get('type')]
406 if t not in register_lengths:
407 register_lengths[t] = len(register_config.findall('register'))
408 else:
409 register_lengths[t] += len(register_config.findall('register'))
410
411 return register_lengths
412
413
414 def generate_register_configs(set):
415 register_configs = set.findall('register_config')
416 for register_config in register_configs:
417 t = register_types[register_config.get('type')]
418
419 availability = register_config.get('availability')
420 if availability:
421 output_availability(set, availability, register_config.get('type') + ' register config')
422 c_indent(3)
423
424 for register in register_config.findall('register'):
425 c("query->config.%s[query->config.n_%s++] = (struct gen_perf_query_register_prog) { .reg = %s, .val = %s };" %
426 (t, t, register.get('address'), register.get('value')))
427
428 if availability:
429 c_outdent(3)
430 c("}")
431 c("\n")
432
433
434 # Wraps a <counter> element from the oa-*.xml files.
435 class Counter:
436 def __init__(self, set, xml):
437 self.xml = xml
438 self.set = set
439 self.read_hash = None
440 self.max_hash = None
441
442 self.read_sym = "{0}__{1}__{2}__read".format(self.set.gen.chipset,
443 self.set.underscore_name,
444 self.xml.get('underscore_name'))
445
446 def get(self, prop):
447 return self.xml.get(prop)
448
449 # Compute the hash of a counter's equation by expanding (including all the
450 # sub-equations it depends on)
451 def compute_hashes(self):
452 if self.read_hash is not None:
453 return
454
455 def replace_token(token):
456 if token[0] != "$":
457 return token
458 if token not in self.set.counter_vars:
459 return token
460 self.set.counter_vars[token].compute_hashes()
461 return self.set.counter_vars[token].read_hash
462
463 read_eq = self.xml.get('equation')
464 self.read_hash = ' '.join(map(replace_token, read_eq.split()))
465
466 max_eq = self.xml.get('max_equation')
467 if max_eq:
468 self.max_hash = ' '.join(map(replace_token, max_eq.split()))
469
470 def has_max_func(self):
471 max_eq = self.xml.get('max_equation')
472 if not max_eq:
473 return False
474
475 try:
476 val = float(max_eq)
477 return False
478 except ValueError:
479 pass
480
481 for token in max_eq.split():
482 if token[0] == '$' and token not in hw_vars:
483 return False
484 return True
485
486 def max_sym(self):
487 assert self.has_max_func()
488 return "{0}__{1}__{2}__max".format(self.set.gen.chipset,
489 self.set.underscore_name,
490 self.xml.get('underscore_name'))
491
492 def max_value(self):
493 max_eq = self.xml.get('max_equation')
494 if not max_eq:
495 return "0 /* undefined */"
496
497 try:
498 return "{0}".format(float(max_eq))
499 except ValueError:
500 pass
501
502 for token in max_eq.split():
503 if token[0] == '$' and token not in hw_vars:
504 return "0 /* unsupported (varies over time) */"
505
506 return "{0}__{1}__{2}__max(perf)".format(self.set.gen.chipset,
507 self.set.underscore_name,
508 self.xml.get('underscore_name'))
509
510 # Wraps a <set> element from the oa-*.xml files.
511 class Set:
512 def __init__(self, gen, xml):
513 self.gen = gen
514 self.xml = xml
515
516 self.counter_vars = {}
517 self.max_values = {}
518 self.read_funcs = {}
519
520 xml_counters = self.xml.findall("counter")
521 self.counters = []
522 for xml_counter in xml_counters:
523 counter = Counter(self, xml_counter)
524 self.counters.append(counter)
525 self.counter_vars["$" + counter.get('symbol_name')] = counter
526 self.read_funcs[counter.get('symbol_name')] = counter.read_sym
527 self.max_values[counter.get('symbol_name')] = counter.max_value()
528
529 for counter in self.counters:
530 counter.compute_hashes()
531
532 @property
533 def hw_config_guid(self):
534 return self.xml.get('hw_config_guid')
535
536 @property
537 def name(self):
538 return self.xml.get('name')
539
540 @property
541 def symbol_name(self):
542 return self.xml.get('symbol_name')
543
544 @property
545 def underscore_name(self):
546 return self.xml.get('underscore_name')
547
548 def findall(self, path):
549 return self.xml.findall(path)
550
551 def find(self, path):
552 return self.xml.find(path)
553
554
555 # Wraps an entire oa-*.xml file.
556 class Gen:
557 def __init__(self, filename):
558 self.filename = filename
559 self.xml = et.parse(self.filename)
560 self.chipset = self.xml.find('.//set').get('chipset').lower()
561 self.sets = []
562
563 for xml_set in self.xml.findall(".//set"):
564 self.sets.append(Set(self, xml_set))
565
566
567 def main():
568 global c_file
569 global header_file
570
571 parser = argparse.ArgumentParser()
572 parser.add_argument("--header", help="Header file to write", required=True)
573 parser.add_argument("--code", help="C file to write", required=True)
574 parser.add_argument("xml_files", nargs='+', help="List of xml metrics files to process")
575
576 args = parser.parse_args()
577
578 c_file = open(args.code, 'w')
579 header_file = open(args.header, 'w')
580
581 gens = []
582 for xml_file in args.xml_files:
583 gens.append(Gen(xml_file))
584
585
586 copyright = textwrap.dedent("""\
587 /* Autogenerated file, DO NOT EDIT manually! generated by {}
588 *
589 * Copyright (c) 2015 Intel Corporation
590 *
591 * Permission is hereby granted, free of charge, to any person obtaining a
592 * copy of this software and associated documentation files (the "Software"),
593 * to deal in the Software without restriction, including without limitation
594 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
595 * and/or sell copies of the Software, and to permit persons to whom the
596 * Software is furnished to do so, subject to the following conditions:
597 *
598 * The above copyright notice and this permission notice (including the next
599 * paragraph) shall be included in all copies or substantial portions of the
600 * Software.
601 *
602 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
603 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
604 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
605 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
606 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
607 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
608 * DEALINGS IN THE SOFTWARE.
609 */
610
611 """).format(os.path.basename(__file__))
612
613 h(copyright)
614 h(textwrap.dedent("""\
615 #pragma once
616
617 struct gen_perf_config;
618
619 """))
620
621 c(copyright)
622 c(textwrap.dedent("""\
623 #include <stdint.h>
624 #include <stdbool.h>
625
626 #include <drm-uapi/i915_drm.h>
627
628 #include "util/hash_table.h"
629
630 """))
631
632 c("#include \"" + os.path.basename(args.header) + "\"")
633
634 c(textwrap.dedent("""\
635 #include "perf/gen_perf.h"
636
637
638 #define MIN(a, b) ((a < b) ? (a) : (b))
639 #define MAX(a, b) ((a > b) ? (a) : (b))
640
641
642 """))
643
644 # Print out all equation functions.
645 for gen in gens:
646 for set in gen.sets:
647 for counter in set.counters:
648 output_counter_read(gen, set, counter)
649 output_counter_max(gen, set, counter)
650
651 # Print out all metric sets registration functions for each set in each
652 # generation.
653 for gen in gens:
654 for set in gen.sets:
655 counters = set.counters
656
657 c("\n")
658 register_lengths = compute_register_lengths(set);
659 for reg_type, reg_length in register_lengths.items():
660 c("static struct gen_perf_query_register_prog {0}_{1}_{2}[{3}];".format(gen.chipset,
661 set.underscore_name,
662 reg_type, reg_length))
663
664 c("\nstatic struct gen_perf_query_counter {0}_{1}_query_counters[{2}];\n".format(gen.chipset, set.underscore_name, len(counters)))
665 c("static struct gen_perf_query_info " + gen.chipset + "_" + set.underscore_name + "_query = {\n")
666 c_indent(3)
667
668 c(".kind = GEN_PERF_QUERY_TYPE_OA,\n")
669 c(".name = \"" + set.name + "\",\n")
670 c(".guid = \"" + set.hw_config_guid + "\",\n")
671
672 c(".counters = {0}_{1}_query_counters,".format(gen.chipset, set.underscore_name))
673 c(".n_counters = 0,")
674 c(".oa_metrics_set_id = 0, /* determined at runtime, via sysfs */")
675
676 if gen.chipset == "hsw":
677 c(textwrap.dedent("""\
678 .oa_format = I915_OA_FORMAT_A45_B8_C8,
679
680 /* Accumulation buffer offsets... */
681 .gpu_time_offset = 0,
682 .a_offset = 1,
683 .b_offset = 46,
684 .c_offset = 54,
685 """))
686 else:
687 c(textwrap.dedent("""\
688 .oa_format = I915_OA_FORMAT_A32u40_A4u32_B8_C8,
689
690 /* Accumulation buffer offsets... */
691 .gpu_time_offset = 0,
692 .gpu_clock_offset = 1,
693 .a_offset = 2,
694 .b_offset = 38,
695 .c_offset = 46,
696 """))
697
698 c(".config = {")
699 c_indent(3)
700 for reg_type, reg_length in register_lengths.items():
701 c(".{0} = {1}_{2}_{3},".format(reg_type, gen.chipset, set.underscore_name, reg_type))
702 c(".n_{0} = 0, /* Determined at runtime */".format(reg_type))
703 c_outdent(3)
704 c("},")
705
706 c_outdent(3)
707 c("};\n")
708
709 c("\nstatic void\n")
710 c("{0}_register_{1}_counter_query(struct gen_perf_config *perf)\n".format(gen.chipset, set.underscore_name))
711 c("{\n")
712 c_indent(3)
713
714 c("static struct gen_perf_query_info *query = &" + gen.chipset + "_" + set.underscore_name + "_query;\n")
715 c("struct gen_perf_query_counter *counter;\n")
716
717 c("\n")
718 c("/* Note: we're assuming there can't be any variation in the definition ")
719 c(" * of a query between contexts so it's ok to describe a query within a ")
720 c(" * global variable which only needs to be initialized once... */")
721 c("\nif (!query->data_size) {")
722 c_indent(3)
723
724 generate_register_configs(set)
725
726 offset = 0
727 for counter in counters:
728 offset = output_counter_report(set, counter, offset)
729
730
731 c("\nquery->data_size = counter->offset + gen_perf_query_counter_get_size(counter);\n")
732
733 c_outdent(3)
734 c("}");
735
736 c("\n_mesa_hash_table_insert(perf->oa_metrics_table, query->guid, query);")
737
738 c_outdent(3)
739 c("}\n")
740
741 h("void gen_oa_register_queries_" + gen.chipset + "(struct gen_perf_config *perf);\n")
742
743 c("\nvoid")
744 c("gen_oa_register_queries_" + gen.chipset + "(struct gen_perf_config *perf)")
745 c("{")
746 c_indent(3)
747
748 for set in gen.sets:
749 c("{0}_register_{1}_counter_query(perf);".format(gen.chipset, set.underscore_name))
750
751 c_outdent(3)
752 c("}")
753
754
755 if __name__ == '__main__':
756 main()