我正在嘗試使用 perf 分析具有用戶空間和內核空間代碼的應用程式。我嘗試了啟用各種內核配置的所有其他可能性,但我無法單獨獲得用戶空間/內核空間中的指令/周期數。我嘗試使用 ":u" 和 ":k 擴展指令和周期計數,但我得到的答復是
$ perf stat -e cycles:u,instructions:u ls
Performance counter stats for 'ls':
<not supported> cycles:u
<not supported> instructions:u
0.006047045 seconds time elapsed
0.000000000 seconds user
0.008098000 seconds sys
但是,僅針對回圈/指令運行會給出如下所示的正確結果。
$ perf stat -e cycles,instructions ls
Performance counter stats for 'ls':
5362086 cycles
528783 instructions # 0.10 insn per cycle
0.005487940 seconds time elapsed
0.007800000 seconds user
0.000000000 seconds sys
注意:這里 ls 只是作為一個例子來突出問題。
我正在運行 Linux 5.4 和 perf 版本 5.4.77.g1206eede9156。而且,我在 ARM 板上運行上述命令。下面是我在 Linux 內核中啟用的配置
CONFIG_PERF_EVENTS=y
CONFIG_PROFILING=y
CONFIG_TRACEPOINTS=y
CONFIG_KPROBES=y
CONFIG_OPTPROBES=y
CONFIG_KPROBES_ON_FTRACE=y
CONFIG_UPROBES=y
CONFIG_HAVE_KPROBES=y
CONFIG_HAVE_KRETPROBES=y
CONFIG_HAVE_OPTPROBES=y
CONFIG_HAVE_KPROBES_ON_FTRACE=y
CONFIG_DEBUG_INFO=y
CONFIG_DEBUG_INFO_DWARF4=y
CONFIG_FRAME_POINTER=y
CONFIG_FTRACE=y
CONFIG_KPROBE_EVENTS=y
CONFIG_UPROBE_EVENTS=y
CONFIG_PROBE_EVENTS=y
此外,命令列上的 perf list 列出了硬體/軟體事件等等
$ perf list
branch-instructions OR branches [Hardware event]
branch-misses [Hardware event]
cache-misses [Hardware event]
cache-references [Hardware event]
cpu-cycles OR cycles [Hardware event]
instructions [Hardware event]
alignment-faults [Software event]
bpf-output [Software event]
context-switches OR cs [Software event]
cpu-clock [Software event]
cpu-migrations OR migrations [Software event]
dummy [Software event]
emulation-faults [Software event]
major-faults [Software event]
minor-faults [Software event]
page-faults OR faults [Software event]
task-clock [Software event]
duration_time [Tool event]
L1-dcache-load-misses [Hardware cache event]
L1-dcache-loads [Hardware cache event]
L1-dcache-prefetch-misses [Hardware cache event]
L1-dcache-prefetches [Hardware cache event]
L1-dcache-store-misses [Hardware cache event]
L1-dcache-stores [Hardware cache event]
L1-icache-load-misses [Hardware cache event]
L1-icache-loads [Hardware cache event]
L1-icache-prefetch-misses [Hardware cache event]
L1-icache-prefetches [Hardware cache event]
branch-load-misses [Hardware cache event]
branch-loads [Hardware cache event]
dTLB-load-misses [Hardware cache event]
dTLB-store-misses [Hardware cache event]
iTLB-load-misses [Hardware cache event]
請建議如何解決此問題。我做錯了什么嗎?
uj5u.com熱心網友回復:
為我作業,444,022 cycles:u為perf stat -e cycles:u ls. perf 版本 5.13.g62fb9874f5da,在 Linux 5.12.15-arch1-1 上,在裸機 (x86-64 Skylake) 上,帶有perf_event_paranoid=0.
(使用現代 perf,您還可以使用perf stat --all-user來暗示:u所有事件。)
我猜您的 ARM CPU 的硬體性能計數器不支持使用權限級別的掩碼進行編程,因此perf報告說沒有硬體計數器能夠僅計算用戶空間指令。
AFAIK,在每個中斷入口點都沒有鉤子來啟用/禁用硬體計數器;僅計算內核、僅用戶或兩者,純粹是硬體功能。
硬體支持對于準確計數顯然是必不可少的,因為在軟體實作中,計數器仍然會計數,直到內核代碼運行保存當前計數。(以及在恢復狀態之后,回傳用戶空間之前的內核代碼。)此外,它會使每個中斷和系統呼叫更加昂貴,而不僅僅是通過在任務/執行緒之間的每個背景關系切換中保存/恢復它們來虛擬化性能計數器。因此,即使在沒有硬體支持特權掩碼的 CPU 上,內核也有充分的理由不支持在軟體中進行松散的嘗試。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/319098.html
