Re: Cache conflict detection support in cachegrind

Paul Floyd <[email protected]> Sun, 29 Jan 2023 16:23:51 +0100
Newsgroups gmane.comp.debugging.valgrind
Message-ID <[email protected]>

On 29-01-23 14:31, Ivica B wrote:
> Hi!
> 
> I am looking for a tool that can detect cache conflicts, but I am not
> finding any. There are a few that are mostly academic, and thus not
> maintained. I think it is important for the performance analysis
> community to have a tool that to some extent can detect cache
> conflicts. Is it possible to implement support for detecting source
> code lines where cache conflicts occur? More info on cache conflicts
> below.

[snip]

I agree that this is an interesting topic. If anyone else has ideas I'm 
all ears.

My recommendations for this are:

1/ PMU/PMC (performance monitoring unit/counter) event counting tools 
(perf record on Linux, pmcstat on FreeBSD, Oracle Studio collect on 
Solaris, don't know for macOS). These can record events such as cache 
misses with the associated callstacks. You can then use tools HotSpot 
and perfgrind/kcachegrind (I hae used HotSpot but not perfgrind).

The big advantage of this is that the PMCs are part of the hardware and 
the overhead of doing this is minor. The only slight limitation is that 
then number of counters is limited.

2/ pahole
https://github.com/acmel/dwarves
A really nice binary analysis tool. It will analyze your binary (with 
debuginfo) and generate a report for all structures showing holes, 
padding and cache lines. It can even generate modified source with 
members reordered to improve the packing. However as this is a static 
tool working only on the data structures it knows nothing about your 
access patterns.

3/ DHAT
One of the Valgrind tools. This profiles heap memory. If the block is 
less than 1k it will also generate a kind of ascii-html heat map. That 
map is an aggregate, but you can usually guess which offsets get hit the 
most together.

Cachegrind doesn't really do this with the kind of accuracy that PMCs 
do. It has a reduced model of the cache and has a basic branch 
predictor. I don't know if or how speculative execution affects the 
cache hit rate, but Valgrind doesn't do any of that.

A+
Paul