All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andy Shevchenko <andriy.shevchenko@intel.com>
To: Feng Jiang <jiangfeng@kylinos.cn>
Cc: pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu,
	alex@ghiti.fr, akpm@linux-foundation.org, kees@kernel.org,
	andy@kernel.org, ebiggers@kernel.org, martin.petersen@oracle.com,
	ardb@kernel.org, charlie@rivosinc.com,
	conor.dooley@microchip.com, ajones@ventanamicro.com,
	linus.walleij@linaro.org, nathan@kernel.org,
	linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org,
	linux-hardening@vger.kernel.org, Joel Stanley <joel@jms.id.au>
Subject: Re: [PATCH v3 4/8] lib/string_kunit: add performance benchmarks for strlen
Date: Tue, 20 Jan 2026 09:46:49 +0200	[thread overview]
Message-ID: <aW8y6eC7ZGRYIWkn@smile.fi.intel.com> (raw)
In-Reply-To: <20260120065852.166857-5-jiangfeng@kylinos.cn>

On Tue, Jan 20, 2026 at 02:58:48PM +0800, Feng Jiang wrote:
> Introduce a benchmarking framework to the string_kunit test suite to
> measure the execution efficiency of string functions.
> 
> The implementation is inspired by crc_benchmark(), measuring throughput
> (MB/s) and latency (ns/call) across a range of string lengths. It
> includes a warm-up phase, disables preemption during measurement, and
> uses a fixed seed for reproducible results.
> 
> This allows for comparing different implementations (e.g., generic C vs.
> architecture-optimized assembly) within the KUnit environment.
> 
> Initially, provide benchmarks for strlen().

...

> +#define STRING_BENCH_SEED	888
> +#define STRING_BENCH_WORKLOAD	1000000UL

Can also be (1 * MEGA) from units.h.

...

> +static const size_t bench_lens[] = {
> +	0, 1, 7, 8, 16, 31, 64, 127, 512, 1024, 3173, 4096

Leave trailing comma.

> +};

...

> +static void *alloc_max_bench_buffer(struct kunit *test,
> +		const size_t *lens, size_t count, size_t *buf_len)
> +{
> +	void *buf;
> +	size_t i, max_len = 0;
> +
> +	for (i = 0; i < count; i++) {
> +		if (max_len < lens[i])
> +			max_len = lens[i];
> +	}
> +
> +	/* Add space for NUL terminator */
> +	max_len += 1;

> +	buf = kunit_kzalloc(test, max_len, GFP_KERNEL);
> +	if (buf && buf_len)
> +		*buf_len = max_len;
> +
> +	return buf;

	if (!buf)
		return NULL;

	*buf_len ...
	return buf;

> +}

...

> +static void fill_random_string(char *buf, size_t len)
> +{
> +	size_t i;
> +	struct rnd_state state;

Reversed xmas tree ordering?

> +	if (!buf || !len)
> +		return;
> +
> +	/* Use a fixed seed to ensure deterministic benchmark results */
> +	prandom_seed_state(&state, 888);
> +	prandom_bytes_state(&state, buf, len);
> +
> +	/* Replace null bytes to avoid early string termination */
> +	for (i = 0; i < len; i++) {
> +		if (buf[i] == '\0')
> +			buf[i] = 0x01;
> +	}
> +
> +	buf[len - 1] = '\0';
> +}

...

> +#define STRING_BENCH(iters, func, ...)					\

Is this same / similar code to crc_benchmark()? Perhaps we need to have KUnit
provided macro / environment to perform such tests... Have you talked to KUnit
people about all this?

> +({									\
> +	u64 __bn_t;							\
> +	size_t __bn_i;							\
> +	size_t __bn_iters = (iters);					\
> +	size_t __bn_warm_iters = max_t(size_t, __bn_iters / 10, 50U);	\

Try to avoid max_t() as much as possible. Wouldn't max() suffice?

> +	/* Volatile function pointer prevents dead code elimination */	\
> +	typeof(func) (* volatile __func) = (func);			\
> +									\
> +	for (__bn_i = 0; __bn_i < __bn_warm_iters; __bn_i++)		\
> +		(void)__func(__VA_ARGS__);				\
> +									\
> +	preempt_disable();						\
> +	__bn_t = ktime_get_ns();					\
> +	for (__bn_i = 0; __bn_i < __bn_iters; __bn_i++)			\
> +		(void)__func(__VA_ARGS__);				\
> +	__bn_t = ktime_get_ns() - __bn_t;				\
> +	preempt_enable();						\
> +	__bn_t;								\
> +})
> +
> +/**
> + * STRING_BENCH_BUF() - Benchmark harness for single-buffer functions.
> + * @test: KUnit context.
> + * @buf_name: Local char * variable name to be defined.
> + * @buf_size: Local size_t variable name to be defined.
> + * @func: Function to benchmark.
> + * @...: Extra arguments for @func.
> + *
> + * Prepares a randomized, null-terminated buffer and iterates through lengths
> + * in bench_lens, defining @buf_name and @buf_size in each loop.
> + */
> +#define STRING_BENCH_BUF(test, buf_name, buf_size, func, ...)		\
> +do {									\
> +	char *buf_name, *_bn_buf;					\
> +	size_t buf_size, _bn_i, _bn_iters, _bn_size = 0;		\
> +	u64 _bn_t, _bn_mbps = 0, _bn_lat = 0;				\
> +									\
> +	if (!IS_ENABLED(CONFIG_STRING_KUNIT_BENCH))			\
> +		kunit_skip(test, "not enabled");			\
> +									\
> +	_bn_buf = alloc_max_bench_buffer(test, bench_lens,		\
> +			ARRAY_SIZE(bench_lens), &_bn_size);		\
> +	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, _bn_buf);			\
> +									\
> +	fill_random_string(_bn_buf, _bn_size);				\

