直接从 C++ 应用程序中轻量级记录和采样性能计数器,针对特定代码段。
Quick Start | Building | Documentation | System Requirements
perf-cpp lets you profile specific parts of your code, not the entire program.
Tools like Linux Perf, Intel® VTune™, and AMD uProf profile everything: application startup, configuration parsing, data loading, and all your helper functions.
perf-cpp is different: place start() and stop() around exactly the code you want to measure.
Profile one sorting algorithm, or count cache misses in a single hash table lookup.
Wrap two memory allocators separately, and you get a fair comparison.
Built around Linux's perf subsystem, perf-cpp supports counting and sampling hardware events for specific code blocks:
perf stat, but only around the code you care about (documentation)perf record and perf mem record, but targeted at specific functions (documentation)See the examples and full documentation for details.
…cpp #include
// Create the sampler auto sampler = perf::Sampler{};
// Specify when a sample is recorded: every 50,000th cycle sampler.trigger("cycles", perf::Period{50000U});
// Specify what data is included in a sample: time, CPU ID, instruction sampler.values() .timestamp(true) .cpu_id(true) .logical_instruction_pointer(true);
// Run the workload sampler.start(); code_to_profile(); // [!NOTE]
See the sampling guide for what data you can record. Also check out the sampling on multiple CPUs/threads guide for parallel sampling.
perf-cpp can be built as a static or shared library.
git clone https://github.com/jmuehlig/perf-cpp.git
cd perf-cpp
cmake . -B build
cmake --build build[!NOTE] See the building guide for CMake integration and build options.
The full documentation is available at jmuehlig.github.io/perf-cpp.
See also: Examples | Changelog
perf_event_paranoid setting: Adjust as needed to allow access to performance counters (see the perf paranoid documentation).Contributions are welcome. Open an issue or submit a pull request.
To build and run the tests:
cmake . -B build -DBUILD_TESTS=ON
cmake --build build --target tests
./build/bin/tests[!NOTE] Most tests require access to hardware performance counters via
perf_event_open. If your system restricts access (e.g., in containers or VMs), some tests will fail. See the perf paranoid documentation.
For questions or feedback: [email protected].
Other profiling tools:
暂无开放 Issues,或尚未同步最近议题。