2 #include <sys/syscall.h>
11 #include "thread_map.h"
26 static struct thread_data threads[THREADS];
28 static int thread_init(struct thread_data *td)
32 map = mmap(NULL, page_size,
33 PROT_READ|PROT_WRITE|PROT_EXEC,
34 MAP_SHARED|MAP_ANONYMOUS, -1, 0);
36 if (map == MAP_FAILED) {
37 perror("mmap failed");
42 td->tid = syscall(SYS_gettid);
44 pr_debug("tid = %d, map = %p\n", td->tid, map);
48 static void *thread_fn(void *arg)
50 struct thread_data *td = arg;
57 /* Signal thread_create thread is initialized. */
58 ret = write(td->ready[1], &go, sizeof(int));
59 if (ret != sizeof(int)) {
60 pr_err("failed to notify\n");
65 /* Waiting for main thread to kill us. */
69 munmap(td->map, page_size);
73 static int thread_create(int i)
75 struct thread_data *td = &threads[i];
81 err = pthread_create(&td->pt, NULL, thread_fn, td);
83 /* Wait for thread initialization. */
84 ssize_t ret = read(td->ready[0], &go, sizeof(int));
85 err = ret != sizeof(int);
93 static int threads_create(void)
95 struct thread_data *td0 = &threads[0];
100 /* 0 is main thread */
101 if (thread_init(td0))
104 for (i = 1; !err && i < THREADS; i++)
105 err = thread_create(i);
110 static int threads_destroy(void)
112 struct thread_data *td0 = &threads[0];
115 /* cleanup the main thread */
116 munmap(td0->map, page_size);
120 for (i = 1; !err && i < THREADS; i++)
121 err = pthread_join(threads[i].pt, NULL);
126 typedef int (*synth_cb)(struct machine *machine);
128 static int synth_all(struct machine *machine)
130 return perf_event__synthesize_threads(NULL,
135 static int synth_process(struct machine *machine)
137 struct thread_map *map;
140 map = thread_map__new_by_pid(getpid());
142 err = perf_event__synthesize_thread_map(NULL, map,
146 thread_map__put(map);
150 static int mmap_events(synth_cb synth)
152 struct machines machines;
153 struct machine *machine;
157 * The threads_create will not return before all threads
158 * are spawned and all created memory map.
160 * They will loop until threads_destroy is called, so we
161 * can safely run synthesizing function.
163 TEST_ASSERT_VAL("failed to create threads", !threads_create());
165 machines__init(&machines);
166 machine = &machines.host;
168 dump_trace = verbose > 1 ? 1 : 0;
170 err = synth(machine);
174 TEST_ASSERT_VAL("failed to destroy threads", !threads_destroy());
175 TEST_ASSERT_VAL("failed to synthesize maps", !err);
178 * All data is synthesized, try to find map for each
181 for (i = 0; i < THREADS; i++) {
182 struct thread_data *td = &threads[i];
183 struct addr_location al;
184 struct thread *thread;
186 thread = machine__findnew_thread(machine, getpid(), td->tid);
188 pr_debug("looking for map %p\n", td->map);
190 thread__find_addr_map(thread,
191 PERF_RECORD_MISC_USER, MAP__FUNCTION,
192 (unsigned long) (td->map + 1), &al);
197 pr_debug("failed, couldn't find map\n");
202 pr_debug("map %p, addr %" PRIx64 "\n", al.map, al.map->start);
205 machine__delete_threads(machine);
206 machines__exit(&machines);
211 * This test creates 'THREADS' number of threads (including
212 * main thread) and each thread creates memory map.
214 * When threads are created, we synthesize them with both
216 * perf_event__synthesize_thread_map (process based)
217 * perf_event__synthesize_threads (global)
219 * We test we can find all memory maps via:
220 * thread__find_addr_map
222 * by using all thread objects.
224 int test__mmap_thread_lookup(void)
226 /* perf_event__synthesize_threads synthesize */
227 TEST_ASSERT_VAL("failed with sythesizing all",
228 !mmap_events(synth_all));
230 /* perf_event__synthesize_thread_map synthesize */
231 TEST_ASSERT_VAL("failed with sythesizing process",
232 !mmap_events(synth_process));