Hitoshi Mitake | 827f3b4 | 2009-11-18 00:20:09 +0900 | [diff] [blame] | 1 | /* |
| 2 | * mem-memcpy.c |
| 3 | * |
Ingo Molnar | 13839ec | 2015-10-19 10:04:17 +0200 | [diff] [blame] | 4 | * Simple memcpy() and memset() benchmarks |
Hitoshi Mitake | 827f3b4 | 2009-11-18 00:20:09 +0900 | [diff] [blame] | 5 | * |
| 6 | * Written by Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp> |
| 7 | */ |
Hitoshi Mitake | 827f3b4 | 2009-11-18 00:20:09 +0900 | [diff] [blame] | 8 | |
| 9 | #include "../perf.h" |
| 10 | #include "../util/util.h" |
| 11 | #include "../util/parse-options.h" |
Hitoshi Mitake | 827f3b4 | 2009-11-18 00:20:09 +0900 | [diff] [blame] | 12 | #include "../util/header.h" |
Yann Droneaud | 57480d2 | 2014-06-30 22:28:47 +0200 | [diff] [blame] | 13 | #include "../util/cloexec.h" |
Hitoshi Mitake | 827f3b4 | 2009-11-18 00:20:09 +0900 | [diff] [blame] | 14 | #include "bench.h" |
Hitoshi Mitake | 49ce8fc | 2010-11-25 16:04:52 +0900 | [diff] [blame] | 15 | #include "mem-memcpy-arch.h" |
Rabin Vincent | 5bce1a5 | 2014-12-02 16:50:40 +0100 | [diff] [blame] | 16 | #include "mem-memset-arch.h" |
Hitoshi Mitake | 827f3b4 | 2009-11-18 00:20:09 +0900 | [diff] [blame] | 17 | |
| 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <string.h> |
| 21 | #include <sys/time.h> |
| 22 | #include <errno.h> |
| 23 | |
| 24 | #define K 1024 |
| 25 | |
Hitoshi Mitake | 12eac0b | 2009-11-20 12:37:17 +0900 | [diff] [blame] | 26 | static const char *length_str = "1MB"; |
Ingo Molnar | 2761974 | 2015-10-19 10:04:18 +0200 | [diff] [blame] | 27 | static const char *routine = "all"; |
Jan Beulich | e3e877e | 2012-01-18 13:29:59 +0000 | [diff] [blame] | 28 | static int iterations = 1; |
Hitoshi Mitake | 17d7a11 | 2012-07-02 22:46:17 +0900 | [diff] [blame] | 29 | static bool use_cycle; |
| 30 | static int cycle_fd; |
Hitoshi Mitake | 827f3b4 | 2009-11-18 00:20:09 +0900 | [diff] [blame] | 31 | |
| 32 | static const struct option options[] = { |
| 33 | OPT_STRING('l', "length", &length_str, "1MB", |
| 34 | "Specify length of memory to copy. " |
Namhyung Kim | 08942f6 | 2012-06-20 15:08:06 +0900 | [diff] [blame] | 35 | "Available units: B, KB, MB, GB and TB (upper and lower)"), |
Ingo Molnar | 2761974 | 2015-10-19 10:04:18 +0200 | [diff] [blame] | 36 | OPT_STRING('r', "routine", &routine, "all", |
Borislav Petkov | dfecb95 | 2015-02-26 19:02:43 +0100 | [diff] [blame] | 37 | "Specify routine to copy, \"all\" runs all available routines"), |
Jan Beulich | e3e877e | 2012-01-18 13:29:59 +0000 | [diff] [blame] | 38 | OPT_INTEGER('i', "iterations", &iterations, |
| 39 | "repeat memcpy() invocation this number of times"), |
Hitoshi Mitake | 17d7a11 | 2012-07-02 22:46:17 +0900 | [diff] [blame] | 40 | OPT_BOOLEAN('c', "cycle", &use_cycle, |
Namhyung Kim | 08942f6 | 2012-06-20 15:08:06 +0900 | [diff] [blame] | 41 | "Use cycles event instead of gettimeofday() for measuring"), |
Hitoshi Mitake | 827f3b4 | 2009-11-18 00:20:09 +0900 | [diff] [blame] | 42 | OPT_END() |
| 43 | }; |
| 44 | |
Hitoshi Mitake | 49ce8fc | 2010-11-25 16:04:52 +0900 | [diff] [blame] | 45 | typedef void *(*memcpy_t)(void *, const void *, size_t); |
Rabin Vincent | 5bce1a5 | 2014-12-02 16:50:40 +0100 | [diff] [blame] | 46 | typedef void *(*memset_t)(void *, int, size_t); |
Hitoshi Mitake | 49ce8fc | 2010-11-25 16:04:52 +0900 | [diff] [blame] | 47 | |
Hitoshi Mitake | 827f3b4 | 2009-11-18 00:20:09 +0900 | [diff] [blame] | 48 | struct routine { |
| 49 | const char *name; |
| 50 | const char *desc; |
Rabin Vincent | 308197b | 2014-12-02 16:50:39 +0100 | [diff] [blame] | 51 | union { |
| 52 | memcpy_t memcpy; |
Rabin Vincent | 5bce1a5 | 2014-12-02 16:50:40 +0100 | [diff] [blame] | 53 | memset_t memset; |
Rabin Vincent | 308197b | 2014-12-02 16:50:39 +0100 | [diff] [blame] | 54 | } fn; |
Hitoshi Mitake | 827f3b4 | 2009-11-18 00:20:09 +0900 | [diff] [blame] | 55 | }; |
| 56 | |
Rabin Vincent | 308197b | 2014-12-02 16:50:39 +0100 | [diff] [blame] | 57 | struct routine memcpy_routines[] = { |
Ingo Molnar | 13839ec | 2015-10-19 10:04:17 +0200 | [diff] [blame] | 58 | { .name = "default", |
| 59 | .desc = "Default memcpy() provided by glibc", |
| 60 | .fn.memcpy = memcpy }, |
| 61 | |
Ingo Molnar | 89fe808 | 2013-09-30 12:07:11 +0200 | [diff] [blame] | 62 | #ifdef HAVE_ARCH_X86_64_SUPPORT |
Ingo Molnar | 13839ec | 2015-10-19 10:04:17 +0200 | [diff] [blame] | 63 | # define MEMCPY_FN(_fn, _name, _desc) {.name = _name, .desc = _desc, .fn.memcpy = _fn}, |
| 64 | # include "mem-memcpy-x86-64-asm-def.h" |
| 65 | # undef MEMCPY_FN |
Hitoshi Mitake | 49ce8fc | 2010-11-25 16:04:52 +0900 | [diff] [blame] | 66 | #endif |
| 67 | |
Ingo Molnar | 13839ec | 2015-10-19 10:04:17 +0200 | [diff] [blame] | 68 | { NULL, } |
Hitoshi Mitake | 827f3b4 | 2009-11-18 00:20:09 +0900 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | static const char * const bench_mem_memcpy_usage[] = { |
| 72 | "perf bench mem memcpy <options>", |
| 73 | NULL |
| 74 | }; |
| 75 | |
Hitoshi Mitake | 17d7a11 | 2012-07-02 22:46:17 +0900 | [diff] [blame] | 76 | static struct perf_event_attr cycle_attr = { |
Hitoshi Mitake | 12eac0b | 2009-11-20 12:37:17 +0900 | [diff] [blame] | 77 | .type = PERF_TYPE_HARDWARE, |
| 78 | .config = PERF_COUNT_HW_CPU_CYCLES |
Hitoshi Mitake | 827f3b4 | 2009-11-18 00:20:09 +0900 | [diff] [blame] | 79 | }; |
| 80 | |
Hitoshi Mitake | 17d7a11 | 2012-07-02 22:46:17 +0900 | [diff] [blame] | 81 | static void init_cycle(void) |
Hitoshi Mitake | 827f3b4 | 2009-11-18 00:20:09 +0900 | [diff] [blame] | 82 | { |
Ingo Molnar | 13839ec | 2015-10-19 10:04:17 +0200 | [diff] [blame] | 83 | cycle_fd = sys_perf_event_open(&cycle_attr, getpid(), -1, -1, perf_event_open_cloexec_flag()); |
Hitoshi Mitake | 12eac0b | 2009-11-20 12:37:17 +0900 | [diff] [blame] | 84 | |
Hitoshi Mitake | 17d7a11 | 2012-07-02 22:46:17 +0900 | [diff] [blame] | 85 | if (cycle_fd < 0 && errno == ENOSYS) |
Hitoshi Mitake | 12eac0b | 2009-11-20 12:37:17 +0900 | [diff] [blame] | 86 | die("No CONFIG_PERF_EVENTS=y kernel support configured?\n"); |
| 87 | else |
Hitoshi Mitake | 17d7a11 | 2012-07-02 22:46:17 +0900 | [diff] [blame] | 88 | BUG_ON(cycle_fd < 0); |
Hitoshi Mitake | 827f3b4 | 2009-11-18 00:20:09 +0900 | [diff] [blame] | 89 | } |
| 90 | |
Hitoshi Mitake | 17d7a11 | 2012-07-02 22:46:17 +0900 | [diff] [blame] | 91 | static u64 get_cycle(void) |
Hitoshi Mitake | 827f3b4 | 2009-11-18 00:20:09 +0900 | [diff] [blame] | 92 | { |
| 93 | int ret; |
| 94 | u64 clk; |
| 95 | |
Hitoshi Mitake | 17d7a11 | 2012-07-02 22:46:17 +0900 | [diff] [blame] | 96 | ret = read(cycle_fd, &clk, sizeof(u64)); |
Hitoshi Mitake | 827f3b4 | 2009-11-18 00:20:09 +0900 | [diff] [blame] | 97 | BUG_ON(ret != sizeof(u64)); |
| 98 | |
| 99 | return clk; |
| 100 | } |
| 101 | |
| 102 | static double timeval2double(struct timeval *ts) |
| 103 | { |
Ingo Molnar | 13839ec | 2015-10-19 10:04:17 +0200 | [diff] [blame] | 104 | return (double)ts->tv_sec + (double)ts->tv_usec / (double)1000000; |
Hitoshi Mitake | 827f3b4 | 2009-11-18 00:20:09 +0900 | [diff] [blame] | 105 | } |
| 106 | |
Ingo Molnar | 6db175c | 2015-10-19 10:04:21 +0200 | [diff] [blame^] | 107 | #define print_bps(x) do { \ |
| 108 | if (x < K) \ |
| 109 | printf(" %14lf B/Sec\n", x); \ |
| 110 | else if (x < K * K) \ |
| 111 | printf(" %14lfd KB/Sec\n", x / K); \ |
| 112 | else if (x < K * K * K) \ |
| 113 | printf(" %14lf MB/Sec\n", x / K / K); \ |
| 114 | else \ |
| 115 | printf(" %14lf GB/Sec\n", x / K / K / K); \ |
Hitoshi Mitake | 49ce8fc | 2010-11-25 16:04:52 +0900 | [diff] [blame] | 116 | } while (0) |
| 117 | |
Rabin Vincent | 308197b | 2014-12-02 16:50:39 +0100 | [diff] [blame] | 118 | struct bench_mem_info { |
| 119 | const struct routine *routines; |
Ingo Molnar | 6db175c | 2015-10-19 10:04:21 +0200 | [diff] [blame^] | 120 | u64 (*do_cycle)(const struct routine *r, size_t len); |
| 121 | double (*do_gettimeofday)(const struct routine *r, size_t len); |
Rabin Vincent | 308197b | 2014-12-02 16:50:39 +0100 | [diff] [blame] | 122 | const char *const *usage; |
| 123 | }; |
| 124 | |
Borislav Petkov | 515e23f | 2015-02-26 18:51:37 +0100 | [diff] [blame] | 125 | static void __bench_mem_routine(struct bench_mem_info *info, int r_idx, size_t len, double totallen) |
Hitoshi Mitake | 827f3b4 | 2009-11-18 00:20:09 +0900 | [diff] [blame] | 126 | { |
Borislav Petkov | 515e23f | 2015-02-26 18:51:37 +0100 | [diff] [blame] | 127 | const struct routine *r = &info->routines[r_idx]; |
Ingo Molnar | 6db175c | 2015-10-19 10:04:21 +0200 | [diff] [blame^] | 128 | double result_bps = 0.0; |
| 129 | u64 result_cycle = 0; |
Hitoshi Mitake | 49ce8fc | 2010-11-25 16:04:52 +0900 | [diff] [blame] | 130 | |
Borislav Petkov | dfecb95 | 2015-02-26 19:02:43 +0100 | [diff] [blame] | 131 | printf("Routine %s (%s)\n", r->name, r->desc); |
Hitoshi Mitake | 827f3b4 | 2009-11-18 00:20:09 +0900 | [diff] [blame] | 132 | |
Hitoshi Mitake | 49ce8fc | 2010-11-25 16:04:52 +0900 | [diff] [blame] | 133 | if (bench_format == BENCH_FORMAT_DEFAULT) |
| 134 | printf("# Copying %s Bytes ...\n\n", length_str); |
Hitoshi Mitake | 12eac0b | 2009-11-20 12:37:17 +0900 | [diff] [blame] | 135 | |
Ingo Molnar | 6db175c | 2015-10-19 10:04:21 +0200 | [diff] [blame^] | 136 | if (use_cycle) { |
| 137 | result_cycle = info->do_cycle(r, len); |
Hitoshi Mitake | 12eac0b | 2009-11-20 12:37:17 +0900 | [diff] [blame] | 138 | } else { |
Ingo Molnar | 6db175c | 2015-10-19 10:04:21 +0200 | [diff] [blame^] | 139 | result_bps = info->do_gettimeofday(r, len); |
Hitoshi Mitake | 827f3b4 | 2009-11-18 00:20:09 +0900 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | switch (bench_format) { |
| 143 | case BENCH_FORMAT_DEFAULT: |
Ingo Molnar | 6db175c | 2015-10-19 10:04:21 +0200 | [diff] [blame^] | 144 | if (use_cycle) { |
| 145 | printf(" %14lf Cycle/Byte\n", (double)result_cycle/totallen); |
Hitoshi Mitake | 49ce8fc | 2010-11-25 16:04:52 +0900 | [diff] [blame] | 146 | } else { |
Ingo Molnar | 6db175c | 2015-10-19 10:04:21 +0200 | [diff] [blame^] | 147 | print_bps(result_bps); |
| 148 | } |
| 149 | break; |
Hitoshi Mitake | 49ce8fc | 2010-11-25 16:04:52 +0900 | [diff] [blame] | 150 | |
Hitoshi Mitake | 827f3b4 | 2009-11-18 00:20:09 +0900 | [diff] [blame] | 151 | case BENCH_FORMAT_SIMPLE: |
Ingo Molnar | 6db175c | 2015-10-19 10:04:21 +0200 | [diff] [blame^] | 152 | if (use_cycle) { |
| 153 | printf("%lf\n", (double)result_cycle/totallen); |
Hitoshi Mitake | 49ce8fc | 2010-11-25 16:04:52 +0900 | [diff] [blame] | 154 | } else { |
Ingo Molnar | 6db175c | 2015-10-19 10:04:21 +0200 | [diff] [blame^] | 155 | printf("%lf\n", result_bps); |
Hitoshi Mitake | 49ce8fc | 2010-11-25 16:04:52 +0900 | [diff] [blame] | 156 | } |
Hitoshi Mitake | 827f3b4 | 2009-11-18 00:20:09 +0900 | [diff] [blame] | 157 | break; |
Ingo Molnar | 6db175c | 2015-10-19 10:04:21 +0200 | [diff] [blame^] | 158 | |
Hitoshi Mitake | 827f3b4 | 2009-11-18 00:20:09 +0900 | [diff] [blame] | 159 | default: |
Ingo Molnar | 6db175c | 2015-10-19 10:04:21 +0200 | [diff] [blame^] | 160 | BUG_ON(1); |
Hitoshi Mitake | 827f3b4 | 2009-11-18 00:20:09 +0900 | [diff] [blame] | 161 | break; |
| 162 | } |
Borislav Petkov | 515e23f | 2015-02-26 18:51:37 +0100 | [diff] [blame] | 163 | } |
| 164 | |
Ingo Molnar | 2946f59 | 2015-10-19 10:04:19 +0200 | [diff] [blame] | 165 | static int bench_mem_common(int argc, const char **argv, struct bench_mem_info *info) |
Borislav Petkov | 515e23f | 2015-02-26 18:51:37 +0100 | [diff] [blame] | 166 | { |
| 167 | int i; |
| 168 | size_t len; |
| 169 | double totallen; |
| 170 | |
Ingo Molnar | 13839ec | 2015-10-19 10:04:17 +0200 | [diff] [blame] | 171 | argc = parse_options(argc, argv, options, info->usage, 0); |
Borislav Petkov | 515e23f | 2015-02-26 18:51:37 +0100 | [diff] [blame] | 172 | |
Borislav Petkov | 515e23f | 2015-02-26 18:51:37 +0100 | [diff] [blame] | 173 | if (use_cycle) |
| 174 | init_cycle(); |
| 175 | |
| 176 | len = (size_t)perf_atoll((char *)length_str); |
| 177 | totallen = (double)len * iterations; |
| 178 | |
| 179 | if ((s64)len <= 0) { |
| 180 | fprintf(stderr, "Invalid length:%s\n", length_str); |
| 181 | return 1; |
| 182 | } |
| 183 | |
Borislav Petkov | dfecb95 | 2015-02-26 19:02:43 +0100 | [diff] [blame] | 184 | if (!strncmp(routine, "all", 3)) { |
| 185 | for (i = 0; info->routines[i].name; i++) |
| 186 | __bench_mem_routine(info, i, len, totallen); |
| 187 | return 0; |
| 188 | } |
| 189 | |
Borislav Petkov | 515e23f | 2015-02-26 18:51:37 +0100 | [diff] [blame] | 190 | for (i = 0; info->routines[i].name; i++) { |
| 191 | if (!strcmp(info->routines[i].name, routine)) |
| 192 | break; |
| 193 | } |
| 194 | if (!info->routines[i].name) { |
| 195 | printf("Unknown routine:%s\n", routine); |
| 196 | printf("Available routines...\n"); |
| 197 | for (i = 0; info->routines[i].name; i++) { |
| 198 | printf("\t%s ... %s\n", |
| 199 | info->routines[i].name, info->routines[i].desc); |
| 200 | } |
| 201 | return 1; |
| 202 | } |
| 203 | |
| 204 | __bench_mem_routine(info, i, len, totallen); |
Hitoshi Mitake | 827f3b4 | 2009-11-18 00:20:09 +0900 | [diff] [blame] | 205 | |
| 206 | return 0; |
| 207 | } |
Rabin Vincent | 308197b | 2014-12-02 16:50:39 +0100 | [diff] [blame] | 208 | |
| 209 | static void memcpy_alloc_mem(void **dst, void **src, size_t length) |
| 210 | { |
| 211 | *dst = zalloc(length); |
| 212 | if (!*dst) |
| 213 | die("memory allocation failed - maybe length is too large?\n"); |
| 214 | |
| 215 | *src = zalloc(length); |
| 216 | if (!*src) |
| 217 | die("memory allocation failed - maybe length is too large?\n"); |
Ingo Molnar | 13839ec | 2015-10-19 10:04:17 +0200 | [diff] [blame] | 218 | |
| 219 | /* Make sure to always prefault zero pages even if MMAP_THRESH is crossed: */ |
Rabin Vincent | 308197b | 2014-12-02 16:50:39 +0100 | [diff] [blame] | 220 | memset(*src, 0, length); |
| 221 | } |
| 222 | |
Ingo Molnar | 6db175c | 2015-10-19 10:04:21 +0200 | [diff] [blame^] | 223 | static u64 do_memcpy_cycle(const struct routine *r, size_t len) |
Rabin Vincent | 308197b | 2014-12-02 16:50:39 +0100 | [diff] [blame] | 224 | { |
| 225 | u64 cycle_start = 0ULL, cycle_end = 0ULL; |
| 226 | void *src = NULL, *dst = NULL; |
| 227 | memcpy_t fn = r->fn.memcpy; |
| 228 | int i; |
| 229 | |
Bruce Merry | e17fdae | 2015-01-15 11:20:22 +0200 | [diff] [blame] | 230 | memcpy_alloc_mem(&dst, &src, len); |
Rabin Vincent | 308197b | 2014-12-02 16:50:39 +0100 | [diff] [blame] | 231 | |
Ingo Molnar | 6db175c | 2015-10-19 10:04:21 +0200 | [diff] [blame^] | 232 | /* |
| 233 | * We prefault the freshly allocated memory range here, |
| 234 | * to not measure page fault overhead: |
| 235 | */ |
| 236 | fn(dst, src, len); |
Rabin Vincent | 308197b | 2014-12-02 16:50:39 +0100 | [diff] [blame] | 237 | |
| 238 | cycle_start = get_cycle(); |
| 239 | for (i = 0; i < iterations; ++i) |
| 240 | fn(dst, src, len); |
| 241 | cycle_end = get_cycle(); |
| 242 | |
| 243 | free(src); |
| 244 | free(dst); |
| 245 | return cycle_end - cycle_start; |
| 246 | } |
| 247 | |
Ingo Molnar | 6db175c | 2015-10-19 10:04:21 +0200 | [diff] [blame^] | 248 | static double do_memcpy_gettimeofday(const struct routine *r, size_t len) |
Rabin Vincent | 308197b | 2014-12-02 16:50:39 +0100 | [diff] [blame] | 249 | { |
| 250 | struct timeval tv_start, tv_end, tv_diff; |
| 251 | memcpy_t fn = r->fn.memcpy; |
| 252 | void *src = NULL, *dst = NULL; |
| 253 | int i; |
| 254 | |
Bruce Merry | e17fdae | 2015-01-15 11:20:22 +0200 | [diff] [blame] | 255 | memcpy_alloc_mem(&dst, &src, len); |
Rabin Vincent | 308197b | 2014-12-02 16:50:39 +0100 | [diff] [blame] | 256 | |
Ingo Molnar | 6db175c | 2015-10-19 10:04:21 +0200 | [diff] [blame^] | 257 | /* |
| 258 | * We prefault the freshly allocated memory range here, |
| 259 | * to not measure page fault overhead: |
| 260 | */ |
| 261 | fn(dst, src, len); |
Rabin Vincent | 308197b | 2014-12-02 16:50:39 +0100 | [diff] [blame] | 262 | |
| 263 | BUG_ON(gettimeofday(&tv_start, NULL)); |
| 264 | for (i = 0; i < iterations; ++i) |
| 265 | fn(dst, src, len); |
| 266 | BUG_ON(gettimeofday(&tv_end, NULL)); |
| 267 | |
| 268 | timersub(&tv_end, &tv_start, &tv_diff); |
| 269 | |
| 270 | free(src); |
| 271 | free(dst); |
Ingo Molnar | 6db175c | 2015-10-19 10:04:21 +0200 | [diff] [blame^] | 272 | |
Rabin Vincent | 1182f88 | 2014-12-02 16:50:41 +0100 | [diff] [blame] | 273 | return (double)(((double)len * iterations) / timeval2double(&tv_diff)); |
Rabin Vincent | 308197b | 2014-12-02 16:50:39 +0100 | [diff] [blame] | 274 | } |
| 275 | |
Ingo Molnar | 2946f59 | 2015-10-19 10:04:19 +0200 | [diff] [blame] | 276 | int bench_mem_memcpy(int argc, const char **argv, const char *prefix __maybe_unused) |
Rabin Vincent | 308197b | 2014-12-02 16:50:39 +0100 | [diff] [blame] | 277 | { |
| 278 | struct bench_mem_info info = { |
Ingo Molnar | 13839ec | 2015-10-19 10:04:17 +0200 | [diff] [blame] | 279 | .routines = memcpy_routines, |
| 280 | .do_cycle = do_memcpy_cycle, |
| 281 | .do_gettimeofday = do_memcpy_gettimeofday, |
| 282 | .usage = bench_mem_memcpy_usage, |
Rabin Vincent | 308197b | 2014-12-02 16:50:39 +0100 | [diff] [blame] | 283 | }; |
| 284 | |
Ingo Molnar | 2946f59 | 2015-10-19 10:04:19 +0200 | [diff] [blame] | 285 | return bench_mem_common(argc, argv, &info); |
Rabin Vincent | 308197b | 2014-12-02 16:50:39 +0100 | [diff] [blame] | 286 | } |
Rabin Vincent | 5bce1a5 | 2014-12-02 16:50:40 +0100 | [diff] [blame] | 287 | |
| 288 | static void memset_alloc_mem(void **dst, size_t length) |
| 289 | { |
| 290 | *dst = zalloc(length); |
| 291 | if (!*dst) |
| 292 | die("memory allocation failed - maybe length is too large?\n"); |
| 293 | } |
| 294 | |
Ingo Molnar | 6db175c | 2015-10-19 10:04:21 +0200 | [diff] [blame^] | 295 | static u64 do_memset_cycle(const struct routine *r, size_t len) |
Rabin Vincent | 5bce1a5 | 2014-12-02 16:50:40 +0100 | [diff] [blame] | 296 | { |
| 297 | u64 cycle_start = 0ULL, cycle_end = 0ULL; |
| 298 | memset_t fn = r->fn.memset; |
| 299 | void *dst = NULL; |
| 300 | int i; |
| 301 | |
| 302 | memset_alloc_mem(&dst, len); |
| 303 | |
Ingo Molnar | 6db175c | 2015-10-19 10:04:21 +0200 | [diff] [blame^] | 304 | /* |
| 305 | * We prefault the freshly allocated memory range here, |
| 306 | * to not measure page fault overhead: |
| 307 | */ |
| 308 | fn(dst, -1, len); |
Rabin Vincent | 5bce1a5 | 2014-12-02 16:50:40 +0100 | [diff] [blame] | 309 | |
| 310 | cycle_start = get_cycle(); |
| 311 | for (i = 0; i < iterations; ++i) |
| 312 | fn(dst, i, len); |
| 313 | cycle_end = get_cycle(); |
| 314 | |
| 315 | free(dst); |
| 316 | return cycle_end - cycle_start; |
| 317 | } |
| 318 | |
Ingo Molnar | 6db175c | 2015-10-19 10:04:21 +0200 | [diff] [blame^] | 319 | static double do_memset_gettimeofday(const struct routine *r, size_t len) |
Rabin Vincent | 5bce1a5 | 2014-12-02 16:50:40 +0100 | [diff] [blame] | 320 | { |
| 321 | struct timeval tv_start, tv_end, tv_diff; |
| 322 | memset_t fn = r->fn.memset; |
| 323 | void *dst = NULL; |
| 324 | int i; |
| 325 | |
| 326 | memset_alloc_mem(&dst, len); |
| 327 | |
Ingo Molnar | 6db175c | 2015-10-19 10:04:21 +0200 | [diff] [blame^] | 328 | /* |
| 329 | * We prefault the freshly allocated memory range here, |
| 330 | * to not measure page fault overhead: |
| 331 | */ |
| 332 | fn(dst, -1, len); |
Rabin Vincent | 5bce1a5 | 2014-12-02 16:50:40 +0100 | [diff] [blame] | 333 | |
| 334 | BUG_ON(gettimeofday(&tv_start, NULL)); |
| 335 | for (i = 0; i < iterations; ++i) |
| 336 | fn(dst, i, len); |
| 337 | BUG_ON(gettimeofday(&tv_end, NULL)); |
| 338 | |
| 339 | timersub(&tv_end, &tv_start, &tv_diff); |
| 340 | |
| 341 | free(dst); |
Rabin Vincent | 1182f88 | 2014-12-02 16:50:41 +0100 | [diff] [blame] | 342 | return (double)(((double)len * iterations) / timeval2double(&tv_diff)); |
Rabin Vincent | 5bce1a5 | 2014-12-02 16:50:40 +0100 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | static const char * const bench_mem_memset_usage[] = { |
| 346 | "perf bench mem memset <options>", |
| 347 | NULL |
| 348 | }; |
| 349 | |
| 350 | static const struct routine memset_routines[] = { |
Ingo Molnar | 13839ec | 2015-10-19 10:04:17 +0200 | [diff] [blame] | 351 | { .name = "default", |
| 352 | .desc = "Default memset() provided by glibc", |
| 353 | .fn.memset = memset }, |
| 354 | |
Rabin Vincent | 5bce1a5 | 2014-12-02 16:50:40 +0100 | [diff] [blame] | 355 | #ifdef HAVE_ARCH_X86_64_SUPPORT |
Ingo Molnar | 13839ec | 2015-10-19 10:04:17 +0200 | [diff] [blame] | 356 | # define MEMSET_FN(_fn, _name, _desc) { .name = _name, .desc = _desc, .fn.memset = _fn }, |
| 357 | # include "mem-memset-x86-64-asm-def.h" |
| 358 | # undef MEMSET_FN |
Rabin Vincent | 5bce1a5 | 2014-12-02 16:50:40 +0100 | [diff] [blame] | 359 | #endif |
| 360 | |
Ingo Molnar | 13839ec | 2015-10-19 10:04:17 +0200 | [diff] [blame] | 361 | { NULL, } |
Rabin Vincent | 5bce1a5 | 2014-12-02 16:50:40 +0100 | [diff] [blame] | 362 | }; |
| 363 | |
Ingo Molnar | 13839ec | 2015-10-19 10:04:17 +0200 | [diff] [blame] | 364 | int bench_mem_memset(int argc, const char **argv, const char *prefix __maybe_unused) |
Rabin Vincent | 5bce1a5 | 2014-12-02 16:50:40 +0100 | [diff] [blame] | 365 | { |
| 366 | struct bench_mem_info info = { |
Ingo Molnar | 13839ec | 2015-10-19 10:04:17 +0200 | [diff] [blame] | 367 | .routines = memset_routines, |
| 368 | .do_cycle = do_memset_cycle, |
| 369 | .do_gettimeofday = do_memset_gettimeofday, |
| 370 | .usage = bench_mem_memset_usage, |
Rabin Vincent | 5bce1a5 | 2014-12-02 16:50:40 +0100 | [diff] [blame] | 371 | }; |
| 372 | |
Ingo Molnar | 2946f59 | 2015-10-19 10:04:19 +0200 | [diff] [blame] | 373 | return bench_mem_common(argc, argv, &info); |
Rabin Vincent | 5bce1a5 | 2014-12-02 16:50:40 +0100 | [diff] [blame] | 374 | } |