bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [RFC PATCH bpf-next 0/3] support string key for hash-table
@ 2021-12-19  5:22 Hou Tao
  2021-12-19  5:22 ` [RFC PATCH bpf-next 1/3] bpf: factor out helpers for htab bucket and element lookup Hou Tao
                   ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: Hou Tao @ 2021-12-19  5:22 UTC (permalink / raw)
  To: Alexei Starovoitov
  Cc: Martin KaFai Lau, Yonghong Song, Song Liu, Daniel Borkmann,
	Andrii Nakryiko, netdev, bpf, houtao1, yunbo.xufeng

Hi,

In order to use string as hash-table key, key_size must be the storage
size of longest string. If there are large differencies in string
length, the hash distribution will be sub-optimal due to the unused
zero bytes in shorter strings and the lookup will be inefficient due to
unnecessary memcpy().

Also it is possible the unused part of string key returned from bpf helper
(e.g. bpf_d_path) is not mem-zeroed and if using it directly as lookup key,
the lookup will fail with -ENOENT (as reported in [1]).

The patchset tries to address the inefficiency by adding support for
string key. During the key comparison, the string length is checked
first to reduce the uunecessary memcmp. Also update the hash function
from jhash() to full_name_hash() to reduce hash collision of string key.

There are about 16% and 106% improvment in benchmark under x86-64 and
arm64 when key_size is 256. About 45% and %161 when key size is greater
than 1024.

Also testing the performance improvment by using all files under linux
kernel sources as the string key input. There are about 74k files and the
maximum string length is 101. When key_size is 104, there are about 9%
and 35% win under x86-64 and arm64 in lookup performance, and when key_size
is 256, the win increases to 78% and 109% respectively.

Beside the optimization of lookup for string key, it seems that the
allocated space for BPF_F_NO_PREALLOC-case can also be optimized. More
trials and tests will be conducted if the idea of string key is accepted.

Comments are always welcome.

Regards,
Tao

[1]: https://lore.kernel.org/bpf/20211120051839.28212-2-yunbo.xufeng@linux.alibaba.com

Hou Tao (3):
  bpf: factor out helpers for htab bucket and element lookup
  bpf: add BPF_F_STR_KEY to support string key in htab
  selftests/bpf: add benchmark for string-key hash-table

 include/uapi/linux/bpf.h                      |   3 +
 kernel/bpf/hashtab.c                          | 448 +++++++++++-------
 tools/include/uapi/linux/bpf.h                |   3 +
 tools/testing/selftests/bpf/Makefile          |   4 +-
 tools/testing/selftests/bpf/bench.c           |  10 +
 .../selftests/bpf/benchs/bench_str_htab.c     | 255 ++++++++++
 .../testing/selftests/bpf/benchs/run_htab.sh  |  14 +
 .../selftests/bpf/progs/str_htab_bench.c      | 123 +++++
 8 files changed, 685 insertions(+), 175 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/benchs/bench_str_htab.c
 create mode 100755 tools/testing/selftests/bpf/benchs/run_htab.sh
 create mode 100644 tools/testing/selftests/bpf/progs/str_htab_bench.c

-- 
2.29.2


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2021-12-23 16:37 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-19  5:22 [RFC PATCH bpf-next 0/3] support string key for hash-table Hou Tao
2021-12-19  5:22 ` [RFC PATCH bpf-next 1/3] bpf: factor out helpers for htab bucket and element lookup Hou Tao
2021-12-21 19:32   ` Yonghong Song
2021-12-19  5:22 ` [RFC PATCH bpf-next 2/3] bpf: add BPF_F_STR_KEY to support string key in htab Hou Tao
2021-12-19  5:22 ` [RFC PATCH bpf-next 3/3] selftests/bpf: add benchmark for string-key hash-table Hou Tao
2021-12-20  3:00 ` [RFC PATCH bpf-next 0/3] support string key for hash-table Alexei Starovoitov
2021-12-23 12:02   ` Hou Tao
2021-12-23 16:36     ` Yonghong Song

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).