mem-memcpy.c 6.8 KB

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