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.
9 *
10 * The test tries different subsets of memory nodes, sets the mask with
11 * memopolicy, and checks that the memory was interleaved between the nodes
12 * accordingly.
13 */
14
15 #include <errno.h>
16 #include "config.h"
17 #ifdef HAVE_NUMA_V2
18 # include <numa.h>
19 # include <numaif.h>
20 #endif
21 #include "tst_test.h"
22 #include "tst_numa.h"
23
24 #ifdef HAVE_NUMA_V2
25
26 #include "set_mempolicy.h"
27
28 #define ALLOC_ON_NODE 8
29
30 static size_t page_size;
31 static struct tst_nodemap *nodes;
32
setup(void)33 static void setup(void)
34 {
35 page_size = getpagesize();
36
37 nodes = tst_get_nodemap(TST_NUMA_MEM, 2 * ALLOC_ON_NODE * page_size / 1024);
38 if (nodes->cnt <= 1)
39 tst_brk(TCONF, "Test requires at least two NUMA memory nodes");
40 }
41
cleanup(void)42 static void cleanup(void)
43 {
44 tst_nodemap_free(nodes);
45 }
46
alloc_and_check(size_t size,unsigned int * exp_alloc)47 static void alloc_and_check(size_t size, unsigned int *exp_alloc)
48 {
49 unsigned int i;
50 const char *prefix = "child: ";
51
52 if (SAFE_FORK()) {
53 prefix = "parent: ";
54 tst_reap_children();
55 }
56
57 tst_nodemap_reset_counters(nodes);
58 alloc_fault_count(nodes, NULL, size * page_size);
59
60 for (i = 0; i < nodes->cnt; i++) {
61 if (nodes->counters[i] == exp_alloc[i]) {
62 tst_res(TPASS, "%sNode %u allocated %u",
63 prefix, nodes->map[i], exp_alloc[i]);
64 } else {
65 tst_res(TFAIL, "%sNode %u allocated %u, expected %u",
66 prefix, nodes->map[i], nodes->counters[i],
67 exp_alloc[i]);
68 }
69 }
70 }
71
verify_set_mempolicy(unsigned int n)72 static void verify_set_mempolicy(unsigned int n)
73 {
74 struct bitmask *bm = numa_allocate_nodemask();
75 unsigned int exp_alloc[nodes->cnt];
76 unsigned int alloc_per_node = n ? ALLOC_ON_NODE : 2;
77 unsigned int alloc_on_nodes = n ? 2 : nodes->cnt;
78 unsigned int alloc_total = alloc_per_node * alloc_on_nodes;
79 unsigned int i;
80
81 memset(exp_alloc, 0, sizeof(exp_alloc));
82
83 for (i = 0; i < alloc_on_nodes; i++) {
84 exp_alloc[i] = alloc_per_node;
85 numa_bitmask_setbit(bm, nodes->map[i]);
86 }
87
88 TEST(set_mempolicy(MPOL_INTERLEAVE, bm->maskp, bm->size+1));
89
90 tst_res(TINFO, "Allocating on nodes 1-%u - %u pages",
91 alloc_on_nodes, alloc_total);
92
93 if (TST_RET) {
94 tst_res(TFAIL | TTERRNO,
95 "set_mempolicy(MPOL_INTERLEAVE)");
96 return;
97 }
98
99 tst_res(TPASS, "set_mempolicy(MPOL_INTERLEAVE)");
100
101 numa_free_nodemask(bm);
102
103 alloc_and_check(alloc_total, exp_alloc);
104 }
105
106 static struct tst_test test = {
107 .setup = setup,
108 .cleanup = cleanup,
109 .test = verify_set_mempolicy,
110 .tcnt = 2,
111 .forks_child = 1,
112 .needs_checkpoints = 1,
113 };
114
115 #else
116
117 TST_TEST_TCONF(NUMA_ERROR_MSG);
118
119 #endif /* HAVE_NUMA_V2 */
120