mem-memset.c 6.3 KB

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