1 #include <linux/compiler.h>
8 #include "thread_map.h"
11 static int attach__enable_on_exec(struct perf_evlist
*evlist
)
13 struct perf_evsel
*evsel
= perf_evlist__last(evlist
);
14 struct target target
= {
17 const char *argv
[] = { "true", NULL
, };
18 char sbuf
[STRERR_BUFSIZE
];
21 pr_debug("attaching to spawned child, enable on exec\n");
23 err
= perf_evlist__create_maps(evlist
, &target
);
25 pr_debug("Not enough memory to create thread/cpu maps\n");
29 err
= perf_evlist__prepare_workload(evlist
, &target
, argv
, false, NULL
);
31 pr_debug("Couldn't run the workload!\n");
35 evsel
->attr
.enable_on_exec
= 1;
37 err
= perf_evlist__open(evlist
);
39 pr_debug("perf_evlist__open: %s\n",
40 str_error_r(errno
, sbuf
, sizeof(sbuf
)));
44 return perf_evlist__start_workload(evlist
) == 1 ? TEST_OK
: TEST_FAIL
;
47 static int detach__enable_on_exec(struct perf_evlist
*evlist
)
49 waitpid(evlist
->workload
.pid
, NULL
, 0);
53 static int attach__current_disabled(struct perf_evlist
*evlist
)
55 struct perf_evsel
*evsel
= perf_evlist__last(evlist
);
56 struct thread_map
*threads
;
59 pr_debug("attaching to current thread as disabled\n");
61 threads
= thread_map__new(-1, getpid(), UINT_MAX
);
62 if (threads
== NULL
) {
63 pr_debug("thread_map__new\n");
67 evsel
->attr
.disabled
= 1;
69 err
= perf_evsel__open_per_thread(evsel
, threads
);
71 pr_debug("Failed to open event cpu-clock:u\n");
75 thread_map__put(threads
);
76 return perf_evsel__enable(evsel
) == 0 ? TEST_OK
: TEST_FAIL
;
79 static int attach__current_enabled(struct perf_evlist
*evlist
)
81 struct perf_evsel
*evsel
= perf_evlist__last(evlist
);
82 struct thread_map
*threads
;
85 pr_debug("attaching to current thread as enabled\n");
87 threads
= thread_map__new(-1, getpid(), UINT_MAX
);
88 if (threads
== NULL
) {
89 pr_debug("failed to call thread_map__new\n");
93 err
= perf_evsel__open_per_thread(evsel
, threads
);
95 thread_map__put(threads
);
96 return err
== 0 ? TEST_OK
: TEST_FAIL
;
99 static int detach__disable(struct perf_evlist
*evlist
)
101 struct perf_evsel
*evsel
= perf_evlist__last(evlist
);
103 return perf_evsel__enable(evsel
);
106 static int attach__cpu_disabled(struct perf_evlist
*evlist
)
108 struct perf_evsel
*evsel
= perf_evlist__last(evlist
);
109 struct cpu_map
*cpus
;
112 pr_debug("attaching to CPU 0 as enabled\n");
114 cpus
= cpu_map__new("0");
116 pr_debug("failed to call cpu_map__new\n");
120 evsel
->attr
.disabled
= 1;
122 err
= perf_evsel__open_per_cpu(evsel
, cpus
);
127 pr_debug("Failed to open event cpu-clock:u\n");
132 return perf_evsel__enable(evsel
);
135 static int attach__cpu_enabled(struct perf_evlist
*evlist
)
137 struct perf_evsel
*evsel
= perf_evlist__last(evlist
);
138 struct cpu_map
*cpus
;
141 pr_debug("attaching to CPU 0 as enabled\n");
143 cpus
= cpu_map__new("0");
145 pr_debug("failed to call cpu_map__new\n");
149 err
= perf_evsel__open_per_cpu(evsel
, cpus
);
154 return err
? TEST_FAIL
: TEST_OK
;
157 static int test_times(int (attach
)(struct perf_evlist
*),
158 int (detach
)(struct perf_evlist
*))
160 struct perf_counts_values count
;
161 struct perf_evlist
*evlist
= NULL
;
162 struct perf_evsel
*evsel
;
165 evlist
= perf_evlist__new();
167 pr_debug("failed to create event list\n");
171 err
= parse_events(evlist
, "cpu-clock:u", NULL
);
173 pr_debug("failed to parse event cpu-clock:u\n");
177 evsel
= perf_evlist__last(evlist
);
178 evsel
->attr
.read_format
|=
179 PERF_FORMAT_TOTAL_TIME_ENABLED
|
180 PERF_FORMAT_TOTAL_TIME_RUNNING
;
182 err
= attach(evlist
);
183 if (err
== TEST_SKIP
) {
184 pr_debug(" SKIP : not enough rights\n");
188 TEST_ASSERT_VAL("failed to attach", !err
);
190 for (i
= 0; i
< 100000000; i
++) { }
192 TEST_ASSERT_VAL("failed to detach", !detach(evlist
));
194 perf_evsel__read(evsel
, 0, 0, &count
);
196 err
= !(count
.ena
== count
.run
);
198 pr_debug(" %s: ena %" PRIu64
", run %" PRIu64
"\n",
199 !err
? "OK " : "FAILED",
200 count
.ena
, count
.run
);
203 perf_evlist__delete(evlist
);
204 return !err
? TEST_OK
: TEST_FAIL
;
208 * This test creates software event 'cpu-clock'
209 * attaches it in several ways (explained below)
210 * and checks that enabled and running times
213 int test__event_times(int subtest __maybe_unused
)
217 #define _T(attach, detach) \
218 err = test_times(attach, detach); \
219 if (err && (ret == TEST_OK || ret == TEST_SKIP)) \
222 /* attach on newly spawned process after exec */
223 _T(attach__enable_on_exec
, detach__enable_on_exec
)
224 /* attach on current process as enabled */
225 _T(attach__current_enabled
, detach__disable
)
226 /* attach on current process as disabled */
227 _T(attach__current_disabled
, detach__disable
)
228 /* attach on cpu as disabled */
229 _T(attach__cpu_disabled
, detach__disable
)
230 /* attach on cpu as enabled */
231 _T(attach__cpu_enabled
, detach__disable
)