# How to measure cache misses in Julia?

**URL:** <https://discourse.julialang.org/t/how-to-measure-cache-misses-in-julia/20875>\
**Category:** Performance\
**Created:** [February 16, 2019, 9:23pm UTC](https://discourse.julialang.org/t/how-to-measure-cache-misses-in-julia/20875 "2019-02-16T21:23:42Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![phrmoy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phrmoy/32/8912_2.png) [@phrmoy](https://discourse.julialang.org/u/phrmoy)\
**Post date:** [February 16, 2019, 9:23pm UTC](https://discourse.julialang.org/t/how-to-measure-cache-misses-in-julia/20875/1 "2019-02-16T21:23:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [February 17, 2019, 12:19am UTC](https://discourse.julialang.org/t/how-to-measure-cache-misses-in-julia/20875/2 "2019-02-17T00:19:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![phrmoy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phrmoy/32/8912_2.png) [@phrmoy](https://discourse.julialang.org/u/phrmoy)\
**Post date:** [February 17, 2019, 12:24am UTC](https://discourse.julialang.org/t/how-to-measure-cache-misses-in-julia/20875/3 "2019-02-17T00:24:25Z")

</div>

> **[How do you measure cache misses in C/C++?](https://www.quora.com/How-do-you-measure-cache-misses-in-C-C++)**
>
> Answer (1 of 2): PAPI is the reasonable answer to the question asked. If you want to poke model-specific registers yourself check out LLNL’s libmsr and msr-safe. Likwid might also work, but I haven’t used it personally. Ditto VTune.

---

<div class="post-metadata">

**Author:** ![tkf](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tkf/32/17635_2.png) [@tkf](https://discourse.julialang.org/u/tkf)\
**Post date:** [February 17, 2019, 1:33am UTC](https://discourse.julialang.org/t/how-to-measure-cache-misses-in-julia/20875/4 "2019-02-17T01:33:10Z")

</div>

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

````julia
"""
    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`)

````

---

<div class="post-metadata">

**Author:** ![jpsamaroo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jpsamaroo/32/46804_2.png) [@jpsamaroo](https://discourse.julialang.org/u/jpsamaroo)\
**Post date:** [February 17, 2019, 2:21am UTC](https://discourse.julialang.org/t/how-to-measure-cache-misses-in-julia/20875/5 "2019-02-17T02:21:53Z")

</div>

`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!

---

<div class="post-metadata">

**Author:** ![phrmoy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phrmoy/32/8912_2.png) [@phrmoy](https://discourse.julialang.org/u/phrmoy)\
**Post date:** [February 17, 2019, 9:00am UTC](https://discourse.julialang.org/t/how-to-measure-cache-misses-in-julia/20875/6 "2019-02-17T09:00:15Z")

</div>

Thank you!

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [February 17, 2019, 9:35am UTC](https://discourse.julialang.org/t/how-to-measure-cache-misses-in-julia/20875/7 "2019-02-17T09:35:43Z")

</div>

[GitHub - carnaval/LinuxPerf.jl](https://github.com/carnaval/LinuxPerf.jl) could perhaps give some inspiration. Package most likely needs updating though.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [February 17, 2019, 3:52pm UTC](https://discourse.julialang.org/t/how-to-measure-cache-misses-in-julia/20875/8 "2019-02-17T15:52:58Z")

</div>

> [@kristoffer.carlsson](#):
>
> [https://github.com/carnaval/LinuxPerf.jl](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.

---

<div class="post-metadata">

**Author:** ![phrmoy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phrmoy/32/8912_2.png) [@phrmoy](https://discourse.julialang.org/u/phrmoy)\
**Post date:** [February 17, 2019, 7:28pm UTC](https://discourse.julialang.org/t/how-to-measure-cache-misses-in-julia/20875/9 "2019-02-17T19:28:17Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![johnh](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnh/32/3615_2.png) [@johnh](https://discourse.julialang.org/u/johnh)\
**Post date:** [February 17, 2019, 8:56pm UTC](https://discourse.julialang.org/t/how-to-measure-cache-misses-in-julia/20875/10 "2019-02-17T20:56:01Z")

</div>

@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.

---

<div class="post-metadata">

**Author:** ![phrmoy](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phrmoy/32/8912_2.png) [@phrmoy](https://discourse.julialang.org/u/phrmoy)\
**Post date:** [February 17, 2019, 11:08pm UTC](https://discourse.julialang.org/t/how-to-measure-cache-misses-in-julia/20875/11 "2019-02-17T23:08:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [February 18, 2019, 5:03am UTC](https://discourse.julialang.org/t/how-to-measure-cache-misses-in-julia/20875/12 "2019-02-18T05:03:05Z")

</div>

Slack invite requests are here: [https://slackinvite.julialang.org/](https://slackinvite.julialang.org/)
