Re: [PATCH] KUnit: memcpy: add benchmark

From: Matteo Croce

Date: Thu Jan 29 2026 - 18:37:32 EST


Il giorno ven 30 gen 2026 alle ore 00:20 Andrew Morton
<akpm@xxxxxxxxxxxxxxxxxxxx> ha scritto:
>
> On Thu, 29 Jan 2026 01:43:28 +0100 Matteo Croce <technoboy85@xxxxxxxxx> wrote:
>
> > Add optional benchmarks for memcpy() and memmove() functions.
> > Each benchmark is run twice: first with buffers aligned and then with
> > buffers unaligned, to spot unaligned accesses on platforms where they
> > have a noticeable performance impact.
> >
> > ...
> >
> > +static int memcpy_bench_align(struct kunit *test, bool unalign)
> > +{
> > + u64 start, end, total_ns = 0;
> > + char *buf1;
> > + char *buf2;
> > + int ret = 0;
> > +
> > + buf1 = kzalloc(COPY_SIZE, GFP_KERNEL);
> > + if (!buf1)
> > + return -ENOMEM;
> > +
> > + buf2 = kzalloc(COPY_SIZE, GFP_KERNEL);
> > + if (!buf2) {
> > + ret = -ENOMEM;
> > + goto out_free;
> > + }
> > +
> > + preempt_disable();
> > + for (int i = 0; i < COPIES_NUM; i++) {
> > + start = ktime_get_ns();
> > + memcpy(buf1 + unalign, buf2, COPY_SIZE - unalign);
> > + end = ktime_get_ns();
> > + total_ns += end - start;
> > + cond_resched();
>
> Is cond_resched() inside preempt_disable() actually legal?
>
> Might be, but it doesn't make a lot of sense, does it?
>
> > + }
> > + preempt_enable();
> > +
>

Right. In a previous version I was doing
preempt_disable()/preempt_enable() around the two ktime_get_ns(), but
then I thought that enabling and disabling preemption 100 time was too
much.
I'll restore the preempt macros around the actual copy and remove
cond_resched().
Thanks.

--
Matteo Croce

perl -e 'for($t=0;;$t++){print chr($t*($t>>8|$t>>13)&255)}' |aplay