1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/compiler.h>
3 #include <linux/string.h>
8 #include <perf/cpumap.h>
13 #include "parse-events.h"
14 #include "thread_map.h"
17 static int attach__enable_on_exec(struct evlist
*evlist
)
19 struct evsel
*evsel
= evlist__last(evlist
);
20 struct target target
= {
23 const char *argv
[] = { "true", NULL
, };
24 char sbuf
[STRERR_BUFSIZE
];
27 pr_debug("attaching to spawned child, enable on exec\n");
29 err
= perf_evlist__create_maps(evlist
, &target
);
31 pr_debug("Not enough memory to create thread/cpu maps\n");
35 err
= perf_evlist__prepare_workload(evlist
, &target
, argv
, false, NULL
);
37 pr_debug("Couldn't run the workload!\n");
41 evsel
->core
.attr
.enable_on_exec
= 1;
43 err
= evlist__open(evlist
);
45 pr_debug("perf_evlist__open: %s\n",
46 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
50 return perf_evlist__start_workload(evlist
) == 1 ? TEST_OK
: TEST_FAIL
;
53 static int detach__enable_on_exec(struct evlist
*evlist
)
55 waitpid(evlist
->workload
.pid
, NULL
, 0);
59 static int attach__current_disabled(struct evlist
*evlist
)
61 struct evsel
*evsel
= evlist__last(evlist
);
62 struct perf_thread_map
*threads
;
65 pr_debug("attaching to current thread as disabled\n");
67 threads
= thread_map__new(-1, getpid(), UINT_MAX
);
68 if (threads
== NULL
) {
69 pr_debug("thread_map__new\n");
73 evsel
->core
.attr
.disabled
= 1;
75 err
= evsel__open_per_thread(evsel
, threads
);
77 pr_debug("Failed to open event cpu-clock:u\n");
81 perf_thread_map__put(threads
);
82 return evsel__enable(evsel
) == 0 ? TEST_OK
: TEST_FAIL
;
85 static int attach__current_enabled(struct evlist
*evlist
)
87 struct evsel
*evsel
= evlist__last(evlist
);
88 struct perf_thread_map
*threads
;
91 pr_debug("attaching to current thread as enabled\n");
93 threads
= thread_map__new(-1, getpid(), UINT_MAX
);
94 if (threads
== NULL
) {
95 pr_debug("failed to call thread_map__new\n");
99 err
= evsel__open_per_thread(evsel
, threads
);
101 perf_thread_map__put(threads
);
102 return err
== 0 ? TEST_OK
: TEST_FAIL
;
105 static int detach__disable(struct evlist
*evlist
)
107 struct evsel
*evsel
= evlist__last(evlist
);
109 return evsel__enable(evsel
);
112 static int attach__cpu_disabled(struct evlist
*evlist
)
114 struct evsel
*evsel
= evlist__last(evlist
);
115 struct perf_cpu_map
*cpus
;
118 pr_debug("attaching to CPU 0 as enabled\n");
120 cpus
= perf_cpu_map__new("0");
122 pr_debug("failed to call perf_cpu_map__new\n");
126 evsel
->core
.attr
.disabled
= 1;
128 err
= evsel__open_per_cpu(evsel
, cpus
, -1);
133 pr_debug("Failed to open event cpu-clock:u\n");
137 perf_cpu_map__put(cpus
);
138 return evsel__enable(evsel
);
141 static int attach__cpu_enabled(struct evlist
*evlist
)
143 struct evsel
*evsel
= evlist__last(evlist
);
144 struct perf_cpu_map
*cpus
;
147 pr_debug("attaching to CPU 0 as enabled\n");
149 cpus
= perf_cpu_map__new("0");
151 pr_debug("failed to call perf_cpu_map__new\n");
155 err
= evsel__open_per_cpu(evsel
, cpus
, -1);
159 perf_cpu_map__put(cpus
);
160 return err
? TEST_FAIL
: TEST_OK
;
163 static int test_times(int (attach
)(struct evlist
*),
164 int (detach
)(struct evlist
*))
166 struct perf_counts_values count
;
167 struct evlist
*evlist
= NULL
;
171 evlist
= evlist__new();
173 pr_debug("failed to create event list\n");
177 err
= parse_events(evlist
, "cpu-clock:u", NULL
);
179 pr_debug("failed to parse event cpu-clock:u\n");
183 evsel
= evlist__last(evlist
);
184 evsel
->core
.attr
.read_format
|=
185 PERF_FORMAT_TOTAL_TIME_ENABLED
|
186 PERF_FORMAT_TOTAL_TIME_RUNNING
;
188 err
= attach(evlist
);
189 if (err
== TEST_SKIP
) {
190 pr_debug(" SKIP : not enough rights\n");
194 TEST_ASSERT_VAL("failed to attach", !err
);
196 for (i
= 0; i
< 100000000; i
++) { }
198 TEST_ASSERT_VAL("failed to detach", !detach(evlist
));
200 perf_evsel__read(&evsel
->core
, 0, 0, &count
);
202 err
= !(count
.ena
== count
.run
);
204 pr_debug(" %s: ena %" PRIu64
", run %" PRIu64
"\n",
205 !err
? "OK " : "FAILED",
206 count
.ena
, count
.run
);
209 evlist__delete(evlist
);
210 return !err
? TEST_OK
: TEST_FAIL
;
214 * This test creates software event 'cpu-clock'
215 * attaches it in several ways (explained below)
216 * and checks that enabled and running times
219 int test__event_times(struct test
*test __maybe_unused
, int subtest __maybe_unused
)
223 #define _T(attach, detach) \
224 err = test_times(attach, detach); \
225 if (err && (ret == TEST_OK || ret == TEST_SKIP)) \
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
)