1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (c) 2018 Cyril Hrubis <chrubis@suse.cz>
4 */
5
6/*
7 * We are testing set_mempolicy() with MPOL_INTERLEAVE on mmaped buffers backed
8 * by files.
9 *
10 * Apparently it takes a larger sample for the allocations to be correctly
11 * interleaved. The reason for this is that buffers for file metadata are
12 * allocated in batches in order not to loose performance. Also the pages
13 * cannot be interleaved completely evenly unless the number of pages is
14 * divideable by the number of nodes, which will not happen even if we tried
15 * hard since we do not have controll over metadata blocks for instance. Hence
16 * we cannot really expect to allocate a single file and have the memory
17 * interleaved precisely but it works well if we allocate statistic for a more
18 * than a few files.
19 */
20
21#include <stdio.h>
22#include <errno.h>
23#include "config.h"
24#ifdef HAVE_NUMA_V2
25# include <numa.h>
26# include <numaif.h>
27#endif
28#include "tst_test.h"
29#include "tst_numa.h"
30
31#define MNTPOINT "mntpoint"
32#define FILES 10
33
34#ifdef HAVE_NUMA_V2
35
36#include "set_mempolicy.h"
37
38static size_t page_size;
39static struct tst_nodemap *nodes;
40
41static void setup(void)
42{
43	int node_min_pages = FILES * (FILES + 1) / 2 * 10 + FILES * 10;
44
45	page_size = getpagesize();
46
47	nodes = tst_get_nodemap(TST_NUMA_MEM, node_min_pages * page_size / 1024);
48	if (nodes->cnt <= 1)
49		tst_brk(TCONF, "Test requires at least two NUMA memory nodes");
50}
51
52static void cleanup(void)
53{
54	tst_nodemap_free(nodes);
55}
56
57static void alloc_and_check(void)
58{
59	unsigned int i, j;
60	char path[1024];
61	unsigned int total_pages = 0;
62	unsigned int sum_pages = 0;
63
64	tst_nodemap_reset_counters(nodes);
65
66	/*
67	 * The inner loop loops node->cnt times to ensure the sum could
68	 * be evenly distributed among the nodes.
69	 */
70	for (i = 1; i <= FILES; i++) {
71		for (j = 1; j <= nodes->cnt; j++) {
72			size_t size = 10 * i + j % 10;
73			snprintf(path, sizeof(path), MNTPOINT "/numa-test-file-%i-%i", i, j);
74			alloc_fault_count(nodes, path, size * page_size);
75			total_pages += size;
76		}
77	}
78
79	for (i = 0; i < nodes->cnt; i++) {
80		float treshold = 1.00 * total_pages / 60; /* five percents */
81		float min_pages = 1.00 * total_pages / nodes->cnt - treshold;
82		float max_pages = 1.00 * total_pages / nodes->cnt + treshold;
83
84		if (nodes->counters[i] > min_pages && nodes->counters[i] < max_pages) {
85			tst_res(TPASS, "Node %u allocated %u <%.2f,%.2f>",
86			        nodes->map[i], nodes->counters[i], min_pages, max_pages);
87		} else {
88			tst_res(TFAIL, "Node %u allocated %u, expected <%.2f,%.2f>",
89			        nodes->map[i], nodes->counters[i], min_pages, max_pages);
90		}
91
92		sum_pages += nodes->counters[i];
93	}
94
95	if (sum_pages != total_pages) {
96		tst_res(TFAIL, "Sum of nodes %u != allocated pages %u",
97		        sum_pages, total_pages);
98		return;
99	}
100
101	tst_res(TPASS, "Sum of nodes equals to allocated pages (%u)", total_pages);
102}
103
104static void verify_set_mempolicy(void)
105{
106	struct bitmask *bm = numa_allocate_nodemask();
107	unsigned int alloc_on_nodes = nodes->cnt;
108	unsigned int i;
109
110	for (i = 0; i < alloc_on_nodes; i++)
111		numa_bitmask_setbit(bm, nodes->map[i]);
112
113	TEST(set_mempolicy(MPOL_INTERLEAVE, bm->maskp, bm->size+1));
114
115	if (TST_RET) {
116		tst_res(TFAIL | TTERRNO,
117		        "set_mempolicy(MPOL_INTERLEAVE)");
118		return;
119	}
120
121	tst_res(TPASS, "set_mempolicy(MPOL_INTERLEAVE)");
122
123	alloc_and_check();
124
125	numa_free_nodemask(bm);
126}
127
128static struct tst_test test = {
129	.setup = setup,
130	.cleanup = cleanup,
131	.test_all = verify_set_mempolicy,
132	.forks_child = 1,
133	.needs_root = 1,
134	.all_filesystems = 1,
135	.mntpoint = MNTPOINT,
136	.needs_checkpoints = 1,
137};
138
139#else
140
141TST_TEST_TCONF(NUMA_ERROR_MSG);
142
143#endif /* HAVE_NUMA_V2 */
144