xref: /linux/tools/perf/util/thread-stack.h (revision 8be98d2f2a0a262f8bf8a0bc1fdf522b3c7aab17)
12025cf9eSThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
200447ccdSAdrian Hunter /*
300447ccdSAdrian Hunter  * thread-stack.h: Synthesize a thread's stack using call / return events
400447ccdSAdrian Hunter  * Copyright (c) 2014, Intel Corporation.
500447ccdSAdrian Hunter  */
600447ccdSAdrian Hunter 
700447ccdSAdrian Hunter #ifndef __PERF_THREAD_STACK_H
800447ccdSAdrian Hunter #define __PERF_THREAD_STACK_H
900447ccdSAdrian Hunter 
1000447ccdSAdrian Hunter #include <sys/types.h>
1100447ccdSAdrian Hunter 
1200447ccdSAdrian Hunter #include <linux/types.h>
1300447ccdSAdrian Hunter 
1400447ccdSAdrian Hunter struct thread;
1592a9e4f7SAdrian Hunter struct comm;
1600447ccdSAdrian Hunter struct ip_callchain;
1792a9e4f7SAdrian Hunter struct symbol;
1892a9e4f7SAdrian Hunter struct dso;
1992a9e4f7SAdrian Hunter struct perf_sample;
2092a9e4f7SAdrian Hunter struct addr_location;
21451db126SChris Phlipot struct call_path;
2292a9e4f7SAdrian Hunter 
2392a9e4f7SAdrian Hunter /*
2492a9e4f7SAdrian Hunter  * Call/Return flags.
2592a9e4f7SAdrian Hunter  *
2692a9e4f7SAdrian Hunter  * CALL_RETURN_NO_CALL: 'return' but no matching 'call'
2792a9e4f7SAdrian Hunter  * CALL_RETURN_NO_RETURN: 'call' but no matching 'return'
28f08046cbSAdrian Hunter  * CALL_RETURN_NON_CALL: a branch but not a 'call' to the start of a different
29f08046cbSAdrian Hunter  *                       symbol
3092a9e4f7SAdrian Hunter  */
3192a9e4f7SAdrian Hunter enum {
3292a9e4f7SAdrian Hunter 	CALL_RETURN_NO_CALL	= 1 << 0,
3392a9e4f7SAdrian Hunter 	CALL_RETURN_NO_RETURN	= 1 << 1,
34f08046cbSAdrian Hunter 	CALL_RETURN_NON_CALL	= 1 << 2,
3592a9e4f7SAdrian Hunter };
3692a9e4f7SAdrian Hunter 
3792a9e4f7SAdrian Hunter /**
3892a9e4f7SAdrian Hunter  * struct call_return - paired call/return information.
3992a9e4f7SAdrian Hunter  * @thread: thread in which call/return occurred
4092a9e4f7SAdrian Hunter  * @comm: comm in which call/return occurred
4192a9e4f7SAdrian Hunter  * @cp: call path
4292a9e4f7SAdrian Hunter  * @call_time: timestamp of call (if known)
4392a9e4f7SAdrian Hunter  * @return_time: timestamp of return (if known)
4492a9e4f7SAdrian Hunter  * @branch_count: number of branches seen between call and return
45003ccdc7SAdrian Hunter  * @insn_count: approx. number of instructions between call and return
46003ccdc7SAdrian Hunter  * @cyc_count: approx. number of cycles between call and return
4792a9e4f7SAdrian Hunter  * @call_ref: external reference to 'call' sample (e.g. db_id)
4892a9e4f7SAdrian Hunter  * @return_ref:  external reference to 'return' sample (e.g. db_id)
4992a9e4f7SAdrian Hunter  * @db_id: id used for db-export
50f435887eSAdrian Hunter  * @parent_db_id: id of parent call used for db-export
5192a9e4f7SAdrian Hunter  * @flags: Call/Return flags
5292a9e4f7SAdrian Hunter  */
5392a9e4f7SAdrian Hunter struct call_return {
5492a9e4f7SAdrian Hunter 	struct thread *thread;
5592a9e4f7SAdrian Hunter 	struct comm *comm;
5692a9e4f7SAdrian Hunter 	struct call_path *cp;
5792a9e4f7SAdrian Hunter 	u64 call_time;
5892a9e4f7SAdrian Hunter 	u64 return_time;
5992a9e4f7SAdrian Hunter 	u64 branch_count;
60003ccdc7SAdrian Hunter 	u64 insn_count;
61003ccdc7SAdrian Hunter 	u64 cyc_count;
6292a9e4f7SAdrian Hunter 	u64 call_ref;
6392a9e4f7SAdrian Hunter 	u64 return_ref;
6492a9e4f7SAdrian Hunter 	u64 db_id;
65f435887eSAdrian Hunter 	u64 parent_db_id;
6692a9e4f7SAdrian Hunter 	u32 flags;
6792a9e4f7SAdrian Hunter };
6892a9e4f7SAdrian Hunter 
692c15f5ebSChris Phlipot /**
702c15f5ebSChris Phlipot  * struct call_return_processor - provides a call-back to consume call-return
712c15f5ebSChris Phlipot  *                                information.
722c15f5ebSChris Phlipot  * @cpr: call path root
732c15f5ebSChris Phlipot  * @process: call-back that accepts call/return information
742c15f5ebSChris Phlipot  * @data: anonymous data for call-back
752c15f5ebSChris Phlipot  */
762c15f5ebSChris Phlipot struct call_return_processor {
772c15f5ebSChris Phlipot 	struct call_path_root *cpr;
78f435887eSAdrian Hunter 	int (*process)(struct call_return *cr, u64 *parent_db_id, void *data);
792c15f5ebSChris Phlipot 	void *data;
802c15f5ebSChris Phlipot };
812c15f5ebSChris Phlipot 
82256d92bcSAdrian Hunter int thread_stack__event(struct thread *thread, int cpu, u32 flags, u64 from_ip,
8386d67180SAdrian Hunter 			u64 to_ip, u16 insn_len, u64 trace_nr, bool callstack,
8486d67180SAdrian Hunter 			unsigned int br_stack_sz, bool mispred_all);
85256d92bcSAdrian Hunter void thread_stack__set_trace_nr(struct thread *thread, int cpu, u64 trace_nr);
86256d92bcSAdrian Hunter void thread_stack__sample(struct thread *thread, int cpu, struct ip_callchain *chain,
8724248306SAdrian Hunter 			  size_t sz, u64 ip, u64 kernel_start);
884fef41bfSAdrian Hunter void thread_stack__sample_late(struct thread *thread, int cpu,
894fef41bfSAdrian Hunter 			       struct ip_callchain *chain, size_t sz, u64 ip,
904fef41bfSAdrian Hunter 			       u64 kernel_start);
9186d67180SAdrian Hunter void thread_stack__br_sample(struct thread *thread, int cpu,
9286d67180SAdrian Hunter 			     struct branch_stack *dst, unsigned int sz);
93*3749e0bbSAdrian Hunter void thread_stack__br_sample_late(struct thread *thread, int cpu,
94*3749e0bbSAdrian Hunter 				  struct branch_stack *dst, unsigned int sz,
95*3749e0bbSAdrian Hunter 				  u64 sample_ip, u64 kernel_start);
96a5499b37SAdrian Hunter int thread_stack__flush(struct thread *thread);
9700447ccdSAdrian Hunter void thread_stack__free(struct thread *thread);
98256d92bcSAdrian Hunter size_t thread_stack__depth(struct thread *thread, int cpu);
9900447ccdSAdrian Hunter 
10092a9e4f7SAdrian Hunter struct call_return_processor *
101f435887eSAdrian Hunter call_return_processor__new(int (*process)(struct call_return *cr, u64 *parent_db_id, void *data),
10292a9e4f7SAdrian Hunter 			   void *data);
10392a9e4f7SAdrian Hunter void call_return_processor__free(struct call_return_processor *crp);
10492a9e4f7SAdrian Hunter int thread_stack__process(struct thread *thread, struct comm *comm,
10592a9e4f7SAdrian Hunter 			  struct perf_sample *sample,
10692a9e4f7SAdrian Hunter 			  struct addr_location *from_al,
10792a9e4f7SAdrian Hunter 			  struct addr_location *to_al, u64 ref,
10892a9e4f7SAdrian Hunter 			  struct call_return_processor *crp);
10992a9e4f7SAdrian Hunter 
11000447ccdSAdrian Hunter #endif
111