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