Lines Matching full:thread
9 #include "thread.h"
10 #include "thread-stack.h"
19 int thread__init_map_groups(struct thread *thread, struct machine *machine) in thread__init_map_groups() argument
21 pid_t pid = thread->pid_; in thread__init_map_groups()
23 if (pid == thread->tid || pid == -1) { in thread__init_map_groups()
24 thread->mg = map_groups__new(machine); in thread__init_map_groups()
26 struct thread *leader = __machine__findnew_thread(machine, pid, pid); in thread__init_map_groups()
28 thread->mg = map_groups__get(leader->mg); in thread__init_map_groups()
33 return thread->mg ? 0 : -1; in thread__init_map_groups()
36 struct thread *thread__new(pid_t pid, pid_t tid) in thread__new()
40 struct thread *thread = zalloc(sizeof(*thread)); in thread__new() local
42 if (thread != NULL) { in thread__new()
43 thread->pid_ = pid; in thread__new()
44 thread->tid = tid; in thread__new()
45 thread->ppid = -1; in thread__new()
46 thread->cpu = -1; in thread__new()
47 INIT_LIST_HEAD(&thread->namespaces_list); in thread__new()
48 INIT_LIST_HEAD(&thread->comm_list); in thread__new()
49 init_rwsem(&thread->namespaces_lock); in thread__new()
50 init_rwsem(&thread->comm_lock); in thread__new()
62 list_add(&comm->list, &thread->comm_list); in thread__new()
63 refcount_set(&thread->refcnt, 1); in thread__new()
64 RB_CLEAR_NODE(&thread->rb_node); in thread__new()
65 /* Thread holds first ref to nsdata. */ in thread__new()
66 thread->nsinfo = nsinfo__new(pid); in thread__new()
69 return thread; in thread__new()
72 free(thread); in thread__new()
76 void thread__delete(struct thread *thread) in thread__delete() argument
81 BUG_ON(!RB_EMPTY_NODE(&thread->rb_node)); in thread__delete()
83 thread_stack__free(thread); in thread__delete()
85 if (thread->mg) { in thread__delete()
86 map_groups__put(thread->mg); in thread__delete()
87 thread->mg = NULL; in thread__delete()
89 down_write(&thread->namespaces_lock); in thread__delete()
91 &thread->namespaces_list, list) { in thread__delete()
95 up_write(&thread->namespaces_lock); in thread__delete()
97 down_write(&thread->comm_lock); in thread__delete()
98 list_for_each_entry_safe(comm, tmp_comm, &thread->comm_list, list) { in thread__delete()
102 up_write(&thread->comm_lock); in thread__delete()
104 unwind__finish_access(thread); in thread__delete()
105 nsinfo__zput(thread->nsinfo); in thread__delete()
107 exit_rwsem(&thread->namespaces_lock); in thread__delete()
108 exit_rwsem(&thread->comm_lock); in thread__delete()
109 free(thread); in thread__delete()
112 struct thread *thread__get(struct thread *thread) in thread__get() argument
114 if (thread) in thread__get()
115 refcount_inc(&thread->refcnt); in thread__get()
116 return thread; in thread__get()
119 void thread__put(struct thread *thread) in thread__put() argument
121 if (thread && refcount_dec_and_test(&thread->refcnt)) { in thread__put()
126 list_del_init(&thread->node); in thread__put()
127 thread__delete(thread); in thread__put()
131 static struct namespaces *__thread__namespaces(const struct thread *thread) in __thread__namespaces() argument
133 if (list_empty(&thread->namespaces_list)) in __thread__namespaces()
136 return list_first_entry(&thread->namespaces_list, struct namespaces, list); in __thread__namespaces()
139 struct namespaces *thread__namespaces(const struct thread *thread) in thread__namespaces() argument
143 down_read((struct rw_semaphore *)&thread->namespaces_lock); in thread__namespaces()
144 ns = __thread__namespaces(thread); in thread__namespaces()
145 up_read((struct rw_semaphore *)&thread->namespaces_lock); in thread__namespaces()
150 static int __thread__set_namespaces(struct thread *thread, u64 timestamp, in __thread__set_namespaces() argument
153 struct namespaces *new, *curr = __thread__namespaces(thread); in __thread__set_namespaces()
159 list_add(&new->list, &thread->namespaces_list); in __thread__set_namespaces()
164 * of this thread. Update end time for the namespaces in __thread__set_namespaces()
174 int thread__set_namespaces(struct thread *thread, u64 timestamp, in thread__set_namespaces() argument
179 down_write(&thread->namespaces_lock); in thread__set_namespaces()
180 ret = __thread__set_namespaces(thread, timestamp, event); in thread__set_namespaces()
181 up_write(&thread->namespaces_lock); in thread__set_namespaces()
185 struct comm *thread__comm(const struct thread *thread) in thread__comm() argument
187 if (list_empty(&thread->comm_list)) in thread__comm()
190 return list_first_entry(&thread->comm_list, struct comm, list); in thread__comm()
193 struct comm *thread__exec_comm(const struct thread *thread) in thread__exec_comm() argument
197 list_for_each_entry(comm, &thread->comm_list, list) { in thread__exec_comm()
206 * thread (created by processing a synthesized fork event). For a main in thread__exec_comm()
207 * thread, that is very probably wrong. Prefer a later comm to avoid in thread__exec_comm()
210 if (second_last && !last->start && thread->pid_ == thread->tid) in thread__exec_comm()
216 static int ____thread__set_comm(struct thread *thread, const char *str, in ____thread__set_comm() argument
219 struct comm *new, *curr = thread__comm(thread); in ____thread__set_comm()
222 if (!thread->comm_set) { in ____thread__set_comm()
230 list_add(&new->list, &thread->comm_list); in ____thread__set_comm()
233 unwind__flush_access(thread); in ____thread__set_comm()
236 thread->comm_set = true; in ____thread__set_comm()
241 int __thread__set_comm(struct thread *thread, const char *str, u64 timestamp, in __thread__set_comm() argument
246 down_write(&thread->comm_lock); in __thread__set_comm()
247 ret = ____thread__set_comm(thread, str, timestamp, exec); in __thread__set_comm()
248 up_write(&thread->comm_lock); in __thread__set_comm()
252 int thread__set_comm_from_proc(struct thread *thread) in thread__set_comm_from_proc() argument
260 thread->pid_, thread->tid) >= (int)sizeof(path)) && in thread__set_comm_from_proc()
263 err = thread__set_comm(thread, comm, 0); in thread__set_comm_from_proc()
269 static const char *__thread__comm_str(const struct thread *thread) in __thread__comm_str() argument
271 const struct comm *comm = thread__comm(thread); in __thread__comm_str()
279 const char *thread__comm_str(const struct thread *thread) in thread__comm_str() argument
283 down_read((struct rw_semaphore *)&thread->comm_lock); in thread__comm_str()
284 str = __thread__comm_str(thread); in thread__comm_str()
285 up_read((struct rw_semaphore *)&thread->comm_lock); in thread__comm_str()
291 int thread__comm_len(struct thread *thread) in thread__comm_len() argument
293 if (!thread->comm_len) { in thread__comm_len()
294 const char *comm = thread__comm_str(thread); in thread__comm_len()
297 thread->comm_len = strlen(comm); in thread__comm_len()
300 return thread->comm_len; in thread__comm_len()
303 size_t thread__fprintf(struct thread *thread, FILE *fp) in thread__fprintf() argument
305 return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread)) + in thread__fprintf()
306 map_groups__fprintf(thread->mg, fp); in thread__fprintf()
309 int thread__insert_map(struct thread *thread, struct map *map) in thread__insert_map() argument
313 ret = unwind__prepare_access(thread, map, NULL); in thread__insert_map()
317 map_groups__fixup_overlappings(thread->mg, map, stderr); in thread__insert_map()
318 map_groups__insert(thread->mg, map); in thread__insert_map()
323 static int __thread__prepare_access(struct thread *thread) in __thread__prepare_access() argument
327 struct maps *maps = &thread->mg->maps; in __thread__prepare_access()
333 err = unwind__prepare_access(thread, map, &initialized); in __thread__prepare_access()
343 static int thread__prepare_access(struct thread *thread) in thread__prepare_access() argument
348 err = __thread__prepare_access(thread); in thread__prepare_access()
353 static int thread__clone_map_groups(struct thread *thread, in thread__clone_map_groups() argument
354 struct thread *parent) in thread__clone_map_groups()
356 /* This is new thread, we share map groups for process. */ in thread__clone_map_groups()
357 if (thread->pid_ == parent->pid_) in thread__clone_map_groups()
358 return thread__prepare_access(thread); in thread__clone_map_groups()
360 if (thread->mg == parent->mg) { in thread__clone_map_groups()
361 pr_debug("broken map groups on thread %d/%d parent %d/%d\n", in thread__clone_map_groups()
362 thread->pid_, thread->tid, parent->pid_, parent->tid); in thread__clone_map_groups()
367 if (map_groups__clone(thread, parent->mg) < 0) in thread__clone_map_groups()
373 int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp) in thread__fork() argument
380 err = thread__set_comm(thread, comm, timestamp); in thread__fork()
385 thread->ppid = parent->tid; in thread__fork()
386 return thread__clone_map_groups(thread, parent); in thread__fork()
389 void thread__find_cpumode_addr_location(struct thread *thread, u64 addr, in thread__find_cpumode_addr_location() argument
401 thread__find_symbol(thread, cpumodes[i], addr, al); in thread__find_cpumode_addr_location()
407 struct thread *thread__main_thread(struct machine *machine, struct thread *thread) in thread__main_thread() argument
409 if (thread->pid_ == thread->tid) in thread__main_thread()
410 return thread__get(thread); in thread__main_thread()
412 if (thread->pid_ == -1) in thread__main_thread()
415 return machine__find_thread(machine, thread->pid_, thread->pid_); in thread__main_thread()