evlist.h 12.4 KB
Newer Older
1 2 3
#ifndef __PERF_EVLIST_H
#define __PERF_EVLIST_H 1

4
#include <linux/kernel.h>
5
#include <linux/refcount.h>
6
#include <linux/list.h>
7
#include <api/fd/array.h>
8
#include <stdio.h>
9
#include "../perf.h"
10
#include "event.h"
11
#include "evsel.h"
12
#include "util.h"
13
#include "auxtrace.h"
14
#include <unistd.h>
15

16
struct pollfd;
17 18
struct thread_map;
struct cpu_map;
19
struct record_opts;
20

21 22 23
#define PERF_EVLIST__HLIST_BITS 8
#define PERF_EVLIST__HLIST_SIZE (1 << PERF_EVLIST__HLIST_BITS)

24 25 26 27 28
/**
 * struct perf_mmap - perf's ring buffer mmap details
 *
 * @refcnt - e.g. code using PERF_EVENT_IOC_SET_OUTPUT to share this
 */
29 30 31
struct perf_mmap {
	void		 *base;
	int		 mask;
W
Wang Nan 已提交
32
	int		 fd;
33
	refcount_t	 refcnt;
34
	u64		 prev;
35
	struct auxtrace_mmap auxtrace_mmap;
D
David Ahern 已提交
36
	char		 event_copy[PERF_SAMPLE_MAX_SIZE] __attribute__((aligned(8)));
37 38
};

39 40 41 42 43 44
static inline size_t
perf_mmap__mmap_len(struct perf_mmap *map)
{
	return map->mask + 1 + page_size;
}

45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
/*
 * State machine of bkw_mmap_state:
 *
 *                     .________________(forbid)_____________.
 *                     |                                     V
 * NOTREADY --(0)--> RUNNING --(1)--> DATA_PENDING --(2)--> EMPTY
 *                     ^  ^              |   ^               |
 *                     |  |__(forbid)____/   |___(forbid)___/|
 *                     |                                     |
 *                      \_________________(3)_______________/
 *
 * NOTREADY     : Backward ring buffers are not ready
 * RUNNING      : Backward ring buffers are recording
 * DATA_PENDING : We are required to collect data from backward ring buffers
 * EMPTY        : We have collected data from backward ring buffers.
 *
 * (0): Setup backward ring buffer
 * (1): Pause ring buffers for reading
 * (2): Read from ring buffers
 * (3): Resume ring buffers for recording
 */
enum bkw_mmap_state {
	BKW_MMAP_NOTREADY,
	BKW_MMAP_RUNNING,
	BKW_MMAP_DATA_PENDING,
	BKW_MMAP_EMPTY,
};

73 74
struct perf_evlist {
	struct list_head entries;
75
	struct hlist_head heads[PERF_EVLIST__HLIST_SIZE];
76
	int		 nr_entries;
77
	int		 nr_groups;
78
	int		 nr_mmaps;
79
	bool		 overwrite;
80
	bool		 enabled;
81
	bool		 has_user_cpus;
82
	size_t		 mmap_len;
83 84 85
	int		 id_pos;
	int		 is_pos;
	u64		 combined_sample_type;
86
	enum bkw_mmap_state bkw_mmap_state;
87 88 89 90
	struct {
		int	cork_fd;
		pid_t	pid;
	} workload;
91
	struct fdarray	 pollfd;
92
	struct perf_mmap *mmap;
93
	struct perf_mmap *backward_mmap;
94 95
	struct thread_map *threads;
	struct cpu_map	  *cpus;
96
	struct perf_evsel *selected;
97
	struct events_stats stats;
98
	struct perf_env	*env;
99 100
};

101 102 103 104 105
struct perf_evsel_str_handler {
	const char *name;
	void	   *handler;
};

106
struct perf_evlist *perf_evlist__new(void);
107
struct perf_evlist *perf_evlist__new_default(void);
108
struct perf_evlist *perf_evlist__new_dummy(void);
109 110
void perf_evlist__init(struct perf_evlist *evlist, struct cpu_map *cpus,
		       struct thread_map *threads);
111
void perf_evlist__exit(struct perf_evlist *evlist);
112 113 114
void perf_evlist__delete(struct perf_evlist *evlist);

