1 // SPDX-License-Identifier: GPL-2.0
4 #include <sys/syscall.h>
13 #include "thread_map.h"
30 static struct thread_data threads
[THREADS
];
32 static int thread_init(struct thread_data
*td
)
36 map
= mmap(NULL
, page_size
,
37 PROT_READ
|PROT_WRITE
|PROT_EXEC
,
38 MAP_SHARED
|MAP_ANONYMOUS
, -1, 0);
40 if (map
== MAP_FAILED
) {
41 perror("mmap failed");
46 td
->tid
= syscall(SYS_gettid
);
48 pr_debug("tid = %d, map = %p\n", td
->tid
, map
);
52 static void *thread_fn(void *arg
)
54 struct thread_data
*td
= arg
;
61 /* Signal thread_create thread is initialized. */
62 ret
= write(td
->ready
[1], &go
, sizeof(int));
63 if (ret
!= sizeof(int)) {
64 pr_err("failed to notify\n");
69 /* Waiting for main thread to kill us. */
73 munmap(td
->map
, page_size
);
77 static int thread_create(int i
)
79 struct thread_data
*td
= &threads
[i
];
85 err
= pthread_create(&td
->pt
, NULL
, thread_fn
, td
);
87 /* Wait for thread initialization. */
88 ssize_t ret
= read(td
->ready
[0], &go
, sizeof(int));
89 err
= ret
!= sizeof(int);
97 static int threads_create(void)
99 struct thread_data
*td0
= &threads
[0];
104 /* 0 is main thread */
105 if (thread_init(td0
))
108 for (i
= 1; !err
&& i
< THREADS
; i
++)
109 err
= thread_create(i
);
114 static int threads_destroy(void)
116 struct thread_data
*td0
= &threads
[0];
119 /* cleanup the main thread */
120 munmap(td0
->map
, page_size
);
124 for (i
= 1; !err
&& i
< THREADS
; i
++)
125 err
= pthread_join(threads
[i
].pt
, NULL
);
130 typedef int (*synth_cb
)(struct machine
*machine
);
132 static int synth_all(struct machine
*machine
)
134 return perf_event__synthesize_threads(NULL
,
139 static int synth_process(struct machine
*machine
)
141 struct thread_map
*map
;
144 map
= thread_map__new_by_pid(getpid());
146 err
= perf_event__synthesize_thread_map(NULL
, map
,
150 thread_map__put(map
);
154 static int mmap_events(synth_cb synth
)
156 struct machine
*machine
;
160 * The threads_create will not return before all threads
161 * are spawned and all created memory map.
163 * They will loop until threads_destroy is called, so we
164 * can safely run synthesizing function.
166 TEST_ASSERT_VAL("failed to create threads", !threads_create());
168 machine
= machine__new_host();
170 dump_trace
= verbose
> 1 ? 1 : 0;
172 err
= synth(machine
);
176 TEST_ASSERT_VAL("failed to destroy threads", !threads_destroy());
177 TEST_ASSERT_VAL("failed to synthesize maps", !err
);
180 * All data is synthesized, try to find map for each
183 for (i
= 0; i
< THREADS
; i
++) {
184 struct thread_data
*td
= &threads
[i
];
185 struct addr_location al
;
186 struct thread
*thread
;
188 thread
= machine__findnew_thread(machine
, getpid(), td
->tid
);
190 pr_debug("looking for map %p\n", td
->map
);
192 thread__find_map(thread
, PERF_RECORD_MISC_USER
,
193 (unsigned long) (td
->map
+ 1), &al
);
198 pr_debug("failed, couldn't find map\n");
203 pr_debug("map %p, addr %" PRIx64
"\n", al
.map
, al
.map
->start
);
206 machine__delete_threads(machine
);
207 machine__delete(machine
);
212 * This test creates 'THREADS' number of threads (including
213 * main thread) and each thread creates memory map.
215 * When threads are created, we synthesize them with both
217 * perf_event__synthesize_thread_map (process based)
218 * perf_event__synthesize_threads (global)
220 * We test we can find all memory maps via:
223 * by using all thread objects.
225 int test__mmap_thread_lookup(struct test
*test __maybe_unused
, int subtest __maybe_unused
)
227 /* perf_event__synthesize_threads synthesize */
228 TEST_ASSERT_VAL("failed with sythesizing all",
229 !mmap_events(synth_all
));
231 /* perf_event__synthesize_thread_map synthesize */
232 TEST_ASSERT_VAL("failed with sythesizing process",
233 !mmap_events(synth_process
));