mem-functions.c 8.7 KB

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