mem-memcpy.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 "bench.h"
  13. #include "mem-memcpy-arch.h"
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <sys/time.h>
  18. #include <errno.h>
  19. #define K 1024
  20. static const char *length_str = "1MB";
  21. static const char *routine = "default";
  22. static int iterations = 1;
  23. static bool use_clock;
  24. static int clock_fd;
  25. static bool only_prefault;
  26. static bool no_prefault;
  27. static const struct option options[] = {
  28. OPT_STRING('l', "length", &length_str, "1MB",
  29. "Specify length of memory to copy. "
  30. "available unit: B, MB, GB (upper and lower)"),
  31. OPT_STRING('r', "routine", &routine, "default",
  32. "Specify routine to copy"),
  33. OPT_INTEGER('i', "iterations", &iterations,
  34. "repeat memcpy() invocation this number of times"),
  35. OPT_BOOLEAN('c', "clock", &use_clock,
  36. "Use CPU clock for measuring"),
  37. OPT_BOOLEAN('o', "only-prefault", &only_prefault,
  38. "Show only the result with page faults before memcpy()"),
  39. OPT_BOOLEAN('n', "no-prefault", &no_prefault,
  40. "Show only the result without page faults before memcpy()"),
  41. OPT_END()
  42. };
  43. typedef void *(*memcpy_t)(void *, const void *, size_t);
  44. struct routine {
  45. const char *name;
  46. const char *desc;
  47. memcpy_t fn;
  48. };
  49. struct routine routines[] = {
  50. { "default",
  51. "Default memcpy() provided by glibc",
  52. memcpy },
  53. #ifdef ARCH_X86_64
  54. #define MEMCPY_FN(fn, name, desc) { name, desc, fn },
  55. #include "mem-memcpy-x86-64-asm-def.h"
  56. #undef MEMCPY_FN
  57. #endif
  58. { NULL,
  59. NULL,
  60. NULL }
  61. };
  62. static const char * const bench_mem_memcpy_usage[] = {
  63. "perf bench mem memcpy <options>",
  64. NULL
  65. };
  66. static struct perf_event_attr clock_attr = {
  67. .type = PERF_TYPE_HARDWARE,
  68. .config = PERF_COUNT_HW_CPU_CYCLES
  69. };
  70. static void init_clock(void)
  71. {
  72. clock_fd = sys_perf_event_open(&clock_attr, getpid(), -1, -1, 0);
  73. if (clock_fd < 0 && errno == ENOSYS)
  74. die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
  75. else
  76. BUG_ON(clock_fd < 0);
  77. }
  78. static u64 get_clock(void)
  79. {
  80. int ret;
  81. u64 clk;
  82. ret = read(clock_fd, &clk, sizeof(u64));
  83. BUG_ON(ret != sizeof(u64));
  84. return clk;
  85. }
  86. static double timeval2double(struct timeval *ts)
  87. {
  88. return (double)ts->tv_sec +
  89. (double)ts->tv_usec / (double)1000000;
  90. }
  91. static void alloc_mem(void **dst, void **src, size_t length)
  92. {
  93. *dst = zalloc(length);
  94. if (!dst)
  95. die("memory allocation failed - maybe length is too large?\n");
  96. *src = zalloc(length);
  97. if (!src)
  98. die("memory allocation failed - maybe length is too large?\n");
  99. }
  100. static u64 do_memcpy_clock(memcpy_t fn, size_t len, bool prefault)
  101. {
  102. u64 clock_start = 0ULL, clock_end = 0ULL;
  103. void *src = NULL, *dst = NULL;
  104. int i;
  105. alloc_mem(&src, &dst, len);
  106. if (prefault)
  107. fn(dst, src, len);
  108. clock_start = get_clock();
  109. for (i = 0; i < iterations; ++i)
  110. fn(dst, src, len);
  111. clock_end = get_clock();
  112. free(src);
  113. free(dst);
  114. return clock_end - clock_start;
  115. }
  116. static double do_memcpy_gettimeofday(memcpy_t fn, size_t len, bool prefault)
  117. {
  118. struct timeval tv_start, tv_end, tv_diff;
  119. void *src = NULL, *dst = NULL;
  120. int i;
  121. alloc_mem(&src, &dst, len);
  122. if (prefault)
  123. fn(dst, src, len);
  124. BUG_ON(gettimeofday(&tv_start, NULL));
  125. for (i = 0; i < iterations; ++i)
  126. fn(dst, src, len);
  127. BUG_ON(gettimeofday(&tv_end, NULL));
  128. timersub(&tv_end, &tv_start, &tv_diff);
  129. free(src);
  130. free(dst);
  131. return (double)((double)len / timeval2double(&tv_diff));
  132. }
  133. #define pf (no_prefault ? 0 : 1)
  134. #define print_bps(x) do { \
  135. if (x < K) \
  136. printf(" %14lf B/Sec", x); \
  137. else if (x < K * K) \
  138. printf(" %14lfd KB/Sec", x / K); \
  139. else if (x < K * K * K) \
  140. printf(" %14lf MB/Sec", x / K / K); \
  141. else \
  142. printf(" %14lf GB/Sec", x / K / K / K); \
  143. } while (0)
  144. int bench_mem_memcpy(int argc, const char **argv,
  145. const char *prefix __used)
  146. {
  147. int i;
  148. size_t len;
  149. double result_bps[2];
  150. u64 result_clock[2];
  151. argc = parse_options(argc, argv, options,
  152. bench_mem_memcpy_usage, 0);
  153. if (use_clock)
  154. init_clock();
  155. len = (size_t)perf_atoll((char *)length_str);
  156. result_clock[0] = result_clock[1] = 0ULL;
  157. result_bps[0] = result_bps[1] = 0.0;
  158. if ((s64)len <= 0) {
  159. fprintf(stderr, "Invalid length:%s\n", length_str);
  160. return 1;
  161. }
  162. /* same to without specifying either of prefault and no-prefault */
  163. if (only_prefault && no_prefault)
  164. only_prefault = no_prefault = false;
  165. for (i = 0; routines[i].name; i++) {
  166. if (!strcmp(routines[i].name, routine))
  167. break;
  168. }
  169. if (!routines[i].name) {
  170. printf("Unknown routine:%s\n", routine);
  171. printf("Available routines...\n");
  172. for (i = 0; routines[i].name; i++) {
  173. printf("\t%s ... %s\n",
  174. routines[i].name, routines[i].desc);
  175. }
  176. return 1;
  177. }
  178. if (bench_format == BENCH_FORMAT_DEFAULT)
  179. printf("# Copying %s Bytes ...\n\n", length_str);
  180. if (!only_prefault && !no_prefault) {
  181. /* show both of results */
  182. if (use_clock) {
  183. result_clock[0] =
  184. do_memcpy_clock(routines[i].fn, len, false);
  185. result_clock[1] =
  186. do_memcpy_clock(routines[i].fn, len, true);
  187. } else {
  188. result_bps[0] =
  189. do_memcpy_gettimeofday(routines[i].fn,
  190. len, false);
  191. result_bps[1] =
  192. do_memcpy_gettimeofday(routines[i].fn,
  193. len, true);
  194. }
  195. } else {
  196. if (use_clock) {
  197. result_clock[pf] =
  198. do_memcpy_clock(routines[i].fn,
  199. len, only_prefault);
  200. } else {
  201. result_bps[pf] =
  202. do_memcpy_gettimeofday(routines[i].fn,
  203. len, only_prefault);
  204. }
  205. }
  206. switch (bench_format) {
  207. case BENCH_FORMAT_DEFAULT:
  208. if (!only_prefault && !no_prefault) {
  209. if (use_clock) {
  210. printf(" %14lf Clock/Byte\n",
  211. (double)result_clock[0]
  212. / (double)len);
  213. printf(" %14lf Clock/Byte (with prefault)\n",
  214. (double)result_clock[1]
  215. / (double)len);
  216. } else {
  217. print_bps(result_bps[0]);
  218. printf("\n");
  219. print_bps(result_bps[1]);
  220. printf(" (with prefault)\n");
  221. }
  222. } else {
  223. if (use_clock) {
  224. printf(" %14lf Clock/Byte",
  225. (double)result_clock[pf]
  226. / (double)len);
  227. } else
  228. print_bps(result_bps[pf]);
  229. printf("%s\n", only_prefault ? " (with prefault)" : "");
  230. }
  231. break;
  232. case BENCH_FORMAT_SIMPLE:
  233. if (!only_prefault && !no_prefault) {
  234. if (use_clock) {
  235. printf("%lf %lf\n",
  236. (double)result_clock[0] / (double)len,
  237. (double)result_clock[1] / (double)len);
  238. } else {
  239. printf("%lf %lf\n",
  240. result_bps[0], result_bps[1]);
  241. }
  242. } else {
  243. if (use_clock) {
  244. printf("%lf\n", (double)result_clock[pf]
  245. / (double)len);
  246. } else
  247. printf("%lf\n", result_bps[pf]);
  248. }
  249. break;
  250. default:
  251. /* reaching this means there's some disaster: */
  252. die("unknown format: %d\n", bench_format);
  253. break;
  254. }
  255. return 0;
  256. }