Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use Linux Perf profiler inside C++ code?

I would like to measure L1, L2 and L3 Cache hit/miss ratio of some parts of my C++ code. I am not interested to use Perf for my entire application. Can Perf be used as a library inside C++?

int main() {
    ...
    ...
    start_profiling()
    // The part I'm interested in
    ...
    end_profiling()
    ...
    ...
}

I gave Intel PCM a shot, but I had two issues with it. First, it gave me some strange numbers. Second, it doesn't support L1 Cache profiling.

If it's not possible with Perf, what is the easiest way to get that information?

like image 763
narengi Avatar asked May 18 '15 01:05

narengi


People also ask

How does Linux perf work?

Perf is a facility comprised of kernel infrastructure for gathering various events and userspace tool to get gathered data from the kernel and analyze it. It is like a gprof, but it is non-invasive, low-overhead and profile the whole stack, including your app, libraries, system calls AND kernel with CPU!


1 Answers

Sounds like all you're trying to do is read a few perf counters, something that the PAPI library is ideal for.

Example.

The full list of supported counters is quite long, but it sounds like you're most interested in PAPI_L1_TCM, PAPI_L1_TCA, and their L2 and L3 counterparts. Note that you can also break down the accesses into reads/writes, and you can distinguish instruction and data caches.

like image 140
Adam Avatar answered Sep 26 '22 09:09

Adam