tracing_map.h 11 KB

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