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