• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
38 static size_t page_size;
39 static struct tst_nodemap *nodes;
40 
setup(void)41 static void setup(void)
42 {
43 	page_size = getpagesize();
44 
45 	nodes = tst_get_nodemap(TST_NUMA_MEM, 20 * FILES * page_size / 1024);
46 	if (nodes->cnt <= 1)
47 		tst_brk(TCONF, "Test requires at least two NUMA memory nodes");
48 }
49 
cleanup(void)50 static void cleanup(void)
51 {
52 	tst_nodemap_free(nodes);
53 }
54 
alloc_and_check(void)55 static void alloc_and_check(void)
56 {
57 	unsigned int i, j;
58 	char path[1024];
59 	unsigned int total_pages = 0;
60 	unsigned int sum_pages = 0;
61 
62 	tst_nodemap_reset_counters(nodes);
63 
64 	/*
65 	 * The inner loop loops node->cnt times to ensure the sum could
66 	 * be evenly distributed among the nodes.
67 	 */
68 	for (i = 1; i <= FILES; i++) {
69 		for (j = 1; j <= nodes->cnt; j++) {
70 			size_t size = 10 * i + j % 10;
71 			snprintf(path, sizeof(path), MNTPOINT "/numa-test-file-%i-%i", i, j);
72 			alloc_fault_count(nodes, path, size * page_size);
73 			total_pages += size;
74 		}
75 	}
76 
77 	for (i = 0; i < nodes->cnt; i++) {
78 		float treshold = 1.00 * total_pages / 60; /* five percents */
79 		float min_pages = 1.00 * total_pages / nodes->cnt - treshold;
80 		float max_pages = 1.00 * total_pages / nodes->cnt + treshold;
81 
82 		if (nodes->counters[i] > min_pages && nodes->counters[i] < max_pages) {
83 			tst_res(TPASS, "Node %u allocated %u <%.2f,%.2f>",
84 			        nodes->map[i], nodes->counters[i], min_pages, max_pages);
85 		} else {
86 			tst_res(TFAIL, "Node %u allocated %u, expected <%.2f,%.2f>",
87 			        nodes->map[i], nodes->counters[i], min_pages, max_pages);
88 		}
89 
90 		sum_pages += nodes->counters[i];
91 	}
92 
93 	if (sum_pages != total_pages) {
94 		tst_res(TFAIL, "Sum of nodes %u != allocated pages %u",
95 		        sum_pages, total_pages);
96 		return;
97 	}
98 
99 	tst_res(TPASS, "Sum of nodes equals to allocated pages (%u)", total_pages);
100 }
101 
verify_set_mempolicy(void)102 static void verify_set_mempolicy(void)
103 {
104 	struct bitmask *bm = numa_allocate_nodemask();
105 	unsigned int alloc_on_nodes = nodes->cnt;
106 	unsigned int i;
107 
108 	for (i = 0; i < alloc_on_nodes; i++)
109 		numa_bitmask_setbit(bm, nodes->map[i]);
110 
111 	TEST(set_mempolicy(MPOL_INTERLEAVE, bm->maskp, bm->size+1));
112 
113 	if (TST_RET) {
114 		tst_res(TFAIL | TTERRNO,
115 		        "set_mempolicy(MPOL_INTERLEAVE)");
116 		return;
117 	}
118 
119 	tst_res(TPASS, "set_mempolicy(MPOL_INTERLEAVE)");
120 
121 	alloc_and_check();
122 
123 	numa_free_nodemask(bm);
124 }
125 
126 static struct tst_test test = {
127 	.setup = setup,
128 	.cleanup = cleanup,
129 	.test_all = verify_set_mempolicy,
130 	.forks_child = 1,
131 	.needs_root = 1,
132 	.all_filesystems = 1,
133 	.mntpoint = MNTPOINT,
134 	.needs_checkpoints = 1,
135 };
136 
137 #else
138 
139 TST_TEST_TCONF(NUMA_ERROR_MSG);
140 
141 #endif /* HAVE_NUMA_V2 */
142