mem-memset.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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_cycle;
  24. static int cycle_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 set. "
  30. "Available units: B, KB, MB, GB and TB (upper and lower)"),
  31. OPT_STRING('r', "routine", &routine, "default",
  32. "Specify routine to set"),
  33. OPT_INTEGER('i', "iterations", &iterations,
  34. "repeat memset() invocation this number of times"),
  35. OPT_BOOLEAN('c', "cycle", &use_cycle,
  36. "Use cycles event instead of gettimeofday() 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 HAVE_ARCH_X86_64_SUPPORT
  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 cycle_attr = {
  67. .type = PERF_TYPE_HARDWARE,
  68. .config = PERF_COUNT_HW_CPU_CYCLES
  69. };
  70. static void init_cycle(void)
  71. {
  72. cycle_fd = sys_perf_event_open(&cycle_attr, getpid(), -1, -1, 0);
  73. if (cycle_fd < 0 && errno == ENOSYS)
  74. die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
  75. else
  76. BUG_ON(cycle_fd < 0);
  77. }
  78. static u64 get_cycle(void)
  79. {
  80. int ret;
  81. u64 clk;
  82. ret = read(cycle_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_cycle(memset_t fn, size_t len, bool prefault)
  98. {
  99. u64 cycle_start = 0ULL, cycle_end = 0ULL;
  100. void *dst = NULL;
  101. int i;
  102. alloc_mem(&dst, len);
  103. if (prefault)
  104. fn(dst, -1, len);
  105. cycle_start = get_cycle();
  106. for (i = 0; i < iterations; ++i)
  107. fn(dst, i, len);
  108. cycle_end = get_cycle();
  109. free(dst);
  110. return cycle_end - cycle_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 __maybe_unused)
  141. {
  142. int i;
  143. size_t len;
  144. double result_bps[2];
  145. u64 result_cycle[2];
  146. argc = parse_options(argc, argv, options,
  147. bench_mem_memset_usage, 0);
  148. if (no_prefault && only_prefault) {
  149. fprintf(stderr, "Invalid options: -o and -n are mutually exclusive\n");
  150. return 1;
  151. }
  152. if (use_cycle)
  153. init_cycle();
  154. len = (size_t)perf_atoll((char *)length_str);
  155. result_cycle[0] = result_cycle[1] = 0ULL;
  156. result_bps[0] = result_bps[1] = 0.0;
  157. if ((s64)len <= 0) {
  158. fprintf(stderr, "Invalid length:%s\n", length_str);
  159. return 1;
  160. }
  161. /* same to without specifying either of prefault and no-prefault */
  162. if (only_prefault && no_prefault)
  163. only_prefault = no_prefault = false;
  164. for (i = 0; routines[i].name; i++) {
  165. if (!strcmp(routines[i].name, routine))
  166. break;
  167. }
  168. if (!routines[i].name) {
  169. printf("Unknown routine:%s\n", routine);
  170. printf("Available routines...\n");
  171. for (i = 0; routines[i].name; i++) {
  172. printf("\t%s ... %s\n",
  173. routines[i].name, routines[i].desc);
  174. }
  175. return 1;
  176. }
  177. if (bench_format == BENCH_FORMAT_DEFAULT)
  178. printf("# Copying %s Bytes ...\n\n", length_str);
  179. if (!only_prefault && !no_prefault) {
  180. /* show both of results */
  181. if (use_cycle) {
  182. result_cycle[0] =
  183. do_memset_cycle(routines[i].fn, len, false);
  184. result_cycle[1] =
  185. do_memset_cycle(routines[i].fn, len, true);
  186. } else {
  187. result_bps[0] =
  188. do_memset_gettimeofday(routines[i].fn,
  189. len, false);
  190. result_bps[1] =
  191. do_memset_gettimeofday(routines[i].fn,
  192. len, true);
  193. }
  194. } else {
  195. if (use_cycle) {
  196. result_cycle[pf] =
  197. do_memset_cycle(routines[i].fn,
  198. len, only_prefault);
  199. } else {
  200. result_bps[pf] =
  201. do_memset_gettimeofday(routines[i].fn,
  202. len, only_prefault);
  203. }
  204. }
  205. switch (bench_format) {
  206. case BENCH_FORMAT_DEFAULT:
  207. if (!only_prefault && !no_prefault) {
  208. if (use_cycle) {
  209. printf(" %14lf Cycle/Byte\n",
  210. (double)result_cycle[0]
  211. / (double)len);
  212. printf(" %14lf Cycle/Byte (with prefault)\n ",
  213. (double)result_cycle[1]
  214. / (double)len);
  215. } else {
  216. print_bps(result_bps[0]);
  217. printf("\n");
  218. print_bps(result_bps[1]);
  219. printf(" (with prefault)\n");
  220. }
  221. } else {
  222. if (use_cycle) {
  223. printf(" %14lf Cycle/Byte",
  224. (double)result_cycle[pf]
  225. / (double)len);
  226. } else
  227. print_bps(result_bps[pf]);
  228. printf("%s\n", only_prefault ? " (with prefault)" : "");
  229. }
  230. break;
  231. case BENCH_FORMAT_SIMPLE:
  232. if (!only_prefault && !no_prefault) {
  233. if (use_cycle) {
  234. printf("%lf %lf\n",
  235. (double)result_cycle[0] / (double)len,
  236. (double)result_cycle[1] / (double)len);
  237. } else {
  238. printf("%lf %lf\n",
  239. result_bps[0], result_bps[1]);
  240. }
  241. } else {
  242. if (use_cycle) {
  243. printf("%lf\n", (double)result_cycle[pf]
  244. / (double)len);
  245. } else
  246. printf("%lf\n", result_bps[pf]);
  247. }
  248. break;
  249. default:
  250. /* reaching this means there's some disaster: */
  251. die("unknown format: %d\n", bench_format);
  252. break;
  253. }
  254. return 0;
  255. }