summaryrefslogtreecommitdiffstats
path: root/meta/recipes-kernel/perf
diff options
context:
space:
mode:
authorBruce Ashfield <bruce.ashfield@gmail.com>2021-03-10 20:52:14 -0500
committerRichard Purdie <richard.purdie@linuxfoundation.org>2021-03-12 15:35:08 +0000
commit6bb1621815f41fd09c971a0e26fb4bcd0963ab8a (patch)
tree1dfdf4e199902136d2821277340d23f4643aaa73 /meta/recipes-kernel/perf
parentcca5433bafb2d7efc7d93a43bdf02e4f53534fd5 (diff)
downloadpoky-6bb1621815f41fd09c971a0e26fb4bcd0963ab8a.tar.gz
perf: reproducibility fixes for pmu-events.c
perf generates pmu-events.c as part of the build process. The code that generates the events is doing tree walks and potentially other non-determinstic things. We'd rather not mess with that implementation, so we add a script that knows how to read the pmu-events.c, sort the entries and then copy it over the generated one. With this, we should always have events in the same order, improving reproducibility. (From OE-Core rev: 5281b2a6e16b6d24b66172b8269478356c0ce6c9) Signed-off-by: Bruce Ashfield <bruce.ashfield@gmail.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'meta/recipes-kernel/perf')
-rw-r--r--meta/recipes-kernel/perf/perf.bb16
-rwxr-xr-xmeta/recipes-kernel/perf/perf/sort-pmuevents.py93
2 files changed, 109 insertions, 0 deletions
diff --git a/meta/recipes-kernel/perf/perf.bb b/meta/recipes-kernel/perf/perf.bb
index 2beb404c03..c7653e523c 100644
--- a/meta/recipes-kernel/perf/perf.bb
+++ b/meta/recipes-kernel/perf/perf.bb
@@ -250,6 +250,14 @@ do_configure_prepend () {
250 # all the calls have YFLAGS, which contains prefix mapping information. 250 # all the calls have YFLAGS, which contains prefix mapping information.
251 sed -i -e 's,$(YACC),$(YACC) $(YFLAGS),g' ${S}/scripts/Makefile.host 251 sed -i -e 's,$(YACC),$(YACC) $(YFLAGS),g' ${S}/scripts/Makefile.host
252 fi 252 fi
253 if [ -e "${S}/tools/perf/pmu-events/Build" ]; then
254 target='$(OUTPUT)pmu-events/pmu-events.c $(V)'
255 replacement1='$(OUTPUT)pmu-events/pmu-events.c $(V)\n'
256 replacement2='\t$(srctree)/sort-pmuevents.py $(OUTPUT)pmu-events/pmu-events.c $(OUTPUT)pmu-events/pmu-events.c.new\n'
257 replacement3='\tcp $(OUTPUT)pmu-events/pmu-events.c.new $(OUTPUT)pmu-events/pmu-events.c'
258 sed -i -e "s,$target,$replacement1$replacement2$replacement3,g" \
259 "${S}/tools/perf/pmu-events/Build"
260 fi
253 # end reproducibility substitutions 261 # end reproducibility substitutions
254 262
255 # We need to ensure the --sysroot option in CC is preserved 263 # We need to ensure the --sysroot option in CC is preserved
@@ -292,6 +300,14 @@ do_configure_prepend () {
292 # so we copy it from the sysroot unistd.h to the perf unistd.h 300 # so we copy it from the sysroot unistd.h to the perf unistd.h
293 install -D -m0644 ${STAGING_INCDIR}/asm-generic/unistd.h ${S}/tools/include/uapi/asm-generic/unistd.h 301 install -D -m0644 ${STAGING_INCDIR}/asm-generic/unistd.h ${S}/tools/include/uapi/asm-generic/unistd.h
294 install -D -m0644 ${STAGING_INCDIR}/asm-generic/unistd.h ${S}/include/uapi/asm-generic/unistd.h 302 install -D -m0644 ${STAGING_INCDIR}/asm-generic/unistd.h ${S}/include/uapi/asm-generic/unistd.h
303
304 # the fetcher is inhibited by the 'inherit kernelsrc', so we do a quick check and
305 # copy for a helper script we need
306 for p in $(echo ${FILESPATH} | tr ':' '\n'); do
307 if [ -e $p/sort-pmuevents.py ]; then
308 cp $p/sort-pmuevents.py ${S}
309 fi
310 done
295} 311}
296 312
297python do_package_prepend() { 313python do_package_prepend() {
diff --git a/meta/recipes-kernel/perf/perf/sort-pmuevents.py b/meta/recipes-kernel/perf/perf/sort-pmuevents.py
new file mode 100755
index 0000000000..5ddf0f144f
--- /dev/null
+++ b/meta/recipes-kernel/perf/perf/sort-pmuevents.py
@@ -0,0 +1,93 @@
1#!/usr/bin/env python3
2
3# perf pmu-events sorting tool
4#
5# Copyright (C) 2021 Bruce Ashfield
6#
7# SPDX-License-Identifier: MIT
8#
9
10import sys
11import os
12import re
13from collections import OrderedDict
14
15if len(sys.argv) < 2:
16 print( "[ERROR]: input and output pmu files missing" )
17 sys.exit(1)
18
19if len(sys.argv) < 3:
20 print( "[ERROR]: output pmu file missing" )
21 sys.exit(1)
22
23infile = sys.argv[1]
24outfile = sys.argv[2]
25
26if not os.path.exists(infile):
27 print( "ERROR. input file does not exist: %s" % infile )
28 sys.exit(1)
29
30if os.path.exists(outfile):
31 print( "WARNING. output file will be overwritten: %s" % infile )
32
33with open(infile, 'r') as file:
34 data = file.read()
35
36preamble_regex = re.compile( '^(.*?)^struct', re.MULTILINE | re.DOTALL )
37
38preamble = re.search( preamble_regex, data )
39struct_block_regex = re.compile( '^struct.*?(\w+) (.*?)\[\] = {(.*?)^};', re.MULTILINE | re.DOTALL )
40field_regex = re.compile( '{.*?},', re.MULTILINE | re.DOTALL )
41cpuid_regex = re.compile( '\.cpuid = (.*?),', re.MULTILINE | re.DOTALL )
42name_regex = re.compile( '\.name = (.*?),', re.MULTILINE | re.DOTALL )
43
44# create a dictionary structure to store all the structs, their
45# types and then their fields.
46entry_dict = {}
47for struct in re.findall( struct_block_regex, data ):
48 # print( "struct: %s %s" % (struct[0],struct[1]) )
49 entry_dict[struct[1]] = {}
50 entry_dict[struct[1]]['type'] = struct[0]
51 entry_dict[struct[1]]['fields'] = {}
52 for entry in re.findall( field_regex, struct[2] ):
53 #print( " entry: %s" % entry )
54 cpuid = re.search( cpuid_regex, entry )
55 if cpuid:
56 #print( " cpuid found: %s" % cpuid.group(1) )
57 entry_dict[struct[1]]['fields'][cpuid.group(1)] = entry
58
59 name = re.search( name_regex, entry )
60 if name:
61 #print( " name found: %s" % name.group(1) )
62 entry_dict[struct[1]]['fields'][name.group(1)] = entry
63
64
65# created ordered dictionaries from the captured values. These are ordered by
66# a sorted() iteration of the keys. We don't care about the order we read
67# things, just the sorted order. Hency why we couldn't create these during
68# reading.
69#
70# yes, there's a more concise way to do this, but our nested dictionaries of
71# fields make it complex enough that it becomes unreadable.
72entry_dict_sorted = OrderedDict()
73for i in sorted(entry_dict.keys()):
74 entry_dict_sorted[i] = {}
75 entry_dict_sorted[i]['type'] = entry_dict[i]['type']
76 entry_dict_sorted[i]['fields'] = {}
77 for f in sorted(entry_dict[i]['fields'].keys()):
78 entry_dict_sorted[i]['fields'][f] = entry_dict[i]['fields'][f]
79
80# dump the sorted elements to the outfile
81outf = open( outfile, 'w' )
82
83print( preamble.group(1) )
84outf.write( preamble.group(1) )
85for d in entry_dict_sorted:
86 outf.write( "struct %s %s[] = {\n" % (entry_dict_sorted[d]['type'],d) )
87 for f in entry_dict_sorted[d]['fields']:
88 outf.write( entry_dict_sorted[d]['fields'][f] + '\n' )
89
90 outf.write( "};\n" )
91
92outf.close()
93