regulator: s2mps11: Adjust supported buck voltages to real values
[linux/fpc-iii.git] / tools / perf / util / thread.c
blob50678d318185496c35983dda3270497ee6668e65
1 // SPDX-License-Identifier: GPL-2.0
2 #include "../perf.h"
3 #include <errno.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <string.h>
7 #include <linux/kernel.h>
8 #include "session.h"
9 #include "thread.h"
10 #include "thread-stack.h"
11 #include "util.h"
12 #include "debug.h"
13 #include "namespaces.h"
14 #include "comm.h"
15 #include "map.h"
16 #include "symbol.h"
17 #include "unwind.h"
19 #include <api/fs/fs.h>
21 int thread__init_map_groups(struct thread *thread, struct machine *machine)
23 pid_t pid = thread->pid_;
25 if (pid == thread->tid || pid == -1) {
26 thread->mg = map_groups__new(machine);
27 } else {
28 struct thread *leader = __machine__findnew_thread(machine, pid, pid);
29 if (leader) {
30 thread->mg = map_groups__get(leader->mg);
31 thread__put(leader);
35 return thread->mg ? 0 : -1;
38 struct thread *thread__new(pid_t pid, pid_t tid)
40 char *comm_str;
41 struct comm *comm;
42 struct thread *thread = zalloc(sizeof(*thread));
44 if (thread != NULL) {
45 thread->pid_ = pid;
46 thread->tid = tid;
47 thread->ppid = -1;
48 thread->cpu = -1;
49 INIT_LIST_HEAD(&thread->namespaces_list);
50 INIT_LIST_HEAD(&thread->comm_list);
51 init_rwsem(&thread->namespaces_lock);
52 init_rwsem(&thread->comm_lock);
54 comm_str = malloc(32);
55 if (!comm_str)
56 goto err_thread;
58 snprintf(comm_str, 32, ":%d", tid);
59 comm = comm__new(comm_str, 0, false);
60 free(comm_str);
61 if (!comm)
62 goto err_thread;
64 list_add(&comm->list, &thread->comm_list);
65 refcount_set(&thread->refcnt, 1);
66 RB_CLEAR_NODE(&thread->rb_node);
67 /* Thread holds first ref to nsdata. */
68 thread->nsinfo = nsinfo__new(pid);
69 srccode_state_init(&thread->srccode_state);
72 return thread;
74 err_thread:
75 free(thread);
76 return NULL;
79 void thread__delete(struct thread *thread)
81 struct namespaces *namespaces, *tmp_namespaces;
82 struct comm *comm, *tmp_comm;
84 BUG_ON(!RB_EMPTY_NODE(&thread->rb_node));
86 thread_stack__free(thread);
88 if (thread->mg) {
89 map_groups__put(thread->mg);
90 thread->mg = NULL;
92 down_write(&thread->namespaces_lock);
93 list_for_each_entry_safe(namespaces, tmp_namespaces,
94 &thread->namespaces_list, list) {
95 list_del(&namespaces->list);
96 namespaces__free(namespaces);
98 up_write(&thread->namespaces_lock);
100 down_write(&thread->comm_lock);
101 list_for_each_entry_safe(comm, tmp_comm, &thread->comm_list, list) {
102 list_del(&comm->list);
103 comm__free(comm);
105 up_write(&thread->comm_lock);
107 unwind__finish_access(thread);
108 nsinfo__zput(thread->nsinfo);
109 srccode_state_free(&thread->srccode_state);
111 exit_rwsem(&thread->namespaces_lock);
112 exit_rwsem(&thread->comm_lock);
113 free(thread);
116 struct thread *thread__get(struct thread *thread)
118 if (thread)
119 refcount_inc(&thread->refcnt);
120 return thread;
123 void thread__put(struct thread *thread)
125 if (thread && refcount_dec_and_test(&thread->refcnt)) {
127 * Remove it from the dead_threads list, as last reference
128 * is gone.
130 list_del_init(&thread->node);
131 thread__delete(thread);
135 struct namespaces *thread__namespaces(const struct thread *thread)
137 if (list_empty(&thread->namespaces_list))
138 return NULL;
140 return list_first_entry(&thread->namespaces_list, struct namespaces, list);
143 static int __thread__set_namespaces(struct thread *thread, u64 timestamp,
144 struct namespaces_event *event)
146 struct namespaces *new, *curr = thread__namespaces(thread);
148 new = namespaces__new(event);
149 if (!new)
150 return -ENOMEM;
152 list_add(&new->list, &thread->namespaces_list);
154 if (timestamp && curr) {
156 * setns syscall must have changed few or all the namespaces
157 * of this thread. Update end time for the namespaces
158 * previously used.
160 curr = list_next_entry(new, list);
161 curr->end_time = timestamp;
164 return 0;
167 int thread__set_namespaces(struct thread *thread, u64 timestamp,
168 struct namespaces_event *event)
170 int ret;
172 down_write(&thread->namespaces_lock);
173 ret = __thread__set_namespaces(thread, timestamp, event);
174 up_write(&thread->namespaces_lock);
175 return ret;
178 struct comm *thread__comm(const struct thread *thread)
180 if (list_empty(&thread->comm_list))
181 return NULL;
183 return list_first_entry(&thread->comm_list, struct comm, list);
186 struct comm *thread__exec_comm(const struct thread *thread)
188 struct comm *comm, *last = NULL;
190 list_for_each_entry(comm, &thread->comm_list, list) {
191 if (comm->exec)
192 return comm;
193 last = comm;
196 return last;
199 static int ____thread__set_comm(struct thread *thread, const char *str,
200 u64 timestamp, bool exec)
202 struct comm *new, *curr = thread__comm(thread);
204 /* Override the default :tid entry */
205 if (!thread->comm_set) {
206 int err = comm__override(curr, str, timestamp, exec);
207 if (err)
208 return err;
209 } else {
210 new = comm__new(str, timestamp, exec);
211 if (!new)
212 return -ENOMEM;
213 list_add(&new->list, &thread->comm_list);
215 if (exec)
216 unwind__flush_access(thread);
219 thread->comm_set = true;
221 return 0;
224 int __thread__set_comm(struct thread *thread, const char *str, u64 timestamp,
225 bool exec)
227 int ret;
229 down_write(&thread->comm_lock);
230 ret = ____thread__set_comm(thread, str, timestamp, exec);
231 up_write(&thread->comm_lock);
232 return ret;
235 int thread__set_comm_from_proc(struct thread *thread)
237 char path[64];
238 char *comm = NULL;
239 size_t sz;
240 int err = -1;
242 if (!(snprintf(path, sizeof(path), "%d/task/%d/comm",
243 thread->pid_, thread->tid) >= (int)sizeof(path)) &&
244 procfs__read_str(path, &comm, &sz) == 0) {
245 comm[sz - 1] = '\0';
246 err = thread__set_comm(thread, comm, 0);
249 return err;
252 static const char *__thread__comm_str(const struct thread *thread)
254 const struct comm *comm = thread__comm(thread);
256 if (!comm)
257 return NULL;
259 return comm__str(comm);
262 const char *thread__comm_str(const struct thread *thread)
264 const char *str;
266 down_read((struct rw_semaphore *)&thread->comm_lock);
267 str = __thread__comm_str(thread);
268 up_read((struct rw_semaphore *)&thread->comm_lock);
270 return str;
273 /* CHECKME: it should probably better return the max comm len from its comm list */
274 int thread__comm_len(struct thread *thread)
276 if (!thread->comm_len) {
277 const char *comm = thread__comm_str(thread);
278 if (!comm)
279 return 0;
280 thread->comm_len = strlen(comm);
283 return thread->comm_len;
286 size_t thread__fprintf(struct thread *thread, FILE *fp)
288 return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread)) +
289 map_groups__fprintf(thread->mg, fp);
292 int thread__insert_map(struct thread *thread, struct map *map)
294 int ret;
296 ret = unwind__prepare_access(thread, map, NULL);
297 if (ret)
298 return ret;
300 map_groups__fixup_overlappings(thread->mg, map, stderr);
301 map_groups__insert(thread->mg, map);
303 return 0;
306 static int __thread__prepare_access(struct thread *thread)
308 bool initialized = false;
309 int err = 0;
310 struct maps *maps = &thread->mg->maps;
311 struct map *map;
313 down_read(&maps->lock);
315 for (map = maps__first(maps); map; map = map__next(map)) {
316 err = unwind__prepare_access(thread, map, &initialized);
317 if (err || initialized)
318 break;
321 up_read(&maps->lock);
323 return err;
326 static int thread__prepare_access(struct thread *thread)
328 int err = 0;
330 if (symbol_conf.use_callchain)
331 err = __thread__prepare_access(thread);
333 return err;
336 static int thread__clone_map_groups(struct thread *thread,
337 struct thread *parent,
338 bool do_maps_clone)
340 /* This is new thread, we share map groups for process. */
341 if (thread->pid_ == parent->pid_)
342 return thread__prepare_access(thread);
344 if (thread->mg == parent->mg) {
345 pr_debug("broken map groups on thread %d/%d parent %d/%d\n",
346 thread->pid_, thread->tid, parent->pid_, parent->tid);
347 return 0;
349 /* But this one is new process, copy maps. */
350 return do_maps_clone ? map_groups__clone(thread, parent->mg) : 0;
353 int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp, bool do_maps_clone)
355 if (parent->comm_set) {
356 const char *comm = thread__comm_str(parent);
357 int err;
358 if (!comm)
359 return -ENOMEM;
360 err = thread__set_comm(thread, comm, timestamp);
361 if (err)
362 return err;
365 thread->ppid = parent->tid;
366 return thread__clone_map_groups(thread, parent, do_maps_clone);
369 void thread__find_cpumode_addr_location(struct thread *thread, u64 addr,
370 struct addr_location *al)
372 size_t i;
373 const u8 cpumodes[] = {
374 PERF_RECORD_MISC_USER,
375 PERF_RECORD_MISC_KERNEL,
376 PERF_RECORD_MISC_GUEST_USER,
377 PERF_RECORD_MISC_GUEST_KERNEL
380 for (i = 0; i < ARRAY_SIZE(cpumodes); i++) {
381 thread__find_symbol(thread, cpumodes[i], addr, al);
382 if (al->map)
383 break;
387 struct thread *thread__main_thread(struct machine *machine, struct thread *thread)
389 if (thread->pid_ == thread->tid)
390 return thread__get(thread);
392 if (thread->pid_ == -1)
393 return NULL;
395 return machine__find_thread(machine, thread->pid_, thread->pid_);
398 int thread__memcpy(struct thread *thread, struct machine *machine,
399 void *buf, u64 ip, int len, bool *is64bit)
401 u8 cpumode = PERF_RECORD_MISC_USER;
402 struct addr_location al;
403 long offset;
405 if (machine__kernel_ip(machine, ip))
406 cpumode = PERF_RECORD_MISC_KERNEL;
408 if (!thread__find_map(thread, cpumode, ip, &al) || !al.map->dso ||
409 al.map->dso->data.status == DSO_DATA_STATUS_ERROR ||
410 map__load(al.map) < 0)
411 return -1;
413 offset = al.map->map_ip(al.map, ip);
414 if (is64bit)
415 *is64bit = al.map->dso->is_64_bit;
417 return dso__data_read_offset(al.map->dso, machine, offset, buf, len);