From: Namhyung Kim <namhyung@kernel.org>
To: Arnaldo Carvalho de Melo <acme@kernel.org>
Cc: Ingo Molnar <mingo@kernel.org>,
Peter Zijlstra <a.p.zijlstra@chello.nl>,
Jiri Olsa <jolsa@redhat.com>, LKML <linux-kernel@vger.kernel.org>,
Frederic Weisbecker <fweisbec@gmail.com>,
Stephane Eranian <eranian@google.com>,
David Ahern <dsahern@gmail.com>, Andi Kleen <andi@firstfloor.org>
Subject: [RFC/PATCH 15/38] perf tools: Introduce machine__find*_thread_by_time()
Date: Fri, 2 Oct 2015 14:18:56 +0900 [thread overview]
Message-ID: <1443763159-29098-16-git-send-email-namhyung@kernel.org> (raw)
In-Reply-To: <1443763159-29098-1-git-send-email-namhyung@kernel.org>
With data file indexing is enabled, it needs to search thread based on
sample time since sample processing is done after other (task, comm and
mmap) events are processed. This can be a problem if a session is very
long and pid is recycled - in that case it'll only see the last one.
So keep thread start time in it, and search thread based on the time.
This patch introduces machine__find{,new}_thread_by_time() function
for this. It'll first search current (i.e. recent) thread rbtree and
then dead thread tree (and tid list). If it couldn't find anyone,
it'll create a new (missing) thread.
The sample timestamp of 0 means that this is called from synthesized
event so just use current rbtree. The timestamp will be -1 if sample
didn't record the timestamp so will see current threads automatically.
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
tools/perf/tests/dwarf-unwind.c | 8 +--
tools/perf/tests/hists_common.c | 3 +-
tools/perf/tests/hists_link.c | 2 +-
tools/perf/util/event.c | 6 +-
tools/perf/util/machine.c | 126 +++++++++++++++++++++++++++++++++++++++-
tools/perf/util/machine.h | 10 +++-
tools/perf/util/thread.c | 4 ++
tools/perf/util/thread.h | 1 +
8 files changed, 148 insertions(+), 12 deletions(-)
diff --git a/tools/perf/tests/dwarf-unwind.c b/tools/perf/tests/dwarf-unwind.c
index 40b36c462427..b9ca0a72fc4d 100644
--- a/tools/perf/tests/dwarf-unwind.c
+++ b/tools/perf/tests/dwarf-unwind.c
@@ -16,10 +16,10 @@
static int mmap_handler(struct perf_tool *tool __maybe_unused,
union perf_event *event,
- struct perf_sample *sample __maybe_unused,
+ struct perf_sample *sample,
struct machine *machine)
{
- return machine__process_mmap2_event(machine, event, NULL);
+ return machine__process_mmap2_event(machine, event, sample);
}
static int init_live_machine(struct machine *machine)
@@ -66,12 +66,10 @@ static int unwind_entry(struct unwind_entry *entry, void *arg)
__attribute__ ((noinline))
static int unwind_thread(struct thread *thread)
{
- struct perf_sample sample;
+ struct perf_sample sample = { .time = -1ULL, };
unsigned long cnt = 0;
int err = -1;
- memset(&sample, 0, sizeof(sample));
-
if (test__arch_unwind_sample(&sample, thread)) {
pr_debug("failed to get unwind sample\n");
goto out;
diff --git a/tools/perf/tests/hists_common.c b/tools/perf/tests/hists_common.c
index ce80b274b097..1d657fa2830f 100644
--- a/tools/perf/tests/hists_common.c
+++ b/tools/perf/tests/hists_common.c
@@ -80,6 +80,7 @@ static struct {
struct machine *setup_fake_machine(struct machines *machines)
{
struct machine *machine = machines__find(machines, HOST_KERNEL_ID);
+ struct perf_sample sample = { .time = -1ULL, };
size_t i;
if (machine == NULL) {
@@ -114,7 +115,7 @@ struct machine *setup_fake_machine(struct machines *machines)
strcpy(fake_mmap_event.mmap.filename,
fake_mmap_info[i].filename);
- machine__process_mmap_event(machine, &fake_mmap_event, NULL);
+ machine__process_mmap_event(machine, &fake_mmap_event, &sample);
}
for (i = 0; i < ARRAY_SIZE(fake_symbols); i++) {
diff --git a/tools/perf/tests/hists_link.c b/tools/perf/tests/hists_link.c
index 27bae90c9a95..cacc8617bf02 100644
--- a/tools/perf/tests/hists_link.c
+++ b/tools/perf/tests/hists_link.c
@@ -64,7 +64,7 @@ static int add_hist_entries(struct perf_evlist *evlist, struct machine *machine)
struct perf_evsel *evsel;
struct addr_location al;
struct hist_entry *he;
- struct perf_sample sample = { .period = 1, };
+ struct perf_sample sample = { .period = 1, .time = -1ULL, };
size_t i = 0, k;
/*
diff --git a/tools/perf/util/event.c b/tools/perf/util/event.c
index cb98b5af9e17..3dff1b5cd4cc 100644
--- a/tools/perf/util/event.c
+++ b/tools/perf/util/event.c
@@ -9,6 +9,7 @@
#include "strlist.h"
#include "thread.h"
#include "thread_map.h"
+#include "session.h"
#include "symbol/kallsyms.h"
static const char *perf_event__names[] = {
@@ -992,9 +993,10 @@ int perf_event__preprocess_sample(const union perf_event *event,
struct perf_sample *sample)
{
u8 cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK;
- struct thread *thread = machine__findnew_thread(machine, sample->pid,
- sample->tid);
+ struct thread *thread;
+ thread = machine__findnew_thread_by_time(machine, sample->pid,
+ sample->tid, sample->time);
if (thread == NULL)
return -1;
diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c
index 2b9fbe55c896..7cfaa2c3f131 100644
--- a/tools/perf/util/machine.c
+++ b/tools/perf/util/machine.c
@@ -475,6 +475,120 @@ struct thread *machine__find_thread(struct machine *machine, pid_t pid,
return th;
}
+static struct thread *
+__machine__findnew_thread_by_time(struct machine *machine, pid_t pid, pid_t tid,
+ u64 timestamp, bool create)
+{
+ struct thread *curr, *pos, *new;
+ struct thread *th = NULL;
+ struct rb_node **p;
+ struct rb_node *parent = NULL;
+
+ if (!perf_has_index)
+ return ____machine__findnew_thread(machine, pid, tid, create);
+
+ /* lookup current thread first */
+ curr = ____machine__findnew_thread(machine, pid, tid, false);
+ if (curr && timestamp >= curr->start_time)
+ return curr;
+
+ /* and then check dead threads tree & list */
+ p = &machine->dead_threads.rb_node;
+ while (*p != NULL) {
+ parent = *p;
+ th = rb_entry(parent, struct thread, rb_node);
+
+ if (th->tid == tid) {
+ list_for_each_entry(pos, &th->tid_list, tid_list) {
+ if (timestamp >= pos->start_time &&
+ pos->start_time > th->start_time) {
+ th = pos;
+ break;
+ }
+ }
+
+ if (timestamp >= th->start_time) {
+ machine__update_thread_pid(machine, th, pid);
+ return th;
+ }
+ break;
+ }
+
+ if (tid < th->tid)
+ p = &(*p)->rb_left;
+ else
+ p = &(*p)->rb_right;
+ }
+
+ if (!create)
+ return NULL;
+
+ if (!curr && !*p) {
+ /* found no thread. create one as current thread */
+ return __machine__findnew_thread(machine, pid, tid);
+ }
+
+ new = thread__new(pid, tid);
+ if (new == NULL)
+ return NULL;
+
+ new->dead = true;
+ new->start_time = timestamp;
+
+ if (*p) {
+ list_for_each_entry(pos, &th->tid_list, tid_list) {
+ /* sort by time */
+ if (timestamp >= pos->start_time) {
+ th = pos;
+ break;
+ }
+ }
+ list_add_tail(&new->tid_list, &th->tid_list);
+ } else {
+ rb_link_node(&new->rb_node, parent, p);
+ rb_insert_color(&new->rb_node, &machine->dead_threads);
+ }
+
+ thread__get(new);
+
+ /*
+ * We have to initialize map_groups separately
+ * after rb tree is updated.
+ *
+ * The reason is that we call machine__findnew_thread
+ * within thread__init_map_groups to find the thread
+ * leader and that would screwed the rb tree.
+ */
+ if (thread__init_map_groups(new, machine))
+ thread__zput(new);
+
+ return new;
+}
+
+struct thread *machine__find_thread_by_time(struct machine *machine, pid_t pid,
+ pid_t tid, u64 timestamp)
+{
+ struct thread *th;
+
+ pthread_rwlock_rdlock(&machine->threads_lock);
+ th = thread__get(__machine__findnew_thread_by_time(machine, pid, tid,
+ timestamp, false));
+ pthread_rwlock_unlock(&machine->threads_lock);
+ return th;
+}
+
+struct thread *machine__findnew_thread_by_time(struct machine *machine, pid_t pid,
+ pid_t tid, u64 timestamp)
+{
+ struct thread *th;
+
+ pthread_rwlock_wrlock(&machine->threads_lock);
+ th = thread__get(__machine__findnew_thread_by_time(machine, pid, tid,
+ timestamp, true));
+ pthread_rwlock_unlock(&machine->threads_lock);
+ return th;
+}
+
struct comm *machine__thread_exec_comm(struct machine *machine,
struct thread *thread)
{
@@ -1299,7 +1413,7 @@ int machine__process_mmap2_event(struct machine *machine,
}
thread = machine__findnew_thread(machine, event->mmap2.pid,
- event->mmap2.tid);
+ event->mmap2.tid);
if (thread == NULL)
goto out_problem;
@@ -1419,6 +1533,16 @@ static void __machine__remove_thread(struct machine *machine, struct thread *th,
pos = rb_entry(parent, struct thread, rb_node);
if (pos->tid == th->tid) {
+ struct thread *old;
+
+ /* sort by time */
+ list_for_each_entry(old, &pos->tid_list, tid_list) {
+ if (th->start_time >= old->start_time) {
+ pos = old;
+ break;
+ }
+ }
+
list_add_tail(&th->tid_list, &pos->tid_list);
goto out;
}
diff --git a/tools/perf/util/machine.h b/tools/perf/util/machine.h
index 23242de926df..f0c2cc9c90ae 100644
--- a/tools/perf/util/machine.h
+++ b/tools/perf/util/machine.h
@@ -75,8 +75,6 @@ static inline bool machine__kernel_ip(struct machine *machine, u64 ip)
return ip >= kernel_start;
}
-struct thread *machine__find_thread(struct machine *machine, pid_t pid,
- pid_t tid);
struct comm *machine__thread_exec_comm(struct machine *machine,
struct thread *thread);
@@ -164,6 +162,14 @@ static inline bool machine__is_host(struct machine *machine)
struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid);
struct thread *machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid);
+struct thread *machine__find_thread(struct machine *machine, pid_t pid,
+ pid_t tid);
+struct thread *machine__findnew_thread_by_time(struct machine *machine,
+ pid_t pid, pid_t tid,
+ u64 timestamp);
+struct thread *machine__find_thread_by_time(struct machine *machine,
+ pid_t pid, pid_t tid,
+ u64 timestamp);
struct dso *machine__findnew_dso(struct machine *machine, const char *filename);
diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c
index 674792e8fa2f..ad7c2a00bff8 100644
--- a/tools/perf/util/thread.c
+++ b/tools/perf/util/thread.c
@@ -160,6 +160,9 @@ int __thread__set_comm(struct thread *thread, const char *str, u64 timestamp,
/* Override the default :tid entry */
if (!thread->comm_set) {
+ if (!thread->start_time)
+ thread->start_time = timestamp;
+
err = comm__override(curr, str, timestamp, exec);
if (err)
return err;
@@ -266,6 +269,7 @@ int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp)
}
thread->ppid = parent->tid;
+ thread->start_time = timestamp;
return thread__clone_map_groups(thread, parent);
}
diff --git a/tools/perf/util/thread.h b/tools/perf/util/thread.h
index b8f794d97b75..97026a9660ec 100644
--- a/tools/perf/util/thread.h
+++ b/tools/perf/util/thread.h
@@ -28,6 +28,7 @@ struct thread {
bool dead; /* thread is in dead_threads list */
struct list_head comm_list;
u64 db_id;
+ u64 start_time;
void *priv;
struct thread_stack *ts;
--
2.6.0
next prev parent reply other threads:[~2015-10-02 5:28 UTC|newest]
Thread overview: 63+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-10-02 5:18 [RFC/PATCH 00/38] perf tools: Speed-up perf report by using multi thread (v5) Namhyung Kim
2015-10-02 5:18 ` [RFC/PATCH 01/38] perf tools: Use a software dummy event to track task/mmap events Namhyung Kim
2015-10-05 12:51 ` Jiri Olsa
2015-10-06 8:31 ` Namhyung Kim
2015-10-02 5:18 ` [RFC/PATCH 02/38] perf tools: Save mmap_param.len instead of mask Namhyung Kim
2015-10-02 18:44 ` Arnaldo Carvalho de Melo
2015-10-06 8:34 ` Namhyung Kim
2015-10-08 10:17 ` Jiri Olsa
2015-10-09 6:03 ` Namhyung Kim
2015-10-12 12:42 ` Jiri Olsa
2015-10-02 5:18 ` [RFC/PATCH 03/38] perf tools: Move auxtrace_mmap field to struct perf_evlist Namhyung Kim
2015-10-02 18:45 ` Arnaldo Carvalho de Melo
2015-10-05 11:29 ` Adrian Hunter
2015-10-06 9:03 ` Namhyung Kim
2015-10-06 9:26 ` Adrian Hunter
2015-10-07 9:06 ` Namhyung Kim
2015-10-08 16:07 ` Adrian Hunter
2015-10-09 7:54 ` Namhyung Kim
2015-10-06 8:56 ` Namhyung Kim
2015-10-05 13:14 ` Jiri Olsa
2015-10-06 8:40 ` Namhyung Kim
2015-10-08 10:18 ` Jiri Olsa
2015-10-02 5:18 ` [RFC/PATCH 04/38] perf tools: pass perf_mmap desc directly Namhyung Kim
2015-10-02 18:47 ` Arnaldo Carvalho de Melo
2015-10-02 5:18 ` [RFC/PATCH 05/38] perf tools: Create separate mmap for dummy tracking event Namhyung Kim
2015-10-02 5:18 ` [RFC/PATCH 06/38] perf tools: Extend perf_evlist__mmap_ex() to use track mmap Namhyung Kim
2015-10-02 5:18 ` [RFC/PATCH 07/38] perf tools: Add HEADER_DATA_INDEX feature Namhyung Kim
2015-10-02 5:18 ` [RFC/PATCH 08/38] perf tools: Handle indexed data file properly Namhyung Kim
2015-10-02 5:18 ` [RFC/PATCH 09/38] perf record: Add --index option for building index table Namhyung Kim
2015-10-02 18:58 ` Arnaldo Carvalho de Melo
2015-10-05 13:46 ` Jiri Olsa
2015-10-07 8:21 ` Namhyung Kim
2015-10-07 12:10 ` Jiri Olsa
2015-10-02 5:18 ` [RFC/PATCH 10/38] perf report: Skip dummy tracking event Namhyung Kim
2015-10-02 5:18 ` [RFC/PATCH 11/38] perf tools: Introduce thread__comm(_str)_by_time() helpers Namhyung Kim
2015-10-02 5:18 ` [RFC/PATCH 12/38] perf tools: Add a test case for thread comm handling Namhyung Kim
2015-10-02 5:18 ` [RFC/PATCH 13/38] perf tools: Use thread__comm_by_time() when adding hist entries Namhyung Kim
2015-10-02 5:18 ` [RFC/PATCH 14/38] perf tools: Convert dead thread list into rbtree Namhyung Kim
2015-10-02 5:18 ` Namhyung Kim [this message]
2015-10-08 12:20 ` [RFC/PATCH 15/38] perf tools: Introduce machine__find*_thread_by_time() Jiri Olsa
2015-10-09 6:04 ` Namhyung Kim
2015-10-02 5:18 ` [RFC/PATCH 16/38] perf tools: Add a test case for timed thread handling Namhyung Kim
2015-10-02 5:18 ` [RFC/PATCH 17/38] perf tools: Maintain map groups list in a leader thread Namhyung Kim
2015-10-08 12:51 ` Jiri Olsa
2015-10-09 6:24 ` Namhyung Kim
2015-10-08 12:58 ` Jiri Olsa
2015-10-09 6:58 ` Namhyung Kim
2015-10-12 12:43 ` Jiri Olsa
2015-10-02 5:18 ` [RFC/PATCH 18/38] perf tools: Introduce thread__find_addr_location_by_time() and friends Namhyung Kim
2015-10-12 13:35 ` Jiri Olsa
2015-10-02 5:19 ` [RFC/PATCH 19/38] perf callchain: Use " Namhyung Kim
2015-10-02 5:19 ` [RFC/PATCH 20/38] perf tools: Add a test case for timed map groups handling Namhyung Kim
2015-10-02 5:19 ` [RFC/PATCH 21/38] perf tools: Save timestamp of a map creation Namhyung Kim
2015-10-02 5:19 ` [RFC/PATCH 22/38] perf tools: Introduce map_groups__{insert,find}_by_time() Namhyung Kim
2015-10-02 5:19 ` [RFC/PATCH 23/38] perf tools: Use map_groups__find_addr_by_time() Namhyung Kim
2015-10-02 5:19 ` [RFC/PATCH 24/38] perf tools: Add testcase for managing maps with time Namhyung Kim
2015-10-02 5:19 ` [RFC/PATCH 25/38] perf callchain: Maintain libunwind's address space in map_groups Namhyung Kim
2015-10-02 5:19 ` [RFC/PATCH 26/38] perf session: Pass struct events stats to event processing functions Namhyung Kim
2015-10-02 5:19 ` [RFC/PATCH 27/38] perf hists: Pass hists struct to hist_entry_iter struct Namhyung Kim
2015-10-02 5:19 ` [RFC/PATCH 28/38] perf tools: Move BUILD_ID_SIZE definition to perf.h Namhyung Kim
2015-10-02 5:22 ` Namhyung Kim
2015-10-02 6:58 ` Namhyung Kim
2015-10-12 14:32 ` Jiri Olsa
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1443763159-29098-16-git-send-email-namhyung@kernel.org \
--to=namhyung@kernel.org \
--cc=a.p.zijlstra@chello.nl \
--cc=acme@kernel.org \
--cc=andi@firstfloor.org \
--cc=dsahern@gmail.com \
--cc=eranian@google.com \
--cc=fweisbec@gmail.com \
--cc=jolsa@redhat.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Powered by JetHome