[PATCH 1/4] perf bench mem: Report why mmap() failed
From: Pierre-Clément Tosi
Date: Tue Dec 01 2026 - 07:12:43 EST
When mapping a buffer fails, 'perf bench mem' prints
# Memory allocation failed - maybe size (1MB) is too large?
whatever the error, and the mmap benchmark records -ENOMEM for any
failure. Errors such as EINVAL or EACCES are then blamed on the size of
the buffers or on the number of hugepages.
Print the error returned by mmap(), and keep the hint about the size
and hugepages for ENOMEM. When several threads fail, report the error
of the first one instead of ORing negative error codes together.
Signed-off-by: Pierre-Clément Tosi <ptosi@xxxxxxxxxx>
---
tools/perf/bench/mem-functions.c | 38 +++++++++++++++++++++++++-------------
1 file changed, 25 insertions(+), 13 deletions(-)
diff --git a/tools/perf/bench/mem-functions.c b/tools/perf/bench/mem-functions.c
index 5ede52853953..412281b03c75 100644
--- a/tools/perf/bench/mem-functions.c
+++ b/tools/perf/bench/mem-functions.c
@@ -193,6 +193,19 @@ static double timeval2double(struct timeval *ts)
printf(" %14lf GB/sec", x / K / K / K); \
} while (0)
+static void print_mmap_error(const struct bench_params *p, int err)
+{
+ char sbuf[STRERR_BUFSIZE];
+
+ printf("# Memory allocation failed: %s", str_error_r(err, sbuf, sizeof(sbuf)));
+ if (err == ENOMEM) {
+ printf(" - maybe size (%s) %s?", size_str,
+ p->page_shift != PAGE_SHIFT_4KB ?
+ "has insufficient hugepages" : "is too large");
+ }
+ printf("\n");
+}
+
static void __bench_mem_function(struct bench_mem_info *info, struct bench_params *p,
int r_idx)
{
@@ -247,8 +260,7 @@ static void __bench_mem_function(struct bench_mem_info *info, struct bench_param
if (r->fn.fini) r->fn.fini(info, p, &src, &dst);
return;
out_init_failed:
- printf("# Memory allocation failed - maybe size (%s) %s?\n", size_str,
- p->page_shift != PAGE_SHIFT_4KB ? "has insufficient hugepages" : "is too large");
+ print_mmap_error(p, errno);
goto out_free;
}
@@ -373,20 +385,21 @@ static void bench_munmap(void *p, size_t size)
munmap(p, size);
}
+/* Return true on failure, with errno set. */
static bool mem_alloc(struct bench_mem_info *info, struct bench_params *p,
void **src, void **dst)
{
- bool failed;
-
*dst = bench_mmap(p->size, true, p->page_shift);
- failed = *dst == NULL;
+ if (!*dst)
+ return true;
if (info->alloc_src) {
*src = bench_mmap(p->size, true, p->page_shift);
- failed = failed || *src == NULL;
+ if (!*src)
+ return true;
}
- return failed;
+ return false;
}
static void mem_free(struct bench_mem_info *info __maybe_unused,
@@ -543,7 +556,7 @@ static void *do_mmap_thread(void *arg)
return data;
out:
- data->error = -ENOMEM;
+ data->error = -errno;
return NULL;
}
@@ -580,14 +593,13 @@ static int do_mmap(const struct function *r, struct bench_params *p,
update_stats(&stats, t->cycles);
else
update_stats(&stats, t->tv.tv_sec * 1e6 + t->tv.tv_usec);
- error |= data[i].error;
+ if (!error)
+ error = data[i].error;
}
free(data);
- if (error) {
- printf("# Memory allocation failed - maybe size (%s) %s?\n", size_str,
- p->page_shift != PAGE_SHIFT_4KB ? "has insufficient hugepages" : "is too large");
- }
+ if (error)
+ print_mmap_error(p, -error);
return error ? -1 : 0;
}
--
2.56.1.839.g22a8290db9-goog