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