1SUMMARY = "Performance analysis tools for Linux"
2DESCRIPTION = "Performance counters for Linux are a new kernel-based \
3subsystem that provide a framework for all things \
4performance analysis. It covers hardware level \
5(CPU/PMU, Performance Monitoring Unit) features \
6and software features (software counters, tracepoints) \
7as well."
8HOMEPAGE = "https://perf.wiki.kernel.org/index.php/Main_Page"
9
10LICENSE = "GPL-2.0-only"
11
12PR = "r9"
13
14PACKAGECONFIG ??= "scripting tui libunwind libtraceevent"
15PACKAGECONFIG[dwarf] = ",NO_DWARF=1"
16PACKAGECONFIG[scripting] = ",NO_LIBPERL=1 NO_LIBPYTHON=1,perl python3 python3-setuptools-native"
17# gui support was added with kernel 3.6.35
18# since 3.10 libnewt was replaced by slang
19# to cover a wide range of kernel we add both dependencies
20PACKAGECONFIG[tui] = ",NO_NEWT=1,libnewt slang"
21PACKAGECONFIG[libunwind] = ",NO_LIBUNWIND=1 NO_LIBDW_DWARF_UNWIND=1,libunwind"
22PACKAGECONFIG[libnuma] = ",NO_LIBNUMA=1"
23PACKAGECONFIG[bfd] = ",NO_LIBBFD=1"
24PACKAGECONFIG[systemtap] = ",NO_SDT=1,systemtap"
25PACKAGECONFIG[jvmti] = ",NO_JVMTI=1"
26# libaudit support would need scripting to be enabled
27PACKAGECONFIG[audit] = ",NO_LIBAUDIT=1,audit"
28PACKAGECONFIG[manpages] = ",,xmlto-native asciidoc-native"
29PACKAGECONFIG[cap] = ",,libcap"
30PACKAGECONFIG[libtraceevent] = ",NO_LIBTRACEEVENT=1,libtraceevent"
31# Arm CoreSight
32PACKAGECONFIG[coresight] = "CORESIGHT=1,,opencsd"
33
34# libunwind is not yet ported for some architectures
35PACKAGECONFIG:remove:arc = "libunwind"
36PACKAGECONFIG:remove:riscv32 = "libunwind"
37
38DEPENDS = " \
39    virtual/${MLPREFIX}libc \
40    ${MLPREFIX}elfutils \
41    ${MLPREFIX}binutils \
42    bison-native flex-native xz \
43"
44
45do_configure[depends] += "virtual/kernel:do_shared_workdir"
46
47PROVIDES = "virtual/perf"
48
49inherit linux-kernel-base kernel-arch manpages
50
51# needed for building the tools/perf Python bindings
52inherit ${@bb.utils.contains('PACKAGECONFIG', 'scripting', 'python3targetconfig', '', d)}
53inherit python3-dir
54export PYTHON_SITEPACKAGES_DIR
55
56#kernel 3.1+ supports WERROR to disable warnings as errors
57export WERROR = "0"
58
59do_populate_lic[depends] += "virtual/kernel:do_shared_workdir"
60
61# needed for building the tools/perf Perl binding
62include ${@bb.utils.contains('PACKAGECONFIG', 'scripting', 'perf-perl.inc', '', d)}
63
64inherit kernelsrc
65
66S = "${WORKDIR}/${BP}"
67SPDX_S = "${S}/tools/perf"
68
69# The LDFLAGS is required or some old kernels fails due missing
70# symbols and this is preferred than requiring patches to every old
71# supported kernel.
72LDFLAGS="-ldl -lutil"
73
74EXTRA_OEMAKE = '\
75    V=1 \
76    -C ${S}/tools/perf \
77    O=${B} \
78    CROSS_COMPILE=${TARGET_PREFIX} \
79    ARCH=${ARCH} \
80    CC="${CC}" \
81    CCLD="${CC}" \
82    LDSHARED="${CC} -shared" \
83    AR="${AR}" \
84    LD="${LD}" \
85    EXTRA_CFLAGS="-ldw -I${S}" \
86    YFLAGS='-y --file-prefix-map=${WORKDIR}=/usr/src/debug/${PN}/${EXTENDPE}${PV}-${PR}' \
87    EXTRA_LDFLAGS="${PERF_EXTRA_LDFLAGS}" \
88    perfexecdir=${libexecdir} \
89    NO_GTK2=1 \
90    ${PACKAGECONFIG_CONFARGS} \
91    PKG_CONFIG=pkg-config \
92    TMPDIR="${B}" \
93    LIBUNWIND_DIR=${STAGING_EXECPREFIXDIR} \
94'
95
96EXTRA_OEMAKE += "\
97    'DESTDIR=${D}' \
98    'prefix=${prefix}' \
99    'bindir=${bindir}' \
100    'sharedir=${datadir}' \
101    'sysconfdir=${sysconfdir}' \
102    'perfexecdir=${libexecdir}/perf-core' \
103    'ETC_PERFCONFIG=${@os.path.relpath(sysconfdir, prefix)}' \
104    'sharedir=${@os.path.relpath(datadir, prefix)}' \
105    'mandir=${@os.path.relpath(mandir, prefix)}' \
106    'infodir=${@os.path.relpath(infodir, prefix)}' \
107    ${@bb.utils.contains('PACKAGECONFIG', 'scripting', 'PYTHON=python3 PYTHON_CONFIG=python3-config', '', d)} \
108"
109
110# During do_configure, we might run a 'make clean'. That often breaks
111# when done in parallel, so disable parallelism for do_configure. Note
112# that it has to be done this way rather than by passing -j1, since
113# perf's build system by default ignores any -j argument, but does
114# honour a JOBS variable.
115EXTRA_OEMAKE:append:task-configure = " JOBS=1"
116
117PERF_SRC ?= "Makefile \
118             tools/arch \
119             tools/build \
120             tools/include \
121             tools/lib \
122             tools/Makefile \
123             tools/perf \
124             tools/scripts \
125             scripts/ \
126             arch/${ARCH}/Makefile \
127"
128
129PERF_EXTRA_LDFLAGS = ""
130
131# MIPS N32/N64
132PERF_EXTRA_LDFLAGS:mipsarchn32eb = "-m elf32btsmipn32"
133PERF_EXTRA_LDFLAGS:mipsarchn32el = "-m elf32ltsmipn32"
134PERF_EXTRA_LDFLAGS:mipsarchn64eb = "-m elf64btsmip"
135PERF_EXTRA_LDFLAGS:mipsarchn64el = "-m elf64ltsmip"
136
137do_compile() {
138	# Linux kernel build system is expected to do the right thing
139	unset CFLAGS
140        test -e ${S}/tools/lib/traceevent/plugins/Makefile && \
141            sed -i -e 's|\$(libdir)/traceevent/plugins|\$(libdir)/traceevent_${KERNEL_VERSION}/plugins|g' ${S}/tools/lib/traceevent/plugins/Makefile
142	test -e ${S}/tools/perf/Makefile.config && \
143            sed -i -e 's|\$(libdir)/traceevent/plugins|\$(libdir)/traceevent_${KERNEL_VERSION}/plugins|g' ${S}/tools/perf/Makefile.config
144	oe_runmake all
145}
146
147do_install() {
148	# Linux kernel build system is expected to do the right thing
149	unset CFLAGS
150	oe_runmake install
151	# we are checking for this make target to be compatible with older perf versions
152	if ${@bb.utils.contains('PACKAGECONFIG', 'scripting', 'true', 'false', d)} && grep -q install-python_ext ${S}/tools/perf/Makefile*; then
153	    oe_runmake DESTDIR=${D} install-python_ext
154	    if [ -e ${D}${libdir}/python*/site-packages/perf-*/SOURCES.txt ]; then
155		sed -i -e 's#${WORKDIR}##g' ${D}${libdir}/python*/site-packages/perf-*/SOURCES.txt
156	    fi
157	fi
158}
159
160do_configure[prefuncs] += "copy_perf_source_from_kernel"
161python copy_perf_source_from_kernel() {
162    sources = (d.getVar("PERF_SRC") or "").split()
163    src_dir = d.getVar("STAGING_KERNEL_DIR")
164    dest_dir = d.getVar("S")
165    bb.utils.mkdirhier(dest_dir)
166    bb.utils.prunedir(dest_dir)
167    for s in sources:
168        src = oe.path.join(src_dir, s)
169        dest = oe.path.join(dest_dir, s)
170        if not os.path.exists(src):
171            bb.fatal("Path does not exist: %s. Maybe PERF_SRC does not match the kernel version." % src)
172        if os.path.isdir(src):
173            oe.path.copyhardlinktree(src, dest)
174        else:
175            src_path = os.path.dirname(s)
176            os.makedirs(os.path.join(dest_dir,src_path),exist_ok=True)
177            bb.utils.copyfile(src, dest)
178}
179
180do_configure:prepend () {
181    # If building a multlib based perf, the incorrect library path will be
182    # detected by perf, since it triggers via: ifeq ($(ARCH),x86_64). In a 32 bit
183    # build, with a 64 bit multilib, the arch won't match and the detection of a
184    # 64 bit build (and library) are not exected. To ensure that libraries are
185    # installed to the correct location, we can use the weak assignment in the
186    # config/Makefile.
187    #
188    # Also need to relocate .config-detected to $(OUTPUT)/config-detected
189    # for kernel sources that do not already do this
190    # as two builds (e.g. perf and lib32-perf from mutlilib can conflict
191    # with each other if its in the shared source directory
192    #
193    if [ -e "${S}/tools/perf/config/Makefile" ]; then
194        perfconfig="${S}/tools/perf/config/Makefile"
195    fi
196    if [ -e "${S}/tools/perf/Makefile.config" ]; then
197        perfconfig="${S}/tools/perf/Makefile.config"
198    fi
199    if [ -n "${perfconfig}" ]; then
200        # Match $(prefix)/$(lib) and $(prefix)/lib
201        sed -i -e 's,^libdir = \($(prefix)/.*lib\),libdir ?= \1,' \
202               -e 's,^perfexecdir = \(.*\),perfexecdir ?= \1,' \
203               -e 's,\ .config-detected, $(OUTPUT)/config-detected,g' \
204            ${perfconfig}
205    fi
206    # The man pages installation is "$(INSTALL) -d -m 755 $(DESTDIR)$(man1dir)"
207    # in ${S}/tools/perf/Documentation/Makefile, if the mandir set to '?=', it
208    # will use the relative path 'share/man', in the way it will resulting in
209    # incorrect installation for man pages.
210    if [ -e "${S}/tools/perf/Documentation/Makefile" ]; then
211	sed -i 's,^mandir?=,mandir:=,' ${S}/tools/perf/Documentation/Makefile
212    fi
213    if [ -e "${S}/tools/perf/Makefile.perf" ]; then
214        sed -i -e 's,\ .config-detected, $(OUTPUT)/config-detected,g' \
215            ${S}/tools/perf/Makefile.perf
216        sed -i -e "s,prefix='\$(DESTDIR_SQ)/usr'$,prefix='\$(DESTDIR_SQ)/usr' --install-lib='\$(PYTHON_SITEPACKAGES_DIR)' --root='\$(DESTDIR)',g" \
217            ${S}/tools/perf/Makefile.perf
218        # backport https://github.com/torvalds/linux/commit/e4ffd066ff440a57097e9140fa9e16ceef905de8
219        sed -i -e 's,\($(Q)$(SHELL) .$(arch_errno_tbl).\) $(CC) $(arch_errno_hdr_dir),\1 $(firstword $(CC)) $(arch_errno_hdr_dir),g' \
220            ${S}/tools/perf/Makefile.perf
221    fi
222    sed -i -e "s,--root='/\$(DESTDIR_SQ)',--prefix='\$(DESTDIR_SQ)/usr' --install-lib='\$(DESTDIR)\$(PYTHON_SITEPACKAGES_DIR)',g" \
223        ${S}/tools/perf/Makefile*
224
225    if [ -e "${S}/tools/build/Makefile.build" ]; then
226        sed -i -e 's,\ .config-detected, $(OUTPUT)/config-detected,g' \
227            ${S}/tools/build/Makefile.build
228    fi
229
230    # start reproducibility substitutions
231    if [ -e "${S}/tools/perf/Makefile.config" ]; then
232        # The following line in the Makefle:
233        #     override PYTHON := $(call get-executable-or-default,PYTHON,$(PYTHON_AUTO))
234        # "PYTHON" / "PYTHON_AUTO" have the full path as part of the variable. We've
235        # ensure that the environment is setup and we do not need the full path to be
236        # captured, since the symbol gets built into the executable, making it not
237        # reproducible.
238        sed -i -e 's,$(call get-executable-or-default\,PYTHON\,$(PYTHON_AUTO)),$(notdir $(call get-executable-or-default\,PYTHON\,$(PYTHON_AUTO))),g' \
239            ${S}/tools/perf/Makefile.config
240        # The same line is in older releases, but looking explicitly for Python 2
241        sed -i -e 's,$(call get-executable-or-default\,PYTHON\,$(PYTHON2)),$(notdir $(call get-executable-or-default\,PYTHON\,$(PYTHON2))),g' \
242            ${S}/tools/perf/Makefile.config
243
244	# likewise with this substitution. Kernels with commit 18f2967418d031a39
245	# [perf tools: Use Python devtools for version autodetection rather than runtime]
246	# need this substitution for reproducibility.
247	sed -i -e 's,$(call get-executable-or-default\,PYTHON\,$(subst -config\,\,$(PYTHON_AUTO))),$(notdir $(call get-executable-or-default\,PYTHON\,$(subst -config\,\,$(PYTHON_AUTO)))),g' \
248	    ${S}/tools/perf/Makefile.config
249
250        # The following line:
251        #     srcdir_SQ = $(patsubst %tools/perf,tools/perf,$(subst ','\'',$(srcdir))),
252        # Captures the full src path of perf, which of course makes it not
253        # reproducible. We really only need the relative location 'tools/perf', so we
254        # change the Makefile line to remove everything before 'tools/perf'
255        sed -i -e "s%srcdir_SQ = \$(subst ','\\\'',\$(srcdir))%srcdir_SQ = \$(patsubst \%tools/perf,tools/perf,\$(subst ','\\\'',\$(srcdir)))%g" \
256            ${S}/tools/perf/Makefile.config
257        # Avoid hardcoded path to python-native
258        sed -i -e 's#\(PYTHON_WORD := \)$(call shell-wordify,$(PYTHON))#\1 python3#g' \
259            ${S}/tools/perf/Makefile.config
260    fi
261    if [ -e "${S}/tools/perf/tests/Build" ]; then
262        # OUTPUT is the full path, we have python on the path so we remove it from the
263        # definition. This is captured in the perf binary, so breaks reproducibility
264        sed -i -e 's,PYTHONPATH="BUILD_STR($(OUTPUT)python)",PYTHONPATH="BUILD_STR(python)",g' \
265            ${S}/tools/perf/tests/Build
266    fi
267    if [ -e "${S}/tools/perf/util/Build" ]; then
268        # To avoid bison generating #ifdefs that have captured paths, we make sure
269        # all the calls have YFLAGS, which contains prefix mapping information.
270        sed -i -e 's,$(BISON),$(BISON) $(YFLAGS),g' ${S}/tools/perf/util/Build
271    fi
272    if [ -e "${S}/scripts/Makefile.host" ]; then
273        # To avoid yacc (bison) generating #ifdefs that have captured paths, we make sure
274        # all the calls have YFLAGS, which contains prefix mapping information.
275        sed -i -e 's,$(YACC),$(YACC) $(YFLAGS),g' ${S}/scripts/Makefile.host
276    fi
277    if [ -e "${S}/tools/perf/pmu-events/Build" ]; then
278        target='$(OUTPUT)pmu-events/pmu-events.c $(V)'
279        replacement1='$(OUTPUT)pmu-events/pmu-events.c $(V)\n'
280        replacement2='\t$(srctree)/sort-pmuevents.py $(OUTPUT)pmu-events/pmu-events.c $(OUTPUT)pmu-events/pmu-events.c.new\n'
281        replacement3='\tcp $(OUTPUT)pmu-events/pmu-events.c.new $(OUTPUT)pmu-events/pmu-events.c'
282        sed -i -e "s,$target,$replacement1$replacement2$replacement3,g" \
283                       "${S}/tools/perf/pmu-events/Build"
284    fi
285    if [ -e "${S}/tools/perf/pmu-events/jevents.py" ]; then
286        sed -i -e "s#os.scandir(path)#sorted(os.scandir(path), key=lambda e: e.name)#g" \
287                       "${S}/tools/perf/pmu-events/jevents.py"
288    fi
289    if [ -e "${S}/tools/perf/arch/arm64/Makefile" ]; then
290	sed -i 's,sysdef := $(srctree)/,sysdef := ,' ${S}/tools/perf/arch/arm64/Makefile
291	sed -i 's,$(incpath) $(sysdef),$(incpath) $(srctree)/$(sysdef) $(sysdef),' ${S}/tools/perf/arch/arm64/Makefile
292    fi
293    if [ -e "${S}/tools/perf/arch/arm64/entry/syscalls/mksyscalltbl" ]; then
294	if ! grep -q input_rel ${S}/tools/perf/arch/arm64/entry/syscalls/mksyscalltbl; then
295	    sed -i 's,input=$4,input=$4\ninput_rel=$5,' ${S}/tools/perf/arch/arm64/entry/syscalls/mksyscalltbl
296	fi
297	sed -i 's,#include \\"\$input\\",#include \\"\$input_rel\\",'  ${S}/tools/perf/arch/arm64/entry/syscalls/mksyscalltbl
298    fi
299    # end reproducibility substitutions
300
301    # We need to ensure the --sysroot option in CC is preserved
302    if [ -e "${S}/tools/perf/Makefile.perf" ]; then
303        sed -i 's,CC = $(CROSS_COMPILE)gcc,#CC,' ${S}/tools/perf/Makefile.perf
304        sed -i 's,AR = $(CROSS_COMPILE)ar,#AR,' ${S}/tools/perf/Makefile.perf
305        sed -i 's,LD = $(CROSS_COMPILE)ld,#LD,' ${S}/tools/perf/Makefile.perf
306        sed -i 's,PKG_CONFIG = $(CROSS_COMPILE)pkg-config,#PKG_CONFIG,' ${S}/tools/perf/Makefile.perf
307    fi
308    if [ -e "${S}/tools/lib/api/Makefile" ]; then
309        sed -i 's,CC = $(CROSS_COMPILE)gcc,#CC,' ${S}/tools/lib/api/Makefile
310        sed -i 's,AR = $(CROSS_COMPILE)ar,#AR,' ${S}/tools/lib/api/Makefile
311        sed -i 's,LD = $(CROSS_COMPILE)ld,#LD,' ${S}/tools/lib/api/Makefile
312    fi
313    if [ -e "${S}/tools/lib/subcmd/Makefile" ]; then
314        sed -i 's,CC = $(CROSS_COMPILE)gcc,#CC,' ${S}/tools/lib/subcmd/Makefile
315        sed -i 's,AR = $(CROSS_COMPILE)ar,#AR,' ${S}/tools/lib/subcmd/Makefile
316    fi
317    if [ -e "${S}/tools/perf/config/feature-checks/Makefile" ]; then
318        sed -i 's,CC := $(CROSS_COMPILE)gcc -MD,CC += -MD,' ${S}/tools/perf/config/feature-checks/Makefile
319    fi
320    if [ -e "${S}/tools/build/Makefile.feature" ]; then
321        sed -i 's,CFLAGS=,CC="\$(CC)" CFLAGS=,' ${S}/tools/build/Makefile.feature
322    fi
323
324    # 3.17-rc1+ has a include issue for arm/powerpc. Temporarily sed in the appropriate include
325    if [ -e "${S}/tools/perf/arch/$ARCH/util/skip-callchain-idx.c" ]; then
326        sed -i 's,#include "util/callchain.h",#include "util/callchain.h"\n#include "util/debug.h",' ${S}/tools/perf/arch/$ARCH/util/skip-callchain-idx.c
327    fi
328    if [ -e "${S}/tools/perf/arch/arm/util/unwind-libunwind.c" ] && [ -e "${S}/tools/perf/arch/arm/tests/dwarf-unwind.c" ]; then
329        sed -i 's,#include "tests/tests.h",#include "tests/tests.h"\n#include "util/debug.h",' ${S}/tools/perf/arch/arm/tests/dwarf-unwind.c
330        sed -i 's,#include "perf_regs.h",#include "perf_regs.h"\n#include "util/debug.h",' ${S}/tools/perf/arch/arm/util/unwind-libunwind.c
331    fi
332
333    # use /usr/bin/env instead of version specific python
334    for s in `find ${S}/tools/perf/ -name '*.py'` `find ${S}/scripts/ -name 'bpf_helpers_doc.py'`; do
335        sed -i -e "s,#!.*python.*,#!${USRBINPATH}/env python3," ${s}
336    done
337
338    # unistd.h can be out of sync between libc-headers and the captured version in the perf source
339    # so we copy it from the sysroot unistd.h to the perf unistd.h
340    install -D -m0644 ${STAGING_INCDIR}/asm-generic/unistd.h ${S}/tools/include/uapi/asm-generic/unistd.h
341    install -D -m0644 ${STAGING_INCDIR}/asm-generic/unistd.h ${S}/include/uapi/asm-generic/unistd.h
342
343    # the fetcher is inhibited by the 'inherit kernelsrc', so we do a quick check and
344    # copy for a helper script we need
345    for p in $(echo ${FILESPATH} | tr ':' '\n'); do
346	if [ -e $p/sort-pmuevents.py ]; then
347	    cp $p/sort-pmuevents.py ${S}
348	fi
349    done
350}
351
352python do_package:prepend() {
353    d.setVar('PKGV', d.getVar("KERNEL_VERSION").split("-")[0])
354}
355
356PACKAGE_ARCH = "${MACHINE_ARCH}"
357
358
359PACKAGES =+ "${PN}-archive ${PN}-tests ${PN}-perl ${PN}-python"
360
361RDEPENDS:${PN} += "elfutils bash"
362RDEPENDS:${PN}-archive =+ "bash"
363RDEPENDS:${PN}-python =+ "bash python3 python3-modules ${@bb.utils.contains('PACKAGECONFIG', 'audit', 'audit-python', '', d)}"
364RDEPENDS:${PN}-perl =+ "bash perl perl-modules"
365RDEPENDS:${PN}-tests =+ "python3 bash"
366
367RSUGGESTS_SCRIPTING = "${@bb.utils.contains('PACKAGECONFIG', 'scripting', '${PN}-perl ${PN}-python', '',d)}"
368RSUGGESTS:${PN} += "${PN}-archive ${PN}-tests ${RSUGGESTS_SCRIPTING}"
369
370FILES_SOLIBSDEV = ""
371FILES:${PN} += "${libexecdir}/perf-core ${exec_prefix}/libexec/perf-core ${libdir}/traceevent* ${libdir}/libperf-jvmti.so"
372FILES:${PN}-archive = "${libdir}/perf/perf-core/perf-archive"
373FILES:${PN}-tests = "${libdir}/perf/perf-core/tests ${libexecdir}/perf-core/tests"
374FILES:${PN}-python = " \
375                       ${PYTHON_SITEPACKAGES_DIR} \
376                       ${libexecdir}/perf-core/scripts/python \
377                       "
378FILES:${PN}-perl = "${libexecdir}/perf-core/scripts/perl"
379
380DEBUG_OPTIMIZATION:append = " -Wno-error=maybe-uninitialized"
381
382PACKAGESPLITFUNCS =+ "perf_fix_sources"
383
384perf_fix_sources () {
385	for f in util/parse-events-flex.h util/parse-events-flex.c util/pmu-flex.c \
386			util/expr-flex.h util/expr-flex.c; do
387		f=${PKGD}/usr/src/debug/${PN}/${EXTENDPE}${PV}-${PR}/$f
388		if [ -e $f ]; then
389			sed -i -e 's#${S}/##g' $f
390		fi
391	done
392}
393