1- #include " nanobench.h"
1+ #include < iostream>
2+ #include < papi.h>
23#include " bench_options.h"
4+ #include " nanobench.h"
35
46extern " C" void run_benchmark (BenchOptions *options)
57{
@@ -8,10 +10,45 @@ extern "C" void run_benchmark(BenchOptions *options)
810 b.title (options->name )
911 .warmup (options->warmup )
1012 .epochs (options->epochs )
11- .relative (options->num_functions > 1 );
13+ .relative (options->num_functions > 1 )
14+ .minEpochIterations (30 );
15+
16+ if (PAPI_library_init (PAPI_VER_CURRENT ) != PAPI_VER_CURRENT )
17+ {
18+ std::cerr << " Error inicializando PAPI\n " ;
19+ }
1220
1321 for (int i = 0 ; i < options->num_functions ; ++i)
1422 {
23+ #define EVENTS_LEN 4
24+ int EventSet = PAPI_NULL ;
25+ long long values[EVENTS_LEN ] = {0 };
26+
27+ PAPI_create_eventset (&EventSet);
28+ PAPI_add_named_event (EventSet, " MEM_LOAD_RETIRED:L1_HIT" );
29+ PAPI_add_named_event (EventSet, " MEM_LOAD_RETIRED:L1_MISS" );
30+ PAPI_add_named_event (EventSet, " MEM_LOAD_RETIRED:L2_HIT" );
31+ PAPI_add_named_event (EventSet, " MEM_LOAD_RETIRED:L2_MISS" );
32+
33+ if (PAPI_start (EventSet) != PAPI_OK )
34+ {
35+ std::cerr << " Error starting counters PAPI\n " ;
36+ }
37+
1538 b.run ([&] { ((void (*)(void )) options->functions [i])(); });
39+
40+ if (PAPI_stop (EventSet, values) != PAPI_OK )
41+ {
42+ std::cerr << " Error stopping counters PAPI\n " ;
43+ }
44+
45+ PAPI_cleanup_eventset (EventSet);
46+ PAPI_destroy_eventset (&EventSet);
47+
48+ std::cout << " #### Cache analysis for " << options->name << " ###\n "
49+ << " L1 cache misses: " << values[0 ] << " , L2 cache misses: " << values[1 ] << " \n "
50+ << " L1 cache hits: " << values[2 ] << " , L2 cache hits: " << values[3 ] << " \n " ;
1651 }
52+
53+ PAPI_shutdown ();
1754}
0 commit comments