aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--tools/perf/Makefile2
-rw-r--r--tools/perf/util/machine.c57
-rw-r--r--tools/perf/util/machine.h11
-rw-r--r--tools/perf/util/thread.c41
-rw-r--r--tools/perf/util/thread.h2
5 files changed, 73 insertions, 40 deletions
diff --git a/tools/perf/Makefile b/tools/perf/Makefile
index f9126f89efef..d80a33324785 100644
--- a/tools/perf/Makefile
+++ b/tools/perf/Makefile
@@ -300,6 +300,7 @@ LIB_H += util/evlist.h
300LIB_H += util/exec_cmd.h 300LIB_H += util/exec_cmd.h
301LIB_H += util/types.h 301LIB_H += util/types.h
302LIB_H += util/levenshtein.h 302LIB_H += util/levenshtein.h
303LIB_H += util/machine.h
303LIB_H += util/map.h 304LIB_H += util/map.h
304LIB_H += util/parse-options.h 305LIB_H += util/parse-options.h
305LIB_H += util/parse-events.h 306LIB_H += util/parse-events.h
@@ -383,6 +384,7 @@ LIB_OBJS += $(OUTPUT)util/header.o
383LIB_OBJS += $(OUTPUT)util/callchain.o 384LIB_OBJS += $(OUTPUT)util/callchain.o
384LIB_OBJS += $(OUTPUT)util/values.o 385LIB_OBJS += $(OUTPUT)util/values.o
385LIB_OBJS += $(OUTPUT)util/debug.o 386LIB_OBJS += $(OUTPUT)util/debug.o
387LIB_OBJS += $(OUTPUT)util/machine.o
386LIB_OBJS += $(OUTPUT)util/map.o 388LIB_OBJS += $(OUTPUT)util/map.o
387LIB_OBJS += $(OUTPUT)util/pstack.o 389LIB_OBJS += $(OUTPUT)util/pstack.o
388LIB_OBJS += $(OUTPUT)util/session.o 390LIB_OBJS += $(OUTPUT)util/session.o
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
6static 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
49struct thread *machine__findnew_thread(struct machine *machine, pid_t pid)
50{
51 return __machine__findnew_thread(machine, pid, true);
52}
53
54struct 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
6struct thread;
7struct machine;
8
9struct 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
10static struct thread *thread__new(pid_t pid) 10struct 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
64struct 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
103void thread__insert_map(struct thread *self, struct map *map) 64void 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
8struct thread { 9struct thread {
@@ -22,6 +23,7 @@ struct thread {
22 23
23struct machine; 24struct machine;
24 25
26struct thread *thread__new(pid_t pid);
25void thread__delete(struct thread *self); 27void thread__delete(struct thread *self);
26 28
27int thread__set_comm(struct thread *self, const char *comm); 29int thread__set_comm(struct thread *self, const char *comm);