> +	_bn_buf[_bn_size - 1] = '\0';					\

You have already this there in the function, no?

> +	for (_bn_i = 0; _bn_i < ARRAY_SIZE(bench_lens); _bn_i++) {	\
> +		buf_size = bench_lens[_bn_i];				\
> +		buf_name = _bn_buf + _bn_size - buf_size - 1;		\
> +		_bn_iters = STRING_BENCH_WORKLOAD /			\
> +				max_t(size_t, buf_size, 1U);		\

max()

> +		_bn_t = STRING_BENCH(_bn_iters, func, ##__VA_ARGS__);	\
> +									\
> +		if (_bn_t > 0) {					\
> +			_bn_mbps = (u64)(buf_size) * _bn_iters * 1000;	\
> +			_bn_mbps = div64_u64(_bn_mbps, _bn_t);		\
> +			_bn_lat = div64_u64(_bn_t, _bn_iters);		\
> +		}							\
> +		kunit_info(test, "len=%zu: %llu MB/s (%llu ns/call)\n",	\
> +				buf_size, _bn_mbps, _bn_lat);		\
> +	}								\
> +} while (0)

-- 
With Best Regards,
Andy Shevchenko



WARNING: multiple messages have this Message-ID (diff)
From: Andy Shevchenko <andriy.shevchenko@intel.com>
To: Feng Jiang <jiangfeng@kylinos.cn>
Cc: pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu,
	alex@ghiti.fr, akpm@linux-foundation.org, kees@kernel.org,
	andy@kernel.org, ebiggers@kernel.org, martin.petersen@oracle.com,
	ardb@kernel.org, charlie@rivosinc.com,
	conor.dooley@microchip.com, ajones@ventanamicro.com,
	linus.walleij@linaro.org, nathan@kernel.org,
	linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org,
	linux-hardening@vger.kernel.org, Joel Stanley <joel@jms.id.au>
Subject: Re: [PATCH v3 4/8] lib/string_kunit: add performance benchmarks for strlen
Date: Tue, 20 Jan 2026 09:46:49 +0200	[thread overview]
Message-ID: <aW8y6eC7ZGRYIWkn@smile.fi.intel.com> (raw)
In-Reply-To: <20260120065852.166857-5-jiangfeng@kylinos.cn>

On Tue, Jan 20, 2026 at 02:58:48PM +0800, Feng Jiang wrote:
> Introduce a benchmarking framework to the string_kunit test suite to
> measure the execution efficiency of string functions.
> 
> The implementation is inspired by crc_benchmark(), measuring throughput
> (MB/s) and latency (ns/call) across a range of string lengths. It
> includes a warm-up phase, disables preemption during measurement, and
> uses a fixed seed for reproducible results.
> 
> This allows for comparing different implementations (e.g., generic C vs.
> architecture-optimized assembly) within the KUnit environment.
> 
> Initially, provide benchmarks for strlen().

...

> +#define STRING_BENCH_SEED	888
> +#define STRING_BENCH_WORKLOAD	1000000UL

Can also be (1 * MEGA) from units.h.

...

> +static const size_t bench_lens[] = {
> +	0, 1, 7, 8, 16, 31, 64, 127, 512, 1024, 3173, 4096

Leave trailing comma.

> +};

...

