ARM: dts: rockchip: Add SPI DMA into rk3288.dtsi
[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 #include "unwind.h"
11
12 int thread__init_map_groups(struct thread *thread, struct machine *machine)
13 {
14         struct thread *leader;
15         pid_t pid = thread->pid_;
16
17         if (pid == thread->tid || pid == -1) {
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
28 struct thread *thread__new(pid_t pid, pid_t tid)
29 {
30         char *comm_str;
31         struct comm *comm;
32         struct thread *thread = zalloc(sizeof(*thread));
33
34         if (thread != NULL) {
35                 thread->pid_ = pid;
36                 thread->tid = tid;
37                 thread->ppid = -1;
38                 thread->cpu = -1;
39                 INIT_LIST_HEAD(&thread->comm_list);
40
41                 if (unwind__prepare_access(thread) < 0)
42                         goto err_thread;
43
44                 comm_str = malloc(32);
45                 if (!comm_str)
46                         goto err_thread;
47
48                 snprintf(comm_str, 32, ":%d", tid);
49                 comm = comm__new(comm_str, 0, false);
50                 free(comm_str);
51                 if (!comm)
52                         goto err_thread;
53
54                 list_add(&comm->list, &thread->comm_list);
55
56         }
57
58         return thread;
59
60 err_thread:
61         free(thread);
62         return NULL;
63 }
64
65 void thread__delete(struct thread *thread)
66 {
67         struct comm *comm, *tmp;
68
69         if (thread->mg) {
70                 map_groups__put(thread->mg);
71                 thread->mg = NULL;
72         }
73         list_for_each_entry_safe(comm, tmp, &thread->comm_list, list) {
74                 list_del(&comm->list);
75                 comm__free(comm);
76         }
77         unwind__finish_access(thread);
78
79         free(thread);
80 }
81
82 struct comm *thread__comm(const struct thread *thread)
83 {
84         if (list_empty(&thread->comm_list))
85                 return NULL;
86
87         return list_first_entry(&thread->comm_list, struct comm, list);
88 }
89
90 struct 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
103 /* CHECKME: time should always be 0 if event aren't ordered */
104 int __thread__set_comm(struct thread *thread, const char *str, u64 timestamp,
105                        bool exec)
106 {
107         struct comm *new, *curr = thread__comm(thread);
108         int err;
109
110         /* Override latest entry if it had no specific time coverage */
111         if (!curr->start && !curr->exec) {
112                 err = comm__override(curr, str, timestamp, exec);
113                 if (err)
114                         return err;
115         } else {
116                 new = comm__new(str, timestamp, exec);
117                 if (!new)
118                         return -ENOMEM;
119                 list_add(&new->list, &thread->comm_list);
120         }
121
122         thread->comm_set = true;
123
124         return 0;
125 }
126
127 const char *thread__comm_str(const struct thread *thread)
128 {
129         const struct comm *comm = thread__comm(thread);
130
131         if (!comm)
132                 return NULL;
133
134         return comm__str(comm);
135 }
136
137 /* CHECKME: it should probably better return the max comm len from its comm list */
138 int thread__comm_len(struct thread *thread)
139 {
140         if (!thread->comm_len) {
141                 const char *comm = thread__comm_str(thread);
142                 if (!comm)
143                         return 0;
144                 thread->comm_len = strlen(comm);
145         }
146
147         return thread->comm_len;
148 }
149
150 size_t thread__fprintf(struct thread *thread, FILE *fp)
151 {
152         return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread)) +
153                map_groups__fprintf(thread->mg, fp);
154 }
155
156 void thread__insert_map(struct thread *thread, struct map *map)
157 {
158         map_groups__fixup_overlappings(thread->mg, map, stderr);
159         map_groups__insert(thread->mg, map);
160 }
161
162 static 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
179 int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp)
180 {
181         int err;
182
183         if (parent->comm_set) {
184                 const char *comm = thread__comm_str(parent);
185                 if (!comm)
186                         return -ENOMEM;
187                 err = thread__set_comm(thread, comm, timestamp);
188                 if (err)
189                         return err;
190                 thread->comm_set = true;
191         }
192
193         thread->ppid = parent->tid;
194         return thread__clone_map_groups(thread, parent);
195 }
196
197 void 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 }