xref: /openbmc/linux/tools/perf/util/thread.h (revision 19e85d93)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __PERF_THREAD_H
3 #define __PERF_THREAD_H
4 
5 #include <linux/refcount.h>
6 #include <linux/rbtree.h>
7 #include <linux/list.h>
8 #include <stdio.h>
9 #include <unistd.h>
10 #include <sys/types.h>
11 #include "srccode.h"
12 #include "symbol_conf.h"
13 #include <strlist.h>
14 #include <intlist.h>
15 #include "rwsem.h"
16 #include "event.h"
17 #include "callchain.h"
18 #include <internal/rc_check.h>
19 
20 struct addr_location;
21 struct map;
22 struct perf_record_namespaces;
23 struct thread_stack;
24 struct unwind_libunwind_ops;
25 
26 struct lbr_stitch {
27 	struct list_head		lists;
28 	struct list_head		free_lists;
29 	struct perf_sample		prev_sample;
30 	struct callchain_cursor_node	*prev_lbr_cursor;
31 };
32 
33 struct thread_rb_node {
34 	struct rb_node rb_node;
35 	struct thread *thread;
36 };
37 
38 DECLARE_RC_STRUCT(thread) {
39 	/** @maps: mmaps associated with this thread. */
40 	struct maps		*maps;
41 	pid_t			pid_; /* Not all tools update this */
42 	/** @tid: thread ID number unique to a machine. */
43 	pid_t			tid;
44 	/** @ppid: parent process of the process this thread belongs to. */
45 	pid_t			ppid;
46 	int			cpu;
47 	int			guest_cpu; /* For QEMU thread */
48 	refcount_t		refcnt;
49 	/**
50 	 * @exited: Has the thread had an exit event. Such threads are usually
51 	 * removed from the machine's threads but some events/tools require
52 	 * access to dead threads.
53 	 */
54 	bool			exited;
55 	bool			comm_set;
56 	int			comm_len;
57 	struct list_head	namespaces_list;
58 	struct rw_semaphore	namespaces_lock;
59 	struct list_head	comm_list;
60 	struct rw_semaphore	comm_lock;
61 	u64			db_id;
62 
63 	void			*priv;
64 	struct thread_stack	*ts;
65 	struct nsinfo		*nsinfo;
66 	struct srccode_state	srccode_state;
67 	bool			filter;
68 	int			filter_entry_depth;
69 
70 	/* LBR call stack stitch */
71 	bool			lbr_stitch_enable;
72 	struct lbr_stitch	*lbr_stitch;
73 };
74 
75 struct machine;
76 struct namespaces;
77 struct comm;
78 
79 struct thread *thread__new(pid_t pid, pid_t tid);
80 int thread__init_maps(struct thread *thread, struct machine *machine);
81 void thread__delete(struct thread *thread);
82 
83 void thread__set_priv_destructor(void (*destructor)(void *priv));
84 
85 struct thread *thread__get(struct thread *thread);
86 void thread__put(struct thread *thread);
87 
88 static inline void __thread__zput(struct thread **thread)
89 {
90 	thread__put(*thread);
91 	*thread = NULL;
92 }
93 
94 #define thread__zput(thread) __thread__zput(&thread)
95 
96 struct namespaces *thread__namespaces(struct thread *thread);
97 int thread__set_namespaces(struct thread *thread, u64 timestamp,
98 			   struct perf_record_namespaces *event);
99 
100 int __thread__set_comm(struct thread *thread, const char *comm, u64 timestamp,
101 		       bool exec);
102 static inline int thread__set_comm(struct thread *thread, const char *comm,
103 				   u64 timestamp)
104 {
105 	return __thread__set_comm(thread, comm, timestamp, false);
106 }
107 
108 int thread__set_comm_from_proc(struct thread *thread);
109 
110 int thread__comm_len(struct thread *thread);
111 struct comm *thread__comm(struct thread *thread);
112 struct comm *thread__exec_comm(struct thread *thread);
113 const char *thread__comm_str(struct thread *thread);
114 int thread__insert_map(struct thread *thread, struct map *map);
115 int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp, bool do_maps_clone);
116 size_t thread__fprintf(struct thread *thread, FILE *fp);
117 
118 struct thread *thread__main_thread(struct machine *machine, struct thread *thread);
119 
120 struct map *thread__find_map(struct thread *thread, u8 cpumode, u64 addr,
121 			     struct addr_location *al);
122 struct map *thread__find_map_fb(struct thread *thread, u8 cpumode, u64 addr,
123 				struct addr_location *al);
124 
125 struct symbol *thread__find_symbol(struct thread *thread, u8 cpumode,
126 				   u64 addr, struct addr_location *al);
127 struct symbol *thread__find_symbol_fb(struct thread *thread, u8 cpumode,
128 				      u64 addr, struct addr_location *al);
129 
130 void thread__find_cpumode_addr_location(struct thread *thread, u64 addr,
131 					struct addr_location *al);
132 
133 int thread__memcpy(struct thread *thread, struct machine *machine,
134 		   void *buf, u64 ip, int len, bool *is64bit);
135 
136 static inline struct maps *thread__maps(struct thread *thread)
137 {
138 	return RC_CHK_ACCESS(thread)->maps;
139 }
140 
141 static inline void thread__set_maps(struct thread *thread, struct maps *maps)
142 {
143 	RC_CHK_ACCESS(thread)->maps = maps;
144 }
145 
146 static inline pid_t thread__pid(const struct thread *thread)
147 {
148 	return RC_CHK_ACCESS(thread)->pid_;
149 }
150 
151 static inline void thread__set_pid(struct thread *thread, pid_t pid_)
152 {
153 	RC_CHK_ACCESS(thread)->pid_ = pid_;
154 }
155 
156 static inline pid_t thread__tid(const struct thread *thread)
157 {
158 	return RC_CHK_ACCESS(thread)->tid;
159 }
160 
161 static inline void thread__set_tid(struct thread *thread, pid_t tid)
162 {
163 	RC_CHK_ACCESS(thread)->tid = tid;
164 }
165 
166 static inline pid_t thread__ppid(const struct thread *thread)
167 {
168 	return RC_CHK_ACCESS(thread)->ppid;
169 }
170 
171 static inline void thread__set_ppid(struct thread *thread, pid_t ppid)
172 {
173 	RC_CHK_ACCESS(thread)->ppid = ppid;
174 }
175 
176 static inline int thread__cpu(const struct thread *thread)
177 {
178 	return RC_CHK_ACCESS(thread)->cpu;
179 }
180 
181 static inline void thread__set_cpu(struct thread *thread, int cpu)
182 {
183 	RC_CHK_ACCESS(thread)->cpu = cpu;
184 }
185 
186 static inline int thread__guest_cpu(const struct thread *thread)
187 {
188 	return RC_CHK_ACCESS(thread)->guest_cpu;
189 }
190 
191 static inline void thread__set_guest_cpu(struct thread *thread, int guest_cpu)
192 {
193 	RC_CHK_ACCESS(thread)->guest_cpu = guest_cpu;
194 }
195 
196 static inline refcount_t *thread__refcnt(struct thread *thread)
197 {
198 	return &RC_CHK_ACCESS(thread)->refcnt;
199 }
200 
201 static inline void thread__set_exited(struct thread *thread, bool exited)
202 {
203 	RC_CHK_ACCESS(thread)->exited = exited;
204 }
205 
206 static inline bool thread__comm_set(const struct thread *thread)
207 {
208 	return RC_CHK_ACCESS(thread)->comm_set;
209 }
210 
211 static inline void thread__set_comm_set(struct thread *thread, bool set)
212 {
213 	RC_CHK_ACCESS(thread)->comm_set = set;
214 }
215 
216 static inline int thread__var_comm_len(const struct thread *thread)
217 {
218 	return RC_CHK_ACCESS(thread)->comm_len;
219 }
220 
221 static inline void thread__set_comm_len(struct thread *thread, int len)
222 {
223 	RC_CHK_ACCESS(thread)->comm_len = len;
224 }
225 
226 static inline struct list_head *thread__namespaces_list(struct thread *thread)
227 {
228 	return &RC_CHK_ACCESS(thread)->namespaces_list;
229 }
230 
231 static inline int thread__namespaces_list_empty(const struct thread *thread)
232 {
233 	return list_empty(&RC_CHK_ACCESS(thread)->namespaces_list);
234 }
235 
236 static inline struct rw_semaphore *thread__namespaces_lock(struct thread *thread)
237 {
238 	return &RC_CHK_ACCESS(thread)->namespaces_lock;
239 }
240 
241 static inline struct list_head *thread__comm_list(struct thread *thread)
242 {
243 	return &RC_CHK_ACCESS(thread)->comm_list;
244 }
245 
246 static inline struct rw_semaphore *thread__comm_lock(struct thread *thread)
247 {
248 	return &RC_CHK_ACCESS(thread)->comm_lock;
249 }
250 
251 static inline u64 thread__db_id(const struct thread *thread)
252 {
253 	return RC_CHK_ACCESS(thread)->db_id;
254 }
255 
256 static inline void thread__set_db_id(struct thread *thread, u64 db_id)
257 {
258 	RC_CHK_ACCESS(thread)->db_id = db_id;
259 }
260 
261 static inline void *thread__priv(struct thread *thread)
262 {
263 	return RC_CHK_ACCESS(thread)->priv;
264 }
265 
266 static inline void thread__set_priv(struct thread *thread, void *p)
267 {
268 	RC_CHK_ACCESS(thread)->priv = p;
269 }
270 
271 static inline struct thread_stack *thread__ts(struct thread *thread)
272 {
273 	return RC_CHK_ACCESS(thread)->ts;
274 }
275 
276 static inline void thread__set_ts(struct thread *thread, struct thread_stack *ts)
277 {
278 	RC_CHK_ACCESS(thread)->ts = ts;
279 }
280 
281 static inline struct nsinfo *thread__nsinfo(struct thread *thread)
282 {
283 	return RC_CHK_ACCESS(thread)->nsinfo;
284 }
285 
286 static inline struct srccode_state *thread__srccode_state(struct thread *thread)
287 {
288 	return &RC_CHK_ACCESS(thread)->srccode_state;
289 }
290 
291 static inline bool thread__filter(const struct thread *thread)
292 {
293 	return RC_CHK_ACCESS(thread)->filter;
294 }
295 
296 static inline void thread__set_filter(struct thread *thread, bool filter)
297 {
298 	RC_CHK_ACCESS(thread)->filter = filter;
299 }
300 
301 static inline int thread__filter_entry_depth(const struct thread *thread)
302 {
303 	return RC_CHK_ACCESS(thread)->filter_entry_depth;
304 }
305 
306 static inline void thread__set_filter_entry_depth(struct thread *thread, int depth)
307 {
308 	RC_CHK_ACCESS(thread)->filter_entry_depth = depth;
309 }
310 
311 static inline bool thread__lbr_stitch_enable(const struct thread *thread)
312 {
313 	return RC_CHK_ACCESS(thread)->lbr_stitch_enable;
314 }
315 
316 static inline void thread__set_lbr_stitch_enable(struct thread *thread, bool en)
317 {
318 	RC_CHK_ACCESS(thread)->lbr_stitch_enable = en;
319 }
320 
321 static inline struct lbr_stitch	*thread__lbr_stitch(struct thread *thread)
322 {
323 	return RC_CHK_ACCESS(thread)->lbr_stitch;
324 }
325 
326 static inline void thread__set_lbr_stitch(struct thread *thread, struct lbr_stitch *lbrs)
327 {
328 	RC_CHK_ACCESS(thread)->lbr_stitch = lbrs;
329 }
330 
331 static inline bool thread__is_filtered(struct thread *thread)
332 {
333 	if (symbol_conf.comm_list &&
334 	    !strlist__has_entry(symbol_conf.comm_list, thread__comm_str(thread))) {
335 		return true;
336 	}
337 
338 	if (symbol_conf.pid_list &&
339 	    !intlist__has_entry(symbol_conf.pid_list, thread__pid(thread))) {
340 		return true;
341 	}
342 
343 	if (symbol_conf.tid_list &&
344 	    !intlist__has_entry(symbol_conf.tid_list, thread__tid(thread))) {
345 		return true;
346 	}
347 
348 	return false;
349 }
350 
351 void thread__free_stitch_list(struct thread *thread);
352 
353 void thread__resolve(struct thread *thread, struct addr_location *al,
354 		     struct perf_sample *sample);
355 
356 #endif	/* __PERF_THREAD_H */
357