blob: 59d4933eff4401b267392ae75c126bbeb9dacb83 [file] [log] [blame]
Jan Beulichbe3de802012-01-24 10:03:22 -02001/*
2 * mem-memset.c
3 *
4 * memset: Simple memory set in various ways
5 *
6 * Trivial clone of mem-memcpy.c.
7 */
8#include <ctype.h>
9
10#include "../perf.h"
11#include "../util/util.h"
12#include "../util/parse-options.h"
13#include "../util/header.h"
14#include "bench.h"
15#include "mem-memset-arch.h"
16
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
20#include <sys/time.h>
21#include <errno.h>
22
23#define K 1024
24
25static const char *length_str = "1MB";
26static const char *routine = "default";
Jan Beuliche3e877e2012-01-18 13:29:59 +000027static int iterations = 1;
Jan Beulichbe3de802012-01-24 10:03:22 -020028static bool use_clock;
29static int clock_fd;
30static bool only_prefault;
31static bool no_prefault;
32
33static const struct option options[] = {
34 OPT_STRING('l', "length", &length_str, "1MB",
35 "Specify length of memory to copy. "
36 "available unit: B, MB, GB (upper and lower)"),
37 OPT_STRING('r', "routine", &routine, "default",
38 "Specify routine to copy"),
Jan Beuliche3e877e2012-01-18 13:29:59 +000039 OPT_INTEGER('i', "iterations", &iterations,
40 "repeat memset() invocation this number of times"),
Jan Beulichbe3de802012-01-24 10:03:22 -020041 OPT_BOOLEAN('c', "clock", &use_clock,
42 "Use CPU clock for measuring"),
43 OPT_BOOLEAN('o', "only-prefault", &only_prefault,
44 "Show only the result with page faults before memset()"),
45 OPT_BOOLEAN('n', "no-prefault", &no_prefault,
46 "Show only the result without page faults before memset()"),
47 OPT_END()
48};
49
50typedef void *(*memset_t)(void *, int, size_t);
51
52struct routine {
53 const char *name;
54 const char *desc;
55 memset_t fn;
56};
57
58static const struct routine routines[] = {
59 { "default",
60 "Default memset() provided by glibc",
61 memset },
62#ifdef ARCH_X86_64
63
64#define MEMSET_FN(fn, name, desc) { name, desc, fn },
65#include "mem-memset-x86-64-asm-def.h"
66#undef MEMSET_FN
67
68#endif
69
70 { NULL,
71 NULL,
72 NULL }
73};
74
75static const char * const bench_mem_memset_usage[] = {
76 "perf bench mem memset <options>",
77 NULL
78};
79
80static struct perf_event_attr clock_attr = {
81 .type = PERF_TYPE_HARDWARE,
82 .config = PERF_COUNT_HW_CPU_CYCLES
83};
84
85static void init_clock(void)
86{
87 clock_fd = sys_perf_event_open(&clock_attr, getpid(), -1, -1, 0);
88
89 if (clock_fd < 0 && errno == ENOSYS)
90 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
91 else
92 BUG_ON(clock_fd < 0);
93}
94
95static u64 get_clock(void)
96{
97 int ret;
98 u64 clk;
99
100 ret = read(clock_fd, &clk, sizeof(u64));
101 BUG_ON(ret != sizeof(u64));
102
103 return clk;
104}
105
106static double timeval2double(struct timeval *ts)
107{
108 return (double)ts->tv_sec +
109 (double)ts->tv_usec / (double)1000000;
110}
111
112static void alloc_mem(void **dst, size_t length)
113{
114 *dst = zalloc(length);
115 if (!dst)
116 die("memory allocation failed - maybe length is too large?\n");
117}
118
119static u64 do_memset_clock(memset_t fn, size_t len, bool prefault)
120{
121 u64 clock_start = 0ULL, clock_end = 0ULL;
122 void *dst = NULL;
Jan Beuliche3e877e2012-01-18 13:29:59 +0000123 int i;
Jan Beulichbe3de802012-01-24 10:03:22 -0200124
125 alloc_mem(&dst, len);
126
127 if (prefault)
128 fn(dst, -1, len);
129
130 clock_start = get_clock();
Jan Beuliche3e877e2012-01-18 13:29:59 +0000131 for (i = 0; i < iterations; ++i)
132 fn(dst, i, len);
Jan Beulichbe3de802012-01-24 10:03:22 -0200133 clock_end = get_clock();
134
135 free(dst);
136 return clock_end - clock_start;
137}
138
139static double do_memset_gettimeofday(memset_t fn, size_t len, bool prefault)
140{
141 struct timeval tv_start, tv_end, tv_diff;
142 void *dst = NULL;
Jan Beuliche3e877e2012-01-18 13:29:59 +0000143 int i;
Jan Beulichbe3de802012-01-24 10:03:22 -0200144
145 alloc_mem(&dst, len);
146
147 if (prefault)
148 fn(dst, -1, len);
149
150 BUG_ON(gettimeofday(&tv_start, NULL));
Jan Beuliche3e877e2012-01-18 13:29:59 +0000151 for (i = 0; i < iterations; ++i)
152 fn(dst, i, len);
Jan Beulichbe3de802012-01-24 10:03:22 -0200153 BUG_ON(gettimeofday(&tv_end, NULL));
154
155 timersub(&tv_end, &tv_start, &tv_diff);
156
157 free(dst);
158 return (double)((double)len / timeval2double(&tv_diff));
159}
160
161#define pf (no_prefault ? 0 : 1)
162
163#define print_bps(x) do { \
164 if (x < K) \
165 printf(" %14lf B/Sec", x); \
166 else if (x < K * K) \
167 printf(" %14lfd KB/Sec", x / K); \
168 else if (x < K * K * K) \
169 printf(" %14lf MB/Sec", x / K / K); \
170 else \
171 printf(" %14lf GB/Sec", x / K / K / K); \
172 } while (0)
173
174int bench_mem_memset(int argc, const char **argv,
175 const char *prefix __used)
176{
177 int i;
178 size_t len;
179 double result_bps[2];
180 u64 result_clock[2];
181
182 argc = parse_options(argc, argv, options,
183 bench_mem_memset_usage, 0);
184
185 if (use_clock)
186 init_clock();
187
188 len = (size_t)perf_atoll((char *)length_str);
189
190 result_clock[0] = result_clock[1] = 0ULL;
191 result_bps[0] = result_bps[1] = 0.0;
192
193 if ((s64)len <= 0) {
194 fprintf(stderr, "Invalid length:%s\n", length_str);
195 return 1;
196 }
197
198 /* same to without specifying either of prefault and no-prefault */
199 if (only_prefault && no_prefault)
200 only_prefault = no_prefault = false;
201
202 for (i = 0; routines[i].name; i++) {
203 if (!strcmp(routines[i].name, routine))
204 break;
205 }
206 if (!routines[i].name) {
207 printf("Unknown routine:%s\n", routine);
208 printf("Available routines...\n");
209 for (i = 0; routines[i].name; i++) {
210 printf("\t%s ... %s\n",
211 routines[i].name, routines[i].desc);
212 }
213 return 1;
214 }
215
216 if (bench_format == BENCH_FORMAT_DEFAULT)
217 printf("# Copying %s Bytes ...\n\n", length_str);
218
219 if (!only_prefault && !no_prefault) {
220 /* show both of results */
221 if (use_clock) {
222 result_clock[0] =
223 do_memset_clock(routines[i].fn, len, false);
224 result_clock[1] =
225 do_memset_clock(routines[i].fn, len, true);
226 } else {
227 result_bps[0] =
228 do_memset_gettimeofday(routines[i].fn,
229 len, false);
230 result_bps[1] =
231 do_memset_gettimeofday(routines[i].fn,
232 len, true);
233 }
234 } else {
235 if (use_clock) {
236 result_clock[pf] =
237 do_memset_clock(routines[i].fn,
238 len, only_prefault);
239 } else {
240 result_bps[pf] =
241 do_memset_gettimeofday(routines[i].fn,
242 len, only_prefault);
243 }
244 }
245
246 switch (bench_format) {
247 case BENCH_FORMAT_DEFAULT:
248 if (!only_prefault && !no_prefault) {
249 if (use_clock) {
250 printf(" %14lf Clock/Byte\n",
251 (double)result_clock[0]
252 / (double)len);
253 printf(" %14lf Clock/Byte (with prefault)\n ",
254 (double)result_clock[1]
255 / (double)len);
256 } else {
257 print_bps(result_bps[0]);
258 printf("\n");
259 print_bps(result_bps[1]);
260 printf(" (with prefault)\n");
261 }
262 } else {
263 if (use_clock) {
264 printf(" %14lf Clock/Byte",
265 (double)result_clock[pf]
266 / (double)len);
267 } else
268 print_bps(result_bps[pf]);
269
270 printf("%s\n", only_prefault ? " (with prefault)" : "");
271 }
272 break;
273 case BENCH_FORMAT_SIMPLE:
274 if (!only_prefault && !no_prefault) {
275 if (use_clock) {
276 printf("%lf %lf\n",
277 (double)result_clock[0] / (double)len,
278 (double)result_clock[1] / (double)len);
279 } else {
280 printf("%lf %lf\n",
281 result_bps[0], result_bps[1]);
282 }
283 } else {
284 if (use_clock) {
285 printf("%lf\n", (double)result_clock[pf]
286 / (double)len);
287 } else
288 printf("%lf\n", result_bps[pf]);
289 }
290 break;
291 default:
292 /* reaching this means there's some disaster: */
293 die("unknown format: %d\n", bench_format);
294 break;
295 }
296
297 return 0;
298}