mem-memcpy.c 4.1 KB

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