-
Notifications
You must be signed in to change notification settings - Fork 0
/
pmu_utils.cc
156 lines (133 loc) · 3.83 KB
/
pmu_utils.cc
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#include <unistd.h>
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <iostream>
#ifdef __linux__
#include <sys/ioctl.h>
#include <linux/perf_event.h>
#include <asm/unistd.h>
#endif
static timespec start, end;
#ifdef __linux__
static inline int sys_perf_event_open(struct perf_event_attr *attr, pid_t pid,
int cpu, int group_fd,
unsigned long flags)
{
attr->size = sizeof(*attr);
return syscall(__NR_perf_event_open, attr, pid, cpu, group_fd, flags);
}
static int cycles_fd;
static int instructions_fd;
static bool pmu_ready = false;
void close_pmu_counters(void)
{
if (pmu_ready) {
close(cycles_fd);
close(instructions_fd);
}
pmu_ready = false;
}
int setup_pmu_counters(void)
{
struct perf_event_attr attr;
memset(&attr, 0, sizeof(attr));
attr.exclude_kernel = 1;
attr.exclude_hv = 1;
attr.disabled = 1;
attr.type = PERF_TYPE_HARDWARE;
attr.config = PERF_COUNT_HW_CPU_CYCLES;
cycles_fd = sys_perf_event_open(&attr, 0, -1, -1, 0);
if (cycles_fd < 0) {
perror("pmu counter not available. sys_perf_event_open cpu cycles");
return -1;
}
/*
* We use cycles_fd as the group leader in order to ensure
* both counters run at the same time and our CPI statistics are
* valid.
*/
attr.disabled = 0; /* The group leader will start/stop us */
attr.type = PERF_TYPE_HARDWARE;
attr.config = PERF_COUNT_HW_INSTRUCTIONS;
instructions_fd = sys_perf_event_open(&attr, 0, -1, cycles_fd, 0);
if (instructions_fd < 0) {
perror("pmu counter not available. sys_perf_event_open hw instructions");
close(cycles_fd);
return -1;
}
pmu_ready = true;
return 0;
}
void start_pmu_counters(void)
{
clock_gettime(CLOCK_MONOTONIC, &start);
if (pmu_ready) {
/* Only need to start and stop the group leader */
ioctl(instructions_fd, PERF_EVENT_IOC_RESET, 0);
ioctl(cycles_fd, PERF_EVENT_IOC_RESET, 0);
ioctl(instructions_fd, PERF_EVENT_IOC_ENABLE);
ioctl(cycles_fd, PERF_EVENT_IOC_ENABLE);
}
}
void stop_pmu_counters(void)
{
if (pmu_ready)
ioctl(cycles_fd, PERF_EVENT_IOC_DISABLE);
clock_gettime(CLOCK_MONOTONIC, &end);
}
void print_pmu_counters(void)
{
size_t res;
unsigned long long cycles;
unsigned long long instructions;
if (pmu_ready) {
res = read(cycles_fd, &cycles, sizeof(unsigned long long));
assert(res == sizeof(unsigned long long));
res = read(instructions_fd, &instructions, sizeof(unsigned long long));
assert(res == sizeof(unsigned long long));
printf("cycles:\t\t%lld\n", cycles);
printf("instructions:\t%lld\n", instructions);
if (instructions > 0)
printf("CPI:\t\t%0.2f\n", (float)cycles/instructions);
}
static timespec delta;
delta.tv_sec = end.tv_sec - start.tv_sec;
delta.tv_nsec = end.tv_nsec - start.tv_nsec;
if (delta.tv_nsec < 0) {
delta.tv_nsec += 1000000000LL;
delta.tv_sec -= 1;
}
std::cout << "CPU Wall Time spent: " << delta.tv_sec << "s and " << delta.tv_nsec << "ns" << std::endl;
}
#else
int setup_pmu_counters(void)
{
std::cout << "No PMU counter available" << std::endl;
return 0;
}
void start_pmu_counters(void)
{
clock_gettime(CLOCK_MONOTONIC, &start);
}
void stop_pmu_counters(void)
{
clock_gettime(CLOCK_MONOTONIC, &end);
}
void close_pmu_counters(void)
{
}
void print_pmu_counters(void)
{
static timespec delta;
delta.tv_sec = end.tv_sec - start.tv_sec;
delta.tv_nsec = end.tv_nsec - start.tv_nsec;
if (delta.tv_nsec < 0) {
delta.tv_nsec += 1000000000LL;
delta.tv_sec -= 1;
}
std::cout << "CPU Wall Time spent: " << delta.tv_sec << "s and " << delta.tv_nsec << "ns" << std::endl;
}
#endif