File srhd.c added (mode: 100644) (index 0000000..fb84fdd) |
|
1 |
|
#include <stdlib.h> |
|
2 |
|
#include <stdio.h> |
|
3 |
|
#include <unistd.h> |
|
4 |
|
#include <string.h> |
|
5 |
|
#include <errno.h> |
|
6 |
|
#include <string.h> |
|
7 |
|
#include <sys/ioctl.h> |
|
8 |
|
#include <linux/perf_event.h> |
|
9 |
|
#include <asm/unistd.h> |
|
10 |
|
|
|
11 |
|
static long perf_event_open(struct perf_event_attr *hw_event, pid_t pid, |
|
12 |
|
int cpu, int group_fd, unsigned long flags) |
|
13 |
|
{ |
|
14 |
|
return syscall(__NR_perf_event_open, hw_event, pid, cpu, group_fd, flags); |
|
15 |
|
} |
|
16 |
|
|
|
17 |
|
static void test_function(void) |
|
18 |
|
{ |
|
19 |
|
int i, j; |
|
20 |
|
|
|
21 |
|
for (i = 0; i < 1000; i++) { |
|
22 |
|
asm volatile("clflush (%0);" : : "r" (&j) : "memory"); |
|
23 |
|
} |
|
24 |
|
} |
|
25 |
|
|
|
26 |
|
int main(void) |
|
27 |
|
{ |
|
28 |
|
struct perf_event_attr attr; |
|
29 |
|
long long count; |
|
30 |
|
int fd; |
|
31 |
|
ssize_t n; |
|
32 |
|
unsigned char buf[4096]; |
|
33 |
|
|
|
34 |
|
memset(&attr, 0, sizeof(struct perf_event_attr)); |
|
35 |
|
attr.type = PERF_TYPE_HARDWARE; |
|
36 |
|
attr.size = sizeof(struct perf_event_attr); |
|
37 |
|
attr.config = PERF_COUNT_HW_INSTRUCTIONS; /* CHECK: PERF_TYPE_HW_CACHE */ |
|
38 |
|
attr.sample_freq = 990; |
|
39 |
|
attr.freq = 1; |
|
40 |
|
attr.enable_on_exec = 1; |
|
41 |
|
attr.disabled = 1; |
|
42 |
|
attr.exclude_kernel = 1; |
|
43 |
|
attr.exclude_hv = 1; |
|
44 |
|
attr.wakeup_events = 16; |
|
45 |
|
|
|
46 |
|
fd = perf_event_open(&attr, 0, -1, -1, PERF_FLAG_FD_CLOEXEC); |
|
47 |
|
if (fd == -1) { |
|
48 |
|
fprintf(stderr, "Cannot call perf_event_open, config %llx\n", |
|
49 |
|
attr.config); |
|
50 |
|
return EXIT_FAILURE; |
|
51 |
|
} |
|
52 |
|
|
|
53 |
|
ioctl(fd, PERF_EVENT_IOC_RESET, 0); |
|
54 |
|
ioctl(fd, PERF_EVENT_IOC_ENABLE, 0); |
|
55 |
|
test_function(); |
|
56 |
|
ioctl(fd, PERF_EVENT_IOC_DISABLE, 0); |
|
57 |
|
|
|
58 |
|
n = read(fd, buf, sizeof(buf)); |
|
59 |
|
fprintf(stderr, "n = %zd\n", n); |
|
60 |
|
if (n <= 0) { |
|
61 |
|
fprintf(stderr, "Cannot read: %s!\n", strerror(errno)); |
|
62 |
|
return EXIT_FAILURE; |
|
63 |
|
} |
|
64 |
|
|
|
65 |
|
memcpy(&count, buf, sizeof(long long)); |
|
66 |
|
printf("Instructions: %lld\n", count); |
|
67 |
|
|
|
68 |
|
close(fd); |
|
69 |
|
|
|
70 |
|
return 0; |
|
71 |
|
} |