Skip to content

Performance monitoring unit utils for microbenchmark & security exploration. (rdpmc, PMCCNTR_EL0, rdcycle)

License

Notifications You must be signed in to change notification settings

Kingfish404/pmu-utils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PMU Utils

Features

  • Support for AMD64 (x86_64, Intel ia64 and AMD amd64), ARM64 (aarch64), and RISC-V (riscv).
  • Support for reading and writing to the MSR register of the Performance Monitoring Unit (PMU).
  • Support for reading and writing the Performance Monitoring Counter (PMC) value.
  • Support enable user space access to high resolution timers.
    • "MRS %0, PMCCNTR_EL0 for ARM64 chips.
    • rdcycle for RISC-V chips.
  • Support for reading the Performance Monitoring Event (PME).
    • For x86_64 ia64 chips, the PME is getting from the intel/perfmon
    • For x86_64 amd64 and aarch64 chips, the PME is getting from the Linux Perf tool.

Read the following sections for more details.

Preparation on Linux

Currently, only Linux is supported. The library does not rely on any other library. It uses only standard C functionality.

# Building the kernel module requires the kernel headers of the kernel. On Ubuntu or Debian, they can be installed by running
# The kernel module can be built by running
sudo apt install linux-headers-$(uname -r)
make unload load

# Additionally step for x86_64 chips machines
sudo modprobe msr   # load msr module

# For RISC-V or ARM chips with linux kernel version >= 6.6, enable userspace access to perf
echo 1 | sudo tee /proc/sys/kernel/perf_user_access
# https://github.com/torvalds/linux/blob/master/drivers/perf/riscv_pmu_sbi.c

Script Utils

Change the directory to scripts and run *utils.py for your architecture.

# build the PMU Event Header for x86-64 chips (for `ia64` and `amd64` architectures), or for ARM chips
cd scripts
python3 pmu_init.py
# or for intel chips with hybrid core
python3 pmu_init.py --hybridcore
# equal to `python3 pmu_intel_utils.py --hybridcore`
# then you could see the PMU header: `ia64_pmu_event.h`, `amd64_pmu_event.h`, or `arm_pmu_event.h`
# just include the header in your c or c++ project

Yet another way to get the PMU event header is to use perf tool.

sudo perf list -j > this-cpu-pmu-events.json

Using the library:

Refer to the tests directory for more details about how to use the library.

Copy the header file (*.h) in header to your project include path or add the path of header to CFLAGS, after that, include the pmu_utils.h.

#include <pmu_utils.h>

For AMD64 (x86_64) chips

See tests/src/x86_64/basic.c for more details.

For ARM64 (aarch64) chips

See tests/src/aarch64/basic.c for more details.

For RISC-V (riscv64) chips

See tests/src/riscv64/basic.c for more details.

Test and Example

Seen in tests directory.

make test test_verbose

References

About

Performance monitoring unit utils for microbenchmark & security exploration. (rdpmc, PMCCNTR_EL0, rdcycle)

Resources

License

Stars

Watchers

Forks

Packages

No packages published