Skip to content

Commit dbf5dad

Browse files
terminusacmel
authored andcommitted
perf bench mem: Move mem op parameters into a structure
Move benchmark function parameters in struct bench_params. Reviewed-by: Namhyung Kim <namhyung@kernel.org> Signed-off-by: Ankur Arora <ankur.a.arora@oracle.com> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Andy Lutomirski <luto@kernel.org> Cc: Boris Ostrovsky <boris.ostrovsky@oracle.com> Cc: Borislav Petkov <bp@alien8.de> Cc: Dave Hansen <dave.hansen@linux.intel.com> Cc: David Hildenbrand <david@redhat.com> Cc: H. Peter Anvin <hpa@zytor.com> Cc: Ingo Molnar <mingo@redhat.com> Cc: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com> Cc: Mateusz Guzik <mjguzik@gmail.com> Cc: Matthew Wilcox <willy@infradead.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Raghavendra K T <raghavendra.kt@amd.com> Cc: Thomas Gleixner <tglx@linutronix.de> Link: https://lore.kernel.org/r/20250917152418.4077386-4-ankur.a.arora@oracle.com Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent 07e2572 commit dbf5dad

File tree

1 file changed

+34
-28
lines changed

1 file changed

+34
-28
lines changed

tools/perf/bench/mem-functions.c

Lines changed: 34 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
static const char *size_str = "1MB";
3232
static const char *function_str = "all";
33-
static int nr_loops = 1;
33+
static unsigned int nr_loops = 1;
3434
static bool use_cycles;
3535
static int cycles_fd;
3636

@@ -42,7 +42,7 @@ static const struct option options[] = {
4242
OPT_STRING('f', "function", &function_str, "all",
4343
"Specify the function to run, \"all\" runs all available functions, \"help\" lists them"),
4444

45-
OPT_INTEGER('l', "nr_loops", &nr_loops,
45+
OPT_UINTEGER('l', "nr_loops", &nr_loops,
4646
"Specify the number of loops to run. (default: 1)"),
4747

4848
OPT_BOOLEAN('c', "cycles", &use_cycles,
@@ -56,6 +56,12 @@ union bench_clock {
5656
struct timeval tv;
5757
};
5858

59+
struct bench_params {
60+
size_t size;
61+
size_t size_total;
62+
unsigned int nr_loops;
63+
};
64+
5965
typedef void *(*memcpy_t)(void *, const void *, size_t);
6066
typedef void *(*memset_t)(void *, int, size_t);
6167

@@ -134,49 +140,51 @@ static double timeval2double(struct timeval *ts)
134140

135141
struct bench_mem_info {
136142
const struct function *functions;
137-
union bench_clock (*do_op)(const struct function *r, size_t size, void *src, void *dst);
143+
union bench_clock (*do_op)(const struct function *r, struct bench_params *p,
144+
void *src, void *dst);
138145
const char *const *usage;
139146
bool alloc_src;
140147
};
141148

142-
static void __bench_mem_function(struct bench_mem_info *info, int r_idx, size_t size, size_t size_total)
149+
static void __bench_mem_function(struct bench_mem_info *info, struct bench_params *p,
150+
int r_idx)
143151
{
144152
const struct function *r = &info->functions[r_idx];
145153
double result_bps = 0.0;
146154
union bench_clock rt = { 0 };
147-
void *src = NULL, *dst = zalloc(size);
155+
void *src = NULL, *dst = zalloc(p->size);
148156

149157
printf("# function '%s' (%s)\n", r->name, r->desc);
150158

151159
if (dst == NULL)
152160
goto out_alloc_failed;
153161

154162
if (info->alloc_src) {
155-
src = zalloc(size);
163+
src = zalloc(p->size);
156164
if (src == NULL)
157165
goto out_alloc_failed;
158166
}
159167

160168
if (bench_format == BENCH_FORMAT_DEFAULT)
161169
printf("# Copying %s bytes ...\n\n", size_str);
162170

163-
rt = info->do_op(r, size, src, dst);
171+
rt = info->do_op(r, p, src, dst);
164172

165173
switch (bench_format) {
166174
case BENCH_FORMAT_DEFAULT:
167175
if (use_cycles) {
168-
printf(" %14lf cycles/byte\n", (double)rt.cycles/(double)size_total);
176+
printf(" %14lf cycles/byte\n", (double)rt.cycles/(double)p->size_total);
169177
} else {
170-
result_bps = (double)size_total/timeval2double(&rt.tv);
178+
result_bps = (double)p->size_total/timeval2double(&rt.tv);
171179
print_bps(result_bps);
172180
}
173181
break;
174182

175183
case BENCH_FORMAT_SIMPLE:
176184
if (use_cycles) {
177-
printf("%lf\n", (double)rt.cycles/(double)size_total);
185+
printf("%lf\n", (double)rt.cycles/(double)p->size_total);
178186
} else {
179-
result_bps = (double)size_total/timeval2double(&rt.tv);
187+
result_bps = (double)p->size_total/timeval2double(&rt.tv);
180188
printf("%lf\n", result_bps);
181189
}
182190
break;
@@ -198,8 +206,7 @@ static void __bench_mem_function(struct bench_mem_info *info, int r_idx, size_t
198206
static int bench_mem_common(int argc, const char **argv, struct bench_mem_info *info)
199207
{
200208
int i;
201-
size_t size;
202-
size_t size_total;
209+
struct bench_params p = { 0 };
203210

204211
argc = parse_options(argc, argv, options, info->usage, 0);
205212

@@ -211,17 +218,18 @@ static int bench_mem_common(int argc, const char **argv, struct bench_mem_info *
211218
}
212219
}
213220

214-
size = (size_t)perf_atoll((char *)size_str);
215-
size_total = size * nr_loops;
221+
p.nr_loops = nr_loops;
222+
p.size = (size_t)perf_atoll((char *)size_str);
216223

217-
if ((s64)size <= 0) {
224+
if ((s64)p.size <= 0) {
218225
fprintf(stderr, "Invalid size:%s\n", size_str);
219226
return 1;
220227
}
228+
p.size_total = p.size * p.nr_loops;
221229

222230
if (!strncmp(function_str, "all", 3)) {
223231
for (i = 0; info->functions[i].name; i++)
224-
__bench_mem_function(info, i, size, size_total);
232+
__bench_mem_function(info, &p, i);
225233
return 0;
226234
}
227235

@@ -240,7 +248,7 @@ static int bench_mem_common(int argc, const char **argv, struct bench_mem_info *
240248
return 1;
241249
}
242250

243-
__bench_mem_function(info, i, size, size_total);
251+
__bench_mem_function(info, &p, i);
244252

245253
return 0;
246254
}
@@ -257,18 +265,17 @@ static void memcpy_prefault(memcpy_t fn, size_t size, void *src, void *dst)
257265
fn(dst, src, size);
258266
}
259267

260-
static union bench_clock do_memcpy(const struct function *r, size_t size,
268+
static union bench_clock do_memcpy(const struct function *r, struct bench_params *p,
261269
void *src, void *dst)
262270
{
263271
union bench_clock start, end;
264272
memcpy_t fn = r->fn.memcpy;
265-
int i;
266273

267-
memcpy_prefault(fn, size, src, dst);
274+
memcpy_prefault(fn, p->size, src, dst);
268275

269276
clock_get(&start);
270-
for (i = 0; i < nr_loops; ++i)
271-
fn(dst, src, size);
277+
for (unsigned int i = 0; i < p->nr_loops; ++i)
278+
fn(dst, src, p->size);
272279
clock_get(&end);
273280

274281
return clock_diff(&start, &end);
@@ -305,22 +312,21 @@ int bench_mem_memcpy(int argc, const char **argv)
305312
return bench_mem_common(argc, argv, &info);
306313
}
307314

308-
static union bench_clock do_memset(const struct function *r, size_t size,
315+
static union bench_clock do_memset(const struct function *r, struct bench_params *p,
309316
void *src __maybe_unused, void *dst)
310317
{
311318
union bench_clock start, end;
312319
memset_t fn = r->fn.memset;
313-
int i;
314320

315321
/*
316322
* We prefault the freshly allocated memory range here,
317323
* to not measure page fault overhead:
318324
*/
319-
fn(dst, -1, size);
325+
fn(dst, -1, p->size);
320326

321327
clock_get(&start);
322-
for (i = 0; i < nr_loops; ++i)
323-
fn(dst, i, size);
328+
for (unsigned int i = 0; i < p->nr_loops; ++i)
329+
fn(dst, i, p->size);
324330
clock_get(&end);
325331

326332
return clock_diff(&start, &end);

0 commit comments

Comments
 (0)