xref: /linux/tools/testing/selftests/powerpc/pmu/ebb/ebb_vs_cpu_event_test.c (revision 3bdab16c55f57a24245c97d707241dd9b48d1a91)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright 2014, Michael Ellerman, IBM Corp.
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 an EBB vs a cpu event - in that order. The EBB should force the cpu
19  * event off the PMU.
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.exclude_kernel = 1;
27 	event->attr.exclude_hv = 1;
28 	event->attr.exclude_idle = 1;
29 
30 	SKIP_IF(require_paranoia_below(1));
31 	FAIL_IF(event_open_with_cpu(event, cpu));
32 	FAIL_IF(event_enable(event));
33 
34 	return 0;
35 }
36 
37 int ebb_vs_cpu_event(void)
38 {
39 	union pipe read_pipe, write_pipe;
40 	struct event event;
41 	int cpu, rc;
42 	pid_t pid;
43 
44 	SKIP_IF(!ebb_is_supported());
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 	/* Signal the child to install its EBB event and wait */
60 	FAIL_IF(sync_with_child(read_pipe, write_pipe));
61 
62 	/* Now try to install our CPU event */
63 	rc = setup_cpu_event(&event, cpu);
64 	if (rc) {
65 		kill_child_and_wait(pid);
66 		return rc;
67 	}
68 
69 	/* Signal the child to run */
70 	FAIL_IF(sync_with_child(read_pipe, write_pipe));
71 
72 	/* .. and wait for it to complete */
73 	FAIL_IF(wait_for_child(pid));
74 	FAIL_IF(event_disable(&event));
75 	FAIL_IF(event_read(&event));
76 
77 	event_report(&event);
78 
79 	/* The cpu event may have run, but we don't expect 100% */
80 	FAIL_IF(event.result.enabled >= event.result.running);
81 
82 	return 0;
83 }
84 
85 int main(void)
86 {
87 	return test_harness(ebb_vs_cpu_event, "ebb_vs_cpu_event");
88 }
89