]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - tools/perf/util/thread.c
perf_events, x86: Split PMU definitions into separate files
[mirror_ubuntu-bionic-kernel.git] / tools / perf / util / thread.c
CommitLineData
6baa0a5a
FW
1#include "../perf.h"
2#include <stdlib.h>
3#include <stdio.h>
4#include <string.h>
b3165f41 5#include "session.h"
6baa0a5a
FW
6#include "thread.h"
7#include "util.h"
6e086437 8#include "debug.h"
6baa0a5a 9
9958e1f0 10void map_groups__init(struct map_groups *self)
95011c60
ACM
11{
12 int i;
95011c60
ACM
13 for (i = 0; i < MAP__NR_TYPES; ++i) {
14 self->maps[i] = RB_ROOT;
15 INIT_LIST_HEAD(&self->removed_maps[i]);
16 }
17}
18
97ea1a7f 19static struct thread *thread__new(pid_t pid)
6baa0a5a 20{
36479484 21 struct thread *self = zalloc(sizeof(*self));
6baa0a5a
FW
22
23 if (self != NULL) {
9958e1f0
ACM
24 map_groups__init(&self->mg);
25 self->pid = pid;
97ea1a7f
FW
26 self->comm = malloc(32);
27 if (self->comm)
28 snprintf(self->comm, 32, ":%d", self->pid);
6baa0a5a
FW
29 }
30
31 return self;
32}
33
34int thread__set_comm(struct thread *self, const char *comm)
35{
36 if (self->comm)
37 free(self->comm);
38 self->comm = strdup(comm);
faa5c5c3
ACM
39 if (self->comm == NULL)
40 return -ENOMEM;
41 self->comm_set = true;
42 return 0;
6baa0a5a
FW
43}
44
a4fb581b
FW
45int thread__comm_len(struct thread *self)
46{
47 if (!self->comm_len) {
48 if (!self->comm)
49 return 0;
50 self->comm_len = strlen(self->comm);
51 }
52
53 return self->comm_len;
54}
55
9958e1f0
ACM
56static size_t __map_groups__fprintf_maps(struct map_groups *self,
57 enum map_type type, FILE *fp)
6baa0a5a 58{
95011c60 59 size_t printed = fprintf(fp, "%s:\n", map_type__name[type]);
1b46cddf 60 struct rb_node *nd;
6baa0a5a 61
95011c60
ACM
62 for (nd = rb_first(&self->maps[type]); nd; nd = rb_next(nd)) {
63 struct map *pos = rb_entry(nd, struct map, rb_node);
64 printed += fprintf(fp, "Map:");
65 printed += map__fprintf(pos, fp);
66 if (verbose > 1) {
67 printed += dso__fprintf(pos->dso, type, fp);
68 printed += fprintf(fp, "--\n");
69 }
1b46cddf 70 }
6baa0a5a 71
95011c60
ACM
72 return printed;
73}
74
9958e1f0 75size_t map_groups__fprintf_maps(struct map_groups *self, FILE *fp)
95011c60
ACM
76{
77 size_t printed = 0, i;
78 for (i = 0; i < MAP__NR_TYPES; ++i)
9958e1f0 79 printed += __map_groups__fprintf_maps(self, i, fp);
95011c60
ACM
80 return printed;
81}
439d473b 82
9958e1f0
ACM
83static size_t __map_groups__fprintf_removed_maps(struct map_groups *self,
84 enum map_type type, FILE *fp)
95011c60
ACM
85{
86 struct map *pos;
87 size_t printed = 0;
88
89 list_for_each_entry(pos, &self->removed_maps[type], node) {
90 printed += fprintf(fp, "Map:");
91 printed += map__fprintf(pos, fp);
92 if (verbose > 1) {
93 printed += dso__fprintf(pos->dso, type, fp);
94 printed += fprintf(fp, "--\n");
95 }
96 }
97 return printed;
98}
439d473b 99
9958e1f0 100static size_t map_groups__fprintf_removed_maps(struct map_groups *self, FILE *fp)
95011c60
ACM
101{
102 size_t printed = 0, i;
103 for (i = 0; i < MAP__NR_TYPES; ++i)
9958e1f0 104 printed += __map_groups__fprintf_removed_maps(self, i, fp);
95011c60
ACM
105 return printed;
106}
107
9958e1f0 108static size_t map_groups__fprintf(struct map_groups *self, FILE *fp)
95011c60 109{
9958e1f0 110 size_t printed = map_groups__fprintf_maps(self, fp);
95011c60 111 printed += fprintf(fp, "Removed maps:\n");
9958e1f0
ACM
112 return printed + map_groups__fprintf_removed_maps(self, fp);
113}
114
115static size_t thread__fprintf(struct thread *self, FILE *fp)
116{
117 return fprintf(fp, "Thread %d %s\n", self->pid, self->comm) +
118 map_groups__fprintf(&self->mg, fp);
6baa0a5a
FW
119}
120
b3165f41 121struct thread *perf_session__findnew(struct perf_session *self, pid_t pid)
6baa0a5a 122{
b3165f41 123 struct rb_node **p = &self->threads.rb_node;
6baa0a5a
FW
124 struct rb_node *parent = NULL;
125 struct thread *th;
126
127 /*
128 * Font-end cache - PID lookups come in blocks,
129 * so most of the time we dont have to look up
130 * the full rbtree:
131 */
b3165f41
ACM
132 if (self->last_match && self->last_match->pid == pid)
133 return self->last_match;
6baa0a5a
FW
134
135 while (*p != NULL) {
136 parent = *p;
137 th = rb_entry(parent, struct thread, rb_node);
138
139 if (th->pid == pid) {
b3165f41 140 self->last_match = th;
6baa0a5a
FW
141 return th;
142 }
143
144 if (pid < th->pid)
145 p = &(*p)->rb_left;
146 else
147 p = &(*p)->rb_right;
148 }
149
97ea1a7f 150 th = thread__new(pid);
6baa0a5a
FW
151 if (th != NULL) {
152 rb_link_node(&th->rb_node, parent, p);
b3165f41
ACM
153 rb_insert_color(&th->rb_node, &self->threads);
154 self->last_match = th;
6baa0a5a
FW
155 }
156
157 return th;
158}
159
9958e1f0
ACM
160static void map_groups__remove_overlappings(struct map_groups *self,
161 struct map *map)
6baa0a5a 162{
95011c60
ACM
163 struct rb_root *root = &self->maps[map->type];
164 struct rb_node *next = rb_first(root);
1b46cddf
ACM
165
166 while (next) {
167 struct map *pos = rb_entry(next, struct map, rb_node);
168 next = rb_next(&pos->rb_node);
169
170 if (!map__overlap(pos, map))
171 continue;
172
173 if (verbose >= 2) {
6beba7ad
ACM
174 fputs("overlapping maps:\n", stderr);
175 map__fprintf(map, stderr);
176 map__fprintf(pos, stderr);
1b46cddf
ACM
177 }
178
95011c60 179 rb_erase(&pos->rb_node, root);
439d473b
ACM
180 /*
181 * We may have references to this map, for instance in some
182 * hist_entry instances, so just move them to a separate
183 * list.
184 */
95011c60 185 list_add_tail(&pos->node, &self->removed_maps[map->type]);
6baa0a5a 186 }
1b46cddf
ACM
187}
188
189void maps__insert(struct rb_root *maps, struct map *map)
190{
191 struct rb_node **p = &maps->rb_node;
192 struct rb_node *parent = NULL;
193 const u64 ip = map->start;
194 struct map *m;
195
196 while (*p != NULL) {
197 parent = *p;
198 m = rb_entry(parent, struct map, rb_node);
199 if (ip < m->start)
200 p = &(*p)->rb_left;
201 else
202 p = &(*p)->rb_right;
203 }
204
205 rb_link_node(&map->rb_node, parent, p);
206 rb_insert_color(&map->rb_node, maps);
207}
208
209struct map *maps__find(struct rb_root *maps, u64 ip)
210{
211 struct rb_node **p = &maps->rb_node;
212 struct rb_node *parent = NULL;
213 struct map *m;
214
215 while (*p != NULL) {
216 parent = *p;
217 m = rb_entry(parent, struct map, rb_node);
218 if (ip < m->start)
219 p = &(*p)->rb_left;
220 else if (ip > m->end)
221 p = &(*p)->rb_right;
222 else
223 return m;
224 }
225
226 return NULL;
227}
6baa0a5a 228
1b46cddf
ACM
229void thread__insert_map(struct thread *self, struct map *map)
230{
9958e1f0
ACM
231 map_groups__remove_overlappings(&self->mg, map);
232 map_groups__insert(&self->mg, map);
6baa0a5a
FW
233}
234
9958e1f0
ACM
235/*
236 * XXX This should not really _copy_ te maps, but refcount them.
237 */
238static int map_groups__clone(struct map_groups *self,
239 struct map_groups *parent, enum map_type type)
6baa0a5a 240{
1b46cddf 241 struct rb_node *nd;
95011c60
ACM
242 for (nd = rb_first(&parent->maps[type]); nd; nd = rb_next(nd)) {
243 struct map *map = rb_entry(nd, struct map, rb_node);
244 struct map *new = map__clone(map);
245 if (new == NULL)
246 return -ENOMEM;
9958e1f0 247 map_groups__insert(self, new);
95011c60
ACM
248 }
249 return 0;
250}
251
252int thread__fork(struct thread *self, struct thread *parent)
253{
254 int i;
6baa0a5a 255
faa5c5c3
ACM
256 if (parent->comm_set) {
257 if (self->comm)
258 free(self->comm);
259 self->comm = strdup(parent->comm);
260 if (!self->comm)
261 return -ENOMEM;
262 self->comm_set = true;
263 }
6baa0a5a 264
95011c60 265 for (i = 0; i < MAP__NR_TYPES; ++i)
9958e1f0 266 if (map_groups__clone(&self->mg, &parent->mg, i) < 0)
6baa0a5a 267 return -ENOMEM;
6baa0a5a
FW
268 return 0;
269}
270
b3165f41 271size_t perf_session__fprintf(struct perf_session *self, FILE *fp)
6baa0a5a
FW
272{
273 size_t ret = 0;
274 struct rb_node *nd;
275
b3165f41 276 for (nd = rb_first(&self->threads); nd; nd = rb_next(nd)) {
6baa0a5a
FW
277 struct thread *pos = rb_entry(nd, struct thread, rb_node);
278
279 ret += thread__fprintf(pos, fp);
280 }
281
282 return ret;
283}
1ed091c4 284
9958e1f0
ACM
285struct symbol *map_groups__find_symbol(struct map_groups *self,
286 enum map_type type, u64 addr,
287 symbol_filter_t filter)
1ed091c4 288{
9958e1f0 289 struct map *map = map_groups__find(self, type, addr);
1ed091c4
ACM
290
291 if (map != NULL)
9de89fe7 292 return map__find_symbol(map, map->map_ip(map, addr), filter);
1ed091c4
ACM
293
294 return NULL;
295}