1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/compiler.h>
12 #include "thread_map.h"
15 static int attach__enable_on_exec(struct perf_evlist
*evlist
)
17 struct perf_evsel
*evsel
= perf_evlist__last(evlist
);
18 struct target target
= {
21 const char *argv
[] = { "true", NULL
, };
22 char sbuf
[STRERR_BUFSIZE
];
25 pr_debug("attaching to spawned child, enable on exec\n");
27 err
= perf_evlist__create_maps(evlist
, &target
);
29 pr_debug("Not enough memory to create thread/cpu maps\n");
33 err
= perf_evlist__prepare_workload(evlist
, &target
, argv
, false, NULL
);
35 pr_debug("Couldn't run the workload!\n");
39 evsel
->attr
.enable_on_exec
= 1;
41 err
= perf_evlist__open(evlist
);
43 pr_debug("perf_evlist__open: %s\n",
44 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
48 return perf_evlist__start_workload(evlist
) == 1 ? TEST_OK
: TEST_FAIL
;
51 static int detach__enable_on_exec(struct perf_evlist
*evlist
)
53 waitpid(evlist
->workload
.pid
, NULL
, 0);
57 static int attach__current_disabled(struct perf_evlist
*evlist
)
59 struct perf_evsel
*evsel
= perf_evlist__last(evlist
);
60 struct thread_map
*threads
;
63 pr_debug("attaching to current thread as disabled\n");
65 threads
= thread_map__new(-1, getpid(), UINT_MAX
);
66 if (threads
== NULL
) {
67 pr_debug("thread_map__new\n");
71 evsel
->attr
.disabled
= 1;
73 err
= perf_evsel__open_per_thread(evsel
, threads
);
75 pr_debug("Failed to open event cpu-clock:u\n");
79 thread_map__put(threads
);
80 return perf_evsel__enable(evsel
) == 0 ? TEST_OK
: TEST_FAIL
;
83 static int attach__current_enabled(struct perf_evlist
*evlist
)
85 struct perf_evsel
*evsel
= perf_evlist__last(evlist
);
86 struct thread_map
*threads
;
89 pr_debug("attaching to current thread as enabled\n");
91 threads
= thread_map__new(-1, getpid(), UINT_MAX
);
92 if (threads
== NULL
) {
93 pr_debug("failed to call thread_map__new\n");
97 err
= perf_evsel__open_per_thread(evsel
, threads
);
99 thread_map__put(threads
);
100 return err
== 0 ? TEST_OK
: TEST_FAIL
;
103 static int detach__disable(struct perf_evlist
*evlist
)
105 struct perf_evsel
*evsel
= perf_evlist__last(evlist
);
107 return perf_evsel__enable(evsel
);
110 static int attach__cpu_disabled(struct perf_evlist
*evlist
)
112 struct perf_evsel
*evsel
= perf_evlist__last(evlist
);
113 struct cpu_map
*cpus
;
116 pr_debug("attaching to CPU 0 as enabled\n");
118 cpus
= cpu_map__new("0");
120 pr_debug("failed to call cpu_map__new\n");
124 evsel
->attr
.disabled
= 1;
126 err
= perf_evsel__open_per_cpu(evsel
, cpus
);
131 pr_debug("Failed to open event cpu-clock:u\n");
136 return perf_evsel__enable(evsel
);
139 static int attach__cpu_enabled(struct perf_evlist
*evlist
)
141 struct perf_evsel
*evsel
= perf_evlist__last(evlist
);
142 struct cpu_map
*cpus
;
145 pr_debug("attaching to CPU 0 as enabled\n");
147 cpus
= cpu_map__new("0");
149 pr_debug("failed to call cpu_map__new\n");
153 err
= perf_evsel__open_per_cpu(evsel
, cpus
);
158 return err
? TEST_FAIL
: TEST_OK
;
161 static int test_times(int (attach
)(struct perf_evlist
*),
162 int (detach
)(struct perf_evlist
*))
164 struct perf_counts_values count
;
165 struct perf_evlist
*evlist
= NULL
;
166 struct perf_evsel
*evsel
;
169 evlist
= perf_evlist__new();
171 pr_debug("failed to create event list\n");
175 err
= parse_events(evlist
, "cpu-clock:u", NULL
);
177 pr_debug("failed to parse event cpu-clock:u\n");
181 evsel
= perf_evlist__last(evlist
);
182 evsel
->attr
.read_format
|=
183 PERF_FORMAT_TOTAL_TIME_ENABLED
|
184 PERF_FORMAT_TOTAL_TIME_RUNNING
;
186 err
= attach(evlist
);
187 if (err
== TEST_SKIP
) {
188 pr_debug(" SKIP : not enough rights\n");
192 TEST_ASSERT_VAL("failed to attach", !err
);
194 for (i
= 0; i
< 100000000; i
++) { }
196 TEST_ASSERT_VAL("failed to detach", !detach(evlist
));
198 perf_evsel__read(evsel
, 0, 0, &count
);
200 err
= !(count
.ena
== count
.run
);
202 pr_debug(" %s: ena %" PRIu64
", run %" PRIu64
"\n",
203 !err
? "OK " : "FAILED",
204 count
.ena
, count
.run
);
207 perf_evlist__delete(evlist
);
208 return !err
? TEST_OK
: TEST_FAIL
;
212 * This test creates software event 'cpu-clock'
213 * attaches it in several ways (explained below)
214 * and checks that enabled and running times
217 int test__event_times(struct test
*test __maybe_unused
, int subtest __maybe_unused
)
221 #define _T(attach, detach) \
222 err = test_times(attach, detach); \
223 if (err && (ret == TEST_OK || ret == TEST_SKIP)) \
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
)