]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - tools/perf/util/thread.c
Merge branch 'for-2.6.39' of git://git.kernel.org/pub/scm/linux/kernel/git/tj/percpu
[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
97ea1a7f 10static struct thread *thread__new(pid_t pid)
6baa0a5a 11{
36479484 12 struct thread *self = zalloc(sizeof(*self));
6baa0a5a
FW
13
14 if (self != NULL) {
9958e1f0
ACM
15 map_groups__init(&self->mg);
16 self->pid = pid;
97ea1a7f
FW
17 self->comm = malloc(32);
18 if (self->comm)
19 snprintf(self->comm, 32, ":%d", self->pid);
6baa0a5a
FW
20 }
21
22 return self;
23}
24
591765fd
ACM
25void thread__delete(struct thread *self)
26{
27 map_groups__exit(&self->mg);
28 free(self->comm);
29 free(self);
30}
31
6baa0a5a
FW
32int thread__set_comm(struct thread *self, const char *comm)
33{
4385d580
DM
34 int err;
35
6baa0a5a
FW
36 if (self->comm)
37 free(self->comm);
38 self->comm = strdup(comm);
4385d580
DM
39 err = self->comm == NULL ? -ENOMEM : 0;
40 if (!err) {
41 self->comm_set = true;
42 map_groups__flush(&self->mg);
43 }
44 return err;
6baa0a5a
FW
45}
46
a4fb581b
FW
47int thread__comm_len(struct thread *self)
48{
49 if (!self->comm_len) {
50 if (!self->comm)
51 return 0;
52 self->comm_len = strlen(self->comm);
53 }
54
55 return self->comm_len;
56}
57
9958e1f0
ACM
58static size_t thread__fprintf(struct thread *self, FILE *fp)
59{
60 return fprintf(fp, "Thread %d %s\n", self->pid, self->comm) +
c6e718ff 61 map_groups__fprintf(&self->mg, verbose, fp);
6baa0a5a
FW
62}
63
b3165f41 64struct thread *perf_session__findnew(struct perf_session *self, pid_t pid)
6baa0a5a 65{
b3165f41 66 struct rb_node **p = &self->threads.rb_node;
6baa0a5a
FW
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 */
b3165f41
ACM
75 if (self->last_match && self->last_match->pid == pid)
76 return self->last_match;
6baa0a5a
FW
77
78 while (*p != NULL) {
79 parent = *p;
80 th = rb_entry(parent, struct thread, rb_node);
81
82 if (th->pid == pid) {
b3165f41 83 self->last_match = th;
6baa0a5a
FW
84 return th;
85 }
86
87 if (pid < th->pid)
88 p = &(*p)->rb_left;
89 else
90 p = &(*p)->rb_right;
91 }
92
97ea1a7f 93 th = thread__new(pid);
6baa0a5a
FW
94 if (th != NULL) {
95 rb_link_node(&th->rb_node, parent, p);
b3165f41
ACM
96 rb_insert_color(&th->rb_node, &self->threads);
97 self->last_match = th;
6baa0a5a
FW
98 }
99
100 return th;
101}
102
1b46cddf
ACM
103void thread__insert_map(struct thread *self, struct map *map)
104{
c6e718ff 105 map_groups__fixup_overlappings(&self->mg, map, verbose, stderr);
9958e1f0 106 map_groups__insert(&self->mg, map);
6baa0a5a
FW
107}
108
95011c60
ACM
109int thread__fork(struct thread *self, struct thread *parent)
110{
111 int i;
6baa0a5a 112
faa5c5c3
ACM
113 if (parent->comm_set) {
114 if (self->comm)
115 free(self->comm);
116 self->comm = strdup(parent->comm);
117 if (!self->comm)
118 return -ENOMEM;
119 self->comm_set = true;
120 }
6baa0a5a 121
95011c60 122 for (i = 0; i < MAP__NR_TYPES; ++i)
9958e1f0 123 if (map_groups__clone(&self->mg, &parent->mg, i) < 0)
6baa0a5a 124 return -ENOMEM;
6baa0a5a
FW
125 return 0;
126}
127
b3165f41 128size_t perf_session__fprintf(struct perf_session *self, FILE *fp)
6baa0a5a
FW
129{
130 size_t ret = 0;
131 struct rb_node *nd;
132
b3165f41 133 for (nd = rb_first(&self->threads); nd; nd = rb_next(nd)) {
6baa0a5a
FW
134 struct thread *pos = rb_entry(nd, struct thread, rb_node);
135
136 ret += thread__fprintf(pos, fp);
137 }
138
139 return ret;
140}