Linux Perf
machine.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __PERF_MACHINE_H
3 #define __PERF_MACHINE_H
4 
5 #include <sys/types.h>
6 #include <linux/rbtree.h>
7 #include "map.h"
8 #include "dso.h"
9 #include "event.h"
10 #include "rwsem.h"
11 
12 struct addr_location;
13 struct branch_stack;
14 struct perf_evsel;
15 struct perf_sample;
16 struct symbol;
17 struct thread;
18 union perf_event;
19 
20 /* Native host kernel uses -1 as pid index in machine */
21 #define HOST_KERNEL_ID (-1)
22 #define DEFAULT_GUEST_KERNEL_ID (0)
23 
24 extern const char *ref_reloc_sym_names[];
25 
26 struct vdso_info;
27 
28 #define THREADS__TABLE_BITS 8
29 #define THREADS__TABLE_SIZE (1 << THREADS__TABLE_BITS)
30 
31 struct threads {
32  struct rb_root entries;
34  unsigned int nr;
35  struct list_head dead;
36  struct thread *last_match;
37 };
38 
39 struct machine {
40  struct rb_node rb_node;
41  pid_t pid;
43  bool comm_exec;
45  char *root_dir;
46  char *mmap_name;
49  struct perf_env *env;
50  struct dsos dsos;
51  struct map_groups kmaps;
52  struct map *vmlinux_map;
54  pid_t *current_tid;
55  union { /* Tool specific area */
56  void *priv;
57  u64 db_id;
58  };
60 };
61 
62 static inline struct threads *machine__threads(struct machine *machine, pid_t tid)
63 {
64  /* Cast it to handle tid == -1 */
65  return &machine->threads[(unsigned int)tid % THREADS__TABLE_SIZE];
66 }
67 
68 /*
69  * The main kernel (vmlinux) map
70  */
71 static inline
73 {
74  return machine->vmlinux_map;
75 }
76 
77 /*
78  * kernel (the one returned by machine__kernel_map()) plus kernel modules maps
79  */
80 static inline
82 {
83  return &machine->kmaps.maps;
84 }
85 
87 
88 static inline u64 machine__kernel_start(struct machine *machine)
89 {
90  if (!machine->kernel_start)
92  return machine->kernel_start;
93 }
94 
95 static inline bool machine__kernel_ip(struct machine *machine, u64 ip)
96 {
97  u64 kernel_start = machine__kernel_start(machine);
98 
99  return ip >= kernel_start;
100 }
101 
102 struct thread *machine__find_thread(struct machine *machine, pid_t pid,
103  pid_t tid);
104 struct comm *machine__thread_exec_comm(struct machine *machine,
105  struct thread *thread);
106 
107 int machine__process_comm_event(struct machine *machine, union perf_event *event,
108  struct perf_sample *sample);
109 int machine__process_exit_event(struct machine *machine, union perf_event *event,
110  struct perf_sample *sample);
111 int machine__process_fork_event(struct machine *machine, union perf_event *event,
112  struct perf_sample *sample);
113 int machine__process_lost_event(struct machine *machine, union perf_event *event,
114  struct perf_sample *sample);
115 int machine__process_lost_samples_event(struct machine *machine, union perf_event *event,
116  struct perf_sample *sample);
117 int machine__process_aux_event(struct machine *machine,
118  union perf_event *event);
119 int machine__process_itrace_start_event(struct machine *machine,
120  union perf_event *event);
121 int machine__process_switch_event(struct machine *machine,
122  union perf_event *event);
123 int machine__process_namespaces_event(struct machine *machine,
124  union perf_event *event,
125  struct perf_sample *sample);
126 int machine__process_mmap_event(struct machine *machine, union perf_event *event,
127  struct perf_sample *sample);
128 int machine__process_mmap2_event(struct machine *machine, union perf_event *event,
129  struct perf_sample *sample);
130 int machine__process_event(struct machine *machine, union perf_event *event,
131  struct perf_sample *sample);
132 
133 typedef void (*machine__process_t)(struct machine *machine, void *data);
134 
135 struct machines {
136  struct machine host;
137  struct rb_root guests;
138 };
139 
140 void machines__init(struct machines *machines);
141 void machines__exit(struct machines *machines);
142 
144  machine__process_t process, void *data);
145 
146 struct machine *machines__add(struct machines *machines, pid_t pid,
147  const char *root_dir);
148 struct machine *machines__find_host(struct machines *machines);
149 struct machine *machines__find(struct machines *machines, pid_t pid);
150 struct machine *machines__findnew(struct machines *machines, pid_t pid);
151 
154 
155 struct machine *machine__new_host(void);
156 struct machine *machine__new_kallsyms(void);
157 int machine__init(struct machine *machine, const char *root_dir, pid_t pid);
158 void machine__exit(struct machine *machine);
159 void machine__delete_threads(struct machine *machine);
160 void machine__delete(struct machine *machine);
161 void machine__remove_thread(struct machine *machine, struct thread *th);
162 
163 struct branch_info *sample__resolve_bstack(struct perf_sample *sample,
164  struct addr_location *al);
165 struct mem_info *sample__resolve_mem(struct perf_sample *sample,
166  struct addr_location *al);
167 
168 struct callchain_cursor;
169 
170 int thread__resolve_callchain(struct thread *thread,
171  struct callchain_cursor *cursor,
172  struct perf_evsel *evsel,
173  struct perf_sample *sample,
174  struct symbol **parent,
175  struct addr_location *root_al,
176  int max_stack);
177 
178 /*
179  * Default guest kernel is defined by parameter --guestkallsyms
180  * and --guestmodules
181  */
182 static inline bool machine__is_default_guest(struct machine *machine)
183 {
184  return machine ? machine->pid == DEFAULT_GUEST_KERNEL_ID : false;
185 }
186 
187 static inline bool machine__is_host(struct machine *machine)
188 {
189  return machine ? machine->pid == HOST_KERNEL_ID : false;
190 }
191 
192 bool machine__is(struct machine *machine, const char *arch);
193 int machine__nr_cpus_avail(struct machine *machine);
194 
195 struct thread *__machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid);
196 struct thread *machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid);
197 
198 struct dso *machine__findnew_dso(struct machine *machine, const char *filename);
199 
200 size_t machine__fprintf(struct machine *machine, FILE *fp);
201 
202 static inline
203 struct symbol *machine__find_kernel_symbol(struct machine *machine, u64 addr,
204  struct map **mapp)
205 {
206  return map_groups__find_symbol(&machine->kmaps, addr, mapp);
207 }
208 
209 static inline
210 struct symbol *machine__find_kernel_symbol_by_name(struct machine *machine,
211  const char *name,
212  struct map **mapp)
213 {
214  return map_groups__find_symbol_by_name(&machine->kmaps, name, mapp);
215 }
216 
217 struct map *machine__findnew_module_map(struct machine *machine, u64 start,
218  const char *filename);
219 int arch__fix_module_text_start(u64 *start, const char *name);
220 
221 int machine__load_kallsyms(struct machine *machine, const char *filename);
222 
223 int machine__load_vmlinux_path(struct machine *machine);
224 
225 size_t machine__fprintf_dsos_buildid(struct machine *machine, FILE *fp,
226  bool (skip)(struct dso *dso, int parm), int parm);
227 size_t machines__fprintf_dsos(struct machines *machines, FILE *fp);
228 size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp,
229  bool (skip)(struct dso *dso, int parm), int parm);
230 
231 void machine__destroy_kernel_maps(struct machine *machine);
232 int machine__create_kernel_maps(struct machine *machine);
233 
234 int machines__create_kernel_maps(struct machines *machines, pid_t pid);
235 int machines__create_guest_kernel_maps(struct machines *machines);
236 void machines__destroy_kernel_maps(struct machines *machines);
237 
238 size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp);
239 
240 int machine__for_each_thread(struct machine *machine,
241  int (*fn)(struct thread *thread, void *p),
242  void *priv);
243 int machines__for_each_thread(struct machines *machines,
244  int (*fn)(struct thread *thread, void *p),
245  void *priv);
246 
247 int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool,
248  struct target *target, struct thread_map *threads,
249  perf_event__handler_t process, bool data_mmap,
250  unsigned int proc_map_timeout,
251  unsigned int nr_threads_synthesize);
252 static inline
253 int machine__synthesize_threads(struct machine *machine, struct target *target,
254  struct thread_map *threads, bool data_mmap,
255  unsigned int proc_map_timeout,
256  unsigned int nr_threads_synthesize)
257 {
258  return __machine__synthesize_threads(machine, NULL, target, threads,
259  perf_event__process, data_mmap,
260  proc_map_timeout,
261  nr_threads_synthesize);
262 }
263 
264 pid_t machine__get_current_tid(struct machine *machine, int cpu);
265 int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid,
266  pid_t tid);
267 /*
268  * For use with libtraceevent's pevent_set_function_resolver()
269  */
270 char *machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp);
271 
272 void machine__get_kallsyms_filename(struct machine *machine, char *buf,
273  size_t bufsz);
274 
275 int machine__create_extra_kernel_maps(struct machine *machine,
276  struct dso *kernel);
277 
278 /* Kernel-space maps for symbols that are outside the main kernel map and module maps */
280  u64 start;
281  u64 end;
282  u64 pgoff;
284 };
285 
286 int machine__create_extra_kernel_map(struct machine *machine,
287  struct dso *kernel,
288  struct extra_kernel_map *xm);
289 
290 int machine__map_x86_64_entry_trampolines(struct machine *machine,
291  struct dso *kernel);
292 
293 #endif /* __PERF_MACHINE_H */
static struct threads * machine__threads(struct machine *machine, pid_t tid)
Definition: machine.h:62
struct machine * machines__add(struct machines *machines, pid_t pid, const char *root_dir)
Definition: machine.c:234
struct maps maps
Definition: map.h:64
struct mem_info * sample__resolve_mem(struct perf_sample *sample, struct addr_location *al)
Definition: machine.c:1813
bool trampolines_mapped
Definition: machine.h:59
#define KMAP_NAME_LEN
Definition: map.h:50
struct machine * machines__find(struct machines *machines, pid_t pid)
Definition: machine.c:277
Definition: env.h:36
bool comm_exec
Definition: machine.h:43
const char * ref_reloc_sym_names[]
Definition: machine.c:819
int(* perf_event__handler_t)(struct perf_tool *tool, union perf_event *event, struct perf_sample *sample, struct machine *machine)
Definition: event.h:664
void machines__set_id_hdr_size(struct machines *machines, u16 id_hdr_size)
Definition: machine.c:348
static bool machine__is_host(struct machine *machine)
Definition: machine.h:187
static void skip(int size)
Definition: vdso.c:36
int machine__init(struct machine *machine, const char *root_dir, pid_t pid)
Definition: machine.c:65
const char * filename
Definition: hists_common.c:26
static int machine__synthesize_threads(struct machine *machine, struct target *target, struct thread_map *threads, bool data_mmap, unsigned int proc_map_timeout, unsigned int nr_threads_synthesize)
Definition: machine.h:253
dictionary data
Definition: stat-cpi.py:4
int machine__process_comm_event(struct machine *machine, union perf_event *event, struct perf_sample *sample)
Definition: machine.c:525
int machine__process_fork_event(struct machine *machine, union perf_event *event, struct perf_sample *sample)
Definition: machine.c:1662
#define DEFAULT_GUEST_KERNEL_ID
Definition: machine.h:22
struct map * machine__findnew_module_map(struct machine *machine, u64 start, const char *filename)
Definition: machine.c:664
int machine__process_itrace_start_event(struct machine *machine, union perf_event *event)
void machines__destroy_kernel_maps(struct machines *machines)
Definition: machine.c:1070
void * priv
Definition: machine.h:56
int machine__process_exit_event(struct machine *machine, union perf_event *event, struct perf_sample *sample)
int machine__create_extra_kernel_maps(struct machine *machine, struct dso *kernel)
struct rb_root entries
Definition: machine.h:32
bool machine__is(struct machine *machine, const char *arch)
Definition: machine.c:2459
int machine__get_kernel_start(struct machine *machine)
Definition: machine.c:2469
int machine__map_x86_64_entry_trampolines(struct machine *machine, struct dso *kernel)
Definition: machine.c:915
int machine__load_vmlinux_path(struct machine *machine)
Definition: machine.c:1113
struct vdso_info * vdso_info
Definition: machine.h:48
static struct symbol * machine__find_kernel_symbol_by_name(struct machine *machine, const char *name, struct map **mapp)
Definition: machine.h:210
Definition: comm.h:11
int machine__set_current_tid(struct machine *machine, int cpu, pid_t pid, pid_t tid)
Definition: machine.c:2419
int machine__nr_cpus_avail(struct machine *machine)
Definition: machine.c:2464
static struct map * machine__kernel_map(struct machine *machine)
Definition: machine.h:72
int machine__process_lost_event(struct machine *machine, union perf_event *event, struct perf_sample *sample)
size_t machines__fprintf_dsos_buildid(struct machines *machines, FILE *fp, bool(skip)(struct dso *dso, int parm), int parm)
Definition: machine.c:723
struct thread * machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid)
Definition: machine.c:492
Definition: thread.h:18
const char * name
size_t machines__fprintf_dsos(struct machines *machines, FILE *fp)
Definition: machine.c:704
int machine__process_aux_event(struct machine *machine, union perf_event *event)
int machine__process_switch_event(struct machine *machine, union perf_event *event)
struct thread * __machine__findnew_thread(struct machine *machine, pid_t pid, pid_t tid)
Definition: machine.c:487
int perf_event__process(struct perf_tool *tool __maybe_unused, union perf_event *event, struct perf_sample *sample, struct machine *machine)
Definition: event.c:1503
struct machine * machine__new_host(void)
Definition: machine.c:118
Definition: tool.h:44
struct machine * machine__new_kallsyms(void)
Definition: machine.c:135
struct map_groups kmaps
Definition: machine.h:51
struct threads threads[THREADS__TABLE_SIZE]
Definition: machine.h:47
int machine__process_event(struct machine *machine, union perf_event *event, struct perf_sample *sample)
Definition: machine.c:1729
int __machine__synthesize_threads(struct machine *machine, struct perf_tool *tool, struct target *target, struct thread_map *threads, perf_event__handler_t process, bool data_mmap, unsigned int proc_map_timeout, unsigned int nr_threads_synthesize)
Definition: machine.c:2394
struct symbol * map_groups__find_symbol_by_name(struct map_groups *mg, const char *name, struct map **mapp)
Definition: map.c:619
static struct perf_tool tool
Definition: builtin-diff.c:362
static bool machine__kernel_ip(struct machine *machine, u64 ip)
Definition: machine.h:95
pid_t tid
Definition: thread.h:25
void machines__set_comm_exec(struct machines *machines, bool comm_exec)
Definition: machine.c:264
bool kptr_restrict_warned
Definition: machine.h:44
int arch__fix_module_text_start(u64 *start, const char *name)
Definition: machine.c:9
unsigned int nr
Definition: machine.h:34
u64 kernel_start
Definition: machine.h:53
void machine__delete_threads(struct machine *machine)
Definition: machine.c:174
Definition: map.h:58
int machines__create_guest_kernel_maps(struct machines *machines)
Definition: machine.c:1021
Definition: dso.h:138
#define event
struct symbol * map_groups__find_symbol(struct map_groups *mg, u64 addr, struct map **mapp)
Definition: map.c:578
Definition: map.h:63
void machine__destroy_kernel_maps(struct machine *machine)
Definition: machine.c:1003
int machine__for_each_thread(struct machine *machine, int(*fn)(struct thread *thread, void *p), void *priv)
Definition: machine.c:2345
u64 db_id
Definition: machine.h:57
void machine__remove_thread(struct machine *machine, struct thread *th)
Definition: machine.c:1657
char * machine__resolve_kernel_addr(void *vmachine, unsigned long long *addrp, char **modp)
Definition: machine.c:2501
int machine__process_lost_samples_event(struct machine *machine, union perf_event *event, struct perf_sample *sample)
int machines__create_kernel_maps(struct machines *machines, pid_t pid)
Definition: machine.c:1085
int machine__create_kernel_maps(struct machine *machine)
Definition: machine.c:1321
u16 id_hdr_size
Definition: machine.h:42
pid_t * current_tid
Definition: machine.h:54
void(* machine__process_t)(struct machine *machine, void *data)
Definition: machine.h:133
int machine__process_mmap_event(struct machine *machine, union perf_event *event, struct perf_sample *sample)
Definition: machine.c:1580
u32 pid
Definition: hists_common.c:15
u64 start
Definition: hists_common.c:25
struct machine * machines__find_host(struct machines *machines)
size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp)
Definition: machine.c:736
Definition: jevents.c:228
Definition: dso.h:130
#define HOST_KERNEL_ID
Definition: machine.h:21
int machine__process_namespaces_event(struct machine *machine, union perf_event *event, struct perf_sample *sample)
struct comm * machine__thread_exec_comm(struct machine *machine, struct thread *thread)
Definition: machine.c:516
void machines__init(struct machines *machines)
Definition: machine.c:222
int machine__process_mmap2_event(struct machine *machine, union perf_event *event, struct perf_sample *sample)
Definition: machine.c:1527
Definition: annotate.c:60
char * root_dir
Definition: machine.h:45
void machines__process_guests(struct machines *machines, machine__process_t process, void *data)
Definition: machine.c:337
void machine__exit(struct machine *machine)
Definition: machine.c:193
static struct symbol * machine__find_kernel_symbol(struct machine *machine, u64 addr, struct map **mapp)
Definition: machine.h:203
char * mmap_name
Definition: machine.h:46
#define THREADS__TABLE_SIZE
Definition: machine.h:29
struct map * vmlinux_map
Definition: machine.h:52
void machines__exit(struct machines *machines)
Definition: machine.c:228
static struct maps * machine__kernel_maps(struct machine *machine)
Definition: machine.h:81
pid_t pid
Definition: machine.h:41
size_t machine__fprintf(struct machine *machine, FILE *fp)
Definition: machine.c:756
pid_t machine__get_current_tid(struct machine *machine, int cpu)
Definition: machine.c:2411
struct dso * machine__findnew_dso(struct machine *machine, const char *filename)
Definition: machine.c:2496
struct thread * last_match
Definition: machine.h:36
int machines__for_each_thread(struct machines *machines, int(*fn)(struct thread *thread, void *p), void *priv)
Definition: machine.c:2373
Definition: symbol.h:55
struct thread * machine__find_thread(struct machine *machine, pid_t pid, pid_t tid)
Definition: machine.c:504
void machine__delete(struct machine *machine)
Definition: machine.c:214
struct branch_info * sample__resolve_bstack(struct perf_sample *sample, struct addr_location *al)
Definition: machine.c:1928
struct list_head dead
Definition: machine.h:35
int machine__load_kallsyms(struct machine *machine, const char *filename)
Definition: machine.c:1095
int machine__create_extra_kernel_map(struct machine *machine, struct dso *kernel, struct extra_kernel_map *xm)
Definition: machine.c:854
void machine__get_kallsyms_filename(struct machine *machine, char *buf, size_t bufsz)
Definition: machine.c:810
size_t machine__fprintf_dsos_buildid(struct machine *machine, FILE *fp, bool(skip)(struct dso *dso, int parm), int parm)
Definition: machine.c:717
static u64 machine__kernel_start(struct machine *machine)
Definition: machine.h:88
int thread__resolve_callchain(struct thread *thread, struct callchain_cursor *cursor, struct perf_evsel *evsel, struct perf_sample *sample, struct symbol **parent, struct addr_location *root_al, int max_stack)
Definition: machine.c:2308
struct machine * machines__findnew(struct machines *machines, pid_t pid)
Definition: machine.c:303
struct rw_semaphore lock
Definition: machine.h:33
u32 priv
Definition: map.h:31
Definition: target.h:8
struct perf_env * env
Definition: machine.h:49
static bool machine__is_default_guest(struct machine *machine)
Definition: machine.h:182