xref: /openbmc/linux/tools/perf/util/machine.h (revision 930beb5a)
1 #ifndef __PERF_MACHINE_H
2 #define __PERF_MACHINE_H
3 
4 #include <sys/types.h>
5 #include <linux/rbtree.h>
6 #include "map.h"
7 #include "event.h"
8 
9 struct addr_location;
10 struct branch_stack;
11 struct perf_evsel;
12 struct perf_sample;
13 struct symbol;
14 struct thread;
15 union perf_event;
16 
17 /* Native host kernel uses -1 as pid index in machine */
18 #define	HOST_KERNEL_ID			(-1)
19 #define	DEFAULT_GUEST_KERNEL_ID		(0)
20 
21 struct machine {
22 	struct rb_node	  rb_node;
23 	pid_t		  pid;
24 	u16		  id_hdr_size;
25 	char		  *root_dir;
26 	struct rb_root	  threads;
27 	struct list_head  dead_threads;
28 	struct thread	  *last_match;
29 	struct list_head  user_dsos;
30 	struct list_head  kernel_dsos;
31 	struct map_groups kmaps;
32 	struct map	  *vmlinux_maps[MAP__NR_TYPES];
33 	symbol_filter_t	  symbol_filter;
34 };
35 
36 static inline
37 struct map *machine__kernel_map(struct machine *machine, enum map_type type)
38 {
39 	return machine->vmlinux_maps[type];
40 }
41 
42 struct thread *machine__find_thread(struct machine *machine, pid_t tid);
43 
44 int machine__process_comm_event(struct machine *machine, union perf_event *event,
45 				struct perf_sample *sample);
46 int machine__process_exit_event(struct machine *machine, union perf_event *event,
47 				struct perf_sample *sample);
48 int machine__process_fork_event(struct machine *machine, union perf_event *event,
49 				struct perf_sample *sample);
50 int machine__process_lost_event(struct machine *machine, union perf_event *event,
51 				struct perf_sample *sample);
52 int machine__process_mmap_event(struct machine *machine, union perf_event *event,
53 				struct perf_sample *sample);
54 int machine__process_mmap2_event(struct machine *machine, union perf_event *event,
55 				 struct perf_sample *sample);
56 int machine__process_event(struct machine *machine, union perf_event *event,
57 				struct perf_sample *sample);
58 
59 typedef void (*machine__process_t)(struct machine *machine, void *data);
60 
61 struct machines {
62 	struct machine host;
63 	struct rb_root guests;
64 	symbol_filter_t symbol_filter;
65 };
66 
67 void machines__init(struct machines *machines);
68 void machines__exit(struct machines *machines);
69 
70 void machines__process_guests(struct machines *machines,
71 			      machine__process_t process, void *data);
72 
73 struct machine *machines__add(struct machines *machines, pid_t pid,
74 			      const char *root_dir);
75 struct machine *machines__find_host(struct machines *machines);
76 struct machine *machines__find(struct machines *machines, pid_t pid);
77 struct machine *machines__findnew(struct machines *machines, pid_t pid);
78 
79 void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size);
80 char *machine__mmap_name(struct machine *machine, char *bf, size_t size);
81 
82 void machines__set_symbol_filter(struct machines *machines,
83 				 symbol_filter_t symbol_filter);
84 
85 struct machine *machine__new_host(void);
86 int machine__init(struct machine *machine, const char *root_dir, pid_t pid);
87 void machine__exit(struct machine *machine);
88 void machine__delete_dead_threads(struct machine *machine);
89 void machine__delete_threads(struct machine *machine);
90 void machine__delete(struct machine *machine);
91 
92 struct branch_info *machine__resolve_bstack(struct machine *machine,
93 					    struct thread *thread,
94 					    struct branch_stack *bs);
95 struct mem_info *machine__resolve_mem(struct machine *machine,
96 				      struct thread *thread,
97 				      struct perf_sample *sample, u8 cpumode);
98 int machine__resolve_callchain(struct machine *machine,
99 			       struct perf_evsel *evsel,
100 			       struct thread *thread,
101 			       struct perf_sample *sample,
102 			       struct symbol **parent,
103 			       struct addr_location *root_al,
104 			       int max_stack);
105 
106 /*
107  * Default guest kernel is defined by parameter --guestkallsyms
108  * and --guestmodules
109  */
110 static inline bool machine__is_default_guest(struct machine *machine)
111 {
112 	return machine ? machine->pid == DEFAULT_GUEST_KERNEL_ID : false;
113 }
114 
115 static inline bool machine__is_host(struct machine *machine)
116 {
117 	return machine ? machine->pid == HOST_KERNEL_ID : false;
118 }
119 
120 struct thread *machine__findnew_thread(struct machine *machine, pid_t pid,
121 				       pid_t tid);
122 
123 size_t machine__fprintf(struct machine *machine, FILE *fp);
124 
125 static inline
126 struct symbol *machine__find_kernel_symbol(struct machine *machine,
127 					   enum map_type type, u64 addr,
128 					   struct map **mapp,
129 					   symbol_filter_t filter)
130 {
131 	return map_groups__find_symbol(&machine->kmaps, type, addr,
132 				       mapp, filter);
133 }
134 
135 static inline
136 struct symbol *machine__find_kernel_function(struct machine *machine, u64 addr,
137 					     struct map **mapp,
138 					     symbol_filter_t filter)
139 {
140 	return machine__find_kernel_symbol(machine, MAP__FUNCTION, addr,
141 					   mapp, filter);
142 }
143 
144 static inline
145 struct symbol *machine__find_kernel_function_by_name(struct machine *machine,
146 						     const char *name,
147 						     struct map **mapp,
148 						     symbol_filter_t filter)
149 {
150 	return map_groups__find_function_by_name(&machine->kmaps, name, mapp,
151 						 filter);
152 }
153 
154 struct map *machine__new_module(struct machine *machine, u64 start,
155 				const char *filename);
156 
157 int machine__load_kallsyms(struct machine *machine, const char *filename,
158 			   enum map_type type, symbol_filter_t filter);
159 int machine__load_vmlinux_path(struct machine *machine, enum map_type type,
160 			       symbol_filter_t filter);
161 
162 size_t machine__fprintf_dsos_buildid(struct machine *machine, FILE *fp,
163 				     bool (skip)(struct dso *dso, int parm), int parm);
164 size_t machines__fprintf_dsos(struct machines *machines, FILE *fp);
165 size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
166 				     bool (skip)(struct dso *dso, int parm), int parm);
167 
168 void machine__destroy_kernel_maps(struct machine *machine);
169 int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel);
170 int machine__create_kernel_maps(struct machine *machine);
171 
172 int machines__create_kernel_maps(struct machines *machines, pid_t pid);
173 int machines__create_guest_kernel_maps(struct machines *machines);
174 void machines__destroy_kernel_maps(struct machines *machines);
175 
176 size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp);
177 
178 int machine__for_each_thread(struct machine *machine,
179 			     int (*fn)(struct thread *thread, void *p),
180 			     void *priv);
181 
182 int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool,
183 				  struct target *target, struct thread_map *threads,
184 				  perf_event__handler_t process, bool data_mmap);
185 static inline
186 int machine__synthesize_threads(struct machine *machine, struct target *target,
187 				struct thread_map *threads, bool data_mmap)
188 {
189 	return __machine__synthesize_threads(machine, NULL, target, threads,
190 					     perf_event__process, data_mmap);
191 }
192 
193 #endif /* __PERF_MACHINE_H */
194