Skip to content

Commit

Permalink
stress-mremap: add --mremap-numa option
Browse files Browse the repository at this point in the history
Add option to try to bind mmap'd memory across NUMA nodes for
more page stressing

Signed-off-by: Colin Ian King <colin.i.king@gmail.com>
  • Loading branch information
ColinIanKing committed Jan 22, 2025
1 parent ab98d3d commit 0a8301b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 2 deletions.
1 change: 1 addition & 0 deletions core-opts.c
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,7 @@ const struct option stress_long_options[] = {
{ "mremap", 1, 0, OPT_mremap },
{ "mremap-bytes", 1, 0, OPT_mremap_bytes },
{ "mremap-mlock", 0, 0, OPT_mremap_mlock },
{ "mremap-numa", 0, 0, OPT_mremap_numa },
{ "mremap-ops", 1, 0, OPT_mremap_ops },
{ "mseal", 1, 0, OPT_mseal },
{ "mseal-ops", 1, 0, OPT_mseal_ops,},
Expand Down
1 change: 1 addition & 0 deletions core-opts.h
Original file line number Diff line number Diff line change
Expand Up @@ -943,6 +943,7 @@ typedef enum {
OPT_mremap_ops,
OPT_mremap_bytes,
OPT_mremap_mlock,
OPT_mremap_numa,

OPT_mseal,
OPT_mseal_ops,
Expand Down
45 changes: 44 additions & 1 deletion stress-mremap.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "core-madvise.h"
#include "core-mincore.h"
#include "core-mmap.h"
#include "core-numa.h"
#include "core-out-of-memory.h"

#define DEFAULT_MREMAP_BYTES (256 * MB)
Expand All @@ -31,13 +32,15 @@ static const stress_help_t help[] = {
{ NULL, "mremap N", "start N workers stressing mremap" },
{ NULL, "mremap-bytes N", "mremap N bytes maximum for each stress iteration" },
{ NULL, "mremap-mlock", "mlock remap pages, force pages to be unswappable" },
{ NULL, "mremap-numa", "bind memory mappings to randomly selected NUMA nodes" },
{ NULL, "mremap-ops N", "stop after N mremap bogo operations" },
{ NULL, NULL, NULL }
};

static const stress_opt_t opts[] = {
{ OPT_mremap_bytes, "mremap-bytes", TYPE_ID_SIZE_T_BYTES_VM, MIN_MREMAP_BYTES, MAX_MREMAP_BYTES, NULL },
{ OPT_mremap_mlock, "mremap-mlock", TYPE_ID_BOOL, 0, 1, NULL },
{ OPT_mremap_numa, "mremap-numa", TYPE_ID_BOOL, 0, 1, NULL },
END_OPT,
};

Expand Down Expand Up @@ -107,7 +110,6 @@ static int try_remap(
for (retry = 0; retry < 100; retry++) {
double t = 0.0;


#if defined(MREMAP_FIXED)
void *addr = rand_mremap_addr(new_sz + args->page_size, flags);
#endif
Expand Down Expand Up @@ -199,8 +201,12 @@ static int stress_mremap_child(stress_args_t *args, void *context)
int flags = MAP_PRIVATE | MAP_ANONYMOUS;
const size_t page_size = args->page_size;
bool mremap_mlock = false;
bool mremap_numa = false;
double duration = 0.0, count = 0.0, rate;
int ret = EXIT_SUCCESS;
#if defined(HAVE_LINUX_MEMPOLICY_H)
stress_numa_mask_t *numa_mask = NULL;
#endif

#if defined(MAP_POPULATE)
flags |= MAP_POPULATE;
Expand All @@ -221,6 +227,26 @@ static int stress_mremap_child(stress_args_t *args, void *context)
new_sz = sz = mremap_bytes & ~(page_size - 1);

(void)stress_get_setting("mremap-mlock", &mremap_mlock);
(void)stress_get_setting("mremap-numa", &mremap_numa);

if (mremap_numa) {
#if defined(HAVE_LINUX_MEMPOLICY_H)
if (stress_numa_nodes() > 1) {
numa_mask = stress_numa_mask_alloc();
} else {
if (args->instance == 0) {
pr_inf("%s: only 1 NUMA node available, disabling --mremap-numa\n",
args->name);
mremap_numa = false;
}
}
#else
if (args->instance == 0)
pr_inf("%s: --mremap-numa selected but not supported by this system, disabling option\n",
args->name);
mremap_numa = false;
#endif
}

stress_set_proc_state(args->name, STRESS_STATE_SYNC_WAIT);
stress_sync_start_wait(args);
Expand All @@ -241,6 +267,10 @@ static int stress_mremap_child(stress_args_t *args, void *context)
#endif
continue; /* Try again */
}
#if defined(HAVE_LINUX_MEMPOLICY_H)
if (mremap_numa)
stress_numa_randomize_pages(numa_mask, buf, page_size, sz);
#endif
(void)stress_madvise_random(buf, new_sz);
(void)stress_madvise_mergeable(buf, new_sz);
(void)stress_mincore_touch_pages(buf, mremap_bytes);
Expand Down Expand Up @@ -268,6 +298,10 @@ static int stress_mremap_child(stress_args_t *args, void *context)
}
if (!stress_continue(args))
goto deinit;
#if defined(HAVE_LINUX_MEMPOLICY_H)
if (mremap_numa)
stress_numa_randomize_pages(numa_mask, buf, page_size, new_sz);
#endif
(void)stress_madvise_random(buf, new_sz);
if (g_opt_flags & OPT_FLAGS_VERIFY) {
if (stress_mmap_check(buf, new_sz, page_size) < 0) {
Expand All @@ -293,6 +327,10 @@ static int stress_mremap_child(stress_args_t *args, void *context)
}
if (!stress_continue(args))
goto deinit;
#if defined(HAVE_LINUX_MEMPOLICY_H)
if (mremap_numa)
stress_numa_randomize_pages(numa_mask, buf, page_size, new_sz);
#endif
(void)stress_madvise_random(buf, new_sz);
old_sz = new_sz;
new_sz <<= 1;
Expand Down Expand Up @@ -323,6 +361,11 @@ static int stress_mremap_child(stress_args_t *args, void *context)
stress_metrics_set(args, 0, "nanosecs per mremap call",
rate * STRESS_DBL_NANOSECOND, STRESS_METRIC_HARMONIC_MEAN);

#if defined(HAVE_LINUX_MEMPOLICY_H)
if (mremap_numa)
stress_numa_mask_free(numa_mask);
#endif

return ret;
}

Expand Down
6 changes: 5 additions & 1 deletion stress-ng.1
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
.\" First parameter, NAME, should be all caps
.\" Second parameter, SECTION, should be 1-8, maybe w/ subsection
.\" other parameters are allowed: see man(7), man(1)
.TH STRESS-NG 1 "21 January 2025"
.TH STRESS-NG 1 "22 January 2025"
.\" Please adjust this date whenever revising the manpage.
.\"
.\" Some roff macros, for reference:
Expand Down Expand Up @@ -5476,6 +5476,10 @@ suffix b, k, m or g.
attempt to mlock remap'd pages into memory causing more memory pressure by
preventing pages from swapped out.
.TP
.B \-\-mreamp\-numa
assign memory mapped pages to randomly selected NUMA nodes. This is disabled
for systems that do not support NUMA or have less than 2 NUMA nodes.
.TP
.B \-\-mremap\-ops N
stop mremap stress workers after N bogo operations.
.RE
Expand Down

0 comments on commit 0a8301b

Please sign in to comment.