diff options
Diffstat (limited to 'tools/testing/memblock/tests/common.c')
-rw-r--r-- | tools/testing/memblock/tests/common.c | 87 |
1 files changed, 74 insertions, 13 deletions
diff --git a/tools/testing/memblock/tests/common.c b/tools/testing/memblock/tests/common.c index ebc06b4c3255..e43b2676af81 100644 --- a/tools/testing/memblock/tests/common.c +++ b/tools/testing/memblock/tests/common.c @@ -1,6 +1,9 @@ // SPDX-License-Identifier: GPL-2.0-or-later #include "tests/common.h" #include <string.h> +#include <getopt.h> +#include <linux/memory_hotplug.h> +#include <linux/build_bug.h> #define INIT_MEMBLOCK_REGIONS 128 #define INIT_MEMBLOCK_RESERVED_REGIONS INIT_MEMBLOCK_REGIONS @@ -11,6 +14,27 @@ static struct test_memory memory_block; static const char __maybe_unused *prefixes[PREFIXES_MAX]; static int __maybe_unused nr_prefixes; +static const char *short_opts = "mv"; +static const struct option long_opts[] = { + {"movable-node", 0, NULL, 'm'}, + {"verbose", 0, NULL, 'v'}, + {NULL, 0, NULL, 0} +}; + +static const char * const help_opts[] = { + "disallow allocations from regions marked as hotplugged\n\t\t\t" + "by simulating enabling the \"movable_node\" kernel\n\t\t\t" + "parameter", + "enable verbose output, which includes the name of the\n\t\t\t" + "memblock function being tested, the name of the test,\n\t\t\t" + "and whether the test passed or failed." +}; + +static int verbose; + +/* sets global variable returned by movable_node_is_enabled() stub */ +bool movable_node_enabled; + void reset_memblock_regions(void) { memset(memblock.memory.regions, 0, @@ -51,7 +75,39 @@ void dummy_physical_memory_cleanup(void) free(memory_block.base); } -#ifdef VERBOSE +static void usage(const char *prog) +{ + BUILD_BUG_ON(ARRAY_SIZE(help_opts) != ARRAY_SIZE(long_opts) - 1); + + printf("Usage: %s [-%s]\n", prog, short_opts); + + for (int i = 0; long_opts[i].name; i++) { + printf(" -%c, --%-12s\t%s\n", long_opts[i].val, + long_opts[i].name, help_opts[i]); + } + + exit(1); +} + +void parse_args(int argc, char **argv) +{ + int c; + + while ((c = getopt_long_only(argc, argv, short_opts, long_opts, + NULL)) != -1) { + switch (c) { + case 'm': + movable_node_enabled = true; + break; + case 'v': + verbose = 1; + break; + default: + usage(argv[0]); + } + } +} + void print_prefixes(const char *postfix) { for (int i = 0; i < nr_prefixes; i++) @@ -61,25 +117,31 @@ void print_prefixes(const char *postfix) void test_fail(void) { - ksft_test_result_fail(": "); - print_prefixes("failed\n"); + if (verbose) { + ksft_test_result_fail(": "); + print_prefixes("failed\n"); + } } void test_pass(void) { - ksft_test_result_pass(": "); - print_prefixes("passed\n"); + if (verbose) { + ksft_test_result_pass(": "); + print_prefixes("passed\n"); + } } void test_print(const char *fmt, ...) { - int saved_errno = errno; - va_list args; - - va_start(args, fmt); - errno = saved_errno; - vprintf(fmt, args); - va_end(args); + if (verbose) { + int saved_errno = errno; + va_list args; + + va_start(args, fmt); + errno = saved_errno; + vprintf(fmt, args); + va_end(args); + } } void prefix_reset(void) @@ -102,4 +164,3 @@ void prefix_pop(void) nr_prefixes--; } } -#endif /* VERBOSE */ |