Monitor the CPU loading using ebpf to reduce system call overhead

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Monitor the CPU loading using ebpf to reduce system call overhead

ZihengXu
NXP Employee
NXP Employee
0 0 86
A lightweight CPU utilization monitor built with eBPF + libbpf.  
It hooks into the kernel scheduler (sched_switch tracepoint) to measure per-CPU
active time with nanosecond precision — more accurate than polling-based tools like
top, and with lower overhead at high process counts.

How it works

 
Kernel (eBPF)                          User space (C + libbpf)
-----------------------                -------------------------
sched_switch tracepoint                poll BPF maps every 1 s
  -> record on-CPU time per core   ->  compute active % and idle %
  -> accumulate in BPF Array map       print per-core + average
 

The sched_switch tracepoint context structure is defined manually in
cpu_monitor_bpf.c.

Output


The monitor displays per-core active/idle percentages, with the idle column
matching top's id field for easy comparison:
 
CPU Core   | Active %   | Idle % (=top id)
-------------------------------------------
CPU 0      |     0.34%  |         99.66%
CPU 1      |    21.89%  |         78.11%
CPU 2      |     0.15%  |         99.85%
CPU 3      |     0.03%  |         99.97%
-------------------------------------------
TOTAL AVG  |     5.60%  |         94.40%
 
To compare with top, press 1 in top to show per-core stats, then compare
the id column with the Idle % column above.

Prerequisites

Host (build machine)

Package Purpose
clang + llvm version 21 Compile BPF C source to BPF ELF
bpftool Generate BFP skeleton headers
aarch64 Poky Toolchain Corss-compile the user-space binary
 

Install on Ubuntu/Debian:
wget https://apt.llvm.org/llvm.sh
chmod +x llvm.sh
./llvm.sh 21
apt update
apt install -y llvm-21 clang-21 lld-21 lldb-21
update-alternatives --install /usr/bin/llvm-link llvm-link /usr/bin/llvm-link-21 100
update-alternatives --install /usr/bin/clang clang /usr/bin/clang-21 100
update-alternatives --install /usr/bin/llc llc /usr/bin/llc-21 100
llvm-link --version
clang --version

# install bpftool
apt install linux-tools-$(uname -r)

Target board

  • Tested Linux kernel == 6.18.20-2.0.0
  • Tested image == imx-image-full
  • Most BPF configurations are enabled by default. In addition, you need CONFIG_FTRACE=y to enable tracepoint support (required for the sched_switch hook).

Build

Build the project on your host machine:
TOOLCHAIN_PATH=/opt/fsl-imx-internal-wayland/6.18-whinlatter/environment-setup-armv8a-poky-linux ./build_and_deploy.sh
 

The script will:
  1. Source the Poky toolchain environment
  2. Run make clean && make all (compiles BPF object, generates skeleton,
  3. cross-compiles user-space binary)
  4. Install the binary to ./board_deploy/cpu_monitor

Deploy and run

 
# Copy binary to the board
scp board_deploy/cpu_monitor root@<board-ip>:/usr/bin/

# Run on the board (root required for eBPF)
ssh root@<board-ip>

./cpu_monitor
 
Press Ctrl+C to exit.
 

Benchmarking: eBPF vs top


A scheduler stress test script is included to compare monitoring overhead
between cpu_monitor and top under high scheduling pressure.

Run the stress test


On the target board, open three terminals:

 
# Terminal 1: start scheduler pressure (200 threads, 30 seconds)
./stress_sched.sh -t 200 -d 30

# Terminal 2: measure eBPF monitoring overhead
perf stat -e cpu-clock,task-clock,context-switches,cpu-migrations,instructions timeout -s KILL 10 ./cpu_monitor

# Terminal 3: measure top monitoring overhead
perf stat -e cpu-clock,task-clock,context-switches,cpu-migrations,instructions top -b -d 1 -n 10

Compare task-clock, instructions, and context-switches from perf stat
output. Try different thread counts to observe scaling behavior:
 
./stress_sched.sh -t 50 -d 30     # low pressure
./stress_sched.sh -t 200 -d 30    # medium pressure
./stress_sched.sh -t 500 -d 30    # high pressure

Result

Test environment: i.MX943 (4x Cortex-A55), Linux 6.18.20, 10-second measurement window.

Comparison results in free system load:
Indicator eBPF cpu_monitor top delta
task-clock (CPU timing) 12.0 ms 263.9 ms 22x
CPU usage 0.1% 2.8% 28x
instructions 3.38M 197M 58x
context-switches 12 17  
sys time (kernel time) 5.0ms 199.5ms 40x
user time 0 62.1ms  
 

