1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __PERF_THREAD_H
3 #define __PERF_THREAD_H
5 #include <linux/refcount.h>
6 #include <linux/rbtree.h>
7 #include <linux/list.h>
10 #include <sys/types.h>
12 #include "symbol_conf.h"
17 #include "callchain.h"
18 #include <internal/rc_check.h>
22 struct perf_record_namespaces;
24 struct unwind_libunwind_ops;
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;
33 struct thread_rb_node {
34 struct rb_node rb_node;
35 struct thread *thread;
38 DECLARE_RC_STRUCT(thread) {
40 pid_t pid_; /* Not all tools update this */
44 int guest_cpu; /* For QEMU thread */
48 struct list_head namespaces_list;
49 struct rw_semaphore namespaces_lock;
50 struct list_head comm_list;
51 struct rw_semaphore comm_lock;
55 struct thread_stack *ts;
56 struct nsinfo *nsinfo;
57 struct srccode_state srccode_state;
59 int filter_entry_depth;
61 /* LBR call stack stitch */
62 bool lbr_stitch_enable;
63 struct lbr_stitch *lbr_stitch;
70 struct thread *thread__new(pid_t pid, pid_t tid);
71 int thread__init_maps(struct thread *thread, struct machine *machine);
72 void thread__delete(struct thread *thread);
74 struct thread *thread__get(struct thread *thread);
75 void thread__put(struct thread *thread);
77 static inline void __thread__zput(struct thread **thread)
83 #define thread__zput(thread) __thread__zput(&thread)
85 struct namespaces *thread__namespaces(struct thread *thread);
86 int thread__set_namespaces(struct thread *thread, u64 timestamp,
87 struct perf_record_namespaces *event);
89 int __thread__set_comm(struct thread *thread, const char *comm, u64 timestamp,
91 static inline int thread__set_comm(struct thread *thread, const char *comm,
94 return __thread__set_comm(thread, comm, timestamp, false);
97 int thread__set_comm_from_proc(struct thread *thread);
99 int thread__comm_len(struct thread *thread);
100 struct comm *thread__comm(struct thread *thread);
101 struct comm *thread__exec_comm(struct thread *thread);
102 const char *thread__comm_str(struct thread *thread);
103 int thread__insert_map(struct thread *thread, struct map *map);
104 int thread__fork(struct thread *thread, struct thread *parent, u64 timestamp, bool do_maps_clone);
105 size_t thread__fprintf(struct thread *thread, FILE *fp);
107 struct thread *thread__main_thread(struct machine *machine, struct thread *thread);
109 struct map *thread__find_map(struct thread *thread, u8 cpumode, u64 addr,
110 struct addr_location *al);
111 struct map *thread__find_map_fb(struct thread *thread, u8 cpumode, u64 addr,
112 struct addr_location *al);
114 struct symbol *thread__find_symbol(struct thread *thread, u8 cpumode,
115 u64 addr, struct addr_location *al);
116 struct symbol *thread__find_symbol_fb(struct thread *thread, u8 cpumode,
117 u64 addr, struct addr_location *al);
119 void thread__find_cpumode_addr_location(struct thread *thread, u64 addr,
120 struct addr_location *al);
122 int thread__memcpy(struct thread *thread, struct machine *machine,
123 void *buf, u64 ip, int len, bool *is64bit);
125 static inline struct maps *thread__maps(struct thread *thread)
127 return RC_CHK_ACCESS(thread)->maps;
130 static inline void thread__set_maps(struct thread *thread, struct maps *maps)
132 RC_CHK_ACCESS(thread)->maps = maps;
135 static inline pid_t thread__pid(const struct thread *thread)
137 return RC_CHK_ACCESS(thread)->pid_;
140 static inline void thread__set_pid(struct thread *thread, pid_t pid_)
142 RC_CHK_ACCESS(thread)->pid_ = pid_;
145 static inline pid_t thread__tid(const struct thread *thread)
147 return RC_CHK_ACCESS(thread)->tid;
150 static inline void thread__set_tid(struct thread *thread, pid_t tid)
152 RC_CHK_ACCESS(thread)->tid = tid;
155 static inline pid_t thread__ppid(const struct thread *thread)
157 return RC_CHK_ACCESS(thread)->ppid;
160 static inline void thread__set_ppid(struct thread *thread, pid_t ppid)
162 RC_CHK_ACCESS(thread)->ppid = ppid;
165 static inline int thread__cpu(const struct thread *thread)
167 return RC_CHK_ACCESS(thread)->cpu;
170 static inline void thread__set_cpu(struct thread *thread, int cpu)
172 RC_CHK_ACCESS(thread)->cpu = cpu;
175 static inline int thread__guest_cpu(const struct thread *thread)
177 return RC_CHK_ACCESS(thread)->guest_cpu;
180 static inline void thread__set_guest_cpu(struct thread *thread, int guest_cpu)
182 RC_CHK_ACCESS(thread)->guest_cpu = guest_cpu;
185 static inline refcount_t *thread__refcnt(struct thread *thread)
187 return &RC_CHK_ACCESS(thread)->refcnt;
190 static inline bool thread__comm_set(const struct thread *thread)
192 return RC_CHK_ACCESS(thread)->comm_set;
195 static inline void thread__set_comm_set(struct thread *thread, bool set)
197 RC_CHK_ACCESS(thread)->comm_set = set;
200 static inline int thread__var_comm_len(const struct thread *thread)
202 return RC_CHK_ACCESS(thread)->comm_len;
205 static inline void thread__set_comm_len(struct thread *thread, int len)
207 RC_CHK_ACCESS(thread)->comm_len = len;
210 static inline struct list_head *thread__namespaces_list(struct thread *thread)
212 return &RC_CHK_ACCESS(thread)->namespaces_list;
215 static inline int thread__namespaces_list_empty(const struct thread *thread)
217 return list_empty(&RC_CHK_ACCESS(thread)->namespaces_list);
220 static inline struct rw_semaphore *thread__namespaces_lock(struct thread *thread)
222 return &RC_CHK_ACCESS(thread)->namespaces_lock;
225 static inline struct list_head *thread__comm_list(struct thread *thread)
227 return &RC_CHK_ACCESS(thread)->comm_list;
230 static inline struct rw_semaphore *thread__comm_lock(struct thread *thread)
232 return &RC_CHK_ACCESS(thread)->comm_lock;
235 static inline u64 thread__db_id(const struct thread *thread)
237 return RC_CHK_ACCESS(thread)->db_id;
240 static inline void thread__set_db_id(struct thread *thread, u64 db_id)
242 RC_CHK_ACCESS(thread)->db_id = db_id;
245 static inline void *thread__priv(struct thread *thread)
247 return RC_CHK_ACCESS(thread)->priv;
250 static inline void thread__set_priv(struct thread *thread, void *p)
252 RC_CHK_ACCESS(thread)->priv = p;
255 static inline struct thread_stack *thread__ts(struct thread *thread)
257 return RC_CHK_ACCESS(thread)->ts;
260 static inline void thread__set_ts(struct thread *thread, struct thread_stack *ts)
262 RC_CHK_ACCESS(thread)->ts = ts;
265 static inline struct nsinfo *thread__nsinfo(struct thread *thread)
267 return RC_CHK_ACCESS(thread)->nsinfo;
270 static inline struct srccode_state *thread__srccode_state(struct thread *thread)
272 return &RC_CHK_ACCESS(thread)->srccode_state;
275 static inline bool thread__filter(const struct thread *thread)
277 return RC_CHK_ACCESS(thread)->filter;
280 static inline void thread__set_filter(struct thread *thread, bool filter)
282 RC_CHK_ACCESS(thread)->filter = filter;
285 static inline int thread__filter_entry_depth(const struct thread *thread)
287 return RC_CHK_ACCESS(thread)->filter_entry_depth;
290 static inline void thread__set_filter_entry_depth(struct thread *thread, int depth)
292 RC_CHK_ACCESS(thread)->filter_entry_depth = depth;
295 static inline bool thread__lbr_stitch_enable(const struct thread *thread)
297 return RC_CHK_ACCESS(thread)->lbr_stitch_enable;
300 static inline void thread__set_lbr_stitch_enable(struct thread *thread, bool en)
302 RC_CHK_ACCESS(thread)->lbr_stitch_enable = en;
305 static inline struct lbr_stitch *thread__lbr_stitch(struct thread *thread)
307 return RC_CHK_ACCESS(thread)->lbr_stitch;
310 static inline void thread__set_lbr_stitch(struct thread *thread, struct lbr_stitch *lbrs)
312 RC_CHK_ACCESS(thread)->lbr_stitch = lbrs;
315 static inline bool thread__is_filtered(struct thread *thread)
317 if (symbol_conf.comm_list &&
318 !strlist__has_entry(symbol_conf.comm_list, thread__comm_str(thread))) {
322 if (symbol_conf.pid_list &&
323 !intlist__has_entry(symbol_conf.pid_list, thread__pid(thread))) {
327 if (symbol_conf.tid_list &&
328 !intlist__has_entry(symbol_conf.tid_list, thread__tid(thread))) {
335 void thread__free_stitch_list(struct thread *thread);
337 void thread__resolve(struct thread *thread, struct addr_location *al,
338 struct perf_sample *sample);
340 #endif /* __PERF_THREAD_H */