2 * tracing_map - lock-free map for tracing
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * Copyright (C) 2015 Tom Zanussi <tom.zanussi@linux.intel.com>
16 * tracing_map implementation inspired by lock-free map algorithms
17 * originated by Dr. Cliff Click:
19 * http://www.azulsystems.com/blog/cliff/2007-03-26-non-blocking-hashtable
20 * http://www.azulsystems.com/events/javaone_2007/2007_LockFreeHash.pdf
23 #include <linux/vmalloc.h>
24 #include <linux/jhash.h>
25 #include <linux/slab.h>
26 #include <linux/sort.h>
28 #include "tracing_map.h"
32 * NOTE: For a detailed description of the data structures used by
33 * these functions (such as tracing_map_elt) please see the overview
34 * of tracing_map data structures at the beginning of tracing_map.h.
38 * tracing_map_update_sum - Add a value to a tracing_map_elt's sum field
39 * @elt: The tracing_map_elt
40 * @i: The index of the given sum associated with the tracing_map_elt
41 * @n: The value to add to the sum
43 * Add n to sum i associated with the specified tracing_map_elt
44 * instance. The index i is the index returned by the call to
45 * tracing_map_add_sum_field() when the tracing map was set up.
47 void tracing_map_update_sum(struct tracing_map_elt
*elt
, unsigned int i
, u64 n
)
49 atomic64_add(n
, &elt
->fields
[i
].sum
);
53 * tracing_map_read_sum - Return the value of a tracing_map_elt's sum field
54 * @elt: The tracing_map_elt
55 * @i: The index of the given sum associated with the tracing_map_elt
57 * Retrieve the value of the sum i associated with the specified
58 * tracing_map_elt instance. The index i is the index returned by the
59 * call to tracing_map_add_sum_field() when the tracing map was set
62 * Return: The sum associated with field i for elt.
64 u64
tracing_map_read_sum(struct tracing_map_elt
*elt
, unsigned int i
)
66 return (u64
)atomic64_read(&elt
->fields
[i
].sum
);
69 int tracing_map_cmp_string(void *val_a
, void *val_b
)
77 int tracing_map_cmp_none(void *val_a
, void *val_b
)
82 static int tracing_map_cmp_atomic64(void *val_a
, void *val_b
)
84 u64 a
= atomic64_read((atomic64_t
*)val_a
);
85 u64 b
= atomic64_read((atomic64_t
*)val_b
);
87 return (a
> b
) ? 1 : ((a
< b
) ? -1 : 0);
90 #define DEFINE_TRACING_MAP_CMP_FN(type) \
91 static int tracing_map_cmp_##type(void *val_a, void *val_b) \
93 type a = *(type *)val_a; \
94 type b = *(type *)val_b; \
96 return (a > b) ? 1 : ((a < b) ? -1 : 0); \
99 DEFINE_TRACING_MAP_CMP_FN(s64
);
100 DEFINE_TRACING_MAP_CMP_FN(u64
);
101 DEFINE_TRACING_MAP_CMP_FN(s32
);
102 DEFINE_TRACING_MAP_CMP_FN(u32
);
103 DEFINE_TRACING_MAP_CMP_FN(s16
);
104 DEFINE_TRACING_MAP_CMP_FN(u16
);
105 DEFINE_TRACING_MAP_CMP_FN(s8
);
106 DEFINE_TRACING_MAP_CMP_FN(u8
);
108 tracing_map_cmp_fn_t
tracing_map_cmp_num(int field_size
,
111 tracing_map_cmp_fn_t fn
= tracing_map_cmp_none
;
113 switch (field_size
) {
116 fn
= tracing_map_cmp_s64
;
118 fn
= tracing_map_cmp_u64
;
122 fn
= tracing_map_cmp_s32
;
124 fn
= tracing_map_cmp_u32
;
128 fn
= tracing_map_cmp_s16
;
130 fn
= tracing_map_cmp_u16
;
134 fn
= tracing_map_cmp_s8
;
136 fn
= tracing_map_cmp_u8
;
143 static int tracing_map_add_field(struct tracing_map
*map
,
144 tracing_map_cmp_fn_t cmp_fn
)
148 if (map
->n_fields
< TRACING_MAP_FIELDS_MAX
) {
150 map
->fields
[map
->n_fields
++].cmp_fn
= cmp_fn
;
157 * tracing_map_add_sum_field - Add a field describing a tracing_map sum
158 * @map: The tracing_map
160 * Add a sum field to the key and return the index identifying it in
161 * the map and associated tracing_map_elts. This is the index used
162 * for instance to update a sum for a particular tracing_map_elt using
163 * tracing_map_update_sum() or reading it via tracing_map_read_sum().
165 * Return: The index identifying the field in the map and associated
166 * tracing_map_elts, or -EINVAL on error.
168 int tracing_map_add_sum_field(struct tracing_map
*map
)
170 return tracing_map_add_field(map
, tracing_map_cmp_atomic64
);
174 * tracing_map_add_key_field - Add a field describing a tracing_map key
175 * @map: The tracing_map
176 * @offset: The offset within the key
177 * @cmp_fn: The comparison function that will be used to sort on the key
179 * Let the map know there is a key and that if it's used as a sort key
182 * A key can be a subset of a compound key; for that purpose, the
183 * offset param is used to describe where within the the compound key
184 * the key referenced by this key field resides.
186 * Return: The index identifying the field in the map and associated
187 * tracing_map_elts, or -EINVAL on error.
189 int tracing_map_add_key_field(struct tracing_map
*map
,
191 tracing_map_cmp_fn_t cmp_fn
)
194 int idx
= tracing_map_add_field(map
, cmp_fn
);
199 map
->fields
[idx
].offset
= offset
;
201 map
->key_idx
[map
->n_keys
++] = idx
;
206 void tracing_map_array_clear(struct tracing_map_array
*a
)
213 for (i
= 0; i
< a
->n_pages
; i
++)
214 memset(a
->pages
[i
], 0, PAGE_SIZE
);
217 void tracing_map_array_free(struct tracing_map_array
*a
)
227 for (i
= 0; i
< a
->n_pages
; i
++) {
230 free_page((unsigned long)a
->pages
[i
]);
239 struct tracing_map_array
*tracing_map_array_alloc(unsigned int n_elts
,
240 unsigned int entry_size
)
242 struct tracing_map_array
*a
;
245 a
= kzalloc(sizeof(*a
), GFP_KERNEL
);
249 a
->entry_size_shift
= fls(roundup_pow_of_two(entry_size
) - 1);
250 a
->entries_per_page
= PAGE_SIZE
/ (1 << a
->entry_size_shift
);
251 a
->n_pages
= n_elts
/ a
->entries_per_page
;
254 a
->entry_shift
= fls(a
->entries_per_page
) - 1;
255 a
->entry_mask
= (1 << a
->entry_shift
) - 1;
257 a
->pages
= kcalloc(a
->n_pages
, sizeof(void *), GFP_KERNEL
);
261 for (i
= 0; i
< a
->n_pages
; i
++) {
262 a
->pages
[i
] = (void *)get_zeroed_page(GFP_KERNEL
);
269 tracing_map_array_free(a
);
275 static void tracing_map_elt_clear(struct tracing_map_elt
*elt
)
279 for (i
= 0; i
< elt
->map
->n_fields
; i
++)
280 if (elt
->fields
[i
].cmp_fn
== tracing_map_cmp_atomic64
)
281 atomic64_set(&elt
->fields
[i
].sum
, 0);
283 if (elt
->map
->ops
&& elt
->map
->ops
->elt_clear
)
284 elt
->map
->ops
->elt_clear(elt
);
287 static void tracing_map_elt_init_fields(struct tracing_map_elt
*elt
)
291 tracing_map_elt_clear(elt
);
293 for (i
= 0; i
< elt
->map
->n_fields
; i
++) {
294 elt
->fields
[i
].cmp_fn
= elt
->map
->fields
[i
].cmp_fn
;
296 if (elt
->fields
[i
].cmp_fn
!= tracing_map_cmp_atomic64
)
297 elt
->fields
[i
].offset
= elt
->map
->fields
[i
].offset
;
301 static void tracing_map_elt_free(struct tracing_map_elt
*elt
)
306 if (elt
->map
->ops
&& elt
->map
->ops
->elt_free
)
307 elt
->map
->ops
->elt_free(elt
);
313 static struct tracing_map_elt
*tracing_map_elt_alloc(struct tracing_map
*map
)
315 struct tracing_map_elt
*elt
;
318 elt
= kzalloc(sizeof(*elt
), GFP_KERNEL
);
320 return ERR_PTR(-ENOMEM
);
324 elt
->key
= kzalloc(map
->key_size
, GFP_KERNEL
);
330 elt
->fields
= kcalloc(map
->n_fields
, sizeof(*elt
->fields
), GFP_KERNEL
);
336 tracing_map_elt_init_fields(elt
);
338 if (map
->ops
&& map
->ops
->elt_alloc
) {
339 err
= map
->ops
->elt_alloc(elt
);
345 tracing_map_elt_free(elt
);
350 static struct tracing_map_elt
*get_free_elt(struct tracing_map
*map
)
352 struct tracing_map_elt
*elt
= NULL
;
355 idx
= atomic_inc_return(&map
->next_elt
);
356 if (idx
< map
->max_elts
) {
357 elt
= *(TRACING_MAP_ELT(map
->elts
, idx
));
358 if (map
->ops
&& map
->ops
->elt_init
)
359 map
->ops
->elt_init(elt
);
365 static void tracing_map_free_elts(struct tracing_map
*map
)
372 for (i
= 0; i
< map
->max_elts
; i
++) {
373 tracing_map_elt_free(*(TRACING_MAP_ELT(map
->elts
, i
)));
374 *(TRACING_MAP_ELT(map
->elts
, i
)) = NULL
;
377 tracing_map_array_free(map
->elts
);
381 static int tracing_map_alloc_elts(struct tracing_map
*map
)
385 map
->elts
= tracing_map_array_alloc(map
->max_elts
,
386 sizeof(struct tracing_map_elt
*));
390 for (i
= 0; i
< map
->max_elts
; i
++) {
391 *(TRACING_MAP_ELT(map
->elts
, i
)) = tracing_map_elt_alloc(map
);
392 if (IS_ERR(*(TRACING_MAP_ELT(map
->elts
, i
)))) {
393 *(TRACING_MAP_ELT(map
->elts
, i
)) = NULL
;
394 tracing_map_free_elts(map
);
403 static inline bool keys_match(void *key
, void *test_key
, unsigned key_size
)
407 if (memcmp(key
, test_key
, key_size
))
413 static inline struct tracing_map_elt
*
414 __tracing_map_insert(struct tracing_map
*map
, void *key
, bool lookup_only
)
416 u32 idx
, key_hash
, test_key
;
417 struct tracing_map_entry
*entry
;
419 key_hash
= jhash(key
, map
->key_size
, 0);
422 idx
= key_hash
>> (32 - (map
->map_bits
+ 1));
425 idx
&= (map
->map_size
- 1);
426 entry
= TRACING_MAP_ENTRY(map
->map
, idx
);
427 test_key
= entry
->key
;
429 if (test_key
&& test_key
== key_hash
&& entry
->val
&&
430 keys_match(key
, entry
->val
->key
, map
->key_size
)) {
432 atomic64_inc(&map
->hits
);
440 if (!cmpxchg(&entry
->key
, 0, key_hash
)) {
441 struct tracing_map_elt
*elt
;
443 elt
= get_free_elt(map
);
445 atomic64_inc(&map
->drops
);
450 memcpy(elt
->key
, key
, map
->key_size
);
452 atomic64_inc(&map
->hits
);
465 * tracing_map_insert - Insert key and/or retrieve val from a tracing_map
466 * @map: The tracing_map to insert into
467 * @key: The key to insert
469 * Inserts a key into a tracing_map and creates and returns a new
470 * tracing_map_elt for it, or if the key has already been inserted by
471 * a previous call, returns the tracing_map_elt already associated
472 * with it. When the map was created, the number of elements to be
473 * allocated for the map was specified (internally maintained as
474 * 'max_elts' in struct tracing_map), and that number of
475 * tracing_map_elts was created by tracing_map_init(). This is the
476 * pre-allocated pool of tracing_map_elts that tracing_map_insert()
477 * will allocate from when adding new keys. Once that pool is
478 * exhausted, tracing_map_insert() is useless and will return NULL to
479 * signal that state. There are two user-visible tracing_map
480 * variables, 'hits' and 'drops', which are updated by this function.
481 * Every time an element is either successfully inserted or retrieved,
482 * the 'hits' value is incrememented. Every time an element insertion
483 * fails, the 'drops' value is incremented.
485 * This is a lock-free tracing map insertion function implementing a
486 * modified form of Cliff Click's basic insertion algorithm. It
487 * requires the table size be a power of two. To prevent any
488 * possibility of an infinite loop we always make the internal table
489 * size double the size of the requested table size (max_elts * 2).
490 * Likewise, we never reuse a slot or resize or delete elements - when
491 * we've reached max_elts entries, we simply return NULL once we've
492 * run out of entries. Readers can at any point in time traverse the
493 * tracing map and safely access the key/val pairs.
495 * Return: the tracing_map_elt pointer val associated with the key.
496 * If this was a newly inserted key, the val will be a newly allocated
497 * and associated tracing_map_elt pointer val. If the key wasn't
498 * found and the pool of tracing_map_elts has been exhausted, NULL is
499 * returned and no further insertions will succeed.
501 struct tracing_map_elt
*tracing_map_insert(struct tracing_map
*map
, void *key
)
503 return __tracing_map_insert(map
, key
, false);
507 * tracing_map_lookup - Retrieve val from a tracing_map
508 * @map: The tracing_map to perform the lookup on
509 * @key: The key to look up
511 * Looks up key in tracing_map and if found returns the matching
512 * tracing_map_elt. This is a lock-free lookup; see
513 * tracing_map_insert() for details on tracing_map and how it works.
514 * Every time an element is retrieved, the 'hits' value is
515 * incrememented. There is one user-visible tracing_map variable,
516 * 'hits', which is updated by this function. Every time an element
517 * is successfully retrieved, the 'hits' value is incrememented. The
518 * 'drops' value is never updated by this function.
520 * Return: the tracing_map_elt pointer val associated with the key.
521 * If the key wasn't found, NULL is returned.
523 struct tracing_map_elt
*tracing_map_lookup(struct tracing_map
*map
, void *key
)
525 return __tracing_map_insert(map
, key
, true);
529 * tracing_map_destroy - Destroy a tracing_map
530 * @map: The tracing_map to destroy
532 * Frees a tracing_map along with its associated array of
535 * Callers should make sure there are no readers or writers actively
536 * reading or inserting into the map before calling this.
538 void tracing_map_destroy(struct tracing_map
*map
)
543 tracing_map_free_elts(map
);
545 tracing_map_array_free(map
->map
);
550 * tracing_map_clear - Clear a tracing_map
551 * @map: The tracing_map to clear
553 * Resets the tracing map to a cleared or initial state. The
554 * tracing_map_elts are all cleared, and the array of struct
555 * tracing_map_entry is reset to an initialized state.
557 * Callers should make sure there are no writers actively inserting
558 * into the map before calling this.
560 void tracing_map_clear(struct tracing_map
*map
)
564 atomic_set(&map
->next_elt
, -1);
565 atomic64_set(&map
->hits
, 0);
566 atomic64_set(&map
->drops
, 0);
568 tracing_map_array_clear(map
->map
);
570 for (i
= 0; i
< map
->max_elts
; i
++)
571 tracing_map_elt_clear(*(TRACING_MAP_ELT(map
->elts
, i
)));
574 static void set_sort_key(struct tracing_map
*map
,
575 struct tracing_map_sort_key
*sort_key
)
577 map
->sort_key
= *sort_key
;
581 * tracing_map_create - Create a lock-free map and element pool
582 * @map_bits: The size of the map (2 ** map_bits)
583 * @key_size: The size of the key for the map in bytes
584 * @ops: Optional client-defined tracing_map_ops instance
585 * @private_data: Client data associated with the map
587 * Creates and sets up a map to contain 2 ** map_bits number of
588 * elements (internally maintained as 'max_elts' in struct
589 * tracing_map). Before using, map fields should be added to the map
590 * with tracing_map_add_sum_field() and tracing_map_add_key_field().
591 * tracing_map_init() should then be called to allocate the array of
592 * tracing_map_elts, in order to avoid allocating anything in the map
593 * insertion path. The user-specified map size reflects the maximum
594 * number of elements that can be contained in the table requested by
595 * the user - internally we double that in order to keep the table
596 * sparse and keep collisions manageable.
598 * A tracing_map is a special-purpose map designed to aggregate or
599 * 'sum' one or more values associated with a specific object of type
600 * tracing_map_elt, which is attached by the map to a given key.
602 * tracing_map_create() sets up the map itself, and provides
603 * operations for inserting tracing_map_elts, but doesn't allocate the
604 * tracing_map_elts themselves, or provide a means for describing the
605 * keys or sums associated with the tracing_map_elts. All
606 * tracing_map_elts for a given map have the same set of sums and
607 * keys, which are defined by the client using the functions
608 * tracing_map_add_key_field() and tracing_map_add_sum_field(). Once
609 * the fields are defined, the pool of elements allocated for the map
610 * can be created, which occurs when the client code calls
611 * tracing_map_init().
613 * When tracing_map_init() returns, tracing_map_elt elements can be
614 * inserted into the map using tracing_map_insert(). When called,
615 * tracing_map_insert() grabs a free tracing_map_elt from the pool, or
616 * finds an existing match in the map and in either case returns it.
617 * The client can then use tracing_map_update_sum() and
618 * tracing_map_read_sum() to update or read a given sum field for the
621 * The client can at any point retrieve and traverse the current set
622 * of inserted tracing_map_elts in a tracing_map, via
623 * tracing_map_sort_entries(). Sorting can be done on any field,
626 * See tracing_map.h for a description of tracing_map_ops.
628 * Return: the tracing_map pointer if successful, ERR_PTR if not.
630 struct tracing_map
*tracing_map_create(unsigned int map_bits
,
631 unsigned int key_size
,
632 const struct tracing_map_ops
*ops
,
635 struct tracing_map
*map
;
638 if (map_bits
< TRACING_MAP_BITS_MIN
||
639 map_bits
> TRACING_MAP_BITS_MAX
)
640 return ERR_PTR(-EINVAL
);
642 map
= kzalloc(sizeof(*map
), GFP_KERNEL
);
644 return ERR_PTR(-ENOMEM
);
646 map
->map_bits
= map_bits
;
647 map
->max_elts
= (1 << map_bits
);
648 atomic_set(&map
->next_elt
, -1);
650 map
->map_size
= (1 << (map_bits
+ 1));
653 map
->private_data
= private_data
;
655 map
->map
= tracing_map_array_alloc(map
->map_size
,
656 sizeof(struct tracing_map_entry
));
660 map
->key_size
= key_size
;
661 for (i
= 0; i
< TRACING_MAP_KEYS_MAX
; i
++)
662 map
->key_idx
[i
] = -1;
666 tracing_map_destroy(map
);
667 map
= ERR_PTR(-ENOMEM
);
673 * tracing_map_init - Allocate and clear a map's tracing_map_elts
674 * @map: The tracing_map to initialize
676 * Allocates a clears a pool of tracing_map_elts equal to the
677 * user-specified size of 2 ** map_bits (internally maintained as
678 * 'max_elts' in struct tracing_map). Before using, the map fields
679 * should be added to the map with tracing_map_add_sum_field() and
680 * tracing_map_add_key_field(). tracing_map_init() should then be
681 * called to allocate the array of tracing_map_elts, in order to avoid
682 * allocating anything in the map insertion path. The user-specified
683 * map size reflects the max number of elements requested by the user
684 * - internally we double that in order to keep the table sparse and
685 * keep collisions manageable.
687 * See tracing_map.h for a description of tracing_map_ops.
689 * Return: the tracing_map pointer if successful, ERR_PTR if not.
691 int tracing_map_init(struct tracing_map
*map
)
695 if (map
->n_fields
< 2)
696 return -EINVAL
; /* need at least 1 key and 1 val */
698 err
= tracing_map_alloc_elts(map
);
702 tracing_map_clear(map
);
707 static int cmp_entries_dup(const struct tracing_map_sort_entry
**a
,
708 const struct tracing_map_sort_entry
**b
)
712 if (memcmp((*a
)->key
, (*b
)->key
, (*a
)->elt
->map
->key_size
))
718 static int cmp_entries_sum(const struct tracing_map_sort_entry
**a
,
719 const struct tracing_map_sort_entry
**b
)
721 const struct tracing_map_elt
*elt_a
, *elt_b
;
722 struct tracing_map_sort_key
*sort_key
;
723 struct tracing_map_field
*field
;
724 tracing_map_cmp_fn_t cmp_fn
;
731 sort_key
= &elt_a
->map
->sort_key
;
733 field
= &elt_a
->fields
[sort_key
->field_idx
];
734 cmp_fn
= field
->cmp_fn
;
736 val_a
= &elt_a
->fields
[sort_key
->field_idx
].sum
;
737 val_b
= &elt_b
->fields
[sort_key
->field_idx
].sum
;
739 ret
= cmp_fn(val_a
, val_b
);
740 if (sort_key
->descending
)
746 static int cmp_entries_key(const struct tracing_map_sort_entry
**a
,
747 const struct tracing_map_sort_entry
**b
)
749 const struct tracing_map_elt
*elt_a
, *elt_b
;
750 struct tracing_map_sort_key
*sort_key
;
751 struct tracing_map_field
*field
;
752 tracing_map_cmp_fn_t cmp_fn
;
759 sort_key
= &elt_a
->map
->sort_key
;
761 field
= &elt_a
->fields
[sort_key
->field_idx
];
763 cmp_fn
= field
->cmp_fn
;
765 val_a
= elt_a
->key
+ field
->offset
;
766 val_b
= elt_b
->key
+ field
->offset
;
768 ret
= cmp_fn(val_a
, val_b
);
769 if (sort_key
->descending
)
775 static void destroy_sort_entry(struct tracing_map_sort_entry
*entry
)
780 if (entry
->elt_copied
)
781 tracing_map_elt_free(entry
->elt
);
787 * tracing_map_destroy_sort_entries - Destroy an array of sort entries
788 * @entries: The entries to destroy
789 * @n_entries: The number of entries in the array
791 * Destroy the elements returned by a tracing_map_sort_entries() call.
793 void tracing_map_destroy_sort_entries(struct tracing_map_sort_entry
**entries
,
794 unsigned int n_entries
)
798 for (i
= 0; i
< n_entries
; i
++)
799 destroy_sort_entry(entries
[i
]);
804 static struct tracing_map_sort_entry
*
805 create_sort_entry(void *key
, struct tracing_map_elt
*elt
)
807 struct tracing_map_sort_entry
*sort_entry
;
809 sort_entry
= kzalloc(sizeof(*sort_entry
), GFP_KERNEL
);
813 sort_entry
->key
= key
;
814 sort_entry
->elt
= elt
;
819 static struct tracing_map_elt
*copy_elt(struct tracing_map_elt
*elt
)
821 struct tracing_map_elt
*dup_elt
;
824 dup_elt
= tracing_map_elt_alloc(elt
->map
);
828 if (elt
->map
->ops
&& elt
->map
->ops
->elt_copy
)
829 elt
->map
->ops
->elt_copy(dup_elt
, elt
);
831 dup_elt
->private_data
= elt
->private_data
;
832 memcpy(dup_elt
->key
, elt
->key
, elt
->map
->key_size
);
834 for (i
= 0; i
< elt
->map
->n_fields
; i
++) {
835 atomic64_set(&dup_elt
->fields
[i
].sum
,
836 atomic64_read(&elt
->fields
[i
].sum
));
837 dup_elt
->fields
[i
].cmp_fn
= elt
->fields
[i
].cmp_fn
;
843 static int merge_dup(struct tracing_map_sort_entry
**sort_entries
,
844 unsigned int target
, unsigned int dup
)
846 struct tracing_map_elt
*target_elt
, *elt
;
847 bool first_dup
= (target
- dup
) == 1;
851 elt
= sort_entries
[target
]->elt
;
852 target_elt
= copy_elt(elt
);
855 sort_entries
[target
]->elt
= target_elt
;
856 sort_entries
[target
]->elt_copied
= true;
858 target_elt
= sort_entries
[target
]->elt
;
860 elt
= sort_entries
[dup
]->elt
;
862 for (i
= 0; i
< elt
->map
->n_fields
; i
++)
863 atomic64_add(atomic64_read(&elt
->fields
[i
].sum
),
864 &target_elt
->fields
[i
].sum
);
866 sort_entries
[dup
]->dup
= true;
871 static int merge_dups(struct tracing_map_sort_entry
**sort_entries
,
872 int n_entries
, unsigned int key_size
)
874 unsigned int dups
= 0, total_dups
= 0;
881 sort(sort_entries
, n_entries
, sizeof(struct tracing_map_sort_entry
*),
882 (int (*)(const void *, const void *))cmp_entries_dup
, NULL
);
884 key
= sort_entries
[0]->key
;
885 for (i
= 1; i
< n_entries
; i
++) {
886 if (!memcmp(sort_entries
[i
]->key
, key
, key_size
)) {
887 dups
++; total_dups
++;
888 err
= merge_dup(sort_entries
, i
- dups
, i
);
893 key
= sort_entries
[i
]->key
;
900 for (i
= 0, j
= 0; i
< n_entries
; i
++) {
901 if (!sort_entries
[i
]->dup
) {
902 sort_entries
[j
] = sort_entries
[i
];
904 sort_entries
[i
] = NULL
;
906 destroy_sort_entry(sort_entries
[i
]);
907 sort_entries
[i
] = NULL
;
914 static bool is_key(struct tracing_map
*map
, unsigned int field_idx
)
918 for (i
= 0; i
< map
->n_keys
; i
++)
919 if (map
->key_idx
[i
] == field_idx
)
924 static void sort_secondary(struct tracing_map
*map
,
925 const struct tracing_map_sort_entry
**entries
,
926 unsigned int n_entries
,
927 struct tracing_map_sort_key
*primary_key
,
928 struct tracing_map_sort_key
*secondary_key
)
930 int (*primary_fn
)(const struct tracing_map_sort_entry
**,
931 const struct tracing_map_sort_entry
**);
932 int (*secondary_fn
)(const struct tracing_map_sort_entry
**,
933 const struct tracing_map_sort_entry
**);
934 unsigned i
, start
= 0, n_sub
= 1;
936 if (is_key(map
, primary_key
->field_idx
))
937 primary_fn
= cmp_entries_key
;
939 primary_fn
= cmp_entries_sum
;
941 if (is_key(map
, secondary_key
->field_idx
))
942 secondary_fn
= cmp_entries_key
;
944 secondary_fn
= cmp_entries_sum
;
946 for (i
= 0; i
< n_entries
- 1; i
++) {
947 const struct tracing_map_sort_entry
**a
= &entries
[i
];
948 const struct tracing_map_sort_entry
**b
= &entries
[i
+ 1];
950 if (primary_fn(a
, b
) == 0) {
952 if (i
< n_entries
- 2)
962 set_sort_key(map
, secondary_key
);
963 sort(&entries
[start
], n_sub
,
964 sizeof(struct tracing_map_sort_entry
*),
965 (int (*)(const void *, const void *))secondary_fn
, NULL
);
966 set_sort_key(map
, primary_key
);
974 * tracing_map_sort_entries - Sort the current set of tracing_map_elts in a map
975 * @map: The tracing_map
976 * @sort_key: The sort key to use for sorting
977 * @sort_entries: outval: pointer to allocated and sorted array of entries
979 * tracing_map_sort_entries() sorts the current set of entries in the
980 * map and returns the list of tracing_map_sort_entries containing
981 * them to the client in the sort_entries param. The client can
982 * access the struct tracing_map_elt element of interest directly as
983 * the 'elt' field of a returned struct tracing_map_sort_entry object.
985 * The sort_key has only two fields: idx and descending. 'idx' refers
986 * to the index of the field added via tracing_map_add_sum_field() or
987 * tracing_map_add_key_field() when the tracing_map was initialized.
988 * 'descending' is a flag that if set reverses the sort order, which
989 * by default is ascending.
991 * The client should not hold on to the returned array but should use
992 * it and call tracing_map_destroy_sort_entries() when done.
994 * Return: the number of sort_entries in the struct tracing_map_sort_entry
995 * array, negative on error
997 int tracing_map_sort_entries(struct tracing_map
*map
,
998 struct tracing_map_sort_key
*sort_keys
,
999 unsigned int n_sort_keys
,
1000 struct tracing_map_sort_entry
***sort_entries
)
1002 int (*cmp_entries_fn
)(const struct tracing_map_sort_entry
**,
1003 const struct tracing_map_sort_entry
**);
1004 struct tracing_map_sort_entry
*sort_entry
, **entries
;
1005 int i
, n_entries
, ret
;
1007 entries
= vmalloc(map
->max_elts
* sizeof(sort_entry
));
1011 for (i
= 0, n_entries
= 0; i
< map
->map_size
; i
++) {
1012 struct tracing_map_entry
*entry
;
1014 entry
= TRACING_MAP_ENTRY(map
->map
, i
);
1016 if (!entry
->key
|| !entry
->val
)
1019 entries
[n_entries
] = create_sort_entry(entry
->val
->key
,
1021 if (!entries
[n_entries
++]) {
1027 if (n_entries
== 0) {
1032 if (n_entries
== 1) {
1033 *sort_entries
= entries
;
1037 ret
= merge_dups(entries
, n_entries
, map
->key_size
);
1042 if (is_key(map
, sort_keys
[0].field_idx
))
1043 cmp_entries_fn
= cmp_entries_key
;
1045 cmp_entries_fn
= cmp_entries_sum
;
1047 set_sort_key(map
, &sort_keys
[0]);
1049 sort(entries
, n_entries
, sizeof(struct tracing_map_sort_entry
*),
1050 (int (*)(const void *, const void *))cmp_entries_fn
, NULL
);
1052 if (n_sort_keys
> 1)
1054 (const struct tracing_map_sort_entry
**)entries
,
1059 *sort_entries
= entries
;
1063 tracing_map_destroy_sort_entries(entries
, n_entries
);