blob: 12c7a253a63ceecfa30f93d7f52f4361a5842c44 [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"
7#include "util.h"
Frederic Weisbecker6e086432009-08-18 17:04:03 +02008#include "debug.h"
Frederic Weisbecker1902efe2013-09-11 16:56:44 +02009#include "comm.h"
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020010
Jiri Olsacddcef62014-04-09 20:54:29 +020011int thread__init_map_groups(struct thread *thread, struct machine *machine)
12{
13 struct thread *leader;
14 pid_t pid = thread->pid_;
15
Adrian Hunter1fcb8762014-07-14 13:02:25 +030016 if (pid == thread->tid || pid == -1) {
Jiri Olsacddcef62014-04-09 20:54:29 +020017 thread->mg = map_groups__new();
18 } else {
19 leader = machine__findnew_thread(machine, pid, pid);
20 if (leader)
21 thread->mg = map_groups__get(leader->mg);
22 }
23
24 return thread->mg ? 0 : -1;
25}
26
Adrian Hunter99d725f2013-08-26 16:00:19 +030027struct thread *thread__new(pid_t pid, pid_t tid)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020028{
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020029 char *comm_str;
30 struct comm *comm;
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030031 struct thread *thread = zalloc(sizeof(*thread));
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020032
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030033 if (thread != NULL) {
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030034 thread->pid_ = pid;
35 thread->tid = tid;
36 thread->ppid = -1;
Adrian Hunterbf49c352014-07-22 16:17:24 +030037 thread->cpu = -1;
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020038 INIT_LIST_HEAD(&thread->comm_list);
39
40 comm_str = malloc(32);
41 if (!comm_str)
42 goto err_thread;
43
44 snprintf(comm_str, 32, ":%d", tid);
45 comm = comm__new(comm_str, 0);
46 free(comm_str);
47 if (!comm)
48 goto err_thread;
49
50 list_add(&comm->list, &thread->comm_list);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020051 }
52
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030053 return thread;
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020054
55err_thread:
56 free(thread);
57 return NULL;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020058}
59
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030060void thread__delete(struct thread *thread)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -030061{
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020062 struct comm *comm, *tmp;
63
Adrian Hunter9608b842014-07-16 10:19:43 +030064 if (thread->mg) {
65 map_groups__put(thread->mg);
66 thread->mg = NULL;
67 }
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020068 list_for_each_entry_safe(comm, tmp, &thread->comm_list, list) {
69 list_del(&comm->list);
70 comm__free(comm);
71 }
72
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030073 free(thread);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -030074}
75
Namhyung Kim4dfced32013-09-13 16:28:57 +090076struct comm *thread__comm(const struct thread *thread)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020077{
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020078 if (list_empty(&thread->comm_list))
79 return NULL;
David S. Miller4385d582010-02-26 12:08:34 -030080
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020081 return list_first_entry(&thread->comm_list, struct comm, list);
82}
83
84/* CHECKME: time should always be 0 if event aren't ordered */
85int thread__set_comm(struct thread *thread, const char *str, u64 timestamp)
86{
87 struct comm *new, *curr = thread__comm(thread);
Frederic Weisbecker3178f582014-01-14 16:37:14 +010088 int err;
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020089
90 /* Override latest entry if it had no specific time coverage */
91 if (!curr->start) {
Frederic Weisbecker3178f582014-01-14 16:37:14 +010092 err = comm__override(curr, str, timestamp);
93 if (err)
94 return err;
Frederic Weisbeckera5285ad2013-11-16 02:02:09 +010095 } else {
96 new = comm__new(str, timestamp);
97 if (!new)
98 return -ENOMEM;
99 list_add(&new->list, &thread->comm_list);
David S. Miller4385d582010-02-26 12:08:34 -0300100 }
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200101
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200102 thread->comm_set = true;
103
104 return 0;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200105}
106
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200107const char *thread__comm_str(const struct thread *thread)
108{
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200109 const struct comm *comm = thread__comm(thread);
110
111 if (!comm)
112 return NULL;
113
114 return comm__str(comm);
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200115}
116
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200117/* CHECKME: it should probably better return the max comm len from its comm list */
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300118int thread__comm_len(struct thread *thread)
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200119{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300120 if (!thread->comm_len) {
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200121 const char *comm = thread__comm_str(thread);
122 if (!comm)
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200123 return 0;
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200124 thread->comm_len = strlen(comm);
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200125 }
126
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300127 return thread->comm_len;
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200128}
129
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300130size_t thread__fprintf(struct thread *thread, FILE *fp)
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200131{
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200132 return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread)) +
Jiri Olsaacebd402014-07-14 23:46:47 +0200133 map_groups__fprintf(thread->mg, fp);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200134}
135
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300136void thread__insert_map(struct thread *thread, struct map *map)
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300137{
Jiri Olsaacebd402014-07-14 23:46:47 +0200138 map_groups__fixup_overlappings(thread->mg, map, stderr);
Arnaldo Carvalho de Melo93d57312014-03-21 17:57:01 -0300139 map_groups__insert(thread->mg, map);
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200140}
141
Jiri Olsacddcef62014-04-09 20:54:29 +0200142static int thread__clone_map_groups(struct thread *thread,
143 struct thread *parent)
144{
145 int i;
146
147 /* This is new thread, we share map groups for process. */
148 if (thread->pid_ == parent->pid_)
149 return 0;
150
151 /* But this one is new process, copy maps. */
152 for (i = 0; i < MAP__NR_TYPES; ++i)
153 if (map_groups__clone(thread->mg, parent->mg, i) < 0)
154 return -ENOMEM;
155
156 return 0;
157}
158
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200159int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200160{
Jiri Olsacddcef62014-04-09 20:54:29 +0200161 int err;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200162
Arnaldo Carvalho de Melofaa5c5c2010-02-19 23:02:07 -0200163 if (parent->comm_set) {
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200164 const char *comm = thread__comm_str(parent);
165 if (!comm)
Arnaldo Carvalho de Melofaa5c5c2010-02-19 23:02:07 -0200166 return -ENOMEM;
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200167 err = thread__set_comm(thread, comm, timestamp);
David Ahern8d00be82013-12-10 21:35:38 -0700168 if (err)
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200169 return err;
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300170 thread->comm_set = true;
Arnaldo Carvalho de Melofaa5c5c2010-02-19 23:02:07 -0200171 }
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200172
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300173 thread->ppid = parent->tid;
Jiri Olsacddcef62014-04-09 20:54:29 +0200174 return thread__clone_map_groups(thread, parent);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200175}
Arnaldo Carvalho de Melo52a3cb82014-03-11 16:16:49 -0300176
177void thread__find_cpumode_addr_location(struct thread *thread,
178 struct machine *machine,
179 enum map_type type, u64 addr,
180 struct addr_location *al)
181{
182 size_t i;
183 const u8 const cpumodes[] = {
184 PERF_RECORD_MISC_USER,
185 PERF_RECORD_MISC_KERNEL,
186 PERF_RECORD_MISC_GUEST_USER,
187 PERF_RECORD_MISC_GUEST_KERNEL
188 };
189
190 for (i = 0; i < ARRAY_SIZE(cpumodes); i++) {
191 thread__find_addr_location(thread, machine, cpumodes[i], type,
192 addr, al);
193 if (al->map)
194 break;
195 }
196}