diff options
author | Arnaldo Carvalho de Melo <acme@redhat.com> | 2012-10-06 14:43:20 -0400 |
---|---|---|
committer | Arnaldo Carvalho de Melo <acme@redhat.com> | 2012-10-06 15:33:22 -0400 |
commit | 9d2f8e22fc965bcdd5561d000d234fe2d23657ba (patch) | |
tree | b0f328b99b87e904c3222ab815e8599e0fe7c29e /tools/perf/util | |
parent | 0c1fe6b2f30fa275d939071293b6e28771283f6d (diff) |
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 <dsahern@gmail.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Mike Galbraith <efault@gmx.de>
Cc: Namhyung Kim <namhyung@gmail.com>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: http://lkml.kernel.org/n/tip-8wpzqs9kfupng6xq8hx6lnxa@git.kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools/perf/util')
-rw-r--r-- | tools/perf/util/machine.c | 57 | ||||
-rw-r--r-- | tools/perf/util/machine.h | 11 | ||||
-rw-r--r-- | tools/perf/util/thread.c | 41 | ||||
-rw-r--r-- | tools/perf/util/thread.h | 2 |
4 files changed, 71 insertions, 40 deletions
diff --git a/tools/perf/util/machine.c b/tools/perf/util/machine.c new file mode 100644 index 000000000000..9d36d7eeda92 --- /dev/null +++ b/tools/perf/util/machine.c | |||
@@ -0,0 +1,57 @@ | |||
1 | #include "machine.h" | ||
2 | #include "map.h" | ||
3 | #include "thread.h" | ||
4 | #include <stdbool.h> | ||
5 | |||
6 | static struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid, | ||
7 | bool create) | ||
8 | { | ||
9 | struct rb_node **p = &machine->threads.rb_node; | ||
10 | struct rb_node *parent = NULL; | ||
11 | struct thread *th; | ||
12 | |||
13 | /* | ||
14 | * Font-end cache - PID lookups come in blocks, | ||
15 | * so most of the time we dont have to look up | ||
16 | * the full rbtree: | ||
17 | */ | ||
18 | if (machine->last_match && machine->last_match->pid == pid) | ||
19 | return machine->last_match; | ||
20 | |||
21 | while (*p != NULL) { | ||
22 | parent = *p; | ||
23 | th = rb_entry(parent, struct thread, rb_node); | ||
24 | |||
25 | if (th->pid == pid) { | ||
26 | machine->last_match = th; | ||
27 | return th; | ||
28 | } | ||
29 | |||
30 | if (pid < th->pid) | ||
31 | p = &(*p)->rb_left; | ||
32 | else | ||
33 | p = &(*p)->rb_right; | ||
34 | } | ||
35 | |||
36 | if (!create) | ||
37 | return NULL; | ||
38 | |||
39 | th = thread__new(pid); | ||
40 | if (th != NULL) { | ||
41 | rb_link_node(&th->rb_node, parent, p); | ||
42 | rb_insert_color(&th->rb_node, &machine->threads); | ||
43 | machine->last_match = th; | ||
44 | } | ||
45 | |||
46 | return th; | ||
47 | } | ||
48 | |||
49 | struct thread *machine__findnew_thread(struct machine *machine, pid_t pid) | ||
50 | { | ||
51 | return __machine__findnew_thread(machine, pid, true); | ||
52 | } | ||
53 | |||
54 | struct thread *machine__find_thread(struct machine *machine, pid_t pid) | ||
55 | { | ||
56 | return __machine__findnew_thread(machine, pid, false); | ||
57 | } | ||
diff --git a/tools/perf/util/machine.h b/tools/perf/util/machine.h new file mode 100644 index 000000000000..54df0cdd3000 --- /dev/null +++ b/tools/perf/util/machine.h | |||
@@ -0,0 +1,11 @@ | |||
1 | #ifndef __PERF_MACHINE_H | ||
2 | #define __PERF_MACHINE_H | ||
3 | |||
4 | #include <sys/types.h> | ||
5 | |||
6 | struct thread; | ||
7 | struct machine; | ||
8 | |||
9 | struct thread *machine__find_thread(struct machine *machine, pid_t pid); | ||
10 | |||
11 | #endif /* __PERF_MACHINE_H */ | ||
diff --git a/tools/perf/util/thread.c b/tools/perf/util/thread.c index fb4b7ea6752f..fe3bb1ec1887 100644 --- a/tools/perf/util/thread.c +++ b/tools/perf/util/thread.c | |||
@@ -7,7 +7,7 @@ | |||
7 | #include "util.h" | 7 | #include "util.h" |
8 | #include "debug.h" | 8 | #include "debug.h" |
9 | 9 | ||
10 | static struct thread *thread__new(pid_t pid) | 10 | struct thread *thread__new(pid_t pid) |
11 | { | 11 | { |
12 | struct thread *self = zalloc(sizeof(*self)); | 12 | struct thread *self = zalloc(sizeof(*self)); |
13 | 13 | ||
@@ -61,45 +61,6 @@ static size_t thread__fprintf(struct thread *self, FILE *fp) | |||
61 | map_groups__fprintf(&self->mg, verbose, fp); | 61 | map_groups__fprintf(&self->mg, verbose, fp); |
62 | } | 62 | } |
63 | 63 | ||
64 | struct thread *machine__findnew_thread(struct machine *self, pid_t pid) | ||
65 | { | ||
66 | struct rb_node **p = &self->threads.rb_node; | ||
67 | struct rb_node *parent = NULL; | ||
68 | struct thread *th; | ||
69 | |||
70 | /* | ||
71 | * Font-end cache - PID lookups come in blocks, | ||
72 | * so most of the time we dont have to look up | ||
73 | * the full rbtree: | ||
74 | */ | ||
75 | if (self->last_match && self->last_match->pid == pid) | ||
76 | return self->last_match; | ||
77 | |||
78 | while (*p != NULL) { | ||
79 | parent = *p; | ||
80 | th = rb_entry(parent, struct thread, rb_node); | ||
81 | |||
82 | if (th->pid == pid) { | ||
83 | self->last_match = th; | ||
84 | return th; | ||
85 | } | ||
86 | |||
87 | if (pid < th->pid) | ||
88 | p = &(*p)->rb_left; | ||
89 | else | ||
90 | p = &(*p)->rb_right; | ||
91 | } | ||
92 | |||
93 | th = thread__new(pid); | ||
94 | if (th != NULL) { | ||
95 | rb_link_node(&th->rb_node, parent, p); | ||
96 | rb_insert_color(&th->rb_node, &self->threads); | ||
97 | self->last_match = th; | ||
98 | } | ||
99 | |||
100 | return th; | ||
101 | } | ||
102 | |||
103 | void thread__insert_map(struct thread *self, struct map *map) | 64 | void thread__insert_map(struct thread *self, struct map *map) |
104 | { | 65 | { |
105 | map_groups__fixup_overlappings(&self->mg, map, verbose, stderr); | 66 | map_groups__fixup_overlappings(&self->mg, map, verbose, stderr); |
diff --git a/tools/perf/util/thread.h b/tools/perf/util/thread.h index f66610b7bacf..f2fa17caa7d5 100644 --- a/tools/perf/util/thread.h +++ b/tools/perf/util/thread.h | |||
@@ -3,6 +3,7 @@ | |||
3 | 3 | ||
4 | #include <linux/rbtree.h> | 4 | #include <linux/rbtree.h> |
5 | #include <unistd.h> | 5 | #include <unistd.h> |
6 | #include <sys/types.h> | ||
6 | #include "symbol.h" | 7 | #include "symbol.h" |
7 | 8 | ||
8 | struct thread { | 9 | struct thread { |
@@ -22,6 +23,7 @@ struct thread { | |||
22 | 23 | ||
23 | struct machine; | 24 | struct machine; |
24 | 25 | ||
26 | struct thread *thread__new(pid_t pid); | ||
25 | void thread__delete(struct thread *self); | 27 | void thread__delete(struct thread *self); |
26 | 28 | ||
27 | int thread__set_comm(struct thread *self, const char *comm); | 29 | int thread__set_comm(struct thread *self, const char *comm); |