diff options
Diffstat (limited to 'tools/perf/bench')
-rw-r--r-- | tools/perf/bench/bench.h | 1 | ||||
-rw-r--r-- | tools/perf/bench/mem-memcpy-x86-64-asm-def.h | 8 | ||||
-rw-r--r-- | tools/perf/bench/mem-memcpy-x86-64-asm.S | 6 | ||||
-rw-r--r-- | tools/perf/bench/mem-memcpy.c | 12 | ||||
-rw-r--r-- | tools/perf/bench/mem-memset-arch.h | 12 | ||||
-rw-r--r-- | tools/perf/bench/mem-memset-x86-64-asm-def.h | 12 | ||||
-rw-r--r-- | tools/perf/bench/mem-memset-x86-64-asm.S | 13 | ||||
-rw-r--r-- | tools/perf/bench/mem-memset.c | 297 |
8 files changed, 357 insertions, 4 deletions
diff --git a/tools/perf/bench/bench.h b/tools/perf/bench/bench.h index f7781c6267c0..a09bece6dad2 100644 --- a/tools/perf/bench/bench.h +++ b/tools/perf/bench/bench.h | |||
@@ -4,6 +4,7 @@ | |||
4 | extern int bench_sched_messaging(int argc, const char **argv, const char *prefix); | 4 | extern int bench_sched_messaging(int argc, const char **argv, const char *prefix); |
5 | extern int bench_sched_pipe(int argc, const char **argv, const char *prefix); | 5 | extern int bench_sched_pipe(int argc, const char **argv, const char *prefix); |
6 | extern int bench_mem_memcpy(int argc, const char **argv, const char *prefix __used); | 6 | extern int bench_mem_memcpy(int argc, const char **argv, const char *prefix __used); |
7 | extern int bench_mem_memset(int argc, const char **argv, const char *prefix); | ||
7 | 8 | ||
8 | #define BENCH_FORMAT_DEFAULT_STR "default" | 9 | #define BENCH_FORMAT_DEFAULT_STR "default" |
9 | #define BENCH_FORMAT_DEFAULT 0 | 10 | #define BENCH_FORMAT_DEFAULT 0 |
diff --git a/tools/perf/bench/mem-memcpy-x86-64-asm-def.h b/tools/perf/bench/mem-memcpy-x86-64-asm-def.h index d588b87696fc..d66ab799b35f 100644 --- a/tools/perf/bench/mem-memcpy-x86-64-asm-def.h +++ b/tools/perf/bench/mem-memcpy-x86-64-asm-def.h | |||
@@ -2,3 +2,11 @@ | |||
2 | MEMCPY_FN(__memcpy, | 2 | MEMCPY_FN(__memcpy, |
3 | "x86-64-unrolled", | 3 | "x86-64-unrolled", |
4 | "unrolled memcpy() in arch/x86/lib/memcpy_64.S") | 4 | "unrolled memcpy() in arch/x86/lib/memcpy_64.S") |
5 | |||
6 | MEMCPY_FN(memcpy_c, | ||
7 | "x86-64-movsq", | ||
8 | "movsq-based memcpy() in arch/x86/lib/memcpy_64.S") | ||
9 | |||
10 | MEMCPY_FN(memcpy_c_e, | ||
11 | "x86-64-movsb", | ||
12 | "movsb-based memcpy() in arch/x86/lib/memcpy_64.S") | ||
diff --git a/tools/perf/bench/mem-memcpy-x86-64-asm.S b/tools/perf/bench/mem-memcpy-x86-64-asm.S index 185a96d66dd1..fcd9cf00600a 100644 --- a/tools/perf/bench/mem-memcpy-x86-64-asm.S +++ b/tools/perf/bench/mem-memcpy-x86-64-asm.S | |||
@@ -1,4 +1,8 @@ | |||
1 | 1 | #define memcpy MEMCPY /* don't hide glibc's memcpy() */ | |
2 | #define altinstr_replacement text | ||
3 | #define globl p2align 4; .globl | ||
4 | #define Lmemcpy_c globl memcpy_c; memcpy_c | ||
5 | #define Lmemcpy_c_e globl memcpy_c_e; memcpy_c_e | ||
2 | #include "../../../arch/x86/lib/memcpy_64.S" | 6 | #include "../../../arch/x86/lib/memcpy_64.S" |
3 | /* | 7 | /* |
4 | * We need to provide note.GNU-stack section, saying that we want | 8 | * We need to provide note.GNU-stack section, saying that we want |
diff --git a/tools/perf/bench/mem-memcpy.c b/tools/perf/bench/mem-memcpy.c index db82021f4b91..71557225bf92 100644 --- a/tools/perf/bench/mem-memcpy.c +++ b/tools/perf/bench/mem-memcpy.c | |||
@@ -5,7 +5,6 @@ | |||
5 | * | 5 | * |
6 | * Written by Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp> | 6 | * Written by Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp> |
7 | */ | 7 | */ |
8 | #include <ctype.h> | ||
9 | 8 | ||
10 | #include "../perf.h" | 9 | #include "../perf.h" |
11 | #include "../util/util.h" | 10 | #include "../util/util.h" |
@@ -24,6 +23,7 @@ | |||
24 | 23 | ||
25 | static const char *length_str = "1MB"; | 24 | static const char *length_str = "1MB"; |
26 | static const char *routine = "default"; | 25 | static const char *routine = "default"; |
26 | static int iterations = 1; | ||
27 | static bool use_clock; | 27 | static bool use_clock; |
28 | static int clock_fd; | 28 | static int clock_fd; |
29 | static bool only_prefault; | 29 | static bool only_prefault; |
@@ -35,6 +35,8 @@ static const struct option options[] = { | |||
35 | "available unit: B, MB, GB (upper and lower)"), | 35 | "available unit: B, MB, GB (upper and lower)"), |
36 | OPT_STRING('r', "routine", &routine, "default", | 36 | OPT_STRING('r', "routine", &routine, "default", |
37 | "Specify routine to copy"), | 37 | "Specify routine to copy"), |
38 | OPT_INTEGER('i', "iterations", &iterations, | ||
39 | "repeat memcpy() invocation this number of times"), | ||
38 | OPT_BOOLEAN('c', "clock", &use_clock, | 40 | OPT_BOOLEAN('c', "clock", &use_clock, |
39 | "Use CPU clock for measuring"), | 41 | "Use CPU clock for measuring"), |
40 | OPT_BOOLEAN('o', "only-prefault", &only_prefault, | 42 | OPT_BOOLEAN('o', "only-prefault", &only_prefault, |
@@ -121,6 +123,7 @@ static u64 do_memcpy_clock(memcpy_t fn, size_t len, bool prefault) | |||
121 | { | 123 | { |
122 | u64 clock_start = 0ULL, clock_end = 0ULL; | 124 | u64 clock_start = 0ULL, clock_end = 0ULL; |
123 | void *src = NULL, *dst = NULL; | 125 | void *src = NULL, *dst = NULL; |
126 | int i; | ||
124 | 127 | ||
125 | alloc_mem(&src, &dst, len); | 128 | alloc_mem(&src, &dst, len); |
126 | 129 | ||
@@ -128,7 +131,8 @@ static u64 do_memcpy_clock(memcpy_t fn, size_t len, bool prefault) | |||
128 | fn(dst, src, len); | 131 | fn(dst, src, len); |
129 | 132 | ||
130 | clock_start = get_clock(); | 133 | clock_start = get_clock(); |
131 | fn(dst, src, len); | 134 | for (i = 0; i < iterations; ++i) |
135 | fn(dst, src, len); | ||
132 | clock_end = get_clock(); | 136 | clock_end = get_clock(); |
133 | 137 | ||
134 | free(src); | 138 | free(src); |
@@ -140,6 +144,7 @@ static double do_memcpy_gettimeofday(memcpy_t fn, size_t len, bool prefault) | |||
140 | { | 144 | { |
141 | struct timeval tv_start, tv_end, tv_diff; | 145 | struct timeval tv_start, tv_end, tv_diff; |
142 | void *src = NULL, *dst = NULL; | 146 | void *src = NULL, *dst = NULL; |
147 | int i; | ||
143 | 148 | ||
144 | alloc_mem(&src, &dst, len); | 149 | alloc_mem(&src, &dst, len); |
145 | 150 | ||
@@ -147,7 +152,8 @@ static double do_memcpy_gettimeofday(memcpy_t fn, size_t len, bool prefault) | |||
147 | fn(dst, src, len); | 152 | fn(dst, src, len); |
148 | 153 | ||
149 | BUG_ON(gettimeofday(&tv_start, NULL)); | 154 | BUG_ON(gettimeofday(&tv_start, NULL)); |
150 | fn(dst, src, len); | 155 | for (i = 0; i < iterations; ++i) |
156 | fn(dst, src, len); | ||
151 | BUG_ON(gettimeofday(&tv_end, NULL)); | 157 | BUG_ON(gettimeofday(&tv_end, NULL)); |
152 | 158 | ||
153 | timersub(&tv_end, &tv_start, &tv_diff); | 159 | timersub(&tv_end, &tv_start, &tv_diff); |
diff --git a/tools/perf/bench/mem-memset-arch.h b/tools/perf/bench/mem-memset-arch.h new file mode 100644 index 000000000000..a040fa77665b --- /dev/null +++ b/tools/perf/bench/mem-memset-arch.h | |||
@@ -0,0 +1,12 @@ | |||
1 | |||
2 | #ifdef ARCH_X86_64 | ||
3 | |||
4 | #define MEMSET_FN(fn, name, desc) \ | ||
5 | extern void *fn(void *, int, size_t); | ||
6 | |||
7 | #include "mem-memset-x86-64-asm-def.h" | ||
8 | |||
9 | #undef MEMSET_FN | ||
10 | |||
11 | #endif | ||
12 | |||
diff --git a/tools/perf/bench/mem-memset-x86-64-asm-def.h b/tools/perf/bench/mem-memset-x86-64-asm-def.h new file mode 100644 index 000000000000..a71dff97c1f5 --- /dev/null +++ b/tools/perf/bench/mem-memset-x86-64-asm-def.h | |||
@@ -0,0 +1,12 @@ | |||
1 | |||
2 | MEMSET_FN(__memset, | ||
3 | "x86-64-unrolled", | ||
4 | "unrolled memset() in arch/x86/lib/memset_64.S") | ||
5 | |||
6 | MEMSET_FN(memset_c, | ||
7 | "x86-64-stosq", | ||
8 | "movsq-based memset() in arch/x86/lib/memset_64.S") | ||
9 | |||
10 | MEMSET_FN(memset_c_e, | ||
11 | "x86-64-stosb", | ||
12 | "movsb-based memset() in arch/x86/lib/memset_64.S") | ||
diff --git a/tools/perf/bench/mem-memset-x86-64-asm.S b/tools/perf/bench/mem-memset-x86-64-asm.S new file mode 100644 index 000000000000..9e5af89ed13a --- /dev/null +++ b/tools/perf/bench/mem-memset-x86-64-asm.S | |||
@@ -0,0 +1,13 @@ | |||
1 | #define memset MEMSET /* don't hide glibc's memset() */ | ||
2 | #define altinstr_replacement text | ||
3 | #define globl p2align 4; .globl | ||
4 | #define Lmemset_c globl memset_c; memset_c | ||
5 | #define Lmemset_c_e globl memset_c_e; memset_c_e | ||
6 | #include "../../../arch/x86/lib/memset_64.S" | ||
7 | |||
8 | /* | ||
9 | * We need to provide note.GNU-stack section, saying that we want | ||
10 | * NOT executable stack. Otherwise the final linking will assume that | ||
11 | * the ELF stack should not be restricted at all and set it RWX. | ||
12 | */ | ||
13 | .section .note.GNU-stack,"",@progbits | ||
diff --git a/tools/perf/bench/mem-memset.c b/tools/perf/bench/mem-memset.c new file mode 100644 index 000000000000..e9079185bd72 --- /dev/null +++ b/tools/perf/bench/mem-memset.c | |||
@@ -0,0 +1,297 @@ | |||
1 | /* | ||
2 | * mem-memset.c | ||
3 | * | ||
4 | * memset: Simple memory set in various ways | ||
5 | * | ||
6 | * Trivial clone of mem-memcpy.c. | ||
7 | */ | ||
8 | |||
9 | #include "../perf.h" | ||
10 | #include "../util/util.h" | ||
11 | #include "../util/parse-options.h" | ||
12 | #include "../util/header.h" | ||
13 | #include "bench.h" | ||
14 | #include "mem-memset-arch.h" | ||
15 | |||
16 | #include <stdio.h> | ||
17 | #include <stdlib.h> | ||
18 | #include <string.h> | ||
19 | #include <sys/time.h> | ||
20 | #include <errno.h> | ||
21 | |||
22 | #define K 1024 | ||
23 | |||
24 | static const char *length_str = "1MB"; | ||
25 | static const char *routine = "default"; | ||
26 | static int iterations = 1; | ||
27 | static bool use_clock; | ||
28 | static int clock_fd; | ||
29 | static bool only_prefault; | ||
30 | static bool no_prefault; | ||
31 | |||
32 | static const struct option options[] = { | ||
33 | OPT_STRING('l', "length", &length_str, "1MB", | ||
34 | "Specify length of memory to copy. " | ||
35 | "available unit: B, MB, GB (upper and lower)"), | ||
36 | OPT_STRING('r', "routine", &routine, "default", | ||
37 | "Specify routine to copy"), | ||
38 | OPT_INTEGER('i', "iterations", &iterations, | ||
39 | "repeat memset() invocation this number of times"), | ||
40 | OPT_BOOLEAN('c', "clock", &use_clock, | ||
41 | "Use CPU clock for measuring"), | ||
42 | OPT_BOOLEAN('o', "only-prefault", &only_prefault, | ||
43 | "Show only the result with page faults before memset()"), | ||
44 | OPT_BOOLEAN('n', "no-prefault", &no_prefault, | ||
45 | "Show only the result without page faults before memset()"), | ||
46 | OPT_END() | ||
47 | }; | ||
48 | |||
49 | typedef void *(*memset_t)(void *, int, size_t); | ||
50 | |||
51 | struct routine { | ||
52 | const char *name; | ||
53 | const char *desc; | ||
54 | memset_t fn; | ||
55 | }; | ||
56 | |||
57 | static const struct routine routines[] = { | ||
58 | { "default", | ||
59 | "Default memset() provided by glibc", | ||
60 | memset }, | ||
61 | #ifdef ARCH_X86_64 | ||
62 | |||
63 | #define MEMSET_FN(fn, name, desc) { name, desc, fn }, | ||
64 | #include "mem-memset-x86-64-asm-def.h" | ||
65 | #undef MEMSET_FN | ||
66 | |||
67 | #endif | ||
68 | |||
69 | { NULL, | ||
70 | NULL, | ||
71 | NULL } | ||
72 | }; | ||
73 | |||
74 | static const char * const bench_mem_memset_usage[] = { | ||
75 | "perf bench mem memset <options>", | ||
76 | NULL | ||
77 | }; | ||
78 | |||
79 | static struct perf_event_attr clock_attr = { | ||
80 | .type = PERF_TYPE_HARDWARE, | ||
81 | .config = PERF_COUNT_HW_CPU_CYCLES | ||
82 | }; | ||
83 | |||
84 | static void init_clock(void) | ||
85 | { | ||
86 | clock_fd = sys_perf_event_open(&clock_attr, getpid(), -1, -1, 0); | ||
87 | |||
88 | if (clock_fd < 0 && errno == ENOSYS) | ||
89 | die("No CONFIG_PERF_EVENTS=y kernel support configured?\n"); | ||
90 | else | ||
91 | BUG_ON(clock_fd < 0); | ||
92 | } | ||
93 | |||
94 | static u64 get_clock(void) | ||
95 | { | ||
96 | int ret; | ||
97 | u64 clk; | ||
98 | |||
99 | ret = read(clock_fd, &clk, sizeof(u64)); | ||
100 | BUG_ON(ret != sizeof(u64)); | ||
101 | |||
102 | return clk; | ||
103 | } | ||
104 | |||
105 | static double timeval2double(struct timeval *ts) | ||
106 | { | ||
107 | return (double)ts->tv_sec + | ||
108 | (double)ts->tv_usec / (double)1000000; | ||
109 | } | ||
110 | |||
111 | static void alloc_mem(void **dst, size_t length) | ||
112 | { | ||
113 | *dst = zalloc(length); | ||
114 | if (!dst) | ||
115 | die("memory allocation failed - maybe length is too large?\n"); | ||
116 | } | ||
117 | |||
118 | static u64 do_memset_clock(memset_t fn, size_t len, bool prefault) | ||
119 | { | ||
120 | u64 clock_start = 0ULL, clock_end = 0ULL; | ||
121 | void *dst = NULL; | ||
122 | int i; | ||
123 | |||
124 | alloc_mem(&dst, len); | ||
125 | |||
126 | if (prefault) | ||
127 | fn(dst, -1, len); | ||
128 | |||
129 | clock_start = get_clock(); | ||
130 | for (i = 0; i < iterations; ++i) | ||
131 | fn(dst, i, len); | ||
132 | clock_end = get_clock(); | ||
133 | |||
134 | free(dst); | ||
135 | return clock_end - clock_start; | ||
136 | } | ||
137 | |||
138 | static double do_memset_gettimeofday(memset_t fn, size_t len, bool prefault) | ||
139 | { | ||
140 | struct timeval tv_start, tv_end, tv_diff; | ||
141 | void *dst = NULL; | ||
142 | int i; | ||
143 | |||
144 | alloc_mem(&dst, len); | ||
145 | |||
146 | if (prefault) | ||
147 | fn(dst, -1, len); | ||
148 | |||
149 | BUG_ON(gettimeofday(&tv_start, NULL)); | ||
150 | for (i = 0; i < iterations; ++i) | ||
151 | fn(dst, i, len); | ||
152 | BUG_ON(gettimeofday(&tv_end, NULL)); | ||
153 | |||
154 | timersub(&tv_end, &tv_start, &tv_diff); | ||
155 | |||
156 | free(dst); | ||
157 | return (double)((double)len / timeval2double(&tv_diff)); | ||
158 | } | ||
159 | |||
160 | #define pf (no_prefault ? 0 : 1) | ||
161 | |||
162 | #define print_bps(x) do { \ | ||
163 | if (x < K) \ | ||
164 | printf(" %14lf B/Sec", x); \ | ||
165 | else if (x < K * K) \ | ||
166 | printf(" %14lfd KB/Sec", x / K); \ | ||
167 | else if (x < K * K * K) \ | ||
168 | printf(" %14lf MB/Sec", x / K / K); \ | ||
169 | else \ | ||
170 | printf(" %14lf GB/Sec", x / K / K / K); \ | ||
171 | } while (0) | ||
172 | |||
173 | int bench_mem_memset(int argc, const char **argv, | ||
174 | const char *prefix __used) | ||
175 | { | ||
176 | int i; | ||
177 | size_t len; | ||
178 | double result_bps[2]; | ||
179 | u64 result_clock[2]; | ||
180 | |||
181 | argc = parse_options(argc, argv, options, | ||
182 | bench_mem_memset_usage, 0); | ||
183 | |||
184 | if (use_clock) | ||
185 | init_clock(); | ||
186 | |||
187 | len = (size_t)perf_atoll((char *)length_str); | ||
188 | |||
189 | result_clock[0] = result_clock[1] = 0ULL; | ||
190 | result_bps[0] = result_bps[1] = 0.0; | ||
191 | |||
192 | if ((s64)len <= 0) { | ||
193 | fprintf(stderr, "Invalid length:%s\n", length_str); | ||
194 | return 1; | ||
195 | } | ||
196 | |||
197 | /* same to without specifying either of prefault and no-prefault */ | ||
198 | if (only_prefault && no_prefault) | ||
199 | only_prefault = no_prefault = false; | ||
200 | |||
201 | for (i = 0; routines[i].name; i++) { | ||
202 | if (!strcmp(routines[i].name, routine)) | ||
203 | break; | ||
204 | } | ||
205 | if (!routines[i].name) { | ||
206 | printf("Unknown routine:%s\n", routine); | ||
207 | printf("Available routines...\n"); | ||
208 | for (i = 0; routines[i].name; i++) { | ||
209 | printf("\t%s ... %s\n", | ||
210 | routines[i].name, routines[i].desc); | ||
211 | } | ||
212 | return 1; | ||
213 | } | ||
214 | |||
215 | if (bench_format == BENCH_FORMAT_DEFAULT) | ||
216 | printf("# Copying %s Bytes ...\n\n", length_str); | ||
217 | |||
218 | if (!only_prefault && !no_prefault) { | ||
219 | /* show both of results */ | ||
220 | if (use_clock) { | ||
221 | result_clock[0] = | ||
222 | do_memset_clock(routines[i].fn, len, false); | ||
223 | result_clock[1] = | ||
224 | do_memset_clock(routines[i].fn, len, true); | ||
225 | } else { | ||
226 | result_bps[0] = | ||
227 | do_memset_gettimeofday(routines[i].fn, | ||
228 | len, false); | ||
229 | result_bps[1] = | ||
230 | do_memset_gettimeofday(routines[i].fn, | ||
231 | len, true); | ||
232 | } | ||
233 | } else { | ||
234 | if (use_clock) { | ||
235 | result_clock[pf] = | ||
236 | do_memset_clock(routines[i].fn, | ||
237 | len, only_prefault); | ||
238 | } else { | ||
239 | result_bps[pf] = | ||
240 | do_memset_gettimeofday(routines[i].fn, | ||
241 | len, only_prefault); | ||
242 | } | ||
243 | } | ||
244 | |||
245 | switch (bench_format) { | ||
246 | case BENCH_FORMAT_DEFAULT: | ||
247 | if (!only_prefault && !no_prefault) { | ||
248 | if (use_clock) { | ||
249 | printf(" %14lf Clock/Byte\n", | ||
250 | (double)result_clock[0] | ||
251 | / (double)len); | ||
252 | printf(" %14lf Clock/Byte (with prefault)\n ", | ||
253 | (double)result_clock[1] | ||
254 | / (double)len); | ||
255 | } else { | ||
256 | print_bps(result_bps[0]); | ||
257 | printf("\n"); | ||
258 | print_bps(result_bps[1]); | ||
259 | printf(" (with prefault)\n"); | ||
260 | } | ||
261 | } else { | ||
262 | if (use_clock) { | ||
263 | printf(" %14lf Clock/Byte", | ||
264 | (double)result_clock[pf] | ||
265 | / (double)len); | ||
266 | } else | ||
267 | print_bps(result_bps[pf]); | ||
268 | |||
269 | printf("%s\n", only_prefault ? " (with prefault)" : ""); | ||
270 | } | ||
271 | break; | ||
272 | case BENCH_FORMAT_SIMPLE: | ||
273 | if (!only_prefault && !no_prefault) { | ||
274 | if (use_clock) { | ||
275 | printf("%lf %lf\n", | ||
276 | (double)result_clock[0] / (double)len, | ||
277 | (double)result_clock[1] / (double)len); | ||
278 | } else { | ||
279 | printf("%lf %lf\n", | ||
280 | result_bps[0], result_bps[1]); | ||
281 | } | ||
282 | } else { | ||
283 | if (use_clock) { | ||
284 | printf("%lf\n", (double)result_clock[pf] | ||
285 | / (double)len); | ||
286 | } else | ||
287 | printf("%lf\n", result_bps[pf]); | ||
288 | } | ||
289 | break; | ||
290 | default: | ||
291 | /* reaching this means there's some disaster: */ | ||
292 | die("unknown format: %d\n", bench_format); | ||
293 | break; | ||
294 | } | ||
295 | |||
296 | return 0; | ||
297 | } | ||