mem-memcpy.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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 "mem-memset-arch.h"
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <sys/time.h>
  20. #include <errno.h>
  21. #define K 1024
  22. static const char *length_str = "1MB";
  23. static const char *routine = "default";
  24. static int iterations = 1;
  25. static bool use_cycle;
  26. static int cycle_fd;
  27. static bool only_prefault;
  28. static bool no_prefault;
  29. static const struct option options[] = {
  30. OPT_STRING('l', "length", &length_str, "1MB",
  31. "Specify length of memory to copy. "
  32. "Available units: B, KB, MB, GB and TB (upper and lower)"),
  33. OPT_STRING('r', "routine", &routine, "default",
  34. "Specify routine to copy"),
  35. OPT_INTEGER('i', "iterations", &iterations,
  36. "repeat memcpy() invocation this number of times"),
  37. OPT_BOOLEAN('c', "cycle", &use_cycle,
  38. "Use cycles event instead of gettimeofday() for measuring"),
  39. OPT_BOOLEAN('o', "only-prefault", &only_prefault,
  40. "Show only the result with page faults before memcpy()"),
  41. OPT_BOOLEAN('n', "no-prefault", &no_prefault,
  42. "Show only the result without page faults before memcpy()"),
  43. OPT_END()
  44. };
  45. typedef void *(*memcpy_t)(void *, const void *, size_t);
  46. typedef void *(*memset_t)(void *, int, size_t);
  47. struct routine {
  48. const char *name;
  49. const char *desc;
  50. union {
  51. memcpy_t memcpy;
  52. memset_t memset;
  53. } fn;
  54. };
  55. struct routine memcpy_routines[] = {
  56. { .name = "default",
  57. .desc = "Default memcpy() provided by glibc",
  58. .fn.memcpy = memcpy },
  59. #ifdef HAVE_ARCH_X86_64_SUPPORT
  60. #define MEMCPY_FN(_fn, _name, _desc) {.name = _name, .desc = _desc, .fn.memcpy = _fn},
  61. #include "mem-memcpy-x86-64-asm-def.h"
  62. #undef MEMCPY_FN
  63. #endif
  64. { NULL,
  65. NULL,
  66. {NULL} }
  67. };
  68. static const char * const bench_mem_memcpy_usage[] = {
  69. "perf bench mem memcpy <options>",
  70. NULL
  71. };
  72. static struct perf_event_attr cycle_attr = {
  73. .type = PERF_TYPE_HARDWARE,
  74. .config = PERF_COUNT_HW_CPU_CYCLES
  75. };
  76. static void init_cycle(void)
  77. {
  78. cycle_fd = sys_perf_event_open(&cycle_attr, getpid(), -1, -1,
  79. perf_event_open_cloexec_flag());
  80. if (cycle_fd < 0 && errno == ENOSYS)
  81. die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
  82. else
  83. BUG_ON(cycle_fd < 0);
  84. }
  85. static u64 get_cycle(void)
  86. {
  87. int ret;
  88. u64 clk;
  89. ret = read(cycle_fd, &clk, sizeof(u64));
  90. BUG_ON(ret != sizeof(u64));
  91. return clk;
  92. }
  93. static double timeval2double(struct timeval *ts)
  94. {
  95. return (double)ts->tv_sec +
  96. (double)ts->tv_usec / (double)1000000;
  97. }
  98. #define pf (no_prefault ? 0 : 1)
  99. #define print_bps(x) do { \
  100. if (x < K) \
  101. printf(" %14lf B/Sec", x); \
  102. else if (x < K * K) \
  103. printf(" %14lfd KB/Sec", x / K); \
  104. else if (x < K * K * K) \
  105. printf(" %14lf MB/Sec", x / K / K); \
  106. else \
  107. printf(" %14lf GB/Sec", x / K / K / K); \
  108. } while (0)
  109. struct bench_mem_info {
  110. const struct routine *routines;
  111. u64 (*do_cycle)(const struct routine *r, size_t len, bool prefault);
  112. double (*do_gettimeofday)(const struct routine *r, size_t len, bool prefault);
  113. const char *const *usage;
  114. };
  115. static void __bench_mem_routine(struct bench_mem_info *info, int r_idx, size_t len, double totallen)
  116. {
  117. const struct routine *r = &info->routines[r_idx];
  118. double result_bps[2];
  119. u64 result_cycle[2];
  120. result_cycle[0] = result_cycle[1] = 0ULL;
  121. result_bps[0] = result_bps[1] = 0.0;
  122. if (bench_format == BENCH_FORMAT_DEFAULT)
  123. printf("# Copying %s Bytes ...\n\n", length_str);
  124. if (!only_prefault && !no_prefault) {
  125. /* show both of results */
  126. if (use_cycle) {
  127. result_cycle[0] = info->do_cycle(r, len, false);
  128. result_cycle[1] = info->do_cycle(r, len, true);
  129. } else {
  130. result_bps[0] = info->do_gettimeofday(r, len, false);
  131. result_bps[1] = info->do_gettimeofday(r, len, true);
  132. }
  133. } else {
  134. if (use_cycle)
  135. result_cycle[pf] = info->do_cycle(r, len, only_prefault);
  136. else
  137. result_bps[pf] = info->do_gettimeofday(r, len, only_prefault);
  138. }
  139. switch (bench_format) {
  140. case BENCH_FORMAT_DEFAULT:
  141. if (!only_prefault && !no_prefault) {
  142. if (use_cycle) {
  143. printf(" %14lf Cycle/Byte\n",
  144. (double)result_cycle[0]
  145. / totallen);
  146. printf(" %14lf Cycle/Byte (with prefault)\n",
  147. (double)result_cycle[1]
  148. / totallen);
  149. } else {
  150. print_bps(result_bps[0]);
  151. printf("\n");
  152. print_bps(result_bps[1]);
  153. printf(" (with prefault)\n");
  154. }
  155. } else {
  156. if (use_cycle) {
  157. printf(" %14lf Cycle/Byte",
  158. (double)result_cycle[pf]
  159. / totallen);
  160. } else
  161. print_bps(result_bps[pf]);
  162. printf("%s\n", only_prefault ? " (with prefault)" : "");
  163. }
  164. break;
  165. case BENCH_FORMAT_SIMPLE:
  166. if (!only_prefault && !no_prefault) {
  167. if (use_cycle) {
  168. printf("%lf %lf\n",
  169. (double)result_cycle[0] / totallen,
  170. (double)result_cycle[1] / totallen);
  171. } else {
  172. printf("%lf %lf\n",
  173. result_bps[0], result_bps[1]);
  174. }
  175. } else {
  176. if (use_cycle) {
  177. printf("%lf\n", (double)result_cycle[pf]
  178. / totallen);
  179. } else
  180. printf("%lf\n", result_bps[pf]);
  181. }
  182. break;
  183. default:
  184. /* reaching this means there's some disaster: */
  185. die("unknown format: %d\n", bench_format);
  186. break;
  187. }
  188. }
  189. static int bench_mem_common(int argc, const char **argv,
  190. const char *prefix __maybe_unused,
  191. struct bench_mem_info *info)
  192. {
  193. int i;
  194. size_t len;
  195. double totallen;
  196. argc = parse_options(argc, argv, options,
  197. info->usage, 0);
  198. if (no_prefault && only_prefault) {
  199. fprintf(stderr, "Invalid options: -o and -n are mutually exclusive\n");
  200. return 1;
  201. }
  202. if (use_cycle)
  203. init_cycle();
  204. len = (size_t)perf_atoll((char *)length_str);
  205. totallen = (double)len * iterations;
  206. if ((s64)len <= 0) {
  207. fprintf(stderr, "Invalid length:%s\n", length_str);
  208. return 1;
  209. }
  210. /* same to without specifying either of prefault and no-prefault */
  211. if (only_prefault && no_prefault)
  212. only_prefault = no_prefault = false;
  213. for (i = 0; info->routines[i].name; i++) {
  214. if (!strcmp(info->routines[i].name, routine))
  215. break;
  216. }
  217. if (!info->routines[i].name) {
  218. printf("Unknown routine:%s\n", routine);
  219. printf("Available routines...\n");
  220. for (i = 0; info->routines[i].name; i++) {
  221. printf("\t%s ... %s\n",
  222. info->routines[i].name, info->routines[i].desc);
  223. }
  224. return 1;
  225. }
  226. __bench_mem_routine(info, i, len, totallen);
  227. return 0;
  228. }
  229. static void memcpy_alloc_mem(void **dst, void **src, size_t length)
  230. {
  231. *dst = zalloc(length);
  232. if (!*dst)
  233. die("memory allocation failed - maybe length is too large?\n");
  234. *src = zalloc(length);
  235. if (!*src)
  236. die("memory allocation failed - maybe length is too large?\n");
  237. /* Make sure to always replace the zero pages even if MMAP_THRESH is crossed */
  238. memset(*src, 0, length);
  239. }
  240. static u64 do_memcpy_cycle(const struct routine *r, size_t len, bool prefault)
  241. {
  242. u64 cycle_start = 0ULL, cycle_end = 0ULL;
  243. void *src = NULL, *dst = NULL;
  244. memcpy_t fn = r->fn.memcpy;
  245. int i;
  246. memcpy_alloc_mem(&src, &dst, len);
  247. if (prefault)
  248. fn(dst, src, len);
  249. cycle_start = get_cycle();
  250. for (i = 0; i < iterations; ++i)
  251. fn(dst, src, len);
  252. cycle_end = get_cycle();
  253. free(src);
  254. free(dst);
  255. return cycle_end - cycle_start;
  256. }
  257. static double do_memcpy_gettimeofday(const struct routine *r, size_t len,
  258. bool prefault)
  259. {
  260. struct timeval tv_start, tv_end, tv_diff;
  261. memcpy_t fn = r->fn.memcpy;
  262. void *src = NULL, *dst = NULL;
  263. int i;
  264. memcpy_alloc_mem(&src, &dst, len);
  265. if (prefault)
  266. fn(dst, src, len);
  267. BUG_ON(gettimeofday(&tv_start, NULL));
  268. for (i = 0; i < iterations; ++i)
  269. fn(dst, src, len);
  270. BUG_ON(gettimeofday(&tv_end, NULL));
  271. timersub(&tv_end, &tv_start, &tv_diff);
  272. free(src);
  273. free(dst);
  274. return (double)(((double)len * iterations) / timeval2double(&tv_diff));
  275. }
  276. int bench_mem_memcpy(int argc, const char **argv,
  277. const char *prefix __maybe_unused)
  278. {
  279. struct bench_mem_info info = {
  280. .routines = memcpy_routines,
  281. .do_cycle = do_memcpy_cycle,
  282. .do_gettimeofday = do_memcpy_gettimeofday,
  283. .usage = bench_mem_memcpy_usage,
  284. };
  285. return bench_mem_common(argc, argv, prefix, &info);
  286. }
  287. static void memset_alloc_mem(void **dst, size_t length)
  288. {
  289. *dst = zalloc(length);
  290. if (!*dst)
  291. die("memory allocation failed - maybe length is too large?\n");
  292. }
  293. static u64 do_memset_cycle(const struct routine *r, size_t len, bool prefault)
  294. {
  295. u64 cycle_start = 0ULL, cycle_end = 0ULL;
  296. memset_t fn = r->fn.memset;
  297. void *dst = NULL;
  298. int i;
  299. memset_alloc_mem(&dst, len);
  300. if (prefault)
  301. fn(dst, -1, len);
  302. cycle_start = get_cycle();
  303. for (i = 0; i < iterations; ++i)
  304. fn(dst, i, len);
  305. cycle_end = get_cycle();
  306. free(dst);
  307. return cycle_end - cycle_start;
  308. }
  309. static double do_memset_gettimeofday(const struct routine *r, size_t len,
  310. bool prefault)
  311. {
  312. struct timeval tv_start, tv_end, tv_diff;
  313. memset_t fn = r->fn.memset;
  314. void *dst = NULL;
  315. int i;
  316. memset_alloc_mem(&dst, len);
  317. if (prefault)
  318. fn(dst, -1, len);
  319. BUG_ON(gettimeofday(&tv_start, NULL));
  320. for (i = 0; i < iterations; ++i)
  321. fn(dst, i, len);
  322. BUG_ON(gettimeofday(&tv_end, NULL));
  323. timersub(&tv_end, &tv_start, &tv_diff);
  324. free(dst);
  325. return (double)(((double)len * iterations) / timeval2double(&tv_diff));
  326. }
  327. static const char * const bench_mem_memset_usage[] = {
  328. "perf bench mem memset <options>",
  329. NULL
  330. };
  331. static const struct routine memset_routines[] = {
  332. { .name ="default",
  333. .desc = "Default memset() provided by glibc",
  334. .fn.memset = memset },
  335. #ifdef HAVE_ARCH_X86_64_SUPPORT
  336. #define MEMSET_FN(_fn, _name, _desc) { .name = _name, .desc = _desc, .fn.memset = _fn },
  337. #include "mem-memset-x86-64-asm-def.h"
  338. #undef MEMSET_FN
  339. #endif
  340. { .name = NULL,
  341. .desc = NULL,
  342. .fn.memset = NULL }
  343. };
  344. int bench_mem_memset(int argc, const char **argv,
  345. const char *prefix __maybe_unused)
  346. {
  347. struct bench_mem_info info = {
  348. .routines = memset_routines,
  349. .do_cycle = do_memset_cycle,
  350. .do_gettimeofday = do_memset_gettimeofday,
  351. .usage = bench_mem_memset_usage,
  352. };
  353. return bench_mem_common(argc, argv, prefix, &info);
  354. }