xref: /openbmc/linux/tools/perf/util/thread.c (revision 2ff40400)
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 		if (exec)
122 			unwind__flush_access(thread);
123 	}
124 
125 	thread->comm_set = true;
126 
127 	return 0;
128 }
129 
130 const char *thread__comm_str(const struct thread *thread)
131 {
132 	const struct comm *comm = thread__comm(thread);
133 
134 	if (!comm)
135 		return NULL;
136 
137 	return comm__str(comm);
138 }
139 
140 /* CHECKME: it should probably better return the max comm len from its comm list */
141 int thread__comm_len(struct thread *thread)
142 {
143 	if (!thread->comm_len) {
144 		const char *comm = thread__comm_str(thread);
145 		if (!comm)
146 			return 0;
147 		thread->comm_len = strlen(comm);
148 	}
149 
150 	return thread->comm_len;
151 }
152 
153 size_t thread__fprintf(struct thread *thread, FILE *fp)
154 {
155 	return fprintf(fp, "Thread %d %s\n", thread->tid, thread__comm_str(thread)) +
156 	       map_groups__fprintf(thread->mg, fp);
157 }
158 
159 void thread__insert_map(struct thread *thread, struct map *map)
160 {
161 	map_groups__fixup_overlappings(thread->mg, map, stderr);
162 	map_groups__insert(thread->mg, map);
163 }
164 
165 static int thread__clone_map_groups(struct thread *thread,
166 				    struct thread *parent)
167 {
168 	int i;
169 
170 	/* This is new thread, we share map groups for process. */
171 	if (thread->pid_ == parent->pid_)
172 		return 0;
173 
174 	/* But this one is new process, copy maps. */
175 	for (i = 0; i < MAP__NR_TYPES; ++i)
176 		if (map_groups__clone(thread->mg, parent->mg, i) < 0)
177 			return -ENOMEM;
178 
179 	return 0;
180 }
181 
182 int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp)
183 {
184 	int err;
185 
186 	if (parent->comm_set) {
187 		const char *comm = thread__comm_str(parent);
188 		if (!comm)
189 			return -ENOMEM;
190 		err = thread__set_comm(thread, comm, timestamp);
191 		if (err)
192 			return err;
193 		thread->comm_set = true;
194 	}
195 
196 	thread->ppid = parent->tid;
197 	return thread__clone_map_groups(thread, parent);
198 }
199 
200 void thread__find_cpumode_addr_location(struct thread *thread,
201 					struct machine *machine,
202 					enum map_type type, u64 addr,
203 					struct addr_location *al)
204 {
205 	size_t i;
206 	const u8 const cpumodes[] = {
207 		PERF_RECORD_MISC_USER,
208 		PERF_RECORD_MISC_KERNEL,
209 		PERF_RECORD_MISC_GUEST_USER,
210 		PERF_RECORD_MISC_GUEST_KERNEL
211 	};
212 
213 	for (i = 0; i < ARRAY_SIZE(cpumodes); i++) {
214 		thread__find_addr_location(thread, machine, cpumodes[i], type,
215 					   addr, al);
216 		if (al->map)
217 			break;
218 	}
219 }
220