mem-memcpy.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 <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 <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 bool use_clock = false;
  23. static int clock_fd;
  24. static const struct option options[] = {
  25. OPT_STRING('l', "length", &length_str, "1MB",
  26. "Specify length of memory to copy. "
  27. "available unit: B, MB, GB (upper and lower)"),
  28. OPT_STRING('r', "routine", &routine, "default",
  29. "Specify routine to copy"),
  30. OPT_BOOLEAN('c', "clock", &use_clock,
  31. "Use CPU clock for measuring"),
  32. OPT_END()
  33. };
  34. struct routine {
  35. const char *name;
  36. const char *desc;
  37. void * (*fn)(void *dst, const void *src, size_t len);
  38. };
  39. struct routine routines[] = {
  40. { "default",
  41. "Default memcpy() provided by glibc",
  42. memcpy },
  43. { NULL,
  44. NULL,
  45. NULL }
  46. };
  47. static const char * const bench_mem_memcpy_usage[] = {
  48. "perf bench mem memcpy <options>",
  49. NULL
  50. };
  51. static struct perf_event_attr clock_attr = {
  52. .type = PERF_TYPE_HARDWARE,
  53. .config = PERF_COUNT_HW_CPU_CYCLES
  54. };
  55. static void init_clock(void)
  56. {
  57. clock_fd = sys_perf_event_open(&clock_attr, getpid(), -1, -1, 0);
  58. if (clock_fd < 0 && errno == ENOSYS)
  59. die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
  60. else
  61. BUG_ON(clock_fd < 0);
  62. }
  63. static u64 get_clock(void)
  64. {
  65. int ret;
  66. u64 clk;
  67. ret = read(clock_fd, &clk, sizeof(u64));
  68. BUG_ON(ret != sizeof(u64));
  69. return clk;
  70. }
  71. static double timeval2double(struct timeval *ts)
  72. {
  73. return (double)ts->tv_sec +
  74. (double)ts->tv_usec / (double)1000000;
  75. }
  76. int bench_mem_memcpy(int argc, const char **argv,
  77. const char *prefix __used)
  78. {
  79. int i;
  80. void *dst, *src;
  81. size_t length;
  82. double bps = 0.0;
  83. struct timeval tv_start, tv_end, tv_diff;
  84. u64 clock_start, clock_end, clock_diff;
  85. clock_start = clock_end = clock_diff = 0ULL;
  86. argc = parse_options(argc, argv, options,
  87. bench_mem_memcpy_usage, 0);
  88. tv_diff.tv_sec = 0;
  89. tv_diff.tv_usec = 0;
  90. length = (size_t)perf_atoll((char *)length_str);
  91. if ((s64)length <= 0) {
  92. fprintf(stderr, "Invalid length:%s\n", length_str);
  93. return 1;
  94. }
  95. for (i = 0; routines[i].name; i++) {
  96. if (!strcmp(routines[i].name, routine))
  97. break;
  98. }
  99. if (!routines[i].name) {
  100. printf("Unknown routine:%s\n", routine);
  101. printf("Available routines...\n");
  102. for (i = 0; routines[i].name; i++) {
  103. printf("\t%s ... %s\n",
  104. routines[i].name, routines[i].desc);
  105. }
  106. return 1;
  107. }
  108. dst = zalloc(length);
  109. if (!dst)
  110. die("memory allocation failed - maybe length is too large?\n");
  111. src = zalloc(length);
  112. if (!src)
  113. die("memory allocation failed - maybe length is too large?\n");
  114. if (bench_format == BENCH_FORMAT_DEFAULT) {
  115. printf("# Copying %s Bytes from %p to %p ...\n\n",
  116. length_str, src, dst);
  117. }
  118. if (use_clock) {
  119. init_clock();
  120. clock_start = get_clock();
  121. } else {
  122. BUG_ON(gettimeofday(&tv_start, NULL));
  123. }
  124. routines[i].fn(dst, src, length);
  125. if (use_clock) {
  126. clock_end = get_clock();
  127. clock_diff = clock_end - clock_start;
  128. } else {
  129. BUG_ON(gettimeofday(&tv_end, NULL));
  130. timersub(&tv_end, &tv_start, &tv_diff);
  131. bps = (double)((double)length / timeval2double(&tv_diff));
  132. }
  133. switch (bench_format) {
  134. case BENCH_FORMAT_DEFAULT:
  135. if (use_clock) {
  136. printf(" %14lf Clock/Byte\n",
  137. (double)clock_diff / (double)length);
  138. } else {
  139. if (bps < K)
  140. printf(" %14lf B/Sec\n", bps);
  141. else if (bps < K * K)
  142. printf(" %14lfd KB/Sec\n", bps / 1024);
  143. else if (bps < K * K * K)
  144. printf(" %14lf MB/Sec\n", bps / 1024 / 1024);
  145. else {
  146. printf(" %14lf GB/Sec\n",
  147. bps / 1024 / 1024 / 1024);
  148. }
  149. }
  150. break;
  151. case BENCH_FORMAT_SIMPLE:
  152. if (use_clock) {
  153. printf("%14lf\n",
  154. (double)clock_diff / (double)length);
  155. } else
  156. printf("%lf\n", bps);
  157. break;
  158. default:
  159. /* reaching this means there's some disaster: */
  160. die("unknown format: %d\n", bench_format);
  161. break;
  162. }
  163. return 0;
  164. }