Implement bloom filter map
Subsystems (4)
v2 cover Implement bloom filter map
This patchset adds a new kind of bpf map: the bloom filter map. Bloom filters are a space-efficient probabilistic data structure used to quickly test whether an element exists in a set. For a brief overview about how bloom filters work,
https://en.wikipedia.org/wiki/Bloom_filter
may be helpful.
One example use-case is an application leveraging a bloom filter map to determine whether a computationally expensive hashmap lookup can be avoided. If the element was not found in the bloom filter map, the hashmap lookup can be skipped.
This patchset includes benchmarks for testing out the performance of the bloom filter for different entry sizes and different number of hash functions used, as well as comparisons for hashmap lookups with vs. without the bloom filter.
v1 -> v2:
* Remove libbpf changes, and pass the number of hash functions through map_flags instead.
* Default to using 5 hash functions if no number of hash functions is specified.
* Use set_bit instead of spinlocks in the bloom filter bitmap. This improved the speed significantly. For example, using 5 hash functions with 100k entries, there was roughly a 35% speed increase.
* Use jhash2 (instead of jhash) for u32-aligned value sizes. This increased the speed by roughly 5 to 15%. When using jhash2 on value sizes non-u32 aligned (truncating any remainder bits), there was not a noticeable difference.
* Add test for using the bloom filter as an inner map.
* Reran the benchmarks, updated the commit messages to correspond to the new results.
Joanne Koong (4):
bpf: Add bloom filter map implementation selftests/bpf: Add bloom filter map test cases bpf/benchs: Add benchmark test for bloom filter maps bpf/benchs: Add benchmarks for comparing hashmap lookups with vs.
without bloom filter
include/linux/bpf_types.h | 1 + include/uapi/linux/bpf.h | 10 + kernel/bpf/Makefile | 2 +- kernel/bpf/bloom_filter.c | 205 +++++++++ kernel/bpf/syscall.c | 14 +- kernel/bpf/verifier.c | 19 +- tools/include/uapi/linux/bpf.h | 10 + tools/testing/selftests/bpf/Makefile | 4 +- tools/testing/selftests/bpf/bench.c | 57 ++- tools/testing/selftests/bpf/bench.h | 3 + .../bpf/benchs/bench_bloom_filter_map.c | 393 ++++++++++++++++++ .../bpf/benchs/run_bench_bloom_filter_map.sh | 43 ++ .../bpf/benchs/run_bench_ringbufs.sh | 30 +- .../selftests/bpf/benchs/run_common.sh | 60 +++ .../bpf/prog_tests/bloom_filter_map.c | 177 ++++++++ .../selftests/bpf/progs/bloom_filter_map.c | 156 +++++++ 16 files changed, 1144 insertions(+), 40 deletions(-) create mode 100644 kernel/bpf/bloom_filter.c create mode 100644 tools/testing/selftests/bpf/benchs/bench_bloom_filter_map.c create mode 100755 tools/testing/selftests/bpf/benchs/run_bench_bloom_filter_map.sh create mode 100644 tools/testing/selftests/bpf/benchs/run_common.sh create mode 100644 tools/testing/selftests/bpf/prog_tests/bloom_filter_map.c create mode 100644 tools/testing/selftests/bpf/progs/bloom_filter_map.c
Signature · 1 line
2.30.2
PATCH v2 1/4 bpf: Add bloom filter map implementation
Bloom filters are a space-efficient probabilistic data structure used to quickly test whether an element exists in a set. In a bloom filter, false positives are possible whereas false negatives should never be.
This patch adds a bloom filter map for bpf programs. The bloom filter map supports peek (determining whether an element is present in the map) and push (adding an element to the map) operations.These operations are exposed to userspace applications through the already existing syscalls in the following way:
BPF_MAP_LOOKUP_ELEM -> peekBPF_MAP_UPDATE_ELEM -> push
The bloom filter map does not have keys, only values. In light of this, the bloom filter map's API matches that of queue stack maps:
user applications use BPF_MAP_LOOKUP_ELEM/BPF_MAP_UPDATE_ELEM which correspond internally to bpf_map_peek_elem/bpf_map_push_elem, and bpf programs must use the bpf_map_peek_elem and bpf_map_push_elem APIs to query or add an element to the bloom filter map. When the bloom filter map is created, it must be created with a key_size of 0.
For updates, the user will pass in the element to add to the map as the value, with a NULL key. For lookups, the user will pass in the element to query in the map as the value. In the verifier layer, this requires us to modify the argument type of a bloom filter's BPF_FUNC_map_peek_elem call to ARG_PTR_TO_MAP_VALUE; as well, in the syscall layer, we need to copy over the user value so that in bpf_map_peek_elem, we know which specific value to query.
A few things to please take note of:
* If there are any concurrent lookups + updates, the user is
responsible for synchronizing this to ensure no false negative lookups occur.
* The number of hashes to use for the bloom filter is configurable from
userspace. If no number is specified, the default used will be 5 hash functions. The benchmarks later in this patchset can help compare the performance of using different number of hashes on different entry sizes. In general, using more hashes decreases the speed of a lookup, but increases the false positive rate of an element being detected in the bloom filter.
* Deleting an element in the bloom filter map is not supported.
* The bloom filter map may be used as an inner map.
* The "max_entries" size that is specified at map creation time is used to
approximate a reasonable bitmap size for the bloom filter, and is not otherwise strictly enforced. If the user wishes to insert more entries into the bloom filter than "max_entries", they may do so but they should be aware that this may lead to a higher false positive rate.
--- include/linux/bpf_types.h | 1 + include/uapi/linux/bpf.h | 10 ++ kernel/bpf/Makefile | 2 +- kernel/bpf/bloom_filter.c | 205 +++++++++++++++++++++++++++++++++ kernel/bpf/syscall.c | 14 ++- kernel/bpf/verifier.c | 19 ++- tools/include/uapi/linux/bpf.h | 10 ++ 7 files changed, 255 insertions(+), 6 deletions(-) create mode 100644 kernel/bpf/bloom_filter.c
include/linux/bpf_types.h +1 -0
@@ -125,6 +125,7 @@ BPF_MAP_TYPE(BPF_MAP_TYPE_STACK, stack_map_ops) BPF_MAP_TYPE(BPF_MAP_TYPE_STRUCT_OPS, bpf_struct_ops_map_ops)#endif BPF_MAP_TYPE(BPF_MAP_TYPE_RINGBUF, ringbuf_map_ops) +BPF_MAP_TYPE(BPF_MAP_TYPE_BLOOM_FILTER, bloom_filter_map_ops) BPF_LINK_TYPE(BPF_LINK_TYPE_RAW_TRACEPOINT, raw_tracepoint)BPF_LINK_TYPE (BPF_LINK_TYPE_TRACING , tracing)
include/uapi/linux/bpf.h +10 -0
@@ -906,6 +906,7 @@ enum bpf_map_type {BPF_MAP_TYPE_RINGBUF ,BPF_MAP_TYPE_INODE_STORAGE ,BPF_MAP_TYPE_TASK_STORAGE , + BPF_MAP_TYPE_BLOOM_FILTER, };/* Note that tracing related programs such as
@@ -1210,6 +1211,15 @@ enum {/* Create a map that is suitable to be an inner map with dynamic max entries */ BPF_F_INNER_MAP = (1U <<12 ), + +/* For bloom filter maps, the next 4 bits represent how many hashes to use. +* The maximum number of hash functions supported is 15. If this is not set, +* the default number of hash functions used will be 5. +*/ +BPF_F_BLOOM_FILTER_HASH_BIT_1 = (1U <<13 ), +BPF_F_BLOOM_FILTER_HASH_BIT_2 = (1U <<14 ), +BPF_F_BLOOM_FILTER_HASH_BIT_3 = (1U <<15 ), + BPF_F_BLOOM_FILTER_HASH_BIT_4 = (1U << 16), };/* Flags for BPF_PROG_QUERY. */
kernel/bpf/Makefile +1 -1
@@ -7,7 +7,7 @@ endif CFLAGS_core.o += $(call cc-disable-warning, override-init) $(cflags-nogcse-yy) obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o tnum.o bpf_iter.o map_iter.o task_iter.o prog_iter.o -obj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o percpu_freelist.o bpf_lru_list.o lpm_trie.o map_in_map.o +obj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o percpu_freelist.o bpf_lru_list.o lpm_trie.o map_in_map.o bloom_filter.o obj-$(CONFIG_BPF_SYSCALL) += local_storage.o queue_stack_maps.o ringbuf.o obj-$(CONFIG_BPF_SYSCALL) += bpf_local_storage.o bpf_task_storage.o obj-${CONFIG_BPF_LSM} += bpf_inode_storage.o
kernel/bpf/bloom_filter.c new file +205 -0
205 lines not on this page · raw message
kernel/bpf/syscall.c +12 -2
@@ -199,7 +199,8 @@ static int bpf_map_update_value(struct bpf_map *map, struct fd f, void *key, err =bpf_fd_reuseport_array_update_elem (map, key, value, flags); }else if (map->map_type ==BPF_MAP_TYPE_QUEUE || - map->map_type ==BPF_MAP_TYPE_STACK ) { + map->map_type ==BPF_MAP_TYPE_STACK || + map->map_type ==BPF_MAP_TYPE_BLOOM_FILTER ) { err = map->ops->map_push_elem (map, value, flags); }else {rcu_read_lock ();
@@ -238,7 +239,8 @@ static int bpf_map_copy_value(struct bpf_map *map, void *key, void *value, }else if (map->map_type ==BPF_MAP_TYPE_REUSEPORT_SOCKARRAY ) { err =bpf_fd_reuseport_array_lookup_elem (map, key, value); }else if (map->map_type ==BPF_MAP_TYPE_QUEUE || - map->map_type ==BPF_MAP_TYPE_STACK ) { + map->map_type ==BPF_MAP_TYPE_STACK || + map->map_type ==BPF_MAP_TYPE_BLOOM_FILTER ) { err = map->ops->map_peek_elem (map, value); }else if (map->map_type ==BPF_MAP_TYPE_STRUCT_OPS ) {/* struct_ops map requires directly updating "value" */
@@ -1080,6 +1082,14 @@ static int map_lookup_elem(union bpf_attr *attr)if (!value) goto free_key; +if (map->map_type ==BPF_MAP_TYPE_BLOOM_FILTER ) { +if (copy_from_user (value, uvalue, value_size)) + err = -EFAULT ; +else + err =bpf_map_copy_value (map, key, value, attr->flags); + goto free_value; + } + err =bpf_map_copy_value (map, key, value, attr->flags);if (err) goto free_value;
kernel/bpf/verifier.c +16 -3
@@ -4813,7 +4813,10 @@ static int resolve_map_arg_type(struct bpf_verifier_env *env,return -EINVAL ; }break ; - +case BPF_MAP_TYPE_BLOOM_FILTER : +if (meta->func_id == BPF_FUNC_map_peek_elem) + *arg_type =ARG_PTR_TO_MAP_VALUE ; +break ;default :break ; }
@@ -5388,6 +5391,11 @@ static int check_map_func_compatibility(struct bpf_verifier_env *env, func_id != BPF_FUNC_task_storage_delete) goto error;break ; +case BPF_MAP_TYPE_BLOOM_FILTER : +if (func_id != BPF_FUNC_map_push_elem && + func_id != BPF_FUNC_map_peek_elem) + goto error; +break ;default :break ; }
@@ -5455,13 +5463,18 @@ static int check_map_func_compatibility(struct bpf_verifier_env *env, map->map_type != BPF_MAP_TYPE_SOCKHASH) goto error;break ; -case BPF_FUNC_map_peek_elem:case BPF_FUNC_map_pop_elem: -case BPF_FUNC_map_push_elem:if (map->map_type !=BPF_MAP_TYPE_QUEUE && map->map_type !=BPF_MAP_TYPE_STACK ) goto error;break ; +case BPF_FUNC_map_push_elem: +case BPF_FUNC_map_peek_elem: +if (map->map_type !=BPF_MAP_TYPE_QUEUE && + map->map_type !=BPF_MAP_TYPE_STACK && + map->map_type !=BPF_MAP_TYPE_BLOOM_FILTER ) + goto error; +break ;case BPF_FUNC_sk_storage_get:case BPF_FUNC_sk_storage_delete:if (map->map_type !=BPF_MAP_TYPE_SK_STORAGE )
tools/include/uapi/linux/bpf.h +10 -0
@@ -906,6 +906,7 @@ enum bpf_map_type {BPF_MAP_TYPE_RINGBUF ,BPF_MAP_TYPE_INODE_STORAGE ,BPF_MAP_TYPE_TASK_STORAGE , + BPF_MAP_TYPE_BLOOM_FILTER, };/* Note that tracing related programs such as
@@ -1210,6 +1211,15 @@ enum {/* Create a map that is suitable to be an inner map with dynamic max entries */ BPF_F_INNER_MAP = (1U <<12 ), + +/* For bloom filter maps, the next 4 bits represent how many hashes to use. +* The maximum number of hash functions supported is 15. If this is not set, +* the default number of hash functions used will be 5. +*/ +BPF_F_BLOOM_FILTER_HASH_BIT_1 = (1U <<13 ), +BPF_F_BLOOM_FILTER_HASH_BIT_2 = (1U <<14 ), +BPF_F_BLOOM_FILTER_HASH_BIT_3 = (1U <<15 ), + BPF_F_BLOOM_FILTER_HASH_BIT_4 = (1U << 16), };/* Flags for BPF_PROG_QUERY. */
Signature · 1 line
2.30.2
Re: [PATCH v2 bpf-next 1/4] bpf: Add bloom filter map implementation
include/uapi/linux/bpf.h enum { · line 1222
Quoted 9 lines · Joanne Koong
include/uapi/linux/bpf.h +9
-0+ +/* For bloom filter maps, the next 4 bits represent how many hashes to use. +* The maximum number of hash functions supported is 15. If this is not set, +* the default number of hash functions used will be 5. +*/ +BPF_F_BLOOM_FILTER_HASH_BIT_1 = (1U <<13 ), +BPF_F_BLOOM_FILTER_HASH_BIT_2 = (1U <<14 ), +BPF_F_BLOOM_FILTER_HASH_BIT_3 = (1U <<15 ), + BPF_F_BLOOM_FILTER_HASH_BIT_4 = (1U << 16),
The bit selection is unintuitive.
Since key_size has to be zero may be used that instead to indicate the number of hash functions in the rare case when 5 is not good enough?
Or use inner_map_fd since there is no possibility of having an inner map in bloomfilter. It could be a union:
__u32 max_entries; /* max number of entries in a map */
__u32 map_flags; /* BPF_MAP_CREATE related
* flags defined above.
*/
union {
__u32 inner_map_fd; /* fd pointing to the inner map */
__u32 nr_hash_funcs; /* or number of hash functions */
};
__u32 numa_node; /* numa node */kernel/bpf/bloom_filter.c line 28
Quoted 11 lines
kernel/bpf/bloom_filter.c new file +11
-0+struct bpf_bloom_filter { +struct bpf_map map; +u32 bit_array_mask; +u32 hash_seed; +/* If the size of the values in the bloom filter is u32 aligned, +* then it is more performant to use jhash2 as the underlying hash +* function, else we use jhash. This tracks the number of u32s +* in an u32-aligned value size. If the value size is not u32 aligned, +* this will be 0. +*/ +u32 aligned_u32_count;
what is the performance difference?
May be we enforce 4-byte sized value for simplicity?
Re: [PATCH v2 bpf-next 1/4] bpf: Add bloom filter map implementation
Quoted 15 lines · Alexei Starovoitov
include/uapi/linux/bpf.h enum { · line 1222
Quoted 9 lines · Joanne Koong
include/uapi/linux/bpf.h +9
-0+ +/* For bloom filter maps, the next 4 bits represent how many hashes to use. +* The maximum number of hash functions supported is 15. If this is not set, +* the default number of hash functions used will be 5. +*/ +BPF_F_BLOOM_FILTER_HASH_BIT_1 = (1U <<13 ), +BPF_F_BLOOM_FILTER_HASH_BIT_2 = (1U <<14 ), +BPF_F_BLOOM_FILTER_HASH_BIT_3 = (1U <<15 ), + BPF_F_BLOOM_FILTER_HASH_BIT_4 = (1U << 16),The bit selection is unintuitive.
Sincekey_sizehas to be zero may be used that instead to indicate the number of hash functions in the rare case when 5 is not good enough?
Or useinner_map_fdsince there is no possibility of having an inner map in bloomfilter. It could be a union:
Quoted 9 lines
__u32 max_entries; /* max number of entries in a map */
__u32 map_flags; /* BPF_MAP_CREATE related
* flags defined above.
*/
union {
__u32 inner_map_fd; /* fd pointing to the inner map */
__u32 nr_hash_funcs; /* or number of hash functions */
};
__u32 numa_node; /* numa node */I really like the idea of union-ing inner_map_fd with the number of hash functions (my worry with using key_size is that it might be a confusing / non-intuitive API quirk for users), but I think this would later require us to add some bloom filter specific APIs to libbpf (such as bpf_map__set_nr_hashes).
To make the bit selection more intuitive, Andrii suggested defining some helper like
BPF_F_BLOOM_NR_HASH_OFF = 13
where the user could then do something like
struct {
__uint(type, BPF_MAP_TYPE_BLOOM_FILTER),
...
__uint(map_flags, 5 << BPF_F_BLOOM_NR_HASH_OFF),
};to set the number of hash functions.
Would this approach address your concerns about the unintuitiveness of the bit selection?
Quoted 4 lines
Quoted 4 lines
kernel/bpf/bloom_filter.c new file +4
-0+struct bpf_bloom_filter { +struct bpf_map map; +u32 bit_array_mask; +u32 hash_seed;
Quoted 8 lines
kernel/bpf/bloom_filter.c line 28
Quoted 7 lines
kernel/bpf/bloom_filter.c new file +7
-0+/* If the size of the values in the bloom filter is u32 aligned, +* then it is more performant to use jhash2 as the underlying hash +* function, else we use jhash. This tracks the number of u32s +* in an u32-aligned value size. If the value size is not u32 aligned, +* this will be 0. +*/ +u32 aligned_u32_count;what is the performance difference?
Using results from the hashmap benchmark tests, using jhash2 instead of jhash for 4-byte aligned value sizes improved the performance by roughly 5% to 15%. For non-4-byte aligned value sizes, there wasn't a noticeable difference between using jhash2 (and truncating the remainder bits) vs. using jhash.
Quoted 1 line
May be we enforce 4-byte sized value for simplicity?
Sounds great! And if in the future this becomes too restrictive, we could always loosen this as well
Re: [PATCH v2 bpf-next 1/4] bpf: Add bloom filter map implementation
In reply to Alexei StarovoitovQuoted 15 lines · Alexei Starovoitov
include/uapi/linux/bpf.h enum { · line 1222
Quoted 9 lines · Joanne Koong
include/uapi/linux/bpf.h +9
-0+ +/* For bloom filter maps, the next 4 bits represent how many hashes to use. +* The maximum number of hash functions supported is 15. If this is not set, +* the default number of hash functions used will be 5. +*/ +BPF_F_BLOOM_FILTER_HASH_BIT_1 = (1U <<13 ), +BPF_F_BLOOM_FILTER_HASH_BIT_2 = (1U <<14 ), +BPF_F_BLOOM_FILTER_HASH_BIT_3 = (1U <<15 ), + BPF_F_BLOOM_FILTER_HASH_BIT_4 = (1U << 16),The bit selection is unintuitive.
Sincekey_sizehas to be zero may be used that instead to indicate the number of hash functions in the rare case when 5 is not good enough?
Hm... I was initially thinking about proposing something like that, but it felt a bit ugly at the time. But now thinking about this a bit more, I think this would be a bit more meaningful if we change the terminology a bit. Instead of saying that Bloom filter has values and no keys, we actually have keys and no values. So all those bytes that are hashed are treated as keys (which is actually how sets are implemented on top of maps, where you have keys and no values, or at least the value is always true).
So with that we'll have key/key_size to specify number of bytes that needs to be hashed (and it's type info). And then we can squint a bit and say that number of hashes are specified by value_size, as in values are those nr_hash bits that we set in Bloom filter.
Still a bit of terminology stretch, but won't necessitate those specialized fields just for Bloom filter map. But if default value is going to be good enough for most cases and most cases won't need to adjust number of hashes, this seems to be pretty clean to me.
Quoted 10 lines
Or use
inner_map_fdsince there is no possibility of having an inner map in bloomfilter. It could be a union:__u32 max_entries; /* max number of entries in a map */ __u32 map_flags; /* BPF_MAP_CREATE related* flags defined above. */
union {
__u32inner_map_fd; /* fd pointing to the inner map */
__u32nr_hash_funcs; /* or number of hash functions */
};
This works as well. A bit more Bloom filter-only terminology throughout UAPI and libbpf, but I'd be fine with that as well.
Quoted 14 lines
__u32 numa_node; /* numa node */kernel/bpf/bloom_filter.c line 28
Quoted 11 lines
kernel/bpf/bloom_filter.c new file +11
-0+struct bpf_bloom_filter { +struct bpf_map map; +u32 bit_array_mask; +u32 hash_seed; +/* If the size of the values in the bloom filter is u32 aligned, +* then it is more performant to use jhash2 as the underlying hash +* function, else we use jhash. This tracks the number of u32s +* in an u32-aligned value size. If the value size is not u32 aligned, +* this will be 0. +*/ +u32 aligned_u32_count;
Quoted 2 lines
what is the performance difference?
May be we enforce 4-byte sized value for simplicity?
This might be a bit too surprising, especially if keys are just some strings, where people might not expect that it has to 4-byte multiple size. And debugging this without extra tooling (like retsnoop) is going to be nightmarish.
If the performance diff is huge and that if/else logic is unacceptable, we can also internally pad with up to 3 zero bytes and include those into the hash.
Re: [PATCH v2 bpf-next 1/4] bpf: Add bloom filter map implementation
My previous email replied to Alexei's email before I saw Andrii's new email, so please feel free to disregard my previous email.
Quoted 29 lines · Andrii Nakryiko
Quoted 13 lines · Alexei Starovoitov
include/uapi/linux/bpf.h enum { · line 1222
Quoted 9 lines · Joanne Koong
include/uapi/linux/bpf.h +9
-0+ +/* For bloom filter maps, the next 4 bits represent how many hashes to use. +* The maximum number of hash functions supported is 15. If this is not set, +* the default number of hash functions used will be 5. +*/ +BPF_F_BLOOM_FILTER_HASH_BIT_1 = (1U <<13 ), +BPF_F_BLOOM_FILTER_HASH_BIT_2 = (1U <<14 ), +BPF_F_BLOOM_FILTER_HASH_BIT_3 = (1U <<15 ), + BPF_F_BLOOM_FILTER_HASH_BIT_4 = (1U << 16),The bit selection is unintuitive.
Sincekey_sizehas to be zero may be used that instead to indicate the number of hash functions in the rare case when 5 is not good enough?Hm... I was initially thinking about proposing something like that, but it felt a bit ugly at the time. But now thinking about this a bit more, I think this would be a bit more meaningful if we change the terminology a bit. Instead of saying that Bloom filter has values and no keys, we actually have keys and no values. So all those bytes that are hashed are treated as keys (which is actually how sets are implemented on top of maps, where you have keys and no values, or at least the value is always true).
So with that we'll have key/key_size to specify number of bytes that needs to be hashed (and it's type info). And then we can squint a bit and say that number of hashes are specified by
value_size, as in values are thosenr_hashbits that we set in Bloom filter.
Quoted 4 lines
Still a bit of terminology stretch, but won't necessitate those specialized fields just for Bloom filter map. But if default value is going to be good enough for most cases and most cases won't need to adjust number of hashes, this seems to be pretty clean to me.
With having bloom filter map keys instead of values, I think this would lead to messier code in the kernel for handling map_lookup_elem and map_update_elem calls, due to the fact that the bloom filter map is a non-associative map and the current APIs for non-associative map types (peek_elem/push_elem/pop_elem) all have the map data as the value and not the key.
For example, for map_update_elem, the API from the eBPF program side is
int (*map_update_elem)(struct bpf_map *map, void *key, void *value, u64 flags);
This would require us to either
a) Add some custom logic in syscall.c so that we bypass the copy_from_bpfptr call on bloom filter map values (necessary because memcpying 0 bytes still requires the src pointer to be valid), which would allow us to pass in a NULL value
b) Add a new function like
int (*map_push_key)(struct bpf_map *map, void *key, u64 flags)
that eBPF programs can call instead of map_update_elem.
or
c) Try to repurpose the existing map_push_elem API by passing in the key instead of the value, which would lead to inconsistent use of the API
I think if we could change the non-associative map types (currently only stack maps and queue maps, I believe) to have their data be a key instead of a value, and have the pop/peek APIs use keys instead of values, then this would be cleaner, since we could then just use the existing peek/pop APIs.
Quoted 10 lines
Quoted 10 lines
Or use
inner_map_fdsince there is no possibility of having an inner map in bloomfilter. It could be a union:__u32 max_entries; /* max number of entries in a map */ __u32 map_flags; /* BPF_MAP_CREATE related* flags defined above. */
union {
__u32inner_map_fd; /* fd pointing to the inner map */
__u32nr_hash_funcs; /* or number of hash functions */
};
Quoted 3 lines
This works as well. A bit more Bloom filter-only terminology throughout UAPI and libbpf, but I'd be fine with that as well.
Great, it looks like this is the consensus - I will go with this option for v3!
Quoted 15 lines
Quoted 13 lines
__u32 numa_node; /* numa node */kernel/bpf/bloom_filter.c line 28
Quoted 11 lines
kernel/bpf/bloom_filter.c new file +11
-0+struct bpf_bloom_filter { +struct bpf_map map; +u32 bit_array_mask; +u32 hash_seed; +/* If the size of the values in the bloom filter is u32 aligned, +* then it is more performant to use jhash2 as the underlying hash +* function, else we use jhash. This tracks the number of u32s +* in an u32-aligned value size. If the value size is not u32 aligned, +* this will be 0. +*/ +u32 aligned_u32_count;Quoted 2 lines
what is the performance difference?
May be we enforce 4-byte sized value for simplicity?
Quoted 8 lines
This might be a bit too surprising, especially if keys are just some strings, where people might not expect that it has to 4-byte multiple size. And debugging this without extra tooling (like retsnoop) is going to be nightmarish.
If the performance diff is huge and that if/else logic is unacceptable, we can also internally pad with up to 3 zero bytes and include those into the hash.
I think the if/else logic is unavoidable if we support non 4-byte aligned value sizes, unless we are okay with truncating any remainder bytes of non 4-byte aligned values and stipulating that a bloom filter map value size has to be greater than 4 bytes (these conditions would allow us to use jhash2 for every value without an if/else check). If we internally pad, we will have to pad on every update and lookup, which would also require an if/else.
Thanks for the comments and reviews, Alexei and Andrii. They are much appreciated!
Re: [PATCH v2 bpf-next 1/4] bpf: Add bloom filter map implementation
Quoted 4 lines · Joanne Koong
My previous email replied to Alexei's email before I saw Andrii's new email, so please
feel free to disregard my previous email.
Never got that reply of yours. Alexei's email arrived a few hours after I've already replied to you. It was a time warp anomaly :)
Quoted 69 lines · Andrii Nakryiko
Quoted 30 lines
Quoted 13 lines · Alexei Starovoitov
include/uapi/linux/bpf.h enum { · line 1222
Quoted 9 lines · Joanne Koong
include/uapi/linux/bpf.h +9
-0+ +/* For bloom filter maps, the next 4 bits represent how many hashes to use. +* The maximum number of hash functions supported is 15. If this is not set, +* the default number of hash functions used will be 5. +*/ +BPF_F_BLOOM_FILTER_HASH_BIT_1 = (1U <<13 ), +BPF_F_BLOOM_FILTER_HASH_BIT_2 = (1U <<14 ), +BPF_F_BLOOM_FILTER_HASH_BIT_3 = (1U <<15 ), + BPF_F_BLOOM_FILTER_HASH_BIT_4 = (1U << 16),The bit selection is unintuitive.
Sincekey_sizehas to be zero may be used that instead to indicate the number of hash functions in the rare case when 5 is not good enough?Hm... I was initially thinking about proposing something like that, but it felt a bit ugly at the time. But now thinking about this a bit more, I think this would be a bit more meaningful if we change the terminology a bit. Instead of saying that Bloom filter has values and no keys, we actually have keys and no values. So all those bytes that are hashed are treated as keys (which is actually how sets are implemented on top of maps, where you have keys and no values, or at least the value is always true).
So with that we'll have key/key_size to specify number of bytes that needs to be hashed (and it's type info). And then we can squint a bit and say that number of hashes are specified by
value_size, as in values are thosenr_hashbits that we set in Bloom filter.Quoted 4 lines
Still a bit of terminology stretch, but won't necessitate those specialized fields just for Bloom filter map. But if default value is going to be good enough for most cases and most cases won't need to adjust number of hashes, this seems to be pretty clean to me.
With having bloom filter map keys instead of values, I think this would lead to messier code in the kernel for handling
map_lookup_elemandmap_update_elemcalls, due to the fact that the bloom filter map is a non-associative map and the current APIs for non-associative map types (peek_elem/push_elem/pop_elem) all have the map data as the value and not the key.For example, for
map_update_elem, the API from the eBPF program side isint (*map_update_elem)(struct
bpf_map*map, void *key, void *value, u64 flags);This would require us to either
a) Add some custom logic in
syscall.cso that we bypass thecopy_from_bpfptrcall on
bloom filter map values (necessary because memcpying 0 bytes still requires the src pointer
to be valid), which would allow us to pass in a NULL valueb) Add a new function like
int (*map_push_key)(struct
bpf_map*map, void *key, u64 flags)that eBPF programs can call instead of
map_update_elem.or
c) Try to repurpose the existing
map_push_elemAPI by passing in the key instead of the value,
which would lead to inconsistent use of the API
Quoted 7 lines
I think if we could change the non-associative map types (currently only stack maps and queue
maps, I believe) to have their data be a key instead of a value, and have the pop/peek APIs use
keys instead of values, then this would be cleaner, since we could then just use the existing peek/pop
APIs.
I don't think we can change existing map APIs anymore, unfortunately.
Quoted 39 lines
Quoted 11 lines
Quoted 11 lines
Or use
inner_map_fdsince there is no possibility of having an inner map in bloomfilter. It could be a union:__u32 max_entries; /* max number of entries in a map */ __u32 map_flags; /* BPF_MAP_CREATE related* flags defined above. */
union {
__u32inner_map_fd; /* fd pointing to the inner map */
__u32nr_hash_funcs; /* or number of hash functions */
};Quoted 3 lines
This works as well. A bit more Bloom filter-only terminology throughout UAPI and libbpf, but I'd be fine with that as well.
Great, it looks like this is the consensus - I will go with this option for v3!
Quoted 15 lines
Quoted 13 lines
__u32 numa_node; /* numa node */kernel/bpf/bloom_filter.c line 28
Quoted 11 lines
kernel/bpf/bloom_filter.c new file +11
-0+struct bpf_bloom_filter { +struct bpf_map map; +u32 bit_array_mask; +u32 hash_seed; +/* If the size of the values in the bloom filter is u32 aligned, +* then it is more performant to use jhash2 as the underlying hash +* function, else we use jhash. This tracks the number of u32s +* in an u32-aligned value size. If the value size is not u32 aligned, +* this will be 0. +*/ +u32 aligned_u32_count;Quoted 2 lines
what is the performance difference?
May be we enforce 4-byte sized value for simplicity?Quoted 8 lines
This might be a bit too surprising, especially if keys are just some strings, where people might not expect that it has to 4-byte multiple size. And debugging this without extra tooling (like retsnoop) is going to be nightmarish.
If the performance diff is huge and that if/else logic is unacceptable, we can also internally pad with up to 3 zero bytes and include those into the hash.
Quoted 13 lines
I think the if/else logic is unavoidable if we support non 4-byte aligned value sizes,
unless we are okay with truncating any remainder bytes of non 4-byte aligned values
and stipulating that a bloom filter map value size has to be greater than 4 bytes (these
conditions would allow us to use jhash2 for every value without an if/else check). If we
internally pad, we will have to pad on every update and lookup, which would also
require an if/else.
Thanks for the comments and reviews, Alexei and Andrii. They are much appreciated!
I don't think truncation is an option. And I also forgot that we don't really store values, so there is nothing to pad, really. So yeah, I'd keep it as is, especially if that is not expensive (which I assume it's not). As I mentioned before, that logic can be encapsulated in a dedicated helper function and reused in a few places.
Re: [PATCH v2 bpf-next 1/4] bpf: Add bloom filter map implementation
In reply to Joanne KoongQuoted 89 lines · Joanne Koong
Bloom filters are a space-efficient probabilistic data structure used to quickly test whether an element exists in a set. In a bloom filter, false positives are possible whereas false negatives should never be.
This patch adds a bloom filter map for bpf programs. The bloom filter map supports peek (determining whether an element is present in the map) and push (adding an element to the map) operations.These operations are exposed to userspace applications through the already existing syscalls in the following way:
BPF_MAP_LOOKUP_ELEM-> peekBPF_MAP_UPDATE_ELEM-> pushThe bloom filter map does not have keys, only values. In light of this, the bloom filter map's API matches that of queue stack maps:
user applications use BPF_MAP_LOOKUP_ELEM/BPF_MAP_UPDATE_ELEM which correspond internally to bpf_map_peek_elem/bpf_map_push_elem, and bpf programs must use thebpf_map_peek_elemandbpf_map_push_elemAPIs to query or add an element to the bloom filter map. When the bloom filter map is created, it must be created with akey_sizeof 0.For updates, the user will pass in the element to add to the map as the value, with a NULL key. For lookups, the user will pass in the element to query in the map as the value. In the verifier layer, this requires us to modify the argument type of a bloom filter's
BPF_FUNC_map_peek_elemcall toARG_PTR_TO_MAP_VALUE; as well, in the syscall layer, we need to copy over the user value so that inbpf_map_peek_elem, we know which specific value to query.A few things to please take note of:
* If there are any concurrent lookups + updates, the user is
responsible for synchronizing this to ensure no false negative lookups occur.
* The number of hashes to use for the bloom filter is configurable from
userspace. If no number is specified, the default used will be 5 hash functions. The benchmarks later in this patchset can help compare the performance of using different number of hashes on different entry sizes. In general, using more hashes decreases the speed of a lookup, but increases the false positive rate of an element being detected in the bloom filter.
* Deleting an element in the bloom filter map is not supported.
* The bloom filter map may be used as an inner map.
* The "max_entries" size that is specified at map creation time is used to
approximate a reasonable bitmap size for the bloom filter, and is not otherwise strictly enforced. If the user wishes to insert more entries into the bloom filter than "max_entries", they may do so but they should be aware that this may lead to a higher false positive rate.Signed-off-by Joanne Koong <[email protected]>--- include/linux/bpf_types.h | 1 + include/uapi/linux/bpf.h | 10 ++ kernel/bpf/Makefile | 2 +- kernel/bpf/bloom_filter.c | 205 +++++++++++++++++++++++++++++++++ kernel/bpf/syscall.c | 14 ++- kernel/bpf/verifier.c | 19 ++- tools/include/uapi/linux/bpf.h | 10 ++ 7 files changed, 255 insertions(+), 6 deletions(-) create mode 100644 kernel/bpf/bloom_filter.cinclude/linux/bpf_types.h +1
-0@@ -125,6 +125,7 @@ BPF_MAP_TYPE(BPF_MAP_TYPE_STACK, stack_map_ops) BPF_MAP_TYPE(BPF_MAP_TYPE_STRUCT_OPS, bpf_struct_ops_map_ops)#endif BPF_MAP_TYPE(BPF_MAP_TYPE_RINGBUF, ringbuf_map_ops) +BPF_MAP_TYPE(BPF_MAP_TYPE_BLOOM_FILTER, bloom_filter_map_ops) BPF_LINK_TYPE(BPF_LINK_TYPE_RAW_TRACEPOINT, raw_tracepoint)BPF_LINK_TYPE (BPF_LINK_TYPE_TRACING , tracing)include/uapi/linux/bpf.h +1
-0@@ -906,6 +906,7 @@ enum bpf_map_type {BPF_MAP_TYPE_RINGBUF ,BPF_MAP_TYPE_INODE_STORAGE ,BPF_MAP_TYPE_TASK_STORAGE , + BPF_MAP_TYPE_BLOOM_FILTER, };/* Note that tracing related programs such as @@ -1210,6 +1211,15 @@ enum {/* Create a map that is suitable to be an inner map with dynamic max entries */
include/uapi/linux/bpf.h enum { · line 1222
Quoted 10 lines
include/uapi/linux/bpf.h +9
-0BPF_F_INNER_MAP = (1U <<12 ), + +/* For bloom filter maps, the next 4 bits represent how many hashes to use. +* The maximum number of hash functions supported is 15. If this is not set, +* the default number of hash functions used will be 5. +*/ +BPF_F_BLOOM_FILTER_HASH_BIT_1 = (1U <<13 ), +BPF_F_BLOOM_FILTER_HASH_BIT_2 = (1U <<14 ), +BPF_F_BLOOM_FILTER_HASH_BIT_3 = (1U <<15 ), + BPF_F_BLOOM_FILTER_HASH_BIT_4 = (1U << 16),
I didn't realize all those map_flags are sequentially numbered, but I guess we are not yet running out of space, so this might be ok. But to be usable from BPF program nicely, I would be better to define this as offset of a first hash bit and number of bits:
BPF_F_BLOOM_NR_HASH_OFF = 13,BPF_F_BLOOM_NR_HASH_CNT = 4,
So that in BPF map definiton in BPF program we could do
struct {
__uint(type, BPF_MAP_TYPE_BLOOM_FILTER),
...
__uint(map_flags, 5 << BPF_F_BLOOM_NR_HASH_OFF),
};It's still quite ugly, but given it's unlikely to be used very frequently, might be ok.
[...]
Quoted 7 lines
include/uapi/linux/bpf.h +7
-0+ +static int bloom_filter_map_peek_elem (struct bpf_map *map,void *value) +{ +struct bpf_bloom_filter *bloom_filter = + container_of(map, struct bpf_bloom_filter, map); +u32 hash; +u8 i;
include/uapi/linux/bpf.h enum {
Quoted 10 lines
include/uapi/linux/bpf.h +10
-0+ +for (i =0 ; i < bloom_filter->nr_hashes; i++) { +if (bloom_filter->aligned_u32_count) + hash =jhash2 (value, bloom_filter->aligned_u32_count, + bloom_filter->hash_seed + i) & + bloom_filter->bit_array_mask; +else + hash =jhash (value, map->value_size, + bloom_filter->hash_seed + i) & + bloom_filter->bit_array_mask;
this looks like a good candidate for helper function used in at least two places
Quoted 8 lines
include/uapi/linux/bpf.h +8
-0+ +if (!test_bit (hash, bloom_filter->bit_array)) +return -ENOENT ; + } + +return 0 ; +} +
[...]
PATCH v2 2/4 selftests/bpf: Add bloom filter map test cases
In reply to Joanne KoongThis patch adds test cases for bpf bloom filter maps. They include tests checking against invalid operations by userspace, tests for using the bloom filter map as an inner map, and a bpf program that queries the bloom filter map for values added by a userspace program.
--- .../bpf/prog_tests/bloom_filter_map.c | 177 ++++++++++++++++++ .../selftests/bpf/progs/bloom_filter_map.c | 82 ++++++++ 2 files changed, 259 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/bloom_filter_map.c create mode 100644 tools/testing/selftests/bpf/progs/bloom_filter_map.c
tools/testing/selftests/bpf/prog_tests/bloom_filter_map.c new file +177 -0
@@ -0,0 +1,177 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2021 Facebook */ + +#include <sys/syscall.h> +#include <test_progs.h> +#include "bloom_filter_map.skel.h" + +static void test_bloom_filter_map_fail (void ) +{ +struct bpf_create_map_attr xattr = { + .name ="bloom_filter_map" , + .map_type =BPF_MAP_TYPE_BLOOM_FILTER , + .max_entries =100 , + .value_size =sizeof (__u32), + .map_flags =BPF_F_BLOOM_FILTER_HASH_BIT_2 , + }; +__u32 value; +int fd, err; + +/* Invalid key size */ + xattr.key_size =4 ; + fd =bpf_create_map_xattr (&xattr); +if (!ASSERT_LT (fd,0 ,"bpf_create_map bloom filter invalid key size" )) +close (fd); + xattr.key_size =0 ; + +/* Invalid value size */ + xattr.value_size =0 ; + fd =bpf_create_map_xattr (&xattr); +if (!ASSERT_LT (fd,0 ,"bpf_create_map bloom filter invalid value size" )) +close (fd); + xattr.value_size =sizeof (__u32); + +/* Invalid max entries size */ + xattr.max_entries =0 ; + fd =bpf_create_map_xattr (&xattr); +if (!ASSERT_LT (fd,0 ,"bpf_create_map bloom filter invalid max entries size" )) +close (fd); + xattr.max_entries =100 ; + +/* Bloom filter maps do not support BPF_F_NO_PREALLOC */ + xattr.map_flags =BPF_F_NO_PREALLOC ; + fd =bpf_create_map_xattr (&xattr); +if (!ASSERT_LT (fd,0 ,"bpf_create_map bloom filter invalid flags" )) +close (fd); + xattr.map_flags =0 ; + + fd =bpf_create_map_xattr (&xattr); +if (!ASSERT_GE (fd,0 ,"bpf_create_map bloom filter" )) +return ; + +/* Test invalid flags */ + err =bpf_map_update_elem (fd,NULL , &value,-1 ); +ASSERT_EQ (err, -EINVAL ,"bpf_map_update_elem bloom filter invalid flags" ); + + err =bpf_map_update_elem (fd,NULL , &value,BPF_EXIST ); +ASSERT_EQ (err, -EINVAL ,"bpf_map_update_elem bloom filter invalid flags" ); + + err =bpf_map_update_elem (fd,NULL , &value,BPF_F_LOCK ); +ASSERT_EQ (err, -EINVAL ,"bpf_map_update_elem bloom filter invalid flags" ); + + err =bpf_map_update_elem (fd,NULL , &value,BPF_NOEXIST ); +ASSERT_EQ (err, -EINVAL ,"bpf_map_update_elem bloom filter invalid flags" ); + + err =bpf_map_update_elem (fd,NULL , &value,10000 ); +ASSERT_EQ (err, -EINVAL ,"bpf_map_update_elem bloom filter invalid flags" ); + +close (fd); +} + +static void bloom_filter_map (struct bloom_filter_map *skel) +{ +const int map_size =bpf_map__max_entries (skel->maps.map_random_data); +int err, map_random_data_fd, map_bloom_filter_fd, i; +__u64 val; +struct bpf_link *link; + + map_random_data_fd =bpf_map__fd (skel->maps.map_random_data); + map_bloom_filter_fd =bpf_map__fd (skel->maps.map_bloom_filter); + +/* Generate random values and add them to the maps */ +for (i =0 ; i < map_size; i++) { + val =rand (); + err =bpf_map_update_elem (map_random_data_fd, &i, &val,BPF_ANY ); +if (!ASSERT_OK (err,"Add random value to map_random_data" )) +continue ; + + err =bpf_map_update_elem (map_bloom_filter_fd,NULL , &val,0 ); +if (!ASSERT_OK (err,"Add random value to map_bloom_filter" )) +return ; + } + + link =bpf_program__attach (skel->progs.prog_bloom_filter); +if (!ASSERT_OK_PTR (link,"link" )) +return ; + +syscall (SYS_getpgid); + +ASSERT_EQ (skel->bss->error,0 ,"error" ); + +bpf_link__destroy (link); +} + +static void bloom_filter_inner_map (struct bloom_filter_map *skel) +{ +const int map_size =bpf_map__max_entries (skel->maps.map_random_data); +int outer_map_fd, inner_map_fd, map_random_data_fd, err, i, key =0 ; +struct bpf_create_map_attr xattr = { + .name ="bloom_filter_inner_map" , + .map_type =BPF_MAP_TYPE_BLOOM_FILTER , + .max_entries = map_size, + .value_size =sizeof (__u64), + }; +struct bpf_link *link; +__u64 val; + +/* Create a bloom filter map that will be used as the inner map */ + inner_map_fd =bpf_create_map_xattr (&xattr); +if (!ASSERT_GE (inner_map_fd,0 ,"bpf_create_map bloom filter map as inner map" )) +return ; + +/* Generate random values and add them to the maps */ + map_random_data_fd =bpf_map__fd (skel->maps.map_random_data); +for (i =0 ; i < map_size; i++) { + val =rand (); + err =bpf_map_update_elem (map_random_data_fd, &i, &val,BPF_ANY ); +if (!ASSERT_OK (err,"Add random value to map_random_data" )) +continue ; + + err =bpf_map_update_elem (inner_map_fd,NULL , &val,0 ); +if (!ASSERT_OK (err,"Add random value to inner_map_fd" )) + goto done; + } + + outer_map_fd =bpf_map__fd (skel->maps.outer_map); +/* Add the bloom filter map to the outer map */ + err =bpf_map_update_elem (outer_map_fd, &key, &inner_map_fd,0 ); +if (!ASSERT_OK (err,"Add bloom filter map to outer map" )) + goto done; + +/* Attach the bloom_filter_inner_map prog */ + link =bpf_program__attach (skel->progs.prog_bloom_filter_inner_map); +if (!ASSERT_OK_PTR (link,"link" )) + goto delete_inner_map; + +syscall (SYS_getpgid); + +ASSERT_EQ (skel->bss->error,0 ,"error" ); + +bpf_link__destroy (link); + +delete_inner_map: +/* Ensure the inner bloom filter map can be deleted */ + err =bpf_map_delete_elem (outer_map_fd, &key); +ASSERT_OK (err,"Delete inner bloom filter map" ); + +done: +close (inner_map_fd); +} + +void test_bloom_filter_map (void ) +{ +struct bloom_filter_map *skel; + +test_bloom_filter_map_fail (); + + skel =bloom_filter_map__open_and_load (); +if (!ASSERT_OK_PTR (skel,"bloom_filter_map__open_and_load" )) + goto cleanup; + +bloom_filter_map (skel); + +bloom_filter_inner_map (skel); + +cleanup: +bloom_filter_map__destroy (skel); +}
tools/testing/selftests/bpf/progs/bloom_filter_map.c new file +82 -0
82 lines not on this page · raw message
Signature · 1 line
2.30.2
PATCH v2 3/4 bpf/benchs: Add benchmark test for bloom filter maps
In reply to Joanne KoongThis patch adds benchmark tests for the throughput and false positive rate of bloom filter map lookups for a given number of entries and a given number of hash functions.
These benchmarks show that as the number of hash functions increases, the throughput and the false positive rate of the bloom filter map decreases. From the benchmark data, the approximate average false-positive rates are roughly as follows:
1 hash function = ~30%
2 hash functions = ~15%
3 hash functions = ~5%
4 hash functions = ~2.5%
5 hash functions = ~1%
6 hash functions = ~0.5%
7 hash functions = ~0.35%
8 hash functions = ~0.15%
9 hash functions = ~0.1%
10 hash functions = ~0%
--- tools/testing/selftests/bpf/Makefile | 4 +- tools/testing/selftests/bpf/bench.c | 35 ++ tools/testing/selftests/bpf/bench.h | 3 + .../bpf/benchs/bench_bloom_filter_map.c | 354 ++++++++++++++++++ .../bpf/benchs/run_bench_bloom_filter_map.sh | 28 ++ .../bpf/benchs/run_bench_ringbufs.sh | 30 +- .../selftests/bpf/benchs/run_common.sh | 48 +++ .../selftests/bpf/progs/bloom_filter_map.c | 74 ++++ 8 files changed, 547 insertions(+), 29 deletions(-) create mode 100644 tools/testing/selftests/bpf/benchs/bench_bloom_filter_map.c create mode 100755 tools/testing/selftests/bpf/benchs/run_bench_bloom_filter_map.sh create mode 100644 tools/testing/selftests/bpf/benchs/run_common.sh
tools/testing/selftests/bpf/Makefile +3 -1
@@ -519,13 +519,15 @@ $(OUTPUT)/bench_rename.o: $(OUTPUT)/test_overhead.skel.h $(OUTPUT)/bench_trigger.o: $(OUTPUT)/trigger_bench.skel.h $(OUTPUT)/bench_ringbufs.o: $(OUTPUT)/ringbuf_bench.skel.h \ $(OUTPUT)/perfbuf_bench.skel.h +$(OUTPUT)/bench_bloom_filter_map.o: $(OUTPUT)/bloom_filter_map.skel.h $(OUTPUT)/bench.o: bench.h testing_helpers.h $(OUTPUT)/bench: LDLIBS += -lm $(OUTPUT)/bench: $(OUTPUT)/bench.o $(OUTPUT)/testing_helpers.o \ $(OUTPUT)/bench_count.o \ $(OUTPUT)/bench_rename.o \ $(OUTPUT)/bench_trigger.o \ - $(OUTPUT)/bench_ringbufs.o + $(OUTPUT)/bench_ringbufs.o \ + $(OUTPUT)/bench_bloom_filter_map.o $(call msg,BINARY,,$@) $(Q)$(CC) $(LDFLAGS) -o $@ $(filter %.a %.o,$^) $(LDLIBS)
tools/testing/selftests/bpf/bench.c +35 -0
@@ -51,6 +51,35 @@ void setup_libbpf()fprintf (stderr,"failed to increase RLIMIT_MEMLOCK: %d" , err); } +void false_hits_report_progress (int iter,struct bench_res *res,long delta_ns) +{ +long total = res->false_hits + res->hits + res->drops; + +printf ("Iter %3d (%7.3lfus): " , + iter, (delta_ns -1000000000 ) /1000.0 ); + +printf ("%ld false hits of %ld total operations. Percentage = %2.2f %%\n" , + res->false_hits, total, ((float )res->false_hits / total) *100 ); +} + +void false_hits_report_final (struct bench_res res[],int res_cnt) +{ +long total_hits =0 , total_drops =0 , total_false_hits =0 , total_ops =0 ; +int i; + +for (i =0 ; i < res_cnt; i++) { + total_hits += res[i].hits; + total_false_hits += res[i].false_hits; + total_drops += res[i].drops; + } + total_ops = total_hits + total_false_hits + total_drops; + +printf ("Summary: %ld false hits of %ld total operations. " , + total_false_hits, total_ops); +printf ("Percentage = %2.2f %%\n" , + ((float )total_false_hits / total_ops) *100 ); +} +void hits_drops_report_progress (int iter,struct bench_res *res,long delta_ns) {double hits_per_sec, drops_per_sec;
@@ -132,9 +161,11 @@ static const struct argp_option opts[] = { };extern struct argp bench_ringbufs_argp; +extern struct argp bench_bloom_filter_map_argp;static const struct argp_child bench_parsers[] = { { &bench_ringbufs_argp,0 ,"Ring buffers benchmark" ,0 }, + { &bench_bloom_filter_map_argp,0 ,"Bloom filter map benchmark" ,0 }, {}, };
@@ -323,6 +354,8 @@ extern const struct bench bench_rb_libbpf;extern const struct bench bench_rb_custom;extern const struct bench bench_pb_libbpf;extern const struct bench bench_pb_custom; +extern const struct bench bench_bloom_filter_map; +extern const struct bench bench_bloom_filter_false_positive;static const struct bench *benchs[] = { &bench_count_global,
@@ -344,6 +377,8 @@ static const struct bench *benchs[] = { &bench_rb_custom, &bench_pb_libbpf, &bench_pb_custom, + &bench_bloom_filter_map, + &bench_bloom_filter_false_positive, };static void setup_benchmark ()
tools/testing/selftests/bpf/bench.h +3 -0
@@ -33,6 +33,7 @@ struct env {struct bench_res {long hits;long drops; +long false_hits; };struct bench {
@@ -56,6 +57,8 @@ extern const struct bench *bench;void setup_libbpf ();void hits_drops_report_progress (int iter,struct bench_res *res,long delta_ns);void hits_drops_report_final (struct bench_res res[],int res_cnt); +void false_hits_report_progress (int iter,struct bench_res *res,long delta_ns); +void false_hits_report_final (struct bench_res res[],int res_cnt);static inline __u64 get_time_ns () {struct timespec t;
tools/testing/selftests/bpf/benchs/bench_bloom_filter_map.c new file +354 -0
354 lines not on this page · raw message
tools/testing/selftests/bpf/benchs/run_bench_bloom_filter_map.sh new file +28 -0
@@ -0,0 +1,28 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0 + +source ./benchs/run_common.sh + +set -eufo pipefail + +header "Bloom filter map" +for t in 1 4 8; do +for h in {1..10}; do +subtitle "# threads: $t, # hashes: $h" + for e in 10000 50000 75000 100000 250000 500000 750000 1000000 2500000 5000000; do + printf "%'d entries -\n" $e + printf "\t" + summarize "Total operations: " \ + "$($RUN_BENCH -p $t --nr_hashes $h --nr_entries $e bloom-filter-map)" + printf "\t" + summarize_percentage "False positive rate: " \ + "$($RUN_BENCH -p $t --nr_hashes $h --nr_entries $e bloom-filter-false-positive)" + done + printf "\n" +done +done + +header "Bloom filter map, multi-producer contention" +for t in 1 2 3 4 8 12 16 20 24 28 32 36 40 44 48 52; do + summarize "$t threads - " "$($RUN_BENCH -p $t bloom-filter-map)" +done
tools/testing/selftests/bpf/benchs/run_bench_ringbufs.sh +2 -28
@@ -1,34 +1,8 @@ #!/bin/bash -set -eufo pipefail - -RUN_BENCH="sudo ./bench -w3 -d10 -a" - -function hits() -{ - echo "$*" | sed -E "s/.*hits\s+([0-9]+\.[0-9]+ ± [0-9]+\.[0-9]+M\/s).*/\1/" -} - -function drops() -{ - echo "$*" | sed -E "s/.*drops\s+([0-9]+\.[0-9]+ ± [0-9]+\.[0-9]+M\/s).*/\1/" -} +source ./benchs/run_common.sh -function header() -{ - local len=${#1} - - printf "\n%s\n" "$1" - for i in $(seq 1 $len); do printf '='; done - printf '\n' -} - -function summarize() -{ - bench="$1" - summary=$(echo $2 | tail -n1) - printf "%-20s %s (drops %s)\n" "$bench" "$(hits $summary)" "$(drops $summary)" -} +set -eufo pipefail header "Single-producer, parallel producer" for b in rb-libbpf rb-custom pb-libbpf pb-custom; do
tools/testing/selftests/bpf/benchs/run_common.sh new file +48 -0
48 lines not on this page · raw message
tools/testing/selftests/bpf/progs/bloom_filter_map.c +74 -0
98 lines not on this page · raw message
Signature · 1 line
2.30.2
PATCH v2 4/4 bpf/benchs: Add benchmarks for comparing hashmap lookups with vs. without bloom filter
In reply to Joanne KoongThis patch adds benchmark tests for comparing the performance of hashmap lookups without the bloom filter vs. hashmap lookups with the bloom filter.
Checking the bloom filter first for whether the element exists should overall enable a higher throughput for hashmap lookups, since if the element does not exist in the bloom filter, we can avoid a costly lookup in the hashmap.
On average, using 5 hash functions in the bloom filter tended to perform the best across the widest range of different entry sizes. The benchmark results using 5 hash functions (running on 8 threads on a machine with one numa node, and taking the average of 3 runs) were roughly as follows:
value_size = 4 bytes -
10k entries: 30% faster 50k entries: 50% faster 100k entries: 55% faster 500k entres: 80% faster 1 million entries: 120% faster 5 million entries: 135% faster
value_size = 8 bytes -
10k entries: 35% faster 50k entries: 55% faster 100k entries: 70% faster 500k entres: 110% faster 1 million entries: 215% faster 5 million entries: 215% faster
value_size = 16 bytes -
10k entries: 5% slower 50k entries: 25% faster 100k entries: 35% faster 500k entres: 105% faster 1 million entries: 130% faster 5 million entries: 105% faster
value_size = 40 bytes -
10k entries: 5% slower 50k entries: 10% faster 100k entries: 20% faster 500k entres: 45% faster 1 million entries: 60% faster 5 million entries: 75% faster
--- tools/testing/selftests/bpf/bench.c | 22 ++++++++--- .../bpf/benchs/bench_bloom_filter_map.c | 39 +++++++++++++++++++ .../bpf/benchs/run_bench_bloom_filter_map.sh | 15 +++++++ .../selftests/bpf/benchs/run_common.sh | 12 ++++++ 4 files changed, 83 insertions(+), 5 deletions(-)
tools/testing/selftests/bpf/bench.c +17 -5
@@ -92,20 +92,21 @@ void hits_drops_report_progress(int iter, struct bench_res *res, long delta_ns)printf ("Iter %3d (%7.3lfus): " , iter, (delta_ns -1000000000 ) /1000.0 ); -printf ("hits %8.3lfM/s (%7.3lfM/prod), drops %8.3lfM/s\n" , - hits_per_sec, hits_per_prod, drops_per_sec); +printf ("hits %8.3lfM/s (%7.3lfM/prod), drops %8.3lfM/s, total operations %8.3lfM/s\n" , + hits_per_sec, hits_per_prod, drops_per_sec, hits_per_sec + drops_per_sec); }void hits_drops_report_final (struct bench_res res[],int res_cnt) {int i; -double hits_mean =0.0 , drops_mean =0.0 ; -double hits_stddev =0.0 , drops_stddev =0.0 ; +double hits_mean =0.0 , drops_mean =0.0 , total_ops_mean =0.0 ; +double hits_stddev =0.0 , drops_stddev =0.0 , total_ops_stddev =0.0 ;for (i =0 ; i < res_cnt; i++) { hits_mean += res[i].hits /1000000.0 / (0.0 + res_cnt); drops_mean += res[i].drops /1000000.0 / (0.0 + res_cnt); } + total_ops_mean = hits_mean + drops_mean;if (res_cnt >1 ) {for (i =0 ; i < res_cnt; i++) {
@@ -115,14 +116,21 @@ void hits_drops_report_final(struct bench_res res[], int res_cnt) drops_stddev += (drops_mean - res[i].drops /1000000.0 ) * (drops_mean - res[i].drops /1000000.0 ) / (res_cnt -1.0 ); + total_ops_stddev += (total_ops_mean - + (res[i].hits + res[i].drops) /1000000.0 ) * + (total_ops_mean - (res[i].hits + res[i].drops) /1000000.0 ) + / (res_cnt -1.0 ); } hits_stddev =sqrt (hits_stddev); drops_stddev =sqrt (drops_stddev); + total_ops_stddev =sqrt (total_ops_stddev); }printf ("Summary: hits %8.3lf \u00B1 %5.3lfM/s (%7.3lfM/prod), " , hits_mean, hits_stddev, hits_mean / env.producer_cnt); -printf ("drops %8.3lf \u00B1 %5.3lfM/s\n" , +printf ("drops %8.3lf \u00B1 %5.3lfM/s, " , drops_mean, drops_stddev); +printf ("total operations %8.3lf \u00B1 %5.3lfM/s\n" , + total_ops_mean, total_ops_stddev); }const char *argp_program_version ="benchmark" ;
@@ -356,6 +364,8 @@ extern const struct bench bench_pb_libbpf;extern const struct bench bench_pb_custom;extern const struct bench bench_bloom_filter_map;extern const struct bench bench_bloom_filter_false_positive; +extern const struct bench bench_hashmap_without_bloom_filter; +extern const struct bench bench_hashmap_with_bloom_filter;static const struct bench *benchs[] = { &bench_count_global,
@@ -379,6 +389,8 @@ static const struct bench *benchs[] = { &bench_pb_custom, &bench_bloom_filter_map, &bench_bloom_filter_false_positive, + &bench_hashmap_without_bloom_filter, + &bench_hashmap_with_bloom_filter, };static void setup_benchmark ()
tools/testing/selftests/bpf/benchs/bench_bloom_filter_map.c +39 -0
@@ -251,6 +251,23 @@ static void hashmap_lookup_setup(void) } } +static void hashmap_no_bloom_filter_setup (void ) +{ +struct bpf_link *link; + + ctx.skel =setup_skeleton (); + + ctx.skel->data->hashmap_use_bloom_filter = false; + +populate_maps (); + + link =bpf_program__attach (ctx.skel->progs.prog_bloom_filter_hashmap_lookup); +if (!link) { +fprintf (stderr,"failed to attach program!\n" ); +exit (1 ); + } +} +static void measure (struct bench_res *res) {long total_hits =0 , total_drops =0 , total_false_hits =0 ;
@@ -352,3 +369,25 @@ const struct bench bench_bloom_filter_false_positive = { .report_progress = false_hits_report_progress, .report_final = false_hits_report_final, }; + +const struct bench bench_hashmap_without_bloom_filter = { + .name ="hashmap-without-bloom-filter" , + .validate = validate, + .setup = hashmap_no_bloom_filter_setup, + .producer_thread = producer, + .consumer_thread = consumer, + .measure = measure, + .report_progress = hits_drops_report_progress, + .report_final = hits_drops_report_final, +}; + +const struct bench bench_hashmap_with_bloom_filter = { + .name ="hashmap-with-bloom-filter" , + .validate = validate, + .setup = hashmap_lookup_setup, + .producer_thread = producer, + .consumer_thread = consumer, + .measure = measure, + .report_progress = hits_drops_report_progress, + .report_final = hits_drops_report_final, +};
tools/testing/selftests/bpf/benchs/run_bench_bloom_filter_map.sh +15 -0
@@ -26,3 +26,18 @@ header "Bloom filter map, multi-producer contention" for t in 1 2 3 4 8 12 16 20 24 28 32 36 40 44 48 52; do summarize "$t threads - " "$($RUN_BENCH -p $t bloom-filter-map)" done + +header "Hashmap without bloom filter vs. hashmap with bloom filter (throughput, 8 threads)" +for h in {1..10}; do +subtitle "# hashes: $h" + for e in 10000 50000 75000 100000 250000 500000 750000 1000000 2500000 5000000; do + printf "%'d entries -\n" $e + printf "\t" + summarize_total "Hashmap without bloom filter: " \ + "$($RUN_BENCH --nr_hashes $h --nr_entries $e -p 8 hashmap-without-bloom-filter)" + printf "\t" + summarize_total "Hashmap with bloom filter: " \ + "$($RUN_BENCH --nr_hashes $h --nr_entries $e -p 8 hashmap-with-bloom-filter)" + done + printf "\n" +done
tools/testing/selftests/bpf/benchs/run_common.sh +12 -0
@@ -33,6 +33,11 @@ function percentage() echo "$*" | sed -E "s/.*Percentage\s=\s+([0-9]+\.[0-9]+).*/\1/" } +function total() +{ + echo "$*" | sed -E "s/.*total operations\s+([0-9]+\.[0-9]+ ± [0-9]+\.[0-9]+M\/s).*/\1/" +} + function summarize() { bench="$1"
@@ -46,3 +51,10 @@ function summarize_percentage() summary=$(echo $2 | tail -n1) printf "%-20s %s%%\n" "$bench" "$(percentage $summary)" } + +function summarize_total() +{ + bench="$1" + summary=$(echo $2 | tail -n1) + printf "%-20s %s\n" "$bench" "$(total $summary)" +}
Signature · 1 line
2.30.2