mem-memcpy.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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 int bench_mem_common(int argc, const char **argv,
  116. const char *prefix __maybe_unused,
  117. struct bench_mem_info *info)
  118. {
  119. int i;
  120. size_t len;
  121. double totallen;
  122. double result_bps[2];
  123. u64 result_cycle[2];
  124. argc = parse_options(argc, argv, options,
  125. info->usage, 0);
  126. if (no_prefault && only_prefault) {
  127. fprintf(stderr, "Invalid options: -o and -n are mutually exclusive\n");
  128. return 1;
  129. }
  130. if (use_cycle)
  131. init_cycle();
  132. len = (size_t)perf_atoll((char *)length_str);
  133. totallen = (double)len * iterations;
  134. result_cycle[0] = result_cycle[1] = 0ULL;
  135. result_bps[0] = result_bps[1] = 0.0;
  136. if ((s64)len <= 0) {
  137. fprintf(stderr, "Invalid length:%s\n", length_str);
  138. return 1;
  139. }
  140. /* same to without specifying either of prefault and no-prefault */
  141. if (only_prefault && no_prefault)
  142. only_prefault = no_prefault = false;
  143. for (i = 0; info->routines[i].name; i++) {
  144. if (!strcmp(info->routines[i].name, routine))
  145. break;
  146. }
  147. if (!info->routines[i].name) {
  148. printf("Unknown routine:%s\n", routine);
  149. printf("Available routines...\n");
  150. for (i = 0; info->routines[i].name; i++) {
  151. printf("\t%s ... %s\n",
  152. info->routines[i].name, info->routines[i].desc);
  153. }
  154. return 1;
  155. }
  156. if (bench_format == BENCH_FORMAT_DEFAULT)
  157. printf("# Copying %s Bytes ...\n\n", length_str);
  158. if (!only_prefault && !no_prefault) {
  159. /* show both of results */
  160. if (use_cycle) {
  161. result_cycle[0] =
  162. info->do_cycle(&info->routines[i], len, false);
  163. result_cycle[1] =
  164. info->do_cycle(&info->routines[i], len, true);
  165. } else {
  166. result_bps[0] =
  167. info->do_gettimeofday(&info->routines[i],
  168. len, false);
  169. result_bps[1] =
  170. info->do_gettimeofday(&info->routines[i],
  171. len, true);
  172. }
  173. } else {
  174. if (use_cycle) {
  175. result_cycle[pf] =
  176. info->do_cycle(&info->routines[i],
  177. len, only_prefault);
  178. } else {
  179. result_bps[pf] =
  180. info->do_gettimeofday(&info->routines[i],
  181. len, only_prefault);
  182. }
  183. }
  184. switch (bench_format) {
  185. case BENCH_FORMAT_DEFAULT:
  186. if (!only_prefault && !no_prefault) {
  187. if (use_cycle) {
  188. printf(" %14lf Cycle/Byte\n",
  189. (double)result_cycle[0]
  190. / totallen);
  191. printf(" %14lf Cycle/Byte (with prefault)\n",
  192. (double)result_cycle[1]
  193. / totallen);
  194. } else {
  195. print_bps(result_bps[0]);
  196. printf("\n");
  197. print_bps(result_bps[1]);
  198. printf(" (with prefault)\n");
  199. }
  200. } else {
  201. if (use_cycle) {
  202. printf(" %14lf Cycle/Byte",
  203. (double)result_cycle[pf]
  204. / totallen);
  205. } else
  206. print_bps(result_bps[pf]);
  207. printf("%s\n", only_prefault ? " (with prefault)" : "");
  208. }
  209. break;
  210. case BENCH_FORMAT_SIMPLE:
  211. if (!only_prefault && !no_prefault) {
  212. if (use_cycle) {
  213. printf("%lf %lf\n",
  214. (double)result_cycle[0] / totallen,
  215. (double)result_cycle[1] / totallen);
  216. } else {
  217. printf("%lf %lf\n",
  218. result_bps[0], result_bps[1]);
  219. }
  220. } else {
  221. if (use_cycle) {
  222. printf("%lf\n", (double)result_cycle[pf]
  223. / totallen);
  224. } else
  225. printf("%lf\n", result_bps[pf]);
  226. }
  227. break;
  228. default:
  229. /* reaching this means there's some disaster: */
  230. die("unknown format: %d\n", bench_format);
  231. break;
  232. }
  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(&src, &dst, 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(&src, &dst, 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. }