Skip to content

arch/x86_64/intel64: Fix the perf events with the HPET timer. - #21965

Open
casaroli wants to merge 2 commits into
apache:masterfrom
casaroli:intel64-perf-freq-up
Open

casaroli wants to merge 2 commits into
apache:masterfrom
casaroli:intel64-perf-freq-up

Conversation

@casaroli

@casaroli casaroli commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

depends-on: /pull/21975

Summary

On x86_64 (intel64), intel64_perf.c gives up_perf_*() from the TSC (rdtscp) and converts with g_x86_64_timer_freq. Only the TSC DEADLINE and APIC timers set this frequency (x86_64_timer_calibrate_freq()). With the HPET timer (CONFIG_ARCH_INTEL64_HPET_ALARM) it is 0: perf_getfreq() returns 0 and up_perf_convert() divides by 0. CONFIG_ARCH_PERF_EVENTS is on by default with SCHED_IRQMONITOR and SCHED_CRITMONITOR, so qemu-intel64:citest_smp (HPET, SCHED_IRQMONITOR) has it. A read of /proc/irqs then divides by 0 in the kernel: in knsh_romfs the system hangs, in the flat nsh the shell ends with SIGFPE. ostest reports Performance frequency = 0 Hz.

The second commit selects ARCH_HAVE_PERF_EVENTS from ARCH_INTEL64_HAVE_TSC (the two TSC timers) instead of from ARCH_X86_64. With the HPET timer, CONFIG_ARCH_PERF_EVENTS is then not available, and drivers/timers/arch_alarm.c gives up_perf_*() from the HPET counter with the HPET frequency, as on other architectures without a cycle counter.

The first commit fixes arch/x86_64/src/intel64/Make.defs, which put #ifdef CONFIG_ARCH_PERF_EVENTS around the line that adds intel64_perf.c. Make reads a line that starts with # as a comment, so the Make build compiled intel64_perf.c in every configuration. CMakeLists.txt already adds the file only with CONFIG_ARCH_PERF_EVENTS. The commit uses ifeq, and removes the CONFIG_ARCH_HAVE_PERF_EVENTS check inside intel64_perf.c, which is then always true.

Impact

x86_64 intel64 only. With the TSC DEADLINE or APIC timer, nothing changes when CONFIG_ARCH_PERF_EVENTS is on; when it is off, the Make build no longer compiles the unused intel64_perf.c, as the CMake build already does. With the HPET timer, CONFIG_ARCH_PERF_EVENTS is not available, and perf events count the HPET. An HPET read is slower than rdtscp; ostest under KVM takes the same time as on master (see Testing). All qemu-intel64 defconfigs give the same make savedefconfig. No API change.

Testing

QEMU 10.2.2 under KVM, GCC 12.3 in the NuttX CI image, make. "HPET" means ARCH_INTEL64_HPET_ALARM and INTEL64_HPET instead of the TSC deadline timer. +mon means SCHED_IRQMONITOR and ARCH_PERF_EVENTS on (on this PR ARCH_PERF_EVENTS is not available with the HPET); the flat nsh also has SCHED_CRITMONITOR.

Config Test master this PR
knsh_romfs (kernel), HPET, +mon cat /proc/irqs no output, the system hangs (divide by zero in a system call) the IRQ table with times
nsh (flat), HPET, +mon cat /proc/irqs no output; the shell gets SIGFPE and ends (ps: a new sh) the IRQ table with times
nsh, HPET, +mon ostest Performance frequency = 0 Hz, WARNING - Performance frequency is invalid, exit 0 exit 0 (ARCH_PERF_EVENTS not available: the perf test of ostest is skipped)
nsh, HPET, SCHED_IRQMONITOR (+ SCHED_CRITMONITOR) ostest time under KVM 2 min 39 s (2 min 38 s) 2 min 37 s (2 min 39 s)
nsh, TSC deadline, +mon cat /proc/irqs, ostest - IRQ table; Performance frequency = 2600000000 Hz, exit 0

All 17 qemu-intel64 defconfigs build (make) with this PR and the other three intel64 fixes applied, and make savedefconfig gives the same defconfig as on master for each.

x86_64 has no protected build. Not built: CMake (no CMake file change; the Kconfig change applies to both). No CI test covers this: CI builds the x86_64 defconfigs but runs none of them.

tools/checkpatch.sh -c -u -m -g passes.

…EVENTS.

Make.defs put "#ifdef CONFIG_ARCH_PERF_EVENTS" around the line that adds
intel64_perf.c.  Make reads a line that starts with "#" as a comment, so
the Make build compiled intel64_perf.c in every configuration.
CMakeLists.txt already adds the file only with CONFIG_ARCH_PERF_EVENTS.
Use ifeq, as for the other optional files.

The CONFIG_ARCH_HAVE_PERF_EVENTS check in intel64_perf.c is then always
true, because CONFIG_ARCH_PERF_EVENTS depends on it: remove it.

[skip-cmake-parity]: CMakeLists.txt already has this condition.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
intel64_perf.c counts the TSC and converts with g_x86_64_timer_freq.
Only the TSC DEADLINE and APIC timers set this frequency.  With the HPET
timer it is 0: perf_getfreq() returns 0 and up_perf_convert() divides by
0.  CONFIG_ARCH_PERF_EVENTS is on by default with SCHED_IRQMONITOR and
SCHED_CRITMONITOR, so qemu-intel64:citest_smp (HPET) has it.

Select ARCH_HAVE_PERF_EVENTS from ARCH_INTEL64_HAVE_TSC, not from
ARCH_X86_64.  With the HPET timer, drivers/timers/arch_alarm.c then gives
up_perf_*() from the HPET counter and its frequency.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <marco.casaroli@gmail.com>
@github-actions github-actions Bot added Arch: x86_64 Issues related to the x86_64 architecture Size: XS The size of the change in this PR is very small labels Nov 17, 2026
@github-actions

Copy link
Copy Markdown

MemBrowse Memory Report · Explore what changed →

No memory changes detected for:


Catch memory regressions in CI with MemBrowse. Free for open source.

@casaroli
casaroli marked this pull request as ready for review November 17, 2026 23:34
@casaroli
casaroli marked this pull request as draft November 18, 2026 00:39
@casaroli
casaroli marked this pull request as ready for review November 18, 2026 00:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Arch: x86_64 Issues related to the x86_64 architecture Size: XS The size of the change in this PR is very small

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants