4 * memset: Simple memory set in various ways
6 * Trivial clone of mem-memcpy.c.
10 #include "../util/util.h"
11 #include "../util/parse-options.h"
12 #include "../util/header.h"
14 #include "mem-memset-arch.h"
24 static const char *length_str
= "1MB";
25 static const char *routine
= "default";
26 static int iterations
= 1;
27 static bool use_cycle
;
29 static bool only_prefault
;
30 static bool no_prefault
;
32 static const struct option options
[] = {
33 OPT_STRING('l', "length", &length_str
, "1MB",
34 "Specify length of memory to set. "
35 "Available units: B, KB, MB, GB and TB (upper and lower)"),
36 OPT_STRING('r', "routine", &routine
, "default",
37 "Specify routine to set"),
38 OPT_INTEGER('i', "iterations", &iterations
,
39 "repeat memset() invocation this number of times"),
40 OPT_BOOLEAN('c', "cycle", &use_cycle
,
41 "Use cycles event instead of gettimeofday() 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()"),
49 typedef void *(*memset_t
)(void *, int, size_t);
57 static const struct routine routines
[] = {
59 "Default memset() provided by glibc",
63 #define MEMSET_FN(fn, name, desc) { name, desc, fn },
64 #include "mem-memset-x86-64-asm-def.h"
74 static const char * const bench_mem_memset_usage
[] = {
75 "perf bench mem memset <options>",
79 static struct perf_event_attr cycle_attr
= {
80 .type
= PERF_TYPE_HARDWARE
,
81 .config
= PERF_COUNT_HW_CPU_CYCLES
84 static void init_cycle(void)
86 cycle_fd
= sys_perf_event_open(&cycle_attr
, getpid(), -1, -1, 0);
88 if (cycle_fd
< 0 && errno
== ENOSYS
)
89 die("No CONFIG_PERF_EVENTS=y kernel support configured?\n");
94 static u64
get_cycle(void)
99 ret
= read(cycle_fd
, &clk
, sizeof(u64
));
100 BUG_ON(ret
!= sizeof(u64
));
105 static double timeval2double(struct timeval
*ts
)
107 return (double)ts
->tv_sec
+
108 (double)ts
->tv_usec
/ (double)1000000;
111 static void alloc_mem(void **dst
, size_t length
)
113 *dst
= zalloc(length
);
115 die("memory allocation failed - maybe length is too large?\n");
118 static u64
do_memset_cycle(memset_t fn
, size_t len
, bool prefault
)
120 u64 cycle_start
= 0ULL, cycle_end
= 0ULL;
124 alloc_mem(&dst
, len
);
129 cycle_start
= get_cycle();
130 for (i
= 0; i
< iterations
; ++i
)
132 cycle_end
= get_cycle();
135 return cycle_end
- cycle_start
;
138 static double do_memset_gettimeofday(memset_t fn
, size_t len
, bool prefault
)
140 struct timeval tv_start
, tv_end
, tv_diff
;
144 alloc_mem(&dst
, len
);
149 BUG_ON(gettimeofday(&tv_start
, NULL
));
150 for (i
= 0; i
< iterations
; ++i
)
152 BUG_ON(gettimeofday(&tv_end
, NULL
));
154 timersub(&tv_end
, &tv_start
, &tv_diff
);
157 return (double)((double)len
/ timeval2double(&tv_diff
));
160 #define pf (no_prefault ? 0 : 1)
162 #define print_bps(x) do { \
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); \
170 printf(" %14lf GB/Sec", x / K / K / K); \
173 int bench_mem_memset(int argc
, const char **argv
,
174 const char *prefix __maybe_unused
)
178 double result_bps
[2];
181 argc
= parse_options(argc
, argv
, options
,
182 bench_mem_memset_usage
, 0);
187 len
= (size_t)perf_atoll((char *)length_str
);
189 result_cycle
[0] = result_cycle
[1] = 0ULL;
190 result_bps
[0] = result_bps
[1] = 0.0;
193 fprintf(stderr
, "Invalid length:%s\n", length_str
);
197 /* same to without specifying either of prefault and no-prefault */
198 if (only_prefault
&& no_prefault
)
199 only_prefault
= no_prefault
= false;
201 for (i
= 0; routines
[i
].name
; i
++) {
202 if (!strcmp(routines
[i
].name
, routine
))
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
);
215 if (bench_format
== BENCH_FORMAT_DEFAULT
)
216 printf("# Copying %s Bytes ...\n\n", length_str
);
218 if (!only_prefault
&& !no_prefault
) {
219 /* show both of results */
222 do_memset_cycle(routines
[i
].fn
, len
, false);
224 do_memset_cycle(routines
[i
].fn
, len
, true);
227 do_memset_gettimeofday(routines
[i
].fn
,
230 do_memset_gettimeofday(routines
[i
].fn
,
236 do_memset_cycle(routines
[i
].fn
,
240 do_memset_gettimeofday(routines
[i
].fn
,
245 switch (bench_format
) {
246 case BENCH_FORMAT_DEFAULT
:
247 if (!only_prefault
&& !no_prefault
) {
249 printf(" %14lf Cycle/Byte\n",
250 (double)result_cycle
[0]
252 printf(" %14lf Cycle/Byte (with prefault)\n ",
253 (double)result_cycle
[1]
256 print_bps(result_bps
[0]);
258 print_bps(result_bps
[1]);
259 printf(" (with prefault)\n");
263 printf(" %14lf Cycle/Byte",
264 (double)result_cycle
[pf
]
267 print_bps(result_bps
[pf
]);
269 printf("%s\n", only_prefault
? " (with prefault)" : "");
272 case BENCH_FORMAT_SIMPLE
:
273 if (!only_prefault
&& !no_prefault
) {
276 (double)result_cycle
[0] / (double)len
,
277 (double)result_cycle
[1] / (double)len
);
280 result_bps
[0], result_bps
[1]);
284 printf("%lf\n", (double)result_cycle
[pf
]
287 printf("%lf\n", result_bps
[pf
]);
291 /* reaching this means there's some disaster: */
292 die("unknown format: %d\n", bench_format
);