powerpc/selftests/perf-hwbreak: Coalesce event creation code

perf-hwbreak selftest opens hw-breakpoint event at multiple places for
which it has same code repeated. Coalesce that code into a function.

Signed-off-by: Ravi Bangoria <ravi.bangoria@linux.ibm.com>
Signed-off-by: Michael Ellerman <mpe@ellerman.id.au>
Link: https://lore.kernel.org/r/20210412112218.128183-3-ravi.bangoria@linux.ibm.com
This commit is contained in:
Ravi Bangoria 2021-04-12 16:52:16 +05:30 committed by Michael Ellerman
parent dae4ff8031
commit c9cb0afb4e

View File

@ -38,28 +38,46 @@
#define DAWR_LENGTH_MAX ((0x3f + 1) * 8) #define DAWR_LENGTH_MAX ((0x3f + 1) * 8)
static inline int sys_perf_event_open(struct perf_event_attr *attr, pid_t pid, static void perf_event_attr_set(struct perf_event_attr *attr,
int cpu, int group_fd, __u32 type, __u64 addr, __u64 len,
unsigned long flags) bool exclude_user)
{ {
attr->size = sizeof(*attr); memset(attr, 0, sizeof(struct perf_event_attr));
return syscall(__NR_perf_event_open, attr, pid, cpu, group_fd, flags); attr->type = PERF_TYPE_BREAKPOINT;
attr->size = sizeof(struct perf_event_attr);
attr->bp_type = type;
attr->bp_addr = addr;
attr->bp_len = len;
attr->exclude_kernel = 1;
attr->exclude_hv = 1;
attr->exclude_guest = 1;
attr->exclude_user = exclude_user;
attr->disabled = 1;
}
static int
perf_process_event_open_exclude_user(__u32 type, __u64 addr, __u64 len, bool exclude_user)
{
struct perf_event_attr attr;
perf_event_attr_set(&attr, type, addr, len, exclude_user);
return syscall(__NR_perf_event_open, &attr, getpid(), -1, -1, 0);
}
static int perf_process_event_open(__u32 type, __u64 addr, __u64 len)
{
struct perf_event_attr attr;
perf_event_attr_set(&attr, type, addr, len, 0);
return syscall(__NR_perf_event_open, &attr, getpid(), -1, -1, 0);
} }
static inline bool breakpoint_test(int len) static inline bool breakpoint_test(int len)
{ {
struct perf_event_attr attr;
int fd; int fd;
/* setup counters */
memset(&attr, 0, sizeof(attr));
attr.disabled = 1;
attr.type = PERF_TYPE_BREAKPOINT;
attr.bp_type = HW_BREAKPOINT_R;
/* bp_addr can point anywhere but needs to be aligned */ /* bp_addr can point anywhere but needs to be aligned */
attr.bp_addr = (__u64)(&attr) & 0xfffffffffffff800; fd = perf_process_event_open(HW_BREAKPOINT_R, (__u64)(&fd) & 0xfffffffffffff800, len);
attr.bp_len = len;
fd = sys_perf_event_open(&attr, 0, -1, -1, 0);
if (fd < 0) if (fd < 0)
return false; return false;
close(fd); close(fd);
@ -79,7 +97,6 @@ static inline bool dawr_supported(void)
static int runtestsingle(int readwriteflag, int exclude_user, int arraytest) static int runtestsingle(int readwriteflag, int exclude_user, int arraytest)
{ {
int i,j; int i,j;
struct perf_event_attr attr;
size_t res; size_t res;
unsigned long long breaks, needed; unsigned long long breaks, needed;
int readint; int readint;
@ -89,6 +106,7 @@ static int runtestsingle(int readwriteflag, int exclude_user, int arraytest)
int break_fd; int break_fd;
int loop_num = MAX_LOOPS - (rand() % 100); /* provide some variability */ int loop_num = MAX_LOOPS - (rand() % 100); /* provide some variability */
volatile int *k; volatile int *k;
__u64 len;
/* align to 0x400 boundary as required by DAWR */ /* align to 0x400 boundary as required by DAWR */
readintalign = (int *)(((unsigned long)readintarraybig + 0x7ff) & readintalign = (int *)(((unsigned long)readintarraybig + 0x7ff) &
@ -98,19 +116,11 @@ static int runtestsingle(int readwriteflag, int exclude_user, int arraytest)
if (arraytest) if (arraytest)
ptr = &readintalign[0]; ptr = &readintalign[0];
/* setup counters */ len = arraytest ? DAWR_LENGTH_MAX : sizeof(int);
memset(&attr, 0, sizeof(attr)); break_fd = perf_process_event_open_exclude_user(readwriteflag, (__u64)ptr,
attr.disabled = 1; len, exclude_user);
attr.type = PERF_TYPE_BREAKPOINT;
attr.bp_type = readwriteflag;
attr.bp_addr = (__u64)ptr;
attr.bp_len = sizeof(int);
if (arraytest)
attr.bp_len = DAWR_LENGTH_MAX;
attr.exclude_user = exclude_user;
break_fd = sys_perf_event_open(&attr, 0, -1, -1, 0);
if (break_fd < 0) { if (break_fd < 0) {
perror("sys_perf_event_open"); perror("perf_process_event_open_exclude_user");
exit(1); exit(1);
} }
@ -157,7 +167,6 @@ static int runtest_dar_outside(void)
void *target; void *target;
volatile __u16 temp16; volatile __u16 temp16;
volatile __u64 temp64; volatile __u64 temp64;
struct perf_event_attr attr;
int break_fd; int break_fd;
unsigned long long breaks; unsigned long long breaks;
int fail = 0; int fail = 0;
@ -169,21 +178,11 @@ static int runtest_dar_outside(void)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
/* setup counters */
memset(&attr, 0, sizeof(attr));
attr.disabled = 1;
attr.type = PERF_TYPE_BREAKPOINT;
attr.exclude_kernel = 1;
attr.exclude_hv = 1;
attr.exclude_guest = 1;
attr.bp_type = HW_BREAKPOINT_RW;
/* watch middle half of target array */ /* watch middle half of target array */
attr.bp_addr = (__u64)(target + 2); break_fd = perf_process_event_open(HW_BREAKPOINT_RW, (__u64)(target + 2), 4);
attr.bp_len = 4;
break_fd = sys_perf_event_open(&attr, 0, -1, -1, 0);
if (break_fd < 0) { if (break_fd < 0) {
free(target); free(target);
perror("sys_perf_event_open"); perror("perf_process_event_open");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }