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