Skip to content

Commit 3752e45

Browse files
mpeozbenh
authored andcommitted
selftests/powerpc: Add tests of PMU EBBs
The Power8 Performance Monitor Unit (PMU) has a new feature called Event Based Branches (EBB). This commit adds tests of the kernel API for using EBBs. Signed-off-by: Michael Ellerman <mpe@ellerman.id.au> Signed-off-by: Benjamin Herrenschmidt <benh@kernel.crashing.org>
1 parent 33b4819 commit 3752e45

34 files changed

Lines changed: 3913 additions & 4 deletions

‎tools/testing/selftests/powerpc/pmu/Makefile‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,38 @@ noarg:
44
PROGS := count_instructions
55
EXTRA_SOURCES := ../harness.c event.c
66

7-
all: $(PROGS)
7+
all: $(PROGS) sub_all
88

99
$(PROGS): $(EXTRA_SOURCES)
1010

1111
# loop.S can only be built 64-bit
1212
count_instructions: loop.S count_instructions.c $(EXTRA_SOURCES)
1313
$(CC) $(CFLAGS) -m64 -o $@ $^
1414

15-
run_tests: all
15+
run_tests: all sub_run_tests
1616
@-for PROG in $(PROGS); do \
1717
./$$PROG; \
1818
done;
1919

20-
clean:
20+
clean: sub_clean
2121
rm -f $(PROGS) loop.o
2222

