1 #include <linux/compiler.h>
11 #include "thread_map.h"
14 static int attach__enable_on_exec(struct perf_evlist
*evlist
)
16 struct perf_evsel
*evsel
= perf_evlist__last(evlist
);
17 struct target target
= {
20 const char *argv
[] = { "true", NULL
, };
21 char sbuf
[STRERR_BUFSIZE
];
24 pr_debug("attaching to spawned child, enable on exec\n");
26 err
= perf_evlist__create_maps(evlist
, &target
);
28 pr_debug("Not enough memory to create thread/cpu maps\n");
32 err
= perf_evlist__prepare_workload(evlist
, &target
, argv
, false, NULL
);
34 pr_debug("Couldn't run the workload!\n");
38 evsel
->attr
.enable_on_exec
= 1;
40 err
= perf_evlist__open(evlist
);
42 pr_debug("perf_evlist__open: %s\n",
43 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
47 return perf_evlist__start_workload(evlist
) == 1 ? TEST_OK
: TEST_FAIL
;
50 static int detach__enable_on_exec(struct perf_evlist
*evlist
)
52 waitpid(evlist
->workload
.pid
, NULL
, 0);
56 static int attach__current_disabled(struct perf_evlist
*evlist
)
58 struct perf_evsel
*evsel
= perf_evlist__last(evlist
);
59 struct thread_map
*threads
;
62 pr_debug("attaching to current thread as disabled\n");
64 threads
= thread_map__new(-1, getpid(), UINT_MAX
);
65 if (threads
== NULL
) {
66 pr_debug("thread_map__new\n");
70 evsel
->attr
.disabled
= 1;
72 err
= perf_evsel__open_per_thread(evsel
, threads
);
74 pr_debug("Failed to open event cpu-clock:u\n");
78 thread_map__put(threads
);
79 return perf_evsel__enable(evsel
) == 0 ? TEST_OK
: TEST_FAIL
;
82 static int attach__current_enabled(struct perf_evlist
*evlist
)
84 struct perf_evsel
*evsel
= perf_evlist__last(evlist
);
85 struct thread_map
*threads
;
88 pr_debug("attaching to current thread as enabled\n");
90 threads
= thread_map__new(-1, getpid(), UINT_MAX
);
91 if (threads
== NULL
) {
92 pr_debug("failed to call thread_map__new\n");
96 err
= perf_evsel__open_per_thread(evsel
, threads
);
98 thread_map__put(threads
);
99 return err
== 0 ? TEST_OK
: TEST_FAIL
;
102 static int detach__disable(struct perf_evlist
*evlist
)
104 struct perf_evsel
*evsel
= perf_evlist__last(evlist
);
106 return perf_evsel__enable(evsel
);
109 static int attach__cpu_disabled(struct perf_evlist
*evlist
)
111 struct perf_evsel
*evsel
= perf_evlist__last(evlist
);
112 struct cpu_map
*cpus
;
115 pr_debug("attaching to CPU 0 as enabled\n");
117 cpus
= cpu_map__new("0");
119 pr_debug("failed to call cpu_map__new\n");
123 evsel
->attr
.disabled
= 1;
125 err
= perf_evsel__open_per_cpu(evsel
, cpus
);
130 pr_debug("Failed to open event cpu-clock:u\n");
135 return perf_evsel__enable(evsel
);
138 static int attach__cpu_enabled(struct perf_evlist
*evlist
)
140 struct perf_evsel
*evsel
= perf_evlist__last(evlist
);
141 struct cpu_map
*cpus
;
144 pr_debug("attaching to CPU 0 as enabled\n");
146 cpus
= cpu_map__new("0");
148 pr_debug("failed to call cpu_map__new\n");
152 err
= perf_evsel__open_per_cpu(evsel
, cpus
);
157 return err
? TEST_FAIL
: TEST_OK
;
160 static int test_times(int (attach
)(struct perf_evlist
*),
161 int (detach
)(struct perf_evlist
*))
163 struct perf_counts_values count
;
164 struct perf_evlist
*evlist
= NULL
;
165 struct perf_evsel
*evsel
;
168 evlist
= perf_evlist__new();
170 pr_debug("failed to create event list\n");
174 err
= parse_events(evlist
, "cpu-clock:u", NULL
);
176 pr_debug("failed to parse event cpu-clock:u\n");
180 evsel
= perf_evlist__last(evlist
);
181 evsel
->attr
.read_format
|=
182 PERF_FORMAT_TOTAL_TIME_ENABLED
|
183 PERF_FORMAT_TOTAL_TIME_RUNNING
;
185 err
= attach(evlist
);
186 if (err
== TEST_SKIP
) {
187 pr_debug(" SKIP : not enough rights\n");
191 TEST_ASSERT_VAL("failed to attach", !err
);
193 for (i
= 0; i
< 100000000; i
++) { }
195 TEST_ASSERT_VAL("failed to detach", !detach(evlist
));
197 perf_evsel__read(evsel
, 0, 0, &count
);
199 err
= !(count
.ena
== count
.run
);
201 pr_debug(" %s: ena %" PRIu64
", run %" PRIu64
"\n",
202 !err
? "OK " : "FAILED",
203 count
.ena
, count
.run
);
206 perf_evlist__delete(evlist
);
207 return !err
? TEST_OK
: TEST_FAIL
;
211 * This test creates software event 'cpu-clock'
212 * attaches it in several ways (explained below)
213 * and checks that enabled and running times
216 int test__event_times(struct test
*test __maybe_unused
, int subtest __maybe_unused
)
220 #define _T(attach, detach) \
221 err = test_times(attach, detach); \
222 if (err && (ret == TEST_OK || ret == TEST_SKIP)) \
225 /* attach on newly spawned process after exec */
226 _T(attach__enable_on_exec
, detach__enable_on_exec
)
227 /* attach on current process as enabled */
228 _T(attach__current_enabled
, detach__disable
)
229 /* attach on current process as disabled */
230 _T(attach__current_disabled
, detach__disable
)
231 /* attach on cpu as disabled */
232 _T(attach__cpu_disabled
, detach__disable
)
233 /* attach on cpu as enabled */
234 _T(attach__cpu_enabled
, detach__disable
)