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