(root)/
glibc-2.38/
benchtests/
bench-memcpy.c
       1  /* Measure memcpy functions.
       2     Copyright (C) 2013-2023 Free Software Foundation, Inc.
       3     This file is part of the GNU C Library.
       4  
       5     The GNU C Library is free software; you can redistribute it and/or
       6     modify it under the terms of the GNU Lesser General Public
       7     License as published by the Free Software Foundation; either
       8     version 2.1 of the License, or (at your option) any later version.
       9  
      10     The GNU C Library is distributed in the hope that it will be useful,
      11     but WITHOUT ANY WARRANTY; without even the implied warranty of
      12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      13     Lesser General Public License for more details.
      14  
      15     You should have received a copy of the GNU Lesser General Public
      16     License along with the GNU C Library; if not, see
      17     <https://www.gnu.org/licenses/>.  */
      18  
      19  #ifndef MEMCPY_RESULT
      20  # define MEMCPY_RESULT(dst, len) dst
      21  # define MIN_PAGE_SIZE 131072
      22  # define TEST_MAIN
      23  # define TEST_NAME "memcpy"
      24  # include "bench-string.h"
      25  
      26  void *generic_memcpy (void *, const void *, size_t);
      27  
      28  IMPL (memcpy, 1)
      29  IMPL (generic_memcpy, 0)
      30  
      31  #endif
      32  
      33  # include "json-lib.h"
      34  
      35  typedef void *(*proto_t) (void *, const void *, size_t);
      36  
      37  static void
      38  do_one_test (json_ctx_t *json_ctx, impl_t *impl, char *dst, const char *src,
      39  	     size_t len)
      40  {
      41    size_t i, iters = INNER_LOOP_ITERS / 2;
      42    timing_t start, stop, cur;
      43    for (i = 0; i < iters / 64; ++i)
      44      {
      45        CALL (impl, dst, src, len);
      46      }
      47    TIMING_NOW (start);
      48    for (i = 0; i < iters; ++i)
      49      {
      50        CALL (impl, dst, src, len);
      51      }
      52    TIMING_NOW (stop);
      53  
      54    TIMING_DIFF (cur, start, stop);
      55  
      56    json_element_double (json_ctx, (double) cur / (double) iters);
      57  }
      58  
      59  static void
      60  do_test (json_ctx_t *json_ctx, size_t align1, size_t align2, size_t len,
      61           int both_ways)
      62  {
      63    size_t i, j;
      64    char *s1, *s2;
      65    size_t repeats;
      66    align1 &= (getpagesize () - 1);
      67    if (align1 + len >= page_size)
      68      return;
      69  
      70    align2 &= (getpagesize () - 1);
      71    if (align2 + len >= page_size)
      72      return;
      73  
      74    s1 = (char *) (buf1 + align1);
      75    s2 = (char *) (buf2 + align2);
      76  
      77    for (repeats = both_ways ? 2 : 1; repeats; --repeats)
      78      {
      79        for (i = 0, j = 1; i < len; i++, j += 23)
      80          s1[i] = j;
      81  
      82        json_element_object_begin (json_ctx);
      83        json_attr_uint (json_ctx, "length", (double) len);
      84        json_attr_uint (json_ctx, "align1", (double) align1);
      85        json_attr_uint (json_ctx, "align2", (double) align2);
      86        json_attr_uint (json_ctx, "dst > src", (double) (s2 > s1));
      87        json_array_begin (json_ctx, "timings");
      88  
      89        FOR_EACH_IMPL (impl, 0)
      90          do_one_test (json_ctx, impl, s2, s1, len);
      91  
      92        json_array_end (json_ctx);
      93        json_element_object_end (json_ctx);
      94  
      95        s1 = (char *) (buf2 + align1);
      96        s2 = (char *) (buf1 + align2);
      97      }
      98  }
      99  
     100  int
     101  test_main (void)
     102  {
     103    json_ctx_t json_ctx;
     104    size_t i;
     105    size_t half_page = getpagesize () / 2;
     106    test_init ();
     107  
     108    json_init (&json_ctx, 0, stdout);
     109  
     110    json_document_begin (&json_ctx);
     111    json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
     112  
     113    json_attr_object_begin (&json_ctx, "functions");
     114    json_attr_object_begin (&json_ctx, TEST_NAME);
     115    json_attr_string (&json_ctx, "bench-variant", "default");
     116  
     117    json_array_begin (&json_ctx, "ifuncs");
     118    FOR_EACH_IMPL (impl, 0)
     119      json_element_string (&json_ctx, impl->name);
     120    json_array_end (&json_ctx);
     121  
     122    json_array_begin (&json_ctx, "results");
     123    for (i = 0; i < 18; ++i)
     124      {
     125        do_test (&json_ctx, 0, 0, 1 << i, 1);
     126        do_test (&json_ctx, i, 0, 1 << i, 1);
     127        do_test (&json_ctx, i + 32, 0, 1 << i, 1);
     128        do_test (&json_ctx, 0, i, 1 << i, 1);
     129        do_test (&json_ctx, 0, i + 32, 1 << i, 1);
     130        do_test (&json_ctx, i, i, 1 << i, 1);
     131        do_test (&json_ctx, i + 32, i + 32, 1 << i, 1);
     132        do_test (&json_ctx, half_page, 0, 1 << i, 1);
     133        do_test (&json_ctx, half_page + i, 0, 1 << i, 1);
     134        do_test (&json_ctx, half_page, i, 1 << i, 1);
     135        do_test (&json_ctx, half_page + i, i, 1 << i, 1);
     136      }
     137  
     138    for (i = 0; i < 32; ++i)
     139      {
     140        do_test (&json_ctx, 0, 0, i, 0);
     141        do_test (&json_ctx, i, 0, i, 0);
     142        do_test (&json_ctx, 0, i, i, 0);
     143        do_test (&json_ctx, i, i, i, 0);
     144        do_test (&json_ctx, half_page, 0, i, 0);
     145        do_test (&json_ctx, half_page + i, 0, i, 0);
     146        do_test (&json_ctx, half_page, i, i, 0);
     147        do_test (&json_ctx, half_page + i, i, i, 0);
     148        do_test (&json_ctx, getpagesize () - 1, 0, i, 0);
     149        do_test (&json_ctx, 0, getpagesize () - 1, i, 0);
     150      }
     151  
     152    for (i = 3; i < 32; ++i)
     153      {
     154        if ((i & (i - 1)) == 0)
     155          continue;
     156        do_test (&json_ctx, 0, 0, 16 * i, 1);
     157        do_test (&json_ctx, i, 0, 16 * i, 1);
     158        do_test (&json_ctx, 0, i, 16 * i, 1);
     159        do_test (&json_ctx, i, i, 16 * i, 1);
     160        do_test (&json_ctx, half_page, 0, 16 * i, 1);
     161        do_test (&json_ctx, half_page + i, 0, 16 * i, 1);
     162        do_test (&json_ctx, half_page, i, 16 * i, 1);
     163        do_test (&json_ctx, half_page + i, i, 16 * i, 1);
     164      }
     165  
     166    for (i = 32; i < 64; ++i)
     167      {
     168        do_test (&json_ctx, 0, 0, 32 * i, 1);
     169        do_test (&json_ctx, i, 0, 32 * i, 1);
     170        do_test (&json_ctx, 0, i, 32 * i, 1);
     171        do_test (&json_ctx, i, i, 32 * i, 1);
     172        do_test (&json_ctx, half_page, 0, 32 * i, 1);
     173        do_test (&json_ctx, half_page + i, 0, 32 * i, 1);
     174        do_test (&json_ctx, half_page, i, 32 * i, 1);
     175        do_test (&json_ctx, half_page + i, i, 32 * i, 1);
     176      }
     177  
     178    do_test (&json_ctx, 0, 0, getpagesize (), 1);
     179  
     180    for (i = 0; i <= 48; ++i)
     181      {
     182        do_test (&json_ctx, 0, 0, 2048 + 64 * i, 1);
     183        do_test (&json_ctx, i, 0, 2048 + 64 * i, 1);
     184        do_test (&json_ctx, i + 32, 0, 2048 + 64 * i, 1);
     185        do_test (&json_ctx, 0, i, 2048 + 64 * i, 1);
     186        do_test (&json_ctx, 0, i + 32, 2048 + 64 * i, 1);
     187        do_test (&json_ctx, i, i, 2048 + 64 * i, 1);
     188        do_test (&json_ctx, i + 32, i + 32, 2048 + 64 * i, 1);
     189        do_test (&json_ctx, half_page, 0, 2048 + 64 * i, 1);
     190        do_test (&json_ctx, half_page + i, 0, 2048 + 64 * i, 1);
     191        do_test (&json_ctx, half_page, i, 2048 + 64 * i, 1);
     192        do_test (&json_ctx, half_page + i, i, 2048 + 64 * i, 1);
     193        do_test (&json_ctx, i, 1, 2048 + 64 * i, 1);
     194        do_test (&json_ctx, 1, i, 2048 + 64 * i, 1);
     195        do_test (&json_ctx, i + 32, 1, 2048 + 64 * i, 1);
     196        do_test (&json_ctx, 1, i + 32, 2048 + 64 * i, 1);
     197        do_test (&json_ctx, half_page + i, 1, 2048 + 64 * i, 1);
     198        do_test (&json_ctx, half_page + 1, i, 2048 + 64 * i, 1);
     199      }
     200  
     201    json_array_end (&json_ctx);
     202    json_attr_object_end (&json_ctx);
     203    json_attr_object_end (&json_ctx);
     204    json_document_end (&json_ctx);
     205  
     206    return ret;
     207  }
     208  
     209  #include <support/test-driver.c>
     210  
     211  #define libc_hidden_builtin_def(X)
     212  #undef MEMCPY
     213  #define MEMCPY generic_memcpy
     214  #include <string/memcpy.c>
     215  #include <string/wordcopy.c>