mem-memcpy.c 7.0 KB

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