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