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