blob: 7a32f447a8e78937863ff66da6f4e0f0e1fd6b3f [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;
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020037 INIT_LIST_HEAD(&thread->comm_list);
38
39 comm_str = malloc(32);
40 if (!comm_str)
41 goto err_thread;
42
43 snprintf(comm_str, 32, ":%d", tid);
44 comm = comm__new(comm_str, 0);
45 free(comm_str);
46 if (!comm)
47 goto err_thread;
48
49 list_add(&comm->list, &thread->comm_list);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020050 }
51
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030052 return thread;
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020053
54err_thread:
55 free(thread);
56 return NULL;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020057}
58
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030059void thread__delete(struct thread *thread)
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -030060{
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020061 struct comm *comm, *tmp;
62
Arnaldo Carvalho de Meloa26ca672014-03-25 15:26:44 -030063 map_groups__put(thread->mg);
Arnaldo Carvalho de Melo93d57312014-03-21 17:57:01 -030064 thread->mg = NULL;
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020065 list_for_each_entry_safe(comm, tmp, &thread->comm_list, list) {
66 list_del(&comm->list);
67 comm__free(comm);
68 }
69
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -030070 free(thread);
Arnaldo Carvalho de Melo591765f2010-07-30 18:28:42 -030071}
72
Namhyung Kim4dfced32013-09-13 16:28:57 +090073struct comm *thread__comm(const struct thread *thread)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +020074{
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020075 if (list_empty(&thread->comm_list))
76 return NULL;
David S. Miller4385d582010-02-26 12:08:34 -030077
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020078 return list_first_entry(&thread->comm_list, struct comm, list);
79}
80
81/* CHECKME: time should always be 0 if event aren't ordered */
82int thread__set_comm(struct thread *thread, const char *str, u64 timestamp)
83{
84 struct comm *new, *curr = thread__comm(thread);
Frederic Weisbecker3178f582014-01-14 16:37:14 +010085 int err;
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020086
87 /* Override latest entry if it had no specific time coverage */
88 if (!curr->start) {
Frederic Weisbecker3178f582014-01-14 16:37:14 +010089 err = comm__override(curr, str, timestamp);
90 if (err)
91 return err;
Frederic Weisbeckera5285ad2013-11-16 02:02:09 +010092 } else {
93 new = comm__new(str, timestamp);
94 if (!new)
95 return -ENOMEM;
96 list_add(&new->list, &thread->comm_list);
David S. Miller4385d582010-02-26 12:08:34 -030097 }
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020098
Frederic Weisbecker1902efe2013-09-11 16:56:44 +020099 thread->comm_set = true;
100
101 return 0;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200102}
103
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200104const char *thread__comm_str(const struct thread *thread)
105{
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200106 const struct comm *comm = thread__comm(thread);
107
108 if (!comm)
109 return NULL;
110
111 return comm__str(comm);
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200112}
113
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200114/* CHECKME: it should probably better return the max comm len from its comm list */
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300115int thread__comm_len(struct thread *thread)
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200116{
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300117 if (!thread->comm_len) {
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200118 const char *comm = thread__comm_str(thread);
119 if (!comm)
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200120 return 0;
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200121 thread->comm_len = strlen(comm);
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200122 }
123
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300124 return thread->comm_len;
Frederic Weisbeckera4fb5812009-10-22 23:23:23 +0200125}
126
Arnaldo Carvalho de Melo3f067dc2012-12-07 17:39:39 -0300127size_t thread__fprintf(struct thread *thread, FILE *fp)
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200128{
Frederic Weisbeckerb9c51432013-09-11 14:46:56 +0200129 return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread)) +
Arnaldo Carvalho de Melo93d57312014-03-21 17:57:01 -0300130 map_groups__fprintf(thread->mg, verbose, fp);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200131}
132
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300133void thread__insert_map(struct thread *thread, struct map *map)
Arnaldo Carvalho de Melo1b46cdd2009-09-28 14:48:46 -0300134{
Arnaldo Carvalho de Melo93d57312014-03-21 17:57:01 -0300135 map_groups__fixup_overlappings(thread->mg, map, verbose, stderr);
136 map_groups__insert(thread->mg, map);
Arnaldo Carvalho de Melo95011c62009-11-27 16:29:20 -0200137}
138
Jiri Olsacddcef62014-04-09 20:54:29 +0200139static int thread__clone_map_groups(struct thread *thread,
140 struct thread *parent)
141{
142 int i;
143
144 /* This is new thread, we share map groups for process. */
145 if (thread->pid_ == parent->pid_)
146 return 0;
147
148 /* But this one is new process, copy maps. */
149 for (i = 0; i < MAP__NR_TYPES; ++i)
150 if (map_groups__clone(thread->mg, parent->mg, i) < 0)
151 return -ENOMEM;
152
153 return 0;
154}
155
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200156int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp)
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200157{
Jiri Olsacddcef62014-04-09 20:54:29 +0200158 int err;
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200159
Arnaldo Carvalho de Melofaa5c5c2010-02-19 23:02:07 -0200160 if (parent->comm_set) {
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200161 const char *comm = thread__comm_str(parent);
162 if (!comm)
Arnaldo Carvalho de Melofaa5c5c2010-02-19 23:02:07 -0200163 return -ENOMEM;
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200164 err = thread__set_comm(thread, comm, timestamp);
David Ahern8d00be82013-12-10 21:35:38 -0700165 if (err)
Frederic Weisbecker1902efe2013-09-11 16:56:44 +0200166 return err;
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300167 thread->comm_set = true;
Arnaldo Carvalho de Melofaa5c5c2010-02-19 23:02:07 -0200168 }
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200169
Arnaldo Carvalho de Meloc824c432013-10-22 19:01:31 -0300170 thread->ppid = parent->tid;
Jiri Olsacddcef62014-04-09 20:54:29 +0200171 return thread__clone_map_groups(thread, parent);
Frederic Weisbecker6baa0a52009-08-14 12:21:53 +0200172}
Arnaldo Carvalho de Melo52a3cb82014-03-11 16:16:49 -0300173
174void thread__find_cpumode_addr_location(struct thread *thread,
175 struct machine *machine,
176 enum map_type type, u64 addr,
177 struct addr_location *al)
178{
179 size_t i;
180 const u8 const cpumodes[] = {
181 PERF_RECORD_MISC_USER,
182 PERF_RECORD_MISC_KERNEL,
183 PERF_RECORD_MISC_GUEST_USER,
184 PERF_RECORD_MISC_GUEST_KERNEL
185 };
186
187 for (i = 0; i < ARRAY_SIZE(cpumodes); i++) {
188 thread__find_addr_location(thread, machine, cpumodes[i], type,
189 addr, al);
190 if (al->map)
191 break;
192 }
193}