mem-memcpy.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * mem-memcpy.c
  3. *
  4. * memcpy: Simple memory copy in various ways
  5. *
  6. * Written by Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp>
  7. */
  8. #include <ctype.h>
  9. #include "../perf.h"
  10. #include "../util/util.h"
  11. #include "../util/parse-options.h"
  12. #include "../util/header.h"
  13. #include "bench.h"
  14. #include "mem-memcpy-arch.h"
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <sys/time.h>
  19. #include <errno.h>
  20. #define K 1024
  21. static const char *length_str = "1MB";
  22. static const char *routine = "default";
  23. static bool use_clock;
  24. static int clock_fd;
  25. static bool only_prefault;
  26. static bool no_prefault;
  27. static const struct option options[] = {
  28. OPT_STRING('l', "length", &length_str, "1MB",
  29. "Specify length of memory to copy. "
  30. "available unit: B, MB, GB (upper and lower)"),
  31. OPT_STRING('r', "routine", &routine, "default",
  32. "Specify routine to copy"),
  33. OPT_BOOLEAN('c', "clock", &use_clock,
  34. "Use CPU clock for measuring"),
  35. OPT_BOOLEAN('o', "only-prefault", &only_prefault,
  36. "Show only the result with page faults before memcpy()"),
  37. OPT_BOOLEAN('n', "no-prefault", &no_prefault,
  38. "Show only the result without page faults before memcpy()"),
  39. OPT_END()
  40. };
  41. typedef void *(*memcpy_t)(void *, const void *, size_t);
  42. struct routine {
  43. const char *name;
  44. const char *desc;
  45. memcpy_t fn;
  46. };
  47. struct routine routines[] = {
  48. { "default",
  49. "Default memcpy() provided by glibc",
  50. memcpy },
  51. #ifdef ARCH_X86_64
  52. #define MEMCPY_FN(fn, name, desc) { name, desc, fn },
  53. #include "mem-memcpy-x86-64-asm-def.h"
  54. #undef MEMCPY_FN
  55. #endif
  56. { NULL,
  57. NULL,
  58. NULL }
  59. };
  60. static const char * const bench_mem_memcpy_usage[] = {
  61. "perf bench mem memcpy <options>",
  62. NULL
  63. };
  64. static struct perf_event_attr clock_attr = {
  65. .type = PERF_TYPE_HARDWARE,
  66. .config = PERF_COUNT_HW_CPU_CYCLES
  67. };
  68. static void init_clock(void)
  69. {
  70. clock_fd = sys_perf_event_open(&clock_attr, getpid(), -1, -1, 0);
  71. if (clock_fd < 0 && errno == ENOSYS)
  72. die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
  73. else
  74. BUG_ON(clock_fd < 0);
  75. }
  76. static u64 get_clock(void)
  77. {
  78. int ret;
  79. u64 clk;
  80. ret = read(clock_fd, &clk, sizeof(u64));
  81. BUG_ON(ret != sizeof(u64));
  82. return clk;
  83. }
  84. static double timeval2double(struct timeval *ts)
  85. {
  86. return (double)ts->tv_sec +
  87. (double)ts->tv_usec / (double)1000000;
  88. }
  89. static void alloc_mem(void **dst, void **src, size_t length)
  90. {
  91. *dst = zalloc(length);
  92. if (!dst)
  93. die("memory allocation failed - maybe length is too large?\n");
  94. *src = zalloc(length);
  95. if (!src)
  96. die("memory allocation failed - maybe length is too large?\n");
  97. }
  98. static u64 do_memcpy_clock(memcpy_t fn, size_t len, bool prefault)
  99. {
  100. u64 clock_start = 0ULL, clock_end = 0ULL;
  101. void *src = NULL, *dst = NULL;
  102. alloc_mem(&src, &dst, len);
  103. if (prefault)
  104. fn(dst, src, len);
  105. clock_start = get_clock();
  106. fn(dst, src, len);
  107. clock_end = get_clock();
  108. free(src);
  109. free(dst);
  110. return clock_end - clock_start;
  111. }
  112. static double do_memcpy_gettimeofday(memcpy_t fn, size_t len, bool prefault)
  113. {
  114. struct timeval tv_start, tv_end, tv_diff;
  115. void *src = NULL, *dst = NULL;
  116. alloc_mem(&src, &dst, len);
  117. if (prefault)
  118. fn(dst, src, len);
  119. BUG_ON(gettimeofday(&tv_start, NULL));
  120. fn(dst, src, len);
  121. BUG_ON(gettimeofday(&tv_end, NULL));
  122. timersub(&tv_end, &tv_start, &tv_diff);
  123. free(src);
  124. free(dst);
  125. return (double)((double)len / timeval2double(&tv_diff));
  126. }
  127. #define pf (no_prefault ? 0 : 1)
  128. #define print_bps(x) do { \
  129. if (x < K) \
  130. printf(" %14lf B/Sec", x); \
  131. else if (x < K * K) \
  132. printf(" %14lfd KB/Sec", x / K); \
  133. else if (x < K * K * K) \
  134. printf(" %14lf MB/Sec", x / K / K); \
  135. else \
  136. printf(" %14lf GB/Sec", x / K / K / K); \
  137. } while (0)
  138. int bench_mem_memcpy(int argc, const char **argv,
  139. const char *prefix __used)
  140. {
  141. int i;
  142. size_t len;
  143. double result_bps[2];
  144. u64 result_clock[2];
  145. argc = parse_options(argc, argv, options,
  146. bench_mem_memcpy_usage, 0);
  147. if (use_clock)
  148. init_clock();
  149. len = (size_t)perf_atoll((char *)length_str);
  150. result_clock[0] = result_clock[1] = 0ULL;
  151. result_bps[0] = result_bps[1] = 0.0;
  152. if ((s64)len <= 0) {
  153. fprintf(stderr, "Invalid length:%s\n", length_str);
  154. return 1;
  155. }
  156. /* same to without specifying either of prefault and no-prefault */
  157. if (only_prefault && no_prefault)
  158. only_prefault = no_prefault = false;
  159. for (i = 0; routines[i].name; i++) {
  160. if (!strcmp(routines[i].name, routine))
  161. break;
  162. }
  163. if (!routines[i].name) {
  164. printf("Unknown routine:%s\n", routine);
  165. printf("Available routines...\n");
  166. for (i = 0; routines[i].name; i++) {
  167. printf("\t%s ... %s\n",
  168. routines[i].name, routines[i].desc);
  169. }
  170. return 1;
  171. }
  172. if (bench_format == BENCH_FORMAT_DEFAULT)
  173. printf("# Copying %s Bytes ...\n\n", length_str);
  174. if (!only_prefault && !no_prefault) {
  175. /* show both of results */
  176. if (use_clock) {
  177. result_clock[0] =
  178. do_memcpy_clock(routines[i].fn, len, false);
  179. result_clock[1] =
  180. do_memcpy_clock(routines[i].fn, len, true);
  181. } else {
  182. result_bps[0] =
  183. do_memcpy_gettimeofday(routines[i].fn,
  184. len, false);
  185. result_bps[1] =
  186. do_memcpy_gettimeofday(routines[i].fn,
  187. len, true);
  188. }
  189. } else {
  190. if (use_clock) {
  191. result_clock[pf] =
  192. do_memcpy_clock(routines[i].fn,
  193. len, only_prefault);
  194. } else {
  195. result_bps[pf] =
  196. do_memcpy_gettimeofday(routines[i].fn,
  197. len, only_prefault);
  198. }
  199. }
  200. switch (bench_format) {
  201. case BENCH_FORMAT_DEFAULT:
  202. if (!only_prefault && !no_prefault) {
  203. if (use_clock) {
  204. printf(" %14lf Clock/Byte\n",
  205. (double)result_clock[0]
  206. / (double)len);
  207. printf(" %14lf Clock/Byte (with prefault)\n",
  208. (double)result_clock[1]
  209. / (double)len);
  210. } else {
  211. print_bps(result_bps[0]);
  212. printf("\n");
  213. print_bps(result_bps[1]);
  214. printf(" (with prefault)\n");
  215. }
  216. } else {
  217. if (use_clock) {
  218. printf(" %14lf Clock/Byte",
  219. (double)result_clock[pf]
  220. / (double)len);
  221. } else
  222. print_bps(result_bps[pf]);
  223. printf("%s\n", only_prefault ? " (with prefault)" : "");
  224. }
  225. break;
  226. case BENCH_FORMAT_SIMPLE:
  227. if (!only_prefault && !no_prefault) {
  228. if (use_clock) {
  229. printf("%lf %lf\n",
  230. (double)result_clock[0] / (double)len,
  231. (double)result_clock[1] / (double)len);
  232. } else {
  233. printf("%lf %lf\n",
  234. result_bps[0], result_bps[1]);
  235. }
  236. } else {
  237. if (use_clock) {
  238. printf("%lf\n", (double)result_clock[pf]
  239. / (double)len);
  240. } else
  241. printf("%lf\n", result_bps[pf]);
  242. }
  243. break;
  244. default:
  245. /* reaching this means there's some disaster: */
  246. die("unknown format: %d\n", bench_format);
  247. break;
  248. }
  249. return 0;
  250. }