string_tests.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * Copyright (c) 2008 Travis Geiselbrecht
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files
  6. * (the "Software"), to deal in the Software without restriction,
  7. * including without limitation the rights to use, copy, modify, merge,
  8. * publish, distribute, sublicense, and/or sell copies of the Software,
  9. * and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <debug.h>
  24. #include <string.h>
  25. #include <malloc.h>
  26. #include <app.h>
  27. #include <platform.h>
  28. #include <kernel/thread.h>
  29. static uint8_t *src;
  30. static uint8_t *dst;
  31. static uint8_t *src2;
  32. static uint8_t *dst2;
  33. #define BUFFER_SIZE (1024*1024)
  34. #define ITERATIONS 16
  35. extern void *mymemcpy(void *dst, const void *src, size_t len);
  36. extern void *mymemset(void *dst, int c, size_t len);
  37. static void *null_memcpy(void *dst, const void *src, size_t len)
  38. {
  39. return dst;
  40. }
  41. static time_t bench_memcpy_routine(void *memcpy_routine(void *, const void *, size_t), size_t srcalign, size_t dstalign)
  42. {
  43. int i;
  44. time_t t0;
  45. t0 = current_time();
  46. for (i=0; i < ITERATIONS; i++) {
  47. memcpy_routine(dst + dstalign, src + srcalign, BUFFER_SIZE);
  48. }
  49. return current_time() - t0;
  50. }
  51. static void bench_memcpy(void)
  52. {
  53. time_t null, libc, mine;
  54. size_t srcalign, dstalign;
  55. printf("memcpy speed test\n");
  56. thread_sleep(200); // let the debug string clear the serial port
  57. for (srcalign = 0; srcalign < 64; ) {
  58. for (dstalign = 0; dstalign < 64; ) {
  59. null = bench_memcpy_routine(&null_memcpy, srcalign, dstalign);
  60. libc = bench_memcpy_routine(&memcpy, srcalign, dstalign);
  61. mine = bench_memcpy_routine(&mymemcpy, srcalign, dstalign);
  62. printf("srcalign %lu, dstalign %lu\n", srcalign, dstalign);
  63. printf(" null memcpy %u msecs\n", null);
  64. printf(" libc memcpy %u msecs, %llu bytes/sec\n", libc, BUFFER_SIZE * ITERATIONS * 1000ULL / libc);
  65. printf(" my memcpy %u msecs, %llu bytes/sec\n", mine, BUFFER_SIZE * ITERATIONS * 1000ULL / mine);
  66. if (dstalign == 0)
  67. dstalign = 1;
  68. else
  69. dstalign <<= 1;
  70. }
  71. if (srcalign == 0)
  72. srcalign = 1;
  73. else
  74. srcalign <<= 1;
  75. }
  76. }
  77. static void fillbuf(void *ptr, size_t len, uint32_t seed)
  78. {
  79. size_t i;
  80. for (i = 0; i < len; i++) {
  81. ((char *)ptr)[i] = seed;
  82. seed *= 0x1234567;
  83. }
  84. }
  85. static void validate_memcpy(void)
  86. {
  87. size_t srcalign, dstalign, size;
  88. const size_t maxsize = 256;
  89. printf("testing memcpy for correctness\n");
  90. /*
  91. * do the simple tests to make sure that memcpy doesn't color outside
  92. * the lines for all alignment cases
  93. */
  94. for (srcalign = 0; srcalign < 64; srcalign++) {
  95. for (dstalign = 0; dstalign < 64; dstalign++) {
  96. // printf("srcalign %zu, dstalign %zu\n", srcalign, dstalign);
  97. for (size = 0; size < maxsize; size++) {
  98. // printf("srcalign %zu, dstalign %zu, size %zu\n", srcalign, dstalign, size);
  99. fillbuf(src, maxsize * 2, 567);
  100. fillbuf(src2, maxsize * 2, 567);
  101. fillbuf(dst, maxsize * 2, 123514);
  102. fillbuf(dst2, maxsize * 2, 123514);
  103. memcpy(dst + dstalign, src + srcalign, size);
  104. mymemcpy(dst2 + dstalign, src2 + srcalign, size);
  105. int comp = memcmp(dst, dst2, maxsize * 2);
  106. if (comp != 0) {
  107. printf("error! srcalign %zu, dstalign %zu, size %zu\n", srcalign, dstalign, size);
  108. }
  109. }
  110. }
  111. }
  112. }
  113. static void validate_memcpy_overlap(void)
  114. {
  115. size_t srcalign, dstalign, size;
  116. const size_t maxsize = 256;
  117. int comp;
  118. printf("testing memcpy for correctness in overlap cases\n");
  119. for (dstalign = 0; dstalign < 64; dstalign++) {
  120. for (size = 0; size < maxsize; size++) {
  121. fillbuf(src, maxsize * 2, 567);
  122. fillbuf(src2, maxsize * 2, 567);
  123. /* Case one will check cpy memory is the same - fwd*/
  124. memcpy(src + dstalign, src, size);
  125. comp = memcmp(src + dstalign, src2, size);
  126. if (comp != 0) {
  127. printf("ERROR (Case1): dstalign %zu, size %zu, ret %d\n", dstalign, size, comp);
  128. }
  129. fillbuf(src, maxsize * 2, 8588485);
  130. fillbuf(src2, maxsize * 2, 8588485);
  131. /* Case two will check cpy memory is the same - bkwd*/
  132. memcpy(src, src + dstalign, size);
  133. comp = memcmp(src, src2 + dstalign, size);
  134. if (comp != 0) {
  135. printf("ERROR (Case2): dstalign %zu, size %zu, ret %d\n", dstalign, size, comp);
  136. }
  137. }
  138. }
  139. }
  140. static time_t bench_memset_routine(void *memset_routine(void *, int, size_t), size_t dstalign)
  141. {
  142. int i;
  143. time_t t0;
  144. t0 = current_time();
  145. for (i=0; i < ITERATIONS; i++) {
  146. memset_routine(dst + dstalign, 0, BUFFER_SIZE);
  147. }
  148. return current_time() - t0;
  149. }
  150. static void bench_memset(void)
  151. {
  152. time_t libc, mine;
  153. size_t dstalign;
  154. printf("memset speed test\n");
  155. thread_sleep(200); // let the debug string clear the serial port
  156. for (dstalign = 0; dstalign < 64; dstalign++) {
  157. libc = bench_memset_routine(&memset, dstalign);
  158. mine = bench_memset_routine(&mymemset, dstalign);
  159. printf("dstalign %lu\n", dstalign);
  160. printf(" libc memset %u msecs, %llu bytes/sec\n", libc, BUFFER_SIZE * ITERATIONS * 1000ULL / libc);
  161. printf(" my memset %u msecs, %llu bytes/sec\n", mine, BUFFER_SIZE * ITERATIONS * 1000ULL / mine);
  162. }
  163. }
  164. static void validate_memset(void)
  165. {
  166. size_t dstalign, size;
  167. int c;
  168. const size_t maxsize = 256;
  169. printf("testing memset for correctness\n");
  170. for (dstalign = 0; dstalign < 64; dstalign++) {
  171. printf("align %zd\n", dstalign);
  172. for (size = 0; size < maxsize; size++) {
  173. for (c = 0; c < 256; c++) {
  174. fillbuf(dst, maxsize * 2, 123514);
  175. fillbuf(dst2, maxsize * 2, 123514);
  176. memset(dst + dstalign, c, size);
  177. mymemset(dst2 + dstalign, c, size);
  178. int comp = memcmp(dst, dst2, maxsize * 2);
  179. if (comp != 0) {
  180. printf("error! align %zu, c %d, size %zu\n", dstalign, c, size);
  181. }
  182. }
  183. }
  184. }
  185. }
  186. #if defined(WITH_LIB_CONSOLE)
  187. #include <lib/console.h>
  188. static int string_tests(int argc, cmd_args *argv)
  189. {
  190. src = memalign(64, BUFFER_SIZE + 256);
  191. dst = memalign(64, BUFFER_SIZE + 256);
  192. src2 = memalign(64, BUFFER_SIZE + 256);
  193. dst2 = memalign(64, BUFFER_SIZE + 256);
  194. printf("src %p, dst %p\n", src, dst);
  195. printf("src2 %p, dst2 %p\n", src2, dst2);
  196. if (argc < 3) {
  197. printf("not enough arguments:\n");
  198. usage:
  199. printf("%s validate <routine>\n", argv[0].str);
  200. printf("%s bench <routine>\n", argv[0].str);
  201. goto out;
  202. }
  203. if (!strcmp(argv[1].str, "validate")) {
  204. if (!strcmp(argv[2].str, "memcpy")) {
  205. validate_memcpy();
  206. } else if (!strcmp(argv[2].str, "memset")) {
  207. validate_memset();
  208. } else if (!strcmp(argv[2].str, "memcpy_overlap")) {
  209. validate_memcpy_overlap();
  210. }
  211. } else if (!strcmp(argv[1].str, "bench")) {
  212. if (!strcmp(argv[2].str, "memcpy")) {
  213. bench_memcpy();
  214. } else if (!strcmp(argv[2].str, "memset")) {
  215. bench_memset();
  216. }
  217. } else {
  218. goto usage;
  219. }
  220. out:
  221. free(src);
  222. free(dst);
  223. free(src2);
  224. free(dst2);
  225. return 0;
  226. }
  227. STATIC_COMMAND_START
  228. { "string", NULL, &string_tests },
  229. STATIC_COMMAND_END(stringtests);
  230. #endif
  231. APP_START(stringtests)
  232. APP_END