1 #include <linux/compiler.h>
10 #include "thread_map.h"
13 static int attach__enable_on_exec(struct perf_evlist *evlist)
15 struct perf_evsel *evsel = perf_evlist__last(evlist);
16 struct target target = {
19 const char *argv[] = { "true", NULL, };
20 char sbuf[STRERR_BUFSIZE];
23 pr_debug("attaching to spawned child, enable on exec\n");
25 err = perf_evlist__create_maps(evlist, &target);
27 pr_debug("Not enough memory to create thread/cpu maps\n");
31 err = perf_evlist__prepare_workload(evlist, &target, argv, false, NULL);
33 pr_debug("Couldn't run the workload!\n");
37 evsel->attr.enable_on_exec = 1;
39 err = perf_evlist__open(evlist);
41 pr_debug("perf_evlist__open: %s\n",
42 str_error_r(errno, sbuf, sizeof(sbuf)));
46 return perf_evlist__start_workload(evlist) == 1 ? TEST_OK : TEST_FAIL;
49 static int detach__enable_on_exec(struct perf_evlist *evlist)
51 waitpid(evlist->workload.pid, NULL, 0);
55 static int attach__current_disabled(struct perf_evlist *evlist)
57 struct perf_evsel *evsel = perf_evlist__last(evlist);
58 struct thread_map *threads;
61 pr_debug("attaching to current thread as disabled\n");
63 threads = thread_map__new(-1, getpid(), UINT_MAX);
64 if (threads == NULL) {
65 pr_debug("thread_map__new\n");
69 evsel->attr.disabled = 1;
71 err = perf_evsel__open_per_thread(evsel, threads);
73 pr_debug("Failed to open event cpu-clock:u\n");
77 thread_map__put(threads);
78 return perf_evsel__enable(evsel) == 0 ? TEST_OK : TEST_FAIL;
81 static int attach__current_enabled(struct perf_evlist *evlist)
83 struct perf_evsel *evsel = perf_evlist__last(evlist);
84 struct thread_map *threads;
87 pr_debug("attaching to current thread as enabled\n");
89 threads = thread_map__new(-1, getpid(), UINT_MAX);
90 if (threads == NULL) {
91 pr_debug("failed to call thread_map__new\n");
95 err = perf_evsel__open_per_thread(evsel, threads);
97 thread_map__put(threads);
98 return err == 0 ? TEST_OK : TEST_FAIL;
101 static int detach__disable(struct perf_evlist *evlist)
103 struct perf_evsel *evsel = perf_evlist__last(evlist);
105 return perf_evsel__enable(evsel);
108 static int attach__cpu_disabled(struct perf_evlist *evlist)
110 struct perf_evsel *evsel = perf_evlist__last(evlist);
111 struct cpu_map *cpus;
114 pr_debug("attaching to CPU 0 as enabled\n");
116 cpus = cpu_map__new("0");
118 pr_debug("failed to call cpu_map__new\n");
122 evsel->attr.disabled = 1;
124 err = perf_evsel__open_per_cpu(evsel, cpus);
129 pr_debug("Failed to open event cpu-clock:u\n");
134 return perf_evsel__enable(evsel);
137 static int attach__cpu_enabled(struct perf_evlist *evlist)
139 struct perf_evsel *evsel = perf_evlist__last(evlist);
140 struct cpu_map *cpus;
143 pr_debug("attaching to CPU 0 as enabled\n");
145 cpus = cpu_map__new("0");
147 pr_debug("failed to call cpu_map__new\n");
151 err = perf_evsel__open_per_cpu(evsel, cpus);
156 return err ? TEST_FAIL : TEST_OK;
159 static int test_times(int (attach)(struct perf_evlist *),
160 int (detach)(struct perf_evlist *))
162 struct perf_counts_values count;
163 struct perf_evlist *evlist = NULL;
164 struct perf_evsel *evsel;
167 evlist = perf_evlist__new();
169 pr_debug("failed to create event list\n");
173 err = parse_events(evlist, "cpu-clock:u", NULL);
175 pr_debug("failed to parse event cpu-clock:u\n");
179 evsel = perf_evlist__last(evlist);
180 evsel->attr.read_format |=
181 PERF_FORMAT_TOTAL_TIME_ENABLED |
182 PERF_FORMAT_TOTAL_TIME_RUNNING;
184 err = attach(evlist);
185 if (err == TEST_SKIP) {
186 pr_debug(" SKIP : not enough rights\n");
190 TEST_ASSERT_VAL("failed to attach", !err);
192 for (i = 0; i < 100000000; i++) { }
194 TEST_ASSERT_VAL("failed to detach", !detach(evlist));
196 perf_evsel__read(evsel, 0, 0, &count);
198 err = !(count.ena == count.run);
200 pr_debug(" %s: ena %" PRIu64", run %" PRIu64"\n",
201 !err ? "OK " : "FAILED",
202 count.ena, count.run);
205 perf_evlist__delete(evlist);
206 return !err ? TEST_OK : TEST_FAIL;
210 * This test creates software event 'cpu-clock'
211 * attaches it in several ways (explained below)
212 * and checks that enabled and running times
215 int test__event_times(int subtest __maybe_unused)
219 #define _T(attach, detach) \
220 err = test_times(attach, detach); \
221 if (err && (ret == TEST_OK || ret == TEST_SKIP)) \
224 /* attach on newly spawned process after exec */
225 _T(attach__enable_on_exec, detach__enable_on_exec)
226 /* attach on current process as enabled */
227 _T(attach__current_enabled, detach__disable)
228 /* attach on current process as disabled */
229 _T(attach__current_disabled, detach__disable)
230 /* attach on cpu as disabled */
231 _T(attach__cpu_disabled, detach__disable)
232 /* attach on cpu as enabled */
233 _T(attach__cpu_enabled, detach__disable)