void perf_evlist__add(struct perf_evlist *evlist, struct perf_evsel *entry);
115
void perf_evlist__remove(struct perf_evlist *evlist, struct perf_evsel *evsel);
116
int perf_evlist__add_default(struct perf_evlist *evlist);
117 118
int __perf_evlist__add_default_attrs(struct perf_evlist *evlist,
				     struct perf_event_attr *attrs, size_t nr_attrs);
119

120 121
#define perf_evlist__add_default_attrs(evlist, array) \
	__perf_evlist__add_default_attrs(evlist, array, ARRAY_SIZE(array))
122

123 124
int perf_evlist__add_dummy(struct perf_evlist *evlist);

125 126 127
int perf_evlist__add_newtp(struct perf_evlist *evlist,
			   const char *sys, const char *name, void *handler);

128 129 130 131 132 133 134 135 136 137 138
void __perf_evlist__set_sample_bit(struct perf_evlist *evlist,
				   enum perf_event_sample_format bit);
void __perf_evlist__reset_sample_bit(struct perf_evlist *evlist,
				     enum perf_event_sample_format bit);

#define perf_evlist__set_sample_bit(evlist, bit) \
	__perf_evlist__set_sample_bit(evlist, PERF_SAMPLE_##bit)

#define perf_evlist__reset_sample_bit(evlist, bit) \
	__perf_evlist__reset_sample_bit(evlist, PERF_SAMPLE_##bit)

139
int perf_evlist__set_filter(struct perf_evlist *evlist, const char *filter);
140
int perf_evlist__set_filter_pid(struct perf_evlist *evlist, pid_t pid);
141
int perf_evlist__set_filter_pids(struct perf_evlist *evlist, size_t npids, pid_t *pids);
142

143 144 145
struct perf_evsel *
perf_evlist__find_tracepoint_by_id(struct perf_evlist *evlist, int id);

146 147 148 149
struct perf_evsel *
perf_evlist__find_tracepoint_by_name(struct perf_evlist *evlist,
				     const char *name);

150 151
void perf_evlist__id_add(struct perf_evlist *evlist, struct perf_evsel *evsel,
			 int cpu, int thread, u64 id);
J
Jiri Olsa 已提交
152 153 154
int perf_evlist__id_add_fd(struct perf_evlist *evlist,
			   struct perf_evsel *evsel,
			   int cpu, int thread, int fd);
155

156 157
int perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd);
int perf_evlist__alloc_pollfd(struct perf_evlist *evlist);
158 159
int perf_evlist__filter_pollfd(struct perf_evlist *evlist, short revents_and_mask);

160 161
int perf_evlist__poll(struct perf_evlist *evlist, int timeout);

162
struct perf_evsel *perf_evlist__id2evsel(struct perf_evlist *evlist, u64 id);
163 164
struct perf_evsel *perf_evlist__id2evsel_strict(struct perf_evlist *evlist,
						u64 id);
165

166 167
struct perf_sample_id *perf_evlist__id2sid(struct perf_evlist *evlist, u64 id);

168 169
void perf_evlist__toggle_bkw_mmap(struct perf_evlist *evlist, enum bkw_mmap_state state);

170 171 172 173 174 175
union perf_event *perf_mmap__read_forward(struct perf_mmap *map, bool check_messup);
union perf_event *perf_mmap__read_backward(struct perf_mmap *map);

void perf_mmap__read_catchup(struct perf_mmap *md);
void perf_mmap__consume(struct perf_mmap *md, bool overwrite);

176
union perf_event *perf_evlist__mmap_read(struct perf_evlist *evlist, int idx);
177

178 179
union perf_event *perf_evlist__mmap_read_forward(struct perf_evlist *evlist,
						 int idx);
180 181 182 183
union perf_event *perf_evlist__mmap_read_backward(struct perf_evlist *evlist,
						  int idx);
void perf_evlist__mmap_read_catchup(struct perf_evlist *evlist, int idx);

184 185
void perf_evlist__mmap_consume(struct perf_evlist *evlist, int idx);

186
int perf_evlist__open(struct perf_evlist *evlist);
187
void perf_evlist__close(struct perf_evlist *evlist);
188

189 190
struct callchain_param;

191 192
void perf_evlist__set_id_pos(struct perf_evlist *evlist);
bool perf_can_sample_identifier(void);
193
bool perf_can_record_switch_events(void);
194
bool perf_can_record_cpu_wide(void);
195 196
void perf_evlist__config(struct perf_evlist *evlist, struct record_opts *opts,
			 struct callchain_param *callchain);
197
int record_opts__config(struct record_opts *opts);
198

199
int perf_evlist__prepare_workload(struct perf_evlist *evlist,
200
				  struct target *target,
201
				  const char *argv[], bool pipe_output,
202 203
				  void (*exec_error)(int signo, siginfo_t *info,
						     void *ucontext));
204 205
int perf_evlist__start_workload(struct perf_evlist *evlist);

206 207
struct option;

208
int __perf_evlist__parse_mmap_pages(unsigned int *mmap_pages, const char *str);
209 210 211 212
int perf_evlist__parse_mmap_pages(const struct option *opt,
				  const char *str,
				  int unset);

213 214
unsigned long perf_event_mlock_kb_in_pages(void);

215 216 217
int perf_evlist__mmap_ex(struct perf_evlist *evlist, unsigned int pages,
			 bool overwrite, unsigned int auxtrace_pages,
			 bool auxtrace_overwrite);
218 219
int perf_evlist__mmap(struct perf_evlist *evlist, unsigned int pages,
		      bool overwrite);
220 221
void perf_evlist__munmap(struct perf_evlist *evlist);

222 223
size_t perf_evlist__mmap_size(unsigned long pages);

224
void perf_evlist__disable(struct perf_evlist *evlist);
225
void perf_evlist__enable(struct perf_evlist *evlist);
226
void perf_evlist__toggle_enable(struct perf_evlist *evlist);
227

228 229
int perf_evlist__enable_event_idx(struct perf_evlist *evlist,
				  struct perf_evsel *evsel, int idx);
230

231 232 233
void perf_evlist__set_selected(struct perf_evlist *evlist,
			       struct perf_evsel *evsel);

234 235
void perf_evlist__set_maps(struct perf_evlist *evlist, struct cpu_map *cpus,
			   struct thread_map *threads);
236
int perf_evlist__create_maps(struct perf_evlist *evlist, struct target *target);
237
int perf_evlist__apply_filters(struct perf_evlist *evlist, struct perf_evsel **err_evsel);
238

239 240 241
void __perf_evlist__set_leader(struct list_head *list);
void perf_evlist__set_leader(struct perf_evlist *evlist);

242
u64 perf_evlist__read_format(struct perf_evlist *evlist);
243 244
u64 __perf_evlist__combined_sample_type(struct perf_evlist *evlist);
u64 perf_evlist__combined_sample_type(struct perf_evlist *evlist);
245
u64 perf_evlist__combined_branch_type(struct perf_evlist *evlist);
246 247
bool perf_evlist__sample_id_all(struct perf_evlist *evlist);
u16 perf_evlist__id_hdr_size(struct perf_evlist *evlist);
248

249
int perf_evlist__parse_sample(struct perf_evlist *evlist, union perf_event *event,
250
			      struct perf_sample *sample);
251

252 253
bool perf_evlist__valid_sample_type(struct perf_evlist *evlist);
bool perf_evlist__valid_sample_id_all(struct perf_evlist *evlist);
254
bool perf_evlist__valid_read_format(struct perf_evlist *evlist);
255 256

void perf_evlist__splice_list_tail(struct perf_evlist *evlist,
257
				   struct list_head *list);
258 259 260 261 262 263 264 265 266 267

static inline struct perf_evsel *perf_evlist__first(struct perf_evlist *evlist)
{
	return list_entry(evlist->entries.next, struct perf_evsel, node);
}

static inline struct perf_evsel *perf_evlist__last(struct perf_evlist *evlist)
{
	return list_entry(evlist->entries.prev, struct perf_evsel, node);
}
268 269

size_t perf_evlist__fprintf(struct perf_evlist *evlist, FILE *fp);
270

271
int perf_evlist__strerror_open(struct perf_evlist *evlist, int err, char *buf, size_t size);
272
int perf_evlist__strerror_mmap(struct perf_evlist *evlist, int err, char *buf, size_t size);
273

274
static inline u64 perf_mmap__read_head(struct perf_mmap *mm)
275 276
{
	struct perf_event_mmap_page *pc = mm->base;
277
	u64 head = ACCESS_ONCE(pc->data_head);
278 279 280 281
	rmb();
	return head;
}

282
static inline void perf_mmap__write_tail(struct perf_mmap *md, u64 tail)
283 284 285 286 287 288
{
	struct perf_event_mmap_page *pc = md->base;

	/*
	 * ensure all reads are done before we write the tail out.
	 */
289
	mb();
290 291 292
	pc->data_tail = tail;
}

293
bool perf_evlist__can_select_event(struct perf_evlist *evlist, const char *str);
294 295 296
void perf_evlist__to_front(struct perf_evlist *evlist,
			   struct perf_evsel *move_evsel);

297
/**
298
 * __evlist__for_each_entry - iterate thru all the evsels
299 300 301
 * @list: list_head instance to iterate
 * @evsel: struct evsel iterator
 */
302
#define __evlist__for_each_entry(list, evsel) \
303 304 305
        list_for_each_entry(evsel, list, node)

/**
306
 * evlist__for_each_entry - iterate thru all the evsels
307 308 309
 * @evlist: evlist instance to iterate
 * @evsel: struct evsel iterator
 */
310 311
#define evlist__for_each_entry(evlist, evsel) \
	__evlist__for_each_entry(&(evlist)->entries, evsel)
312 313

/**
314
 * __evlist__for_each_entry_continue - continue iteration thru all the evsels
315 316 317
 * @list: list_head instance to iterate
 * @evsel: struct evsel iterator
 */
318
#define __evlist__for_each_entry_continue(list, evsel) \
319 320 321
        list_for_each_entry_continue(evsel, list, node)

/**
322
 * evlist__for_each_entry_continue - continue iteration thru all the evsels
323 324 325
 * @evlist: evlist instance to iterate
 * @evsel: struct evsel iterator
 */
326 327
#define evlist__for_each_entry_continue(evlist, evsel) \
	__evlist__for_each_entry_continue(&(evlist)->entries, evsel)
328 329

/**
330
 * __evlist__for_each_entry_reverse - iterate thru all the evsels in reverse order
331 332 333
 * @list: list_head instance to iterate
 * @evsel: struct evsel iterator
 */
334
#define __evlist__for_each_entry_reverse(list, evsel) \
335 336 337
        list_for_each_entry_reverse(evsel, list, node)

/**
338
 * evlist__for_each_entry_reverse - iterate thru all the evsels in reverse order
339 340 341
 * @evlist: evlist instance to iterate
 * @evsel: struct evsel iterator
 */
342 343
#define evlist__for_each_entry_reverse(evlist, evsel) \
	__evlist__for_each_entry_reverse(&(evlist)->entries, evsel)
344 345

/**
346
 * __evlist__for_each_entry_safe - safely iterate thru all the evsels
347 348 349 350
 * @list: list_head instance to iterate
 * @tmp: struct evsel temp iterator
 * @evsel: struct evsel iterator
 */
351
#define __evlist__for_each_entry_safe(list, tmp, evsel) \
352 353 354
        list_for_each_entry_safe(evsel, tmp, list, node)

/**
355
 * evlist__for_each_entry_safe - safely iterate thru all the evsels
356 357 358 359
 * @evlist: evlist instance to iterate
 * @evsel: struct evsel iterator
 * @tmp: struct evsel temp iterator
 */
360 361
#define evlist__for_each_entry_safe(evlist, tmp, evsel) \
	__evlist__for_each_entry_safe(&(evlist)->entries, tmp, evsel)
362

363 364
void perf_evlist__set_tracking_event(struct perf_evlist *evlist,
				     struct perf_evsel *tracking_evsel);
365 366

void perf_event_attr__set_max_precise_ip(struct perf_event_attr *attr);
367 368 369

struct perf_evsel *
perf_evlist__find_evsel_by_str(struct perf_evlist *evlist, const char *str);
J
Jiri Olsa 已提交
370 371 372

struct perf_evsel *perf_evlist__event2evsel(struct perf_evlist *evlist,
					    union perf_event *event);
373
#endif /* __PERF_EVLIST_H */