1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LIBPERF_INTERNAL_EVLIST_H
3 #define __LIBPERF_INTERNAL_EVLIST_H
4 
5 #include <linux/list.h>
6 #include <api/fd/array.h>
7 #include <internal/cpumap.h>
8 #include <internal/evsel.h>
9 
10 #define PERF_EVLIST__HLIST_BITS 8
11 #define PERF_EVLIST__HLIST_SIZE (1 << PERF_EVLIST__HLIST_BITS)
12 
13 struct perf_cpu_map;
14 struct perf_thread_map;
15 struct perf_mmap_param;
16 
17 struct perf_evlist {
18 	struct list_head	 entries;
19 	int			 nr_entries;
20 	int			 nr_groups;
21 	bool			 has_user_cpus;
22 	bool			 needs_map_propagation;
23 	/**
24 	 * The cpus passed from the command line or all online CPUs by
25 	 * default.
26 	 */
27 	struct perf_cpu_map	*user_requested_cpus;
28 	/** The union of all evsel cpu maps. */
29 	struct perf_cpu_map	*all_cpus;
30 	struct perf_thread_map	*threads;
31 	int			 nr_mmaps;
32 	size_t			 mmap_len;
33 	struct fdarray		 pollfd;
34 	struct hlist_head	 heads[PERF_EVLIST__HLIST_SIZE];
35 	struct perf_mmap	*mmap;
36 	struct perf_mmap	*mmap_ovw;
37 	struct perf_mmap	*mmap_first;
38 	struct perf_mmap	*mmap_ovw_first;
39 };
40 
41 typedef void
42 (*perf_evlist_mmap__cb_idx_t)(struct perf_evlist*, struct perf_evsel*,
43 			      struct perf_mmap_param*, int);
44 typedef struct perf_mmap*
45 (*perf_evlist_mmap__cb_get_t)(struct perf_evlist*, bool, int);
46 typedef int
47 (*perf_evlist_mmap__cb_mmap_t)(struct perf_mmap*, struct perf_mmap_param*, int, struct perf_cpu);
48 
49 struct perf_evlist_mmap_ops {
50 	perf_evlist_mmap__cb_idx_t	idx;
51 	perf_evlist_mmap__cb_get_t	get;
52 	perf_evlist_mmap__cb_mmap_t	mmap;
53 };
54 
55 int perf_evlist__alloc_pollfd(struct perf_evlist *evlist);
56 int perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd,
57 			    void *ptr, short revent, enum fdarray_flags flags);
58 
59 int perf_evlist__mmap_ops(struct perf_evlist *evlist,
60 			  struct perf_evlist_mmap_ops *ops,
61 			  struct perf_mmap_param *mp);
62 
63 void perf_evlist__init(struct perf_evlist *evlist);
64 void perf_evlist__exit(struct perf_evlist *evlist);
65 
66 /**
67  * __perf_evlist__for_each_entry - iterate thru all the evsels
68  * @list: list_head instance to iterate
69  * @evsel: struct perf_evsel iterator
70  */
71 #define __perf_evlist__for_each_entry(list, evsel) \
72 	list_for_each_entry(evsel, list, node)
73 
74 /**
75  * evlist__for_each_entry - iterate thru all the evsels
76  * @evlist: perf_evlist instance to iterate
77  * @evsel: struct perf_evsel iterator
78  */
79 #define perf_evlist__for_each_entry(evlist, evsel) \
80 	__perf_evlist__for_each_entry(&(evlist)->entries, evsel)
81 
82 /**
83  * __perf_evlist__for_each_entry_reverse - iterate thru all the evsels in reverse order
84  * @list: list_head instance to iterate
85  * @evsel: struct evsel iterator
86  */
87 #define __perf_evlist__for_each_entry_reverse(list, evsel) \
88 	list_for_each_entry_reverse(evsel, list, node)
89 
90 /**
91  * perf_evlist__for_each_entry_reverse - iterate thru all the evsels in reverse order
92  * @evlist: evlist instance to iterate
93  * @evsel: struct evsel iterator
94  */
95 #define perf_evlist__for_each_entry_reverse(evlist, evsel) \
96 	__perf_evlist__for_each_entry_reverse(&(evlist)->entries, evsel)
97 
98 /**
99  * __perf_evlist__for_each_entry_safe - safely iterate thru all the evsels
100  * @list: list_head instance to iterate
101  * @tmp: struct evsel temp iterator
102  * @evsel: struct evsel iterator
103  */
104 #define __perf_evlist__for_each_entry_safe(list, tmp, evsel) \
105 	list_for_each_entry_safe(evsel, tmp, list, node)
106 
107 /**
108  * perf_evlist__for_each_entry_safe - safely iterate thru all the evsels
109  * @evlist: evlist instance to iterate
110  * @evsel: struct evsel iterator
111  * @tmp: struct evsel temp iterator
112  */
113 #define perf_evlist__for_each_entry_safe(evlist, tmp, evsel) \
114 	__perf_evlist__for_each_entry_safe(&(evlist)->entries, tmp, evsel)
115 
116 static inline struct perf_evsel *perf_evlist__first(struct perf_evlist *evlist)
117 {
118 	return list_entry(evlist->entries.next, struct perf_evsel, node);
119 }
120 
121 static inline struct perf_evsel *perf_evlist__last(struct perf_evlist *evlist)
122 {
123 	return list_entry(evlist->entries.prev, struct perf_evsel, node);
124 }
125 
126 u64 perf_evlist__read_format(struct perf_evlist *evlist);
127 
128 void perf_evlist__id_add(struct perf_evlist *evlist,
129 			 struct perf_evsel *evsel,
130 			 int cpu, int thread, u64 id);
131 
132 int perf_evlist__id_add_fd(struct perf_evlist *evlist,
133 			   struct perf_evsel *evsel,
134 			   int cpu, int thread, int fd);
135 
136 void perf_evlist__reset_id_hash(struct perf_evlist *evlist);
137 
138 void __perf_evlist__set_leader(struct list_head *list, struct perf_evsel *leader);
139 #endif /* __LIBPERF_INTERNAL_EVLIST_H */
140