> +static void *alloc_max_bench_buffer(struct kunit *test,
> +		const size_t *lens, size_t count, size_t *buf_len)
> +{
> +	void *buf;
> +	size_t i, max_len = 0;
> +
> +	for (i = 0; i < count; i++) {
> +		if (max_len < lens[i])
> +			max_len = lens[i];
> +	}
> +
> +	/* Add space for NUL terminator */
> +	max_len += 1;

> +	buf = kunit_kzalloc(test, max_len, GFP_KERNEL);
> +	if (buf && buf_len)
> +		*buf_len = max_len;
> +
> +	return buf;

	if (!buf)
		return NULL;

	*buf_len ...
	return buf;

> +}

...

> +static void fill_random_string(char *buf, size_t len)
> +{
> +	size_t i;
> +	struct rnd_state state;

Reversed xmas tree ordering?

> +	if (!buf || !len)
> +		return;
> +
> +	/* Use a fixed seed to ensure deterministic benchmark results */
> +	prandom_seed_state(&state, 888);
> +	prandom_bytes_state(&state, buf, len);
> +
> +	/* Replace null bytes to avoid early string termination */
> +	for (i = 0; i < len; i++) {
> +		if (buf[i] == '\0')
> +			buf[i] = 0x01;
> +	}
> +
> +	buf[len - 1] = '\0';
> +}

...

> +#define STRING_BENCH(iters, func, ...)					\

Is this same / similar code to crc_benchmark()? Perhaps we need to have KUnit
provided macro / environment to perform such tests... Have you talked to KUnit
people about all this?

