Merge tag 'locks-v3.16-2' of git://git.samba.org/jlayton/linux
[linux/fpc-iii.git] / tools / perf / util / thread.c
blob2fde0d5e40b5f4ef409b1be27197043bbf740a3f
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"
11 int thread__init_map_groups(struct thread *thread, struct machine *machine)
13 struct thread *leader;
14 pid_t pid = thread->pid_;
16 if (pid == thread->tid) {
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);
24 return thread->mg ? 0 : -1;
27 struct thread *thread__new(pid_t pid, pid_t tid)
29 char *comm_str;
30 struct comm *comm;
31 struct thread *thread = zalloc(sizeof(*thread));
33 if (thread != NULL) {
34 thread->pid_ = pid;
35 thread->tid = tid;
36 thread->ppid = -1;
37 INIT_LIST_HEAD(&thread->comm_list);
39 comm_str = malloc(32);
40 if (!comm_str)
41 goto err_thread;
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;
49 list_add(&comm->list, &thread->comm_list);
52 return thread;
54 err_thread:
55 free(thread);
56 return NULL;
59 void thread__delete(struct thread *thread)
61 struct comm *comm, *tmp;
63 map_groups__put(thread->mg);
64 thread->mg = NULL;
65 list_for_each_entry_safe(comm, tmp, &thread->comm_list, list) {
66 list_del(&comm->list);
67 comm__free(comm);
70 free(thread);
73 struct comm *thread__comm(const struct thread *thread)
75 if (list_empty(&thread->comm_list))
76 return NULL;
78 return list_first_entry(&thread->comm_list, struct comm, list);
81 /* CHECKME: time should always be 0 if event aren't ordered */
82 int thread__set_comm(struct thread *thread, const char *str, u64 timestamp)
84 struct comm *new, *curr = thread__comm(thread);
85 int err;
87 /* Override latest entry if it had no specific time coverage */
88 if (!curr->start) {
89 err = comm__override(curr, str, timestamp);
90 if (err)
91 return err;
92 } else {
93 new = comm__new(str, timestamp);
94 if (!new)
95 return -ENOMEM;
96 list_add(&new->list, &thread->comm_list);
99 thread->comm_set = true;
101 return 0;
104 const char *thread__comm_str(const struct thread *thread)
106 const struct comm *comm = thread__comm(thread);
108 if (!comm)
109 return NULL;
111 return comm__str(comm);
114 /* CHECKME: it should probably better return the max comm len from its comm list */
115 int thread__comm_len(struct thread *thread)
117 if (!thread->comm_len) {
118 const char *comm = thread__comm_str(thread);
119 if (!comm)
120 return 0;
121 thread->comm_len = strlen(comm);
124 return thread->comm_len;
127 size_t thread__fprintf(struct thread *thread, FILE *fp)
129 return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread)) +
130 map_groups__fprintf(thread->mg, verbose, fp);
133 void thread__insert_map(struct thread *thread, struct map *map)
135 map_groups__fixup_overlappings(thread->mg, map, verbose, stderr);
136 map_groups__insert(thread->mg, map);
139 static int thread__clone_map_groups(struct thread *thread,
140 struct thread *parent)
142 int i;
144 /* This is new thread, we share map groups for process. */
145 if (thread->pid_ == parent->pid_)
146 return 0;
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;
153 return 0;
156 int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp)
158 int err;
160 if (parent->comm_set) {
161 const char *comm = thread__comm_str(parent);
162 if (!comm)
163 return -ENOMEM;
164 err = thread__set_comm(thread, comm, timestamp);
165 if (err)
166 return err;
167 thread->comm_set = true;
170 thread->ppid = parent->tid;
171 return thread__clone_map_groups(thread, parent);
174 void thread__find_cpumode_addr_location(struct thread *thread,
175 struct machine *machine,
176 enum map_type type, u64 addr,
177 struct addr_location *al)
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
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;