1 // SPDX-License-Identifier: GPL-2.0
4 #include <sys/syscall.h>
14 #include "thread_map.h"
17 #include "util/synthetic-events.h"
19 #include <internal/lib.h> // page_size
32 static struct thread_data threads
[THREADS
];
34 static int thread_init(struct thread_data
*td
)
38 map
= mmap(NULL
, page_size
,
39 PROT_READ
|PROT_WRITE
|PROT_EXEC
,
40 MAP_SHARED
|MAP_ANONYMOUS
, -1, 0);
42 if (map
== MAP_FAILED
) {
43 perror("mmap failed");
48 td
->tid
= syscall(SYS_gettid
);
50 pr_debug("tid = %d, map = %p\n", td
->tid
, map
);
54 static void *thread_fn(void *arg
)
56 struct thread_data
*td
= arg
;
63 /* Signal thread_create thread is initialized. */
64 ret
= write(td
->ready
[1], &go
, sizeof(int));
65 if (ret
!= sizeof(int)) {
66 pr_err("failed to notify\n");
71 /* Waiting for main thread to kill us. */
75 munmap(td
->map
, page_size
);
79 static int thread_create(int i
)
81 struct thread_data
*td
= &threads
[i
];
87 err
= pthread_create(&td
->pt
, NULL
, thread_fn
, td
);
89 /* Wait for thread initialization. */
90 ssize_t ret
= read(td
->ready
[0], &go
, sizeof(int));
91 err
= ret
!= sizeof(int);
99 static int threads_create(void)
101 struct thread_data
*td0
= &threads
[0];
106 /* 0 is main thread */
107 if (thread_init(td0
))
110 for (i
= 1; !err
&& i
< THREADS
; i
++)
111 err
= thread_create(i
);
116 static int threads_destroy(void)
118 struct thread_data
*td0
= &threads
[0];
121 /* cleanup the main thread */
122 munmap(td0
->map
, page_size
);
126 for (i
= 1; !err
&& i
< THREADS
; i
++)
127 err
= pthread_join(threads
[i
].pt
, NULL
);
132 typedef int (*synth_cb
)(struct machine
*machine
);
134 static int synth_all(struct machine
*machine
)
136 return perf_event__synthesize_threads(NULL
,
141 static int synth_process(struct machine
*machine
)
143 struct perf_thread_map
*map
;
146 map
= thread_map__new_by_pid(getpid());
148 err
= perf_event__synthesize_thread_map(NULL
, map
,
152 perf_thread_map__put(map
);
156 static int mmap_events(synth_cb synth
)
158 struct machine
*machine
;
162 * The threads_create will not return before all threads
163 * are spawned and all created memory map.
165 * They will loop until threads_destroy is called, so we
166 * can safely run synthesizing function.
168 TEST_ASSERT_VAL("failed to create threads", !threads_create());
170 machine
= machine__new_host();
172 dump_trace
= verbose
> 1 ? 1 : 0;
174 err
= synth(machine
);
178 TEST_ASSERT_VAL("failed to destroy threads", !threads_destroy());
179 TEST_ASSERT_VAL("failed to synthesize maps", !err
);
182 * All data is synthesized, try to find map for each
185 for (i
= 0; i
< THREADS
; i
++) {
186 struct thread_data
*td
= &threads
[i
];
187 struct addr_location al
;
188 struct thread
*thread
;
190 thread
= machine__findnew_thread(machine
, getpid(), td
->tid
);
192 pr_debug("looking for map %p\n", td
->map
);
194 thread__find_map(thread
, PERF_RECORD_MISC_USER
,
195 (unsigned long) (td
->map
+ 1), &al
);
200 pr_debug("failed, couldn't find map\n");
205 pr_debug("map %p, addr %" PRIx64
"\n", al
.map
, al
.map
->start
);
208 machine__delete_threads(machine
);
209 machine__delete(machine
);
214 * This test creates 'THREADS' number of threads (including
215 * main thread) and each thread creates memory map.
217 * When threads are created, we synthesize them with both
219 * perf_event__synthesize_thread_map (process based)
220 * perf_event__synthesize_threads (global)
222 * We test we can find all memory maps via:
225 * by using all thread objects.
227 int test__mmap_thread_lookup(struct test
*test __maybe_unused
, int subtest __maybe_unused
)
229 /* perf_event__synthesize_threads synthesize */
230 TEST_ASSERT_VAL("failed with sythesizing all",
231 !mmap_events(synth_all
));
233 /* perf_event__synthesize_thread_map synthesize */
234 TEST_ASSERT_VAL("failed with sythesizing process",
235 !mmap_events(synth_process
));