> +({									\
> +	u64 __bn_t;							\
> +	size_t __bn_i;							\
> +	size_t __bn_iters = (iters);					\
> +	size_t __bn_warm_iters = max_t(size_t, __bn_iters / 10, 50U);	\

Try to avoid max_t() as much as possible. Wouldn't max() suffice?

> +	/* Volatile function pointer prevents dead code elimination */	\
> +	typeof(func) (* volatile __func) = (func);			\
> +									\
> +	for (__bn_i = 0; __bn_i < __bn_warm_iters; __bn_i++)		\
> +		(void)__func(__VA_ARGS__);				\
> +									\
> +	preempt_disable();						\
> +	__bn_t = ktime_get_ns();					\
> +	for (__bn_i = 0; __bn_i < __bn_iters; __bn_i++)			\
> +		(void)__func(__VA_ARGS__);				\
> +	__bn_t = ktime_get_ns() - __bn_t;				\
> +	preempt_enable();						\
> +	__bn_t;								\
> +})
> +
> +/**
> + * STRING_BENCH_BUF() - Benchmark harness for single-buffer functions.
> + * @test: KUnit context.
> + * @buf_name: Local char * variable name to be defined.
> + * @buf_size: Local size_t variable name to be defined.
> + * @func: Function to benchmark.
> + * @...: Extra arguments for @func.
> + *
> + * Prepares a randomized, null-terminated buffer and iterates through lengths
> + * in bench_lens, defining @buf_name and @buf_size in each loop.
> + */
> +#define STRING_BENCH_BUF(test, buf_name, buf_size, func, ...)		\
> +do {									\
> +	char *buf_name, *_bn_buf;					\
> +	size_t buf_size, _bn_i, _bn_iters, _bn_size = 0;		\
> +	u64 _bn_t, _bn_mbps = 0, _bn_lat = 0;				\
> +									\
> +	if (!IS_ENABLED(CONFIG_STRING_KUNIT_BENCH))			\
> +		kunit_skip(test, "not enabled");			\
> +									\
> +	_bn_buf = alloc_max_bench_buffer(test, bench_lens,		\
> +			ARRAY_SIZE(bench_lens), &_bn_size);		\
> +	KUNIT_ASSERT_NOT_ERR_OR_NULL(test, _bn_buf);			\
> +									\
> +	fill_random_string(_bn_buf, _bn_size);				\

> +	_bn_buf[_bn_size - 1] = '\0';					\

You have already this there in the function, no?

> +	for (_bn_i = 0; _bn_i < ARRAY_SIZE(bench_lens); _bn_i++) {	\
> +		buf_size = bench_lens[_bn_i];				\
> +		buf_name = _bn_buf + _bn_size - buf_size - 1;		\
> +		_bn_iters = STRING_BENCH_WORKLOAD /			\
> +				max_t(size_t, buf_size, 1U);		\

max()

> +		_bn_t = STRING_BENCH(_bn_iters, func, ##__VA_ARGS__);	\
> +									\
> +		if (_bn_t > 0) {					\
> +			_bn_mbps = (u64)(buf_size) * _bn_iters * 1000;	\
> +			_bn_mbps = div64_u64(_bn_mbps, _bn_t);		\
> +			_bn_lat = div64_u64(_bn_t, _bn_iters);		\
> +		}							\
> +		kunit_info(test, "len=%zu: %llu MB/s (%llu ns/call)\n",	\
> +				buf_size, _bn_mbps, _bn_lat);		\
> +	}								\
> +} while (0)

-- 
With Best Regards,
Andy Shevchenko



_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

  reply	other threads:[~2026-01-20  7:46 UTC|newest]

Thread overview: 58+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-20  6:58 [PATCH v3 0/8] riscv: optimize string functions and add kunit tests Feng Jiang
2026-01-20  6:58 ` Feng Jiang
2026-01-20  6:58 ` [PATCH v3 1/8] lib/string_kunit: add correctness test for strlen Feng Jiang
2026-01-20  6:58   ` Feng Jiang
2026-01-20  7:28   ` Andy Shevchenko
2026-01-20  7:28     ` Andy Shevchenko
2026-01-20  6:58 ` [PATCH v3 2/8] lib/string_kunit: add correctness test for strnlen Feng Jiang
2026-01-20  6:58   ` Feng Jiang
2026-01-20  7:29   ` Andy Shevchenko
2026-01-20  7:29     ` Andy Shevchenko
2026-01-20  6:58 ` [PATCH v3 3/8] lib/string_kunit: add correctness test for strrchr() Feng Jiang
2026-01-20  6:58   ` Feng Jiang
2026-01-20  7:30   ` Andy Shevchenko
2026-01-20  7:30     ` Andy Shevchenko
2026-01-20  6:58 ` [PATCH v3 4/8] lib/string_kunit: add performance benchmarks for strlen Feng Jiang
2026-01-20  6:58   ` Feng Jiang
2026-01-20  7:46   ` Andy Shevchenko [this message]
2026-01-20  7:46     ` Andy Shevchenko
2026-01-21  5:45     ` Feng Jiang
2026-01-21  5:45       ` Feng Jiang
2026-01-20  6:58 ` [PATCH v3 5/8] lib/string_kunit: extend benchmarks to strnlen and chr searches Feng Jiang
2026-01-20  6:58   ` Feng Jiang
2026-01-20  7:48   ` Andy Shevchenko
2026-01-20  7:48     ` Andy Shevchenko
2026-01-21  5:48     ` Feng Jiang
2026-01-21  5:48       ` Feng Jiang
2026-01-20  6:58 ` [PATCH v3 6/8] riscv: lib: add strnlen implementation Feng Jiang
2026-01-20  6:58   ` Feng Jiang
2026-01-20  7:31   ` Andy Shevchenko
2026-01-20  7:31     ` Andy Shevchenko
2026-01-21  5:52     ` Feng Jiang
2026-01-21  5:52       ` Feng Jiang
2026-01-21  7:24   ` Qingfang Deng
2026-01-21  7:24     ` Qingfang Deng
2026-01-23  1:28     ` Feng Jiang
2026-01-23  1:28       ` Feng Jiang
2026-01-20  6:58 ` [PATCH v3 7/8] riscv: lib: add strchr implementation Feng Jiang
2026-01-20  6:58   ` Feng Jiang
2026-01-20  7:31   ` Andy Shevchenko
2026-01-20  7:31     ` Andy Shevchenko
2026-01-20  6:58 ` [PATCH v3 8/8] riscv: lib: add strrchr implementation Feng Jiang
2026-01-20  6:58   ` Feng Jiang
2026-01-20  7:32   ` Andy Shevchenko
2026-01-20  7:32     ` Andy Shevchenko
2026-01-20  7:36 ` [PATCH v3 0/8] riscv: optimize string functions and add kunit tests Andy Shevchenko
2026-01-20  7:36   ` Andy Shevchenko
2026-01-21  6:44   ` Feng Jiang
2026-01-21  6:44     ` Feng Jiang
2026-01-21  7:01     ` Andy Shevchenko
2026-01-21  7:01       ` Andy Shevchenko
2026-01-21  8:12       ` Feng Jiang
2026-01-21  8:12         ` Feng Jiang
2026-01-21 10:57       ` David Laight
2026-01-21 10:57         ` David Laight
2026-01-23  3:12         ` Feng Jiang
2026-01-23  3:12           ` Feng Jiang
2026-01-23 10:16           ` David Laight
2026-01-23 10:16             ` David Laight

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=aW8y6eC7ZGRYIWkn@smile.fi.intel.com \
    --to=andriy.shevchenko@intel.com \
    --cc=ajones@ventanamicro.com \
    --cc=akpm@linux-foundation.org \
    --cc=alex@ghiti.fr \
    --cc=andy@kernel.org \
    --cc=aou@eecs.berkeley.edu \
    --cc=ardb@kernel.org \
    --cc=charlie@rivosinc.com \
    --cc=conor.dooley@microchip.com \
    --cc=ebiggers@kernel.org \
    --cc=jiangfeng@kylinos.cn \
    --cc=joel@jms.id.au \
    --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 \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.