Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf_event_open fails on rpi4/32-bit #3508

Closed
kraj opened this issue Mar 16, 2020 · 11 comments
Closed

perf_event_open fails on rpi4/32-bit #3508

kraj opened this issue Mar 16, 2020 · 11 comments

Comments

@kraj
Copy link
Contributor

kraj commented Mar 16, 2020

strace has serveral failures which all lead to one problem that is __NR_perf_event_open syscall fails reliably on rpi4/32-bit kernel. Here is small example which should work fine if PMU was enabled and functioning correctly

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <linux/perf_event.h>
#include <asm/unistd.h>

static long
perf_event_open(struct perf_event_attr *hw_event, pid_t pid,
int cpu, int group_fd, unsigned long flags)
{
int ret;

ret = syscall(__NR_perf_event_open, hw_event, pid, cpu,
group_fd, flags);
return ret;
}

int
main(int argc, char **argv)
{
struct perf_event_attr pe;
long long count;
int fd;

memset(&pe, 0, sizeof(struct perf_event_attr));
pe.type = PERF_TYPE_HARDWARE;
pe.size = sizeof(struct perf_event_attr);
pe.config = PERF_COUNT_HW_INSTRUCTIONS;
pe.disabled = 1;
pe.exclude_kernel = 1;
pe.exclude_hv = 1;

fd = perf_event_open(&pe, 0, -1, -1, 0);
if (fd == -1) {
fprintf(stderr, "Error opening leader %llx\n", pe.config);
exit(EXIT_FAILURE);
}

ioctl(fd, PERF_EVENT_IOC_RESET, 0);
ioctl(fd, PERF_EVENT_IOC_ENABLE, 0);

printf("Measuring instruction count for this printf\n");

ioctl(fd, PERF_EVENT_IOC_DISABLE, 0);
read(fd, &count, sizeof(long long));

printf("Used %lld instructions\n", count);

close(fd);
}

This however returns Error opening leader 1

a working system returns

Measuring instruction count for this printf
Used 76584 instructions

There is no trace of PMU driver in dmesg either, which lets me think that is PMU not
enabled on this kernel somehow. I am using u-boot for bootloader and latest kernel
Linux version 4.19.108-v7l

CONFIG_HW_PERF_EVENTS and CONFIG_PERF_EVENTS are set to 'y' in .config

@pelwell
Copy link
Contributor

pelwell commented Mar 17, 2020

Interesting. Pi 3 works, as does Pi 4 on 64-bit (just add arm_64bit=1 to config.txt).

@kraj
Copy link
Contributor Author

kraj commented Mar 17, 2020

yes with musl based rootfs I am seeing

[    0.338552] hw perfevents: enabled with armv8_cortex_a72 PMU driver, 7 counters available

and the example seems to work ok too. Last time I tested with glibc based rootfs and that's where I had the problem moreover above dmesg was also not there which is puzzling i will build a glibc based rootfs again today and retry

@pelwell
Copy link
Contributor

pelwell commented Mar 17, 2020

I've been writing documentation, but I'm taking a closer look at this now.

@pelwell
Copy link
Contributor

pelwell commented Mar 17, 2020

The problem stems from the fact that ARCH=arm has no driver for the A72 PMU. However, both the A15 and A7 drivers seem to work to my limited testing (running your test app). Can you save me some searching and say which you think should be the fallback?

@pelwell
Copy link
Contributor

pelwell commented Mar 17, 2020

If you can, try building your own updated DTB after applying this patch:

