tracing_map.h 11 KB

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