mem-memset.c 6.7 KB

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