On Fri, 11 Jul 2014, Martin Ichilevici de Oliveira wrote:

> /* Gets called whenever my program generates a cache-miss */
> void my_func()
> {
>     unsigned long addr = get_ip_that_generated_event();
>     printf("A cache-miss occurred at IP: %x!\n", addr);
> }
> 
> int main()
> {
>     struct perf_event_attr pe;
>     pe.type = PERF_TYPE_HARDWARE.
>     pe.config = PERF_COUNT_HW_CACHE_MISSES;
>     ...
>     register_handler(&pe, my_func);
>     // code to be monitored
>     unregister_handler();
>     return 0;
> }
> /*******************/
> 
> Every time an event occurs (a cache-miss in my example), my_func should
> get called. The get_ip_that_generated_event() function should return the
> Instruction Pointer that generated the event.

In general you can't do this.  Cache misses happen way too often, your 
program would not be able to keep up, and likely it would be causing cache 
misses itself.

Also you should read up on skid, it's very hard to track an event to an 
exact IP, although if your CPU has PEBS it's possible with certain 
limitations.

> My impression is that this could be done with a low overhead (other then
> that introduced by what I do in my_func), because 
> "perf record -e cache-misses ./a.out"
> can pinpoint with little overhead how many times the event was caused by
> each instruction. But maybe I'm just missing something.

No, what perf record is doing is sampling.  It's getting interrupted say 
every 100,000 cache misses and reporting the values, and hopefully the 
ones that trigger are statistically relevant enough to extract the full 
program behavior.

> I'm unsure how to configure the mmap buffer you mentioned, and I
> couldn't find any examples online. Could you please point me to some
> reference, or maybe provide a little example?

There are some examples in my perf_event_tests package.

See for example
        
https://github.com/deater/perf_event_tests/blob/master/tests/x86_intel/pebs.c

although that example really isn't using PEBS yet, I'm currently working 
on that code, so beware I might be re-writing and moving around the code 
in that directory in the next few days.

Vince
--
To unsubscribe from this list: send the line "unsubscribe linux-perf-users" in
the body of a message to majord...@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Reply via email to