Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically counting cache faults

I need to evaluate the time taken by a C++ function in a bunch of hypothesis about memory hierarchy efficiency (e.g: time taken when we have a cache miss, a cache hit or page fault when reading a portion of an array), so I'd like to have some libraries that let me count the cache miss / page faults in order to be capable of auto-generating a performance summary.

I know there are some tools like cachegrind that gives some related statistics on a given application execution, but I'd like a library, as I've already said.

edit Oh, I forgot: I'm using Linux and I'm not interested in portability, it's an academic thing.

Any suggestion is welcome!

like image 946
akappa Avatar asked Mar 27 '11 07:03

akappa


2 Answers

Most recent CPUs (both AMD and Intel) have performance monitor registers that can be used for this kind of job. For Intel, they're covered in the programmer's reference manual, volume 3B, chapter 30. For AMD, it's in the BIOS and Kernel Developer's Guide.

Either way, you can count things like cache hits, cache misses, memory requests, data prefetches, etc. They have pretty specific selectors, so you could get a count of (for example) the number of reads on the L2 cache to fill lines in the L1 instruction cache (while still excluding L2 reads to fill lines in the L1 data cache).

There is a Linux kernel module to give access to MSRs (Model-specific registers). Offhand, I don't know whether it gives access to the performance monitor registers, but I'd expect it probably does.

like image 63
Jerry Coffin Avatar answered Sep 23 '22 00:09

Jerry Coffin


It looks like now there is exactly what I was searching for: perf_event_open.

It lets you do interesting things like initializing/enabling/disabling some performance counters for subsequently fetching their values through an uniform and intuitive API (it gives you a special file descriptor which hosts a struct containing the previously requested informations).

It is a linux-only solution and the functionalities varies depending on the kernel version, so be careful :)

like image 24
akappa Avatar answered Sep 25 '22 00:09

akappa