23-
.PHONY: all run_tests clean
23+
24+
SUB_TARGETS = ebb
25+
26+
sub_all:
27+
@for TARGET in $(SUB_TARGETS); do \
28+
$(MAKE) -C $$TARGET all; \
29+
done;
30+
31+
sub_run_tests: all
32+
@for TARGET in $(SUB_TARGETS); do \
33+
$(MAKE) -C $$TARGET run_tests; \
34+
done;
35+
36+
sub_clean:
37+
@for TARGET in $(SUB_TARGETS); do \
38+
$(MAKE) -C $$TARGET clean; \
39+
done;
40+
41+
.PHONY: all run_tests clean sub_all sub_run_tests sub_clean
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
noarg:
2+
$(MAKE) -C ../../
3+
4+
# The EBB handler is 64-bit code and everything links against it
5+
CFLAGS += -m64
6+
7+
PROGS := reg_access_test event_attributes_test cycles_test \
8+
cycles_with_freeze_test pmc56_overflow_test \
9+
ebb_vs_cpu_event_test cpu_event_vs_ebb_test \
10+
cpu_event_pinned_vs_ebb_test task_event_vs_ebb_test \
11+
task_event_pinned_vs_ebb_test multi_ebb_procs_test \
12+
multi_counter_test pmae_handling_test \
13+
close_clears_pmcc_test instruction_count_test \
14+
fork_cleanup_test ebb_on_child_test \
15+
ebb_on_willing_child_test back_to_back_ebbs_test \
16+
lost_exception_test no_handler_test
17+
18+
all: $(PROGS)
19+
20+
$(PROGS): ../../harness.c ../event.c ../lib.c ebb.c ebb_handler.S trace.c
21+
22+
instruction_count_test: ../loop.S
23+
24+
lost_exception_test: ../lib.c
25+
26+
run_tests: all
27+
@-for PROG in $(PROGS); do \
28+
./$$PROG; \
29+
done;
30+
31+
clean:
32+
rm -f $(PROGS)
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* Copyright 2014, Michael Ellerman, IBM Corp.
3+
* Licensed under GPLv2.
4+
*/
5+
6+
#include <stdbool.h>
7+
#include <stdio.h>
8+
#include <stdlib.h>
9+
10+
#include "ebb.h"
11+
12+
13+
#define NUMBER_OF_EBBS 50
14+
15+
/*
16+
* Test that if we overflow the counter while in the EBB handler, we take
17+
* another EBB on exiting from the handler.
18+
*
19+
* We do this by counting with a stupidly low sample period, causing us to
20+
* overflow the PMU while we're still in the EBB handler, leading to another
21+
* EBB.
22+
*
23+
* We get out of what would otherwise be an infinite loop by leaving the
24+
* counter frozen once we've taken enough EBBs.
25+
*/
26+
27+
static void ebb_callee(void)
28+
{
29+
uint64_t siar, val;
30+
31+
val = mfspr(SPRN_BESCR);
32+
if (!(val & BESCR_PMEO)) {
33+
ebb_state.stats.spurious++;
34+
goto out;
35+
}
36+
37+
ebb_state.stats.ebb_count++;
38+
trace_log_counter(ebb_state.trace, ebb_state.stats.ebb_count);
39+
40+
/* Resets the PMC */
41+
count_pmc(1, sample_period);
42+
43+
out:
44+
if (ebb_state.stats.ebb_count == NUMBER_OF_EBBS)
45+
/* Reset but leave counters frozen */
46+
reset_ebb_with_clear_mask(MMCR0_PMAO);
47+
else
48+
/* Unfreezes */
49+
reset_ebb();
50+
51+
/* Do some stuff to chew some cycles and pop the counter */
52+
siar = mfspr(SPRN_SIAR);
53+
trace_log_reg(ebb_state.trace, SPRN_SIAR, siar);
54+
55+
val = mfspr(SPRN_PMC1);
56+
trace_log_reg(ebb_state.trace, SPRN_PMC1, val);
57+
58+
val = mfspr(SPRN_MMCR0);
59+
trace_log_reg(ebb_state.trace, SPRN_MMCR0, val);
60+
}
61+
62+
int back_to_back_ebbs(void)
63+
{
64+
struct event event;
65+
66+
event_init_named(&event, 0x1001e, "cycles");
67+
event_leader_ebb_init(&event);
68+
69+
event.attr.exclude_kernel = 1;
70+
event.attr.exclude_hv = 1;
71+
event.attr.exclude_idle = 1;
72+
73+
FAIL_IF(event_open(&event));
74+
75+
setup_ebb_handler(ebb_callee);
76+
77+
FAIL_IF(ebb_event_enable(&event));
78+
79+
sample_period = 5;
80+
81+
ebb_freeze_pmcs();
82+
mtspr(SPRN_PMC1, pmc_sample_period(sample_period));
83+
ebb_global_enable();
84+
ebb_unfreeze_pmcs();
85+
86+
while (ebb_state.stats.ebb_count < NUMBER_OF_EBBS)
87+
FAIL_IF(core_busy_loop());
88+
89+
ebb_global_disable();
90+
ebb_freeze_pmcs();
91+
92+
count_pmc(1, sample_period);
93+
94+
dump_ebb_state();
95+
96+
event_close(&event);
97+
98+
FAIL_IF(ebb_state.stats.ebb_count != NUMBER_OF_EBBS);
99+
100+
return 0;
101+
}
102+
103+
int main(void)
104+
{
105+
return test_harness(back_to_back_ebbs, "back_to_back_ebbs");
106+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright 2014, Michael Ellerman, IBM Corp.
3+
* Licensed under GPLv2.
4+
*/
5+
6+
#include <stdio.h>
7+
#include <stdlib.h>
8+
#include <setjmp.h>
9+
#include <signal.h>
10+
11+
#include "ebb.h"
12+
13+
14+
/*
15+
* Test that closing the EBB event clears MMCR0_PMCC, preventing further access
16+
* by userspace to the PMU hardware.
17+
*/
18+
19+
int close_clears_pmcc(void)
20+
{
21+
struct event event;
22+
23+
event_init_named(&event, 0x1001e, "cycles");
24+
event_leader_ebb_init(&event);
25+
26+
FAIL_IF(event_open(&event));
27+
28+
ebb_enable_pmc_counting(1);
29+
setup_ebb_handler(standard_ebb_callee);
30+
ebb_global_enable();
31+
FAIL_IF(ebb_event_enable(&event));
32+
33+
mtspr(SPRN_PMC1, pmc_sample_period(sample_period));
34+
35+
while (ebb_state.stats.ebb_count < 1)
36+
FAIL_IF(core_busy_loop());
37+
38+
ebb_global_disable();
39+
event_close(&event);
40+
41+
FAIL_IF(ebb_state.stats.ebb_count == 0);
42+
43+
/* The real test is here, do we take a SIGILL when writing PMU regs now
44+
* that we have closed the event. We expect that we will. */
45+
46+
FAIL_IF(catch_sigill(write_pmc1));
47+
48+
/* We should still be able to read EBB regs though */
49+
mfspr(SPRN_EBBHR);
50+
mfspr(SPRN_EBBRR);
51+
mfspr(SPRN_BESCR);
52+
53+
return 0;
54+
}
55+
56+
int main(void)
57+
{
58+
return test_harness(close_clears_pmcc, "close_clears_pmcc");
59+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
* Copyright 2014, Michael Ellerman, IBM Corp.
3+
* Licensed under GPLv2.
4+
*/
5+
6+
#include <signal.h>
7+
#include <stdio.h>
8+
#include <stdlib.h>
9+
#include <stdbool.h>
10+
#include <sys/types.h>
11+
#include <sys/wait.h>
12+
#include <unistd.h>
13+
14+
#include "ebb.h"
15+
16+
17+
/*
18+
* Tests a pinned cpu event vs an EBB - in that order. The pinned cpu event
19+
* should remain and the EBB event should fail to enable.
20+
*/
21+
22+
static int setup_cpu_event(struct event *event, int cpu)
23+
{
24+
event_init_named(event, 0x400FA, "PM_RUN_INST_CMPL");
25+
26+
event->attr.pinned = 1;
27+
28+
event->attr.exclude_kernel = 1;
29+
event->attr.exclude_hv = 1;
30+
event->attr.exclude_idle = 1;
31+
32+
SKIP_IF(require_paranoia_below(1));
33+
FAIL_IF(event_open_with_cpu(event, cpu));
34+
FAIL_IF(event_enable(event));
35+
36+
return 0;
37+
}
38+
39+
int cpu_event_pinned_vs_ebb(void)
40+
{
41+
union pipe read_pipe, write_pipe;
42+
struct event event;
43+
int cpu, rc;
44+
pid_t pid;
45+
46+
cpu = pick_online_cpu();
47+
FAIL_IF(cpu < 0);
48+
FAIL_IF(bind_to_cpu(cpu));
49+
50+
FAIL_IF(pipe(read_pipe.fds) == -1);
51+
FAIL_IF(pipe(write_pipe.fds) == -1);
52+
53+
pid = fork();
54+
if (pid == 0) {
55+
/* NB order of pipes looks reversed */
56+
exit(ebb_child(write_pipe, read_pipe));
57+
}
58+
59+
/* We setup the cpu event first */
60+
rc = setup_cpu_event(&event, cpu);
61+
if (rc) {
62+
kill_child_and_wait(pid);
63+
return rc;
64+
}
65+
66+
/* Signal the child to install its EBB event and wait */
67+
if (sync_with_child(read_pipe, write_pipe))
68+
/* If it fails, wait for it to exit */
69+
goto wait;
70+
71+
/* Signal the child to run */
72+
FAIL_IF(sync_with_child(read_pipe, write_pipe));
73+
74+
wait:
75+
/* We expect it to fail to read the event */
76+
FAIL_IF(wait_for_child(pid) != 2);
77+
78+
FAIL_IF(event_disable(&event));
79+
FAIL_IF(event_read(&event));
80+
81+
event_report(&event);
82+
83+
/* The cpu event should have run */
84+
FAIL_IF(event.result.value == 0);
85+
FAIL_IF(event.result.enabled != event.result.running);
86+
87+
return 0;
88+
}
89+
90+
int main(void)
91+
{
92+
return test_harness(cpu_event_pinned_vs_ebb, "cpu_event_pinned_vs_ebb");
93+
}

0 commit comments

Comments
 (0)