bench-stringop 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #!/bin/bash
  2. # Script to measure memset and memcpy for different sizes and strategies.
  3. #
  4. # Contributed by Jan Hubicka <jh@suse.cz>
  5. #
  6. # Copyright (C) 2019 Free Software Foundation, Inc.
  7. #
  8. # This file is part of GCC.
  9. #
  10. # GCC is free software; you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation; either version 3, or (at your option)
  13. # any later version.
  14. #
  15. # GCC is distributed in the hope that it will be useful,
  16. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. # GNU General Public License for more details.
  19. #
  20. # You should have received a copy of the GNU General Public License
  21. # along with GCC; see the file COPYING. If not, write to
  22. # the Free Software Foundation, 51 Franklin Street, Fifth Floor,
  23. # Boston, MA 02110-1301, USA.
  24. # This script will search a line starting with 'spawn' that includes the
  25. # pattern you are looking for (typically a source file name).
  26. #
  27. # Once it finds that pattern, it re-executes the whole command
  28. # in the spawn line. If the pattern matches more than one spawn
  29. # command, it asks which one you want.
  30. test()
  31. {
  32. rm -f a.out
  33. cat <<END | $1 -x c -O3 $3 -DAVG_SIZE=$2 $STRINGOP -DMEMORY_COPIES=$memsize -
  34. #define BUFFER_SIZE (16*1024*1024 + AVG_SIZE*2)
  35. /*#define MEMORY_COPIES (1024*1024*64*(long long)10)*/
  36. $type t[BUFFER_SIZE];
  37. int main()
  38. {
  39. unsigned int i;
  40. for (i=0;i<((long long)MEMORY_COPIES + AVG_SIZE * 2 - 1)/AVG_SIZE*2;i++)
  41. #ifdef test_memset
  42. __builtin_memset (t+(i*1024*1024+i*1)%(BUFFER_SIZE - AVG_SIZE*2), i, (AVG_SIZE + i) % (AVG_SIZE * 2 + 0));
  43. #else
  44. __builtin_memcpy (t+(i*1024*1024+i*1)%(BUFFER_SIZE - AVG_SIZE*2), t+((i+1)*1024*1024*4+i*1)%(BUFFER_SIZE - AVG_SIZE *2), (AVG_SIZE + i) % (AVG_SIZE * 2 + 0));
  45. #endif
  46. return 0;
  47. }
  48. END
  49. TIME=`/usr/bin/time -f "%E" ./a.out 2>&1`
  50. echo -n " "$TIME
  51. echo $TIME $4 >>/tmp/accum
  52. }
  53. test2()
  54. {
  55. rm -f a.out
  56. cat <<END | clang -x c -O3 $3 -DAVG_SIZE=$2 $STRINGOP -DMEMORY_COPIES=$memsize 2>/dev/null -
  57. #define BUFFER_SIZE (16*1024*1024 + AVG_SIZE*2)
  58. /*#define MEMORY_COPIES (1024*1024*64*(long long)10)*/
  59. $type t[BUFFER_SIZE];
  60. int main()
  61. {
  62. unsigned int i;
  63. for (i=0;i<((long long)MEMORY_COPIES + AVG_SIZE * 2 - 1)/AVG_SIZE*2;i++)
  64. #ifdef test_memset
  65. __builtin_memset (t+(i*1024*1024+i*1)%(BUFFER_SIZE - AVG_SIZE*2), i, (AVG_SIZE + i) % (AVG_SIZE * 2 + 0));
  66. #else
  67. __builtin_memcpy (t+(i*1024*1024+i*1)%(BUFFER_SIZE - AVG_SIZE*2), t+((i+1)*1024*1024*4+i*1)%(BUFFER_SIZE - AVG_SIZE *2), (AVG_SIZE + i) % (AVG_SIZE * 2 + 0));
  68. #endif
  69. return 0;
  70. }
  71. END
  72. TIME=`/usr/bin/time -f "%E" ./a.out 2>&1`
  73. echo -n " "$TIME
  74. echo $TIME $4 >>/tmp/accum
  75. }
  76. testrow()
  77. {
  78. echo -n "" >/tmp/accum
  79. printf "%12i " $3
  80. test "$2" "$3" "-mstringop-strategy=libcall" libcall
  81. test "$2" "$3" "-mstringop-strategy=rep_byte -malign-stringops" rep1
  82. test "$2" "$3" "-mstringop-strategy=rep_byte -mno-align-stringops" rep1noalign
  83. test "$2" "$3" "-mstringop-strategy=rep_4byte -malign-stringops" rep4
  84. test "$2" "$3" "-mstringop-strategy=rep_4byte -mno-align-stringops" rep4noalign
  85. if [ "$mode" == 64 ]
  86. then
  87. test "$2" "$3" "-mstringop-strategy=rep_8byte -malign-stringops" rep8
  88. test "$2" "$3" "-mstringop-strategy=rep_8byte -mno-align-stringops" rep8noalign
  89. fi
  90. test "$2" "$3" "-mstringop-strategy=loop -malign-stringops" loop
  91. test "$2" "$3" "-mstringop-strategy=loop -mno-align-stringops" loopnoalign
  92. test "$2" "$3" "-mstringop-strategy=unrolled_loop -malign-stringops" unrl
  93. test "$2" "$3" "-mstringop-strategy=unrolled_loop -mno-align-stringops" unrlnoalign
  94. test "$2" "$3" "-mstringop-strategy=vector_loop -malign-stringops" sse
  95. test "$2" "$3" "-mstringop-strategy=vector_loop -mno-align-stringops -msse2" ssenoalign
  96. #test2 "$2" "$3" ""
  97. test "$2" "$3" "-mstringop-strategy=byte_loop" byte
  98. best=`cat /tmp/accum | sort | head -1`
  99. test "$2" "$3" " -fprofile-generate" >/dev/null 2>&1
  100. test "$2" "$3" " -fprofile-use"
  101. test "$2" "$3" " -minline-stringops-dynamically"
  102. echo " $best"
  103. }
  104. test_all_sizes()
  105. {
  106. if [ "$mode" == 64 ]
  107. then
  108. echo " block size libcall rep1 noalg rep4 noalg rep8 noalg loop noalg unrl noalg sse noalg byte PGO dynamic BEST"
  109. else
  110. echo " block size libcall rep1 noalg rep4 noalg loop noalg unrl noalg sse noalg byte PGO dynamic BEST"
  111. fi
  112. #for size in 1 2 3 4 6 8 10 12 14 16 24 32 48 64 128 256 512 1024 4096 8192 81920 819200 8192000
  113. #for size in 8192000 819200 81920 8192 4096 2048 1024 512 256 128 64 48 32 24 16 14 12 10 8 6 5 4 3 2 1
  114. for size in 8192000 819200 81920 20480 8192 4096 2048 1024 512 256 128 64 48 32 24 16 14 12 10 8 6 4 1
  115. #for size in 128 256 1024 4096 8192 81920 819200
  116. do
  117. testrow "$1" "$2" $size
  118. done
  119. }
  120. mode=$1
  121. shift
  122. export memsize=$1
  123. shift
  124. cmdline=$*
  125. if [ "$mode" != 32 ]
  126. then
  127. if [ "$mode" != 64 ]
  128. then
  129. echo "Usage:"
  130. echo "test_stringop mode size cmdline"
  131. echo "mode is either 32 or 64"
  132. echo "size is amount of memory copied in each test. Should be chosed small enough so runtime is less than minute for each test and sorting works"
  133. echo "Example: test_stringop 32 640000000 ./xgcc -B ./ -march=pentium3"
  134. exit
  135. fi
  136. fi
  137. echo "memcpy"
  138. export STRINGOP=""
  139. type=char
  140. test_all_sizes $mode "$cmdline -m$mode"
  141. echo "Aligned"
  142. type=long
  143. test_all_sizes $mode "$cmdline -m$mode"
  144. echo "memset"
  145. export STRINGOP="-Dtest_memset=1"
  146. type=char
  147. test_all_sizes $mode "$cmdline -m$mode"
  148. echo "Aligned"
  149. type=long
  150. test_all_sizes $mode "$cmdline -m$mode"