xref: /openbmc/linux/tools/perf/arch/x86/tests/intel-cqm.c (revision b830f94f)
1 // SPDX-License-Identifier: GPL-2.0
2 #include "tests/tests.h"
3 #include "perf.h"
4 #include "cloexec.h"
5 #include "debug.h"
6 #include "evlist.h"
7 #include "evsel.h"
8 #include "arch-tests.h"
9 #include "util.h"
10 
11 #include <signal.h>
12 #include <sys/mman.h>
13 #include <sys/wait.h>
14 #include <errno.h>
15 #include <string.h>
16 
17 static pid_t spawn(void)
18 {
19 	pid_t pid;
20 
21 	pid = fork();
22 	if (pid)
23 		return pid;
24 
25 	while(1)
26 		sleep(5);
27 	return 0;
28 }
29 
30 /*
31  * Create an event group that contains both a sampled hardware
32  * (cpu-cycles) and software (intel_cqm/llc_occupancy/) event. We then
33  * wait for the hardware perf counter to overflow and generate a PMI,
34  * which triggers an event read for both of the events in the group.
35  *
36  * Since reading Intel CQM event counters requires sending SMP IPIs, the
37  * CQM pmu needs to handle the above situation gracefully, and return
38  * the last read counter value to avoid triggering a WARN_ON_ONCE() in
39  * smp_call_function_many() caused by sending IPIs from NMI context.
40  */
41 int test__intel_cqm_count_nmi_context(struct test *test __maybe_unused, int subtest __maybe_unused)
42 {
43 	struct perf_evlist *evlist = NULL;
44 	struct perf_evsel *evsel = NULL;
45 	struct perf_event_attr pe;
46 	int i, fd[2], flag, ret;
47 	size_t mmap_len;
48 	void *event;
49 	pid_t pid;
50 	int err = TEST_FAIL;
51 
52 	flag = perf_event_open_cloexec_flag();
53 
54 	evlist = perf_evlist__new();
55 	if (!evlist) {
56 		pr_debug("perf_evlist__new failed\n");
57 		return TEST_FAIL;
58 	}
59 
60 	ret = parse_events(evlist, "intel_cqm/llc_occupancy/", NULL);
61 	if (ret) {
62 		pr_debug("parse_events failed, is \"intel_cqm/llc_occupancy/\" available?\n");
63 		err = TEST_SKIP;
64 		goto out;
65 	}
66 
67 	evsel = perf_evlist__first(evlist);
68 	if (!evsel) {
69 		pr_debug("perf_evlist__first failed\n");
70 		goto out;
71 	}
72 
73 	memset(&pe, 0, sizeof(pe));
74 	pe.size = sizeof(pe);
75 
76 	pe.type = PERF_TYPE_HARDWARE;
77 	pe.config = PERF_COUNT_HW_CPU_CYCLES;
78 	pe.read_format = PERF_FORMAT_GROUP;
79 
80 	pe.sample_period = 128;
81 	pe.sample_type = PERF_SAMPLE_IP | PERF_SAMPLE_READ;
82 
83 	pid = spawn();
84 
85 	fd[0] = sys_perf_event_open(&pe, pid, -1, -1, flag);
86 	if (fd[0] < 0) {
87 		pr_debug("failed to open event\n");
88 		goto out;
89 	}
90 
91 	memset(&pe, 0, sizeof(pe));
92 	pe.size = sizeof(pe);
93 
94 	pe.type = evsel->attr.type;
95 	pe.config = evsel->attr.config;
96 
97 	fd[1] = sys_perf_event_open(&pe, pid, -1, fd[0], flag);
98 	if (fd[1] < 0) {
99 		pr_debug("failed to open event\n");
100 		goto out;
101 	}
102 
103 	/*
104 	 * Pick a power-of-two number of pages + 1 for the meta-data
105 	 * page (struct perf_event_mmap_page). See tools/perf/design.txt.
106 	 */
107 	mmap_len = page_size * 65;
108 
109 	event = mmap(NULL, mmap_len, PROT_READ, MAP_SHARED, fd[0], 0);
110 	if (event == (void *)(-1)) {
111 		pr_debug("failed to mmap %d\n", errno);
112 		goto out;
113 	}
114 
115 	sleep(1);
116 
117 	err = TEST_OK;
118 
119 	munmap(event, mmap_len);
120 
121 	for (i = 0; i < 2; i++)
122 		close(fd[i]);
123 
124 	kill(pid, SIGKILL);
125 	wait(NULL);
126 out:
127 	perf_evlist__delete(evlist);
128 	return err;
129 }
130