[PATCH v2] Benchtests: Improve large memcpy/memset benchmarks

Adhemerval Zanella Netto adhemerval.zanella@linaro.org
Tue Mar 4 15:20:34 GMT 2025



On 20/02/25 12:12, Wilco Dijkstra wrote:
> 
>  
> ping
> 
>  
> Hi Carlos,
> 
>> Would it be possible for you to post a v2 that rolls up the feedback
>> from Noah and HJ here?
>>
>> I know that some of their feedback you have chosen not to integrate,
>> but any other changes you made would be good to get into the tree.
> 
> Sure v2 is below. This doesn't include fixes for page_size = getpagesize ()
> since that affects a lot more benchmarks and incorrect range checks
> (there are many uses of 4095 and 4096 for page alignment), so I've kept the
> rest the same as v1.
> 
> Cheers,
> Wilco
> 
> 
> v2: Use getpagesize () as suggested.
> 
> Adjust sizes between 64KB and 16MB and iterations based on length.
> Remove incorrect uses of alloc_bufs since we're not interested in measuring
> Linux clear_page time.  Use getpagesize() - 1 instead of 4095 when
> aligning within a page.
> 

LGTM, thanks.

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>

> ---
> 
> diff --git a/benchtests/bench-bzero-large.c b/benchtests/bench-bzero-large.c
> index 71300e878afbfcc92c378d41487e469fde08093a..2eebd08315d75a23f19a7b750493d1a63ce0319d 100644
> --- a/benchtests/bench-bzero-large.c
> +++ b/benchtests/bench-bzero-large.c
> @@ -22,9 +22,8 @@
>  #else
>  # define TEST_NAME "bzero"
>  #endif
> -#define START_SIZE (128 * 1024)
> -#define MIN_PAGE_SIZE (getpagesize () + 64 * 1024 * 1024)
> -#define TIMEOUT (20 * 60)
> +#define START_SIZE (64 * 1024)
> +#define MIN_PAGE_SIZE (getpagesize () + 16 * 1024 * 1024)
>  #include "bench-string.h"
>  
>  #include "json-lib.h"
> @@ -52,7 +51,7 @@ IMPL (memset_zero, 0)
>  static void
>  do_one_test (json_ctx_t *json_ctx, impl_t *impl, CHAR *s, size_t n)
>  {
> -  size_t i, iters = 16;
> +  size_t i, iters = (MIN_PAGE_SIZE * 64) / n;
>    timing_t start, stop, cur;
>  
>    TIMING_NOW (start);
> @@ -74,20 +73,13 @@ do_one_test (json_ctx_t *json_ctx, impl_t *impl, CHAR *s, size_t n)
>  static void
>  do_test (json_ctx_t *json_ctx, size_t align, size_t len)
>  {
> -  align &= 63;
> -  if ((align + len) * sizeof (CHAR) > page_size)
> -    return;
> -
>    json_element_object_begin (json_ctx);
>    json_attr_uint (json_ctx, "length", len);
>    json_attr_uint (json_ctx, "alignment", align);
>    json_array_begin (json_ctx, "timings");
>  
>    FOR_EACH_IMPL (impl, 0)
> -    {
> -      do_one_test (json_ctx, impl, (CHAR *) (buf1) + align, len);
> -      alloc_bufs ();
> -    }
> +    do_one_test (json_ctx, impl, (CHAR *) (buf1) + align, len);
>  
>    json_array_end (json_ctx);
>    json_element_object_end (json_ctx);
> diff --git a/benchtests/bench-bzero.c b/benchtests/bench-bzero.c
> index d0497dd8b0ab852ab661cac6df82202b0d5969f5..637b4f765eeac2a7bcdac35a785060e8afbbbc35 100644
> --- a/benchtests/bench-bzero.c
> +++ b/benchtests/bench-bzero.c
> @@ -73,7 +73,7 @@ do_one_test (json_ctx_t *json_ctx, impl_t *impl, CHAR *s, size_t n)
>  static void
>  do_test (json_ctx_t *json_ctx, size_t align, size_t len)
>  {
> -  align &= 4095;
> +  align &= getpagesize () - 1;
>    if ((align + len) * sizeof (CHAR) > page_size)
>      return;
>  
> diff --git a/benchtests/bench-memcpy-large.c b/benchtests/bench-memcpy-large.c
> index 713588e8ce8835d382b9e8e781e351ad21b54418..acdc175a7843acade1defa0244b35f003bac8241 100644
> --- a/benchtests/bench-memcpy-large.c
> +++ b/benchtests/bench-memcpy-large.c
> @@ -19,10 +19,9 @@
>  #ifndef MEMCPY_RESULT
>  # define MEMCPY_RESULT(dst, len) dst
>  # define START_SIZE (64 * 1024)
> -# define MIN_PAGE_SIZE (getpagesize () + 32 * 1024 * 1024)
> +# define MIN_PAGE_SIZE (getpagesize () + 16 * 1024 * 1024)
>  # define TEST_MAIN
>  # define TEST_NAME "memcpy"
> -# define TIMEOUT (20 * 60)
>  # include "bench-string.h"
>  
>  IMPL (memcpy, 1)
> @@ -36,7 +35,7 @@ static void
>  do_one_test (json_ctx_t *json_ctx, impl_t *impl, char *dst, const char *src,
>               size_t len)
>  {
> -  size_t i, iters = 16;
> +  size_t i, iters = (MIN_PAGE_SIZE * 8) / len;
>    timing_t start, stop, cur;
>  
>    TIMING_NOW (start);
> @@ -58,13 +57,8 @@ do_test (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t len,
>    size_t i, j;
>    char *s1, *s2;
>    size_t repeats;
> -  align1 &= 4095;
> -  if (align1 + len >= page_size)
> -    return;
> -
> -  align2 &= 4095;
> -  if (align2 + len >= page_size)
> -    return;
> +  align1 &= getpagesize () - 1;
> +  align2 &= getpagesize () - 1;
>  
>    s1 = (char *) (buf1 + align1);
>    s2 = (char *) (buf2 + align2);
> diff --git a/benchtests/bench-memmove-large.c b/benchtests/bench-memmove-large.c
> index cc0145534de690652ce05e07608470ea3cf0efd1..c660288f96d5eaeddc5b83ef53a17efe1c572ae8 100644
> --- a/benchtests/bench-memmove-large.c
> +++ b/benchtests/bench-memmove-large.c
> @@ -16,12 +16,10 @@
>     License along with the GNU C Library; if not, see
>     <https://www.gnu.org/licenses/>.  */
>  
> -#define BASE_PAGE_SIZE (1024 * 1024)
> -#define START_SIZE (4 * 1024)
> +#define START_SIZE (64 * 1024)
>  #define MIN_PAGE_SIZE (getpagesize () + 16 * 1024 * 1024)
>  #define TEST_MAIN
>  #define TEST_NAME "memmove"
> -#define TIMEOUT (20 * 60)
>  #include "bench-string.h"
>  #include "json-lib.h"
>  
> @@ -33,7 +31,7 @@ static void
>  do_one_test (json_ctx_t *json_ctx, impl_t *impl, char *dst, char *src,
>               size_t len)
>  {
> -  size_t i, iters = 16;
> +  size_t i, iters = (MIN_PAGE_SIZE * 8) / len;
>    timing_t start, stop, cur;
>  
>    TIMING_NOW (start);
> @@ -54,13 +52,8 @@ do_test (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t len)
>    size_t i, j;
>    char *s1, *s2;
>  
> -  align1 &= 127;
> -  if (align1 + len >= page_size)
> -    return;
> -
> -  align2 &= 127;
> -  if (align2 + len >= page_size)
> -    return;
> +  align1 &= getpagesize () - 1;
> +  align2 &= getpagesize () - 1;
>  
>    s1 = (char *) (buf2 + align1);
>    s2 = (char *) (buf2 + align2);
> diff --git a/benchtests/bench-memset-large.c b/benchtests/bench-memset-large.c
> index 22ecea4da8a726c4751056a1cbabc0a505046625..3a5169940b94b7cd118ca2b461c15f5e53fc7b46 100644
> --- a/benchtests/bench-memset-large.c
> +++ b/benchtests/bench-memset-large.c
> @@ -18,9 +18,8 @@
>  
>  #define TEST_MAIN
>  #define TEST_NAME "memset"
> -#define START_SIZE (128 * 1024)
> -#define MIN_PAGE_SIZE (getpagesize () + 64 * 1024 * 1024)
> -#define TIMEOUT (20 * 60)
> +#define START_SIZE (64 * 1024)
> +#define MIN_PAGE_SIZE (getpagesize () + 16 * 1024 * 1024)
>  #include "bench-string.h"
>  
>  #include "json-lib.h"
> @@ -35,7 +34,7 @@ static void
>  do_one_test (json_ctx_t *json_ctx, impl_t *impl, CHAR *s,
>               int c __attribute ((unused)), size_t n)
>  {
> -  size_t i, iters = 16;
> +  size_t i, iters = (MIN_PAGE_SIZE * 64) / n;
>    timing_t start, stop, cur;
>  
>    TIMING_NOW (start);
> @@ -53,10 +52,6 @@ do_one_test (json_ctx_t *json_ctx, impl_t *impl, CHAR *s,
>  static void
>  do_test (json_ctx_t *json_ctx, size_t align, int c, size_t len)
>  {
> -  align &= 63;
> -  if ((align + len) * sizeof (CHAR) > page_size)
> -    return;
> -
>    json_element_object_begin (json_ctx);
>    json_attr_uint (json_ctx, "length", len);
>    json_attr_uint (json_ctx, "alignment", align);
> @@ -64,10 +59,7 @@ do_test (json_ctx_t *json_ctx, size_t align, int c, size_t len)
>    json_array_begin (json_ctx, "timings");
>  
>    FOR_EACH_IMPL (impl, 0)
> -    {
> -      do_one_test (json_ctx, impl, (CHAR *) (buf1) + align, c, len);
> -      alloc_bufs ();
> -    }
> +    do_one_test (json_ctx, impl, (CHAR *) (buf1) + align, c, len);
>  
>    json_array_end (json_ctx);
>    json_element_object_end (json_ctx);
> diff --git a/benchtests/bench-memset.c b/benchtests/bench-memset.c
> index cbeb6b942b3a0fcf712cefd7c1a75b97336fa677..5682f7298b7fbaaaebb607dead7ea005a3d4a790 100644
> --- a/benchtests/bench-memset.c
> +++ b/benchtests/bench-memset.c
> @@ -61,7 +61,7 @@ do_one_test (json_ctx_t *json_ctx, impl_t *impl, CHAR *s,
>  static void
>  do_test (json_ctx_t *json_ctx, size_t align, int c, size_t len)
>  {
> -  align &= 4095;
> +  align &= getpagesize () - 1;
>    if ((align + len) * sizeof (CHAR) > page_size)
>      return;
>  



More information about the Libc-alpha mailing list