Merge branch 'fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/evalenti/linux...
[linux/fpc-iii.git] / tools / perf / tests / mmap-thread-lookup.c
blob0c5ce44f723fcbfd9c43b6c15377c27d2d503038
1 #include <unistd.h>
2 #include <sys/syscall.h>
3 #include <sys/types.h>
4 #include <sys/mman.h>
5 #include <pthread.h>
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include "debug.h"
9 #include "tests.h"
10 #include "machine.h"
11 #include "thread_map.h"
12 #include "symbol.h"
13 #include "thread.h"
15 #define THREADS 4
17 static int go_away;
19 struct thread_data {
20 pthread_t pt;
21 pid_t tid;
22 void *map;
23 int ready[2];
26 static struct thread_data threads[THREADS];
28 static int thread_init(struct thread_data *td)
30 void *map;
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");
38 return -1;
41 td->map = map;
42 td->tid = syscall(SYS_gettid);
44 pr_debug("tid = %d, map = %p\n", td->tid, map);
45 return 0;
48 static void *thread_fn(void *arg)
50 struct thread_data *td = arg;
51 ssize_t ret;
52 int go;
54 if (thread_init(td))
55 return NULL;
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");
61 return NULL;
64 while (!go_away) {
65 /* Waiting for main thread to kill us. */
66 usleep(100);
69 munmap(td->map, page_size);
70 return NULL;
73 static int thread_create(int i)
75 struct thread_data *td = &threads[i];
76 int err, go;
78 if (pipe(td->ready))
79 return -1;
81 err = pthread_create(&td->pt, NULL, thread_fn, td);
82 if (!err) {
83 /* Wait for thread initialization. */
84 ssize_t ret = read(td->ready[0], &go, sizeof(int));
85 err = ret != sizeof(int);
88 close(td->ready[0]);
89 close(td->ready[1]);
90 return err;
93 static int threads_create(void)
95 struct thread_data *td0 = &threads[0];
96 int i, err = 0;
98 go_away = 0;
100 /* 0 is main thread */
101 if (thread_init(td0))
102 return -1;
104 for (i = 1; !err && i < THREADS; i++)
105 err = thread_create(i);
107 return err;
110 static int threads_destroy(void)
112 struct thread_data *td0 = &threads[0];
113 int i, err = 0;
115 /* cleanup the main thread */
116 munmap(td0->map, page_size);
118 go_away = 1;
120 for (i = 1; !err && i < THREADS; i++)
121 err = pthread_join(threads[i].pt, NULL);
123 return err;
126 typedef int (*synth_cb)(struct machine *machine);
128 static int synth_all(struct machine *machine)
130 return perf_event__synthesize_threads(NULL,
131 perf_event__process,
132 machine, 0, 500);
135 static int synth_process(struct machine *machine)
137 struct thread_map *map;
138 int err;
140 map = thread_map__new_by_pid(getpid());
142 err = perf_event__synthesize_thread_map(NULL, map,
143 perf_event__process,
144 machine, 0, 500);
146 thread_map__put(map);
147 return err;
150 static int mmap_events(synth_cb synth)
152 struct machine *machine;
153 int err, i;
156 * The threads_create will not return before all threads
157 * are spawned and all created memory map.
159 * They will loop until threads_destroy is called, so we
160 * can safely run synthesizing function.
162 TEST_ASSERT_VAL("failed to create threads", !threads_create());
164 machine = machine__new_host();
166 dump_trace = verbose > 1 ? 1 : 0;
168 err = synth(machine);
170 dump_trace = 0;
172 TEST_ASSERT_VAL("failed to destroy threads", !threads_destroy());
173 TEST_ASSERT_VAL("failed to synthesize maps", !err);
176 * All data is synthesized, try to find map for each
177 * thread object.
179 for (i = 0; i < THREADS; i++) {
180 struct thread_data *td = &threads[i];
181 struct addr_location al;
182 struct thread *thread;
184 thread = machine__findnew_thread(machine, getpid(), td->tid);
186 pr_debug("looking for map %p\n", td->map);
188 thread__find_addr_map(thread,
189 PERF_RECORD_MISC_USER, MAP__FUNCTION,
190 (unsigned long) (td->map + 1), &al);
192 thread__put(thread);
194 if (!al.map) {
195 pr_debug("failed, couldn't find map\n");
196 err = -1;
197 break;
200 pr_debug("map %p, addr %" PRIx64 "\n", al.map, al.map->start);
203 machine__delete_threads(machine);
204 machine__delete(machine);
205 return err;
209 * This test creates 'THREADS' number of threads (including
210 * main thread) and each thread creates memory map.
212 * When threads are created, we synthesize them with both
213 * (separate tests):
214 * perf_event__synthesize_thread_map (process based)
215 * perf_event__synthesize_threads (global)
217 * We test we can find all memory maps via:
218 * thread__find_addr_map
220 * by using all thread objects.
222 int test__mmap_thread_lookup(int subtest __maybe_unused)
224 /* perf_event__synthesize_threads synthesize */
225 TEST_ASSERT_VAL("failed with sythesizing all",
226 !mmap_events(synth_all));
228 /* perf_event__synthesize_thread_map synthesize */
229 TEST_ASSERT_VAL("failed with sythesizing process",
230 !mmap_events(synth_process));
232 return 0;