Comparison results in high scheduling pressure (200 threads):
Indicator eBPF cpu_monitor top delta
task-clock (CPU timing) 12.6ms 690ms 54x
CPU usage 0.1% 6.1% 61x
instructions 4.8M 538M 112x
context-switches 122 9019 75x
sys time (kernel time) 4ms 461ms 115x
user time 0 206ms  
 

Project structure

ebpf_cpu_usage/
├── cpu_monitor_bpf.c      # Kernel-side eBPF program (C, compiled to BPF)
├── cpu_monitor.c          # User-space program (C, cross-compiled to aarch64)
├── Makefile               # Build rules
├── build_and_deploy.sh    # One-shot build + package script
├── stress_sched.sh        # Scheduler stress test for benchmarking
└── README.md
 
%3CLINGO-SUB%20id%3D%22lingo-sub-2394283%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3EMonitor%20the%20CPU%20loading%20using%20ebpf%20to%20reduce%20system%20call%20overhead%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2394283%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CDIV%3E%0A%3CDIV%3E%3CSPAN%3EA%20lightweight%20CPU%20utilization%20monitor%20built%20with%20eBPF%20%2B%20libbpf.%20%26nbsp%3B%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CDIV%3E%3CSPAN%3EIt%20hooks%20into%20the%20kernel%20scheduler%20(%3C%2FSPAN%3E%3CEM%3Esched_switch%3C%2FEM%3E%3CSPAN%3E%20tracepoint)%20to%20measure%20per-CPU%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CDIV%3E%3CSPAN%3Eactive%20time%20with%20nanosecond%20precision%20%E2%80%94%20more%20accurate%20than%20polling-based%20tools%20like%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CDIV%3E%3CEM%3Etop%3C%2FEM%3E%3CSPAN%3E%2C%20and%20with%20lower%20overhead%20at%20high%20process%20counts.%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CBR%20%2F%3E%0A%3CH2%20id%3D%22toc-hId--463271375%22%20id%3D%22toc-hId--463267588%22%3E%3CSPAN%3EHow%20it%20works%3C%2FSPAN%3E%3C%2FH2%3E%0A%3C%2FDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3EKernel%20(eBPF)%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20User%20space%20(C%20%2B%20libbpf)%0A-----------------------%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20-------------------------%0Asched_switch%20tracepoint%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20poll%20BPF%20maps%20every%201%20s%0A%20%20-%26gt%3B%20record%20on-CPU%20time%20per%20core%20%20%20-%26gt%3B%20%20compute%20active%20%25%20and%20idle%20%25%0A%20%20-%26gt%3B%20accumulate%20in%20BPF%20Array%20map%20%20%20%20%20%20%20print%20per-core%20%2B%20average%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FDIV%3E%0A%3CDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CBR%20%2F%3E%0A%3CDIV%3E%3CSPAN%3EThe%20%3C%2FSPAN%3E%3CEM%3Esched_switch%3C%2FEM%3E%3CSPAN%3E%26nbsp%3Btracepoint%20context%20structure%20is%20defined%20manually%20in%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CDIV%3E%3CEM%3Ecpu_monitor_bpf.c.%3C%2FEM%3E%3C%2FDIV%3E%0A%3CBR%20%2F%3E%0A%3CH2%20id%3D%22toc-hId-2024241458%22%20id%3D%22toc-hId-2024245245%22%3E%3CSPAN%3EOutput%3C%2FSPAN%3E%3C%2FH2%3E%0A%3CBR%20%2F%3E%0A%3CDIV%3E%3CSPAN%3EThe%20monitor%20displays%20per-core%20active%2Fidle%20percentages%2C%20with%20the%20idle%20column%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CDIV%3E%3CSPAN%3Ematching%20%3C%2FSPAN%3E%3CEM%3Etop%3C%2FEM%3E%3CSPAN%3E's%20%3C%2FSPAN%3E%3CEM%3Eid%3C%2FEM%3E%3CSPAN%3E%26nbsp%3Bfield%20for%20easy%20comparison%3A%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CPRE%3E%3CSPAN%3ECPU%20Core%20%26nbsp%3B%20%7C%20Active%20%25%20%26nbsp%3B%20%7C%20Idle%20%25%20(%3Dtop%20id)%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E-------------------------------------------%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3ECPU%200%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%7C%20%26nbsp%3B%20%26nbsp%3B%200.34%25%20%26nbsp%3B%7C%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%2099.66%25%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3ECPU%201%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%7C%20%26nbsp%3B%20%26nbsp%3B21.89%25%20%26nbsp%3B%7C%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%2078.11%25%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3ECPU%202%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%7C%20%26nbsp%3B%20%26nbsp%3B%200.15%25%20%26nbsp%3B%7C%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%2099.85%25%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3ECPU%203%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%7C%20%26nbsp%3B%20%26nbsp%3B%200.03%25%20%26nbsp%3B%7C%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%2099.97%25%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E-------------------------------------------%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3ETOTAL%20AVG%20%26nbsp%3B%7C%20%26nbsp%3B%20%26nbsp%3B%205.60%25%20%26nbsp%3B%7C%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%2094.40%25%3C%2FSPAN%3E%3C%2FPRE%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%3E%3CSPAN%3ETo%20compare%20with%20%3C%2FSPAN%3E%3CEM%3Etop%3C%2FEM%3E%3CSPAN%3E%2C%20press%20%3C%2FSPAN%3E%3CSPAN%3E1%3C%2FSPAN%3E%3CSPAN%3E%20in%20%3C%2FSPAN%3E%3CEM%3Etop%3C%2FEM%3E%3CSPAN%3E%20to%20show%20per-core%20stats%2C%20then%20compare%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CDIV%3E%3CSPAN%3Ethe%20%3C%2FSPAN%3E%3CEM%3Eid%3C%2FEM%3E%3CSPAN%3E%20column%20with%20the%20%3C%2FSPAN%3E%3CEM%3EIdle%20%25%3C%2FEM%3E%3CSPAN%3E%20column%20above.%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CBR%20%2F%3E%0A%3CH2%20id%3D%22toc-hId-216786995%22%20id%3D%22toc-hId-216790782%22%3E%3CSPAN%3EPrerequisites%3C%2FSPAN%3E%3C%2FH2%3E%0A%3CH3%20id%3D%22toc-hId-907348469%22%20id%3D%22toc-hId-907352256%22%3E%3CSPAN%3EHost%20(build%20machine)%3C%2FSPAN%3E%3C%2FH3%3E%0A%3C%2FDIV%3E%0A%3CDIV%3E%0A%3CTABLE%20border%3D%221%22%20width%3D%22100%25%22%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%20width%3D%2250%25%22%3E%3CSTRONG%3EPackage%3C%2FSTRONG%3E%3C%2FTD%3E%0A%3CTD%20width%3D%2250%25%22%3E%3CSTRONG%3EPurpose%3C%2FSTRONG%3E%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%2250%25%22%3Eclang%20%2B%20llvm%20version%2021%3C%2FTD%3E%0A%3CTD%20width%3D%2250%25%22%3ECompile%20BPF%20C%20source%20to%20BPF%20ELF%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%2250%25%22%3Ebpftool%3C%2FTD%3E%0A%3CTD%20width%3D%2250%25%22%3EGenerate%20BFP%20skeleton%20headers%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%2250%25%22%3Eaarch64%20Poky%20Toolchain%3C%2FTD%3E%0A%3CTD%20width%3D%2250%25%22%3ECorss-compile%20the%20user-space%20binary%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3C%2FDIV%3E%0A%3CDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CBR%20%2F%3E%0A%3CDIV%3E%3CSPAN%3EInstall%20on%20Ubuntu%2FDebian%3A%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CPRE%3E%3CSPAN%3Ewget%3C%2FSPAN%3E%20%3CSPAN%3E%3CA%20href%3D%22https%3A%2F%2Fapt.llvm.org%2Fllvm.sh%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noreferrer%22%3Ehttps%3A%2F%2Fapt.llvm.org%2Fllvm.sh%3C%2FA%3E%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3Echmod%3C%2FSPAN%3E%20%3CSPAN%3E%2Bx%3C%2FSPAN%3E%20%3CSPAN%3Ellvm.sh%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E.%2Fllvm.sh%3C%2FSPAN%3E%20%3CSPAN%3E21%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3Eapt%3C%2FSPAN%3E%20%3CSPAN%3Eupdate%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3Eapt%3C%2FSPAN%3E%20%3CSPAN%3Einstall%3C%2FSPAN%3E%20%3CSPAN%3E-y%3C%2FSPAN%3E%20%3CSPAN%3Ellvm-21%3C%2FSPAN%3E%20%3CSPAN%3Eclang-21%3C%2FSPAN%3E%20%3CSPAN%3Elld-21%3C%2FSPAN%3E%20%3CSPAN%3Elldb-21%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3Eupdate-alternatives%3C%2FSPAN%3E%20%3CSPAN%3E--install%3C%2FSPAN%3E%20%3CSPAN%3E%2Fusr%2Fbin%2Fllvm-link%3C%2FSPAN%3E%20%3CSPAN%3Ellvm-link%3C%2FSPAN%3E%20%3CSPAN%3E%2Fusr%2Fbin%2Fllvm-link-21%3C%2FSPAN%3E%20%3CSPAN%3E100%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3Eupdate-alternatives%3C%2FSPAN%3E%20%3CSPAN%3E--install%3C%2FSPAN%3E%20%3CSPAN%3E%2Fusr%2Fbin%2Fclang%3C%2FSPAN%3E%20%3CSPAN%3Eclang%3C%2FSPAN%3E%20%3CSPAN%3E%2Fusr%2Fbin%2Fclang-21%3C%2FSPAN%3E%20%3CSPAN%3E100%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3Eupdate-alternatives%3C%2FSPAN%3E%20%3CSPAN%3E--install%3C%2FSPAN%3E%20%3CSPAN%3E%2Fusr%2Fbin%2Fllc%3C%2FSPAN%3E%20%3CSPAN%3Ellc%3C%2FSPAN%3E%20%3CSPAN%3E%2Fusr%2Fbin%2Fllc-21%3C%2FSPAN%3E%20%3CSPAN%3E100%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3Ellvm-link%3C%2FSPAN%3E%20%3CSPAN%3E--version%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3Eclang%3C%2FSPAN%3E%20%3CSPAN%3E--version%3CBR%20%2F%3E%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%23%20install%20bpftool%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3Eapt%3C%2FSPAN%3E%20%3CSPAN%3Einstall%3C%2FSPAN%3E%20%3CSPAN%3Elinux-tools-%3C%2FSPAN%3E%3CSPAN%3E%24(%3C%2FSPAN%3E%3CSPAN%3Euname%3C%2FSPAN%3E%20%3CSPAN%3E-r%3C%2FSPAN%3E%3CSPAN%3E)%3C%2FSPAN%3E%3C%2FPRE%3E%0A%3CH3%20id%3D%22toc-hId--900105994%22%20id%3D%22toc-hId--900102207%22%3E%3CSPAN%3ETarget%20board%3C%2FSPAN%3E%3C%2FH3%3E%0A%3CUL%3E%0A%3CLI%3E%3CSPAN%3ETested%20Linux%20kernel%20%3D%3D%206.18.20-2.0.0%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%3E%3CSPAN%3E%20Tested%20image%20%3D%3D%20imx-image-full%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%3E%3CSPAN%3E%20Most%20BPF%20configurations%20are%20enabled%20by%20default.%20In%20addition%2C%20you%20need%20%3C%2FSPAN%3E%3CEM%3ECONFIG_FTRACE%3Dy%3C%2FEM%3E%3CSPAN%3E%20to%20enable%20tracepoint%20support%20(required%20for%20the%20%3C%2FSPAN%3E%3CEM%3Esched_switch%3C%2FEM%3E%3CSPAN%3E%20hook).%3C%2FSPAN%3E%3C%2FLI%3E%0A%3C%2FUL%3E%0A%3CBR%20%2F%3E%0A%3CH2%20id%3D%22toc-hId--910609098%22%20id%3D%22toc-hId--910605311%22%3E%3CSPAN%3EBuild%3C%2FSPAN%3E%3C%2FH2%3E%0A%3CDIV%3E%3CSPAN%3EBuild%20the%20project%20on%20your%20host%20machine%3A%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CPRE%3E%3CSPAN%3ETOOLCHAIN_PATH%3C%2FSPAN%3E%3CSPAN%3E%3D%3C%2FSPAN%3E%3CSPAN%3E%2Fopt%2Ffsl-imx-internal-wayland%2F6.18-whinlatter%2Fenvironment-setup-armv8a-poky-linux%3C%2FSPAN%3E%20%3CSPAN%3E.%2Fbuild_and_deploy.sh%3C%2FSPAN%3E%3C%2FPRE%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CBR%20%2F%3E%0A%3CDIV%3E%3CSPAN%3EThe%20script%20will%3A%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3COL%3E%0A%3CLI%3E%3CSPAN%3ESource%20the%20Poky%20toolchain%20environment%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%3E%3CSPAN%3E%20Run%20%3C%2FSPAN%3E%3CEM%3Emake%20clean%20%26amp%3B%26amp%3B%20make%20all%3C%2FEM%3E%3CSPAN%3E%20(compiles%20BPF%20object%2C%20generates%20skeleton%2C%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%3E%3CSPAN%3Ecross-compiles%20user-space%20binary)%3C%2FSPAN%3E%3C%2FLI%3E%0A%3CLI%3E%3CSPAN%3E%20Install%20the%20binary%20to%20%3C%2FSPAN%3E%3CEM%3E.%2Fboard_deploy%2Fcpu_monitor%3C%2FEM%3E%3C%2FLI%3E%0A%3C%2FOL%3E%0A%3CBR%20%2F%3E%0A%3CH2%20id%3D%22toc-hId-1576903735%22%20id%3D%22toc-hId-1576907522%22%3E%3CSPAN%3EDeploy%20and%20run%3C%2FSPAN%3E%3C%2FH2%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CPRE%3E%3CSPAN%3E%23%20Copy%20binary%20to%20the%20board%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3Escp%3C%2FSPAN%3E%20%3CSPAN%3Eboard_deploy%2Fcpu_monitor%3C%2FSPAN%3E%20%3CSPAN%3Eroot%40%3C%2FSPAN%3E%3CSPAN%3E%26lt%3B%3C%2FSPAN%3E%3CSPAN%3Eboard-i%3C%2FSPAN%3E%3CSPAN%3Ep%26gt%3B%3C%2FSPAN%3E%3CSPAN%3E%3A%2Fusr%2Fbin%2F%3C%2FSPAN%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%3E%23%20Run%20on%20the%20board%20(root%20required%20for%20eBPF)%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3Essh%3C%2FSPAN%3E%20%3CSPAN%3Eroot%40%3C%2FSPAN%3E%3CSPAN%3E%26lt%3B%3C%2FSPAN%3E%3CSPAN%3Eboard-i%3C%2FSPAN%3E%3CSPAN%3Ep%26gt%3B%3C%2FSPAN%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%3E.%2Fcpu_monitor%3C%2FSPAN%3E%3C%2FPRE%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CDIV%3E%3CSPAN%3EPress%20%3C%2FSPAN%3E%3CEM%3ECtrl%2BC%3C%2FEM%3E%3CSPAN%3E%20to%20exit.%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CBR%20%2F%3E%0A%3CH2%20id%3D%22toc-hId--230550728%22%20id%3D%22toc-hId--230546941%22%3E%3CSPAN%3EBenchmarking%3A%20eBPF%20vs%20top%3C%2FSPAN%3E%3C%2FH2%3E%0A%3CBR%20%2F%3E%0A%3CDIV%3E%3CSPAN%3EA%20scheduler%20stress%20test%20script%20is%20included%20to%20compare%20monitoring%20overhead%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CDIV%3E%3CSPAN%3Ebetween%20%3C%2FSPAN%3E%3CEM%3Ecpu_monitor%3C%2FEM%3E%3CSPAN%3E%20and%20%3C%2FSPAN%3E%3CEM%3Etop%3C%2FEM%3E%3CSPAN%3E%20under%20high%20scheduling%20pressure.%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CBR%20%2F%3E%0A%3CH3%20id%3D%22toc-hId-460010746%22%20id%3D%22toc-hId-460014533%22%3E%3CSPAN%3ERun%20the%20stress%20test%3C%2FSPAN%3E%3C%2FH3%3E%0A%3CBR%20%2F%3E%0A%3CDIV%3E%3CSPAN%3EOn%20the%20target%20board%2C%20open%20three%20terminals%3A%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CBR%20%2F%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CPRE%3E%3CSPAN%3E%23%20Terminal%201%3A%20start%20scheduler%20pressure%20(200%20threads%2C%2030%20seconds)%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E.%2Fstress_sched.sh%3C%2FSPAN%3E%20%3CSPAN%3E-t%3C%2FSPAN%3E%20%3CSPAN%3E200%3C%2FSPAN%3E%20%3CSPAN%3E-d%3C%2FSPAN%3E%20%3CSPAN%3E30%3C%2FSPAN%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%3E%23%20Terminal%202%3A%20measure%20eBPF%20monitoring%20overhead%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3Eperf%3C%2FSPAN%3E%20%3CSPAN%3Estat%3C%2FSPAN%3E%20%3CSPAN%3E-e%3C%2FSPAN%3E%20%3CSPAN%3Ecpu-clock%2Ctask-clock%2Ccontext-switches%2Ccpu-migrations%2Cinstructions%3C%2FSPAN%3E%20%3CSPAN%3Etimeout%3C%2FSPAN%3E%20%3CSPAN%3E-s%3C%2FSPAN%3E%20%3CSPAN%3EKILL%3C%2FSPAN%3E%20%3CSPAN%3E10%3C%2FSPAN%3E%20%3CSPAN%3E.%2Fcpu_monitor%3C%2FSPAN%3E%3CBR%20%2F%3E%3CBR%20%2F%3E%3CSPAN%3E%23%20Terminal%203%3A%20measure%20top%20monitoring%20overhead%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3Eperf%3C%2FSPAN%3E%20%3CSPAN%3Estat%3C%2FSPAN%3E%20%3CSPAN%3E-e%3C%2FSPAN%3E%20%3CSPAN%3Ecpu-clock%2Ctask-clock%2Ccontext-switches%2Ccpu-migrations%2Cinstructions%3C%2FSPAN%3E%20%3CSPAN%3Etop%3C%2FSPAN%3E%20%3CSPAN%3E-b%3C%2FSPAN%3E%20%3CSPAN%3E-d%3C%2FSPAN%3E%20%3CSPAN%3E1%3C%2FSPAN%3E%20%3CSPAN%3E-n%3C%2FSPAN%3E%20%3CSPAN%3E10%3C%2FSPAN%3E%3C%2FPRE%3E%0A%3CBR%20%2F%3E%0A%3CDIV%3E%3CSPAN%3ECompare%20%3C%2FSPAN%3E%3CEM%3Etask-clock%3C%2FEM%3E%3CSPAN%3E%2C%20%3C%2FSPAN%3E%3CEM%3Einstructions%3C%2FEM%3E%3CSPAN%3E%2C%20and%20%3C%2FSPAN%3E%3CEM%3Econtext-switches%3C%2FEM%3E%3CSPAN%3E%20from%20%3C%2FSPAN%3E%3CEM%3Eperf%20stat%3C%2FEM%3E%3C%2FDIV%3E%0A%3CDIV%3E%3CSPAN%3Eoutput.%20Try%20different%20thread%20counts%20to%20observe%20scaling%20behavior%3A%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CPRE%3E%3CSPAN%3E.%2Fstress_sched.sh%3C%2FSPAN%3E%20%3CSPAN%3E-t%3C%2FSPAN%3E%20%3CSPAN%3E50%3C%2FSPAN%3E%20%3CSPAN%3E-d%3C%2FSPAN%3E%20%3CSPAN%3E30%3C%2FSPAN%3E%3CSPAN%3E%20%26nbsp%3B%20%26nbsp%3B%20%3C%2FSPAN%3E%3CSPAN%3E%23%20low%20pressure%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E.%2Fstress_sched.sh%3C%2FSPAN%3E%20%3CSPAN%3E-t%3C%2FSPAN%3E%20%3CSPAN%3E200%3C%2FSPAN%3E%20%3CSPAN%3E-d%3C%2FSPAN%3E%20%3CSPAN%3E30%3C%2FSPAN%3E%3CSPAN%3E%20%26nbsp%3B%20%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3E%23%20medium%20pressure%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E.%2Fstress_sched.sh%3C%2FSPAN%3E%20%3CSPAN%3E-t%3C%2FSPAN%3E%20%3CSPAN%3E500%3C%2FSPAN%3E%20%3CSPAN%3E-d%3C%2FSPAN%3E%20%3CSPAN%3E30%3C%2FSPAN%3E%3CSPAN%3E%20%26nbsp%3B%20%26nbsp%3B%3C%2FSPAN%3E%3CSPAN%3E%23%20high%20pressure%3C%2FSPAN%3E%3C%2FPRE%3E%0A%3CH3%20id%3D%22toc-hId--1347443717%22%20id%3D%22toc-hId--1347439930%22%3E%3CSPAN%3EResult%3C%2FSPAN%3E%3C%2FH3%3E%0A%3CDIV%3E%3CSPAN%3ETest%20environment%3A%20i.MX943%20(4x%20Cortex-A55)%2C%20Linux%206.18.20%2C%2010-second%20measurement%20window.%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CBR%20%2F%3E%0A%3CDIV%3E%3CSPAN%3EComparison%20results%20in%20free%20system%20load%3A%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3C%2FDIV%3E%0A%3CDIV%3E%0A%3CTABLE%20border%3D%221%22%20width%3D%22100%25%22%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%20width%3D%2233.333333333333336%25%22%20height%3D%2224px%22%3E%3CSTRONG%3EIndicator%3C%2FSTRONG%3E%3C%2FTD%3E%0A%3CTD%20width%3D%2233.333333333333336%25%22%20height%3D%2224px%22%3E%3CSTRONG%3EeBPF%26nbsp%3Bcpu_monitor%3C%2FSTRONG%3E%3C%2FTD%3E%0A%3CTD%20width%3D%2216.666666666666668%25%22%20height%3D%2224px%22%3E%3CSTRONG%3Etop%3C%2FSTRONG%3E%3C%2FTD%3E%0A%3CTD%20width%3D%2216.666666666666668%25%22%20height%3D%2224px%22%3E%3CSTRONG%3Edelta%3C%2FSTRONG%3E%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%2233.333333333333336%25%22%20height%3D%2224px%22%3E%3CSPAN%3Etask-clock%20(CPU%20timing)%3C%2FSPAN%3E%3C%2FTD%3E%0A%3CTD%20width%3D%2233.333333333333336%25%22%20height%3D%2224px%22%3E%3CSPAN%3E12.0%20ms%3C%2FSPAN%3E%3C%2FTD%3E%0A%3CTD%20width%3D%2216.666666666666668%25%22%20height%3D%2224px%22%3E%3CSPAN%3E263.9%20ms%3C%2FSPAN%3E%3C%2FTD%3E%0A%3CTD%20width%3D%2216.666666666666668%25%22%20height%3D%2224px%22%3E22x%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%2233.333333333333336%25%22%20height%3D%2224px%22%3E%3CSPAN%3ECPU%20usage%3C%2FSPAN%3E%3C%2FTD%3E%0A%3CTD%20width%3D%2233.333333333333336%25%22%20height%3D%2224px%22%3E%3CSPAN%3E0.1%25%3C%2FSPAN%3E%3C%2FTD%3E%0A%3CTD%20width%3D%2216.666666666666668%25%22%20height%3D%2224px%22%3E2.8%25%3C%2FTD%3E%0A%3CTD%20width%3D%2216.666666666666668%25%22%20height%3D%2224px%22%3E28x%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%2233.333333333333336%25%22%20height%3D%2224px%22%3E%3CSPAN%3Einstructions%3C%2FSPAN%3E%3C%2FTD%3E%0A%3CTD%20width%3D%2233.333333333333336%25%22%20height%3D%2224px%22%3E%3CSPAN%3E3.38M%3C%2FSPAN%3E%3C%2FTD%3E%0A%3CTD%20width%3D%2216.666666666666668%25%22%20height%3D%2224px%22%3E%3CSPAN%3E197M%3C%2FSPAN%3E%3C%2FTD%3E%0A%3CTD%20width%3D%2216.666666666666668%25%22%20height%3D%2224px%22%3E%3CSPAN%3E58x%3C%2FSPAN%3E%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%2233.333333333333336%25%22%20height%3D%2224px%22%3E%3CSPAN%3Econtext-switches%3C%2FSPAN%3E%3C%2FTD%3E%0A%3CTD%20width%3D%2233.333333333333336%25%22%20height%3D%2224px%22%3E12%3C%2FTD%3E%0A%3CTD%20width%3D%2216.666666666666668%25%22%20height%3D%2224px%22%3E17%3C%2FTD%3E%0A%3CTD%20width%3D%2216.666666666666668%25%22%20height%3D%2224px%22%3E%26nbsp%3B%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%2233.333333333333336%25%22%20height%3D%2224px%22%3E%3CSPAN%3Esys%20time%20(kernel%20time)%3C%2FSPAN%3E%3C%2FTD%3E%0A%3CTD%20width%3D%2233.333333333333336%25%22%20height%3D%2224px%22%3E%3CSPAN%3E5.0ms%3C%2FSPAN%3E%3C%2FTD%3E%0A%3CTD%20width%3D%2216.666666666666668%25%22%20height%3D%2224px%22%3E%3CSPAN%3E199.5ms%3C%2FSPAN%3E%3C%2FTD%3E%0A%3CTD%20width%3D%2216.666666666666668%25%22%20height%3D%2224px%22%3E40x%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%2233.333333333333336%25%22%20height%3D%2224px%22%3E%3CSPAN%3Euser%20time%3C%2FSPAN%3E%3C%2FTD%3E%0A%3CTD%20width%3D%2233.333333333333336%25%22%20height%3D%2224px%22%3E0%3C%2FTD%3E%0A%3CTD%20width%3D%2216.666666666666668%25%22%20height%3D%2224px%22%3E62.1ms%3C%2FTD%3E%0A%3CTD%20width%3D%2216.666666666666668%25%22%20height%3D%2224px%22%3E%26nbsp%3B%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3C%2FDIV%3E%0A%3CDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CBR%20%2F%3E%0A%3CDIV%3E%3CSPAN%3EComparison%20results%20in%20high%20scheduling%20pressure%20(200%20threads)%3A%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CDIV%3E%0A%3CTABLE%20border%3D%221%22%20width%3D%22100%25%22%3E%0A%3CTBODY%3E%0A%3CTR%3E%0A%3CTD%20width%3D%2225%25%22%3E%3CSTRONG%3EIndicator%3C%2FSTRONG%3E%3C%2FTD%3E%0A%3CTD%20width%3D%2225%25%22%3E%3CSTRONG%3EeBPF%20cpu_monitor%3C%2FSTRONG%3E%3C%2FTD%3E%0A%3CTD%20width%3D%2225%25%22%3E%3CSTRONG%3Etop%3C%2FSTRONG%3E%3C%2FTD%3E%0A%3CTD%20width%3D%2225%25%22%3E%3CSTRONG%3Edelta%3C%2FSTRONG%3E%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%2225%25%22%3E%3CSPAN%3Etask-clock%20(CPU%20timing)%3C%2FSPAN%3E%3C%2FTD%3E%0A%3CTD%20width%3D%2225%25%22%3E12.6ms%3C%2FTD%3E%0A%3CTD%20width%3D%2225%25%22%3E690ms%3C%2FTD%3E%0A%3CTD%20width%3D%2225%25%22%3E54x%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%2225%25%22%3E%3CSPAN%3ECPU%20usage%3C%2FSPAN%3E%3C%2FTD%3E%0A%3CTD%20width%3D%2225%25%22%3E0.1%25%3C%2FTD%3E%0A%3CTD%20width%3D%2225%25%22%3E6.1%25%3C%2FTD%3E%0A%3CTD%20width%3D%2225%25%22%3E61x%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%2225%25%22%3E%3CSPAN%3Einstructions%3C%2FSPAN%3E%3C%2FTD%3E%0A%3CTD%20width%3D%2225%25%22%3E4.8M%3C%2FTD%3E%0A%3CTD%20width%3D%2225%25%22%3E538M%3C%2FTD%3E%0A%3CTD%20width%3D%2225%25%22%3E112x%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%2225%25%22%3E%3CSPAN%3Econtext-switches%3C%2FSPAN%3E%3C%2FTD%3E%0A%3CTD%20width%3D%2225%25%22%3E122%3C%2FTD%3E%0A%3CTD%20width%3D%2225%25%22%3E9019%3C%2FTD%3E%0A%3CTD%20width%3D%2225%25%22%3E75x%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%2225%25%22%3E%3CSPAN%3Esys%20time%20(kernel%20time)%3C%2FSPAN%3E%3C%2FTD%3E%0A%3CTD%20width%3D%2225%25%22%3E4ms%3C%2FTD%3E%0A%3CTD%20width%3D%2225%25%22%3E461ms%3C%2FTD%3E%0A%3CTD%20width%3D%2225%25%22%3E115x%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3CTR%3E%0A%3CTD%20width%3D%2225%25%22%3E%3CSPAN%3Euser%20time%3C%2FSPAN%3E%3C%2FTD%3E%0A%3CTD%20width%3D%2225%25%22%3E0%3C%2FTD%3E%0A%3CTD%20width%3D%2225%25%22%3E206ms%3C%2FTD%3E%0A%3CTD%20width%3D%2225%25%22%3E%26nbsp%3B%3C%2FTD%3E%0A%3C%2FTR%3E%0A%3C%2FTBODY%3E%0A%3C%2FTABLE%3E%0A%3C%2FDIV%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3CBR%20%2F%3E%0A%3CH2%20id%3D%22toc-hId-315822918%22%20id%3D%22toc-hId-315826705%22%3E%3CSPAN%3EProject%20structure%3C%2FSPAN%3E%3C%2FH2%3E%0A%3CPRE%3E%3CSPAN%3Eebpf_cpu_usage%2F%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%E2%94%9C%E2%94%80%E2%94%80%20cpu_monitor_bpf.c%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%23%20Kernel-side%20eBPF%20program%20(C%2C%20compiled%20to%20BPF)%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%E2%94%9C%E2%94%80%E2%94%80%20cpu_monitor.c%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%23%20User-space%20program%20(C%2C%20cross-compiled%20to%20aarch64)%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%E2%94%9C%E2%94%80%E2%94%80%20Makefile%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%23%20Build%20rules%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%E2%94%9C%E2%94%80%E2%94%80%20build_and_deploy.sh%20%26nbsp%3B%20%26nbsp%3B%23%20One-shot%20build%20%2B%20package%20script%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%E2%94%9C%E2%94%80%E2%94%80%20stress_sched.sh%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%20%26nbsp%3B%23%20Scheduler%20stress%20test%20for%20benchmarking%3C%2FSPAN%3E%3CBR%20%2F%3E%3CSPAN%3E%E2%94%94%E2%94%80%E2%94%80%20README.md%3C%2FSPAN%3E%3C%2FPRE%3E%0A%3CDIV%3E%26nbsp%3B%3C%2FDIV%3E%0A%3C%2FDIV%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-2394283%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CDIV%3E%0A%3CDIV%3E%3CSPAN%3EA%20lightweight%20CPU%20utilization%20monitor%20built%20with%20eBPF%20%2B%20libbpf.%20%26nbsp%3B%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CDIV%3E%3CSPAN%3EIt%20hooks%20into%20the%20kernel%20scheduler%20(%3C%2FSPAN%3E%3CEM%3Esched_switch%3C%2FEM%3E%3CSPAN%3E%26nbsp%3Btracepoint)%20to%20measure%20per-CPU%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CDIV%3E%3CSPAN%3Eactive%20time%20with%20nanosecond%20precision%20%E2%80%94%20more%20accurate%20than%20polling-based%20tools%20like%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3CDIV%3E%3CEM%3Etop%3C%2FEM%3E%3CSPAN%3E%2C%20and%20with%20lower%20overhead%20at%20high%20process%20counts.%3C%2FSPAN%3E%3C%2FDIV%3E%0A%3C%2FDIV%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2394283%22%20slang%3D%22en-US%22%20mode%3D%22CREATE%22%3E%3CLINGO-LABEL%3Ei.MX%26nbsp%3BProcessors%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E