diff --git a/arch/arm/boot/dts/bcm2838.dtsi b/arch/arm/boot/dts/bcm2838.dtsi
index 5f837e8..0f54f05 100644
--- a/arch/arm/boot/dts/bcm2838.dtsi
+++ b/arch/arm/boot/dts/bcm2838.dtsi
@@ -224,7 +224,7 @@
        };
 
        arm-pmu {
-               compatible = "arm,cortex-a72-pmu";
+               compatible = "arm,cortex-a72-pmu", "arm,cortex-a15-pmu";
                interrupts = <GIC_SPI 16 IRQ_TYPE_LEVEL_HIGH>,
                        <GIC_SPI 17 IRQ_TYPE_LEVEL_HIGH>,
                        <GIC_SPI 18 IRQ_TYPE_LEVEL_HIGH>,

Also try with "arm,cortex-a7-pmu" instead.

@pelwell
Copy link
Contributor

pelwell commented Mar 19, 2020

I've gone with the cortex-a15-pmu version, on the grounds that it works better than nothing. I'm willing to change this again if somebody comes up with a better suggestion.

@kraj
Copy link
Contributor Author

kraj commented Mar 19, 2020

@pelwell I am building an image to test this patch out on pi4/32bit today. Will let you know the results on my side

@lategoodbye
Copy link
Contributor

I've gone with the cortex-a15-pmu version, on the grounds that it works better than nothing. I'm willing to change this again if somebody comes up with a better suggestion.

This issue has been discussed during upstreaming the DTS for the BCM2711. The proper solution would be to add arm,cortex-a72-pmu support for arm32, but the motivation of the responsible kernel developer wasn't that high :-(

@kraj
Copy link
Contributor Author

kraj commented Mar 19, 2020

I've gone with the cortex-a15-pmu version, on the grounds that it works better than nothing. I'm willing to change this again if somebody comes up with a better suggestion.

this patch works on my end. So perhaps we can apply it and keep it until its fixed right way upstream

@pelwell
Copy link
Contributor

pelwell commented Mar 19, 2020

It's already applied, and will be ported forward to the newer kernels.

@kraj
Copy link
Contributor Author

kraj commented Mar 19, 2020

fantastic fixed with e99b135

@kraj kraj closed this as completed Mar 19, 2020
popcornmix added a commit to raspberrypi/firmware that referenced this issue Mar 26, 2020
kernel: Reduce noise from rpi poe hat fan
See: raspberrypi/linux#3493

kernel: ARM: dts: bcm271x: Use a53 pmu, drop RPI364
See: raspberrypi/linux#3508

firmware: isp component: rtos_common_mem: Fix smallalloc test in mem_handle_acquire_if_valid

firmware: platform: 2711: Make chicken-bit pip size vary with pmic quantum

firmware: USB device boot for CM4

firmware: arm_loader: Add SET_LAUNCH_VPU1 mailbox message

firmware: il: camera: Add config.txt param awb_auto_is_greyworld for NoIR camera
See: #1167

firmware: arm_loader: Provisional support for high peris

firmware: arm_loader: Only add margins to cmdline if non-zero

firmware: clock: Support clock_measure_pll on pi0-3
firmware: platform: Back to CLOCK_PLL_CHAN_CPER for emmc on pi0-3
popcornmix added a commit to Hexxeh/rpi-firmware that referenced this issue Mar 26, 2020
kernel: Reduce noise from rpi poe hat fan
See: raspberrypi/linux#3493

kernel: ARM: dts: bcm271x: Use a53 pmu, drop RPI364
See: raspberrypi/linux#3508

firmware: isp component: rtos_common_mem: Fix smallalloc test in mem_handle_acquire_if_valid

firmware: platform: 2711: Make chicken-bit pip size vary with pmic quantum

firmware: USB device boot for CM4

firmware: arm_loader: Add SET_LAUNCH_VPU1 mailbox message

firmware: il: camera: Add config.txt param awb_auto_is_greyworld for NoIR camera
See: raspberrypi/firmware#1167

firmware: arm_loader: Provisional support for high peris

firmware: arm_loader: Only add margins to cmdline if non-zero

firmware: clock: Support clock_measure_pll on pi0-3
firmware: platform: Back to CLOCK_PLL_CHAN_CPER for emmc on pi0-3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants