mem-functions.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * mem-memcpy.c
  4. *
  5. * Simple memcpy() and memset() benchmarks
  6. *
  7. * Written by Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp>
  8. */
  9. #include "debug.h"
  10. #include "../perf.h"
  11. #include "../util/util.h"
  12. #include <subcmd/parse-options.h>
  13. #include "../util/header.h"
  14. #include "../util/cloexec.h"
  15. #include "../util/string2.h"
  16. #include "bench.h"
  17. #include "mem-memcpy-arch.h"
  18. #include "mem-memset-arch.h"
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <sys/time.h>
  23. #include <errno.h>
  24. #include <linux/time64.h>
  25. #define K 1024
  26. static const char *size_str = "1MB";
  27. static const char *function_str = "all";
  28. static int nr_loops = 1;
  29. static bool use_cycles;
  30. static int cycles_fd;
  31. static const struct option options[] = {
  32. OPT_STRING('s', "size", &size_str, "1MB",
  33. "Specify the size of the memory buffers. "
  34. "Available units: B, KB, MB, GB and TB (case insensitive)"),
  35. OPT_STRING('f', "function", &function_str, "all",
  36. "Specify the function to run, \"all\" runs all available functions, \"help\" lists them"),
  37. OPT_INTEGER('l', "nr_loops", &nr_loops,
  38. "Specify the number of loops to run. (default: 1)"),
  39. OPT_BOOLEAN('c', "cycles", &use_cycles,
  40. "Use a cycles event instead of gettimeofday() to measure performance"),
  41. OPT_END()
  42. };
  43. typedef void *(*memcpy_t)(void *, const void *, size_t);
  44. typedef void *(*memset_t)(void *, int, size_t);
  45. struct function {
  46. const char *name;
  47. const char *desc;
  48. union {
  49. memcpy_t memcpy;
  50. memset_t memset;
  51. } fn;
  52. };
  53. static struct perf_event_attr cycle_attr = {
  54. .type = PERF_TYPE_HARDWARE,
  55. .config = PERF_COUNT_HW_CPU_CYCLES
  56. };
  57. static int init_cycles(void)
  58. {
  59. cycles_fd = sys_perf_event_open(&cycle_attr, getpid(), -1, -1, perf_event_open_cloexec_flag());
  60. if (cycles_fd < 0 && errno == ENOSYS) {
  61. pr_debug("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
  62. return -1;
  63. }
  64. return cycles_fd;
  65. }
  66. static u64 get_cycles(void)
  67. {
  68. int ret;
  69. u64 clk;
  70. ret = read(cycles_fd, &clk, sizeof(u64));
  71. BUG_ON(ret != sizeof(u64));
  72. return clk;
  73. }
  74. static double timeval2double(struct timeval *ts)
  75. {
  76. return (double)ts->tv_sec + (double)ts->tv_usec / (double)USEC_PER_SEC;
  77. }
  78. #define print_bps(x) do { \
  79. if (x < K) \
  80. printf(" %14lf bytes/sec\n", x); \
  81. else if (x < K * K) \
  82. printf(" %14lfd KB/sec\n", x / K); \
  83. else if (x < K * K * K) \
  84. printf(" %14lf MB/sec\n", x / K / K); \
  85. else \
  86. printf(" %14lf GB/sec\n", x / K / K / K); \
  87. } while (0)
  88. struct bench_mem_info {
  89. const struct function *functions;
  90. u64 (*do_cycles)(const struct function *r, size_t size, void *src, void *dst);
  91. double (*do_gettimeofday)(const struct function *r, size_t size, void *src, void *dst);
  92. const char *const *usage;
  93. bool alloc_src;
  94. };
  95. static void __bench_mem_function(struct bench_mem_info *info, int r_idx, size_t size, double size_total)
  96. {
  97. const struct function *r = &info->functions[r_idx];
  98. double result_bps = 0.0;
  99. u64 result_cycles = 0;
  100. void *src = NULL, *dst = zalloc(size);
  101. printf("# function '%s' (%s)\n", r->name, r->desc);
  102. if (dst == NULL)
  103. goto out_alloc_failed;
  104. if (info->alloc_src) {
  105. src = zalloc(size);
  106. if (src == NULL)
  107. goto out_alloc_failed;
  108. }
  109. if (bench_format == BENCH_FORMAT_DEFAULT)
  110. printf("# Copying %s bytes ...\n\n", size_str);
  111. if (use_cycles) {
  112. result_cycles = info->do_cycles(r, size, src, dst);
  113. } else {
  114. result_bps = info->do_gettimeofday(r, size, src, dst);
  115. }
  116. switch (bench_format) {
  117. case BENCH_FORMAT_DEFAULT:
  118. if (use_cycles) {
  119. printf(" %14lf cycles/byte\n", (double)result_cycles/size_total);
  120. } else {
  121. print_bps(result_bps);
  122. }
  123. break;
  124. case BENCH_FORMAT_SIMPLE:
  125. if (use_cycles) {
  126. printf("%lf\n", (double)result_cycles/size_total);
  127. } else {
  128. printf("%lf\n", result_bps);
  129. }
  130. break;
  131. default:
  132. BUG_ON(1);
  133. break;
  134. }
  135. out_free:
  136. free(src);
  137. free(dst);
  138. return;
  139. out_alloc_failed:
  140. printf("# Memory allocation failed - maybe size (%s) is too large?\n", size_str);
  141. goto out_free;
  142. }
  143. static int bench_mem_common(int argc, const char **argv, struct bench_mem_info *info)
  144. {
  145. int i;
  146. size_t size;
  147. double size_total;
  148. argc = parse_options(argc, argv, options, info->usage, 0);
  149. if (use_cycles) {
  150. i = init_cycles();
  151. if (i < 0) {
  152. fprintf(stderr, "Failed to open cycles counter\n");
  153. return i;
  154. }
  155. }
  156. size = (size_t)perf_atoll((char *)size_str);
  157. size_total = (double)size * nr_loops;
  158. if ((s64)size <= 0) {
  159. fprintf(stderr, "Invalid size:%s\n", size_str);
  160. return 1;
  161. }
  162. if (!strncmp(function_str, "all", 3)) {
  163. for (i = 0; info->functions[i].name; i++)
  164. __bench_mem_function(info, i, size, size_total);
  165. return 0;
  166. }
  167. for (i = 0; info->functions[i].name; i++) {
  168. if (!strcmp(info->functions[i].name, function_str))
  169. break;
  170. }
  171. if (!info->functions[i].name) {
  172. if (strcmp(function_str, "help") && strcmp(function_str, "h"))
  173. printf("Unknown function: %s\n", function_str);
  174. printf("Available functions:\n");
  175. for (i = 0; info->functions[i].name; i++) {
  176. printf("\t%s ... %s\n",
  177. info->functions[i].name, info->functions[i].desc);
  178. }
  179. return 1;
  180. }
  181. __bench_mem_function(info, i, size, size_total);
  182. return 0;
  183. }
  184. static u64 do_memcpy_cycles(const struct function *r, size_t size, void *src, void *dst)
  185. {
  186. u64 cycle_start = 0ULL, cycle_end = 0ULL;
  187. memcpy_t fn = r->fn.memcpy;
  188. int i;
  189. /* Make sure to always prefault zero pages even if MMAP_THRESH is crossed: */
  190. memset(src, 0, size);
  191. /*
  192. * We prefault the freshly allocated memory range here,
  193. * to not measure page fault overhead:
  194. */
  195. fn(dst, src, size);
  196. cycle_start = get_cycles();
  197. for (i = 0; i < nr_loops; ++i)
  198. fn(dst, src, size);
  199. cycle_end = get_cycles();
  200. return cycle_end - cycle_start;
  201. }
  202. static double do_memcpy_gettimeofday(const struct function *r, size_t size, void *src, void *dst)
  203. {
  204. struct timeval tv_start, tv_end, tv_diff;
  205. memcpy_t fn = r->fn.memcpy;
  206. int i;
  207. /*
  208. * We prefault the freshly allocated memory range here,
  209. * to not measure page fault overhead:
  210. */
  211. fn(dst, src, size);
  212. BUG_ON(gettimeofday(&tv_start, NULL));
  213. for (i = 0; i < nr_loops; ++i)
  214. fn(dst, src, size);
  215. BUG_ON(gettimeofday(&tv_end, NULL));
  216. timersub(&tv_end, &tv_start, &tv_diff);
  217. return (double)(((double)size * nr_loops) / timeval2double(&tv_diff));
  218. }
  219. struct function memcpy_functions[] = {
  220. { .name = "default",
  221. .desc = "Default memcpy() provided by glibc",
  222. .fn.memcpy = memcpy },
  223. #ifdef HAVE_ARCH_X86_64_SUPPORT
  224. # define MEMCPY_FN(_fn, _name, _desc) {.name = _name, .desc = _desc, .fn.memcpy = _fn},
  225. # include "mem-memcpy-x86-64-asm-def.h"
  226. # undef MEMCPY_FN
  227. #endif
  228. { .name = NULL, }
  229. };
  230. static const char * const bench_mem_memcpy_usage[] = {
  231. "perf bench mem memcpy <options>",
  232. NULL
  233. };
  234. int bench_mem_memcpy(int argc, const char **argv)
  235. {
  236. struct bench_mem_info info = {
  237. .functions = memcpy_functions,
  238. .do_cycles = do_memcpy_cycles,
  239. .do_gettimeofday = do_memcpy_gettimeofday,
  240. .usage = bench_mem_memcpy_usage,
  241. .alloc_src = true,
  242. };
  243. return bench_mem_common(argc, argv, &info);
  244. }
  245. static u64 do_memset_cycles(const struct function *r, size_t size, void *src __maybe_unused, void *dst)
  246. {
  247. u64 cycle_start = 0ULL, cycle_end = 0ULL;
  248. memset_t fn = r->fn.memset;
  249. int i;
  250. /*
  251. * We prefault the freshly allocated memory range here,
  252. * to not measure page fault overhead:
  253. */
  254. fn(dst, -1, size);
  255. cycle_start = get_cycles();
  256. for (i = 0; i < nr_loops; ++i)
  257. fn(dst, i, size);
  258. cycle_end = get_cycles();
  259. return cycle_end - cycle_start;
  260. }
  261. static double do_memset_gettimeofday(const struct function *r, size_t size, void *src __maybe_unused, void *dst)
  262. {
  263. struct timeval tv_start, tv_end, tv_diff;
  264. memset_t fn = r->fn.memset;
  265. int i;
  266. /*
  267. * We prefault the freshly allocated memory range here,
  268. * to not measure page fault overhead:
  269. */
  270. fn(dst, -1, size);
  271. BUG_ON(gettimeofday(&tv_start, NULL));
  272. for (i = 0; i < nr_loops; ++i)
  273. fn(dst, i, size);
  274. BUG_ON(gettimeofday(&tv_end, NULL));
  275. timersub(&tv_end, &tv_start, &tv_diff);
  276. return (double)(((double)size * nr_loops) / timeval2double(&tv_diff));
  277. }
  278. static const char * const bench_mem_memset_usage[] = {
  279. "perf bench mem memset <options>",
  280. NULL
  281. };
  282. static const struct function memset_functions[] = {
  283. { .name = "default",
  284. .desc = "Default memset() provided by glibc",
  285. .fn.memset = memset },
  286. #ifdef HAVE_ARCH_X86_64_SUPPORT
  287. # define MEMSET_FN(_fn, _name, _desc) { .name = _name, .desc = _desc, .fn.memset = _fn },
  288. # include "mem-memset-x86-64-asm-def.h"
  289. # undef MEMSET_FN
  290. #endif
  291. { .name = NULL, }
  292. };
  293. int bench_mem_memset(int argc, const char **argv)
  294. {
  295. struct bench_mem_info info = {
  296. .functions = memset_functions,
  297. .do_cycles = do_memset_cycles,
  298. .do_gettimeofday = do_memset_gettimeofday,
  299. .usage = bench_mem_memset_usage,
  300. };
  301. return bench_mem_common(argc, argv, &info);
  302. }