mem-functions.c 8.8 KB

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