Skip to content

Commit 7b6837e

Browse files
terminusacmel
authored andcommitted
perf bench mem: Allow mapping of hugepages
Page sizes that can be selected: 4KB, 2MB, 1GB. Both the reservation and node from which hugepages are allocated from are expected to be addressed by the user. An example of page-size selection: $ perf bench mem memset -s 4gb -p 2mb # Running 'mem/memset' benchmark: # function 'default' (Default memset() provided by glibc) # Copying 4gb bytes ... 14.919194 GB/sec # function 'x86-64-unrolled' (unrolled memset() in arch/x86/lib/memset_64.S) # Copying 4gb bytes ... 11.514503 GB/sec # function 'x86-64-stosq' (movsq-based memset() in arch/x86/lib/memset_64.S) # Copying 4gb bytes ... 12.600568 GB/sec Signed-off-by: Ankur Arora <ankur.a.arora@oracle.com> Tested-by: Arnaldo Carvalho de Melo <acme@redhat.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: Namhyung Kim <namhyung@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Raghavendra K T <raghavendra.kt@amd.com> Cc: Thomas Gleixner <tglx@linutronix.de> Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
1 parent fe0f321 commit 7b6837e

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

tools/perf/Documentation/perf-bench.txt

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,11 +177,16 @@ Suite for evaluating performance of simple memory copy in various ways.
177177

178178
Options of *memcpy*
179179
^^^^^^^^^^^^^^^^^^^
180-
-l::
180+
-s::
181181
--size::
182182
Specify size of memory to copy (default: 1MB).
183183
Available units are B, KB, MB, GB and TB (case insensitive).
184184

185+
-p::
186+
--page::
187+
Specify page-size for mapping memory buffers (default: 4KB).
188+
Available values are 4KB, 2MB, 1GB (case insensitive).
189+
185190
-f::
186191
--function::
187192
Specify function to copy (default: default).
@@ -201,11 +206,16 @@ Suite for evaluating performance of simple memory set in various ways.
201206

202207
Options of *memset*
203208
^^^^^^^^^^^^^^^^^^^
204-
-l::
209+
-s::
205210
--size::
206211
Specify size of memory to set (default: 1MB).
207212
Available units are B, KB, MB, GB and TB (case insensitive).
208213

214+
-p::
215+
--page::
216+
Specify page-size for mapping memory buffers (default: 4KB).
217+
Available values are 4KB, 2MB, 1GB (case insensitive).
218+
209219
-f::
210220
--function::
211221
Specify function to set (default: default).

tools/perf/bench/mem-functions.c

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,17 @@
2525
#include <sys/mman.h>
2626
#include <errno.h>
2727
#include <linux/time64.h>
28+
#include <linux/log2.h>
2829

2930
#define K 1024
3031

32+
#define PAGE_SHIFT_4KB 12
33+
#define PAGE_SHIFT_2MB 21
34+
#define PAGE_SHIFT_1GB 30
35+
3136
static const char *size_str = "1MB";
3237
static const char *function_str = "all";
38+
static const char *page_size_str = "4KB";
3339
static unsigned int nr_loops = 1;
3440
static bool use_cycles;
3541
static int cycles_fd;
@@ -39,6 +45,10 @@ static const struct option options[] = {
3945
"Specify the size of the memory buffers. "
4046
"Available units: B, KB, MB, GB and TB (case insensitive)"),
4147

48+
OPT_STRING('p', "page", &page_size_str, "4KB",
49+
"Specify page-size for mapping memory buffers. "
50+
"Available sizes: 4KB, 2MB, 1GB (case insensitive)"),
51+
4252
OPT_STRING('f', "function", &function_str, "all",
4353
"Specify the function to run, \"all\" runs all available functions, \"help\" lists them"),
4454

@@ -60,6 +70,7 @@ struct bench_params {
6070
size_t size;
6171
size_t size_total;
6272
unsigned int nr_loops;
73+
unsigned int page_shift;
6374
};
6475

6576
struct bench_mem_info {
@@ -202,14 +213,16 @@ static void __bench_mem_function(struct bench_mem_info *info, struct bench_param
202213
if (r->fn.fini) r->fn.fini(info, p, &src, &dst);
203214
return;
204215
out_init_failed:
205-
printf("# Memory allocation failed - maybe size (%s) is too large?\n", size_str);
216+
printf("# Memory allocation failed - maybe size (%s) %s?\n", size_str,
217+
p->page_shift != PAGE_SHIFT_4KB ? "has insufficient hugepages" : "is too large");
206218
goto out_free;
207219
}
208220

209221
static int bench_mem_common(int argc, const char **argv, struct bench_mem_info *info)
210222
{
211223
int i;
212224
struct bench_params p = { 0 };
225+
unsigned int page_size;
213226

214227
argc = parse_options(argc, argv, options, info->usage, 0);
215228

@@ -230,6 +243,15 @@ static int bench_mem_common(int argc, const char **argv, struct bench_mem_info *
230243
}
231244
p.size_total = p.size * p.nr_loops;
232245

246+
page_size = (unsigned int)perf_atoll((char *)page_size_str);
247+
if (page_size != (1 << PAGE_SHIFT_4KB) &&
248+
page_size != (1 << PAGE_SHIFT_2MB) &&
249+
page_size != (1 << PAGE_SHIFT_1GB)) {
250+
fprintf(stderr, "Invalid page-size:%s\n", page_size_str);
251+
return 1;
252+
}
253+
p.page_shift = ilog2(page_size);
254+
233255
if (!strncmp(function_str, "all", 3)) {
234256
for (i = 0; info->functions[i].name; i++)
235257
__bench_mem_function(info, &p, i);
@@ -286,11 +308,14 @@ static int do_memcpy(const struct function *r, struct bench_params *p,
286308
return 0;
287309
}
288310

289-
static void *bench_mmap(size_t size, bool populate)
311+
static void *bench_mmap(size_t size, bool populate, unsigned int page_shift)
290312
{
291313
void *p;
292314
int extra = populate ? MAP_POPULATE : 0;
293315

316+
if (page_shift != PAGE_SHIFT_4KB)
317+
extra |= MAP_HUGETLB | (page_shift << MAP_HUGE_SHIFT);
318+
294319
p = mmap(NULL, size, PROT_READ|PROT_WRITE,
295320
extra | MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
296321

@@ -308,11 +333,11 @@ static bool mem_alloc(struct bench_mem_info *info, struct bench_params *p,
308333
{
309334
bool failed;
310335

311-
*dst = bench_mmap(p->size, true);
336+
*dst = bench_mmap(p->size, true, p->page_shift);
312337
failed = *dst == NULL;
313338

314339
if (info->alloc_src) {
315-
*src = bench_mmap(p->size, true);
340+
*src = bench_mmap(p->size, true, p->page_shift);
316341
failed = failed || *src == NULL;
317342
}
318343

0 commit comments

Comments
 (0)