1
+ #pragma once
2
+
3
+ #include < stdlib.h>
4
+ #include < stdio.h>
5
+ #include < unistd.h>
6
+ #include < string.h>
7
+ #include < sys/ioctl.h>
8
+ #include < linux/perf_event.h>
9
+ #include < asm/unistd.h>
10
+ #include < sys/mman.h>
11
+ #include < vector>
12
+ #include < cstdio>
13
+
14
+ __u32 buf_size_shift = 8 ;
15
+ __u32 PERF_FD = 0 ;
16
+
17
+ static long perf_event_open (perf_event_attr *hw_event, pid_t pid, int cpu, int group_fd, unsigned long flags)
18
+ {
19
+ int ret;
20
+ ret = syscall (__NR_perf_event_open, hw_event, pid, cpu,
21
+ group_fd, flags);
22
+ return ret;
23
+ }
24
+
25
+ static unsigned perf_mmap_size (int buf_size_shift)
26
+ {
27
+ return ((1U << buf_size_shift) + 1 ) * sysconf (_SC_PAGESIZE);
28
+ }
29
+
30
+
31
+ int mumu_perf_init ()
32
+ {
33
+ struct perf_event_attr pe;
34
+ long long count;
35
+ int fd;
36
+
37
+ memset (&pe, 0 , sizeof (pe));
38
+ pe.type = PERF_TYPE_HW_CACHE;
39
+ pe.size = sizeof (pe);
40
+ pe.config = (PERF_COUNT_HW_CACHE_LL) | (PERF_COUNT_HW_CACHE_OP_READ << 8 ) | (PERF_COUNT_HW_CACHE_RESULT_MISS << 16 );
41
+
42
+ pe.exclude_host = 1 ;
43
+ pe.exclude_hv = 0 ;
44
+
45
+ fd = perf_event_open (&pe, 0 , -1 , -1 ,0 );
46
+ if (fd == -1 )
47
+ {
48
+ fprintf (stderr, " Error opening leader %llx\n " , pe.config );
49
+ exit (EXIT_FAILURE);
50
+ }
51
+
52
+ struct perf_event_mmap_page *mpage;
53
+ mpage = (perf_event_mmap_page *) mmap (NULL , perf_mmap_size (buf_size_shift), PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0 );
54
+
55
+ if (mpage == (struct perf_event_mmap_page *)-1L ) {
56
+ close (fd);
57
+ exit (EXIT_FAILURE);
58
+ }
59
+
60
+ PERF_FD = fd;
61
+ return PERF_FD;
62
+ }
63
+
64
+
65
+ void mumu_perf_reset_counters ()
66
+ {
67
+ ioctl (PERF_FD, PERF_EVENT_IOC_RESET, 0 );
68
+ return ;
69
+ }
70
+
71
+ void mumu_perf_start_counters ()
72
+ {
73
+ ioctl (PERF_FD, PERF_EVENT_IOC_ENABLE, 0 );
74
+ return ;
75
+ }
76
+
77
+ void mumu_perf_stop_counters ()
78
+ {
79
+ ioctl (PERF_FD, PERF_EVENT_IOC_DISABLE, 0 );
80
+ return ;
81
+ }
82
+
83
+ long long mumu_perf_read_counters ()
84
+ {
85
+ long long count;
86
+ read (PERF_FD, &count, sizeof (count));
87
+ return count;
88
+ }
0 commit comments