1215976Sjmallett// SPDX-License-Identifier: GPL-2.0 2232812Sjmallett#ifndef __TRACING_MAP_H 3215976Sjmallett#define __TRACING_MAP_H 4215976Sjmallett 5215976Sjmallett#define TRACING_MAP_BITS_DEFAULT 11 6215976Sjmallett#define TRACING_MAP_BITS_MAX 17 7215976Sjmallett#define TRACING_MAP_BITS_MIN 7 8215976Sjmallett 9215976Sjmallett#define TRACING_MAP_KEYS_MAX 3 10215976Sjmallett#define TRACING_MAP_VALS_MAX 3 11215976Sjmallett#define TRACING_MAP_FIELDS_MAX (TRACING_MAP_KEYS_MAX + \ 12215976Sjmallett TRACING_MAP_VALS_MAX) 13215976Sjmallett#define TRACING_MAP_VARS_MAX 16 14215976Sjmallett#define TRACING_MAP_SORT_KEYS_MAX 2 15215976Sjmallett 16215976Sjmalletttypedef int (*tracing_map_cmp_fn_t) (void *val_a, void *val_b); 17215976Sjmallett 18232812Sjmallett/* 19215976Sjmallett * This is an overview of the tracing_map data structures and how they 20215976Sjmallett * relate to the tracing_map API. The details of the algorithms 21215976Sjmallett * aren't discussed here - this is just a general overview of the data 22215976Sjmallett * structures and how they interact with the API. 23215976Sjmallett * 24215976Sjmallett * The central data structure of the tracing_map is an initially 25215976Sjmallett * zeroed array of struct tracing_map_entry (stored in the map field 26215976Sjmallett * of struct tracing_map). tracing_map_entry is a very simple data 27215976Sjmallett * structure containing only two fields: a 32-bit unsigned 'key' 28215976Sjmallett * variable and a pointer named 'val'. This array of struct 29232812Sjmallett * tracing_map_entry is essentially a hash table which will be 30215976Sjmallett * modified by a single function, tracing_map_insert(), but which can 31215976Sjmallett * be traversed and read by a user at any time (though the user does 32215976Sjmallett * this indirectly via an array of tracing_map_sort_entry - see the 33215976Sjmallett * explanation of that data structure in the discussion of the 34215976Sjmallett * sorting-related data structures below). 35215976Sjmallett * 36215976Sjmallett * The central function of the tracing_map API is 37215976Sjmallett * tracing_map_insert(). tracing_map_insert() hashes the 38215976Sjmallett * arbitrarily-sized key passed into it into a 32-bit unsigned key. 39215976Sjmallett * It then uses this key, truncated to the array size, as an index 40215976Sjmallett * into the array of tracing_map_entries. If the value of the 'key' 41215976Sjmallett * field of the tracing_map_entry found at that location is 0, then 42215976Sjmallett * that entry is considered to be free and can be claimed, by 43215976Sjmallett * replacing the 0 in the 'key' field of the tracing_map_entry with 44215976Sjmallett * the new 32-bit hashed key. Once claimed, that tracing_map_entry's 45215976Sjmallett * 'val' field is then used to store a unique element which will be 46215976Sjmallett * forever associated with that 32-bit hashed key in the 47215976Sjmallett * tracing_map_entry. 48215976Sjmallett * 49215976Sjmallett * That unique element now in the tracing_map_entry's 'val' field is 50215976Sjmallett * an instance of tracing_map_elt, where 'elt' in the latter part of 51215976Sjmallett * that variable name is short for 'element'. The purpose of a 52215976Sjmallett * tracing_map_elt is to hold values specific to the particular 53215976Sjmallett * 32-bit hashed key it's associated with. Things such as the unique 54215976Sjmallett * set of aggregated sums associated with the 32-bit hashed key, along 55215976Sjmallett * with a copy of the full key associated with the entry, and which 56215976Sjmallett * was used to produce the 32-bit hashed key. 57215976Sjmallett * 58215976Sjmallett * When tracing_map_create() is called to create the tracing map, the 59215976Sjmallett * user specifies (indirectly via the map_bits param, the details are 60215976Sjmallett * unimportant for this discussion) the maximum number of elements 61215976Sjmallett * that the map can hold (stored in the max_elts field of struct 62215976Sjmallett * tracing_map). This is the maximum possible number of 63215976Sjmallett * tracing_map_entries in the tracing_map_entry array which can be 64215976Sjmallett * 'claimed' as described in the above discussion, and therefore is 65215976Sjmallett * also the maximum number of tracing_map_elts that can be associated 66215976Sjmallett * with the tracing_map_entry array in the tracing_map. Because of 67215976Sjmallett * the way the insertion algorithm works, the size of the allocated 68215976Sjmallett * tracing_map_entry array is always twice the maximum number of 69215976Sjmallett * elements (2 * max_elts). This value is stored in the map_size 70215976Sjmallett * field of struct tracing_map. 71215976Sjmallett * 72215976Sjmallett * Because tracing_map_insert() needs to work from any context, 73215976Sjmallett * including from within the memory allocation functions themselves, 74215976Sjmallett * both the tracing_map_entry array and a pool of max_elts 75215976Sjmallett * tracing_map_elts are pre-allocated before any call is made to 76215976Sjmallett * tracing_map_insert(). 77215976Sjmallett * 78215976Sjmallett * The tracing_map_entry array is allocated as a single block by 79215976Sjmallett * tracing_map_create(). 80215976Sjmallett * 81215976Sjmallett * Because the tracing_map_elts are much larger objects and can't 82215976Sjmallett * generally be allocated together as a single large array without 83215976Sjmallett * failure, they're allocated individually, by tracing_map_init(). 84215976Sjmallett * 85215976Sjmallett * The pool of tracing_map_elts are allocated by tracing_map_init() 86215976Sjmallett * rather than by tracing_map_create() because at the time 87215976Sjmallett * tracing_map_create() is called, there isn't enough information to 88215976Sjmallett * create the tracing_map_elts. Specifically,the user first needs to 89215976Sjmallett * tell the tracing_map implementation how many fields the 90215976Sjmallett * tracing_map_elts contain, and which types of fields they are (key 91215976Sjmallett * or sum). The user does this via the tracing_map_add_sum_field() 92215976Sjmallett * and tracing_map_add_key_field() functions, following which the user 93215976Sjmallett * calls tracing_map_init() to finish up the tracing map setup. The 94215976Sjmallett * array holding the pointers which make up the pre-allocated pool of 95215976Sjmallett * tracing_map_elts is allocated as a single block and is stored in 96215976Sjmallett * the elts field of struct tracing_map. 97215976Sjmallett * 98215976Sjmallett * There is also a set of structures used for sorting that might 99215976Sjmallett * benefit from some minimal explanation. 100215976Sjmallett * 101215976Sjmallett * struct tracing_map_sort_key is used to drive the sort at any given 102232812Sjmallett * time. By 'any given time' we mean that a different 103232812Sjmallett * tracing_map_sort_key will be used at different times depending on 104215976Sjmallett * whether the sort currently being performed is a primary or a 105215976Sjmallett * secondary sort. 106215976Sjmallett * 107215976Sjmallett * The sort key is very simple, consisting of the field index of the 108215976Sjmallett * tracing_map_elt field to sort on (which the user saved when adding 109215976Sjmallett * the field), and whether the sort should be done in an ascending or 110215976Sjmallett * descending order. 111215976Sjmallett * 112215976Sjmallett * For the convenience of the sorting code, a tracing_map_sort_entry 113215976Sjmallett * is created for each tracing_map_elt, again individually allocated 114215976Sjmallett * to avoid failures that might be expected if allocated as a single 115215976Sjmallett * large array of struct tracing_map_sort_entry. 116215976Sjmallett * tracing_map_sort_entry instances are the objects expected by the 117215976Sjmallett * various internal sorting functions, and are also what the user 118232812Sjmallett * ultimately receives after calling tracing_map_sort_entries(). 119215976Sjmallett * Because it doesn't make sense for users to access an unordered and 120215976Sjmallett * sparsely populated tracing_map directly, the 121215976Sjmallett * tracing_map_sort_entries() function is provided so that users can 122215976Sjmallett * retrieve a sorted list of all existing elements. In addition to 123215976Sjmallett * the associated tracing_map_elt 'elt' field contained within the 124215976Sjmallett * tracing_map_sort_entry, which is the object of interest to the 125215976Sjmallett * user, tracing_map_sort_entry objects contain a number of additional 126215976Sjmallett * fields which are used for caching and internal purposes and can 127215976Sjmallett * safely be ignored. 128215976Sjmallett*/ 129215976Sjmallett 130215976Sjmallettstruct tracing_map_field { 131215976Sjmallett tracing_map_cmp_fn_t cmp_fn; 132215976Sjmallett union { 133215976Sjmallett atomic64_t sum; 134215976Sjmallett unsigned int offset; 135215976Sjmallett }; 136215976Sjmallett}; 137215976Sjmallett 138215976Sjmallettstruct tracing_map_elt { 139215976Sjmallett struct tracing_map *map; 140215976Sjmallett struct tracing_map_field *fields; 141215976Sjmallett atomic64_t *vars; 142215976Sjmallett bool *var_set; 143215976Sjmallett void *key; 144215976Sjmallett void *private_data; 145215976Sjmallett}; 146215976Sjmallett 147215976Sjmallettstruct tracing_map_entry { 148215976Sjmallett u32 key; 149215976Sjmallett struct tracing_map_elt *val; 150215976Sjmallett}; 151215976Sjmallett 152215976Sjmallettstruct tracing_map_sort_key { 153215976Sjmallett unsigned int field_idx; 154215976Sjmallett bool descending; 155215976Sjmallett}; 156215976Sjmallett 157215976Sjmallettstruct tracing_map_sort_entry { 158215976Sjmallett void *key; 159215976Sjmallett struct tracing_map_elt *elt; 160215976Sjmallett bool elt_copied; 161215976Sjmallett bool dup; 162215976Sjmallett}; 163215976Sjmallett 164215976Sjmallettstruct tracing_map_array { 165215976Sjmallett unsigned int entries_per_page; 166215976Sjmallett unsigned int entry_size_shift; 167215976Sjmallett unsigned int entry_shift; 168215976Sjmallett unsigned int entry_mask; 169215976Sjmallett unsigned int n_pages; 170215976Sjmallett void **pages; 171215976Sjmallett}; 172215976Sjmallett 173215976Sjmallett#define TRACING_MAP_ARRAY_ELT(array, idx) \ 174215976Sjmallett (array->pages[idx >> array->entry_shift] + \ 175215976Sjmallett ((idx & array->entry_mask) << array->entry_size_shift)) 176215976Sjmallett 177215976Sjmallett#define TRACING_MAP_ENTRY(array, idx) \ 178215976Sjmallett ((struct tracing_map_entry *)TRACING_MAP_ARRAY_ELT(array, idx)) 179215976Sjmallett 180215976Sjmallett#define TRACING_MAP_ELT(array, idx) \ 181215976Sjmallett ((struct tracing_map_elt **)TRACING_MAP_ARRAY_ELT(array, idx)) 182215976Sjmallett 183215976Sjmallettstruct tracing_map { 184215976Sjmallett unsigned int key_size; 185215976Sjmallett unsigned int map_bits; 186232812Sjmallett unsigned int map_size; 187215976Sjmallett unsigned int max_elts; 188215976Sjmallett atomic_t next_elt; 189215976Sjmallett struct tracing_map_array *elts; 190215976Sjmallett struct tracing_map_array *map; 191215976Sjmallett const struct tracing_map_ops *ops; 192215976Sjmallett void *private_data; 193215976Sjmallett struct tracing_map_field fields[TRACING_MAP_FIELDS_MAX]; 194215976Sjmallett unsigned int n_fields; 195215976Sjmallett int key_idx[TRACING_MAP_KEYS_MAX]; 196215976Sjmallett unsigned int n_keys; 197215976Sjmallett struct tracing_map_sort_key sort_key; 198215976Sjmallett unsigned int n_vars; 199215976Sjmallett atomic64_t hits; 200215976Sjmallett atomic64_t drops; 201215976Sjmallett}; 202215976Sjmallett 203215976Sjmallett/** 204215976Sjmallett * struct tracing_map_ops - callbacks for tracing_map 205215976Sjmallett * 206215976Sjmallett * The methods in this structure define callback functions for various 207215976Sjmallett * operations on a tracing_map or objects related to a tracing_map. 208215976Sjmallett * 209215976Sjmallett * For a detailed description of tracing_map_elt objects please see 210215976Sjmallett * the overview of tracing_map data structures at the beginning of 211215976Sjmallett * this file. 212215976Sjmallett * 213215976Sjmallett * All the methods below are optional. 214215976Sjmallett * 215215976Sjmallett * @elt_alloc: When a tracing_map_elt is allocated, this function, if 216215976Sjmallett * defined, will be called and gives clients the opportunity to 217215976Sjmallett * allocate additional data and attach it to the element 218215976Sjmallett * (tracing_map_elt->private_data is meant for that purpose). 219215976Sjmallett * Element allocation occurs before tracing begins, when the 220215976Sjmallett * tracing_map_init() call is made by client code. 221215976Sjmallett * 222215976Sjmallett * @elt_free: When a tracing_map_elt is freed, this function is called 223215976Sjmallett * and allows client-allocated per-element data to be freed. 224215976Sjmallett * 225215976Sjmallett * @elt_clear: This callback allows per-element client-defined data to 226215976Sjmallett * be cleared, if applicable. 227215976Sjmallett * 228215976Sjmallett * @elt_init: This callback allows per-element client-defined data to 229215976Sjmallett * be initialized when used i.e. when the element is actually 230215976Sjmallett * claimed by tracing_map_insert() in the context of the map 231215976Sjmallett * insertion. 232215976Sjmallett */ 233215976Sjmallettstruct tracing_map_ops { 234215976Sjmallett int (*elt_alloc)(struct tracing_map_elt *elt); 235215976Sjmallett void (*elt_free)(struct tracing_map_elt *elt); 236215976Sjmallett void (*elt_clear)(struct tracing_map_elt *elt); 237215976Sjmallett void (*elt_init)(struct tracing_map_elt *elt); 238215976Sjmallett}; 239215976Sjmallett 240215976Sjmallettextern struct tracing_map * 241215976Sjmalletttracing_map_create(unsigned int map_bits, 242215976Sjmallett unsigned int key_size, 243215976Sjmallett const struct tracing_map_ops *ops, 244215976Sjmallett void *private_data); 245215976Sjmallettextern int tracing_map_init(struct tracing_map *map); 246215976Sjmallett 247215976Sjmallettextern int tracing_map_add_sum_field(struct tracing_map *map); 248215976Sjmallettextern int tracing_map_add_var(struct tracing_map *map); 249215976Sjmallettextern int tracing_map_add_key_field(struct tracing_map *map, 250215976Sjmallett unsigned int offset, 251215976Sjmallett tracing_map_cmp_fn_t cmp_fn); 252215976Sjmallett 253215976Sjmallettextern void tracing_map_destroy(struct tracing_map *map); 254215976Sjmallettextern void tracing_map_clear(struct tracing_map *map); 255215976Sjmallett 256215976Sjmallettextern struct tracing_map_elt * 257215976Sjmalletttracing_map_insert(struct tracing_map *map, void *key); 258215976Sjmallettextern struct tracing_map_elt * 259215976Sjmalletttracing_map_lookup(struct tracing_map *map, void *key); 260215976Sjmallett 261215976Sjmallettextern tracing_map_cmp_fn_t tracing_map_cmp_num(int field_size, 262215976Sjmallett int field_is_signed); 263215976Sjmallettextern int tracing_map_cmp_string(void *val_a, void *val_b); 264215976Sjmallettextern int tracing_map_cmp_none(void *val_a, void *val_b); 265215976Sjmallett 266215976Sjmallettextern void tracing_map_update_sum(struct tracing_map_elt *elt, 267215976Sjmallett unsigned int i, u64 n); 268215976Sjmallettextern void tracing_map_set_var(struct tracing_map_elt *elt, 269215976Sjmallett unsigned int i, u64 n); 270215976Sjmallettextern bool tracing_map_var_set(struct tracing_map_elt *elt, unsigned int i); 271215976Sjmallettextern u64 tracing_map_read_sum(struct tracing_map_elt *elt, unsigned int i); 272215976Sjmallettextern u64 tracing_map_read_var(struct tracing_map_elt *elt, unsigned int i); 273215976Sjmallettextern u64 tracing_map_read_var_once(struct tracing_map_elt *elt, unsigned int i); 274215976Sjmallett 275215976Sjmallettextern int 276215976Sjmalletttracing_map_sort_entries(struct tracing_map *map, 277215976Sjmallett struct tracing_map_sort_key *sort_keys, 278215976Sjmallett unsigned int n_sort_keys, 279215976Sjmallett struct tracing_map_sort_entry ***sort_entries); 280215976Sjmallett 281215976Sjmallettextern void 282215976Sjmalletttracing_map_destroy_sort_entries(struct tracing_map_sort_entry **entries, 283215976Sjmallett unsigned int n_entries); 284215976Sjmallett#endif /* __TRACING_MAP_H */ 285215976Sjmallett