From: David Laight <david.laight.linux@gmail.com>
To: Feng Jiang <jiangfeng@kylinos.cn>
Cc: Andy Shevchenko <andriy.shevchenko@intel.com>,
pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu,
alex@ghiti.fr, kees@kernel.org, andy@kernel.org,
akpm@linux-foundation.org, ebiggers@kernel.org,
martin.petersen@oracle.com, ardb@kernel.org,
ajones@ventanamicro.com, conor.dooley@microchip.com,
samuel.holland@sifive.com, linus.walleij@linaro.org,
nathan@kernel.org, linux-riscv@lists.infradead.org,
linux-kernel@vger.kernel.org, linux-hardening@vger.kernel.org
Subject: Re: [PATCH v2 08/14] lib/string_kunit: add performance benchmark for strlen()
Date: Thu, 15 Jan 2026 10:40:45 +0000 [thread overview]
Message-ID: <20260115104045.30d385b7@pumpkin> (raw)
In-Reply-To: <8bf4c689-fee5-4f6b-b79e-854249a897d0@kylinos.cn>
On Thu, 15 Jan 2026 14:24:16 +0800
Feng Jiang <jiangfeng@kylinos.cn> wrote:
> On 2026/1/14 18:21, David Laight wrote:
> > On Wed, 14 Jan 2026 15:04:58 +0800
> > Feng Jiang <jiangfeng@kylinos.cn> wrote:
> >
> >> On 2026/1/14 14:14, Feng Jiang wrote:
> >>> On 2026/1/13 16:46, Andy Shevchenko wrote:
> >>>> On Tue, Jan 13, 2026 at 04:27:42PM +0800, Feng Jiang wrote:
> >>>>> Introduce a benchmark to compare the architecture-optimized strlen()
> >>>>> implementation against the generic C version (__generic_strlen).
> >>>>>
> >>>>> The benchmark uses a table-driven approach to evaluate performance
> >>>>> across different string lengths (short, medium, and long). It employs
> >>>>> ktime_get() for timing and get_random_bytes() followed by null-byte
> >>>>> filtering to generate test data that prevents early termination.
> >>>>>
> >>>>> This helps in quantifying the performance gains of architecture-specific
> >>>>> optimizations on various platforms.
> > ...
> >> Preliminary results with this change look much more reasonable:
> >>
> >> ok 4 string_test_strlen
> >> # string_test_strlen_bench: strlen performance (short, len: 8, iters: 100000):
> >> # string_test_strlen_bench: arch-optimized: 4767500 ns
> >> # string_test_strlen_bench: generic C: 5815800 ns
> >> # string_test_strlen_bench: speedup: 1.21x
> >> # string_test_strlen_bench: strlen performance (medium, len: 64, iters: 100000):
> >> # string_test_strlen_bench: arch-optimized: 6573600 ns
> >> # string_test_strlen_bench: generic C: 16342500 ns
> >> # string_test_strlen_bench: speedup: 2.48x
> >> # string_test_strlen_bench: strlen performance (long, len: 2048, iters: 10000):
> >> # string_test_strlen_bench: arch-optimized: 7931000 ns
> >> # string_test_strlen_bench: generic C: 35347300 ns
> >> # string_test_strlen_bench: speedup: 4.45x
> >
> > That is far too long.
> > In 35ms you are including a lot of timer interrupts.
> > You are also just testing the 'hot cache' case.
> > The kernel runs 'cold cache' a lot of the time - especially for instructions.
> >
> > To time short loops (or even single passes) you need a data dependency
> > between the 'start time' and the code being tested (easy enough, just add
> > (time & non_compile_time_zero) to a parameter), and between the result of
> > the code and the 'end time' - somewhat harder (doable in x86 if you use
> > the pmc cycle counter).
>
> Hi David,
>
> I appreciate the feedback! You're absolutely right that 35ms is quite long; it
> was measured in a TCG environment, and on real hardware (ARM64 KVM), it's
> actually an order of magnitude faster. I'll definitely tighten the iterations
> in v3 to avoid potential noise.
Doing time-based measurements on anything but real hardware is pointless.
(It is even problematic on some real hardware because the cpu clock speed
changes dynamically - which is why I've started using the x86 pmc to count
actual clock cycles.)
You only really need enough iterations to get enough 'ticks' from the
timer for the answer to make sense.
Other effects mean you can't really quote values to even 1% - so 100 ticks
from the timer is more than enough.
I'm not sure what the resolution of ktime_get_ns() is (will be hardware
dependant)
You are better off running the test a few times and using the best value.
Also you don't need to do a very long test to show a x4 improvement!
To see how good an algorithm really is you really need to work out the
'fixed cost' and 'cost per byte' in 'clocks' and 'clocks per byte'
(or 'bytes per clock') although they can be 'noisy' for short lengths.
The latter tells you how near to 'optimal' the algorithm is and lets you
compare results between different cpus (eg Zen-5 v i7-12xxx).
For instance the x86-64 IP checksum code (nominally 16bit add with carry)
actually runs at more than 8 bytes/clock on most cpu (IIRC it manages 12
but not 16).
>
> For the more advanced suggestions like cold cache and data dependency, I can
> see how they would make the benchmark much more rigorous. My plan is to follow
> the pattern in crc_benchmark() to refine the logic, as I feel this approach is
> simple, easy to maintain, and provides a good enough baseline for our needs.
>
> While I understand that simulating a cold cache would be more precise, I'm
> concerned it might introduce significant complexity at this stage. I hope the
> current focus on hot-path throughput is a reasonable starting point for a
> general KUnit test.
>
I've only done 'cold cache' testing in userspace - counting the actual
clocks for the each call (the first value is cold cache).
Gives a massive difference for large functions like blake2s where the
unrolled loop is somewhat faster, but for the cold-cache it is only
worth it for buffers over (about) 8k (and that might be worse if the
cpu is running at full speed which makes the memory effectively slower).
The other issue with running a test multiple times is that the branch
predictor will correctly predict all the branches.
So something like memcpy() which might have different code for different
lengths will always pick the correct one.
Branch mis-prediction seems to cost about 20 clocks on my zen-5.
Anyway, some measurements are better than none.
David
next prev parent reply other threads:[~2026-01-15 10:40 UTC|newest]
Thread overview: 36+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-13 8:27 [PATCH v2 00/14] riscv: optimize string functions and add kunit tests Feng Jiang
2026-01-13 8:27 ` [PATCH v2 01/14] lib/string: extract generic strlen() into __generic_strlen() Feng Jiang
2026-01-13 8:33 ` Andy Shevchenko
2026-01-14 0:01 ` Eric Biggers
2026-01-14 1:41 ` Feng Jiang
2026-01-14 7:07 ` Andy Shevchenko
2026-01-14 10:10 ` David Laight
2026-01-15 6:50 ` Feng Jiang
2026-01-15 6:55 ` Andy Shevchenko
2026-01-13 8:27 ` [PATCH v2 02/14] lib/string: extract generic strnlen() into __generic_strnlen() Feng Jiang
2026-01-13 8:27 ` [PATCH v2 03/14] lib/string: extract generic strchr() into __generic_strchr() Feng Jiang
2026-01-13 8:27 ` [PATCH v2 04/14] lib/string: extract generic strrchr() into __generic_strrchr() Feng Jiang
2026-01-13 8:27 ` [PATCH v2 05/14] lib/string_kunit: add correctness test for strlen Feng Jiang
2026-01-13 8:27 ` [PATCH v2 06/14] lib/string_kunit: add correctness test for strnlen Feng Jiang
2026-01-13 8:41 ` Andy Shevchenko
2026-01-13 8:27 ` [PATCH v2 07/14] lib/string_kunit: add correctness test for strrchr() Feng Jiang
2026-01-13 8:27 ` [PATCH v2 08/14] lib/string_kunit: add performance benchmark for strlen() Feng Jiang
2026-01-13 8:46 ` Andy Shevchenko
2026-01-14 6:14 ` Feng Jiang
2026-01-14 7:04 ` Feng Jiang
2026-01-14 7:21 ` Andy Shevchenko
2026-01-14 8:05 ` Feng Jiang
2026-01-14 10:21 ` David Laight
2026-01-15 6:24 ` Feng Jiang
2026-01-15 10:40 ` David Laight [this message]
2026-01-18 11:11 ` kernel test robot
2026-01-13 8:27 ` [PATCH v2 09/14] lib/string_kunit: add performance benchmark for strnlen() Feng Jiang
2026-01-13 8:27 ` [PATCH v2 10/14] lib/string_kunit: add performance benchmark for strchr() Feng Jiang
2026-01-13 8:27 ` [PATCH v2 11/14] lib/string_kunit: add performance benchmark for strrchr() Feng Jiang
2026-01-13 8:27 ` [PATCH v2 12/14] riscv: lib: add strnlen implementation Feng Jiang
2026-01-13 8:48 ` Andy Shevchenko
2026-01-13 8:27 ` [PATCH v2 13/14] riscv: lib: add strchr implementation Feng Jiang
2026-01-13 8:27 ` [PATCH v2 14/14] riscv: lib: add strrchr implementation Feng Jiang
2026-01-13 8:52 ` [PATCH v2 00/14] riscv: optimize string functions and add kunit tests Andy Shevchenko
2026-01-15 4:43 ` Joel Stanley
2026-01-19 9:24 ` Feng Jiang
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260115104045.30d385b7@pumpkin \
--to=david.laight.linux@gmail.com \
--cc=ajones@ventanamicro.com \
--cc=akpm@linux-foundation.org \
--cc=alex@ghiti.fr \
--cc=andriy.shevchenko@intel.com \
--cc=andy@kernel.org \
--cc=aou@eecs.berkeley.edu \
--cc=ardb@kernel.org \
--cc=conor.dooley@microchip.com \
--cc=ebiggers@kernel.org \
--cc=jiangfeng@kylinos.cn \
--cc=kees@kernel.org \
--cc=linus.walleij@linaro.org \
--cc=linux-hardening@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=martin.petersen@oracle.com \
--cc=nathan@kernel.org \
--cc=palmer@dabbelt.com \
--cc=pjw@kernel.org \
--cc=samuel.holland@sifive.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox