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