Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[cascardo/linux.git] / tools / perf / util / thread.c
1 #include "../perf.h"
2 #include <stdlib.h>
3 #include <stdio.h>
4 #include <string.h>
5 #include "session.h"
6 #include "thread.h"
7 #include "util.h"
8 #include "debug.h"
9 #include "comm.h"
10
11 int thread__init_map_groups(struct thread *thread, struct machine *machine)
12 {
13         struct thread *leader;
14         pid_t pid = thread->pid_;
15
16         if (pid == thread->tid || pid == -1) {
17                 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
27 struct thread *thread__new(pid_t pid, pid_t tid)
28 {
29         char *comm_str;
30         struct comm *comm;
31         struct thread *thread = zalloc(sizeof(*thread));
32
33         if (thread != NULL) {
34                 thread->pid_ = pid;
35                 thread->tid = tid;
36                 thread->ppid = -1;
37                 thread->cpu = -1;
38                 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);
51         }
52
53         return thread;
54
55 err_thread:
56         free(thread);
57         return NULL;
58 }
59
60 void thread__delete(struct thread *thread)
61 {
62         struct comm *comm, *tmp;
63
64         if (thread->mg) {
65                 map_groups__put(thread->mg);
66                 thread->mg = NULL;
67         }
68         list_for_each_entry_safe(comm, tmp, &thread->comm_list, list) {
69                 list_del(&comm->list);
70                 comm__free(comm);
71         }
72
73         free(thread);
74 }
75
76 struct comm *thread__comm(const struct thread *thread)
77 {
78         if (list_empty(&thread->comm_list))
79                 return NULL;
80
81         return list_first_entry(&thread->comm_list, struct comm, list);
82 }
83
84 /* CHECKME: time should always be 0 if event aren't ordered */
85 int thread__set_comm(struct thread *thread, const char *str, u64 timestamp)
86 {
87         struct comm *new, *curr = thread__comm(thread);
88         int err;
89
90         /* Override latest entry if it had no specific time coverage */
91         if (!curr->start) {
92                 err = comm__override(curr, str, timestamp);
93                 if (err)
94                         return err;
95         } else {
96                 new = comm__new(str, timestamp);
97                 if (!new)
98                         return -ENOMEM;
99                 list_add(&new->list, &thread->comm_list);
100         }
101
102         thread->comm_set = true;
103
104         return 0;
105 }
106
107 const char *thread__comm_str(const struct thread *thread)
108 {
109         const struct comm *comm = thread__comm(thread);
110
111         if (!comm)
112                 return NULL;
113
114         return comm__str(comm);
115 }
116
117 /* CHECKME: it should probably better return the max comm len from its comm list */
118 int thread__comm_len(struct thread *thread)
119 {
120         if (!thread->comm_len) {
121                 const char *comm = thread__comm_str(thread);
122                 if (!comm)
123                         return 0;
124                 thread->comm_len = strlen(comm);
125         }
126
127         return thread->comm_len;
128 }
129
130 size_t thread__fprintf(struct thread *thread, FILE *fp)
131 {
132         return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread)) +
133                map_groups__fprintf(thread->mg, fp);
134 }
135
136 void thread__insert_map(struct thread *thread, struct map *map)
137 {
138         map_groups__fixup_overlappings(thread->mg, map, stderr);
139         map_groups__insert(thread->mg, map);
140 }
141
142 static 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
159 int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp)
160 {
161         int err;
162
163         if (parent->comm_set) {
164                 const char *comm = thread__comm_str(parent);
165                 if (!comm)
166                         return -ENOMEM;
167                 err = thread__set_comm(thread, comm, timestamp);
168                 if (err)
169                         return err;
170                 thread->comm_set = true;
171         }
172
173         thread->ppid = parent->tid;
174         return thread__clone_map_groups(thread, parent);
175 }
176
177 void 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 }