Loading...
1// SPDX-License-Identifier: GPL-2.0
2#include <linux/compiler.h>
3#include <linux/string.h>
4#include <errno.h>
5#include <inttypes.h>
6#include <string.h>
7#include <sys/wait.h>
8#include <perf/cpumap.h>
9#include "tests.h"
10#include "evlist.h"
11#include "evsel.h"
12#include "debug.h"
13#include "parse-events.h"
14#include "thread_map.h"
15#include "target.h"
16
17static int attach__enable_on_exec(struct evlist *evlist)
18{
19 struct evsel *evsel = evlist__last(evlist);
20 struct target target = {
21 .uid = UINT_MAX,
22 };
23 const char *argv[] = { "true", NULL, };
24 char sbuf[STRERR_BUFSIZE];
25 int err;
26
27 pr_debug("attaching to spawned child, enable on exec\n");
28
29 err = perf_evlist__create_maps(evlist, &target);
30 if (err < 0) {
31 pr_debug("Not enough memory to create thread/cpu maps\n");
32 return err;
33 }
34
35 err = perf_evlist__prepare_workload(evlist, &target, argv, false, NULL);
36 if (err < 0) {
37 pr_debug("Couldn't run the workload!\n");
38 return err;
39 }
40
41 evsel->core.attr.enable_on_exec = 1;
42
43 err = evlist__open(evlist);
44 if (err < 0) {
45 pr_debug("perf_evlist__open: %s\n",
46 str_error_r(errno, sbuf, sizeof(sbuf)));
47 return err;
48 }
49
50 return perf_evlist__start_workload(evlist) == 1 ? TEST_OK : TEST_FAIL;
51}
52
53static int detach__enable_on_exec(struct evlist *evlist)
54{
55 waitpid(evlist->workload.pid, NULL, 0);
56 return 0;
57}
58
59static int attach__current_disabled(struct evlist *evlist)
60{
61 struct evsel *evsel = evlist__last(evlist);
62 struct perf_thread_map *threads;
63 int err;
64
65 pr_debug("attaching to current thread as disabled\n");
66
67 threads = thread_map__new(-1, getpid(), UINT_MAX);
68 if (threads == NULL) {
69 pr_debug("thread_map__new\n");
70 return -1;
71 }
72
73 evsel->core.attr.disabled = 1;
74
75 err = perf_evsel__open_per_thread(evsel, threads);
76 if (err) {
77 pr_debug("Failed to open event cpu-clock:u\n");
78 return err;
79 }
80
81 perf_thread_map__put(threads);
82 return evsel__enable(evsel) == 0 ? TEST_OK : TEST_FAIL;
83}
84
85static int attach__current_enabled(struct evlist *evlist)
86{
87 struct evsel *evsel = evlist__last(evlist);
88 struct perf_thread_map *threads;
89 int err;
90
91 pr_debug("attaching to current thread as enabled\n");
92
93 threads = thread_map__new(-1, getpid(), UINT_MAX);
94 if (threads == NULL) {
95 pr_debug("failed to call thread_map__new\n");
96 return -1;
97 }
98
99 err = perf_evsel__open_per_thread(evsel, threads);
100
101 perf_thread_map__put(threads);
102 return err == 0 ? TEST_OK : TEST_FAIL;
103}
104
105static int detach__disable(struct evlist *evlist)
106{
107 struct evsel *evsel = evlist__last(evlist);
108
109 return evsel__enable(evsel);
110}
111
112static int attach__cpu_disabled(struct evlist *evlist)
113{
114 struct evsel *evsel = evlist__last(evlist);
115 struct perf_cpu_map *cpus;
116 int err;
117
118 pr_debug("attaching to CPU 0 as enabled\n");
119
120 cpus = perf_cpu_map__new("0");
121 if (cpus == NULL) {
122 pr_debug("failed to call perf_cpu_map__new\n");
123 return -1;
124 }
125
126 evsel->core.attr.disabled = 1;
127
128 err = perf_evsel__open_per_cpu(evsel, cpus);
129 if (err) {
130 if (err == -EACCES)
131 return TEST_SKIP;
132
133 pr_debug("Failed to open event cpu-clock:u\n");
134 return err;
135 }
136
137 perf_cpu_map__put(cpus);
138 return evsel__enable(evsel);
139}
140
141static int attach__cpu_enabled(struct evlist *evlist)
142{
143 struct evsel *evsel = evlist__last(evlist);
144 struct perf_cpu_map *cpus;
145 int err;
146
147 pr_debug("attaching to CPU 0 as enabled\n");
148
149 cpus = perf_cpu_map__new("0");
150 if (cpus == NULL) {
151 pr_debug("failed to call perf_cpu_map__new\n");
152 return -1;
153 }
154
155 err = perf_evsel__open_per_cpu(evsel, cpus);
156 if (err == -EACCES)
157 return TEST_SKIP;
158
159 perf_cpu_map__put(cpus);
160 return err ? TEST_FAIL : TEST_OK;
161}
162
163static int test_times(int (attach)(struct evlist *),
164 int (detach)(struct evlist *))
165{
166 struct perf_counts_values count;
167 struct evlist *evlist = NULL;
168 struct evsel *evsel;
169 int err = -1, i;
170
171 evlist = evlist__new();
172 if (!evlist) {
173 pr_debug("failed to create event list\n");
174 goto out_err;
175 }
176
177 err = parse_events(evlist, "cpu-clock:u", NULL);
178 if (err) {
179 pr_debug("failed to parse event cpu-clock:u\n");
180 goto out_err;
181 }
182
183 evsel = evlist__last(evlist);
184 evsel->core.attr.read_format |=
185 PERF_FORMAT_TOTAL_TIME_ENABLED |
186 PERF_FORMAT_TOTAL_TIME_RUNNING;
187
188 err = attach(evlist);
189 if (err == TEST_SKIP) {
190 pr_debug(" SKIP : not enough rights\n");
191 return err;
192 }
193
194 TEST_ASSERT_VAL("failed to attach", !err);
195
196 for (i = 0; i < 100000000; i++) { }
197
198 TEST_ASSERT_VAL("failed to detach", !detach(evlist));
199
200 perf_evsel__read(&evsel->core, 0, 0, &count);
201
202 err = !(count.ena == count.run);
203
204 pr_debug(" %s: ena %" PRIu64", run %" PRIu64"\n",
205 !err ? "OK " : "FAILED",
206 count.ena, count.run);
207
208out_err:
209 evlist__delete(evlist);
210 return !err ? TEST_OK : TEST_FAIL;
211}
212
213/*
214 * This test creates software event 'cpu-clock'
215 * attaches it in several ways (explained below)
216 * and checks that enabled and running times
217 * match.
218 */
219int test__event_times(struct test *test __maybe_unused, int subtest __maybe_unused)
220{
221 int err, ret = 0;
222
223#define _T(attach, detach) \
224 err = test_times(attach, detach); \
225 if (err && (ret == TEST_OK || ret == TEST_SKIP)) \
226 ret = err;
227
228 /* attach on newly spawned process after exec */
229 _T(attach__enable_on_exec, detach__enable_on_exec)
230 /* attach on current process as enabled */
231 _T(attach__current_enabled, detach__disable)
232 /* attach on current process as disabled */
233 _T(attach__current_disabled, detach__disable)
234 /* attach on cpu as disabled */
235 _T(attach__cpu_disabled, detach__disable)
236 /* attach on cpu as enabled */
237 _T(attach__cpu_enabled, detach__disable)
238
239#undef _T
240 return ret;
241}
1// SPDX-License-Identifier: GPL-2.0
2#include <linux/compiler.h>
3#include <errno.h>
4#include <inttypes.h>
5#include <string.h>
6#include <sys/wait.h>
7#include "tests.h"
8#include "evlist.h"
9#include "evsel.h"
10#include "util.h"
11#include "debug.h"
12#include "thread_map.h"
13#include "target.h"
14
15static int attach__enable_on_exec(struct perf_evlist *evlist)
16{
17 struct perf_evsel *evsel = perf_evlist__last(evlist);
18 struct target target = {
19 .uid = UINT_MAX,
20 };
21 const char *argv[] = { "true", NULL, };
22 char sbuf[STRERR_BUFSIZE];
23 int err;
24
25 pr_debug("attaching to spawned child, enable on exec\n");
26
27 err = perf_evlist__create_maps(evlist, &target);
28 if (err < 0) {
29 pr_debug("Not enough memory to create thread/cpu maps\n");
30 return err;
31 }
32
33 err = perf_evlist__prepare_workload(evlist, &target, argv, false, NULL);
34 if (err < 0) {
35 pr_debug("Couldn't run the workload!\n");
36 return err;
37 }
38
39 evsel->attr.enable_on_exec = 1;
40
41 err = perf_evlist__open(evlist);
42 if (err < 0) {
43 pr_debug("perf_evlist__open: %s\n",
44 str_error_r(errno, sbuf, sizeof(sbuf)));
45 return err;
46 }
47
48 return perf_evlist__start_workload(evlist) == 1 ? TEST_OK : TEST_FAIL;
49}
50
51static int detach__enable_on_exec(struct perf_evlist *evlist)
52{
53 waitpid(evlist->workload.pid, NULL, 0);
54 return 0;
55}
56
57static int attach__current_disabled(struct perf_evlist *evlist)
58{
59 struct perf_evsel *evsel = perf_evlist__last(evlist);
60 struct thread_map *threads;
61 int err;
62
63 pr_debug("attaching to current thread as disabled\n");
64
65 threads = thread_map__new(-1, getpid(), UINT_MAX);
66 if (threads == NULL) {
67 pr_debug("thread_map__new\n");
68 return -1;
69 }
70
71 evsel->attr.disabled = 1;
72
73 err = perf_evsel__open_per_thread(evsel, threads);
74 if (err) {
75 pr_debug("Failed to open event cpu-clock:u\n");
76 return err;
77 }
78
79 thread_map__put(threads);
80 return perf_evsel__enable(evsel) == 0 ? TEST_OK : TEST_FAIL;
81}
82
83static int attach__current_enabled(struct perf_evlist *evlist)
84{
85 struct perf_evsel *evsel = perf_evlist__last(evlist);
86 struct thread_map *threads;
87 int err;
88
89 pr_debug("attaching to current thread as enabled\n");
90
91 threads = thread_map__new(-1, getpid(), UINT_MAX);
92 if (threads == NULL) {
93 pr_debug("failed to call thread_map__new\n");
94 return -1;
95 }
96
97 err = perf_evsel__open_per_thread(evsel, threads);
98
99 thread_map__put(threads);
100 return err == 0 ? TEST_OK : TEST_FAIL;
101}
102
103static int detach__disable(struct perf_evlist *evlist)
104{
105 struct perf_evsel *evsel = perf_evlist__last(evlist);
106
107 return perf_evsel__enable(evsel);
108}
109
110static int attach__cpu_disabled(struct perf_evlist *evlist)
111{
112 struct perf_evsel *evsel = perf_evlist__last(evlist);
113 struct cpu_map *cpus;
114 int err;
115
116 pr_debug("attaching to CPU 0 as enabled\n");
117
118 cpus = cpu_map__new("0");
119 if (cpus == NULL) {
120 pr_debug("failed to call cpu_map__new\n");
121 return -1;
122 }
123
124 evsel->attr.disabled = 1;
125
126 err = perf_evsel__open_per_cpu(evsel, cpus);
127 if (err) {
128 if (err == -EACCES)
129 return TEST_SKIP;
130
131 pr_debug("Failed to open event cpu-clock:u\n");
132 return err;
133 }
134
135 cpu_map__put(cpus);
136 return perf_evsel__enable(evsel);
137}
138
139static int attach__cpu_enabled(struct perf_evlist *evlist)
140{
141 struct perf_evsel *evsel = perf_evlist__last(evlist);
142 struct cpu_map *cpus;
143 int err;
144
145 pr_debug("attaching to CPU 0 as enabled\n");
146
147 cpus = cpu_map__new("0");
148 if (cpus == NULL) {
149 pr_debug("failed to call cpu_map__new\n");
150 return -1;
151 }
152
153 err = perf_evsel__open_per_cpu(evsel, cpus);
154 if (err == -EACCES)
155 return TEST_SKIP;
156
157 cpu_map__put(cpus);
158 return err ? TEST_FAIL : TEST_OK;
159}
160
161static int test_times(int (attach)(struct perf_evlist *),
162 int (detach)(struct perf_evlist *))
163{
164 struct perf_counts_values count;
165 struct perf_evlist *evlist = NULL;
166 struct perf_evsel *evsel;
167 int err = -1, i;
168
169 evlist = perf_evlist__new();
170 if (!evlist) {
171 pr_debug("failed to create event list\n");
172 goto out_err;
173 }
174
175 err = parse_events(evlist, "cpu-clock:u", NULL);
176 if (err) {
177 pr_debug("failed to parse event cpu-clock:u\n");
178 goto out_err;
179 }
180
181 evsel = perf_evlist__last(evlist);
182 evsel->attr.read_format |=
183 PERF_FORMAT_TOTAL_TIME_ENABLED |
184 PERF_FORMAT_TOTAL_TIME_RUNNING;
185
186 err = attach(evlist);
187 if (err == TEST_SKIP) {
188 pr_debug(" SKIP : not enough rights\n");
189 return err;
190 }
191
192 TEST_ASSERT_VAL("failed to attach", !err);
193
194 for (i = 0; i < 100000000; i++) { }
195
196 TEST_ASSERT_VAL("failed to detach", !detach(evlist));
197
198 perf_evsel__read(evsel, 0, 0, &count);
199
200 err = !(count.ena == count.run);
201
202 pr_debug(" %s: ena %" PRIu64", run %" PRIu64"\n",
203 !err ? "OK " : "FAILED",
204 count.ena, count.run);
205
206out_err:
207 perf_evlist__delete(evlist);
208 return !err ? TEST_OK : TEST_FAIL;
209}
210
211/*
212 * This test creates software event 'cpu-clock'
213 * attaches it in several ways (explained below)
214 * and checks that enabled and running times
215 * match.
216 */
217int test__event_times(struct test *test __maybe_unused, int subtest __maybe_unused)
218{
219 int err, ret = 0;
220
221#define _T(attach, detach) \
222 err = test_times(attach, detach); \
223 if (err && (ret == TEST_OK || ret == TEST_SKIP)) \
224 ret = err;
225
226 /* attach on newly spawned process after exec */
227 _T(attach__enable_on_exec, detach__enable_on_exec)
228 /* attach on current process as enabled */
229 _T(attach__current_enabled, detach__disable)
230 /* attach on current process as disabled */
231 _T(attach__current_disabled, detach__disable)
232 /* attach on cpu as disabled */
233 _T(attach__cpu_disabled, detach__disable)
234 /* attach on cpu as enabled */
235 _T(attach__cpu_enabled, detach__disable)
236
237#undef _T
238 return ret;
239}