bench-memcpy.c 6.6 KB

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