xref: /linux-6.15/kernel/trace/tracing_map.h (revision 8d44f2f3)
1 #ifndef __TRACING_MAP_H
2 #define __TRACING_MAP_H
3 
4 #define TRACING_MAP_BITS_DEFAULT	11
5 #define TRACING_MAP_BITS_MAX		17
6 #define TRACING_MAP_BITS_MIN		7
7 
8 #define TRACING_MAP_FIELDS_MAX		4
9 #define TRACING_MAP_KEYS_MAX		2
10 
11 #define TRACING_MAP_SORT_KEYS_MAX	2
12 
13 typedef int (*tracing_map_cmp_fn_t) (void *val_a, void *val_b);
14 
15 /*
16  * This is an overview of the tracing_map data structures and how they
17  * relate to the tracing_map API.  The details of the algorithms
18  * aren't discussed here - this is just a general overview of the data
19  * structures and how they interact with the API.
20  *
21  * The central data structure of the tracing_map is an initially
22  * zeroed array of struct tracing_map_entry (stored in the map field
23  * of struct tracing_map).  tracing_map_entry is a very simple data
24  * structure containing only two fields: a 32-bit unsigned 'key'
25  * variable and a pointer named 'val'.  This array of struct
26  * tracing_map_entry is essentially a hash table which will be
27  * modified by a single function, tracing_map_insert(), but which can
28  * be traversed and read by a user at any time (though the user does
29  * this indirectly via an array of tracing_map_sort_entry - see the
30  * explanation of that data structure in the discussion of the
31  * sorting-related data structures below).
32  *
33  * The central function of the tracing_map API is
34  * tracing_map_insert().  tracing_map_insert() hashes the
35  * arbitrarily-sized key passed into it into a 32-bit unsigned key.
36  * It then uses this key, truncated to the array size, as an index
37  * into the array of tracing_map_entries.  If the value of the 'key'
38  * field of the tracing_map_entry found at that location is 0, then
39  * that entry is considered to be free and can be claimed, by
40  * replacing the 0 in the 'key' field of the tracing_map_entry with
41  * the new 32-bit hashed key.  Once claimed, that tracing_map_entry's
42  * 'val' field is then used to store a unique element which will be
43  * forever associated with that 32-bit hashed key in the
44  * tracing_map_entry.
45  *
46  * That unique element now in the tracing_map_entry's 'val' field is
47  * an instance of tracing_map_elt, where 'elt' in the latter part of
48  * that variable name is short for 'element'.  The purpose of a
49  * tracing_map_elt is to hold values specific to the particular
50  * 32-bit hashed key it's assocated with.  Things such as the unique
51  * set of aggregated sums associated with the 32-bit hashed key, along
52  * with a copy of the full key associated with the entry, and which
53  * was used to produce the 32-bit hashed key.
54  *
55  * When tracing_map_create() is called to create the tracing map, the
56  * user specifies (indirectly via the map_bits param, the details are
57  * unimportant for this discussion) the maximum number of elements
58  * that the map can hold (stored in the max_elts field of struct
59  * tracing_map).  This is the maximum possible number of
60  * tracing_map_entries in the tracing_map_entry array which can be
61  * 'claimed' as described in the above discussion, and therefore is
62  * also the maximum number of tracing_map_elts that can be associated
63  * with the tracing_map_entry array in the tracing_map.  Because of
64  * the way the insertion algorithm works, the size of the allocated
65  * tracing_map_entry array is always twice the maximum number of
66  * elements (2 * max_elts).  This value is stored in the map_size
67  * field of struct tracing_map.
68  *
69  * Because tracing_map_insert() needs to work from any context,
70  * including from within the memory allocation functions themselves,
71  * both the tracing_map_entry array and a pool of max_elts
72  * tracing_map_elts are pre-allocated before any call is made to
73  * tracing_map_insert().
74  *
75  * The tracing_map_entry array is allocated as a single block by
76  * tracing_map_create().
77  *
78  * Because the tracing_map_elts are much larger objects and can't
79  * generally be allocated together as a single large array without
80  * failure, they're allocated individually, by tracing_map_init().
81  *
82  * The pool of tracing_map_elts are allocated by tracing_map_init()
83  * rather than by tracing_map_create() because at the time
84  * tracing_map_create() is called, there isn't enough information to
85  * create the tracing_map_elts.  Specifically,the user first needs to
86  * tell the tracing_map implementation how many fields the
87  * tracing_map_elts contain, and which types of fields they are (key
88  * or sum).  The user does this via the tracing_map_add_sum_field()
89  * and tracing_map_add_key_field() functions, following which the user
90  * calls tracing_map_init() to finish up the tracing map setup.  The
91  * array holding the pointers which make up the pre-allocated pool of
92  * tracing_map_elts is allocated as a single block and is stored in
93  * the elts field of struct tracing_map.
94  *
95  * There is also a set of structures used for sorting that might
96  * benefit from some minimal explanation.
97  *
98  * struct tracing_map_sort_key is used to drive the sort at any given
99  * time.  By 'any given time' we mean that a different
100  * tracing_map_sort_key will be used at different times depending on
101  * whether the sort currently being performed is a primary or a
102  * secondary sort.
103  *
104  * The sort key is very simple, consisting of the field index of the
105  * tracing_map_elt field to sort on (which the user saved when adding
106  * the field), and whether the sort should be done in an ascending or
107  * descending order.
108  *
109  * For the convenience of the sorting code, a tracing_map_sort_entry
110  * is created for each tracing_map_elt, again individually allocated
111  * to avoid failures that might be expected if allocated as a single
112  * large array of struct tracing_map_sort_entry.
113  * tracing_map_sort_entry instances are the objects expected by the
114  * various internal sorting functions, and are also what the user
115  * ultimately receives after calling tracing_map_sort_entries().
116  * Because it doesn't make sense for users to access an unordered and
117  * sparsely populated tracing_map directly, the
118  * tracing_map_sort_entries() function is provided so that users can
119  * retrieve a sorted list of all existing elements.  In addition to
120  * the associated tracing_map_elt 'elt' field contained within the
121  * tracing_map_sort_entry, which is the object of interest to the
122  * user, tracing_map_sort_entry objects contain a number of additional
123  * fields which are used for caching and internal purposes and can
124  * safely be ignored.
125 */
126 
127 struct tracing_map_field {
128 	tracing_map_cmp_fn_t		cmp_fn;
129 	union {
130 		atomic64_t			sum;
131 		unsigned int			offset;
132 	};
133 };
134 
135 struct tracing_map_elt {
136 	struct tracing_map		*map;
137 	struct tracing_map_field	*fields;
138 	void				*key;
139 	void				*private_data;
140 };
141 
142 struct tracing_map_entry {
143 	u32				key;
144 	struct tracing_map_elt		*val;
145 };
146 
147 struct tracing_map_sort_key {
148 	unsigned int			field_idx;
149 	bool				descending;
150 };
151 
152 struct tracing_map_sort_entry {
153 	void				*key;
154 	struct tracing_map_elt		*elt;
155 	bool				elt_copied;
156 	bool				dup;
157 };
158 
159 struct tracing_map_array {
160 	unsigned int entries_per_page;
161 	unsigned int entry_size_shift;
162 	unsigned int entry_shift;
163 	unsigned int entry_mask;
164 	unsigned int n_pages;
165 	void **pages;
166 };
167 
168 #define TRACING_MAP_ARRAY_ELT(array, idx)				\
169 	(array->pages[idx >> array->entry_shift] +			\
170 	 ((idx & array->entry_mask) << array->entry_size_shift))
171 
172 #define TRACING_MAP_ENTRY(array, idx)					\
173 	((struct tracing_map_entry *)TRACING_MAP_ARRAY_ELT(array, idx))
174 
175 #define TRACING_MAP_ELT(array, idx)					\
176 	((struct tracing_map_elt **)TRACING_MAP_ARRAY_ELT(array, idx))
177 
178 struct tracing_map {
179 	unsigned int			key_size;
180 	unsigned int			map_bits;
181 	unsigned int			map_size;
182 	unsigned int			max_elts;
183 	atomic_t			next_elt;
184 	struct tracing_map_array	*elts;
185 	struct tracing_map_array	*map;
186 	const struct tracing_map_ops	*ops;
187 	void				*private_data;
188 	struct tracing_map_field	fields[TRACING_MAP_FIELDS_MAX];
189 	unsigned int			n_fields;
190 	int				key_idx[TRACING_MAP_KEYS_MAX];
191 	unsigned int			n_keys;
192 	struct tracing_map_sort_key	sort_key;
193 	atomic64_t			hits;
194 	atomic64_t			drops;
195 };
196 
197 /**
198  * struct tracing_map_ops - callbacks for tracing_map
199  *
200  * The methods in this structure define callback functions for various
201  * operations on a tracing_map or objects related to a tracing_map.
202  *
203  * For a detailed description of tracing_map_elt objects please see
204  * the overview of tracing_map data structures at the beginning of
205  * this file.
206  *
207  * All the methods below are optional.
208  *
209  * @elt_alloc: When a tracing_map_elt is allocated, this function, if
210  *	defined, will be called and gives clients the opportunity to
211  *	allocate additional data and attach it to the element
212  *	(tracing_map_elt->private_data is meant for that purpose).
213  *	Element allocation occurs before tracing begins, when the
214  *	tracing_map_init() call is made by client code.
215  *
216  * @elt_copy: At certain points in the lifetime of an element, it may
217  *	need to be copied.  The copy should include a copy of the
218  *	client-allocated data, which can be copied into the 'to'
219  *	element from the 'from' element.
220  *
221  * @elt_free: When a tracing_map_elt is freed, this function is called
222  *	and allows client-allocated per-element data to be freed.
223  *
224  * @elt_clear: This callback allows per-element client-defined data to
225  *	be cleared, if applicable.
226  *
227  * @elt_init: This callback allows per-element client-defined data to
228  *	be initialized when used i.e. when the element is actually
229  *	claimed by tracing_map_insert() in the context of the map
230  *	insertion.
231  */
232 struct tracing_map_ops {
233 	int			(*elt_alloc)(struct tracing_map_elt *elt);
234 	void			(*elt_copy)(struct tracing_map_elt *to,
235 					    struct tracing_map_elt *from);
236 	void			(*elt_free)(struct tracing_map_elt *elt);
237 	void			(*elt_clear)(struct tracing_map_elt *elt);
238 	void			(*elt_init)(struct tracing_map_elt *elt);
239 };
240 
241 extern struct tracing_map *
242 tracing_map_create(unsigned int map_bits,
243 		   unsigned int key_size,
244 		   const struct tracing_map_ops *ops,
245 		   void *private_data);
246 extern int tracing_map_init(struct tracing_map *map);
247 
248 extern int tracing_map_add_sum_field(struct tracing_map *map);
249 extern int tracing_map_add_key_field(struct tracing_map *map,
250 				     unsigned int offset,
251 				     tracing_map_cmp_fn_t cmp_fn);
252 
253 extern void tracing_map_destroy(struct tracing_map *map);
254 extern void tracing_map_clear(struct tracing_map *map);
255 
256 extern struct tracing_map_elt *
257 tracing_map_insert(struct tracing_map *map, void *key);
258 extern struct tracing_map_elt *
259 tracing_map_lookup(struct tracing_map *map, void *key);
260 
261 extern tracing_map_cmp_fn_t tracing_map_cmp_num(int field_size,
262 						int field_is_signed);
263 extern int tracing_map_cmp_string(void *val_a, void *val_b);
264 extern int tracing_map_cmp_none(void *val_a, void *val_b);
265 
266 extern void tracing_map_update_sum(struct tracing_map_elt *elt,
267 				   unsigned int i, u64 n);
268 extern u64 tracing_map_read_sum(struct tracing_map_elt *elt, unsigned int i);
269 extern void tracing_map_set_field_descr(struct tracing_map *map,
270 					unsigned int i,
271 					unsigned int key_offset,
272 					tracing_map_cmp_fn_t cmp_fn);
273 extern int
274 tracing_map_sort_entries(struct tracing_map *map,
275 			 struct tracing_map_sort_key *sort_keys,
276 			 unsigned int n_sort_keys,
277 			 struct tracing_map_sort_entry ***sort_entries);
278 
279 extern void
280 tracing_map_destroy_sort_entries(struct tracing_map_sort_entry **entries,
281 				 unsigned int n_entries);
282 #endif /* __TRACING_MAP_H */
283