blob: f5af87f6666303df886d4c70fd2d565c1d0436e9 [file] [log] [blame]
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +02001#include "../perf.h"
2#include <stdlib.h>
3#include <stdio.h>
4#include <string.h>
Arnaldo Carvalho de Melob3165f42009-12-13 19:50:28 -02005#include "session.h"
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +02006#include "thread.h"
Adrian Hunter00447cc2014-10-30 16:09:42 +02007#include "thread-stack.h"
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +02008#include "util.h"
Frederic Weisbecker6e086432009-08-18 17:04:03 +02009#include "debug.h"
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020010#include "comm.h"
Namhyung Kim66f066d82014-10-06 09:46:00 +090011#include "unwind.h"
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020012
Arnaldo Carvalho de Melo2f3027a2016-04-26 12:32:50 -030013#include <api/fs/fs.h>
14
Jiri Olsacddcef62014-04-09 20:54:29 +020015int thread__init_map_groups(struct thread *thread, struct machine *machine)
16{
Jiri Olsacddcef62014-04-09 20:54:29 +020017 pid_t pid = thread->pid_;
18
Adrian Hunter1fcb8762014-07-14 13:02:25 +030019 if (pid == thread->tid || pid == -1) {
Arnaldo Carvalho de Melo11246c72014-10-21 17:29:02 -030020 thread->mg = map_groups__new(machine);
Jiri Olsacddcef62014-04-09 20:54:29 +020021 } else {
Arnaldo Carvalho de Melo18ef15c2016-10-03 11:07:24 -030022 struct thread *leader = __machine__findnew_thread(machine, pid, pid);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -030023 if (leader) {
Jiri Olsacddcef62014-04-09 20:54:29 +020024 thread->mg = map_groups__get(leader->mg);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -030025 thread__put(leader);
26 }
Jiri Olsacddcef62014-04-09 20:54:29 +020027 }
28
29 return thread->mg ? 0 : -1;
30}
31
Adrian Hunter99d725f2013-08-26 16:00:19 +030032struct thread *thread__new(pid_t pid, pid_t tid)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020033{
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020034 char *comm_str;
35 struct comm *comm;
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030036 struct thread *thread = zalloc(sizeof(*thread));
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020037
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030038 if (thread != NULL) {
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030039 thread->pid_ = pid;
40 thread->tid = tid;
41 thread->ppid = -1;
Adrian Hunterbf49c352014-07-22 16:17:24 +030042 thread->cpu = -1;
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020043 INIT_LIST_HEAD(&thread->comm_list);
44
45 comm_str = malloc(32);
46 if (!comm_str)
47 goto err_thread;
48
49 snprintf(comm_str, 32, ":%d", tid);
Adrian Hunter65de51f2014-07-31 09:00:44 +030050 comm = comm__new(comm_str, 0, false);
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020051 free(comm_str);
52 if (!comm)
53 goto err_thread;
54
55 list_add(&comm->list, &thread->comm_list);
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -030056 atomic_set(&thread->refcnt, 1);
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030057 RB_CLEAR_NODE(&thread->rb_node);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020058 }
59
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030060 return thread;
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020061
62err_thread:
63 free(thread);
64 return NULL;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020065}
66
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030067void thread__delete(struct thread *thread)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -030068{
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020069 struct comm *comm, *tmp;
70
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030071 BUG_ON(!RB_EMPTY_NODE(&thread->rb_node));
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030072
Adrian Hunter00447cc2014-10-30 16:09:42 +020073 thread_stack__free(thread);
74
Adrian Hunter9608b842014-07-16 10:19:43 +030075 if (thread->mg) {
76 map_groups__put(thread->mg);
77 thread->mg = NULL;
78 }
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020079 list_for_each_entry_safe(comm, tmp, &thread->comm_list, list) {
80 list_del(&comm->list);
81 comm__free(comm);
82 }
Namhyung Kim66f066d82014-10-06 09:46:00 +090083 unwind__finish_access(thread);
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020084
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030085 free(thread);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -030086}
87
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -030088struct thread *thread__get(struct thread *thread)
89{
Arnaldo Carvalho de Melob91fc392015-04-06 20:43:22 -030090 if (thread)
91 atomic_inc(&thread->refcnt);
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -030092 return thread;
93}
94
95void thread__put(struct thread *thread)
96{
Arnaldo Carvalho de Meloe1ed3a52015-04-07 11:59:50 -030097 if (thread && atomic_dec_and_test(&thread->refcnt)) {
Arnaldo Carvalho de Meloabd82862015-12-11 19:11:23 -030098 /*
99 * Remove it from the dead_threads list, as last reference
100 * is gone.
101 */
Arnaldo Carvalho de Melof3b623b2015-03-02 22:21:35 -0300102 list_del_init(&thread->node);
103 thread__delete(thread);
104 }
105}
106
Namhyung Kim4dfced32013-09-13 16:28:57 +0900107struct comm *thread__comm(const struct thread *thread)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200108{
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200109 if (list_empty(&thread->comm_list))
110 return NULL;
David S. Miller4385d582010-02-26 12:08:34 -0300111
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200112 return list_first_entry(&thread->comm_list, struct comm, list);
113}
114
Adrian Hunter65de51f2014-07-31 09:00:44 +0300115struct comm *thread__exec_comm(const struct thread *thread)
116{
117 struct comm *comm, *last = NULL;
118
119 list_for_each_entry(comm, &thread->comm_list, list) {
120 if (comm->exec)
121 return comm;
122 last = comm;
123 }
124
125 return last;
126}
127
Adrian Hunter65de51f2014-07-31 09:00:44 +0300128int __thread__set_comm(struct thread *thread, const char *str, u64 timestamp,
129 bool exec)
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200130{
131 struct comm *new, *curr = thread__comm(thread);
132
Adrian Huntera8480802014-11-11 16:16:41 +0200133 /* Override the default :tid entry */
134 if (!thread->comm_set) {
Arnaldo Carvalho de Melo18ef15c2016-10-03 11:07:24 -0300135 int err = comm__override(curr, str, timestamp, exec);
Frederic Weisbecker3178f582014-01-14 16:37:14 +0100136 if (err)
137 return err;
Frederic Weisbeckera5285ad2013-11-16 02:02:09 +0100138 } else {
Adrian Hunter65de51f2014-07-31 09:00:44 +0300139 new = comm__new(str, timestamp, exec);
Frederic Weisbeckera5285ad2013-11-16 02:02:09 +0100140 if (!new)
141 return -ENOMEM;
142 list_add(&new->list, &thread->comm_list);
Namhyung Kim380b5142014-10-06 09:46:01 +0900143
144 if (exec)
145 unwind__flush_access(thread);
David S. Miller4385d582010-02-26 12:08:34 -0300146 }
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200147
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200148 thread->comm_set = true;
149
150 return 0;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200151}
152
Arnaldo Carvalho de Melo2f3027a2016-04-26 12:32:50 -0300153int thread__set_comm_from_proc(struct thread *thread)
154{
155 char path[64];
156 char *comm = NULL;
157 size_t sz;
158 int err = -1;
159
160 if (!(snprintf(path, sizeof(path), "%d/task/%d/comm",
161 thread->pid_, thread->tid) >= (int)sizeof(path)) &&
162 procfs__read_str(path, &comm, &sz) == 0) {
163 comm[sz - 1] = '\0';
164 err = thread__set_comm(thread, comm, 0);
165 }
166
167 return err;
168}
169
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200170const char *thread__comm_str(const struct thread *thread)
171{
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200172 const struct comm *comm = thread__comm(thread);
173
174 if (!comm)
175 return NULL;
176
177 return comm__str(comm);
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200178}
179
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200180/* CHECKME: it should probably better return the max comm len from its comm list */
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300181int thread__comm_len(struct thread *thread)
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200182{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300183 if (!thread->comm_len) {
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200184 const char *comm = thread__comm_str(thread);
185 if (!comm)
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200186 return 0;
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200187 thread->comm_len = strlen(comm);
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200188 }
189
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300190 return thread->comm_len;
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200191}
192
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300193size_t thread__fprintf(struct thread *thread, FILE *fp)
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200194{
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200195 return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread)) +
Jiri Olsaacebd402014-07-14 23:46:47 +0200196 map_groups__fprintf(thread->mg, fp);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200197}
198
He Kuang8132a2a2016-06-03 03:33:13 +0000199int thread__insert_map(struct thread *thread, struct map *map)
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300200{
He Kuang8132a2a2016-06-03 03:33:13 +0000201 int ret;
202
Jiri Olsaa2873322016-07-04 14:16:22 +0200203 ret = unwind__prepare_access(thread, map, NULL);
He Kuang8132a2a2016-06-03 03:33:13 +0000204 if (ret)
205 return ret;
206
Jiri Olsaacebd402014-07-14 23:46:47 +0200207 map_groups__fixup_overlappings(thread->mg, map, stderr);
Arnaldo Carvalho de Melo93d57312014-03-21 17:57:01 -0300208 map_groups__insert(thread->mg, map);
He Kuang8132a2a2016-06-03 03:33:13 +0000209
210 return 0;
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200211}
212
Jiri Olsa6c502582016-07-04 14:16:23 +0200213static int __thread__prepare_access(struct thread *thread)
214{
215 bool initialized = false;
216 int i, err = 0;
217
218 for (i = 0; i < MAP__NR_TYPES; ++i) {
219 struct maps *maps = &thread->mg->maps[i];
220 struct map *map;
221
222 pthread_rwlock_rdlock(&maps->lock);
223
224 for (map = maps__first(maps); map; map = map__next(map)) {
225 err = unwind__prepare_access(thread, map, &initialized);
226 if (err || initialized)
227 break;
228 }
229
230 pthread_rwlock_unlock(&maps->lock);
231 }
232
233 return err;
234}
235
236static int thread__prepare_access(struct thread *thread)
237{
238 int err = 0;
239
240 if (symbol_conf.use_callchain)
241 err = __thread__prepare_access(thread);
242
243 return err;
244}
245
Jiri Olsacddcef62014-04-09 20:54:29 +0200246static int thread__clone_map_groups(struct thread *thread,
247 struct thread *parent)
248{
249 int i;
250
251 /* This is new thread, we share map groups for process. */
252 if (thread->pid_ == parent->pid_)
Jiri Olsa6c502582016-07-04 14:16:23 +0200253 return thread__prepare_access(thread);
Jiri Olsacddcef62014-04-09 20:54:29 +0200254
Adrian Hunter0d7e7ac2015-08-19 17:29:19 +0300255 if (thread->mg == parent->mg) {
256 pr_debug("broken map groups on thread %d/%d parent %d/%d\n",
257 thread->pid_, thread->tid, parent->pid_, parent->tid);
258 return 0;
259 }
260
Jiri Olsacddcef62014-04-09 20:54:29 +0200261 /* But this one is new process, copy maps. */
262 for (i = 0; i < MAP__NR_TYPES; ++i)
Jiri Olsa6c502582016-07-04 14:16:23 +0200263 if (map_groups__clone(thread, parent->mg, i) < 0)
Jiri Olsacddcef62014-04-09 20:54:29 +0200264 return -ENOMEM;
265
266 return 0;
267}
268
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200269int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200270{
Arnaldo Carvalho de Melofaa5c5c2010-02-19 23:02:07 -0200271 if (parent->comm_set) {
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200272 const char *comm = thread__comm_str(parent);
Arnaldo Carvalho de Melo18ef15c2016-10-03 11:07:24 -0300273 int err;
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200274 if (!comm)
Arnaldo Carvalho de Melofaa5c5c2010-02-19 23:02:07 -0200275 return -ENOMEM;
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200276 err = thread__set_comm(thread, comm, timestamp);
David Ahern8d00be82013-12-10 21:35:38 -0700277 if (err)
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200278 return err;
Arnaldo Carvalho de Melofaa5c5c2010-02-19 23:02:07 -0200279 }
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200280
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300281 thread->ppid = parent->tid;
Jiri Olsacddcef62014-04-09 20:54:29 +0200282 return thread__clone_map_groups(thread, parent);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200283}
Arnaldo Carvalho de Melo52a3cb82014-03-11 16:16:49 -0300284
285void thread__find_cpumode_addr_location(struct thread *thread,
Arnaldo Carvalho de Melo52a3cb82014-03-11 16:16:49 -0300286 enum map_type type, u64 addr,
287 struct addr_location *al)
288{
289 size_t i;
Eric Engestrom3b556bc2016-04-25 10:47:54 +0100290 const u8 cpumodes[] = {
Arnaldo Carvalho de Melo52a3cb82014-03-11 16:16:49 -0300291 PERF_RECORD_MISC_USER,
292 PERF_RECORD_MISC_KERNEL,
293 PERF_RECORD_MISC_GUEST_USER,
294 PERF_RECORD_MISC_GUEST_KERNEL
295 };
296
297 for (i = 0; i < ARRAY_SIZE(cpumodes); i++) {
Arnaldo Carvalho de Melobb871a92014-10-23 12:50:25 -0300298 thread__find_addr_location(thread, cpumodes[i], type, addr, al);
Arnaldo Carvalho de Melo52a3cb82014-03-11 16:16:49 -0300299 if (al->map)
300 break;
301 }
302}
Andi Kleen480ca352016-05-23 17:52:24 -0700303
304struct thread *thread__main_thread(struct machine *machine, struct thread *thread)
305{
306 if (thread->pid_ == thread->tid)
307 return thread__get(thread);
308
309 if (thread->pid_ == -1)
310 return NULL;
311
312 return machine__find_thread(machine, thread->pid_, thread->pid_);
313}