[tip:perf/core] perf machine: Introduce find_thread method
Commit-ID: 9d2f8e22fc965bcdd5561d000d234fe2d23657ba Gitweb: http://git.kernel.org/tip/9d2f8e22fc965bcdd5561d000d234fe2d23657ba Author: Arnaldo Carvalho de Melo AuthorDate: Sat, 6 Oct 2012 15:43:20 -0300 Committer: Arnaldo Carvalho de Melo CommitDate: Sat, 6 Oct 2012 16:33:22 -0300 perf machine: Introduce find_thread method There are cases where we want just to find a thread if it exists already, so provide a method for that. While doing that start moving 'machine' methods to a separate file. Cc: David Ahern Cc: Frederic Weisbecker Cc: Jiri Olsa Cc: Mike Galbraith Cc: Namhyung Kim Cc: Paul Mackerras Cc: Peter Zijlstra Cc: Stephane Eranian Link: http://lkml.kernel.org/n/tip-8wpzqs9kfupng6xq8hx6l...@git.kernel.org Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/Makefile |2 + tools/perf/util/machine.c | 57 + tools/perf/util/machine.h | 11 tools/perf/util/thread.c | 41 +--- tools/perf/util/thread.h |2 + 5 files changed, 73 insertions(+), 40 deletions(-) diff --git a/tools/perf/Makefile b/tools/perf/Makefile index f9126f8..d80a333 100644 --- a/tools/perf/Makefile +++ b/tools/perf/Makefile @@ -300,6 +300,7 @@ LIB_H += util/evlist.h LIB_H += util/exec_cmd.h LIB_H += util/types.h LIB_H += util/levenshtein.h +LIB_H += util/machine.h LIB_H += util/map.h LIB_H += util/parse-options.h LIB_H += util/parse-events.h @@ -383,6 +384,7 @@ LIB_OBJS += $(OUTPUT)util/header.o LIB_OBJS += $(OUTPUT)util/callchain.o LIB_OBJS += $(OUTPUT)util/values.o LIB_OBJS += $(OUTPUT)util/debug.o +LIB_OBJS += $(OUTPUT)util/machine.o LIB_OBJS += $(OUTPUT)util/map.o LIB_OBJS += $(OUTPUT)util/pstack.o LIB_OBJS += $(OUTPUT)util/session.o diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c new file mode 100644 index 000..9d36d7e --- /dev/null +++ b/tools/perf/util/machine.c @@ -0,0 +1,57 @@ +#include "machine.h" +#include "map.h" +#include "thread.h" +#include + +static struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid, + bool create) +{ + struct rb_node **p = >threads.rb_node; + struct rb_node *parent = NULL; + struct thread *th; + + /* +* Font-end cache - PID lookups come in blocks, +* so most of the time we dont have to look up +* the full rbtree: +*/ + if (machine->last_match && machine->last_match->pid == pid) + return machine->last_match; + + while (*p != NULL) { + parent = *p; + th = rb_entry(parent, struct thread, rb_node); + + if (th->pid == pid) { + machine->last_match = th; + return th; + } + + if (pid < th->pid) + p = &(*p)->rb_left; + else + p = &(*p)->rb_right; + } + + if (!create) + return NULL; + + th = thread__new(pid); + if (th != NULL) { + rb_link_node(>rb_node, parent, p); + rb_insert_color(>rb_node, >threads); + machine->last_match = th; + } + + return th; +} + +struct thread *machine__findnew_thread(struct machine *machine, pid_t pid) +{ + return __machine__findnew_thread(machine, pid, true); +} + +struct thread *machine__find_thread(struct machine *machine, pid_t pid) +{ + return __machine__findnew_thread(machine, pid, false); +} diff --git a/tools/perf/util/machine.h b/tools/perf/util/machine.h new file mode 100644 index 000..54df0cd --- /dev/null +++ b/tools/perf/util/machine.h @@ -0,0 +1,11 @@ +#ifndef __PERF_MACHINE_H +#define __PERF_MACHINE_H + +#include + +struct thread; +struct machine; + +struct thread *machine__find_thread(struct machine *machine, pid_t pid); + +#endif /* __PERF_MACHINE_H */ diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c index fb4b7ea..fe3bb1e 100644 --- a/tools/perf/util/thread.c +++ b/tools/perf/util/thread.c @@ -7,7 +7,7 @@ #include "util.h" #include "debug.h" -static struct thread *thread__new(pid_t pid) +struct thread *thread__new(pid_t pid) { struct thread *self = zalloc(sizeof(*self)); @@ -61,45 +61,6 @@ static size_t thread__fprintf(struct thread *self, FILE *fp) map_groups__fprintf(>mg, verbose, fp); } -struct thread *machine__findnew_thread(struct machine *self, pid_t pid) -{ - struct rb_node **p = >threads.rb_node; - struct rb_node *parent = NULL; - struct thread *th; - - /* -* Font-end cache - PID lookups come in blocks, -* so most of the time we dont have to look up -* the full rbtree: -*/ - if (self->last_match && self->last_match->pid == pid) - return self->last_match; - - while (*p != NULL) { - parent = *p; - th =
[tip:perf/core] perf machine: Introduce find_thread method
Commit-ID: 9d2f8e22fc965bcdd5561d000d234fe2d23657ba Gitweb: http://git.kernel.org/tip/9d2f8e22fc965bcdd5561d000d234fe2d23657ba Author: Arnaldo Carvalho de Melo a...@redhat.com AuthorDate: Sat, 6 Oct 2012 15:43:20 -0300 Committer: Arnaldo Carvalho de Melo a...@redhat.com CommitDate: Sat, 6 Oct 2012 16:33:22 -0300 perf machine: Introduce find_thread method There are cases where we want just to find a thread if it exists already, so provide a method for that. While doing that start moving 'machine' methods to a separate file. Cc: David Ahern dsah...@gmail.com Cc: Frederic Weisbecker fweis...@gmail.com Cc: Jiri Olsa jo...@redhat.com Cc: Mike Galbraith efa...@gmx.de Cc: Namhyung Kim namhy...@gmail.com Cc: Paul Mackerras pau...@samba.org Cc: Peter Zijlstra pet...@infradead.org Cc: Stephane Eranian eran...@google.com Link: http://lkml.kernel.org/n/tip-8wpzqs9kfupng6xq8hx6l...@git.kernel.org Signed-off-by: Arnaldo Carvalho de Melo a...@redhat.com --- tools/perf/Makefile |2 + tools/perf/util/machine.c | 57 + tools/perf/util/machine.h | 11 tools/perf/util/thread.c | 41 +--- tools/perf/util/thread.h |2 + 5 files changed, 73 insertions(+), 40 deletions(-) diff --git a/tools/perf/Makefile b/tools/perf/Makefile index f9126f8..d80a333 100644 --- a/tools/perf/Makefile +++ b/tools/perf/Makefile @@ -300,6 +300,7 @@ LIB_H += util/evlist.h LIB_H += util/exec_cmd.h LIB_H += util/types.h LIB_H += util/levenshtein.h +LIB_H += util/machine.h LIB_H += util/map.h LIB_H += util/parse-options.h LIB_H += util/parse-events.h @@ -383,6 +384,7 @@ LIB_OBJS += $(OUTPUT)util/header.o LIB_OBJS += $(OUTPUT)util/callchain.o LIB_OBJS += $(OUTPUT)util/values.o LIB_OBJS += $(OUTPUT)util/debug.o +LIB_OBJS += $(OUTPUT)util/machine.o LIB_OBJS += $(OUTPUT)util/map.o LIB_OBJS += $(OUTPUT)util/pstack.o LIB_OBJS += $(OUTPUT)util/session.o diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c new file mode 100644 index 000..9d36d7e --- /dev/null +++ b/tools/perf/util/machine.c @@ -0,0 +1,57 @@ +#include machine.h +#include map.h +#include thread.h +#include stdbool.h + +static struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid, + bool create) +{ + struct rb_node **p = machine-threads.rb_node; + struct rb_node *parent = NULL; + struct thread *th; + + /* +* Font-end cache - PID lookups come in blocks, +* so most of the time we dont have to look up +* the full rbtree: +*/ + if (machine-last_match machine-last_match-pid == pid) + return machine-last_match; + + while (*p != NULL) { + parent = *p; + th = rb_entry(parent, struct thread, rb_node); + + if (th-pid == pid) { + machine-last_match = th; + return th; + } + + if (pid th-pid) + p = (*p)-rb_left; + else + p = (*p)-rb_right; + } + + if (!create) + return NULL; + + th = thread__new(pid); + if (th != NULL) { + rb_link_node(th-rb_node, parent, p); + rb_insert_color(th-rb_node, machine-threads); + machine-last_match = th; + } + + return th; +} + +struct thread *machine__findnew_thread(struct machine *machine, pid_t pid) +{ + return __machine__findnew_thread(machine, pid, true); +} + +struct thread *machine__find_thread(struct machine *machine, pid_t pid) +{ + return __machine__findnew_thread(machine, pid, false); +} diff --git a/tools/perf/util/machine.h b/tools/perf/util/machine.h new file mode 100644 index 000..54df0cd --- /dev/null +++ b/tools/perf/util/machine.h @@ -0,0 +1,11 @@ +#ifndef __PERF_MACHINE_H +#define __PERF_MACHINE_H + +#include sys/types.h + +struct thread; +struct machine; + +struct thread *machine__find_thread(struct machine *machine, pid_t pid); + +#endif /* __PERF_MACHINE_H */ diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c index fb4b7ea..fe3bb1e 100644 --- a/tools/perf/util/thread.c +++ b/tools/perf/util/thread.c @@ -7,7 +7,7 @@ #include util.h #include debug.h -static struct thread *thread__new(pid_t pid) +struct thread *thread__new(pid_t pid) { struct thread *self = zalloc(sizeof(*self)); @@ -61,45 +61,6 @@ static size_t thread__fprintf(struct thread *self, FILE *fp) map_groups__fprintf(self-mg, verbose, fp); } -struct thread *machine__findnew_thread(struct machine *self, pid_t pid) -{ - struct rb_node **p = self-threads.rb_node; - struct rb_node *parent = NULL; - struct thread *th; - - /* -* Font-end cache - PID lookups come in blocks, -* so most of the time we dont have to look up -* the full rbtree: -