How to measure cache misses in Julia?

Hi there!
Do we have a tool or the means for measuring cache misses in Julia? If we had a macro for this would really set Julia apart in the HPC world. I did search around here but did not find anything, thank you.
Pedro

3 Likes

How does one measure cache misses in other systems? Presumably this requires hardware support.

1 Like

https://www.quora.com/How-do-you-measure-cache-misses-in-C-C++

I’m not sure this is usable for serious usecase, but this works for me in some toy examples. Requires perf CLI.

"""
    perf(f, args)

Run `perf` program with `args` while executing `f`.

# Examples
```
julia> A = randn(100, 100); B = randn(100, 100);

julia> perf(`stat`) do
           @benchmark \$A * \$B
       end
```
"""
function perf(f, args)
    pid = getpid()
    cmd = `perf $args --pid=$pid`
    proc = run(pipeline(cmd, stdout=stdout, stderr=stderr); wait=false)
    try
        return f()
    finally
        flush(stdout)
        flush(stderr)
        kill(proc, Base.SIGINT)
        wait(proc)
    end
end

perf_stat_l1(f, args = ``) =
    perf(f, `stat -e L1-dcache-load-misses,L1-dcache-loads,L1-dcache-stores,L1-icache-load-misses $args`)
12 Likes

perf is definitely the best option on Linux for measuring cache misses (or cache/CPU data in general). It has the benefit of being zero-overhead due to using the CPU’s in-built hardware performance counters. +1 to your solution!

2 Likes

Thank you!

https://github.com/carnaval/LinuxPerf.jl could perhaps give some inspiration. Package most likely needs updating though.

This would be a great GSoC project. Phase 1: resurrect the Linux version. Phase 2: implement similar functionality on Windows and/or macOS and/or FreeBSD. Phase 3: wrap them in a common interface, et voilà, cross-platform performance counters in Julia! Even if not completely finished in a summer any initial portion of this would be worthwhile.

11 Likes

Thanks for sharing the package, and yes, that would be an outstanding HPC feature. In fact, just looking around Julia’s website, we have a reference tag for GPU, but why don’t have a more general pitch to the public for HPC?

@phrmoy On the Julia Slack there is a specicific #HPC channel.
On here the discussion on HPC would be in Parallel/Distributed. I agree with you - perhaps there is a case for renaming this section.

Thanks for the info John, how do I get an invite to the slack channel?

Slack invite requests are here: https://slackinvite.julialang.org/

3 Likes