2014-03-05 17:20:31 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/syscall.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "debug.h"
|
|
|
|
#include "tests.h"
|
|
|
|
#include "machine.h"
|
|
|
|
#include "thread_map.h"
|
|
|
|
#include "symbol.h"
|
|
|
|
#include "thread.h"
|
|
|
|
|
|
|
|
#define THREADS 4
|
|
|
|
|
|
|
|
static int go_away;
|
|
|
|
|
|
|
|
struct thread_data {
|
|
|
|
pthread_t pt;
|
|
|
|
pid_t tid;
|
|
|
|
void *map;
|
|
|
|
int ready[2];
|
|
|
|
};
|
|
|
|
|
|
|
|
static struct thread_data threads[THREADS];
|
|
|
|
|
|
|
|
static int thread_init(struct thread_data *td)
|
|
|
|
{
|
|
|
|
void *map;
|
|
|
|
|
|
|
|
map = mmap(NULL, page_size,
|
|
|
|
PROT_READ|PROT_WRITE|PROT_EXEC,
|
|
|
|
MAP_SHARED|MAP_ANONYMOUS, -1, 0);
|
|
|
|
|
|
|
|
if (map == MAP_FAILED) {
|
|
|
|
perror("mmap failed");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
td->map = map;
|
|
|
|
td->tid = syscall(SYS_gettid);
|
|
|
|
|
|
|
|
pr_debug("tid = %d, map = %p\n", td->tid, map);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void *thread_fn(void *arg)
|
|
|
|
{
|
|
|
|
struct thread_data *td = arg;
|
|
|
|
ssize_t ret;
|
|
|
|
int go;
|
|
|
|
|
|
|
|
if (thread_init(td))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Signal thread_create thread is initialized. */
|
|
|
|
ret = write(td->ready[1], &go, sizeof(int));
|
|
|
|
if (ret != sizeof(int)) {
|
|
|
|
pr_err("failed to notify\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!go_away) {
|
|
|
|
/* Waiting for main thread to kill us. */
|
|
|
|
usleep(100);
|
|
|
|
}
|
|
|
|
|
|
|
|
munmap(td->map, page_size);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int thread_create(int i)
|
|
|
|
{
|
|
|
|
struct thread_data *td = &threads[i];
|
|
|
|
int err, go;
|
|
|
|
|
|
|
|
if (pipe(td->ready))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
err = pthread_create(&td->pt, NULL, thread_fn, td);
|
|
|
|
if (!err) {
|
|
|
|
/* Wait for thread initialization. */
|
|
|
|
ssize_t ret = read(td->ready[0], &go, sizeof(int));
|
|
|
|
err = ret != sizeof(int);
|
|
|
|
}
|
|
|
|
|
|
|
|
close(td->ready[0]);
|
|
|
|
close(td->ready[1]);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int threads_create(void)
|
|
|
|
{
|
|
|
|
struct thread_data *td0 = &threads[0];
|
|
|
|
int i, err = 0;
|
|
|
|
|
|
|
|
go_away = 0;
|
|
|
|
|
|
|
|
/* 0 is main thread */
|
|
|
|
if (thread_init(td0))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (i = 1; !err && i < THREADS; i++)
|
|
|
|
err = thread_create(i);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int threads_destroy(void)
|
|
|
|
{
|
|
|
|
struct thread_data *td0 = &threads[0];
|
|
|
|
int i, err = 0;
|
|
|
|
|
|
|
|
/* cleanup the main thread */
|
|
|
|
munmap(td0->map, page_size);
|
|
|
|
|
|
|
|
go_away = 1;
|
|
|
|
|
|
|
|
for (i = 1; !err && i < THREADS; i++)
|
|
|
|
err = pthread_join(threads[i].pt, NULL);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef int (*synth_cb)(struct machine *machine);
|
|
|
|
|
|
|
|
static int synth_all(struct machine *machine)
|
|
|
|
{
|
|
|
|
return perf_event__synthesize_threads(NULL,
|
|
|
|
perf_event__process,
|
2015-06-17 15:51:11 +02:00
|
|
|
machine, 0, 500);
|
2014-03-05 17:20:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int synth_process(struct machine *machine)
|
|
|
|
{
|
|
|
|
struct thread_map *map;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
map = thread_map__new_by_pid(getpid());
|
|
|
|
|
|
|
|
err = perf_event__synthesize_thread_map(NULL, map,
|
|
|
|
perf_event__process,
|
2015-06-17 15:51:11 +02:00
|
|
|
machine, 0, 500);
|
2014-03-05 17:20:31 +01:00
|
|
|
|
2015-06-23 00:36:05 +02:00
|
|
|
thread_map__put(map);
|
2014-03-05 17:20:31 +01:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mmap_events(synth_cb synth)
|
|
|
|
{
|
|
|
|
struct machine *machine;
|
|
|
|
int err, i;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The threads_create will not return before all threads
|
|
|
|
* are spawned and all created memory map.
|
|
|
|
*
|
|
|
|
* They will loop until threads_destroy is called, so we
|
|
|
|
* can safely run synthesizing function.
|
|
|
|
*/
|
|
|
|
TEST_ASSERT_VAL("failed to create threads", !threads_create());
|
|
|
|
|
2015-12-03 09:34:13 +01:00
|
|
|
machine = machine__new_host();
|
2014-03-05 17:20:31 +01:00
|
|
|
|
|
|
|
dump_trace = verbose > 1 ? 1 : 0;
|
|
|
|
|
|
|
|
err = synth(machine);
|
|
|
|
|
|
|
|
dump_trace = 0;
|
|
|
|
|
|
|
|
TEST_ASSERT_VAL("failed to destroy threads", !threads_destroy());
|
|
|
|
TEST_ASSERT_VAL("failed to synthesize maps", !err);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* All data is synthesized, try to find map for each
|
|
|
|
* thread object.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < THREADS; i++) {
|
|
|
|
struct thread_data *td = &threads[i];
|
|
|
|
struct addr_location al;
|
|
|
|
struct thread *thread;
|
|
|
|
|
|
|
|
thread = machine__findnew_thread(machine, getpid(), td->tid);
|
|
|
|
|
|
|
|
pr_debug("looking for map %p\n", td->map);
|
|
|
|
|
2014-10-23 17:50:25 +02:00
|
|
|
thread__find_addr_map(thread,
|
2014-03-05 17:20:31 +01:00
|
|
|
PERF_RECORD_MISC_USER, MAP__FUNCTION,
|
|
|
|
(unsigned long) (td->map + 1), &al);
|
|
|
|
|
perf machine: Protect the machine->threads with a rwlock
In addition to using refcounts for the struct thread lifetime
management, we need to protect access to machine->threads from
concurrent access.
That happens in 'perf top', where a thread processes events, inserting
and deleting entries from that rb_tree while another thread decays
hist_entries, that end up dropping references and ultimately deleting
threads from the rb_tree and releasing its resources when no further
hist_entry (or other data structures, like in 'perf sched') references
it.
So the rule is the same for refcounts + protected trees in the kernel,
get the tree lock, find object, bump the refcount, drop the tree lock,
return, use object, drop the refcount if no more use of it is needed,
keep it if storing it in some other data structure, drop when releasing
that data structure.
I.e. pair "t = machine__find(new)_thread()" with a "thread__put(t)", and
"perf_event__preprocess_sample(&al)" with "addr_location__put(&al)".
The addr_location__put() one is because as we return references to
several data structures, we may end up adding more reference counting
for the other data structures and then we'll drop it at
addr_location__put() time.
Acked-by: David Ahern <dsahern@gmail.com>
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: Borislav Petkov <bp@suse.de>
Cc: Don Zickus <dzickus@redhat.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lkml.kernel.org/n/tip-bs9rt4n0jw3hi9f3zxyy3xln@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2015-04-07 01:43:22 +02:00
|
|
|
thread__put(thread);
|
|
|
|
|
2014-03-05 17:20:31 +01:00
|
|
|
if (!al.map) {
|
|
|
|
pr_debug("failed, couldn't find map\n");
|
|
|
|
err = -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
pr_debug("map %p, addr %" PRIx64 "\n", al.map, al.map->start);
|
|
|
|
}
|
|
|
|
|
|
|
|
machine__delete_threads(machine);
|
2015-12-03 09:34:13 +01:00
|
|
|
machine__delete(machine);
|
2014-03-05 17:20:31 +01:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This test creates 'THREADS' number of threads (including
|
|
|
|
* main thread) and each thread creates memory map.
|
|
|
|
*
|
|
|
|
* When threads are created, we synthesize them with both
|
|
|
|
* (separate tests):
|
|
|
|
* perf_event__synthesize_thread_map (process based)
|
|
|
|
* perf_event__synthesize_threads (global)
|
|
|
|
*
|
|
|
|
* We test we can find all memory maps via:
|
|
|
|
* thread__find_addr_map
|
|
|
|
*
|
|
|
|
* by using all thread objects.
|
|
|
|
*/
|
perf tests: Pass the subtest index to each test routine
Some tests have sub-tests we want to run, so allow passing this.
Wang tried to avoid having to touch all tests, but then, having the
test.func in an anonymous union makes the build fail on older compilers,
like the one in RHEL6, where:
test a = {
.func = foo,
};
fails.
To fix it leave the func pointer in the main structure and pass the subtest
index to all tests, end result function is the same, but we have just one
function pointer, not two, with and without the subtest index as an argument.
Cc: Adrian Hunter <adrian.hunter@intel.com>
Cc: David Ahern <dsahern@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Wang Nan <wangnan0@huawei.com>
Link: http://lkml.kernel.org/n/tip-5genj0ficwdmelpoqlds0u4y@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2015-11-19 16:01:48 +01:00
|
|
|
int test__mmap_thread_lookup(int subtest __maybe_unused)
|
2014-03-05 17:20:31 +01:00
|
|
|
{
|
|
|
|
/* perf_event__synthesize_threads synthesize */
|
|
|
|
TEST_ASSERT_VAL("failed with sythesizing all",
|
|
|
|
!mmap_events(synth_all));
|
|
|
|
|
|
|
|
/* perf_event__synthesize_thread_map synthesize */
|
|
|
|
TEST_ASSERT_VAL("failed with sythesizing process",
|
|
|
|
!mmap_events(synth_process));
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|