mem-memcpy.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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, \"all\" runs all available routines"),
  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. printf("Routine %s (%s)\n", r->name, r->desc);
  123. if (bench_format == BENCH_FORMAT_DEFAULT)
  124. printf("# Copying %s Bytes ...\n\n", length_str);
  125. if (!only_prefault && !no_prefault) {
  126. /* show both of results */
  127. if (use_cycle) {
  128. result_cycle[0] = info->do_cycle(r, len, false);
  129. result_cycle[1] = info->do_cycle(r, len, true);
  130. } else {
  131. result_bps[0] = info->do_gettimeofday(r, len, false);
  132. result_bps[1] = info->do_gettimeofday(r, len, true);
  133. }
  134. } else {
  135. if (use_cycle)
  136. result_cycle[pf] = info->do_cycle(r, len, only_prefault);
  137. else
  138. result_bps[pf] = info->do_gettimeofday(r, len, only_prefault);
  139. }
  140. switch (bench_format) {
  141. case BENCH_FORMAT_DEFAULT:
  142. if (!only_prefault && !no_prefault) {
  143. if (use_cycle) {
  144. printf(" %14lf Cycle/Byte\n",
  145. (double)result_cycle[0]
  146. / totallen);
  147. printf(" %14lf Cycle/Byte (with prefault)\n",
  148. (double)result_cycle[1]
  149. / totallen);
  150. } else {
  151. print_bps(result_bps[0]);
  152. printf("\n");
  153. print_bps(result_bps[1]);
  154. printf(" (with prefault)\n");
  155. }
  156. } else {
  157. if (use_cycle) {
  158. printf(" %14lf Cycle/Byte",
  159. (double)result_cycle[pf]
  160. / totallen);
  161. } else
  162. print_bps(result_bps[pf]);
  163. printf("%s\n", only_prefault ? " (with prefault)" : "");
  164. }
  165. break;
  166. case BENCH_FORMAT_SIMPLE:
  167. if (!only_prefault && !no_prefault) {
  168. if (use_cycle) {
  169. printf("%lf %lf\n",
  170. (double)result_cycle[0] / totallen,
  171. (double)result_cycle[1] / totallen);
  172. } else {
  173. printf("%lf %lf\n",
  174. result_bps[0], result_bps[1]);
  175. }
  176. } else {
  177. if (use_cycle) {
  178. printf("%lf\n", (double)result_cycle[pf]
  179. / totallen);
  180. } else
  181. printf("%lf\n", result_bps[pf]);
  182. }
  183. break;
  184. default:
  185. /* reaching this means there's some disaster: */
  186. die("unknown format: %d\n", bench_format);
  187. break;
  188. }
  189. }
  190. static int bench_mem_common(int argc, const char **argv,
  191. const char *prefix __maybe_unused,
  192. struct bench_mem_info *info)
  193. {
  194. int i;
  195. size_t len;
  196. double totallen;
  197. argc = parse_options(argc, argv, options,
  198. info->usage, 0);
  199. if (no_prefault && only_prefault) {
  200. fprintf(stderr, "Invalid options: -o and -n are mutually exclusive\n");
  201. return 1;
  202. }
  203. if (use_cycle)
  204. init_cycle();
  205. len = (size_t)perf_atoll((char *)length_str);
  206. totallen = (double)len * iterations;
  207. if ((s64)len <= 0) {
  208. fprintf(stderr, "Invalid length:%s\n", length_str);
  209. return 1;
  210. }
  211. /* same to without specifying either of prefault and no-prefault */
  212. if (only_prefault && no_prefault)
  213. only_prefault = no_prefault = false;
  214. if (!strncmp(routine, "all", 3)) {
  215. for (i = 0; info->routines[i].name; i++)
  216. __bench_mem_routine(info, i, len, totallen);
  217. return 0;
  218. }
  219. for (i = 0; info->routines[i].name; i++) {
  220. if (!strcmp(info->routines[i].name, routine))
  221. break;
  222. }
  223. if (!info->routines[i].name) {
  224. printf("Unknown routine:%s\n", routine);
  225. printf("Available routines...\n");
  226. for (i = 0; info->routines[i].name; i++) {
  227. printf("\t%s ... %s\n",
  228. info->routines[i].name, info->routines[i].desc);
  229. }
  230. return 1;
  231. }
  232. __bench_mem_routine(info, i, len, totallen);
  233. return 0;
  234. }
  235. static void memcpy_alloc_mem(void **dst, void **src, size_t length)
  236. {
  237. *dst = zalloc(length);
  238. if (!*dst)
  239. die("memory allocation failed - maybe length is too large?\n");
  240. *src = zalloc(length);
  241. if (!*src)
  242. die("memory allocation failed - maybe length is too large?\n");
  243. /* Make sure to always replace the zero pages even if MMAP_THRESH is crossed */
  244. memset(*src, 0, length);
  245. }
  246. static u64 do_memcpy_cycle(const struct routine *r, size_t len, bool prefault)
  247. {
  248. u64 cycle_start = 0ULL, cycle_end = 0ULL;
  249. void *src = NULL, *dst = NULL;
  250. memcpy_t fn = r->fn.memcpy;
  251. int i;
  252. memcpy_alloc_mem(&dst, &src, len);
  253. if (prefault)
  254. fn(dst, src, len);
  255. cycle_start = get_cycle();
  256. for (i = 0; i < iterations; ++i)
  257. fn(dst, src, len);
  258. cycle_end = get_cycle();
  259. free(src);
  260. free(dst);
  261. return cycle_end - cycle_start;
  262. }
  263. static double do_memcpy_gettimeofday(const struct routine *r, size_t len,
  264. bool prefault)
  265. {
  266. struct timeval tv_start, tv_end, tv_diff;
  267. memcpy_t fn = r->fn.memcpy;
  268. void *src = NULL, *dst = NULL;
  269. int i;
  270. memcpy_alloc_mem(&dst, &src, len);
  271. if (prefault)
  272. fn(dst, src, len);
  273. BUG_ON(gettimeofday(&tv_start, NULL));
  274. for (i = 0; i < iterations; ++i)
  275. fn(dst, src, len);
  276. BUG_ON(gettimeofday(&tv_end, NULL));
  277. timersub(&tv_end, &tv_start, &tv_diff);
  278. free(src);
  279. free(dst);
  280. return (double)(((double)len * iterations) / timeval2double(&tv_diff));
  281. }
  282. int bench_mem_memcpy(int argc, const char **argv,
  283. const char *prefix __maybe_unused)
  284. {
  285. struct bench_mem_info info = {
  286. .routines = memcpy_routines,
  287. .do_cycle = do_memcpy_cycle,
  288. .do_gettimeofday = do_memcpy_gettimeofday,
  289. .usage = bench_mem_memcpy_usage,
  290. };
  291. return bench_mem_common(argc, argv, prefix, &info);
  292. }
  293. static void memset_alloc_mem(void **dst, size_t length)
  294. {
  295. *dst = zalloc(length);
  296. if (!*dst)
  297. die("memory allocation failed - maybe length is too large?\n");
  298. }
  299. static u64 do_memset_cycle(const struct routine *r, size_t len, bool prefault)
  300. {
  301. u64 cycle_start = 0ULL, cycle_end = 0ULL;
  302. memset_t fn = r->fn.memset;
  303. void *dst = NULL;
  304. int i;
  305. memset_alloc_mem(&dst, len);
  306. if (prefault)
  307. fn(dst, -1, len);
  308. cycle_start = get_cycle();
  309. for (i = 0; i < iterations; ++i)
  310. fn(dst, i, len);
  311. cycle_end = get_cycle();
  312. free(dst);
  313. return cycle_end - cycle_start;
  314. }
  315. static double do_memset_gettimeofday(const struct routine *r, size_t len,
  316. bool prefault)
  317. {
  318. struct timeval tv_start, tv_end, tv_diff;
  319. memset_t fn = r->fn.memset;
  320. void *dst = NULL;
  321. int i;
  322. memset_alloc_mem(&dst, len);
  323. if (prefault)
  324. fn(dst, -1, len);
  325. BUG_ON(gettimeofday(&tv_start, NULL));
  326. for (i = 0; i < iterations; ++i)
  327. fn(dst, i, len);
  328. BUG_ON(gettimeofday(&tv_end, NULL));
  329. timersub(&tv_end, &tv_start, &tv_diff);
  330. free(dst);
  331. return (double)(((double)len * iterations) / timeval2double(&tv_diff));
  332. }
  333. static const char * const bench_mem_memset_usage[] = {
  334. "perf bench mem memset <options>",
  335. NULL
  336. };
  337. static const struct routine memset_routines[] = {
  338. { .name ="default",
  339. .desc = "Default memset() provided by glibc",
  340. .fn.memset = memset },
  341. #ifdef HAVE_ARCH_X86_64_SUPPORT
  342. #define MEMSET_FN(_fn, _name, _desc) { .name = _name, .desc = _desc, .fn.memset = _fn },
  343. #include "mem-memset-x86-64-asm-def.h"
  344. #undef MEMSET_FN
  345. #endif
  346. { .name = NULL,
  347. .desc = NULL,
  348. .fn.memset = NULL }
  349. };
  350. int bench_mem_memset(int argc, const char **argv,
  351. const char *prefix __maybe_unused)
  352. {
  353. struct bench_mem_info info = {
  354. .routines = memset_routines,
  355. .do_cycle = do_memset_cycle,
  356. .do_gettimeofday = do_memset_gettimeofday,
  357. .usage = bench_mem_memset_usage,
  358. };
  359. return bench_mem_common(argc, argv, prefix, &info);
  360. }