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