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