bench-memset-random.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* Measure memset performance.
  2. Copyright (C) 2024-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. #define MIN_SIZE 32768
  16. #define MAX_SIZE (1024*1024)
  17. #define NUM_TESTS 16384
  18. #define MIN_PAGE_SIZE (MAX_SIZE + getpagesize())
  19. #define TEST_MAIN
  20. #define TEST_NAME "memset"
  21. #include "bench-string.h"
  22. #include <assert.h>
  23. #include "json-lib.h"
  24. typedef void *(*proto_t) (void *, int, size_t);
  25. IMPL (MEMSET, 1)
  26. typedef struct { uint32_t offset : 20, len : 12; } memset_test_t;
  27. static memset_test_t test_arr[NUM_TESTS];
  28. typedef struct { uint16_t size; uint16_t freq; } freq_data_t;
  29. typedef struct { uint8_t align; uint16_t freq; } align_data_t;
  30. #define SIZE_NUM 65536
  31. #define SIZE_MASK (SIZE_NUM-1)
  32. static uint16_t len_arr[SIZE_NUM];
  33. /* Frequency data for memset sizes up to 4096 bytes based on SPEC2017. */
  34. static freq_data_t memset_len_freq[] =
  35. {
  36. {40,28817}, {32,15336}, { 16,3823}, {296,3545}, { 24,3454}, { 8,1412},
  37. {292,1202}, { 48, 927}, { 12, 613}, { 11, 539}, {284, 493}, {108, 414},
  38. { 88, 380}, { 20, 295}, {312, 271}, { 72, 233}, { 2, 200}, { 4, 192},
  39. { 15, 180}, { 14, 174}, { 13, 160}, { 56, 151}, { 36, 144}, { 64, 140},
  40. {4095,133}, { 10, 130}, { 9, 124}, { 3, 124}, { 28, 120}, { 0, 118},
  41. {288, 110}, {1152, 96}, {104, 90}, { 1, 86}, {832, 76}, {248, 74},
  42. {1024, 69}, {120, 64}, {512, 63}, {384, 60}, { 6, 59}, { 80, 54},
  43. { 17, 50}, { 7, 49}, {520, 47}, {2048, 39}, {256, 37}, {864, 33},
  44. {1440, 28}, { 22, 27}, {2056, 24}, {260, 23}, { 68, 23}, { 5, 22},
  45. { 18, 21}, {200, 18}, {2120, 18}, { 60, 17}, { 52, 16}, {336, 15},
  46. { 44, 13}, {192, 13}, {160, 12}, {2064, 12}, {128, 12}, { 76, 11},
  47. {164, 11}, {152, 10}, {136, 9}, {488, 7}, { 96, 6}, {560, 6},
  48. {1016, 6}, {112, 5}, {232, 5}, {168, 5}, {952, 5}, {184, 5},
  49. {144, 4}, {252, 4}, { 84, 3}, {960, 3}, {3808, 3}, {244, 3},
  50. {280, 3}, {224, 3}, {156, 3}, {1088, 3}, {440, 3}, {216, 2},
  51. {304, 2}, { 23, 2}, { 25, 2}, { 26, 2}, {264, 2}, {328, 2},
  52. {1096, 2}, {240, 2}, {1104, 2}, {704, 2}, {1664, 2}, {360, 2},
  53. {808, 1}, {544, 1}, {236, 1}, {720, 1}, {368, 1}, {424, 1},
  54. {640, 1}, {1112, 1}, {552, 1}, {272, 1}, {776, 1}, {376, 1},
  55. { 92, 1}, {536, 1}, {824, 1}, {496, 1}, {760, 1}, {792, 1},
  56. {504, 1}, {344, 1}, {1816, 1}, {880, 1}, {176, 1}, {320, 1},
  57. {352, 1}, {2008, 1}, {208, 1}, {408, 1}, {228, 1}, {2072, 1},
  58. {568, 1}, {220, 1}, {616, 1}, {600, 1}, {392, 1}, {696, 1},
  59. {2144, 1}, {1280, 1}, {2136, 1}, {632, 1}, {584, 1}, {456, 1},
  60. {472, 1}, {3440, 1}, {2088, 1}, {680, 1}, {2928, 1}, {212, 1},
  61. {648, 1}, {1752, 1}, {664, 1}, {3512, 1}, {1032, 1}, {528, 1},
  62. {4072, 1}, {204, 1}, {2880, 1}, {3392, 1}, {712, 1}, { 59, 1},
  63. {736, 1}, {592, 1}, {2520, 1}, {744, 1}, {196, 1}, {172, 1},
  64. {728, 1}, {2040, 1}, {1192, 1}, {3600, 1}, {0, 0}
  65. };
  66. #define ALIGN_NUM 1024
  67. #define ALIGN_MASK (ALIGN_NUM-1)
  68. static uint8_t align_arr[ALIGN_NUM];
  69. /* Alignment data for memset based on SPEC2017. */
  70. static align_data_t memset_align_freq[] =
  71. {
  72. {16, 338}, {8, 307}, {32, 148}, {64, 131}, {4, 72}, {1, 23}, {2, 5}, {0, 0}
  73. };
  74. static void
  75. init_memset_distribution (void)
  76. {
  77. int i, j, freq, size, n;
  78. for (n = i = 0; (freq = memset_len_freq[i].freq) != 0; i++)
  79. for (j = 0, size = memset_len_freq[i].size; j < freq; j++)
  80. len_arr[n++] = size;
  81. assert (n == SIZE_NUM);
  82. for (n = i = 0; (freq = memset_align_freq[i].freq) != 0; i++)
  83. for (j = 0, size = memset_align_freq[i].align; j < freq; j++)
  84. align_arr[n++] = size - 1;
  85. assert (n == ALIGN_NUM);
  86. }
  87. static void
  88. do_one_test (json_ctx_t *json_ctx, impl_t *impl, size_t iters,
  89. memset_test_t *input, size_t n)
  90. {
  91. timing_t start, stop, cur;
  92. /* Avoid 'cold start' performance penalty. */
  93. for (int j = 0; j < n; j++)
  94. CALL (impl, buf1 + input[j].offset, 0, input[j].len);
  95. TIMING_NOW (start);
  96. for (int i = 0; i < iters; ++i)
  97. for (int j = 0; j < n; j++)
  98. CALL (impl, buf1 + input[j].offset, 0, input[j].len);
  99. TIMING_NOW (stop);
  100. TIMING_DIFF (cur, start, stop);
  101. json_element_double (json_ctx, (double) cur / (double) iters);
  102. }
  103. static void
  104. do_test (json_ctx_t *json_ctx, size_t max_size)
  105. {
  106. int n;
  107. /* Create a random set of memset inputs strings with the given length
  108. and alignment distributions. */
  109. for (n = 0; n < NUM_TESTS; n++)
  110. {
  111. int align = align_arr[rand () & ALIGN_MASK];
  112. int len = len_arr[rand () & SIZE_MASK];
  113. test_arr[n].offset = (rand () & (max_size - 1)) & ~align;
  114. test_arr[n].len = len;
  115. }
  116. json_element_object_begin (json_ctx);
  117. json_attr_uint (json_ctx, "length", (double) max_size);
  118. json_array_begin (json_ctx, "timings");
  119. FOR_EACH_IMPL (impl, 0)
  120. do_one_test (json_ctx, impl, INNER_LOOP_ITERS_MEDIUM * 2, test_arr, n);
  121. json_array_end (json_ctx);
  122. json_element_object_end (json_ctx);
  123. }
  124. int
  125. test_main (void)
  126. {
  127. json_ctx_t json_ctx;
  128. test_init ();
  129. init_memset_distribution ();
  130. memset (buf1, 1, MIN_PAGE_SIZE);
  131. json_init (&json_ctx, 0, stdout);
  132. json_document_begin (&json_ctx);
  133. json_attr_string (&json_ctx, "timing_type", TIMING_TYPE);
  134. json_attr_object_begin (&json_ctx, "functions");
  135. json_attr_object_begin (&json_ctx, TEST_NAME);
  136. json_attr_string (&json_ctx, "bench-variant", "random");
  137. json_array_begin (&json_ctx, "ifuncs");
  138. FOR_EACH_IMPL (impl, 0)
  139. json_element_string (&json_ctx, impl->name);
  140. json_array_end (&json_ctx);
  141. json_array_begin (&json_ctx, "results");
  142. for (int i = MIN_SIZE; i <= MAX_SIZE; i = i * 2)
  143. do_test (&json_ctx, i);
  144. json_array_end (&json_ctx);
  145. json_attr_object_end (&json_ctx);
  146. json_attr_object_end (&json_ctx);
  147. json_document_end (&json_ctx);
  148. return ret;
  149. }
  150. #include <support/test-driver.c>