perf callchain: Create an address space per thread
[linux-block.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"
1902efe7 9#include "comm.h"
66f066d8 10#include "unwind.h"
6baa0a5a 11
cddcef60
JO
12int thread__init_map_groups(struct thread *thread, struct machine *machine)
13{
14 struct thread *leader;
15 pid_t pid = thread->pid_;
16
1fcb8768 17 if (pid == thread->tid || pid == -1) {
cddcef60
JO
18 thread->mg = map_groups__new();
19 } else {
20 leader = machine__findnew_thread(machine, pid, pid);
21 if (leader)
22 thread->mg = map_groups__get(leader->mg);
23 }
24
25 return thread->mg ? 0 : -1;
26}
27
99d725fc 28struct thread *thread__new(pid_t pid, pid_t tid)
6baa0a5a 29{
1902efe7
FW
30 char *comm_str;
31 struct comm *comm;
c824c433 32 struct thread *thread = zalloc(sizeof(*thread));
6baa0a5a 33
c824c433 34 if (thread != NULL) {
c824c433
ACM
35 thread->pid_ = pid;
36 thread->tid = tid;
37 thread->ppid = -1;
bf49c35f 38 thread->cpu = -1;
1902efe7
FW
39 INIT_LIST_HEAD(&thread->comm_list);
40
66f066d8
NK
41 if (unwind__prepare_access(thread) < 0)
42 goto err_thread;
43
1902efe7
FW
44 comm_str = malloc(32);
45 if (!comm_str)
46 goto err_thread;
47
48 snprintf(comm_str, 32, ":%d", tid);
65de51f9 49 comm = comm__new(comm_str, 0, false);
1902efe7
FW
50 free(comm_str);
51 if (!comm)
52 goto err_thread;
53
54 list_add(&comm->list, &thread->comm_list);
66f066d8 55
6baa0a5a
FW
56 }
57
c824c433 58 return thread;
1902efe7
FW
59
60err_thread:
61 free(thread);
62 return NULL;
6baa0a5a
FW
63}
64
c824c433 65void thread__delete(struct thread *thread)
591765fd 66{
1902efe7
FW
67 struct comm *comm, *tmp;
68
9608b84e
AH
69 if (thread->mg) {
70 map_groups__put(thread->mg);
71 thread->mg = NULL;
72 }
1902efe7
FW
73 list_for_each_entry_safe(comm, tmp, &thread->comm_list, list) {
74 list_del(&comm->list);
75 comm__free(comm);
76 }
66f066d8 77 unwind__finish_access(thread);
1902efe7 78
c824c433 79 free(thread);
591765fd
ACM
80}
81
4dfced35 82struct comm *thread__comm(const struct thread *thread)
6baa0a5a 83{
1902efe7
FW
84 if (list_empty(&thread->comm_list))
85 return NULL;
4385d580 86
1902efe7
FW
87 return list_first_entry(&thread->comm_list, struct comm, list);
88}
89
65de51f9
AH
90struct comm *thread__exec_comm(const struct thread *thread)
91{
92 struct comm *comm, *last = NULL;
93
94 list_for_each_entry(comm, &thread->comm_list, list) {
95 if (comm->exec)
96 return comm;
97 last = comm;
98 }
99
100 return last;
101}
102
1902efe7 103/* CHECKME: time should always be 0 if event aren't ordered */
65de51f9
AH
104int __thread__set_comm(struct thread *thread, const char *str, u64 timestamp,
105 bool exec)
1902efe7
FW
106{
107 struct comm *new, *curr = thread__comm(thread);
3178f58b 108 int err;
1902efe7
FW
109
110 /* Override latest entry if it had no specific time coverage */
65de51f9
AH
111 if (!curr->start && !curr->exec) {
112 err = comm__override(curr, str, timestamp, exec);
3178f58b
FW
113 if (err)
114 return err;
a5285ad9 115 } else {
65de51f9 116 new = comm__new(str, timestamp, exec);
a5285ad9
FW
117 if (!new)
118 return -ENOMEM;
119 list_add(&new->list, &thread->comm_list);
4385d580 120 }
1902efe7 121
1902efe7
FW
122 thread->comm_set = true;
123
124 return 0;
6baa0a5a
FW
125}
126
b9c5143a
FW
127const char *thread__comm_str(const struct thread *thread)
128{
1902efe7
FW
129 const struct comm *comm = thread__comm(thread);
130
131 if (!comm)
132 return NULL;
133
134 return comm__str(comm);
b9c5143a
FW
135}
136
1902efe7 137/* CHECKME: it should probably better return the max comm len from its comm list */
c824c433 138int thread__comm_len(struct thread *thread)
a4fb581b 139{
c824c433 140 if (!thread->comm_len) {
1902efe7
FW
141 const char *comm = thread__comm_str(thread);
142 if (!comm)
a4fb581b 143 return 0;
1902efe7 144 thread->comm_len = strlen(comm);
a4fb581b
FW
145 }
146
c824c433 147 return thread->comm_len;
a4fb581b
FW
148}
149
3f067dca 150size_t thread__fprintf(struct thread *thread, FILE *fp)
9958e1f0 151{
b9c5143a 152 return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread)) +
acebd408 153 map_groups__fprintf(thread->mg, fp);
6baa0a5a
FW
154}
155
c824c433 156void thread__insert_map(struct thread *thread, struct map *map)
1b46cddf 157{
acebd408 158 map_groups__fixup_overlappings(thread->mg, map, stderr);
93d5731d 159 map_groups__insert(thread->mg, map);
6baa0a5a
FW
160}
161
cddcef60
JO
162static int thread__clone_map_groups(struct thread *thread,
163 struct thread *parent)
164{
165 int i;
166
167 /* This is new thread, we share map groups for process. */
168 if (thread->pid_ == parent->pid_)
169 return 0;
170
171 /* But this one is new process, copy maps. */
172 for (i = 0; i < MAP__NR_TYPES; ++i)
173 if (map_groups__clone(thread->mg, parent->mg, i) < 0)
174 return -ENOMEM;
175
176 return 0;
177}
178
1902efe7 179int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp)
95011c60 180{
cddcef60 181 int err;
6baa0a5a 182
faa5c5c3 183 if (parent->comm_set) {
1902efe7
FW
184 const char *comm = thread__comm_str(parent);
185 if (!comm)
faa5c5c3 186 return -ENOMEM;
1902efe7 187 err = thread__set_comm(thread, comm, timestamp);
8d00be81 188 if (err)
1902efe7 189 return err;
c824c433 190 thread->comm_set = true;
faa5c5c3 191 }
6baa0a5a 192
c824c433 193 thread->ppid = parent->tid;
cddcef60 194 return thread__clone_map_groups(thread, parent);
6baa0a5a 195}
52a3cb8c
ACM
196
197void thread__find_cpumode_addr_location(struct thread *thread,
198 struct machine *machine,
199 enum map_type type, u64 addr,
200 struct addr_location *al)
201{
202 size_t i;
203 const u8 const cpumodes[] = {
204 PERF_RECORD_MISC_USER,
205 PERF_RECORD_MISC_KERNEL,
206 PERF_RECORD_MISC_GUEST_USER,
207 PERF_RECORD_MISC_GUEST_KERNEL
208 };
209
210 for (i = 0; i < ARRAY_SIZE(cpumodes); i++) {
211 thread__find_addr_location(thread, machine, cpumodes[i], type,
212 addr, al);
213 if (al->map)
214 break;
215 }
216}