mem-memset.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * mem-memset.c
  3. *
  4. * memset: Simple memory set in various ways
  5. *
  6. * Trivial clone of mem-memcpy.c.
  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-memset-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 memset() 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 memset()"),
  39. OPT_BOOLEAN('n', "no-prefault", &no_prefault,
  40. "Show only the result without page faults before memset()"),
  41. OPT_END()
  42. };
  43. typedef void *(*memset_t)(void *, int, size_t);
  44. struct routine {
  45. const char *name;
  46. const char *desc;
  47. memset_t fn;
  48. };
  49. static const struct routine routines[] = {
  50. { "default",
  51. "Default memset() provided by glibc",
  52. memset },
  53. #ifdef ARCH_X86_64
  54. #define MEMSET_FN(fn, name, desc) { name, desc, fn },
  55. #include "mem-memset-x86-64-asm-def.h"
  56. #undef MEMSET_FN
  57. #endif
  58. { NULL,
  59. NULL,
  60. NULL }
  61. };
  62. static const char * const bench_mem_memset_usage[] = {
  63. "perf bench mem memset <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, size_t length)
  92. {
  93. *dst = zalloc(length);
  94. if (!dst)
  95. die("memory allocation failed - maybe length is too large?\n");
  96. }
  97. static u64 do_memset_clock(memset_t fn, size_t len, bool prefault)
  98. {
  99. u64 clock_start = 0ULL, clock_end = 0ULL;
  100. void *dst = NULL;
  101. int i;
  102. alloc_mem(&dst, len);
  103. if (prefault)
  104. fn(dst, -1, len);
  105. clock_start = get_clock();
  106. for (i = 0; i < iterations; ++i)
  107. fn(dst, i, len);
  108. clock_end = get_clock();
  109. free(dst);
  110. return clock_end - clock_start;
  111. }
  112. static double do_memset_gettimeofday(memset_t fn, size_t len, bool prefault)
  113. {
  114. struct timeval tv_start, tv_end, tv_diff;
  115. void *dst = NULL;
  116. int i;
  117. alloc_mem(&dst, len);
  118. if (prefault)
  119. fn(dst, -1, len);
  120. BUG_ON(gettimeofday(&tv_start, NULL));
  121. for (i = 0; i < iterations; ++i)
  122. fn(dst, i, len);
  123. BUG_ON(gettimeofday(&tv_end, NULL));
  124. timersub(&tv_end, &tv_start, &tv_diff);
  125. free(dst);
  126. return (double)((double)len / timeval2double(&tv_diff));
  127. }
  128. #define pf (no_prefault ? 0 : 1)
  129. #define print_bps(x) do { \
  130. if (x < K) \
  131. printf(" %14lf B/Sec", x); \
  132. else if (x < K * K) \
  133. printf(" %14lfd KB/Sec", x / K); \
  134. else if (x < K * K * K) \
  135. printf(" %14lf MB/Sec", x / K / K); \
  136. else \
  137. printf(" %14lf GB/Sec", x / K / K / K); \
  138. } while (0)
  139. int bench_mem_memset(int argc, const char **argv,
  140. const char *prefix __used)
  141. {
  142. int i;
  143. size_t len;
  144. double result_bps[2];
  145. u64 result_clock[2];
  146. argc = parse_options(argc, argv, options,
  147. bench_mem_memset_usage, 0);
  148. if (use_clock)
  149. init_clock();
  150. len = (size_t)perf_atoll((char *)length_str);
  151. result_clock[0] = result_clock[1] = 0ULL;
  152. result_bps[0] = result_bps[1] = 0.0;
  153. if ((s64)len <= 0) {
  154. fprintf(stderr, "Invalid length:%s\n", length_str);
  155. return 1;
  156. }
  157. /* same to without specifying either of prefault and no-prefault */
  158. if (only_prefault && no_prefault)
  159. only_prefault = no_prefault = false;
  160. for (i = 0; routines[i].name; i++) {
  161. if (!strcmp(routines[i].name, routine))
  162. break;
  163. }
  164. if (!routines[i].name) {
  165. printf("Unknown routine:%s\n", routine);
  166. printf("Available routines...\n");
  167. for (i = 0; routines[i].name; i++) {
  168. printf("\t%s ... %s\n",
  169. routines[i].name, routines[i].desc);
  170. }
  171. return 1;
  172. }
  173. if (bench_format == BENCH_FORMAT_DEFAULT)
  174. printf("# Copying %s Bytes ...\n\n", length_str);
  175. if (!only_prefault && !no_prefault) {
  176. /* show both of results */
  177. if (use_clock) {
  178. result_clock[0] =
  179. do_memset_clock(routines[i].fn, len, false);
  180. result_clock[1] =
  181. do_memset_clock(routines[i].fn, len, true);
  182. } else {
  183. result_bps[0] =
  184. do_memset_gettimeofday(routines[i].fn,
  185. len, false);
  186. result_bps[1] =
  187. do_memset_gettimeofday(routines[i].fn,
  188. len, true);
  189. }
  190. } else {
  191. if (use_clock) {
  192. result_clock[pf] =
  193. do_memset_clock(routines[i].fn,
  194. len, only_prefault);
  195. } else {
  196. result_bps[pf] =
  197. do_memset_gettimeofday(routines[i].fn,
  198. len, only_prefault);
  199. }
  200. }
  201. switch (bench_format) {
  202. case BENCH_FORMAT_DEFAULT:
  203. if (!only_prefault && !no_prefault) {
  204. if (use_clock) {
  205. printf(" %14lf Clock/Byte\n",
  206. (double)result_clock[0]
  207. / (double)len);
  208. printf(" %14lf Clock/Byte (with prefault)\n ",
  209. (double)result_clock[1]
  210. / (double)len);
  211. } else {
  212. print_bps(result_bps[0]);
  213. printf("\n");
  214. print_bps(result_bps[1]);
  215. printf(" (with prefault)\n");
  216. }
  217. } else {
  218. if (use_clock) {
  219. printf(" %14lf Clock/Byte",
  220. (double)result_clock[pf]
  221. / (double)len);
  222. } else
  223. print_bps(result_bps[pf]);
  224. printf("%s\n", only_prefault ? " (with prefault)" : "");
  225. }
  226. break;
  227. case BENCH_FORMAT_SIMPLE:
  228. if (!only_prefault && !no_prefault) {
  229. if (use_clock) {
  230. printf("%lf %lf\n",
  231. (double)result_clock[0] / (double)len,
  232. (double)result_clock[1] / (double)len);
  233. } else {
  234. printf("%lf %lf\n",
  235. result_bps[0], result_bps[1]);
  236. }
  237. } else {
  238. if (use_clock) {
  239. printf("%lf\n", (double)result_clock[pf]
  240. / (double)len);
  241. } else
  242. printf("%lf\n", result_bps[pf]);
  243. }
  244. break;
  245. default:
  246. /* reaching this means there's some disaster: */
  247. die("unknown format: %d\n", bench_format);
  248. break;
  249. }
  250. return 0;
  251. }