mem-memcpy.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 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 int clock_fd;
  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. BUG_ON(clock_fd < 0);
  60. }
  61. static u64 get_clock(void)
  62. {
  63. int ret;
  64. u64 clk;
  65. ret = read(clock_fd, &clk, sizeof(u64));
  66. BUG_ON(ret != sizeof(u64));
  67. return clk;
  68. }
  69. static double timeval2double(struct timeval *ts)
  70. {
  71. return (double)ts->tv_sec +
  72. (double)ts->tv_usec / (double)1000000;
  73. }
  74. int bench_mem_memcpy(int argc, const char **argv,
  75. const char *prefix __used)
  76. {
  77. int i;
  78. void *dst, *src;
  79. size_t length;
  80. double bps = 0.0;
  81. struct timeval tv_start, tv_end, tv_diff;
  82. u64 clock_start, clock_end, clock_diff;
  83. clock_start = clock_end = clock_diff = 0ULL;
  84. argc = parse_options(argc, argv, options,
  85. bench_mem_memcpy_usage, 0);
  86. tv_diff.tv_sec = 0;
  87. tv_diff.tv_usec = 0;
  88. length = (size_t)perf_atoll((char *)length_str);
  89. if ((long long int)length <= 0) {
  90. fprintf(stderr, "Invalid length:%s\n", length_str);
  91. return 1;
  92. }
  93. for (i = 0; routines[i].name; i++) {
  94. if (!strcmp(routines[i].name, routine))
  95. break;
  96. }
  97. if (!routines[i].name) {
  98. printf("Unknown routine:%s\n", routine);
  99. printf("Available routines...\n");
  100. for (i = 0; routines[i].name; i++) {
  101. printf("\t%s ... %s\n",
  102. routines[i].name, routines[i].desc);
  103. }
  104. return 1;
  105. }
  106. dst = calloc(length, sizeof(char));
  107. assert(dst);
  108. src = calloc(length, sizeof(char));
  109. assert(src);
  110. if (bench_format == BENCH_FORMAT_DEFAULT) {
  111. printf("# Copying %s Bytes from %p to %p ...\n\n",
  112. length_str, src, dst);
  113. }
  114. if (use_clock) {
  115. init_clock();
  116. clock_start = get_clock();
  117. } else
  118. BUG_ON(gettimeofday(&tv_start, NULL));
  119. routines[i].fn(dst, src, length);
  120. if (use_clock) {
  121. clock_end = get_clock();
  122. clock_diff = clock_end - clock_start;
  123. } else {
  124. BUG_ON(gettimeofday(&tv_end, NULL));
  125. timersub(&tv_end, &tv_start, &tv_diff);
  126. bps = (double)((double)length / timeval2double(&tv_diff));
  127. }
  128. switch (bench_format) {
  129. case BENCH_FORMAT_DEFAULT:
  130. if (use_clock) {
  131. printf(" %14lf Clock/Byte\n",
  132. (double)clock_diff / (double)length);
  133. } else {
  134. if (bps < K)
  135. printf(" %14lf B/Sec\n", bps);
  136. else if (bps < K * K)
  137. printf(" %14lfd KB/Sec\n", bps / 1024);
  138. else if (bps < K * K * K)
  139. printf(" %14lf MB/Sec\n", bps / 1024 / 1024);
  140. else {
  141. printf(" %14lf GB/Sec\n",
  142. bps / 1024 / 1024 / 1024);
  143. }
  144. }
  145. break;
  146. case BENCH_FORMAT_SIMPLE:
  147. if (use_clock) {
  148. printf("%14lf\n",
  149. (double)clock_diff / (double)length);
  150. } else
  151. printf("%lf\n", bps);
  152. break;
  153. default:
  154. /* reaching here is something disaster */
  155. fprintf(stderr, "Unknown format:%d\n", bench_format);
  156. exit(1);
  157. break;
  158. }
  159. return 0;
  160. }