-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpapi-perf-counters-latency.c
61 lines (49 loc) · 1.51 KB
/
papi-perf-counters-latency.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
* papi-perf-counters-timing.c
*
* Test the latency of reading performance counters.
*
* Based on: http://stackoverflow.com/questions/8091182/how-to-read-performance-counters-on-i5-i7-cpus
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <papi.h>
#define NUM_EVENTS 4
static double gettimeofday_double() {
struct timeval now;
gettimeofday(&now, NULL);
return now.tv_sec + now.tv_usec * 1e-6;
}
int main()
{
int event[NUM_EVENTS] = {PAPI_TOT_INS, PAPI_TOT_CYC, PAPI_BR_MSP, PAPI_L1_DCM };
long long values[NUM_EVENTS];
int i, n = 100000;
/* Start counting events */
if (PAPI_start_counters(event, NUM_EVENTS) != PAPI_OK) {
fprintf(stderr, "PAPI_start_counters - FAILED\n");
exit(1);
}
double start = gettimeofday_double();
/* Read the counters */
for (i = 0; i < n; i++) {
if (PAPI_read_counters(values, NUM_EVENTS) != PAPI_OK) {
fprintf(stderr, "PAPI_read_counters - FAILED\n");
exit(1);
}
}
double end = gettimeofday_double();
printf("Average PAPI_read_counters() latency: %f nanoseconds\n", (end - start) / n * 1000000000.0);
printf("Total instructions: %lld\n", values[0]);
printf("Total cycles: %lld\n", values[1]);
printf("Instr per cycle: %2.3f\n", (double)values[0] / (double) values[1]);
printf("Branches mispredicted: %lld\n", values[2]);
printf("L1 Cache misses: %lld\n", values[3]);
/* Stop counting events */
if (PAPI_stop_counters(values, NUM_EVENTS) != PAPI_OK) {
fprintf(stderr, "PAPI_stoped_counters - FAILED\n");
exit(1);
}
return 0;
}